~ubuntu-branches/ubuntu/trusty/ginkgocadx/trusty

« back to all changes in this revision

Viewing changes to .pc/fix-spelling.patch/src/cadxcore/sqlite/sqlite3.c

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2013-07-21 11:58:53 UTC
  • mfrom: (7.2.1 sid)
  • Revision ID: package-import@ubuntu.com-20130721115853-44e7n1xujqglu78e
Tags: 3.4.0.928.29+dfsg-1
* New upstream release [July 2013]
  + new B-D: "libjsoncpp-dev".
  + new patch "unbundle-libjsoncpp.patch" to avoid building bundled
    "libjsoncpp-dev".
  + new patch "fix-wx.patch" to avoid FTBFS due to missing
    "-lwx_gtk2u_html-2.8".
* Removed unnecessary versioned Build-Depends.
* Removed obsolete lintian override.
* Reference get-orig-source implementation for orig.tar clean-up and
  DFSG-repackaging.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/******************************************************************************
2
 
** This file is an amalgamation of many separate C source files from SQLite
3
 
** version 3.7.6.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
 
#ifndef SQLITE_PRIVATE
23
 
# define SQLITE_PRIVATE static
24
 
#endif
25
 
#ifndef SQLITE_API
26
 
# define SQLITE_API
27
 
#endif
28
 
/************** Begin file sqliteInt.h ***************************************/
29
 
/*
30
 
** 2001 September 15
31
 
**
32
 
** The author disclaims copyright to this source code.  In place of
33
 
** a legal notice, here is a blessing:
34
 
**
35
 
**    May you do good and not evil.
36
 
**    May you find forgiveness for yourself and forgive others.
37
 
**    May you share freely, never taking more than you give.
38
 
**
39
 
*************************************************************************
40
 
** Internal interface definitions for SQLite.
41
 
**
42
 
*/
43
 
#ifndef _SQLITEINT_H_
44
 
#define _SQLITEINT_H_
45
 
 
46
 
/*
47
 
** These #defines should enable >2GB file support on POSIX if the
48
 
** underlying operating system supports it.  If the OS lacks
49
 
** large file support, or if the OS is windows, these should be no-ops.
50
 
**
51
 
** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
52
 
** system #includes.  Hence, this block of code must be the very first
53
 
** code in all source files.
54
 
**
55
 
** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
56
 
** on the compiler command line.  This is necessary if you are compiling
57
 
** on a recent machine (ex: Red Hat 7.2) but you want your code to work
58
 
** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
59
 
** without this option, LFS is enable.  But LFS does not exist in the kernel
60
 
** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
61
 
** portability you should omit LFS.
62
 
**
63
 
** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
64
 
*/
65
 
#ifndef SQLITE_DISABLE_LFS
66
 
# define _LARGE_FILE       1
67
 
# ifndef _FILE_OFFSET_BITS
68
 
#   define _FILE_OFFSET_BITS 64
69
 
# endif
70
 
# define _LARGEFILE_SOURCE 1
71
 
#endif
72
 
 
73
 
/*
74
 
** Include the configuration header output by 'configure' if we're using the
75
 
** autoconf-based build
76
 
*/
77
 
#ifdef _HAVE_SQLITE_CONFIG_H
78
 
#include "config.h"
79
 
#endif
80
 
 
81
 
/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
82
 
/************** Begin file sqliteLimit.h *************************************/
83
 
/*
84
 
** 2007 May 7
85
 
**
86
 
** The author disclaims copyright to this source code.  In place of
87
 
** a legal notice, here is a blessing:
88
 
**
89
 
**    May you do good and not evil.
90
 
**    May you find forgiveness for yourself and forgive others.
91
 
**    May you share freely, never taking more than you give.
92
 
**
93
 
*************************************************************************
94
 
** 
95
 
** This file defines various limits of what SQLite can process.
96
 
*/
97
 
 
98
 
/*
99
 
** The maximum length of a TEXT or BLOB in bytes.   This also
100
 
** limits the size of a row in a table or index.
101
 
**
102
 
** The hard limit is the ability of a 32-bit signed integer
103
 
** to count the size: 2^31-1 or 2147483647.
104
 
*/
105
 
#ifndef SQLITE_MAX_LENGTH
106
 
# define SQLITE_MAX_LENGTH 1000000000
107
 
#endif
108
 
 
109
 
/*
110
 
** This is the maximum number of
111
 
**
112
 
**    * Columns in a table
113
 
**    * Columns in an index
114
 
**    * Columns in a view
115
 
**    * Terms in the SET clause of an UPDATE statement
116
 
**    * Terms in the result set of a SELECT statement
117
 
**    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
118
 
**    * Terms in the VALUES clause of an INSERT statement
119
 
**
120
 
** The hard upper limit here is 32676.  Most database people will
121
 
** tell you that in a well-normalized database, you usually should
122
 
** not have more than a dozen or so columns in any table.  And if
123
 
** that is the case, there is no point in having more than a few
124
 
** dozen values in any of the other situations described above.
125
 
*/
126
 
#ifndef SQLITE_MAX_COLUMN
127
 
# define SQLITE_MAX_COLUMN 2000
128
 
#endif
129
 
 
130
 
/*
131
 
** The maximum length of a single SQL statement in bytes.
132
 
**
133
 
** It used to be the case that setting this value to zero would
134
 
** turn the limit off.  That is no longer true.  It is not possible
135
 
** to turn this limit off.
136
 
*/
137
 
#ifndef SQLITE_MAX_SQL_LENGTH
138
 
# define SQLITE_MAX_SQL_LENGTH 1000000000
139
 
#endif
140
 
 
141
 
/*
142
 
** The maximum depth of an expression tree. This is limited to 
143
 
** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might 
144
 
** want to place more severe limits on the complexity of an 
145
 
** expression.
146
 
**
147
 
** A value of 0 used to mean that the limit was not enforced.
148
 
** But that is no longer true.  The limit is now strictly enforced
149
 
** at all times.
150
 
*/
151
 
#ifndef SQLITE_MAX_EXPR_DEPTH
152
 
# define SQLITE_MAX_EXPR_DEPTH 1000
153
 
#endif
154
 
 
155
 
/*
156
 
** The maximum number of terms in a compound SELECT statement.
157
 
** The code generator for compound SELECT statements does one
158
 
** level of recursion for each term.  A stack overflow can result
159
 
** if the number of terms is too large.  In practice, most SQL
160
 
** never has more than 3 or 4 terms.  Use a value of 0 to disable
161
 
** any limit on the number of terms in a compount SELECT.
162
 
*/
163
 
#ifndef SQLITE_MAX_COMPOUND_SELECT
164
 
# define SQLITE_MAX_COMPOUND_SELECT 500
165
 
#endif
166
 
 
167
 
/*
168
 
** The maximum number of opcodes in a VDBE program.
169
 
** Not currently enforced.
170
 
*/
171
 
#ifndef SQLITE_MAX_VDBE_OP
172
 
# define SQLITE_MAX_VDBE_OP 25000
173
 
#endif
174
 
 
175
 
/*
176
 
** The maximum number of arguments to an SQL function.
177
 
*/
178
 
#ifndef SQLITE_MAX_FUNCTION_ARG
179
 
# define SQLITE_MAX_FUNCTION_ARG 127
180
 
#endif
181
 
 
182
 
/*
183
 
** The maximum number of in-memory pages to use for the main database
184
 
** table and for temporary tables.  The SQLITE_DEFAULT_CACHE_SIZE
185
 
*/
186
 
#ifndef SQLITE_DEFAULT_CACHE_SIZE
187
 
# define SQLITE_DEFAULT_CACHE_SIZE  2000
188
 
#endif
189
 
#ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
190
 
# define SQLITE_DEFAULT_TEMP_CACHE_SIZE  500
191
 
#endif
192
 
 
193
 
/*
194
 
** The default number of frames to accumulate in the log file before
195
 
** checkpointing the database in WAL mode.
196
 
*/
197
 
#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
198
 
# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT  1000
199
 
#endif
200
 
 
201
 
/*
202
 
** The maximum number of attached databases.  This must be between 0
203
 
** and 62.  The upper bound on 62 is because a 64-bit integer bitmap
204
 
** is used internally to track attached databases.
205
 
*/
206
 
#ifndef SQLITE_MAX_ATTACHED
207
 
# define SQLITE_MAX_ATTACHED 10
208
 
#endif
209
 
 
210
 
 
211
 
/*
212
 
** The maximum value of a ?nnn wildcard that the parser will accept.
213
 
*/
214
 
#ifndef SQLITE_MAX_VARIABLE_NUMBER
215
 
# define SQLITE_MAX_VARIABLE_NUMBER 999
216
 
#endif
217
 
 
218
 
/* Maximum page size.  The upper bound on this value is 65536.  This a limit
219
 
** imposed by the use of 16-bit offsets within each page.
220
 
**
221
 
** Earlier versions of SQLite allowed the user to change this value at
222
 
** compile time. This is no longer permitted, on the grounds that it creates
223
 
** a library that is technically incompatible with an SQLite library 
224
 
** compiled with a different limit. If a process operating on a database 
225
 
** with a page-size of 65536 bytes crashes, then an instance of SQLite 
226
 
** compiled with the default page-size limit will not be able to rollback 
227
 
** the aborted transaction. This could lead to database corruption.
228
 
*/
229
 
#ifdef SQLITE_MAX_PAGE_SIZE
230
 
# undef SQLITE_MAX_PAGE_SIZE
231
 
#endif
232
 
#define SQLITE_MAX_PAGE_SIZE 65536
233
 
 
234
 
 
235
 
/*
236
 
** The default size of a database page.
237
 
*/
238
 
#ifndef SQLITE_DEFAULT_PAGE_SIZE
239
 
# define SQLITE_DEFAULT_PAGE_SIZE 1024
240
 
#endif
241
 
#if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
242
 
# undef SQLITE_DEFAULT_PAGE_SIZE
243
 
# define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
244
 
#endif
245
 
 
246
 
/*
247
 
** Ordinarily, if no value is explicitly provided, SQLite creates databases
248
 
** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
249
 
** device characteristics (sector-size and atomic write() support),
250
 
** SQLite may choose a larger value. This constant is the maximum value
251
 
** SQLite will choose on its own.
252
 
*/
253
 
#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
254
 
# define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
255
 
#endif
256
 
#if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
257
 
# undef SQLITE_MAX_DEFAULT_PAGE_SIZE
258
 
# define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
259
 
#endif
260
 
 
261
 
 
262
 
/*
263
 
** Maximum number of pages in one database file.
264
 
**
265
 
** This is really just the default value for the max_page_count pragma.
266
 
** This value can be lowered (or raised) at run-time using that the
267
 
** max_page_count macro.
268
 
*/
269
 
#ifndef SQLITE_MAX_PAGE_COUNT
270
 
# define SQLITE_MAX_PAGE_COUNT 1073741823
271
 
#endif
272
 
 
273
 
/*
274
 
** Maximum length (in bytes) of the pattern in a LIKE or GLOB
275
 
** operator.
276
 
*/
277
 
#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
278
 
# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
279
 
#endif
280
 
 
281
 
/*
282
 
** Maximum depth of recursion for triggers.
283
 
**
284
 
** A value of 1 means that a trigger program will not be able to itself
285
 
** fire any triggers. A value of 0 means that no trigger programs at all 
286
 
** may be executed.
287
 
*/
288
 
#ifndef SQLITE_MAX_TRIGGER_DEPTH
289
 
# define SQLITE_MAX_TRIGGER_DEPTH 1000
290
 
#endif
291
 
 
292
 
/************** End of sqliteLimit.h *****************************************/
293
 
/************** Continuing where we left off in sqliteInt.h ******************/
294
 
 
295
 
/* Disable nuisance warnings on Borland compilers */
296
 
#if defined(__BORLANDC__)
297
 
#pragma warn -rch /* unreachable code */
298
 
#pragma warn -ccc /* Condition is always true or false */
299
 
#pragma warn -aus /* Assigned value is never used */
300
 
#pragma warn -csu /* Comparing signed and unsigned */
301
 
#pragma warn -spa /* Suspicious pointer arithmetic */
302
 
#endif
303
 
 
304
 
/* Needed for various definitions... */
305
 
#ifndef _GNU_SOURCE
306
 
# define _GNU_SOURCE
307
 
#endif
308
 
 
309
 
/*
310
 
** Include standard header files as necessary
311
 
*/
312
 
#ifdef HAVE_STDINT_H
313
 
#include <stdint.h>
314
 
#endif
315
 
#ifdef HAVE_INTTYPES_H
316
 
#include <inttypes.h>
317
 
#endif
318
 
 
319
 
/*
320
 
** The number of samples of an index that SQLite takes in order to 
321
 
** construct a histogram of the table content when running ANALYZE
322
 
** and with SQLITE_ENABLE_STAT2
323
 
*/
324
 
#define SQLITE_INDEX_SAMPLES 10
325
 
 
326
 
/*
327
 
** The following macros are used to cast pointers to integers and
328
 
** integers to pointers.  The way you do this varies from one compiler
329
 
** to the next, so we have developed the following set of #if statements
330
 
** to generate appropriate macros for a wide range of compilers.
331
 
**
332
 
** The correct "ANSI" way to do this is to use the intptr_t type. 
333
 
** Unfortunately, that typedef is not available on all compilers, or
334
 
** if it is available, it requires an #include of specific headers
335
 
** that vary from one machine to the next.
336
 
**
337
 
** Ticket #3860:  The llvm-gcc-4.2 compiler from Apple chokes on
338
 
** the ((void*)&((char*)0)[X]) construct.  But MSVC chokes on ((void*)(X)).
339
 
** So we have to define the macros in different ways depending on the
340
 
** compiler.
341
 
*/
342
 
#if defined(__PTRDIFF_TYPE__)  /* This case should work for GCC */
343
 
# define SQLITE_INT_TO_PTR(X)  ((void*)(__PTRDIFF_TYPE__)(X))
344
 
# define SQLITE_PTR_TO_INT(X)  ((int)(__PTRDIFF_TYPE__)(X))
345
 
#elif !defined(__GNUC__)       /* Works for compilers other than LLVM */
346
 
# define SQLITE_INT_TO_PTR(X)  ((void*)&((char*)0)[X])
347
 
# define SQLITE_PTR_TO_INT(X)  ((int)(((char*)X)-(char*)0))
348
 
#elif defined(HAVE_STDINT_H)   /* Use this case if we have ANSI headers */
349
 
# define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
350
 
# define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
351
 
#else                          /* Generates a warning - but it always works */
352
 
# define SQLITE_INT_TO_PTR(X)  ((void*)(X))
353
 
# define SQLITE_PTR_TO_INT(X)  ((int)(X))
354
 
#endif
355
 
 
356
 
/*
357
 
** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
358
 
** 0 means mutexes are permanently disable and the library is never
359
 
** threadsafe.  1 means the library is serialized which is the highest
360
 
** level of threadsafety.  2 means the libary is multithreaded - multiple
361
 
** threads can use SQLite as long as no two threads try to use the same
362
 
** database connection at the same time.
363
 
**
364
 
** Older versions of SQLite used an optional THREADSAFE macro.
365
 
** We support that for legacy.
366
 
*/
367
 
#if !defined(SQLITE_THREADSAFE)
368
 
#if defined(THREADSAFE)
369
 
# define SQLITE_THREADSAFE THREADSAFE
370
 
#else
371
 
# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
372
 
#endif
373
 
#endif
374
 
 
375
 
/*
376
 
** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
377
 
** It determines whether or not the features related to 
378
 
** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
379
 
** be overridden at runtime using the sqlite3_config() API.
380
 
*/
381
 
#if !defined(SQLITE_DEFAULT_MEMSTATUS)
382
 
# define SQLITE_DEFAULT_MEMSTATUS 1
383
 
#endif
384
 
 
385
 
/*
386
 
** Exactly one of the following macros must be defined in order to
387
 
** specify which memory allocation subsystem to use.
388
 
**
389
 
**     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
390
 
**     SQLITE_MEMDEBUG               // Debugging version of system malloc()
391
 
**
392
 
** (Historical note:  There used to be several other options, but we've
393
 
** pared it down to just these two.)
394
 
**
395
 
** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
396
 
** the default.
397
 
*/
398
 
#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)>1
399
 
# error "At most one of the following compile-time configuration options\
400
 
 is allows: SQLITE_SYSTEM_MALLOC, SQLITE_MEMDEBUG"
401
 
#endif
402
 
#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)==0
403
 
# define SQLITE_SYSTEM_MALLOC 1
404
 
#endif
405
 
 
406
 
/*
407
 
** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
408
 
** sizes of memory allocations below this value where possible.
409
 
*/
410
 
#if !defined(SQLITE_MALLOC_SOFT_LIMIT)
411
 
# define SQLITE_MALLOC_SOFT_LIMIT 1024
412
 
#endif
413
 
 
414
 
/*
415
 
** We need to define _XOPEN_SOURCE as follows in order to enable
416
 
** recursive mutexes on most Unix systems.  But Mac OS X is different.
417
 
** The _XOPEN_SOURCE define causes problems for Mac OS X we are told,
418
 
** so it is omitted there.  See ticket #2673.
419
 
**
420
 
** Later we learn that _XOPEN_SOURCE is poorly or incorrectly
421
 
** implemented on some systems.  So we avoid defining it at all
422
 
** if it is already defined or if it is unneeded because we are
423
 
** not doing a threadsafe build.  Ticket #2681.
424
 
**
425
 
** See also ticket #2741.
426
 
*/
427
 
#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE
428
 
#  define _XOPEN_SOURCE 500  /* Needed to enable pthread recursive mutexes */
429
 
#endif
430
 
 
431
 
/*
432
 
** The TCL headers are only needed when compiling the TCL bindings.
433
 
*/
434
 
#if defined(SQLITE_TCL) || defined(TCLSH)
435
 
# include <tcl.h>
436
 
#endif
437
 
 
438
 
/*
439
 
** Many people are failing to set -DNDEBUG=1 when compiling SQLite.
440
 
** Setting NDEBUG makes the code smaller and run faster.  So the following
441
 
** lines are added to automatically set NDEBUG unless the -DSQLITE_DEBUG=1
442
 
** option is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
443
 
** feature.
444
 
*/
445
 
#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
446
 
# define NDEBUG 1
447
 
#endif
448
 
 
449
 
/*
450
 
** The testcase() macro is used to aid in coverage testing.  When 
451
 
** doing coverage testing, the condition inside the argument to
452
 
** testcase() must be evaluated both true and false in order to
453
 
** get full branch coverage.  The testcase() macro is inserted
454
 
** to help ensure adequate test coverage in places where simple
455
 
** condition/decision coverage is inadequate.  For example, testcase()
456
 
** can be used to make sure boundary values are tested.  For
457
 
** bitmask tests, testcase() can be used to make sure each bit
458
 
** is significant and used at least once.  On switch statements
459
 
** where multiple cases go to the same block of code, testcase()
460
 
** can insure that all cases are evaluated.
461
 
**
462
 
*/
463
 
#ifdef SQLITE_COVERAGE_TEST
464
 
SQLITE_PRIVATE   void sqlite3Coverage(int);
465
 
# define testcase(X)  if( X ){ sqlite3Coverage(__LINE__); }
466
 
#else
467
 
# define testcase(X)
468
 
#endif
469
 
 
470
 
/*
471
 
** The TESTONLY macro is used to enclose variable declarations or
472
 
** other bits of code that are needed to support the arguments
473
 
** within testcase() and assert() macros.
474
 
*/
475
 
#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
476
 
# define TESTONLY(X)  X
477
 
#else
478
 
# define TESTONLY(X)
479
 
#endif
480
 
 
481
 
/*
482
 
** Sometimes we need a small amount of code such as a variable initialization
483
 
** to setup for a later assert() statement.  We do not want this code to
484
 
** appear when assert() is disabled.  The following macro is therefore
485
 
** used to contain that setup code.  The "VVA" acronym stands for
486
 
** "Verification, Validation, and Accreditation".  In other words, the
487
 
** code within VVA_ONLY() will only run during verification processes.
488
 
*/
489
 
#ifndef NDEBUG
490
 
# define VVA_ONLY(X)  X
491
 
#else
492
 
# define VVA_ONLY(X)
493
 
#endif
494
 
 
495
 
/*
496
 
** The ALWAYS and NEVER macros surround boolean expressions which 
497
 
** are intended to always be true or false, respectively.  Such
498
 
** expressions could be omitted from the code completely.  But they
499
 
** are included in a few cases in order to enhance the resilience
500
 
** of SQLite to unexpected behavior - to make the code "self-healing"
501
 
** or "ductile" rather than being "brittle" and crashing at the first
502
 
** hint of unplanned behavior.
503
 
**
504
 
** In other words, ALWAYS and NEVER are added for defensive code.
505
 
**
506
 
** When doing coverage testing ALWAYS and NEVER are hard-coded to
507
 
** be true and false so that the unreachable code then specify will
508
 
** not be counted as untested code.
509
 
*/
510
 
#if defined(SQLITE_COVERAGE_TEST)
511
 
# define ALWAYS(X)      (1)
512
 
# define NEVER(X)       (0)
513
 
#elif !defined(NDEBUG)
514
 
# define ALWAYS(X)      ((X)?1:(assert(0),0))
515
 
# define NEVER(X)       ((X)?(assert(0),1):0)
516
 
#else
517
 
# define ALWAYS(X)      (X)
518
 
# define NEVER(X)       (X)
519
 
#endif
520
 
 
521
 
/*
522
 
** Return true (non-zero) if the input is a integer that is too large
523
 
** to fit in 32-bits.  This macro is used inside of various testcase()
524
 
** macros to verify that we have tested SQLite for large-file support.
525
 
*/
526
 
#define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
527
 
 
528
 
/*
529
 
** The macro unlikely() is a hint that surrounds a boolean
530
 
** expression that is usually false.  Macro likely() surrounds
531
 
** a boolean expression that is usually true.  GCC is able to
532
 
** use these hints to generate better code, sometimes.
533
 
*/
534
 
#if defined(__GNUC__) && 0
535
 
# define likely(X)    __builtin_expect((X),1)
536
 
# define unlikely(X)  __builtin_expect((X),0)
537
 
#else
538
 
# define likely(X)    !!(X)
539
 
# define unlikely(X)  !!(X)
540
 
#endif
541
 
 
542
 
/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
543
 
/************** Begin file sqlite3.h *****************************************/
544
 
/*
545
 
** 2001 September 15
546
 
**
547
 
** The author disclaims copyright to this source code.  In place of
548
 
** a legal notice, here is a blessing:
549
 
**
550
 
**    May you do good and not evil.
551
 
**    May you find forgiveness for yourself and forgive others.
552
 
**    May you share freely, never taking more than you give.
553
 
**
554
 
*************************************************************************
555
 
** This header file defines the interface that the SQLite library
556
 
** presents to client programs.  If a C-function, structure, datatype,
557
 
** or constant definition does not appear in this file, then it is
558
 
** not a published API of SQLite, is subject to change without
559
 
** notice, and should not be referenced by programs that use SQLite.
560
 
**
561
 
** Some of the definitions that are in this file are marked as
562
 
** "experimental".  Experimental interfaces are normally new
563
 
** features recently added to SQLite.  We do not anticipate changes
564
 
** to experimental interfaces but reserve the right to make minor changes
565
 
** if experience from use "in the wild" suggest such changes are prudent.
566
 
**
567
 
** The official C-language API documentation for SQLite is derived
568
 
** from comments in this file.  This file is the authoritative source
569
 
** on how SQLite interfaces are suppose to operate.
570
 
**
571
 
** The name of this file under configuration management is "sqlite.h.in".
572
 
** The makefile makes some minor changes to this file (such as inserting
573
 
** the version number) and changes its name to "sqlite3.h" as
574
 
** part of the build process.
575
 
*/
576
 
#ifndef _SQLITE3_H_
577
 
#define _SQLITE3_H_
578
 
#include <stdarg.h>     /* Needed for the definition of va_list */
579
 
 
580
 
/*
581
 
** Make sure we can call this stuff from C++.
582
 
*/
583
 
#if 0
584
 
extern "C" {
585
 
#endif
586
 
 
587
 
 
588
 
/*
589
 
** Add the ability to override 'extern'
590
 
*/
591
 
#ifndef SQLITE_EXTERN
592
 
# define SQLITE_EXTERN extern
593
 
#endif
594
 
 
595
 
#ifndef SQLITE_API
596
 
# define SQLITE_API
597
 
#endif
598
 
 
599
 
 
600
 
/*
601
 
** These no-op macros are used in front of interfaces to mark those
602
 
** interfaces as either deprecated or experimental.  New applications
603
 
** should not use deprecated interfaces - they are support for backwards
604
 
** compatibility only.  Application writers should be aware that
605
 
** experimental interfaces are subject to change in point releases.
606
 
**
607
 
** These macros used to resolve to various kinds of compiler magic that
608
 
** would generate warning messages when they were used.  But that
609
 
** compiler magic ended up generating such a flurry of bug reports
610
 
** that we have taken it all out and gone back to using simple
611
 
** noop macros.
612
 
*/
613
 
#define SQLITE_DEPRECATED
614
 
#define SQLITE_EXPERIMENTAL
615
 
 
616
 
/*
617
 
** Ensure these symbols were not defined by some previous header file.
618
 
*/
619
 
#ifdef SQLITE_VERSION
620
 
# undef SQLITE_VERSION
621
 
#endif
622
 
#ifdef SQLITE_VERSION_NUMBER
623
 
# undef SQLITE_VERSION_NUMBER
624
 
#endif
625
 
 
626
 
/*
627
 
** CAPI3REF: Compile-Time Library Version Numbers
628
 
**
629
 
** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
630
 
** evaluates to a string literal that is the SQLite version in the
631
 
** format "X.Y.Z" where X is the major version number (always 3 for
632
 
** SQLite3) and Y is the minor version number and Z is the release number.)^
633
 
** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
634
 
** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
635
 
** numbers used in [SQLITE_VERSION].)^
636
 
** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
637
 
** be larger than the release from which it is derived.  Either Y will
638
 
** be held constant and Z will be incremented or else Y will be incremented
639
 
** and Z will be reset to zero.
640
 
**
641
 
** Since version 3.6.18, SQLite source code has been stored in the
642
 
** <a href="http://www.fossil-scm.org/">Fossil configuration management
643
 
** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
644
 
** a string which identifies a particular check-in of SQLite
645
 
** within its configuration management system.  ^The SQLITE_SOURCE_ID
646
 
** string contains the date and time of the check-in (UTC) and an SHA1
647
 
** hash of the entire source tree.
648
 
**
649
 
** See also: [sqlite3_libversion()],
650
 
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
651
 
** [sqlite_version()] and [sqlite_source_id()].
652
 
*/
653
 
#define SQLITE_VERSION        "3.7.6.2"
654
 
#define SQLITE_VERSION_NUMBER 3007006
655
 
#define SQLITE_SOURCE_ID      "2011-04-17 17:25:17 154ddbc17120be2915eb03edc52af1225eb7cb5e"
656
 
 
657
 
/*
658
 
** CAPI3REF: Run-Time Library Version Numbers
659
 
** KEYWORDS: sqlite3_version, sqlite3_sourceid
660
 
**
661
 
** These interfaces provide the same information as the [SQLITE_VERSION],
662
 
** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
663
 
** but are associated with the library instead of the header file.  ^(Cautious
664
 
** programmers might include assert() statements in their application to
665
 
** verify that values returned by these interfaces match the macros in
666
 
** the header, and thus insure that the application is
667
 
** compiled with matching library and header files.
668
 
**
669
 
** <blockquote><pre>
670
 
** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
671
 
** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
672
 
** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
673
 
** </pre></blockquote>)^
674
 
**
675
 
** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
676
 
** macro.  ^The sqlite3_libversion() function returns a pointer to the
677
 
** to the sqlite3_version[] string constant.  The sqlite3_libversion()
678
 
** function is provided for use in DLLs since DLL users usually do not have
679
 
** direct access to string constants within the DLL.  ^The
680
 
** sqlite3_libversion_number() function returns an integer equal to
681
 
** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns 
682
 
** a pointer to a string constant whose value is the same as the 
683
 
** [SQLITE_SOURCE_ID] C preprocessor macro.
684
 
**
685
 
** See also: [sqlite_version()] and [sqlite_source_id()].
686
 
*/
687
 
SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
688
 
SQLITE_API const char *sqlite3_libversion(void);
689
 
SQLITE_API const char *sqlite3_sourceid(void);
690
 
SQLITE_API int sqlite3_libversion_number(void);
691
 
 
692
 
/*
693
 
** CAPI3REF: Run-Time Library Compilation Options Diagnostics
694
 
**
695
 
** ^The sqlite3_compileoption_used() function returns 0 or 1 
696
 
** indicating whether the specified option was defined at 
697
 
** compile time.  ^The SQLITE_ prefix may be omitted from the 
698
 
** option name passed to sqlite3_compileoption_used().  
699
 
**
700
 
** ^The sqlite3_compileoption_get() function allows iterating
701
 
** over the list of options that were defined at compile time by
702
 
** returning the N-th compile time option string.  ^If N is out of range,
703
 
** sqlite3_compileoption_get() returns a NULL pointer.  ^The SQLITE_ 
704
 
** prefix is omitted from any strings returned by 
705
 
** sqlite3_compileoption_get().
706
 
**
707
 
** ^Support for the diagnostic functions sqlite3_compileoption_used()
708
 
** and sqlite3_compileoption_get() may be omitted by specifying the 
709
 
** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
710
 
**
711
 
** See also: SQL functions [sqlite_compileoption_used()] and
712
 
** [sqlite_compileoption_get()] and the [compile_options pragma].
713
 
*/
714
 
#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
715
 
SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
716
 
SQLITE_API const char *sqlite3_compileoption_get(int N);
717
 
#endif
718
 
 
719
 
/*
720
 
** CAPI3REF: Test To See If The Library Is Threadsafe
721
 
**
722
 
** ^The sqlite3_threadsafe() function returns zero if and only if
723
 
** SQLite was compiled mutexing code omitted due to the
724
 
** [SQLITE_THREADSAFE] compile-time option being set to 0.
725
 
**
726
 
** SQLite can be compiled with or without mutexes.  When
727
 
** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
728
 
** are enabled and SQLite is threadsafe.  When the
729
 
** [SQLITE_THREADSAFE] macro is 0, 
730
 
** the mutexes are omitted.  Without the mutexes, it is not safe
731
 
** to use SQLite concurrently from more than one thread.
732
 
**
733
 
** Enabling mutexes incurs a measurable performance penalty.
734
 
** So if speed is of utmost importance, it makes sense to disable
735
 
** the mutexes.  But for maximum safety, mutexes should be enabled.
736
 
** ^The default behavior is for mutexes to be enabled.
737
 
**
738
 
** This interface can be used by an application to make sure that the
739
 
** version of SQLite that it is linking against was compiled with
740
 
** the desired setting of the [SQLITE_THREADSAFE] macro.
741
 
**
742
 
** This interface only reports on the compile-time mutex setting
743
 
** of the [SQLITE_THREADSAFE] flag.  If SQLite is compiled with
744
 
** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
745
 
** can be fully or partially disabled using a call to [sqlite3_config()]
746
 
** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
747
 
** or [SQLITE_CONFIG_MUTEX].  ^(The return value of the
748
 
** sqlite3_threadsafe() function shows only the compile-time setting of
749
 
** thread safety, not any run-time changes to that setting made by
750
 
** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
751
 
** is unchanged by calls to sqlite3_config().)^
752
 
**
753
 
** See the [threading mode] documentation for additional information.
754
 
*/
755
 
SQLITE_API int sqlite3_threadsafe(void);
756
 
 
757
 
/*
758
 
** CAPI3REF: Database Connection Handle
759
 
** KEYWORDS: {database connection} {database connections}
760
 
**
761
 
** Each open SQLite database is represented by a pointer to an instance of
762
 
** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
763
 
** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
764
 
** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
765
 
** is its destructor.  There are many other interfaces (such as
766
 
** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
767
 
** [sqlite3_busy_timeout()] to name but three) that are methods on an
768
 
** sqlite3 object.
769
 
*/
770
 
typedef struct sqlite3 sqlite3;
771
 
 
772
 
/*
773
 
** CAPI3REF: 64-Bit Integer Types
774
 
** KEYWORDS: sqlite_int64 sqlite_uint64
775
 
**
776
 
** Because there is no cross-platform way to specify 64-bit integer types
777
 
** SQLite includes typedefs for 64-bit signed and unsigned integers.
778
 
**
779
 
** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
780
 
** The sqlite_int64 and sqlite_uint64 types are supported for backwards
781
 
** compatibility only.
782
 
**
783
 
** ^The sqlite3_int64 and sqlite_int64 types can store integer values
784
 
** between -9223372036854775808 and +9223372036854775807 inclusive.  ^The
785
 
** sqlite3_uint64 and sqlite_uint64 types can store integer values 
786
 
** between 0 and +18446744073709551615 inclusive.
787
 
*/
788
 
#ifdef SQLITE_INT64_TYPE
789
 
  typedef SQLITE_INT64_TYPE sqlite_int64;
790
 
  typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
791
 
#elif defined(_MSC_VER) || defined(__BORLANDC__)
792
 
  typedef __int64 sqlite_int64;
793
 
  typedef unsigned __int64 sqlite_uint64;
794
 
#else
795
 
  typedef long long int sqlite_int64;
796
 
  typedef unsigned long long int sqlite_uint64;
797
 
#endif
798
 
typedef sqlite_int64 sqlite3_int64;
799
 
typedef sqlite_uint64 sqlite3_uint64;
800
 
 
801
 
/*
802
 
** If compiling for a processor that lacks floating point support,
803
 
** substitute integer for floating-point.
804
 
*/
805
 
#ifdef SQLITE_OMIT_FLOATING_POINT
806
 
# define double sqlite3_int64
807
 
#endif
808
 
 
809
 
/*
810
 
** CAPI3REF: Closing A Database Connection
811
 
**
812
 
** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
813
 
** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
814
 
** successfully destroyed and all associated resources are deallocated.
815
 
**
816
 
** Applications must [sqlite3_finalize | finalize] all [prepared statements]
817
 
** and [sqlite3_blob_close | close] all [BLOB handles] associated with
818
 
** the [sqlite3] object prior to attempting to close the object.  ^If
819
 
** sqlite3_close() is called on a [database connection] that still has
820
 
** outstanding [prepared statements] or [BLOB handles], then it returns
821
 
** SQLITE_BUSY.
822
 
**
823
 
** ^If [sqlite3_close()] is invoked while a transaction is open,
824
 
** the transaction is automatically rolled back.
825
 
**
826
 
** The C parameter to [sqlite3_close(C)] must be either a NULL
827
 
** pointer or an [sqlite3] object pointer obtained
828
 
** from [sqlite3_open()], [sqlite3_open16()], or
829
 
** [sqlite3_open_v2()], and not previously closed.
830
 
** ^Calling sqlite3_close() with a NULL pointer argument is a 
831
 
** harmless no-op.
832
 
*/
833
 
SQLITE_API int sqlite3_close(sqlite3 *);
834
 
 
835
 
/*
836
 
** The type for a callback function.
837
 
** This is legacy and deprecated.  It is included for historical
838
 
** compatibility and is not documented.
839
 
*/
840
 
typedef int (*sqlite3_callback)(void*,int,char**, char**);
841
 
 
842
 
/*
843
 
** CAPI3REF: One-Step Query Execution Interface
844
 
**
845
 
** The sqlite3_exec() interface is a convenience wrapper around
846
 
** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
847
 
** that allows an application to run multiple statements of SQL
848
 
** without having to use a lot of C code. 
849
 
**
850
 
** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
851
 
** semicolon-separate SQL statements passed into its 2nd argument,
852
 
** in the context of the [database connection] passed in as its 1st
853
 
** argument.  ^If the callback function of the 3rd argument to
854
 
** sqlite3_exec() is not NULL, then it is invoked for each result row
855
 
** coming out of the evaluated SQL statements.  ^The 4th argument to
856
 
** to sqlite3_exec() is relayed through to the 1st argument of each
857
 
** callback invocation.  ^If the callback pointer to sqlite3_exec()
858
 
** is NULL, then no callback is ever invoked and result rows are
859
 
** ignored.
860
 
**
861
 
** ^If an error occurs while evaluating the SQL statements passed into
862
 
** sqlite3_exec(), then execution of the current statement stops and
863
 
** subsequent statements are skipped.  ^If the 5th parameter to sqlite3_exec()
864
 
** is not NULL then any error message is written into memory obtained
865
 
** from [sqlite3_malloc()] and passed back through the 5th parameter.
866
 
** To avoid memory leaks, the application should invoke [sqlite3_free()]
867
 
** on error message strings returned through the 5th parameter of
868
 
** of sqlite3_exec() after the error message string is no longer needed.
869
 
** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
870
 
** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
871
 
** NULL before returning.
872
 
**
873
 
** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
874
 
** routine returns SQLITE_ABORT without invoking the callback again and
875
 
** without running any subsequent SQL statements.
876
 
**
877
 
** ^The 2nd argument to the sqlite3_exec() callback function is the
878
 
** number of columns in the result.  ^The 3rd argument to the sqlite3_exec()
879
 
** callback is an array of pointers to strings obtained as if from
880
 
** [sqlite3_column_text()], one for each column.  ^If an element of a
881
 
** result row is NULL then the corresponding string pointer for the
882
 
** sqlite3_exec() callback is a NULL pointer.  ^The 4th argument to the
883
 
** sqlite3_exec() callback is an array of pointers to strings where each
884
 
** entry represents the name of corresponding result column as obtained
885
 
** from [sqlite3_column_name()].
886
 
**
887
 
** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
888
 
** to an empty string, or a pointer that contains only whitespace and/or 
889
 
** SQL comments, then no SQL statements are evaluated and the database
890
 
** is not changed.
891
 
**
892
 
** Restrictions:
893
 
**
894
 
** <ul>
895
 
** <li> The application must insure that the 1st parameter to sqlite3_exec()
896
 
**      is a valid and open [database connection].
897
 
** <li> The application must not close [database connection] specified by
898
 
**      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
899
 
** <li> The application must not modify the SQL statement text passed into
900
 
**      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
901
 
** </ul>
902
 
*/
903
 
SQLITE_API int sqlite3_exec(
904
 
  sqlite3*,                                  /* An open database */
905
 
  const char *sql,                           /* SQL to be evaluated */
906
 
  int (*callback)(void*,int,char**,char**),  /* Callback function */
907
 
  void *,                                    /* 1st argument to callback */
908
 
  char **errmsg                              /* Error msg written here */
909
 
);
910
 
 
911
 
/*
912
 
** CAPI3REF: Result Codes
913
 
** KEYWORDS: SQLITE_OK {error code} {error codes}
914
 
** KEYWORDS: {result code} {result codes}
915
 
**
916
 
** Many SQLite functions return an integer result code from the set shown
917
 
** here in order to indicates success or failure.
918
 
**
919
 
** New error codes may be added in future versions of SQLite.
920
 
**
921
 
** See also: [SQLITE_IOERR_READ | extended result codes]
922
 
*/
923
 
#define SQLITE_OK           0   /* Successful result */
924
 
/* beginning-of-error-codes */
925
 
#define SQLITE_ERROR        1   /* SQL error or missing database */
926
 
#define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
927
 
#define SQLITE_PERM         3   /* Access permission denied */
928
 
#define SQLITE_ABORT        4   /* Callback routine requested an abort */
929
 
#define SQLITE_BUSY         5   /* The database file is locked */
930
 
#define SQLITE_LOCKED       6   /* A table in the database is locked */
931
 
#define SQLITE_NOMEM        7   /* A malloc() failed */
932
 
#define SQLITE_READONLY     8   /* Attempt to write a readonly database */
933
 
#define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
934
 
#define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
935
 
#define SQLITE_CORRUPT     11   /* The database disk image is malformed */
936
 
#define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
937
 
#define SQLITE_FULL        13   /* Insertion failed because database is full */
938
 
#define SQLITE_CANTOPEN    14   /* Unable to open the database file */
939
 
#define SQLITE_PROTOCOL    15   /* Database lock protocol error */
940
 
#define SQLITE_EMPTY       16   /* Database is empty */
941
 
#define SQLITE_SCHEMA      17   /* The database schema changed */
942
 
#define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
943
 
#define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
944
 
#define SQLITE_MISMATCH    20   /* Data type mismatch */
945
 
#define SQLITE_MISUSE      21   /* Library used incorrectly */
946
 
#define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
947
 
#define SQLITE_AUTH        23   /* Authorization denied */
948
 
#define SQLITE_FORMAT      24   /* Auxiliary database format error */
949
 
#define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
950
 
#define SQLITE_NOTADB      26   /* File opened that is not a database file */
951
 
#define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
952
 
#define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
953
 
/* end-of-error-codes */
954
 
 
955
 
/*
956
 
** CAPI3REF: Extended Result Codes
957
 
** KEYWORDS: {extended error code} {extended error codes}
958
 
** KEYWORDS: {extended result code} {extended result codes}
959
 
**
960
 
** In its default configuration, SQLite API routines return one of 26 integer
961
 
** [SQLITE_OK | result codes].  However, experience has shown that many of
962
 
** these result codes are too coarse-grained.  They do not provide as
963
 
** much information about problems as programmers might like.  In an effort to
964
 
** address this, newer versions of SQLite (version 3.3.8 and later) include
965
 
** support for additional result codes that provide more detailed information
966
 
** about errors. The extended result codes are enabled or disabled
967
 
** on a per database connection basis using the
968
 
** [sqlite3_extended_result_codes()] API.
969
 
**
970
 
** Some of the available extended result codes are listed here.
971
 
** One may expect the number of extended result codes will be expand
972
 
** over time.  Software that uses extended result codes should expect
973
 
** to see new result codes in future releases of SQLite.
974
 
**
975
 
** The SQLITE_OK result code will never be extended.  It will always
976
 
** be exactly zero.
977
 
*/
978
 
#define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
979
 
#define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
980
 
#define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
981
 
#define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
982
 
#define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
983
 
#define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
984
 
#define SQLITE_IOERR_FSTAT             (SQLITE_IOERR | (7<<8))
985
 
#define SQLITE_IOERR_UNLOCK            (SQLITE_IOERR | (8<<8))
986
 
#define SQLITE_IOERR_RDLOCK            (SQLITE_IOERR | (9<<8))
987
 
#define SQLITE_IOERR_DELETE            (SQLITE_IOERR | (10<<8))
988
 
#define SQLITE_IOERR_BLOCKED           (SQLITE_IOERR | (11<<8))
989
 
#define SQLITE_IOERR_NOMEM             (SQLITE_IOERR | (12<<8))
990
 
#define SQLITE_IOERR_ACCESS            (SQLITE_IOERR | (13<<8))
991
 
#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
992
 
#define SQLITE_IOERR_LOCK              (SQLITE_IOERR | (15<<8))
993
 
#define SQLITE_IOERR_CLOSE             (SQLITE_IOERR | (16<<8))
994
 
#define SQLITE_IOERR_DIR_CLOSE         (SQLITE_IOERR | (17<<8))
995
 
#define SQLITE_IOERR_SHMOPEN           (SQLITE_IOERR | (18<<8))
996
 
#define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
997
 
#define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
998
 
#define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
999
 
#define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
1000
 
#define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
1001
 
 
1002
 
/*
1003
 
** CAPI3REF: Flags For File Open Operations
1004
 
**
1005
 
** These bit values are intended for use in the
1006
 
** 3rd parameter to the [sqlite3_open_v2()] interface and
1007
 
** in the 4th parameter to the xOpen method of the
1008
 
** [sqlite3_vfs] object.
1009
 
*/
1010
 
#define SQLITE_OPEN_READONLY         0x00000001  /* Ok for sqlite3_open_v2() */
1011
 
#define SQLITE_OPEN_READWRITE        0x00000002  /* Ok for sqlite3_open_v2() */
1012
 
#define SQLITE_OPEN_CREATE           0x00000004  /* Ok for sqlite3_open_v2() */
1013
 
#define SQLITE_OPEN_DELETEONCLOSE    0x00000008  /* VFS only */
1014
 
#define SQLITE_OPEN_EXCLUSIVE        0x00000010  /* VFS only */
1015
 
#define SQLITE_OPEN_AUTOPROXY        0x00000020  /* VFS only */
1016
 
#define SQLITE_OPEN_MAIN_DB          0x00000100  /* VFS only */
1017
 
#define SQLITE_OPEN_TEMP_DB          0x00000200  /* VFS only */
1018
 
#define SQLITE_OPEN_TRANSIENT_DB     0x00000400  /* VFS only */
1019
 
#define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  /* VFS only */
1020
 
#define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  /* VFS only */
1021
 
#define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
1022
 
#define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
1023
 
#define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
1024
 
#define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
1025
 
#define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
1026
 
#define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
1027
 
#define SQLITE_OPEN_WAL              0x00080000  /* VFS only */
1028
 
 
1029
 
/* Reserved:                         0x00F00000 */
1030
 
 
1031
 
/*
1032
 
** CAPI3REF: Device Characteristics
1033
 
**
1034
 
** The xDeviceCharacteristics method of the [sqlite3_io_methods]
1035
 
** object returns an integer which is a vector of the these
1036
 
** bit values expressing I/O characteristics of the mass storage
1037
 
** device that holds the file that the [sqlite3_io_methods]
1038
 
** refers to.
1039
 
**
1040
 
** The SQLITE_IOCAP_ATOMIC property means that all writes of
1041
 
** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
1042
 
** mean that writes of blocks that are nnn bytes in size and
1043
 
** are aligned to an address which is an integer multiple of
1044
 
** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
1045
 
** that when data is appended to a file, the data is appended
1046
 
** first then the size of the file is extended, never the other
1047
 
** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
1048
 
** information is written to disk in the same order as calls
1049
 
** to xWrite().
1050
 
*/
1051
 
#define SQLITE_IOCAP_ATOMIC                 0x00000001
1052
 
#define SQLITE_IOCAP_ATOMIC512              0x00000002
1053
 
#define SQLITE_IOCAP_ATOMIC1K               0x00000004
1054
 
#define SQLITE_IOCAP_ATOMIC2K               0x00000008
1055
 
#define SQLITE_IOCAP_ATOMIC4K               0x00000010
1056
 
#define SQLITE_IOCAP_ATOMIC8K               0x00000020
1057
 
#define SQLITE_IOCAP_ATOMIC16K              0x00000040
1058
 
#define SQLITE_IOCAP_ATOMIC32K              0x00000080
1059
 
#define SQLITE_IOCAP_ATOMIC64K              0x00000100
1060
 
#define SQLITE_IOCAP_SAFE_APPEND            0x00000200
1061
 
#define SQLITE_IOCAP_SEQUENTIAL             0x00000400
1062
 
#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
1063
 
 
1064
 
/*
1065
 
** CAPI3REF: File Locking Levels
1066
 
**
1067
 
** SQLite uses one of these integer values as the second
1068
 
** argument to calls it makes to the xLock() and xUnlock() methods
1069
 
** of an [sqlite3_io_methods] object.
1070
 
*/
1071
 
#define SQLITE_LOCK_NONE          0
1072
 
#define SQLITE_LOCK_SHARED        1
1073
 
#define SQLITE_LOCK_RESERVED      2
1074
 
#define SQLITE_LOCK_PENDING       3
1075
 
#define SQLITE_LOCK_EXCLUSIVE     4
1076
 
 
1077
 
/*
1078
 
** CAPI3REF: Synchronization Type Flags
1079
 
**
1080
 
** When SQLite invokes the xSync() method of an
1081
 
** [sqlite3_io_methods] object it uses a combination of
1082
 
** these integer values as the second argument.
1083
 
**
1084
 
** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
1085
 
** sync operation only needs to flush data to mass storage.  Inode
1086
 
** information need not be flushed. If the lower four bits of the flag
1087
 
** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
1088
 
** If the lower four bits equal SQLITE_SYNC_FULL, that means
1089
 
** to use Mac OS X style fullsync instead of fsync().
1090
 
**
1091
 
** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
1092
 
** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
1093
 
** settings.  The [synchronous pragma] determines when calls to the
1094
 
** xSync VFS method occur and applies uniformly across all platforms.
1095
 
** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
1096
 
** energetic or rigorous or forceful the sync operations are and
1097
 
** only make a difference on Mac OSX for the default SQLite code.
1098
 
** (Third-party VFS implementations might also make the distinction
1099
 
** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
1100
 
** operating systems natively supported by SQLite, only Mac OSX
1101
 
** cares about the difference.)
1102
 
*/
1103
 
#define SQLITE_SYNC_NORMAL        0x00002
1104
 
#define SQLITE_SYNC_FULL          0x00003
1105
 
#define SQLITE_SYNC_DATAONLY      0x00010
1106
 
 
1107
 
/*
1108
 
** CAPI3REF: OS Interface Open File Handle
1109
 
**
1110
 
** An [sqlite3_file] object represents an open file in the 
1111
 
** [sqlite3_vfs | OS interface layer].  Individual OS interface
1112
 
** implementations will
1113
 
** want to subclass this object by appending additional fields
1114
 
** for their own use.  The pMethods entry is a pointer to an
1115
 
** [sqlite3_io_methods] object that defines methods for performing
1116
 
** I/O operations on the open file.
1117
 
*/
1118
 
typedef struct sqlite3_file sqlite3_file;
1119
 
struct sqlite3_file {
1120
 
  const struct sqlite3_io_methods *pMethods;  /* Methods for an open file */
1121
 
};
1122
 
 
1123
 
/*
1124
 
** CAPI3REF: OS Interface File Virtual Methods Object
1125
 
**
1126
 
** Every file opened by the [sqlite3_vfs] xOpen method populates an
1127
 
** [sqlite3_file] object (or, more commonly, a subclass of the
1128
 
** [sqlite3_file] object) with a pointer to an instance of this object.
1129
 
** This object defines the methods used to perform various operations
1130
 
** against the open file represented by the [sqlite3_file] object.
1131
 
**
1132
 
** If the xOpen method sets the sqlite3_file.pMethods element 
1133
 
** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1134
 
** may be invoked even if the xOpen reported that it failed.  The
1135
 
** only way to prevent a call to xClose following a failed xOpen
1136
 
** is for the xOpen to set the sqlite3_file.pMethods element to NULL.
1137
 
**
1138
 
** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1139
 
** [SQLITE_SYNC_FULL].  The first choice is the normal fsync().
1140
 
** The second choice is a Mac OS X style fullsync.  The [SQLITE_SYNC_DATAONLY]
1141
 
** flag may be ORed in to indicate that only the data of the file
1142
 
** and not its inode needs to be synced.
1143
 
**
1144
 
** The integer values to xLock() and xUnlock() are one of
1145
 
** <ul>
1146
 
** <li> [SQLITE_LOCK_NONE],
1147
 
** <li> [SQLITE_LOCK_SHARED],
1148
 
** <li> [SQLITE_LOCK_RESERVED],
1149
 
** <li> [SQLITE_LOCK_PENDING], or
1150
 
** <li> [SQLITE_LOCK_EXCLUSIVE].
1151
 
** </ul>
1152
 
** xLock() increases the lock. xUnlock() decreases the lock.
1153
 
** The xCheckReservedLock() method checks whether any database connection,
1154
 
** either in this process or in some other process, is holding a RESERVED,
1155
 
** PENDING, or EXCLUSIVE lock on the file.  It returns true
1156
 
** if such a lock exists and false otherwise.
1157
 
**
1158
 
** The xFileControl() method is a generic interface that allows custom
1159
 
** VFS implementations to directly control an open file using the
1160
 
** [sqlite3_file_control()] interface.  The second "op" argument is an
1161
 
** integer opcode.  The third argument is a generic pointer intended to
1162
 
** point to a structure that may contain arguments or space in which to
1163
 
** write return values.  Potential uses for xFileControl() might be
1164
 
** functions to enable blocking locks with timeouts, to change the
1165
 
** locking strategy (for example to use dot-file locks), to inquire
1166
 
** about the status of a lock, or to break stale locks.  The SQLite
1167
 
** core reserves all opcodes less than 100 for its own use.
1168
 
** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
1169
 
** Applications that define a custom xFileControl method should use opcodes
1170
 
** greater than 100 to avoid conflicts.  VFS implementations should
1171
 
** return [SQLITE_NOTFOUND] for file control opcodes that they do not
1172
 
** recognize.
1173
 
**
1174
 
** The xSectorSize() method returns the sector size of the
1175
 
** device that underlies the file.  The sector size is the
1176
 
** minimum write that can be performed without disturbing
1177
 
** other bytes in the file.  The xDeviceCharacteristics()
1178
 
** method returns a bit vector describing behaviors of the
1179
 
** underlying device:
1180
 
**
1181
 
** <ul>
1182
 
** <li> [SQLITE_IOCAP_ATOMIC]
1183
 
** <li> [SQLITE_IOCAP_ATOMIC512]
1184
 
** <li> [SQLITE_IOCAP_ATOMIC1K]
1185
 
** <li> [SQLITE_IOCAP_ATOMIC2K]
1186
 
** <li> [SQLITE_IOCAP_ATOMIC4K]
1187
 
** <li> [SQLITE_IOCAP_ATOMIC8K]
1188
 
** <li> [SQLITE_IOCAP_ATOMIC16K]
1189
 
** <li> [SQLITE_IOCAP_ATOMIC32K]
1190
 
** <li> [SQLITE_IOCAP_ATOMIC64K]
1191
 
** <li> [SQLITE_IOCAP_SAFE_APPEND]
1192
 
** <li> [SQLITE_IOCAP_SEQUENTIAL]
1193
 
** </ul>
1194
 
**
1195
 
** The SQLITE_IOCAP_ATOMIC property means that all writes of
1196
 
** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
1197
 
** mean that writes of blocks that are nnn bytes in size and
1198
 
** are aligned to an address which is an integer multiple of
1199
 
** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
1200
 
** that when data is appended to a file, the data is appended
1201
 
** first then the size of the file is extended, never the other
1202
 
** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
1203
 
** information is written to disk in the same order as calls
1204
 
** to xWrite().
1205
 
**
1206
 
** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
1207
 
** in the unread portions of the buffer with zeros.  A VFS that
1208
 
** fails to zero-fill short reads might seem to work.  However,
1209
 
** failure to zero-fill short reads will eventually lead to
1210
 
** database corruption.
1211
 
*/
1212
 
typedef struct sqlite3_io_methods sqlite3_io_methods;
1213
 
struct sqlite3_io_methods {
1214
 
  int iVersion;
1215
 
  int (*xClose)(sqlite3_file*);
1216
 
  int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
1217
 
  int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
1218
 
  int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
1219
 
  int (*xSync)(sqlite3_file*, int flags);
1220
 
  int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
1221
 
  int (*xLock)(sqlite3_file*, int);
1222
 
  int (*xUnlock)(sqlite3_file*, int);
1223
 
  int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1224
 
  int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1225
 
  int (*xSectorSize)(sqlite3_file*);
1226
 
  int (*xDeviceCharacteristics)(sqlite3_file*);
1227
 
  /* Methods above are valid for version 1 */
1228
 
  int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1229
 
  int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1230
 
  void (*xShmBarrier)(sqlite3_file*);
1231
 
  int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1232
 
  /* Methods above are valid for version 2 */
1233
 
  /* Additional methods may be added in future releases */
1234
 
};
1235
 
 
1236
 
/*
1237
 
** CAPI3REF: Standard File Control Opcodes
1238
 
**
1239
 
** These integer constants are opcodes for the xFileControl method
1240
 
** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
1241
 
** interface.
1242
 
**
1243
 
** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
1244
 
** opcode causes the xFileControl method to write the current state of
1245
 
** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1246
 
** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1247
 
** into an integer that the pArg argument points to. This capability
1248
 
** is used during testing and only needs to be supported when SQLITE_TEST
1249
 
** is defined.
1250
 
**
1251
 
** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1252
 
** layer a hint of how large the database file will grow to be during the
1253
 
** current transaction.  This hint is not guaranteed to be accurate but it
1254
 
** is often close.  The underlying VFS might choose to preallocate database
1255
 
** file space based on this hint in order to help writes to the database
1256
 
** file run faster.
1257
 
**
1258
 
** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
1259
 
** extends and truncates the database file in chunks of a size specified
1260
 
** by the user. The fourth argument to [sqlite3_file_control()] should 
1261
 
** point to an integer (type int) containing the new chunk-size to use
1262
 
** for the nominated database. Allocating database file space in large
1263
 
** chunks (say 1MB at a time), may reduce file-system fragmentation and
1264
 
** improve performance on some systems.
1265
 
**
1266
 
** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1267
 
** to the [sqlite3_file] object associated with a particular database
1268
 
** connection.  See the [sqlite3_file_control()] documentation for
1269
 
** additional information.
1270
 
**
1271
 
** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
1272
 
** SQLite and sent to all VFSes in place of a call to the xSync method
1273
 
** when the database connection has [PRAGMA synchronous] set to OFF.)^
1274
 
** Some specialized VFSes need this signal in order to operate correctly
1275
 
** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most 
1276
 
** VFSes do not need this signal and should silently ignore this opcode.
1277
 
** Applications should not call [sqlite3_file_control()] with this
1278
 
** opcode as doing so may disrupt the operation of the specialized VFSes
1279
 
** that do require it.  
1280
 
*/
1281
 
#define SQLITE_FCNTL_LOCKSTATE        1
1282
 
#define SQLITE_GET_LOCKPROXYFILE      2
1283
 
#define SQLITE_SET_LOCKPROXYFILE      3
1284
 
#define SQLITE_LAST_ERRNO             4
1285
 
#define SQLITE_FCNTL_SIZE_HINT        5
1286
 
#define SQLITE_FCNTL_CHUNK_SIZE       6
1287
 
#define SQLITE_FCNTL_FILE_POINTER     7
1288
 
#define SQLITE_FCNTL_SYNC_OMITTED     8
1289
 
 
1290
 
 
1291
 
/*
1292
 
** CAPI3REF: Mutex Handle
1293
 
**
1294
 
** The mutex module within SQLite defines [sqlite3_mutex] to be an
1295
 
** abstract type for a mutex object.  The SQLite core never looks
1296
 
** at the internal representation of an [sqlite3_mutex].  It only
1297
 
** deals with pointers to the [sqlite3_mutex] object.
1298
 
**
1299
 
** Mutexes are created using [sqlite3_mutex_alloc()].
1300
 
*/
1301
 
typedef struct sqlite3_mutex sqlite3_mutex;
1302
 
 
1303
 
/*
1304
 
** CAPI3REF: OS Interface Object
1305
 
**
1306
 
** An instance of the sqlite3_vfs object defines the interface between
1307
 
** the SQLite core and the underlying operating system.  The "vfs"
1308
 
** in the name of the object stands for "virtual file system".
1309
 
**
1310
 
** The value of the iVersion field is initially 1 but may be larger in
1311
 
** future versions of SQLite.  Additional fields may be appended to this
1312
 
** object when the iVersion value is increased.  Note that the structure
1313
 
** of the sqlite3_vfs object changes in the transaction between
1314
 
** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
1315
 
** modified.
1316
 
**
1317
 
** The szOsFile field is the size of the subclassed [sqlite3_file]
1318
 
** structure used by this VFS.  mxPathname is the maximum length of
1319
 
** a pathname in this VFS.
1320
 
**
1321
 
** Registered sqlite3_vfs objects are kept on a linked list formed by
1322
 
** the pNext pointer.  The [sqlite3_vfs_register()]
1323
 
** and [sqlite3_vfs_unregister()] interfaces manage this list
1324
 
** in a thread-safe way.  The [sqlite3_vfs_find()] interface
1325
 
** searches the list.  Neither the application code nor the VFS
1326
 
** implementation should use the pNext pointer.
1327
 
**
1328
 
** The pNext field is the only field in the sqlite3_vfs
1329
 
** structure that SQLite will ever modify.  SQLite will only access
1330
 
** or modify this field while holding a particular static mutex.
1331
 
** The application should never modify anything within the sqlite3_vfs
1332
 
** object once the object has been registered.
1333
 
**
1334
 
** The zName field holds the name of the VFS module.  The name must
1335
 
** be unique across all VFS modules.
1336
 
**
1337
 
** ^SQLite guarantees that the zFilename parameter to xOpen
1338
 
** is either a NULL pointer or string obtained
1339
 
** from xFullPathname() with an optional suffix added.
1340
 
** ^If a suffix is added to the zFilename parameter, it will
1341
 
** consist of a single "-" character followed by no more than
1342
 
** 10 alphanumeric and/or "-" characters.
1343
 
** ^SQLite further guarantees that
1344
 
** the string will be valid and unchanged until xClose() is
1345
 
** called. Because of the previous sentence,
1346
 
** the [sqlite3_file] can safely store a pointer to the
1347
 
** filename if it needs to remember the filename for some reason.
1348
 
** If the zFilename parameter to xOpen is a NULL pointer then xOpen
1349
 
** must invent its own temporary name for the file.  ^Whenever the 
1350
 
** xFilename parameter is NULL it will also be the case that the
1351
 
** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
1352
 
**
1353
 
** The flags argument to xOpen() includes all bits set in
1354
 
** the flags argument to [sqlite3_open_v2()].  Or if [sqlite3_open()]
1355
 
** or [sqlite3_open16()] is used, then flags includes at least
1356
 
** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. 
1357
 
** If xOpen() opens a file read-only then it sets *pOutFlags to
1358
 
** include [SQLITE_OPEN_READONLY].  Other bits in *pOutFlags may be set.
1359
 
**
1360
 
** ^(SQLite will also add one of the following flags to the xOpen()
1361
 
** call, depending on the object being opened:
1362
 
**
1363
 
** <ul>
1364
 
** <li>  [SQLITE_OPEN_MAIN_DB]
1365
 
** <li>  [SQLITE_OPEN_MAIN_JOURNAL]
1366
 
** <li>  [SQLITE_OPEN_TEMP_DB]
1367
 
** <li>  [SQLITE_OPEN_TEMP_JOURNAL]
1368
 
** <li>  [SQLITE_OPEN_TRANSIENT_DB]
1369
 
** <li>  [SQLITE_OPEN_SUBJOURNAL]
1370
 
** <li>  [SQLITE_OPEN_MASTER_JOURNAL]
1371
 
** <li>  [SQLITE_OPEN_WAL]
1372
 
** </ul>)^
1373
 
**
1374
 
** The file I/O implementation can use the object type flags to
1375
 
** change the way it deals with files.  For example, an application
1376
 
** that does not care about crash recovery or rollback might make
1377
 
** the open of a journal file a no-op.  Writes to this journal would
1378
 
** also be no-ops, and any attempt to read the journal would return
1379
 
** SQLITE_IOERR.  Or the implementation might recognize that a database
1380
 
** file will be doing page-aligned sector reads and writes in a random
1381
 
** order and set up its I/O subsystem accordingly.
1382
 
**
1383
 
** SQLite might also add one of the following flags to the xOpen method:
1384
 
**
1385
 
** <ul>
1386
 
** <li> [SQLITE_OPEN_DELETEONCLOSE]
1387
 
** <li> [SQLITE_OPEN_EXCLUSIVE]
1388
 
** </ul>
1389
 
**
1390
 
** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
1391
 
** deleted when it is closed.  ^The [SQLITE_OPEN_DELETEONCLOSE]
1392
 
** will be set for TEMP databases and their journals, transient
1393
 
** databases, and subjournals.
1394
 
**
1395
 
** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
1396
 
** with the [SQLITE_OPEN_CREATE] flag, which are both directly
1397
 
** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
1398
 
** API.  The SQLITE_OPEN_EXCLUSIVE flag, when paired with the 
1399
 
** SQLITE_OPEN_CREATE, is used to indicate that file should always
1400
 
** be created, and that it is an error if it already exists.
1401
 
** It is <i>not</i> used to indicate the file should be opened 
1402
 
** for exclusive access.
1403
 
**
1404
 
** ^At least szOsFile bytes of memory are allocated by SQLite
1405
 
** to hold the  [sqlite3_file] structure passed as the third
1406
 
** argument to xOpen.  The xOpen method does not have to
1407
 
** allocate the structure; it should just fill it in.  Note that
1408
 
** the xOpen method must set the sqlite3_file.pMethods to either
1409
 
** a valid [sqlite3_io_methods] object or to NULL.  xOpen must do
1410
 
** this even if the open fails.  SQLite expects that the sqlite3_file.pMethods
1411
 
** element will be valid after xOpen returns regardless of the success
1412
 
** or failure of the xOpen call.
1413
 
**
1414
 
** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
1415
 
** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
1416
 
** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
1417
 
** to test whether a file is at least readable.   The file can be a
1418
 
** directory.
1419
 
**
1420
 
** ^SQLite will always allocate at least mxPathname+1 bytes for the
1421
 
** output buffer xFullPathname.  The exact size of the output buffer
1422
 
** is also passed as a parameter to both  methods. If the output buffer
1423
 
** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
1424
 
** handled as a fatal error by SQLite, vfs implementations should endeavor
1425
 
** to prevent this by setting mxPathname to a sufficiently large value.
1426
 
**
1427
 
** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
1428
 
** interfaces are not strictly a part of the filesystem, but they are
1429
 
** included in the VFS structure for completeness.
1430
 
** The xRandomness() function attempts to return nBytes bytes
1431
 
** of good-quality randomness into zOut.  The return value is
1432
 
** the actual number of bytes of randomness obtained.
1433
 
** The xSleep() method causes the calling thread to sleep for at
1434
 
** least the number of microseconds given.  ^The xCurrentTime()
1435
 
** method returns a Julian Day Number for the current date and time as
1436
 
** a floating point value.
1437
 
** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
1438
 
** Day Number multipled by 86400000 (the number of milliseconds in 
1439
 
** a 24-hour day).  
1440
 
** ^SQLite will use the xCurrentTimeInt64() method to get the current
1441
 
** date and time if that method is available (if iVersion is 2 or 
1442
 
** greater and the function pointer is not NULL) and will fall back
1443
 
** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
1444
 
**
1445
 
** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
1446
 
** are not used by the SQLite core.  These optional interfaces are provided
1447
 
** by some VFSes to facilitate testing of the VFS code. By overriding 
1448
 
** system calls with functions under its control, a test program can
1449
 
** simulate faults and error conditions that would otherwise be difficult
1450
 
** or impossible to induce.  The set of system calls that can be overridden
1451
 
** varies from one VFS to another, and from one version of the same VFS to the
1452
 
** next.  Applications that use these interfaces must be prepared for any
1453
 
** or all of these interfaces to be NULL or for their behavior to change
1454
 
** from one release to the next.  Applications must not attempt to access
1455
 
** any of these methods if the iVersion of the VFS is less than 3.
1456
 
*/
1457
 
typedef struct sqlite3_vfs sqlite3_vfs;
1458
 
typedef void (*sqlite3_syscall_ptr)(void);
1459
 
struct sqlite3_vfs {
1460
 
  int iVersion;            /* Structure version number (currently 3) */
1461
 
  int szOsFile;            /* Size of subclassed sqlite3_file */
1462
 
  int mxPathname;          /* Maximum file pathname length */
1463
 
  sqlite3_vfs *pNext;      /* Next registered VFS */
1464
 
  const char *zName;       /* Name of this virtual file system */
1465
 
  void *pAppData;          /* Pointer to application-specific data */
1466
 
  int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
1467
 
               int flags, int *pOutFlags);
1468
 
  int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1469
 
  int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1470
 
  int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1471
 
  void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
1472
 
  void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
1473
 
  void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
1474
 
  void (*xDlClose)(sqlite3_vfs*, void*);
1475
 
  int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
1476
 
  int (*xSleep)(sqlite3_vfs*, int microseconds);
1477
 
  int (*xCurrentTime)(sqlite3_vfs*, double*);
1478
 
  int (*xGetLastError)(sqlite3_vfs*, int, char *);
1479
 
  /*
1480
 
  ** The methods above are in version 1 of the sqlite_vfs object
1481
 
  ** definition.  Those that follow are added in version 2 or later
1482
 
  */
1483
 
  int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
1484
 
  /*
1485
 
  ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
1486
 
  ** Those below are for version 3 and greater.
1487
 
  */
1488
 
  int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
1489
 
  sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
1490
 
  const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
1491
 
  /*
1492
 
  ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
1493
 
  ** New fields may be appended in figure versions.  The iVersion
1494
 
  ** value will increment whenever this happens. 
1495
 
  */
1496
 
};
1497
 
 
1498
 
/*
1499
 
** CAPI3REF: Flags for the xAccess VFS method
1500
 
**
1501
 
** These integer constants can be used as the third parameter to
1502
 
** the xAccess method of an [sqlite3_vfs] object.  They determine
1503
 
** what kind of permissions the xAccess method is looking for.
1504
 
** With SQLITE_ACCESS_EXISTS, the xAccess method
1505
 
** simply checks whether the file exists.
1506
 
** With SQLITE_ACCESS_READWRITE, the xAccess method
1507
 
** checks whether the named directory is both readable and writable
1508
 
** (in other words, if files can be added, removed, and renamed within
1509
 
** the directory).
1510
 
** The SQLITE_ACCESS_READWRITE constant is currently used only by the
1511
 
** [temp_store_directory pragma], though this could change in a future
1512
 
** release of SQLite.
1513
 
** With SQLITE_ACCESS_READ, the xAccess method
1514
 
** checks whether the file is readable.  The SQLITE_ACCESS_READ constant is
1515
 
** currently unused, though it might be used in a future release of
1516
 
** SQLite.
1517
 
*/
1518
 
#define SQLITE_ACCESS_EXISTS    0
1519
 
#define SQLITE_ACCESS_READWRITE 1   /* Used by PRAGMA temp_store_directory */
1520
 
#define SQLITE_ACCESS_READ      2   /* Unused */
1521
 
 
1522
 
/*
1523
 
** CAPI3REF: Flags for the xShmLock VFS method
1524
 
**
1525
 
** These integer constants define the various locking operations
1526
 
** allowed by the xShmLock method of [sqlite3_io_methods].  The
1527
 
** following are the only legal combinations of flags to the
1528
 
** xShmLock method:
1529
 
**
1530
 
** <ul>
1531
 
** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
1532
 
** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
1533
 
** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
1534
 
** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
1535
 
** </ul>
1536
 
**
1537
 
** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
1538
 
** was given no the corresponding lock.  
1539
 
**
1540
 
** The xShmLock method can transition between unlocked and SHARED or
1541
 
** between unlocked and EXCLUSIVE.  It cannot transition between SHARED
1542
 
** and EXCLUSIVE.
1543
 
*/
1544
 
#define SQLITE_SHM_UNLOCK       1
1545
 
#define SQLITE_SHM_LOCK         2
1546
 
#define SQLITE_SHM_SHARED       4
1547
 
#define SQLITE_SHM_EXCLUSIVE    8
1548
 
 
1549
 
/*
1550
 
** CAPI3REF: Maximum xShmLock index
1551
 
**
1552
 
** The xShmLock method on [sqlite3_io_methods] may use values
1553
 
** between 0 and this upper bound as its "offset" argument.
1554
 
** The SQLite core will never attempt to acquire or release a
1555
 
** lock outside of this range
1556
 
*/
1557
 
#define SQLITE_SHM_NLOCK        8
1558
 
 
1559
 
 
1560
 
/*
1561
 
** CAPI3REF: Initialize The SQLite Library
1562
 
**
1563
 
** ^The sqlite3_initialize() routine initializes the
1564
 
** SQLite library.  ^The sqlite3_shutdown() routine
1565
 
** deallocates any resources that were allocated by sqlite3_initialize().
1566
 
** These routines are designed to aid in process initialization and
1567
 
** shutdown on embedded systems.  Workstation applications using
1568
 
** SQLite normally do not need to invoke either of these routines.
1569
 
**
1570
 
** A call to sqlite3_initialize() is an "effective" call if it is
1571
 
** the first time sqlite3_initialize() is invoked during the lifetime of
1572
 
** the process, or if it is the first time sqlite3_initialize() is invoked
1573
 
** following a call to sqlite3_shutdown().  ^(Only an effective call
1574
 
** of sqlite3_initialize() does any initialization.  All other calls
1575
 
** are harmless no-ops.)^
1576
 
**
1577
 
** A call to sqlite3_shutdown() is an "effective" call if it is the first
1578
 
** call to sqlite3_shutdown() since the last sqlite3_initialize().  ^(Only
1579
 
** an effective call to sqlite3_shutdown() does any deinitialization.
1580
 
** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1581
 
**
1582
 
** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
1583
 
** is not.  The sqlite3_shutdown() interface must only be called from a
1584
 
** single thread.  All open [database connections] must be closed and all
1585
 
** other SQLite resources must be deallocated prior to invoking
1586
 
** sqlite3_shutdown().
1587
 
**
1588
 
** Among other things, ^sqlite3_initialize() will invoke
1589
 
** sqlite3_os_init().  Similarly, ^sqlite3_shutdown()
1590
 
** will invoke sqlite3_os_end().
1591
 
**
1592
 
** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
1593
 
** ^If for some reason, sqlite3_initialize() is unable to initialize
1594
 
** the library (perhaps it is unable to allocate a needed resource such
1595
 
** as a mutex) it returns an [error code] other than [SQLITE_OK].
1596
 
**
1597
 
** ^The sqlite3_initialize() routine is called internally by many other
1598
 
** SQLite interfaces so that an application usually does not need to
1599
 
** invoke sqlite3_initialize() directly.  For example, [sqlite3_open()]
1600
 
** calls sqlite3_initialize() so the SQLite library will be automatically
1601
 
** initialized when [sqlite3_open()] is called if it has not be initialized
1602
 
** already.  ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
1603
 
** compile-time option, then the automatic calls to sqlite3_initialize()
1604
 
** are omitted and the application must call sqlite3_initialize() directly
1605
 
** prior to using any other SQLite interface.  For maximum portability,
1606
 
** it is recommended that applications always invoke sqlite3_initialize()
1607
 
** directly prior to using any other SQLite interface.  Future releases
1608
 
** of SQLite may require this.  In other words, the behavior exhibited
1609
 
** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
1610
 
** default behavior in some future release of SQLite.
1611
 
**
1612
 
** The sqlite3_os_init() routine does operating-system specific
1613
 
** initialization of the SQLite library.  The sqlite3_os_end()
1614
 
** routine undoes the effect of sqlite3_os_init().  Typical tasks
1615
 
** performed by these routines include allocation or deallocation
1616
 
** of static resources, initialization of global variables,
1617
 
** setting up a default [sqlite3_vfs] module, or setting up
1618
 
** a default configuration using [sqlite3_config()].
1619
 
**
1620
 
** The application should never invoke either sqlite3_os_init()
1621
 
** or sqlite3_os_end() directly.  The application should only invoke
1622
 
** sqlite3_initialize() and sqlite3_shutdown().  The sqlite3_os_init()
1623
 
** interface is called automatically by sqlite3_initialize() and
1624
 
** sqlite3_os_end() is called by sqlite3_shutdown().  Appropriate
1625
 
** implementations for sqlite3_os_init() and sqlite3_os_end()
1626
 
** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
1627
 
** When [custom builds | built for other platforms]
1628
 
** (using the [SQLITE_OS_OTHER=1] compile-time
1629
 
** option) the application must supply a suitable implementation for
1630
 
** sqlite3_os_init() and sqlite3_os_end().  An application-supplied
1631
 
** implementation of sqlite3_os_init() or sqlite3_os_end()
1632
 
** must return [SQLITE_OK] on success and some other [error code] upon
1633
 
** failure.
1634
 
*/
1635
 
SQLITE_API int sqlite3_initialize(void);
1636
 
SQLITE_API int sqlite3_shutdown(void);
1637
 
SQLITE_API int sqlite3_os_init(void);
1638
 
SQLITE_API int sqlite3_os_end(void);
1639
 
 
1640
 
/*
1641
 
** CAPI3REF: Configuring The SQLite Library
1642
 
**
1643
 
** The sqlite3_config() interface is used to make global configuration
1644
 
** changes to SQLite in order to tune SQLite to the specific needs of
1645
 
** the application.  The default configuration is recommended for most
1646
 
** applications and so this routine is usually not necessary.  It is
1647
 
** provided to support rare applications with unusual needs.
1648
 
**
1649
 
** The sqlite3_config() interface is not threadsafe.  The application
1650
 
** must insure that no other SQLite interfaces are invoked by other
1651
 
** threads while sqlite3_config() is running.  Furthermore, sqlite3_config()
1652
 
** may only be invoked prior to library initialization using
1653
 
** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
1654
 
** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
1655
 
** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
1656
 
** Note, however, that ^sqlite3_config() can be called as part of the
1657
 
** implementation of an application-defined [sqlite3_os_init()].
1658
 
**
1659
 
** The first argument to sqlite3_config() is an integer
1660
 
** [SQLITE_CONFIG_SINGLETHREAD | configuration option] that determines
1661
 
** what property of SQLite is to be configured.  Subsequent arguments
1662
 
** vary depending on the [SQLITE_CONFIG_SINGLETHREAD | configuration option]
1663
 
** in the first argument.
1664
 
**
1665
 
** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
1666
 
** ^If the option is unknown or SQLite is unable to set the option
1667
 
** then this routine returns a non-zero [error code].
1668
 
*/
1669
 
SQLITE_API int sqlite3_config(int, ...);
1670
 
 
1671
 
/*
1672
 
** CAPI3REF: Configure database connections
1673
 
**
1674
 
** The sqlite3_db_config() interface is used to make configuration
1675
 
** changes to a [database connection].  The interface is similar to
1676
 
** [sqlite3_config()] except that the changes apply to a single
1677
 
** [database connection] (specified in the first argument).
1678
 
**
1679
 
** The second argument to sqlite3_db_config(D,V,...)  is the
1680
 
** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code 
1681
 
** that indicates what aspect of the [database connection] is being configured.
1682
 
** Subsequent arguments vary depending on the configuration verb.
1683
 
**
1684
 
** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
1685
 
** the call is considered successful.
1686
 
*/
1687
 
SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
1688
 
 
1689
 
/*
1690
 
** CAPI3REF: Memory Allocation Routines
1691
 
**
1692
 
** An instance of this object defines the interface between SQLite
1693
 
** and low-level memory allocation routines.
1694
 
**
1695
 
** This object is used in only one place in the SQLite interface.
1696
 
** A pointer to an instance of this object is the argument to
1697
 
** [sqlite3_config()] when the configuration option is
1698
 
** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].  
1699
 
** By creating an instance of this object
1700
 
** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
1701
 
** during configuration, an application can specify an alternative
1702
 
** memory allocation subsystem for SQLite to use for all of its
1703
 
** dynamic memory needs.
1704
 
**
1705
 
** Note that SQLite comes with several [built-in memory allocators]
1706
 
** that are perfectly adequate for the overwhelming majority of applications
1707
 
** and that this object is only useful to a tiny minority of applications
1708
 
** with specialized memory allocation requirements.  This object is
1709
 
** also used during testing of SQLite in order to specify an alternative
1710
 
** memory allocator that simulates memory out-of-memory conditions in
1711
 
** order to verify that SQLite recovers gracefully from such
1712
 
** conditions.
1713
 
**
1714
 
** The xMalloc and xFree methods must work like the
1715
 
** malloc() and free() functions from the standard C library.
1716
 
** The xRealloc method must work like realloc() from the standard C library
1717
 
** with the exception that if the second argument to xRealloc is zero,
1718
 
** xRealloc must be a no-op - it must not perform any allocation or
1719
 
** deallocation.  ^SQLite guarantees that the second argument to
1720
 
** xRealloc is always a value returned by a prior call to xRoundup.
1721
 
** And so in cases where xRoundup always returns a positive number,
1722
 
** xRealloc can perform exactly as the standard library realloc() and
1723
 
** still be in compliance with this specification.
1724
 
**
1725
 
** xSize should return the allocated size of a memory allocation
1726
 
** previously obtained from xMalloc or xRealloc.  The allocated size
1727
 
** is always at least as big as the requested size but may be larger.
1728
 
**
1729
 
** The xRoundup method returns what would be the allocated size of
1730
 
** a memory allocation given a particular requested size.  Most memory
1731
 
** allocators round up memory allocations at least to the next multiple
1732
 
** of 8.  Some allocators round up to a larger multiple or to a power of 2.
1733
 
** Every memory allocation request coming in through [sqlite3_malloc()]
1734
 
** or [sqlite3_realloc()] first calls xRoundup.  If xRoundup returns 0, 
1735
 
** that causes the corresponding memory allocation to fail.
1736
 
**
1737
 
** The xInit method initializes the memory allocator.  (For example,
1738
 
** it might allocate any require mutexes or initialize internal data
1739
 
** structures.  The xShutdown method is invoked (indirectly) by
1740
 
** [sqlite3_shutdown()] and should deallocate any resources acquired
1741
 
** by xInit.  The pAppData pointer is used as the only parameter to
1742
 
** xInit and xShutdown.
1743
 
**
1744
 
** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
1745
 
** the xInit method, so the xInit method need not be threadsafe.  The
1746
 
** xShutdown method is only called from [sqlite3_shutdown()] so it does
1747
 
** not need to be threadsafe either.  For all other methods, SQLite
1748
 
** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
1749
 
** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
1750
 
** it is by default) and so the methods are automatically serialized.
1751
 
** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
1752
 
** methods must be threadsafe or else make their own arrangements for
1753
 
** serialization.
1754
 
**
1755
 
** SQLite will never invoke xInit() more than once without an intervening
1756
 
** call to xShutdown().
1757
 
*/
1758
 
typedef struct sqlite3_mem_methods sqlite3_mem_methods;
1759
 
struct sqlite3_mem_methods {
1760
 
  void *(*xMalloc)(int);         /* Memory allocation function */
1761
 
  void (*xFree)(void*);          /* Free a prior allocation */
1762
 
  void *(*xRealloc)(void*,int);  /* Resize an allocation */
1763
 
  int (*xSize)(void*);           /* Return the size of an allocation */
1764
 
  int (*xRoundup)(int);          /* Round up request size to allocation size */
1765
 
  int (*xInit)(void*);           /* Initialize the memory allocator */
1766
 
  void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
1767
 
  void *pAppData;                /* Argument to xInit() and xShutdown() */
1768
 
};
1769
 
 
1770
 
/*
1771
 
** CAPI3REF: Configuration Options
1772
 
**
1773
 
** These constants are the available integer configuration options that
1774
 
** can be passed as the first argument to the [sqlite3_config()] interface.
1775
 
**
1776
 
** New configuration options may be added in future releases of SQLite.
1777
 
** Existing configuration options might be discontinued.  Applications
1778
 
** should check the return code from [sqlite3_config()] to make sure that
1779
 
** the call worked.  The [sqlite3_config()] interface will return a
1780
 
** non-zero [error code] if a discontinued or unsupported configuration option
1781
 
** is invoked.
1782
 
**
1783
 
** <dl>
1784
 
** <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
1785
 
** <dd>There are no arguments to this option.  ^This option sets the
1786
 
** [threading mode] to Single-thread.  In other words, it disables
1787
 
** all mutexing and puts SQLite into a mode where it can only be used
1788
 
** by a single thread.   ^If SQLite is compiled with
1789
 
** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1790
 
** it is not possible to change the [threading mode] from its default
1791
 
** value of Single-thread and so [sqlite3_config()] will return 
1792
 
** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
1793
 
** configuration option.</dd>
1794
 
**
1795
 
** <dt>SQLITE_CONFIG_MULTITHREAD</dt>
1796
 
** <dd>There are no arguments to this option.  ^This option sets the
1797
 
** [threading mode] to Multi-thread.  In other words, it disables
1798
 
** mutexing on [database connection] and [prepared statement] objects.
1799
 
** The application is responsible for serializing access to
1800
 
** [database connections] and [prepared statements].  But other mutexes
1801
 
** are enabled so that SQLite will be safe to use in a multi-threaded
1802
 
** environment as long as no two threads attempt to use the same
1803
 
** [database connection] at the same time.  ^If SQLite is compiled with
1804
 
** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1805
 
** it is not possible to set the Multi-thread [threading mode] and
1806
 
** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1807
 
** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
1808
 
**
1809
 
** <dt>SQLITE_CONFIG_SERIALIZED</dt>
1810
 
** <dd>There are no arguments to this option.  ^This option sets the
1811
 
** [threading mode] to Serialized. In other words, this option enables
1812
 
** all mutexes including the recursive
1813
 
** mutexes on [database connection] and [prepared statement] objects.
1814
 
** In this mode (which is the default when SQLite is compiled with
1815
 
** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
1816
 
** to [database connections] and [prepared statements] so that the
1817
 
** application is free to use the same [database connection] or the
1818
 
** same [prepared statement] in different threads at the same time.
1819
 
** ^If SQLite is compiled with
1820
 
** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1821
 
** it is not possible to set the Serialized [threading mode] and
1822
 
** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1823
 
** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
1824
 
**
1825
 
** <dt>SQLITE_CONFIG_MALLOC</dt>
1826
 
** <dd> ^(This option takes a single argument which is a pointer to an
1827
 
** instance of the [sqlite3_mem_methods] structure.  The argument specifies
1828
 
** alternative low-level memory allocation routines to be used in place of
1829
 
** the memory allocation routines built into SQLite.)^ ^SQLite makes
1830
 
** its own private copy of the content of the [sqlite3_mem_methods] structure
1831
 
** before the [sqlite3_config()] call returns.</dd>
1832
 
**
1833
 
** <dt>SQLITE_CONFIG_GETMALLOC</dt>
1834
 
** <dd> ^(This option takes a single argument which is a pointer to an
1835
 
** instance of the [sqlite3_mem_methods] structure.  The [sqlite3_mem_methods]
1836
 
** structure is filled with the currently defined memory allocation routines.)^
1837
 
** This option can be used to overload the default memory allocation
1838
 
** routines with a wrapper that simulations memory allocation failure or
1839
 
** tracks memory usage, for example. </dd>
1840
 
**
1841
 
** <dt>SQLITE_CONFIG_MEMSTATUS</dt>
1842
 
** <dd> ^This option takes single argument of type int, interpreted as a 
1843
 
** boolean, which enables or disables the collection of memory allocation 
1844
 
** statistics. ^(When memory allocation statistics are disabled, the 
1845
 
** following SQLite interfaces become non-operational:
1846
 
**   <ul>
1847
 
**   <li> [sqlite3_memory_used()]
1848
 
**   <li> [sqlite3_memory_highwater()]
1849
 
**   <li> [sqlite3_soft_heap_limit64()]
1850
 
**   <li> [sqlite3_status()]
1851
 
**   </ul>)^
1852
 
** ^Memory allocation statistics are enabled by default unless SQLite is
1853
 
** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
1854
 
** allocation statistics are disabled by default.
1855
 
** </dd>
1856
 
**
1857
 
** <dt>SQLITE_CONFIG_SCRATCH</dt>
1858
 
** <dd> ^This option specifies a static memory buffer that SQLite can use for
1859
 
** scratch memory.  There are three arguments:  A pointer an 8-byte
1860
 
** aligned memory buffer from which the scratch allocations will be
1861
 
** drawn, the size of each scratch allocation (sz),
1862
 
** and the maximum number of scratch allocations (N).  The sz
1863
 
** argument must be a multiple of 16.
1864
 
** The first argument must be a pointer to an 8-byte aligned buffer
1865
 
** of at least sz*N bytes of memory.
1866
 
** ^SQLite will use no more than two scratch buffers per thread.  So
1867
 
** N should be set to twice the expected maximum number of threads.
1868
 
** ^SQLite will never require a scratch buffer that is more than 6
1869
 
** times the database page size. ^If SQLite needs needs additional
1870
 
** scratch memory beyond what is provided by this configuration option, then 
1871
 
** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
1872
 
**
1873
 
** <dt>SQLITE_CONFIG_PAGECACHE</dt>
1874
 
** <dd> ^This option specifies a static memory buffer that SQLite can use for
1875
 
** the database page cache with the default page cache implemenation.  
1876
 
** This configuration should not be used if an application-define page
1877
 
** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1878
 
** There are three arguments to this option: A pointer to 8-byte aligned
1879
 
** memory, the size of each page buffer (sz), and the number of pages (N).
1880
 
** The sz argument should be the size of the largest database page
1881
 
** (a power of two between 512 and 32768) plus a little extra for each
1882
 
** page header.  ^The page header size is 20 to 40 bytes depending on
1883
 
** the host architecture.  ^It is harmless, apart from the wasted memory,
1884
 
** to make sz a little too large.  The first
1885
 
** argument should point to an allocation of at least sz*N bytes of memory.
1886
 
** ^SQLite will use the memory provided by the first argument to satisfy its
1887
 
** memory needs for the first N pages that it adds to cache.  ^If additional
1888
 
** page cache memory is needed beyond what is provided by this option, then
1889
 
** SQLite goes to [sqlite3_malloc()] for the additional storage space.
1890
 
** The pointer in the first argument must
1891
 
** be aligned to an 8-byte boundary or subsequent behavior of SQLite
1892
 
** will be undefined.</dd>
1893
 
**
1894
 
** <dt>SQLITE_CONFIG_HEAP</dt>
1895
 
** <dd> ^This option specifies a static memory buffer that SQLite will use
1896
 
** for all of its dynamic memory allocation needs beyond those provided
1897
 
** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1898
 
** There are three arguments: An 8-byte aligned pointer to the memory,
1899
 
** the number of bytes in the memory buffer, and the minimum allocation size.
1900
 
** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
1901
 
** to using its default memory allocator (the system malloc() implementation),
1902
 
** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
1903
 
** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
1904
 
** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
1905
 
** allocator is engaged to handle all of SQLites memory allocation needs.
1906
 
** The first pointer (the memory pointer) must be aligned to an 8-byte
1907
 
** boundary or subsequent behavior of SQLite will be undefined.
1908
 
** The minimum allocation size is capped at 2^12. Reasonable values
1909
 
** for the minimum allocation size are 2^5 through 2^8.</dd>
1910
 
**
1911
 
** <dt>SQLITE_CONFIG_MUTEX</dt>
1912
 
** <dd> ^(This option takes a single argument which is a pointer to an
1913
 
** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
1914
 
** alternative low-level mutex routines to be used in place
1915
 
** the mutex routines built into SQLite.)^  ^SQLite makes a copy of the
1916
 
** content of the [sqlite3_mutex_methods] structure before the call to
1917
 
** [sqlite3_config()] returns. ^If SQLite is compiled with
1918
 
** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1919
 
** the entire mutexing subsystem is omitted from the build and hence calls to
1920
 
** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
1921
 
** return [SQLITE_ERROR].</dd>
1922
 
**
1923
 
** <dt>SQLITE_CONFIG_GETMUTEX</dt>
1924
 
** <dd> ^(This option takes a single argument which is a pointer to an
1925
 
** instance of the [sqlite3_mutex_methods] structure.  The
1926
 
** [sqlite3_mutex_methods]
1927
 
** structure is filled with the currently defined mutex routines.)^
1928
 
** This option can be used to overload the default mutex allocation
1929
 
** routines with a wrapper used to track mutex usage for performance
1930
 
** profiling or testing, for example.   ^If SQLite is compiled with
1931
 
** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1932
 
** the entire mutexing subsystem is omitted from the build and hence calls to
1933
 
** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
1934
 
** return [SQLITE_ERROR].</dd>
1935
 
**
1936
 
** <dt>SQLITE_CONFIG_LOOKASIDE</dt>
1937
 
** <dd> ^(This option takes two arguments that determine the default
1938
 
** memory allocation for the lookaside memory allocator on each
1939
 
** [database connection].  The first argument is the
1940
 
** size of each lookaside buffer slot and the second is the number of
1941
 
** slots allocated to each database connection.)^  ^(This option sets the
1942
 
** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1943
 
** verb to [sqlite3_db_config()] can be used to change the lookaside
1944
 
** configuration on individual connections.)^ </dd>
1945
 
**
1946
 
** <dt>SQLITE_CONFIG_PCACHE</dt>
1947
 
** <dd> ^(This option takes a single argument which is a pointer to
1948
 
** an [sqlite3_pcache_methods] object.  This object specifies the interface
1949
 
** to a custom page cache implementation.)^  ^SQLite makes a copy of the
1950
 
** object and uses it for page cache memory allocations.</dd>
1951
 
**
1952
 
** <dt>SQLITE_CONFIG_GETPCACHE</dt>
1953
 
** <dd> ^(This option takes a single argument which is a pointer to an
1954
 
** [sqlite3_pcache_methods] object.  SQLite copies of the current
1955
 
** page cache implementation into that object.)^ </dd>
1956
 
**
1957
 
** <dt>SQLITE_CONFIG_LOG</dt>
1958
 
** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
1959
 
** function with a call signature of void(*)(void*,int,const char*), 
1960
 
** and a pointer to void. ^If the function pointer is not NULL, it is
1961
 
** invoked by [sqlite3_log()] to process each logging event.  ^If the
1962
 
** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
1963
 
** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
1964
 
** passed through as the first parameter to the application-defined logger
1965
 
** function whenever that function is invoked.  ^The second parameter to
1966
 
** the logger function is a copy of the first parameter to the corresponding
1967
 
** [sqlite3_log()] call and is intended to be a [result code] or an
1968
 
** [extended result code].  ^The third parameter passed to the logger is
1969
 
** log message after formatting via [sqlite3_snprintf()].
1970
 
** The SQLite logging interface is not reentrant; the logger function
1971
 
** supplied by the application must not invoke any SQLite interface.
1972
 
** In a multi-threaded application, the application-defined logger
1973
 
** function must be threadsafe. </dd>
1974
 
**
1975
 
** </dl>
1976
 
*/
1977
 
#define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
1978
 
#define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
1979
 
#define SQLITE_CONFIG_SERIALIZED    3  /* nil */
1980
 
#define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
1981
 
#define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
1982
 
#define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
1983
 
#define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
1984
 
#define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
1985
 
#define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
1986
 
#define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
1987
 
#define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
1988
 
/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ 
1989
 
#define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
1990
 
#define SQLITE_CONFIG_PCACHE       14  /* sqlite3_pcache_methods* */
1991
 
#define SQLITE_CONFIG_GETPCACHE    15  /* sqlite3_pcache_methods* */
1992
 
#define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
1993
 
 
1994
 
/*
1995
 
** CAPI3REF: Database Connection Configuration Options
1996
 
**
1997
 
** These constants are the available integer configuration options that
1998
 
** can be passed as the second argument to the [sqlite3_db_config()] interface.
1999
 
**
2000
 
** New configuration options may be added in future releases of SQLite.
2001
 
** Existing configuration options might be discontinued.  Applications
2002
 
** should check the return code from [sqlite3_db_config()] to make sure that
2003
 
** the call worked.  ^The [sqlite3_db_config()] interface will return a
2004
 
** non-zero [error code] if a discontinued or unsupported configuration option
2005
 
** is invoked.
2006
 
**
2007
 
** <dl>
2008
 
** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
2009
 
** <dd> ^This option takes three additional arguments that determine the 
2010
 
** [lookaside memory allocator] configuration for the [database connection].
2011
 
** ^The first argument (the third parameter to [sqlite3_db_config()] is a
2012
 
** pointer to a memory buffer to use for lookaside memory.
2013
 
** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
2014
 
** may be NULL in which case SQLite will allocate the
2015
 
** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
2016
 
** size of each lookaside buffer slot.  ^The third argument is the number of
2017
 
** slots.  The size of the buffer in the first argument must be greater than
2018
 
** or equal to the product of the second and third arguments.  The buffer
2019
 
** must be aligned to an 8-byte boundary.  ^If the second argument to
2020
 
** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
2021
 
** rounded down to the next smaller multiple of 8.  ^(The lookaside memory
2022
 
** configuration for a database connection can only be changed when that
2023
 
** connection is not currently using lookaside memory, or in other words
2024
 
** when the "current value" returned by
2025
 
** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
2026
 
** Any attempt to change the lookaside memory configuration when lookaside
2027
 
** memory is in use leaves the configuration unchanged and returns 
2028
 
** [SQLITE_BUSY].)^</dd>
2029
 
**
2030
 
** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
2031
 
** <dd> ^This option is used to enable or disable the enforcement of
2032
 
** [foreign key constraints].  There should be two additional arguments.
2033
 
** The first argument is an integer which is 0 to disable FK enforcement,
2034
 
** positive to enable FK enforcement or negative to leave FK enforcement
2035
 
** unchanged.  The second parameter is a pointer to an integer into which
2036
 
** is written 0 or 1 to indicate whether FK enforcement is off or on
2037
 
** following this call.  The second parameter may be a NULL pointer, in
2038
 
** which case the FK enforcement setting is not reported back. </dd>
2039
 
**
2040
 
** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
2041
 
** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
2042
 
** There should be two additional arguments.
2043
 
** The first argument is an integer which is 0 to disable triggers,
2044
 
** positive to enable triggers or negative to leave the setting unchanged.
2045
 
** The second parameter is a pointer to an integer into which
2046
 
** is written 0 or 1 to indicate whether triggers are disabled or enabled
2047
 
** following this call.  The second parameter may be a NULL pointer, in
2048
 
** which case the trigger setting is not reported back. </dd>
2049
 
**
2050
 
** </dl>
2051
 
*/
2052
 
#define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
2053
 
#define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
2054
 
#define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
2055
 
 
2056
 
 
2057
 
/*
2058
 
** CAPI3REF: Enable Or Disable Extended Result Codes
2059
 
**
2060
 
** ^The sqlite3_extended_result_codes() routine enables or disables the
2061
 
** [extended result codes] feature of SQLite. ^The extended result
2062
 
** codes are disabled by default for historical compatibility.
2063
 
*/
2064
 
SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
2065
 
 
2066
 
/*
2067
 
** CAPI3REF: Last Insert Rowid
2068
 
**
2069
 
** ^Each entry in an SQLite table has a unique 64-bit signed
2070
 
** integer key called the [ROWID | "rowid"]. ^The rowid is always available
2071
 
** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
2072
 
** names are not also used by explicitly declared columns. ^If
2073
 
** the table has a column of type [INTEGER PRIMARY KEY] then that column
2074
 
** is another alias for the rowid.
2075
 
**
2076
 
** ^This routine returns the [rowid] of the most recent
2077
 
** successful [INSERT] into the database from the [database connection]
2078
 
** in the first argument.  ^If no successful [INSERT]s
2079
 
** have ever occurred on that database connection, zero is returned.
2080
 
**
2081
 
** ^(If an [INSERT] occurs within a trigger, then the [rowid] of the inserted
2082
 
** row is returned by this routine as long as the trigger is running.
2083
 
** But once the trigger terminates, the value returned by this routine
2084
 
** reverts to the last value inserted before the trigger fired.)^
2085
 
**
2086
 
** ^An [INSERT] that fails due to a constraint violation is not a
2087
 
** successful [INSERT] and does not change the value returned by this
2088
 
** routine.  ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
2089
 
** and INSERT OR ABORT make no changes to the return value of this
2090
 
** routine when their insertion fails.  ^(When INSERT OR REPLACE
2091
 
** encounters a constraint violation, it does not fail.  The
2092
 
** INSERT continues to completion after deleting rows that caused
2093
 
** the constraint problem so INSERT OR REPLACE will always change
2094
 
** the return value of this interface.)^
2095
 
**
2096
 
** ^For the purposes of this routine, an [INSERT] is considered to
2097
 
** be successful even if it is subsequently rolled back.
2098
 
**
2099
 
** This function is accessible to SQL statements via the
2100
 
** [last_insert_rowid() SQL function].
2101
 
**
2102
 
** If a separate thread performs a new [INSERT] on the same
2103
 
** database connection while the [sqlite3_last_insert_rowid()]
2104
 
** function is running and thus changes the last insert [rowid],
2105
 
** then the value returned by [sqlite3_last_insert_rowid()] is
2106
 
** unpredictable and might not equal either the old or the new
2107
 
** last insert [rowid].
2108
 
*/
2109
 
SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
2110
 
 
2111
 
/*
2112
 
** CAPI3REF: Count The Number Of Rows Modified
2113
 
**
2114
 
** ^This function returns the number of database rows that were changed
2115
 
** or inserted or deleted by the most recently completed SQL statement
2116
 
** on the [database connection] specified by the first parameter.
2117
 
** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
2118
 
** or [DELETE] statement are counted.  Auxiliary changes caused by
2119
 
** triggers or [foreign key actions] are not counted.)^ Use the
2120
 
** [sqlite3_total_changes()] function to find the total number of changes
2121
 
** including changes caused by triggers and foreign key actions.
2122
 
**
2123
 
** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
2124
 
** are not counted.  Only real table changes are counted.
2125
 
**
2126
 
** ^(A "row change" is a change to a single row of a single table
2127
 
** caused by an INSERT, DELETE, or UPDATE statement.  Rows that
2128
 
** are changed as side effects of [REPLACE] constraint resolution,
2129
 
** rollback, ABORT processing, [DROP TABLE], or by any other
2130
 
** mechanisms do not count as direct row changes.)^
2131
 
**
2132
 
** A "trigger context" is a scope of execution that begins and
2133
 
** ends with the script of a [CREATE TRIGGER | trigger]. 
2134
 
** Most SQL statements are
2135
 
** evaluated outside of any trigger.  This is the "top level"
2136
 
** trigger context.  If a trigger fires from the top level, a
2137
 
** new trigger context is entered for the duration of that one
2138
 
** trigger.  Subtriggers create subcontexts for their duration.
2139
 
**
2140
 
** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
2141
 
** not create a new trigger context.
2142
 
**
2143
 
** ^This function returns the number of direct row changes in the
2144
 
** most recent INSERT, UPDATE, or DELETE statement within the same
2145
 
** trigger context.
2146
 
**
2147
 
** ^Thus, when called from the top level, this function returns the
2148
 
** number of changes in the most recent INSERT, UPDATE, or DELETE
2149
 
** that also occurred at the top level.  ^(Within the body of a trigger,
2150
 
** the sqlite3_changes() interface can be called to find the number of
2151
 
** changes in the most recently completed INSERT, UPDATE, or DELETE
2152
 
** statement within the body of the same trigger.
2153
 
** However, the number returned does not include changes
2154
 
** caused by subtriggers since those have their own context.)^
2155
 
**
2156
 
** See also the [sqlite3_total_changes()] interface, the
2157
 
** [count_changes pragma], and the [changes() SQL function].
2158
 
**
2159
 
** If a separate thread makes changes on the same database connection
2160
 
** while [sqlite3_changes()] is running then the value returned
2161
 
** is unpredictable and not meaningful.
2162
 
*/
2163
 
SQLITE_API int sqlite3_changes(sqlite3*);
2164
 
 
2165
 
/*
2166
 
** CAPI3REF: Total Number Of Rows Modified
2167
 
**
2168
 
** ^This function returns the number of row changes caused by [INSERT],
2169
 
** [UPDATE] or [DELETE] statements since the [database connection] was opened.
2170
 
** ^(The count returned by sqlite3_total_changes() includes all changes
2171
 
** from all [CREATE TRIGGER | trigger] contexts and changes made by
2172
 
** [foreign key actions]. However,
2173
 
** the count does not include changes used to implement [REPLACE] constraints,
2174
 
** do rollbacks or ABORT processing, or [DROP TABLE] processing.  The
2175
 
** count does not include rows of views that fire an [INSTEAD OF trigger],
2176
 
** though if the INSTEAD OF trigger makes changes of its own, those changes 
2177
 
** are counted.)^
2178
 
** ^The sqlite3_total_changes() function counts the changes as soon as
2179
 
** the statement that makes them is completed (when the statement handle
2180
 
** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
2181
 
**
2182
 
** See also the [sqlite3_changes()] interface, the
2183
 
** [count_changes pragma], and the [total_changes() SQL function].
2184
 
**
2185
 
** If a separate thread makes changes on the same database connection
2186
 
** while [sqlite3_total_changes()] is running then the value
2187
 
** returned is unpredictable and not meaningful.
2188
 
*/
2189
 
SQLITE_API int sqlite3_total_changes(sqlite3*);
2190
 
 
2191
 
/*
2192
 
** CAPI3REF: Interrupt A Long-Running Query
2193
 
**
2194
 
** ^This function causes any pending database operation to abort and
2195
 
** return at its earliest opportunity. This routine is typically
2196
 
** called in response to a user action such as pressing "Cancel"
2197
 
** or Ctrl-C where the user wants a long query operation to halt
2198
 
** immediately.
2199
 
**
2200
 
** ^It is safe to call this routine from a thread different from the
2201
 
** thread that is currently running the database operation.  But it
2202
 
** is not safe to call this routine with a [database connection] that
2203
 
** is closed or might close before sqlite3_interrupt() returns.
2204
 
**
2205
 
** ^If an SQL operation is very nearly finished at the time when
2206
 
** sqlite3_interrupt() is called, then it might not have an opportunity
2207
 
** to be interrupted and might continue to completion.
2208
 
**
2209
 
** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
2210
 
** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
2211
 
** that is inside an explicit transaction, then the entire transaction
2212
 
** will be rolled back automatically.
2213
 
**
2214
 
** ^The sqlite3_interrupt(D) call is in effect until all currently running
2215
 
** SQL statements on [database connection] D complete.  ^Any new SQL statements
2216
 
** that are started after the sqlite3_interrupt() call and before the 
2217
 
** running statements reaches zero are interrupted as if they had been
2218
 
** running prior to the sqlite3_interrupt() call.  ^New SQL statements
2219
 
** that are started after the running statement count reaches zero are
2220
 
** not effected by the sqlite3_interrupt().
2221
 
** ^A call to sqlite3_interrupt(D) that occurs when there are no running
2222
 
** SQL statements is a no-op and has no effect on SQL statements
2223
 
** that are started after the sqlite3_interrupt() call returns.
2224
 
**
2225
 
** If the database connection closes while [sqlite3_interrupt()]
2226
 
** is running then bad things will likely happen.
2227
 
*/
2228
 
SQLITE_API void sqlite3_interrupt(sqlite3*);
2229
 
 
2230
 
/*
2231
 
** CAPI3REF: Determine If An SQL Statement Is Complete
2232
 
**
2233
 
** These routines are useful during command-line input to determine if the
2234
 
** currently entered text seems to form a complete SQL statement or
2235
 
** if additional input is needed before sending the text into
2236
 
** SQLite for parsing.  ^These routines return 1 if the input string
2237
 
** appears to be a complete SQL statement.  ^A statement is judged to be
2238
 
** complete if it ends with a semicolon token and is not a prefix of a
2239
 
** well-formed CREATE TRIGGER statement.  ^Semicolons that are embedded within
2240
 
** string literals or quoted identifier names or comments are not
2241
 
** independent tokens (they are part of the token in which they are
2242
 
** embedded) and thus do not count as a statement terminator.  ^Whitespace
2243
 
** and comments that follow the final semicolon are ignored.
2244
 
**
2245
 
** ^These routines return 0 if the statement is incomplete.  ^If a
2246
 
** memory allocation fails, then SQLITE_NOMEM is returned.
2247
 
**
2248
 
** ^These routines do not parse the SQL statements thus
2249
 
** will not detect syntactically incorrect SQL.
2250
 
**
2251
 
** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior 
2252
 
** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
2253
 
** automatically by sqlite3_complete16().  If that initialization fails,
2254
 
** then the return value from sqlite3_complete16() will be non-zero
2255
 
** regardless of whether or not the input SQL is complete.)^
2256
 
**
2257
 
** The input to [sqlite3_complete()] must be a zero-terminated
2258
 
** UTF-8 string.
2259
 
**
2260
 
** The input to [sqlite3_complete16()] must be a zero-terminated
2261
 
** UTF-16 string in native byte order.
2262
 
*/
2263
 
SQLITE_API int sqlite3_complete(const char *sql);
2264
 
SQLITE_API int sqlite3_complete16(const void *sql);
2265
 
 
2266
 
/*
2267
 
** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
2268
 
**
2269
 
** ^This routine sets a callback function that might be invoked whenever
2270
 
** an attempt is made to open a database table that another thread
2271
 
** or process has locked.
2272
 
**
2273
 
** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
2274
 
** is returned immediately upon encountering the lock.  ^If the busy callback
2275
 
** is not NULL, then the callback might be invoked with two arguments.
2276
 
**
2277
 
** ^The first argument to the busy handler is a copy of the void* pointer which
2278
 
** is the third argument to sqlite3_busy_handler().  ^The second argument to
2279
 
** the busy handler callback is the number of times that the busy handler has
2280
 
** been invoked for this locking event.  ^If the
2281
 
** busy callback returns 0, then no additional attempts are made to
2282
 
** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
2283
 
** ^If the callback returns non-zero, then another attempt
2284
 
** is made to open the database for reading and the cycle repeats.
2285
 
**
2286
 
** The presence of a busy handler does not guarantee that it will be invoked
2287
 
** when there is lock contention. ^If SQLite determines that invoking the busy
2288
 
** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
2289
 
** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
2290
 
** Consider a scenario where one process is holding a read lock that
2291
 
** it is trying to promote to a reserved lock and
2292
 
** a second process is holding a reserved lock that it is trying
2293
 
** to promote to an exclusive lock.  The first process cannot proceed
2294
 
** because it is blocked by the second and the second process cannot
2295
 
** proceed because it is blocked by the first.  If both processes
2296
 
** invoke the busy handlers, neither will make any progress.  Therefore,
2297
 
** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
2298
 
** will induce the first process to release its read lock and allow
2299
 
** the second process to proceed.
2300
 
**
2301
 
** ^The default busy callback is NULL.
2302
 
**
2303
 
** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
2304
 
** when SQLite is in the middle of a large transaction where all the
2305
 
** changes will not fit into the in-memory cache.  SQLite will
2306
 
** already hold a RESERVED lock on the database file, but it needs
2307
 
** to promote this lock to EXCLUSIVE so that it can spill cache
2308
 
** pages into the database file without harm to concurrent
2309
 
** readers.  ^If it is unable to promote the lock, then the in-memory
2310
 
** cache will be left in an inconsistent state and so the error
2311
 
** code is promoted from the relatively benign [SQLITE_BUSY] to
2312
 
** the more severe [SQLITE_IOERR_BLOCKED].  ^This error code promotion
2313
 
** forces an automatic rollback of the changes.  See the
2314
 
** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
2315
 
** CorruptionFollowingBusyError</a> wiki page for a discussion of why
2316
 
** this is important.
2317
 
**
2318
 
** ^(There can only be a single busy handler defined for each
2319
 
** [database connection].  Setting a new busy handler clears any
2320
 
** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
2321
 
** will also set or clear the busy handler.
2322
 
**
2323
 
** The busy callback should not take any actions which modify the
2324
 
** database connection that invoked the busy handler.  Any such actions
2325
 
** result in undefined behavior.
2326
 
** 
2327
 
** A busy handler must not close the database connection
2328
 
** or [prepared statement] that invoked the busy handler.
2329
 
*/
2330
 
SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
2331
 
 
2332
 
/*
2333
 
** CAPI3REF: Set A Busy Timeout
2334
 
**
2335
 
** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
2336
 
** for a specified amount of time when a table is locked.  ^The handler
2337
 
** will sleep multiple times until at least "ms" milliseconds of sleeping
2338
 
** have accumulated.  ^After at least "ms" milliseconds of sleeping,
2339
 
** the handler returns 0 which causes [sqlite3_step()] to return
2340
 
** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
2341
 
**
2342
 
** ^Calling this routine with an argument less than or equal to zero
2343
 
** turns off all busy handlers.
2344
 
**
2345
 
** ^(There can only be a single busy handler for a particular
2346
 
** [database connection] any any given moment.  If another busy handler
2347
 
** was defined  (using [sqlite3_busy_handler()]) prior to calling
2348
 
** this routine, that other busy handler is cleared.)^
2349
 
*/
2350
 
SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
2351
 
 
2352
 
/*
2353
 
** CAPI3REF: Convenience Routines For Running Queries
2354
 
**
2355
 
** This is a legacy interface that is preserved for backwards compatibility.
2356
 
** Use of this interface is not recommended.
2357
 
**
2358
 
** Definition: A <b>result table</b> is memory data structure created by the
2359
 
** [sqlite3_get_table()] interface.  A result table records the
2360
 
** complete query results from one or more queries.
2361
 
**
2362
 
** The table conceptually has a number of rows and columns.  But
2363
 
** these numbers are not part of the result table itself.  These
2364
 
** numbers are obtained separately.  Let N be the number of rows
2365
 
** and M be the number of columns.
2366
 
**
2367
 
** A result table is an array of pointers to zero-terminated UTF-8 strings.
2368
 
** There are (N+1)*M elements in the array.  The first M pointers point
2369
 
** to zero-terminated strings that  contain the names of the columns.
2370
 
** The remaining entries all point to query results.  NULL values result
2371
 
** in NULL pointers.  All other values are in their UTF-8 zero-terminated
2372
 
** string representation as returned by [sqlite3_column_text()].
2373
 
**
2374
 
** A result table might consist of one or more memory allocations.
2375
 
** It is not safe to pass a result table directly to [sqlite3_free()].
2376
 
** A result table should be deallocated using [sqlite3_free_table()].
2377
 
**
2378
 
** ^(As an example of the result table format, suppose a query result
2379
 
** is as follows:
2380
 
**
2381
 
** <blockquote><pre>
2382
 
**        Name        | Age
2383
 
**        -----------------------
2384
 
**        Alice       | 43
2385
 
**        Bob         | 28
2386
 
**        Cindy       | 21
2387
 
** </pre></blockquote>
2388
 
**
2389
 
** There are two column (M==2) and three rows (N==3).  Thus the
2390
 
** result table has 8 entries.  Suppose the result table is stored
2391
 
** in an array names azResult.  Then azResult holds this content:
2392
 
**
2393
 
** <blockquote><pre>
2394
 
**        azResult&#91;0] = "Name";
2395
 
**        azResult&#91;1] = "Age";
2396
 
**        azResult&#91;2] = "Alice";
2397
 
**        azResult&#91;3] = "43";
2398
 
**        azResult&#91;4] = "Bob";
2399
 
**        azResult&#91;5] = "28";
2400
 
**        azResult&#91;6] = "Cindy";
2401
 
**        azResult&#91;7] = "21";
2402
 
** </pre></blockquote>)^
2403
 
**
2404
 
** ^The sqlite3_get_table() function evaluates one or more
2405
 
** semicolon-separated SQL statements in the zero-terminated UTF-8
2406
 
** string of its 2nd parameter and returns a result table to the
2407
 
** pointer given in its 3rd parameter.
2408
 
**
2409
 
** After the application has finished with the result from sqlite3_get_table(),
2410
 
** it must pass the result table pointer to sqlite3_free_table() in order to
2411
 
** release the memory that was malloced.  Because of the way the
2412
 
** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
2413
 
** function must not try to call [sqlite3_free()] directly.  Only
2414
 
** [sqlite3_free_table()] is able to release the memory properly and safely.
2415
 
**
2416
 
** The sqlite3_get_table() interface is implemented as a wrapper around
2417
 
** [sqlite3_exec()].  The sqlite3_get_table() routine does not have access
2418
 
** to any internal data structures of SQLite.  It uses only the public
2419
 
** interface defined here.  As a consequence, errors that occur in the
2420
 
** wrapper layer outside of the internal [sqlite3_exec()] call are not
2421
 
** reflected in subsequent calls to [sqlite3_errcode()] or
2422
 
** [sqlite3_errmsg()].
2423
 
*/
2424
 
SQLITE_API int sqlite3_get_table(
2425
 
  sqlite3 *db,          /* An open database */
2426
 
  const char *zSql,     /* SQL to be evaluated */
2427
 
  char ***pazResult,    /* Results of the query */
2428
 
  int *pnRow,           /* Number of result rows written here */
2429
 
  int *pnColumn,        /* Number of result columns written here */
2430
 
  char **pzErrmsg       /* Error msg written here */
2431
 
);
2432
 
SQLITE_API void sqlite3_free_table(char **result);
2433
 
 
2434
 
/*
2435
 
** CAPI3REF: Formatted String Printing Functions
2436
 
**
2437
 
** These routines are work-alikes of the "printf()" family of functions
2438
 
** from the standard C library.
2439
 
**
2440
 
** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
2441
 
** results into memory obtained from [sqlite3_malloc()].
2442
 
** The strings returned by these two routines should be
2443
 
** released by [sqlite3_free()].  ^Both routines return a
2444
 
** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
2445
 
** memory to hold the resulting string.
2446
 
**
2447
 
** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
2448
 
** the standard C library.  The result is written into the
2449
 
** buffer supplied as the second parameter whose size is given by
2450
 
** the first parameter. Note that the order of the
2451
 
** first two parameters is reversed from snprintf().)^  This is an
2452
 
** historical accident that cannot be fixed without breaking
2453
 
** backwards compatibility.  ^(Note also that sqlite3_snprintf()
2454
 
** returns a pointer to its buffer instead of the number of
2455
 
** characters actually written into the buffer.)^  We admit that
2456
 
** the number of characters written would be a more useful return
2457
 
** value but we cannot change the implementation of sqlite3_snprintf()
2458
 
** now without breaking compatibility.
2459
 
**
2460
 
** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
2461
 
** guarantees that the buffer is always zero-terminated.  ^The first
2462
 
** parameter "n" is the total size of the buffer, including space for
2463
 
** the zero terminator.  So the longest string that can be completely
2464
 
** written will be n-1 characters.
2465
 
**
2466
 
** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
2467
 
**
2468
 
** These routines all implement some additional formatting
2469
 
** options that are useful for constructing SQL statements.
2470
 
** All of the usual printf() formatting options apply.  In addition, there
2471
 
** is are "%q", "%Q", and "%z" options.
2472
 
**
2473
 
** ^(The %q option works like %s in that it substitutes a null-terminated
2474
 
** string from the argument list.  But %q also doubles every '\'' character.
2475
 
** %q is designed for use inside a string literal.)^  By doubling each '\''
2476
 
** character it escapes that character and allows it to be inserted into
2477
 
** the string.
2478
 
**
2479
 
** For example, assume the string variable zText contains text as follows:
2480
 
**
2481
 
** <blockquote><pre>
2482
 
**  char *zText = "It's a happy day!";
2483
 
** </pre></blockquote>
2484
 
**
2485
 
** One can use this text in an SQL statement as follows:
2486
 
**
2487
 
** <blockquote><pre>
2488
 
**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
2489
 
**  sqlite3_exec(db, zSQL, 0, 0, 0);
2490
 
**  sqlite3_free(zSQL);
2491
 
** </pre></blockquote>
2492
 
**
2493
 
** Because the %q format string is used, the '\'' character in zText
2494
 
** is escaped and the SQL generated is as follows:
2495
 
**
2496
 
** <blockquote><pre>
2497
 
**  INSERT INTO table1 VALUES('It''s a happy day!')
2498
 
** </pre></blockquote>
2499
 
**
2500
 
** This is correct.  Had we used %s instead of %q, the generated SQL
2501
 
** would have looked like this:
2502
 
**
2503
 
** <blockquote><pre>
2504
 
**  INSERT INTO table1 VALUES('It's a happy day!');
2505
 
** </pre></blockquote>
2506
 
**
2507
 
** This second example is an SQL syntax error.  As a general rule you should
2508
 
** always use %q instead of %s when inserting text into a string literal.
2509
 
**
2510
 
** ^(The %Q option works like %q except it also adds single quotes around
2511
 
** the outside of the total string.  Additionally, if the parameter in the
2512
 
** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
2513
 
** single quotes).)^  So, for example, one could say:
2514
 
**
2515
 
** <blockquote><pre>
2516
 
**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
2517
 
**  sqlite3_exec(db, zSQL, 0, 0, 0);
2518
 
**  sqlite3_free(zSQL);
2519
 
** </pre></blockquote>
2520
 
**
2521
 
** The code above will render a correct SQL statement in the zSQL
2522
 
** variable even if the zText variable is a NULL pointer.
2523
 
**
2524
 
** ^(The "%z" formatting option works like "%s" but with the
2525
 
** addition that after the string has been read and copied into
2526
 
** the result, [sqlite3_free()] is called on the input string.)^
2527
 
*/
2528
 
SQLITE_API char *sqlite3_mprintf(const char*,...);
2529
 
SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
2530
 
SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
2531
 
SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
2532
 
 
2533
 
/*
2534
 
** CAPI3REF: Memory Allocation Subsystem
2535
 
**
2536
 
** The SQLite core uses these three routines for all of its own
2537
 
** internal memory allocation needs. "Core" in the previous sentence
2538
 
** does not include operating-system specific VFS implementation.  The
2539
 
** Windows VFS uses native malloc() and free() for some operations.
2540
 
**
2541
 
** ^The sqlite3_malloc() routine returns a pointer to a block
2542
 
** of memory at least N bytes in length, where N is the parameter.
2543
 
** ^If sqlite3_malloc() is unable to obtain sufficient free
2544
 
** memory, it returns a NULL pointer.  ^If the parameter N to
2545
 
** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
2546
 
** a NULL pointer.
2547
 
**
2548
 
** ^Calling sqlite3_free() with a pointer previously returned
2549
 
** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
2550
 
** that it might be reused.  ^The sqlite3_free() routine is
2551
 
** a no-op if is called with a NULL pointer.  Passing a NULL pointer
2552
 
** to sqlite3_free() is harmless.  After being freed, memory
2553
 
** should neither be read nor written.  Even reading previously freed
2554
 
** memory might result in a segmentation fault or other severe error.
2555
 
** Memory corruption, a segmentation fault, or other severe error
2556
 
** might result if sqlite3_free() is called with a non-NULL pointer that
2557
 
** was not obtained from sqlite3_malloc() or sqlite3_realloc().
2558
 
**
2559
 
** ^(The sqlite3_realloc() interface attempts to resize a
2560
 
** prior memory allocation to be at least N bytes, where N is the
2561
 
** second parameter.  The memory allocation to be resized is the first
2562
 
** parameter.)^ ^ If the first parameter to sqlite3_realloc()
2563
 
** is a NULL pointer then its behavior is identical to calling
2564
 
** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
2565
 
** ^If the second parameter to sqlite3_realloc() is zero or
2566
 
** negative then the behavior is exactly the same as calling
2567
 
** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
2568
 
** ^sqlite3_realloc() returns a pointer to a memory allocation
2569
 
** of at least N bytes in size or NULL if sufficient memory is unavailable.
2570
 
** ^If M is the size of the prior allocation, then min(N,M) bytes
2571
 
** of the prior allocation are copied into the beginning of buffer returned
2572
 
** by sqlite3_realloc() and the prior allocation is freed.
2573
 
** ^If sqlite3_realloc() returns NULL, then the prior allocation
2574
 
** is not freed.
2575
 
**
2576
 
** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
2577
 
** is always aligned to at least an 8 byte boundary, or to a
2578
 
** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
2579
 
** option is used.
2580
 
**
2581
 
** In SQLite version 3.5.0 and 3.5.1, it was possible to define
2582
 
** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
2583
 
** implementation of these routines to be omitted.  That capability
2584
 
** is no longer provided.  Only built-in memory allocators can be used.
2585
 
**
2586
 
** The Windows OS interface layer calls
2587
 
** the system malloc() and free() directly when converting
2588
 
** filenames between the UTF-8 encoding used by SQLite
2589
 
** and whatever filename encoding is used by the particular Windows
2590
 
** installation.  Memory allocation errors are detected, but
2591
 
** they are reported back as [SQLITE_CANTOPEN] or
2592
 
** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
2593
 
**
2594
 
** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
2595
 
** must be either NULL or else pointers obtained from a prior
2596
 
** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
2597
 
** not yet been released.
2598
 
**
2599
 
** The application must not read or write any part of
2600
 
** a block of memory after it has been released using
2601
 
** [sqlite3_free()] or [sqlite3_realloc()].
2602
 
*/
2603
 
SQLITE_API void *sqlite3_malloc(int);
2604
 
SQLITE_API void *sqlite3_realloc(void*, int);
2605
 
SQLITE_API void sqlite3_free(void*);
2606
 
 
2607
 
/*
2608
 
** CAPI3REF: Memory Allocator Statistics
2609
 
**
2610
 
** SQLite provides these two interfaces for reporting on the status
2611
 
** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
2612
 
** routines, which form the built-in memory allocation subsystem.
2613
 
**
2614
 
** ^The [sqlite3_memory_used()] routine returns the number of bytes
2615
 
** of memory currently outstanding (malloced but not freed).
2616
 
** ^The [sqlite3_memory_highwater()] routine returns the maximum
2617
 
** value of [sqlite3_memory_used()] since the high-water mark
2618
 
** was last reset.  ^The values returned by [sqlite3_memory_used()] and
2619
 
** [sqlite3_memory_highwater()] include any overhead
2620
 
** added by SQLite in its implementation of [sqlite3_malloc()],
2621
 
** but not overhead added by the any underlying system library
2622
 
** routines that [sqlite3_malloc()] may call.
2623
 
**
2624
 
** ^The memory high-water mark is reset to the current value of
2625
 
** [sqlite3_memory_used()] if and only if the parameter to
2626
 
** [sqlite3_memory_highwater()] is true.  ^The value returned
2627
 
** by [sqlite3_memory_highwater(1)] is the high-water mark
2628
 
** prior to the reset.
2629
 
*/
2630
 
SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
2631
 
SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
2632
 
 
2633
 
/*
2634
 
** CAPI3REF: Pseudo-Random Number Generator
2635
 
**
2636
 
** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
2637
 
** select random [ROWID | ROWIDs] when inserting new records into a table that
2638
 
** already uses the largest possible [ROWID].  The PRNG is also used for
2639
 
** the build-in random() and randomblob() SQL functions.  This interface allows
2640
 
** applications to access the same PRNG for other purposes.
2641
 
**
2642
 
** ^A call to this routine stores N bytes of randomness into buffer P.
2643
 
**
2644
 
** ^The first time this routine is invoked (either internally or by
2645
 
** the application) the PRNG is seeded using randomness obtained
2646
 
** from the xRandomness method of the default [sqlite3_vfs] object.
2647
 
** ^On all subsequent invocations, the pseudo-randomness is generated
2648
 
** internally and without recourse to the [sqlite3_vfs] xRandomness
2649
 
** method.
2650
 
*/
2651
 
SQLITE_API void sqlite3_randomness(int N, void *P);
2652
 
 
2653
 
/*
2654
 
** CAPI3REF: Compile-Time Authorization Callbacks
2655
 
**
2656
 
** ^This routine registers an authorizer callback with a particular
2657
 
** [database connection], supplied in the first argument.
2658
 
** ^The authorizer callback is invoked as SQL statements are being compiled
2659
 
** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
2660
 
** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
2661
 
** points during the compilation process, as logic is being created
2662
 
** to perform various actions, the authorizer callback is invoked to
2663
 
** see if those actions are allowed.  ^The authorizer callback should
2664
 
** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
2665
 
** specific action but allow the SQL statement to continue to be
2666
 
** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
2667
 
** rejected with an error.  ^If the authorizer callback returns
2668
 
** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
2669
 
** then the [sqlite3_prepare_v2()] or equivalent call that triggered
2670
 
** the authorizer will fail with an error message.
2671
 
**
2672
 
** When the callback returns [SQLITE_OK], that means the operation
2673
 
** requested is ok.  ^When the callback returns [SQLITE_DENY], the
2674
 
** [sqlite3_prepare_v2()] or equivalent call that triggered the
2675
 
** authorizer will fail with an error message explaining that
2676
 
** access is denied. 
2677
 
**
2678
 
** ^The first parameter to the authorizer callback is a copy of the third
2679
 
** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
2680
 
** to the callback is an integer [SQLITE_COPY | action code] that specifies
2681
 
** the particular action to be authorized. ^The third through sixth parameters
2682
 
** to the callback are zero-terminated strings that contain additional
2683
 
** details about the action to be authorized.
2684
 
**
2685
 
** ^If the action code is [SQLITE_READ]
2686
 
** and the callback returns [SQLITE_IGNORE] then the
2687
 
** [prepared statement] statement is constructed to substitute
2688
 
** a NULL value in place of the table column that would have
2689
 
** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
2690
 
** return can be used to deny an untrusted user access to individual
2691
 
** columns of a table.
2692
 
** ^If the action code is [SQLITE_DELETE] and the callback returns
2693
 
** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
2694
 
** [truncate optimization] is disabled and all rows are deleted individually.
2695
 
**
2696
 
** An authorizer is used when [sqlite3_prepare | preparing]
2697
 
** SQL statements from an untrusted source, to ensure that the SQL statements
2698
 
** do not try to access data they are not allowed to see, or that they do not
2699
 
** try to execute malicious statements that damage the database.  For
2700
 
** example, an application may allow a user to enter arbitrary
2701
 
** SQL queries for evaluation by a database.  But the application does
2702
 
** not want the user to be able to make arbitrary changes to the
2703
 
** database.  An authorizer could then be put in place while the
2704
 
** user-entered SQL is being [sqlite3_prepare | prepared] that
2705
 
** disallows everything except [SELECT] statements.
2706
 
**
2707
 
** Applications that need to process SQL from untrusted sources
2708
 
** might also consider lowering resource limits using [sqlite3_limit()]
2709
 
** and limiting database size using the [max_page_count] [PRAGMA]
2710
 
** in addition to using an authorizer.
2711
 
**
2712
 
** ^(Only a single authorizer can be in place on a database connection
2713
 
** at a time.  Each call to sqlite3_set_authorizer overrides the
2714
 
** previous call.)^  ^Disable the authorizer by installing a NULL callback.
2715
 
** The authorizer is disabled by default.
2716
 
**
2717
 
** The authorizer callback must not do anything that will modify
2718
 
** the database connection that invoked the authorizer callback.
2719
 
** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
2720
 
** database connections for the meaning of "modify" in this paragraph.
2721
 
**
2722
 
** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
2723
 
** statement might be re-prepared during [sqlite3_step()] due to a 
2724
 
** schema change.  Hence, the application should ensure that the
2725
 
** correct authorizer callback remains in place during the [sqlite3_step()].
2726
 
**
2727
 
** ^Note that the authorizer callback is invoked only during
2728
 
** [sqlite3_prepare()] or its variants.  Authorization is not
2729
 
** performed during statement evaluation in [sqlite3_step()], unless
2730
 
** as stated in the previous paragraph, sqlite3_step() invokes
2731
 
** sqlite3_prepare_v2() to reprepare a statement after a schema change.
2732
 
*/
2733
 
SQLITE_API int sqlite3_set_authorizer(
2734
 
  sqlite3*,
2735
 
  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
2736
 
  void *pUserData
2737
 
);
2738
 
 
2739
 
/*
2740
 
** CAPI3REF: Authorizer Return Codes
2741
 
**
2742
 
** The [sqlite3_set_authorizer | authorizer callback function] must
2743
 
** return either [SQLITE_OK] or one of these two constants in order
2744
 
** to signal SQLite whether or not the action is permitted.  See the
2745
 
** [sqlite3_set_authorizer | authorizer documentation] for additional
2746
 
** information.
2747
 
*/
2748
 
#define SQLITE_DENY   1   /* Abort the SQL statement with an error */
2749
 
#define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
2750
 
 
2751
 
/*
2752
 
** CAPI3REF: Authorizer Action Codes
2753
 
**
2754
 
** The [sqlite3_set_authorizer()] interface registers a callback function
2755
 
** that is invoked to authorize certain SQL statement actions.  The
2756
 
** second parameter to the callback is an integer code that specifies
2757
 
** what action is being authorized.  These are the integer action codes that
2758
 
** the authorizer callback may be passed.
2759
 
**
2760
 
** These action code values signify what kind of operation is to be
2761
 
** authorized.  The 3rd and 4th parameters to the authorization
2762
 
** callback function will be parameters or NULL depending on which of these
2763
 
** codes is used as the second parameter.  ^(The 5th parameter to the
2764
 
** authorizer callback is the name of the database ("main", "temp",
2765
 
** etc.) if applicable.)^  ^The 6th parameter to the authorizer callback
2766
 
** is the name of the inner-most trigger or view that is responsible for
2767
 
** the access attempt or NULL if this access attempt is directly from
2768
 
** top-level SQL code.
2769
 
*/
2770
 
/******************************************* 3rd ************ 4th ***********/
2771
 
#define SQLITE_CREATE_INDEX          1   /* Index Name      Table Name      */
2772
 
#define SQLITE_CREATE_TABLE          2   /* Table Name      NULL            */
2773
 
#define SQLITE_CREATE_TEMP_INDEX     3   /* Index Name      Table Name      */
2774
 
#define SQLITE_CREATE_TEMP_TABLE     4   /* Table Name      NULL            */
2775
 
#define SQLITE_CREATE_TEMP_TRIGGER   5   /* Trigger Name    Table Name      */
2776
 
#define SQLITE_CREATE_TEMP_VIEW      6   /* View Name       NULL            */
2777
 
#define SQLITE_CREATE_TRIGGER        7   /* Trigger Name    Table Name      */
2778
 
#define SQLITE_CREATE_VIEW           8   /* View Name       NULL            */
2779
 
#define SQLITE_DELETE                9   /* Table Name      NULL            */
2780
 
#define SQLITE_DROP_INDEX           10   /* Index Name      Table Name      */
2781
 
#define SQLITE_DROP_TABLE           11   /* Table Name      NULL            */
2782
 
#define SQLITE_DROP_TEMP_INDEX      12   /* Index Name      Table Name      */
2783
 
#define SQLITE_DROP_TEMP_TABLE      13   /* Table Name      NULL            */
2784
 
#define SQLITE_DROP_TEMP_TRIGGER    14   /* Trigger Name    Table Name      */
2785
 
#define SQLITE_DROP_TEMP_VIEW       15   /* View Name       NULL            */
2786
 
#define SQLITE_DROP_TRIGGER         16   /* Trigger Name    Table Name      */
2787
 
#define SQLITE_DROP_VIEW            17   /* View Name       NULL            */
2788
 
#define SQLITE_INSERT               18   /* Table Name      NULL            */
2789
 
#define SQLITE_PRAGMA               19   /* Pragma Name     1st arg or NULL */
2790
 
#define SQLITE_READ                 20   /* Table Name      Column Name     */
2791
 
#define SQLITE_SELECT               21   /* NULL            NULL            */
2792
 
#define SQLITE_TRANSACTION          22   /* Operation       NULL            */
2793
 
#define SQLITE_UPDATE               23   /* Table Name      Column Name     */
2794
 
#define SQLITE_ATTACH               24   /* Filename        NULL            */
2795
 
#define SQLITE_DETACH               25   /* Database Name   NULL            */
2796
 
#define SQLITE_ALTER_TABLE          26   /* Database Name   Table Name      */
2797
 
#define SQLITE_REINDEX              27   /* Index Name      NULL            */
2798
 
#define SQLITE_ANALYZE              28   /* Table Name      NULL            */
2799
 
#define SQLITE_CREATE_VTABLE        29   /* Table Name      Module Name     */
2800
 
#define SQLITE_DROP_VTABLE          30   /* Table Name      Module Name     */
2801
 
#define SQLITE_FUNCTION             31   /* NULL            Function Name   */
2802
 
#define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
2803
 
#define SQLITE_COPY                  0   /* No longer used */
2804
 
 
2805
 
/*
2806
 
** CAPI3REF: Tracing And Profiling Functions
2807
 
**
2808
 
** These routines register callback functions that can be used for
2809
 
** tracing and profiling the execution of SQL statements.
2810
 
**
2811
 
** ^The callback function registered by sqlite3_trace() is invoked at
2812
 
** various times when an SQL statement is being run by [sqlite3_step()].
2813
 
** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
2814
 
** SQL statement text as the statement first begins executing.
2815
 
** ^(Additional sqlite3_trace() callbacks might occur
2816
 
** as each triggered subprogram is entered.  The callbacks for triggers
2817
 
** contain a UTF-8 SQL comment that identifies the trigger.)^
2818
 
**
2819
 
** ^The callback function registered by sqlite3_profile() is invoked
2820
 
** as each SQL statement finishes.  ^The profile callback contains
2821
 
** the original statement text and an estimate of wall-clock time
2822
 
** of how long that statement took to run.  ^The profile callback
2823
 
** time is in units of nanoseconds, however the current implementation
2824
 
** is only capable of millisecond resolution so the six least significant
2825
 
** digits in the time are meaningless.  Future versions of SQLite
2826
 
** might provide greater resolution on the profiler callback.  The
2827
 
** sqlite3_profile() function is considered experimental and is
2828
 
** subject to change in future versions of SQLite.
2829
 
*/
2830
 
SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
2831
 
SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*,
2832
 
   void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
2833
 
 
2834
 
/*
2835
 
** CAPI3REF: Query Progress Callbacks
2836
 
**
2837
 
** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
2838
 
** function X to be invoked periodically during long running calls to
2839
 
** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
2840
 
** database connection D.  An example use for this
2841
 
** interface is to keep a GUI updated during a large query.
2842
 
**
2843
 
** ^The parameter P is passed through as the only parameter to the 
2844
 
** callback function X.  ^The parameter N is the number of 
2845
 
** [virtual machine instructions] that are evaluated between successive
2846
 
** invocations of the callback X.
2847
 
**
2848
 
** ^Only a single progress handler may be defined at one time per
2849
 
** [database connection]; setting a new progress handler cancels the
2850
 
** old one.  ^Setting parameter X to NULL disables the progress handler.
2851
 
** ^The progress handler is also disabled by setting N to a value less
2852
 
** than 1.
2853
 
**
2854
 
** ^If the progress callback returns non-zero, the operation is
2855
 
** interrupted.  This feature can be used to implement a
2856
 
** "Cancel" button on a GUI progress dialog box.
2857
 
**
2858
 
** The progress handler callback must not do anything that will modify
2859
 
** the database connection that invoked the progress handler.
2860
 
** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
2861
 
** database connections for the meaning of "modify" in this paragraph.
2862
 
**
2863
 
*/
2864
 
SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
2865
 
 
2866
 
/*
2867
 
** CAPI3REF: Opening A New Database Connection
2868
 
**
2869
 
** ^These routines open an SQLite database file whose name is given by the
2870
 
** filename argument. ^The filename argument is interpreted as UTF-8 for
2871
 
** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
2872
 
** order for sqlite3_open16(). ^(A [database connection] handle is usually
2873
 
** returned in *ppDb, even if an error occurs.  The only exception is that
2874
 
** if SQLite is unable to allocate memory to hold the [sqlite3] object,
2875
 
** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
2876
 
** object.)^ ^(If the database is opened (and/or created) successfully, then
2877
 
** [SQLITE_OK] is returned.  Otherwise an [error code] is returned.)^ ^The
2878
 
** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
2879
 
** an English language description of the error following a failure of any
2880
 
** of the sqlite3_open() routines.
2881
 
**
2882
 
** ^The default encoding for the database will be UTF-8 if
2883
 
** sqlite3_open() or sqlite3_open_v2() is called and
2884
 
** UTF-16 in the native byte order if sqlite3_open16() is used.
2885
 
**
2886
 
** Whether or not an error occurs when it is opened, resources
2887
 
** associated with the [database connection] handle should be released by
2888
 
** passing it to [sqlite3_close()] when it is no longer required.
2889
 
**
2890
 
** The sqlite3_open_v2() interface works like sqlite3_open()
2891
 
** except that it accepts two additional parameters for additional control
2892
 
** over the new database connection.  ^(The flags parameter to
2893
 
** sqlite3_open_v2() can take one of
2894
 
** the following three values, optionally combined with the 
2895
 
** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
2896
 
** and/or [SQLITE_OPEN_PRIVATECACHE] flags:)^
2897
 
**
2898
 
** <dl>
2899
 
** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
2900
 
** <dd>The database is opened in read-only mode.  If the database does not
2901
 
** already exist, an error is returned.</dd>)^
2902
 
**
2903
 
** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
2904
 
** <dd>The database is opened for reading and writing if possible, or reading
2905
 
** only if the file is write protected by the operating system.  In either
2906
 
** case the database must already exist, otherwise an error is returned.</dd>)^
2907
 
**
2908
 
** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
2909
 
** <dd>The database is opened for reading and writing, and is created if
2910
 
** it does not already exist. This is the behavior that is always used for
2911
 
** sqlite3_open() and sqlite3_open16().</dd>)^
2912
 
** </dl>
2913
 
**
2914
 
** If the 3rd parameter to sqlite3_open_v2() is not one of the
2915
 
** combinations shown above or one of the combinations shown above combined
2916
 
** with the [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX],
2917
 
** [SQLITE_OPEN_SHAREDCACHE] and/or [SQLITE_OPEN_PRIVATECACHE] flags,
2918
 
** then the behavior is undefined.
2919
 
**
2920
 
** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
2921
 
** opens in the multi-thread [threading mode] as long as the single-thread
2922
 
** mode has not been set at compile-time or start-time.  ^If the
2923
 
** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
2924
 
** in the serialized [threading mode] unless single-thread was
2925
 
** previously selected at compile-time or start-time.
2926
 
** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
2927
 
** eligible to use [shared cache mode], regardless of whether or not shared
2928
 
** cache is enabled using [sqlite3_enable_shared_cache()].  ^The
2929
 
** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
2930
 
** participate in [shared cache mode] even if it is enabled.
2931
 
**
2932
 
** ^If the filename is ":memory:", then a private, temporary in-memory database
2933
 
** is created for the connection.  ^This in-memory database will vanish when
2934
 
** the database connection is closed.  Future versions of SQLite might
2935
 
** make use of additional special filenames that begin with the ":" character.
2936
 
** It is recommended that when a database filename actually does begin with
2937
 
** a ":" character you should prefix the filename with a pathname such as
2938
 
** "./" to avoid ambiguity.
2939
 
**
2940
 
** ^If the filename is an empty string, then a private, temporary
2941
 
** on-disk database will be created.  ^This private database will be
2942
 
** automatically deleted as soon as the database connection is closed.
2943
 
**
2944
 
** ^The fourth parameter to sqlite3_open_v2() is the name of the
2945
 
** [sqlite3_vfs] object that defines the operating system interface that
2946
 
** the new database connection should use.  ^If the fourth parameter is
2947
 
** a NULL pointer then the default [sqlite3_vfs] object is used.
2948
 
**
2949
 
** <b>Note to Windows users:</b>  The encoding used for the filename argument
2950
 
** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
2951
 
** codepage is currently defined.  Filenames containing international
2952
 
** characters must be converted to UTF-8 prior to passing them into
2953
 
** sqlite3_open() or sqlite3_open_v2().
2954
 
*/
2955
 
SQLITE_API int sqlite3_open(
2956
 
  const char *filename,   /* Database filename (UTF-8) */
2957
 
  sqlite3 **ppDb          /* OUT: SQLite db handle */
2958
 
);
2959
 
SQLITE_API int sqlite3_open16(
2960
 
  const void *filename,   /* Database filename (UTF-16) */
2961
 
  sqlite3 **ppDb          /* OUT: SQLite db handle */
2962
 
);
2963
 
SQLITE_API int sqlite3_open_v2(
2964
 
  const char *filename,   /* Database filename (UTF-8) */
2965
 
  sqlite3 **ppDb,         /* OUT: SQLite db handle */
2966
 
  int flags,              /* Flags */
2967
 
  const char *zVfs        /* Name of VFS module to use */
2968
 
);
2969
 
 
2970
 
/*
2971
 
** CAPI3REF: Error Codes And Messages
2972
 
**
2973
 
** ^The sqlite3_errcode() interface returns the numeric [result code] or
2974
 
** [extended result code] for the most recent failed sqlite3_* API call
2975
 
** associated with a [database connection]. If a prior API call failed
2976
 
** but the most recent API call succeeded, the return value from
2977
 
** sqlite3_errcode() is undefined.  ^The sqlite3_extended_errcode()
2978
 
** interface is the same except that it always returns the 
2979
 
** [extended result code] even when extended result codes are
2980
 
** disabled.
2981
 
**
2982
 
** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
2983
 
** text that describes the error, as either UTF-8 or UTF-16 respectively.
2984
 
** ^(Memory to hold the error message string is managed internally.
2985
 
** The application does not need to worry about freeing the result.
2986
 
** However, the error string might be overwritten or deallocated by
2987
 
** subsequent calls to other SQLite interface functions.)^
2988
 
**
2989
 
** When the serialized [threading mode] is in use, it might be the
2990
 
** case that a second error occurs on a separate thread in between
2991
 
** the time of the first error and the call to these interfaces.
2992
 
** When that happens, the second error will be reported since these
2993
 
** interfaces always report the most recent result.  To avoid
2994
 
** this, each thread can obtain exclusive use of the [database connection] D
2995
 
** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
2996
 
** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
2997
 
** all calls to the interfaces listed here are completed.
2998
 
**
2999
 
** If an interface fails with SQLITE_MISUSE, that means the interface
3000
 
** was invoked incorrectly by the application.  In that case, the
3001
 
** error code and message may or may not be set.
3002
 
*/
3003
 
SQLITE_API int sqlite3_errcode(sqlite3 *db);
3004
 
SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
3005
 
SQLITE_API const char *sqlite3_errmsg(sqlite3*);
3006
 
SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
3007
 
 
3008
 
/*
3009
 
** CAPI3REF: SQL Statement Object
3010
 
** KEYWORDS: {prepared statement} {prepared statements}
3011
 
**
3012
 
** An instance of this object represents a single SQL statement.
3013
 
** This object is variously known as a "prepared statement" or a
3014
 
** "compiled SQL statement" or simply as a "statement".
3015
 
**
3016
 
** The life of a statement object goes something like this:
3017
 
**
3018
 
** <ol>
3019
 
** <li> Create the object using [sqlite3_prepare_v2()] or a related
3020
 
**      function.
3021
 
** <li> Bind values to [host parameters] using the sqlite3_bind_*()
3022
 
**      interfaces.
3023
 
** <li> Run the SQL by calling [sqlite3_step()] one or more times.
3024
 
** <li> Reset the statement using [sqlite3_reset()] then go back
3025
 
**      to step 2.  Do this zero or more times.
3026
 
** <li> Destroy the object using [sqlite3_finalize()].
3027
 
** </ol>
3028
 
**
3029
 
** Refer to documentation on individual methods above for additional
3030
 
** information.
3031
 
*/
3032
 
typedef struct sqlite3_stmt sqlite3_stmt;
3033
 
 
3034
 
/*
3035
 
** CAPI3REF: Run-time Limits
3036
 
**
3037
 
** ^(This interface allows the size of various constructs to be limited
3038
 
** on a connection by connection basis.  The first parameter is the
3039
 
** [database connection] whose limit is to be set or queried.  The
3040
 
** second parameter is one of the [limit categories] that define a
3041
 
** class of constructs to be size limited.  The third parameter is the
3042
 
** new limit for that construct.)^
3043
 
**
3044
 
** ^If the new limit is a negative number, the limit is unchanged.
3045
 
** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a 
3046
 
** [limits | hard upper bound]
3047
 
** set at compile-time by a C preprocessor macro called
3048
 
** [limits | SQLITE_MAX_<i>NAME</i>].
3049
 
** (The "_LIMIT_" in the name is changed to "_MAX_".))^
3050
 
** ^Attempts to increase a limit above its hard upper bound are
3051
 
** silently truncated to the hard upper bound.
3052
 
**
3053
 
** ^Regardless of whether or not the limit was changed, the 
3054
 
** [sqlite3_limit()] interface returns the prior value of the limit.
3055
 
** ^Hence, to find the current value of a limit without changing it,
3056
 
** simply invoke this interface with the third parameter set to -1.
3057
 
**
3058
 
** Run-time limits are intended for use in applications that manage
3059
 
** both their own internal database and also databases that are controlled
3060
 
** by untrusted external sources.  An example application might be a
3061
 
** web browser that has its own databases for storing history and
3062
 
** separate databases controlled by JavaScript applications downloaded
3063
 
** off the Internet.  The internal databases can be given the
3064
 
** large, default limits.  Databases managed by external sources can
3065
 
** be given much smaller limits designed to prevent a denial of service
3066
 
** attack.  Developers might also want to use the [sqlite3_set_authorizer()]
3067
 
** interface to further control untrusted SQL.  The size of the database
3068
 
** created by an untrusted script can be contained using the
3069
 
** [max_page_count] [PRAGMA].
3070
 
**
3071
 
** New run-time limit categories may be added in future releases.
3072
 
*/
3073
 
SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
3074
 
 
3075
 
/*
3076
 
** CAPI3REF: Run-Time Limit Categories
3077
 
** KEYWORDS: {limit category} {*limit categories}
3078
 
**
3079
 
** These constants define various performance limits
3080
 
** that can be lowered at run-time using [sqlite3_limit()].
3081
 
** The synopsis of the meanings of the various limits is shown below.
3082
 
** Additional information is available at [limits | Limits in SQLite].
3083
 
**
3084
 
** <dl>
3085
 
** ^(<dt>SQLITE_LIMIT_LENGTH</dt>
3086
 
** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
3087
 
**
3088
 
** ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
3089
 
** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
3090
 
**
3091
 
** ^(<dt>SQLITE_LIMIT_COLUMN</dt>
3092
 
** <dd>The maximum number of columns in a table definition or in the
3093
 
** result set of a [SELECT] or the maximum number of columns in an index
3094
 
** or in an ORDER BY or GROUP BY clause.</dd>)^
3095
 
**
3096
 
** ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
3097
 
** <dd>The maximum depth of the parse tree on any expression.</dd>)^
3098
 
**
3099
 
** ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
3100
 
** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
3101
 
**
3102
 
** ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
3103
 
** <dd>The maximum number of instructions in a virtual machine program
3104
 
** used to implement an SQL statement.  This limit is not currently
3105
 
** enforced, though that might be added in some future release of
3106
 
** SQLite.</dd>)^
3107
 
**
3108
 
** ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
3109
 
** <dd>The maximum number of arguments on a function.</dd>)^
3110
 
**
3111
 
** ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
3112
 
** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
3113
 
**
3114
 
** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
3115
 
** <dd>The maximum length of the pattern argument to the [LIKE] or
3116
 
** [GLOB] operators.</dd>)^
3117
 
**
3118
 
** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
3119
 
** <dd>The maximum index number of any [parameter] in an SQL statement.)^
3120
 
**
3121
 
** ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
3122
 
** <dd>The maximum depth of recursion for triggers.</dd>)^
3123
 
** </dl>
3124
 
*/
3125
 
#define SQLITE_LIMIT_LENGTH                    0
3126
 
#define SQLITE_LIMIT_SQL_LENGTH                1
3127
 
#define SQLITE_LIMIT_COLUMN                    2
3128
 
#define SQLITE_LIMIT_EXPR_DEPTH                3
3129
 
#define SQLITE_LIMIT_COMPOUND_SELECT           4
3130
 
#define SQLITE_LIMIT_VDBE_OP                   5
3131
 
#define SQLITE_LIMIT_FUNCTION_ARG              6
3132
 
#define SQLITE_LIMIT_ATTACHED                  7
3133
 
#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
3134
 
#define SQLITE_LIMIT_VARIABLE_NUMBER           9
3135
 
#define SQLITE_LIMIT_TRIGGER_DEPTH            10
3136
 
 
3137
 
/*
3138
 
** CAPI3REF: Compiling An SQL Statement
3139
 
** KEYWORDS: {SQL statement compiler}
3140
 
**
3141
 
** To execute an SQL query, it must first be compiled into a byte-code
3142
 
** program using one of these routines.
3143
 
**
3144
 
** The first argument, "db", is a [database connection] obtained from a
3145
 
** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
3146
 
** [sqlite3_open16()].  The database connection must not have been closed.
3147
 
**
3148
 
** The second argument, "zSql", is the statement to be compiled, encoded
3149
 
** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
3150
 
** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
3151
 
** use UTF-16.
3152
 
**
3153
 
** ^If the nByte argument is less than zero, then zSql is read up to the
3154
 
** first zero terminator. ^If nByte is non-negative, then it is the maximum
3155
 
** number of  bytes read from zSql.  ^When nByte is non-negative, the
3156
 
** zSql string ends at either the first '\000' or '\u0000' character or
3157
 
** the nByte-th byte, whichever comes first. If the caller knows
3158
 
** that the supplied string is nul-terminated, then there is a small
3159
 
** performance advantage to be gained by passing an nByte parameter that
3160
 
** is equal to the number of bytes in the input string <i>including</i>
3161
 
** the nul-terminator bytes.
3162
 
**
3163
 
** ^If pzTail is not NULL then *pzTail is made to point to the first byte
3164
 
** past the end of the first SQL statement in zSql.  These routines only
3165
 
** compile the first statement in zSql, so *pzTail is left pointing to
3166
 
** what remains uncompiled.
3167
 
**
3168
 
** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
3169
 
** executed using [sqlite3_step()].  ^If there is an error, *ppStmt is set
3170
 
** to NULL.  ^If the input text contains no SQL (if the input is an empty
3171
 
** string or a comment) then *ppStmt is set to NULL.
3172
 
** The calling procedure is responsible for deleting the compiled
3173
 
** SQL statement using [sqlite3_finalize()] after it has finished with it.
3174
 
** ppStmt may not be NULL.
3175
 
**
3176
 
** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
3177
 
** otherwise an [error code] is returned.
3178
 
**
3179
 
** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
3180
 
** recommended for all new programs. The two older interfaces are retained
3181
 
** for backwards compatibility, but their use is discouraged.
3182
 
** ^In the "v2" interfaces, the prepared statement
3183
 
** that is returned (the [sqlite3_stmt] object) contains a copy of the
3184
 
** original SQL text. This causes the [sqlite3_step()] interface to
3185
 
** behave differently in three ways:
3186
 
**
3187
 
** <ol>
3188
 
** <li>
3189
 
** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
3190
 
** always used to do, [sqlite3_step()] will automatically recompile the SQL
3191
 
** statement and try to run it again.
3192
 
** </li>
3193
 
**
3194
 
** <li>
3195
 
** ^When an error occurs, [sqlite3_step()] will return one of the detailed
3196
 
** [error codes] or [extended error codes].  ^The legacy behavior was that
3197
 
** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
3198
 
** and the application would have to make a second call to [sqlite3_reset()]
3199
 
** in order to find the underlying cause of the problem. With the "v2" prepare
3200
 
** interfaces, the underlying reason for the error is returned immediately.
3201
 
** </li>
3202
 
**
3203
 
** <li>
3204
 
** ^If the specific value bound to [parameter | host parameter] in the 
3205
 
** WHERE clause might influence the choice of query plan for a statement,
3206
 
** then the statement will be automatically recompiled, as if there had been 
3207
 
** a schema change, on the first  [sqlite3_step()] call following any change
3208
 
** to the [sqlite3_bind_text | bindings] of that [parameter]. 
3209
 
** ^The specific value of WHERE-clause [parameter] might influence the 
3210
 
** choice of query plan if the parameter is the left-hand side of a [LIKE]
3211
 
** or [GLOB] operator or if the parameter is compared to an indexed column
3212
 
** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
3213
 
** the 
3214
 
** </li>
3215
 
** </ol>
3216
 
*/
3217
 
SQLITE_API int sqlite3_prepare(
3218
 
  sqlite3 *db,            /* Database handle */
3219
 
  const char *zSql,       /* SQL statement, UTF-8 encoded */
3220
 
  int nByte,              /* Maximum length of zSql in bytes. */
3221
 
  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3222
 
  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
3223
 
);
3224
 
SQLITE_API int sqlite3_prepare_v2(
3225
 
  sqlite3 *db,            /* Database handle */
3226
 
  const char *zSql,       /* SQL statement, UTF-8 encoded */
3227
 
  int nByte,              /* Maximum length of zSql in bytes. */
3228
 
  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3229
 
  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
3230
 
);
3231
 
SQLITE_API int sqlite3_prepare16(
3232
 
  sqlite3 *db,            /* Database handle */
3233
 
  const void *zSql,       /* SQL statement, UTF-16 encoded */
3234
 
  int nByte,              /* Maximum length of zSql in bytes. */
3235
 
  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3236
 
  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
3237
 
);
3238
 
SQLITE_API int sqlite3_prepare16_v2(
3239
 
  sqlite3 *db,            /* Database handle */
3240
 
  const void *zSql,       /* SQL statement, UTF-16 encoded */
3241
 
  int nByte,              /* Maximum length of zSql in bytes. */
3242
 
  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3243
 
  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
3244
 
);
3245
 
 
3246
 
/*
3247
 
** CAPI3REF: Retrieving Statement SQL
3248
 
**
3249
 
** ^This interface can be used to retrieve a saved copy of the original
3250
 
** SQL text used to create a [prepared statement] if that statement was
3251
 
** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
3252
 
*/
3253
 
SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
3254
 
 
3255
 
/*
3256
 
** CAPI3REF: Determine If An SQL Statement Writes The Database
3257
 
**
3258
 
** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
3259
 
** and only if the [prepared statement] X makes no direct changes to
3260
 
** the content of the database file.
3261
 
**
3262
 
** Note that [application-defined SQL functions] or
3263
 
** [virtual tables] might change the database indirectly as a side effect.  
3264
 
** ^(For example, if an application defines a function "eval()" that 
3265
 
** calls [sqlite3_exec()], then the following SQL statement would
3266
 
** change the database file through side-effects:
3267
 
**
3268
 
** <blockquote><pre>
3269
 
**    SELECT eval('DELETE FROM t1') FROM t2;
3270
 
** </pre></blockquote>
3271
 
**
3272
 
** But because the [SELECT] statement does not change the database file
3273
 
** directly, sqlite3_stmt_readonly() would still return true.)^
3274
 
**
3275
 
** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
3276
 
** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
3277
 
** since the statements themselves do not actually modify the database but
3278
 
** rather they control the timing of when other statements modify the 
3279
 
** database.  ^The [ATTACH] and [DETACH] statements also cause
3280
 
** sqlite3_stmt_readonly() to return true since, while those statements
3281
 
** change the configuration of a database connection, they do not make 
3282
 
** changes to the content of the database files on disk.
3283
 
*/
3284
 
SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
3285
 
 
3286
 
/*
3287
 
** CAPI3REF: Dynamically Typed Value Object
3288
 
** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
3289
 
**
3290
 
** SQLite uses the sqlite3_value object to represent all values
3291
 
** that can be stored in a database table. SQLite uses dynamic typing
3292
 
** for the values it stores.  ^Values stored in sqlite3_value objects
3293
 
** can be integers, floating point values, strings, BLOBs, or NULL.
3294
 
**
3295
 
** An sqlite3_value object may be either "protected" or "unprotected".
3296
 
** Some interfaces require a protected sqlite3_value.  Other interfaces
3297
 
** will accept either a protected or an unprotected sqlite3_value.
3298
 
** Every interface that accepts sqlite3_value arguments specifies
3299
 
** whether or not it requires a protected sqlite3_value.
3300
 
**
3301
 
** The terms "protected" and "unprotected" refer to whether or not
3302
 
** a mutex is held.  An internal mutex is held for a protected
3303
 
** sqlite3_value object but no mutex is held for an unprotected
3304
 
** sqlite3_value object.  If SQLite is compiled to be single-threaded
3305
 
** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
3306
 
** or if SQLite is run in one of reduced mutex modes 
3307
 
** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
3308
 
** then there is no distinction between protected and unprotected
3309
 
** sqlite3_value objects and they can be used interchangeably.  However,
3310
 
** for maximum code portability it is recommended that applications
3311
 
** still make the distinction between protected and unprotected
3312
 
** sqlite3_value objects even when not strictly required.
3313
 
**
3314
 
** ^The sqlite3_value objects that are passed as parameters into the
3315
 
** implementation of [application-defined SQL functions] are protected.
3316
 
** ^The sqlite3_value object returned by
3317
 
** [sqlite3_column_value()] is unprotected.
3318
 
** Unprotected sqlite3_value objects may only be used with
3319
 
** [sqlite3_result_value()] and [sqlite3_bind_value()].
3320
 
** The [sqlite3_value_blob | sqlite3_value_type()] family of
3321
 
** interfaces require protected sqlite3_value objects.
3322
 
*/
3323
 
typedef struct Mem sqlite3_value;
3324
 
 
3325
 
/*
3326
 
** CAPI3REF: SQL Function Context Object
3327
 
**
3328
 
** The context in which an SQL function executes is stored in an
3329
 
** sqlite3_context object.  ^A pointer to an sqlite3_context object
3330
 
** is always first parameter to [application-defined SQL functions].
3331
 
** The application-defined SQL function implementation will pass this
3332
 
** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
3333
 
** [sqlite3_aggregate_context()], [sqlite3_user_data()],
3334
 
** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
3335
 
** and/or [sqlite3_set_auxdata()].
3336
 
*/
3337
 
typedef struct sqlite3_context sqlite3_context;
3338
 
 
3339
 
/*
3340
 
** CAPI3REF: Binding Values To Prepared Statements
3341
 
** KEYWORDS: {host parameter} {host parameters} {host parameter name}
3342
 
** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
3343
 
**
3344
 
** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
3345
 
** literals may be replaced by a [parameter] that matches one of following
3346
 
** templates:
3347
 
**
3348
 
** <ul>
3349
 
** <li>  ?
3350
 
** <li>  ?NNN
3351
 
** <li>  :VVV
3352
 
** <li>  @VVV
3353
 
** <li>  $VVV
3354
 
** </ul>
3355
 
**
3356
 
** In the templates above, NNN represents an integer literal,
3357
 
** and VVV represents an alphanumeric identifier.)^  ^The values of these
3358
 
** parameters (also called "host parameter names" or "SQL parameters")
3359
 
** can be set using the sqlite3_bind_*() routines defined here.
3360
 
**
3361
 
** ^The first argument to the sqlite3_bind_*() routines is always
3362
 
** a pointer to the [sqlite3_stmt] object returned from
3363
 
** [sqlite3_prepare_v2()] or its variants.
3364
 
**
3365
 
** ^The second argument is the index of the SQL parameter to be set.
3366
 
** ^The leftmost SQL parameter has an index of 1.  ^When the same named
3367
 
** SQL parameter is used more than once, second and subsequent
3368
 
** occurrences have the same index as the first occurrence.
3369
 
** ^The index for named parameters can be looked up using the
3370
 
** [sqlite3_bind_parameter_index()] API if desired.  ^The index
3371
 
** for "?NNN" parameters is the value of NNN.
3372
 
** ^The NNN value must be between 1 and the [sqlite3_limit()]
3373
 
** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
3374
 
**
3375
 
** ^The third argument is the value to bind to the parameter.
3376
 
**
3377
 
** ^(In those routines that have a fourth argument, its value is the
3378
 
** number of bytes in the parameter.  To be clear: the value is the
3379
 
** number of <u>bytes</u> in the value, not the number of characters.)^
3380
 
** ^If the fourth parameter is negative, the length of the string is
3381
 
** the number of bytes up to the first zero terminator.
3382
 
**
3383
 
** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
3384
 
** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
3385
 
** string after SQLite has finished with it.  ^The destructor is called
3386
 
** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
3387
 
** sqlite3_bind_text(), or sqlite3_bind_text16() fails.  
3388
 
** ^If the fifth argument is
3389
 
** the special value [SQLITE_STATIC], then SQLite assumes that the
3390
 
** information is in static, unmanaged space and does not need to be freed.
3391
 
** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
3392
 
** SQLite makes its own private copy of the data immediately, before
3393
 
** the sqlite3_bind_*() routine returns.
3394
 
**
3395
 
** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
3396
 
** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
3397
 
** (just an integer to hold its size) while it is being processed.
3398
 
** Zeroblobs are intended to serve as placeholders for BLOBs whose
3399
 
** content is later written using
3400
 
** [sqlite3_blob_open | incremental BLOB I/O] routines.
3401
 
** ^A negative value for the zeroblob results in a zero-length BLOB.
3402
 
**
3403
 
** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
3404
 
** for the [prepared statement] or with a prepared statement for which
3405
 
** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
3406
 
** then the call will return [SQLITE_MISUSE].  If any sqlite3_bind_()
3407
 
** routine is passed a [prepared statement] that has been finalized, the
3408
 
** result is undefined and probably harmful.
3409
 
**
3410
 
** ^Bindings are not cleared by the [sqlite3_reset()] routine.
3411
 
** ^Unbound parameters are interpreted as NULL.
3412
 
**
3413
 
** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
3414
 
** [error code] if anything goes wrong.
3415
 
** ^[SQLITE_RANGE] is returned if the parameter
3416
 
** index is out of range.  ^[SQLITE_NOMEM] is returned if malloc() fails.
3417
 
**
3418
 
** See also: [sqlite3_bind_parameter_count()],
3419
 
** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
3420
 
*/
3421
 
SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
3422
 
SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
3423
 
SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
3424
 
SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
3425
 
SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
3426
 
SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
3427
 
SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
3428
 
SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
3429
 
SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
3430
 
 
3431
 
/*
3432
 
** CAPI3REF: Number Of SQL Parameters
3433
 
**
3434
 
** ^This routine can be used to find the number of [SQL parameters]
3435
 
** in a [prepared statement].  SQL parameters are tokens of the
3436
 
** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
3437
 
** placeholders for values that are [sqlite3_bind_blob | bound]
3438
 
** to the parameters at a later time.
3439
 
**
3440
 
** ^(This routine actually returns the index of the largest (rightmost)
3441
 
** parameter. For all forms except ?NNN, this will correspond to the
3442
 
** number of unique parameters.  If parameters of the ?NNN form are used,
3443
 
** there may be gaps in the list.)^
3444
 
**
3445
 
** See also: [sqlite3_bind_blob|sqlite3_bind()],
3446
 
** [sqlite3_bind_parameter_name()], and
3447
 
** [sqlite3_bind_parameter_index()].
3448
 
*/
3449
 
SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
3450
 
 
3451
 
/*
3452
 
** CAPI3REF: Name Of A Host Parameter
3453
 
**
3454
 
** ^The sqlite3_bind_parameter_name(P,N) interface returns
3455
 
** the name of the N-th [SQL parameter] in the [prepared statement] P.
3456
 
** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
3457
 
** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
3458
 
** respectively.
3459
 
** In other words, the initial ":" or "$" or "@" or "?"
3460
 
** is included as part of the name.)^
3461
 
** ^Parameters of the form "?" without a following integer have no name
3462
 
** and are referred to as "nameless" or "anonymous parameters".
3463
 
**
3464
 
** ^The first host parameter has an index of 1, not 0.
3465
 
**
3466
 
** ^If the value N is out of range or if the N-th parameter is
3467
 
** nameless, then NULL is returned.  ^The returned string is
3468
 
** always in UTF-8 encoding even if the named parameter was
3469
 
** originally specified as UTF-16 in [sqlite3_prepare16()] or
3470
 
** [sqlite3_prepare16_v2()].
3471
 
**
3472
 
** See also: [sqlite3_bind_blob|sqlite3_bind()],
3473
 
** [sqlite3_bind_parameter_count()], and
3474
 
** [sqlite3_bind_parameter_index()].
3475
 
*/
3476
 
SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
3477
 
 
3478
 
/*
3479
 
** CAPI3REF: Index Of A Parameter With A Given Name
3480
 
**
3481
 
** ^Return the index of an SQL parameter given its name.  ^The
3482
 
** index value returned is suitable for use as the second
3483
 
** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
3484
 
** is returned if no matching parameter is found.  ^The parameter
3485
 
** name must be given in UTF-8 even if the original statement
3486
 
** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
3487
 
**
3488
 
** See also: [sqlite3_bind_blob|sqlite3_bind()],
3489
 
** [sqlite3_bind_parameter_count()], and
3490
 
** [sqlite3_bind_parameter_index()].
3491
 
*/
3492
 
SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
3493
 
 
3494
 
/*
3495
 
** CAPI3REF: Reset All Bindings On A Prepared Statement
3496
 
**
3497
 
** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
3498
 
** the [sqlite3_bind_blob | bindings] on a [prepared statement].
3499
 
** ^Use this routine to reset all host parameters to NULL.
3500
 
*/
3501
 
SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
3502
 
 
3503
 
/*
3504
 
** CAPI3REF: Number Of Columns In A Result Set
3505
 
**
3506
 
** ^Return the number of columns in the result set returned by the
3507
 
** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
3508
 
** statement that does not return data (for example an [UPDATE]).
3509
 
**
3510
 
** See also: [sqlite3_data_count()]
3511
 
*/
3512
 
SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
3513
 
 
3514
 
/*
3515
 
** CAPI3REF: Column Names In A Result Set
3516
 
**
3517
 
** ^These routines return the name assigned to a particular column
3518
 
** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
3519
 
** interface returns a pointer to a zero-terminated UTF-8 string
3520
 
** and sqlite3_column_name16() returns a pointer to a zero-terminated
3521
 
** UTF-16 string.  ^The first parameter is the [prepared statement]
3522
 
** that implements the [SELECT] statement. ^The second parameter is the
3523
 
** column number.  ^The leftmost column is number 0.
3524
 
**
3525
 
** ^The returned string pointer is valid until either the [prepared statement]
3526
 
** is destroyed by [sqlite3_finalize()] or until the statement is automatically
3527
 
** reprepared by the first call to [sqlite3_step()] for a particular run
3528
 
** or until the next call to
3529
 
** sqlite3_column_name() or sqlite3_column_name16() on the same column.
3530
 
**
3531
 
** ^If sqlite3_malloc() fails during the processing of either routine
3532
 
** (for example during a conversion from UTF-8 to UTF-16) then a
3533
 
** NULL pointer is returned.
3534
 
**
3535
 
** ^The name of a result column is the value of the "AS" clause for
3536
 
** that column, if there is an AS clause.  If there is no AS clause
3537
 
** then the name of the column is unspecified and may change from
3538
 
** one release of SQLite to the next.
3539
 
*/
3540
 
SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
3541
 
SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
3542
 
 
3543
 
/*
3544
 
** CAPI3REF: Source Of Data In A Query Result
3545
 
**
3546
 
** ^These routines provide a means to determine the database, table, and
3547
 
** table column that is the origin of a particular result column in
3548
 
** [SELECT] statement.
3549
 
** ^The name of the database or table or column can be returned as
3550
 
** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
3551
 
** the database name, the _table_ routines return the table name, and
3552
 
** the origin_ routines return the column name.
3553
 
** ^The returned string is valid until the [prepared statement] is destroyed
3554
 
** using [sqlite3_finalize()] or until the statement is automatically
3555
 
** reprepared by the first call to [sqlite3_step()] for a particular run
3556
 
** or until the same information is requested
3557
 
** again in a different encoding.
3558
 
**
3559
 
** ^The names returned are the original un-aliased names of the
3560
 
** database, table, and column.
3561
 
**
3562
 
** ^The first argument to these interfaces is a [prepared statement].
3563
 
** ^These functions return information about the Nth result column returned by
3564
 
** the statement, where N is the second function argument.
3565
 
** ^The left-most column is column 0 for these routines.
3566
 
**
3567
 
** ^If the Nth column returned by the statement is an expression or
3568
 
** subquery and is not a column value, then all of these functions return
3569
 
** NULL.  ^These routine might also return NULL if a memory allocation error
3570
 
** occurs.  ^Otherwise, they return the name of the attached database, table,
3571
 
** or column that query result column was extracted from.
3572
 
**
3573
 
** ^As with all other SQLite APIs, those whose names end with "16" return
3574
 
** UTF-16 encoded strings and the other functions return UTF-8.
3575
 
**
3576
 
** ^These APIs are only available if the library was compiled with the
3577
 
** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
3578
 
**
3579
 
** If two or more threads call one or more of these routines against the same
3580
 
** prepared statement and column at the same time then the results are
3581
 
** undefined.
3582
 
**
3583
 
** If two or more threads call one or more
3584
 
** [sqlite3_column_database_name | column metadata interfaces]
3585
 
** for the same [prepared statement] and result column
3586
 
** at the same time then the results are undefined.
3587
 
*/
3588
 
SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
3589
 
SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
3590
 
SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
3591
 
SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
3592
 
SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
3593
 
SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
3594
 
 
3595
 
/*
3596
 
** CAPI3REF: Declared Datatype Of A Query Result
3597
 
**
3598
 
** ^(The first parameter is a [prepared statement].
3599
 
** If this statement is a [SELECT] statement and the Nth column of the
3600
 
** returned result set of that [SELECT] is a table column (not an
3601
 
** expression or subquery) then the declared type of the table
3602
 
** column is returned.)^  ^If the Nth column of the result set is an
3603
 
** expression or subquery, then a NULL pointer is returned.
3604
 
** ^The returned string is always UTF-8 encoded.
3605
 
**
3606
 
** ^(For example, given the database schema:
3607
 
**
3608
 
** CREATE TABLE t1(c1 VARIANT);
3609
 
**
3610
 
** and the following statement to be compiled:
3611
 
**
3612
 
** SELECT c1 + 1, c1 FROM t1;
3613
 
**
3614
 
** this routine would return the string "VARIANT" for the second result
3615
 
** column (i==1), and a NULL pointer for the first result column (i==0).)^
3616
 
**
3617
 
** ^SQLite uses dynamic run-time typing.  ^So just because a column
3618
 
** is declared to contain a particular type does not mean that the
3619
 
** data stored in that column is of the declared type.  SQLite is
3620
 
** strongly typed, but the typing is dynamic not static.  ^Type
3621
 
** is associated with individual values, not with the containers
3622
 
** used to hold those values.
3623
 
*/
3624
 
SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
3625
 
SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
3626
 
 
3627
 
/*
3628
 
** CAPI3REF: Evaluate An SQL Statement
3629
 
**
3630
 
** After a [prepared statement] has been prepared using either
3631
 
** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
3632
 
** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
3633
 
** must be called one or more times to evaluate the statement.
3634
 
**
3635
 
** The details of the behavior of the sqlite3_step() interface depend
3636
 
** on whether the statement was prepared using the newer "v2" interface
3637
 
** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
3638
 
** interface [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
3639
 
** new "v2" interface is recommended for new applications but the legacy
3640
 
** interface will continue to be supported.
3641
 
**
3642
 
** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
3643
 
** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
3644
 
** ^With the "v2" interface, any of the other [result codes] or
3645
 
** [extended result codes] might be returned as well.
3646
 
**
3647
 
** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
3648
 
** database locks it needs to do its job.  ^If the statement is a [COMMIT]
3649
 
** or occurs outside of an explicit transaction, then you can retry the
3650
 
** statement.  If the statement is not a [COMMIT] and occurs within a
3651
 
** explicit transaction then you should rollback the transaction before
3652
 
** continuing.
3653
 
**
3654
 
** ^[SQLITE_DONE] means that the statement has finished executing
3655
 
** successfully.  sqlite3_step() should not be called again on this virtual
3656
 
** machine without first calling [sqlite3_reset()] to reset the virtual
3657
 
** machine back to its initial state.
3658
 
**
3659
 
** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
3660
 
** is returned each time a new row of data is ready for processing by the
3661
 
** caller. The values may be accessed using the [column access functions].
3662
 
** sqlite3_step() is called again to retrieve the next row of data.
3663
 
**
3664
 
** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
3665
 
** violation) has occurred.  sqlite3_step() should not be called again on
3666
 
** the VM. More information may be found by calling [sqlite3_errmsg()].
3667
 
** ^With the legacy interface, a more specific error code (for example,
3668
 
** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
3669
 
** can be obtained by calling [sqlite3_reset()] on the
3670
 
** [prepared statement].  ^In the "v2" interface,
3671
 
** the more specific error code is returned directly by sqlite3_step().
3672
 
**
3673
 
** [SQLITE_MISUSE] means that the this routine was called inappropriately.
3674
 
** Perhaps it was called on a [prepared statement] that has
3675
 
** already been [sqlite3_finalize | finalized] or on one that had
3676
 
** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
3677
 
** be the case that the same database connection is being used by two or
3678
 
** more threads at the same moment in time.
3679
 
**
3680
 
** For all versions of SQLite up to and including 3.6.23.1, a call to
3681
 
** [sqlite3_reset()] was required after sqlite3_step() returned anything
3682
 
** other than [SQLITE_ROW] before any subsequent invocation of
3683
 
** sqlite3_step().  Failure to reset the prepared statement using 
3684
 
** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
3685
 
** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
3686
 
** calling [sqlite3_reset()] automatically in this circumstance rather
3687
 
** than returning [SQLITE_MISUSE].  This is not considered a compatibility
3688
 
** break because any application that ever receives an SQLITE_MISUSE error
3689
 
** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
3690
 
** can be used to restore the legacy behavior.
3691
 
**
3692
 
** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
3693
 
** API always returns a generic error code, [SQLITE_ERROR], following any
3694
 
** error other than [SQLITE_BUSY] and [SQLITE_MISUSE].  You must call
3695
 
** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
3696
 
** specific [error codes] that better describes the error.
3697
 
** We admit that this is a goofy design.  The problem has been fixed
3698
 
** with the "v2" interface.  If you prepare all of your SQL statements
3699
 
** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
3700
 
** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
3701
 
** then the more specific [error codes] are returned directly
3702
 
** by sqlite3_step().  The use of the "v2" interface is recommended.
3703
 
*/
3704
 
SQLITE_API int sqlite3_step(sqlite3_stmt*);
3705
 
 
3706
 
/*
3707
 
** CAPI3REF: Number of columns in a result set
3708
 
**
3709
 
** ^The sqlite3_data_count(P) interface returns the number of columns in the
3710
 
** current row of the result set of [prepared statement] P.
3711
 
** ^If prepared statement P does not have results ready to return
3712
 
** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
3713
 
** interfaces) then sqlite3_data_count(P) returns 0.
3714
 
** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
3715
 
**
3716
 
** See also: [sqlite3_column_count()]
3717
 
*/
3718
 
SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
3719
 
 
3720
 
/*
3721
 
** CAPI3REF: Fundamental Datatypes
3722
 
** KEYWORDS: SQLITE_TEXT
3723
 
**
3724
 
** ^(Every value in SQLite has one of five fundamental datatypes:
3725
 
**
3726
 
** <ul>
3727
 
** <li> 64-bit signed integer
3728
 
** <li> 64-bit IEEE floating point number
3729
 
** <li> string
3730
 
** <li> BLOB
3731
 
** <li> NULL
3732
 
** </ul>)^
3733
 
**
3734
 
** These constants are codes for each of those types.
3735
 
**
3736
 
** Note that the SQLITE_TEXT constant was also used in SQLite version 2
3737
 
** for a completely different meaning.  Software that links against both
3738
 
** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
3739
 
** SQLITE_TEXT.
3740
 
*/
3741
 
#define SQLITE_INTEGER  1
3742
 
#define SQLITE_FLOAT    2
3743
 
#define SQLITE_BLOB     4
3744
 
#define SQLITE_NULL     5
3745
 
#ifdef SQLITE_TEXT
3746
 
# undef SQLITE_TEXT
3747
 
#else
3748
 
# define SQLITE_TEXT     3
3749
 
#endif
3750
 
#define SQLITE3_TEXT     3
3751
 
 
3752
 
/*
3753
 
** CAPI3REF: Result Values From A Query
3754
 
** KEYWORDS: {column access functions}
3755
 
**
3756
 
** These routines form the "result set" interface.
3757
 
**
3758
 
** ^These routines return information about a single column of the current
3759
 
** result row of a query.  ^In every case the first argument is a pointer
3760
 
** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
3761
 
** that was returned from [sqlite3_prepare_v2()] or one of its variants)
3762
 
** and the second argument is the index of the column for which information
3763
 
** should be returned. ^The leftmost column of the result set has the index 0.
3764
 
** ^The number of columns in the result can be determined using
3765
 
** [sqlite3_column_count()].
3766
 
**
3767
 
** If the SQL statement does not currently point to a valid row, or if the
3768
 
** column index is out of range, the result is undefined.
3769
 
** These routines may only be called when the most recent call to
3770
 
** [sqlite3_step()] has returned [SQLITE_ROW] and neither
3771
 
** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
3772
 
** If any of these routines are called after [sqlite3_reset()] or
3773
 
** [sqlite3_finalize()] or after [sqlite3_step()] has returned
3774
 
** something other than [SQLITE_ROW], the results are undefined.
3775
 
** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
3776
 
** are called from a different thread while any of these routines
3777
 
** are pending, then the results are undefined.
3778
 
**
3779
 
** ^The sqlite3_column_type() routine returns the
3780
 
** [SQLITE_INTEGER | datatype code] for the initial data type
3781
 
** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
3782
 
** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].  The value
3783
 
** returned by sqlite3_column_type() is only meaningful if no type
3784
 
** conversions have occurred as described below.  After a type conversion,
3785
 
** the value returned by sqlite3_column_type() is undefined.  Future
3786
 
** versions of SQLite may change the behavior of sqlite3_column_type()
3787
 
** following a type conversion.
3788
 
**
3789
 
** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
3790
 
** routine returns the number of bytes in that BLOB or string.
3791
 
** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
3792
 
** the string to UTF-8 and then returns the number of bytes.
3793
 
** ^If the result is a numeric value then sqlite3_column_bytes() uses
3794
 
** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
3795
 
** the number of bytes in that string.
3796
 
** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
3797
 
**
3798
 
** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
3799
 
** routine returns the number of bytes in that BLOB or string.
3800
 
** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
3801
 
** the string to UTF-16 and then returns the number of bytes.
3802
 
** ^If the result is a numeric value then sqlite3_column_bytes16() uses
3803
 
** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
3804
 
** the number of bytes in that string.
3805
 
** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
3806
 
**
3807
 
** ^The values returned by [sqlite3_column_bytes()] and 
3808
 
** [sqlite3_column_bytes16()] do not include the zero terminators at the end
3809
 
** of the string.  ^For clarity: the values returned by
3810
 
** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
3811
 
** bytes in the string, not the number of characters.
3812
 
**
3813
 
** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
3814
 
** even empty strings, are always zero terminated.  ^The return
3815
 
** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
3816
 
**
3817
 
** ^The object returned by [sqlite3_column_value()] is an
3818
 
** [unprotected sqlite3_value] object.  An unprotected sqlite3_value object
3819
 
** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
3820
 
** If the [unprotected sqlite3_value] object returned by
3821
 
** [sqlite3_column_value()] is used in any other way, including calls
3822
 
** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
3823
 
** or [sqlite3_value_bytes()], then the behavior is undefined.
3824
 
**
3825
 
** These routines attempt to convert the value where appropriate.  ^For
3826
 
** example, if the internal representation is FLOAT and a text result
3827
 
** is requested, [sqlite3_snprintf()] is used internally to perform the
3828
 
** conversion automatically.  ^(The following table details the conversions
3829
 
** that are applied:
3830
 
**
3831
 
** <blockquote>
3832
 
** <table border="1">
3833
 
** <tr><th> Internal<br>Type <th> Requested<br>Type <th>  Conversion
3834
 
**
3835
 
** <tr><td>  NULL    <td> INTEGER   <td> Result is 0
3836
 
** <tr><td>  NULL    <td>  FLOAT    <td> Result is 0.0
3837
 
** <tr><td>  NULL    <td>   TEXT    <td> Result is NULL pointer
3838
 
** <tr><td>  NULL    <td>   BLOB    <td> Result is NULL pointer
3839
 
** <tr><td> INTEGER  <td>  FLOAT    <td> Convert from integer to float
3840
 
** <tr><td> INTEGER  <td>   TEXT    <td> ASCII rendering of the integer
3841
 
** <tr><td> INTEGER  <td>   BLOB    <td> Same as INTEGER->TEXT
3842
 
** <tr><td>  FLOAT   <td> INTEGER   <td> Convert from float to integer
3843
 
** <tr><td>  FLOAT   <td>   TEXT    <td> ASCII rendering of the float
3844
 
** <tr><td>  FLOAT   <td>   BLOB    <td> Same as FLOAT->TEXT
3845
 
** <tr><td>  TEXT    <td> INTEGER   <td> Use atoi()
3846
 
** <tr><td>  TEXT    <td>  FLOAT    <td> Use atof()
3847
 
** <tr><td>  TEXT    <td>   BLOB    <td> No change
3848
 
** <tr><td>  BLOB    <td> INTEGER   <td> Convert to TEXT then use atoi()
3849
 
** <tr><td>  BLOB    <td>  FLOAT    <td> Convert to TEXT then use atof()
3850
 
** <tr><td>  BLOB    <td>   TEXT    <td> Add a zero terminator if needed
3851
 
** </table>
3852
 
** </blockquote>)^
3853
 
**
3854
 
** The table above makes reference to standard C library functions atoi()
3855
 
** and atof().  SQLite does not really use these functions.  It has its
3856
 
** own equivalent internal routines.  The atoi() and atof() names are
3857
 
** used in the table for brevity and because they are familiar to most
3858
 
** C programmers.
3859
 
**
3860
 
** Note that when type conversions occur, pointers returned by prior
3861
 
** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
3862
 
** sqlite3_column_text16() may be invalidated.
3863
 
** Type conversions and pointer invalidations might occur
3864
 
** in the following cases:
3865
 
**
3866
 
** <ul>
3867
 
** <li> The initial content is a BLOB and sqlite3_column_text() or
3868
 
**      sqlite3_column_text16() is called.  A zero-terminator might
3869
 
**      need to be added to the string.</li>
3870
 
** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
3871
 
**      sqlite3_column_text16() is called.  The content must be converted
3872
 
**      to UTF-16.</li>
3873
 
** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
3874
 
**      sqlite3_column_text() is called.  The content must be converted
3875
 
**      to UTF-8.</li>
3876
 
** </ul>
3877
 
**
3878
 
** ^Conversions between UTF-16be and UTF-16le are always done in place and do
3879
 
** not invalidate a prior pointer, though of course the content of the buffer
3880
 
** that the prior pointer references will have been modified.  Other kinds
3881
 
** of conversion are done in place when it is possible, but sometimes they
3882
 
** are not possible and in those cases prior pointers are invalidated.
3883
 
**
3884
 
** The safest and easiest to remember policy is to invoke these routines
3885
 
** in one of the following ways:
3886
 
**
3887
 
** <ul>
3888
 
**  <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
3889
 
**  <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
3890
 
**  <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
3891
 
** </ul>
3892
 
**
3893
 
** In other words, you should call sqlite3_column_text(),
3894
 
** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
3895
 
** into the desired format, then invoke sqlite3_column_bytes() or
3896
 
** sqlite3_column_bytes16() to find the size of the result.  Do not mix calls
3897
 
** to sqlite3_column_text() or sqlite3_column_blob() with calls to
3898
 
** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
3899
 
** with calls to sqlite3_column_bytes().
3900
 
**
3901
 
** ^The pointers returned are valid until a type conversion occurs as
3902
 
** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
3903
 
** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
3904
 
** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
3905
 
** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
3906
 
** [sqlite3_free()].
3907
 
**
3908
 
** ^(If a memory allocation error occurs during the evaluation of any
3909
 
** of these routines, a default value is returned.  The default value
3910
 
** is either the integer 0, the floating point number 0.0, or a NULL
3911
 
** pointer.  Subsequent calls to [sqlite3_errcode()] will return
3912
 
** [SQLITE_NOMEM].)^
3913
 
*/
3914
 
SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
3915
 
SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
3916
 
SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
3917
 
SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
3918
 
SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
3919
 
SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
3920
 
SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
3921
 
SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
3922
 
SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
3923
 
SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
3924
 
 
3925
 
/*
3926
 
** CAPI3REF: Destroy A Prepared Statement Object
3927
 
**
3928
 
** ^The sqlite3_finalize() function is called to delete a [prepared statement].
3929
 
** ^If the most recent evaluation of the statement encountered no errors or
3930
 
** or if the statement is never been evaluated, then sqlite3_finalize() returns
3931
 
** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
3932
 
** sqlite3_finalize(S) returns the appropriate [error code] or
3933
 
** [extended error code].
3934
 
**
3935
 
** ^The sqlite3_finalize(S) routine can be called at any point during
3936
 
** the life cycle of [prepared statement] S:
3937
 
** before statement S is ever evaluated, after
3938
 
** one or more calls to [sqlite3_reset()], or after any call
3939
 
** to [sqlite3_step()] regardless of whether or not the statement has
3940
 
** completed execution.
3941
 
**
3942
 
** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
3943
 
**
3944
 
** The application must finalize every [prepared statement] in order to avoid
3945
 
** resource leaks.  It is a grievous error for the application to try to use
3946
 
** a prepared statement after it has been finalized.  Any use of a prepared
3947
 
** statement after it has been finalized can result in undefined and
3948
 
** undesirable behavior such as segfaults and heap corruption.
3949
 
*/
3950
 
SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
3951
 
 
3952
 
/*
3953
 
** CAPI3REF: Reset A Prepared Statement Object
3954
 
**
3955
 
** The sqlite3_reset() function is called to reset a [prepared statement]
3956
 
** object back to its initial state, ready to be re-executed.
3957
 
** ^Any SQL statement variables that had values bound to them using
3958
 
** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
3959
 
** Use [sqlite3_clear_bindings()] to reset the bindings.
3960
 
**
3961
 
** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
3962
 
** back to the beginning of its program.
3963
 
**
3964
 
** ^If the most recent call to [sqlite3_step(S)] for the
3965
 
** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
3966
 
** or if [sqlite3_step(S)] has never before been called on S,
3967
 
** then [sqlite3_reset(S)] returns [SQLITE_OK].
3968
 
**
3969
 
** ^If the most recent call to [sqlite3_step(S)] for the
3970
 
** [prepared statement] S indicated an error, then
3971
 
** [sqlite3_reset(S)] returns an appropriate [error code].
3972
 
**
3973
 
** ^The [sqlite3_reset(S)] interface does not change the values
3974
 
** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
3975
 
*/
3976
 
SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
3977
 
 
3978
 
/*
3979
 
** CAPI3REF: Create Or Redefine SQL Functions
3980
 
** KEYWORDS: {function creation routines}
3981
 
** KEYWORDS: {application-defined SQL function}
3982
 
** KEYWORDS: {application-defined SQL functions}
3983
 
**
3984
 
** ^These functions (collectively known as "function creation routines")
3985
 
** are used to add SQL functions or aggregates or to redefine the behavior
3986
 
** of existing SQL functions or aggregates.  The only differences between
3987
 
** these routines are the text encoding expected for
3988
 
** the second parameter (the name of the function being created)
3989
 
** and the presence or absence of a destructor callback for
3990
 
** the application data pointer.
3991
 
**
3992
 
** ^The first parameter is the [database connection] to which the SQL
3993
 
** function is to be added.  ^If an application uses more than one database
3994
 
** connection then application-defined SQL functions must be added
3995
 
** to each database connection separately.
3996
 
**
3997
 
** ^The second parameter is the name of the SQL function to be created or
3998
 
** redefined.  ^The length of the name is limited to 255 bytes in a UTF-8
3999
 
** representation, exclusive of the zero-terminator.  ^Note that the name
4000
 
** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.  
4001
 
** ^Any attempt to create a function with a longer name
4002
 
** will result in [SQLITE_MISUSE] being returned.
4003
 
**
4004
 
** ^The third parameter (nArg)
4005
 
** is the number of arguments that the SQL function or
4006
 
** aggregate takes. ^If this parameter is -1, then the SQL function or
4007
 
** aggregate may take any number of arguments between 0 and the limit
4008
 
** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]).  If the third
4009
 
** parameter is less than -1 or greater than 127 then the behavior is
4010
 
** undefined.
4011
 
**
4012
 
** ^The fourth parameter, eTextRep, specifies what
4013
 
** [SQLITE_UTF8 | text encoding] this SQL function prefers for
4014
 
** its parameters.  Every SQL function implementation must be able to work
4015
 
** with UTF-8, UTF-16le, or UTF-16be.  But some implementations may be
4016
 
** more efficient with one encoding than another.  ^An application may
4017
 
** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
4018
 
** times with the same function but with different values of eTextRep.
4019
 
** ^When multiple implementations of the same function are available, SQLite
4020
 
** will pick the one that involves the least amount of data conversion.
4021
 
** If there is only a single implementation which does not care what text
4022
 
** encoding is used, then the fourth argument should be [SQLITE_ANY].
4023
 
**
4024
 
** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
4025
 
** function can gain access to this pointer using [sqlite3_user_data()].)^
4026
 
**
4027
 
** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
4028
 
** pointers to C-language functions that implement the SQL function or
4029
 
** aggregate. ^A scalar SQL function requires an implementation of the xFunc
4030
 
** callback only; NULL pointers must be passed as the xStep and xFinal
4031
 
** parameters. ^An aggregate SQL function requires an implementation of xStep
4032
 
** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
4033
 
** SQL function or aggregate, pass NULL pointers for all three function
4034
 
** callbacks.
4035
 
**
4036
 
** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
4037
 
** then it is destructor for the application data pointer. 
4038
 
** The destructor is invoked when the function is deleted, either by being
4039
 
** overloaded or when the database connection closes.)^
4040
 
** ^The destructor is also invoked if the call to
4041
 
** sqlite3_create_function_v2() fails.
4042
 
** ^When the destructor callback of the tenth parameter is invoked, it
4043
 
** is passed a single argument which is a copy of the application data 
4044
 
** pointer which was the fifth parameter to sqlite3_create_function_v2().
4045
 
**
4046
 
** ^It is permitted to register multiple implementations of the same
4047
 
** functions with the same name but with either differing numbers of
4048
 
** arguments or differing preferred text encodings.  ^SQLite will use
4049
 
** the implementation that most closely matches the way in which the
4050
 
** SQL function is used.  ^A function implementation with a non-negative
4051
 
** nArg parameter is a better match than a function implementation with
4052
 
** a negative nArg.  ^A function where the preferred text encoding
4053
 
** matches the database encoding is a better
4054
 
** match than a function where the encoding is different.  
4055
 
** ^A function where the encoding difference is between UTF16le and UTF16be
4056
 
** is a closer match than a function where the encoding difference is
4057
 
** between UTF8 and UTF16.
4058
 
**
4059
 
** ^Built-in functions may be overloaded by new application-defined functions.
4060
 
**
4061
 
** ^An application-defined function is permitted to call other
4062
 
** SQLite interfaces.  However, such calls must not
4063
 
** close the database connection nor finalize or reset the prepared
4064
 
** statement in which the function is running.
4065
 
*/
4066
 
SQLITE_API int sqlite3_create_function(
4067
 
  sqlite3 *db,
4068
 
  const char *zFunctionName,
4069
 
  int nArg,
4070
 
  int eTextRep,
4071
 
  void *pApp,
4072
 
  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4073
 
  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4074
 
  void (*xFinal)(sqlite3_context*)
4075
 
);
4076
 
SQLITE_API int sqlite3_create_function16(
4077
 
  sqlite3 *db,
4078
 
  const void *zFunctionName,
4079
 
  int nArg,
4080
 
  int eTextRep,
4081
 
  void *pApp,
4082
 
  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4083
 
  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4084
 
  void (*xFinal)(sqlite3_context*)
4085
 
);
4086
 
SQLITE_API int sqlite3_create_function_v2(
4087
 
  sqlite3 *db,
4088
 
  const char *zFunctionName,
4089
 
  int nArg,
4090
 
  int eTextRep,
4091
 
  void *pApp,
4092
 
  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4093
 
  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4094
 
  void (*xFinal)(sqlite3_context*),
4095
 
  void(*xDestroy)(void*)
4096
 
);
4097
 
 
4098
 
/*
4099
 
** CAPI3REF: Text Encodings
4100
 
**
4101
 
** These constant define integer codes that represent the various
4102
 
** text encodings supported by SQLite.
4103
 
*/
4104
 
#define SQLITE_UTF8           1
4105
 
#define SQLITE_UTF16LE        2
4106
 
#define SQLITE_UTF16BE        3
4107
 
#define SQLITE_UTF16          4    /* Use native byte order */
4108
 
#define SQLITE_ANY            5    /* sqlite3_create_function only */
4109
 
#define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
4110
 
 
4111
 
/*
4112
 
** CAPI3REF: Deprecated Functions
4113
 
** DEPRECATED
4114
 
**
4115
 
** These functions are [deprecated].  In order to maintain
4116
 
** backwards compatibility with older code, these functions continue 
4117
 
** to be supported.  However, new applications should avoid
4118
 
** the use of these functions.  To help encourage people to avoid
4119
 
** using these functions, we are not going to tell you what they do.
4120
 
*/
4121
 
#ifndef SQLITE_OMIT_DEPRECATED
4122
 
SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
4123
 
SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
4124
 
SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
4125
 
SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
4126
 
SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
4127
 
SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64);
4128
 
#endif
4129
 
 
4130
 
/*
4131
 
** CAPI3REF: Obtaining SQL Function Parameter Values
4132
 
**
4133
 
** The C-language implementation of SQL functions and aggregates uses
4134
 
** this set of interface routines to access the parameter values on
4135
 
** the function or aggregate.
4136
 
**
4137
 
** The xFunc (for scalar functions) or xStep (for aggregates) parameters
4138
 
** to [sqlite3_create_function()] and [sqlite3_create_function16()]
4139
 
** define callbacks that implement the SQL functions and aggregates.
4140
 
** The 3rd parameter to these callbacks is an array of pointers to
4141
 
** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
4142
 
** each parameter to the SQL function.  These routines are used to
4143
 
** extract values from the [sqlite3_value] objects.
4144
 
**
4145
 
** These routines work only with [protected sqlite3_value] objects.
4146
 
** Any attempt to use these routines on an [unprotected sqlite3_value]
4147
 
** object results in undefined behavior.
4148
 
**
4149
 
** ^These routines work just like the corresponding [column access functions]
4150
 
** except that  these routines take a single [protected sqlite3_value] object
4151
 
** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
4152
 
**
4153
 
** ^The sqlite3_value_text16() interface extracts a UTF-16 string
4154
 
** in the native byte-order of the host machine.  ^The
4155
 
** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
4156
 
** extract UTF-16 strings as big-endian and little-endian respectively.
4157
 
**
4158
 
** ^(The sqlite3_value_numeric_type() interface attempts to apply
4159
 
** numeric affinity to the value.  This means that an attempt is
4160
 
** made to convert the value to an integer or floating point.  If
4161
 
** such a conversion is possible without loss of information (in other
4162
 
** words, if the value is a string that looks like a number)
4163
 
** then the conversion is performed.  Otherwise no conversion occurs.
4164
 
** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
4165
 
**
4166
 
** Please pay particular attention to the fact that the pointer returned
4167
 
** from [sqlite3_value_blob()], [sqlite3_value_text()], or
4168
 
** [sqlite3_value_text16()] can be invalidated by a subsequent call to
4169
 
** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
4170
 
** or [sqlite3_value_text16()].
4171
 
**
4172
 
** These routines must be called from the same thread as
4173
 
** the SQL function that supplied the [sqlite3_value*] parameters.
4174
 
*/
4175
 
SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
4176
 
SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
4177
 
SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
4178
 
SQLITE_API double sqlite3_value_double(sqlite3_value*);
4179
 
SQLITE_API int sqlite3_value_int(sqlite3_value*);
4180
 
SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
4181
 
SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
4182
 
SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
4183
 
SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
4184
 
SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
4185
 
SQLITE_API int sqlite3_value_type(sqlite3_value*);
4186
 
SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
4187
 
 
4188
 
/*
4189
 
** CAPI3REF: Obtain Aggregate Function Context
4190
 
**
4191
 
** Implementations of aggregate SQL functions use this
4192
 
** routine to allocate memory for storing their state.
4193
 
**
4194
 
** ^The first time the sqlite3_aggregate_context(C,N) routine is called 
4195
 
** for a particular aggregate function, SQLite
4196
 
** allocates N of memory, zeroes out that memory, and returns a pointer
4197
 
** to the new memory. ^On second and subsequent calls to
4198
 
** sqlite3_aggregate_context() for the same aggregate function instance,
4199
 
** the same buffer is returned.  Sqlite3_aggregate_context() is normally
4200
 
** called once for each invocation of the xStep callback and then one
4201
 
** last time when the xFinal callback is invoked.  ^(When no rows match
4202
 
** an aggregate query, the xStep() callback of the aggregate function
4203
 
** implementation is never called and xFinal() is called exactly once.
4204
 
** In those cases, sqlite3_aggregate_context() might be called for the
4205
 
** first time from within xFinal().)^
4206
 
**
4207
 
** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer if N is
4208
 
** less than or equal to zero or if a memory allocate error occurs.
4209
 
**
4210
 
** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
4211
 
** determined by the N parameter on first successful call.  Changing the
4212
 
** value of N in subsequent call to sqlite3_aggregate_context() within
4213
 
** the same aggregate function instance will not resize the memory
4214
 
** allocation.)^
4215
 
**
4216
 
** ^SQLite automatically frees the memory allocated by 
4217
 
** sqlite3_aggregate_context() when the aggregate query concludes.
4218
 
**
4219
 
** The first parameter must be a copy of the
4220
 
** [sqlite3_context | SQL function context] that is the first parameter
4221
 
** to the xStep or xFinal callback routine that implements the aggregate
4222
 
** function.
4223
 
**
4224
 
** This routine must be called from the same thread in which
4225
 
** the aggregate SQL function is running.
4226
 
*/
4227
 
SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
4228
 
 
4229
 
/*
4230
 
** CAPI3REF: User Data For Functions
4231
 
**
4232
 
** ^The sqlite3_user_data() interface returns a copy of
4233
 
** the pointer that was the pUserData parameter (the 5th parameter)
4234
 
** of the [sqlite3_create_function()]
4235
 
** and [sqlite3_create_function16()] routines that originally
4236
 
** registered the application defined function.
4237
 
**
4238
 
** This routine must be called from the same thread in which
4239
 
** the application-defined function is running.
4240
 
*/
4241
 
SQLITE_API void *sqlite3_user_data(sqlite3_context*);
4242
 
 
4243
 
/*
4244
 
** CAPI3REF: Database Connection For Functions
4245
 
**
4246
 
** ^The sqlite3_context_db_handle() interface returns a copy of
4247
 
** the pointer to the [database connection] (the 1st parameter)
4248
 
** of the [sqlite3_create_function()]
4249
 
** and [sqlite3_create_function16()] routines that originally
4250
 
** registered the application defined function.
4251
 
*/
4252
 
SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
4253
 
 
4254
 
/*
4255
 
** CAPI3REF: Function Auxiliary Data
4256
 
**
4257
 
** The following two functions may be used by scalar SQL functions to
4258
 
** associate metadata with argument values. If the same value is passed to
4259
 
** multiple invocations of the same SQL function during query execution, under
4260
 
** some circumstances the associated metadata may be preserved. This may
4261
 
** be used, for example, to add a regular-expression matching scalar
4262
 
** function. The compiled version of the regular expression is stored as
4263
 
** metadata associated with the SQL value passed as the regular expression
4264
 
** pattern.  The compiled regular expression can be reused on multiple
4265
 
** invocations of the same function so that the original pattern string
4266
 
** does not need to be recompiled on each invocation.
4267
 
**
4268
 
** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
4269
 
** associated by the sqlite3_set_auxdata() function with the Nth argument
4270
 
** value to the application-defined function. ^If no metadata has been ever
4271
 
** been set for the Nth argument of the function, or if the corresponding
4272
 
** function parameter has changed since the meta-data was set,
4273
 
** then sqlite3_get_auxdata() returns a NULL pointer.
4274
 
**
4275
 
** ^The sqlite3_set_auxdata() interface saves the metadata
4276
 
** pointed to by its 3rd parameter as the metadata for the N-th
4277
 
** argument of the application-defined function.  Subsequent
4278
 
** calls to sqlite3_get_auxdata() might return this data, if it has
4279
 
** not been destroyed.
4280
 
** ^If it is not NULL, SQLite will invoke the destructor
4281
 
** function given by the 4th parameter to sqlite3_set_auxdata() on
4282
 
** the metadata when the corresponding function parameter changes
4283
 
** or when the SQL statement completes, whichever comes first.
4284
 
**
4285
 
** SQLite is free to call the destructor and drop metadata on any
4286
 
** parameter of any function at any time.  ^The only guarantee is that
4287
 
** the destructor will be called before the metadata is dropped.
4288
 
**
4289
 
** ^(In practice, metadata is preserved between function calls for
4290
 
** expressions that are constant at compile time. This includes literal
4291
 
** values and [parameters].)^
4292
 
**
4293
 
** These routines must be called from the same thread in which
4294
 
** the SQL function is running.
4295
 
*/
4296
 
SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
4297
 
SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
4298
 
 
4299
 
 
4300
 
/*
4301
 
** CAPI3REF: Constants Defining Special Destructor Behavior
4302
 
**
4303
 
** These are special values for the destructor that is passed in as the
4304
 
** final argument to routines like [sqlite3_result_blob()].  ^If the destructor
4305
 
** argument is SQLITE_STATIC, it means that the content pointer is constant
4306
 
** and will never change.  It does not need to be destroyed.  ^The
4307
 
** SQLITE_TRANSIENT value means that the content will likely change in
4308
 
** the near future and that SQLite should make its own private copy of
4309
 
** the content before returning.
4310
 
**
4311
 
** The typedef is necessary to work around problems in certain
4312
 
** C++ compilers.  See ticket #2191.
4313
 
*/
4314
 
typedef void (*sqlite3_destructor_type)(void*);
4315
 
#define SQLITE_STATIC      ((sqlite3_destructor_type)0)
4316
 
#define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
4317
 
 
4318
 
/*
4319
 
** CAPI3REF: Setting The Result Of An SQL Function
4320
 
**
4321
 
** These routines are used by the xFunc or xFinal callbacks that
4322
 
** implement SQL functions and aggregates.  See
4323
 
** [sqlite3_create_function()] and [sqlite3_create_function16()]
4324
 
** for additional information.
4325
 
**
4326
 
** These functions work very much like the [parameter binding] family of
4327
 
** functions used to bind values to host parameters in prepared statements.
4328
 
** Refer to the [SQL parameter] documentation for additional information.
4329
 
**
4330
 
** ^The sqlite3_result_blob() interface sets the result from
4331
 
** an application-defined function to be the BLOB whose content is pointed
4332
 
** to by the second parameter and which is N bytes long where N is the
4333
 
** third parameter.
4334
 
**
4335
 
** ^The sqlite3_result_zeroblob() interfaces set the result of
4336
 
** the application-defined function to be a BLOB containing all zero
4337
 
** bytes and N bytes in size, where N is the value of the 2nd parameter.
4338
 
**
4339
 
** ^The sqlite3_result_double() interface sets the result from
4340
 
** an application-defined function to be a floating point value specified
4341
 
** by its 2nd argument.
4342
 
**
4343
 
** ^The sqlite3_result_error() and sqlite3_result_error16() functions
4344
 
** cause the implemented SQL function to throw an exception.
4345
 
** ^SQLite uses the string pointed to by the
4346
 
** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
4347
 
** as the text of an error message.  ^SQLite interprets the error
4348
 
** message string from sqlite3_result_error() as UTF-8. ^SQLite
4349
 
** interprets the string from sqlite3_result_error16() as UTF-16 in native
4350
 
** byte order.  ^If the third parameter to sqlite3_result_error()
4351
 
** or sqlite3_result_error16() is negative then SQLite takes as the error
4352
 
** message all text up through the first zero character.
4353
 
** ^If the third parameter to sqlite3_result_error() or
4354
 
** sqlite3_result_error16() is non-negative then SQLite takes that many
4355
 
** bytes (not characters) from the 2nd parameter as the error message.
4356
 
** ^The sqlite3_result_error() and sqlite3_result_error16()
4357
 
** routines make a private copy of the error message text before
4358
 
** they return.  Hence, the calling function can deallocate or
4359
 
** modify the text after they return without harm.
4360
 
** ^The sqlite3_result_error_code() function changes the error code
4361
 
** returned by SQLite as a result of an error in a function.  ^By default,
4362
 
** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
4363
 
** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
4364
 
**
4365
 
** ^The sqlite3_result_toobig() interface causes SQLite to throw an error
4366
 
** indicating that a string or BLOB is too long to represent.
4367
 
**
4368
 
** ^The sqlite3_result_nomem() interface causes SQLite to throw an error
4369
 
** indicating that a memory allocation failed.
4370
 
**
4371
 
** ^The sqlite3_result_int() interface sets the return value
4372
 
** of the application-defined function to be the 32-bit signed integer
4373
 
** value given in the 2nd argument.
4374
 
** ^The sqlite3_result_int64() interface sets the return value
4375
 
** of the application-defined function to be the 64-bit signed integer
4376
 
** value given in the 2nd argument.
4377
 
**
4378
 
** ^The sqlite3_result_null() interface sets the return value
4379
 
** of the application-defined function to be NULL.
4380
 
**
4381
 
** ^The sqlite3_result_text(), sqlite3_result_text16(),
4382
 
** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
4383
 
** set the return value of the application-defined function to be
4384
 
** a text string which is represented as UTF-8, UTF-16 native byte order,
4385
 
** UTF-16 little endian, or UTF-16 big endian, respectively.
4386
 
** ^SQLite takes the text result from the application from
4387
 
** the 2nd parameter of the sqlite3_result_text* interfaces.
4388
 
** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4389
 
** is negative, then SQLite takes result text from the 2nd parameter
4390
 
** through the first zero character.
4391
 
** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4392
 
** is non-negative, then as many bytes (not characters) of the text
4393
 
** pointed to by the 2nd parameter are taken as the application-defined
4394
 
** function result.
4395
 
** ^If the 4th parameter to the sqlite3_result_text* interfaces
4396
 
** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
4397
 
** function as the destructor on the text or BLOB result when it has
4398
 
** finished using that result.
4399
 
** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
4400
 
** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
4401
 
** assumes that the text or BLOB result is in constant space and does not
4402
 
** copy the content of the parameter nor call a destructor on the content
4403
 
** when it has finished using that result.
4404
 
** ^If the 4th parameter to the sqlite3_result_text* interfaces
4405
 
** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
4406
 
** then SQLite makes a copy of the result into space obtained from
4407
 
** from [sqlite3_malloc()] before it returns.
4408
 
**
4409
 
** ^The sqlite3_result_value() interface sets the result of
4410
 
** the application-defined function to be a copy the
4411
 
** [unprotected sqlite3_value] object specified by the 2nd parameter.  ^The
4412
 
** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
4413
 
** so that the [sqlite3_value] specified in the parameter may change or
4414
 
** be deallocated after sqlite3_result_value() returns without harm.
4415
 
** ^A [protected sqlite3_value] object may always be used where an
4416
 
** [unprotected sqlite3_value] object is required, so either
4417
 
** kind of [sqlite3_value] object can be used with this interface.
4418
 
**
4419
 
** If these routines are called from within the different thread
4420
 
** than the one containing the application-defined function that received
4421
 
** the [sqlite3_context] pointer, the results are undefined.
4422
 
*/
4423
 
SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
4424
 
SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
4425
 
SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
4426
 
SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
4427
 
SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
4428
 
SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
4429
 
SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
4430
 
SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
4431
 
SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
4432
 
SQLITE_API void sqlite3_result_null(sqlite3_context*);
4433
 
SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
4434
 
SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
4435
 
SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
4436
 
SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
4437
 
SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
4438
 
SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
4439
 
 
4440
 
/*
4441
 
** CAPI3REF: Define New Collating Sequences
4442
 
**
4443
 
** ^These functions add, remove, or modify a [collation] associated
4444
 
** with the [database connection] specified as the first argument.
4445
 
**
4446
 
** ^The name of the collation is a UTF-8 string
4447
 
** for sqlite3_create_collation() and sqlite3_create_collation_v2()
4448
 
** and a UTF-16 string in native byte order for sqlite3_create_collation16().
4449
 
** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
4450
 
** considered to be the same name.
4451
 
**
4452
 
** ^(The third argument (eTextRep) must be one of the constants:
4453
 
** <ul>
4454
 
** <li> [SQLITE_UTF8],
4455
 
** <li> [SQLITE_UTF16LE],
4456
 
** <li> [SQLITE_UTF16BE],
4457
 
** <li> [SQLITE_UTF16], or
4458
 
** <li> [SQLITE_UTF16_ALIGNED].
4459
 
** </ul>)^
4460
 
** ^The eTextRep argument determines the encoding of strings passed
4461
 
** to the collating function callback, xCallback.
4462
 
** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
4463
 
** force strings to be UTF16 with native byte order.
4464
 
** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
4465
 
** on an even byte address.
4466
 
**
4467
 
** ^The fourth argument, pArg, is an application data pointer that is passed
4468
 
** through as the first argument to the collating function callback.
4469
 
**
4470
 
** ^The fifth argument, xCallback, is a pointer to the collating function.
4471
 
** ^Multiple collating functions can be registered using the same name but
4472
 
** with different eTextRep parameters and SQLite will use whichever
4473
 
** function requires the least amount of data transformation.
4474
 
** ^If the xCallback argument is NULL then the collating function is
4475
 
** deleted.  ^When all collating functions having the same name are deleted,
4476
 
** that collation is no longer usable.
4477
 
**
4478
 
** ^The collating function callback is invoked with a copy of the pArg 
4479
 
** application data pointer and with two strings in the encoding specified
4480
 
** by the eTextRep argument.  The collating function must return an
4481
 
** integer that is negative, zero, or positive
4482
 
** if the first string is less than, equal to, or greater than the second,
4483
 
** respectively.  A collating function must always return the same answer
4484
 
** given the same inputs.  If two or more collating functions are registered
4485
 
** to the same collation name (using different eTextRep values) then all
4486
 
** must give an equivalent answer when invoked with equivalent strings.
4487
 
** The collating function must obey the following properties for all
4488
 
** strings A, B, and C:
4489
 
**
4490
 
** <ol>
4491
 
** <li> If A==B then B==A.
4492
 
** <li> If A==B and B==C then A==C.
4493
 
** <li> If A&lt;B THEN B&gt;A.
4494
 
** <li> If A&lt;B and B&lt;C then A&lt;C.
4495
 
** </ol>
4496
 
**
4497
 
** If a collating function fails any of the above constraints and that
4498
 
** collating function is  registered and used, then the behavior of SQLite
4499
 
** is undefined.
4500
 
**
4501
 
** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
4502
 
** with the addition that the xDestroy callback is invoked on pArg when
4503
 
** the collating function is deleted.
4504
 
** ^Collating functions are deleted when they are overridden by later
4505
 
** calls to the collation creation functions or when the
4506
 
** [database connection] is closed using [sqlite3_close()].
4507
 
**
4508
 
** ^The xDestroy callback is <u>not</u> called if the 
4509
 
** sqlite3_create_collation_v2() function fails.  Applications that invoke
4510
 
** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should 
4511
 
** check the return code and dispose of the application data pointer
4512
 
** themselves rather than expecting SQLite to deal with it for them.
4513
 
** This is different from every other SQLite interface.  The inconsistency 
4514
 
** is unfortunate but cannot be changed without breaking backwards 
4515
 
** compatibility.
4516
 
**
4517
 
** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
4518
 
*/
4519
 
SQLITE_API int sqlite3_create_collation(
4520
 
  sqlite3*, 
4521
 
  const char *zName, 
4522
 
  int eTextRep, 
4523
 
  void *pArg,
4524
 
  int(*xCompare)(void*,int,const void*,int,const void*)
4525
 
);
4526
 
SQLITE_API int sqlite3_create_collation_v2(
4527
 
  sqlite3*, 
4528
 
  const char *zName, 
4529
 
  int eTextRep, 
4530
 
  void *pArg,
4531
 
  int(*xCompare)(void*,int,const void*,int,const void*),
4532
 
  void(*xDestroy)(void*)
4533
 
);
4534
 
SQLITE_API int sqlite3_create_collation16(
4535
 
  sqlite3*, 
4536
 
  const void *zName,
4537
 
  int eTextRep, 
4538
 
  void *pArg,
4539
 
  int(*xCompare)(void*,int,const void*,int,const void*)
4540
 
);
4541
 
 
4542
 
/*
4543
 
** CAPI3REF: Collation Needed Callbacks
4544
 
**
4545
 
** ^To avoid having to register all collation sequences before a database
4546
 
** can be used, a single callback function may be registered with the
4547
 
** [database connection] to be invoked whenever an undefined collation
4548
 
** sequence is required.
4549
 
**
4550
 
** ^If the function is registered using the sqlite3_collation_needed() API,
4551
 
** then it is passed the names of undefined collation sequences as strings
4552
 
** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
4553
 
** the names are passed as UTF-16 in machine native byte order.
4554
 
** ^A call to either function replaces the existing collation-needed callback.
4555
 
**
4556
 
** ^(When the callback is invoked, the first argument passed is a copy
4557
 
** of the second argument to sqlite3_collation_needed() or
4558
 
** sqlite3_collation_needed16().  The second argument is the database
4559
 
** connection.  The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
4560
 
** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
4561
 
** sequence function required.  The fourth parameter is the name of the
4562
 
** required collation sequence.)^
4563
 
**
4564
 
** The callback function should register the desired collation using
4565
 
** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
4566
 
** [sqlite3_create_collation_v2()].
4567
 
*/
4568
 
SQLITE_API int sqlite3_collation_needed(
4569
 
  sqlite3*, 
4570
 
  void*, 
4571
 
  void(*)(void*,sqlite3*,int eTextRep,const char*)
4572
 
);
4573
 
SQLITE_API int sqlite3_collation_needed16(
4574
 
  sqlite3*, 
4575
 
  void*,
4576
 
  void(*)(void*,sqlite3*,int eTextRep,const void*)
4577
 
);
4578
 
 
4579
 
#ifdef SQLITE_HAS_CODEC
4580
 
/*
4581
 
** Specify the key for an encrypted database.  This routine should be
4582
 
** called right after sqlite3_open().
4583
 
**
4584
 
** The code to implement this API is not available in the public release
4585
 
** of SQLite.
4586
 
*/
4587
 
SQLITE_API int sqlite3_key(
4588
 
  sqlite3 *db,                   /* Database to be rekeyed */
4589
 
  const void *pKey, int nKey     /* The key */
4590
 
);
4591
 
 
4592
 
/*
4593
 
** Change the key on an open database.  If the current database is not
4594
 
** encrypted, this routine will encrypt it.  If pNew==0 or nNew==0, the
4595
 
** database is decrypted.
4596
 
**
4597
 
** The code to implement this API is not available in the public release
4598
 
** of SQLite.
4599
 
*/
4600
 
SQLITE_API int sqlite3_rekey(
4601
 
  sqlite3 *db,                   /* Database to be rekeyed */
4602
 
  const void *pKey, int nKey     /* The new key */
4603
 
);
4604
 
 
4605
 
/*
4606
 
** Specify the activation key for a SEE database.  Unless 
4607
 
** activated, none of the SEE routines will work.
4608
 
*/
4609
 
SQLITE_API void sqlite3_activate_see(
4610
 
  const char *zPassPhrase        /* Activation phrase */
4611
 
);
4612
 
#endif
4613
 
 
4614
 
#ifdef SQLITE_ENABLE_CEROD
4615
 
/*
4616
 
** Specify the activation key for a CEROD database.  Unless 
4617
 
** activated, none of the CEROD routines will work.
4618
 
*/
4619
 
SQLITE_API void sqlite3_activate_cerod(
4620
 
  const char *zPassPhrase        /* Activation phrase */
4621
 
);
4622
 
#endif
4623
 
 
4624
 
/*
4625
 
** CAPI3REF: Suspend Execution For A Short Time
4626
 
**
4627
 
** The sqlite3_sleep() function causes the current thread to suspend execution
4628
 
** for at least a number of milliseconds specified in its parameter.
4629
 
**
4630
 
** If the operating system does not support sleep requests with
4631
 
** millisecond time resolution, then the time will be rounded up to
4632
 
** the nearest second. The number of milliseconds of sleep actually
4633
 
** requested from the operating system is returned.
4634
 
**
4635
 
** ^SQLite implements this interface by calling the xSleep()
4636
 
** method of the default [sqlite3_vfs] object.  If the xSleep() method
4637
 
** of the default VFS is not implemented correctly, or not implemented at
4638
 
** all, then the behavior of sqlite3_sleep() may deviate from the description
4639
 
** in the previous paragraphs.
4640
 
*/
4641
 
SQLITE_API int sqlite3_sleep(int);
4642
 
 
4643
 
/*
4644
 
** CAPI3REF: Name Of The Folder Holding Temporary Files
4645
 
**
4646
 
** ^(If this global variable is made to point to a string which is
4647
 
** the name of a folder (a.k.a. directory), then all temporary files
4648
 
** created by SQLite when using a built-in [sqlite3_vfs | VFS]
4649
 
** will be placed in that directory.)^  ^If this variable
4650
 
** is a NULL pointer, then SQLite performs a search for an appropriate
4651
 
** temporary file directory.
4652
 
**
4653
 
** It is not safe to read or modify this variable in more than one
4654
 
** thread at a time.  It is not safe to read or modify this variable
4655
 
** if a [database connection] is being used at the same time in a separate
4656
 
** thread.
4657
 
** It is intended that this variable be set once
4658
 
** as part of process initialization and before any SQLite interface
4659
 
** routines have been called and that this variable remain unchanged
4660
 
** thereafter.
4661
 
**
4662
 
** ^The [temp_store_directory pragma] may modify this variable and cause
4663
 
** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
4664
 
** the [temp_store_directory pragma] always assumes that any string
4665
 
** that this variable points to is held in memory obtained from 
4666
 
** [sqlite3_malloc] and the pragma may attempt to free that memory
4667
 
** using [sqlite3_free].
4668
 
** Hence, if this variable is modified directly, either it should be
4669
 
** made NULL or made to point to memory obtained from [sqlite3_malloc]
4670
 
** or else the use of the [temp_store_directory pragma] should be avoided.
4671
 
*/
4672
 
SQLITE_API char *sqlite3_temp_directory;
4673
 
 
4674
 
/*
4675
 
** CAPI3REF: Test For Auto-Commit Mode
4676
 
** KEYWORDS: {autocommit mode}
4677
 
**
4678
 
** ^The sqlite3_get_autocommit() interface returns non-zero or
4679
 
** zero if the given database connection is or is not in autocommit mode,
4680
 
** respectively.  ^Autocommit mode is on by default.
4681
 
** ^Autocommit mode is disabled by a [BEGIN] statement.
4682
 
** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
4683
 
**
4684
 
** If certain kinds of errors occur on a statement within a multi-statement
4685
 
** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
4686
 
** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
4687
 
** transaction might be rolled back automatically.  The only way to
4688
 
** find out whether SQLite automatically rolled back the transaction after
4689
 
** an error is to use this function.
4690
 
**
4691
 
** If another thread changes the autocommit status of the database
4692
 
** connection while this routine is running, then the return value
4693
 
** is undefined.
4694
 
*/
4695
 
SQLITE_API int sqlite3_get_autocommit(sqlite3*);
4696
 
 
4697
 
/*
4698
 
** CAPI3REF: Find The Database Handle Of A Prepared Statement
4699
 
**
4700
 
** ^The sqlite3_db_handle interface returns the [database connection] handle
4701
 
** to which a [prepared statement] belongs.  ^The [database connection]
4702
 
** returned by sqlite3_db_handle is the same [database connection]
4703
 
** that was the first argument
4704
 
** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
4705
 
** create the statement in the first place.
4706
 
*/
4707
 
SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
4708
 
 
4709
 
/*
4710
 
** CAPI3REF: Find the next prepared statement
4711
 
**
4712
 
** ^This interface returns a pointer to the next [prepared statement] after
4713
 
** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
4714
 
** then this interface returns a pointer to the first prepared statement
4715
 
** associated with the database connection pDb.  ^If no prepared statement
4716
 
** satisfies the conditions of this routine, it returns NULL.
4717
 
**
4718
 
** The [database connection] pointer D in a call to
4719
 
** [sqlite3_next_stmt(D,S)] must refer to an open database
4720
 
** connection and in particular must not be a NULL pointer.
4721
 
*/
4722
 
SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
4723
 
 
4724
 
/*
4725
 
** CAPI3REF: Commit And Rollback Notification Callbacks
4726
 
**
4727
 
** ^The sqlite3_commit_hook() interface registers a callback
4728
 
** function to be invoked whenever a transaction is [COMMIT | committed].
4729
 
** ^Any callback set by a previous call to sqlite3_commit_hook()
4730
 
** for the same database connection is overridden.
4731
 
** ^The sqlite3_rollback_hook() interface registers a callback
4732
 
** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
4733
 
** ^Any callback set by a previous call to sqlite3_rollback_hook()
4734
 
** for the same database connection is overridden.
4735
 
** ^The pArg argument is passed through to the callback.
4736
 
** ^If the callback on a commit hook function returns non-zero,
4737
 
** then the commit is converted into a rollback.
4738
 
**
4739
 
** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
4740
 
** return the P argument from the previous call of the same function
4741
 
** on the same [database connection] D, or NULL for
4742
 
** the first call for each function on D.
4743
 
**
4744
 
** The callback implementation must not do anything that will modify
4745
 
** the database connection that invoked the callback.  Any actions
4746
 
** to modify the database connection must be deferred until after the
4747
 
** completion of the [sqlite3_step()] call that triggered the commit
4748
 
** or rollback hook in the first place.
4749
 
** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4750
 
** database connections for the meaning of "modify" in this paragraph.
4751
 
**
4752
 
** ^Registering a NULL function disables the callback.
4753
 
**
4754
 
** ^When the commit hook callback routine returns zero, the [COMMIT]
4755
 
** operation is allowed to continue normally.  ^If the commit hook
4756
 
** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
4757
 
** ^The rollback hook is invoked on a rollback that results from a commit
4758
 
** hook returning non-zero, just as it would be with any other rollback.
4759
 
**
4760
 
** ^For the purposes of this API, a transaction is said to have been
4761
 
** rolled back if an explicit "ROLLBACK" statement is executed, or
4762
 
** an error or constraint causes an implicit rollback to occur.
4763
 
** ^The rollback callback is not invoked if a transaction is
4764
 
** automatically rolled back because the database connection is closed.
4765
 
**
4766
 
** See also the [sqlite3_update_hook()] interface.
4767
 
*/
4768
 
SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
4769
 
SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
4770
 
 
4771
 
/*
4772
 
** CAPI3REF: Data Change Notification Callbacks
4773
 
**
4774
 
** ^The sqlite3_update_hook() interface registers a callback function
4775
 
** with the [database connection] identified by the first argument
4776
 
** to be invoked whenever a row is updated, inserted or deleted.
4777
 
** ^Any callback set by a previous call to this function
4778
 
** for the same database connection is overridden.
4779
 
**
4780
 
** ^The second argument is a pointer to the function to invoke when a
4781
 
** row is updated, inserted or deleted.
4782
 
** ^The first argument to the callback is a copy of the third argument
4783
 
** to sqlite3_update_hook().
4784
 
** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
4785
 
** or [SQLITE_UPDATE], depending on the operation that caused the callback
4786
 
** to be invoked.
4787
 
** ^The third and fourth arguments to the callback contain pointers to the
4788
 
** database and table name containing the affected row.
4789
 
** ^The final callback parameter is the [rowid] of the row.
4790
 
** ^In the case of an update, this is the [rowid] after the update takes place.
4791
 
**
4792
 
** ^(The update hook is not invoked when internal system tables are
4793
 
** modified (i.e. sqlite_master and sqlite_sequence).)^
4794
 
**
4795
 
** ^In the current implementation, the update hook
4796
 
** is not invoked when duplication rows are deleted because of an
4797
 
** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
4798
 
** invoked when rows are deleted using the [truncate optimization].
4799
 
** The exceptions defined in this paragraph might change in a future
4800
 
** release of SQLite.
4801
 
**
4802
 
** The update hook implementation must not do anything that will modify
4803
 
** the database connection that invoked the update hook.  Any actions
4804
 
** to modify the database connection must be deferred until after the
4805
 
** completion of the [sqlite3_step()] call that triggered the update hook.
4806
 
** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4807
 
** database connections for the meaning of "modify" in this paragraph.
4808
 
**
4809
 
** ^The sqlite3_update_hook(D,C,P) function
4810
 
** returns the P argument from the previous call
4811
 
** on the same [database connection] D, or NULL for
4812
 
** the first call on D.
4813
 
**
4814
 
** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
4815
 
** interfaces.
4816
 
*/
4817
 
SQLITE_API void *sqlite3_update_hook(
4818
 
  sqlite3*, 
4819
 
  void(*)(void *,int ,char const *,char const *,sqlite3_int64),
4820
 
  void*
4821
 
);
4822
 
 
4823
 
/*
4824
 
** CAPI3REF: Enable Or Disable Shared Pager Cache
4825
 
** KEYWORDS: {shared cache}
4826
 
**
4827
 
** ^(This routine enables or disables the sharing of the database cache
4828
 
** and schema data structures between [database connection | connections]
4829
 
** to the same database. Sharing is enabled if the argument is true
4830
 
** and disabled if the argument is false.)^
4831
 
**
4832
 
** ^Cache sharing is enabled and disabled for an entire process.
4833
 
** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
4834
 
** sharing was enabled or disabled for each thread separately.
4835
 
**
4836
 
** ^(The cache sharing mode set by this interface effects all subsequent
4837
 
** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
4838
 
** Existing database connections continue use the sharing mode
4839
 
** that was in effect at the time they were opened.)^
4840
 
**
4841
 
** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
4842
 
** successfully.  An [error code] is returned otherwise.)^
4843
 
**
4844
 
** ^Shared cache is disabled by default. But this might change in
4845
 
** future releases of SQLite.  Applications that care about shared
4846
 
** cache setting should set it explicitly.
4847
 
**
4848
 
** See Also:  [SQLite Shared-Cache Mode]
4849
 
*/
4850
 
SQLITE_API int sqlite3_enable_shared_cache(int);
4851
 
 
4852
 
/*
4853
 
** CAPI3REF: Attempt To Free Heap Memory
4854
 
**
4855
 
** ^The sqlite3_release_memory() interface attempts to free N bytes
4856
 
** of heap memory by deallocating non-essential memory allocations
4857
 
** held by the database library.   Memory used to cache database
4858
 
** pages to improve performance is an example of non-essential memory.
4859
 
** ^sqlite3_release_memory() returns the number of bytes actually freed,
4860
 
** which might be more or less than the amount requested.
4861
 
** ^The sqlite3_release_memory() routine is a no-op returning zero
4862
 
** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
4863
 
*/
4864
 
SQLITE_API int sqlite3_release_memory(int);
4865
 
 
4866
 
/*
4867
 
** CAPI3REF: Impose A Limit On Heap Size
4868
 
**
4869
 
** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
4870
 
** soft limit on the amount of heap memory that may be allocated by SQLite.
4871
 
** ^SQLite strives to keep heap memory utilization below the soft heap
4872
 
** limit by reducing the number of pages held in the page cache
4873
 
** as heap memory usages approaches the limit.
4874
 
** ^The soft heap limit is "soft" because even though SQLite strives to stay
4875
 
** below the limit, it will exceed the limit rather than generate
4876
 
** an [SQLITE_NOMEM] error.  In other words, the soft heap limit 
4877
 
** is advisory only.
4878
 
**
4879
 
** ^The return value from sqlite3_soft_heap_limit64() is the size of
4880
 
** the soft heap limit prior to the call.  ^If the argument N is negative
4881
 
** then no change is made to the soft heap limit.  Hence, the current
4882
 
** size of the soft heap limit can be determined by invoking
4883
 
** sqlite3_soft_heap_limit64() with a negative argument.
4884
 
**
4885
 
** ^If the argument N is zero then the soft heap limit is disabled.
4886
 
**
4887
 
** ^(The soft heap limit is not enforced in the current implementation
4888
 
** if one or more of following conditions are true:
4889
 
**
4890
 
** <ul>
4891
 
** <li> The soft heap limit is set to zero.
4892
 
** <li> Memory accounting is disabled using a combination of the
4893
 
**      [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
4894
 
**      the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
4895
 
** <li> An alternative page cache implementation is specified using
4896
 
**      [sqlite3_config]([SQLITE_CONFIG_PCACHE],...).
4897
 
** <li> The page cache allocates from its own memory pool supplied
4898
 
**      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
4899
 
**      from the heap.
4900
 
** </ul>)^
4901
 
**
4902
 
** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
4903
 
** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
4904
 
** compile-time option is invoked.  With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
4905
 
** the soft heap limit is enforced on every memory allocation.  Without
4906
 
** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
4907
 
** when memory is allocated by the page cache.  Testing suggests that because
4908
 
** the page cache is the predominate memory user in SQLite, most
4909
 
** applications will achieve adequate soft heap limit enforcement without
4910
 
** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
4911
 
**
4912
 
** The circumstances under which SQLite will enforce the soft heap limit may
4913
 
** changes in future releases of SQLite.
4914
 
*/
4915
 
SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
4916
 
 
4917
 
/*
4918
 
** CAPI3REF: Deprecated Soft Heap Limit Interface
4919
 
** DEPRECATED
4920
 
**
4921
 
** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
4922
 
** interface.  This routine is provided for historical compatibility
4923
 
** only.  All new applications should use the
4924
 
** [sqlite3_soft_heap_limit64()] interface rather than this one.
4925
 
*/
4926
 
SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
4927
 
 
4928
 
 
4929
 
/*
4930
 
** CAPI3REF: Extract Metadata About A Column Of A Table
4931
 
**
4932
 
** ^This routine returns metadata about a specific column of a specific
4933
 
** database table accessible using the [database connection] handle
4934
 
** passed as the first function argument.
4935
 
**
4936
 
** ^The column is identified by the second, third and fourth parameters to
4937
 
** this function. ^The second parameter is either the name of the database
4938
 
** (i.e. "main", "temp", or an attached database) containing the specified
4939
 
** table or NULL. ^If it is NULL, then all attached databases are searched
4940
 
** for the table using the same algorithm used by the database engine to
4941
 
** resolve unqualified table references.
4942
 
**
4943
 
** ^The third and fourth parameters to this function are the table and column
4944
 
** name of the desired column, respectively. Neither of these parameters
4945
 
** may be NULL.
4946
 
**
4947
 
** ^Metadata is returned by writing to the memory locations passed as the 5th
4948
 
** and subsequent parameters to this function. ^Any of these arguments may be
4949
 
** NULL, in which case the corresponding element of metadata is omitted.
4950
 
**
4951
 
** ^(<blockquote>
4952
 
** <table border="1">
4953
 
** <tr><th> Parameter <th> Output<br>Type <th>  Description
4954
 
**
4955
 
** <tr><td> 5th <td> const char* <td> Data type
4956
 
** <tr><td> 6th <td> const char* <td> Name of default collation sequence
4957
 
** <tr><td> 7th <td> int         <td> True if column has a NOT NULL constraint
4958
 
** <tr><td> 8th <td> int         <td> True if column is part of the PRIMARY KEY
4959
 
** <tr><td> 9th <td> int         <td> True if column is [AUTOINCREMENT]
4960
 
** </table>
4961
 
** </blockquote>)^
4962
 
**
4963
 
** ^The memory pointed to by the character pointers returned for the
4964
 
** declaration type and collation sequence is valid only until the next
4965
 
** call to any SQLite API function.
4966
 
**
4967
 
** ^If the specified table is actually a view, an [error code] is returned.
4968
 
**
4969
 
** ^If the specified column is "rowid", "oid" or "_rowid_" and an
4970
 
** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
4971
 
** parameters are set for the explicitly declared column. ^(If there is no
4972
 
** explicitly declared [INTEGER PRIMARY KEY] column, then the output
4973
 
** parameters are set as follows:
4974
 
**
4975
 
** <pre>
4976
 
**     data type: "INTEGER"
4977
 
**     collation sequence: "BINARY"
4978
 
**     not null: 0
4979
 
**     primary key: 1
4980
 
**     auto increment: 0
4981
 
** </pre>)^
4982
 
**
4983
 
** ^(This function may load one or more schemas from database files. If an
4984
 
** error occurs during this process, or if the requested table or column
4985
 
** cannot be found, an [error code] is returned and an error message left
4986
 
** in the [database connection] (to be retrieved using sqlite3_errmsg()).)^
4987
 
**
4988
 
** ^This API is only available if the library was compiled with the
4989
 
** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined.
4990
 
*/
4991
 
SQLITE_API int sqlite3_table_column_metadata(
4992
 
  sqlite3 *db,                /* Connection handle */
4993
 
  const char *zDbName,        /* Database name or NULL */
4994
 
  const char *zTableName,     /* Table name */
4995
 
  const char *zColumnName,    /* Column name */
4996
 
  char const **pzDataType,    /* OUTPUT: Declared data type */
4997
 
  char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
4998
 
  int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
4999
 
  int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
5000
 
  int *pAutoinc               /* OUTPUT: True if column is auto-increment */
5001
 
);
5002
 
 
5003
 
/*
5004
 
** CAPI3REF: Load An Extension
5005
 
**
5006
 
** ^This interface loads an SQLite extension library from the named file.
5007
 
**
5008
 
** ^The sqlite3_load_extension() interface attempts to load an
5009
 
** SQLite extension library contained in the file zFile.
5010
 
**
5011
 
** ^The entry point is zProc.
5012
 
** ^zProc may be 0, in which case the name of the entry point
5013
 
** defaults to "sqlite3_extension_init".
5014
 
** ^The sqlite3_load_extension() interface returns
5015
 
** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
5016
 
** ^If an error occurs and pzErrMsg is not 0, then the
5017
 
** [sqlite3_load_extension()] interface shall attempt to
5018
 
** fill *pzErrMsg with error message text stored in memory
5019
 
** obtained from [sqlite3_malloc()]. The calling function
5020
 
** should free this memory by calling [sqlite3_free()].
5021
 
**
5022
 
** ^Extension loading must be enabled using
5023
 
** [sqlite3_enable_load_extension()] prior to calling this API,
5024
 
** otherwise an error will be returned.
5025
 
**
5026
 
** See also the [load_extension() SQL function].
5027
 
*/
5028
 
SQLITE_API int sqlite3_load_extension(
5029
 
  sqlite3 *db,          /* Load the extension into this database connection */
5030
 
  const char *zFile,    /* Name of the shared library containing extension */
5031
 
  const char *zProc,    /* Entry point.  Derived from zFile if 0 */
5032
 
  char **pzErrMsg       /* Put error message here if not 0 */
5033
 
);
5034
 
 
5035
 
/*
5036
 
** CAPI3REF: Enable Or Disable Extension Loading
5037
 
**
5038
 
** ^So as not to open security holes in older applications that are
5039
 
** unprepared to deal with extension loading, and as a means of disabling
5040
 
** extension loading while evaluating user-entered SQL, the following API
5041
 
** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
5042
 
**
5043
 
** ^Extension loading is off by default. See ticket #1863.
5044
 
** ^Call the sqlite3_enable_load_extension() routine with onoff==1
5045
 
** to turn extension loading on and call it with onoff==0 to turn
5046
 
** it back off again.
5047
 
*/
5048
 
SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
5049
 
 
5050
 
/*
5051
 
** CAPI3REF: Automatically Load Statically Linked Extensions
5052
 
**
5053
 
** ^This interface causes the xEntryPoint() function to be invoked for
5054
 
** each new [database connection] that is created.  The idea here is that
5055
 
** xEntryPoint() is the entry point for a statically linked SQLite extension
5056
 
** that is to be automatically loaded into all new database connections.
5057
 
**
5058
 
** ^(Even though the function prototype shows that xEntryPoint() takes
5059
 
** no arguments and returns void, SQLite invokes xEntryPoint() with three
5060
 
** arguments and expects and integer result as if the signature of the
5061
 
** entry point where as follows:
5062
 
**
5063
 
** <blockquote><pre>
5064
 
** &nbsp;  int xEntryPoint(
5065
 
** &nbsp;    sqlite3 *db,
5066
 
** &nbsp;    const char **pzErrMsg,
5067
 
** &nbsp;    const struct sqlite3_api_routines *pThunk
5068
 
** &nbsp;  );
5069
 
** </pre></blockquote>)^
5070
 
**
5071
 
** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
5072
 
** point to an appropriate error message (obtained from [sqlite3_mprintf()])
5073
 
** and return an appropriate [error code].  ^SQLite ensures that *pzErrMsg
5074
 
** is NULL before calling the xEntryPoint().  ^SQLite will invoke
5075
 
** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns.  ^If any
5076
 
** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
5077
 
** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
5078
 
**
5079
 
** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
5080
 
** on the list of automatic extensions is a harmless no-op. ^No entry point
5081
 
** will be called more than once for each database connection that is opened.
5082
 
**
5083
 
** See also: [sqlite3_reset_auto_extension()].
5084
 
*/
5085
 
SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void));
5086
 
 
5087
 
/*
5088
 
** CAPI3REF: Reset Automatic Extension Loading
5089
 
**
5090
 
** ^This interface disables all automatic extensions previously
5091
 
** registered using [sqlite3_auto_extension()].
5092
 
*/
5093
 
SQLITE_API void sqlite3_reset_auto_extension(void);
5094
 
 
5095
 
/*
5096
 
** The interface to the virtual-table mechanism is currently considered
5097
 
** to be experimental.  The interface might change in incompatible ways.
5098
 
** If this is a problem for you, do not use the interface at this time.
5099
 
**
5100
 
** When the virtual-table mechanism stabilizes, we will declare the
5101
 
** interface fixed, support it indefinitely, and remove this comment.
5102
 
*/
5103
 
 
5104
 
/*
5105
 
** Structures used by the virtual table interface
5106
 
*/
5107
 
typedef struct sqlite3_vtab sqlite3_vtab;
5108
 
typedef struct sqlite3_index_info sqlite3_index_info;
5109
 
typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
5110
 
typedef struct sqlite3_module sqlite3_module;
5111
 
 
5112
 
/*
5113
 
** CAPI3REF: Virtual Table Object
5114
 
** KEYWORDS: sqlite3_module {virtual table module}
5115
 
**
5116
 
** This structure, sometimes called a "virtual table module", 
5117
 
** defines the implementation of a [virtual tables].  
5118
 
** This structure consists mostly of methods for the module.
5119
 
**
5120
 
** ^A virtual table module is created by filling in a persistent
5121
 
** instance of this structure and passing a pointer to that instance
5122
 
** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
5123
 
** ^The registration remains valid until it is replaced by a different
5124
 
** module or until the [database connection] closes.  The content
5125
 
** of this structure must not change while it is registered with
5126
 
** any database connection.
5127
 
*/
5128
 
struct sqlite3_module {
5129
 
  int iVersion;
5130
 
  int (*xCreate)(sqlite3*, void *pAux,
5131
 
               int argc, const char *const*argv,
5132
 
               sqlite3_vtab **ppVTab, char**);
5133
 
  int (*xConnect)(sqlite3*, void *pAux,
5134
 
               int argc, const char *const*argv,
5135
 
               sqlite3_vtab **ppVTab, char**);
5136
 
  int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
5137
 
  int (*xDisconnect)(sqlite3_vtab *pVTab);
5138
 
  int (*xDestroy)(sqlite3_vtab *pVTab);
5139
 
  int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
5140
 
  int (*xClose)(sqlite3_vtab_cursor*);
5141
 
  int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
5142
 
                int argc, sqlite3_value **argv);
5143
 
  int (*xNext)(sqlite3_vtab_cursor*);
5144
 
  int (*xEof)(sqlite3_vtab_cursor*);
5145
 
  int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
5146
 
  int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
5147
 
  int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
5148
 
  int (*xBegin)(sqlite3_vtab *pVTab);
5149
 
  int (*xSync)(sqlite3_vtab *pVTab);
5150
 
  int (*xCommit)(sqlite3_vtab *pVTab);
5151
 
  int (*xRollback)(sqlite3_vtab *pVTab);
5152
 
  int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
5153
 
                       void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
5154
 
                       void **ppArg);
5155
 
  int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
5156
 
};
5157
 
 
5158
 
/*
5159
 
** CAPI3REF: Virtual Table Indexing Information
5160
 
** KEYWORDS: sqlite3_index_info
5161
 
**
5162
 
** The sqlite3_index_info structure and its substructures is used as part
5163
 
** of the [virtual table] interface to
5164
 
** pass information into and receive the reply from the [xBestIndex]
5165
 
** method of a [virtual table module].  The fields under **Inputs** are the
5166
 
** inputs to xBestIndex and are read-only.  xBestIndex inserts its
5167
 
** results into the **Outputs** fields.
5168
 
**
5169
 
** ^(The aConstraint[] array records WHERE clause constraints of the form:
5170
 
**
5171
 
** <blockquote>column OP expr</blockquote>
5172
 
**
5173
 
** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^  ^(The particular operator is
5174
 
** stored in aConstraint[].op using one of the
5175
 
** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
5176
 
** ^(The index of the column is stored in
5177
 
** aConstraint[].iColumn.)^  ^(aConstraint[].usable is TRUE if the
5178
 
** expr on the right-hand side can be evaluated (and thus the constraint
5179
 
** is usable) and false if it cannot.)^
5180
 
**
5181
 
** ^The optimizer automatically inverts terms of the form "expr OP column"
5182
 
** and makes other simplifications to the WHERE clause in an attempt to
5183
 
** get as many WHERE clause terms into the form shown above as possible.
5184
 
** ^The aConstraint[] array only reports WHERE clause terms that are
5185
 
** relevant to the particular virtual table being queried.
5186
 
**
5187
 
** ^Information about the ORDER BY clause is stored in aOrderBy[].
5188
 
** ^Each term of aOrderBy records a column of the ORDER BY clause.
5189
 
**
5190
 
** The [xBestIndex] method must fill aConstraintUsage[] with information
5191
 
** about what parameters to pass to xFilter.  ^If argvIndex>0 then
5192
 
** the right-hand side of the corresponding aConstraint[] is evaluated
5193
 
** and becomes the argvIndex-th entry in argv.  ^(If aConstraintUsage[].omit
5194
 
** is true, then the constraint is assumed to be fully handled by the
5195
 
** virtual table and is not checked again by SQLite.)^
5196
 
**
5197
 
** ^The idxNum and idxPtr values are recorded and passed into the
5198
 
** [xFilter] method.
5199
 
** ^[sqlite3_free()] is used to free idxPtr if and only if
5200
 
** needToFreeIdxPtr is true.
5201
 
**
5202
 
** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
5203
 
** the correct order to satisfy the ORDER BY clause so that no separate
5204
 
** sorting step is required.
5205
 
**
5206
 
** ^The estimatedCost value is an estimate of the cost of doing the
5207
 
** particular lookup.  A full scan of a table with N entries should have
5208
 
** a cost of N.  A binary search of a table of N entries should have a
5209
 
** cost of approximately log(N).
5210
 
*/
5211
 
struct sqlite3_index_info {
5212
 
  /* Inputs */
5213
 
  int nConstraint;           /* Number of entries in aConstraint */
5214
 
  struct sqlite3_index_constraint {
5215
 
     int iColumn;              /* Column on left-hand side of constraint */
5216
 
     unsigned char op;         /* Constraint operator */
5217
 
     unsigned char usable;     /* True if this constraint is usable */
5218
 
     int iTermOffset;          /* Used internally - xBestIndex should ignore */
5219
 
  } *aConstraint;            /* Table of WHERE clause constraints */
5220
 
  int nOrderBy;              /* Number of terms in the ORDER BY clause */
5221
 
  struct sqlite3_index_orderby {
5222
 
     int iColumn;              /* Column number */
5223
 
     unsigned char desc;       /* True for DESC.  False for ASC. */
5224
 
  } *aOrderBy;               /* The ORDER BY clause */
5225
 
  /* Outputs */
5226
 
  struct sqlite3_index_constraint_usage {
5227
 
    int argvIndex;           /* if >0, constraint is part of argv to xFilter */
5228
 
    unsigned char omit;      /* Do not code a test for this constraint */
5229
 
  } *aConstraintUsage;
5230
 
  int idxNum;                /* Number used to identify the index */
5231
 
  char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
5232
 
  int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
5233
 
  int orderByConsumed;       /* True if output is already ordered */
5234
 
  double estimatedCost;      /* Estimated cost of using this index */
5235
 
};
5236
 
 
5237
 
/*
5238
 
** CAPI3REF: Virtual Table Constraint Operator Codes
5239
 
**
5240
 
** These macros defined the allowed values for the
5241
 
** [sqlite3_index_info].aConstraint[].op field.  Each value represents
5242
 
** an operator that is part of a constraint term in the wHERE clause of
5243
 
** a query that uses a [virtual table].
5244
 
*/
5245
 
#define SQLITE_INDEX_CONSTRAINT_EQ    2
5246
 
#define SQLITE_INDEX_CONSTRAINT_GT    4
5247
 
#define SQLITE_INDEX_CONSTRAINT_LE    8
5248
 
#define SQLITE_INDEX_CONSTRAINT_LT    16
5249
 
#define SQLITE_INDEX_CONSTRAINT_GE    32
5250
 
#define SQLITE_INDEX_CONSTRAINT_MATCH 64
5251
 
 
5252
 
/*
5253
 
** CAPI3REF: Register A Virtual Table Implementation
5254
 
**
5255
 
** ^These routines are used to register a new [virtual table module] name.
5256
 
** ^Module names must be registered before
5257
 
** creating a new [virtual table] using the module and before using a
5258
 
** preexisting [virtual table] for the module.
5259
 
**
5260
 
** ^The module name is registered on the [database connection] specified
5261
 
** by the first parameter.  ^The name of the module is given by the 
5262
 
** second parameter.  ^The third parameter is a pointer to
5263
 
** the implementation of the [virtual table module].   ^The fourth
5264
 
** parameter is an arbitrary client data pointer that is passed through
5265
 
** into the [xCreate] and [xConnect] methods of the virtual table module
5266
 
** when a new virtual table is be being created or reinitialized.
5267
 
**
5268
 
** ^The sqlite3_create_module_v2() interface has a fifth parameter which
5269
 
** is a pointer to a destructor for the pClientData.  ^SQLite will
5270
 
** invoke the destructor function (if it is not NULL) when SQLite
5271
 
** no longer needs the pClientData pointer.  ^The destructor will also
5272
 
** be invoked if the call to sqlite3_create_module_v2() fails.
5273
 
** ^The sqlite3_create_module()
5274
 
** interface is equivalent to sqlite3_create_module_v2() with a NULL
5275
 
** destructor.
5276
 
*/
5277
 
SQLITE_API int sqlite3_create_module(
5278
 
  sqlite3 *db,               /* SQLite connection to register module with */
5279
 
  const char *zName,         /* Name of the module */
5280
 
  const sqlite3_module *p,   /* Methods for the module */
5281
 
  void *pClientData          /* Client data for xCreate/xConnect */
5282
 
);
5283
 
SQLITE_API int sqlite3_create_module_v2(
5284
 
  sqlite3 *db,               /* SQLite connection to register module with */
5285
 
  const char *zName,         /* Name of the module */
5286
 
  const sqlite3_module *p,   /* Methods for the module */
5287
 
  void *pClientData,         /* Client data for xCreate/xConnect */
5288
 
  void(*xDestroy)(void*)     /* Module destructor function */
5289
 
);
5290
 
 
5291
 
/*
5292
 
** CAPI3REF: Virtual Table Instance Object
5293
 
** KEYWORDS: sqlite3_vtab
5294
 
**
5295
 
** Every [virtual table module] implementation uses a subclass
5296
 
** of this object to describe a particular instance
5297
 
** of the [virtual table].  Each subclass will
5298
 
** be tailored to the specific needs of the module implementation.
5299
 
** The purpose of this superclass is to define certain fields that are
5300
 
** common to all module implementations.
5301
 
**
5302
 
** ^Virtual tables methods can set an error message by assigning a
5303
 
** string obtained from [sqlite3_mprintf()] to zErrMsg.  The method should
5304
 
** take care that any prior string is freed by a call to [sqlite3_free()]
5305
 
** prior to assigning a new string to zErrMsg.  ^After the error message
5306
 
** is delivered up to the client application, the string will be automatically
5307
 
** freed by sqlite3_free() and the zErrMsg field will be zeroed.
5308
 
*/
5309
 
struct sqlite3_vtab {
5310
 
  const sqlite3_module *pModule;  /* The module for this virtual table */
5311
 
  int nRef;                       /* NO LONGER USED */
5312
 
  char *zErrMsg;                  /* Error message from sqlite3_mprintf() */
5313
 
  /* Virtual table implementations will typically add additional fields */
5314
 
};
5315
 
 
5316
 
/*
5317
 
** CAPI3REF: Virtual Table Cursor Object
5318
 
** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
5319
 
**
5320
 
** Every [virtual table module] implementation uses a subclass of the
5321
 
** following structure to describe cursors that point into the
5322
 
** [virtual table] and are used
5323
 
** to loop through the virtual table.  Cursors are created using the
5324
 
** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
5325
 
** by the [sqlite3_module.xClose | xClose] method.  Cursors are used
5326
 
** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
5327
 
** of the module.  Each module implementation will define
5328
 
** the content of a cursor structure to suit its own needs.
5329
 
**
5330
 
** This superclass exists in order to define fields of the cursor that
5331
 
** are common to all implementations.
5332
 
*/
5333
 
struct sqlite3_vtab_cursor {
5334
 
  sqlite3_vtab *pVtab;      /* Virtual table of this cursor */
5335
 
  /* Virtual table implementations will typically add additional fields */
5336
 
};
5337
 
 
5338
 
/*
5339
 
** CAPI3REF: Declare The Schema Of A Virtual Table
5340
 
**
5341
 
** ^The [xCreate] and [xConnect] methods of a
5342
 
** [virtual table module] call this interface
5343
 
** to declare the format (the names and datatypes of the columns) of
5344
 
** the virtual tables they implement.
5345
 
*/
5346
 
SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
5347
 
 
5348
 
/*
5349
 
** CAPI3REF: Overload A Function For A Virtual Table
5350
 
**
5351
 
** ^(Virtual tables can provide alternative implementations of functions
5352
 
** using the [xFindFunction] method of the [virtual table module].  
5353
 
** But global versions of those functions
5354
 
** must exist in order to be overloaded.)^
5355
 
**
5356
 
** ^(This API makes sure a global version of a function with a particular
5357
 
** name and number of parameters exists.  If no such function exists
5358
 
** before this API is called, a new function is created.)^  ^The implementation
5359
 
** of the new function always causes an exception to be thrown.  So
5360
 
** the new function is not good for anything by itself.  Its only
5361
 
** purpose is to be a placeholder function that can be overloaded
5362
 
** by a [virtual table].
5363
 
*/
5364
 
SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
5365
 
 
5366
 
/*
5367
 
** The interface to the virtual-table mechanism defined above (back up
5368
 
** to a comment remarkably similar to this one) is currently considered
5369
 
** to be experimental.  The interface might change in incompatible ways.
5370
 
** If this is a problem for you, do not use the interface at this time.
5371
 
**
5372
 
** When the virtual-table mechanism stabilizes, we will declare the
5373
 
** interface fixed, support it indefinitely, and remove this comment.
5374
 
*/
5375
 
 
5376
 
/*
5377
 
** CAPI3REF: A Handle To An Open BLOB
5378
 
** KEYWORDS: {BLOB handle} {BLOB handles}
5379
 
**
5380
 
** An instance of this object represents an open BLOB on which
5381
 
** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
5382
 
** ^Objects of this type are created by [sqlite3_blob_open()]
5383
 
** and destroyed by [sqlite3_blob_close()].
5384
 
** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
5385
 
** can be used to read or write small subsections of the BLOB.
5386
 
** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
5387
 
*/
5388
 
typedef struct sqlite3_blob sqlite3_blob;
5389
 
 
5390
 
/*
5391
 
** CAPI3REF: Open A BLOB For Incremental I/O
5392
 
**
5393
 
** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
5394
 
** in row iRow, column zColumn, table zTable in database zDb;
5395
 
** in other words, the same BLOB that would be selected by:
5396
 
**
5397
 
** <pre>
5398
 
**     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
5399
 
** </pre>)^
5400
 
**
5401
 
** ^If the flags parameter is non-zero, then the BLOB is opened for read
5402
 
** and write access. ^If it is zero, the BLOB is opened for read access.
5403
 
** ^It is not possible to open a column that is part of an index or primary 
5404
 
** key for writing. ^If [foreign key constraints] are enabled, it is 
5405
 
** not possible to open a column that is part of a [child key] for writing.
5406
 
**
5407
 
** ^Note that the database name is not the filename that contains
5408
 
** the database but rather the symbolic name of the database that
5409
 
** appears after the AS keyword when the database is connected using [ATTACH].
5410
 
** ^For the main database file, the database name is "main".
5411
 
** ^For TEMP tables, the database name is "temp".
5412
 
**
5413
 
** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
5414
 
** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
5415
 
** to be a null pointer.)^
5416
 
** ^This function sets the [database connection] error code and message
5417
 
** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
5418
 
** functions. ^Note that the *ppBlob variable is always initialized in a
5419
 
** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
5420
 
** regardless of the success or failure of this routine.
5421
 
**
5422
 
** ^(If the row that a BLOB handle points to is modified by an
5423
 
** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
5424
 
** then the BLOB handle is marked as "expired".
5425
 
** This is true if any column of the row is changed, even a column
5426
 
** other than the one the BLOB handle is open on.)^
5427
 
** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
5428
 
** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
5429
 
** ^(Changes written into a BLOB prior to the BLOB expiring are not
5430
 
** rolled back by the expiration of the BLOB.  Such changes will eventually
5431
 
** commit if the transaction continues to completion.)^
5432
 
**
5433
 
** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
5434
 
** the opened blob.  ^The size of a blob may not be changed by this
5435
 
** interface.  Use the [UPDATE] SQL command to change the size of a
5436
 
** blob.
5437
 
**
5438
 
** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
5439
 
** and the built-in [zeroblob] SQL function can be used, if desired,
5440
 
** to create an empty, zero-filled blob in which to read or write using
5441
 
** this interface.
5442
 
**
5443
 
** To avoid a resource leak, every open [BLOB handle] should eventually
5444
 
** be released by a call to [sqlite3_blob_close()].
5445
 
*/
5446
 
SQLITE_API int sqlite3_blob_open(
5447
 
  sqlite3*,
5448
 
  const char *zDb,
5449
 
  const char *zTable,
5450
 
  const char *zColumn,
5451
 
  sqlite3_int64 iRow,
5452
 
  int flags,
5453
 
  sqlite3_blob **ppBlob
5454
 
);
5455
 
 
5456
 
/*
5457
 
** CAPI3REF: Move a BLOB Handle to a New Row
5458
 
**
5459
 
** ^This function is used to move an existing blob handle so that it points
5460
 
** to a different row of the same database table. ^The new row is identified
5461
 
** by the rowid value passed as the second argument. Only the row can be
5462
 
** changed. ^The database, table and column on which the blob handle is open
5463
 
** remain the same. Moving an existing blob handle to a new row can be
5464
 
** faster than closing the existing handle and opening a new one.
5465
 
**
5466
 
** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
5467
 
** it must exist and there must be either a blob or text value stored in
5468
 
** the nominated column.)^ ^If the new row is not present in the table, or if
5469
 
** it does not contain a blob or text value, or if another error occurs, an
5470
 
** SQLite error code is returned and the blob handle is considered aborted.
5471
 
** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
5472
 
** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
5473
 
** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
5474
 
** always returns zero.
5475
 
**
5476
 
** ^This function sets the database handle error code and message.
5477
 
*/
5478
 
SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
5479
 
 
5480
 
/*
5481
 
** CAPI3REF: Close A BLOB Handle
5482
 
**
5483
 
** ^Closes an open [BLOB handle].
5484
 
**
5485
 
** ^Closing a BLOB shall cause the current transaction to commit
5486
 
** if there are no other BLOBs, no pending prepared statements, and the
5487
 
** database connection is in [autocommit mode].
5488
 
** ^If any writes were made to the BLOB, they might be held in cache
5489
 
** until the close operation if they will fit.
5490
 
**
5491
 
** ^(Closing the BLOB often forces the changes
5492
 
** out to disk and so if any I/O errors occur, they will likely occur
5493
 
** at the time when the BLOB is closed.  Any errors that occur during
5494
 
** closing are reported as a non-zero return value.)^
5495
 
**
5496
 
** ^(The BLOB is closed unconditionally.  Even if this routine returns
5497
 
** an error code, the BLOB is still closed.)^
5498
 
**
5499
 
** ^Calling this routine with a null pointer (such as would be returned
5500
 
** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
5501
 
*/
5502
 
SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
5503
 
 
5504
 
/*
5505
 
** CAPI3REF: Return The Size Of An Open BLOB
5506
 
**
5507
 
** ^Returns the size in bytes of the BLOB accessible via the 
5508
 
** successfully opened [BLOB handle] in its only argument.  ^The
5509
 
** incremental blob I/O routines can only read or overwriting existing
5510
 
** blob content; they cannot change the size of a blob.
5511
 
**
5512
 
** This routine only works on a [BLOB handle] which has been created
5513
 
** by a prior successful call to [sqlite3_blob_open()] and which has not
5514
 
** been closed by [sqlite3_blob_close()].  Passing any other pointer in
5515
 
** to this routine results in undefined and probably undesirable behavior.
5516
 
*/
5517
 
SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
5518
 
 
5519
 
/*
5520
 
** CAPI3REF: Read Data From A BLOB Incrementally
5521
 
**
5522
 
** ^(This function is used to read data from an open [BLOB handle] into a
5523
 
** caller-supplied buffer. N bytes of data are copied into buffer Z
5524
 
** from the open BLOB, starting at offset iOffset.)^
5525
 
**
5526
 
** ^If offset iOffset is less than N bytes from the end of the BLOB,
5527
 
** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
5528
 
** less than zero, [SQLITE_ERROR] is returned and no data is read.
5529
 
** ^The size of the blob (and hence the maximum value of N+iOffset)
5530
 
** can be determined using the [sqlite3_blob_bytes()] interface.
5531
 
**
5532
 
** ^An attempt to read from an expired [BLOB handle] fails with an
5533
 
** error code of [SQLITE_ABORT].
5534
 
**
5535
 
** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
5536
 
** Otherwise, an [error code] or an [extended error code] is returned.)^
5537
 
**
5538
 
** This routine only works on a [BLOB handle] which has been created
5539
 
** by a prior successful call to [sqlite3_blob_open()] and which has not
5540
 
** been closed by [sqlite3_blob_close()].  Passing any other pointer in
5541
 
** to this routine results in undefined and probably undesirable behavior.
5542
 
**
5543
 
** See also: [sqlite3_blob_write()].
5544
 
*/
5545
 
SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
5546
 
 
5547
 
/*
5548
 
** CAPI3REF: Write Data Into A BLOB Incrementally
5549
 
**
5550
 
** ^This function is used to write data into an open [BLOB handle] from a
5551
 
** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
5552
 
** into the open BLOB, starting at offset iOffset.
5553
 
**
5554
 
** ^If the [BLOB handle] passed as the first argument was not opened for
5555
 
** writing (the flags parameter to [sqlite3_blob_open()] was zero),
5556
 
** this function returns [SQLITE_READONLY].
5557
 
**
5558
 
** ^This function may only modify the contents of the BLOB; it is
5559
 
** not possible to increase the size of a BLOB using this API.
5560
 
** ^If offset iOffset is less than N bytes from the end of the BLOB,
5561
 
** [SQLITE_ERROR] is returned and no data is written.  ^If N is
5562
 
** less than zero [SQLITE_ERROR] is returned and no data is written.
5563
 
** The size of the BLOB (and hence the maximum value of N+iOffset)
5564
 
** can be determined using the [sqlite3_blob_bytes()] interface.
5565
 
**
5566
 
** ^An attempt to write to an expired [BLOB handle] fails with an
5567
 
** error code of [SQLITE_ABORT].  ^Writes to the BLOB that occurred
5568
 
** before the [BLOB handle] expired are not rolled back by the
5569
 
** expiration of the handle, though of course those changes might
5570
 
** have been overwritten by the statement that expired the BLOB handle
5571
 
** or by other independent statements.
5572
 
**
5573
 
** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
5574
 
** Otherwise, an  [error code] or an [extended error code] is returned.)^
5575
 
**
5576
 
** This routine only works on a [BLOB handle] which has been created
5577
 
** by a prior successful call to [sqlite3_blob_open()] and which has not
5578
 
** been closed by [sqlite3_blob_close()].  Passing any other pointer in
5579
 
** to this routine results in undefined and probably undesirable behavior.
5580
 
**
5581
 
** See also: [sqlite3_blob_read()].
5582
 
*/
5583
 
SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
5584
 
 
5585
 
/*
5586
 
** CAPI3REF: Virtual File System Objects
5587
 
**
5588
 
** A virtual filesystem (VFS) is an [sqlite3_vfs] object
5589
 
** that SQLite uses to interact
5590
 
** with the underlying operating system.  Most SQLite builds come with a
5591
 
** single default VFS that is appropriate for the host computer.
5592
 
** New VFSes can be registered and existing VFSes can be unregistered.
5593
 
** The following interfaces are provided.
5594
 
**
5595
 
** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
5596
 
** ^Names are case sensitive.
5597
 
** ^Names are zero-terminated UTF-8 strings.
5598
 
** ^If there is no match, a NULL pointer is returned.
5599
 
** ^If zVfsName is NULL then the default VFS is returned.
5600
 
**
5601
 
** ^New VFSes are registered with sqlite3_vfs_register().
5602
 
** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
5603
 
** ^The same VFS can be registered multiple times without injury.
5604
 
** ^To make an existing VFS into the default VFS, register it again
5605
 
** with the makeDflt flag set.  If two different VFSes with the
5606
 
** same name are registered, the behavior is undefined.  If a
5607
 
** VFS is registered with a name that is NULL or an empty string,
5608
 
** then the behavior is undefined.
5609
 
**
5610
 
** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
5611
 
** ^(If the default VFS is unregistered, another VFS is chosen as
5612
 
** the default.  The choice for the new VFS is arbitrary.)^
5613
 
*/
5614
 
SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
5615
 
SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
5616
 
SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
5617
 
 
5618
 
/*
5619
 
** CAPI3REF: Mutexes
5620
 
**
5621
 
** The SQLite core uses these routines for thread
5622
 
** synchronization. Though they are intended for internal
5623
 
** use by SQLite, code that links against SQLite is
5624
 
** permitted to use any of these routines.
5625
 
**
5626
 
** The SQLite source code contains multiple implementations
5627
 
** of these mutex routines.  An appropriate implementation
5628
 
** is selected automatically at compile-time.  ^(The following
5629
 
** implementations are available in the SQLite core:
5630
 
**
5631
 
** <ul>
5632
 
** <li>   SQLITE_MUTEX_OS2
5633
 
** <li>   SQLITE_MUTEX_PTHREAD
5634
 
** <li>   SQLITE_MUTEX_W32
5635
 
** <li>   SQLITE_MUTEX_NOOP
5636
 
** </ul>)^
5637
 
**
5638
 
** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
5639
 
** that does no real locking and is appropriate for use in
5640
 
** a single-threaded application.  ^The SQLITE_MUTEX_OS2,
5641
 
** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations
5642
 
** are appropriate for use on OS/2, Unix, and Windows.
5643
 
**
5644
 
** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
5645
 
** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
5646
 
** implementation is included with the library. In this case the
5647
 
** application must supply a custom mutex implementation using the
5648
 
** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
5649
 
** before calling sqlite3_initialize() or any other public sqlite3_
5650
 
** function that calls sqlite3_initialize().)^
5651
 
**
5652
 
** ^The sqlite3_mutex_alloc() routine allocates a new
5653
 
** mutex and returns a pointer to it. ^If it returns NULL
5654
 
** that means that a mutex could not be allocated.  ^SQLite
5655
 
** will unwind its stack and return an error.  ^(The argument
5656
 
** to sqlite3_mutex_alloc() is one of these integer constants:
5657
 
**
5658
 
** <ul>
5659
 
** <li>  SQLITE_MUTEX_FAST
5660
 
** <li>  SQLITE_MUTEX_RECURSIVE
5661
 
** <li>  SQLITE_MUTEX_STATIC_MASTER
5662
 
** <li>  SQLITE_MUTEX_STATIC_MEM
5663
 
** <li>  SQLITE_MUTEX_STATIC_MEM2
5664
 
** <li>  SQLITE_MUTEX_STATIC_PRNG
5665
 
** <li>  SQLITE_MUTEX_STATIC_LRU
5666
 
** <li>  SQLITE_MUTEX_STATIC_LRU2
5667
 
** </ul>)^
5668
 
**
5669
 
** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
5670
 
** cause sqlite3_mutex_alloc() to create
5671
 
** a new mutex.  ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
5672
 
** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
5673
 
** The mutex implementation does not need to make a distinction
5674
 
** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
5675
 
** not want to.  ^SQLite will only request a recursive mutex in
5676
 
** cases where it really needs one.  ^If a faster non-recursive mutex
5677
 
** implementation is available on the host platform, the mutex subsystem
5678
 
** might return such a mutex in response to SQLITE_MUTEX_FAST.
5679
 
**
5680
 
** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
5681
 
** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
5682
 
** a pointer to a static preexisting mutex.  ^Six static mutexes are
5683
 
** used by the current version of SQLite.  Future versions of SQLite
5684
 
** may add additional static mutexes.  Static mutexes are for internal
5685
 
** use by SQLite only.  Applications that use SQLite mutexes should
5686
 
** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
5687
 
** SQLITE_MUTEX_RECURSIVE.
5688
 
**
5689
 
** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
5690
 
** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
5691
 
** returns a different mutex on every call.  ^But for the static
5692
 
** mutex types, the same mutex is returned on every call that has
5693
 
** the same type number.
5694
 
**
5695
 
** ^The sqlite3_mutex_free() routine deallocates a previously
5696
 
** allocated dynamic mutex.  ^SQLite is careful to deallocate every
5697
 
** dynamic mutex that it allocates.  The dynamic mutexes must not be in
5698
 
** use when they are deallocated.  Attempting to deallocate a static
5699
 
** mutex results in undefined behavior.  ^SQLite never deallocates
5700
 
** a static mutex.
5701
 
**
5702
 
** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
5703
 
** to enter a mutex.  ^If another thread is already within the mutex,
5704
 
** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
5705
 
** SQLITE_BUSY.  ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
5706
 
** upon successful entry.  ^(Mutexes created using
5707
 
** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
5708
 
** In such cases the,
5709
 
** mutex must be exited an equal number of times before another thread
5710
 
** can enter.)^  ^(If the same thread tries to enter any other
5711
 
** kind of mutex more than once, the behavior is undefined.
5712
 
** SQLite will never exhibit
5713
 
** such behavior in its own use of mutexes.)^
5714
 
**
5715
 
** ^(Some systems (for example, Windows 95) do not support the operation
5716
 
** implemented by sqlite3_mutex_try().  On those systems, sqlite3_mutex_try()
5717
 
** will always return SQLITE_BUSY.  The SQLite core only ever uses
5718
 
** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^
5719
 
**
5720
 
** ^The sqlite3_mutex_leave() routine exits a mutex that was
5721
 
** previously entered by the same thread.   ^(The behavior
5722
 
** is undefined if the mutex is not currently entered by the
5723
 
** calling thread or is not currently allocated.  SQLite will
5724
 
** never do either.)^
5725
 
**
5726
 
** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
5727
 
** sqlite3_mutex_leave() is a NULL pointer, then all three routines
5728
 
** behave as no-ops.
5729
 
**
5730
 
** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
5731
 
*/
5732
 
SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
5733
 
SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
5734
 
SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
5735
 
SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
5736
 
SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
5737
 
 
5738
 
/*
5739
 
** CAPI3REF: Mutex Methods Object
5740
 
**
5741
 
** An instance of this structure defines the low-level routines
5742
 
** used to allocate and use mutexes.
5743
 
**
5744
 
** Usually, the default mutex implementations provided by SQLite are
5745
 
** sufficient, however the user has the option of substituting a custom
5746
 
** implementation for specialized deployments or systems for which SQLite
5747
 
** does not provide a suitable implementation. In this case, the user
5748
 
** creates and populates an instance of this structure to pass
5749
 
** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
5750
 
** Additionally, an instance of this structure can be used as an
5751
 
** output variable when querying the system for the current mutex
5752
 
** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
5753
 
**
5754
 
** ^The xMutexInit method defined by this structure is invoked as
5755
 
** part of system initialization by the sqlite3_initialize() function.
5756
 
** ^The xMutexInit routine is called by SQLite exactly once for each
5757
 
** effective call to [sqlite3_initialize()].
5758
 
**
5759
 
** ^The xMutexEnd method defined by this structure is invoked as
5760
 
** part of system shutdown by the sqlite3_shutdown() function. The
5761
 
** implementation of this method is expected to release all outstanding
5762
 
** resources obtained by the mutex methods implementation, especially
5763
 
** those obtained by the xMutexInit method.  ^The xMutexEnd()
5764
 
** interface is invoked exactly once for each call to [sqlite3_shutdown()].
5765
 
**
5766
 
** ^(The remaining seven methods defined by this structure (xMutexAlloc,
5767
 
** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
5768
 
** xMutexNotheld) implement the following interfaces (respectively):
5769
 
**
5770
 
** <ul>
5771
 
**   <li>  [sqlite3_mutex_alloc()] </li>
5772
 
**   <li>  [sqlite3_mutex_free()] </li>
5773
 
**   <li>  [sqlite3_mutex_enter()] </li>
5774
 
**   <li>  [sqlite3_mutex_try()] </li>
5775
 
**   <li>  [sqlite3_mutex_leave()] </li>
5776
 
**   <li>  [sqlite3_mutex_held()] </li>
5777
 
**   <li>  [sqlite3_mutex_notheld()] </li>
5778
 
** </ul>)^
5779
 
**
5780
 
** The only difference is that the public sqlite3_XXX functions enumerated
5781
 
** above silently ignore any invocations that pass a NULL pointer instead
5782
 
** of a valid mutex handle. The implementations of the methods defined
5783
 
** by this structure are not required to handle this case, the results
5784
 
** of passing a NULL pointer instead of a valid mutex handle are undefined
5785
 
** (i.e. it is acceptable to provide an implementation that segfaults if
5786
 
** it is passed a NULL pointer).
5787
 
**
5788
 
** The xMutexInit() method must be threadsafe.  ^It must be harmless to
5789
 
** invoke xMutexInit() multiple times within the same process and without
5790
 
** intervening calls to xMutexEnd().  Second and subsequent calls to
5791
 
** xMutexInit() must be no-ops.
5792
 
**
5793
 
** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
5794
 
** and its associates).  ^Similarly, xMutexAlloc() must not use SQLite memory
5795
 
** allocation for a static mutex.  ^However xMutexAlloc() may use SQLite
5796
 
** memory allocation for a fast or recursive mutex.
5797
 
**
5798
 
** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
5799
 
** called, but only if the prior call to xMutexInit returned SQLITE_OK.
5800
 
** If xMutexInit fails in any way, it is expected to clean up after itself
5801
 
** prior to returning.
5802
 
*/
5803
 
typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
5804
 
struct sqlite3_mutex_methods {
5805
 
  int (*xMutexInit)(void);
5806
 
  int (*xMutexEnd)(void);
5807
 
  sqlite3_mutex *(*xMutexAlloc)(int);
5808
 
  void (*xMutexFree)(sqlite3_mutex *);
5809
 
  void (*xMutexEnter)(sqlite3_mutex *);
5810
 
  int (*xMutexTry)(sqlite3_mutex *);
5811
 
  void (*xMutexLeave)(sqlite3_mutex *);
5812
 
  int (*xMutexHeld)(sqlite3_mutex *);
5813
 
  int (*xMutexNotheld)(sqlite3_mutex *);
5814
 
};
5815
 
 
5816
 
/*
5817
 
** CAPI3REF: Mutex Verification Routines
5818
 
**
5819
 
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
5820
 
** are intended for use inside assert() statements.  ^The SQLite core
5821
 
** never uses these routines except inside an assert() and applications
5822
 
** are advised to follow the lead of the core.  ^The SQLite core only
5823
 
** provides implementations for these routines when it is compiled
5824
 
** with the SQLITE_DEBUG flag.  ^External mutex implementations
5825
 
** are only required to provide these routines if SQLITE_DEBUG is
5826
 
** defined and if NDEBUG is not defined.
5827
 
**
5828
 
** ^These routines should return true if the mutex in their argument
5829
 
** is held or not held, respectively, by the calling thread.
5830
 
**
5831
 
** ^The implementation is not required to provided versions of these
5832
 
** routines that actually work. If the implementation does not provide working
5833
 
** versions of these routines, it should at least provide stubs that always
5834
 
** return true so that one does not get spurious assertion failures.
5835
 
**
5836
 
** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
5837
 
** the routine should return 1.   This seems counter-intuitive since
5838
 
** clearly the mutex cannot be held if it does not exist.  But the
5839
 
** the reason the mutex does not exist is because the build is not
5840
 
** using mutexes.  And we do not want the assert() containing the
5841
 
** call to sqlite3_mutex_held() to fail, so a non-zero return is
5842
 
** the appropriate thing to do.  ^The sqlite3_mutex_notheld()
5843
 
** interface should also return 1 when given a NULL pointer.
5844
 
*/
5845
 
#ifndef NDEBUG
5846
 
SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
5847
 
SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
5848
 
#endif
5849
 
 
5850
 
/*
5851
 
** CAPI3REF: Mutex Types
5852
 
**
5853
 
** The [sqlite3_mutex_alloc()] interface takes a single argument
5854
 
** which is one of these integer constants.
5855
 
**
5856
 
** The set of static mutexes may change from one SQLite release to the
5857
 
** next.  Applications that override the built-in mutex logic must be
5858
 
** prepared to accommodate additional static mutexes.
5859
 
*/
5860
 
#define SQLITE_MUTEX_FAST             0
5861
 
#define SQLITE_MUTEX_RECURSIVE        1
5862
 
#define SQLITE_MUTEX_STATIC_MASTER    2
5863
 
#define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
5864
 
#define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
5865
 
#define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
5866
 
#define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
5867
 
#define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
5868
 
#define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
5869
 
#define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
5870
 
 
5871
 
/*
5872
 
** CAPI3REF: Retrieve the mutex for a database connection
5873
 
**
5874
 
** ^This interface returns a pointer the [sqlite3_mutex] object that 
5875
 
** serializes access to the [database connection] given in the argument
5876
 
** when the [threading mode] is Serialized.
5877
 
** ^If the [threading mode] is Single-thread or Multi-thread then this
5878
 
** routine returns a NULL pointer.
5879
 
*/
5880
 
SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
5881
 
 
5882
 
/*
5883
 
** CAPI3REF: Low-Level Control Of Database Files
5884
 
**
5885
 
** ^The [sqlite3_file_control()] interface makes a direct call to the
5886
 
** xFileControl method for the [sqlite3_io_methods] object associated
5887
 
** with a particular database identified by the second argument. ^The
5888
 
** name of the database is "main" for the main database or "temp" for the
5889
 
** TEMP database, or the name that appears after the AS keyword for
5890
 
** databases that are added using the [ATTACH] SQL command.
5891
 
** ^A NULL pointer can be used in place of "main" to refer to the
5892
 
** main database file.
5893
 
** ^The third and fourth parameters to this routine
5894
 
** are passed directly through to the second and third parameters of
5895
 
** the xFileControl method.  ^The return value of the xFileControl
5896
 
** method becomes the return value of this routine.
5897
 
**
5898
 
** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
5899
 
** a pointer to the underlying [sqlite3_file] object to be written into
5900
 
** the space pointed to by the 4th parameter.  ^The SQLITE_FCNTL_FILE_POINTER
5901
 
** case is a short-circuit path which does not actually invoke the
5902
 
** underlying sqlite3_io_methods.xFileControl method.
5903
 
**
5904
 
** ^If the second parameter (zDbName) does not match the name of any
5905
 
** open database file, then SQLITE_ERROR is returned.  ^This error
5906
 
** code is not remembered and will not be recalled by [sqlite3_errcode()]
5907
 
** or [sqlite3_errmsg()].  The underlying xFileControl method might
5908
 
** also return SQLITE_ERROR.  There is no way to distinguish between
5909
 
** an incorrect zDbName and an SQLITE_ERROR return from the underlying
5910
 
** xFileControl method.
5911
 
**
5912
 
** See also: [SQLITE_FCNTL_LOCKSTATE]
5913
 
*/
5914
 
SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
5915
 
 
5916
 
/*
5917
 
** CAPI3REF: Testing Interface
5918
 
**
5919
 
** ^The sqlite3_test_control() interface is used to read out internal
5920
 
** state of SQLite and to inject faults into SQLite for testing
5921
 
** purposes.  ^The first parameter is an operation code that determines
5922
 
** the number, meaning, and operation of all subsequent parameters.
5923
 
**
5924
 
** This interface is not for use by applications.  It exists solely
5925
 
** for verifying the correct operation of the SQLite library.  Depending
5926
 
** on how the SQLite library is compiled, this interface might not exist.
5927
 
**
5928
 
** The details of the operation codes, their meanings, the parameters
5929
 
** they take, and what they do are all subject to change without notice.
5930
 
** Unlike most of the SQLite API, this function is not guaranteed to
5931
 
** operate consistently from one release to the next.
5932
 
*/
5933
 
SQLITE_API int sqlite3_test_control(int op, ...);
5934
 
 
5935
 
/*
5936
 
** CAPI3REF: Testing Interface Operation Codes
5937
 
**
5938
 
** These constants are the valid operation code parameters used
5939
 
** as the first argument to [sqlite3_test_control()].
5940
 
**
5941
 
** These parameters and their meanings are subject to change
5942
 
** without notice.  These values are for testing purposes only.
5943
 
** Applications should not use any of these parameters or the
5944
 
** [sqlite3_test_control()] interface.
5945
 
*/
5946
 
#define SQLITE_TESTCTRL_FIRST                    5
5947
 
#define SQLITE_TESTCTRL_PRNG_SAVE                5
5948
 
#define SQLITE_TESTCTRL_PRNG_RESTORE             6
5949
 
#define SQLITE_TESTCTRL_PRNG_RESET               7
5950
 
#define SQLITE_TESTCTRL_BITVEC_TEST              8
5951
 
#define SQLITE_TESTCTRL_FAULT_INSTALL            9
5952
 
#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
5953
 
#define SQLITE_TESTCTRL_PENDING_BYTE            11
5954
 
#define SQLITE_TESTCTRL_ASSERT                  12
5955
 
#define SQLITE_TESTCTRL_ALWAYS                  13
5956
 
#define SQLITE_TESTCTRL_RESERVE                 14
5957
 
#define SQLITE_TESTCTRL_OPTIMIZATIONS           15
5958
 
#define SQLITE_TESTCTRL_ISKEYWORD               16
5959
 
#define SQLITE_TESTCTRL_PGHDRSZ                 17
5960
 
#define SQLITE_TESTCTRL_SCRATCHMALLOC           18
5961
 
#define SQLITE_TESTCTRL_LAST                    18
5962
 
 
5963
 
/*
5964
 
** CAPI3REF: SQLite Runtime Status
5965
 
**
5966
 
** ^This interface is used to retrieve runtime status information
5967
 
** about the performance of SQLite, and optionally to reset various
5968
 
** highwater marks.  ^The first argument is an integer code for
5969
 
** the specific parameter to measure.  ^(Recognized integer codes
5970
 
** are of the form [SQLITE_STATUS_MEMORY_USED | SQLITE_STATUS_...].)^
5971
 
** ^The current value of the parameter is returned into *pCurrent.
5972
 
** ^The highest recorded value is returned in *pHighwater.  ^If the
5973
 
** resetFlag is true, then the highest record value is reset after
5974
 
** *pHighwater is written.  ^(Some parameters do not record the highest
5975
 
** value.  For those parameters
5976
 
** nothing is written into *pHighwater and the resetFlag is ignored.)^
5977
 
** ^(Other parameters record only the highwater mark and not the current
5978
 
** value.  For these latter parameters nothing is written into *pCurrent.)^
5979
 
**
5980
 
** ^The sqlite3_status() routine returns SQLITE_OK on success and a
5981
 
** non-zero [error code] on failure.
5982
 
**
5983
 
** This routine is threadsafe but is not atomic.  This routine can be
5984
 
** called while other threads are running the same or different SQLite
5985
 
** interfaces.  However the values returned in *pCurrent and
5986
 
** *pHighwater reflect the status of SQLite at different points in time
5987
 
** and it is possible that another thread might change the parameter
5988
 
** in between the times when *pCurrent and *pHighwater are written.
5989
 
**
5990
 
** See also: [sqlite3_db_status()]
5991
 
*/
5992
 
SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
5993
 
 
5994
 
 
5995
 
/*
5996
 
** CAPI3REF: Status Parameters
5997
 
**
5998
 
** These integer constants designate various run-time status parameters
5999
 
** that can be returned by [sqlite3_status()].
6000
 
**
6001
 
** <dl>
6002
 
** ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
6003
 
** <dd>This parameter is the current amount of memory checked out
6004
 
** using [sqlite3_malloc()], either directly or indirectly.  The
6005
 
** figure includes calls made to [sqlite3_malloc()] by the application
6006
 
** and internal memory usage by the SQLite library.  Scratch memory
6007
 
** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
6008
 
** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
6009
 
** this parameter.  The amount returned is the sum of the allocation
6010
 
** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
6011
 
**
6012
 
** ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
6013
 
** <dd>This parameter records the largest memory allocation request
6014
 
** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
6015
 
** internal equivalents).  Only the value returned in the
6016
 
** *pHighwater parameter to [sqlite3_status()] is of interest.  
6017
 
** The value written into the *pCurrent parameter is undefined.</dd>)^
6018
 
**
6019
 
** ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
6020
 
** <dd>This parameter records the number of separate memory allocations
6021
 
** currently checked out.</dd>)^
6022
 
**
6023
 
** ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
6024
 
** <dd>This parameter returns the number of pages used out of the
6025
 
** [pagecache memory allocator] that was configured using 
6026
 
** [SQLITE_CONFIG_PAGECACHE].  The
6027
 
** value returned is in pages, not in bytes.</dd>)^
6028
 
**
6029
 
** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
6030
 
** <dd>This parameter returns the number of bytes of page cache
6031
 
** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
6032
 
** buffer and where forced to overflow to [sqlite3_malloc()].  The
6033
 
** returned value includes allocations that overflowed because they
6034
 
** where too large (they were larger than the "sz" parameter to
6035
 
** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
6036
 
** no space was left in the page cache.</dd>)^
6037
 
**
6038
 
** ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
6039
 
** <dd>This parameter records the largest memory allocation request
6040
 
** handed to [pagecache memory allocator].  Only the value returned in the
6041
 
** *pHighwater parameter to [sqlite3_status()] is of interest.  
6042
 
** The value written into the *pCurrent parameter is undefined.</dd>)^
6043
 
**
6044
 
** ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
6045
 
** <dd>This parameter returns the number of allocations used out of the
6046
 
** [scratch memory allocator] configured using
6047
 
** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
6048
 
** in bytes.  Since a single thread may only have one scratch allocation
6049
 
** outstanding at time, this parameter also reports the number of threads
6050
 
** using scratch memory at the same time.</dd>)^
6051
 
**
6052
 
** ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
6053
 
** <dd>This parameter returns the number of bytes of scratch memory
6054
 
** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
6055
 
** buffer and where forced to overflow to [sqlite3_malloc()].  The values
6056
 
** returned include overflows because the requested allocation was too
6057
 
** larger (that is, because the requested allocation was larger than the
6058
 
** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
6059
 
** slots were available.
6060
 
** </dd>)^
6061
 
**
6062
 
** ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
6063
 
** <dd>This parameter records the largest memory allocation request
6064
 
** handed to [scratch memory allocator].  Only the value returned in the
6065
 
** *pHighwater parameter to [sqlite3_status()] is of interest.  
6066
 
** The value written into the *pCurrent parameter is undefined.</dd>)^
6067
 
**
6068
 
** ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
6069
 
** <dd>This parameter records the deepest parser stack.  It is only
6070
 
** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
6071
 
** </dl>
6072
 
**
6073
 
** New status parameters may be added from time to time.
6074
 
*/
6075
 
#define SQLITE_STATUS_MEMORY_USED          0
6076
 
#define SQLITE_STATUS_PAGECACHE_USED       1
6077
 
#define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
6078
 
#define SQLITE_STATUS_SCRATCH_USED         3
6079
 
#define SQLITE_STATUS_SCRATCH_OVERFLOW     4
6080
 
#define SQLITE_STATUS_MALLOC_SIZE          5
6081
 
#define SQLITE_STATUS_PARSER_STACK         6
6082
 
#define SQLITE_STATUS_PAGECACHE_SIZE       7
6083
 
#define SQLITE_STATUS_SCRATCH_SIZE         8
6084
 
#define SQLITE_STATUS_MALLOC_COUNT         9
6085
 
 
6086
 
/*
6087
 
** CAPI3REF: Database Connection Status
6088
 
**
6089
 
** ^This interface is used to retrieve runtime status information 
6090
 
** about a single [database connection].  ^The first argument is the
6091
 
** database connection object to be interrogated.  ^The second argument
6092
 
** is an integer constant, taken from the set of
6093
 
** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros, that
6094
 
** determines the parameter to interrogate.  The set of 
6095
 
** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros is likely
6096
 
** to grow in future releases of SQLite.
6097
 
**
6098
 
** ^The current value of the requested parameter is written into *pCur
6099
 
** and the highest instantaneous value is written into *pHiwtr.  ^If
6100
 
** the resetFlg is true, then the highest instantaneous value is
6101
 
** reset back down to the current value.
6102
 
**
6103
 
** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
6104
 
** non-zero [error code] on failure.
6105
 
**
6106
 
** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
6107
 
*/
6108
 
SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
6109
 
 
6110
 
/*
6111
 
** CAPI3REF: Status Parameters for database connections
6112
 
**
6113
 
** These constants are the available integer "verbs" that can be passed as
6114
 
** the second argument to the [sqlite3_db_status()] interface.
6115
 
**
6116
 
** New verbs may be added in future releases of SQLite. Existing verbs
6117
 
** might be discontinued. Applications should check the return code from
6118
 
** [sqlite3_db_status()] to make sure that the call worked.
6119
 
** The [sqlite3_db_status()] interface will return a non-zero error code
6120
 
** if a discontinued or unsupported verb is invoked.
6121
 
**
6122
 
** <dl>
6123
 
** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
6124
 
** <dd>This parameter returns the number of lookaside memory slots currently
6125
 
** checked out.</dd>)^
6126
 
**
6127
 
** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
6128
 
** <dd>This parameter returns the number malloc attempts that were 
6129
 
** satisfied using lookaside memory. Only the high-water value is meaningful;
6130
 
** the current value is always zero.)^
6131
 
**
6132
 
** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
6133
 
** <dd>This parameter returns the number malloc attempts that might have
6134
 
** been satisfied using lookaside memory but failed due to the amount of
6135
 
** memory requested being larger than the lookaside slot size.
6136
 
** Only the high-water value is meaningful;
6137
 
** the current value is always zero.)^
6138
 
**
6139
 
** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
6140
 
** <dd>This parameter returns the number malloc attempts that might have
6141
 
** been satisfied using lookaside memory but failed due to all lookaside
6142
 
** memory already being in use.
6143
 
** Only the high-water value is meaningful;
6144
 
** the current value is always zero.)^
6145
 
**
6146
 
** ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
6147
 
** <dd>This parameter returns the approximate number of of bytes of heap
6148
 
** memory used by all pager caches associated with the database connection.)^
6149
 
** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
6150
 
**
6151
 
** ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
6152
 
** <dd>This parameter returns the approximate number of of bytes of heap
6153
 
** memory used to store the schema for all databases associated
6154
 
** with the connection - main, temp, and any [ATTACH]-ed databases.)^ 
6155
 
** ^The full amount of memory used by the schemas is reported, even if the
6156
 
** schema memory is shared with other database connections due to
6157
 
** [shared cache mode] being enabled.
6158
 
** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
6159
 
**
6160
 
** ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
6161
 
** <dd>This parameter returns the approximate number of of bytes of heap
6162
 
** and lookaside memory used by all prepared statements associated with
6163
 
** the database connection.)^
6164
 
** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
6165
 
** </dd>
6166
 
** </dl>
6167
 
*/
6168
 
#define SQLITE_DBSTATUS_LOOKASIDE_USED       0
6169
 
#define SQLITE_DBSTATUS_CACHE_USED           1
6170
 
#define SQLITE_DBSTATUS_SCHEMA_USED          2
6171
 
#define SQLITE_DBSTATUS_STMT_USED            3
6172
 
#define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
6173
 
#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
6174
 
#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
6175
 
#define SQLITE_DBSTATUS_MAX                  6   /* Largest defined DBSTATUS */
6176
 
 
6177
 
 
6178
 
/*
6179
 
** CAPI3REF: Prepared Statement Status
6180
 
**
6181
 
** ^(Each prepared statement maintains various
6182
 
** [SQLITE_STMTSTATUS_SORT | counters] that measure the number
6183
 
** of times it has performed specific operations.)^  These counters can
6184
 
** be used to monitor the performance characteristics of the prepared
6185
 
** statements.  For example, if the number of table steps greatly exceeds
6186
 
** the number of table searches or result rows, that would tend to indicate
6187
 
** that the prepared statement is using a full table scan rather than
6188
 
** an index.  
6189
 
**
6190
 
** ^(This interface is used to retrieve and reset counter values from
6191
 
** a [prepared statement].  The first argument is the prepared statement
6192
 
** object to be interrogated.  The second argument
6193
 
** is an integer code for a specific [SQLITE_STMTSTATUS_SORT | counter]
6194
 
** to be interrogated.)^
6195
 
** ^The current value of the requested counter is returned.
6196
 
** ^If the resetFlg is true, then the counter is reset to zero after this
6197
 
** interface call returns.
6198
 
**
6199
 
** See also: [sqlite3_status()] and [sqlite3_db_status()].
6200
 
*/
6201
 
SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
6202
 
 
6203
 
/*
6204
 
** CAPI3REF: Status Parameters for prepared statements
6205
 
**
6206
 
** These preprocessor macros define integer codes that name counter
6207
 
** values associated with the [sqlite3_stmt_status()] interface.
6208
 
** The meanings of the various counters are as follows:
6209
 
**
6210
 
** <dl>
6211
 
** <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
6212
 
** <dd>^This is the number of times that SQLite has stepped forward in
6213
 
** a table as part of a full table scan.  Large numbers for this counter
6214
 
** may indicate opportunities for performance improvement through 
6215
 
** careful use of indices.</dd>
6216
 
**
6217
 
** <dt>SQLITE_STMTSTATUS_SORT</dt>
6218
 
** <dd>^This is the number of sort operations that have occurred.
6219
 
** A non-zero value in this counter may indicate an opportunity to
6220
 
** improvement performance through careful use of indices.</dd>
6221
 
**
6222
 
** <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
6223
 
** <dd>^This is the number of rows inserted into transient indices that
6224
 
** were created automatically in order to help joins run faster.
6225
 
** A non-zero value in this counter may indicate an opportunity to
6226
 
** improvement performance by adding permanent indices that do not
6227
 
** need to be reinitialized each time the statement is run.</dd>
6228
 
**
6229
 
** </dl>
6230
 
*/
6231
 
#define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
6232
 
#define SQLITE_STMTSTATUS_SORT              2
6233
 
#define SQLITE_STMTSTATUS_AUTOINDEX         3
6234
 
 
6235
 
/*
6236
 
** CAPI3REF: Custom Page Cache Object
6237
 
**
6238
 
** The sqlite3_pcache type is opaque.  It is implemented by
6239
 
** the pluggable module.  The SQLite core has no knowledge of
6240
 
** its size or internal structure and never deals with the
6241
 
** sqlite3_pcache object except by holding and passing pointers
6242
 
** to the object.
6243
 
**
6244
 
** See [sqlite3_pcache_methods] for additional information.
6245
 
*/
6246
 
typedef struct sqlite3_pcache sqlite3_pcache;
6247
 
 
6248
 
/*
6249
 
** CAPI3REF: Application Defined Page Cache.
6250
 
** KEYWORDS: {page cache}
6251
 
**
6252
 
** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can
6253
 
** register an alternative page cache implementation by passing in an 
6254
 
** instance of the sqlite3_pcache_methods structure.)^
6255
 
** In many applications, most of the heap memory allocated by 
6256
 
** SQLite is used for the page cache.
6257
 
** By implementing a 
6258
 
** custom page cache using this API, an application can better control
6259
 
** the amount of memory consumed by SQLite, the way in which 
6260
 
** that memory is allocated and released, and the policies used to 
6261
 
** determine exactly which parts of a database file are cached and for 
6262
 
** how long.
6263
 
**
6264
 
** The alternative page cache mechanism is an
6265
 
** extreme measure that is only needed by the most demanding applications.
6266
 
** The built-in page cache is recommended for most uses.
6267
 
**
6268
 
** ^(The contents of the sqlite3_pcache_methods structure are copied to an
6269
 
** internal buffer by SQLite within the call to [sqlite3_config].  Hence
6270
 
** the application may discard the parameter after the call to
6271
 
** [sqlite3_config()] returns.)^
6272
 
**
6273
 
** ^(The xInit() method is called once for each effective 
6274
 
** call to [sqlite3_initialize()])^
6275
 
** (usually only once during the lifetime of the process). ^(The xInit()
6276
 
** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^
6277
 
** The intent of the xInit() method is to set up global data structures 
6278
 
** required by the custom page cache implementation. 
6279
 
** ^(If the xInit() method is NULL, then the 
6280
 
** built-in default page cache is used instead of the application defined
6281
 
** page cache.)^
6282
 
**
6283
 
** ^The xShutdown() method is called by [sqlite3_shutdown()].
6284
 
** It can be used to clean up 
6285
 
** any outstanding resources before process shutdown, if required.
6286
 
** ^The xShutdown() method may be NULL.
6287
 
**
6288
 
** ^SQLite automatically serializes calls to the xInit method,
6289
 
** so the xInit method need not be threadsafe.  ^The
6290
 
** xShutdown method is only called from [sqlite3_shutdown()] so it does
6291
 
** not need to be threadsafe either.  All other methods must be threadsafe
6292
 
** in multithreaded applications.
6293
 
**
6294
 
** ^SQLite will never invoke xInit() more than once without an intervening
6295
 
** call to xShutdown().
6296
 
**
6297
 
** ^SQLite invokes the xCreate() method to construct a new cache instance.
6298
 
** SQLite will typically create one cache instance for each open database file,
6299
 
** though this is not guaranteed. ^The
6300
 
** first parameter, szPage, is the size in bytes of the pages that must
6301
 
** be allocated by the cache.  ^szPage will not be a power of two.  ^szPage
6302
 
** will the page size of the database file that is to be cached plus an
6303
 
** increment (here called "R") of less than 250.  SQLite will use the
6304
 
** extra R bytes on each page to store metadata about the underlying
6305
 
** database page on disk.  The value of R depends
6306
 
** on the SQLite version, the target platform, and how SQLite was compiled.
6307
 
** ^(R is constant for a particular build of SQLite. Except, there are two
6308
 
** distinct values of R when SQLite is compiled with the proprietary
6309
 
** ZIPVFS extension.)^  ^The second argument to
6310
 
** xCreate(), bPurgeable, is true if the cache being created will
6311
 
** be used to cache database pages of a file stored on disk, or
6312
 
** false if it is used for an in-memory database. The cache implementation
6313
 
** does not have to do anything special based with the value of bPurgeable;
6314
 
** it is purely advisory.  ^On a cache where bPurgeable is false, SQLite will
6315
 
** never invoke xUnpin() except to deliberately delete a page.
6316
 
** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
6317
 
** false will always have the "discard" flag set to true.  
6318
 
** ^Hence, a cache created with bPurgeable false will
6319
 
** never contain any unpinned pages.
6320
 
**
6321
 
** ^(The xCachesize() method may be called at any time by SQLite to set the
6322
 
** suggested maximum cache-size (number of pages stored by) the cache
6323
 
** instance passed as the first argument. This is the value configured using
6324
 
** the SQLite "[PRAGMA cache_size]" command.)^  As with the bPurgeable
6325
 
** parameter, the implementation is not required to do anything with this
6326
 
** value; it is advisory only.
6327
 
**
6328
 
** The xPagecount() method must return the number of pages currently
6329
 
** stored in the cache, both pinned and unpinned.
6330
 
** 
6331
 
** The xFetch() method locates a page in the cache and returns a pointer to 
6332
 
** the page, or a NULL pointer.
6333
 
** A "page", in this context, means a buffer of szPage bytes aligned at an
6334
 
** 8-byte boundary. The page to be fetched is determined by the key. ^The
6335
 
** mimimum key value is 1.  After it has been retrieved using xFetch, the page 
6336
 
** is considered to be "pinned".
6337
 
**
6338
 
** If the requested page is already in the page cache, then the page cache
6339
 
** implementation must return a pointer to the page buffer with its content
6340
 
** intact.  If the requested page is not already in the cache, then the
6341
 
** cache implementation should use the value of the createFlag
6342
 
** parameter to help it determined what action to take:
6343
 
**
6344
 
** <table border=1 width=85% align=center>
6345
 
** <tr><th> createFlag <th> Behaviour when page is not already in cache
6346
 
** <tr><td> 0 <td> Do not allocate a new page.  Return NULL.
6347
 
** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
6348
 
**                 Otherwise return NULL.
6349
 
** <tr><td> 2 <td> Make every effort to allocate a new page.  Only return
6350
 
**                 NULL if allocating a new page is effectively impossible.
6351
 
** </table>
6352
 
**
6353
 
** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1.  SQLite
6354
 
** will only use a createFlag of 2 after a prior call with a createFlag of 1
6355
 
** failed.)^  In between the to xFetch() calls, SQLite may
6356
 
** attempt to unpin one or more cache pages by spilling the content of
6357
 
** pinned pages to disk and synching the operating system disk cache.
6358
 
**
6359
 
** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
6360
 
** as its second argument.  If the third parameter, discard, is non-zero,
6361
 
** then the page must be evicted from the cache.
6362
 
** ^If the discard parameter is
6363
 
** zero, then the page may be discarded or retained at the discretion of
6364
 
** page cache implementation. ^The page cache implementation
6365
 
** may choose to evict unpinned pages at any time.
6366
 
**
6367
 
** The cache must not perform any reference counting. A single 
6368
 
** call to xUnpin() unpins the page regardless of the number of prior calls 
6369
 
** to xFetch().
6370
 
**
6371
 
** The xRekey() method is used to change the key value associated with the
6372
 
** page passed as the second argument. If the cache
6373
 
** previously contains an entry associated with newKey, it must be
6374
 
** discarded. ^Any prior cache entry associated with newKey is guaranteed not
6375
 
** to be pinned.
6376
 
**
6377
 
** When SQLite calls the xTruncate() method, the cache must discard all
6378
 
** existing cache entries with page numbers (keys) greater than or equal
6379
 
** to the value of the iLimit parameter passed to xTruncate(). If any
6380
 
** of these pages are pinned, they are implicitly unpinned, meaning that
6381
 
** they can be safely discarded.
6382
 
**
6383
 
** ^The xDestroy() method is used to delete a cache allocated by xCreate().
6384
 
** All resources associated with the specified cache should be freed. ^After
6385
 
** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
6386
 
** handle invalid, and will not use it with any other sqlite3_pcache_methods
6387
 
** functions.
6388
 
*/
6389
 
typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
6390
 
struct sqlite3_pcache_methods {
6391
 
  void *pArg;
6392
 
  int (*xInit)(void*);
6393
 
  void (*xShutdown)(void*);
6394
 
  sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
6395
 
  void (*xCachesize)(sqlite3_pcache*, int nCachesize);
6396
 
  int (*xPagecount)(sqlite3_pcache*);
6397
 
  void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
6398
 
  void (*xUnpin)(sqlite3_pcache*, void*, int discard);
6399
 
  void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
6400
 
  void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6401
 
  void (*xDestroy)(sqlite3_pcache*);
6402
 
};
6403
 
 
6404
 
/*
6405
 
** CAPI3REF: Online Backup Object
6406
 
**
6407
 
** The sqlite3_backup object records state information about an ongoing
6408
 
** online backup operation.  ^The sqlite3_backup object is created by
6409
 
** a call to [sqlite3_backup_init()] and is destroyed by a call to
6410
 
** [sqlite3_backup_finish()].
6411
 
**
6412
 
** See Also: [Using the SQLite Online Backup API]
6413
 
*/
6414
 
typedef struct sqlite3_backup sqlite3_backup;
6415
 
 
6416
 
/*
6417
 
** CAPI3REF: Online Backup API.
6418
 
**
6419
 
** The backup API copies the content of one database into another.
6420
 
** It is useful either for creating backups of databases or
6421
 
** for copying in-memory databases to or from persistent files. 
6422
 
**
6423
 
** See Also: [Using the SQLite Online Backup API]
6424
 
**
6425
 
** ^SQLite holds a write transaction open on the destination database file
6426
 
** for the duration of the backup operation.
6427
 
** ^The source database is read-locked only while it is being read;
6428
 
** it is not locked continuously for the entire backup operation.
6429
 
** ^Thus, the backup may be performed on a live source database without
6430
 
** preventing other database connections from
6431
 
** reading or writing to the source database while the backup is underway.
6432
 
** 
6433
 
** ^(To perform a backup operation: 
6434
 
**   <ol>
6435
 
**     <li><b>sqlite3_backup_init()</b> is called once to initialize the
6436
 
**         backup, 
6437
 
**     <li><b>sqlite3_backup_step()</b> is called one or more times to transfer 
6438
 
**         the data between the two databases, and finally
6439
 
**     <li><b>sqlite3_backup_finish()</b> is called to release all resources 
6440
 
**         associated with the backup operation. 
6441
 
**   </ol>)^
6442
 
** There should be exactly one call to sqlite3_backup_finish() for each
6443
 
** successful call to sqlite3_backup_init().
6444
 
**
6445
 
** <b>sqlite3_backup_init()</b>
6446
 
**
6447
 
** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the 
6448
 
** [database connection] associated with the destination database 
6449
 
** and the database name, respectively.
6450
 
** ^The database name is "main" for the main database, "temp" for the
6451
 
** temporary database, or the name specified after the AS keyword in
6452
 
** an [ATTACH] statement for an attached database.
6453
 
** ^The S and M arguments passed to 
6454
 
** sqlite3_backup_init(D,N,S,M) identify the [database connection]
6455
 
** and database name of the source database, respectively.
6456
 
** ^The source and destination [database connections] (parameters S and D)
6457
 
** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
6458
 
** an error.
6459
 
**
6460
 
** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
6461
 
** returned and an error code and error message are stored in the
6462
 
** destination [database connection] D.
6463
 
** ^The error code and message for the failed call to sqlite3_backup_init()
6464
 
** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
6465
 
** [sqlite3_errmsg16()] functions.
6466
 
** ^A successful call to sqlite3_backup_init() returns a pointer to an
6467
 
** [sqlite3_backup] object.
6468
 
** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
6469
 
** sqlite3_backup_finish() functions to perform the specified backup 
6470
 
** operation.
6471
 
**
6472
 
** <b>sqlite3_backup_step()</b>
6473
 
**
6474
 
** ^Function sqlite3_backup_step(B,N) will copy up to N pages between 
6475
 
** the source and destination databases specified by [sqlite3_backup] object B.
6476
 
** ^If N is negative, all remaining source pages are copied. 
6477
 
** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
6478
 
** are still more pages to be copied, then the function returns [SQLITE_OK].
6479
 
** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
6480
 
** from source to destination, then it returns [SQLITE_DONE].
6481
 
** ^If an error occurs while running sqlite3_backup_step(B,N),
6482
 
** then an [error code] is returned. ^As well as [SQLITE_OK] and
6483
 
** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
6484
 
** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
6485
 
** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
6486
 
**
6487
 
** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
6488
 
** <ol>
6489
 
** <li> the destination database was opened read-only, or
6490
 
** <li> the destination database is using write-ahead-log journaling
6491
 
** and the destination and source page sizes differ, or
6492
 
** <li> the destination database is an in-memory database and the
6493
 
** destination and source page sizes differ.
6494
 
** </ol>)^
6495
 
**
6496
 
** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
6497
 
** the [sqlite3_busy_handler | busy-handler function]
6498
 
** is invoked (if one is specified). ^If the 
6499
 
** busy-handler returns non-zero before the lock is available, then 
6500
 
** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
6501
 
** sqlite3_backup_step() can be retried later. ^If the source
6502
 
** [database connection]
6503
 
** is being used to write to the source database when sqlite3_backup_step()
6504
 
** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
6505
 
** case the call to sqlite3_backup_step() can be retried later on. ^(If
6506
 
** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
6507
 
** [SQLITE_READONLY] is returned, then 
6508
 
** there is no point in retrying the call to sqlite3_backup_step(). These 
6509
 
** errors are considered fatal.)^  The application must accept 
6510
 
** that the backup operation has failed and pass the backup operation handle 
6511
 
** to the sqlite3_backup_finish() to release associated resources.
6512
 
**
6513
 
** ^The first call to sqlite3_backup_step() obtains an exclusive lock
6514
 
** on the destination file. ^The exclusive lock is not released until either 
6515
 
** sqlite3_backup_finish() is called or the backup operation is complete 
6516
 
** and sqlite3_backup_step() returns [SQLITE_DONE].  ^Every call to
6517
 
** sqlite3_backup_step() obtains a [shared lock] on the source database that
6518
 
** lasts for the duration of the sqlite3_backup_step() call.
6519
 
** ^Because the source database is not locked between calls to
6520
 
** sqlite3_backup_step(), the source database may be modified mid-way
6521
 
** through the backup process.  ^If the source database is modified by an
6522
 
** external process or via a database connection other than the one being
6523
 
** used by the backup operation, then the backup will be automatically
6524
 
** restarted by the next call to sqlite3_backup_step(). ^If the source 
6525
 
** database is modified by the using the same database connection as is used
6526
 
** by the backup operation, then the backup database is automatically
6527
 
** updated at the same time.
6528
 
**
6529
 
** <b>sqlite3_backup_finish()</b>
6530
 
**
6531
 
** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the 
6532
 
** application wishes to abandon the backup operation, the application
6533
 
** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
6534
 
** ^The sqlite3_backup_finish() interfaces releases all
6535
 
** resources associated with the [sqlite3_backup] object. 
6536
 
** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
6537
 
** active write-transaction on the destination database is rolled back.
6538
 
** The [sqlite3_backup] object is invalid
6539
 
** and may not be used following a call to sqlite3_backup_finish().
6540
 
**
6541
 
** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
6542
 
** sqlite3_backup_step() errors occurred, regardless or whether or not
6543
 
** sqlite3_backup_step() completed.
6544
 
** ^If an out-of-memory condition or IO error occurred during any prior
6545
 
** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
6546
 
** sqlite3_backup_finish() returns the corresponding [error code].
6547
 
**
6548
 
** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
6549
 
** is not a permanent error and does not affect the return value of
6550
 
** sqlite3_backup_finish().
6551
 
**
6552
 
** <b>sqlite3_backup_remaining(), sqlite3_backup_pagecount()</b>
6553
 
**
6554
 
** ^Each call to sqlite3_backup_step() sets two values inside
6555
 
** the [sqlite3_backup] object: the number of pages still to be backed
6556
 
** up and the total number of pages in the source database file.
6557
 
** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
6558
 
** retrieve these two values, respectively.
6559
 
**
6560
 
** ^The values returned by these functions are only updated by
6561
 
** sqlite3_backup_step(). ^If the source database is modified during a backup
6562
 
** operation, then the values are not updated to account for any extra
6563
 
** pages that need to be updated or the size of the source database file
6564
 
** changing.
6565
 
**
6566
 
** <b>Concurrent Usage of Database Handles</b>
6567
 
**
6568
 
** ^The source [database connection] may be used by the application for other
6569
 
** purposes while a backup operation is underway or being initialized.
6570
 
** ^If SQLite is compiled and configured to support threadsafe database
6571
 
** connections, then the source database connection may be used concurrently
6572
 
** from within other threads.
6573
 
**
6574
 
** However, the application must guarantee that the destination 
6575
 
** [database connection] is not passed to any other API (by any thread) after 
6576
 
** sqlite3_backup_init() is called and before the corresponding call to
6577
 
** sqlite3_backup_finish().  SQLite does not currently check to see
6578
 
** if the application incorrectly accesses the destination [database connection]
6579
 
** and so no error code is reported, but the operations may malfunction
6580
 
** nevertheless.  Use of the destination database connection while a
6581
 
** backup is in progress might also also cause a mutex deadlock.
6582
 
**
6583
 
** If running in [shared cache mode], the application must
6584
 
** guarantee that the shared cache used by the destination database
6585
 
** is not accessed while the backup is running. In practice this means
6586
 
** that the application must guarantee that the disk file being 
6587
 
** backed up to is not accessed by any connection within the process,
6588
 
** not just the specific connection that was passed to sqlite3_backup_init().
6589
 
**
6590
 
** The [sqlite3_backup] object itself is partially threadsafe. Multiple 
6591
 
** threads may safely make multiple concurrent calls to sqlite3_backup_step().
6592
 
** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
6593
 
** APIs are not strictly speaking threadsafe. If they are invoked at the
6594
 
** same time as another thread is invoking sqlite3_backup_step() it is
6595
 
** possible that they return invalid values.
6596
 
*/
6597
 
SQLITE_API sqlite3_backup *sqlite3_backup_init(
6598
 
  sqlite3 *pDest,                        /* Destination database handle */
6599
 
  const char *zDestName,                 /* Destination database name */
6600
 
  sqlite3 *pSource,                      /* Source database handle */
6601
 
  const char *zSourceName                /* Source database name */
6602
 
);
6603
 
SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
6604
 
SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
6605
 
SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
6606
 
SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
6607
 
 
6608
 
/*
6609
 
** CAPI3REF: Unlock Notification
6610
 
**
6611
 
** ^When running in shared-cache mode, a database operation may fail with
6612
 
** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
6613
 
** individual tables within the shared-cache cannot be obtained. See
6614
 
** [SQLite Shared-Cache Mode] for a description of shared-cache locking. 
6615
 
** ^This API may be used to register a callback that SQLite will invoke 
6616
 
** when the connection currently holding the required lock relinquishes it.
6617
 
** ^This API is only available if the library was compiled with the
6618
 
** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
6619
 
**
6620
 
** See Also: [Using the SQLite Unlock Notification Feature].
6621
 
**
6622
 
** ^Shared-cache locks are released when a database connection concludes
6623
 
** its current transaction, either by committing it or rolling it back. 
6624
 
**
6625
 
** ^When a connection (known as the blocked connection) fails to obtain a
6626
 
** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
6627
 
** identity of the database connection (the blocking connection) that
6628
 
** has locked the required resource is stored internally. ^After an 
6629
 
** application receives an SQLITE_LOCKED error, it may call the
6630
 
** sqlite3_unlock_notify() method with the blocked connection handle as 
6631
 
** the first argument to register for a callback that will be invoked
6632
 
** when the blocking connections current transaction is concluded. ^The
6633
 
** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
6634
 
** call that concludes the blocking connections transaction.
6635
 
**
6636
 
** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
6637
 
** there is a chance that the blocking connection will have already
6638
 
** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
6639
 
** If this happens, then the specified callback is invoked immediately,
6640
 
** from within the call to sqlite3_unlock_notify().)^
6641
 
**
6642
 
** ^If the blocked connection is attempting to obtain a write-lock on a
6643
 
** shared-cache table, and more than one other connection currently holds
6644
 
** a read-lock on the same table, then SQLite arbitrarily selects one of 
6645
 
** the other connections to use as the blocking connection.
6646
 
**
6647
 
** ^(There may be at most one unlock-notify callback registered by a 
6648
 
** blocked connection. If sqlite3_unlock_notify() is called when the
6649
 
** blocked connection already has a registered unlock-notify callback,
6650
 
** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
6651
 
** called with a NULL pointer as its second argument, then any existing
6652
 
** unlock-notify callback is canceled. ^The blocked connections 
6653
 
** unlock-notify callback may also be canceled by closing the blocked
6654
 
** connection using [sqlite3_close()].
6655
 
**
6656
 
** The unlock-notify callback is not reentrant. If an application invokes
6657
 
** any sqlite3_xxx API functions from within an unlock-notify callback, a
6658
 
** crash or deadlock may be the result.
6659
 
**
6660
 
** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
6661
 
** returns SQLITE_OK.
6662
 
**
6663
 
** <b>Callback Invocation Details</b>
6664
 
**
6665
 
** When an unlock-notify callback is registered, the application provides a 
6666
 
** single void* pointer that is passed to the callback when it is invoked.
6667
 
** However, the signature of the callback function allows SQLite to pass
6668
 
** it an array of void* context pointers. The first argument passed to
6669
 
** an unlock-notify callback is a pointer to an array of void* pointers,
6670
 
** and the second is the number of entries in the array.
6671
 
**
6672
 
** When a blocking connections transaction is concluded, there may be
6673
 
** more than one blocked connection that has registered for an unlock-notify
6674
 
** callback. ^If two or more such blocked connections have specified the
6675
 
** same callback function, then instead of invoking the callback function
6676
 
** multiple times, it is invoked once with the set of void* context pointers
6677
 
** specified by the blocked connections bundled together into an array.
6678
 
** This gives the application an opportunity to prioritize any actions 
6679
 
** related to the set of unblocked database connections.
6680
 
**
6681
 
** <b>Deadlock Detection</b>
6682
 
**
6683
 
** Assuming that after registering for an unlock-notify callback a 
6684
 
** database waits for the callback to be issued before taking any further
6685
 
** action (a reasonable assumption), then using this API may cause the
6686
 
** application to deadlock. For example, if connection X is waiting for
6687
 
** connection Y's transaction to be concluded, and similarly connection
6688
 
** Y is waiting on connection X's transaction, then neither connection
6689
 
** will proceed and the system may remain deadlocked indefinitely.
6690
 
**
6691
 
** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
6692
 
** detection. ^If a given call to sqlite3_unlock_notify() would put the
6693
 
** system in a deadlocked state, then SQLITE_LOCKED is returned and no
6694
 
** unlock-notify callback is registered. The system is said to be in
6695
 
** a deadlocked state if connection A has registered for an unlock-notify
6696
 
** callback on the conclusion of connection B's transaction, and connection
6697
 
** B has itself registered for an unlock-notify callback when connection
6698
 
** A's transaction is concluded. ^Indirect deadlock is also detected, so
6699
 
** the system is also considered to be deadlocked if connection B has
6700
 
** registered for an unlock-notify callback on the conclusion of connection
6701
 
** C's transaction, where connection C is waiting on connection A. ^Any
6702
 
** number of levels of indirection are allowed.
6703
 
**
6704
 
** <b>The "DROP TABLE" Exception</b>
6705
 
**
6706
 
** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost 
6707
 
** always appropriate to call sqlite3_unlock_notify(). There is however,
6708
 
** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
6709
 
** SQLite checks if there are any currently executing SELECT statements
6710
 
** that belong to the same connection. If there are, SQLITE_LOCKED is
6711
 
** returned. In this case there is no "blocking connection", so invoking
6712
 
** sqlite3_unlock_notify() results in the unlock-notify callback being
6713
 
** invoked immediately. If the application then re-attempts the "DROP TABLE"
6714
 
** or "DROP INDEX" query, an infinite loop might be the result.
6715
 
**
6716
 
** One way around this problem is to check the extended error code returned
6717
 
** by an sqlite3_step() call. ^(If there is a blocking connection, then the
6718
 
** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
6719
 
** the special "DROP TABLE/INDEX" case, the extended error code is just 
6720
 
** SQLITE_LOCKED.)^
6721
 
*/
6722
 
SQLITE_API int sqlite3_unlock_notify(
6723
 
  sqlite3 *pBlocked,                          /* Waiting connection */
6724
 
  void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
6725
 
  void *pNotifyArg                            /* Argument to pass to xNotify */
6726
 
);
6727
 
 
6728
 
 
6729
 
/*
6730
 
** CAPI3REF: String Comparison
6731
 
**
6732
 
** ^The [sqlite3_strnicmp()] API allows applications and extensions to
6733
 
** compare the contents of two buffers containing UTF-8 strings in a
6734
 
** case-independent fashion, using the same definition of case independence 
6735
 
** that SQLite uses internally when comparing identifiers.
6736
 
*/
6737
 
SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
6738
 
 
6739
 
/*
6740
 
** CAPI3REF: Error Logging Interface
6741
 
**
6742
 
** ^The [sqlite3_log()] interface writes a message into the error log
6743
 
** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
6744
 
** ^If logging is enabled, the zFormat string and subsequent arguments are
6745
 
** used with [sqlite3_snprintf()] to generate the final output string.
6746
 
**
6747
 
** The sqlite3_log() interface is intended for use by extensions such as
6748
 
** virtual tables, collating functions, and SQL functions.  While there is
6749
 
** nothing to prevent an application from calling sqlite3_log(), doing so
6750
 
** is considered bad form.
6751
 
**
6752
 
** The zFormat string must not be NULL.
6753
 
**
6754
 
** To avoid deadlocks and other threading problems, the sqlite3_log() routine
6755
 
** will not use dynamically allocated memory.  The log message is stored in
6756
 
** a fixed-length buffer on the stack.  If the log message is longer than
6757
 
** a few hundred characters, it will be truncated to the length of the
6758
 
** buffer.
6759
 
*/
6760
 
SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
6761
 
 
6762
 
/*
6763
 
** CAPI3REF: Write-Ahead Log Commit Hook
6764
 
**
6765
 
** ^The [sqlite3_wal_hook()] function is used to register a callback that
6766
 
** will be invoked each time a database connection commits data to a
6767
 
** [write-ahead log] (i.e. whenever a transaction is committed in
6768
 
** [journal_mode | journal_mode=WAL mode]). 
6769
 
**
6770
 
** ^The callback is invoked by SQLite after the commit has taken place and 
6771
 
** the associated write-lock on the database released, so the implementation 
6772
 
** may read, write or [checkpoint] the database as required.
6773
 
**
6774
 
** ^The first parameter passed to the callback function when it is invoked
6775
 
** is a copy of the third parameter passed to sqlite3_wal_hook() when
6776
 
** registering the callback. ^The second is a copy of the database handle.
6777
 
** ^The third parameter is the name of the database that was written to -
6778
 
** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
6779
 
** is the number of pages currently in the write-ahead log file,
6780
 
** including those that were just committed.
6781
 
**
6782
 
** The callback function should normally return [SQLITE_OK].  ^If an error
6783
 
** code is returned, that error will propagate back up through the
6784
 
** SQLite code base to cause the statement that provoked the callback
6785
 
** to report an error, though the commit will have still occurred. If the
6786
 
** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
6787
 
** that does not correspond to any valid SQLite error code, the results
6788
 
** are undefined.
6789
 
**
6790
 
** A single database handle may have at most a single write-ahead log callback 
6791
 
** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
6792
 
** previously registered write-ahead log callback. ^Note that the
6793
 
** [sqlite3_wal_autocheckpoint()] interface and the
6794
 
** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
6795
 
** those overwrite any prior [sqlite3_wal_hook()] settings.
6796
 
*/
6797
 
SQLITE_API void *sqlite3_wal_hook(
6798
 
  sqlite3*, 
6799
 
  int(*)(void *,sqlite3*,const char*,int),
6800
 
  void*
6801
 
);
6802
 
 
6803
 
/*
6804
 
** CAPI3REF: Configure an auto-checkpoint
6805
 
**
6806
 
** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
6807
 
** [sqlite3_wal_hook()] that causes any database on [database connection] D
6808
 
** to automatically [checkpoint]
6809
 
** after committing a transaction if there are N or
6810
 
** more frames in the [write-ahead log] file.  ^Passing zero or 
6811
 
** a negative value as the nFrame parameter disables automatic
6812
 
** checkpoints entirely.
6813
 
**
6814
 
** ^The callback registered by this function replaces any existing callback
6815
 
** registered using [sqlite3_wal_hook()].  ^Likewise, registering a callback
6816
 
** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
6817
 
** configured by this function.
6818
 
**
6819
 
** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
6820
 
** from SQL.
6821
 
**
6822
 
** ^Every new [database connection] defaults to having the auto-checkpoint
6823
 
** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
6824
 
** pages.  The use of this interface
6825
 
** is only necessary if the default setting is found to be suboptimal
6826
 
** for a particular application.
6827
 
*/
6828
 
SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
6829
 
 
6830
 
/*
6831
 
** CAPI3REF: Checkpoint a database
6832
 
**
6833
 
** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
6834
 
** on [database connection] D to be [checkpointed].  ^If X is NULL or an
6835
 
** empty string, then a checkpoint is run on all databases of
6836
 
** connection D.  ^If the database connection D is not in
6837
 
** [WAL | write-ahead log mode] then this interface is a harmless no-op.
6838
 
**
6839
 
** ^The [wal_checkpoint pragma] can be used to invoke this interface
6840
 
** from SQL.  ^The [sqlite3_wal_autocheckpoint()] interface and the
6841
 
** [wal_autocheckpoint pragma] can be used to cause this interface to be
6842
 
** run whenever the WAL reaches a certain size threshold.
6843
 
**
6844
 
** See also: [sqlite3_wal_checkpoint_v2()]
6845
 
*/
6846
 
SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
6847
 
 
6848
 
/*
6849
 
** CAPI3REF: Checkpoint a database
6850
 
**
6851
 
** Run a checkpoint operation on WAL database zDb attached to database 
6852
 
** handle db. The specific operation is determined by the value of the 
6853
 
** eMode parameter:
6854
 
**
6855
 
** <dl>
6856
 
** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
6857
 
**   Checkpoint as many frames as possible without waiting for any database 
6858
 
**   readers or writers to finish. Sync the db file if all frames in the log
6859
 
**   are checkpointed. This mode is the same as calling 
6860
 
**   sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
6861
 
**
6862
 
** <dt>SQLITE_CHECKPOINT_FULL<dd>
6863
 
**   This mode blocks (calls the busy-handler callback) until there is no
6864
 
**   database writer and all readers are reading from the most recent database
6865
 
**   snapshot. It then checkpoints all frames in the log file and syncs the
6866
 
**   database file. This call blocks database writers while it is running,
6867
 
**   but not database readers.
6868
 
**
6869
 
** <dt>SQLITE_CHECKPOINT_RESTART<dd>
6870
 
**   This mode works the same way as SQLITE_CHECKPOINT_FULL, except after 
6871
 
**   checkpointing the log file it blocks (calls the busy-handler callback)
6872
 
**   until all readers are reading from the database file only. This ensures 
6873
 
**   that the next client to write to the database file restarts the log file 
6874
 
**   from the beginning. This call blocks database writers while it is running,
6875
 
**   but not database readers.
6876
 
** </dl>
6877
 
**
6878
 
** If pnLog is not NULL, then *pnLog is set to the total number of frames in
6879
 
** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
6880
 
** the total number of checkpointed frames (including any that were already
6881
 
** checkpointed when this function is called). *pnLog and *pnCkpt may be
6882
 
** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
6883
 
** If no values are available because of an error, they are both set to -1
6884
 
** before returning to communicate this to the caller.
6885
 
**
6886
 
** All calls obtain an exclusive "checkpoint" lock on the database file. If
6887
 
** any other process is running a checkpoint operation at the same time, the 
6888
 
** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a 
6889
 
** busy-handler configured, it will not be invoked in this case.
6890
 
**
6891
 
** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive 
6892
 
** "writer" lock on the database file. If the writer lock cannot be obtained
6893
 
** immediately, and a busy-handler is configured, it is invoked and the writer
6894
 
** lock retried until either the busy-handler returns 0 or the lock is
6895
 
** successfully obtained. The busy-handler is also invoked while waiting for
6896
 
** database readers as described above. If the busy-handler returns 0 before
6897
 
** the writer lock is obtained or while waiting for database readers, the
6898
 
** checkpoint operation proceeds from that point in the same way as 
6899
 
** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible 
6900
 
** without blocking any further. SQLITE_BUSY is returned in this case.
6901
 
**
6902
 
** If parameter zDb is NULL or points to a zero length string, then the
6903
 
** specified operation is attempted on all WAL databases. In this case the
6904
 
** values written to output parameters *pnLog and *pnCkpt are undefined. If 
6905
 
** an SQLITE_BUSY error is encountered when processing one or more of the 
6906
 
** attached WAL databases, the operation is still attempted on any remaining 
6907
 
** attached databases and SQLITE_BUSY is returned to the caller. If any other 
6908
 
** error occurs while processing an attached database, processing is abandoned 
6909
 
** and the error code returned to the caller immediately. If no error 
6910
 
** (SQLITE_BUSY or otherwise) is encountered while processing the attached 
6911
 
** databases, SQLITE_OK is returned.
6912
 
**
6913
 
** If database zDb is the name of an attached database that is not in WAL
6914
 
** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
6915
 
** zDb is not NULL (or a zero length string) and is not the name of any
6916
 
** attached database, SQLITE_ERROR is returned to the caller.
6917
 
*/
6918
 
SQLITE_API int sqlite3_wal_checkpoint_v2(
6919
 
  sqlite3 *db,                    /* Database handle */
6920
 
  const char *zDb,                /* Name of attached database (or NULL) */
6921
 
  int eMode,                      /* SQLITE_CHECKPOINT_* value */
6922
 
  int *pnLog,                     /* OUT: Size of WAL log in frames */
6923
 
  int *pnCkpt                     /* OUT: Total number of frames checkpointed */
6924
 
);
6925
 
 
6926
 
/*
6927
 
** CAPI3REF: Checkpoint operation parameters
6928
 
**
6929
 
** These constants can be used as the 3rd parameter to
6930
 
** [sqlite3_wal_checkpoint_v2()].  See the [sqlite3_wal_checkpoint_v2()]
6931
 
** documentation for additional information about the meaning and use of
6932
 
** each of these values.
6933
 
*/
6934
 
#define SQLITE_CHECKPOINT_PASSIVE 0
6935
 
#define SQLITE_CHECKPOINT_FULL    1
6936
 
#define SQLITE_CHECKPOINT_RESTART 2
6937
 
 
6938
 
 
6939
 
/*
6940
 
** Undo the hack that converts floating point types to integer for
6941
 
** builds on processors without floating point support.
6942
 
*/
6943
 
#ifdef SQLITE_OMIT_FLOATING_POINT
6944
 
# undef double
6945
 
#endif
6946
 
 
6947
 
#if 0
6948
 
}  /* End of the 'extern "C"' block */
6949
 
#endif
6950
 
#endif
6951
 
 
6952
 
/*
6953
 
** 2010 August 30
6954
 
**
6955
 
** The author disclaims copyright to this source code.  In place of
6956
 
** a legal notice, here is a blessing:
6957
 
**
6958
 
**    May you do good and not evil.
6959
 
**    May you find forgiveness for yourself and forgive others.
6960
 
**    May you share freely, never taking more than you give.
6961
 
**
6962
 
*************************************************************************
6963
 
*/
6964
 
 
6965
 
#ifndef _SQLITE3RTREE_H_
6966
 
#define _SQLITE3RTREE_H_
6967
 
 
6968
 
 
6969
 
#if 0
6970
 
extern "C" {
6971
 
#endif
6972
 
 
6973
 
typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
6974
 
 
6975
 
/*
6976
 
** Register a geometry callback named zGeom that can be used as part of an
6977
 
** R-Tree geometry query as follows:
6978
 
**
6979
 
**   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
6980
 
*/
6981
 
SQLITE_API int sqlite3_rtree_geometry_callback(
6982
 
  sqlite3 *db,
6983
 
  const char *zGeom,
6984
 
  int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes),
6985
 
  void *pContext
6986
 
);
6987
 
 
6988
 
 
6989
 
/*
6990
 
** A pointer to a structure of the following type is passed as the first
6991
 
** argument to callbacks registered using rtree_geometry_callback().
6992
 
*/
6993
 
struct sqlite3_rtree_geometry {
6994
 
  void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
6995
 
  int nParam;                     /* Size of array aParam[] */
6996
 
  double *aParam;                 /* Parameters passed to SQL geom function */
6997
 
  void *pUser;                    /* Callback implementation user data */
6998
 
  void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
6999
 
};
7000
 
 
7001
 
 
7002
 
#if 0
7003
 
}  /* end of the 'extern "C"' block */
7004
 
#endif
7005
 
 
7006
 
#endif  /* ifndef _SQLITE3RTREE_H_ */
7007
 
 
7008
 
 
7009
 
/************** End of sqlite3.h *********************************************/
7010
 
/************** Continuing where we left off in sqliteInt.h ******************/
7011
 
/************** Include hash.h in the middle of sqliteInt.h ******************/
7012
 
/************** Begin file hash.h ********************************************/
7013
 
/*
7014
 
** 2001 September 22
7015
 
**
7016
 
** The author disclaims copyright to this source code.  In place of
7017
 
** a legal notice, here is a blessing:
7018
 
**
7019
 
**    May you do good and not evil.
7020
 
**    May you find forgiveness for yourself and forgive others.
7021
 
**    May you share freely, never taking more than you give.
7022
 
**
7023
 
*************************************************************************
7024
 
** This is the header file for the generic hash-table implemenation
7025
 
** used in SQLite.
7026
 
*/
7027
 
#ifndef _SQLITE_HASH_H_
7028
 
#define _SQLITE_HASH_H_
7029
 
 
7030
 
/* Forward declarations of structures. */
7031
 
typedef struct Hash Hash;
7032
 
typedef struct HashElem HashElem;
7033
 
 
7034
 
/* A complete hash table is an instance of the following structure.
7035
 
** The internals of this structure are intended to be opaque -- client
7036
 
** code should not attempt to access or modify the fields of this structure
7037
 
** directly.  Change this structure only by using the routines below.
7038
 
** However, some of the "procedures" and "functions" for modifying and
7039
 
** accessing this structure are really macros, so we can't really make
7040
 
** this structure opaque.
7041
 
**
7042
 
** All elements of the hash table are on a single doubly-linked list.
7043
 
** Hash.first points to the head of this list.
7044
 
**
7045
 
** There are Hash.htsize buckets.  Each bucket points to a spot in
7046
 
** the global doubly-linked list.  The contents of the bucket are the
7047
 
** element pointed to plus the next _ht.count-1 elements in the list.
7048
 
**
7049
 
** Hash.htsize and Hash.ht may be zero.  In that case lookup is done
7050
 
** by a linear search of the global list.  For small tables, the 
7051
 
** Hash.ht table is never allocated because if there are few elements
7052
 
** in the table, it is faster to do a linear search than to manage
7053
 
** the hash table.
7054
 
*/
7055
 
struct Hash {
7056
 
  unsigned int htsize;      /* Number of buckets in the hash table */
7057
 
  unsigned int count;       /* Number of entries in this table */
7058
 
  HashElem *first;          /* The first element of the array */
7059
 
  struct _ht {              /* the hash table */
7060
 
    int count;                 /* Number of entries with this hash */
7061
 
    HashElem *chain;           /* Pointer to first entry with this hash */
7062
 
  } *ht;
7063
 
};
7064
 
 
7065
 
/* Each element in the hash table is an instance of the following 
7066
 
** structure.  All elements are stored on a single doubly-linked list.
7067
 
**
7068
 
** Again, this structure is intended to be opaque, but it can't really
7069
 
** be opaque because it is used by macros.
7070
 
*/
7071
 
struct HashElem {
7072
 
  HashElem *next, *prev;       /* Next and previous elements in the table */
7073
 
  void *data;                  /* Data associated with this element */
7074
 
  const char *pKey; int nKey;  /* Key associated with this element */
7075
 
};
7076
 
 
7077
 
/*
7078
 
** Access routines.  To delete, insert a NULL pointer.
7079
 
*/
7080
 
SQLITE_PRIVATE void sqlite3HashInit(Hash*);
7081
 
SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
7082
 
SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
7083
 
SQLITE_PRIVATE void sqlite3HashClear(Hash*);
7084
 
 
7085
 
/*
7086
 
** Macros for looping over all elements of a hash table.  The idiom is
7087
 
** like this:
7088
 
**
7089
 
**   Hash h;
7090
 
**   HashElem *p;
7091
 
**   ...
7092
 
**   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
7093
 
**     SomeStructure *pData = sqliteHashData(p);
7094
 
**     // do something with pData
7095
 
**   }
7096
 
*/
7097
 
#define sqliteHashFirst(H)  ((H)->first)
7098
 
#define sqliteHashNext(E)   ((E)->next)
7099
 
#define sqliteHashData(E)   ((E)->data)
7100
 
/* #define sqliteHashKey(E)    ((E)->pKey) // NOT USED */
7101
 
/* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */
7102
 
 
7103
 
/*
7104
 
** Number of entries in a hash table
7105
 
*/
7106
 
/* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
7107
 
 
7108
 
#endif /* _SQLITE_HASH_H_ */
7109
 
 
7110
 
/************** End of hash.h ************************************************/
7111
 
/************** Continuing where we left off in sqliteInt.h ******************/
7112
 
/************** Include parse.h in the middle of sqliteInt.h *****************/
7113
 
/************** Begin file parse.h *******************************************/
7114
 
#define TK_SEMI                            1
7115
 
#define TK_EXPLAIN                         2
7116
 
#define TK_QUERY                           3
7117
 
#define TK_PLAN                            4
7118
 
#define TK_BEGIN                           5
7119
 
#define TK_TRANSACTION                     6
7120
 
#define TK_DEFERRED                        7
7121
 
#define TK_IMMEDIATE                       8
7122
 
#define TK_EXCLUSIVE                       9
7123
 
#define TK_COMMIT                         10
7124
 
#define TK_END                            11
7125
 
#define TK_ROLLBACK                       12
7126
 
#define TK_SAVEPOINT                      13
7127
 
#define TK_RELEASE                        14
7128
 
#define TK_TO                             15
7129
 
#define TK_TABLE                          16
7130
 
#define TK_CREATE                         17
7131
 
#define TK_IF                             18
7132
 
#define TK_NOT                            19
7133
 
#define TK_EXISTS                         20
7134
 
#define TK_TEMP                           21
7135
 
#define TK_LP                             22
7136
 
#define TK_RP                             23
7137
 
#define TK_AS                             24
7138
 
#define TK_COMMA                          25
7139
 
#define TK_ID                             26
7140
 
#define TK_INDEXED                        27
7141
 
#define TK_ABORT                          28
7142
 
#define TK_ACTION                         29
7143
 
#define TK_AFTER                          30
7144
 
#define TK_ANALYZE                        31
7145
 
#define TK_ASC                            32
7146
 
#define TK_ATTACH                         33
7147
 
#define TK_BEFORE                         34
7148
 
#define TK_BY                             35
7149
 
#define TK_CASCADE                        36
7150
 
#define TK_CAST                           37
7151
 
#define TK_COLUMNKW                       38
7152
 
#define TK_CONFLICT                       39
7153
 
#define TK_DATABASE                       40
7154
 
#define TK_DESC                           41
7155
 
#define TK_DETACH                         42
7156
 
#define TK_EACH                           43
7157
 
#define TK_FAIL                           44
7158
 
#define TK_FOR                            45
7159
 
#define TK_IGNORE                         46
7160
 
#define TK_INITIALLY                      47
7161
 
#define TK_INSTEAD                        48
7162
 
#define TK_LIKE_KW                        49
7163
 
#define TK_MATCH                          50
7164
 
#define TK_NO                             51
7165
 
#define TK_KEY                            52
7166
 
#define TK_OF                             53
7167
 
#define TK_OFFSET                         54
7168
 
#define TK_PRAGMA                         55
7169
 
#define TK_RAISE                          56
7170
 
#define TK_REPLACE                        57
7171
 
#define TK_RESTRICT                       58
7172
 
#define TK_ROW                            59
7173
 
#define TK_TRIGGER                        60
7174
 
#define TK_VACUUM                         61
7175
 
#define TK_VIEW                           62
7176
 
#define TK_VIRTUAL                        63
7177
 
#define TK_REINDEX                        64
7178
 
#define TK_RENAME                         65
7179
 
#define TK_CTIME_KW                       66
7180
 
#define TK_ANY                            67
7181
 
#define TK_OR                             68
7182
 
#define TK_AND                            69
7183
 
#define TK_IS                             70
7184
 
#define TK_BETWEEN                        71
7185
 
#define TK_IN                             72
7186
 
#define TK_ISNULL                         73
7187
 
#define TK_NOTNULL                        74
7188
 
#define TK_NE                             75
7189
 
#define TK_EQ                             76
7190
 
#define TK_GT                             77
7191
 
#define TK_LE                             78
7192
 
#define TK_LT                             79
7193
 
#define TK_GE                             80
7194
 
#define TK_ESCAPE                         81
7195
 
#define TK_BITAND                         82
7196
 
#define TK_BITOR                          83
7197
 
#define TK_LSHIFT                         84
7198
 
#define TK_RSHIFT                         85
7199
 
#define TK_PLUS                           86
7200
 
#define TK_MINUS                          87
7201
 
#define TK_STAR                           88
7202
 
#define TK_SLASH                          89
7203
 
#define TK_REM                            90
7204
 
#define TK_CONCAT                         91
7205
 
#define TK_COLLATE                        92
7206
 
#define TK_BITNOT                         93
7207
 
#define TK_STRING                         94
7208
 
#define TK_JOIN_KW                        95
7209
 
#define TK_CONSTRAINT                     96
7210
 
#define TK_DEFAULT                        97
7211
 
#define TK_NULL                           98
7212
 
#define TK_PRIMARY                        99
7213
 
#define TK_UNIQUE                         100
7214
 
#define TK_CHECK                          101
7215
 
#define TK_REFERENCES                     102
7216
 
#define TK_AUTOINCR                       103
7217
 
#define TK_ON                             104
7218
 
#define TK_INSERT                         105
7219
 
#define TK_DELETE                         106
7220
 
#define TK_UPDATE                         107
7221
 
#define TK_SET                            108
7222
 
#define TK_DEFERRABLE                     109
7223
 
#define TK_FOREIGN                        110
7224
 
#define TK_DROP                           111
7225
 
#define TK_UNION                          112
7226
 
#define TK_ALL                            113
7227
 
#define TK_EXCEPT                         114
7228
 
#define TK_INTERSECT                      115
7229
 
#define TK_SELECT                         116
7230
 
#define TK_DISTINCT                       117
7231
 
#define TK_DOT                            118
7232
 
#define TK_FROM                           119
7233
 
#define TK_JOIN                           120
7234
 
#define TK_USING                          121
7235
 
#define TK_ORDER                          122
7236
 
#define TK_GROUP                          123
7237
 
#define TK_HAVING                         124
7238
 
#define TK_LIMIT                          125
7239
 
#define TK_WHERE                          126
7240
 
#define TK_INTO                           127
7241
 
#define TK_VALUES                         128
7242
 
#define TK_INTEGER                        129
7243
 
#define TK_FLOAT                          130
7244
 
#define TK_BLOB                           131
7245
 
#define TK_REGISTER                       132
7246
 
#define TK_VARIABLE                       133
7247
 
#define TK_CASE                           134
7248
 
#define TK_WHEN                           135
7249
 
#define TK_THEN                           136
7250
 
#define TK_ELSE                           137
7251
 
#define TK_INDEX                          138
7252
 
#define TK_ALTER                          139
7253
 
#define TK_ADD                            140
7254
 
#define TK_TO_TEXT                        141
7255
 
#define TK_TO_BLOB                        142
7256
 
#define TK_TO_NUMERIC                     143
7257
 
#define TK_TO_INT                         144
7258
 
#define TK_TO_REAL                        145
7259
 
#define TK_ISNOT                          146
7260
 
#define TK_END_OF_FILE                    147
7261
 
#define TK_ILLEGAL                        148
7262
 
#define TK_SPACE                          149
7263
 
#define TK_UNCLOSED_STRING                150
7264
 
#define TK_FUNCTION                       151
7265
 
#define TK_COLUMN                         152
7266
 
#define TK_AGG_FUNCTION                   153
7267
 
#define TK_AGG_COLUMN                     154
7268
 
#define TK_CONST_FUNC                     155
7269
 
#define TK_UMINUS                         156
7270
 
#define TK_UPLUS                          157
7271
 
 
7272
 
/************** End of parse.h ***********************************************/
7273
 
/************** Continuing where we left off in sqliteInt.h ******************/
7274
 
#include <stdio.h>
7275
 
#include <stdlib.h>
7276
 
#include <string.h>
7277
 
#include <assert.h>
7278
 
#include <stddef.h>
7279
 
 
7280
 
/*
7281
 
** If compiling for a processor that lacks floating point support,
7282
 
** substitute integer for floating-point
7283
 
*/
7284
 
#ifdef SQLITE_OMIT_FLOATING_POINT
7285
 
# define double sqlite_int64
7286
 
# define float sqlite_int64
7287
 
# define LONGDOUBLE_TYPE sqlite_int64
7288
 
# ifndef SQLITE_BIG_DBL
7289
 
#   define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
7290
 
# endif
7291
 
# define SQLITE_OMIT_DATETIME_FUNCS 1
7292
 
# define SQLITE_OMIT_TRACE 1
7293
 
# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
7294
 
# undef SQLITE_HAVE_ISNAN
7295
 
#endif
7296
 
#ifndef SQLITE_BIG_DBL
7297
 
# define SQLITE_BIG_DBL (1e99)
7298
 
#endif
7299
 
 
7300
 
/*
7301
 
** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
7302
 
** afterward. Having this macro allows us to cause the C compiler 
7303
 
** to omit code used by TEMP tables without messy #ifndef statements.
7304
 
*/
7305
 
#ifdef SQLITE_OMIT_TEMPDB
7306
 
#define OMIT_TEMPDB 1
7307
 
#else
7308
 
#define OMIT_TEMPDB 0
7309
 
#endif
7310
 
 
7311
 
/*
7312
 
** The "file format" number is an integer that is incremented whenever
7313
 
** the VDBE-level file format changes.  The following macros define the
7314
 
** the default file format for new databases and the maximum file format
7315
 
** that the library can read.
7316
 
*/
7317
 
#define SQLITE_MAX_FILE_FORMAT 4
7318
 
#ifndef SQLITE_DEFAULT_FILE_FORMAT
7319
 
# define SQLITE_DEFAULT_FILE_FORMAT 1
7320
 
#endif
7321
 
 
7322
 
/*
7323
 
** Determine whether triggers are recursive by default.  This can be
7324
 
** changed at run-time using a pragma.
7325
 
*/
7326
 
#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
7327
 
# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
7328
 
#endif
7329
 
 
7330
 
/*
7331
 
** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
7332
 
** on the command-line
7333
 
*/
7334
 
#ifndef SQLITE_TEMP_STORE
7335
 
# define SQLITE_TEMP_STORE 1
7336
 
#endif
7337
 
 
7338
 
/*
7339
 
** GCC does not define the offsetof() macro so we'll have to do it
7340
 
** ourselves.
7341
 
*/
7342
 
#ifndef offsetof
7343
 
#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
7344
 
#endif
7345
 
 
7346
 
/*
7347
 
** Check to see if this machine uses EBCDIC.  (Yes, believe it or
7348
 
** not, there are still machines out there that use EBCDIC.)
7349
 
*/
7350
 
#if 'A' == '\301'
7351
 
# define SQLITE_EBCDIC 1
7352
 
#else
7353
 
# define SQLITE_ASCII 1
7354
 
#endif
7355
 
 
7356
 
/*
7357
 
** Integers of known sizes.  These typedefs might change for architectures
7358
 
** where the sizes very.  Preprocessor macros are available so that the
7359
 
** types can be conveniently redefined at compile-type.  Like this:
7360
 
**
7361
 
**         cc '-DUINTPTR_TYPE=long long int' ...
7362
 
*/
7363
 
#ifndef UINT32_TYPE
7364
 
# ifdef HAVE_UINT32_T
7365
 
#  define UINT32_TYPE uint32_t
7366
 
# else
7367
 
#  define UINT32_TYPE unsigned int
7368
 
# endif
7369
 
#endif
7370
 
#ifndef UINT16_TYPE
7371
 
# ifdef HAVE_UINT16_T
7372
 
#  define UINT16_TYPE uint16_t
7373
 
# else
7374
 
#  define UINT16_TYPE unsigned short int
7375
 
# endif
7376
 
#endif
7377
 
#ifndef INT16_TYPE
7378
 
# ifdef HAVE_INT16_T
7379
 
#  define INT16_TYPE int16_t
7380
 
# else
7381
 
#  define INT16_TYPE short int
7382
 
# endif
7383
 
#endif
7384
 
#ifndef UINT8_TYPE
7385
 
# ifdef HAVE_UINT8_T
7386
 
#  define UINT8_TYPE uint8_t
7387
 
# else
7388
 
#  define UINT8_TYPE unsigned char
7389
 
# endif
7390
 
#endif
7391
 
#ifndef INT8_TYPE
7392
 
# ifdef HAVE_INT8_T
7393
 
#  define INT8_TYPE int8_t
7394
 
# else
7395
 
#  define INT8_TYPE signed char
7396
 
# endif
7397
 
#endif
7398
 
#ifndef LONGDOUBLE_TYPE
7399
 
# define LONGDOUBLE_TYPE long double
7400
 
#endif
7401
 
typedef sqlite_int64 i64;          /* 8-byte signed integer */
7402
 
typedef sqlite_uint64 u64;         /* 8-byte unsigned integer */
7403
 
typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
7404
 
typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
7405
 
typedef INT16_TYPE i16;            /* 2-byte signed integer */
7406
 
typedef UINT8_TYPE u8;             /* 1-byte unsigned integer */
7407
 
typedef INT8_TYPE i8;              /* 1-byte signed integer */
7408
 
 
7409
 
/*
7410
 
** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
7411
 
** that can be stored in a u32 without loss of data.  The value
7412
 
** is 0x00000000ffffffff.  But because of quirks of some compilers, we
7413
 
** have to specify the value in the less intuitive manner shown:
7414
 
*/
7415
 
#define SQLITE_MAX_U32  ((((u64)1)<<32)-1)
7416
 
 
7417
 
/*
7418
 
** Macros to determine whether the machine is big or little endian,
7419
 
** evaluated at runtime.
7420
 
*/
7421
 
#ifdef SQLITE_AMALGAMATION
7422
 
SQLITE_PRIVATE const int sqlite3one = 1;
7423
 
#else
7424
 
SQLITE_PRIVATE const int sqlite3one;
7425
 
#endif
7426
 
#if defined(i386) || defined(__i386__) || defined(_M_IX86)\
7427
 
                             || defined(__x86_64) || defined(__x86_64__)
7428
 
# define SQLITE_BIGENDIAN    0
7429
 
# define SQLITE_LITTLEENDIAN 1
7430
 
# define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
7431
 
#else
7432
 
# define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
7433
 
# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
7434
 
# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
7435
 
#endif
7436
 
 
7437
 
/*
7438
 
** Constants for the largest and smallest possible 64-bit signed integers.
7439
 
** These macros are designed to work correctly on both 32-bit and 64-bit
7440
 
** compilers.
7441
 
*/
7442
 
#define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
7443
 
#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
7444
 
 
7445
 
/* 
7446
 
** Round up a number to the next larger multiple of 8.  This is used
7447
 
** to force 8-byte alignment on 64-bit architectures.
7448
 
*/
7449
 
#define ROUND8(x)     (((x)+7)&~7)
7450
 
 
7451
 
/*
7452
 
** Round down to the nearest multiple of 8
7453
 
*/
7454
 
#define ROUNDDOWN8(x) ((x)&~7)
7455
 
 
7456
 
/*
7457
 
** Assert that the pointer X is aligned to an 8-byte boundary.  This
7458
 
** macro is used only within assert() to verify that the code gets
7459
 
** all alignment restrictions correct.
7460
 
**
7461
 
** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
7462
 
** underlying malloc() implemention might return us 4-byte aligned
7463
 
** pointers.  In that case, only verify 4-byte alignment.
7464
 
*/
7465
 
#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
7466
 
# define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&3)==0)
7467
 
#else
7468
 
# define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&7)==0)
7469
 
#endif
7470
 
 
7471
 
 
7472
 
/*
7473
 
** An instance of the following structure is used to store the busy-handler
7474
 
** callback for a given sqlite handle. 
7475
 
**
7476
 
** The sqlite.busyHandler member of the sqlite struct contains the busy
7477
 
** callback for the database handle. Each pager opened via the sqlite
7478
 
** handle is passed a pointer to sqlite.busyHandler. The busy-handler
7479
 
** callback is currently invoked only from within pager.c.
7480
 
*/
7481
 
typedef struct BusyHandler BusyHandler;
7482
 
struct BusyHandler {
7483
 
  int (*xFunc)(void *,int);  /* The busy callback */
7484
 
  void *pArg;                /* First arg to busy callback */
7485
 
  int nBusy;                 /* Incremented with each busy call */
7486
 
};
7487
 
 
7488
 
/*
7489
 
** Name of the master database table.  The master database table
7490
 
** is a special table that holds the names and attributes of all
7491
 
** user tables and indices.
7492
 
*/
7493
 
#define MASTER_NAME       "sqlite_master"
7494
 
#define TEMP_MASTER_NAME  "sqlite_temp_master"
7495
 
 
7496
 
/*
7497
 
** The root-page of the master database table.
7498
 
*/
7499
 
#define MASTER_ROOT       1
7500
 
 
7501
 
/*
7502
 
** The name of the schema table.
7503
 
*/
7504
 
#define SCHEMA_TABLE(x)  ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
7505
 
 
7506
 
/*
7507
 
** A convenience macro that returns the number of elements in
7508
 
** an array.
7509
 
*/
7510
 
#define ArraySize(X)    ((int)(sizeof(X)/sizeof(X[0])))
7511
 
 
7512
 
/*
7513
 
** The following value as a destructor means to use sqlite3DbFree().
7514
 
** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT.
7515
 
*/
7516
 
#define SQLITE_DYNAMIC   ((sqlite3_destructor_type)sqlite3DbFree)
7517
 
 
7518
 
/*
7519
 
** When SQLITE_OMIT_WSD is defined, it means that the target platform does
7520
 
** not support Writable Static Data (WSD) such as global and static variables.
7521
 
** All variables must either be on the stack or dynamically allocated from
7522
 
** the heap.  When WSD is unsupported, the variable declarations scattered
7523
 
** throughout the SQLite code must become constants instead.  The SQLITE_WSD
7524
 
** macro is used for this purpose.  And instead of referencing the variable
7525
 
** directly, we use its constant as a key to lookup the run-time allocated
7526
 
** buffer that holds real variable.  The constant is also the initializer
7527
 
** for the run-time allocated buffer.
7528
 
**
7529
 
** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
7530
 
** macros become no-ops and have zero performance impact.
7531
 
*/
7532
 
#ifdef SQLITE_OMIT_WSD
7533
 
  #define SQLITE_WSD const
7534
 
  #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
7535
 
  #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
7536
 
SQLITE_API   int sqlite3_wsd_init(int N, int J);
7537
 
SQLITE_API   void *sqlite3_wsd_find(void *K, int L);
7538
 
#else
7539
 
  #define SQLITE_WSD 
7540
 
  #define GLOBAL(t,v) v
7541
 
  #define sqlite3GlobalConfig sqlite3Config
7542
 
#endif
7543
 
 
7544
 
/*
7545
 
** The following macros are used to suppress compiler warnings and to
7546
 
** make it clear to human readers when a function parameter is deliberately 
7547
 
** left unused within the body of a function. This usually happens when
7548
 
** a function is called via a function pointer. For example the 
7549
 
** implementation of an SQL aggregate step callback may not use the
7550
 
** parameter indicating the number of arguments passed to the aggregate,
7551
 
** if it knows that this is enforced elsewhere.
7552
 
**
7553
 
** When a function parameter is not used at all within the body of a function,
7554
 
** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
7555
 
** However, these macros may also be used to suppress warnings related to
7556
 
** parameters that may or may not be used depending on compilation options.
7557
 
** For example those parameters only used in assert() statements. In these
7558
 
** cases the parameters are named as per the usual conventions.
7559
 
*/
7560
 
#define UNUSED_PARAMETER(x) (void)(x)
7561
 
#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
7562
 
 
7563
 
/*
7564
 
** Forward references to structures
7565
 
*/
7566
 
typedef struct AggInfo AggInfo;
7567
 
typedef struct AuthContext AuthContext;
7568
 
typedef struct AutoincInfo AutoincInfo;
7569
 
typedef struct Bitvec Bitvec;
7570
 
typedef struct CollSeq CollSeq;
7571
 
typedef struct Column Column;
7572
 
typedef struct Db Db;
7573
 
typedef struct Schema Schema;
7574
 
typedef struct Expr Expr;
7575
 
typedef struct ExprList ExprList;
7576
 
typedef struct ExprSpan ExprSpan;
7577
 
typedef struct FKey FKey;
7578
 
typedef struct FuncDestructor FuncDestructor;
7579
 
typedef struct FuncDef FuncDef;
7580
 
typedef struct FuncDefHash FuncDefHash;
7581
 
typedef struct IdList IdList;
7582
 
typedef struct Index Index;
7583
 
typedef struct IndexSample IndexSample;
7584
 
typedef struct KeyClass KeyClass;
7585
 
typedef struct KeyInfo KeyInfo;
7586
 
typedef struct Lookaside Lookaside;
7587
 
typedef struct LookasideSlot LookasideSlot;
7588
 
typedef struct Module Module;
7589
 
typedef struct NameContext NameContext;
7590
 
typedef struct Parse Parse;
7591
 
typedef struct RowSet RowSet;
7592
 
typedef struct Savepoint Savepoint;
7593
 
typedef struct Select Select;
7594
 
typedef struct SrcList SrcList;
7595
 
typedef struct StrAccum StrAccum;
7596
 
typedef struct Table Table;
7597
 
typedef struct TableLock TableLock;
7598
 
typedef struct Token Token;
7599
 
typedef struct Trigger Trigger;
7600
 
typedef struct TriggerPrg TriggerPrg;
7601
 
typedef struct TriggerStep TriggerStep;
7602
 
typedef struct UnpackedRecord UnpackedRecord;
7603
 
typedef struct VTable VTable;
7604
 
typedef struct Walker Walker;
7605
 
typedef struct WherePlan WherePlan;
7606
 
typedef struct WhereInfo WhereInfo;
7607
 
typedef struct WhereLevel WhereLevel;
7608
 
 
7609
 
/*
7610
 
** Defer sourcing vdbe.h and btree.h until after the "u8" and 
7611
 
** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
7612
 
** pointer types (i.e. FuncDef) defined above.
7613
 
*/
7614
 
/************** Include btree.h in the middle of sqliteInt.h *****************/
7615
 
/************** Begin file btree.h *******************************************/
7616
 
/*
7617
 
** 2001 September 15
7618
 
**
7619
 
** The author disclaims copyright to this source code.  In place of
7620
 
** a legal notice, here is a blessing:
7621
 
**
7622
 
**    May you do good and not evil.
7623
 
**    May you find forgiveness for yourself and forgive others.
7624
 
**    May you share freely, never taking more than you give.
7625
 
**
7626
 
*************************************************************************
7627
 
** This header file defines the interface that the sqlite B-Tree file
7628
 
** subsystem.  See comments in the source code for a detailed description
7629
 
** of what each interface routine does.
7630
 
*/
7631
 
#ifndef _BTREE_H_
7632
 
#define _BTREE_H_
7633
 
 
7634
 
/* TODO: This definition is just included so other modules compile. It
7635
 
** needs to be revisited.
7636
 
*/
7637
 
#define SQLITE_N_BTREE_META 10
7638
 
 
7639
 
/*
7640
 
** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
7641
 
** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
7642
 
*/
7643
 
#ifndef SQLITE_DEFAULT_AUTOVACUUM
7644
 
  #define SQLITE_DEFAULT_AUTOVACUUM 0
7645
 
#endif
7646
 
 
7647
 
#define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
7648
 
#define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
7649
 
#define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
7650
 
 
7651
 
/*
7652
 
** Forward declarations of structure
7653
 
*/
7654
 
typedef struct Btree Btree;
7655
 
typedef struct BtCursor BtCursor;
7656
 
typedef struct BtShared BtShared;
7657
 
 
7658
 
 
7659
 
SQLITE_PRIVATE int sqlite3BtreeOpen(
7660
 
  const char *zFilename,   /* Name of database file to open */
7661
 
  sqlite3 *db,             /* Associated database connection */
7662
 
  Btree **ppBtree,         /* Return open Btree* here */
7663
 
  int flags,               /* Flags */
7664
 
  int vfsFlags             /* Flags passed through to VFS open */
7665
 
);
7666
 
 
7667
 
/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
7668
 
** following values.
7669
 
**
7670
 
** NOTE:  These values must match the corresponding PAGER_ values in
7671
 
** pager.h.
7672
 
*/
7673
 
#define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
7674
 
#define BTREE_NO_READLOCK   2  /* Omit readlocks on readonly files */
7675
 
#define BTREE_MEMORY        4  /* This is an in-memory DB */
7676
 
#define BTREE_SINGLE        8  /* The file contains at most 1 b-tree */
7677
 
#define BTREE_UNORDERED    16  /* Use of a hash implementation is OK */
7678
 
 
7679
 
SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
7680
 
SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
7681
 
SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
7682
 
SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
7683
 
SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
7684
 
SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
7685
 
SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
7686
 
SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
7687
 
SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
7688
 
SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
7689
 
SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
7690
 
SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
7691
 
SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
7692
 
SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
7693
 
SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
7694
 
SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
7695
 
SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*);
7696
 
SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
7697
 
SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
7698
 
SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
7699
 
SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
7700
 
SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
7701
 
SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
7702
 
SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
7703
 
SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
7704
 
SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
7705
 
 
7706
 
SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
7707
 
SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
7708
 
SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
7709
 
 
7710
 
SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
7711
 
 
7712
 
/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
7713
 
** of the flags shown below.
7714
 
**
7715
 
** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
7716
 
** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
7717
 
** is stored in the leaves.  (BTREE_INTKEY is used for SQL tables.)  With
7718
 
** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
7719
 
** anywhere - the key is the content.  (BTREE_BLOBKEY is used for SQL
7720
 
** indices.)
7721
 
*/
7722
 
#define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
7723
 
#define BTREE_BLOBKEY    2    /* Table has keys only - no data */
7724
 
 
7725
 
SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
7726
 
SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
7727
 
SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
7728
 
 
7729
 
SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
7730
 
SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
7731
 
 
7732
 
/*
7733
 
** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
7734
 
** should be one of the following values. The integer values are assigned 
7735
 
** to constants so that the offset of the corresponding field in an
7736
 
** SQLite database header may be found using the following formula:
7737
 
**
7738
 
**   offset = 36 + (idx * 4)
7739
 
**
7740
 
** For example, the free-page-count field is located at byte offset 36 of
7741
 
** the database file header. The incr-vacuum-flag field is located at
7742
 
** byte offset 64 (== 36+4*7).
7743
 
*/
7744
 
#define BTREE_FREE_PAGE_COUNT     0
7745
 
#define BTREE_SCHEMA_VERSION      1
7746
 
#define BTREE_FILE_FORMAT         2
7747
 
#define BTREE_DEFAULT_CACHE_SIZE  3
7748
 
#define BTREE_LARGEST_ROOT_PAGE   4
7749
 
#define BTREE_TEXT_ENCODING       5
7750
 
#define BTREE_USER_VERSION        6
7751
 
#define BTREE_INCR_VACUUM         7
7752
 
 
7753
 
SQLITE_PRIVATE int sqlite3BtreeCursor(
7754
 
  Btree*,                              /* BTree containing table to open */
7755
 
  int iTable,                          /* Index of root page */
7756
 
  int wrFlag,                          /* 1 for writing.  0 for read-only */
7757
 
  struct KeyInfo*,                     /* First argument to compare function */
7758
 
  BtCursor *pCursor                    /* Space to write cursor structure */
7759
 
);
7760
 
SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
7761
 
SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
7762
 
 
7763
 
SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
7764
 
SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
7765
 
  BtCursor*,
7766
 
  UnpackedRecord *pUnKey,
7767
 
  i64 intKey,
7768
 
  int bias,
7769
 
  int *pRes
7770
 
);
7771
 
SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
7772
 
SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
7773
 
SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
7774
 
                                  const void *pData, int nData,
7775
 
                                  int nZero, int bias, int seekResult);
7776
 
SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
7777
 
SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
7778
 
SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
7779
 
SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
7780
 
SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
7781
 
SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
7782
 
SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
7783
 
SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
7784
 
SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
7785
 
SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
7786
 
SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
7787
 
SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
7788
 
SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
7789
 
 
7790
 
SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
7791
 
SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
7792
 
 
7793
 
SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
7794
 
SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
7795
 
SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
7796
 
 
7797
 
SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
7798
 
 
7799
 
#ifndef NDEBUG
7800
 
SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
7801
 
#endif
7802
 
 
7803
 
#ifndef SQLITE_OMIT_BTREECOUNT
7804
 
SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
7805
 
#endif
7806
 
 
7807
 
#ifdef SQLITE_TEST
7808
 
SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
7809
 
SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
7810
 
#endif
7811
 
 
7812
 
#ifndef SQLITE_OMIT_WAL
7813
 
SQLITE_PRIVATE   int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
7814
 
#endif
7815
 
 
7816
 
/*
7817
 
** If we are not using shared cache, then there is no need to
7818
 
** use mutexes to access the BtShared structures.  So make the
7819
 
** Enter and Leave procedures no-ops.
7820
 
*/
7821
 
#ifndef SQLITE_OMIT_SHARED_CACHE
7822
 
SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
7823
 
SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
7824
 
#else
7825
 
# define sqlite3BtreeEnter(X) 
7826
 
# define sqlite3BtreeEnterAll(X)
7827
 
#endif
7828
 
 
7829
 
#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
7830
 
SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
7831
 
SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
7832
 
SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
7833
 
SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
7834
 
SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
7835
 
#ifndef NDEBUG
7836
 
  /* These routines are used inside assert() statements only. */
7837
 
SQLITE_PRIVATE   int sqlite3BtreeHoldsMutex(Btree*);
7838
 
SQLITE_PRIVATE   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
7839
 
SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
7840
 
#endif
7841
 
#else
7842
 
 
7843
 
# define sqlite3BtreeSharable(X) 0
7844
 
# define sqlite3BtreeLeave(X)
7845
 
# define sqlite3BtreeEnterCursor(X)
7846
 
# define sqlite3BtreeLeaveCursor(X)
7847
 
# define sqlite3BtreeLeaveAll(X)
7848
 
 
7849
 
# define sqlite3BtreeHoldsMutex(X) 1
7850
 
# define sqlite3BtreeHoldsAllMutexes(X) 1
7851
 
# define sqlite3SchemaMutexHeld(X,Y,Z) 1
7852
 
#endif
7853
 
 
7854
 
 
7855
 
#endif /* _BTREE_H_ */
7856
 
 
7857
 
/************** End of btree.h ***********************************************/
7858
 
/************** Continuing where we left off in sqliteInt.h ******************/
7859
 
/************** Include vdbe.h in the middle of sqliteInt.h ******************/
7860
 
/************** Begin file vdbe.h ********************************************/
7861
 
/*
7862
 
** 2001 September 15
7863
 
**
7864
 
** The author disclaims copyright to this source code.  In place of
7865
 
** a legal notice, here is a blessing:
7866
 
**
7867
 
**    May you do good and not evil.
7868
 
**    May you find forgiveness for yourself and forgive others.
7869
 
**    May you share freely, never taking more than you give.
7870
 
**
7871
 
*************************************************************************
7872
 
** Header file for the Virtual DataBase Engine (VDBE)
7873
 
**
7874
 
** This header defines the interface to the virtual database engine
7875
 
** or VDBE.  The VDBE implements an abstract machine that runs a
7876
 
** simple program to access and modify the underlying database.
7877
 
*/
7878
 
#ifndef _SQLITE_VDBE_H_
7879
 
#define _SQLITE_VDBE_H_
7880
 
 
7881
 
/*
7882
 
** A single VDBE is an opaque structure named "Vdbe".  Only routines
7883
 
** in the source file sqliteVdbe.c are allowed to see the insides
7884
 
** of this structure.
7885
 
*/
7886
 
typedef struct Vdbe Vdbe;
7887
 
 
7888
 
/*
7889
 
** The names of the following types declared in vdbeInt.h are required
7890
 
** for the VdbeOp definition.
7891
 
*/
7892
 
typedef struct VdbeFunc VdbeFunc;
7893
 
typedef struct Mem Mem;
7894
 
typedef struct SubProgram SubProgram;
7895
 
 
7896
 
/*
7897
 
** A single instruction of the virtual machine has an opcode
7898
 
** and as many as three operands.  The instruction is recorded
7899
 
** as an instance of the following structure:
7900
 
*/
7901
 
struct VdbeOp {
7902
 
  u8 opcode;          /* What operation to perform */
7903
 
  signed char p4type; /* One of the P4_xxx constants for p4 */
7904
 
  u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
7905
 
  u8 p5;              /* Fifth parameter is an unsigned character */
7906
 
  int p1;             /* First operand */
7907
 
  int p2;             /* Second parameter (often the jump destination) */
7908
 
  int p3;             /* The third parameter */
7909
 
  union {             /* fourth parameter */
7910
 
    int i;                 /* Integer value if p4type==P4_INT32 */
7911
 
    void *p;               /* Generic pointer */
7912
 
    char *z;               /* Pointer to data for string (char array) types */
7913
 
    i64 *pI64;             /* Used when p4type is P4_INT64 */
7914
 
    double *pReal;         /* Used when p4type is P4_REAL */
7915
 
    FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
7916
 
    VdbeFunc *pVdbeFunc;   /* Used when p4type is P4_VDBEFUNC */
7917
 
    CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
7918
 
    Mem *pMem;             /* Used when p4type is P4_MEM */
7919
 
    VTable *pVtab;         /* Used when p4type is P4_VTAB */
7920
 
    KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
7921
 
    int *ai;               /* Used when p4type is P4_INTARRAY */
7922
 
    SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
7923
 
  } p4;
7924
 
#ifdef SQLITE_DEBUG
7925
 
  char *zComment;          /* Comment to improve readability */
7926
 
#endif
7927
 
#ifdef VDBE_PROFILE
7928
 
  int cnt;                 /* Number of times this instruction was executed */
7929
 
  u64 cycles;              /* Total time spent executing this instruction */
7930
 
#endif
7931
 
};
7932
 
typedef struct VdbeOp VdbeOp;
7933
 
 
7934
 
 
7935
 
/*
7936
 
** A sub-routine used to implement a trigger program.
7937
 
*/
7938
 
struct SubProgram {
7939
 
  VdbeOp *aOp;                  /* Array of opcodes for sub-program */
7940
 
  int nOp;                      /* Elements in aOp[] */
7941
 
  int nMem;                     /* Number of memory cells required */
7942
 
  int nCsr;                     /* Number of cursors required */
7943
 
  void *token;                  /* id that may be used to recursive triggers */
7944
 
  SubProgram *pNext;            /* Next sub-program already visited */
7945
 
};
7946
 
 
7947
 
/*
7948
 
** A smaller version of VdbeOp used for the VdbeAddOpList() function because
7949
 
** it takes up less space.
7950
 
*/
7951
 
struct VdbeOpList {
7952
 
  u8 opcode;          /* What operation to perform */
7953
 
  signed char p1;     /* First operand */
7954
 
  signed char p2;     /* Second parameter (often the jump destination) */
7955
 
  signed char p3;     /* Third parameter */
7956
 
};
7957
 
typedef struct VdbeOpList VdbeOpList;
7958
 
 
7959
 
/*
7960
 
** Allowed values of VdbeOp.p4type
7961
 
*/
7962
 
#define P4_NOTUSED    0   /* The P4 parameter is not used */
7963
 
#define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
7964
 
#define P4_STATIC   (-2)  /* Pointer to a static string */
7965
 
#define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
7966
 
#define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
7967
 
#define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
7968
 
#define P4_VDBEFUNC (-7)  /* P4 is a pointer to a VdbeFunc structure */
7969
 
#define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
7970
 
#define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
7971
 
#define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
7972
 
#define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
7973
 
#define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
7974
 
#define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
7975
 
#define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
7976
 
#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
7977
 
#define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
7978
 
 
7979
 
/* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
7980
 
** is made.  That copy is freed when the Vdbe is finalized.  But if the
7981
 
** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used.  It still
7982
 
** gets freed when the Vdbe is finalized so it still should be obtained
7983
 
** from a single sqliteMalloc().  But no copy is made and the calling
7984
 
** function should *not* try to free the KeyInfo.
7985
 
*/
7986
 
#define P4_KEYINFO_HANDOFF (-16)
7987
 
#define P4_KEYINFO_STATIC  (-17)
7988
 
 
7989
 
/*
7990
 
** The Vdbe.aColName array contains 5n Mem structures, where n is the 
7991
 
** number of columns of data returned by the statement.
7992
 
*/
7993
 
#define COLNAME_NAME     0
7994
 
#define COLNAME_DECLTYPE 1
7995
 
#define COLNAME_DATABASE 2
7996
 
#define COLNAME_TABLE    3
7997
 
#define COLNAME_COLUMN   4
7998
 
#ifdef SQLITE_ENABLE_COLUMN_METADATA
7999
 
# define COLNAME_N        5      /* Number of COLNAME_xxx symbols */
8000
 
#else
8001
 
# ifdef SQLITE_OMIT_DECLTYPE
8002
 
#   define COLNAME_N      1      /* Store only the name */
8003
 
# else
8004
 
#   define COLNAME_N      2      /* Store the name and decltype */
8005
 
# endif
8006
 
#endif
8007
 
 
8008
 
/*
8009
 
** The following macro converts a relative address in the p2 field
8010
 
** of a VdbeOp structure into a negative number so that 
8011
 
** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
8012
 
** the macro again restores the address.
8013
 
*/
8014
 
#define ADDR(X)  (-1-(X))
8015
 
 
8016
 
/*
8017
 
** The makefile scans the vdbe.c source file and creates the "opcodes.h"
8018
 
** header file that defines a number for each opcode used by the VDBE.
8019
 
*/
8020
 
/************** Include opcodes.h in the middle of vdbe.h ********************/
8021
 
/************** Begin file opcodes.h *****************************************/
8022
 
/* Automatically generated.  Do not edit */
8023
 
/* See the mkopcodeh.awk script for details */
8024
 
#define OP_Goto                                 1
8025
 
#define OP_Gosub                                2
8026
 
#define OP_Return                               3
8027
 
#define OP_Yield                                4
8028
 
#define OP_HaltIfNull                           5
8029
 
#define OP_Halt                                 6
8030
 
#define OP_Integer                              7
8031
 
#define OP_Int64                                8
8032
 
#define OP_Real                               130   /* same as TK_FLOAT    */
8033
 
#define OP_String8                             94   /* same as TK_STRING   */
8034
 
#define OP_String                               9
8035
 
#define OP_Null                                10
8036
 
#define OP_Blob                                11
8037
 
#define OP_Variable                            12
8038
 
#define OP_Move                                13
8039
 
#define OP_Copy                                14
8040
 
#define OP_SCopy                               15
8041
 
#define OP_ResultRow                           16
8042
 
#define OP_Concat                              91   /* same as TK_CONCAT   */
8043
 
#define OP_Add                                 86   /* same as TK_PLUS     */
8044
 
#define OP_Subtract                            87   /* same as TK_MINUS    */
8045
 
#define OP_Multiply                            88   /* same as TK_STAR     */
8046
 
#define OP_Divide                              89   /* same as TK_SLASH    */
8047
 
#define OP_Remainder                           90   /* same as TK_REM      */
8048
 
#define OP_CollSeq                             17
8049
 
#define OP_Function                            18
8050
 
#define OP_BitAnd                              82   /* same as TK_BITAND   */
8051
 
#define OP_BitOr                               83   /* same as TK_BITOR    */
8052
 
#define OP_ShiftLeft                           84   /* same as TK_LSHIFT   */
8053
 
#define OP_ShiftRight                          85   /* same as TK_RSHIFT   */
8054
 
#define OP_AddImm                              20
8055
 
#define OP_MustBeInt                           21
8056
 
#define OP_RealAffinity                        22
8057
 
#define OP_ToText                             141   /* same as TK_TO_TEXT  */
8058
 
#define OP_ToBlob                             142   /* same as TK_TO_BLOB  */
8059
 
#define OP_ToNumeric                          143   /* same as TK_TO_NUMERIC*/
8060
 
#define OP_ToInt                              144   /* same as TK_TO_INT   */
8061
 
#define OP_ToReal                             145   /* same as TK_TO_REAL  */
8062
 
#define OP_Eq                                  76   /* same as TK_EQ       */
8063
 
#define OP_Ne                                  75   /* same as TK_NE       */
8064
 
#define OP_Lt                                  79   /* same as TK_LT       */
8065
 
#define OP_Le                                  78   /* same as TK_LE       */
8066
 
#define OP_Gt                                  77   /* same as TK_GT       */
8067
 
#define OP_Ge                                  80   /* same as TK_GE       */
8068
 
#define OP_Permutation                         23
8069
 
#define OP_Compare                             24
8070
 
#define OP_Jump                                25
8071
 
#define OP_And                                 69   /* same as TK_AND      */
8072
 
#define OP_Or                                  68   /* same as TK_OR       */
8073
 
#define OP_Not                                 19   /* same as TK_NOT      */
8074
 
#define OP_BitNot                              93   /* same as TK_BITNOT   */
8075
 
#define OP_If                                  26
8076
 
#define OP_IfNot                               27
8077
 
#define OP_IsNull                              73   /* same as TK_ISNULL   */
8078
 
#define OP_NotNull                             74   /* same as TK_NOTNULL  */
8079
 
#define OP_Column                              28
8080
 
#define OP_Affinity                            29
8081
 
#define OP_MakeRecord                          30
8082
 
#define OP_Count                               31
8083
 
#define OP_Savepoint                           32
8084
 
#define OP_AutoCommit                          33
8085
 
#define OP_Transaction                         34
8086
 
#define OP_ReadCookie                          35
8087
 
#define OP_SetCookie                           36
8088
 
#define OP_VerifyCookie                        37
8089
 
#define OP_OpenRead                            38
8090
 
#define OP_OpenWrite                           39
8091
 
#define OP_OpenAutoindex                       40
8092
 
#define OP_OpenEphemeral                       41
8093
 
#define OP_OpenPseudo                          42
8094
 
#define OP_Close                               43
8095
 
#define OP_SeekLt                              44
8096
 
#define OP_SeekLe                              45
8097
 
#define OP_SeekGe                              46
8098
 
#define OP_SeekGt                              47
8099
 
#define OP_Seek                                48
8100
 
#define OP_NotFound                            49
8101
 
#define OP_Found                               50
8102
 
#define OP_IsUnique                            51
8103
 
#define OP_NotExists                           52
8104
 
#define OP_Sequence                            53
8105
 
#define OP_NewRowid                            54
8106
 
#define OP_Insert                              55
8107
 
#define OP_InsertInt                           56
8108
 
#define OP_Delete                              57
8109
 
#define OP_ResetCount                          58
8110
 
#define OP_RowKey                              59
8111
 
#define OP_RowData                             60
8112
 
#define OP_Rowid                               61
8113
 
#define OP_NullRow                             62
8114
 
#define OP_Last                                63
8115
 
#define OP_Sort                                64
8116
 
#define OP_Rewind                              65
8117
 
#define OP_Prev                                66
8118
 
#define OP_Next                                67
8119
 
#define OP_IdxInsert                           70
8120
 
#define OP_IdxDelete                           71
8121
 
#define OP_IdxRowid                            72
8122
 
#define OP_IdxLT                               81
8123
 
#define OP_IdxGE                               92
8124
 
#define OP_Destroy                             95
8125
 
#define OP_Clear                               96
8126
 
#define OP_CreateIndex                         97
8127
 
#define OP_CreateTable                         98
8128
 
#define OP_ParseSchema                         99
8129
 
#define OP_LoadAnalysis                       100
8130
 
#define OP_DropTable                          101
8131
 
#define OP_DropIndex                          102
8132
 
#define OP_DropTrigger                        103
8133
 
#define OP_IntegrityCk                        104
8134
 
#define OP_RowSetAdd                          105
8135
 
#define OP_RowSetRead                         106
8136
 
#define OP_RowSetTest                         107
8137
 
#define OP_Program                            108
8138
 
#define OP_Param                              109
8139
 
#define OP_FkCounter                          110
8140
 
#define OP_FkIfZero                           111
8141
 
#define OP_MemMax                             112
8142
 
#define OP_IfPos                              113
8143
 
#define OP_IfNeg                              114
8144
 
#define OP_IfZero                             115
8145
 
#define OP_AggStep                            116
8146
 
#define OP_AggFinal                           117
8147
 
#define OP_Checkpoint                         118
8148
 
#define OP_JournalMode                        119
8149
 
#define OP_Vacuum                             120
8150
 
#define OP_IncrVacuum                         121
8151
 
#define OP_Expire                             122
8152
 
#define OP_TableLock                          123
8153
 
#define OP_VBegin                             124
8154
 
#define OP_VCreate                            125
8155
 
#define OP_VDestroy                           126
8156
 
#define OP_VOpen                              127
8157
 
#define OP_VFilter                            128
8158
 
#define OP_VColumn                            129
8159
 
#define OP_VNext                              131
8160
 
#define OP_VRename                            132
8161
 
#define OP_VUpdate                            133
8162
 
#define OP_Pagecount                          134
8163
 
#define OP_MaxPgcnt                           135
8164
 
#define OP_Trace                              136
8165
 
#define OP_Noop                               137
8166
 
#define OP_Explain                            138
8167
 
 
8168
 
/* The following opcode values are never used */
8169
 
#define OP_NotUsed_139                        139
8170
 
#define OP_NotUsed_140                        140
8171
 
 
8172
 
 
8173
 
/* Properties such as "out2" or "jump" that are specified in
8174
 
** comments following the "case" for each opcode in the vdbe.c
8175
 
** are encoded into bitvectors as follows:
8176
 
*/
8177
 
#define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
8178
 
#define OPFLG_OUT2_PRERELEASE 0x0002  /* out2-prerelease: */
8179
 
#define OPFLG_IN1             0x0004  /* in1:   P1 is an input */
8180
 
#define OPFLG_IN2             0x0008  /* in2:   P2 is an input */
8181
 
#define OPFLG_IN3             0x0010  /* in3:   P3 is an input */
8182
 
#define OPFLG_OUT2            0x0020  /* out2:  P2 is an output */
8183
 
#define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */
8184
 
#define OPFLG_INITIALIZER {\
8185
 
/*   0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
8186
 
/*   8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
8187
 
/*  16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
8188
 
/*  24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
8189
 
/*  32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\
8190
 
/*  40 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\
8191
 
/*  48 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00,\
8192
 
/*  56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\
8193
 
/*  64 */ 0x01, 0x01, 0x01, 0x01, 0x4c, 0x4c, 0x08, 0x00,\
8194
 
/*  72 */ 0x02, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
8195
 
/*  80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
8196
 
/*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x02,\
8197
 
/*  96 */ 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
8198
 
/* 104 */ 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01,\
8199
 
/* 112 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
8200
 
/* 120 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
8201
 
/* 128 */ 0x01, 0x00, 0x02, 0x01, 0x00, 0x00, 0x02, 0x02,\
8202
 
/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\
8203
 
/* 144 */ 0x04, 0x04,}
8204
 
 
8205
 
/************** End of opcodes.h *********************************************/
8206
 
/************** Continuing where we left off in vdbe.h ***********************/
8207
 
 
8208
 
/*
8209
 
** Prototypes for the VDBE interface.  See comments on the implementation
8210
 
** for a description of what each of these routines does.
8211
 
*/
8212
 
SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3*);
8213
 
SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
8214
 
SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
8215
 
SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
8216
 
SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
8217
 
SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
8218
 
SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
8219
 
SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
8220
 
SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
8221
 
SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
8222
 
SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
8223
 
SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
8224
 
SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
8225
 
SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
8226
 
SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
8227
 
SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
8228
 
SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
8229
 
SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
8230
 
SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
8231
 
SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
8232
 
SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3*,Vdbe*);
8233
 
SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,int,int,int,int,int,int);
8234
 
SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
8235
 
SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
8236
 
SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
8237
 
#ifdef SQLITE_DEBUG
8238
 
SQLITE_PRIVATE   int sqlite3VdbeAssertMayAbort(Vdbe *, int);
8239
 
SQLITE_PRIVATE   void sqlite3VdbeTrace(Vdbe*,FILE*);
8240
 
#endif
8241
 
SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
8242
 
SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
8243
 
SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
8244
 
SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
8245
 
SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
8246
 
SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
8247
 
SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
8248
 
SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
8249
 
SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
8250
 
SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8);
8251
 
SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
8252
 
#ifndef SQLITE_OMIT_TRACE
8253
 
SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
8254
 
#endif
8255
 
 
8256
 
SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,char*,int);
8257
 
SQLITE_PRIVATE void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord*);
8258
 
SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
8259
 
 
8260
 
#ifndef SQLITE_OMIT_TRIGGER
8261
 
SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
8262
 
#endif
8263
 
 
8264
 
 
8265
 
#ifndef NDEBUG
8266
 
SQLITE_PRIVATE   void sqlite3VdbeComment(Vdbe*, const char*, ...);
8267
 
# define VdbeComment(X)  sqlite3VdbeComment X
8268
 
SQLITE_PRIVATE   void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
8269
 
# define VdbeNoopComment(X)  sqlite3VdbeNoopComment X
8270
 
#else
8271
 
# define VdbeComment(X)
8272
 
# define VdbeNoopComment(X)
8273
 
#endif
8274
 
 
8275
 
#endif
8276
 
 
8277
 
/************** End of vdbe.h ************************************************/
8278
 
/************** Continuing where we left off in sqliteInt.h ******************/
8279
 
/************** Include pager.h in the middle of sqliteInt.h *****************/
8280
 
/************** Begin file pager.h *******************************************/
8281
 
/*
8282
 
** 2001 September 15
8283
 
**
8284
 
** The author disclaims copyright to this source code.  In place of
8285
 
** a legal notice, here is a blessing:
8286
 
**
8287
 
**    May you do good and not evil.
8288
 
**    May you find forgiveness for yourself and forgive others.
8289
 
**    May you share freely, never taking more than you give.
8290
 
**
8291
 
*************************************************************************
8292
 
** This header file defines the interface that the sqlite page cache
8293
 
** subsystem.  The page cache subsystem reads and writes a file a page
8294
 
** at a time and provides a journal for rollback.
8295
 
*/
8296
 
 
8297
 
#ifndef _PAGER_H_
8298
 
#define _PAGER_H_
8299
 
 
8300
 
/*
8301
 
** Default maximum size for persistent journal files. A negative 
8302
 
** value means no limit. This value may be overridden using the 
8303
 
** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
8304
 
*/
8305
 
#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
8306
 
  #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
8307
 
#endif
8308
 
 
8309
 
/*
8310
 
** The type used to represent a page number.  The first page in a file
8311
 
** is called page 1.  0 is used to represent "not a page".
8312
 
*/
8313
 
typedef u32 Pgno;
8314
 
 
8315
 
/*
8316
 
** Each open file is managed by a separate instance of the "Pager" structure.
8317
 
*/
8318
 
typedef struct Pager Pager;
8319
 
 
8320
 
/*
8321
 
** Handle type for pages.
8322
 
*/
8323
 
typedef struct PgHdr DbPage;
8324
 
 
8325
 
/*
8326
 
** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
8327
 
** reserved for working around a windows/posix incompatibility). It is
8328
 
** used in the journal to signify that the remainder of the journal file 
8329
 
** is devoted to storing a master journal name - there are no more pages to
8330
 
** roll back. See comments for function writeMasterJournal() in pager.c 
8331
 
** for details.
8332
 
*/
8333
 
#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
8334
 
 
8335
 
/*
8336
 
** Allowed values for the flags parameter to sqlite3PagerOpen().
8337
 
**
8338
 
** NOTE: These values must match the corresponding BTREE_ values in btree.h.
8339
 
*/
8340
 
#define PAGER_OMIT_JOURNAL  0x0001    /* Do not use a rollback journal */
8341
 
#define PAGER_NO_READLOCK   0x0002    /* Omit readlocks on readonly files */
8342
 
#define PAGER_MEMORY        0x0004    /* In-memory database */
8343
 
 
8344
 
/*
8345
 
** Valid values for the second argument to sqlite3PagerLockingMode().
8346
 
*/
8347
 
#define PAGER_LOCKINGMODE_QUERY      -1
8348
 
#define PAGER_LOCKINGMODE_NORMAL      0
8349
 
#define PAGER_LOCKINGMODE_EXCLUSIVE   1
8350
 
 
8351
 
/*
8352
 
** Numeric constants that encode the journalmode.  
8353
 
*/
8354
 
#define PAGER_JOURNALMODE_QUERY     (-1)  /* Query the value of journalmode */
8355
 
#define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
8356
 
#define PAGER_JOURNALMODE_PERSIST     1   /* Commit by zeroing journal header */
8357
 
#define PAGER_JOURNALMODE_OFF         2   /* Journal omitted.  */
8358
 
#define PAGER_JOURNALMODE_TRUNCATE    3   /* Commit by truncating journal */
8359
 
#define PAGER_JOURNALMODE_MEMORY      4   /* In-memory journal file */
8360
 
#define PAGER_JOURNALMODE_WAL         5   /* Use write-ahead logging */
8361
 
 
8362
 
/*
8363
 
** The remainder of this file contains the declarations of the functions
8364
 
** that make up the Pager sub-system API. See source code comments for 
8365
 
** a detailed description of each routine.
8366
 
*/
8367
 
 
8368
 
/* Open and close a Pager connection. */ 
8369
 
SQLITE_PRIVATE int sqlite3PagerOpen(
8370
 
  sqlite3_vfs*,
8371
 
  Pager **ppPager,
8372
 
  const char*,
8373
 
  int,
8374
 
  int,
8375
 
  int,
8376
 
  void(*)(DbPage*)
8377
 
);
8378
 
SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
8379
 
SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
8380
 
 
8381
 
/* Functions used to configure a Pager object. */
8382
 
SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
8383
 
SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
8384
 
SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
8385
 
SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
8386
 
SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
8387
 
SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
8388
 
SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
8389
 
SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
8390
 
SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
8391
 
SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
8392
 
SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
8393
 
 
8394
 
/* Functions used to obtain and release page references. */ 
8395
 
SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
8396
 
#define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
8397
 
SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
8398
 
SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
8399
 
SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
8400
 
 
8401
 
/* Operations on page references. */
8402
 
SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
8403
 
SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
8404
 
SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
8405
 
SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
8406
 
SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *); 
8407
 
SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *); 
8408
 
 
8409
 
/* Functions used to manage pager transactions and savepoints. */
8410
 
SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
8411
 
SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
8412
 
SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
8413
 
SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
8414
 
SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
8415
 
SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
8416
 
SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
8417
 
SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
8418
 
SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
8419
 
SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
8420
 
 
8421
 
SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
8422
 
SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
8423
 
SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
8424
 
SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
8425
 
SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager);
8426
 
 
8427
 
/* Functions used to query pager state and configuration. */
8428
 
SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
8429
 
SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
8430
 
SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
8431
 
SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*);
8432
 
SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
8433
 
SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
8434
 
SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
8435
 
SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
8436
 
SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
8437
 
SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
8438
 
 
8439
 
/* Functions used to truncate the database file. */
8440
 
SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
8441
 
 
8442
 
#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
8443
 
SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
8444
 
#endif
8445
 
 
8446
 
/* Functions to support testing and debugging. */
8447
 
#if !defined(NDEBUG) || defined(SQLITE_TEST)
8448
 
SQLITE_PRIVATE   Pgno sqlite3PagerPagenumber(DbPage*);
8449
 
SQLITE_PRIVATE   int sqlite3PagerIswriteable(DbPage*);
8450
 
#endif
8451
 
#ifdef SQLITE_TEST
8452
 
SQLITE_PRIVATE   int *sqlite3PagerStats(Pager*);
8453
 
SQLITE_PRIVATE   void sqlite3PagerRefdump(Pager*);
8454
 
  void disable_simulated_io_errors(void);
8455
 
  void enable_simulated_io_errors(void);
8456
 
#else
8457
 
# define disable_simulated_io_errors()
8458
 
# define enable_simulated_io_errors()
8459
 
#endif
8460
 
 
8461
 
#endif /* _PAGER_H_ */
8462
 
 
8463
 
/************** End of pager.h ***********************************************/
8464
 
/************** Continuing where we left off in sqliteInt.h ******************/
8465
 
/************** Include pcache.h in the middle of sqliteInt.h ****************/
8466
 
/************** Begin file pcache.h ******************************************/
8467
 
/*
8468
 
** 2008 August 05
8469
 
**
8470
 
** The author disclaims copyright to this source code.  In place of
8471
 
** a legal notice, here is a blessing:
8472
 
**
8473
 
**    May you do good and not evil.
8474
 
**    May you find forgiveness for yourself and forgive others.
8475
 
**    May you share freely, never taking more than you give.
8476
 
**
8477
 
*************************************************************************
8478
 
** This header file defines the interface that the sqlite page cache
8479
 
** subsystem. 
8480
 
*/
8481
 
 
8482
 
#ifndef _PCACHE_H_
8483
 
 
8484
 
typedef struct PgHdr PgHdr;
8485
 
typedef struct PCache PCache;
8486
 
 
8487
 
/*
8488
 
** Every page in the cache is controlled by an instance of the following
8489
 
** structure.
8490
 
*/
8491
 
struct PgHdr {
8492
 
  void *pData;                   /* Content of this page */
8493
 
  void *pExtra;                  /* Extra content */
8494
 
  PgHdr *pDirty;                 /* Transient list of dirty pages */
8495
 
  Pgno pgno;                     /* Page number for this page */
8496
 
  Pager *pPager;                 /* The pager this page is part of */
8497
 
#ifdef SQLITE_CHECK_PAGES
8498
 
  u32 pageHash;                  /* Hash of page content */
8499
 
#endif
8500
 
  u16 flags;                     /* PGHDR flags defined below */
8501
 
 
8502
 
  /**********************************************************************
8503
 
  ** Elements above are public.  All that follows is private to pcache.c
8504
 
  ** and should not be accessed by other modules.
8505
 
  */
8506
 
  i16 nRef;                      /* Number of users of this page */
8507
 
  PCache *pCache;                /* Cache that owns this page */
8508
 
 
8509
 
  PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
8510
 
  PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
8511
 
};
8512
 
 
8513
 
/* Bit values for PgHdr.flags */
8514
 
#define PGHDR_DIRTY             0x002  /* Page has changed */
8515
 
#define PGHDR_NEED_SYNC         0x004  /* Fsync the rollback journal before
8516
 
                                       ** writing this page to the database */
8517
 
#define PGHDR_NEED_READ         0x008  /* Content is unread */
8518
 
#define PGHDR_REUSE_UNLIKELY    0x010  /* A hint that reuse is unlikely */
8519
 
#define PGHDR_DONT_WRITE        0x020  /* Do not write content to disk */
8520
 
 
8521
 
/* Initialize and shutdown the page cache subsystem */
8522
 
SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
8523
 
SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
8524
 
 
8525
 
/* Page cache buffer management:
8526
 
** These routines implement SQLITE_CONFIG_PAGECACHE.
8527
 
*/
8528
 
SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
8529
 
 
8530
 
/* Create a new pager cache.
8531
 
** Under memory stress, invoke xStress to try to make pages clean.
8532
 
** Only clean and unpinned pages can be reclaimed.
8533
 
*/
8534
 
SQLITE_PRIVATE void sqlite3PcacheOpen(
8535
 
  int szPage,                    /* Size of every page */
8536
 
  int szExtra,                   /* Extra space associated with each page */
8537
 
  int bPurgeable,                /* True if pages are on backing store */
8538
 
  int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
8539
 
  void *pStress,                 /* Argument to xStress */
8540
 
  PCache *pToInit                /* Preallocated space for the PCache */
8541
 
);
8542
 
 
8543
 
/* Modify the page-size after the cache has been created. */
8544
 
SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
8545
 
 
8546
 
/* Return the size in bytes of a PCache object.  Used to preallocate
8547
 
** storage space.
8548
 
*/
8549
 
SQLITE_PRIVATE int sqlite3PcacheSize(void);
8550
 
 
8551
 
/* One release per successful fetch.  Page is pinned until released.
8552
 
** Reference counted. 
8553
 
*/
8554
 
SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
8555
 
SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
8556
 
 
8557
 
SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
8558
 
SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
8559
 
SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
8560
 
SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
8561
 
 
8562
 
/* Change a page number.  Used by incr-vacuum. */
8563
 
SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
8564
 
 
8565
 
/* Remove all pages with pgno>x.  Reset the cache if x==0 */
8566
 
SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
8567
 
 
8568
 
/* Get a list of all dirty pages in the cache, sorted by page number */
8569
 
SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
8570
 
 
8571
 
/* Reset and close the cache object */
8572
 
SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
8573
 
 
8574
 
/* Clear flags from pages of the page cache */
8575
 
SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
8576
 
 
8577
 
/* Discard the contents of the cache */
8578
 
SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
8579
 
 
8580
 
/* Return the total number of outstanding page references */
8581
 
SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
8582
 
 
8583
 
/* Increment the reference count of an existing page */
8584
 
SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
8585
 
 
8586
 
SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
8587
 
 
8588
 
/* Return the total number of pages stored in the cache */
8589
 
SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
8590
 
 
8591
 
#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
8592
 
/* Iterate through all dirty pages currently stored in the cache. This
8593
 
** interface is only available if SQLITE_CHECK_PAGES is defined when the 
8594
 
** library is built.
8595
 
*/
8596
 
SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
8597
 
#endif
8598
 
 
8599
 
/* Set and get the suggested cache-size for the specified pager-cache.
8600
 
**
8601
 
** If no global maximum is configured, then the system attempts to limit
8602
 
** the total number of pages cached by purgeable pager-caches to the sum
8603
 
** of the suggested cache-sizes.
8604
 
*/
8605
 
SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
8606
 
#ifdef SQLITE_TEST
8607
 
SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
8608
 
#endif
8609
 
 
8610
 
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
8611
 
/* Try to return memory used by the pcache module to the main memory heap */
8612
 
SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
8613
 
#endif
8614
 
 
8615
 
#ifdef SQLITE_TEST
8616
 
SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
8617
 
#endif
8618
 
 
8619
 
SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
8620
 
 
8621
 
#endif /* _PCACHE_H_ */
8622
 
 
8623
 
/************** End of pcache.h **********************************************/
8624
 
/************** Continuing where we left off in sqliteInt.h ******************/
8625
 
 
8626
 
/************** Include os.h in the middle of sqliteInt.h ********************/
8627
 
/************** Begin file os.h **********************************************/
8628
 
/*
8629
 
** 2001 September 16
8630
 
**
8631
 
** The author disclaims copyright to this source code.  In place of
8632
 
** a legal notice, here is a blessing:
8633
 
**
8634
 
**    May you do good and not evil.
8635
 
**    May you find forgiveness for yourself and forgive others.
8636
 
**    May you share freely, never taking more than you give.
8637
 
**
8638
 
******************************************************************************
8639
 
**
8640
 
** This header file (together with is companion C source-code file
8641
 
** "os.c") attempt to abstract the underlying operating system so that
8642
 
** the SQLite library will work on both POSIX and windows systems.
8643
 
**
8644
 
** This header file is #include-ed by sqliteInt.h and thus ends up
8645
 
** being included by every source file.
8646
 
*/
8647
 
#ifndef _SQLITE_OS_H_
8648
 
#define _SQLITE_OS_H_
8649
 
 
8650
 
/*
8651
 
** Figure out if we are dealing with Unix, Windows, or some other
8652
 
** operating system.  After the following block of preprocess macros,
8653
 
** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER 
8654
 
** will defined to either 1 or 0.  One of the four will be 1.  The other 
8655
 
** three will be 0.
8656
 
*/
8657
 
#if defined(SQLITE_OS_OTHER)
8658
 
# if SQLITE_OS_OTHER==1
8659
 
#   undef SQLITE_OS_UNIX
8660
 
#   define SQLITE_OS_UNIX 0
8661
 
#   undef SQLITE_OS_WIN
8662
 
#   define SQLITE_OS_WIN 0
8663
 
#   undef SQLITE_OS_OS2
8664
 
#   define SQLITE_OS_OS2 0
8665
 
# else
8666
 
#   undef SQLITE_OS_OTHER
8667
 
# endif
8668
 
#endif
8669
 
#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
8670
 
# define SQLITE_OS_OTHER 0
8671
 
# ifndef SQLITE_OS_WIN
8672
 
#   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
8673
 
#     define SQLITE_OS_WIN 1
8674
 
#     define SQLITE_OS_UNIX 0
8675
 
#     define SQLITE_OS_OS2 0
8676
 
#   elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
8677
 
#     define SQLITE_OS_WIN 0
8678
 
#     define SQLITE_OS_UNIX 0
8679
 
#     define SQLITE_OS_OS2 1
8680
 
#   else
8681
 
#     define SQLITE_OS_WIN 0
8682
 
#     define SQLITE_OS_UNIX 1
8683
 
#     define SQLITE_OS_OS2 0
8684
 
#  endif
8685
 
# else
8686
 
#  define SQLITE_OS_UNIX 0
8687
 
#  define SQLITE_OS_OS2 0
8688
 
# endif
8689
 
#else
8690
 
# ifndef SQLITE_OS_WIN
8691
 
#  define SQLITE_OS_WIN 0
8692
 
# endif
8693
 
#endif
8694
 
 
8695
 
/*
8696
 
** Determine if we are dealing with WindowsCE - which has a much
8697
 
** reduced API.
8698
 
*/
8699
 
#if defined(_WIN32_WCE)
8700
 
# define SQLITE_OS_WINCE 1
8701
 
#else
8702
 
# define SQLITE_OS_WINCE 0
8703
 
#endif
8704
 
 
8705
 
 
8706
 
/*
8707
 
** Define the maximum size of a temporary filename
8708
 
*/
8709
 
#if SQLITE_OS_WIN
8710
 
# include <windows.h>
8711
 
# define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
8712
 
#elif SQLITE_OS_OS2
8713
 
# if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
8714
 
#  include <os2safe.h> /* has to be included before os2.h for linking to work */
8715
 
# endif
8716
 
# define INCL_DOSDATETIME
8717
 
# define INCL_DOSFILEMGR
8718
 
# define INCL_DOSERRORS
8719
 
# define INCL_DOSMISC
8720
 
# define INCL_DOSPROCESS
8721
 
# define INCL_DOSMODULEMGR
8722
 
# define INCL_DOSSEMAPHORES
8723
 
# include <os2.h>
8724
 
# include <uconv.h>
8725
 
# define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
8726
 
#else
8727
 
# define SQLITE_TEMPNAME_SIZE 200
8728
 
#endif
8729
 
 
8730
 
/* If the SET_FULLSYNC macro is not defined above, then make it
8731
 
** a no-op
8732
 
*/
8733
 
#ifndef SET_FULLSYNC
8734
 
# define SET_FULLSYNC(x,y)
8735
 
#endif
8736
 
 
8737
 
/*
8738
 
** The default size of a disk sector
8739
 
*/
8740
 
#ifndef SQLITE_DEFAULT_SECTOR_SIZE
8741
 
# define SQLITE_DEFAULT_SECTOR_SIZE 512
8742
 
#endif
8743
 
 
8744
 
/*
8745
 
** Temporary files are named starting with this prefix followed by 16 random
8746
 
** alphanumeric characters, and no file extension. They are stored in the
8747
 
** OS's standard temporary file directory, and are deleted prior to exit.
8748
 
** If sqlite is being embedded in another program, you may wish to change the
8749
 
** prefix to reflect your program's name, so that if your program exits
8750
 
** prematurely, old temporary files can be easily identified. This can be done
8751
 
** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
8752
 
**
8753
 
** 2006-10-31:  The default prefix used to be "sqlite_".  But then
8754
 
** Mcafee started using SQLite in their anti-virus product and it
8755
 
** started putting files with the "sqlite" name in the c:/temp folder.
8756
 
** This annoyed many windows users.  Those users would then do a 
8757
 
** Google search for "sqlite", find the telephone numbers of the
8758
 
** developers and call to wake them up at night and complain.
8759
 
** For this reason, the default name prefix is changed to be "sqlite" 
8760
 
** spelled backwards.  So the temp files are still identified, but
8761
 
** anybody smart enough to figure out the code is also likely smart
8762
 
** enough to know that calling the developer will not help get rid
8763
 
** of the file.
8764
 
*/
8765
 
#ifndef SQLITE_TEMP_FILE_PREFIX
8766
 
# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
8767
 
#endif
8768
 
 
8769
 
/*
8770
 
** The following values may be passed as the second argument to
8771
 
** sqlite3OsLock(). The various locks exhibit the following semantics:
8772
 
**
8773
 
** SHARED:    Any number of processes may hold a SHARED lock simultaneously.
8774
 
** RESERVED:  A single process may hold a RESERVED lock on a file at
8775
 
**            any time. Other processes may hold and obtain new SHARED locks.
8776
 
** PENDING:   A single process may hold a PENDING lock on a file at
8777
 
**            any one time. Existing SHARED locks may persist, but no new
8778
 
**            SHARED locks may be obtained by other processes.
8779
 
** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
8780
 
**
8781
 
** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
8782
 
** process that requests an EXCLUSIVE lock may actually obtain a PENDING
8783
 
** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
8784
 
** sqlite3OsLock().
8785
 
*/
8786
 
#define NO_LOCK         0
8787
 
#define SHARED_LOCK     1
8788
 
#define RESERVED_LOCK   2
8789
 
#define PENDING_LOCK    3
8790
 
#define EXCLUSIVE_LOCK  4
8791
 
 
8792
 
/*
8793
 
** File Locking Notes:  (Mostly about windows but also some info for Unix)
8794
 
**
8795
 
** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
8796
 
** those functions are not available.  So we use only LockFile() and
8797
 
** UnlockFile().
8798
 
**
8799
 
** LockFile() prevents not just writing but also reading by other processes.
8800
 
** A SHARED_LOCK is obtained by locking a single randomly-chosen 
8801
 
** byte out of a specific range of bytes. The lock byte is obtained at 
8802
 
** random so two separate readers can probably access the file at the 
8803
 
** same time, unless they are unlucky and choose the same lock byte.
8804
 
** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
8805
 
** There can only be one writer.  A RESERVED_LOCK is obtained by locking
8806
 
** a single byte of the file that is designated as the reserved lock byte.
8807
 
** A PENDING_LOCK is obtained by locking a designated byte different from
8808
 
** the RESERVED_LOCK byte.
8809
 
**
8810
 
** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
8811
 
** which means we can use reader/writer locks.  When reader/writer locks
8812
 
** are used, the lock is placed on the same range of bytes that is used
8813
 
** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
8814
 
** will support two or more Win95 readers or two or more WinNT readers.
8815
 
** But a single Win95 reader will lock out all WinNT readers and a single
8816
 
** WinNT reader will lock out all other Win95 readers.
8817
 
**
8818
 
** The following #defines specify the range of bytes used for locking.
8819
 
** SHARED_SIZE is the number of bytes available in the pool from which
8820
 
** a random byte is selected for a shared lock.  The pool of bytes for
8821
 
** shared locks begins at SHARED_FIRST. 
8822
 
**
8823
 
** The same locking strategy and
8824
 
** byte ranges are used for Unix.  This leaves open the possiblity of having
8825
 
** clients on win95, winNT, and unix all talking to the same shared file
8826
 
** and all locking correctly.  To do so would require that samba (or whatever
8827
 
** tool is being used for file sharing) implements locks correctly between
8828
 
** windows and unix.  I'm guessing that isn't likely to happen, but by
8829
 
** using the same locking range we are at least open to the possibility.
8830
 
**
8831
 
** Locking in windows is manditory.  For this reason, we cannot store
8832
 
** actual data in the bytes used for locking.  The pager never allocates
8833
 
** the pages involved in locking therefore.  SHARED_SIZE is selected so
8834
 
** that all locks will fit on a single page even at the minimum page size.
8835
 
** PENDING_BYTE defines the beginning of the locks.  By default PENDING_BYTE
8836
 
** is set high so that we don't have to allocate an unused page except
8837
 
** for very large databases.  But one should test the page skipping logic 
8838
 
** by setting PENDING_BYTE low and running the entire regression suite.
8839
 
**
8840
 
** Changing the value of PENDING_BYTE results in a subtly incompatible
8841
 
** file format.  Depending on how it is changed, you might not notice
8842
 
** the incompatibility right away, even running a full regression test.
8843
 
** The default location of PENDING_BYTE is the first byte past the
8844
 
** 1GB boundary.
8845
 
**
8846
 
*/
8847
 
#ifdef SQLITE_OMIT_WSD
8848
 
# define PENDING_BYTE     (0x40000000)
8849
 
#else
8850
 
# define PENDING_BYTE      sqlite3PendingByte
8851
 
#endif
8852
 
#define RESERVED_BYTE     (PENDING_BYTE+1)
8853
 
#define SHARED_FIRST      (PENDING_BYTE+2)
8854
 
#define SHARED_SIZE       510
8855
 
 
8856
 
/*
8857
 
** Wrapper around OS specific sqlite3_os_init() function.
8858
 
*/
8859
 
SQLITE_PRIVATE int sqlite3OsInit(void);
8860
 
 
8861
 
/* 
8862
 
** Functions for accessing sqlite3_file methods 
8863
 
*/
8864
 
SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
8865
 
SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
8866
 
SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
8867
 
SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
8868
 
SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
8869
 
SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
8870
 
SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
8871
 
SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
8872
 
SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
8873
 
SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
8874
 
#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
8875
 
SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
8876
 
SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
8877
 
SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
8878
 
SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
8879
 
SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
8880
 
SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
8881
 
 
8882
 
/* 
8883
 
** Functions for accessing sqlite3_vfs methods 
8884
 
*/
8885
 
SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
8886
 
SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
8887
 
SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
8888
 
SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
8889
 
#ifndef SQLITE_OMIT_LOAD_EXTENSION
8890
 
SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
8891
 
SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
8892
 
SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
8893
 
SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
8894
 
#endif /* SQLITE_OMIT_LOAD_EXTENSION */
8895
 
SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
8896
 
SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
8897
 
SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
8898
 
 
8899
 
/*
8900
 
** Convenience functions for opening and closing files using 
8901
 
** sqlite3_malloc() to obtain space for the file-handle structure.
8902
 
*/
8903
 
SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
8904
 
SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
8905
 
 
8906
 
#endif /* _SQLITE_OS_H_ */
8907
 
 
8908
 
/************** End of os.h **************************************************/
8909
 
/************** Continuing where we left off in sqliteInt.h ******************/
8910
 
/************** Include mutex.h in the middle of sqliteInt.h *****************/
8911
 
/************** Begin file mutex.h *******************************************/
8912
 
/*
8913
 
** 2007 August 28
8914
 
**
8915
 
** The author disclaims copyright to this source code.  In place of
8916
 
** a legal notice, here is a blessing:
8917
 
**
8918
 
**    May you do good and not evil.
8919
 
**    May you find forgiveness for yourself and forgive others.
8920
 
**    May you share freely, never taking more than you give.
8921
 
**
8922
 
*************************************************************************
8923
 
**
8924
 
** This file contains the common header for all mutex implementations.
8925
 
** The sqliteInt.h header #includes this file so that it is available
8926
 
** to all source files.  We break it out in an effort to keep the code
8927
 
** better organized.
8928
 
**
8929
 
** NOTE:  source files should *not* #include this header file directly.
8930
 
** Source files should #include the sqliteInt.h file and let that file
8931
 
** include this one indirectly.
8932
 
*/
8933
 
 
8934
 
 
8935
 
/*
8936
 
** Figure out what version of the code to use.  The choices are
8937
 
**
8938
 
**   SQLITE_MUTEX_OMIT         No mutex logic.  Not even stubs.  The
8939
 
**                             mutexes implemention cannot be overridden
8940
 
**                             at start-time.
8941
 
**
8942
 
**   SQLITE_MUTEX_NOOP         For single-threaded applications.  No
8943
 
**                             mutual exclusion is provided.  But this
8944
 
**                             implementation can be overridden at
8945
 
**                             start-time.
8946
 
**
8947
 
**   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
8948
 
**
8949
 
**   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
8950
 
**
8951
 
**   SQLITE_MUTEX_OS2          For multi-threaded applications on OS/2.
8952
 
*/
8953
 
#if !SQLITE_THREADSAFE
8954
 
# define SQLITE_MUTEX_OMIT
8955
 
#endif
8956
 
#if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
8957
 
#  if SQLITE_OS_UNIX
8958
 
#    define SQLITE_MUTEX_PTHREADS
8959
 
#  elif SQLITE_OS_WIN
8960
 
#    define SQLITE_MUTEX_W32
8961
 
#  elif SQLITE_OS_OS2
8962
 
#    define SQLITE_MUTEX_OS2
8963
 
#  else
8964
 
#    define SQLITE_MUTEX_NOOP
8965
 
#  endif
8966
 
#endif
8967
 
 
8968
 
#ifdef SQLITE_MUTEX_OMIT
8969
 
/*
8970
 
** If this is a no-op implementation, implement everything as macros.
8971
 
*/
8972
 
#define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
8973
 
#define sqlite3_mutex_free(X)
8974
 
#define sqlite3_mutex_enter(X)
8975
 
#define sqlite3_mutex_try(X)      SQLITE_OK
8976
 
#define sqlite3_mutex_leave(X)
8977
 
#define sqlite3_mutex_held(X)     ((void)(X),1)
8978
 
#define sqlite3_mutex_notheld(X)  ((void)(X),1)
8979
 
#define sqlite3MutexAlloc(X)      ((sqlite3_mutex*)8)
8980
 
#define sqlite3MutexInit()        SQLITE_OK
8981
 
#define sqlite3MutexEnd()
8982
 
#endif /* defined(SQLITE_MUTEX_OMIT) */
8983
 
 
8984
 
/************** End of mutex.h ***********************************************/
8985
 
/************** Continuing where we left off in sqliteInt.h ******************/
8986
 
 
8987
 
 
8988
 
/*
8989
 
** Each database file to be accessed by the system is an instance
8990
 
** of the following structure.  There are normally two of these structures
8991
 
** in the sqlite.aDb[] array.  aDb[0] is the main database file and
8992
 
** aDb[1] is the database file used to hold temporary tables.  Additional
8993
 
** databases may be attached.
8994
 
*/
8995
 
struct Db {
8996
 
  char *zName;         /* Name of this database */
8997
 
  Btree *pBt;          /* The B*Tree structure for this database file */
8998
 
  u8 inTrans;          /* 0: not writable.  1: Transaction.  2: Checkpoint */
8999
 
  u8 safety_level;     /* How aggressive at syncing data to disk */
9000
 
  Schema *pSchema;     /* Pointer to database schema (possibly shared) */
9001
 
};
9002
 
 
9003
 
/*
9004
 
** An instance of the following structure stores a database schema.
9005
 
**
9006
 
** Most Schema objects are associated with a Btree.  The exception is
9007
 
** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
9008
 
** In shared cache mode, a single Schema object can be shared by multiple
9009
 
** Btrees that refer to the same underlying BtShared object.
9010
 
** 
9011
 
** Schema objects are automatically deallocated when the last Btree that
9012
 
** references them is destroyed.   The TEMP Schema is manually freed by
9013
 
** sqlite3_close().
9014
 
*
9015
 
** A thread must be holding a mutex on the corresponding Btree in order
9016
 
** to access Schema content.  This implies that the thread must also be
9017
 
** holding a mutex on the sqlite3 connection pointer that owns the Btree.
9018
 
** For a TEMP Schema, on the connection mutex is required.
9019
 
*/
9020
 
struct Schema {
9021
 
  int schema_cookie;   /* Database schema version number for this file */
9022
 
  int iGeneration;     /* Generation counter.  Incremented with each change */
9023
 
  Hash tblHash;        /* All tables indexed by name */
9024
 
  Hash idxHash;        /* All (named) indices indexed by name */
9025
 
  Hash trigHash;       /* All triggers indexed by name */
9026
 
  Hash fkeyHash;       /* All foreign keys by referenced table name */
9027
 
  Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
9028
 
  u8 file_format;      /* Schema format version for this file */
9029
 
  u8 enc;              /* Text encoding used by this database */
9030
 
  u16 flags;           /* Flags associated with this schema */
9031
 
  int cache_size;      /* Number of pages to use in the cache */
9032
 
};
9033
 
 
9034
 
/*
9035
 
** These macros can be used to test, set, or clear bits in the 
9036
 
** Db.pSchema->flags field.
9037
 
*/
9038
 
#define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
9039
 
#define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->flags&(P))!=0)
9040
 
#define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->flags|=(P)
9041
 
#define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->flags&=~(P)
9042
 
 
9043
 
/*
9044
 
** Allowed values for the DB.pSchema->flags field.
9045
 
**
9046
 
** The DB_SchemaLoaded flag is set after the database schema has been
9047
 
** read into internal hash tables.
9048
 
**
9049
 
** DB_UnresetViews means that one or more views have column names that
9050
 
** have been filled out.  If the schema changes, these column names might
9051
 
** changes and so the view will need to be reset.
9052
 
*/
9053
 
#define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
9054
 
#define DB_UnresetViews    0x0002  /* Some views have defined column names */
9055
 
#define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
9056
 
 
9057
 
/*
9058
 
** The number of different kinds of things that can be limited
9059
 
** using the sqlite3_limit() interface.
9060
 
*/
9061
 
#define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1)
9062
 
 
9063
 
/*
9064
 
** Lookaside malloc is a set of fixed-size buffers that can be used
9065
 
** to satisfy small transient memory allocation requests for objects
9066
 
** associated with a particular database connection.  The use of
9067
 
** lookaside malloc provides a significant performance enhancement
9068
 
** (approx 10%) by avoiding numerous malloc/free requests while parsing
9069
 
** SQL statements.
9070
 
**
9071
 
** The Lookaside structure holds configuration information about the
9072
 
** lookaside malloc subsystem.  Each available memory allocation in
9073
 
** the lookaside subsystem is stored on a linked list of LookasideSlot
9074
 
** objects.
9075
 
**
9076
 
** Lookaside allocations are only allowed for objects that are associated
9077
 
** with a particular database connection.  Hence, schema information cannot
9078
 
** be stored in lookaside because in shared cache mode the schema information
9079
 
** is shared by multiple database connections.  Therefore, while parsing
9080
 
** schema information, the Lookaside.bEnabled flag is cleared so that
9081
 
** lookaside allocations are not used to construct the schema objects.
9082
 
*/
9083
 
struct Lookaside {
9084
 
  u16 sz;                 /* Size of each buffer in bytes */
9085
 
  u8 bEnabled;            /* False to disable new lookaside allocations */
9086
 
  u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
9087
 
  int nOut;               /* Number of buffers currently checked out */
9088
 
  int mxOut;              /* Highwater mark for nOut */
9089
 
  int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
9090
 
  LookasideSlot *pFree;   /* List of available buffers */
9091
 
  void *pStart;           /* First byte of available memory space */
9092
 
  void *pEnd;             /* First byte past end of available space */
9093
 
};
9094
 
struct LookasideSlot {
9095
 
  LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
9096
 
};
9097
 
 
9098
 
/*
9099
 
** A hash table for function definitions.
9100
 
**
9101
 
** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
9102
 
** Collisions are on the FuncDef.pHash chain.
9103
 
*/
9104
 
struct FuncDefHash {
9105
 
  FuncDef *a[23];       /* Hash table for functions */
9106
 
};
9107
 
 
9108
 
/*
9109
 
** Each database connection is an instance of the following structure.
9110
 
**
9111
 
** The sqlite.lastRowid records the last insert rowid generated by an
9112
 
** insert statement.  Inserts on views do not affect its value.  Each
9113
 
** trigger has its own context, so that lastRowid can be updated inside
9114
 
** triggers as usual.  The previous value will be restored once the trigger
9115
 
** exits.  Upon entering a before or instead of trigger, lastRowid is no
9116
 
** longer (since after version 2.8.12) reset to -1.
9117
 
**
9118
 
** The sqlite.nChange does not count changes within triggers and keeps no
9119
 
** context.  It is reset at start of sqlite3_exec.
9120
 
** The sqlite.lsChange represents the number of changes made by the last
9121
 
** insert, update, or delete statement.  It remains constant throughout the
9122
 
** length of a statement and is then updated by OP_SetCounts.  It keeps a
9123
 
** context stack just like lastRowid so that the count of changes
9124
 
** within a trigger is not seen outside the trigger.  Changes to views do not
9125
 
** affect the value of lsChange.
9126
 
** The sqlite.csChange keeps track of the number of current changes (since
9127
 
** the last statement) and is used to update sqlite_lsChange.
9128
 
**
9129
 
** The member variables sqlite.errCode, sqlite.zErrMsg and sqlite.zErrMsg16
9130
 
** store the most recent error code and, if applicable, string. The
9131
 
** internal function sqlite3Error() is used to set these variables
9132
 
** consistently.
9133
 
*/
9134
 
struct sqlite3 {
9135
 
  sqlite3_vfs *pVfs;            /* OS Interface */
9136
 
  int nDb;                      /* Number of backends currently in use */
9137
 
  Db *aDb;                      /* All backends */
9138
 
  int flags;                    /* Miscellaneous flags. See below */
9139
 
  int openFlags;                /* Flags passed to sqlite3_vfs.xOpen() */
9140
 
  int errCode;                  /* Most recent error code (SQLITE_*) */
9141
 
  int errMask;                  /* & result codes with this before returning */
9142
 
  u8 autoCommit;                /* The auto-commit flag. */
9143
 
  u8 temp_store;                /* 1: file 2: memory 0: default */
9144
 
  u8 mallocFailed;              /* True if we have seen a malloc failure */
9145
 
  u8 dfltLockMode;              /* Default locking-mode for attached dbs */
9146
 
  signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
9147
 
  u8 suppressErr;               /* Do not issue error messages if true */
9148
 
  int nextPagesize;             /* Pagesize after VACUUM if >0 */
9149
 
  int nTable;                   /* Number of tables in the database */
9150
 
  CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
9151
 
  i64 lastRowid;                /* ROWID of most recent insert (see above) */
9152
 
  u32 magic;                    /* Magic number for detect library misuse */
9153
 
  int nChange;                  /* Value returned by sqlite3_changes() */
9154
 
  int nTotalChange;             /* Value returned by sqlite3_total_changes() */
9155
 
  sqlite3_mutex *mutex;         /* Connection mutex */
9156
 
  int aLimit[SQLITE_N_LIMIT];   /* Limits */
9157
 
  struct sqlite3InitInfo {      /* Information used during initialization */
9158
 
    int iDb;                    /* When back is being initialized */
9159
 
    int newTnum;                /* Rootpage of table being initialized */
9160
 
    u8 busy;                    /* TRUE if currently initializing */
9161
 
    u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
9162
 
  } init;
9163
 
  int nExtension;               /* Number of loaded extensions */
9164
 
  void **aExtension;            /* Array of shared library handles */
9165
 
  struct Vdbe *pVdbe;           /* List of active virtual machines */
9166
 
  int activeVdbeCnt;            /* Number of VDBEs currently executing */
9167
 
  int writeVdbeCnt;             /* Number of active VDBEs that are writing */
9168
 
  int vdbeExecCnt;              /* Number of nested calls to VdbeExec() */
9169
 
  void (*xTrace)(void*,const char*);        /* Trace function */
9170
 
  void *pTraceArg;                          /* Argument to the trace function */
9171
 
  void (*xProfile)(void*,const char*,u64);  /* Profiling function */
9172
 
  void *pProfileArg;                        /* Argument to profile function */
9173
 
  void *pCommitArg;                 /* Argument to xCommitCallback() */   
9174
 
  int (*xCommitCallback)(void*);    /* Invoked at every commit. */
9175
 
  void *pRollbackArg;               /* Argument to xRollbackCallback() */   
9176
 
  void (*xRollbackCallback)(void*); /* Invoked at every commit. */
9177
 
  void *pUpdateArg;
9178
 
  void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
9179
 
#ifndef SQLITE_OMIT_WAL
9180
 
  int (*xWalCallback)(void *, sqlite3 *, const char *, int);
9181
 
  void *pWalArg;
9182
 
#endif
9183
 
  void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
9184
 
  void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
9185
 
  void *pCollNeededArg;
9186
 
  sqlite3_value *pErr;          /* Most recent error message */
9187
 
  char *zErrMsg;                /* Most recent error message (UTF-8 encoded) */
9188
 
  char *zErrMsg16;              /* Most recent error message (UTF-16 encoded) */
9189
 
  union {
9190
 
    volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
9191
 
    double notUsed1;            /* Spacer */
9192
 
  } u1;
9193
 
  Lookaside lookaside;          /* Lookaside malloc configuration */
9194
 
#ifndef SQLITE_OMIT_AUTHORIZATION
9195
 
  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
9196
 
                                /* Access authorization function */
9197
 
  void *pAuthArg;               /* 1st argument to the access auth function */
9198
 
#endif
9199
 
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9200
 
  int (*xProgress)(void *);     /* The progress callback */
9201
 
  void *pProgressArg;           /* Argument to the progress callback */
9202
 
  int nProgressOps;             /* Number of opcodes for progress callback */
9203
 
#endif
9204
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
9205
 
  Hash aModule;                 /* populated by sqlite3_create_module() */
9206
 
  Table *pVTab;                 /* vtab with active Connect/Create method */
9207
 
  VTable **aVTrans;             /* Virtual tables with open transactions */
9208
 
  int nVTrans;                  /* Allocated size of aVTrans */
9209
 
  VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
9210
 
#endif
9211
 
  FuncDefHash aFunc;            /* Hash table of connection functions */
9212
 
  Hash aCollSeq;                /* All collating sequences */
9213
 
  BusyHandler busyHandler;      /* Busy callback */
9214
 
  int busyTimeout;              /* Busy handler timeout, in msec */
9215
 
  Db aDbStatic[2];              /* Static space for the 2 default backends */
9216
 
  Savepoint *pSavepoint;        /* List of active savepoints */
9217
 
  int nSavepoint;               /* Number of non-transaction savepoints */
9218
 
  int nStatement;               /* Number of nested statement-transactions  */
9219
 
  u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
9220
 
  i64 nDeferredCons;            /* Net deferred constraints this transaction. */
9221
 
  int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
9222
 
 
9223
 
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
9224
 
  /* The following variables are all protected by the STATIC_MASTER 
9225
 
  ** mutex, not by sqlite3.mutex. They are used by code in notify.c. 
9226
 
  **
9227
 
  ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
9228
 
  ** unlock so that it can proceed.
9229
 
  **
9230
 
  ** When X.pBlockingConnection==Y, that means that something that X tried
9231
 
  ** tried to do recently failed with an SQLITE_LOCKED error due to locks
9232
 
  ** held by Y.
9233
 
  */
9234
 
  sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
9235
 
  sqlite3 *pUnlockConnection;           /* Connection to watch for unlock */
9236
 
  void *pUnlockArg;                     /* Argument to xUnlockNotify */
9237
 
  void (*xUnlockNotify)(void **, int);  /* Unlock notify callback */
9238
 
  sqlite3 *pNextBlocked;        /* Next in list of all blocked connections */
9239
 
#endif
9240
 
};
9241
 
 
9242
 
/*
9243
 
** A macro to discover the encoding of a database.
9244
 
*/
9245
 
#define ENC(db) ((db)->aDb[0].pSchema->enc)
9246
 
 
9247
 
/*
9248
 
** Possible values for the sqlite3.flags.
9249
 
*/
9250
 
#define SQLITE_VdbeTrace      0x00000100  /* True to trace VDBE execution */
9251
 
#define SQLITE_InternChanges  0x00000200  /* Uncommitted Hash table changes */
9252
 
#define SQLITE_FullColNames   0x00000400  /* Show full column names on SELECT */
9253
 
#define SQLITE_ShortColNames  0x00000800  /* Show short columns names */
9254
 
#define SQLITE_CountRows      0x00001000  /* Count rows changed by INSERT, */
9255
 
                                          /*   DELETE, or UPDATE and return */
9256
 
                                          /*   the count using a callback. */
9257
 
#define SQLITE_NullCallback   0x00002000  /* Invoke the callback once if the */
9258
 
                                          /*   result set is empty */
9259
 
#define SQLITE_SqlTrace       0x00004000  /* Debug print SQL as it executes */
9260
 
#define SQLITE_VdbeListing    0x00008000  /* Debug listings of VDBE programs */
9261
 
#define SQLITE_WriteSchema    0x00010000  /* OK to update SQLITE_MASTER */
9262
 
#define SQLITE_NoReadlock     0x00020000  /* Readlocks are omitted when 
9263
 
                                          ** accessing read-only databases */
9264
 
#define SQLITE_IgnoreChecks   0x00040000  /* Do not enforce check constraints */
9265
 
#define SQLITE_ReadUncommitted 0x0080000  /* For shared-cache mode */
9266
 
#define SQLITE_LegacyFileFmt  0x00100000  /* Create new databases in format 1 */
9267
 
#define SQLITE_FullFSync      0x00200000  /* Use full fsync on the backend */
9268
 
#define SQLITE_CkptFullFSync  0x00400000  /* Use full fsync for checkpoint */
9269
 
#define SQLITE_RecoveryMode   0x00800000  /* Ignore schema errors */
9270
 
#define SQLITE_ReverseOrder   0x01000000  /* Reverse unordered SELECTs */
9271
 
#define SQLITE_RecTriggers    0x02000000  /* Enable recursive triggers */
9272
 
#define SQLITE_ForeignKeys    0x04000000  /* Enforce foreign key constraints  */
9273
 
#define SQLITE_AutoIndex      0x08000000  /* Enable automatic indexes */
9274
 
#define SQLITE_PreferBuiltin  0x10000000  /* Preference to built-in funcs */
9275
 
#define SQLITE_LoadExtension  0x20000000  /* Enable load_extension */
9276
 
#define SQLITE_EnableTrigger  0x40000000  /* True to enable triggers */
9277
 
 
9278
 
/*
9279
 
** Bits of the sqlite3.flags field that are used by the
9280
 
** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
9281
 
** These must be the low-order bits of the flags field.
9282
 
*/
9283
 
#define SQLITE_QueryFlattener 0x01        /* Disable query flattening */
9284
 
#define SQLITE_ColumnCache    0x02        /* Disable the column cache */
9285
 
#define SQLITE_IndexSort      0x04        /* Disable indexes for sorting */
9286
 
#define SQLITE_IndexSearch    0x08        /* Disable indexes for searching */
9287
 
#define SQLITE_IndexCover     0x10        /* Disable index covering table */
9288
 
#define SQLITE_GroupByOrder   0x20        /* Disable GROUPBY cover of ORDERBY */
9289
 
#define SQLITE_FactorOutConst 0x40        /* Disable factoring out constants */
9290
 
#define SQLITE_OptMask        0xff        /* Mask of all disablable opts */
9291
 
 
9292
 
/*
9293
 
** Possible values for the sqlite.magic field.
9294
 
** The numbers are obtained at random and have no special meaning, other
9295
 
** than being distinct from one another.
9296
 
*/
9297
 
#define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
9298
 
#define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
9299
 
#define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
9300
 
#define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
9301
 
#define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
9302
 
 
9303
 
/*
9304
 
** Each SQL function is defined by an instance of the following
9305
 
** structure.  A pointer to this structure is stored in the sqlite.aFunc
9306
 
** hash table.  When multiple functions have the same name, the hash table
9307
 
** points to a linked list of these structures.
9308
 
*/
9309
 
struct FuncDef {
9310
 
  i16 nArg;            /* Number of arguments.  -1 means unlimited */
9311
 
  u8 iPrefEnc;         /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */
9312
 
  u8 flags;            /* Some combination of SQLITE_FUNC_* */
9313
 
  void *pUserData;     /* User data parameter */
9314
 
  FuncDef *pNext;      /* Next function with same name */
9315
 
  void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
9316
 
  void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
9317
 
  void (*xFinalize)(sqlite3_context*);                /* Aggregate finalizer */
9318
 
  char *zName;         /* SQL name of the function. */
9319
 
  FuncDef *pHash;      /* Next with a different name but the same hash */
9320
 
  FuncDestructor *pDestructor;   /* Reference counted destructor function */
9321
 
};
9322
 
 
9323
 
/*
9324
 
** This structure encapsulates a user-function destructor callback (as
9325
 
** configured using create_function_v2()) and a reference counter. When
9326
 
** create_function_v2() is called to create a function with a destructor,
9327
 
** a single object of this type is allocated. FuncDestructor.nRef is set to 
9328
 
** the number of FuncDef objects created (either 1 or 3, depending on whether
9329
 
** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
9330
 
** member of each of the new FuncDef objects is set to point to the allocated
9331
 
** FuncDestructor.
9332
 
**
9333
 
** Thereafter, when one of the FuncDef objects is deleted, the reference
9334
 
** count on this object is decremented. When it reaches 0, the destructor
9335
 
** is invoked and the FuncDestructor structure freed.
9336
 
*/
9337
 
struct FuncDestructor {
9338
 
  int nRef;
9339
 
  void (*xDestroy)(void *);
9340
 
  void *pUserData;
9341
 
};
9342
 
 
9343
 
/*
9344
 
** Possible values for FuncDef.flags
9345
 
*/
9346
 
#define SQLITE_FUNC_LIKE     0x01 /* Candidate for the LIKE optimization */
9347
 
#define SQLITE_FUNC_CASE     0x02 /* Case-sensitive LIKE-type function */
9348
 
#define SQLITE_FUNC_EPHEM    0x04 /* Ephemeral.  Delete with VDBE */
9349
 
#define SQLITE_FUNC_NEEDCOLL 0x08 /* sqlite3GetFuncCollSeq() might be called */
9350
 
#define SQLITE_FUNC_PRIVATE  0x10 /* Allowed for internal use only */
9351
 
#define SQLITE_FUNC_COUNT    0x20 /* Built-in count(*) aggregate */
9352
 
#define SQLITE_FUNC_COALESCE 0x40 /* Built-in coalesce() or ifnull() function */
9353
 
 
9354
 
/*
9355
 
** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
9356
 
** used to create the initializers for the FuncDef structures.
9357
 
**
9358
 
**   FUNCTION(zName, nArg, iArg, bNC, xFunc)
9359
 
**     Used to create a scalar function definition of a function zName 
9360
 
**     implemented by C function xFunc that accepts nArg arguments. The
9361
 
**     value passed as iArg is cast to a (void*) and made available
9362
 
**     as the user-data (sqlite3_user_data()) for the function. If 
9363
 
**     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
9364
 
**
9365
 
**   AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
9366
 
**     Used to create an aggregate function definition implemented by
9367
 
**     the C functions xStep and xFinal. The first four parameters
9368
 
**     are interpreted in the same way as the first 4 parameters to
9369
 
**     FUNCTION().
9370
 
**
9371
 
**   LIKEFUNC(zName, nArg, pArg, flags)
9372
 
**     Used to create a scalar function definition of a function zName 
9373
 
**     that accepts nArg arguments and is implemented by a call to C 
9374
 
**     function likeFunc. Argument pArg is cast to a (void *) and made
9375
 
**     available as the function user-data (sqlite3_user_data()). The
9376
 
**     FuncDef.flags variable is set to the value passed as the flags
9377
 
**     parameter.
9378
 
*/
9379
 
#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
9380
 
  {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
9381
 
   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
9382
 
#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
9383
 
  {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
9384
 
   pArg, 0, xFunc, 0, 0, #zName, 0, 0}
9385
 
#define LIKEFUNC(zName, nArg, arg, flags) \
9386
 
  {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
9387
 
#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
9388
 
  {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \
9389
 
   SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
9390
 
 
9391
 
/*
9392
 
** All current savepoints are stored in a linked list starting at
9393
 
** sqlite3.pSavepoint. The first element in the list is the most recently
9394
 
** opened savepoint. Savepoints are added to the list by the vdbe
9395
 
** OP_Savepoint instruction.
9396
 
*/
9397
 
struct Savepoint {
9398
 
  char *zName;                        /* Savepoint name (nul-terminated) */
9399
 
  i64 nDeferredCons;                  /* Number of deferred fk violations */
9400
 
  Savepoint *pNext;                   /* Parent savepoint (if any) */
9401
 
};
9402
 
 
9403
 
/*
9404
 
** The following are used as the second parameter to sqlite3Savepoint(),
9405
 
** and as the P1 argument to the OP_Savepoint instruction.
9406
 
*/
9407
 
#define SAVEPOINT_BEGIN      0
9408
 
#define SAVEPOINT_RELEASE    1
9409
 
#define SAVEPOINT_ROLLBACK   2
9410
 
 
9411
 
 
9412
 
/*
9413
 
** Each SQLite module (virtual table definition) is defined by an
9414
 
** instance of the following structure, stored in the sqlite3.aModule
9415
 
** hash table.
9416
 
*/
9417
 
struct Module {
9418
 
  const sqlite3_module *pModule;       /* Callback pointers */
9419
 
  const char *zName;                   /* Name passed to create_module() */
9420
 
  void *pAux;                          /* pAux passed to create_module() */
9421
 
  void (*xDestroy)(void *);            /* Module destructor function */
9422
 
};
9423
 
 
9424
 
/*
9425
 
** information about each column of an SQL table is held in an instance
9426
 
** of this structure.
9427
 
*/
9428
 
struct Column {
9429
 
  char *zName;     /* Name of this column */
9430
 
  Expr *pDflt;     /* Default value of this column */
9431
 
  char *zDflt;     /* Original text of the default value */
9432
 
  char *zType;     /* Data type for this column */
9433
 
  char *zColl;     /* Collating sequence.  If NULL, use the default */
9434
 
  u8 notNull;      /* True if there is a NOT NULL constraint */
9435
 
  u8 isPrimKey;    /* True if this column is part of the PRIMARY KEY */
9436
 
  char affinity;   /* One of the SQLITE_AFF_... values */
9437
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
9438
 
  u8 isHidden;     /* True if this column is 'hidden' */
9439
 
#endif
9440
 
};
9441
 
 
9442
 
/*
9443
 
** A "Collating Sequence" is defined by an instance of the following
9444
 
** structure. Conceptually, a collating sequence consists of a name and
9445
 
** a comparison routine that defines the order of that sequence.
9446
 
**
9447
 
** There may two separate implementations of the collation function, one
9448
 
** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that
9449
 
** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine
9450
 
** native byte order. When a collation sequence is invoked, SQLite selects
9451
 
** the version that will require the least expensive encoding
9452
 
** translations, if any.
9453
 
**
9454
 
** The CollSeq.pUser member variable is an extra parameter that passed in
9455
 
** as the first argument to the UTF-8 comparison function, xCmp.
9456
 
** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function,
9457
 
** xCmp16.
9458
 
**
9459
 
** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the
9460
 
** collating sequence is undefined.  Indices built on an undefined
9461
 
** collating sequence may not be read or written.
9462
 
*/
9463
 
struct CollSeq {
9464
 
  char *zName;          /* Name of the collating sequence, UTF-8 encoded */
9465
 
  u8 enc;               /* Text encoding handled by xCmp() */
9466
 
  u8 type;              /* One of the SQLITE_COLL_... values below */
9467
 
  void *pUser;          /* First argument to xCmp() */
9468
 
  int (*xCmp)(void*,int, const void*, int, const void*);
9469
 
  void (*xDel)(void*);  /* Destructor for pUser */
9470
 
};
9471
 
 
9472
 
/*
9473
 
** Allowed values of CollSeq.type:
9474
 
*/
9475
 
#define SQLITE_COLL_BINARY  1  /* The default memcmp() collating sequence */
9476
 
#define SQLITE_COLL_NOCASE  2  /* The built-in NOCASE collating sequence */
9477
 
#define SQLITE_COLL_REVERSE 3  /* The built-in REVERSE collating sequence */
9478
 
#define SQLITE_COLL_USER    0  /* Any other user-defined collating sequence */
9479
 
 
9480
 
/*
9481
 
** A sort order can be either ASC or DESC.
9482
 
*/
9483
 
#define SQLITE_SO_ASC       0  /* Sort in ascending order */
9484
 
#define SQLITE_SO_DESC      1  /* Sort in ascending order */
9485
 
 
9486
 
/*
9487
 
** Column affinity types.
9488
 
**
9489
 
** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
9490
 
** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
9491
 
** the speed a little by numbering the values consecutively.  
9492
 
**
9493
 
** But rather than start with 0 or 1, we begin with 'a'.  That way,
9494
 
** when multiple affinity types are concatenated into a string and
9495
 
** used as the P4 operand, they will be more readable.
9496
 
**
9497
 
** Note also that the numeric types are grouped together so that testing
9498
 
** for a numeric type is a single comparison.
9499
 
*/
9500
 
#define SQLITE_AFF_TEXT     'a'
9501
 
#define SQLITE_AFF_NONE     'b'
9502
 
#define SQLITE_AFF_NUMERIC  'c'
9503
 
#define SQLITE_AFF_INTEGER  'd'
9504
 
#define SQLITE_AFF_REAL     'e'
9505
 
 
9506
 
#define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
9507
 
 
9508
 
/*
9509
 
** The SQLITE_AFF_MASK values masks off the significant bits of an
9510
 
** affinity value. 
9511
 
*/
9512
 
#define SQLITE_AFF_MASK     0x67
9513
 
 
9514
 
/*
9515
 
** Additional bit values that can be ORed with an affinity without
9516
 
** changing the affinity.
9517
 
*/
9518
 
#define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
9519
 
#define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
9520
 
#define SQLITE_NULLEQ       0x80  /* NULL=NULL */
9521
 
 
9522
 
/*
9523
 
** An object of this type is created for each virtual table present in
9524
 
** the database schema. 
9525
 
**
9526
 
** If the database schema is shared, then there is one instance of this
9527
 
** structure for each database connection (sqlite3*) that uses the shared
9528
 
** schema. This is because each database connection requires its own unique
9529
 
** instance of the sqlite3_vtab* handle used to access the virtual table 
9530
 
** implementation. sqlite3_vtab* handles can not be shared between 
9531
 
** database connections, even when the rest of the in-memory database 
9532
 
** schema is shared, as the implementation often stores the database
9533
 
** connection handle passed to it via the xConnect() or xCreate() method
9534
 
** during initialization internally. This database connection handle may
9535
 
** then be used by the virtual table implementation to access real tables 
9536
 
** within the database. So that they appear as part of the callers 
9537
 
** transaction, these accesses need to be made via the same database 
9538
 
** connection as that used to execute SQL operations on the virtual table.
9539
 
**
9540
 
** All VTable objects that correspond to a single table in a shared
9541
 
** database schema are initially stored in a linked-list pointed to by
9542
 
** the Table.pVTable member variable of the corresponding Table object.
9543
 
** When an sqlite3_prepare() operation is required to access the virtual
9544
 
** table, it searches the list for the VTable that corresponds to the
9545
 
** database connection doing the preparing so as to use the correct
9546
 
** sqlite3_vtab* handle in the compiled query.
9547
 
**
9548
 
** When an in-memory Table object is deleted (for example when the
9549
 
** schema is being reloaded for some reason), the VTable objects are not 
9550
 
** deleted and the sqlite3_vtab* handles are not xDisconnect()ed 
9551
 
** immediately. Instead, they are moved from the Table.pVTable list to
9552
 
** another linked list headed by the sqlite3.pDisconnect member of the
9553
 
** corresponding sqlite3 structure. They are then deleted/xDisconnected 
9554
 
** next time a statement is prepared using said sqlite3*. This is done
9555
 
** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
9556
 
** Refer to comments above function sqlite3VtabUnlockList() for an
9557
 
** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
9558
 
** list without holding the corresponding sqlite3.mutex mutex.
9559
 
**
9560
 
** The memory for objects of this type is always allocated by 
9561
 
** sqlite3DbMalloc(), using the connection handle stored in VTable.db as 
9562
 
** the first argument.
9563
 
*/
9564
 
struct VTable {
9565
 
  sqlite3 *db;              /* Database connection associated with this table */
9566
 
  Module *pMod;             /* Pointer to module implementation */
9567
 
  sqlite3_vtab *pVtab;      /* Pointer to vtab instance */
9568
 
  int nRef;                 /* Number of pointers to this structure */
9569
 
  VTable *pNext;            /* Next in linked list (see above) */
9570
 
};
9571
 
 
9572
 
/*
9573
 
** Each SQL table is represented in memory by an instance of the
9574
 
** following structure.
9575
 
**
9576
 
** Table.zName is the name of the table.  The case of the original
9577
 
** CREATE TABLE statement is stored, but case is not significant for
9578
 
** comparisons.
9579
 
**
9580
 
** Table.nCol is the number of columns in this table.  Table.aCol is a
9581
 
** pointer to an array of Column structures, one for each column.
9582
 
**
9583
 
** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
9584
 
** the column that is that key.   Otherwise Table.iPKey is negative.  Note
9585
 
** that the datatype of the PRIMARY KEY must be INTEGER for this field to
9586
 
** be set.  An INTEGER PRIMARY KEY is used as the rowid for each row of
9587
 
** the table.  If a table has no INTEGER PRIMARY KEY, then a random rowid
9588
 
** is generated for each row of the table.  TF_HasPrimaryKey is set if
9589
 
** the table has any PRIMARY KEY, INTEGER or otherwise.
9590
 
**
9591
 
** Table.tnum is the page number for the root BTree page of the table in the
9592
 
** database file.  If Table.iDb is the index of the database table backend
9593
 
** in sqlite.aDb[].  0 is for the main database and 1 is for the file that
9594
 
** holds temporary tables and indices.  If TF_Ephemeral is set
9595
 
** then the table is stored in a file that is automatically deleted
9596
 
** when the VDBE cursor to the table is closed.  In this case Table.tnum 
9597
 
** refers VDBE cursor number that holds the table open, not to the root
9598
 
** page number.  Transient tables are used to hold the results of a
9599
 
** sub-query that appears instead of a real table name in the FROM clause 
9600
 
** of a SELECT statement.
9601
 
*/
9602
 
struct Table {
9603
 
  char *zName;         /* Name of the table or view */
9604
 
  int iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
9605
 
  int nCol;            /* Number of columns in this table */
9606
 
  Column *aCol;        /* Information about each column */
9607
 
  Index *pIndex;       /* List of SQL indexes on this table. */
9608
 
  int tnum;            /* Root BTree node for this table (see note above) */
9609
 
  unsigned nRowEst;    /* Estimated rows in table - from sqlite_stat1 table */
9610
 
  Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
9611
 
  u16 nRef;            /* Number of pointers to this Table */
9612
 
  u8 tabFlags;         /* Mask of TF_* values */
9613
 
  u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
9614
 
  FKey *pFKey;         /* Linked list of all foreign keys in this table */
9615
 
  char *zColAff;       /* String defining the affinity of each column */
9616
 
#ifndef SQLITE_OMIT_CHECK
9617
 
  Expr *pCheck;        /* The AND of all CHECK constraints */
9618
 
#endif
9619
 
#ifndef SQLITE_OMIT_ALTERTABLE
9620
 
  int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
9621
 
#endif
9622
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
9623
 
  VTable *pVTable;     /* List of VTable objects. */
9624
 
  int nModuleArg;      /* Number of arguments to the module */
9625
 
  char **azModuleArg;  /* Text of all module args. [0] is module name */
9626
 
#endif
9627
 
  Trigger *pTrigger;   /* List of triggers stored in pSchema */
9628
 
  Schema *pSchema;     /* Schema that contains this table */
9629
 
  Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
9630
 
};
9631
 
 
9632
 
/*
9633
 
** Allowed values for Tabe.tabFlags.
9634
 
*/
9635
 
#define TF_Readonly        0x01    /* Read-only system table */
9636
 
#define TF_Ephemeral       0x02    /* An ephemeral table */
9637
 
#define TF_HasPrimaryKey   0x04    /* Table has a primary key */
9638
 
#define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
9639
 
#define TF_Virtual         0x10    /* Is a virtual table */
9640
 
#define TF_NeedMetadata    0x20    /* aCol[].zType and aCol[].pColl missing */
9641
 
 
9642
 
 
9643
 
 
9644
 
/*
9645
 
** Test to see whether or not a table is a virtual table.  This is
9646
 
** done as a macro so that it will be optimized out when virtual
9647
 
** table support is omitted from the build.
9648
 
*/
9649
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
9650
 
#  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
9651
 
#  define IsHiddenColumn(X) ((X)->isHidden)
9652
 
#else
9653
 
#  define IsVirtual(X)      0
9654
 
#  define IsHiddenColumn(X) 0
9655
 
#endif
9656
 
 
9657
 
/*
9658
 
** Each foreign key constraint is an instance of the following structure.
9659
 
**
9660
 
** A foreign key is associated with two tables.  The "from" table is
9661
 
** the table that contains the REFERENCES clause that creates the foreign
9662
 
** key.  The "to" table is the table that is named in the REFERENCES clause.
9663
 
** Consider this example:
9664
 
**
9665
 
**     CREATE TABLE ex1(
9666
 
**       a INTEGER PRIMARY KEY,
9667
 
**       b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
9668
 
**     );
9669
 
**
9670
 
** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
9671
 
**
9672
 
** Each REFERENCES clause generates an instance of the following structure
9673
 
** which is attached to the from-table.  The to-table need not exist when
9674
 
** the from-table is created.  The existence of the to-table is not checked.
9675
 
*/
9676
 
struct FKey {
9677
 
  Table *pFrom;     /* Table containing the REFERENCES clause (aka: Child) */
9678
 
  FKey *pNextFrom;  /* Next foreign key in pFrom */
9679
 
  char *zTo;        /* Name of table that the key points to (aka: Parent) */
9680
 
  FKey *pNextTo;    /* Next foreign key on table named zTo */
9681
 
  FKey *pPrevTo;    /* Previous foreign key on table named zTo */
9682
 
  int nCol;         /* Number of columns in this key */
9683
 
  /* EV: R-30323-21917 */
9684
 
  u8 isDeferred;    /* True if constraint checking is deferred till COMMIT */
9685
 
  u8 aAction[2];          /* ON DELETE and ON UPDATE actions, respectively */
9686
 
  Trigger *apTrigger[2];  /* Triggers for aAction[] actions */
9687
 
  struct sColMap {  /* Mapping of columns in pFrom to columns in zTo */
9688
 
    int iFrom;         /* Index of column in pFrom */
9689
 
    char *zCol;        /* Name of column in zTo.  If 0 use PRIMARY KEY */
9690
 
  } aCol[1];        /* One entry for each of nCol column s */
9691
 
};
9692
 
 
9693
 
/*
9694
 
** SQLite supports many different ways to resolve a constraint
9695
 
** error.  ROLLBACK processing means that a constraint violation
9696
 
** causes the operation in process to fail and for the current transaction
9697
 
** to be rolled back.  ABORT processing means the operation in process
9698
 
** fails and any prior changes from that one operation are backed out,
9699
 
** but the transaction is not rolled back.  FAIL processing means that
9700
 
** the operation in progress stops and returns an error code.  But prior
9701
 
** changes due to the same operation are not backed out and no rollback
9702
 
** occurs.  IGNORE means that the particular row that caused the constraint
9703
 
** error is not inserted or updated.  Processing continues and no error
9704
 
** is returned.  REPLACE means that preexisting database rows that caused
9705
 
** a UNIQUE constraint violation are removed so that the new insert or
9706
 
** update can proceed.  Processing continues and no error is reported.
9707
 
**
9708
 
** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
9709
 
** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
9710
 
** same as ROLLBACK for DEFERRED keys.  SETNULL means that the foreign
9711
 
** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
9712
 
** referenced table row is propagated into the row that holds the
9713
 
** foreign key.
9714
 
** 
9715
 
** The following symbolic values are used to record which type
9716
 
** of action to take.
9717
 
*/
9718
 
#define OE_None     0   /* There is no constraint to check */
9719
 
#define OE_Rollback 1   /* Fail the operation and rollback the transaction */
9720
 
#define OE_Abort    2   /* Back out changes but do no rollback transaction */
9721
 
#define OE_Fail     3   /* Stop the operation but leave all prior changes */
9722
 
#define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
9723
 
#define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
9724
 
 
9725
 
#define OE_Restrict 6   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
9726
 
#define OE_SetNull  7   /* Set the foreign key value to NULL */
9727
 
#define OE_SetDflt  8   /* Set the foreign key value to its default */
9728
 
#define OE_Cascade  9   /* Cascade the changes */
9729
 
 
9730
 
#define OE_Default  99  /* Do whatever the default action is */
9731
 
 
9732
 
 
9733
 
/*
9734
 
** An instance of the following structure is passed as the first
9735
 
** argument to sqlite3VdbeKeyCompare and is used to control the 
9736
 
** comparison of the two index keys.
9737
 
*/
9738
 
struct KeyInfo {
9739
 
  sqlite3 *db;        /* The database connection */
9740
 
  u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
9741
 
  u16 nField;         /* Number of entries in aColl[] */
9742
 
  u8 *aSortOrder;     /* Sort order for each column.  May be NULL */
9743
 
  CollSeq *aColl[1];  /* Collating sequence for each term of the key */
9744
 
};
9745
 
 
9746
 
/*
9747
 
** An instance of the following structure holds information about a
9748
 
** single index record that has already been parsed out into individual
9749
 
** values.
9750
 
**
9751
 
** A record is an object that contains one or more fields of data.
9752
 
** Records are used to store the content of a table row and to store
9753
 
** the key of an index.  A blob encoding of a record is created by
9754
 
** the OP_MakeRecord opcode of the VDBE and is disassembled by the
9755
 
** OP_Column opcode.
9756
 
**
9757
 
** This structure holds a record that has already been disassembled
9758
 
** into its constituent fields.
9759
 
*/
9760
 
struct UnpackedRecord {
9761
 
  KeyInfo *pKeyInfo;  /* Collation and sort-order information */
9762
 
  u16 nField;         /* Number of entries in apMem[] */
9763
 
  u16 flags;          /* Boolean settings.  UNPACKED_... below */
9764
 
  i64 rowid;          /* Used by UNPACKED_PREFIX_SEARCH */
9765
 
  Mem *aMem;          /* Values */
9766
 
};
9767
 
 
9768
 
/*
9769
 
** Allowed values of UnpackedRecord.flags
9770
 
*/
9771
 
#define UNPACKED_NEED_FREE     0x0001  /* Memory is from sqlite3Malloc() */
9772
 
#define UNPACKED_NEED_DESTROY  0x0002  /* apMem[]s should all be destroyed */
9773
 
#define UNPACKED_IGNORE_ROWID  0x0004  /* Ignore trailing rowid on key1 */
9774
 
#define UNPACKED_INCRKEY       0x0008  /* Make this key an epsilon larger */
9775
 
#define UNPACKED_PREFIX_MATCH  0x0010  /* A prefix match is considered OK */
9776
 
#define UNPACKED_PREFIX_SEARCH 0x0020  /* A prefix match is considered OK */
9777
 
 
9778
 
/*
9779
 
** Each SQL index is represented in memory by an
9780
 
** instance of the following structure.
9781
 
**
9782
 
** The columns of the table that are to be indexed are described
9783
 
** by the aiColumn[] field of this structure.  For example, suppose
9784
 
** we have the following table and index:
9785
 
**
9786
 
**     CREATE TABLE Ex1(c1 int, c2 int, c3 text);
9787
 
**     CREATE INDEX Ex2 ON Ex1(c3,c1);
9788
 
**
9789
 
** In the Table structure describing Ex1, nCol==3 because there are
9790
 
** three columns in the table.  In the Index structure describing
9791
 
** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
9792
 
** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the 
9793
 
** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
9794
 
** The second column to be indexed (c1) has an index of 0 in
9795
 
** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
9796
 
**
9797
 
** The Index.onError field determines whether or not the indexed columns
9798
 
** must be unique and what to do if they are not.  When Index.onError=OE_None,
9799
 
** it means this is not a unique index.  Otherwise it is a unique index
9800
 
** and the value of Index.onError indicate the which conflict resolution 
9801
 
** algorithm to employ whenever an attempt is made to insert a non-unique
9802
 
** element.
9803
 
*/
9804
 
struct Index {
9805
 
  char *zName;     /* Name of this index */
9806
 
  int nColumn;     /* Number of columns in the table used by this index */
9807
 
  int *aiColumn;   /* Which columns are used by this index.  1st is 0 */
9808
 
  unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
9809
 
  Table *pTable;   /* The SQL table being indexed */
9810
 
  int tnum;        /* Page containing root of this index in database file */
9811
 
  u8 onError;      /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
9812
 
  u8 autoIndex;    /* True if is automatically created (ex: by UNIQUE) */
9813
 
  u8 bUnordered;   /* Use this index for == or IN queries only */
9814
 
  char *zColAff;   /* String defining the affinity of each column */
9815
 
  Index *pNext;    /* The next index associated with the same table */
9816
 
  Schema *pSchema; /* Schema containing this index */
9817
 
  u8 *aSortOrder;  /* Array of size Index.nColumn. True==DESC, False==ASC */
9818
 
  char **azColl;   /* Array of collation sequence names for index */
9819
 
  IndexSample *aSample;    /* Array of SQLITE_INDEX_SAMPLES samples */
9820
 
};
9821
 
 
9822
 
/*
9823
 
** Each sample stored in the sqlite_stat2 table is represented in memory 
9824
 
** using a structure of this type.
9825
 
*/
9826
 
struct IndexSample {
9827
 
  union {
9828
 
    char *z;        /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
9829
 
    double r;       /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
9830
 
  } u;
9831
 
  u8 eType;         /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
9832
 
  u8 nByte;         /* Size in byte of text or blob. */
9833
 
};
9834
 
 
9835
 
/*
9836
 
** Each token coming out of the lexer is an instance of
9837
 
** this structure.  Tokens are also used as part of an expression.
9838
 
**
9839
 
** Note if Token.z==0 then Token.dyn and Token.n are undefined and
9840
 
** may contain random values.  Do not make any assumptions about Token.dyn
9841
 
** and Token.n when Token.z==0.
9842
 
*/
9843
 
struct Token {
9844
 
  const char *z;     /* Text of the token.  Not NULL-terminated! */
9845
 
  unsigned int n;    /* Number of characters in this token */
9846
 
};
9847
 
 
9848
 
/*
9849
 
** An instance of this structure contains information needed to generate
9850
 
** code for a SELECT that contains aggregate functions.
9851
 
**
9852
 
** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
9853
 
** pointer to this structure.  The Expr.iColumn field is the index in
9854
 
** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
9855
 
** code for that node.
9856
 
**
9857
 
** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
9858
 
** original Select structure that describes the SELECT statement.  These
9859
 
** fields do not need to be freed when deallocating the AggInfo structure.
9860
 
*/
9861
 
struct AggInfo {
9862
 
  u8 directMode;          /* Direct rendering mode means take data directly
9863
 
                          ** from source tables rather than from accumulators */
9864
 
  u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
9865
 
                          ** than the source table */
9866
 
  int sortingIdx;         /* Cursor number of the sorting index */
9867
 
  ExprList *pGroupBy;     /* The group by clause */
9868
 
  int nSortingColumn;     /* Number of columns in the sorting index */
9869
 
  struct AggInfo_col {    /* For each column used in source tables */
9870
 
    Table *pTab;             /* Source table */
9871
 
    int iTable;              /* Cursor number of the source table */
9872
 
    int iColumn;             /* Column number within the source table */
9873
 
    int iSorterColumn;       /* Column number in the sorting index */
9874
 
    int iMem;                /* Memory location that acts as accumulator */
9875
 
    Expr *pExpr;             /* The original expression */
9876
 
  } *aCol;
9877
 
  int nColumn;            /* Number of used entries in aCol[] */
9878
 
  int nColumnAlloc;       /* Number of slots allocated for aCol[] */
9879
 
  int nAccumulator;       /* Number of columns that show through to the output.
9880
 
                          ** Additional columns are used only as parameters to
9881
 
                          ** aggregate functions */
9882
 
  struct AggInfo_func {   /* For each aggregate function */
9883
 
    Expr *pExpr;             /* Expression encoding the function */
9884
 
    FuncDef *pFunc;          /* The aggregate function implementation */
9885
 
    int iMem;                /* Memory location that acts as accumulator */
9886
 
    int iDistinct;           /* Ephemeral table used to enforce DISTINCT */
9887
 
  } *aFunc;
9888
 
  int nFunc;              /* Number of entries in aFunc[] */
9889
 
  int nFuncAlloc;         /* Number of slots allocated for aFunc[] */
9890
 
};
9891
 
 
9892
 
/*
9893
 
** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
9894
 
** Usually it is 16-bits.  But if SQLITE_MAX_VARIABLE_NUMBER is greater
9895
 
** than 32767 we have to make it 32-bit.  16-bit is preferred because
9896
 
** it uses less memory in the Expr object, which is a big memory user
9897
 
** in systems with lots of prepared statements.  And few applications
9898
 
** need more than about 10 or 20 variables.  But some extreme users want
9899
 
** to have prepared statements with over 32767 variables, and for them
9900
 
** the option is available (at compile-time).
9901
 
*/
9902
 
#if SQLITE_MAX_VARIABLE_NUMBER<=32767
9903
 
typedef i16 ynVar;
9904
 
#else
9905
 
typedef int ynVar;
9906
 
#endif
9907
 
 
9908
 
/*
9909
 
** Each node of an expression in the parse tree is an instance
9910
 
** of this structure.
9911
 
**
9912
 
** Expr.op is the opcode. The integer parser token codes are reused
9913
 
** as opcodes here. For example, the parser defines TK_GE to be an integer
9914
 
** code representing the ">=" operator. This same integer code is reused
9915
 
** to represent the greater-than-or-equal-to operator in the expression
9916
 
** tree.
9917
 
**
9918
 
** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, 
9919
 
** or TK_STRING), then Expr.token contains the text of the SQL literal. If
9920
 
** the expression is a variable (TK_VARIABLE), then Expr.token contains the 
9921
 
** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
9922
 
** then Expr.token contains the name of the function.
9923
 
**
9924
 
** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
9925
 
** binary operator. Either or both may be NULL.
9926
 
**
9927
 
** Expr.x.pList is a list of arguments if the expression is an SQL function,
9928
 
** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
9929
 
** Expr.x.pSelect is used if the expression is a sub-select or an expression of
9930
 
** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
9931
 
** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is 
9932
 
** valid.
9933
 
**
9934
 
** An expression of the form ID or ID.ID refers to a column in a table.
9935
 
** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
9936
 
** the integer cursor number of a VDBE cursor pointing to that table and
9937
 
** Expr.iColumn is the column number for the specific column.  If the
9938
 
** expression is used as a result in an aggregate SELECT, then the
9939
 
** value is also stored in the Expr.iAgg column in the aggregate so that
9940
 
** it can be accessed after all aggregates are computed.
9941
 
**
9942
 
** If the expression is an unbound variable marker (a question mark 
9943
 
** character '?' in the original SQL) then the Expr.iTable holds the index 
9944
 
** number for that variable.
9945
 
**
9946
 
** If the expression is a subquery then Expr.iColumn holds an integer
9947
 
** register number containing the result of the subquery.  If the
9948
 
** subquery gives a constant result, then iTable is -1.  If the subquery
9949
 
** gives a different answer at different times during statement processing
9950
 
** then iTable is the address of a subroutine that computes the subquery.
9951
 
**
9952
 
** If the Expr is of type OP_Column, and the table it is selecting from
9953
 
** is a disk table or the "old.*" pseudo-table, then pTab points to the
9954
 
** corresponding table definition.
9955
 
**
9956
 
** ALLOCATION NOTES:
9957
 
**
9958
 
** Expr objects can use a lot of memory space in database schema.  To
9959
 
** help reduce memory requirements, sometimes an Expr object will be
9960
 
** truncated.  And to reduce the number of memory allocations, sometimes
9961
 
** two or more Expr objects will be stored in a single memory allocation,
9962
 
** together with Expr.zToken strings.
9963
 
**
9964
 
** If the EP_Reduced and EP_TokenOnly flags are set when
9965
 
** an Expr object is truncated.  When EP_Reduced is set, then all
9966
 
** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
9967
 
** are contained within the same memory allocation.  Note, however, that
9968
 
** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
9969
 
** allocated, regardless of whether or not EP_Reduced is set.
9970
 
*/
9971
 
struct Expr {
9972
 
  u8 op;                 /* Operation performed by this node */
9973
 
  char affinity;         /* The affinity of the column or 0 if not a column */
9974
 
  u16 flags;             /* Various flags.  EP_* See below */
9975
 
  union {
9976
 
    char *zToken;          /* Token value. Zero terminated and dequoted */
9977
 
    int iValue;            /* Non-negative integer value if EP_IntValue */
9978
 
  } u;
9979
 
 
9980
 
  /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
9981
 
  ** space is allocated for the fields below this point. An attempt to
9982
 
  ** access them will result in a segfault or malfunction. 
9983
 
  *********************************************************************/
9984
 
 
9985
 
  Expr *pLeft;           /* Left subnode */
9986
 
  Expr *pRight;          /* Right subnode */
9987
 
  union {
9988
 
    ExprList *pList;     /* Function arguments or in "<expr> IN (<expr-list)" */
9989
 
    Select *pSelect;     /* Used for sub-selects and "<expr> IN (<select>)" */
9990
 
  } x;
9991
 
  CollSeq *pColl;        /* The collation type of the column or 0 */
9992
 
 
9993
 
  /* If the EP_Reduced flag is set in the Expr.flags mask, then no
9994
 
  ** space is allocated for the fields below this point. An attempt to
9995
 
  ** access them will result in a segfault or malfunction.
9996
 
  *********************************************************************/
9997
 
 
9998
 
  int iTable;            /* TK_COLUMN: cursor number of table holding column
9999
 
                         ** TK_REGISTER: register number
10000
 
                         ** TK_TRIGGER: 1 -> new, 0 -> old */
10001
 
  ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
10002
 
                         ** TK_VARIABLE: variable number (always >= 1). */
10003
 
  i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
10004
 
  i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
10005
 
  u8 flags2;             /* Second set of flags.  EP2_... */
10006
 
  u8 op2;                /* If a TK_REGISTER, the original value of Expr.op */
10007
 
  AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
10008
 
  Table *pTab;           /* Table for TK_COLUMN expressions. */
10009
 
#if SQLITE_MAX_EXPR_DEPTH>0
10010
 
  int nHeight;           /* Height of the tree headed by this node */
10011
 
#endif
10012
 
};
10013
 
 
10014
 
/*
10015
 
** The following are the meanings of bits in the Expr.flags field.
10016
 
*/
10017
 
#define EP_FromJoin   0x0001  /* Originated in ON or USING clause of a join */
10018
 
#define EP_Agg        0x0002  /* Contains one or more aggregate functions */
10019
 
#define EP_Resolved   0x0004  /* IDs have been resolved to COLUMNs */
10020
 
#define EP_Error      0x0008  /* Expression contains one or more errors */
10021
 
#define EP_Distinct   0x0010  /* Aggregate function with DISTINCT keyword */
10022
 
#define EP_VarSelect  0x0020  /* pSelect is correlated, not constant */
10023
 
#define EP_DblQuoted  0x0040  /* token.z was originally in "..." */
10024
 
#define EP_InfixFunc  0x0080  /* True for an infix function: LIKE, GLOB, etc */
10025
 
#define EP_ExpCollate 0x0100  /* Collating sequence specified explicitly */
10026
 
#define EP_FixedDest  0x0200  /* Result needed in a specific register */
10027
 
#define EP_IntValue   0x0400  /* Integer value contained in u.iValue */
10028
 
#define EP_xIsSelect  0x0800  /* x.pSelect is valid (otherwise x.pList is) */
10029
 
 
10030
 
#define EP_Reduced    0x1000  /* Expr struct is EXPR_REDUCEDSIZE bytes only */
10031
 
#define EP_TokenOnly  0x2000  /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
10032
 
#define EP_Static     0x4000  /* Held in memory not obtained from malloc() */
10033
 
 
10034
 
/*
10035
 
** The following are the meanings of bits in the Expr.flags2 field.
10036
 
*/
10037
 
#define EP2_MallocedToken  0x0001  /* Need to sqlite3DbFree() Expr.zToken */
10038
 
#define EP2_Irreducible    0x0002  /* Cannot EXPRDUP_REDUCE this Expr */
10039
 
 
10040
 
/*
10041
 
** The pseudo-routine sqlite3ExprSetIrreducible sets the EP2_Irreducible
10042
 
** flag on an expression structure.  This flag is used for VV&A only.  The
10043
 
** routine is implemented as a macro that only works when in debugging mode,
10044
 
** so as not to burden production code.
10045
 
*/
10046
 
#ifdef SQLITE_DEBUG
10047
 
# define ExprSetIrreducible(X)  (X)->flags2 |= EP2_Irreducible
10048
 
#else
10049
 
# define ExprSetIrreducible(X)
10050
 
#endif
10051
 
 
10052
 
/*
10053
 
** These macros can be used to test, set, or clear bits in the 
10054
 
** Expr.flags field.
10055
 
*/
10056
 
#define ExprHasProperty(E,P)     (((E)->flags&(P))==(P))
10057
 
#define ExprHasAnyProperty(E,P)  (((E)->flags&(P))!=0)
10058
 
#define ExprSetProperty(E,P)     (E)->flags|=(P)
10059
 
#define ExprClearProperty(E,P)   (E)->flags&=~(P)
10060
 
 
10061
 
/*
10062
 
** Macros to determine the number of bytes required by a normal Expr 
10063
 
** struct, an Expr struct with the EP_Reduced flag set in Expr.flags 
10064
 
** and an Expr struct with the EP_TokenOnly flag set.
10065
 
*/
10066
 
#define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
10067
 
#define EXPR_REDUCEDSIZE        offsetof(Expr,iTable)  /* Common features */
10068
 
#define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
10069
 
 
10070
 
/*
10071
 
** Flags passed to the sqlite3ExprDup() function. See the header comment 
10072
 
** above sqlite3ExprDup() for details.
10073
 
*/
10074
 
#define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
10075
 
 
10076
 
/*
10077
 
** A list of expressions.  Each expression may optionally have a
10078
 
** name.  An expr/name combination can be used in several ways, such
10079
 
** as the list of "expr AS ID" fields following a "SELECT" or in the
10080
 
** list of "ID = expr" items in an UPDATE.  A list of expressions can
10081
 
** also be used as the argument to a function, in which case the a.zName
10082
 
** field is not used.
10083
 
*/
10084
 
struct ExprList {
10085
 
  int nExpr;             /* Number of expressions on the list */
10086
 
  int nAlloc;            /* Number of entries allocated below */
10087
 
  int iECursor;          /* VDBE Cursor associated with this ExprList */
10088
 
  struct ExprList_item {
10089
 
    Expr *pExpr;           /* The list of expressions */
10090
 
    char *zName;           /* Token associated with this expression */
10091
 
    char *zSpan;           /* Original text of the expression */
10092
 
    u8 sortOrder;          /* 1 for DESC or 0 for ASC */
10093
 
    u8 done;               /* A flag to indicate when processing is finished */
10094
 
    u16 iCol;              /* For ORDER BY, column number in result set */
10095
 
    u16 iAlias;            /* Index into Parse.aAlias[] for zName */
10096
 
  } *a;                  /* One entry for each expression */
10097
 
};
10098
 
 
10099
 
/*
10100
 
** An instance of this structure is used by the parser to record both
10101
 
** the parse tree for an expression and the span of input text for an
10102
 
** expression.
10103
 
*/
10104
 
struct ExprSpan {
10105
 
  Expr *pExpr;          /* The expression parse tree */
10106
 
  const char *zStart;   /* First character of input text */
10107
 
  const char *zEnd;     /* One character past the end of input text */
10108
 
};
10109
 
 
10110
 
/*
10111
 
** An instance of this structure can hold a simple list of identifiers,
10112
 
** such as the list "a,b,c" in the following statements:
10113
 
**
10114
 
**      INSERT INTO t(a,b,c) VALUES ...;
10115
 
**      CREATE INDEX idx ON t(a,b,c);
10116
 
**      CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
10117
 
**
10118
 
** The IdList.a.idx field is used when the IdList represents the list of
10119
 
** column names after a table name in an INSERT statement.  In the statement
10120
 
**
10121
 
**     INSERT INTO t(a,b,c) ...
10122
 
**
10123
 
** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
10124
 
*/
10125
 
struct IdList {
10126
 
  struct IdList_item {
10127
 
    char *zName;      /* Name of the identifier */
10128
 
    int idx;          /* Index in some Table.aCol[] of a column named zName */
10129
 
  } *a;
10130
 
  int nId;         /* Number of identifiers on the list */
10131
 
  int nAlloc;      /* Number of entries allocated for a[] below */
10132
 
};
10133
 
 
10134
 
/*
10135
 
** The bitmask datatype defined below is used for various optimizations.
10136
 
**
10137
 
** Changing this from a 64-bit to a 32-bit type limits the number of
10138
 
** tables in a join to 32 instead of 64.  But it also reduces the size
10139
 
** of the library by 738 bytes on ix86.
10140
 
*/
10141
 
typedef u64 Bitmask;
10142
 
 
10143
 
/*
10144
 
** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
10145
 
*/
10146
 
#define BMS  ((int)(sizeof(Bitmask)*8))
10147
 
 
10148
 
/*
10149
 
** The following structure describes the FROM clause of a SELECT statement.
10150
 
** Each table or subquery in the FROM clause is a separate element of
10151
 
** the SrcList.a[] array.
10152
 
**
10153
 
** With the addition of multiple database support, the following structure
10154
 
** can also be used to describe a particular table such as the table that
10155
 
** is modified by an INSERT, DELETE, or UPDATE statement.  In standard SQL,
10156
 
** such a table must be a simple name: ID.  But in SQLite, the table can
10157
 
** now be identified by a database name, a dot, then the table name: ID.ID.
10158
 
**
10159
 
** The jointype starts out showing the join type between the current table
10160
 
** and the next table on the list.  The parser builds the list this way.
10161
 
** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
10162
 
** jointype expresses the join between the table and the previous table.
10163
 
**
10164
 
** In the colUsed field, the high-order bit (bit 63) is set if the table
10165
 
** contains more than 63 columns and the 64-th or later column is used.
10166
 
*/
10167
 
struct SrcList {
10168
 
  i16 nSrc;        /* Number of tables or subqueries in the FROM clause */
10169
 
  i16 nAlloc;      /* Number of entries allocated in a[] below */
10170
 
  struct SrcList_item {
10171
 
    char *zDatabase;  /* Name of database holding this table */
10172
 
    char *zName;      /* Name of the table */
10173
 
    char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
10174
 
    Table *pTab;      /* An SQL table corresponding to zName */
10175
 
    Select *pSelect;  /* A SELECT statement used in place of a table name */
10176
 
    u8 isPopulated;   /* Temporary table associated with SELECT is populated */
10177
 
    u8 jointype;      /* Type of join between this able and the previous */
10178
 
    u8 notIndexed;    /* True if there is a NOT INDEXED clause */
10179
 
#ifndef SQLITE_OMIT_EXPLAIN
10180
 
    u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
10181
 
#endif
10182
 
    int iCursor;      /* The VDBE cursor number used to access this table */
10183
 
    Expr *pOn;        /* The ON clause of a join */
10184
 
    IdList *pUsing;   /* The USING clause of a join */
10185
 
    Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
10186
 
    char *zIndex;     /* Identifier from "INDEXED BY <zIndex>" clause */
10187
 
    Index *pIndex;    /* Index structure corresponding to zIndex, if any */
10188
 
  } a[1];             /* One entry for each identifier on the list */
10189
 
};
10190
 
 
10191
 
/*
10192
 
** Permitted values of the SrcList.a.jointype field
10193
 
*/
10194
 
#define JT_INNER     0x0001    /* Any kind of inner or cross join */
10195
 
#define JT_CROSS     0x0002    /* Explicit use of the CROSS keyword */
10196
 
#define JT_NATURAL   0x0004    /* True for a "natural" join */
10197
 
#define JT_LEFT      0x0008    /* Left outer join */
10198
 
#define JT_RIGHT     0x0010    /* Right outer join */
10199
 
#define JT_OUTER     0x0020    /* The "OUTER" keyword is present */
10200
 
#define JT_ERROR     0x0040    /* unknown or unsupported join type */
10201
 
 
10202
 
 
10203
 
/*
10204
 
** A WherePlan object holds information that describes a lookup
10205
 
** strategy.
10206
 
**
10207
 
** This object is intended to be opaque outside of the where.c module.
10208
 
** It is included here only so that that compiler will know how big it
10209
 
** is.  None of the fields in this object should be used outside of
10210
 
** the where.c module.
10211
 
**
10212
 
** Within the union, pIdx is only used when wsFlags&WHERE_INDEXED is true.
10213
 
** pTerm is only used when wsFlags&WHERE_MULTI_OR is true.  And pVtabIdx
10214
 
** is only used when wsFlags&WHERE_VIRTUALTABLE is true.  It is never the
10215
 
** case that more than one of these conditions is true.
10216
 
*/
10217
 
struct WherePlan {
10218
 
  u32 wsFlags;                   /* WHERE_* flags that describe the strategy */
10219
 
  u32 nEq;                       /* Number of == constraints */
10220
 
  double nRow;                   /* Estimated number of rows (for EQP) */
10221
 
  union {
10222
 
    Index *pIdx;                   /* Index when WHERE_INDEXED is true */
10223
 
    struct WhereTerm *pTerm;       /* WHERE clause term for OR-search */
10224
 
    sqlite3_index_info *pVtabIdx;  /* Virtual table index to use */
10225
 
  } u;
10226
 
};
10227
 
 
10228
 
/*
10229
 
** For each nested loop in a WHERE clause implementation, the WhereInfo
10230
 
** structure contains a single instance of this structure.  This structure
10231
 
** is intended to be private the the where.c module and should not be
10232
 
** access or modified by other modules.
10233
 
**
10234
 
** The pIdxInfo field is used to help pick the best index on a
10235
 
** virtual table.  The pIdxInfo pointer contains indexing
10236
 
** information for the i-th table in the FROM clause before reordering.
10237
 
** All the pIdxInfo pointers are freed by whereInfoFree() in where.c.
10238
 
** All other information in the i-th WhereLevel object for the i-th table
10239
 
** after FROM clause ordering.
10240
 
*/
10241
 
struct WhereLevel {
10242
 
  WherePlan plan;       /* query plan for this element of the FROM clause */
10243
 
  int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
10244
 
  int iTabCur;          /* The VDBE cursor used to access the table */
10245
 
  int iIdxCur;          /* The VDBE cursor used to access pIdx */
10246
 
  int addrBrk;          /* Jump here to break out of the loop */
10247
 
  int addrNxt;          /* Jump here to start the next IN combination */
10248
 
  int addrCont;         /* Jump here to continue with the next loop cycle */
10249
 
  int addrFirst;        /* First instruction of interior of the loop */
10250
 
  u8 iFrom;             /* Which entry in the FROM clause */
10251
 
  u8 op, p5;            /* Opcode and P5 of the opcode that ends the loop */
10252
 
  int p1, p2;           /* Operands of the opcode used to ends the loop */
10253
 
  union {               /* Information that depends on plan.wsFlags */
10254
 
    struct {
10255
 
      int nIn;              /* Number of entries in aInLoop[] */
10256
 
      struct InLoop {
10257
 
        int iCur;              /* The VDBE cursor used by this IN operator */
10258
 
        int addrInTop;         /* Top of the IN loop */
10259
 
      } *aInLoop;           /* Information about each nested IN operator */
10260
 
    } in;                 /* Used when plan.wsFlags&WHERE_IN_ABLE */
10261
 
  } u;
10262
 
 
10263
 
  /* The following field is really not part of the current level.  But
10264
 
  ** we need a place to cache virtual table index information for each
10265
 
  ** virtual table in the FROM clause and the WhereLevel structure is
10266
 
  ** a convenient place since there is one WhereLevel for each FROM clause
10267
 
  ** element.
10268
 
  */
10269
 
  sqlite3_index_info *pIdxInfo;  /* Index info for n-th source table */
10270
 
};
10271
 
 
10272
 
/*
10273
 
** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
10274
 
** and the WhereInfo.wctrlFlags member.
10275
 
*/
10276
 
#define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
10277
 
#define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
10278
 
#define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
10279
 
#define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
10280
 
#define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
10281
 
#define WHERE_OMIT_OPEN        0x0010 /* Table cursors are already open */
10282
 
#define WHERE_OMIT_CLOSE       0x0020 /* Omit close of table & index cursors */
10283
 
#define WHERE_FORCE_TABLE      0x0040 /* Do not use an index-only search */
10284
 
#define WHERE_ONETABLE_ONLY    0x0080 /* Only code the 1st table in pTabList */
10285
 
 
10286
 
/*
10287
 
** The WHERE clause processing routine has two halves.  The
10288
 
** first part does the start of the WHERE loop and the second
10289
 
** half does the tail of the WHERE loop.  An instance of
10290
 
** this structure is returned by the first half and passed
10291
 
** into the second half to give some continuity.
10292
 
*/
10293
 
struct WhereInfo {
10294
 
  Parse *pParse;       /* Parsing and code generating context */
10295
 
  u16 wctrlFlags;      /* Flags originally passed to sqlite3WhereBegin() */
10296
 
  u8 okOnePass;        /* Ok to use one-pass algorithm for UPDATE or DELETE */
10297
 
  u8 untestedTerms;    /* Not all WHERE terms resolved by outer loop */
10298
 
  SrcList *pTabList;             /* List of tables in the join */
10299
 
  int iTop;                      /* The very beginning of the WHERE loop */
10300
 
  int iContinue;                 /* Jump here to continue with next record */
10301
 
  int iBreak;                    /* Jump here to break out of the loop */
10302
 
  int nLevel;                    /* Number of nested loop */
10303
 
  struct WhereClause *pWC;       /* Decomposition of the WHERE clause */
10304
 
  double savedNQueryLoop;        /* pParse->nQueryLoop outside the WHERE loop */
10305
 
  double nRowOut;                /* Estimated number of output rows */
10306
 
  WhereLevel a[1];               /* Information about each nest loop in WHERE */
10307
 
};
10308
 
 
10309
 
/*
10310
 
** A NameContext defines a context in which to resolve table and column
10311
 
** names.  The context consists of a list of tables (the pSrcList) field and
10312
 
** a list of named expression (pEList).  The named expression list may
10313
 
** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
10314
 
** to the table being operated on by INSERT, UPDATE, or DELETE.  The
10315
 
** pEList corresponds to the result set of a SELECT and is NULL for
10316
 
** other statements.
10317
 
**
10318
 
** NameContexts can be nested.  When resolving names, the inner-most 
10319
 
** context is searched first.  If no match is found, the next outer
10320
 
** context is checked.  If there is still no match, the next context
10321
 
** is checked.  This process continues until either a match is found
10322
 
** or all contexts are check.  When a match is found, the nRef member of
10323
 
** the context containing the match is incremented. 
10324
 
**
10325
 
** Each subquery gets a new NameContext.  The pNext field points to the
10326
 
** NameContext in the parent query.  Thus the process of scanning the
10327
 
** NameContext list corresponds to searching through successively outer
10328
 
** subqueries looking for a match.
10329
 
*/
10330
 
struct NameContext {
10331
 
  Parse *pParse;       /* The parser */
10332
 
  SrcList *pSrcList;   /* One or more tables used to resolve names */
10333
 
  ExprList *pEList;    /* Optional list of named expressions */
10334
 
  int nRef;            /* Number of names resolved by this context */
10335
 
  int nErr;            /* Number of errors encountered while resolving names */
10336
 
  u8 allowAgg;         /* Aggregate functions allowed here */
10337
 
  u8 hasAgg;           /* True if aggregates are seen */
10338
 
  u8 isCheck;          /* True if resolving names in a CHECK constraint */
10339
 
  int nDepth;          /* Depth of subquery recursion. 1 for no recursion */
10340
 
  AggInfo *pAggInfo;   /* Information about aggregates at this level */
10341
 
  NameContext *pNext;  /* Next outer name context.  NULL for outermost */
10342
 
};
10343
 
 
10344
 
/*
10345
 
** An instance of the following structure contains all information
10346
 
** needed to generate code for a single SELECT statement.
10347
 
**
10348
 
** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
10349
 
** If there is a LIMIT clause, the parser sets nLimit to the value of the
10350
 
** limit and nOffset to the value of the offset (or 0 if there is not
10351
 
** offset).  But later on, nLimit and nOffset become the memory locations
10352
 
** in the VDBE that record the limit and offset counters.
10353
 
**
10354
 
** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
10355
 
** These addresses must be stored so that we can go back and fill in
10356
 
** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
10357
 
** the number of columns in P2 can be computed at the same time
10358
 
** as the OP_OpenEphm instruction is coded because not
10359
 
** enough information about the compound query is known at that point.
10360
 
** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
10361
 
** for the result set.  The KeyInfo for addrOpenTran[2] contains collating
10362
 
** sequences for the ORDER BY clause.
10363
 
*/
10364
 
struct Select {
10365
 
  ExprList *pEList;      /* The fields of the result */
10366
 
  u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
10367
 
  char affinity;         /* MakeRecord with this affinity for SRT_Set */
10368
 
  u16 selFlags;          /* Various SF_* values */
10369
 
  SrcList *pSrc;         /* The FROM clause */
10370
 
  Expr *pWhere;          /* The WHERE clause */
10371
 
  ExprList *pGroupBy;    /* The GROUP BY clause */
10372
 
  Expr *pHaving;         /* The HAVING clause */
10373
 
  ExprList *pOrderBy;    /* The ORDER BY clause */
10374
 
  Select *pPrior;        /* Prior select in a compound select statement */
10375
 
  Select *pNext;         /* Next select to the left in a compound */
10376
 
  Select *pRightmost;    /* Right-most select in a compound select statement */
10377
 
  Expr *pLimit;          /* LIMIT expression. NULL means not used. */
10378
 
  Expr *pOffset;         /* OFFSET expression. NULL means not used. */
10379
 
  int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
10380
 
  int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
10381
 
  double nSelectRow;     /* Estimated number of result rows */
10382
 
};
10383
 
 
10384
 
/*
10385
 
** Allowed values for Select.selFlags.  The "SF" prefix stands for
10386
 
** "Select Flag".
10387
 
*/
10388
 
#define SF_Distinct        0x0001  /* Output should be DISTINCT */
10389
 
#define SF_Resolved        0x0002  /* Identifiers have been resolved */
10390
 
#define SF_Aggregate       0x0004  /* Contains aggregate functions */
10391
 
#define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
10392
 
#define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
10393
 
#define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
10394
 
 
10395
 
 
10396
 
/*
10397
 
** The results of a select can be distributed in several ways.  The
10398
 
** "SRT" prefix means "SELECT Result Type".
10399
 
*/
10400
 
#define SRT_Union        1  /* Store result as keys in an index */
10401
 
#define SRT_Except       2  /* Remove result from a UNION index */
10402
 
#define SRT_Exists       3  /* Store 1 if the result is not empty */
10403
 
#define SRT_Discard      4  /* Do not save the results anywhere */
10404
 
 
10405
 
/* The ORDER BY clause is ignored for all of the above */
10406
 
#define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
10407
 
 
10408
 
#define SRT_Output       5  /* Output each row of result */
10409
 
#define SRT_Mem          6  /* Store result in a memory cell */
10410
 
#define SRT_Set          7  /* Store results as keys in an index */
10411
 
#define SRT_Table        8  /* Store result as data with an automatic rowid */
10412
 
#define SRT_EphemTab     9  /* Create transient tab and store like SRT_Table */
10413
 
#define SRT_Coroutine   10  /* Generate a single row of result */
10414
 
 
10415
 
/*
10416
 
** A structure used to customize the behavior of sqlite3Select(). See
10417
 
** comments above sqlite3Select() for details.
10418
 
*/
10419
 
typedef struct SelectDest SelectDest;
10420
 
struct SelectDest {
10421
 
  u8 eDest;         /* How to dispose of the results */
10422
 
  u8 affinity;      /* Affinity used when eDest==SRT_Set */
10423
 
  int iParm;        /* A parameter used by the eDest disposal method */
10424
 
  int iMem;         /* Base register where results are written */
10425
 
  int nMem;         /* Number of registers allocated */
10426
 
};
10427
 
 
10428
 
/*
10429
 
** During code generation of statements that do inserts into AUTOINCREMENT 
10430
 
** tables, the following information is attached to the Table.u.autoInc.p
10431
 
** pointer of each autoincrement table to record some side information that
10432
 
** the code generator needs.  We have to keep per-table autoincrement
10433
 
** information in case inserts are down within triggers.  Triggers do not
10434
 
** normally coordinate their activities, but we do need to coordinate the
10435
 
** loading and saving of autoincrement information.
10436
 
*/
10437
 
struct AutoincInfo {
10438
 
  AutoincInfo *pNext;   /* Next info block in a list of them all */
10439
 
  Table *pTab;          /* Table this info block refers to */
10440
 
  int iDb;              /* Index in sqlite3.aDb[] of database holding pTab */
10441
 
  int regCtr;           /* Memory register holding the rowid counter */
10442
 
};
10443
 
 
10444
 
/*
10445
 
** Size of the column cache
10446
 
*/
10447
 
#ifndef SQLITE_N_COLCACHE
10448
 
# define SQLITE_N_COLCACHE 10
10449
 
#endif
10450
 
 
10451
 
/*
10452
 
** At least one instance of the following structure is created for each 
10453
 
** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
10454
 
** statement. All such objects are stored in the linked list headed at
10455
 
** Parse.pTriggerPrg and deleted once statement compilation has been
10456
 
** completed.
10457
 
**
10458
 
** A Vdbe sub-program that implements the body and WHEN clause of trigger
10459
 
** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
10460
 
** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
10461
 
** The Parse.pTriggerPrg list never contains two entries with the same
10462
 
** values for both pTrigger and orconf.
10463
 
**
10464
 
** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
10465
 
** accessed (or set to 0 for triggers fired as a result of INSERT 
10466
 
** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
10467
 
** a mask of new.* columns used by the program.
10468
 
*/
10469
 
struct TriggerPrg {
10470
 
  Trigger *pTrigger;      /* Trigger this program was coded from */
10471
 
  int orconf;             /* Default ON CONFLICT policy */
10472
 
  SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
10473
 
  u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
10474
 
  TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
10475
 
};
10476
 
 
10477
 
/*
10478
 
** The yDbMask datatype for the bitmask of all attached databases.
10479
 
*/
10480
 
#if SQLITE_MAX_ATTACHED>30
10481
 
  typedef sqlite3_uint64 yDbMask;
10482
 
#else
10483
 
  typedef unsigned int yDbMask;
10484
 
#endif
10485
 
 
10486
 
/*
10487
 
** An SQL parser context.  A copy of this structure is passed through
10488
 
** the parser and down into all the parser action routine in order to
10489
 
** carry around information that is global to the entire parse.
10490
 
**
10491
 
** The structure is divided into two parts.  When the parser and code
10492
 
** generate call themselves recursively, the first part of the structure
10493
 
** is constant but the second part is reset at the beginning and end of
10494
 
** each recursion.
10495
 
**
10496
 
** The nTableLock and aTableLock variables are only used if the shared-cache 
10497
 
** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
10498
 
** used to store the set of table-locks required by the statement being
10499
 
** compiled. Function sqlite3TableLock() is used to add entries to the
10500
 
** list.
10501
 
*/
10502
 
struct Parse {
10503
 
  sqlite3 *db;         /* The main database structure */
10504
 
  int rc;              /* Return code from execution */
10505
 
  char *zErrMsg;       /* An error message */
10506
 
  Vdbe *pVdbe;         /* An engine for executing database bytecode */
10507
 
  u8 colNamesSet;      /* TRUE after OP_ColumnName has been issued to pVdbe */
10508
 
  u8 nameClash;        /* A permanent table name clashes with temp table name */
10509
 
  u8 checkSchema;      /* Causes schema cookie check after an error */
10510
 
  u8 nested;           /* Number of nested calls to the parser/code generator */
10511
 
  u8 parseError;       /* True after a parsing error.  Ticket #1794 */
10512
 
  u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
10513
 
  u8 nTempInUse;       /* Number of aTempReg[] currently checked out */
10514
 
  int aTempReg[8];     /* Holding area for temporary registers */
10515
 
  int nRangeReg;       /* Size of the temporary register block */
10516
 
  int iRangeReg;       /* First register in temporary register block */
10517
 
  int nErr;            /* Number of errors seen */
10518
 
  int nTab;            /* Number of previously allocated VDBE cursors */
10519
 
  int nMem;            /* Number of memory cells used so far */
10520
 
  int nSet;            /* Number of sets used so far */
10521
 
  int ckBase;          /* Base register of data during check constraints */
10522
 
  int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
10523
 
  int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
10524
 
  u8 nColCache;        /* Number of entries in the column cache */
10525
 
  u8 iColCache;        /* Next entry of the cache to replace */
10526
 
  struct yColCache {
10527
 
    int iTable;           /* Table cursor number */
10528
 
    int iColumn;          /* Table column number */
10529
 
    u8 tempReg;           /* iReg is a temp register that needs to be freed */
10530
 
    int iLevel;           /* Nesting level */
10531
 
    int iReg;             /* Reg with value of this column. 0 means none. */
10532
 
    int lru;              /* Least recently used entry has the smallest value */
10533
 
  } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
10534
 
  yDbMask writeMask;   /* Start a write transaction on these databases */
10535
 
  yDbMask cookieMask;  /* Bitmask of schema verified databases */
10536
 
  u8 isMultiWrite;     /* True if statement may affect/insert multiple rows */
10537
 
  u8 mayAbort;         /* True if statement may throw an ABORT exception */
10538
 
  int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
10539
 
  int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
10540
 
#ifndef SQLITE_OMIT_SHARED_CACHE
10541
 
  int nTableLock;        /* Number of locks in aTableLock */
10542
 
  TableLock *aTableLock; /* Required table locks for shared-cache mode */
10543
 
#endif
10544
 
  int regRowid;        /* Register holding rowid of CREATE TABLE entry */
10545
 
  int regRoot;         /* Register holding root page number for new objects */
10546
 
  AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
10547
 
  int nMaxArg;         /* Max args passed to user function by sub-program */
10548
 
 
10549
 
  /* Information used while coding trigger programs. */
10550
 
  Parse *pToplevel;    /* Parse structure for main program (or NULL) */
10551
 
  Table *pTriggerTab;  /* Table triggers are being coded for */
10552
 
  u32 oldmask;         /* Mask of old.* columns referenced */
10553
 
  u32 newmask;         /* Mask of new.* columns referenced */
10554
 
  u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
10555
 
  u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
10556
 
  u8 disableTriggers;  /* True to disable triggers */
10557
 
  double nQueryLoop;   /* Estimated number of iterations of a query */
10558
 
 
10559
 
  /* Above is constant between recursions.  Below is reset before and after
10560
 
  ** each recursion */
10561
 
 
10562
 
  int nVar;            /* Number of '?' variables seen in the SQL so far */
10563
 
  int nVarExpr;        /* Number of used slots in apVarExpr[] */
10564
 
  int nVarExprAlloc;   /* Number of allocated slots in apVarExpr[] */
10565
 
  Expr **apVarExpr;    /* Pointers to :aaa and $aaaa wildcard expressions */
10566
 
  Vdbe *pReprepare;    /* VM being reprepared (sqlite3Reprepare()) */
10567
 
  int nAlias;          /* Number of aliased result set columns */
10568
 
  int nAliasAlloc;     /* Number of allocated slots for aAlias[] */
10569
 
  int *aAlias;         /* Register used to hold aliased result */
10570
 
  u8 explain;          /* True if the EXPLAIN flag is found on the query */
10571
 
  Token sNameToken;    /* Token with unqualified schema object name */
10572
 
  Token sLastToken;    /* The last token parsed */
10573
 
  const char *zTail;   /* All SQL text past the last semicolon parsed */
10574
 
  Table *pNewTable;    /* A table being constructed by CREATE TABLE */
10575
 
  Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
10576
 
  const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
10577
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
10578
 
  Token sArg;                /* Complete text of a module argument */
10579
 
  u8 declareVtab;            /* True if inside sqlite3_declare_vtab() */
10580
 
  int nVtabLock;             /* Number of virtual tables to lock */
10581
 
  Table **apVtabLock;        /* Pointer to virtual tables needing locking */
10582
 
#endif
10583
 
  int nHeight;            /* Expression tree height of current sub-select */
10584
 
  Table *pZombieTab;      /* List of Table objects to delete after code gen */
10585
 
  TriggerPrg *pTriggerPrg;    /* Linked list of coded triggers */
10586
 
 
10587
 
#ifndef SQLITE_OMIT_EXPLAIN
10588
 
  int iSelectId;
10589
 
  int iNextSelectId;
10590
 
#endif
10591
 
};
10592
 
 
10593
 
#ifdef SQLITE_OMIT_VIRTUALTABLE
10594
 
  #define IN_DECLARE_VTAB 0
10595
 
#else
10596
 
  #define IN_DECLARE_VTAB (pParse->declareVtab)
10597
 
#endif
10598
 
 
10599
 
/*
10600
 
** An instance of the following structure can be declared on a stack and used
10601
 
** to save the Parse.zAuthContext value so that it can be restored later.
10602
 
*/
10603
 
struct AuthContext {
10604
 
  const char *zAuthContext;   /* Put saved Parse.zAuthContext here */
10605
 
  Parse *pParse;              /* The Parse structure */
10606
 
};
10607
 
 
10608
 
/*
10609
 
** Bitfield flags for P5 value in OP_Insert and OP_Delete
10610
 
*/
10611
 
#define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
10612
 
#define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
10613
 
#define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
10614
 
#define OPFLAG_APPEND        0x08    /* This is likely to be an append */
10615
 
#define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
10616
 
#define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
10617
 
 
10618
 
/*
10619
 
 * Each trigger present in the database schema is stored as an instance of
10620
 
 * struct Trigger. 
10621
 
 *
10622
 
 * Pointers to instances of struct Trigger are stored in two ways.
10623
 
 * 1. In the "trigHash" hash table (part of the sqlite3* that represents the 
10624
 
 *    database). This allows Trigger structures to be retrieved by name.
10625
 
 * 2. All triggers associated with a single table form a linked list, using the
10626
 
 *    pNext member of struct Trigger. A pointer to the first element of the
10627
 
 *    linked list is stored as the "pTrigger" member of the associated
10628
 
 *    struct Table.
10629
 
 *
10630
 
 * The "step_list" member points to the first element of a linked list
10631
 
 * containing the SQL statements specified as the trigger program.
10632
 
 */
10633
 
struct Trigger {
10634
 
  char *zName;            /* The name of the trigger                        */
10635
 
  char *table;            /* The table or view to which the trigger applies */
10636
 
  u8 op;                  /* One of TK_DELETE, TK_UPDATE, TK_INSERT         */
10637
 
  u8 tr_tm;               /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
10638
 
  Expr *pWhen;            /* The WHEN clause of the expression (may be NULL) */
10639
 
  IdList *pColumns;       /* If this is an UPDATE OF <column-list> trigger,
10640
 
                             the <column-list> is stored here */
10641
 
  Schema *pSchema;        /* Schema containing the trigger */
10642
 
  Schema *pTabSchema;     /* Schema containing the table */
10643
 
  TriggerStep *step_list; /* Link list of trigger program steps             */
10644
 
  Trigger *pNext;         /* Next trigger associated with the table */
10645
 
};
10646
 
 
10647
 
/*
10648
 
** A trigger is either a BEFORE or an AFTER trigger.  The following constants
10649
 
** determine which. 
10650
 
**
10651
 
** If there are multiple triggers, you might of some BEFORE and some AFTER.
10652
 
** In that cases, the constants below can be ORed together.
10653
 
*/
10654
 
#define TRIGGER_BEFORE  1
10655
 
#define TRIGGER_AFTER   2
10656
 
 
10657
 
/*
10658
 
 * An instance of struct TriggerStep is used to store a single SQL statement
10659
 
 * that is a part of a trigger-program. 
10660
 
 *
10661
 
 * Instances of struct TriggerStep are stored in a singly linked list (linked
10662
 
 * using the "pNext" member) referenced by the "step_list" member of the 
10663
 
 * associated struct Trigger instance. The first element of the linked list is
10664
 
 * the first step of the trigger-program.
10665
 
 * 
10666
 
 * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
10667
 
 * "SELECT" statement. The meanings of the other members is determined by the 
10668
 
 * value of "op" as follows:
10669
 
 *
10670
 
 * (op == TK_INSERT)
10671
 
 * orconf    -> stores the ON CONFLICT algorithm
10672
 
 * pSelect   -> If this is an INSERT INTO ... SELECT ... statement, then
10673
 
 *              this stores a pointer to the SELECT statement. Otherwise NULL.
10674
 
 * target    -> A token holding the quoted name of the table to insert into.
10675
 
 * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
10676
 
 *              this stores values to be inserted. Otherwise NULL.
10677
 
 * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ... 
10678
 
 *              statement, then this stores the column-names to be
10679
 
 *              inserted into.
10680
 
 *
10681
 
 * (op == TK_DELETE)
10682
 
 * target    -> A token holding the quoted name of the table to delete from.
10683
 
 * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
10684
 
 *              Otherwise NULL.
10685
 
 * 
10686
 
 * (op == TK_UPDATE)
10687
 
 * target    -> A token holding the quoted name of the table to update rows of.
10688
 
 * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
10689
 
 *              Otherwise NULL.
10690
 
 * pExprList -> A list of the columns to update and the expressions to update
10691
 
 *              them to. See sqlite3Update() documentation of "pChanges"
10692
 
 *              argument.
10693
 
 * 
10694
 
 */
10695
 
struct TriggerStep {
10696
 
  u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
10697
 
  u8 orconf;           /* OE_Rollback etc. */
10698
 
  Trigger *pTrig;      /* The trigger that this step is a part of */
10699
 
  Select *pSelect;     /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
10700
 
  Token target;        /* Target table for DELETE, UPDATE, INSERT */
10701
 
  Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
10702
 
  ExprList *pExprList; /* SET clause for UPDATE.  VALUES clause for INSERT */
10703
 
  IdList *pIdList;     /* Column names for INSERT */
10704
 
  TriggerStep *pNext;  /* Next in the link-list */
10705
 
  TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
10706
 
};
10707
 
 
10708
 
/*
10709
 
** The following structure contains information used by the sqliteFix...
10710
 
** routines as they walk the parse tree to make database references
10711
 
** explicit.  
10712
 
*/
10713
 
typedef struct DbFixer DbFixer;
10714
 
struct DbFixer {
10715
 
  Parse *pParse;      /* The parsing context.  Error messages written here */
10716
 
  const char *zDb;    /* Make sure all objects are contained in this database */
10717
 
  const char *zType;  /* Type of the container - used for error messages */
10718
 
  const Token *pName; /* Name of the container - used for error messages */
10719
 
};
10720
 
 
10721
 
/*
10722
 
** An objected used to accumulate the text of a string where we
10723
 
** do not necessarily know how big the string will be in the end.
10724
 
*/
10725
 
struct StrAccum {
10726
 
  sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
10727
 
  char *zBase;         /* A base allocation.  Not from malloc. */
10728
 
  char *zText;         /* The string collected so far */
10729
 
  int  nChar;          /* Length of the string so far */
10730
 
  int  nAlloc;         /* Amount of space allocated in zText */
10731
 
  int  mxAlloc;        /* Maximum allowed string length */
10732
 
  u8   mallocFailed;   /* Becomes true if any memory allocation fails */
10733
 
  u8   useMalloc;      /* 0: none,  1: sqlite3DbMalloc,  2: sqlite3_malloc */
10734
 
  u8   tooBig;         /* Becomes true if string size exceeds limits */
10735
 
};
10736
 
 
10737
 
/*
10738
 
** A pointer to this structure is used to communicate information
10739
 
** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
10740
 
*/
10741
 
typedef struct {
10742
 
  sqlite3 *db;        /* The database being initialized */
10743
 
  int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
10744
 
  char **pzErrMsg;    /* Error message stored here */
10745
 
  int rc;             /* Result code stored here */
10746
 
} InitData;
10747
 
 
10748
 
/*
10749
 
** Structure containing global configuration data for the SQLite library.
10750
 
**
10751
 
** This structure also contains some state information.
10752
 
*/
10753
 
struct Sqlite3Config {
10754
 
  int bMemstat;                     /* True to enable memory status */
10755
 
  int bCoreMutex;                   /* True to enable core mutexing */
10756
 
  int bFullMutex;                   /* True to enable full mutexing */
10757
 
  int mxStrlen;                     /* Maximum string length */
10758
 
  int szLookaside;                  /* Default lookaside buffer size */
10759
 
  int nLookaside;                   /* Default lookaside buffer count */
10760
 
  sqlite3_mem_methods m;            /* Low-level memory allocation interface */
10761
 
  sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
10762
 
  sqlite3_pcache_methods pcache;    /* Low-level page-cache interface */
10763
 
  void *pHeap;                      /* Heap storage space */
10764
 
  int nHeap;                        /* Size of pHeap[] */
10765
 
  int mnReq, mxReq;                 /* Min and max heap requests sizes */
10766
 
  void *pScratch;                   /* Scratch memory */
10767
 
  int szScratch;                    /* Size of each scratch buffer */
10768
 
  int nScratch;                     /* Number of scratch buffers */
10769
 
  void *pPage;                      /* Page cache memory */
10770
 
  int szPage;                       /* Size of each page in pPage[] */
10771
 
  int nPage;                        /* Number of pages in pPage[] */
10772
 
  int mxParserStack;                /* maximum depth of the parser stack */
10773
 
  int sharedCacheEnabled;           /* true if shared-cache mode enabled */
10774
 
  /* The above might be initialized to non-zero.  The following need to always
10775
 
  ** initially be zero, however. */
10776
 
  int isInit;                       /* True after initialization has finished */
10777
 
  int inProgress;                   /* True while initialization in progress */
10778
 
  int isMutexInit;                  /* True after mutexes are initialized */
10779
 
  int isMallocInit;                 /* True after malloc is initialized */
10780
 
  int isPCacheInit;                 /* True after malloc is initialized */
10781
 
  sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
10782
 
  int nRefInitMutex;                /* Number of users of pInitMutex */
10783
 
  void (*xLog)(void*,int,const char*); /* Function for logging */
10784
 
  void *pLogArg;                       /* First argument to xLog() */
10785
 
};
10786
 
 
10787
 
/*
10788
 
** Context pointer passed down through the tree-walk.
10789
 
*/
10790
 
struct Walker {
10791
 
  int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
10792
 
  int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
10793
 
  Parse *pParse;                            /* Parser context.  */
10794
 
  union {                                   /* Extra data for callback */
10795
 
    NameContext *pNC;                          /* Naming context */
10796
 
    int i;                                     /* Integer value */
10797
 
  } u;
10798
 
};
10799
 
 
10800
 
/* Forward declarations */
10801
 
SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
10802
 
SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
10803
 
SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
10804
 
SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
10805
 
SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
10806
 
 
10807
 
/*
10808
 
** Return code from the parse-tree walking primitives and their
10809
 
** callbacks.
10810
 
*/
10811
 
#define WRC_Continue    0   /* Continue down into children */
10812
 
#define WRC_Prune       1   /* Omit children but continue walking siblings */
10813
 
#define WRC_Abort       2   /* Abandon the tree walk */
10814
 
 
10815
 
/*
10816
 
** Assuming zIn points to the first byte of a UTF-8 character,
10817
 
** advance zIn to point to the first byte of the next UTF-8 character.
10818
 
*/
10819
 
#define SQLITE_SKIP_UTF8(zIn) {                        \
10820
 
  if( (*(zIn++))>=0xc0 ){                              \
10821
 
    while( (*zIn & 0xc0)==0x80 ){ zIn++; }             \
10822
 
  }                                                    \
10823
 
}
10824
 
 
10825
 
/*
10826
 
** The SQLITE_*_BKPT macros are substitutes for the error codes with
10827
 
** the same name but without the _BKPT suffix.  These macros invoke
10828
 
** routines that report the line-number on which the error originated
10829
 
** using sqlite3_log().  The routines also provide a convenient place
10830
 
** to set a debugger breakpoint.
10831
 
*/
10832
 
SQLITE_PRIVATE int sqlite3CorruptError(int);
10833
 
SQLITE_PRIVATE int sqlite3MisuseError(int);
10834
 
SQLITE_PRIVATE int sqlite3CantopenError(int);
10835
 
#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
10836
 
#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
10837
 
#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
10838
 
 
10839
 
 
10840
 
/*
10841
 
** FTS4 is really an extension for FTS3.  It is enabled using the
10842
 
** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
10843
 
** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
10844
 
*/
10845
 
#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
10846
 
# define SQLITE_ENABLE_FTS3
10847
 
#endif
10848
 
 
10849
 
/*
10850
 
** The ctype.h header is needed for non-ASCII systems.  It is also
10851
 
** needed by FTS3 when FTS3 is included in the amalgamation.
10852
 
*/
10853
 
#if !defined(SQLITE_ASCII) || \
10854
 
    (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
10855
 
# include <ctype.h>
10856
 
#endif
10857
 
 
10858
 
/*
10859
 
** The following macros mimic the standard library functions toupper(),
10860
 
** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
10861
 
** sqlite versions only work for ASCII characters, regardless of locale.
10862
 
*/
10863
 
#ifdef SQLITE_ASCII
10864
 
# define sqlite3Toupper(x)  ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
10865
 
# define sqlite3Isspace(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
10866
 
# define sqlite3Isalnum(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
10867
 
# define sqlite3Isalpha(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
10868
 
# define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
10869
 
# define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
10870
 
# define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
10871
 
#else
10872
 
# define sqlite3Toupper(x)   toupper((unsigned char)(x))
10873
 
# define sqlite3Isspace(x)   isspace((unsigned char)(x))
10874
 
# define sqlite3Isalnum(x)   isalnum((unsigned char)(x))
10875
 
# define sqlite3Isalpha(x)   isalpha((unsigned char)(x))
10876
 
# define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
10877
 
# define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
10878
 
# define sqlite3Tolower(x)   tolower((unsigned char)(x))
10879
 
#endif
10880
 
 
10881
 
/*
10882
 
** Internal function prototypes
10883
 
*/
10884
 
SQLITE_PRIVATE int sqlite3StrICmp(const char *, const char *);
10885
 
SQLITE_PRIVATE int sqlite3Strlen30(const char*);
10886
 
#define sqlite3StrNICmp sqlite3_strnicmp
10887
 
 
10888
 
SQLITE_PRIVATE int sqlite3MallocInit(void);
10889
 
SQLITE_PRIVATE void sqlite3MallocEnd(void);
10890
 
SQLITE_PRIVATE void *sqlite3Malloc(int);
10891
 
SQLITE_PRIVATE void *sqlite3MallocZero(int);
10892
 
SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, int);
10893
 
SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, int);
10894
 
SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
10895
 
SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, int);
10896
 
SQLITE_PRIVATE void *sqlite3Realloc(void*, int);
10897
 
SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
10898
 
SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, int);
10899
 
SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
10900
 
SQLITE_PRIVATE int sqlite3MallocSize(void*);
10901
 
SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
10902
 
SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
10903
 
SQLITE_PRIVATE void sqlite3ScratchFree(void*);
10904
 
SQLITE_PRIVATE void *sqlite3PageMalloc(int);
10905
 
SQLITE_PRIVATE void sqlite3PageFree(void*);
10906
 
SQLITE_PRIVATE void sqlite3MemSetDefault(void);
10907
 
SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
10908
 
SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
10909
 
 
10910
 
/*
10911
 
** On systems with ample stack space and that support alloca(), make
10912
 
** use of alloca() to obtain space for large automatic objects.  By default,
10913
 
** obtain space from malloc().
10914
 
**
10915
 
** The alloca() routine never returns NULL.  This will cause code paths
10916
 
** that deal with sqlite3StackAlloc() failures to be unreachable.
10917
 
*/
10918
 
#ifdef SQLITE_USE_ALLOCA
10919
 
# define sqlite3StackAllocRaw(D,N)   alloca(N)
10920
 
# define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
10921
 
# define sqlite3StackFree(D,P)       
10922
 
#else
10923
 
# define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
10924
 
# define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
10925
 
# define sqlite3StackFree(D,P)       sqlite3DbFree(D,P)
10926
 
#endif
10927
 
 
10928
 
#ifdef SQLITE_ENABLE_MEMSYS3
10929
 
SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
10930
 
#endif
10931
 
#ifdef SQLITE_ENABLE_MEMSYS5
10932
 
SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
10933
 
#endif
10934
 
 
10935
 
 
10936
 
#ifndef SQLITE_MUTEX_OMIT
10937
 
SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
10938
 
SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3NoopMutex(void);
10939
 
SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
10940
 
SQLITE_PRIVATE   int sqlite3MutexInit(void);
10941
 
SQLITE_PRIVATE   int sqlite3MutexEnd(void);
10942
 
#endif
10943
 
 
10944
 
SQLITE_PRIVATE int sqlite3StatusValue(int);
10945
 
SQLITE_PRIVATE void sqlite3StatusAdd(int, int);
10946
 
SQLITE_PRIVATE void sqlite3StatusSet(int, int);
10947
 
 
10948
 
#ifndef SQLITE_OMIT_FLOATING_POINT
10949
 
SQLITE_PRIVATE   int sqlite3IsNaN(double);
10950
 
#else
10951
 
# define sqlite3IsNaN(X)  0
10952
 
#endif
10953
 
 
10954
 
SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, int, const char*, va_list);
10955
 
#ifndef SQLITE_OMIT_TRACE
10956
 
SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...);
10957
 
#endif
10958
 
SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
10959
 
SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
10960
 
SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
10961
 
#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
10962
 
SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
10963
 
#endif
10964
 
#if defined(SQLITE_TEST)
10965
 
SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
10966
 
#endif
10967
 
SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
10968
 
SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
10969
 
SQLITE_PRIVATE int sqlite3Dequote(char*);
10970
 
SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
10971
 
SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
10972
 
SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
10973
 
SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
10974
 
SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
10975
 
SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
10976
 
SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
10977
 
SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
10978
 
SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
10979
 
SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
10980
 
SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
10981
 
SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
10982
 
SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
10983
 
SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
10984
 
SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
10985
 
SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
10986
 
SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
10987
 
SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
10988
 
SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
10989
 
SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
10990
 
SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
10991
 
SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
10992
 
SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3*, int);
10993
 
SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int);
10994
 
SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
10995
 
SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
10996
 
SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
10997
 
SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
10998
 
SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
10999
 
SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
11000
 
SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
11001
 
SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
11002
 
SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
11003
 
SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
11004
 
SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
11005
 
SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,Select*);
11006
 
 
11007
 
SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
11008
 
SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
11009
 
SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
11010
 
SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
11011
 
SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
11012
 
SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
11013
 
SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
11014
 
 
11015
 
SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
11016
 
SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
11017
 
SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
11018
 
SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
11019
 
SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
11020
 
 
11021
 
SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
11022
 
 
11023
 
#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
11024
 
SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
11025
 
#else
11026
 
# define sqlite3ViewGetColumnNames(A,B) 0
11027
 
#endif
11028
 
 
11029
 
SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
11030
 
SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
11031
 
#ifndef SQLITE_OMIT_AUTOINCREMENT
11032
 
SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
11033
 
SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
11034
 
#else
11035
 
# define sqlite3AutoincrementBegin(X)
11036
 
# define sqlite3AutoincrementEnd(X)
11037
 
#endif
11038
 
SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
11039
 
SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
11040
 
SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
11041
 
SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
11042
 
SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
11043
 
SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
11044
 
SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
11045
 
                                      Token*, Select*, Expr*, IdList*);
11046
 
SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
11047
 
SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
11048
 
SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
11049
 
SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
11050
 
SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
11051
 
SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
11052
 
SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
11053
 
                        Token*, int, int);
11054
 
SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
11055
 
SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
11056
 
SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
11057
 
                         Expr*,ExprList*,int,Expr*,Expr*);
11058
 
SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
11059
 
SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
11060
 
SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
11061
 
SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
11062
 
#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
11063
 
SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
11064
 
#endif
11065
 
SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
11066
 
SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
11067
 
SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**, u16);
11068
 
SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
11069
 
SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int);
11070
 
SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
11071
 
SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
11072
 
SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, int, int, int);
11073
 
SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
11074
 
SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
11075
 
SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
11076
 
SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
11077
 
SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
11078
 
SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
11079
 
SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
11080
 
SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
11081
 
SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
11082
 
SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
11083
 
SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*);
11084
 
SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int);
11085
 
SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
11086
 
SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
11087
 
SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
11088
 
SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
11089
 
SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
11090
 
SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
11091
 
SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
11092
 
SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
11093
 
SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
11094
 
SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
11095
 
SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*);
11096
 
SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*);
11097
 
SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
11098
 
SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
11099
 
SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
11100
 
SQLITE_PRIVATE void sqlite3PrngSaveState(void);
11101
 
SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
11102
 
SQLITE_PRIVATE void sqlite3PrngResetState(void);
11103
 
SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*);
11104
 
SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
11105
 
SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
11106
 
SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
11107
 
SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
11108
 
SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
11109
 
SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
11110
 
SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
11111
 
SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
11112
 
SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
11113
 
SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
11114
 
SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
11115
 
SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
11116
 
SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
11117
 
SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
11118
 
SQLITE_PRIVATE int sqlite3IsRowid(const char*);
11119
 
SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int, Trigger *, int);
11120
 
SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
11121
 
SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
11122
 
SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
11123
 
                                     int*,int,int,int,int,int*);
11124
 
SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
11125
 
SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
11126
 
SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
11127
 
SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
11128
 
SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
11129
 
SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, char*, int);
11130
 
SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
11131
 
SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
11132
 
SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
11133
 
SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
11134
 
SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
11135
 
SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
11136
 
SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
11137
 
SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
11138
 
SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
11139
 
SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
11140
 
SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
11141
 
SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
11142
 
SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
11143
 
 
11144
 
#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
11145
 
SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
11146
 
#endif
11147
 
 
11148
 
#ifndef SQLITE_OMIT_TRIGGER
11149
 
SQLITE_PRIVATE   void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
11150
 
                           Expr*,int, int);
11151
 
SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
11152
 
SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
11153
 
SQLITE_PRIVATE   void sqlite3DropTriggerPtr(Parse*, Trigger*);
11154
 
SQLITE_PRIVATE   Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
11155
 
SQLITE_PRIVATE   Trigger *sqlite3TriggerList(Parse *, Table *);
11156
 
SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
11157
 
                            int, int, int);
11158
 
SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
11159
 
  void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
11160
 
SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
11161
 
SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
11162
 
SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
11163
 
                                        ExprList*,Select*,u8);
11164
 
SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
11165
 
SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
11166
 
SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
11167
 
SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
11168
 
SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
11169
 
# define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
11170
 
#else
11171
 
# define sqlite3TriggersExist(B,C,D,E,F) 0
11172
 
# define sqlite3DeleteTrigger(A,B)
11173
 
# define sqlite3DropTriggerPtr(A,B)
11174
 
# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
11175
 
# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
11176
 
# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
11177
 
# define sqlite3TriggerList(X, Y) 0
11178
 
# define sqlite3ParseToplevel(p) p
11179
 
# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
11180
 
#endif
11181
 
 
11182
 
SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
11183
 
SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
11184
 
SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
11185
 
#ifndef SQLITE_OMIT_AUTHORIZATION
11186
 
SQLITE_PRIVATE   void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
11187
 
SQLITE_PRIVATE   int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
11188
 
SQLITE_PRIVATE   void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
11189
 
SQLITE_PRIVATE   void sqlite3AuthContextPop(AuthContext*);
11190
 
SQLITE_PRIVATE   int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
11191
 
#else
11192
 
# define sqlite3AuthRead(a,b,c,d)
11193
 
# define sqlite3AuthCheck(a,b,c,d,e)    SQLITE_OK
11194
 
# define sqlite3AuthContextPush(a,b,c)
11195
 
# define sqlite3AuthContextPop(a)  ((void)(a))
11196
 
#endif
11197
 
SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
11198
 
SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
11199
 
SQLITE_PRIVATE int sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
11200
 
SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
11201
 
SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
11202
 
SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
11203
 
SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
11204
 
SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
11205
 
SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
11206
 
SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
11207
 
SQLITE_PRIVATE int sqlite3Atoi(const char*);
11208
 
SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
11209
 
SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
11210
 
SQLITE_PRIVATE int sqlite3Utf8Read(const u8*, const u8**);
11211
 
 
11212
 
/*
11213
 
** Routines to read and write variable-length integers.  These used to
11214
 
** be defined locally, but now we use the varint routines in the util.c
11215
 
** file.  Code should use the MACRO forms below, as the Varint32 versions
11216
 
** are coded to assume the single byte case is already handled (which 
11217
 
** the MACRO form does).
11218
 
*/
11219
 
SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
11220
 
SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char*, u32);
11221
 
SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
11222
 
SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
11223
 
SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
11224
 
 
11225
 
/*
11226
 
** The header of a record consists of a sequence variable-length integers.
11227
 
** These integers are almost always small and are encoded as a single byte.
11228
 
** The following macros take advantage this fact to provide a fast encode
11229
 
** and decode of the integers in a record header.  It is faster for the common
11230
 
** case where the integer is a single byte.  It is a little slower when the
11231
 
** integer is two or more bytes.  But overall it is faster.
11232
 
**
11233
 
** The following expressions are equivalent:
11234
 
**
11235
 
**     x = sqlite3GetVarint32( A, &B );
11236
 
**     x = sqlite3PutVarint32( A, B );
11237
 
**
11238
 
**     x = getVarint32( A, B );
11239
 
**     x = putVarint32( A, B );
11240
 
**
11241
 
*/
11242
 
#define getVarint32(A,B)  (u8)((*(A)<(u8)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), (u32 *)&(B)))
11243
 
#define putVarint32(A,B)  (u8)(((u32)(B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B)))
11244
 
#define getVarint    sqlite3GetVarint
11245
 
#define putVarint    sqlite3PutVarint
11246
 
 
11247
 
 
11248
 
SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
11249
 
SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *, Table *);
11250
 
SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
11251
 
SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
11252
 
SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
11253
 
SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
11254
 
SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
11255
 
SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
11256
 
SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
11257
 
SQLITE_PRIVATE const char *sqlite3ErrStr(int);
11258
 
SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
11259
 
SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
11260
 
SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
11261
 
SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
11262
 
SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr*, CollSeq*);
11263
 
SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr*, Token*);
11264
 
SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
11265
 
SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
11266
 
SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
11267
 
SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
11268
 
SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
11269
 
SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
11270
 
SQLITE_PRIVATE int sqlite3AbsInt32(int);
11271
 
 
11272
 
SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
11273
 
SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
11274
 
SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
11275
 
                        void(*)(void*));
11276
 
SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
11277
 
SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
11278
 
SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
11279
 
#ifdef SQLITE_ENABLE_STAT2
11280
 
SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
11281
 
#endif
11282
 
SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
11283
 
SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
11284
 
#ifndef SQLITE_AMALGAMATION
11285
 
SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
11286
 
SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
11287
 
SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
11288
 
SQLITE_PRIVATE const Token sqlite3IntTokens[];
11289
 
SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
11290
 
SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
11291
 
#ifndef SQLITE_OMIT_WSD
11292
 
SQLITE_PRIVATE int sqlite3PendingByte;
11293
 
#endif
11294
 
#endif
11295
 
SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
11296
 
SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
11297
 
SQLITE_PRIVATE void sqlite3AlterFunctions(void);
11298
 
SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
11299
 
SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
11300
 
SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
11301
 
SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
11302
 
SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
11303
 
SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
11304
 
SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
11305
 
SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
11306
 
SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
11307
 
SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
11308
 
SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
11309
 
SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
11310
 
SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(sqlite3*, u8, CollSeq *, const char*);
11311
 
SQLITE_PRIVATE char sqlite3AffinityType(const char*);
11312
 
SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
11313
 
SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
11314
 
SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
11315
 
SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
11316
 
SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
11317
 
SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
11318
 
SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
11319
 
SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
11320
 
SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
11321
 
SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
11322
 
SQLITE_PRIVATE void sqlite3SchemaClear(void *);
11323
 
SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
11324
 
SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
11325
 
SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
11326
 
SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, 
11327
 
  void (*)(sqlite3_context*,int,sqlite3_value **),
11328
 
  void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
11329
 
  FuncDestructor *pDestructor
11330
 
);
11331
 
SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
11332
 
SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
11333
 
 
11334
 
SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
11335
 
SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
11336
 
SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
11337
 
SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
11338
 
SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
11339
 
SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
11340
 
 
11341
 
SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
11342
 
SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
11343
 
 
11344
 
/*
11345
 
** The interface to the LEMON-generated parser
11346
 
*/
11347
 
SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
11348
 
SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
11349
 
SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
11350
 
#ifdef YYTRACKMAXSTACKDEPTH
11351
 
SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
11352
 
#endif
11353
 
 
11354
 
SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
11355
 
#ifndef SQLITE_OMIT_LOAD_EXTENSION
11356
 
SQLITE_PRIVATE   void sqlite3CloseExtensions(sqlite3*);
11357
 
#else
11358
 
# define sqlite3CloseExtensions(X)
11359
 
#endif
11360
 
 
11361
 
#ifndef SQLITE_OMIT_SHARED_CACHE
11362
 
SQLITE_PRIVATE   void sqlite3TableLock(Parse *, int, int, u8, const char *);
11363
 
#else
11364
 
  #define sqlite3TableLock(v,w,x,y,z)
11365
 
#endif
11366
 
 
11367
 
#ifdef SQLITE_TEST
11368
 
SQLITE_PRIVATE   int sqlite3Utf8To8(unsigned char*);
11369
 
#endif
11370
 
 
11371
 
#ifdef SQLITE_OMIT_VIRTUALTABLE
11372
 
#  define sqlite3VtabClear(Y)
11373
 
#  define sqlite3VtabSync(X,Y) SQLITE_OK
11374
 
#  define sqlite3VtabRollback(X)
11375
 
#  define sqlite3VtabCommit(X)
11376
 
#  define sqlite3VtabInSync(db) 0
11377
 
#  define sqlite3VtabLock(X) 
11378
 
#  define sqlite3VtabUnlock(X)
11379
 
#  define sqlite3VtabUnlockList(X)
11380
 
#else
11381
 
SQLITE_PRIVATE    void sqlite3VtabClear(sqlite3 *db, Table*);
11382
 
SQLITE_PRIVATE    int sqlite3VtabSync(sqlite3 *db, char **);
11383
 
SQLITE_PRIVATE    int sqlite3VtabRollback(sqlite3 *db);
11384
 
SQLITE_PRIVATE    int sqlite3VtabCommit(sqlite3 *db);
11385
 
SQLITE_PRIVATE    void sqlite3VtabLock(VTable *);
11386
 
SQLITE_PRIVATE    void sqlite3VtabUnlock(VTable *);
11387
 
SQLITE_PRIVATE    void sqlite3VtabUnlockList(sqlite3*);
11388
 
#  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
11389
 
#endif
11390
 
SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
11391
 
SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
11392
 
SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
11393
 
SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
11394
 
SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
11395
 
SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
11396
 
SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
11397
 
SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
11398
 
SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
11399
 
SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
11400
 
SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
11401
 
SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
11402
 
SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
11403
 
SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
11404
 
SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
11405
 
SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
11406
 
SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
11407
 
SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
11408
 
SQLITE_PRIVATE const char *sqlite3JournalModename(int);
11409
 
SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
11410
 
SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
11411
 
 
11412
 
/* Declarations for functions in fkey.c. All of these are replaced by
11413
 
** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
11414
 
** key functionality is available. If OMIT_TRIGGER is defined but
11415
 
** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
11416
 
** this case foreign keys are parsed, but no other functionality is 
11417
 
** provided (enforcement of FK constraints requires the triggers sub-system).
11418
 
*/
11419
 
#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
11420
 
SQLITE_PRIVATE   void sqlite3FkCheck(Parse*, Table*, int, int);
11421
 
SQLITE_PRIVATE   void sqlite3FkDropTable(Parse*, SrcList *, Table*);
11422
 
SQLITE_PRIVATE   void sqlite3FkActions(Parse*, Table*, ExprList*, int);
11423
 
SQLITE_PRIVATE   int sqlite3FkRequired(Parse*, Table*, int*, int);
11424
 
SQLITE_PRIVATE   u32 sqlite3FkOldmask(Parse*, Table*);
11425
 
SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
11426
 
#else
11427
 
  #define sqlite3FkActions(a,b,c,d)
11428
 
  #define sqlite3FkCheck(a,b,c,d)
11429
 
  #define sqlite3FkDropTable(a,b,c)
11430
 
  #define sqlite3FkOldmask(a,b)      0
11431
 
  #define sqlite3FkRequired(a,b,c,d) 0
11432
 
#endif
11433
 
#ifndef SQLITE_OMIT_FOREIGN_KEY
11434
 
SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
11435
 
#else
11436
 
  #define sqlite3FkDelete(a,b)
11437
 
#endif
11438
 
 
11439
 
 
11440
 
/*
11441
 
** Available fault injectors.  Should be numbered beginning with 0.
11442
 
*/
11443
 
#define SQLITE_FAULTINJECTOR_MALLOC     0
11444
 
#define SQLITE_FAULTINJECTOR_COUNT      1
11445
 
 
11446
 
/*
11447
 
** The interface to the code in fault.c used for identifying "benign"
11448
 
** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
11449
 
** is not defined.
11450
 
*/
11451
 
#ifndef SQLITE_OMIT_BUILTIN_TEST
11452
 
SQLITE_PRIVATE   void sqlite3BeginBenignMalloc(void);
11453
 
SQLITE_PRIVATE   void sqlite3EndBenignMalloc(void);
11454
 
#else
11455
 
  #define sqlite3BeginBenignMalloc()
11456
 
  #define sqlite3EndBenignMalloc()
11457
 
#endif
11458
 
 
11459
 
#define IN_INDEX_ROWID           1
11460
 
#define IN_INDEX_EPH             2
11461
 
#define IN_INDEX_INDEX           3
11462
 
SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
11463
 
 
11464
 
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
11465
 
SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
11466
 
SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
11467
 
SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
11468
 
#else
11469
 
  #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
11470
 
#endif
11471
 
 
11472
 
SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
11473
 
SQLITE_PRIVATE int sqlite3MemJournalSize(void);
11474
 
SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
11475
 
 
11476
 
#if SQLITE_MAX_EXPR_DEPTH>0
11477
 
SQLITE_PRIVATE   void sqlite3ExprSetHeight(Parse *pParse, Expr *p);
11478
 
SQLITE_PRIVATE   int sqlite3SelectExprHeight(Select *);
11479
 
SQLITE_PRIVATE   int sqlite3ExprCheckHeight(Parse*, int);
11480
 
#else
11481
 
  #define sqlite3ExprSetHeight(x,y)
11482
 
  #define sqlite3SelectExprHeight(x) 0
11483
 
  #define sqlite3ExprCheckHeight(x,y)
11484
 
#endif
11485
 
 
11486
 
SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
11487
 
SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
11488
 
 
11489
 
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
11490
 
SQLITE_PRIVATE   void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
11491
 
SQLITE_PRIVATE   void sqlite3ConnectionUnlocked(sqlite3 *db);
11492
 
SQLITE_PRIVATE   void sqlite3ConnectionClosed(sqlite3 *db);
11493
 
#else
11494
 
  #define sqlite3ConnectionBlocked(x,y)
11495
 
  #define sqlite3ConnectionUnlocked(x)
11496
 
  #define sqlite3ConnectionClosed(x)
11497
 
#endif
11498
 
 
11499
 
#ifdef SQLITE_DEBUG
11500
 
SQLITE_PRIVATE   void sqlite3ParserTrace(FILE*, char *);
11501
 
#endif
11502
 
 
11503
 
/*
11504
 
** If the SQLITE_ENABLE IOTRACE exists then the global variable
11505
 
** sqlite3IoTrace is a pointer to a printf-like routine used to
11506
 
** print I/O tracing messages. 
11507
 
*/
11508
 
#ifdef SQLITE_ENABLE_IOTRACE
11509
 
# define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
11510
 
SQLITE_PRIVATE   void sqlite3VdbeIOTraceSql(Vdbe*);
11511
 
SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*,...);
11512
 
#else
11513
 
# define IOTRACE(A)
11514
 
# define sqlite3VdbeIOTraceSql(X)
11515
 
#endif
11516
 
 
11517
 
/*
11518
 
** These routines are available for the mem2.c debugging memory allocator
11519
 
** only.  They are used to verify that different "types" of memory
11520
 
** allocations are properly tracked by the system.
11521
 
**
11522
 
** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
11523
 
** the MEMTYPE_* macros defined below.  The type must be a bitmask with
11524
 
** a single bit set.
11525
 
**
11526
 
** sqlite3MemdebugHasType() returns true if any of the bits in its second
11527
 
** argument match the type set by the previous sqlite3MemdebugSetType().
11528
 
** sqlite3MemdebugHasType() is intended for use inside assert() statements.
11529
 
**
11530
 
** sqlite3MemdebugNoType() returns true if none of the bits in its second
11531
 
** argument match the type set by the previous sqlite3MemdebugSetType().
11532
 
**
11533
 
** Perhaps the most important point is the difference between MEMTYPE_HEAP
11534
 
** and MEMTYPE_LOOKASIDE.  If an allocation is MEMTYPE_LOOKASIDE, that means
11535
 
** it might have been allocated by lookaside, except the allocation was
11536
 
** too large or lookaside was already full.  It is important to verify
11537
 
** that allocations that might have been satisfied by lookaside are not
11538
 
** passed back to non-lookaside free() routines.  Asserts such as the
11539
 
** example above are placed on the non-lookaside free() routines to verify
11540
 
** this constraint. 
11541
 
**
11542
 
** All of this is no-op for a production build.  It only comes into
11543
 
** play when the SQLITE_MEMDEBUG compile-time option is used.
11544
 
*/
11545
 
#ifdef SQLITE_MEMDEBUG
11546
 
SQLITE_PRIVATE   void sqlite3MemdebugSetType(void*,u8);
11547
 
SQLITE_PRIVATE   int sqlite3MemdebugHasType(void*,u8);
11548
 
SQLITE_PRIVATE   int sqlite3MemdebugNoType(void*,u8);
11549
 
#else
11550
 
# define sqlite3MemdebugSetType(X,Y)  /* no-op */
11551
 
# define sqlite3MemdebugHasType(X,Y)  1
11552
 
# define sqlite3MemdebugNoType(X,Y)   1
11553
 
#endif
11554
 
#define MEMTYPE_HEAP       0x01  /* General heap allocations */
11555
 
#define MEMTYPE_LOOKASIDE  0x02  /* Might have been lookaside memory */
11556
 
#define MEMTYPE_SCRATCH    0x04  /* Scratch allocations */
11557
 
#define MEMTYPE_PCACHE     0x08  /* Page cache allocations */
11558
 
#define MEMTYPE_DB         0x10  /* Uses sqlite3DbMalloc, not sqlite_malloc */
11559
 
 
11560
 
#endif /* _SQLITEINT_H_ */
11561
 
 
11562
 
/************** End of sqliteInt.h *******************************************/
11563
 
/************** Begin file global.c ******************************************/
11564
 
/*
11565
 
** 2008 June 13
11566
 
**
11567
 
** The author disclaims copyright to this source code.  In place of
11568
 
** a legal notice, here is a blessing:
11569
 
**
11570
 
**    May you do good and not evil.
11571
 
**    May you find forgiveness for yourself and forgive others.
11572
 
**    May you share freely, never taking more than you give.
11573
 
**
11574
 
*************************************************************************
11575
 
**
11576
 
** This file contains definitions of global variables and contants.
11577
 
*/
11578
 
 
11579
 
/* An array to map all upper-case characters into their corresponding
11580
 
** lower-case character. 
11581
 
**
11582
 
** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
11583
 
** handle case conversions for the UTF character set since the tables
11584
 
** involved are nearly as big or bigger than SQLite itself.
11585
 
*/
11586
 
SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
11587
 
#ifdef SQLITE_ASCII
11588
 
      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
11589
 
     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
11590
 
     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
11591
 
     54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
11592
 
    104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
11593
 
    122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
11594
 
    108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
11595
 
    126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
11596
 
    144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
11597
 
    162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
11598
 
    180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
11599
 
    198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
11600
 
    216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
11601
 
    234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
11602
 
    252,253,254,255
11603
 
#endif
11604
 
#ifdef SQLITE_EBCDIC
11605
 
      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
11606
 
     16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
11607
 
     32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
11608
 
     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
11609
 
     64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
11610
 
     80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
11611
 
     96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
11612
 
    112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
11613
 
    128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
11614
 
    144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
11615
 
    160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
11616
 
    176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
11617
 
    192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
11618
 
    208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
11619
 
    224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
11620
 
    239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
11621
 
#endif
11622
 
};
11623
 
 
11624
 
/*
11625
 
** The following 256 byte lookup table is used to support SQLites built-in
11626
 
** equivalents to the following standard library functions:
11627
 
**
11628
 
**   isspace()                        0x01
11629
 
**   isalpha()                        0x02
11630
 
**   isdigit()                        0x04
11631
 
**   isalnum()                        0x06
11632
 
**   isxdigit()                       0x08
11633
 
**   toupper()                        0x20
11634
 
**   SQLite identifier character      0x40
11635
 
**
11636
 
** Bit 0x20 is set if the mapped character requires translation to upper
11637
 
** case. i.e. if the character is a lower-case ASCII character.
11638
 
** If x is a lower-case ASCII character, then its upper-case equivalent
11639
 
** is (x - 0x20). Therefore toupper() can be implemented as:
11640
 
**
11641
 
**   (x & ~(map[x]&0x20))
11642
 
**
11643
 
** Standard function tolower() is implemented using the sqlite3UpperToLower[]
11644
 
** array. tolower() is used more often than toupper() by SQLite.
11645
 
**
11646
 
** Bit 0x40 is set if the character non-alphanumeric and can be used in an 
11647
 
** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
11648
 
** non-ASCII UTF character. Hence the test for whether or not a character is
11649
 
** part of an identifier is 0x46.
11650
 
**
11651
 
** SQLite's versions are identical to the standard versions assuming a
11652
 
** locale of "C". They are implemented as macros in sqliteInt.h.
11653
 
*/
11654
 
#ifdef SQLITE_ASCII
11655
 
SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
11656
 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
11657
 
  0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
11658
 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
11659
 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
11660
 
  0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
11661
 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
11662
 
  0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
11663
 
  0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
11664
 
 
11665
 
  0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
11666
 
  0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
11667
 
  0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
11668
 
  0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
11669
 
  0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
11670
 
  0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
11671
 
  0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
11672
 
  0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
11673
 
 
11674
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
11675
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
11676
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
11677
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
11678
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
11679
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
11680
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
11681
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
11682
 
 
11683
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
11684
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
11685
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
11686
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
11687
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
11688
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
11689
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
11690
 
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
11691
 
};
11692
 
#endif
11693
 
 
11694
 
 
11695
 
 
11696
 
/*
11697
 
** The following singleton contains the global configuration for
11698
 
** the SQLite library.
11699
 
*/
11700
 
SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
11701
 
   SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
11702
 
   1,                         /* bCoreMutex */
11703
 
   SQLITE_THREADSAFE==1,      /* bFullMutex */
11704
 
   0x7ffffffe,                /* mxStrlen */
11705
 
   100,                       /* szLookaside */
11706
 
   500,                       /* nLookaside */
11707
 
   {0,0,0,0,0,0,0,0},         /* m */
11708
 
   {0,0,0,0,0,0,0,0,0},       /* mutex */
11709
 
   {0,0,0,0,0,0,0,0,0,0,0},   /* pcache */
11710
 
   (void*)0,                  /* pHeap */
11711
 
   0,                         /* nHeap */
11712
 
   0, 0,                      /* mnHeap, mxHeap */
11713
 
   (void*)0,                  /* pScratch */
11714
 
   0,                         /* szScratch */
11715
 
   0,                         /* nScratch */
11716
 
   (void*)0,                  /* pPage */
11717
 
   0,                         /* szPage */
11718
 
   0,                         /* nPage */
11719
 
   0,                         /* mxParserStack */
11720
 
   0,                         /* sharedCacheEnabled */
11721
 
   /* All the rest should always be initialized to zero */
11722
 
   0,                         /* isInit */
11723
 
   0,                         /* inProgress */
11724
 
   0,                         /* isMutexInit */
11725
 
   0,                         /* isMallocInit */
11726
 
   0,                         /* isPCacheInit */
11727
 
   0,                         /* pInitMutex */
11728
 
   0,                         /* nRefInitMutex */
11729
 
   0,                         /* xLog */
11730
 
   0,                         /* pLogArg */
11731
 
};
11732
 
 
11733
 
 
11734
 
/*
11735
 
** Hash table for global functions - functions common to all
11736
 
** database connections.  After initialization, this table is
11737
 
** read-only.
11738
 
*/
11739
 
SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
11740
 
 
11741
 
/*
11742
 
** Constant tokens for values 0 and 1.
11743
 
*/
11744
 
SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
11745
 
   { "0", 1 },
11746
 
   { "1", 1 }
11747
 
};
11748
 
 
11749
 
 
11750
 
/*
11751
 
** The value of the "pending" byte must be 0x40000000 (1 byte past the
11752
 
** 1-gibabyte boundary) in a compatible database.  SQLite never uses
11753
 
** the database page that contains the pending byte.  It never attempts
11754
 
** to read or write that page.  The pending byte page is set assign
11755
 
** for use by the VFS layers as space for managing file locks.
11756
 
**
11757
 
** During testing, it is often desirable to move the pending byte to
11758
 
** a different position in the file.  This allows code that has to
11759
 
** deal with the pending byte to run on files that are much smaller
11760
 
** than 1 GiB.  The sqlite3_test_control() interface can be used to
11761
 
** move the pending byte.
11762
 
**
11763
 
** IMPORTANT:  Changing the pending byte to any value other than
11764
 
** 0x40000000 results in an incompatible database file format!
11765
 
** Changing the pending byte during operating results in undefined
11766
 
** and dileterious behavior.
11767
 
*/
11768
 
#ifndef SQLITE_OMIT_WSD
11769
 
SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
11770
 
#endif
11771
 
 
11772
 
/*
11773
 
** Properties of opcodes.  The OPFLG_INITIALIZER macro is
11774
 
** created by mkopcodeh.awk during compilation.  Data is obtained
11775
 
** from the comments following the "case OP_xxxx:" statements in
11776
 
** the vdbe.c file.  
11777
 
*/
11778
 
SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
11779
 
 
11780
 
/************** End of global.c **********************************************/
11781
 
/************** Begin file ctime.c *******************************************/
11782
 
/*
11783
 
** 2010 February 23
11784
 
**
11785
 
** The author disclaims copyright to this source code.  In place of
11786
 
** a legal notice, here is a blessing:
11787
 
**
11788
 
**    May you do good and not evil.
11789
 
**    May you find forgiveness for yourself and forgive others.
11790
 
**    May you share freely, never taking more than you give.
11791
 
**
11792
 
*************************************************************************
11793
 
**
11794
 
** This file implements routines used to report what compile-time options
11795
 
** SQLite was built with.
11796
 
*/
11797
 
 
11798
 
#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
11799
 
 
11800
 
 
11801
 
/*
11802
 
** An array of names of all compile-time options.  This array should 
11803
 
** be sorted A-Z.
11804
 
**
11805
 
** This array looks large, but in a typical installation actually uses
11806
 
** only a handful of compile-time options, so most times this array is usually
11807
 
** rather short and uses little memory space.
11808
 
*/
11809
 
static const char * const azCompileOpt[] = {
11810
 
 
11811
 
/* These macros are provided to "stringify" the value of the define
11812
 
** for those options in which the value is meaningful. */
11813
 
#define CTIMEOPT_VAL_(opt) #opt
11814
 
#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
11815
 
 
11816
 
#ifdef SQLITE_32BIT_ROWID
11817
 
  "32BIT_ROWID",
11818
 
#endif
11819
 
#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
11820
 
  "4_BYTE_ALIGNED_MALLOC",
11821
 
#endif
11822
 
#ifdef SQLITE_CASE_SENSITIVE_LIKE
11823
 
  "CASE_SENSITIVE_LIKE",
11824
 
#endif
11825
 
#ifdef SQLITE_CHECK_PAGES
11826
 
  "CHECK_PAGES",
11827
 
#endif
11828
 
#ifdef SQLITE_COVERAGE_TEST
11829
 
  "COVERAGE_TEST",
11830
 
#endif
11831
 
#ifdef SQLITE_DEBUG
11832
 
  "DEBUG",
11833
 
#endif
11834
 
#ifdef SQLITE_DEFAULT_LOCKING_MODE
11835
 
  "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
11836
 
#endif
11837
 
#ifdef SQLITE_DISABLE_DIRSYNC
11838
 
  "DISABLE_DIRSYNC",
11839
 
#endif
11840
 
#ifdef SQLITE_DISABLE_LFS
11841
 
  "DISABLE_LFS",
11842
 
#endif
11843
 
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
11844
 
  "ENABLE_ATOMIC_WRITE",
11845
 
#endif
11846
 
#ifdef SQLITE_ENABLE_CEROD
11847
 
  "ENABLE_CEROD",
11848
 
#endif
11849
 
#ifdef SQLITE_ENABLE_COLUMN_METADATA
11850
 
  "ENABLE_COLUMN_METADATA",
11851
 
#endif
11852
 
#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
11853
 
  "ENABLE_EXPENSIVE_ASSERT",
11854
 
#endif
11855
 
#ifdef SQLITE_ENABLE_FTS1
11856
 
  "ENABLE_FTS1",
11857
 
#endif
11858
 
#ifdef SQLITE_ENABLE_FTS2
11859
 
  "ENABLE_FTS2",
11860
 
#endif
11861
 
#ifdef SQLITE_ENABLE_FTS3
11862
 
  "ENABLE_FTS3",
11863
 
#endif
11864
 
#ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
11865
 
  "ENABLE_FTS3_PARENTHESIS",
11866
 
#endif
11867
 
#ifdef SQLITE_ENABLE_FTS4
11868
 
  "ENABLE_FTS4",
11869
 
#endif
11870
 
#ifdef SQLITE_ENABLE_ICU
11871
 
  "ENABLE_ICU",
11872
 
#endif
11873
 
#ifdef SQLITE_ENABLE_IOTRACE
11874
 
  "ENABLE_IOTRACE",
11875
 
#endif
11876
 
#ifdef SQLITE_ENABLE_LOAD_EXTENSION
11877
 
  "ENABLE_LOAD_EXTENSION",
11878
 
#endif
11879
 
#ifdef SQLITE_ENABLE_LOCKING_STYLE
11880
 
  "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
11881
 
#endif
11882
 
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
11883
 
  "ENABLE_MEMORY_MANAGEMENT",
11884
 
#endif
11885
 
#ifdef SQLITE_ENABLE_MEMSYS3
11886
 
  "ENABLE_MEMSYS3",
11887
 
#endif
11888
 
#ifdef SQLITE_ENABLE_MEMSYS5
11889
 
  "ENABLE_MEMSYS5",
11890
 
#endif
11891
 
#ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
11892
 
  "ENABLE_OVERSIZE_CELL_CHECK",
11893
 
#endif
11894
 
#ifdef SQLITE_ENABLE_RTREE
11895
 
  "ENABLE_RTREE",
11896
 
#endif
11897
 
#ifdef SQLITE_ENABLE_STAT2
11898
 
  "ENABLE_STAT2",
11899
 
#endif
11900
 
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
11901
 
  "ENABLE_UNLOCK_NOTIFY",
11902
 
#endif
11903
 
#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
11904
 
  "ENABLE_UPDATE_DELETE_LIMIT",
11905
 
#endif
11906
 
#ifdef SQLITE_HAS_CODEC
11907
 
  "HAS_CODEC",
11908
 
#endif
11909
 
#ifdef SQLITE_HAVE_ISNAN
11910
 
  "HAVE_ISNAN",
11911
 
#endif
11912
 
#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
11913
 
  "HOMEGROWN_RECURSIVE_MUTEX",
11914
 
#endif
11915
 
#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
11916
 
  "IGNORE_AFP_LOCK_ERRORS",
11917
 
#endif
11918
 
#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
11919
 
  "IGNORE_FLOCK_LOCK_ERRORS",
11920
 
#endif
11921
 
#ifdef SQLITE_INT64_TYPE
11922
 
  "INT64_TYPE",
11923
 
#endif
11924
 
#ifdef SQLITE_LOCK_TRACE
11925
 
  "LOCK_TRACE",
11926
 
#endif
11927
 
#ifdef SQLITE_MEMDEBUG
11928
 
  "MEMDEBUG",
11929
 
#endif
11930
 
#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
11931
 
  "MIXED_ENDIAN_64BIT_FLOAT",
11932
 
#endif
11933
 
#ifdef SQLITE_NO_SYNC
11934
 
  "NO_SYNC",
11935
 
#endif
11936
 
#ifdef SQLITE_OMIT_ALTERTABLE
11937
 
  "OMIT_ALTERTABLE",
11938
 
#endif
11939
 
#ifdef SQLITE_OMIT_ANALYZE
11940
 
  "OMIT_ANALYZE",
11941
 
#endif
11942
 
#ifdef SQLITE_OMIT_ATTACH
11943
 
  "OMIT_ATTACH",
11944
 
#endif
11945
 
#ifdef SQLITE_OMIT_AUTHORIZATION
11946
 
  "OMIT_AUTHORIZATION",
11947
 
#endif
11948
 
#ifdef SQLITE_OMIT_AUTOINCREMENT
11949
 
  "OMIT_AUTOINCREMENT",
11950
 
#endif
11951
 
#ifdef SQLITE_OMIT_AUTOINIT
11952
 
  "OMIT_AUTOINIT",
11953
 
#endif
11954
 
#ifdef SQLITE_OMIT_AUTOMATIC_INDEX
11955
 
  "OMIT_AUTOMATIC_INDEX",
11956
 
#endif
11957
 
#ifdef SQLITE_OMIT_AUTORESET
11958
 
  "OMIT_AUTORESET",
11959
 
#endif
11960
 
#ifdef SQLITE_OMIT_AUTOVACUUM
11961
 
  "OMIT_AUTOVACUUM",
11962
 
#endif
11963
 
#ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
11964
 
  "OMIT_BETWEEN_OPTIMIZATION",
11965
 
#endif
11966
 
#ifdef SQLITE_OMIT_BLOB_LITERAL
11967
 
  "OMIT_BLOB_LITERAL",
11968
 
#endif
11969
 
#ifdef SQLITE_OMIT_BTREECOUNT
11970
 
  "OMIT_BTREECOUNT",
11971
 
#endif
11972
 
#ifdef SQLITE_OMIT_BUILTIN_TEST
11973
 
  "OMIT_BUILTIN_TEST",
11974
 
#endif
11975
 
#ifdef SQLITE_OMIT_CAST
11976
 
  "OMIT_CAST",
11977
 
#endif
11978
 
#ifdef SQLITE_OMIT_CHECK
11979
 
  "OMIT_CHECK",
11980
 
#endif
11981
 
/* // redundant
11982
 
** #ifdef SQLITE_OMIT_COMPILEOPTION_DIAGS
11983
 
**   "OMIT_COMPILEOPTION_DIAGS",
11984
 
** #endif
11985
 
*/
11986
 
#ifdef SQLITE_OMIT_COMPLETE
11987
 
  "OMIT_COMPLETE",
11988
 
#endif
11989
 
#ifdef SQLITE_OMIT_COMPOUND_SELECT
11990
 
  "OMIT_COMPOUND_SELECT",
11991
 
#endif
11992
 
#ifdef SQLITE_OMIT_DATETIME_FUNCS
11993
 
  "OMIT_DATETIME_FUNCS",
11994
 
#endif
11995
 
#ifdef SQLITE_OMIT_DECLTYPE
11996
 
  "OMIT_DECLTYPE",
11997
 
#endif
11998
 
#ifdef SQLITE_OMIT_DEPRECATED
11999
 
  "OMIT_DEPRECATED",
12000
 
#endif
12001
 
#ifdef SQLITE_OMIT_DISKIO
12002
 
  "OMIT_DISKIO",
12003
 
#endif
12004
 
#ifdef SQLITE_OMIT_EXPLAIN
12005
 
  "OMIT_EXPLAIN",
12006
 
#endif
12007
 
#ifdef SQLITE_OMIT_FLAG_PRAGMAS
12008
 
  "OMIT_FLAG_PRAGMAS",
12009
 
#endif
12010
 
#ifdef SQLITE_OMIT_FLOATING_POINT
12011
 
  "OMIT_FLOATING_POINT",
12012
 
#endif
12013
 
#ifdef SQLITE_OMIT_FOREIGN_KEY
12014
 
  "OMIT_FOREIGN_KEY",
12015
 
#endif
12016
 
#ifdef SQLITE_OMIT_GET_TABLE
12017
 
  "OMIT_GET_TABLE",
12018
 
#endif
12019
 
#ifdef SQLITE_OMIT_INCRBLOB
12020
 
  "OMIT_INCRBLOB",
12021
 
#endif
12022
 
#ifdef SQLITE_OMIT_INTEGRITY_CHECK
12023
 
  "OMIT_INTEGRITY_CHECK",
12024
 
#endif
12025
 
#ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
12026
 
  "OMIT_LIKE_OPTIMIZATION",
12027
 
#endif
12028
 
#ifdef SQLITE_OMIT_LOAD_EXTENSION
12029
 
  "OMIT_LOAD_EXTENSION",
12030
 
#endif
12031
 
#ifdef SQLITE_OMIT_LOCALTIME
12032
 
  "OMIT_LOCALTIME",
12033
 
#endif
12034
 
#ifdef SQLITE_OMIT_LOOKASIDE
12035
 
  "OMIT_LOOKASIDE",
12036
 
#endif
12037
 
#ifdef SQLITE_OMIT_MEMORYDB
12038
 
  "OMIT_MEMORYDB",
12039
 
#endif
12040
 
#ifdef SQLITE_OMIT_OR_OPTIMIZATION
12041
 
  "OMIT_OR_OPTIMIZATION",
12042
 
#endif
12043
 
#ifdef SQLITE_OMIT_PAGER_PRAGMAS
12044
 
  "OMIT_PAGER_PRAGMAS",
12045
 
#endif
12046
 
#ifdef SQLITE_OMIT_PRAGMA
12047
 
  "OMIT_PRAGMA",
12048
 
#endif
12049
 
#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
12050
 
  "OMIT_PROGRESS_CALLBACK",
12051
 
#endif
12052
 
#ifdef SQLITE_OMIT_QUICKBALANCE
12053
 
  "OMIT_QUICKBALANCE",
12054
 
#endif
12055
 
#ifdef SQLITE_OMIT_REINDEX
12056
 
  "OMIT_REINDEX",
12057
 
#endif
12058
 
#ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
12059
 
  "OMIT_SCHEMA_PRAGMAS",
12060
 
#endif
12061
 
#ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
12062
 
  "OMIT_SCHEMA_VERSION_PRAGMAS",
12063
 
#endif
12064
 
#ifdef SQLITE_OMIT_SHARED_CACHE
12065
 
  "OMIT_SHARED_CACHE",
12066
 
#endif
12067
 
#ifdef SQLITE_OMIT_SUBQUERY
12068
 
  "OMIT_SUBQUERY",
12069
 
#endif
12070
 
#ifdef SQLITE_OMIT_TCL_VARIABLE
12071
 
  "OMIT_TCL_VARIABLE",
12072
 
#endif
12073
 
#ifdef SQLITE_OMIT_TEMPDB
12074
 
  "OMIT_TEMPDB",
12075
 
#endif
12076
 
#ifdef SQLITE_OMIT_TRACE
12077
 
  "OMIT_TRACE",
12078
 
#endif
12079
 
#ifdef SQLITE_OMIT_TRIGGER
12080
 
  "OMIT_TRIGGER",
12081
 
#endif
12082
 
#ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
12083
 
  "OMIT_TRUNCATE_OPTIMIZATION",
12084
 
#endif
12085
 
#ifdef SQLITE_OMIT_UTF16
12086
 
  "OMIT_UTF16",
12087
 
#endif
12088
 
#ifdef SQLITE_OMIT_VACUUM
12089
 
  "OMIT_VACUUM",
12090
 
#endif
12091
 
#ifdef SQLITE_OMIT_VIEW
12092
 
  "OMIT_VIEW",
12093
 
#endif
12094
 
#ifdef SQLITE_OMIT_VIRTUALTABLE
12095
 
  "OMIT_VIRTUALTABLE",
12096
 
#endif
12097
 
#ifdef SQLITE_OMIT_WAL
12098
 
  "OMIT_WAL",
12099
 
#endif
12100
 
#ifdef SQLITE_OMIT_WSD
12101
 
  "OMIT_WSD",
12102
 
#endif
12103
 
#ifdef SQLITE_OMIT_XFER_OPT
12104
 
  "OMIT_XFER_OPT",
12105
 
#endif
12106
 
#ifdef SQLITE_PERFORMANCE_TRACE
12107
 
  "PERFORMANCE_TRACE",
12108
 
#endif
12109
 
#ifdef SQLITE_PROXY_DEBUG
12110
 
  "PROXY_DEBUG",
12111
 
#endif
12112
 
#ifdef SQLITE_SECURE_DELETE
12113
 
  "SECURE_DELETE",
12114
 
#endif
12115
 
#ifdef SQLITE_SMALL_STACK
12116
 
  "SMALL_STACK",
12117
 
#endif
12118
 
#ifdef SQLITE_SOUNDEX
12119
 
  "SOUNDEX",
12120
 
#endif
12121
 
#ifdef SQLITE_TCL
12122
 
  "TCL",
12123
 
#endif
12124
 
#ifdef SQLITE_TEMP_STORE
12125
 
  "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
12126
 
#endif
12127
 
#ifdef SQLITE_TEST
12128
 
  "TEST",
12129
 
#endif
12130
 
#ifdef SQLITE_THREADSAFE
12131
 
  "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
12132
 
#endif
12133
 
#ifdef SQLITE_USE_ALLOCA
12134
 
  "USE_ALLOCA",
12135
 
#endif
12136
 
#ifdef SQLITE_ZERO_MALLOC
12137
 
  "ZERO_MALLOC"
12138
 
#endif
12139
 
};
12140
 
 
12141
 
/*
12142
 
** Given the name of a compile-time option, return true if that option
12143
 
** was used and false if not.
12144
 
**
12145
 
** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
12146
 
** is not required for a match.
12147
 
*/
12148
 
SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
12149
 
  int i, n;
12150
 
  if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
12151
 
  n = sqlite3Strlen30(zOptName);
12152
 
 
12153
 
  /* Since ArraySize(azCompileOpt) is normally in single digits, a
12154
 
  ** linear search is adequate.  No need for a binary search. */
12155
 
  for(i=0; i<ArraySize(azCompileOpt); i++){
12156
 
    if(   (sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0)
12157
 
       && ( (azCompileOpt[i][n]==0) || (azCompileOpt[i][n]=='=') ) ) return 1;
12158
 
  }
12159
 
  return 0;
12160
 
}
12161
 
 
12162
 
/*
12163
 
** Return the N-th compile-time option string.  If N is out of range,
12164
 
** return a NULL pointer.
12165
 
*/
12166
 
SQLITE_API const char *sqlite3_compileoption_get(int N){
12167
 
  if( N>=0 && N<ArraySize(azCompileOpt) ){
12168
 
    return azCompileOpt[N];
12169
 
  }
12170
 
  return 0;
12171
 
}
12172
 
 
12173
 
#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
12174
 
 
12175
 
/************** End of ctime.c ***********************************************/
12176
 
/************** Begin file status.c ******************************************/
12177
 
/*
12178
 
** 2008 June 18
12179
 
**
12180
 
** The author disclaims copyright to this source code.  In place of
12181
 
** a legal notice, here is a blessing:
12182
 
**
12183
 
**    May you do good and not evil.
12184
 
**    May you find forgiveness for yourself and forgive others.
12185
 
**    May you share freely, never taking more than you give.
12186
 
**
12187
 
*************************************************************************
12188
 
**
12189
 
** This module implements the sqlite3_status() interface and related
12190
 
** functionality.
12191
 
*/
12192
 
/************** Include vdbeInt.h in the middle of status.c ******************/
12193
 
/************** Begin file vdbeInt.h *****************************************/
12194
 
/*
12195
 
** 2003 September 6
12196
 
**
12197
 
** The author disclaims copyright to this source code.  In place of
12198
 
** a legal notice, here is a blessing:
12199
 
**
12200
 
**    May you do good and not evil.
12201
 
**    May you find forgiveness for yourself and forgive others.
12202
 
**    May you share freely, never taking more than you give.
12203
 
**
12204
 
*************************************************************************
12205
 
** This is the header file for information that is private to the
12206
 
** VDBE.  This information used to all be at the top of the single
12207
 
** source code file "vdbe.c".  When that file became too big (over
12208
 
** 6000 lines long) it was split up into several smaller files and
12209
 
** this header information was factored out.
12210
 
*/
12211
 
#ifndef _VDBEINT_H_
12212
 
#define _VDBEINT_H_
12213
 
 
12214
 
/*
12215
 
** SQL is translated into a sequence of instructions to be
12216
 
** executed by a virtual machine.  Each instruction is an instance
12217
 
** of the following structure.
12218
 
*/
12219
 
typedef struct VdbeOp Op;
12220
 
 
12221
 
/*
12222
 
** Boolean values
12223
 
*/
12224
 
typedef unsigned char Bool;
12225
 
 
12226
 
/*
12227
 
** A cursor is a pointer into a single BTree within a database file.
12228
 
** The cursor can seek to a BTree entry with a particular key, or
12229
 
** loop over all entries of the Btree.  You can also insert new BTree
12230
 
** entries or retrieve the key or data from the entry that the cursor
12231
 
** is currently pointing to.
12232
 
** 
12233
 
** Every cursor that the virtual machine has open is represented by an
12234
 
** instance of the following structure.
12235
 
*/
12236
 
struct VdbeCursor {
12237
 
  BtCursor *pCursor;    /* The cursor structure of the backend */
12238
 
  Btree *pBt;           /* Separate file holding temporary table */
12239
 
  KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
12240
 
  int iDb;              /* Index of cursor database in db->aDb[] (or -1) */
12241
 
  int pseudoTableReg;   /* Register holding pseudotable content. */
12242
 
  int nField;           /* Number of fields in the header */
12243
 
  Bool zeroed;          /* True if zeroed out and ready for reuse */
12244
 
  Bool rowidIsValid;    /* True if lastRowid is valid */
12245
 
  Bool atFirst;         /* True if pointing to first entry */
12246
 
  Bool useRandomRowid;  /* Generate new record numbers semi-randomly */
12247
 
  Bool nullRow;         /* True if pointing to a row with no data */
12248
 
  Bool deferredMoveto;  /* A call to sqlite3BtreeMoveto() is needed */
12249
 
  Bool isTable;         /* True if a table requiring integer keys */
12250
 
  Bool isIndex;         /* True if an index containing keys only - no data */
12251
 
  Bool isOrdered;       /* True if the underlying table is BTREE_UNORDERED */
12252
 
  sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
12253
 
  const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
12254
 
  i64 seqCount;         /* Sequence counter */
12255
 
  i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
12256
 
  i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
12257
 
 
12258
 
  /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or 
12259
 
  ** OP_IsUnique opcode on this cursor. */
12260
 
  int seekResult;
12261
 
 
12262
 
  /* Cached information about the header for the data record that the
12263
 
  ** cursor is currently pointing to.  Only valid if cacheStatus matches
12264
 
  ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
12265
 
  ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
12266
 
  ** the cache is out of date.
12267
 
  **
12268
 
  ** aRow might point to (ephemeral) data for the current row, or it might
12269
 
  ** be NULL.
12270
 
  */
12271
 
  u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
12272
 
  int payloadSize;      /* Total number of bytes in the record */
12273
 
  u32 *aType;           /* Type values for all entries in the record */
12274
 
  u32 *aOffset;         /* Cached offsets to the start of each columns data */
12275
 
  u8 *aRow;             /* Data for the current row, if all on one page */
12276
 
};
12277
 
typedef struct VdbeCursor VdbeCursor;
12278
 
 
12279
 
/*
12280
 
** When a sub-program is executed (OP_Program), a structure of this type
12281
 
** is allocated to store the current value of the program counter, as
12282
 
** well as the current memory cell array and various other frame specific
12283
 
** values stored in the Vdbe struct. When the sub-program is finished, 
12284
 
** these values are copied back to the Vdbe from the VdbeFrame structure,
12285
 
** restoring the state of the VM to as it was before the sub-program
12286
 
** began executing.
12287
 
**
12288
 
** The memory for a VdbeFrame object is allocated and managed by a memory
12289
 
** cell in the parent (calling) frame. When the memory cell is deleted or
12290
 
** overwritten, the VdbeFrame object is not freed immediately. Instead, it
12291
 
** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
12292
 
** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
12293
 
** this instead of deleting the VdbeFrame immediately is to avoid recursive
12294
 
** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
12295
 
** child frame are released.
12296
 
**
12297
 
** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
12298
 
** set to NULL if the currently executing frame is the main program.
12299
 
*/
12300
 
typedef struct VdbeFrame VdbeFrame;
12301
 
struct VdbeFrame {
12302
 
  Vdbe *v;                /* VM this frame belongs to */
12303
 
  int pc;                 /* Program Counter in parent (calling) frame */
12304
 
  Op *aOp;                /* Program instructions for parent frame */
12305
 
  int nOp;                /* Size of aOp array */
12306
 
  Mem *aMem;              /* Array of memory cells for parent frame */
12307
 
  int nMem;               /* Number of entries in aMem */
12308
 
  VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
12309
 
  u16 nCursor;            /* Number of entries in apCsr */
12310
 
  void *token;            /* Copy of SubProgram.token */
12311
 
  int nChildMem;          /* Number of memory cells for child frame */
12312
 
  int nChildCsr;          /* Number of cursors for child frame */
12313
 
  i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
12314
 
  int nChange;            /* Statement changes (Vdbe.nChanges)     */
12315
 
  VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
12316
 
};
12317
 
 
12318
 
#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
12319
 
 
12320
 
/*
12321
 
** A value for VdbeCursor.cacheValid that means the cache is always invalid.
12322
 
*/
12323
 
#define CACHE_STALE 0
12324
 
 
12325
 
/*
12326
 
** Internally, the vdbe manipulates nearly all SQL values as Mem
12327
 
** structures. Each Mem struct may cache multiple representations (string,
12328
 
** integer etc.) of the same value.
12329
 
*/
12330
 
struct Mem {
12331
 
  sqlite3 *db;        /* The associated database connection */
12332
 
  char *z;            /* String or BLOB value */
12333
 
  double r;           /* Real value */
12334
 
  union {
12335
 
    i64 i;              /* Integer value used when MEM_Int is set in flags */
12336
 
    int nZero;          /* Used when bit MEM_Zero is set in flags */
12337
 
    FuncDef *pDef;      /* Used only when flags==MEM_Agg */
12338
 
    RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
12339
 
    VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
12340
 
  } u;
12341
 
  int n;              /* Number of characters in string value, excluding '\0' */
12342
 
  u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
12343
 
  u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
12344
 
  u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
12345
 
#ifdef SQLITE_DEBUG
12346
 
  Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
12347
 
  void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
12348
 
#endif
12349
 
  void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
12350
 
  char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
12351
 
};
12352
 
 
12353
 
/* One or more of the following flags are set to indicate the validOK
12354
 
** representations of the value stored in the Mem struct.
12355
 
**
12356
 
** If the MEM_Null flag is set, then the value is an SQL NULL value.
12357
 
** No other flags may be set in this case.
12358
 
**
12359
 
** If the MEM_Str flag is set then Mem.z points at a string representation.
12360
 
** Usually this is encoded in the same unicode encoding as the main
12361
 
** database (see below for exceptions). If the MEM_Term flag is also
12362
 
** set, then the string is nul terminated. The MEM_Int and MEM_Real 
12363
 
** flags may coexist with the MEM_Str flag.
12364
 
*/
12365
 
#define MEM_Null      0x0001   /* Value is NULL */
12366
 
#define MEM_Str       0x0002   /* Value is a string */
12367
 
#define MEM_Int       0x0004   /* Value is an integer */
12368
 
#define MEM_Real      0x0008   /* Value is a real number */
12369
 
#define MEM_Blob      0x0010   /* Value is a BLOB */
12370
 
#define MEM_RowSet    0x0020   /* Value is a RowSet object */
12371
 
#define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
12372
 
#define MEM_Invalid   0x0080   /* Value is undefined */
12373
 
#define MEM_TypeMask  0x00ff   /* Mask of type bits */
12374
 
 
12375
 
/* Whenever Mem contains a valid string or blob representation, one of
12376
 
** the following flags must be set to determine the memory management
12377
 
** policy for Mem.z.  The MEM_Term flag tells us whether or not the
12378
 
** string is \000 or \u0000 terminated
12379
 
*/
12380
 
#define MEM_Term      0x0200   /* String rep is nul terminated */
12381
 
#define MEM_Dyn       0x0400   /* Need to call sqliteFree() on Mem.z */
12382
 
#define MEM_Static    0x0800   /* Mem.z points to a static string */
12383
 
#define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
12384
 
#define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
12385
 
#define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
12386
 
#ifdef SQLITE_OMIT_INCRBLOB
12387
 
  #undef MEM_Zero
12388
 
  #define MEM_Zero 0x0000
12389
 
#endif
12390
 
 
12391
 
/*
12392
 
** Clear any existing type flags from a Mem and replace them with f
12393
 
*/
12394
 
#define MemSetTypeFlag(p, f) \
12395
 
   ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
12396
 
 
12397
 
/*
12398
 
** Return true if a memory cell is not marked as invalid.  This macro
12399
 
** is for use inside assert() statements only.
12400
 
*/
12401
 
#ifdef SQLITE_DEBUG
12402
 
#define memIsValid(M)  ((M)->flags & MEM_Invalid)==0
12403
 
#endif
12404
 
 
12405
 
 
12406
 
/* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains
12407
 
** additional information about auxiliary information bound to arguments
12408
 
** of the function.  This is used to implement the sqlite3_get_auxdata()
12409
 
** and sqlite3_set_auxdata() APIs.  The "auxdata" is some auxiliary data
12410
 
** that can be associated with a constant argument to a function.  This
12411
 
** allows functions such as "regexp" to compile their constant regular
12412
 
** expression argument once and reused the compiled code for multiple
12413
 
** invocations.
12414
 
*/
12415
 
struct VdbeFunc {
12416
 
  FuncDef *pFunc;               /* The definition of the function */
12417
 
  int nAux;                     /* Number of entries allocated for apAux[] */
12418
 
  struct AuxData {
12419
 
    void *pAux;                   /* Aux data for the i-th argument */
12420
 
    void (*xDelete)(void *);      /* Destructor for the aux data */
12421
 
  } apAux[1];                   /* One slot for each function argument */
12422
 
};
12423
 
 
12424
 
/*
12425
 
** The "context" argument for a installable function.  A pointer to an
12426
 
** instance of this structure is the first argument to the routines used
12427
 
** implement the SQL functions.
12428
 
**
12429
 
** There is a typedef for this structure in sqlite.h.  So all routines,
12430
 
** even the public interface to SQLite, can use a pointer to this structure.
12431
 
** But this file is the only place where the internal details of this
12432
 
** structure are known.
12433
 
**
12434
 
** This structure is defined inside of vdbeInt.h because it uses substructures
12435
 
** (Mem) which are only defined there.
12436
 
*/
12437
 
struct sqlite3_context {
12438
 
  FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
12439
 
  VdbeFunc *pVdbeFunc;  /* Auxilary data, if created. */
12440
 
  Mem s;                /* The return value is stored here */
12441
 
  Mem *pMem;            /* Memory cell used to store aggregate context */
12442
 
  int isError;          /* Error code returned by the function. */
12443
 
  CollSeq *pColl;       /* Collating sequence */
12444
 
};
12445
 
 
12446
 
/*
12447
 
** An instance of the virtual machine.  This structure contains the complete
12448
 
** state of the virtual machine.
12449
 
**
12450
 
** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
12451
 
** is really a pointer to an instance of this structure.
12452
 
**
12453
 
** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
12454
 
** any virtual table method invocations made by the vdbe program. It is
12455
 
** set to 2 for xDestroy method calls and 1 for all other methods. This
12456
 
** variable is used for two purposes: to allow xDestroy methods to execute
12457
 
** "DROP TABLE" statements and to prevent some nasty side effects of
12458
 
** malloc failure when SQLite is invoked recursively by a virtual table 
12459
 
** method function.
12460
 
*/
12461
 
struct Vdbe {
12462
 
  sqlite3 *db;            /* The database connection that owns this statement */
12463
 
  Op *aOp;                /* Space to hold the virtual machine's program */
12464
 
  Mem *aMem;              /* The memory locations */
12465
 
  Mem **apArg;            /* Arguments to currently executing user function */
12466
 
  Mem *aColName;          /* Column names to return */
12467
 
  Mem *pResultSet;        /* Pointer to an array of results */
12468
 
  int nMem;               /* Number of memory locations currently allocated */
12469
 
  int nOp;                /* Number of instructions in the program */
12470
 
  int nOpAlloc;           /* Number of slots allocated for aOp[] */
12471
 
  int nLabel;             /* Number of labels used */
12472
 
  int nLabelAlloc;        /* Number of slots allocated in aLabel[] */
12473
 
  int *aLabel;            /* Space to hold the labels */
12474
 
  u16 nResColumn;         /* Number of columns in one row of the result set */
12475
 
  u16 nCursor;            /* Number of slots in apCsr[] */
12476
 
  u32 magic;              /* Magic number for sanity checking */
12477
 
  char *zErrMsg;          /* Error message written here */
12478
 
  Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
12479
 
  VdbeCursor **apCsr;     /* One element of this array for each open cursor */
12480
 
  Mem *aVar;              /* Values for the OP_Variable opcode. */
12481
 
  char **azVar;           /* Name of variables */
12482
 
  ynVar nVar;             /* Number of entries in aVar[] */
12483
 
  u32 cacheCtr;           /* VdbeCursor row cache generation counter */
12484
 
  int pc;                 /* The program counter */
12485
 
  int rc;                 /* Value to return */
12486
 
  u8 errorAction;         /* Recovery action to do in case of an error */
12487
 
  u8 okVar;               /* True if azVar[] has been initialized */
12488
 
  u8 explain;             /* True if EXPLAIN present on SQL command */
12489
 
  u8 changeCntOn;         /* True to update the change-counter */
12490
 
  u8 expired;             /* True if the VM needs to be recompiled */
12491
 
  u8 runOnlyOnce;         /* Automatically expire on reset */
12492
 
  u8 minWriteFileFormat;  /* Minimum file format for writable database files */
12493
 
  u8 inVtabMethod;        /* See comments above */
12494
 
  u8 usesStmtJournal;     /* True if uses a statement journal */
12495
 
  u8 readOnly;            /* True for read-only statements */
12496
 
  u8 isPrepareV2;         /* True if prepared with prepare_v2() */
12497
 
  int nChange;            /* Number of db changes made since last reset */
12498
 
  yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
12499
 
  yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
12500
 
  int iStatement;         /* Statement number (or 0 if has not opened stmt) */
12501
 
  int aCounter[3];        /* Counters used by sqlite3_stmt_status() */
12502
 
#ifndef SQLITE_OMIT_TRACE
12503
 
  i64 startTime;          /* Time when query started - used for profiling */
12504
 
#endif
12505
 
  i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
12506
 
  i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
12507
 
  char *zSql;             /* Text of the SQL statement that generated this */
12508
 
  void *pFree;            /* Free this when deleting the vdbe */
12509
 
#ifdef SQLITE_DEBUG
12510
 
  FILE *trace;            /* Write an execution trace here, if not NULL */
12511
 
#endif
12512
 
  VdbeFrame *pFrame;      /* Parent frame */
12513
 
  VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
12514
 
  int nFrame;             /* Number of frames in pFrame list */
12515
 
  u32 expmask;            /* Binding to these vars invalidates VM */
12516
 
  SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
12517
 
};
12518
 
 
12519
 
/*
12520
 
** The following are allowed values for Vdbe.magic
12521
 
*/
12522
 
#define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
12523
 
#define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
12524
 
#define VDBE_MAGIC_HALT     0x519c2973    /* VDBE has completed execution */
12525
 
#define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
12526
 
 
12527
 
/*
12528
 
** Function prototypes
12529
 
*/
12530
 
SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
12531
 
void sqliteVdbePopStack(Vdbe*,int);
12532
 
SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
12533
 
#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
12534
 
SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
12535
 
#endif
12536
 
SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
12537
 
SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
12538
 
SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
12539
 
SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
12540
 
SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
12541
 
 
12542
 
int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
12543
 
SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
12544
 
SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
12545
 
SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
12546
 
SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
12547
 
SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
12548
 
SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
12549
 
SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
12550
 
SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
12551
 
SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
12552
 
SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
12553
 
SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
12554
 
SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
12555
 
SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
12556
 
SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
12557
 
#ifdef SQLITE_OMIT_FLOATING_POINT
12558
 
# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
12559
 
#else
12560
 
SQLITE_PRIVATE   void sqlite3VdbeMemSetDouble(Mem*, double);
12561
 
#endif
12562
 
SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
12563
 
SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
12564
 
SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
12565
 
SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
12566
 
SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, int);
12567
 
SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
12568
 
SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
12569
 
SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
12570
 
SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
12571
 
SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
12572
 
SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
12573
 
SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
12574
 
SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
12575
 
SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
12576
 
SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
12577
 
SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
12578
 
SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
12579
 
SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
12580
 
SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
12581
 
SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
12582
 
SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
12583
 
 
12584
 
#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
12585
 
SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
12586
 
SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
12587
 
#else
12588
 
# define sqlite3VdbeEnter(X)
12589
 
# define sqlite3VdbeLeave(X)
12590
 
#endif
12591
 
 
12592
 
#ifdef SQLITE_DEBUG
12593
 
SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe*,Mem*);
12594
 
#endif
12595
 
 
12596
 
#ifndef SQLITE_OMIT_FOREIGN_KEY
12597
 
SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
12598
 
#else
12599
 
# define sqlite3VdbeCheckFk(p,i) 0
12600
 
#endif
12601
 
 
12602
 
SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
12603
 
#ifdef SQLITE_DEBUG
12604
 
SQLITE_PRIVATE   void sqlite3VdbePrintSql(Vdbe*);
12605
 
SQLITE_PRIVATE   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
12606
 
#endif
12607
 
SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
12608
 
 
12609
 
#ifndef SQLITE_OMIT_INCRBLOB
12610
 
SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
12611
 
#else
12612
 
  #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
12613
 
#endif
12614
 
 
12615
 
#endif /* !defined(_VDBEINT_H_) */
12616
 
 
12617
 
/************** End of vdbeInt.h *********************************************/
12618
 
/************** Continuing where we left off in status.c *********************/
12619
 
 
12620
 
/*
12621
 
** Variables in which to record status information.
12622
 
*/
12623
 
typedef struct sqlite3StatType sqlite3StatType;
12624
 
static SQLITE_WSD struct sqlite3StatType {
12625
 
  int nowValue[10];         /* Current value */
12626
 
  int mxValue[10];          /* Maximum value */
12627
 
} sqlite3Stat = { {0,}, {0,} };
12628
 
 
12629
 
 
12630
 
/* The "wsdStat" macro will resolve to the status information
12631
 
** state vector.  If writable static data is unsupported on the target,
12632
 
** we have to locate the state vector at run-time.  In the more common
12633
 
** case where writable static data is supported, wsdStat can refer directly
12634
 
** to the "sqlite3Stat" state vector declared above.
12635
 
*/
12636
 
#ifdef SQLITE_OMIT_WSD
12637
 
# define wsdStatInit  sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
12638
 
# define wsdStat x[0]
12639
 
#else
12640
 
# define wsdStatInit
12641
 
# define wsdStat sqlite3Stat
12642
 
#endif
12643
 
 
12644
 
/*
12645
 
** Return the current value of a status parameter.
12646
 
*/
12647
 
SQLITE_PRIVATE int sqlite3StatusValue(int op){
12648
 
  wsdStatInit;
12649
 
  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
12650
 
  return wsdStat.nowValue[op];
12651
 
}
12652
 
 
12653
 
/*
12654
 
** Add N to the value of a status record.  It is assumed that the
12655
 
** caller holds appropriate locks.
12656
 
*/
12657
 
SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){
12658
 
  wsdStatInit;
12659
 
  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
12660
 
  wsdStat.nowValue[op] += N;
12661
 
  if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
12662
 
    wsdStat.mxValue[op] = wsdStat.nowValue[op];
12663
 
  }
12664
 
}
12665
 
 
12666
 
/*
12667
 
** Set the value of a status to X.
12668
 
*/
12669
 
SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
12670
 
  wsdStatInit;
12671
 
  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
12672
 
  wsdStat.nowValue[op] = X;
12673
 
  if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
12674
 
    wsdStat.mxValue[op] = wsdStat.nowValue[op];
12675
 
  }
12676
 
}
12677
 
 
12678
 
/*
12679
 
** Query status information.
12680
 
**
12681
 
** This implementation assumes that reading or writing an aligned
12682
 
** 32-bit integer is an atomic operation.  If that assumption is not true,
12683
 
** then this routine is not threadsafe.
12684
 
*/
12685
 
SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
12686
 
  wsdStatInit;
12687
 
  if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
12688
 
    return SQLITE_MISUSE_BKPT;
12689
 
  }
12690
 
  *pCurrent = wsdStat.nowValue[op];
12691
 
  *pHighwater = wsdStat.mxValue[op];
12692
 
  if( resetFlag ){
12693
 
    wsdStat.mxValue[op] = wsdStat.nowValue[op];
12694
 
  }
12695
 
  return SQLITE_OK;
12696
 
}
12697
 
 
12698
 
/*
12699
 
** Query status information for a single database connection
12700
 
*/
12701
 
SQLITE_API int sqlite3_db_status(
12702
 
  sqlite3 *db,          /* The database connection whose status is desired */
12703
 
  int op,               /* Status verb */
12704
 
  int *pCurrent,        /* Write current value here */
12705
 
  int *pHighwater,      /* Write high-water mark here */
12706
 
  int resetFlag         /* Reset high-water mark if true */
12707
 
){
12708
 
  int rc = SQLITE_OK;   /* Return code */
12709
 
  sqlite3_mutex_enter(db->mutex);
12710
 
  switch( op ){
12711
 
    case SQLITE_DBSTATUS_LOOKASIDE_USED: {
12712
 
      *pCurrent = db->lookaside.nOut;
12713
 
      *pHighwater = db->lookaside.mxOut;
12714
 
      if( resetFlag ){
12715
 
        db->lookaside.mxOut = db->lookaside.nOut;
12716
 
      }
12717
 
      break;
12718
 
    }
12719
 
 
12720
 
    case SQLITE_DBSTATUS_LOOKASIDE_HIT:
12721
 
    case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
12722
 
    case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
12723
 
      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
12724
 
      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
12725
 
      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
12726
 
      assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
12727
 
      assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
12728
 
      *pCurrent = 0;
12729
 
      *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
12730
 
      if( resetFlag ){
12731
 
        db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
12732
 
      }
12733
 
      break;
12734
 
    }
12735
 
 
12736
 
    /* 
12737
 
    ** Return an approximation for the amount of memory currently used
12738
 
    ** by all pagers associated with the given database connection.  The
12739
 
    ** highwater mark is meaningless and is returned as zero.
12740
 
    */
12741
 
    case SQLITE_DBSTATUS_CACHE_USED: {
12742
 
      int totalUsed = 0;
12743
 
      int i;
12744
 
      sqlite3BtreeEnterAll(db);
12745
 
      for(i=0; i<db->nDb; i++){
12746
 
        Btree *pBt = db->aDb[i].pBt;
12747
 
        if( pBt ){
12748
 
          Pager *pPager = sqlite3BtreePager(pBt);
12749
 
          totalUsed += sqlite3PagerMemUsed(pPager);
12750
 
        }
12751
 
      }
12752
 
      sqlite3BtreeLeaveAll(db);
12753
 
      *pCurrent = totalUsed;
12754
 
      *pHighwater = 0;
12755
 
      break;
12756
 
    }
12757
 
 
12758
 
    /*
12759
 
    ** *pCurrent gets an accurate estimate of the amount of memory used
12760
 
    ** to store the schema for all databases (main, temp, and any ATTACHed
12761
 
    ** databases.  *pHighwater is set to zero.
12762
 
    */
12763
 
    case SQLITE_DBSTATUS_SCHEMA_USED: {
12764
 
      int i;                      /* Used to iterate through schemas */
12765
 
      int nByte = 0;              /* Used to accumulate return value */
12766
 
 
12767
 
      sqlite3BtreeEnterAll(db);
12768
 
      db->pnBytesFreed = &nByte;
12769
 
      for(i=0; i<db->nDb; i++){
12770
 
        Schema *pSchema = db->aDb[i].pSchema;
12771
 
        if( ALWAYS(pSchema!=0) ){
12772
 
          HashElem *p;
12773
 
 
12774
 
          nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
12775
 
              pSchema->tblHash.count 
12776
 
            + pSchema->trigHash.count
12777
 
            + pSchema->idxHash.count
12778
 
            + pSchema->fkeyHash.count
12779
 
          );
12780
 
          nByte += sqlite3MallocSize(pSchema->tblHash.ht);
12781
 
          nByte += sqlite3MallocSize(pSchema->trigHash.ht);
12782
 
          nByte += sqlite3MallocSize(pSchema->idxHash.ht);
12783
 
          nByte += sqlite3MallocSize(pSchema->fkeyHash.ht);
12784
 
 
12785
 
          for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
12786
 
            sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
12787
 
          }
12788
 
          for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
12789
 
            sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
12790
 
          }
12791
 
        }
12792
 
      }
12793
 
      db->pnBytesFreed = 0;
12794
 
      sqlite3BtreeLeaveAll(db);
12795
 
 
12796
 
      *pHighwater = 0;
12797
 
      *pCurrent = nByte;
12798
 
      break;
12799
 
    }
12800
 
 
12801
 
    /*
12802
 
    ** *pCurrent gets an accurate estimate of the amount of memory used
12803
 
    ** to store all prepared statements.
12804
 
    ** *pHighwater is set to zero.
12805
 
    */
12806
 
    case SQLITE_DBSTATUS_STMT_USED: {
12807
 
      struct Vdbe *pVdbe;         /* Used to iterate through VMs */
12808
 
      int nByte = 0;              /* Used to accumulate return value */
12809
 
 
12810
 
      db->pnBytesFreed = &nByte;
12811
 
      for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
12812
 
        sqlite3VdbeDeleteObject(db, pVdbe);
12813
 
      }
12814
 
      db->pnBytesFreed = 0;
12815
 
 
12816
 
      *pHighwater = 0;
12817
 
      *pCurrent = nByte;
12818
 
 
12819
 
      break;
12820
 
    }
12821
 
 
12822
 
    default: {
12823
 
      rc = SQLITE_ERROR;
12824
 
    }
12825
 
  }
12826
 
  sqlite3_mutex_leave(db->mutex);
12827
 
  return rc;
12828
 
}
12829
 
 
12830
 
/************** End of status.c **********************************************/
12831
 
/************** Begin file date.c ********************************************/
12832
 
/*
12833
 
** 2003 October 31
12834
 
**
12835
 
** The author disclaims copyright to this source code.  In place of
12836
 
** a legal notice, here is a blessing:
12837
 
**
12838
 
**    May you do good and not evil.
12839
 
**    May you find forgiveness for yourself and forgive others.
12840
 
**    May you share freely, never taking more than you give.
12841
 
**
12842
 
*************************************************************************
12843
 
** This file contains the C functions that implement date and time
12844
 
** functions for SQLite.  
12845
 
**
12846
 
** There is only one exported symbol in this file - the function
12847
 
** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
12848
 
** All other code has file scope.
12849
 
**
12850
 
** SQLite processes all times and dates as Julian Day numbers.  The
12851
 
** dates and times are stored as the number of days since noon
12852
 
** in Greenwich on November 24, 4714 B.C. according to the Gregorian
12853
 
** calendar system. 
12854
 
**
12855
 
** 1970-01-01 00:00:00 is JD 2440587.5
12856
 
** 2000-01-01 00:00:00 is JD 2451544.5
12857
 
**
12858
 
** This implemention requires years to be expressed as a 4-digit number
12859
 
** which means that only dates between 0000-01-01 and 9999-12-31 can
12860
 
** be represented, even though julian day numbers allow a much wider
12861
 
** range of dates.
12862
 
**
12863
 
** The Gregorian calendar system is used for all dates and times,
12864
 
** even those that predate the Gregorian calendar.  Historians usually
12865
 
** use the Julian calendar for dates prior to 1582-10-15 and for some
12866
 
** dates afterwards, depending on locale.  Beware of this difference.
12867
 
**
12868
 
** The conversion algorithms are implemented based on descriptions
12869
 
** in the following text:
12870
 
**
12871
 
**      Jean Meeus
12872
 
**      Astronomical Algorithms, 2nd Edition, 1998
12873
 
**      ISBM 0-943396-61-1
12874
 
**      Willmann-Bell, Inc
12875
 
**      Richmond, Virginia (USA)
12876
 
*/
12877
 
#include <time.h>
12878
 
 
12879
 
#ifndef SQLITE_OMIT_DATETIME_FUNCS
12880
 
 
12881
 
/*
12882
 
** On recent Windows platforms, the localtime_s() function is available
12883
 
** as part of the "Secure CRT". It is essentially equivalent to 
12884
 
** localtime_r() available under most POSIX platforms, except that the 
12885
 
** order of the parameters is reversed.
12886
 
**
12887
 
** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
12888
 
**
12889
 
** If the user has not indicated to use localtime_r() or localtime_s()
12890
 
** already, check for an MSVC build environment that provides 
12891
 
** localtime_s().
12892
 
*/
12893
 
#if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \
12894
 
     defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
12895
 
#define HAVE_LOCALTIME_S 1
12896
 
#endif
12897
 
 
12898
 
/*
12899
 
** A structure for holding a single date and time.
12900
 
*/
12901
 
typedef struct DateTime DateTime;
12902
 
struct DateTime {
12903
 
  sqlite3_int64 iJD; /* The julian day number times 86400000 */
12904
 
  int Y, M, D;       /* Year, month, and day */
12905
 
  int h, m;          /* Hour and minutes */
12906
 
  int tz;            /* Timezone offset in minutes */
12907
 
  double s;          /* Seconds */
12908
 
  char validYMD;     /* True (1) if Y,M,D are valid */
12909
 
  char validHMS;     /* True (1) if h,m,s are valid */
12910
 
  char validJD;      /* True (1) if iJD is valid */
12911
 
  char validTZ;      /* True (1) if tz is valid */
12912
 
};
12913
 
 
12914
 
 
12915
 
/*
12916
 
** Convert zDate into one or more integers.  Additional arguments
12917
 
** come in groups of 5 as follows:
12918
 
**
12919
 
**       N       number of digits in the integer
12920
 
**       min     minimum allowed value of the integer
12921
 
**       max     maximum allowed value of the integer
12922
 
**       nextC   first character after the integer
12923
 
**       pVal    where to write the integers value.
12924
 
**
12925
 
** Conversions continue until one with nextC==0 is encountered.
12926
 
** The function returns the number of successful conversions.
12927
 
*/
12928
 
static int getDigits(const char *zDate, ...){
12929
 
  va_list ap;
12930
 
  int val;
12931
 
  int N;
12932
 
  int min;
12933
 
  int max;
12934
 
  int nextC;
12935
 
  int *pVal;
12936
 
  int cnt = 0;
12937
 
  va_start(ap, zDate);
12938
 
  do{
12939
 
    N = va_arg(ap, int);
12940
 
    min = va_arg(ap, int);
12941
 
    max = va_arg(ap, int);
12942
 
    nextC = va_arg(ap, int);
12943
 
    pVal = va_arg(ap, int*);
12944
 
    val = 0;
12945
 
    while( N-- ){
12946
 
      if( !sqlite3Isdigit(*zDate) ){
12947
 
        goto end_getDigits;
12948
 
      }
12949
 
      val = val*10 + *zDate - '0';
12950
 
      zDate++;
12951
 
    }
12952
 
    if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
12953
 
      goto end_getDigits;
12954
 
    }
12955
 
    *pVal = val;
12956
 
    zDate++;
12957
 
    cnt++;
12958
 
  }while( nextC );
12959
 
end_getDigits:
12960
 
  va_end(ap);
12961
 
  return cnt;
12962
 
}
12963
 
 
12964
 
/*
12965
 
** Parse a timezone extension on the end of a date-time.
12966
 
** The extension is of the form:
12967
 
**
12968
 
**        (+/-)HH:MM
12969
 
**
12970
 
** Or the "zulu" notation:
12971
 
**
12972
 
**        Z
12973
 
**
12974
 
** If the parse is successful, write the number of minutes
12975
 
** of change in p->tz and return 0.  If a parser error occurs,
12976
 
** return non-zero.
12977
 
**
12978
 
** A missing specifier is not considered an error.
12979
 
*/
12980
 
static int parseTimezone(const char *zDate, DateTime *p){
12981
 
  int sgn = 0;
12982
 
  int nHr, nMn;
12983
 
  int c;
12984
 
  while( sqlite3Isspace(*zDate) ){ zDate++; }
12985
 
  p->tz = 0;
12986
 
  c = *zDate;
12987
 
  if( c=='-' ){
12988
 
    sgn = -1;
12989
 
  }else if( c=='+' ){
12990
 
    sgn = +1;
12991
 
  }else if( c=='Z' || c=='z' ){
12992
 
    zDate++;
12993
 
    goto zulu_time;
12994
 
  }else{
12995
 
    return c!=0;
12996
 
  }
12997
 
  zDate++;
12998
 
  if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
12999
 
    return 1;
13000
 
  }
13001
 
  zDate += 5;
13002
 
  p->tz = sgn*(nMn + nHr*60);
13003
 
zulu_time:
13004
 
  while( sqlite3Isspace(*zDate) ){ zDate++; }
13005
 
  return *zDate!=0;
13006
 
}
13007
 
 
13008
 
/*
13009
 
** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
13010
 
** The HH, MM, and SS must each be exactly 2 digits.  The
13011
 
** fractional seconds FFFF can be one or more digits.
13012
 
**
13013
 
** Return 1 if there is a parsing error and 0 on success.
13014
 
*/
13015
 
static int parseHhMmSs(const char *zDate, DateTime *p){
13016
 
  int h, m, s;
13017
 
  double ms = 0.0;
13018
 
  if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
13019
 
    return 1;
13020
 
  }
13021
 
  zDate += 5;
13022
 
  if( *zDate==':' ){
13023
 
    zDate++;
13024
 
    if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
13025
 
      return 1;
13026
 
    }
13027
 
    zDate += 2;
13028
 
    if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
13029
 
      double rScale = 1.0;
13030
 
      zDate++;
13031
 
      while( sqlite3Isdigit(*zDate) ){
13032
 
        ms = ms*10.0 + *zDate - '0';
13033
 
        rScale *= 10.0;
13034
 
        zDate++;
13035
 
      }
13036
 
      ms /= rScale;
13037
 
    }
13038
 
  }else{
13039
 
    s = 0;
13040
 
  }
13041
 
  p->validJD = 0;
13042
 
  p->validHMS = 1;
13043
 
  p->h = h;
13044
 
  p->m = m;
13045
 
  p->s = s + ms;
13046
 
  if( parseTimezone(zDate, p) ) return 1;
13047
 
  p->validTZ = (p->tz!=0)?1:0;
13048
 
  return 0;
13049
 
}
13050
 
 
13051
 
/*
13052
 
** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
13053
 
** that the YYYY-MM-DD is according to the Gregorian calendar.
13054
 
**
13055
 
** Reference:  Meeus page 61
13056
 
*/
13057
 
static void computeJD(DateTime *p){
13058
 
  int Y, M, D, A, B, X1, X2;
13059
 
 
13060
 
  if( p->validJD ) return;
13061
 
  if( p->validYMD ){
13062
 
    Y = p->Y;
13063
 
    M = p->M;
13064
 
    D = p->D;
13065
 
  }else{
13066
 
    Y = 2000;  /* If no YMD specified, assume 2000-Jan-01 */
13067
 
    M = 1;
13068
 
    D = 1;
13069
 
  }
13070
 
  if( M<=2 ){
13071
 
    Y--;
13072
 
    M += 12;
13073
 
  }
13074
 
  A = Y/100;
13075
 
  B = 2 - A + (A/4);
13076
 
  X1 = 36525*(Y+4716)/100;
13077
 
  X2 = 306001*(M+1)/10000;
13078
 
  p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
13079
 
  p->validJD = 1;
13080
 
  if( p->validHMS ){
13081
 
    p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
13082
 
    if( p->validTZ ){
13083
 
      p->iJD -= p->tz*60000;
13084
 
      p->validYMD = 0;
13085
 
      p->validHMS = 0;
13086
 
      p->validTZ = 0;
13087
 
    }
13088
 
  }
13089
 
}
13090
 
 
13091
 
/*
13092
 
** Parse dates of the form
13093
 
**
13094
 
**     YYYY-MM-DD HH:MM:SS.FFF
13095
 
**     YYYY-MM-DD HH:MM:SS
13096
 
**     YYYY-MM-DD HH:MM
13097
 
**     YYYY-MM-DD
13098
 
**
13099
 
** Write the result into the DateTime structure and return 0
13100
 
** on success and 1 if the input string is not a well-formed
13101
 
** date.
13102
 
*/
13103
 
static int parseYyyyMmDd(const char *zDate, DateTime *p){
13104
 
  int Y, M, D, neg;
13105
 
 
13106
 
  if( zDate[0]=='-' ){
13107
 
    zDate++;
13108
 
    neg = 1;
13109
 
  }else{
13110
 
    neg = 0;
13111
 
  }
13112
 
  if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
13113
 
    return 1;
13114
 
  }
13115
 
  zDate += 10;
13116
 
  while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
13117
 
  if( parseHhMmSs(zDate, p)==0 ){
13118
 
    /* We got the time */
13119
 
  }else if( *zDate==0 ){
13120
 
    p->validHMS = 0;
13121
 
  }else{
13122
 
    return 1;
13123
 
  }
13124
 
  p->validJD = 0;
13125
 
  p->validYMD = 1;
13126
 
  p->Y = neg ? -Y : Y;
13127
 
  p->M = M;
13128
 
  p->D = D;
13129
 
  if( p->validTZ ){
13130
 
    computeJD(p);
13131
 
  }
13132
 
  return 0;
13133
 
}
13134
 
 
13135
 
/*
13136
 
** Set the time to the current time reported by the VFS
13137
 
*/
13138
 
static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
13139
 
  sqlite3 *db = sqlite3_context_db_handle(context);
13140
 
  sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD);
13141
 
  p->validJD = 1;
13142
 
}
13143
 
 
13144
 
/*
13145
 
** Attempt to parse the given string into a Julian Day Number.  Return
13146
 
** the number of errors.
13147
 
**
13148
 
** The following are acceptable forms for the input string:
13149
 
**
13150
 
**      YYYY-MM-DD HH:MM:SS.FFF  +/-HH:MM
13151
 
**      DDDD.DD 
13152
 
**      now
13153
 
**
13154
 
** In the first form, the +/-HH:MM is always optional.  The fractional
13155
 
** seconds extension (the ".FFF") is optional.  The seconds portion
13156
 
** (":SS.FFF") is option.  The year and date can be omitted as long
13157
 
** as there is a time string.  The time string can be omitted as long
13158
 
** as there is a year and date.
13159
 
*/
13160
 
static int parseDateOrTime(
13161
 
  sqlite3_context *context, 
13162
 
  const char *zDate, 
13163
 
  DateTime *p
13164
 
){
13165
 
  double r;
13166
 
  if( parseYyyyMmDd(zDate,p)==0 ){
13167
 
    return 0;
13168
 
  }else if( parseHhMmSs(zDate, p)==0 ){
13169
 
    return 0;
13170
 
  }else if( sqlite3StrICmp(zDate,"now")==0){
13171
 
    setDateTimeToCurrent(context, p);
13172
 
    return 0;
13173
 
  }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
13174
 
    p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
13175
 
    p->validJD = 1;
13176
 
    return 0;
13177
 
  }
13178
 
  return 1;
13179
 
}
13180
 
 
13181
 
/*
13182
 
** Compute the Year, Month, and Day from the julian day number.
13183
 
*/
13184
 
static void computeYMD(DateTime *p){
13185
 
  int Z, A, B, C, D, E, X1;
13186
 
  if( p->validYMD ) return;
13187
 
  if( !p->validJD ){
13188
 
    p->Y = 2000;
13189
 
    p->M = 1;
13190
 
    p->D = 1;
13191
 
  }else{
13192
 
    Z = (int)((p->iJD + 43200000)/86400000);
13193
 
    A = (int)((Z - 1867216.25)/36524.25);
13194
 
    A = Z + 1 + A - (A/4);
13195
 
    B = A + 1524;
13196
 
    C = (int)((B - 122.1)/365.25);
13197
 
    D = (36525*C)/100;
13198
 
    E = (int)((B-D)/30.6001);
13199
 
    X1 = (int)(30.6001*E);
13200
 
    p->D = B - D - X1;
13201
 
    p->M = E<14 ? E-1 : E-13;
13202
 
    p->Y = p->M>2 ? C - 4716 : C - 4715;
13203
 
  }
13204
 
  p->validYMD = 1;
13205
 
}
13206
 
 
13207
 
/*
13208
 
** Compute the Hour, Minute, and Seconds from the julian day number.
13209
 
*/
13210
 
static void computeHMS(DateTime *p){
13211
 
  int s;
13212
 
  if( p->validHMS ) return;
13213
 
  computeJD(p);
13214
 
  s = (int)((p->iJD + 43200000) % 86400000);
13215
 
  p->s = s/1000.0;
13216
 
  s = (int)p->s;
13217
 
  p->s -= s;
13218
 
  p->h = s/3600;
13219
 
  s -= p->h*3600;
13220
 
  p->m = s/60;
13221
 
  p->s += s - p->m*60;
13222
 
  p->validHMS = 1;
13223
 
}
13224
 
 
13225
 
/*
13226
 
** Compute both YMD and HMS
13227
 
*/
13228
 
static void computeYMD_HMS(DateTime *p){
13229
 
  computeYMD(p);
13230
 
  computeHMS(p);
13231
 
}
13232
 
 
13233
 
/*
13234
 
** Clear the YMD and HMS and the TZ
13235
 
*/
13236
 
static void clearYMD_HMS_TZ(DateTime *p){
13237
 
  p->validYMD = 0;
13238
 
  p->validHMS = 0;
13239
 
  p->validTZ = 0;
13240
 
}
13241
 
 
13242
 
#ifndef SQLITE_OMIT_LOCALTIME
13243
 
/*
13244
 
** Compute the difference (in milliseconds)
13245
 
** between localtime and UTC (a.k.a. GMT)
13246
 
** for the time value p where p is in UTC.
13247
 
*/
13248
 
static sqlite3_int64 localtimeOffset(DateTime *p){
13249
 
  DateTime x, y;
13250
 
  time_t t;
13251
 
  x = *p;
13252
 
  computeYMD_HMS(&x);
13253
 
  if( x.Y<1971 || x.Y>=2038 ){
13254
 
    x.Y = 2000;
13255
 
    x.M = 1;
13256
 
    x.D = 1;
13257
 
    x.h = 0;
13258
 
    x.m = 0;
13259
 
    x.s = 0.0;
13260
 
  } else {
13261
 
    int s = (int)(x.s + 0.5);
13262
 
    x.s = s;
13263
 
  }
13264
 
  x.tz = 0;
13265
 
  x.validJD = 0;
13266
 
  computeJD(&x);
13267
 
  t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
13268
 
#ifdef HAVE_LOCALTIME_R
13269
 
  {
13270
 
    struct tm sLocal;
13271
 
    localtime_r(&t, &sLocal);
13272
 
    y.Y = sLocal.tm_year + 1900;
13273
 
    y.M = sLocal.tm_mon + 1;
13274
 
    y.D = sLocal.tm_mday;
13275
 
    y.h = sLocal.tm_hour;
13276
 
    y.m = sLocal.tm_min;
13277
 
    y.s = sLocal.tm_sec;
13278
 
  }
13279
 
#elif defined(HAVE_LOCALTIME_S) && HAVE_LOCALTIME_S
13280
 
  {
13281
 
    struct tm sLocal;
13282
 
    localtime_s(&sLocal, &t);
13283
 
    y.Y = sLocal.tm_year + 1900;
13284
 
    y.M = sLocal.tm_mon + 1;
13285
 
    y.D = sLocal.tm_mday;
13286
 
    y.h = sLocal.tm_hour;
13287
 
    y.m = sLocal.tm_min;
13288
 
    y.s = sLocal.tm_sec;
13289
 
  }
13290
 
#else
13291
 
  {
13292
 
    struct tm *pTm;
13293
 
    sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
13294
 
    pTm = localtime(&t);
13295
 
    y.Y = pTm->tm_year + 1900;
13296
 
    y.M = pTm->tm_mon + 1;
13297
 
    y.D = pTm->tm_mday;
13298
 
    y.h = pTm->tm_hour;
13299
 
    y.m = pTm->tm_min;
13300
 
    y.s = pTm->tm_sec;
13301
 
    sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
13302
 
  }
13303
 
#endif
13304
 
  y.validYMD = 1;
13305
 
  y.validHMS = 1;
13306
 
  y.validJD = 0;
13307
 
  y.validTZ = 0;
13308
 
  computeJD(&y);
13309
 
  return y.iJD - x.iJD;
13310
 
}
13311
 
#endif /* SQLITE_OMIT_LOCALTIME */
13312
 
 
13313
 
/*
13314
 
** Process a modifier to a date-time stamp.  The modifiers are
13315
 
** as follows:
13316
 
**
13317
 
**     NNN days
13318
 
**     NNN hours
13319
 
**     NNN minutes
13320
 
**     NNN.NNNN seconds
13321
 
**     NNN months
13322
 
**     NNN years
13323
 
**     start of month
13324
 
**     start of year
13325
 
**     start of week
13326
 
**     start of day
13327
 
**     weekday N
13328
 
**     unixepoch
13329
 
**     localtime
13330
 
**     utc
13331
 
**
13332
 
** Return 0 on success and 1 if there is any kind of error.
13333
 
*/
13334
 
static int parseModifier(const char *zMod, DateTime *p){
13335
 
  int rc = 1;
13336
 
  int n;
13337
 
  double r;
13338
 
  char *z, zBuf[30];
13339
 
  z = zBuf;
13340
 
  for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
13341
 
    z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
13342
 
  }
13343
 
  z[n] = 0;
13344
 
  switch( z[0] ){
13345
 
#ifndef SQLITE_OMIT_LOCALTIME
13346
 
    case 'l': {
13347
 
      /*    localtime
13348
 
      **
13349
 
      ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
13350
 
      ** show local time.
13351
 
      */
13352
 
      if( strcmp(z, "localtime")==0 ){
13353
 
        computeJD(p);
13354
 
        p->iJD += localtimeOffset(p);
13355
 
        clearYMD_HMS_TZ(p);
13356
 
        rc = 0;
13357
 
      }
13358
 
      break;
13359
 
    }
13360
 
#endif
13361
 
    case 'u': {
13362
 
      /*
13363
 
      **    unixepoch
13364
 
      **
13365
 
      ** Treat the current value of p->iJD as the number of
13366
 
      ** seconds since 1970.  Convert to a real julian day number.
13367
 
      */
13368
 
      if( strcmp(z, "unixepoch")==0 && p->validJD ){
13369
 
        p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
13370
 
        clearYMD_HMS_TZ(p);
13371
 
        rc = 0;
13372
 
      }
13373
 
#ifndef SQLITE_OMIT_LOCALTIME
13374
 
      else if( strcmp(z, "utc")==0 ){
13375
 
        sqlite3_int64 c1;
13376
 
        computeJD(p);
13377
 
        c1 = localtimeOffset(p);
13378
 
        p->iJD -= c1;
13379
 
        clearYMD_HMS_TZ(p);
13380
 
        p->iJD += c1 - localtimeOffset(p);
13381
 
        rc = 0;
13382
 
      }
13383
 
#endif
13384
 
      break;
13385
 
    }
13386
 
    case 'w': {
13387
 
      /*
13388
 
      **    weekday N
13389
 
      **
13390
 
      ** Move the date to the same time on the next occurrence of
13391
 
      ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
13392
 
      ** date is already on the appropriate weekday, this is a no-op.
13393
 
      */
13394
 
      if( strncmp(z, "weekday ", 8)==0
13395
 
               && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
13396
 
               && (n=(int)r)==r && n>=0 && r<7 ){
13397
 
        sqlite3_int64 Z;
13398
 
        computeYMD_HMS(p);
13399
 
        p->validTZ = 0;
13400
 
        p->validJD = 0;
13401
 
        computeJD(p);
13402
 
        Z = ((p->iJD + 129600000)/86400000) % 7;
13403
 
        if( Z>n ) Z -= 7;
13404
 
        p->iJD += (n - Z)*86400000;
13405
 
        clearYMD_HMS_TZ(p);
13406
 
        rc = 0;
13407
 
      }
13408
 
      break;
13409
 
    }
13410
 
    case 's': {
13411
 
      /*
13412
 
      **    start of TTTTT
13413
 
      **
13414
 
      ** Move the date backwards to the beginning of the current day,
13415
 
      ** or month or year.
13416
 
      */
13417
 
      if( strncmp(z, "start of ", 9)!=0 ) break;
13418
 
      z += 9;
13419
 
      computeYMD(p);
13420
 
      p->validHMS = 1;
13421
 
      p->h = p->m = 0;
13422
 
      p->s = 0.0;
13423
 
      p->validTZ = 0;
13424
 
      p->validJD = 0;
13425
 
      if( strcmp(z,"month")==0 ){
13426
 
        p->D = 1;
13427
 
        rc = 0;
13428
 
      }else if( strcmp(z,"year")==0 ){
13429
 
        computeYMD(p);
13430
 
        p->M = 1;
13431
 
        p->D = 1;
13432
 
        rc = 0;
13433
 
      }else if( strcmp(z,"day")==0 ){
13434
 
        rc = 0;
13435
 
      }
13436
 
      break;
13437
 
    }
13438
 
    case '+':
13439
 
    case '-':
13440
 
    case '0':
13441
 
    case '1':
13442
 
    case '2':
13443
 
    case '3':
13444
 
    case '4':
13445
 
    case '5':
13446
 
    case '6':
13447
 
    case '7':
13448
 
    case '8':
13449
 
    case '9': {
13450
 
      double rRounder;
13451
 
      for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
13452
 
      if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
13453
 
        rc = 1;
13454
 
        break;
13455
 
      }
13456
 
      if( z[n]==':' ){
13457
 
        /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
13458
 
        ** specified number of hours, minutes, seconds, and fractional seconds
13459
 
        ** to the time.  The ".FFF" may be omitted.  The ":SS.FFF" may be
13460
 
        ** omitted.
13461
 
        */
13462
 
        const char *z2 = z;
13463
 
        DateTime tx;
13464
 
        sqlite3_int64 day;
13465
 
        if( !sqlite3Isdigit(*z2) ) z2++;
13466
 
        memset(&tx, 0, sizeof(tx));
13467
 
        if( parseHhMmSs(z2, &tx) ) break;
13468
 
        computeJD(&tx);
13469
 
        tx.iJD -= 43200000;
13470
 
        day = tx.iJD/86400000;
13471
 
        tx.iJD -= day*86400000;
13472
 
        if( z[0]=='-' ) tx.iJD = -tx.iJD;
13473
 
        computeJD(p);
13474
 
        clearYMD_HMS_TZ(p);
13475
 
        p->iJD += tx.iJD;
13476
 
        rc = 0;
13477
 
        break;
13478
 
      }
13479
 
      z += n;
13480
 
      while( sqlite3Isspace(*z) ) z++;
13481
 
      n = sqlite3Strlen30(z);
13482
 
      if( n>10 || n<3 ) break;
13483
 
      if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
13484
 
      computeJD(p);
13485
 
      rc = 0;
13486
 
      rRounder = r<0 ? -0.5 : +0.5;
13487
 
      if( n==3 && strcmp(z,"day")==0 ){
13488
 
        p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
13489
 
      }else if( n==4 && strcmp(z,"hour")==0 ){
13490
 
        p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
13491
 
      }else if( n==6 && strcmp(z,"minute")==0 ){
13492
 
        p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
13493
 
      }else if( n==6 && strcmp(z,"second")==0 ){
13494
 
        p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
13495
 
      }else if( n==5 && strcmp(z,"month")==0 ){
13496
 
        int x, y;
13497
 
        computeYMD_HMS(p);
13498
 
        p->M += (int)r;
13499
 
        x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
13500
 
        p->Y += x;
13501
 
        p->M -= x*12;
13502
 
        p->validJD = 0;
13503
 
        computeJD(p);
13504
 
        y = (int)r;
13505
 
        if( y!=r ){
13506
 
          p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
13507
 
        }
13508
 
      }else if( n==4 && strcmp(z,"year")==0 ){
13509
 
        int y = (int)r;
13510
 
        computeYMD_HMS(p);
13511
 
        p->Y += y;
13512
 
        p->validJD = 0;
13513
 
        computeJD(p);
13514
 
        if( y!=r ){
13515
 
          p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
13516
 
        }
13517
 
      }else{
13518
 
        rc = 1;
13519
 
      }
13520
 
      clearYMD_HMS_TZ(p);
13521
 
      break;
13522
 
    }
13523
 
    default: {
13524
 
      break;
13525
 
    }
13526
 
  }
13527
 
  return rc;
13528
 
}
13529
 
 
13530
 
/*
13531
 
** Process time function arguments.  argv[0] is a date-time stamp.
13532
 
** argv[1] and following are modifiers.  Parse them all and write
13533
 
** the resulting time into the DateTime structure p.  Return 0
13534
 
** on success and 1 if there are any errors.
13535
 
**
13536
 
** If there are zero parameters (if even argv[0] is undefined)
13537
 
** then assume a default value of "now" for argv[0].
13538
 
*/
13539
 
static int isDate(
13540
 
  sqlite3_context *context, 
13541
 
  int argc, 
13542
 
  sqlite3_value **argv, 
13543
 
  DateTime *p
13544
 
){
13545
 
  int i;
13546
 
  const unsigned char *z;
13547
 
  int eType;
13548
 
  memset(p, 0, sizeof(*p));
13549
 
  if( argc==0 ){
13550
 
    setDateTimeToCurrent(context, p);
13551
 
  }else if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
13552
 
                   || eType==SQLITE_INTEGER ){
13553
 
    p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
13554
 
    p->validJD = 1;
13555
 
  }else{
13556
 
    z = sqlite3_value_text(argv[0]);
13557
 
    if( !z || parseDateOrTime(context, (char*)z, p) ){
13558
 
      return 1;
13559
 
    }
13560
 
  }
13561
 
  for(i=1; i<argc; i++){
13562
 
    if( (z = sqlite3_value_text(argv[i]))==0 || parseModifier((char*)z, p) ){
13563
 
      return 1;
13564
 
    }
13565
 
  }
13566
 
  return 0;
13567
 
}
13568
 
 
13569
 
 
13570
 
/*
13571
 
** The following routines implement the various date and time functions
13572
 
** of SQLite.
13573
 
*/
13574
 
 
13575
 
/*
13576
 
**    julianday( TIMESTRING, MOD, MOD, ...)
13577
 
**
13578
 
** Return the julian day number of the date specified in the arguments
13579
 
*/
13580
 
static void juliandayFunc(
13581
 
  sqlite3_context *context,
13582
 
  int argc,
13583
 
  sqlite3_value **argv
13584
 
){
13585
 
  DateTime x;
13586
 
  if( isDate(context, argc, argv, &x)==0 ){
13587
 
    computeJD(&x);
13588
 
    sqlite3_result_double(context, x.iJD/86400000.0);
13589
 
  }
13590
 
}
13591
 
 
13592
 
/*
13593
 
**    datetime( TIMESTRING, MOD, MOD, ...)
13594
 
**
13595
 
** Return YYYY-MM-DD HH:MM:SS
13596
 
*/
13597
 
static void datetimeFunc(
13598
 
  sqlite3_context *context,
13599
 
  int argc,
13600
 
  sqlite3_value **argv
13601
 
){
13602
 
  DateTime x;
13603
 
  if( isDate(context, argc, argv, &x)==0 ){
13604
 
    char zBuf[100];
13605
 
    computeYMD_HMS(&x);
13606
 
    sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
13607
 
                     x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
13608
 
    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
13609
 
  }
13610
 
}
13611
 
 
13612
 
/*
13613
 
**    time( TIMESTRING, MOD, MOD, ...)
13614
 
**
13615
 
** Return HH:MM:SS
13616
 
*/
13617
 
static void timeFunc(
13618
 
  sqlite3_context *context,
13619
 
  int argc,
13620
 
  sqlite3_value **argv
13621
 
){
13622
 
  DateTime x;
13623
 
  if( isDate(context, argc, argv, &x)==0 ){
13624
 
    char zBuf[100];
13625
 
    computeHMS(&x);
13626
 
    sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
13627
 
    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
13628
 
  }
13629
 
}
13630
 
 
13631
 
/*
13632
 
**    date( TIMESTRING, MOD, MOD, ...)
13633
 
**
13634
 
** Return YYYY-MM-DD
13635
 
*/
13636
 
static void dateFunc(
13637
 
  sqlite3_context *context,
13638
 
  int argc,
13639
 
  sqlite3_value **argv
13640
 
){
13641
 
  DateTime x;
13642
 
  if( isDate(context, argc, argv, &x)==0 ){
13643
 
    char zBuf[100];
13644
 
    computeYMD(&x);
13645
 
    sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
13646
 
    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
13647
 
  }
13648
 
}
13649
 
 
13650
 
/*
13651
 
**    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
13652
 
**
13653
 
** Return a string described by FORMAT.  Conversions as follows:
13654
 
**
13655
 
**   %d  day of month
13656
 
**   %f  ** fractional seconds  SS.SSS
13657
 
**   %H  hour 00-24
13658
 
**   %j  day of year 000-366
13659
 
**   %J  ** Julian day number
13660
 
**   %m  month 01-12
13661
 
**   %M  minute 00-59
13662
 
**   %s  seconds since 1970-01-01
13663
 
**   %S  seconds 00-59
13664
 
**   %w  day of week 0-6  sunday==0
13665
 
**   %W  week of year 00-53
13666
 
**   %Y  year 0000-9999
13667
 
**   %%  %
13668
 
*/
13669
 
static void strftimeFunc(
13670
 
  sqlite3_context *context,
13671
 
  int argc,
13672
 
  sqlite3_value **argv
13673
 
){
13674
 
  DateTime x;
13675
 
  u64 n;
13676
 
  size_t i,j;
13677
 
  char *z;
13678
 
  sqlite3 *db;
13679
 
  const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
13680
 
  char zBuf[100];
13681
 
  if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
13682
 
  db = sqlite3_context_db_handle(context);
13683
 
  for(i=0, n=1; zFmt[i]; i++, n++){
13684
 
    if( zFmt[i]=='%' ){
13685
 
      switch( zFmt[i+1] ){
13686
 
        case 'd':
13687
 
        case 'H':
13688
 
        case 'm':
13689
 
        case 'M':
13690
 
        case 'S':
13691
 
        case 'W':
13692
 
          n++;
13693
 
          /* fall thru */
13694
 
        case 'w':
13695
 
        case '%':
13696
 
          break;
13697
 
        case 'f':
13698
 
          n += 8;
13699
 
          break;
13700
 
        case 'j':
13701
 
          n += 3;
13702
 
          break;
13703
 
        case 'Y':
13704
 
          n += 8;
13705
 
          break;
13706
 
        case 's':
13707
 
        case 'J':
13708
 
          n += 50;
13709
 
          break;
13710
 
        default:
13711
 
          return;  /* ERROR.  return a NULL */
13712
 
      }
13713
 
      i++;
13714
 
    }
13715
 
  }
13716
 
  testcase( n==sizeof(zBuf)-1 );
13717
 
  testcase( n==sizeof(zBuf) );
13718
 
  testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
13719
 
  testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
13720
 
  if( n<sizeof(zBuf) ){
13721
 
    z = zBuf;
13722
 
  }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
13723
 
    sqlite3_result_error_toobig(context);
13724
 
    return;
13725
 
  }else{
13726
 
    z = sqlite3DbMallocRaw(db, (int)n);
13727
 
    if( z==0 ){
13728
 
      sqlite3_result_error_nomem(context);
13729
 
      return;
13730
 
    }
13731
 
  }
13732
 
  computeJD(&x);
13733
 
  computeYMD_HMS(&x);
13734
 
  for(i=j=0; zFmt[i]; i++){
13735
 
    if( zFmt[i]!='%' ){
13736
 
      z[j++] = zFmt[i];
13737
 
    }else{
13738
 
      i++;
13739
 
      switch( zFmt[i] ){
13740
 
        case 'd':  sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
13741
 
        case 'f': {
13742
 
          double s = x.s;
13743
 
          if( s>59.999 ) s = 59.999;
13744
 
          sqlite3_snprintf(7, &z[j],"%06.3f", s);
13745
 
          j += sqlite3Strlen30(&z[j]);
13746
 
          break;
13747
 
        }
13748
 
        case 'H':  sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
13749
 
        case 'W': /* Fall thru */
13750
 
        case 'j': {
13751
 
          int nDay;             /* Number of days since 1st day of year */
13752
 
          DateTime y = x;
13753
 
          y.validJD = 0;
13754
 
          y.M = 1;
13755
 
          y.D = 1;
13756
 
          computeJD(&y);
13757
 
          nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
13758
 
          if( zFmt[i]=='W' ){
13759
 
            int wd;   /* 0=Monday, 1=Tuesday, ... 6=Sunday */
13760
 
            wd = (int)(((x.iJD+43200000)/86400000)%7);
13761
 
            sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
13762
 
            j += 2;
13763
 
          }else{
13764
 
            sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
13765
 
            j += 3;
13766
 
          }
13767
 
          break;
13768
 
        }
13769
 
        case 'J': {
13770
 
          sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
13771
 
          j+=sqlite3Strlen30(&z[j]);
13772
 
          break;
13773
 
        }
13774
 
        case 'm':  sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
13775
 
        case 'M':  sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
13776
 
        case 's': {
13777
 
          sqlite3_snprintf(30,&z[j],"%lld",
13778
 
                           (i64)(x.iJD/1000 - 21086676*(i64)10000));
13779
 
          j += sqlite3Strlen30(&z[j]);
13780
 
          break;
13781
 
        }
13782
 
        case 'S':  sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
13783
 
        case 'w': {
13784
 
          z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
13785
 
          break;
13786
 
        }
13787
 
        case 'Y': {
13788
 
          sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
13789
 
          break;
13790
 
        }
13791
 
        default:   z[j++] = '%'; break;
13792
 
      }
13793
 
    }
13794
 
  }
13795
 
  z[j] = 0;
13796
 
  sqlite3_result_text(context, z, -1,
13797
 
                      z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
13798
 
}
13799
 
 
13800
 
/*
13801
 
** current_time()
13802
 
**
13803
 
** This function returns the same value as time('now').
13804
 
*/
13805
 
static void ctimeFunc(
13806
 
  sqlite3_context *context,
13807
 
  int NotUsed,
13808
 
  sqlite3_value **NotUsed2
13809
 
){
13810
 
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
13811
 
  timeFunc(context, 0, 0);
13812
 
}
13813
 
 
13814
 
/*
13815
 
** current_date()
13816
 
**
13817
 
** This function returns the same value as date('now').
13818
 
*/
13819
 
static void cdateFunc(
13820
 
  sqlite3_context *context,
13821
 
  int NotUsed,
13822
 
  sqlite3_value **NotUsed2
13823
 
){
13824
 
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
13825
 
  dateFunc(context, 0, 0);
13826
 
}
13827
 
 
13828
 
/*
13829
 
** current_timestamp()
13830
 
**
13831
 
** This function returns the same value as datetime('now').
13832
 
*/
13833
 
static void ctimestampFunc(
13834
 
  sqlite3_context *context,
13835
 
  int NotUsed,
13836
 
  sqlite3_value **NotUsed2
13837
 
){
13838
 
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
13839
 
  datetimeFunc(context, 0, 0);
13840
 
}
13841
 
#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
13842
 
 
13843
 
#ifdef SQLITE_OMIT_DATETIME_FUNCS
13844
 
/*
13845
 
** If the library is compiled to omit the full-scale date and time
13846
 
** handling (to get a smaller binary), the following minimal version
13847
 
** of the functions current_time(), current_date() and current_timestamp()
13848
 
** are included instead. This is to support column declarations that
13849
 
** include "DEFAULT CURRENT_TIME" etc.
13850
 
**
13851
 
** This function uses the C-library functions time(), gmtime()
13852
 
** and strftime(). The format string to pass to strftime() is supplied
13853
 
** as the user-data for the function.
13854
 
*/
13855
 
static void currentTimeFunc(
13856
 
  sqlite3_context *context,
13857
 
  int argc,
13858
 
  sqlite3_value **argv
13859
 
){
13860
 
  time_t t;
13861
 
  char *zFormat = (char *)sqlite3_user_data(context);
13862
 
  sqlite3 *db;
13863
 
  sqlite3_int64 iT;
13864
 
  char zBuf[20];
13865
 
 
13866
 
  UNUSED_PARAMETER(argc);
13867
 
  UNUSED_PARAMETER(argv);
13868
 
 
13869
 
  db = sqlite3_context_db_handle(context);
13870
 
  sqlite3OsCurrentTimeInt64(db->pVfs, &iT);
13871
 
  t = iT/1000 - 10000*(sqlite3_int64)21086676;
13872
 
#ifdef HAVE_GMTIME_R
13873
 
  {
13874
 
    struct tm sNow;
13875
 
    gmtime_r(&t, &sNow);
13876
 
    strftime(zBuf, 20, zFormat, &sNow);
13877
 
  }
13878
 
#else
13879
 
  {
13880
 
    struct tm *pTm;
13881
 
    sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
13882
 
    pTm = gmtime(&t);
13883
 
    strftime(zBuf, 20, zFormat, pTm);
13884
 
    sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
13885
 
  }
13886
 
#endif
13887
 
 
13888
 
  sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
13889
 
}
13890
 
#endif
13891
 
 
13892
 
/*
13893
 
** This function registered all of the above C functions as SQL
13894
 
** functions.  This should be the only routine in this file with
13895
 
** external linkage.
13896
 
*/
13897
 
SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
13898
 
  static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
13899
 
#ifndef SQLITE_OMIT_DATETIME_FUNCS
13900
 
    FUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
13901
 
    FUNCTION(date,             -1, 0, 0, dateFunc      ),
13902
 
    FUNCTION(time,             -1, 0, 0, timeFunc      ),
13903
 
    FUNCTION(datetime,         -1, 0, 0, datetimeFunc  ),
13904
 
    FUNCTION(strftime,         -1, 0, 0, strftimeFunc  ),
13905
 
    FUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
13906
 
    FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
13907
 
    FUNCTION(current_date,      0, 0, 0, cdateFunc     ),
13908
 
#else
13909
 
    STR_FUNCTION(current_time,      0, "%H:%M:%S",          0, currentTimeFunc),
13910
 
    STR_FUNCTION(current_date,      0, "%Y-%m-%d",          0, currentTimeFunc),
13911
 
    STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
13912
 
#endif
13913
 
  };
13914
 
  int i;
13915
 
  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
13916
 
  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
13917
 
 
13918
 
  for(i=0; i<ArraySize(aDateTimeFuncs); i++){
13919
 
    sqlite3FuncDefInsert(pHash, &aFunc[i]);
13920
 
  }
13921
 
}
13922
 
 
13923
 
/************** End of date.c ************************************************/
13924
 
/************** Begin file os.c **********************************************/
13925
 
/*
13926
 
** 2005 November 29
13927
 
**
13928
 
** The author disclaims copyright to this source code.  In place of
13929
 
** a legal notice, here is a blessing:
13930
 
**
13931
 
**    May you do good and not evil.
13932
 
**    May you find forgiveness for yourself and forgive others.
13933
 
**    May you share freely, never taking more than you give.
13934
 
**
13935
 
******************************************************************************
13936
 
**
13937
 
** This file contains OS interface code that is common to all
13938
 
** architectures.
13939
 
*/
13940
 
#define _SQLITE_OS_C_ 1
13941
 
#undef _SQLITE_OS_C_
13942
 
 
13943
 
/*
13944
 
** The default SQLite sqlite3_vfs implementations do not allocate
13945
 
** memory (actually, os_unix.c allocates a small amount of memory
13946
 
** from within OsOpen()), but some third-party implementations may.
13947
 
** So we test the effects of a malloc() failing and the sqlite3OsXXX()
13948
 
** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
13949
 
**
13950
 
** The following functions are instrumented for malloc() failure 
13951
 
** testing:
13952
 
**
13953
 
**     sqlite3OsOpen()
13954
 
**     sqlite3OsRead()
13955
 
**     sqlite3OsWrite()
13956
 
**     sqlite3OsSync()
13957
 
**     sqlite3OsLock()
13958
 
**
13959
 
*/
13960
 
#if defined(SQLITE_TEST)
13961
 
SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
13962
 
  #define DO_OS_MALLOC_TEST(x)                                       \
13963
 
  if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) {  \
13964
 
    void *pTstAlloc = sqlite3Malloc(10);                             \
13965
 
    if (!pTstAlloc) return SQLITE_IOERR_NOMEM;                       \
13966
 
    sqlite3_free(pTstAlloc);                                         \
13967
 
  }
13968
 
#else
13969
 
  #define DO_OS_MALLOC_TEST(x)
13970
 
#endif
13971
 
 
13972
 
/*
13973
 
** The following routines are convenience wrappers around methods
13974
 
** of the sqlite3_file object.  This is mostly just syntactic sugar. All
13975
 
** of this would be completely automatic if SQLite were coded using
13976
 
** C++ instead of plain old C.
13977
 
*/
13978
 
SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
13979
 
  int rc = SQLITE_OK;
13980
 
  if( pId->pMethods ){
13981
 
    rc = pId->pMethods->xClose(pId);
13982
 
    pId->pMethods = 0;
13983
 
  }
13984
 
  return rc;
13985
 
}
13986
 
SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
13987
 
  DO_OS_MALLOC_TEST(id);
13988
 
  return id->pMethods->xRead(id, pBuf, amt, offset);
13989
 
}
13990
 
SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
13991
 
  DO_OS_MALLOC_TEST(id);
13992
 
  return id->pMethods->xWrite(id, pBuf, amt, offset);
13993
 
}
13994
 
SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
13995
 
  return id->pMethods->xTruncate(id, size);
13996
 
}
13997
 
SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
13998
 
  DO_OS_MALLOC_TEST(id);
13999
 
  return id->pMethods->xSync(id, flags);
14000
 
}
14001
 
SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
14002
 
  DO_OS_MALLOC_TEST(id);
14003
 
  return id->pMethods->xFileSize(id, pSize);
14004
 
}
14005
 
SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
14006
 
  DO_OS_MALLOC_TEST(id);
14007
 
  return id->pMethods->xLock(id, lockType);
14008
 
}
14009
 
SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
14010
 
  return id->pMethods->xUnlock(id, lockType);
14011
 
}
14012
 
SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
14013
 
  DO_OS_MALLOC_TEST(id);
14014
 
  return id->pMethods->xCheckReservedLock(id, pResOut);
14015
 
}
14016
 
SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
14017
 
  return id->pMethods->xFileControl(id, op, pArg);
14018
 
}
14019
 
SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
14020
 
  int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
14021
 
  return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
14022
 
}
14023
 
SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
14024
 
  return id->pMethods->xDeviceCharacteristics(id);
14025
 
}
14026
 
SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
14027
 
  return id->pMethods->xShmLock(id, offset, n, flags);
14028
 
}
14029
 
SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
14030
 
  id->pMethods->xShmBarrier(id);
14031
 
}
14032
 
SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
14033
 
  return id->pMethods->xShmUnmap(id, deleteFlag);
14034
 
}
14035
 
SQLITE_PRIVATE int sqlite3OsShmMap(
14036
 
  sqlite3_file *id,               /* Database file handle */
14037
 
  int iPage,
14038
 
  int pgsz,
14039
 
  int bExtend,                    /* True to extend file if necessary */
14040
 
  void volatile **pp              /* OUT: Pointer to mapping */
14041
 
){
14042
 
  return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
14043
 
}
14044
 
 
14045
 
/*
14046
 
** The next group of routines are convenience wrappers around the
14047
 
** VFS methods.
14048
 
*/
14049
 
SQLITE_PRIVATE int sqlite3OsOpen(
14050
 
  sqlite3_vfs *pVfs, 
14051
 
  const char *zPath, 
14052
 
  sqlite3_file *pFile, 
14053
 
  int flags, 
14054
 
  int *pFlagsOut
14055
 
){
14056
 
  int rc;
14057
 
  DO_OS_MALLOC_TEST(0);
14058
 
  /* 0x87f3f is a mask of SQLITE_OPEN_ flags that are valid to be passed
14059
 
  ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
14060
 
  ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
14061
 
  ** reaching the VFS. */
14062
 
  rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f3f, pFlagsOut);
14063
 
  assert( rc==SQLITE_OK || pFile->pMethods==0 );
14064
 
  return rc;
14065
 
}
14066
 
SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
14067
 
  return pVfs->xDelete(pVfs, zPath, dirSync);
14068
 
}
14069
 
SQLITE_PRIVATE int sqlite3OsAccess(
14070
 
  sqlite3_vfs *pVfs, 
14071
 
  const char *zPath, 
14072
 
  int flags, 
14073
 
  int *pResOut
14074
 
){
14075
 
  DO_OS_MALLOC_TEST(0);
14076
 
  return pVfs->xAccess(pVfs, zPath, flags, pResOut);
14077
 
}
14078
 
SQLITE_PRIVATE int sqlite3OsFullPathname(
14079
 
  sqlite3_vfs *pVfs, 
14080
 
  const char *zPath, 
14081
 
  int nPathOut, 
14082
 
  char *zPathOut
14083
 
){
14084
 
  zPathOut[0] = 0;
14085
 
  return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
14086
 
}
14087
 
#ifndef SQLITE_OMIT_LOAD_EXTENSION
14088
 
SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
14089
 
  return pVfs->xDlOpen(pVfs, zPath);
14090
 
}
14091
 
SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
14092
 
  pVfs->xDlError(pVfs, nByte, zBufOut);
14093
 
}
14094
 
SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
14095
 
  return pVfs->xDlSym(pVfs, pHdle, zSym);
14096
 
}
14097
 
SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
14098
 
  pVfs->xDlClose(pVfs, pHandle);
14099
 
}
14100
 
#endif /* SQLITE_OMIT_LOAD_EXTENSION */
14101
 
SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
14102
 
  return pVfs->xRandomness(pVfs, nByte, zBufOut);
14103
 
}
14104
 
SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
14105
 
  return pVfs->xSleep(pVfs, nMicro);
14106
 
}
14107
 
SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
14108
 
  int rc;
14109
 
  /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
14110
 
  ** method to get the current date and time if that method is available
14111
 
  ** (if iVersion is 2 or greater and the function pointer is not NULL) and
14112
 
  ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
14113
 
  ** unavailable.
14114
 
  */
14115
 
  if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
14116
 
    rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
14117
 
  }else{
14118
 
    double r;
14119
 
    rc = pVfs->xCurrentTime(pVfs, &r);
14120
 
    *pTimeOut = (sqlite3_int64)(r*86400000.0);
14121
 
  }
14122
 
  return rc;
14123
 
}
14124
 
 
14125
 
SQLITE_PRIVATE int sqlite3OsOpenMalloc(
14126
 
  sqlite3_vfs *pVfs, 
14127
 
  const char *zFile, 
14128
 
  sqlite3_file **ppFile, 
14129
 
  int flags,
14130
 
  int *pOutFlags
14131
 
){
14132
 
  int rc = SQLITE_NOMEM;
14133
 
  sqlite3_file *pFile;
14134
 
  pFile = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile);
14135
 
  if( pFile ){
14136
 
    rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
14137
 
    if( rc!=SQLITE_OK ){
14138
 
      sqlite3_free(pFile);
14139
 
    }else{
14140
 
      *ppFile = pFile;
14141
 
    }
14142
 
  }
14143
 
  return rc;
14144
 
}
14145
 
SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
14146
 
  int rc = SQLITE_OK;
14147
 
  assert( pFile );
14148
 
  rc = sqlite3OsClose(pFile);
14149
 
  sqlite3_free(pFile);
14150
 
  return rc;
14151
 
}
14152
 
 
14153
 
/*
14154
 
** This function is a wrapper around the OS specific implementation of
14155
 
** sqlite3_os_init(). The purpose of the wrapper is to provide the
14156
 
** ability to simulate a malloc failure, so that the handling of an
14157
 
** error in sqlite3_os_init() by the upper layers can be tested.
14158
 
*/
14159
 
SQLITE_PRIVATE int sqlite3OsInit(void){
14160
 
  void *p = sqlite3_malloc(10);
14161
 
  if( p==0 ) return SQLITE_NOMEM;
14162
 
  sqlite3_free(p);
14163
 
  return sqlite3_os_init();
14164
 
}
14165
 
 
14166
 
/*
14167
 
** The list of all registered VFS implementations.
14168
 
*/
14169
 
static sqlite3_vfs * SQLITE_WSD vfsList = 0;
14170
 
#define vfsList GLOBAL(sqlite3_vfs *, vfsList)
14171
 
 
14172
 
/*
14173
 
** Locate a VFS by name.  If no name is given, simply return the
14174
 
** first VFS on the list.
14175
 
*/
14176
 
SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
14177
 
  sqlite3_vfs *pVfs = 0;
14178
 
#if SQLITE_THREADSAFE
14179
 
  sqlite3_mutex *mutex;
14180
 
#endif
14181
 
#ifndef SQLITE_OMIT_AUTOINIT
14182
 
  int rc = sqlite3_initialize();
14183
 
  if( rc ) return 0;
14184
 
#endif
14185
 
#if SQLITE_THREADSAFE
14186
 
  mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
14187
 
#endif
14188
 
  sqlite3_mutex_enter(mutex);
14189
 
  for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
14190
 
    if( zVfs==0 ) break;
14191
 
    if( strcmp(zVfs, pVfs->zName)==0 ) break;
14192
 
  }
14193
 
  sqlite3_mutex_leave(mutex);
14194
 
  return pVfs;
14195
 
}
14196
 
 
14197
 
/*
14198
 
** Unlink a VFS from the linked list
14199
 
*/
14200
 
static void vfsUnlink(sqlite3_vfs *pVfs){
14201
 
  assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
14202
 
  if( pVfs==0 ){
14203
 
    /* No-op */
14204
 
  }else if( vfsList==pVfs ){
14205
 
    vfsList = pVfs->pNext;
14206
 
  }else if( vfsList ){
14207
 
    sqlite3_vfs *p = vfsList;
14208
 
    while( p->pNext && p->pNext!=pVfs ){
14209
 
      p = p->pNext;
14210
 
    }
14211
 
    if( p->pNext==pVfs ){
14212
 
      p->pNext = pVfs->pNext;
14213
 
    }
14214
 
  }
14215
 
}
14216
 
 
14217
 
/*
14218
 
** Register a VFS with the system.  It is harmless to register the same
14219
 
** VFS multiple times.  The new VFS becomes the default if makeDflt is
14220
 
** true.
14221
 
*/
14222
 
SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
14223
 
  sqlite3_mutex *mutex = 0;
14224
 
#ifndef SQLITE_OMIT_AUTOINIT
14225
 
  int rc = sqlite3_initialize();
14226
 
  if( rc ) return rc;
14227
 
#endif
14228
 
  mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
14229
 
  sqlite3_mutex_enter(mutex);
14230
 
  vfsUnlink(pVfs);
14231
 
  if( makeDflt || vfsList==0 ){
14232
 
    pVfs->pNext = vfsList;
14233
 
    vfsList = pVfs;
14234
 
  }else{
14235
 
    pVfs->pNext = vfsList->pNext;
14236
 
    vfsList->pNext = pVfs;
14237
 
  }
14238
 
  assert(vfsList);
14239
 
  sqlite3_mutex_leave(mutex);
14240
 
  return SQLITE_OK;
14241
 
}
14242
 
 
14243
 
/*
14244
 
** Unregister a VFS so that it is no longer accessible.
14245
 
*/
14246
 
SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
14247
 
#if SQLITE_THREADSAFE
14248
 
  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
14249
 
#endif
14250
 
  sqlite3_mutex_enter(mutex);
14251
 
  vfsUnlink(pVfs);
14252
 
  sqlite3_mutex_leave(mutex);
14253
 
  return SQLITE_OK;
14254
 
}
14255
 
 
14256
 
/************** End of os.c **************************************************/
14257
 
/************** Begin file fault.c *******************************************/
14258
 
/*
14259
 
** 2008 Jan 22
14260
 
**
14261
 
** The author disclaims copyright to this source code.  In place of
14262
 
** a legal notice, here is a blessing:
14263
 
**
14264
 
**    May you do good and not evil.
14265
 
**    May you find forgiveness for yourself and forgive others.
14266
 
**    May you share freely, never taking more than you give.
14267
 
**
14268
 
*************************************************************************
14269
 
**
14270
 
** This file contains code to support the concept of "benign" 
14271
 
** malloc failures (when the xMalloc() or xRealloc() method of the
14272
 
** sqlite3_mem_methods structure fails to allocate a block of memory
14273
 
** and returns 0). 
14274
 
**
14275
 
** Most malloc failures are non-benign. After they occur, SQLite
14276
 
** abandons the current operation and returns an error code (usually
14277
 
** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
14278
 
** fatal. For example, if a malloc fails while resizing a hash table, this 
14279
 
** is completely recoverable simply by not carrying out the resize. The 
14280
 
** hash table will continue to function normally.  So a malloc failure 
14281
 
** during a hash table resize is a benign fault.
14282
 
*/
14283
 
 
14284
 
 
14285
 
#ifndef SQLITE_OMIT_BUILTIN_TEST
14286
 
 
14287
 
/*
14288
 
** Global variables.
14289
 
*/
14290
 
typedef struct BenignMallocHooks BenignMallocHooks;
14291
 
static SQLITE_WSD struct BenignMallocHooks {
14292
 
  void (*xBenignBegin)(void);
14293
 
  void (*xBenignEnd)(void);
14294
 
} sqlite3Hooks = { 0, 0 };
14295
 
 
14296
 
/* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
14297
 
** structure.  If writable static data is unsupported on the target,
14298
 
** we have to locate the state vector at run-time.  In the more common
14299
 
** case where writable static data is supported, wsdHooks can refer directly
14300
 
** to the "sqlite3Hooks" state vector declared above.
14301
 
*/
14302
 
#ifdef SQLITE_OMIT_WSD
14303
 
# define wsdHooksInit \
14304
 
  BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
14305
 
# define wsdHooks x[0]
14306
 
#else
14307
 
# define wsdHooksInit
14308
 
# define wsdHooks sqlite3Hooks
14309
 
#endif
14310
 
 
14311
 
 
14312
 
/*
14313
 
** Register hooks to call when sqlite3BeginBenignMalloc() and
14314
 
** sqlite3EndBenignMalloc() are called, respectively.
14315
 
*/
14316
 
SQLITE_PRIVATE void sqlite3BenignMallocHooks(
14317
 
  void (*xBenignBegin)(void),
14318
 
  void (*xBenignEnd)(void)
14319
 
){
14320
 
  wsdHooksInit;
14321
 
  wsdHooks.xBenignBegin = xBenignBegin;
14322
 
  wsdHooks.xBenignEnd = xBenignEnd;
14323
 
}
14324
 
 
14325
 
/*
14326
 
** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
14327
 
** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
14328
 
** indicates that subsequent malloc failures are non-benign.
14329
 
*/
14330
 
SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
14331
 
  wsdHooksInit;
14332
 
  if( wsdHooks.xBenignBegin ){
14333
 
    wsdHooks.xBenignBegin();
14334
 
  }
14335
 
}
14336
 
SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
14337
 
  wsdHooksInit;
14338
 
  if( wsdHooks.xBenignEnd ){
14339
 
    wsdHooks.xBenignEnd();
14340
 
  }
14341
 
}
14342
 
 
14343
 
#endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
14344
 
 
14345
 
/************** End of fault.c ***********************************************/
14346
 
/************** Begin file mem0.c ********************************************/
14347
 
/*
14348
 
** 2008 October 28
14349
 
**
14350
 
** The author disclaims copyright to this source code.  In place of
14351
 
** a legal notice, here is a blessing:
14352
 
**
14353
 
**    May you do good and not evil.
14354
 
**    May you find forgiveness for yourself and forgive others.
14355
 
**    May you share freely, never taking more than you give.
14356
 
**
14357
 
*************************************************************************
14358
 
**
14359
 
** This file contains a no-op memory allocation drivers for use when
14360
 
** SQLITE_ZERO_MALLOC is defined.  The allocation drivers implemented
14361
 
** here always fail.  SQLite will not operate with these drivers.  These
14362
 
** are merely placeholders.  Real drivers must be substituted using
14363
 
** sqlite3_config() before SQLite will operate.
14364
 
*/
14365
 
 
14366
 
/*
14367
 
** This version of the memory allocator is the default.  It is
14368
 
** used when no other memory allocator is specified using compile-time
14369
 
** macros.
14370
 
*/
14371
 
#ifdef SQLITE_ZERO_MALLOC
14372
 
 
14373
 
/*
14374
 
** No-op versions of all memory allocation routines
14375
 
*/
14376
 
static void *sqlite3MemMalloc(int nByte){ return 0; }
14377
 
static void sqlite3MemFree(void *pPrior){ return; }
14378
 
static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
14379
 
static int sqlite3MemSize(void *pPrior){ return 0; }
14380
 
static int sqlite3MemRoundup(int n){ return n; }
14381
 
static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
14382
 
static void sqlite3MemShutdown(void *NotUsed){ return; }
14383
 
 
14384
 
/*
14385
 
** This routine is the only routine in this file with external linkage.
14386
 
**
14387
 
** Populate the low-level memory allocation function pointers in
14388
 
** sqlite3GlobalConfig.m with pointers to the routines in this file.
14389
 
*/
14390
 
SQLITE_PRIVATE void sqlite3MemSetDefault(void){
14391
 
  static const sqlite3_mem_methods defaultMethods = {
14392
 
     sqlite3MemMalloc,
14393
 
     sqlite3MemFree,
14394
 
     sqlite3MemRealloc,
14395
 
     sqlite3MemSize,
14396
 
     sqlite3MemRoundup,
14397
 
     sqlite3MemInit,
14398
 
     sqlite3MemShutdown,
14399
 
     0
14400
 
  };
14401
 
  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
14402
 
}
14403
 
 
14404
 
#endif /* SQLITE_ZERO_MALLOC */
14405
 
 
14406
 
/************** End of mem0.c ************************************************/
14407
 
/************** Begin file mem1.c ********************************************/
14408
 
/*
14409
 
** 2007 August 14
14410
 
**
14411
 
** The author disclaims copyright to this source code.  In place of
14412
 
** a legal notice, here is a blessing:
14413
 
**
14414
 
**    May you do good and not evil.
14415
 
**    May you find forgiveness for yourself and forgive others.
14416
 
**    May you share freely, never taking more than you give.
14417
 
**
14418
 
*************************************************************************
14419
 
**
14420
 
** This file contains low-level memory allocation drivers for when
14421
 
** SQLite will use the standard C-library malloc/realloc/free interface
14422
 
** to obtain the memory it needs.
14423
 
**
14424
 
** This file contains implementations of the low-level memory allocation
14425
 
** routines specified in the sqlite3_mem_methods object.
14426
 
*/
14427
 
 
14428
 
/*
14429
 
** This version of the memory allocator is the default.  It is
14430
 
** used when no other memory allocator is specified using compile-time
14431
 
** macros.
14432
 
*/
14433
 
#ifdef SQLITE_SYSTEM_MALLOC
14434
 
 
14435
 
/*
14436
 
** Like malloc(), but remember the size of the allocation
14437
 
** so that we can find it later using sqlite3MemSize().
14438
 
**
14439
 
** For this low-level routine, we are guaranteed that nByte>0 because
14440
 
** cases of nByte<=0 will be intercepted and dealt with by higher level
14441
 
** routines.
14442
 
*/
14443
 
static void *sqlite3MemMalloc(int nByte){
14444
 
  sqlite3_int64 *p;
14445
 
  assert( nByte>0 );
14446
 
  nByte = ROUND8(nByte);
14447
 
  p = malloc( nByte+8 );
14448
 
  if( p ){
14449
 
    p[0] = nByte;
14450
 
    p++;
14451
 
  }else{
14452
 
    testcase( sqlite3GlobalConfig.xLog!=0 );
14453
 
    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
14454
 
  }
14455
 
  return (void *)p;
14456
 
}
14457
 
 
14458
 
/*
14459
 
** Like free() but works for allocations obtained from sqlite3MemMalloc()
14460
 
** or sqlite3MemRealloc().
14461
 
**
14462
 
** For this low-level routine, we already know that pPrior!=0 since
14463
 
** cases where pPrior==0 will have been intecepted and dealt with
14464
 
** by higher-level routines.
14465
 
*/
14466
 
static void sqlite3MemFree(void *pPrior){
14467
 
  sqlite3_int64 *p = (sqlite3_int64*)pPrior;
14468
 
  assert( pPrior!=0 );
14469
 
  p--;
14470
 
  free(p);
14471
 
}
14472
 
 
14473
 
/*
14474
 
** Report the allocated size of a prior return from xMalloc()
14475
 
** or xRealloc().
14476
 
*/
14477
 
static int sqlite3MemSize(void *pPrior){
14478
 
  sqlite3_int64 *p;
14479
 
  if( pPrior==0 ) return 0;
14480
 
  p = (sqlite3_int64*)pPrior;
14481
 
  p--;
14482
 
  return (int)p[0];
14483
 
}
14484
 
 
14485
 
/*
14486
 
** Like realloc().  Resize an allocation previously obtained from
14487
 
** sqlite3MemMalloc().
14488
 
**
14489
 
** For this low-level interface, we know that pPrior!=0.  Cases where
14490
 
** pPrior==0 while have been intercepted by higher-level routine and
14491
 
** redirected to xMalloc.  Similarly, we know that nByte>0 becauses
14492
 
** cases where nByte<=0 will have been intercepted by higher-level
14493
 
** routines and redirected to xFree.
14494
 
*/
14495
 
static void *sqlite3MemRealloc(void *pPrior, int nByte){
14496
 
  sqlite3_int64 *p = (sqlite3_int64*)pPrior;
14497
 
  assert( pPrior!=0 && nByte>0 );
14498
 
  assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
14499
 
  p--;
14500
 
  p = realloc(p, nByte+8 );
14501
 
  if( p ){
14502
 
    p[0] = nByte;
14503
 
    p++;
14504
 
  }else{
14505
 
    testcase( sqlite3GlobalConfig.xLog!=0 );
14506
 
    sqlite3_log(SQLITE_NOMEM,
14507
 
      "failed memory resize %u to %u bytes",
14508
 
      sqlite3MemSize(pPrior), nByte);
14509
 
  }
14510
 
  return (void*)p;
14511
 
}
14512
 
 
14513
 
/*
14514
 
** Round up a request size to the next valid allocation size.
14515
 
*/
14516
 
static int sqlite3MemRoundup(int n){
14517
 
  return ROUND8(n);
14518
 
}
14519
 
 
14520
 
/*
14521
 
** Initialize this module.
14522
 
*/
14523
 
static int sqlite3MemInit(void *NotUsed){
14524
 
  UNUSED_PARAMETER(NotUsed);
14525
 
  return SQLITE_OK;
14526
 
}
14527
 
 
14528
 
/*
14529
 
** Deinitialize this module.
14530
 
*/
14531
 
static void sqlite3MemShutdown(void *NotUsed){
14532
 
  UNUSED_PARAMETER(NotUsed);
14533
 
  return;
14534
 
}
14535
 
 
14536
 
/*
14537
 
** This routine is the only routine in this file with external linkage.
14538
 
**
14539
 
** Populate the low-level memory allocation function pointers in
14540
 
** sqlite3GlobalConfig.m with pointers to the routines in this file.
14541
 
*/
14542
 
SQLITE_PRIVATE void sqlite3MemSetDefault(void){
14543
 
  static const sqlite3_mem_methods defaultMethods = {
14544
 
     sqlite3MemMalloc,
14545
 
     sqlite3MemFree,
14546
 
     sqlite3MemRealloc,
14547
 
     sqlite3MemSize,
14548
 
     sqlite3MemRoundup,
14549
 
     sqlite3MemInit,
14550
 
     sqlite3MemShutdown,
14551
 
     0
14552
 
  };
14553
 
  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
14554
 
}
14555
 
 
14556
 
#endif /* SQLITE_SYSTEM_MALLOC */
14557
 
 
14558
 
/************** End of mem1.c ************************************************/
14559
 
/************** Begin file mem2.c ********************************************/
14560
 
/*
14561
 
** 2007 August 15
14562
 
**
14563
 
** The author disclaims copyright to this source code.  In place of
14564
 
** a legal notice, here is a blessing:
14565
 
**
14566
 
**    May you do good and not evil.
14567
 
**    May you find forgiveness for yourself and forgive others.
14568
 
**    May you share freely, never taking more than you give.
14569
 
**
14570
 
*************************************************************************
14571
 
**
14572
 
** This file contains low-level memory allocation drivers for when
14573
 
** SQLite will use the standard C-library malloc/realloc/free interface
14574
 
** to obtain the memory it needs while adding lots of additional debugging
14575
 
** information to each allocation in order to help detect and fix memory
14576
 
** leaks and memory usage errors.
14577
 
**
14578
 
** This file contains implementations of the low-level memory allocation
14579
 
** routines specified in the sqlite3_mem_methods object.
14580
 
*/
14581
 
 
14582
 
/*
14583
 
** This version of the memory allocator is used only if the
14584
 
** SQLITE_MEMDEBUG macro is defined
14585
 
*/
14586
 
#ifdef SQLITE_MEMDEBUG
14587
 
 
14588
 
/*
14589
 
** The backtrace functionality is only available with GLIBC
14590
 
*/
14591
 
#ifdef __GLIBC__
14592
 
  extern int backtrace(void**,int);
14593
 
  extern void backtrace_symbols_fd(void*const*,int,int);
14594
 
#else
14595
 
# define backtrace(A,B) 1
14596
 
# define backtrace_symbols_fd(A,B,C)
14597
 
#endif
14598
 
 
14599
 
/*
14600
 
** Each memory allocation looks like this:
14601
 
**
14602
 
**  ------------------------------------------------------------------------
14603
 
**  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
14604
 
**  ------------------------------------------------------------------------
14605
 
**
14606
 
** The application code sees only a pointer to the allocation.  We have
14607
 
** to back up from the allocation pointer to find the MemBlockHdr.  The
14608
 
** MemBlockHdr tells us the size of the allocation and the number of
14609
 
** backtrace pointers.  There is also a guard word at the end of the
14610
 
** MemBlockHdr.
14611
 
*/
14612
 
struct MemBlockHdr {
14613
 
  i64 iSize;                          /* Size of this allocation */
14614
 
  struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
14615
 
  char nBacktrace;                    /* Number of backtraces on this alloc */
14616
 
  char nBacktraceSlots;               /* Available backtrace slots */
14617
 
  u8 nTitle;                          /* Bytes of title; includes '\0' */
14618
 
  u8 eType;                           /* Allocation type code */
14619
 
  int iForeGuard;                     /* Guard word for sanity */
14620
 
};
14621
 
 
14622
 
/*
14623
 
** Guard words
14624
 
*/
14625
 
#define FOREGUARD 0x80F5E153
14626
 
#define REARGUARD 0xE4676B53
14627
 
 
14628
 
/*
14629
 
** Number of malloc size increments to track.
14630
 
*/
14631
 
#define NCSIZE  1000
14632
 
 
14633
 
/*
14634
 
** All of the static variables used by this module are collected
14635
 
** into a single structure named "mem".  This is to keep the
14636
 
** static variables organized and to reduce namespace pollution
14637
 
** when this module is combined with other in the amalgamation.
14638
 
*/
14639
 
static struct {
14640
 
  
14641
 
  /*
14642
 
  ** Mutex to control access to the memory allocation subsystem.
14643
 
  */
14644
 
  sqlite3_mutex *mutex;
14645
 
 
14646
 
  /*
14647
 
  ** Head and tail of a linked list of all outstanding allocations
14648
 
  */
14649
 
  struct MemBlockHdr *pFirst;
14650
 
  struct MemBlockHdr *pLast;
14651
 
  
14652
 
  /*
14653
 
  ** The number of levels of backtrace to save in new allocations.
14654
 
  */
14655
 
  int nBacktrace;
14656
 
  void (*xBacktrace)(int, int, void **);
14657
 
 
14658
 
  /*
14659
 
  ** Title text to insert in front of each block
14660
 
  */
14661
 
  int nTitle;        /* Bytes of zTitle to save.  Includes '\0' and padding */
14662
 
  char zTitle[100];  /* The title text */
14663
 
 
14664
 
  /* 
14665
 
  ** sqlite3MallocDisallow() increments the following counter.
14666
 
  ** sqlite3MallocAllow() decrements it.
14667
 
  */
14668
 
  int disallow; /* Do not allow memory allocation */
14669
 
 
14670
 
  /*
14671
 
  ** Gather statistics on the sizes of memory allocations.
14672
 
  ** nAlloc[i] is the number of allocation attempts of i*8
14673
 
  ** bytes.  i==NCSIZE is the number of allocation attempts for
14674
 
  ** sizes more than NCSIZE*8 bytes.
14675
 
  */
14676
 
  int nAlloc[NCSIZE];      /* Total number of allocations */
14677
 
  int nCurrent[NCSIZE];    /* Current number of allocations */
14678
 
  int mxCurrent[NCSIZE];   /* Highwater mark for nCurrent */
14679
 
 
14680
 
} mem;
14681
 
 
14682
 
 
14683
 
/*
14684
 
** Adjust memory usage statistics
14685
 
*/
14686
 
static void adjustStats(int iSize, int increment){
14687
 
  int i = ROUND8(iSize)/8;
14688
 
  if( i>NCSIZE-1 ){
14689
 
    i = NCSIZE - 1;
14690
 
  }
14691
 
  if( increment>0 ){
14692
 
    mem.nAlloc[i]++;
14693
 
    mem.nCurrent[i]++;
14694
 
    if( mem.nCurrent[i]>mem.mxCurrent[i] ){
14695
 
      mem.mxCurrent[i] = mem.nCurrent[i];
14696
 
    }
14697
 
  }else{
14698
 
    mem.nCurrent[i]--;
14699
 
    assert( mem.nCurrent[i]>=0 );
14700
 
  }
14701
 
}
14702
 
 
14703
 
/*
14704
 
** Given an allocation, find the MemBlockHdr for that allocation.
14705
 
**
14706
 
** This routine checks the guards at either end of the allocation and
14707
 
** if they are incorrect it asserts.
14708
 
*/
14709
 
static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
14710
 
  struct MemBlockHdr *p;
14711
 
  int *pInt;
14712
 
  u8 *pU8;
14713
 
  int nReserve;
14714
 
 
14715
 
  p = (struct MemBlockHdr*)pAllocation;
14716
 
  p--;
14717
 
  assert( p->iForeGuard==(int)FOREGUARD );
14718
 
  nReserve = ROUND8(p->iSize);
14719
 
  pInt = (int*)pAllocation;
14720
 
  pU8 = (u8*)pAllocation;
14721
 
  assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
14722
 
  /* This checks any of the "extra" bytes allocated due
14723
 
  ** to rounding up to an 8 byte boundary to ensure 
14724
 
  ** they haven't been overwritten.
14725
 
  */
14726
 
  while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
14727
 
  return p;
14728
 
}
14729
 
 
14730
 
/*
14731
 
** Return the number of bytes currently allocated at address p.
14732
 
*/
14733
 
static int sqlite3MemSize(void *p){
14734
 
  struct MemBlockHdr *pHdr;
14735
 
  if( !p ){
14736
 
    return 0;
14737
 
  }
14738
 
  pHdr = sqlite3MemsysGetHeader(p);
14739
 
  return pHdr->iSize;
14740
 
}
14741
 
 
14742
 
/*
14743
 
** Initialize the memory allocation subsystem.
14744
 
*/
14745
 
static int sqlite3MemInit(void *NotUsed){
14746
 
  UNUSED_PARAMETER(NotUsed);
14747
 
  assert( (sizeof(struct MemBlockHdr)&7) == 0 );
14748
 
  if( !sqlite3GlobalConfig.bMemstat ){
14749
 
    /* If memory status is enabled, then the malloc.c wrapper will already
14750
 
    ** hold the STATIC_MEM mutex when the routines here are invoked. */
14751
 
    mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
14752
 
  }
14753
 
  return SQLITE_OK;
14754
 
}
14755
 
 
14756
 
/*
14757
 
** Deinitialize the memory allocation subsystem.
14758
 
*/
14759
 
static void sqlite3MemShutdown(void *NotUsed){
14760
 
  UNUSED_PARAMETER(NotUsed);
14761
 
  mem.mutex = 0;
14762
 
}
14763
 
 
14764
 
/*
14765
 
** Round up a request size to the next valid allocation size.
14766
 
*/
14767
 
static int sqlite3MemRoundup(int n){
14768
 
  return ROUND8(n);
14769
 
}
14770
 
 
14771
 
/*
14772
 
** Fill a buffer with pseudo-random bytes.  This is used to preset
14773
 
** the content of a new memory allocation to unpredictable values and
14774
 
** to clear the content of a freed allocation to unpredictable values.
14775
 
*/
14776
 
static void randomFill(char *pBuf, int nByte){
14777
 
  unsigned int x, y, r;
14778
 
  x = SQLITE_PTR_TO_INT(pBuf);
14779
 
  y = nByte | 1;
14780
 
  while( nByte >= 4 ){
14781
 
    x = (x>>1) ^ (-(x&1) & 0xd0000001);
14782
 
    y = y*1103515245 + 12345;
14783
 
    r = x ^ y;
14784
 
    *(int*)pBuf = r;
14785
 
    pBuf += 4;
14786
 
    nByte -= 4;
14787
 
  }
14788
 
  while( nByte-- > 0 ){
14789
 
    x = (x>>1) ^ (-(x&1) & 0xd0000001);
14790
 
    y = y*1103515245 + 12345;
14791
 
    r = x ^ y;
14792
 
    *(pBuf++) = r & 0xff;
14793
 
  }
14794
 
}
14795
 
 
14796
 
/*
14797
 
** Allocate nByte bytes of memory.
14798
 
*/
14799
 
static void *sqlite3MemMalloc(int nByte){
14800
 
  struct MemBlockHdr *pHdr;
14801
 
  void **pBt;
14802
 
  char *z;
14803
 
  int *pInt;
14804
 
  void *p = 0;
14805
 
  int totalSize;
14806
 
  int nReserve;
14807
 
  sqlite3_mutex_enter(mem.mutex);
14808
 
  assert( mem.disallow==0 );
14809
 
  nReserve = ROUND8(nByte);
14810
 
  totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
14811
 
               mem.nBacktrace*sizeof(void*) + mem.nTitle;
14812
 
  p = malloc(totalSize);
14813
 
  if( p ){
14814
 
    z = p;
14815
 
    pBt = (void**)&z[mem.nTitle];
14816
 
    pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
14817
 
    pHdr->pNext = 0;
14818
 
    pHdr->pPrev = mem.pLast;
14819
 
    if( mem.pLast ){
14820
 
      mem.pLast->pNext = pHdr;
14821
 
    }else{
14822
 
      mem.pFirst = pHdr;
14823
 
    }
14824
 
    mem.pLast = pHdr;
14825
 
    pHdr->iForeGuard = FOREGUARD;
14826
 
    pHdr->eType = MEMTYPE_HEAP;
14827
 
    pHdr->nBacktraceSlots = mem.nBacktrace;
14828
 
    pHdr->nTitle = mem.nTitle;
14829
 
    if( mem.nBacktrace ){
14830
 
      void *aAddr[40];
14831
 
      pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
14832
 
      memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
14833
 
      assert(pBt[0]);
14834
 
      if( mem.xBacktrace ){
14835
 
        mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
14836
 
      }
14837
 
    }else{
14838
 
      pHdr->nBacktrace = 0;
14839
 
    }
14840
 
    if( mem.nTitle ){
14841
 
      memcpy(z, mem.zTitle, mem.nTitle);
14842
 
    }
14843
 
    pHdr->iSize = nByte;
14844
 
    adjustStats(nByte, +1);
14845
 
    pInt = (int*)&pHdr[1];
14846
 
    pInt[nReserve/sizeof(int)] = REARGUARD;
14847
 
    randomFill((char*)pInt, nByte);
14848
 
    memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
14849
 
    p = (void*)pInt;
14850
 
  }
14851
 
  sqlite3_mutex_leave(mem.mutex);
14852
 
  return p; 
14853
 
}
14854
 
 
14855
 
/*
14856
 
** Free memory.
14857
 
*/
14858
 
static void sqlite3MemFree(void *pPrior){
14859
 
  struct MemBlockHdr *pHdr;
14860
 
  void **pBt;
14861
 
  char *z;
14862
 
  assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0 
14863
 
       || mem.mutex!=0 );
14864
 
  pHdr = sqlite3MemsysGetHeader(pPrior);
14865
 
  pBt = (void**)pHdr;
14866
 
  pBt -= pHdr->nBacktraceSlots;
14867
 
  sqlite3_mutex_enter(mem.mutex);
14868
 
  if( pHdr->pPrev ){
14869
 
    assert( pHdr->pPrev->pNext==pHdr );
14870
 
    pHdr->pPrev->pNext = pHdr->pNext;
14871
 
  }else{
14872
 
    assert( mem.pFirst==pHdr );
14873
 
    mem.pFirst = pHdr->pNext;
14874
 
  }
14875
 
  if( pHdr->pNext ){
14876
 
    assert( pHdr->pNext->pPrev==pHdr );
14877
 
    pHdr->pNext->pPrev = pHdr->pPrev;
14878
 
  }else{
14879
 
    assert( mem.pLast==pHdr );
14880
 
    mem.pLast = pHdr->pPrev;
14881
 
  }
14882
 
  z = (char*)pBt;
14883
 
  z -= pHdr->nTitle;
14884
 
  adjustStats(pHdr->iSize, -1);
14885
 
  randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
14886
 
                pHdr->iSize + sizeof(int) + pHdr->nTitle);
14887
 
  free(z);
14888
 
  sqlite3_mutex_leave(mem.mutex);  
14889
 
}
14890
 
 
14891
 
/*
14892
 
** Change the size of an existing memory allocation.
14893
 
**
14894
 
** For this debugging implementation, we *always* make a copy of the
14895
 
** allocation into a new place in memory.  In this way, if the 
14896
 
** higher level code is using pointer to the old allocation, it is 
14897
 
** much more likely to break and we are much more liking to find
14898
 
** the error.
14899
 
*/
14900
 
static void *sqlite3MemRealloc(void *pPrior, int nByte){
14901
 
  struct MemBlockHdr *pOldHdr;
14902
 
  void *pNew;
14903
 
  assert( mem.disallow==0 );
14904
 
  assert( (nByte & 7)==0 );     /* EV: R-46199-30249 */
14905
 
  pOldHdr = sqlite3MemsysGetHeader(pPrior);
14906
 
  pNew = sqlite3MemMalloc(nByte);
14907
 
  if( pNew ){
14908
 
    memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
14909
 
    if( nByte>pOldHdr->iSize ){
14910
 
      randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize);
14911
 
    }
14912
 
    sqlite3MemFree(pPrior);
14913
 
  }
14914
 
  return pNew;
14915
 
}
14916
 
 
14917
 
/*
14918
 
** Populate the low-level memory allocation function pointers in
14919
 
** sqlite3GlobalConfig.m with pointers to the routines in this file.
14920
 
*/
14921
 
SQLITE_PRIVATE void sqlite3MemSetDefault(void){
14922
 
  static const sqlite3_mem_methods defaultMethods = {
14923
 
     sqlite3MemMalloc,
14924
 
     sqlite3MemFree,
14925
 
     sqlite3MemRealloc,
14926
 
     sqlite3MemSize,
14927
 
     sqlite3MemRoundup,
14928
 
     sqlite3MemInit,
14929
 
     sqlite3MemShutdown,
14930
 
     0
14931
 
  };
14932
 
  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
14933
 
}
14934
 
 
14935
 
/*
14936
 
** Set the "type" of an allocation.
14937
 
*/
14938
 
SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
14939
 
  if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
14940
 
    struct MemBlockHdr *pHdr;
14941
 
    pHdr = sqlite3MemsysGetHeader(p);
14942
 
    assert( pHdr->iForeGuard==FOREGUARD );
14943
 
    pHdr->eType = eType;
14944
 
  }
14945
 
}
14946
 
 
14947
 
/*
14948
 
** Return TRUE if the mask of type in eType matches the type of the
14949
 
** allocation p.  Also return true if p==NULL.
14950
 
**
14951
 
** This routine is designed for use within an assert() statement, to
14952
 
** verify the type of an allocation.  For example:
14953
 
**
14954
 
**     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
14955
 
*/
14956
 
SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
14957
 
  int rc = 1;
14958
 
  if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
14959
 
    struct MemBlockHdr *pHdr;
14960
 
    pHdr = sqlite3MemsysGetHeader(p);
14961
 
    assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
14962
 
    if( (pHdr->eType&eType)==0 ){
14963
 
      rc = 0;
14964
 
    }
14965
 
  }
14966
 
  return rc;
14967
 
}
14968
 
 
14969
 
/*
14970
 
** Return TRUE if the mask of type in eType matches no bits of the type of the
14971
 
** allocation p.  Also return true if p==NULL.
14972
 
**
14973
 
** This routine is designed for use within an assert() statement, to
14974
 
** verify the type of an allocation.  For example:
14975
 
**
14976
 
**     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
14977
 
*/
14978
 
SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
14979
 
  int rc = 1;
14980
 
  if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
14981
 
    struct MemBlockHdr *pHdr;
14982
 
    pHdr = sqlite3MemsysGetHeader(p);
14983
 
    assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
14984
 
    if( (pHdr->eType&eType)!=0 ){
14985
 
      rc = 0;
14986
 
    }
14987
 
  }
14988
 
  return rc;
14989
 
}
14990
 
 
14991
 
/*
14992
 
** Set the number of backtrace levels kept for each allocation.
14993
 
** A value of zero turns off backtracing.  The number is always rounded
14994
 
** up to a multiple of 2.
14995
 
*/
14996
 
SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
14997
 
  if( depth<0 ){ depth = 0; }
14998
 
  if( depth>20 ){ depth = 20; }
14999
 
  depth = (depth+1)&0xfe;
15000
 
  mem.nBacktrace = depth;
15001
 
}
15002
 
 
15003
 
SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
15004
 
  mem.xBacktrace = xBacktrace;
15005
 
}
15006
 
 
15007
 
/*
15008
 
** Set the title string for subsequent allocations.
15009
 
*/
15010
 
SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
15011
 
  unsigned int n = sqlite3Strlen30(zTitle) + 1;
15012
 
  sqlite3_mutex_enter(mem.mutex);
15013
 
  if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
15014
 
  memcpy(mem.zTitle, zTitle, n);
15015
 
  mem.zTitle[n] = 0;
15016
 
  mem.nTitle = ROUND8(n);
15017
 
  sqlite3_mutex_leave(mem.mutex);
15018
 
}
15019
 
 
15020
 
SQLITE_PRIVATE void sqlite3MemdebugSync(){
15021
 
  struct MemBlockHdr *pHdr;
15022
 
  for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
15023
 
    void **pBt = (void**)pHdr;
15024
 
    pBt -= pHdr->nBacktraceSlots;
15025
 
    mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
15026
 
  }
15027
 
}
15028
 
 
15029
 
/*
15030
 
** Open the file indicated and write a log of all unfreed memory 
15031
 
** allocations into that log.
15032
 
*/
15033
 
SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
15034
 
  FILE *out;
15035
 
  struct MemBlockHdr *pHdr;
15036
 
  void **pBt;
15037
 
  int i;
15038
 
  out = fopen(zFilename, "w");
15039
 
  if( out==0 ){
15040
 
    fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
15041
 
                    zFilename);
15042
 
    return;
15043
 
  }
15044
 
  for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
15045
 
    char *z = (char*)pHdr;
15046
 
    z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
15047
 
    fprintf(out, "**** %lld bytes at %p from %s ****\n", 
15048
 
            pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
15049
 
    if( pHdr->nBacktrace ){
15050
 
      fflush(out);
15051
 
      pBt = (void**)pHdr;
15052
 
      pBt -= pHdr->nBacktraceSlots;
15053
 
      backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
15054
 
      fprintf(out, "\n");
15055
 
    }
15056
 
  }
15057
 
  fprintf(out, "COUNTS:\n");
15058
 
  for(i=0; i<NCSIZE-1; i++){
15059
 
    if( mem.nAlloc[i] ){
15060
 
      fprintf(out, "   %5d: %10d %10d %10d\n", 
15061
 
            i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
15062
 
    }
15063
 
  }
15064
 
  if( mem.nAlloc[NCSIZE-1] ){
15065
 
    fprintf(out, "   %5d: %10d %10d %10d\n",
15066
 
             NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
15067
 
             mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
15068
 
  }
15069
 
  fclose(out);
15070
 
}
15071
 
 
15072
 
/*
15073
 
** Return the number of times sqlite3MemMalloc() has been called.
15074
 
*/
15075
 
SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
15076
 
  int i;
15077
 
  int nTotal = 0;
15078
 
  for(i=0; i<NCSIZE; i++){
15079
 
    nTotal += mem.nAlloc[i];
15080
 
  }
15081
 
  return nTotal;
15082
 
}
15083
 
 
15084
 
 
15085
 
#endif /* SQLITE_MEMDEBUG */
15086
 
 
15087
 
/************** End of mem2.c ************************************************/
15088
 
/************** Begin file mem3.c ********************************************/
15089
 
/*
15090
 
** 2007 October 14
15091
 
**
15092
 
** The author disclaims copyright to this source code.  In place of
15093
 
** a legal notice, here is a blessing:
15094
 
**
15095
 
**    May you do good and not evil.
15096
 
**    May you find forgiveness for yourself and forgive others.
15097
 
**    May you share freely, never taking more than you give.
15098
 
**
15099
 
*************************************************************************
15100
 
** This file contains the C functions that implement a memory
15101
 
** allocation subsystem for use by SQLite. 
15102
 
**
15103
 
** This version of the memory allocation subsystem omits all
15104
 
** use of malloc(). The SQLite user supplies a block of memory
15105
 
** before calling sqlite3_initialize() from which allocations
15106
 
** are made and returned by the xMalloc() and xRealloc() 
15107
 
** implementations. Once sqlite3_initialize() has been called,
15108
 
** the amount of memory available to SQLite is fixed and cannot
15109
 
** be changed.
15110
 
**
15111
 
** This version of the memory allocation subsystem is included
15112
 
** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
15113
 
*/
15114
 
 
15115
 
/*
15116
 
** This version of the memory allocator is only built into the library
15117
 
** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
15118
 
** mean that the library will use a memory-pool by default, just that
15119
 
** it is available. The mempool allocator is activated by calling
15120
 
** sqlite3_config().
15121
 
*/
15122
 
#ifdef SQLITE_ENABLE_MEMSYS3
15123
 
 
15124
 
/*
15125
 
** Maximum size (in Mem3Blocks) of a "small" chunk.
15126
 
*/
15127
 
#define MX_SMALL 10
15128
 
 
15129
 
 
15130
 
/*
15131
 
** Number of freelist hash slots
15132
 
*/
15133
 
#define N_HASH  61
15134
 
 
15135
 
/*
15136
 
** A memory allocation (also called a "chunk") consists of two or 
15137
 
** more blocks where each block is 8 bytes.  The first 8 bytes are 
15138
 
** a header that is not returned to the user.
15139
 
**
15140
 
** A chunk is two or more blocks that is either checked out or
15141
 
** free.  The first block has format u.hdr.  u.hdr.size4x is 4 times the
15142
 
** size of the allocation in blocks if the allocation is free.
15143
 
** The u.hdr.size4x&1 bit is true if the chunk is checked out and
15144
 
** false if the chunk is on the freelist.  The u.hdr.size4x&2 bit
15145
 
** is true if the previous chunk is checked out and false if the
15146
 
** previous chunk is free.  The u.hdr.prevSize field is the size of
15147
 
** the previous chunk in blocks if the previous chunk is on the
15148
 
** freelist. If the previous chunk is checked out, then
15149
 
** u.hdr.prevSize can be part of the data for that chunk and should
15150
 
** not be read or written.
15151
 
**
15152
 
** We often identify a chunk by its index in mem3.aPool[].  When
15153
 
** this is done, the chunk index refers to the second block of
15154
 
** the chunk.  In this way, the first chunk has an index of 1.
15155
 
** A chunk index of 0 means "no such chunk" and is the equivalent
15156
 
** of a NULL pointer.
15157
 
**
15158
 
** The second block of free chunks is of the form u.list.  The
15159
 
** two fields form a double-linked list of chunks of related sizes.
15160
 
** Pointers to the head of the list are stored in mem3.aiSmall[] 
15161
 
** for smaller chunks and mem3.aiHash[] for larger chunks.
15162
 
**
15163
 
** The second block of a chunk is user data if the chunk is checked 
15164
 
** out.  If a chunk is checked out, the user data may extend into
15165
 
** the u.hdr.prevSize value of the following chunk.
15166
 
*/
15167
 
typedef struct Mem3Block Mem3Block;
15168
 
struct Mem3Block {
15169
 
  union {
15170
 
    struct {
15171
 
      u32 prevSize;   /* Size of previous chunk in Mem3Block elements */
15172
 
      u32 size4x;     /* 4x the size of current chunk in Mem3Block elements */
15173
 
    } hdr;
15174
 
    struct {
15175
 
      u32 next;       /* Index in mem3.aPool[] of next free chunk */
15176
 
      u32 prev;       /* Index in mem3.aPool[] of previous free chunk */
15177
 
    } list;
15178
 
  } u;
15179
 
};
15180
 
 
15181
 
/*
15182
 
** All of the static variables used by this module are collected
15183
 
** into a single structure named "mem3".  This is to keep the
15184
 
** static variables organized and to reduce namespace pollution
15185
 
** when this module is combined with other in the amalgamation.
15186
 
*/
15187
 
static SQLITE_WSD struct Mem3Global {
15188
 
  /*
15189
 
  ** Memory available for allocation. nPool is the size of the array
15190
 
  ** (in Mem3Blocks) pointed to by aPool less 2.
15191
 
  */
15192
 
  u32 nPool;
15193
 
  Mem3Block *aPool;
15194
 
 
15195
 
  /*
15196
 
  ** True if we are evaluating an out-of-memory callback.
15197
 
  */
15198
 
  int alarmBusy;
15199
 
  
15200
 
  /*
15201
 
  ** Mutex to control access to the memory allocation subsystem.
15202
 
  */
15203
 
  sqlite3_mutex *mutex;
15204
 
  
15205
 
  /*
15206
 
  ** The minimum amount of free space that we have seen.
15207
 
  */
15208
 
  u32 mnMaster;
15209
 
 
15210
 
  /*
15211
 
  ** iMaster is the index of the master chunk.  Most new allocations
15212
 
  ** occur off of this chunk.  szMaster is the size (in Mem3Blocks)
15213
 
  ** of the current master.  iMaster is 0 if there is not master chunk.
15214
 
  ** The master chunk is not in either the aiHash[] or aiSmall[].
15215
 
  */
15216
 
  u32 iMaster;
15217
 
  u32 szMaster;
15218
 
 
15219
 
  /*
15220
 
  ** Array of lists of free blocks according to the block size 
15221
 
  ** for smaller chunks, or a hash on the block size for larger
15222
 
  ** chunks.
15223
 
  */
15224
 
  u32 aiSmall[MX_SMALL-1];   /* For sizes 2 through MX_SMALL, inclusive */
15225
 
  u32 aiHash[N_HASH];        /* For sizes MX_SMALL+1 and larger */
15226
 
} mem3 = { 97535575 };
15227
 
 
15228
 
#define mem3 GLOBAL(struct Mem3Global, mem3)
15229
 
 
15230
 
/*
15231
 
** Unlink the chunk at mem3.aPool[i] from list it is currently
15232
 
** on.  *pRoot is the list that i is a member of.
15233
 
*/
15234
 
static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
15235
 
  u32 next = mem3.aPool[i].u.list.next;
15236
 
  u32 prev = mem3.aPool[i].u.list.prev;
15237
 
  assert( sqlite3_mutex_held(mem3.mutex) );
15238
 
  if( prev==0 ){
15239
 
    *pRoot = next;
15240
 
  }else{
15241
 
    mem3.aPool[prev].u.list.next = next;
15242
 
  }
15243
 
  if( next ){
15244
 
    mem3.aPool[next].u.list.prev = prev;
15245
 
  }
15246
 
  mem3.aPool[i].u.list.next = 0;
15247
 
  mem3.aPool[i].u.list.prev = 0;
15248
 
}
15249
 
 
15250
 
/*
15251
 
** Unlink the chunk at index i from 
15252
 
** whatever list is currently a member of.
15253
 
*/
15254
 
static void memsys3Unlink(u32 i){
15255
 
  u32 size, hash;
15256
 
  assert( sqlite3_mutex_held(mem3.mutex) );
15257
 
  assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
15258
 
  assert( i>=1 );
15259
 
  size = mem3.aPool[i-1].u.hdr.size4x/4;
15260
 
  assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
15261
 
  assert( size>=2 );
15262
 
  if( size <= MX_SMALL ){
15263
 
    memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
15264
 
  }else{
15265
 
    hash = size % N_HASH;
15266
 
    memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
15267
 
  }
15268
 
}
15269
 
 
15270
 
/*
15271
 
** Link the chunk at mem3.aPool[i] so that is on the list rooted
15272
 
** at *pRoot.
15273
 
*/
15274
 
static void memsys3LinkIntoList(u32 i, u32 *pRoot){
15275
 
  assert( sqlite3_mutex_held(mem3.mutex) );
15276
 
  mem3.aPool[i].u.list.next = *pRoot;
15277
 
  mem3.aPool[i].u.list.prev = 0;
15278
 
  if( *pRoot ){
15279
 
    mem3.aPool[*pRoot].u.list.prev = i;
15280
 
  }
15281
 
  *pRoot = i;
15282
 
}
15283
 
 
15284
 
/*
15285
 
** Link the chunk at index i into either the appropriate
15286
 
** small chunk list, or into the large chunk hash table.
15287
 
*/
15288
 
static void memsys3Link(u32 i){
15289
 
  u32 size, hash;
15290
 
  assert( sqlite3_mutex_held(mem3.mutex) );
15291
 
  assert( i>=1 );
15292
 
  assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
15293
 
  size = mem3.aPool[i-1].u.hdr.size4x/4;
15294
 
  assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
15295
 
  assert( size>=2 );
15296
 
  if( size <= MX_SMALL ){
15297
 
    memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
15298
 
  }else{
15299
 
    hash = size % N_HASH;
15300
 
    memsys3LinkIntoList(i, &mem3.aiHash[hash]);
15301
 
  }
15302
 
}
15303
 
 
15304
 
/*
15305
 
** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
15306
 
** will already be held (obtained by code in malloc.c) if
15307
 
** sqlite3GlobalConfig.bMemStat is true.
15308
 
*/
15309
 
static void memsys3Enter(void){
15310
 
  if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
15311
 
    mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
15312
 
  }
15313
 
  sqlite3_mutex_enter(mem3.mutex);
15314
 
}
15315
 
static void memsys3Leave(void){
15316
 
  sqlite3_mutex_leave(mem3.mutex);
15317
 
}
15318
 
 
15319
 
/*
15320
 
** Called when we are unable to satisfy an allocation of nBytes.
15321
 
*/
15322
 
static void memsys3OutOfMemory(int nByte){
15323
 
  if( !mem3.alarmBusy ){
15324
 
    mem3.alarmBusy = 1;
15325
 
    assert( sqlite3_mutex_held(mem3.mutex) );
15326
 
    sqlite3_mutex_leave(mem3.mutex);
15327
 
    sqlite3_release_memory(nByte);
15328
 
    sqlite3_mutex_enter(mem3.mutex);
15329
 
    mem3.alarmBusy = 0;
15330
 
  }
15331
 
}
15332
 
 
15333
 
 
15334
 
/*
15335
 
** Chunk i is a free chunk that has been unlinked.  Adjust its 
15336
 
** size parameters for check-out and return a pointer to the 
15337
 
** user portion of the chunk.
15338
 
*/
15339
 
static void *memsys3Checkout(u32 i, u32 nBlock){
15340
 
  u32 x;
15341
 
  assert( sqlite3_mutex_held(mem3.mutex) );
15342
 
  assert( i>=1 );
15343
 
  assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
15344
 
  assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
15345
 
  x = mem3.aPool[i-1].u.hdr.size4x;
15346
 
  mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
15347
 
  mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
15348
 
  mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
15349
 
  return &mem3.aPool[i];
15350
 
}
15351
 
 
15352
 
/*
15353
 
** Carve a piece off of the end of the mem3.iMaster free chunk.
15354
 
** Return a pointer to the new allocation.  Or, if the master chunk
15355
 
** is not large enough, return 0.
15356
 
*/
15357
 
static void *memsys3FromMaster(u32 nBlock){
15358
 
  assert( sqlite3_mutex_held(mem3.mutex) );
15359
 
  assert( mem3.szMaster>=nBlock );
15360
 
  if( nBlock>=mem3.szMaster-1 ){
15361
 
    /* Use the entire master */
15362
 
    void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
15363
 
    mem3.iMaster = 0;
15364
 
    mem3.szMaster = 0;
15365
 
    mem3.mnMaster = 0;
15366
 
    return p;
15367
 
  }else{
15368
 
    /* Split the master block.  Return the tail. */
15369
 
    u32 newi, x;
15370
 
    newi = mem3.iMaster + mem3.szMaster - nBlock;
15371
 
    assert( newi > mem3.iMaster+1 );
15372
 
    mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
15373
 
    mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
15374
 
    mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
15375
 
    mem3.szMaster -= nBlock;
15376
 
    mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
15377
 
    x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
15378
 
    mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
15379
 
    if( mem3.szMaster < mem3.mnMaster ){
15380
 
      mem3.mnMaster = mem3.szMaster;
15381
 
    }
15382
 
    return (void*)&mem3.aPool[newi];
15383
 
  }
15384
 
}
15385
 
 
15386
 
/*
15387
 
** *pRoot is the head of a list of free chunks of the same size
15388
 
** or same size hash.  In other words, *pRoot is an entry in either
15389
 
** mem3.aiSmall[] or mem3.aiHash[].  
15390
 
**
15391
 
** This routine examines all entries on the given list and tries
15392
 
** to coalesce each entries with adjacent free chunks.  
15393
 
**
15394
 
** If it sees a chunk that is larger than mem3.iMaster, it replaces 
15395
 
** the current mem3.iMaster with the new larger chunk.  In order for
15396
 
** this mem3.iMaster replacement to work, the master chunk must be
15397
 
** linked into the hash tables.  That is not the normal state of
15398
 
** affairs, of course.  The calling routine must link the master
15399
 
** chunk before invoking this routine, then must unlink the (possibly
15400
 
** changed) master chunk once this routine has finished.
15401
 
*/
15402
 
static void memsys3Merge(u32 *pRoot){
15403
 
  u32 iNext, prev, size, i, x;
15404
 
 
15405
 
  assert( sqlite3_mutex_held(mem3.mutex) );
15406
 
  for(i=*pRoot; i>0; i=iNext){
15407
 
    iNext = mem3.aPool[i].u.list.next;
15408
 
    size = mem3.aPool[i-1].u.hdr.size4x;
15409
 
    assert( (size&1)==0 );
15410
 
    if( (size&2)==0 ){
15411
 
      memsys3UnlinkFromList(i, pRoot);
15412
 
      assert( i > mem3.aPool[i-1].u.hdr.prevSize );
15413
 
      prev = i - mem3.aPool[i-1].u.hdr.prevSize;
15414
 
      if( prev==iNext ){
15415
 
        iNext = mem3.aPool[prev].u.list.next;
15416
 
      }
15417
 
      memsys3Unlink(prev);
15418
 
      size = i + size/4 - prev;
15419
 
      x = mem3.aPool[prev-1].u.hdr.size4x & 2;
15420
 
      mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
15421
 
      mem3.aPool[prev+size-1].u.hdr.prevSize = size;
15422
 
      memsys3Link(prev);
15423
 
      i = prev;
15424
 
    }else{
15425
 
      size /= 4;
15426
 
    }
15427
 
    if( size>mem3.szMaster ){
15428
 
      mem3.iMaster = i;
15429
 
      mem3.szMaster = size;
15430
 
    }
15431
 
  }
15432
 
}
15433
 
 
15434
 
/*
15435
 
** Return a block of memory of at least nBytes in size.
15436
 
** Return NULL if unable.
15437
 
**
15438
 
** This function assumes that the necessary mutexes, if any, are
15439
 
** already held by the caller. Hence "Unsafe".
15440
 
*/
15441
 
static void *memsys3MallocUnsafe(int nByte){
15442
 
  u32 i;
15443
 
  u32 nBlock;
15444
 
  u32 toFree;
15445
 
 
15446
 
  assert( sqlite3_mutex_held(mem3.mutex) );
15447
 
  assert( sizeof(Mem3Block)==8 );
15448
 
  if( nByte<=12 ){
15449
 
    nBlock = 2;
15450
 
  }else{
15451
 
    nBlock = (nByte + 11)/8;
15452
 
  }
15453
 
  assert( nBlock>=2 );
15454
 
 
15455
 
  /* STEP 1:
15456
 
  ** Look for an entry of the correct size in either the small
15457
 
  ** chunk table or in the large chunk hash table.  This is
15458
 
  ** successful most of the time (about 9 times out of 10).
15459
 
  */
15460
 
  if( nBlock <= MX_SMALL ){
15461
 
    i = mem3.aiSmall[nBlock-2];
15462
 
    if( i>0 ){
15463
 
      memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
15464
 
      return memsys3Checkout(i, nBlock);
15465
 
    }
15466
 
  }else{
15467
 
    int hash = nBlock % N_HASH;
15468
 
    for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
15469
 
      if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
15470
 
        memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
15471
 
        return memsys3Checkout(i, nBlock);
15472
 
      }
15473
 
    }
15474
 
  }
15475
 
 
15476
 
  /* STEP 2:
15477
 
  ** Try to satisfy the allocation by carving a piece off of the end
15478
 
  ** of the master chunk.  This step usually works if step 1 fails.
15479
 
  */
15480
 
  if( mem3.szMaster>=nBlock ){
15481
 
    return memsys3FromMaster(nBlock);
15482
 
  }
15483
 
 
15484
 
 
15485
 
  /* STEP 3:  
15486
 
  ** Loop through the entire memory pool.  Coalesce adjacent free
15487
 
  ** chunks.  Recompute the master chunk as the largest free chunk.
15488
 
  ** Then try again to satisfy the allocation by carving a piece off
15489
 
  ** of the end of the master chunk.  This step happens very
15490
 
  ** rarely (we hope!)
15491
 
  */
15492
 
  for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
15493
 
    memsys3OutOfMemory(toFree);
15494
 
    if( mem3.iMaster ){
15495
 
      memsys3Link(mem3.iMaster);
15496
 
      mem3.iMaster = 0;
15497
 
      mem3.szMaster = 0;
15498
 
    }
15499
 
    for(i=0; i<N_HASH; i++){
15500
 
      memsys3Merge(&mem3.aiHash[i]);
15501
 
    }
15502
 
    for(i=0; i<MX_SMALL-1; i++){
15503
 
      memsys3Merge(&mem3.aiSmall[i]);
15504
 
    }
15505
 
    if( mem3.szMaster ){
15506
 
      memsys3Unlink(mem3.iMaster);
15507
 
      if( mem3.szMaster>=nBlock ){
15508
 
        return memsys3FromMaster(nBlock);
15509
 
      }
15510
 
    }
15511
 
  }
15512
 
 
15513
 
  /* If none of the above worked, then we fail. */
15514
 
  return 0;
15515
 
}
15516
 
 
15517
 
/*
15518
 
** Free an outstanding memory allocation.
15519
 
**
15520
 
** This function assumes that the necessary mutexes, if any, are
15521
 
** already held by the caller. Hence "Unsafe".
15522
 
*/
15523
 
void memsys3FreeUnsafe(void *pOld){
15524
 
  Mem3Block *p = (Mem3Block*)pOld;
15525
 
  int i;
15526
 
  u32 size, x;
15527
 
  assert( sqlite3_mutex_held(mem3.mutex) );
15528
 
  assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
15529
 
  i = p - mem3.aPool;
15530
 
  assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
15531
 
  size = mem3.aPool[i-1].u.hdr.size4x/4;
15532
 
  assert( i+size<=mem3.nPool+1 );
15533
 
  mem3.aPool[i-1].u.hdr.size4x &= ~1;
15534
 
  mem3.aPool[i+size-1].u.hdr.prevSize = size;
15535
 
  mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
15536
 
  memsys3Link(i);
15537
 
 
15538
 
  /* Try to expand the master using the newly freed chunk */
15539
 
  if( mem3.iMaster ){
15540
 
    while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
15541
 
      size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
15542
 
      mem3.iMaster -= size;
15543
 
      mem3.szMaster += size;
15544
 
      memsys3Unlink(mem3.iMaster);
15545
 
      x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
15546
 
      mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
15547
 
      mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
15548
 
    }
15549
 
    x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
15550
 
    while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
15551
 
      memsys3Unlink(mem3.iMaster+mem3.szMaster);
15552
 
      mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
15553
 
      mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
15554
 
      mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
15555
 
    }
15556
 
  }
15557
 
}
15558
 
 
15559
 
/*
15560
 
** Return the size of an outstanding allocation, in bytes.  The
15561
 
** size returned omits the 8-byte header overhead.  This only
15562
 
** works for chunks that are currently checked out.
15563
 
*/
15564
 
static int memsys3Size(void *p){
15565
 
  Mem3Block *pBlock;
15566
 
  if( p==0 ) return 0;
15567
 
  pBlock = (Mem3Block*)p;
15568
 
  assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
15569
 
  return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
15570
 
}
15571
 
 
15572
 
/*
15573
 
** Round up a request size to the next valid allocation size.
15574
 
*/
15575
 
static int memsys3Roundup(int n){
15576
 
  if( n<=12 ){
15577
 
    return 12;
15578
 
  }else{
15579
 
    return ((n+11)&~7) - 4;
15580
 
  }
15581
 
}
15582
 
 
15583
 
/*
15584
 
** Allocate nBytes of memory.
15585
 
*/
15586
 
static void *memsys3Malloc(int nBytes){
15587
 
  sqlite3_int64 *p;
15588
 
  assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
15589
 
  memsys3Enter();
15590
 
  p = memsys3MallocUnsafe(nBytes);
15591
 
  memsys3Leave();
15592
 
  return (void*)p; 
15593
 
}
15594
 
 
15595
 
/*
15596
 
** Free memory.
15597
 
*/
15598
 
void memsys3Free(void *pPrior){
15599
 
  assert( pPrior );
15600
 
  memsys3Enter();
15601
 
  memsys3FreeUnsafe(pPrior);
15602
 
  memsys3Leave();
15603
 
}
15604
 
 
15605
 
/*
15606
 
** Change the size of an existing memory allocation
15607
 
*/
15608
 
void *memsys3Realloc(void *pPrior, int nBytes){
15609
 
  int nOld;
15610
 
  void *p;
15611
 
  if( pPrior==0 ){
15612
 
    return sqlite3_malloc(nBytes);
15613
 
  }
15614
 
  if( nBytes<=0 ){
15615
 
    sqlite3_free(pPrior);
15616
 
    return 0;
15617
 
  }
15618
 
  nOld = memsys3Size(pPrior);
15619
 
  if( nBytes<=nOld && nBytes>=nOld-128 ){
15620
 
    return pPrior;
15621
 
  }
15622
 
  memsys3Enter();
15623
 
  p = memsys3MallocUnsafe(nBytes);
15624
 
  if( p ){
15625
 
    if( nOld<nBytes ){
15626
 
      memcpy(p, pPrior, nOld);
15627
 
    }else{
15628
 
      memcpy(p, pPrior, nBytes);
15629
 
    }
15630
 
    memsys3FreeUnsafe(pPrior);
15631
 
  }
15632
 
  memsys3Leave();
15633
 
  return p;
15634
 
}
15635
 
 
15636
 
/*
15637
 
** Initialize this module.
15638
 
*/
15639
 
static int memsys3Init(void *NotUsed){
15640
 
  UNUSED_PARAMETER(NotUsed);
15641
 
  if( !sqlite3GlobalConfig.pHeap ){
15642
 
    return SQLITE_ERROR;
15643
 
  }
15644
 
 
15645
 
  /* Store a pointer to the memory block in global structure mem3. */
15646
 
  assert( sizeof(Mem3Block)==8 );
15647
 
  mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
15648
 
  mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
15649
 
 
15650
 
  /* Initialize the master block. */
15651
 
  mem3.szMaster = mem3.nPool;
15652
 
  mem3.mnMaster = mem3.szMaster;
15653
 
  mem3.iMaster = 1;
15654
 
  mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
15655
 
  mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
15656
 
  mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
15657
 
 
15658
 
  return SQLITE_OK;
15659
 
}
15660
 
 
15661
 
/*
15662
 
** Deinitialize this module.
15663
 
*/
15664
 
static void memsys3Shutdown(void *NotUsed){
15665
 
  UNUSED_PARAMETER(NotUsed);
15666
 
  mem3.mutex = 0;
15667
 
  return;
15668
 
}
15669
 
 
15670
 
 
15671
 
 
15672
 
/*
15673
 
** Open the file indicated and write a log of all unfreed memory 
15674
 
** allocations into that log.
15675
 
*/
15676
 
SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
15677
 
#ifdef SQLITE_DEBUG
15678
 
  FILE *out;
15679
 
  u32 i, j;
15680
 
  u32 size;
15681
 
  if( zFilename==0 || zFilename[0]==0 ){
15682
 
    out = stdout;
15683
 
  }else{
15684
 
    out = fopen(zFilename, "w");
15685
 
    if( out==0 ){
15686
 
      fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
15687
 
                      zFilename);
15688
 
      return;
15689
 
    }
15690
 
  }
15691
 
  memsys3Enter();
15692
 
  fprintf(out, "CHUNKS:\n");
15693
 
  for(i=1; i<=mem3.nPool; i+=size/4){
15694
 
    size = mem3.aPool[i-1].u.hdr.size4x;
15695
 
    if( size/4<=1 ){
15696
 
      fprintf(out, "%p size error\n", &mem3.aPool[i]);
15697
 
      assert( 0 );
15698
 
      break;
15699
 
    }
15700
 
    if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
15701
 
      fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
15702
 
      assert( 0 );
15703
 
      break;
15704
 
    }
15705
 
    if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
15706
 
      fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
15707
 
      assert( 0 );
15708
 
      break;
15709
 
    }
15710
 
    if( size&1 ){
15711
 
      fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
15712
 
    }else{
15713
 
      fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
15714
 
                  i==mem3.iMaster ? " **master**" : "");
15715
 
    }
15716
 
  }
15717
 
  for(i=0; i<MX_SMALL-1; i++){
15718
 
    if( mem3.aiSmall[i]==0 ) continue;
15719
 
    fprintf(out, "small(%2d):", i);
15720
 
    for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
15721
 
      fprintf(out, " %p(%d)", &mem3.aPool[j],
15722
 
              (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
15723
 
    }
15724
 
    fprintf(out, "\n"); 
15725
 
  }
15726
 
  for(i=0; i<N_HASH; i++){
15727
 
    if( mem3.aiHash[i]==0 ) continue;
15728
 
    fprintf(out, "hash(%2d):", i);
15729
 
    for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
15730
 
      fprintf(out, " %p(%d)", &mem3.aPool[j],
15731
 
              (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
15732
 
    }
15733
 
    fprintf(out, "\n"); 
15734
 
  }
15735
 
  fprintf(out, "master=%d\n", mem3.iMaster);
15736
 
  fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
15737
 
  fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
15738
 
  sqlite3_mutex_leave(mem3.mutex);
15739
 
  if( out==stdout ){
15740
 
    fflush(stdout);
15741
 
  }else{
15742
 
    fclose(out);
15743
 
  }
15744
 
#else
15745
 
  UNUSED_PARAMETER(zFilename);
15746
 
#endif
15747
 
}
15748
 
 
15749
 
/*
15750
 
** This routine is the only routine in this file with external 
15751
 
** linkage.
15752
 
**
15753
 
** Populate the low-level memory allocation function pointers in
15754
 
** sqlite3GlobalConfig.m with pointers to the routines in this file. The
15755
 
** arguments specify the block of memory to manage.
15756
 
**
15757
 
** This routine is only called by sqlite3_config(), and therefore
15758
 
** is not required to be threadsafe (it is not).
15759
 
*/
15760
 
SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
15761
 
  static const sqlite3_mem_methods mempoolMethods = {
15762
 
     memsys3Malloc,
15763
 
     memsys3Free,
15764
 
     memsys3Realloc,
15765
 
     memsys3Size,
15766
 
     memsys3Roundup,
15767
 
     memsys3Init,
15768
 
     memsys3Shutdown,
15769
 
     0
15770
 
  };
15771
 
  return &mempoolMethods;
15772
 
}
15773
 
 
15774
 
#endif /* SQLITE_ENABLE_MEMSYS3 */
15775
 
 
15776
 
/************** End of mem3.c ************************************************/
15777
 
/************** Begin file mem5.c ********************************************/
15778
 
/*
15779
 
** 2007 October 14
15780
 
**
15781
 
** The author disclaims copyright to this source code.  In place of
15782
 
** a legal notice, here is a blessing:
15783
 
**
15784
 
**    May you do good and not evil.
15785
 
**    May you find forgiveness for yourself and forgive others.
15786
 
**    May you share freely, never taking more than you give.
15787
 
**
15788
 
*************************************************************************
15789
 
** This file contains the C functions that implement a memory
15790
 
** allocation subsystem for use by SQLite. 
15791
 
**
15792
 
** This version of the memory allocation subsystem omits all
15793
 
** use of malloc(). The application gives SQLite a block of memory
15794
 
** before calling sqlite3_initialize() from which allocations
15795
 
** are made and returned by the xMalloc() and xRealloc() 
15796
 
** implementations. Once sqlite3_initialize() has been called,
15797
 
** the amount of memory available to SQLite is fixed and cannot
15798
 
** be changed.
15799
 
**
15800
 
** This version of the memory allocation subsystem is included
15801
 
** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
15802
 
**
15803
 
** This memory allocator uses the following algorithm:
15804
 
**
15805
 
**   1.  All memory allocations sizes are rounded up to a power of 2.
15806
 
**
15807
 
**   2.  If two adjacent free blocks are the halves of a larger block,
15808
 
**       then the two blocks are coalesed into the single larger block.
15809
 
**
15810
 
**   3.  New memory is allocated from the first available free block.
15811
 
**
15812
 
** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
15813
 
** Concerning Dynamic Storage Allocation". Journal of the Association for
15814
 
** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
15815
 
** 
15816
 
** Let n be the size of the largest allocation divided by the minimum
15817
 
** allocation size (after rounding all sizes up to a power of 2.)  Let M
15818
 
** be the maximum amount of memory ever outstanding at one time.  Let
15819
 
** N be the total amount of memory available for allocation.  Robson
15820
 
** proved that this memory allocator will never breakdown due to 
15821
 
** fragmentation as long as the following constraint holds:
15822
 
**
15823
 
**      N >=  M*(1 + log2(n)/2) - n + 1
15824
 
**
15825
 
** The sqlite3_status() logic tracks the maximum values of n and M so
15826
 
** that an application can, at any time, verify this constraint.
15827
 
*/
15828
 
 
15829
 
/*
15830
 
** This version of the memory allocator is used only when 
15831
 
** SQLITE_ENABLE_MEMSYS5 is defined.
15832
 
*/
15833
 
#ifdef SQLITE_ENABLE_MEMSYS5
15834
 
 
15835
 
/*
15836
 
** A minimum allocation is an instance of the following structure.
15837
 
** Larger allocations are an array of these structures where the
15838
 
** size of the array is a power of 2.
15839
 
**
15840
 
** The size of this object must be a power of two.  That fact is
15841
 
** verified in memsys5Init().
15842
 
*/
15843
 
typedef struct Mem5Link Mem5Link;
15844
 
struct Mem5Link {
15845
 
  int next;       /* Index of next free chunk */
15846
 
  int prev;       /* Index of previous free chunk */
15847
 
};
15848
 
 
15849
 
/*
15850
 
** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
15851
 
** mem5.szAtom is always at least 8 and 32-bit integers are used,
15852
 
** it is not actually possible to reach this limit.
15853
 
*/
15854
 
#define LOGMAX 30
15855
 
 
15856
 
/*
15857
 
** Masks used for mem5.aCtrl[] elements.
15858
 
*/
15859
 
#define CTRL_LOGSIZE  0x1f    /* Log2 Size of this block */
15860
 
#define CTRL_FREE     0x20    /* True if not checked out */
15861
 
 
15862
 
/*
15863
 
** All of the static variables used by this module are collected
15864
 
** into a single structure named "mem5".  This is to keep the
15865
 
** static variables organized and to reduce namespace pollution
15866
 
** when this module is combined with other in the amalgamation.
15867
 
*/
15868
 
static SQLITE_WSD struct Mem5Global {
15869
 
  /*
15870
 
  ** Memory available for allocation
15871
 
  */
15872
 
  int szAtom;      /* Smallest possible allocation in bytes */
15873
 
  int nBlock;      /* Number of szAtom sized blocks in zPool */
15874
 
  u8 *zPool;       /* Memory available to be allocated */
15875
 
  
15876
 
  /*
15877
 
  ** Mutex to control access to the memory allocation subsystem.
15878
 
  */
15879
 
  sqlite3_mutex *mutex;
15880
 
 
15881
 
  /*
15882
 
  ** Performance statistics
15883
 
  */
15884
 
  u64 nAlloc;         /* Total number of calls to malloc */
15885
 
  u64 totalAlloc;     /* Total of all malloc calls - includes internal frag */
15886
 
  u64 totalExcess;    /* Total internal fragmentation */
15887
 
  u32 currentOut;     /* Current checkout, including internal fragmentation */
15888
 
  u32 currentCount;   /* Current number of distinct checkouts */
15889
 
  u32 maxOut;         /* Maximum instantaneous currentOut */
15890
 
  u32 maxCount;       /* Maximum instantaneous currentCount */
15891
 
  u32 maxRequest;     /* Largest allocation (exclusive of internal frag) */
15892
 
  
15893
 
  /*
15894
 
  ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of
15895
 
  ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2.
15896
 
  ** and so forth.
15897
 
  */
15898
 
  int aiFreelist[LOGMAX+1];
15899
 
 
15900
 
  /*
15901
 
  ** Space for tracking which blocks are checked out and the size
15902
 
  ** of each block.  One byte per block.
15903
 
  */
15904
 
  u8 *aCtrl;
15905
 
 
15906
 
} mem5;
15907
 
 
15908
 
/*
15909
 
** Access the static variable through a macro for SQLITE_OMIT_WSD
15910
 
*/
15911
 
#define mem5 GLOBAL(struct Mem5Global, mem5)
15912
 
 
15913
 
/*
15914
 
** Assuming mem5.zPool is divided up into an array of Mem5Link
15915
 
** structures, return a pointer to the idx-th such lik.
15916
 
*/
15917
 
#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
15918
 
 
15919
 
/*
15920
 
** Unlink the chunk at mem5.aPool[i] from list it is currently
15921
 
** on.  It should be found on mem5.aiFreelist[iLogsize].
15922
 
*/
15923
 
static void memsys5Unlink(int i, int iLogsize){
15924
 
  int next, prev;
15925
 
  assert( i>=0 && i<mem5.nBlock );
15926
 
  assert( iLogsize>=0 && iLogsize<=LOGMAX );
15927
 
  assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
15928
 
 
15929
 
  next = MEM5LINK(i)->next;
15930
 
  prev = MEM5LINK(i)->prev;
15931
 
  if( prev<0 ){
15932
 
    mem5.aiFreelist[iLogsize] = next;
15933
 
  }else{
15934
 
    MEM5LINK(prev)->next = next;
15935
 
  }
15936
 
  if( next>=0 ){
15937
 
    MEM5LINK(next)->prev = prev;
15938
 
  }
15939
 
}
15940
 
 
15941
 
/*
15942
 
** Link the chunk at mem5.aPool[i] so that is on the iLogsize
15943
 
** free list.
15944
 
*/
15945
 
static void memsys5Link(int i, int iLogsize){
15946
 
  int x;
15947
 
  assert( sqlite3_mutex_held(mem5.mutex) );
15948
 
  assert( i>=0 && i<mem5.nBlock );
15949
 
  assert( iLogsize>=0 && iLogsize<=LOGMAX );
15950
 
  assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
15951
 
 
15952
 
  x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
15953
 
  MEM5LINK(i)->prev = -1;
15954
 
  if( x>=0 ){
15955
 
    assert( x<mem5.nBlock );
15956
 
    MEM5LINK(x)->prev = i;
15957
 
  }
15958
 
  mem5.aiFreelist[iLogsize] = i;
15959
 
}
15960
 
 
15961
 
/*
15962
 
** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
15963
 
** will already be held (obtained by code in malloc.c) if
15964
 
** sqlite3GlobalConfig.bMemStat is true.
15965
 
*/
15966
 
static void memsys5Enter(void){
15967
 
  sqlite3_mutex_enter(mem5.mutex);
15968
 
}
15969
 
static void memsys5Leave(void){
15970
 
  sqlite3_mutex_leave(mem5.mutex);
15971
 
}
15972
 
 
15973
 
/*
15974
 
** Return the size of an outstanding allocation, in bytes.  The
15975
 
** size returned omits the 8-byte header overhead.  This only
15976
 
** works for chunks that are currently checked out.
15977
 
*/
15978
 
static int memsys5Size(void *p){
15979
 
  int iSize = 0;
15980
 
  if( p ){
15981
 
    int i = ((u8 *)p-mem5.zPool)/mem5.szAtom;
15982
 
    assert( i>=0 && i<mem5.nBlock );
15983
 
    iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
15984
 
  }
15985
 
  return iSize;
15986
 
}
15987
 
 
15988
 
/*
15989
 
** Find the first entry on the freelist iLogsize.  Unlink that
15990
 
** entry and return its index. 
15991
 
*/
15992
 
static int memsys5UnlinkFirst(int iLogsize){
15993
 
  int i;
15994
 
  int iFirst;
15995
 
 
15996
 
  assert( iLogsize>=0 && iLogsize<=LOGMAX );
15997
 
  i = iFirst = mem5.aiFreelist[iLogsize];
15998
 
  assert( iFirst>=0 );
15999
 
  while( i>0 ){
16000
 
    if( i<iFirst ) iFirst = i;
16001
 
    i = MEM5LINK(i)->next;
16002
 
  }
16003
 
  memsys5Unlink(iFirst, iLogsize);
16004
 
  return iFirst;
16005
 
}
16006
 
 
16007
 
/*
16008
 
** Return a block of memory of at least nBytes in size.
16009
 
** Return NULL if unable.  Return NULL if nBytes==0.
16010
 
**
16011
 
** The caller guarantees that nByte positive.
16012
 
**
16013
 
** The caller has obtained a mutex prior to invoking this
16014
 
** routine so there is never any chance that two or more
16015
 
** threads can be in this routine at the same time.
16016
 
*/
16017
 
static void *memsys5MallocUnsafe(int nByte){
16018
 
  int i;           /* Index of a mem5.aPool[] slot */
16019
 
  int iBin;        /* Index into mem5.aiFreelist[] */
16020
 
  int iFullSz;     /* Size of allocation rounded up to power of 2 */
16021
 
  int iLogsize;    /* Log2 of iFullSz/POW2_MIN */
16022
 
 
16023
 
  /* nByte must be a positive */
16024
 
  assert( nByte>0 );
16025
 
 
16026
 
  /* Keep track of the maximum allocation request.  Even unfulfilled
16027
 
  ** requests are counted */
16028
 
  if( (u32)nByte>mem5.maxRequest ){
16029
 
    mem5.maxRequest = nByte;
16030
 
  }
16031
 
 
16032
 
  /* Abort if the requested allocation size is larger than the largest
16033
 
  ** power of two that we can represent using 32-bit signed integers.
16034
 
  */
16035
 
  if( nByte > 0x40000000 ){
16036
 
    return 0;
16037
 
  }
16038
 
 
16039
 
  /* Round nByte up to the next valid power of two */
16040
 
  for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
16041
 
 
16042
 
  /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
16043
 
  ** block.  If not, then split a block of the next larger power of
16044
 
  ** two in order to create a new free block of size iLogsize.
16045
 
  */
16046
 
  for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
16047
 
  if( iBin>LOGMAX ){
16048
 
    testcase( sqlite3GlobalConfig.xLog!=0 );
16049
 
    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
16050
 
    return 0;
16051
 
  }
16052
 
  i = memsys5UnlinkFirst(iBin);
16053
 
  while( iBin>iLogsize ){
16054
 
    int newSize;
16055
 
 
16056
 
    iBin--;
16057
 
    newSize = 1 << iBin;
16058
 
    mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
16059
 
    memsys5Link(i+newSize, iBin);
16060
 
  }
16061
 
  mem5.aCtrl[i] = iLogsize;
16062
 
 
16063
 
  /* Update allocator performance statistics. */
16064
 
  mem5.nAlloc++;
16065
 
  mem5.totalAlloc += iFullSz;
16066
 
  mem5.totalExcess += iFullSz - nByte;
16067
 
  mem5.currentCount++;
16068
 
  mem5.currentOut += iFullSz;
16069
 
  if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
16070
 
  if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
16071
 
 
16072
 
  /* Return a pointer to the allocated memory. */
16073
 
  return (void*)&mem5.zPool[i*mem5.szAtom];
16074
 
}
16075
 
 
16076
 
/*
16077
 
** Free an outstanding memory allocation.
16078
 
*/
16079
 
static void memsys5FreeUnsafe(void *pOld){
16080
 
  u32 size, iLogsize;
16081
 
  int iBlock;
16082
 
 
16083
 
  /* Set iBlock to the index of the block pointed to by pOld in 
16084
 
  ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
16085
 
  */
16086
 
  iBlock = ((u8 *)pOld-mem5.zPool)/mem5.szAtom;
16087
 
 
16088
 
  /* Check that the pointer pOld points to a valid, non-free block. */
16089
 
  assert( iBlock>=0 && iBlock<mem5.nBlock );
16090
 
  assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
16091
 
  assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
16092
 
 
16093
 
  iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
16094
 
  size = 1<<iLogsize;
16095
 
  assert( iBlock+size-1<(u32)mem5.nBlock );
16096
 
 
16097
 
  mem5.aCtrl[iBlock] |= CTRL_FREE;
16098
 
  mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
16099
 
  assert( mem5.currentCount>0 );
16100
 
  assert( mem5.currentOut>=(size*mem5.szAtom) );
16101
 
  mem5.currentCount--;
16102
 
  mem5.currentOut -= size*mem5.szAtom;
16103
 
  assert( mem5.currentOut>0 || mem5.currentCount==0 );
16104
 
  assert( mem5.currentCount>0 || mem5.currentOut==0 );
16105
 
 
16106
 
  mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
16107
 
  while( ALWAYS(iLogsize<LOGMAX) ){
16108
 
    int iBuddy;
16109
 
    if( (iBlock>>iLogsize) & 1 ){
16110
 
      iBuddy = iBlock - size;
16111
 
    }else{
16112
 
      iBuddy = iBlock + size;
16113
 
    }
16114
 
    assert( iBuddy>=0 );
16115
 
    if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
16116
 
    if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
16117
 
    memsys5Unlink(iBuddy, iLogsize);
16118
 
    iLogsize++;
16119
 
    if( iBuddy<iBlock ){
16120
 
      mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
16121
 
      mem5.aCtrl[iBlock] = 0;
16122
 
      iBlock = iBuddy;
16123
 
    }else{
16124
 
      mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
16125
 
      mem5.aCtrl[iBuddy] = 0;
16126
 
    }
16127
 
    size *= 2;
16128
 
  }
16129
 
  memsys5Link(iBlock, iLogsize);
16130
 
}
16131
 
 
16132
 
/*
16133
 
** Allocate nBytes of memory
16134
 
*/
16135
 
static void *memsys5Malloc(int nBytes){
16136
 
  sqlite3_int64 *p = 0;
16137
 
  if( nBytes>0 ){
16138
 
    memsys5Enter();
16139
 
    p = memsys5MallocUnsafe(nBytes);
16140
 
    memsys5Leave();
16141
 
  }
16142
 
  return (void*)p; 
16143
 
}
16144
 
 
16145
 
/*
16146
 
** Free memory.
16147
 
**
16148
 
** The outer layer memory allocator prevents this routine from
16149
 
** being called with pPrior==0.
16150
 
*/
16151
 
static void memsys5Free(void *pPrior){
16152
 
  assert( pPrior!=0 );
16153
 
  memsys5Enter();
16154
 
  memsys5FreeUnsafe(pPrior);
16155
 
  memsys5Leave();  
16156
 
}
16157
 
 
16158
 
/*
16159
 
** Change the size of an existing memory allocation.
16160
 
**
16161
 
** The outer layer memory allocator prevents this routine from
16162
 
** being called with pPrior==0.  
16163
 
**
16164
 
** nBytes is always a value obtained from a prior call to
16165
 
** memsys5Round().  Hence nBytes is always a non-negative power
16166
 
** of two.  If nBytes==0 that means that an oversize allocation
16167
 
** (an allocation larger than 0x40000000) was requested and this
16168
 
** routine should return 0 without freeing pPrior.
16169
 
*/
16170
 
static void *memsys5Realloc(void *pPrior, int nBytes){
16171
 
  int nOld;
16172
 
  void *p;
16173
 
  assert( pPrior!=0 );
16174
 
  assert( (nBytes&(nBytes-1))==0 );  /* EV: R-46199-30249 */
16175
 
  assert( nBytes>=0 );
16176
 
  if( nBytes==0 ){
16177
 
    return 0;
16178
 
  }
16179
 
  nOld = memsys5Size(pPrior);
16180
 
  if( nBytes<=nOld ){
16181
 
    return pPrior;
16182
 
  }
16183
 
  memsys5Enter();
16184
 
  p = memsys5MallocUnsafe(nBytes);
16185
 
  if( p ){
16186
 
    memcpy(p, pPrior, nOld);
16187
 
    memsys5FreeUnsafe(pPrior);
16188
 
  }
16189
 
  memsys5Leave();
16190
 
  return p;
16191
 
}
16192
 
 
16193
 
/*
16194
 
** Round up a request size to the next valid allocation size.  If
16195
 
** the allocation is too large to be handled by this allocation system,
16196
 
** return 0.
16197
 
**
16198
 
** All allocations must be a power of two and must be expressed by a
16199
 
** 32-bit signed integer.  Hence the largest allocation is 0x40000000
16200
 
** or 1073741824 bytes.
16201
 
*/
16202
 
static int memsys5Roundup(int n){
16203
 
  int iFullSz;
16204
 
  if( n > 0x40000000 ) return 0;
16205
 
  for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
16206
 
  return iFullSz;
16207
 
}
16208
 
 
16209
 
/*
16210
 
** Return the ceiling of the logarithm base 2 of iValue.
16211
 
**
16212
 
** Examples:   memsys5Log(1) -> 0
16213
 
**             memsys5Log(2) -> 1
16214
 
**             memsys5Log(4) -> 2
16215
 
**             memsys5Log(5) -> 3
16216
 
**             memsys5Log(8) -> 3
16217
 
**             memsys5Log(9) -> 4
16218
 
*/
16219
 
static int memsys5Log(int iValue){
16220
 
  int iLog;
16221
 
  for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
16222
 
  return iLog;
16223
 
}
16224
 
 
16225
 
/*
16226
 
** Initialize the memory allocator.
16227
 
**
16228
 
** This routine is not threadsafe.  The caller must be holding a mutex
16229
 
** to prevent multiple threads from entering at the same time.
16230
 
*/
16231
 
static int memsys5Init(void *NotUsed){
16232
 
  int ii;            /* Loop counter */
16233
 
  int nByte;         /* Number of bytes of memory available to this allocator */
16234
 
  u8 *zByte;         /* Memory usable by this allocator */
16235
 
  int nMinLog;       /* Log base 2 of minimum allocation size in bytes */
16236
 
  int iOffset;       /* An offset into mem5.aCtrl[] */
16237
 
 
16238
 
  UNUSED_PARAMETER(NotUsed);
16239
 
 
16240
 
  /* For the purposes of this routine, disable the mutex */
16241
 
  mem5.mutex = 0;
16242
 
 
16243
 
  /* The size of a Mem5Link object must be a power of two.  Verify that
16244
 
  ** this is case.
16245
 
  */
16246
 
  assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
16247
 
 
16248
 
  nByte = sqlite3GlobalConfig.nHeap;
16249
 
  zByte = (u8*)sqlite3GlobalConfig.pHeap;
16250
 
  assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
16251
 
 
16252
 
  /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
16253
 
  nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
16254
 
  mem5.szAtom = (1<<nMinLog);
16255
 
  while( (int)sizeof(Mem5Link)>mem5.szAtom ){
16256
 
    mem5.szAtom = mem5.szAtom << 1;
16257
 
  }
16258
 
 
16259
 
  mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
16260
 
  mem5.zPool = zByte;
16261
 
  mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
16262
 
 
16263
 
  for(ii=0; ii<=LOGMAX; ii++){
16264
 
    mem5.aiFreelist[ii] = -1;
16265
 
  }
16266
 
 
16267
 
  iOffset = 0;
16268
 
  for(ii=LOGMAX; ii>=0; ii--){
16269
 
    int nAlloc = (1<<ii);
16270
 
    if( (iOffset+nAlloc)<=mem5.nBlock ){
16271
 
      mem5.aCtrl[iOffset] = ii | CTRL_FREE;
16272
 
      memsys5Link(iOffset, ii);
16273
 
      iOffset += nAlloc;
16274
 
    }
16275
 
    assert((iOffset+nAlloc)>mem5.nBlock);
16276
 
  }
16277
 
 
16278
 
  /* If a mutex is required for normal operation, allocate one */
16279
 
  if( sqlite3GlobalConfig.bMemstat==0 ){
16280
 
    mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
16281
 
  }
16282
 
 
16283
 
  return SQLITE_OK;
16284
 
}
16285
 
 
16286
 
/*
16287
 
** Deinitialize this module.
16288
 
*/
16289
 
static void memsys5Shutdown(void *NotUsed){
16290
 
  UNUSED_PARAMETER(NotUsed);
16291
 
  mem5.mutex = 0;
16292
 
  return;
16293
 
}
16294
 
 
16295
 
#ifdef SQLITE_TEST
16296
 
/*
16297
 
** Open the file indicated and write a log of all unfreed memory 
16298
 
** allocations into that log.
16299
 
*/
16300
 
SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
16301
 
  FILE *out;
16302
 
  int i, j, n;
16303
 
  int nMinLog;
16304
 
 
16305
 
  if( zFilename==0 || zFilename[0]==0 ){
16306
 
    out = stdout;
16307
 
  }else{
16308
 
    out = fopen(zFilename, "w");
16309
 
    if( out==0 ){
16310
 
      fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
16311
 
                      zFilename);
16312
 
      return;
16313
 
    }
16314
 
  }
16315
 
  memsys5Enter();
16316
 
  nMinLog = memsys5Log(mem5.szAtom);
16317
 
  for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
16318
 
    for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
16319
 
    fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
16320
 
  }
16321
 
  fprintf(out, "mem5.nAlloc       = %llu\n", mem5.nAlloc);
16322
 
  fprintf(out, "mem5.totalAlloc   = %llu\n", mem5.totalAlloc);
16323
 
  fprintf(out, "mem5.totalExcess  = %llu\n", mem5.totalExcess);
16324
 
  fprintf(out, "mem5.currentOut   = %u\n", mem5.currentOut);
16325
 
  fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
16326
 
  fprintf(out, "mem5.maxOut       = %u\n", mem5.maxOut);
16327
 
  fprintf(out, "mem5.maxCount     = %u\n", mem5.maxCount);
16328
 
  fprintf(out, "mem5.maxRequest   = %u\n", mem5.maxRequest);
16329
 
  memsys5Leave();
16330
 
  if( out==stdout ){
16331
 
    fflush(stdout);
16332
 
  }else{
16333
 
    fclose(out);
16334
 
  }
16335
 
}
16336
 
#endif
16337
 
 
16338
 
/*
16339
 
** This routine is the only routine in this file with external 
16340
 
** linkage. It returns a pointer to a static sqlite3_mem_methods
16341
 
** struct populated with the memsys5 methods.
16342
 
*/
16343
 
SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
16344
 
  static const sqlite3_mem_methods memsys5Methods = {
16345
 
     memsys5Malloc,
16346
 
     memsys5Free,
16347
 
     memsys5Realloc,
16348
 
     memsys5Size,
16349
 
     memsys5Roundup,
16350
 
     memsys5Init,
16351
 
     memsys5Shutdown,
16352
 
     0
16353
 
  };
16354
 
  return &memsys5Methods;
16355
 
}
16356
 
 
16357
 
#endif /* SQLITE_ENABLE_MEMSYS5 */
16358
 
 
16359
 
/************** End of mem5.c ************************************************/
16360
 
/************** Begin file mutex.c *******************************************/
16361
 
/*
16362
 
** 2007 August 14
16363
 
**
16364
 
** The author disclaims copyright to this source code.  In place of
16365
 
** a legal notice, here is a blessing:
16366
 
**
16367
 
**    May you do good and not evil.
16368
 
**    May you find forgiveness for yourself and forgive others.
16369
 
**    May you share freely, never taking more than you give.
16370
 
**
16371
 
*************************************************************************
16372
 
** This file contains the C functions that implement mutexes.
16373
 
**
16374
 
** This file contains code that is common across all mutex implementations.
16375
 
*/
16376
 
 
16377
 
#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
16378
 
/*
16379
 
** For debugging purposes, record when the mutex subsystem is initialized
16380
 
** and uninitialized so that we can assert() if there is an attempt to
16381
 
** allocate a mutex while the system is uninitialized.
16382
 
*/
16383
 
static SQLITE_WSD int mutexIsInit = 0;
16384
 
#endif /* SQLITE_DEBUG */
16385
 
 
16386
 
 
16387
 
#ifndef SQLITE_MUTEX_OMIT
16388
 
/*
16389
 
** Initialize the mutex system.
16390
 
*/
16391
 
SQLITE_PRIVATE int sqlite3MutexInit(void){ 
16392
 
  int rc = SQLITE_OK;
16393
 
  if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
16394
 
    /* If the xMutexAlloc method has not been set, then the user did not
16395
 
    ** install a mutex implementation via sqlite3_config() prior to 
16396
 
    ** sqlite3_initialize() being called. This block copies pointers to
16397
 
    ** the default implementation into the sqlite3GlobalConfig structure.
16398
 
    */
16399
 
    sqlite3_mutex_methods const *pFrom;
16400
 
    sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
16401
 
 
16402
 
    if( sqlite3GlobalConfig.bCoreMutex ){
16403
 
      pFrom = sqlite3DefaultMutex();
16404
 
    }else{
16405
 
      pFrom = sqlite3NoopMutex();
16406
 
    }
16407
 
    memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
16408
 
    memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
16409
 
           sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
16410
 
    pTo->xMutexAlloc = pFrom->xMutexAlloc;
16411
 
  }
16412
 
  rc = sqlite3GlobalConfig.mutex.xMutexInit();
16413
 
 
16414
 
#ifdef SQLITE_DEBUG
16415
 
  GLOBAL(int, mutexIsInit) = 1;
16416
 
#endif
16417
 
 
16418
 
  return rc;
16419
 
}
16420
 
 
16421
 
/*
16422
 
** Shutdown the mutex system. This call frees resources allocated by
16423
 
** sqlite3MutexInit().
16424
 
*/
16425
 
SQLITE_PRIVATE int sqlite3MutexEnd(void){
16426
 
  int rc = SQLITE_OK;
16427
 
  if( sqlite3GlobalConfig.mutex.xMutexEnd ){
16428
 
    rc = sqlite3GlobalConfig.mutex.xMutexEnd();
16429
 
  }
16430
 
 
16431
 
#ifdef SQLITE_DEBUG
16432
 
  GLOBAL(int, mutexIsInit) = 0;
16433
 
#endif
16434
 
 
16435
 
  return rc;
16436
 
}
16437
 
 
16438
 
/*
16439
 
** Retrieve a pointer to a static mutex or allocate a new dynamic one.
16440
 
*/
16441
 
SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
16442
 
#ifndef SQLITE_OMIT_AUTOINIT
16443
 
  if( sqlite3_initialize() ) return 0;
16444
 
#endif
16445
 
  return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
16446
 
}
16447
 
 
16448
 
SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
16449
 
  if( !sqlite3GlobalConfig.bCoreMutex ){
16450
 
    return 0;
16451
 
  }
16452
 
  assert( GLOBAL(int, mutexIsInit) );
16453
 
  return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
16454
 
}
16455
 
 
16456
 
/*
16457
 
** Free a dynamic mutex.
16458
 
*/
16459
 
SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
16460
 
  if( p ){
16461
 
    sqlite3GlobalConfig.mutex.xMutexFree(p);
16462
 
  }
16463
 
}
16464
 
 
16465
 
/*
16466
 
** Obtain the mutex p. If some other thread already has the mutex, block
16467
 
** until it can be obtained.
16468
 
*/
16469
 
SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
16470
 
  if( p ){
16471
 
    sqlite3GlobalConfig.mutex.xMutexEnter(p);
16472
 
  }
16473
 
}
16474
 
 
16475
 
/*
16476
 
** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
16477
 
** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
16478
 
*/
16479
 
SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
16480
 
  int rc = SQLITE_OK;
16481
 
  if( p ){
16482
 
    return sqlite3GlobalConfig.mutex.xMutexTry(p);
16483
 
  }
16484
 
  return rc;
16485
 
}
16486
 
 
16487
 
/*
16488
 
** The sqlite3_mutex_leave() routine exits a mutex that was previously
16489
 
** entered by the same thread.  The behavior is undefined if the mutex 
16490
 
** is not currently entered. If a NULL pointer is passed as an argument
16491
 
** this function is a no-op.
16492
 
*/
16493
 
SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
16494
 
  if( p ){
16495
 
    sqlite3GlobalConfig.mutex.xMutexLeave(p);
16496
 
  }
16497
 
}
16498
 
 
16499
 
#ifndef NDEBUG
16500
 
/*
16501
 
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
16502
 
** intended for use inside assert() statements.
16503
 
*/
16504
 
SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
16505
 
  return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
16506
 
}
16507
 
SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
16508
 
  return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
16509
 
}
16510
 
#endif
16511
 
 
16512
 
#endif /* SQLITE_MUTEX_OMIT */
16513
 
 
16514
 
/************** End of mutex.c ***********************************************/
16515
 
/************** Begin file mutex_noop.c **************************************/
16516
 
/*
16517
 
** 2008 October 07
16518
 
**
16519
 
** The author disclaims copyright to this source code.  In place of
16520
 
** a legal notice, here is a blessing:
16521
 
**
16522
 
**    May you do good and not evil.
16523
 
**    May you find forgiveness for yourself and forgive others.
16524
 
**    May you share freely, never taking more than you give.
16525
 
**
16526
 
*************************************************************************
16527
 
** This file contains the C functions that implement mutexes.
16528
 
**
16529
 
** This implementation in this file does not provide any mutual
16530
 
** exclusion and is thus suitable for use only in applications
16531
 
** that use SQLite in a single thread.  The routines defined
16532
 
** here are place-holders.  Applications can substitute working
16533
 
** mutex routines at start-time using the
16534
 
**
16535
 
**     sqlite3_config(SQLITE_CONFIG_MUTEX,...)
16536
 
**
16537
 
** interface.
16538
 
**
16539
 
** If compiled with SQLITE_DEBUG, then additional logic is inserted
16540
 
** that does error checking on mutexes to make sure they are being
16541
 
** called correctly.
16542
 
*/
16543
 
 
16544
 
#ifndef SQLITE_MUTEX_OMIT
16545
 
 
16546
 
#ifndef SQLITE_DEBUG
16547
 
/*
16548
 
** Stub routines for all mutex methods.
16549
 
**
16550
 
** This routines provide no mutual exclusion or error checking.
16551
 
*/
16552
 
static int noopMutexInit(void){ return SQLITE_OK; }
16553
 
static int noopMutexEnd(void){ return SQLITE_OK; }
16554
 
static sqlite3_mutex *noopMutexAlloc(int id){ 
16555
 
  UNUSED_PARAMETER(id);
16556
 
  return (sqlite3_mutex*)8; 
16557
 
}
16558
 
static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
16559
 
static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
16560
 
static int noopMutexTry(sqlite3_mutex *p){
16561
 
  UNUSED_PARAMETER(p);
16562
 
  return SQLITE_OK;
16563
 
}
16564
 
static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
16565
 
 
16566
 
SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
16567
 
  static const sqlite3_mutex_methods sMutex = {
16568
 
    noopMutexInit,
16569
 
    noopMutexEnd,
16570
 
    noopMutexAlloc,
16571
 
    noopMutexFree,
16572
 
    noopMutexEnter,
16573
 
    noopMutexTry,
16574
 
    noopMutexLeave,
16575
 
 
16576
 
    0,
16577
 
    0,
16578
 
  };
16579
 
 
16580
 
  return &sMutex;
16581
 
}
16582
 
#endif /* !SQLITE_DEBUG */
16583
 
 
16584
 
#ifdef SQLITE_DEBUG
16585
 
/*
16586
 
** In this implementation, error checking is provided for testing
16587
 
** and debugging purposes.  The mutexes still do not provide any
16588
 
** mutual exclusion.
16589
 
*/
16590
 
 
16591
 
/*
16592
 
** The mutex object
16593
 
*/
16594
 
typedef struct sqlite3_debug_mutex {
16595
 
  int id;     /* The mutex type */
16596
 
  int cnt;    /* Number of entries without a matching leave */
16597
 
} sqlite3_debug_mutex;
16598
 
 
16599
 
/*
16600
 
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
16601
 
** intended for use inside assert() statements.
16602
 
*/
16603
 
static int debugMutexHeld(sqlite3_mutex *pX){
16604
 
  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
16605
 
  return p==0 || p->cnt>0;
16606
 
}
16607
 
static int debugMutexNotheld(sqlite3_mutex *pX){
16608
 
  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
16609
 
  return p==0 || p->cnt==0;
16610
 
}
16611
 
 
16612
 
/*
16613
 
** Initialize and deinitialize the mutex subsystem.
16614
 
*/
16615
 
static int debugMutexInit(void){ return SQLITE_OK; }
16616
 
static int debugMutexEnd(void){ return SQLITE_OK; }
16617
 
 
16618
 
/*
16619
 
** The sqlite3_mutex_alloc() routine allocates a new
16620
 
** mutex and returns a pointer to it.  If it returns NULL
16621
 
** that means that a mutex could not be allocated. 
16622
 
*/
16623
 
static sqlite3_mutex *debugMutexAlloc(int id){
16624
 
  static sqlite3_debug_mutex aStatic[6];
16625
 
  sqlite3_debug_mutex *pNew = 0;
16626
 
  switch( id ){
16627
 
    case SQLITE_MUTEX_FAST:
16628
 
    case SQLITE_MUTEX_RECURSIVE: {
16629
 
      pNew = sqlite3Malloc(sizeof(*pNew));
16630
 
      if( pNew ){
16631
 
        pNew->id = id;
16632
 
        pNew->cnt = 0;
16633
 
      }
16634
 
      break;
16635
 
    }
16636
 
    default: {
16637
 
      assert( id-2 >= 0 );
16638
 
      assert( id-2 < (int)(sizeof(aStatic)/sizeof(aStatic[0])) );
16639
 
      pNew = &aStatic[id-2];
16640
 
      pNew->id = id;
16641
 
      break;
16642
 
    }
16643
 
  }
16644
 
  return (sqlite3_mutex*)pNew;
16645
 
}
16646
 
 
16647
 
/*
16648
 
** This routine deallocates a previously allocated mutex.
16649
 
*/
16650
 
static void debugMutexFree(sqlite3_mutex *pX){
16651
 
  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
16652
 
  assert( p->cnt==0 );
16653
 
  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
16654
 
  sqlite3_free(p);
16655
 
}
16656
 
 
16657
 
/*
16658
 
** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
16659
 
** to enter a mutex.  If another thread is already within the mutex,
16660
 
** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
16661
 
** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
16662
 
** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
16663
 
** be entered multiple times by the same thread.  In such cases the,
16664
 
** mutex must be exited an equal number of times before another thread
16665
 
** can enter.  If the same thread tries to enter any other kind of mutex
16666
 
** more than once, the behavior is undefined.
16667
 
*/
16668
 
static void debugMutexEnter(sqlite3_mutex *pX){
16669
 
  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
16670
 
  assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
16671
 
  p->cnt++;
16672
 
}
16673
 
static int debugMutexTry(sqlite3_mutex *pX){
16674
 
  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
16675
 
  assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
16676
 
  p->cnt++;
16677
 
  return SQLITE_OK;
16678
 
}
16679
 
 
16680
 
/*
16681
 
** The sqlite3_mutex_leave() routine exits a mutex that was
16682
 
** previously entered by the same thread.  The behavior
16683
 
** is undefined if the mutex is not currently entered or
16684
 
** is not currently allocated.  SQLite will never do either.
16685
 
*/
16686
 
static void debugMutexLeave(sqlite3_mutex *pX){
16687
 
  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
16688
 
  assert( debugMutexHeld(pX) );
16689
 
  p->cnt--;
16690
 
  assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
16691
 
}
16692
 
 
16693
 
SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
16694
 
  static const sqlite3_mutex_methods sMutex = {
16695
 
    debugMutexInit,
16696
 
    debugMutexEnd,
16697
 
    debugMutexAlloc,
16698
 
    debugMutexFree,
16699
 
    debugMutexEnter,
16700
 
    debugMutexTry,
16701
 
    debugMutexLeave,
16702
 
 
16703
 
    debugMutexHeld,
16704
 
    debugMutexNotheld
16705
 
  };
16706
 
 
16707
 
  return &sMutex;
16708
 
}
16709
 
#endif /* SQLITE_DEBUG */
16710
 
 
16711
 
/*
16712
 
** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
16713
 
** is used regardless of the run-time threadsafety setting.
16714
 
*/
16715
 
#ifdef SQLITE_MUTEX_NOOP
16716
 
SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
16717
 
  return sqlite3NoopMutex();
16718
 
}
16719
 
#endif /* SQLITE_MUTEX_NOOP */
16720
 
#endif /* SQLITE_MUTEX_OMIT */
16721
 
 
16722
 
/************** End of mutex_noop.c ******************************************/
16723
 
/************** Begin file mutex_os2.c ***************************************/
16724
 
/*
16725
 
** 2007 August 28
16726
 
**
16727
 
** The author disclaims copyright to this source code.  In place of
16728
 
** a legal notice, here is a blessing:
16729
 
**
16730
 
**    May you do good and not evil.
16731
 
**    May you find forgiveness for yourself and forgive others.
16732
 
**    May you share freely, never taking more than you give.
16733
 
**
16734
 
*************************************************************************
16735
 
** This file contains the C functions that implement mutexes for OS/2
16736
 
*/
16737
 
 
16738
 
/*
16739
 
** The code in this file is only used if SQLITE_MUTEX_OS2 is defined.
16740
 
** See the mutex.h file for details.
16741
 
*/
16742
 
#ifdef SQLITE_MUTEX_OS2
16743
 
 
16744
 
/********************** OS/2 Mutex Implementation **********************
16745
 
**
16746
 
** This implementation of mutexes is built using the OS/2 API.
16747
 
*/
16748
 
 
16749
 
/*
16750
 
** The mutex object
16751
 
** Each recursive mutex is an instance of the following structure.
16752
 
*/
16753
 
struct sqlite3_mutex {
16754
 
  HMTX mutex;       /* Mutex controlling the lock */
16755
 
  int  id;          /* Mutex type */
16756
 
#ifdef SQLITE_DEBUG
16757
 
 int   trace;       /* True to trace changes */
16758
 
#endif
16759
 
};
16760
 
 
16761
 
#ifdef SQLITE_DEBUG
16762
 
#define SQLITE3_MUTEX_INITIALIZER { 0, 0, 0 }
16763
 
#else
16764
 
#define SQLITE3_MUTEX_INITIALIZER { 0, 0 }
16765
 
#endif
16766
 
 
16767
 
/*
16768
 
** Initialize and deinitialize the mutex subsystem.
16769
 
*/
16770
 
static int os2MutexInit(void){ return SQLITE_OK; }
16771
 
static int os2MutexEnd(void){ return SQLITE_OK; }
16772
 
 
16773
 
/*
16774
 
** The sqlite3_mutex_alloc() routine allocates a new
16775
 
** mutex and returns a pointer to it.  If it returns NULL
16776
 
** that means that a mutex could not be allocated. 
16777
 
** SQLite will unwind its stack and return an error.  The argument
16778
 
** to sqlite3_mutex_alloc() is one of these integer constants:
16779
 
**
16780
 
** <ul>
16781
 
** <li>  SQLITE_MUTEX_FAST
16782
 
** <li>  SQLITE_MUTEX_RECURSIVE
16783
 
** <li>  SQLITE_MUTEX_STATIC_MASTER
16784
 
** <li>  SQLITE_MUTEX_STATIC_MEM
16785
 
** <li>  SQLITE_MUTEX_STATIC_MEM2
16786
 
** <li>  SQLITE_MUTEX_STATIC_PRNG
16787
 
** <li>  SQLITE_MUTEX_STATIC_LRU
16788
 
** <li>  SQLITE_MUTEX_STATIC_LRU2
16789
 
** </ul>
16790
 
**
16791
 
** The first two constants cause sqlite3_mutex_alloc() to create
16792
 
** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
16793
 
** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
16794
 
** The mutex implementation does not need to make a distinction
16795
 
** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
16796
 
** not want to.  But SQLite will only request a recursive mutex in
16797
 
** cases where it really needs one.  If a faster non-recursive mutex
16798
 
** implementation is available on the host platform, the mutex subsystem
16799
 
** might return such a mutex in response to SQLITE_MUTEX_FAST.
16800
 
**
16801
 
** The other allowed parameters to sqlite3_mutex_alloc() each return
16802
 
** a pointer to a static preexisting mutex.  Six static mutexes are
16803
 
** used by the current version of SQLite.  Future versions of SQLite
16804
 
** may add additional static mutexes.  Static mutexes are for internal
16805
 
** use by SQLite only.  Applications that use SQLite mutexes should
16806
 
** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
16807
 
** SQLITE_MUTEX_RECURSIVE.
16808
 
**
16809
 
** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
16810
 
** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
16811
 
** returns a different mutex on every call.  But for the static
16812
 
** mutex types, the same mutex is returned on every call that has
16813
 
** the same type number.
16814
 
*/
16815
 
static sqlite3_mutex *os2MutexAlloc(int iType){
16816
 
  sqlite3_mutex *p = NULL;
16817
 
  switch( iType ){
16818
 
    case SQLITE_MUTEX_FAST:
16819
 
    case SQLITE_MUTEX_RECURSIVE: {
16820
 
      p = sqlite3MallocZero( sizeof(*p) );
16821
 
      if( p ){
16822
 
        p->id = iType;
16823
 
        if( DosCreateMutexSem( 0, &p->mutex, 0, FALSE ) != NO_ERROR ){
16824
 
          sqlite3_free( p );
16825
 
          p = NULL;
16826
 
        }
16827
 
      }
16828
 
      break;
16829
 
    }
16830
 
    default: {
16831
 
      static volatile int isInit = 0;
16832
 
      static sqlite3_mutex staticMutexes[6] = {
16833
 
        SQLITE3_MUTEX_INITIALIZER,
16834
 
        SQLITE3_MUTEX_INITIALIZER,
16835
 
        SQLITE3_MUTEX_INITIALIZER,
16836
 
        SQLITE3_MUTEX_INITIALIZER,
16837
 
        SQLITE3_MUTEX_INITIALIZER,
16838
 
        SQLITE3_MUTEX_INITIALIZER,
16839
 
      };
16840
 
      if ( !isInit ){
16841
 
        APIRET rc;
16842
 
        PTIB ptib;
16843
 
        PPIB ppib;
16844
 
        HMTX mutex;
16845
 
        char name[32];
16846
 
        DosGetInfoBlocks( &ptib, &ppib );
16847
 
        sqlite3_snprintf( sizeof(name), name, "\\SEM32\\SQLITE%04x",
16848
 
                          ppib->pib_ulpid );
16849
 
        while( !isInit ){
16850
 
          mutex = 0;
16851
 
          rc = DosCreateMutexSem( name, &mutex, 0, FALSE);
16852
 
          if( rc == NO_ERROR ){
16853
 
            unsigned int i;
16854
 
            if( !isInit ){
16855
 
              for( i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++ ){
16856
 
                DosCreateMutexSem( 0, &staticMutexes[i].mutex, 0, FALSE );
16857
 
              }
16858
 
              isInit = 1;
16859
 
            }
16860
 
            DosCloseMutexSem( mutex );
16861
 
          }else if( rc == ERROR_DUPLICATE_NAME ){
16862
 
            DosSleep( 1 );
16863
 
          }else{
16864
 
            return p;
16865
 
          }
16866
 
        }
16867
 
      }
16868
 
      assert( iType-2 >= 0 );
16869
 
      assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
16870
 
      p = &staticMutexes[iType-2];
16871
 
      p->id = iType;
16872
 
      break;
16873
 
    }
16874
 
  }
16875
 
  return p;
16876
 
}
16877
 
 
16878
 
 
16879
 
/*
16880
 
** This routine deallocates a previously allocated mutex.
16881
 
** SQLite is careful to deallocate every mutex that it allocates.
16882
 
*/
16883
 
static void os2MutexFree(sqlite3_mutex *p){
16884
 
#ifdef SQLITE_DEBUG
16885
 
  TID tid;
16886
 
  PID pid;
16887
 
  ULONG ulCount;
16888
 
  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
16889
 
  assert( ulCount==0 );
16890
 
  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
16891
 
#endif
16892
 
  DosCloseMutexSem( p->mutex );
16893
 
  sqlite3_free( p );
16894
 
}
16895
 
 
16896
 
#ifdef SQLITE_DEBUG
16897
 
/*
16898
 
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
16899
 
** intended for use inside assert() statements.
16900
 
*/
16901
 
static int os2MutexHeld(sqlite3_mutex *p){
16902
 
  TID tid;
16903
 
  PID pid;
16904
 
  ULONG ulCount;
16905
 
  PTIB ptib;
16906
 
  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
16907
 
  if( ulCount==0 || ( ulCount>1 && p->id!=SQLITE_MUTEX_RECURSIVE ) )
16908
 
    return 0;
16909
 
  DosGetInfoBlocks(&ptib, NULL);
16910
 
  return tid==ptib->tib_ptib2->tib2_ultid;
16911
 
}
16912
 
static int os2MutexNotheld(sqlite3_mutex *p){
16913
 
  TID tid;
16914
 
  PID pid;
16915
 
  ULONG ulCount;
16916
 
  PTIB ptib;
16917
 
  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
16918
 
  if( ulCount==0 )
16919
 
    return 1;
16920
 
  DosGetInfoBlocks(&ptib, NULL);
16921
 
  return tid!=ptib->tib_ptib2->tib2_ultid;
16922
 
}
16923
 
static void os2MutexTrace(sqlite3_mutex *p, char *pAction){
16924
 
  TID   tid;
16925
 
  PID   pid;
16926
 
  ULONG ulCount;
16927
 
  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
16928
 
  printf("%s mutex %p (%d) with nRef=%ld\n", pAction, (void*)p, p->trace, ulCount);
16929
 
}
16930
 
#endif
16931
 
 
16932
 
/*
16933
 
** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
16934
 
** to enter a mutex.  If another thread is already within the mutex,
16935
 
** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
16936
 
** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
16937
 
** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
16938
 
** be entered multiple times by the same thread.  In such cases the,
16939
 
** mutex must be exited an equal number of times before another thread
16940
 
** can enter.  If the same thread tries to enter any other kind of mutex
16941
 
** more than once, the behavior is undefined.
16942
 
*/
16943
 
static void os2MutexEnter(sqlite3_mutex *p){
16944
 
  assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
16945
 
  DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT);
16946
 
#ifdef SQLITE_DEBUG
16947
 
  if( p->trace ) os2MutexTrace(p, "enter");
16948
 
#endif
16949
 
}
16950
 
static int os2MutexTry(sqlite3_mutex *p){
16951
 
  int rc = SQLITE_BUSY;
16952
 
  assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
16953
 
  if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR ) {
16954
 
    rc = SQLITE_OK;
16955
 
#ifdef SQLITE_DEBUG
16956
 
    if( p->trace ) os2MutexTrace(p, "try");
16957
 
#endif
16958
 
  }
16959
 
  return rc;
16960
 
}
16961
 
 
16962
 
/*
16963
 
** The sqlite3_mutex_leave() routine exits a mutex that was
16964
 
** previously entered by the same thread.  The behavior
16965
 
** is undefined if the mutex is not currently entered or
16966
 
** is not currently allocated.  SQLite will never do either.
16967
 
*/
16968
 
static void os2MutexLeave(sqlite3_mutex *p){
16969
 
  assert( os2MutexHeld(p) );
16970
 
  DosReleaseMutexSem(p->mutex);
16971
 
#ifdef SQLITE_DEBUG
16972
 
  if( p->trace ) os2MutexTrace(p, "leave");
16973
 
#endif
16974
 
}
16975
 
 
16976
 
SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
16977
 
  static const sqlite3_mutex_methods sMutex = {
16978
 
    os2MutexInit,
16979
 
    os2MutexEnd,
16980
 
    os2MutexAlloc,
16981
 
    os2MutexFree,
16982
 
    os2MutexEnter,
16983
 
    os2MutexTry,
16984
 
    os2MutexLeave,
16985
 
#ifdef SQLITE_DEBUG
16986
 
    os2MutexHeld,
16987
 
    os2MutexNotheld
16988
 
#else
16989
 
    0,
16990
 
    0
16991
 
#endif
16992
 
  };
16993
 
 
16994
 
  return &sMutex;
16995
 
}
16996
 
#endif /* SQLITE_MUTEX_OS2 */
16997
 
 
16998
 
/************** End of mutex_os2.c *******************************************/
16999
 
/************** Begin file mutex_unix.c **************************************/
17000
 
/*
17001
 
** 2007 August 28
17002
 
**
17003
 
** The author disclaims copyright to this source code.  In place of
17004
 
** a legal notice, here is a blessing:
17005
 
**
17006
 
**    May you do good and not evil.
17007
 
**    May you find forgiveness for yourself and forgive others.
17008
 
**    May you share freely, never taking more than you give.
17009
 
**
17010
 
*************************************************************************
17011
 
** This file contains the C functions that implement mutexes for pthreads
17012
 
*/
17013
 
 
17014
 
/*
17015
 
** The code in this file is only used if we are compiling threadsafe
17016
 
** under unix with pthreads.
17017
 
**
17018
 
** Note that this implementation requires a version of pthreads that
17019
 
** supports recursive mutexes.
17020
 
*/
17021
 
#ifdef SQLITE_MUTEX_PTHREADS
17022
 
 
17023
 
#include <pthread.h>
17024
 
 
17025
 
/*
17026
 
** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
17027
 
** are necessary under two condidtions:  (1) Debug builds and (2) using
17028
 
** home-grown mutexes.  Encapsulate these conditions into a single #define.
17029
 
*/
17030
 
#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
17031
 
# define SQLITE_MUTEX_NREF 1
17032
 
#else
17033
 
# define SQLITE_MUTEX_NREF 0
17034
 
#endif
17035
 
 
17036
 
/*
17037
 
** Each recursive mutex is an instance of the following structure.
17038
 
*/
17039
 
struct sqlite3_mutex {
17040
 
  pthread_mutex_t mutex;     /* Mutex controlling the lock */
17041
 
#if SQLITE_MUTEX_NREF
17042
 
  int id;                    /* Mutex type */
17043
 
  volatile int nRef;         /* Number of entrances */
17044
 
  volatile pthread_t owner;  /* Thread that is within this mutex */
17045
 
  int trace;                 /* True to trace changes */
17046
 
#endif
17047
 
};
17048
 
#if SQLITE_MUTEX_NREF
17049
 
#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
17050
 
#else
17051
 
#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
17052
 
#endif
17053
 
 
17054
 
/*
17055
 
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
17056
 
** intended for use only inside assert() statements.  On some platforms,
17057
 
** there might be race conditions that can cause these routines to
17058
 
** deliver incorrect results.  In particular, if pthread_equal() is
17059
 
** not an atomic operation, then these routines might delivery
17060
 
** incorrect results.  On most platforms, pthread_equal() is a 
17061
 
** comparison of two integers and is therefore atomic.  But we are
17062
 
** told that HPUX is not such a platform.  If so, then these routines
17063
 
** will not always work correctly on HPUX.
17064
 
**
17065
 
** On those platforms where pthread_equal() is not atomic, SQLite
17066
 
** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
17067
 
** make sure no assert() statements are evaluated and hence these
17068
 
** routines are never called.
17069
 
*/
17070
 
#if !defined(NDEBUG) || defined(SQLITE_DEBUG)
17071
 
static int pthreadMutexHeld(sqlite3_mutex *p){
17072
 
  return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
17073
 
}
17074
 
static int pthreadMutexNotheld(sqlite3_mutex *p){
17075
 
  return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
17076
 
}
17077
 
#endif
17078
 
 
17079
 
/*
17080
 
** Initialize and deinitialize the mutex subsystem.
17081
 
*/
17082
 
static int pthreadMutexInit(void){ return SQLITE_OK; }
17083
 
static int pthreadMutexEnd(void){ return SQLITE_OK; }
17084
 
 
17085
 
/*
17086
 
** The sqlite3_mutex_alloc() routine allocates a new
17087
 
** mutex and returns a pointer to it.  If it returns NULL
17088
 
** that means that a mutex could not be allocated.  SQLite
17089
 
** will unwind its stack and return an error.  The argument
17090
 
** to sqlite3_mutex_alloc() is one of these integer constants:
17091
 
**
17092
 
** <ul>
17093
 
** <li>  SQLITE_MUTEX_FAST
17094
 
** <li>  SQLITE_MUTEX_RECURSIVE
17095
 
** <li>  SQLITE_MUTEX_STATIC_MASTER
17096
 
** <li>  SQLITE_MUTEX_STATIC_MEM
17097
 
** <li>  SQLITE_MUTEX_STATIC_MEM2
17098
 
** <li>  SQLITE_MUTEX_STATIC_PRNG
17099
 
** <li>  SQLITE_MUTEX_STATIC_LRU
17100
 
** <li>  SQLITE_MUTEX_STATIC_PMEM
17101
 
** </ul>
17102
 
**
17103
 
** The first two constants cause sqlite3_mutex_alloc() to create
17104
 
** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
17105
 
** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
17106
 
** The mutex implementation does not need to make a distinction
17107
 
** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
17108
 
** not want to.  But SQLite will only request a recursive mutex in
17109
 
** cases where it really needs one.  If a faster non-recursive mutex
17110
 
** implementation is available on the host platform, the mutex subsystem
17111
 
** might return such a mutex in response to SQLITE_MUTEX_FAST.
17112
 
**
17113
 
** The other allowed parameters to sqlite3_mutex_alloc() each return
17114
 
** a pointer to a static preexisting mutex.  Six static mutexes are
17115
 
** used by the current version of SQLite.  Future versions of SQLite
17116
 
** may add additional static mutexes.  Static mutexes are for internal
17117
 
** use by SQLite only.  Applications that use SQLite mutexes should
17118
 
** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
17119
 
** SQLITE_MUTEX_RECURSIVE.
17120
 
**
17121
 
** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
17122
 
** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
17123
 
** returns a different mutex on every call.  But for the static 
17124
 
** mutex types, the same mutex is returned on every call that has
17125
 
** the same type number.
17126
 
*/
17127
 
static sqlite3_mutex *pthreadMutexAlloc(int iType){
17128
 
  static sqlite3_mutex staticMutexes[] = {
17129
 
    SQLITE3_MUTEX_INITIALIZER,
17130
 
    SQLITE3_MUTEX_INITIALIZER,
17131
 
    SQLITE3_MUTEX_INITIALIZER,
17132
 
    SQLITE3_MUTEX_INITIALIZER,
17133
 
    SQLITE3_MUTEX_INITIALIZER,
17134
 
    SQLITE3_MUTEX_INITIALIZER
17135
 
  };
17136
 
  sqlite3_mutex *p;
17137
 
  switch( iType ){
17138
 
    case SQLITE_MUTEX_RECURSIVE: {
17139
 
      p = sqlite3MallocZero( sizeof(*p) );
17140
 
      if( p ){
17141
 
#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
17142
 
        /* If recursive mutexes are not available, we will have to
17143
 
        ** build our own.  See below. */
17144
 
        pthread_mutex_init(&p->mutex, 0);
17145
 
#else
17146
 
        /* Use a recursive mutex if it is available */
17147
 
        pthread_mutexattr_t recursiveAttr;
17148
 
        pthread_mutexattr_init(&recursiveAttr);
17149
 
        pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
17150
 
        pthread_mutex_init(&p->mutex, &recursiveAttr);
17151
 
        pthread_mutexattr_destroy(&recursiveAttr);
17152
 
#endif
17153
 
#if SQLITE_MUTEX_NREF
17154
 
        p->id = iType;
17155
 
#endif
17156
 
      }
17157
 
      break;
17158
 
    }
17159
 
    case SQLITE_MUTEX_FAST: {
17160
 
      p = sqlite3MallocZero( sizeof(*p) );
17161
 
      if( p ){
17162
 
#if SQLITE_MUTEX_NREF
17163
 
        p->id = iType;
17164
 
#endif
17165
 
        pthread_mutex_init(&p->mutex, 0);
17166
 
      }
17167
 
      break;
17168
 
    }
17169
 
    default: {
17170
 
      assert( iType-2 >= 0 );
17171
 
      assert( iType-2 < ArraySize(staticMutexes) );
17172
 
      p = &staticMutexes[iType-2];
17173
 
#if SQLITE_MUTEX_NREF
17174
 
      p->id = iType;
17175
 
#endif
17176
 
      break;
17177
 
    }
17178
 
  }
17179
 
  return p;
17180
 
}
17181
 
 
17182
 
 
17183
 
/*
17184
 
** This routine deallocates a previously
17185
 
** allocated mutex.  SQLite is careful to deallocate every
17186
 
** mutex that it allocates.
17187
 
*/
17188
 
static void pthreadMutexFree(sqlite3_mutex *p){
17189
 
  assert( p->nRef==0 );
17190
 
  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
17191
 
  pthread_mutex_destroy(&p->mutex);
17192
 
  sqlite3_free(p);
17193
 
}
17194
 
 
17195
 
/*
17196
 
** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
17197
 
** to enter a mutex.  If another thread is already within the mutex,
17198
 
** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
17199
 
** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
17200
 
** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
17201
 
** be entered multiple times by the same thread.  In such cases the,
17202
 
** mutex must be exited an equal number of times before another thread
17203
 
** can enter.  If the same thread tries to enter any other kind of mutex
17204
 
** more than once, the behavior is undefined.
17205
 
*/
17206
 
static void pthreadMutexEnter(sqlite3_mutex *p){
17207
 
  assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
17208
 
 
17209
 
#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
17210
 
  /* If recursive mutexes are not available, then we have to grow
17211
 
  ** our own.  This implementation assumes that pthread_equal()
17212
 
  ** is atomic - that it cannot be deceived into thinking self
17213
 
  ** and p->owner are equal if p->owner changes between two values
17214
 
  ** that are not equal to self while the comparison is taking place.
17215
 
  ** This implementation also assumes a coherent cache - that 
17216
 
  ** separate processes cannot read different values from the same
17217
 
  ** address at the same time.  If either of these two conditions
17218
 
  ** are not met, then the mutexes will fail and problems will result.
17219
 
  */
17220
 
  {
17221
 
    pthread_t self = pthread_self();
17222
 
    if( p->nRef>0 && pthread_equal(p->owner, self) ){
17223
 
      p->nRef++;
17224
 
    }else{
17225
 
      pthread_mutex_lock(&p->mutex);
17226
 
      assert( p->nRef==0 );
17227
 
      p->owner = self;
17228
 
      p->nRef = 1;
17229
 
    }
17230
 
  }
17231
 
#else
17232
 
  /* Use the built-in recursive mutexes if they are available.
17233
 
  */
17234
 
  pthread_mutex_lock(&p->mutex);
17235
 
#if SQLITE_MUTEX_NREF
17236
 
  assert( p->nRef>0 || p->owner==0 );
17237
 
  p->owner = pthread_self();
17238
 
  p->nRef++;
17239
 
#endif
17240
 
#endif
17241
 
 
17242
 
#ifdef SQLITE_DEBUG
17243
 
  if( p->trace ){
17244
 
    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17245
 
  }
17246
 
#endif
17247
 
}
17248
 
static int pthreadMutexTry(sqlite3_mutex *p){
17249
 
  int rc;
17250
 
  assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
17251
 
 
17252
 
#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
17253
 
  /* If recursive mutexes are not available, then we have to grow
17254
 
  ** our own.  This implementation assumes that pthread_equal()
17255
 
  ** is atomic - that it cannot be deceived into thinking self
17256
 
  ** and p->owner are equal if p->owner changes between two values
17257
 
  ** that are not equal to self while the comparison is taking place.
17258
 
  ** This implementation also assumes a coherent cache - that 
17259
 
  ** separate processes cannot read different values from the same
17260
 
  ** address at the same time.  If either of these two conditions
17261
 
  ** are not met, then the mutexes will fail and problems will result.
17262
 
  */
17263
 
  {
17264
 
    pthread_t self = pthread_self();
17265
 
    if( p->nRef>0 && pthread_equal(p->owner, self) ){
17266
 
      p->nRef++;
17267
 
      rc = SQLITE_OK;
17268
 
    }else if( pthread_mutex_trylock(&p->mutex)==0 ){
17269
 
      assert( p->nRef==0 );
17270
 
      p->owner = self;
17271
 
      p->nRef = 1;
17272
 
      rc = SQLITE_OK;
17273
 
    }else{
17274
 
      rc = SQLITE_BUSY;
17275
 
    }
17276
 
  }
17277
 
#else
17278
 
  /* Use the built-in recursive mutexes if they are available.
17279
 
  */
17280
 
  if( pthread_mutex_trylock(&p->mutex)==0 ){
17281
 
#if SQLITE_MUTEX_NREF
17282
 
    p->owner = pthread_self();
17283
 
    p->nRef++;
17284
 
#endif
17285
 
    rc = SQLITE_OK;
17286
 
  }else{
17287
 
    rc = SQLITE_BUSY;
17288
 
  }
17289
 
#endif
17290
 
 
17291
 
#ifdef SQLITE_DEBUG
17292
 
  if( rc==SQLITE_OK && p->trace ){
17293
 
    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17294
 
  }
17295
 
#endif
17296
 
  return rc;
17297
 
}
17298
 
 
17299
 
/*
17300
 
** The sqlite3_mutex_leave() routine exits a mutex that was
17301
 
** previously entered by the same thread.  The behavior
17302
 
** is undefined if the mutex is not currently entered or
17303
 
** is not currently allocated.  SQLite will never do either.
17304
 
*/
17305
 
static void pthreadMutexLeave(sqlite3_mutex *p){
17306
 
  assert( pthreadMutexHeld(p) );
17307
 
#if SQLITE_MUTEX_NREF
17308
 
  p->nRef--;
17309
 
  if( p->nRef==0 ) p->owner = 0;
17310
 
#endif
17311
 
  assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
17312
 
 
17313
 
#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
17314
 
  if( p->nRef==0 ){
17315
 
    pthread_mutex_unlock(&p->mutex);
17316
 
  }
17317
 
#else
17318
 
  pthread_mutex_unlock(&p->mutex);
17319
 
#endif
17320
 
 
17321
 
#ifdef SQLITE_DEBUG
17322
 
  if( p->trace ){
17323
 
    printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17324
 
  }
17325
 
#endif
17326
 
}
17327
 
 
17328
 
SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
17329
 
  static const sqlite3_mutex_methods sMutex = {
17330
 
    pthreadMutexInit,
17331
 
    pthreadMutexEnd,
17332
 
    pthreadMutexAlloc,
17333
 
    pthreadMutexFree,
17334
 
    pthreadMutexEnter,
17335
 
    pthreadMutexTry,
17336
 
    pthreadMutexLeave,
17337
 
#ifdef SQLITE_DEBUG
17338
 
    pthreadMutexHeld,
17339
 
    pthreadMutexNotheld
17340
 
#else
17341
 
    0,
17342
 
    0
17343
 
#endif
17344
 
  };
17345
 
 
17346
 
  return &sMutex;
17347
 
}
17348
 
 
17349
 
#endif /* SQLITE_MUTEX_PTHREAD */
17350
 
 
17351
 
/************** End of mutex_unix.c ******************************************/
17352
 
/************** Begin file mutex_w32.c ***************************************/
17353
 
/*
17354
 
** 2007 August 14
17355
 
**
17356
 
** The author disclaims copyright to this source code.  In place of
17357
 
** a legal notice, here is a blessing:
17358
 
**
17359
 
**    May you do good and not evil.
17360
 
**    May you find forgiveness for yourself and forgive others.
17361
 
**    May you share freely, never taking more than you give.
17362
 
**
17363
 
*************************************************************************
17364
 
** This file contains the C functions that implement mutexes for win32
17365
 
*/
17366
 
 
17367
 
/*
17368
 
** The code in this file is only used if we are compiling multithreaded
17369
 
** on a win32 system.
17370
 
*/
17371
 
#ifdef SQLITE_MUTEX_W32
17372
 
 
17373
 
/*
17374
 
** Each recursive mutex is an instance of the following structure.
17375
 
*/
17376
 
struct sqlite3_mutex {
17377
 
  CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
17378
 
  int id;                    /* Mutex type */
17379
 
#ifdef SQLITE_DEBUG
17380
 
  volatile int nRef;         /* Number of enterances */
17381
 
  volatile DWORD owner;      /* Thread holding this mutex */
17382
 
  int trace;                 /* True to trace changes */
17383
 
#endif
17384
 
};
17385
 
#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
17386
 
#ifdef SQLITE_DEBUG
17387
 
#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
17388
 
#else
17389
 
#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
17390
 
#endif
17391
 
 
17392
 
/*
17393
 
** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
17394
 
** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
17395
 
**
17396
 
** Here is an interesting observation:  Win95, Win98, and WinME lack
17397
 
** the LockFileEx() API.  But we can still statically link against that
17398
 
** API as long as we don't call it win running Win95/98/ME.  A call to
17399
 
** this routine is used to determine if the host is Win95/98/ME or
17400
 
** WinNT/2K/XP so that we will know whether or not we can safely call
17401
 
** the LockFileEx() API.
17402
 
**
17403
 
** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
17404
 
** which is only available if your application was compiled with 
17405
 
** _WIN32_WINNT defined to a value >= 0x0400.  Currently, the only
17406
 
** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef 
17407
 
** this out as well.
17408
 
*/
17409
 
#if 0
17410
 
#if SQLITE_OS_WINCE
17411
 
# define mutexIsNT()  (1)
17412
 
#else
17413
 
  static int mutexIsNT(void){
17414
 
    static int osType = 0;
17415
 
    if( osType==0 ){
17416
 
      OSVERSIONINFO sInfo;
17417
 
      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
17418
 
      GetVersionEx(&sInfo);
17419
 
      osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
17420
 
    }
17421
 
    return osType==2;
17422
 
  }
17423
 
#endif /* SQLITE_OS_WINCE */
17424
 
#endif
17425
 
 
17426
 
#ifdef SQLITE_DEBUG
17427
 
/*
17428
 
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
17429
 
** intended for use only inside assert() statements.
17430
 
*/
17431
 
static int winMutexHeld(sqlite3_mutex *p){
17432
 
  return p->nRef!=0 && p->owner==GetCurrentThreadId();
17433
 
}
17434
 
static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
17435
 
  return p->nRef==0 || p->owner!=tid;
17436
 
}
17437
 
static int winMutexNotheld(sqlite3_mutex *p){
17438
 
  DWORD tid = GetCurrentThreadId(); 
17439
 
  return winMutexNotheld2(p, tid);
17440
 
}
17441
 
#endif
17442
 
 
17443
 
 
17444
 
/*
17445
 
** Initialize and deinitialize the mutex subsystem.
17446
 
*/
17447
 
static sqlite3_mutex winMutex_staticMutexes[6] = {
17448
 
  SQLITE3_MUTEX_INITIALIZER,
17449
 
  SQLITE3_MUTEX_INITIALIZER,
17450
 
  SQLITE3_MUTEX_INITIALIZER,
17451
 
  SQLITE3_MUTEX_INITIALIZER,
17452
 
  SQLITE3_MUTEX_INITIALIZER,
17453
 
  SQLITE3_MUTEX_INITIALIZER
17454
 
};
17455
 
static int winMutex_isInit = 0;
17456
 
/* As winMutexInit() and winMutexEnd() are called as part
17457
 
** of the sqlite3_initialize and sqlite3_shutdown()
17458
 
** processing, the "interlocked" magic is probably not
17459
 
** strictly necessary.
17460
 
*/
17461
 
static long winMutex_lock = 0;
17462
 
 
17463
 
static int winMutexInit(void){ 
17464
 
  /* The first to increment to 1 does actual initialization */
17465
 
  if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
17466
 
    int i;
17467
 
    for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
17468
 
      InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
17469
 
    }
17470
 
    winMutex_isInit = 1;
17471
 
  }else{
17472
 
    /* Someone else is in the process of initing the static mutexes */
17473
 
    while( !winMutex_isInit ){
17474
 
      Sleep(1);
17475
 
    }
17476
 
  }
17477
 
  return SQLITE_OK; 
17478
 
}
17479
 
 
17480
 
static int winMutexEnd(void){ 
17481
 
  /* The first to decrement to 0 does actual shutdown 
17482
 
  ** (which should be the last to shutdown.) */
17483
 
  if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
17484
 
    if( winMutex_isInit==1 ){
17485
 
      int i;
17486
 
      for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
17487
 
        DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
17488
 
      }
17489
 
      winMutex_isInit = 0;
17490
 
    }
17491
 
  }
17492
 
  return SQLITE_OK; 
17493
 
}
17494
 
 
17495
 
/*
17496
 
** The sqlite3_mutex_alloc() routine allocates a new
17497
 
** mutex and returns a pointer to it.  If it returns NULL
17498
 
** that means that a mutex could not be allocated.  SQLite
17499
 
** will unwind its stack and return an error.  The argument
17500
 
** to sqlite3_mutex_alloc() is one of these integer constants:
17501
 
**
17502
 
** <ul>
17503
 
** <li>  SQLITE_MUTEX_FAST
17504
 
** <li>  SQLITE_MUTEX_RECURSIVE
17505
 
** <li>  SQLITE_MUTEX_STATIC_MASTER
17506
 
** <li>  SQLITE_MUTEX_STATIC_MEM
17507
 
** <li>  SQLITE_MUTEX_STATIC_MEM2
17508
 
** <li>  SQLITE_MUTEX_STATIC_PRNG
17509
 
** <li>  SQLITE_MUTEX_STATIC_LRU
17510
 
** <li>  SQLITE_MUTEX_STATIC_PMEM
17511
 
** </ul>
17512
 
**
17513
 
** The first two constants cause sqlite3_mutex_alloc() to create
17514
 
** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
17515
 
** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
17516
 
** The mutex implementation does not need to make a distinction
17517
 
** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
17518
 
** not want to.  But SQLite will only request a recursive mutex in
17519
 
** cases where it really needs one.  If a faster non-recursive mutex
17520
 
** implementation is available on the host platform, the mutex subsystem
17521
 
** might return such a mutex in response to SQLITE_MUTEX_FAST.
17522
 
**
17523
 
** The other allowed parameters to sqlite3_mutex_alloc() each return
17524
 
** a pointer to a static preexisting mutex.  Six static mutexes are
17525
 
** used by the current version of SQLite.  Future versions of SQLite
17526
 
** may add additional static mutexes.  Static mutexes are for internal
17527
 
** use by SQLite only.  Applications that use SQLite mutexes should
17528
 
** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
17529
 
** SQLITE_MUTEX_RECURSIVE.
17530
 
**
17531
 
** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
17532
 
** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
17533
 
** returns a different mutex on every call.  But for the static 
17534
 
** mutex types, the same mutex is returned on every call that has
17535
 
** the same type number.
17536
 
*/
17537
 
static sqlite3_mutex *winMutexAlloc(int iType){
17538
 
  sqlite3_mutex *p;
17539
 
 
17540
 
  switch( iType ){
17541
 
    case SQLITE_MUTEX_FAST:
17542
 
    case SQLITE_MUTEX_RECURSIVE: {
17543
 
      p = sqlite3MallocZero( sizeof(*p) );
17544
 
      if( p ){  
17545
 
#ifdef SQLITE_DEBUG
17546
 
        p->id = iType;
17547
 
#endif
17548
 
        InitializeCriticalSection(&p->mutex);
17549
 
      }
17550
 
      break;
17551
 
    }
17552
 
    default: {
17553
 
      assert( winMutex_isInit==1 );
17554
 
      assert( iType-2 >= 0 );
17555
 
      assert( iType-2 < ArraySize(winMutex_staticMutexes) );
17556
 
      p = &winMutex_staticMutexes[iType-2];
17557
 
#ifdef SQLITE_DEBUG
17558
 
      p->id = iType;
17559
 
#endif
17560
 
      break;
17561
 
    }
17562
 
  }
17563
 
  return p;
17564
 
}
17565
 
 
17566
 
 
17567
 
/*
17568
 
** This routine deallocates a previously
17569
 
** allocated mutex.  SQLite is careful to deallocate every
17570
 
** mutex that it allocates.
17571
 
*/
17572
 
static void winMutexFree(sqlite3_mutex *p){
17573
 
  assert( p );
17574
 
  assert( p->nRef==0 && p->owner==0 );
17575
 
  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
17576
 
  DeleteCriticalSection(&p->mutex);
17577
 
  sqlite3_free(p);
17578
 
}
17579
 
 
17580
 
/*
17581
 
** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
17582
 
** to enter a mutex.  If another thread is already within the mutex,
17583
 
** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
17584
 
** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
17585
 
** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
17586
 
** be entered multiple times by the same thread.  In such cases the,
17587
 
** mutex must be exited an equal number of times before another thread
17588
 
** can enter.  If the same thread tries to enter any other kind of mutex
17589
 
** more than once, the behavior is undefined.
17590
 
*/
17591
 
static void winMutexEnter(sqlite3_mutex *p){
17592
 
#ifdef SQLITE_DEBUG
17593
 
  DWORD tid = GetCurrentThreadId(); 
17594
 
  assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
17595
 
#endif
17596
 
  EnterCriticalSection(&p->mutex);
17597
 
#ifdef SQLITE_DEBUG
17598
 
  assert( p->nRef>0 || p->owner==0 );
17599
 
  p->owner = tid; 
17600
 
  p->nRef++;
17601
 
  if( p->trace ){
17602
 
    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17603
 
  }
17604
 
#endif
17605
 
}
17606
 
static int winMutexTry(sqlite3_mutex *p){
17607
 
#ifndef NDEBUG
17608
 
  DWORD tid = GetCurrentThreadId(); 
17609
 
#endif
17610
 
  int rc = SQLITE_BUSY;
17611
 
  assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
17612
 
  /*
17613
 
  ** The sqlite3_mutex_try() routine is very rarely used, and when it
17614
 
  ** is used it is merely an optimization.  So it is OK for it to always
17615
 
  ** fail.  
17616
 
  **
17617
 
  ** The TryEnterCriticalSection() interface is only available on WinNT.
17618
 
  ** And some windows compilers complain if you try to use it without
17619
 
  ** first doing some #defines that prevent SQLite from building on Win98.
17620
 
  ** For that reason, we will omit this optimization for now.  See
17621
 
  ** ticket #2685.
17622
 
  */
17623
 
#if 0
17624
 
  if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
17625
 
    p->owner = tid;
17626
 
    p->nRef++;
17627
 
    rc = SQLITE_OK;
17628
 
  }
17629
 
#else
17630
 
  UNUSED_PARAMETER(p);
17631
 
#endif
17632
 
#ifdef SQLITE_DEBUG
17633
 
  if( rc==SQLITE_OK && p->trace ){
17634
 
    printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17635
 
  }
17636
 
#endif
17637
 
  return rc;
17638
 
}
17639
 
 
17640
 
/*
17641
 
** The sqlite3_mutex_leave() routine exits a mutex that was
17642
 
** previously entered by the same thread.  The behavior
17643
 
** is undefined if the mutex is not currently entered or
17644
 
** is not currently allocated.  SQLite will never do either.
17645
 
*/
17646
 
static void winMutexLeave(sqlite3_mutex *p){
17647
 
#ifndef NDEBUG
17648
 
  DWORD tid = GetCurrentThreadId();
17649
 
  assert( p->nRef>0 );
17650
 
  assert( p->owner==tid );
17651
 
  p->nRef--;
17652
 
  if( p->nRef==0 ) p->owner = 0;
17653
 
  assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
17654
 
#endif
17655
 
  LeaveCriticalSection(&p->mutex);
17656
 
#ifdef SQLITE_DEBUG
17657
 
  if( p->trace ){
17658
 
    printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17659
 
  }
17660
 
#endif
17661
 
}
17662
 
 
17663
 
SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
17664
 
  static const sqlite3_mutex_methods sMutex = {
17665
 
    winMutexInit,
17666
 
    winMutexEnd,
17667
 
    winMutexAlloc,
17668
 
    winMutexFree,
17669
 
    winMutexEnter,
17670
 
    winMutexTry,
17671
 
    winMutexLeave,
17672
 
#ifdef SQLITE_DEBUG
17673
 
    winMutexHeld,
17674
 
    winMutexNotheld
17675
 
#else
17676
 
    0,
17677
 
    0
17678
 
#endif
17679
 
  };
17680
 
 
17681
 
  return &sMutex;
17682
 
}
17683
 
#endif /* SQLITE_MUTEX_W32 */
17684
 
 
17685
 
/************** End of mutex_w32.c *******************************************/
17686
 
/************** Begin file malloc.c ******************************************/
17687
 
/*
17688
 
** 2001 September 15
17689
 
**
17690
 
** The author disclaims copyright to this source code.  In place of
17691
 
** a legal notice, here is a blessing:
17692
 
**
17693
 
**    May you do good and not evil.
17694
 
**    May you find forgiveness for yourself and forgive others.
17695
 
**    May you share freely, never taking more than you give.
17696
 
**
17697
 
*************************************************************************
17698
 
**
17699
 
** Memory allocation functions used throughout sqlite.
17700
 
*/
17701
 
 
17702
 
/*
17703
 
** Attempt to release up to n bytes of non-essential memory currently
17704
 
** held by SQLite. An example of non-essential memory is memory used to
17705
 
** cache database pages that are not currently in use.
17706
 
*/
17707
 
SQLITE_API int sqlite3_release_memory(int n){
17708
 
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
17709
 
  return sqlite3PcacheReleaseMemory(n);
17710
 
#else
17711
 
  /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
17712
 
  ** is a no-op returning zero if SQLite is not compiled with
17713
 
  ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
17714
 
  UNUSED_PARAMETER(n);
17715
 
  return 0;
17716
 
#endif
17717
 
}
17718
 
 
17719
 
/*
17720
 
** An instance of the following object records the location of
17721
 
** each unused scratch buffer.
17722
 
*/
17723
 
typedef struct ScratchFreeslot {
17724
 
  struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
17725
 
} ScratchFreeslot;
17726
 
 
17727
 
/*
17728
 
** State information local to the memory allocation subsystem.
17729
 
*/
17730
 
static SQLITE_WSD struct Mem0Global {
17731
 
  sqlite3_mutex *mutex;         /* Mutex to serialize access */
17732
 
 
17733
 
  /*
17734
 
  ** The alarm callback and its arguments.  The mem0.mutex lock will
17735
 
  ** be held while the callback is running.  Recursive calls into
17736
 
  ** the memory subsystem are allowed, but no new callbacks will be
17737
 
  ** issued.
17738
 
  */
17739
 
  sqlite3_int64 alarmThreshold;
17740
 
  void (*alarmCallback)(void*, sqlite3_int64,int);
17741
 
  void *alarmArg;
17742
 
 
17743
 
  /*
17744
 
  ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
17745
 
  ** (so that a range test can be used to determine if an allocation
17746
 
  ** being freed came from pScratch) and a pointer to the list of
17747
 
  ** unused scratch allocations.
17748
 
  */
17749
 
  void *pScratchEnd;
17750
 
  ScratchFreeslot *pScratchFree;
17751
 
  u32 nScratchFree;
17752
 
 
17753
 
  /*
17754
 
  ** True if heap is nearly "full" where "full" is defined by the
17755
 
  ** sqlite3_soft_heap_limit() setting.
17756
 
  */
17757
 
  int nearlyFull;
17758
 
} mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
17759
 
 
17760
 
#define mem0 GLOBAL(struct Mem0Global, mem0)
17761
 
 
17762
 
/*
17763
 
** This routine runs when the memory allocator sees that the
17764
 
** total memory allocation is about to exceed the soft heap
17765
 
** limit.
17766
 
*/
17767
 
static void softHeapLimitEnforcer(
17768
 
  void *NotUsed, 
17769
 
  sqlite3_int64 NotUsed2,
17770
 
  int allocSize
17771
 
){
17772
 
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
17773
 
  sqlite3_release_memory(allocSize);
17774
 
}
17775
 
 
17776
 
/*
17777
 
** Change the alarm callback
17778
 
*/
17779
 
static int sqlite3MemoryAlarm(
17780
 
  void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
17781
 
  void *pArg,
17782
 
  sqlite3_int64 iThreshold
17783
 
){
17784
 
  int nUsed;
17785
 
  sqlite3_mutex_enter(mem0.mutex);
17786
 
  mem0.alarmCallback = xCallback;
17787
 
  mem0.alarmArg = pArg;
17788
 
  mem0.alarmThreshold = iThreshold;
17789
 
  nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
17790
 
  mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
17791
 
  sqlite3_mutex_leave(mem0.mutex);
17792
 
  return SQLITE_OK;
17793
 
}
17794
 
 
17795
 
#ifndef SQLITE_OMIT_DEPRECATED
17796
 
/*
17797
 
** Deprecated external interface.  Internal/core SQLite code
17798
 
** should call sqlite3MemoryAlarm.
17799
 
*/
17800
 
SQLITE_API int sqlite3_memory_alarm(
17801
 
  void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
17802
 
  void *pArg,
17803
 
  sqlite3_int64 iThreshold
17804
 
){
17805
 
  return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
17806
 
}
17807
 
#endif
17808
 
 
17809
 
/*
17810
 
** Set the soft heap-size limit for the library. Passing a zero or 
17811
 
** negative value indicates no limit.
17812
 
*/
17813
 
SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
17814
 
  sqlite3_int64 priorLimit;
17815
 
  sqlite3_int64 excess;
17816
 
#ifndef SQLITE_OMIT_AUTOINIT
17817
 
  sqlite3_initialize();
17818
 
#endif
17819
 
  sqlite3_mutex_enter(mem0.mutex);
17820
 
  priorLimit = mem0.alarmThreshold;
17821
 
  sqlite3_mutex_leave(mem0.mutex);
17822
 
  if( n<0 ) return priorLimit;
17823
 
  if( n>0 ){
17824
 
    sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
17825
 
  }else{
17826
 
    sqlite3MemoryAlarm(0, 0, 0);
17827
 
  }
17828
 
  excess = sqlite3_memory_used() - n;
17829
 
  if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
17830
 
  return priorLimit;
17831
 
}
17832
 
SQLITE_API void sqlite3_soft_heap_limit(int n){
17833
 
  if( n<0 ) n = 0;
17834
 
  sqlite3_soft_heap_limit64(n);
17835
 
}
17836
 
 
17837
 
/*
17838
 
** Initialize the memory allocation subsystem.
17839
 
*/
17840
 
SQLITE_PRIVATE int sqlite3MallocInit(void){
17841
 
  if( sqlite3GlobalConfig.m.xMalloc==0 ){
17842
 
    sqlite3MemSetDefault();
17843
 
  }
17844
 
  memset(&mem0, 0, sizeof(mem0));
17845
 
  if( sqlite3GlobalConfig.bCoreMutex ){
17846
 
    mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
17847
 
  }
17848
 
  if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
17849
 
      && sqlite3GlobalConfig.nScratch>0 ){
17850
 
    int i, n, sz;
17851
 
    ScratchFreeslot *pSlot;
17852
 
    sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
17853
 
    sqlite3GlobalConfig.szScratch = sz;
17854
 
    pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
17855
 
    n = sqlite3GlobalConfig.nScratch;
17856
 
    mem0.pScratchFree = pSlot;
17857
 
    mem0.nScratchFree = n;
17858
 
    for(i=0; i<n-1; i++){
17859
 
      pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
17860
 
      pSlot = pSlot->pNext;
17861
 
    }
17862
 
    pSlot->pNext = 0;
17863
 
    mem0.pScratchEnd = (void*)&pSlot[1];
17864
 
  }else{
17865
 
    mem0.pScratchEnd = 0;
17866
 
    sqlite3GlobalConfig.pScratch = 0;
17867
 
    sqlite3GlobalConfig.szScratch = 0;
17868
 
    sqlite3GlobalConfig.nScratch = 0;
17869
 
  }
17870
 
  if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
17871
 
      || sqlite3GlobalConfig.nPage<1 ){
17872
 
    sqlite3GlobalConfig.pPage = 0;
17873
 
    sqlite3GlobalConfig.szPage = 0;
17874
 
    sqlite3GlobalConfig.nPage = 0;
17875
 
  }
17876
 
  return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
17877
 
}
17878
 
 
17879
 
/*
17880
 
** Return true if the heap is currently under memory pressure - in other
17881
 
** words if the amount of heap used is close to the limit set by
17882
 
** sqlite3_soft_heap_limit().
17883
 
*/
17884
 
SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
17885
 
  return mem0.nearlyFull;
17886
 
}
17887
 
 
17888
 
/*
17889
 
** Deinitialize the memory allocation subsystem.
17890
 
*/
17891
 
SQLITE_PRIVATE void sqlite3MallocEnd(void){
17892
 
  if( sqlite3GlobalConfig.m.xShutdown ){
17893
 
    sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
17894
 
  }
17895
 
  memset(&mem0, 0, sizeof(mem0));
17896
 
}
17897
 
 
17898
 
/*
17899
 
** Return the amount of memory currently checked out.
17900
 
*/
17901
 
SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
17902
 
  int n, mx;
17903
 
  sqlite3_int64 res;
17904
 
  sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
17905
 
  res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
17906
 
  return res;
17907
 
}
17908
 
 
17909
 
/*
17910
 
** Return the maximum amount of memory that has ever been
17911
 
** checked out since either the beginning of this process
17912
 
** or since the most recent reset.
17913
 
*/
17914
 
SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
17915
 
  int n, mx;
17916
 
  sqlite3_int64 res;
17917
 
  sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
17918
 
  res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
17919
 
  return res;
17920
 
}
17921
 
 
17922
 
/*
17923
 
** Trigger the alarm 
17924
 
*/
17925
 
static void sqlite3MallocAlarm(int nByte){
17926
 
  void (*xCallback)(void*,sqlite3_int64,int);
17927
 
  sqlite3_int64 nowUsed;
17928
 
  void *pArg;
17929
 
  if( mem0.alarmCallback==0 ) return;
17930
 
  xCallback = mem0.alarmCallback;
17931
 
  nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
17932
 
  pArg = mem0.alarmArg;
17933
 
  mem0.alarmCallback = 0;
17934
 
  sqlite3_mutex_leave(mem0.mutex);
17935
 
  xCallback(pArg, nowUsed, nByte);
17936
 
  sqlite3_mutex_enter(mem0.mutex);
17937
 
  mem0.alarmCallback = xCallback;
17938
 
  mem0.alarmArg = pArg;
17939
 
}
17940
 
 
17941
 
/*
17942
 
** Do a memory allocation with statistics and alarms.  Assume the
17943
 
** lock is already held.
17944
 
*/
17945
 
static int mallocWithAlarm(int n, void **pp){
17946
 
  int nFull;
17947
 
  void *p;
17948
 
  assert( sqlite3_mutex_held(mem0.mutex) );
17949
 
  nFull = sqlite3GlobalConfig.m.xRoundup(n);
17950
 
  sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
17951
 
  if( mem0.alarmCallback!=0 ){
17952
 
    int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
17953
 
    if( nUsed+nFull >= mem0.alarmThreshold ){
17954
 
      mem0.nearlyFull = 1;
17955
 
      sqlite3MallocAlarm(nFull);
17956
 
    }else{
17957
 
      mem0.nearlyFull = 0;
17958
 
    }
17959
 
  }
17960
 
  p = sqlite3GlobalConfig.m.xMalloc(nFull);
17961
 
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
17962
 
  if( p==0 && mem0.alarmCallback ){
17963
 
    sqlite3MallocAlarm(nFull);
17964
 
    p = sqlite3GlobalConfig.m.xMalloc(nFull);
17965
 
  }
17966
 
#endif
17967
 
  if( p ){
17968
 
    nFull = sqlite3MallocSize(p);
17969
 
    sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
17970
 
    sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
17971
 
  }
17972
 
  *pp = p;
17973
 
  return nFull;
17974
 
}
17975
 
 
17976
 
/*
17977
 
** Allocate memory.  This routine is like sqlite3_malloc() except that it
17978
 
** assumes the memory subsystem has already been initialized.
17979
 
*/
17980
 
SQLITE_PRIVATE void *sqlite3Malloc(int n){
17981
 
  void *p;
17982
 
  if( n<=0               /* IMP: R-65312-04917 */ 
17983
 
   || n>=0x7fffff00
17984
 
  ){
17985
 
    /* A memory allocation of a number of bytes which is near the maximum
17986
 
    ** signed integer value might cause an integer overflow inside of the
17987
 
    ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
17988
 
    ** 255 bytes of overhead.  SQLite itself will never use anything near
17989
 
    ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
17990
 
    p = 0;
17991
 
  }else if( sqlite3GlobalConfig.bMemstat ){
17992
 
    sqlite3_mutex_enter(mem0.mutex);
17993
 
    mallocWithAlarm(n, &p);
17994
 
    sqlite3_mutex_leave(mem0.mutex);
17995
 
  }else{
17996
 
    p = sqlite3GlobalConfig.m.xMalloc(n);
17997
 
  }
17998
 
  assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
17999
 
  return p;
18000
 
}
18001
 
 
18002
 
/*
18003
 
** This version of the memory allocation is for use by the application.
18004
 
** First make sure the memory subsystem is initialized, then do the
18005
 
** allocation.
18006
 
*/
18007
 
SQLITE_API void *sqlite3_malloc(int n){
18008
 
#ifndef SQLITE_OMIT_AUTOINIT
18009
 
  if( sqlite3_initialize() ) return 0;
18010
 
#endif
18011
 
  return sqlite3Malloc(n);
18012
 
}
18013
 
 
18014
 
/*
18015
 
** Each thread may only have a single outstanding allocation from
18016
 
** xScratchMalloc().  We verify this constraint in the single-threaded
18017
 
** case by setting scratchAllocOut to 1 when an allocation
18018
 
** is outstanding clearing it when the allocation is freed.
18019
 
*/
18020
 
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
18021
 
static int scratchAllocOut = 0;
18022
 
#endif
18023
 
 
18024
 
 
18025
 
/*
18026
 
** Allocate memory that is to be used and released right away.
18027
 
** This routine is similar to alloca() in that it is not intended
18028
 
** for situations where the memory might be held long-term.  This
18029
 
** routine is intended to get memory to old large transient data
18030
 
** structures that would not normally fit on the stack of an
18031
 
** embedded processor.
18032
 
*/
18033
 
SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
18034
 
  void *p;
18035
 
  assert( n>0 );
18036
 
 
18037
 
  sqlite3_mutex_enter(mem0.mutex);
18038
 
  if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
18039
 
    p = mem0.pScratchFree;
18040
 
    mem0.pScratchFree = mem0.pScratchFree->pNext;
18041
 
    mem0.nScratchFree--;
18042
 
    sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
18043
 
    sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
18044
 
    sqlite3_mutex_leave(mem0.mutex);
18045
 
  }else{
18046
 
    if( sqlite3GlobalConfig.bMemstat ){
18047
 
      sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
18048
 
      n = mallocWithAlarm(n, &p);
18049
 
      if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
18050
 
      sqlite3_mutex_leave(mem0.mutex);
18051
 
    }else{
18052
 
      sqlite3_mutex_leave(mem0.mutex);
18053
 
      p = sqlite3GlobalConfig.m.xMalloc(n);
18054
 
    }
18055
 
    sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
18056
 
  }
18057
 
  assert( sqlite3_mutex_notheld(mem0.mutex) );
18058
 
 
18059
 
 
18060
 
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
18061
 
  /* Verify that no more than two scratch allocations per thread
18062
 
  ** are outstanding at one time.  (This is only checked in the
18063
 
  ** single-threaded case since checking in the multi-threaded case
18064
 
  ** would be much more complicated.) */
18065
 
  assert( scratchAllocOut<=1 );
18066
 
  if( p ) scratchAllocOut++;
18067
 
#endif
18068
 
 
18069
 
  return p;
18070
 
}
18071
 
SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
18072
 
  if( p ){
18073
 
 
18074
 
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
18075
 
    /* Verify that no more than two scratch allocation per thread
18076
 
    ** is outstanding at one time.  (This is only checked in the
18077
 
    ** single-threaded case since checking in the multi-threaded case
18078
 
    ** would be much more complicated.) */
18079
 
    assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
18080
 
    scratchAllocOut--;
18081
 
#endif
18082
 
 
18083
 
    if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
18084
 
      /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
18085
 
      ScratchFreeslot *pSlot;
18086
 
      pSlot = (ScratchFreeslot*)p;
18087
 
      sqlite3_mutex_enter(mem0.mutex);
18088
 
      pSlot->pNext = mem0.pScratchFree;
18089
 
      mem0.pScratchFree = pSlot;
18090
 
      mem0.nScratchFree++;
18091
 
      assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
18092
 
      sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
18093
 
      sqlite3_mutex_leave(mem0.mutex);
18094
 
    }else{
18095
 
      /* Release memory back to the heap */
18096
 
      assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
18097
 
      assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
18098
 
      sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
18099
 
      if( sqlite3GlobalConfig.bMemstat ){
18100
 
        int iSize = sqlite3MallocSize(p);
18101
 
        sqlite3_mutex_enter(mem0.mutex);
18102
 
        sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
18103
 
        sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
18104
 
        sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
18105
 
        sqlite3GlobalConfig.m.xFree(p);
18106
 
        sqlite3_mutex_leave(mem0.mutex);
18107
 
      }else{
18108
 
        sqlite3GlobalConfig.m.xFree(p);
18109
 
      }
18110
 
    }
18111
 
  }
18112
 
}
18113
 
 
18114
 
/*
18115
 
** TRUE if p is a lookaside memory allocation from db
18116
 
*/
18117
 
#ifndef SQLITE_OMIT_LOOKASIDE
18118
 
static int isLookaside(sqlite3 *db, void *p){
18119
 
  return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
18120
 
}
18121
 
#else
18122
 
#define isLookaside(A,B) 0
18123
 
#endif
18124
 
 
18125
 
/*
18126
 
** Return the size of a memory allocation previously obtained from
18127
 
** sqlite3Malloc() or sqlite3_malloc().
18128
 
*/
18129
 
SQLITE_PRIVATE int sqlite3MallocSize(void *p){
18130
 
  assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
18131
 
  assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
18132
 
  return sqlite3GlobalConfig.m.xSize(p);
18133
 
}
18134
 
SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
18135
 
  assert( db==0 || sqlite3_mutex_held(db->mutex) );
18136
 
  if( db && isLookaside(db, p) ){
18137
 
    return db->lookaside.sz;
18138
 
  }else{
18139
 
    assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
18140
 
    assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
18141
 
    assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
18142
 
    return sqlite3GlobalConfig.m.xSize(p);
18143
 
  }
18144
 
}
18145
 
 
18146
 
/*
18147
 
** Free memory previously obtained from sqlite3Malloc().
18148
 
*/
18149
 
SQLITE_API void sqlite3_free(void *p){
18150
 
  if( p==0 ) return;  /* IMP: R-49053-54554 */
18151
 
  assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
18152
 
  assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
18153
 
  if( sqlite3GlobalConfig.bMemstat ){
18154
 
    sqlite3_mutex_enter(mem0.mutex);
18155
 
    sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
18156
 
    sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
18157
 
    sqlite3GlobalConfig.m.xFree(p);
18158
 
    sqlite3_mutex_leave(mem0.mutex);
18159
 
  }else{
18160
 
    sqlite3GlobalConfig.m.xFree(p);
18161
 
  }
18162
 
}
18163
 
 
18164
 
/*
18165
 
** Free memory that might be associated with a particular database
18166
 
** connection.
18167
 
*/
18168
 
SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
18169
 
  assert( db==0 || sqlite3_mutex_held(db->mutex) );
18170
 
  if( db ){
18171
 
    if( db->pnBytesFreed ){
18172
 
      *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
18173
 
      return;
18174
 
    }
18175
 
    if( isLookaside(db, p) ){
18176
 
      LookasideSlot *pBuf = (LookasideSlot*)p;
18177
 
      pBuf->pNext = db->lookaside.pFree;
18178
 
      db->lookaside.pFree = pBuf;
18179
 
      db->lookaside.nOut--;
18180
 
      return;
18181
 
    }
18182
 
  }
18183
 
  assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
18184
 
  assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
18185
 
  assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
18186
 
  sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
18187
 
  sqlite3_free(p);
18188
 
}
18189
 
 
18190
 
/*
18191
 
** Change the size of an existing memory allocation
18192
 
*/
18193
 
SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, int nBytes){
18194
 
  int nOld, nNew;
18195
 
  void *pNew;
18196
 
  if( pOld==0 ){
18197
 
    return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
18198
 
  }
18199
 
  if( nBytes<=0 ){
18200
 
    sqlite3_free(pOld); /* IMP: R-31593-10574 */
18201
 
    return 0;
18202
 
  }
18203
 
  if( nBytes>=0x7fffff00 ){
18204
 
    /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
18205
 
    return 0;
18206
 
  }
18207
 
  nOld = sqlite3MallocSize(pOld);
18208
 
  /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
18209
 
  ** argument to xRealloc is always a value returned by a prior call to
18210
 
  ** xRoundup. */
18211
 
  nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
18212
 
  if( nOld==nNew ){
18213
 
    pNew = pOld;
18214
 
  }else if( sqlite3GlobalConfig.bMemstat ){
18215
 
    sqlite3_mutex_enter(mem0.mutex);
18216
 
    sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
18217
 
    if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= 
18218
 
          mem0.alarmThreshold ){
18219
 
      sqlite3MallocAlarm(nNew-nOld);
18220
 
    }
18221
 
    assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
18222
 
    assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
18223
 
    pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
18224
 
    if( pNew==0 && mem0.alarmCallback ){
18225
 
      sqlite3MallocAlarm(nBytes);
18226
 
      pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
18227
 
    }
18228
 
    if( pNew ){
18229
 
      nNew = sqlite3MallocSize(pNew);
18230
 
      sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
18231
 
    }
18232
 
    sqlite3_mutex_leave(mem0.mutex);
18233
 
  }else{
18234
 
    pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
18235
 
  }
18236
 
  assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
18237
 
  return pNew;
18238
 
}
18239
 
 
18240
 
/*
18241
 
** The public interface to sqlite3Realloc.  Make sure that the memory
18242
 
** subsystem is initialized prior to invoking sqliteRealloc.
18243
 
*/
18244
 
SQLITE_API void *sqlite3_realloc(void *pOld, int n){
18245
 
#ifndef SQLITE_OMIT_AUTOINIT
18246
 
  if( sqlite3_initialize() ) return 0;
18247
 
#endif
18248
 
  return sqlite3Realloc(pOld, n);
18249
 
}
18250
 
 
18251
 
 
18252
 
/*
18253
 
** Allocate and zero memory.
18254
 
*/ 
18255
 
SQLITE_PRIVATE void *sqlite3MallocZero(int n){
18256
 
  void *p = sqlite3Malloc(n);
18257
 
  if( p ){
18258
 
    memset(p, 0, n);
18259
 
  }
18260
 
  return p;
18261
 
}
18262
 
 
18263
 
/*
18264
 
** Allocate and zero memory.  If the allocation fails, make
18265
 
** the mallocFailed flag in the connection pointer.
18266
 
*/
18267
 
SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, int n){
18268
 
  void *p = sqlite3DbMallocRaw(db, n);
18269
 
  if( p ){
18270
 
    memset(p, 0, n);
18271
 
  }
18272
 
  return p;
18273
 
}
18274
 
 
18275
 
/*
18276
 
** Allocate and zero memory.  If the allocation fails, make
18277
 
** the mallocFailed flag in the connection pointer.
18278
 
**
18279
 
** If db!=0 and db->mallocFailed is true (indicating a prior malloc
18280
 
** failure on the same database connection) then always return 0.
18281
 
** Hence for a particular database connection, once malloc starts
18282
 
** failing, it fails consistently until mallocFailed is reset.
18283
 
** This is an important assumption.  There are many places in the
18284
 
** code that do things like this:
18285
 
**
18286
 
**         int *a = (int*)sqlite3DbMallocRaw(db, 100);
18287
 
**         int *b = (int*)sqlite3DbMallocRaw(db, 200);
18288
 
**         if( b ) a[10] = 9;
18289
 
**
18290
 
** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
18291
 
** that all prior mallocs (ex: "a") worked too.
18292
 
*/
18293
 
SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, int n){
18294
 
  void *p;
18295
 
  assert( db==0 || sqlite3_mutex_held(db->mutex) );
18296
 
  assert( db==0 || db->pnBytesFreed==0 );
18297
 
#ifndef SQLITE_OMIT_LOOKASIDE
18298
 
  if( db ){
18299
 
    LookasideSlot *pBuf;
18300
 
    if( db->mallocFailed ){
18301
 
      return 0;
18302
 
    }
18303
 
    if( db->lookaside.bEnabled ){
18304
 
      if( n>db->lookaside.sz ){
18305
 
        db->lookaside.anStat[1]++;
18306
 
      }else if( (pBuf = db->lookaside.pFree)==0 ){
18307
 
        db->lookaside.anStat[2]++;
18308
 
      }else{
18309
 
        db->lookaside.pFree = pBuf->pNext;
18310
 
        db->lookaside.nOut++;
18311
 
        db->lookaside.anStat[0]++;
18312
 
        if( db->lookaside.nOut>db->lookaside.mxOut ){
18313
 
          db->lookaside.mxOut = db->lookaside.nOut;
18314
 
        }
18315
 
        return (void*)pBuf;
18316
 
      }
18317
 
    }
18318
 
  }
18319
 
#else
18320
 
  if( db && db->mallocFailed ){
18321
 
    return 0;
18322
 
  }
18323
 
#endif
18324
 
  p = sqlite3Malloc(n);
18325
 
  if( !p && db ){
18326
 
    db->mallocFailed = 1;
18327
 
  }
18328
 
  sqlite3MemdebugSetType(p, MEMTYPE_DB |
18329
 
         ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
18330
 
  return p;
18331
 
}
18332
 
 
18333
 
/*
18334
 
** Resize the block of memory pointed to by p to n bytes. If the
18335
 
** resize fails, set the mallocFailed flag in the connection object.
18336
 
*/
18337
 
SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
18338
 
  void *pNew = 0;
18339
 
  assert( db!=0 );
18340
 
  assert( sqlite3_mutex_held(db->mutex) );
18341
 
  if( db->mallocFailed==0 ){
18342
 
    if( p==0 ){
18343
 
      return sqlite3DbMallocRaw(db, n);
18344
 
    }
18345
 
    if( isLookaside(db, p) ){
18346
 
      if( n<=db->lookaside.sz ){
18347
 
        return p;
18348
 
      }
18349
 
      pNew = sqlite3DbMallocRaw(db, n);
18350
 
      if( pNew ){
18351
 
        memcpy(pNew, p, db->lookaside.sz);
18352
 
        sqlite3DbFree(db, p);
18353
 
      }
18354
 
    }else{
18355
 
      assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
18356
 
      assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
18357
 
      sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
18358
 
      pNew = sqlite3_realloc(p, n);
18359
 
      if( !pNew ){
18360
 
        sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
18361
 
        db->mallocFailed = 1;
18362
 
      }
18363
 
      sqlite3MemdebugSetType(pNew, MEMTYPE_DB | 
18364
 
            (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
18365
 
    }
18366
 
  }
18367
 
  return pNew;
18368
 
}
18369
 
 
18370
 
/*
18371
 
** Attempt to reallocate p.  If the reallocation fails, then free p
18372
 
** and set the mallocFailed flag in the database connection.
18373
 
*/
18374
 
SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
18375
 
  void *pNew;
18376
 
  pNew = sqlite3DbRealloc(db, p, n);
18377
 
  if( !pNew ){
18378
 
    sqlite3DbFree(db, p);
18379
 
  }
18380
 
  return pNew;
18381
 
}
18382
 
 
18383
 
/*
18384
 
** Make a copy of a string in memory obtained from sqliteMalloc(). These 
18385
 
** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
18386
 
** is because when memory debugging is turned on, these two functions are 
18387
 
** called via macros that record the current file and line number in the
18388
 
** ThreadData structure.
18389
 
*/
18390
 
SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
18391
 
  char *zNew;
18392
 
  size_t n;
18393
 
  if( z==0 ){
18394
 
    return 0;
18395
 
  }
18396
 
  n = sqlite3Strlen30(z) + 1;
18397
 
  assert( (n&0x7fffffff)==n );
18398
 
  zNew = sqlite3DbMallocRaw(db, (int)n);
18399
 
  if( zNew ){
18400
 
    memcpy(zNew, z, n);
18401
 
  }
18402
 
  return zNew;
18403
 
}
18404
 
SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
18405
 
  char *zNew;
18406
 
  if( z==0 ){
18407
 
    return 0;
18408
 
  }
18409
 
  assert( (n&0x7fffffff)==n );
18410
 
  zNew = sqlite3DbMallocRaw(db, n+1);
18411
 
  if( zNew ){
18412
 
    memcpy(zNew, z, n);
18413
 
    zNew[n] = 0;
18414
 
  }
18415
 
  return zNew;
18416
 
}
18417
 
 
18418
 
/*
18419
 
** Create a string from the zFromat argument and the va_list that follows.
18420
 
** Store the string in memory obtained from sqliteMalloc() and make *pz
18421
 
** point to that string.
18422
 
*/
18423
 
SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
18424
 
  va_list ap;
18425
 
  char *z;
18426
 
 
18427
 
  va_start(ap, zFormat);
18428
 
  z = sqlite3VMPrintf(db, zFormat, ap);
18429
 
  va_end(ap);
18430
 
  sqlite3DbFree(db, *pz);
18431
 
  *pz = z;
18432
 
}
18433
 
 
18434
 
 
18435
 
/*
18436
 
** This function must be called before exiting any API function (i.e. 
18437
 
** returning control to the user) that has called sqlite3_malloc or
18438
 
** sqlite3_realloc.
18439
 
**
18440
 
** The returned value is normally a copy of the second argument to this
18441
 
** function. However, if a malloc() failure has occurred since the previous
18442
 
** invocation SQLITE_NOMEM is returned instead. 
18443
 
**
18444
 
** If the first argument, db, is not NULL and a malloc() error has occurred,
18445
 
** then the connection error-code (the value returned by sqlite3_errcode())
18446
 
** is set to SQLITE_NOMEM.
18447
 
*/
18448
 
SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
18449
 
  /* If the db handle is not NULL, then we must hold the connection handle
18450
 
  ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 
18451
 
  ** is unsafe, as is the call to sqlite3Error().
18452
 
  */
18453
 
  assert( !db || sqlite3_mutex_held(db->mutex) );
18454
 
  if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
18455
 
    sqlite3Error(db, SQLITE_NOMEM, 0);
18456
 
    db->mallocFailed = 0;
18457
 
    rc = SQLITE_NOMEM;
18458
 
  }
18459
 
  return rc & (db ? db->errMask : 0xff);
18460
 
}
18461
 
 
18462
 
/************** End of malloc.c **********************************************/
18463
 
/************** Begin file printf.c ******************************************/
18464
 
/*
18465
 
** The "printf" code that follows dates from the 1980's.  It is in
18466
 
** the public domain.  The original comments are included here for
18467
 
** completeness.  They are very out-of-date but might be useful as
18468
 
** an historical reference.  Most of the "enhancements" have been backed
18469
 
** out so that the functionality is now the same as standard printf().
18470
 
**
18471
 
**************************************************************************
18472
 
**
18473
 
** The following modules is an enhanced replacement for the "printf" subroutines
18474
 
** found in the standard C library.  The following enhancements are
18475
 
** supported:
18476
 
**
18477
 
**      +  Additional functions.  The standard set of "printf" functions
18478
 
**         includes printf, fprintf, sprintf, vprintf, vfprintf, and
18479
 
**         vsprintf.  This module adds the following:
18480
 
**
18481
 
**           *  snprintf -- Works like sprintf, but has an extra argument
18482
 
**                          which is the size of the buffer written to.
18483
 
**
18484
 
**           *  mprintf --  Similar to sprintf.  Writes output to memory
18485
 
**                          obtained from malloc.
18486
 
**
18487
 
**           *  xprintf --  Calls a function to dispose of output.
18488
 
**
18489
 
**           *  nprintf --  No output, but returns the number of characters
18490
 
**                          that would have been output by printf.
18491
 
**
18492
 
**           *  A v- version (ex: vsnprintf) of every function is also
18493
 
**              supplied.
18494
 
**
18495
 
**      +  A few extensions to the formatting notation are supported:
18496
 
**
18497
 
**           *  The "=" flag (similar to "-") causes the output to be
18498
 
**              be centered in the appropriately sized field.
18499
 
**
18500
 
**           *  The %b field outputs an integer in binary notation.
18501
 
**
18502
 
**           *  The %c field now accepts a precision.  The character output
18503
 
**              is repeated by the number of times the precision specifies.
18504
 
**
18505
 
**           *  The %' field works like %c, but takes as its character the
18506
 
**              next character of the format string, instead of the next
18507
 
**              argument.  For example,  printf("%.78'-")  prints 78 minus
18508
 
**              signs, the same as  printf("%.78c",'-').
18509
 
**
18510
 
**      +  When compiled using GCC on a SPARC, this version of printf is
18511
 
**         faster than the library printf for SUN OS 4.1.
18512
 
**
18513
 
**      +  All functions are fully reentrant.
18514
 
**
18515
 
*/
18516
 
 
18517
 
/*
18518
 
** Conversion types fall into various categories as defined by the
18519
 
** following enumeration.
18520
 
*/
18521
 
#define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
18522
 
#define etFLOAT       2 /* Floating point.  %f */
18523
 
#define etEXP         3 /* Exponentional notation. %e and %E */
18524
 
#define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
18525
 
#define etSIZE        5 /* Return number of characters processed so far. %n */
18526
 
#define etSTRING      6 /* Strings. %s */
18527
 
#define etDYNSTRING   7 /* Dynamically allocated strings. %z */
18528
 
#define etPERCENT     8 /* Percent symbol. %% */
18529
 
#define etCHARX       9 /* Characters. %c */
18530
 
/* The rest are extensions, not normally found in printf() */
18531
 
#define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
18532
 
#define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
18533
 
                          NULL pointers replaced by SQL NULL.  %Q */
18534
 
#define etTOKEN      12 /* a pointer to a Token structure */
18535
 
#define etSRCLIST    13 /* a pointer to a SrcList */
18536
 
#define etPOINTER    14 /* The %p conversion */
18537
 
#define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
18538
 
#define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
18539
 
 
18540
 
#define etINVALID     0 /* Any unrecognized conversion type */
18541
 
 
18542
 
 
18543
 
/*
18544
 
** An "etByte" is an 8-bit unsigned value.
18545
 
*/
18546
 
typedef unsigned char etByte;
18547
 
 
18548
 
/*
18549
 
** Each builtin conversion character (ex: the 'd' in "%d") is described
18550
 
** by an instance of the following structure
18551
 
*/
18552
 
typedef struct et_info {   /* Information about each format field */
18553
 
  char fmttype;            /* The format field code letter */
18554
 
  etByte base;             /* The base for radix conversion */
18555
 
  etByte flags;            /* One or more of FLAG_ constants below */
18556
 
  etByte type;             /* Conversion paradigm */
18557
 
  etByte charset;          /* Offset into aDigits[] of the digits string */
18558
 
  etByte prefix;           /* Offset into aPrefix[] of the prefix string */
18559
 
} et_info;
18560
 
 
18561
 
/*
18562
 
** Allowed values for et_info.flags
18563
 
*/
18564
 
#define FLAG_SIGNED  1     /* True if the value to convert is signed */
18565
 
#define FLAG_INTERN  2     /* True if for internal use only */
18566
 
#define FLAG_STRING  4     /* Allow infinity precision */
18567
 
 
18568
 
 
18569
 
/*
18570
 
** The following table is searched linearly, so it is good to put the
18571
 
** most frequently used conversion types first.
18572
 
*/
18573
 
static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
18574
 
static const char aPrefix[] = "-x0\000X0";
18575
 
static const et_info fmtinfo[] = {
18576
 
  {  'd', 10, 1, etRADIX,      0,  0 },
18577
 
  {  's',  0, 4, etSTRING,     0,  0 },
18578
 
  {  'g',  0, 1, etGENERIC,    30, 0 },
18579
 
  {  'z',  0, 4, etDYNSTRING,  0,  0 },
18580
 
  {  'q',  0, 4, etSQLESCAPE,  0,  0 },
18581
 
  {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
18582
 
  {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
18583
 
  {  'c',  0, 0, etCHARX,      0,  0 },
18584
 
  {  'o',  8, 0, etRADIX,      0,  2 },
18585
 
  {  'u', 10, 0, etRADIX,      0,  0 },
18586
 
  {  'x', 16, 0, etRADIX,      16, 1 },
18587
 
  {  'X', 16, 0, etRADIX,      0,  4 },
18588
 
#ifndef SQLITE_OMIT_FLOATING_POINT
18589
 
  {  'f',  0, 1, etFLOAT,      0,  0 },
18590
 
  {  'e',  0, 1, etEXP,        30, 0 },
18591
 
  {  'E',  0, 1, etEXP,        14, 0 },
18592
 
  {  'G',  0, 1, etGENERIC,    14, 0 },
18593
 
#endif
18594
 
  {  'i', 10, 1, etRADIX,      0,  0 },
18595
 
  {  'n',  0, 0, etSIZE,       0,  0 },
18596
 
  {  '%',  0, 0, etPERCENT,    0,  0 },
18597
 
  {  'p', 16, 0, etPOINTER,    0,  1 },
18598
 
 
18599
 
/* All the rest have the FLAG_INTERN bit set and are thus for internal
18600
 
** use only */
18601
 
  {  'T',  0, 2, etTOKEN,      0,  0 },
18602
 
  {  'S',  0, 2, etSRCLIST,    0,  0 },
18603
 
  {  'r', 10, 3, etORDINAL,    0,  0 },
18604
 
};
18605
 
 
18606
 
/*
18607
 
** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
18608
 
** conversions will work.
18609
 
*/
18610
 
#ifndef SQLITE_OMIT_FLOATING_POINT
18611
 
/*
18612
 
** "*val" is a double such that 0.1 <= *val < 10.0
18613
 
** Return the ascii code for the leading digit of *val, then
18614
 
** multiply "*val" by 10.0 to renormalize.
18615
 
**
18616
 
** Example:
18617
 
**     input:     *val = 3.14159
18618
 
**     output:    *val = 1.4159    function return = '3'
18619
 
**
18620
 
** The counter *cnt is incremented each time.  After counter exceeds
18621
 
** 16 (the number of significant digits in a 64-bit float) '0' is
18622
 
** always returned.
18623
 
*/
18624
 
static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
18625
 
  int digit;
18626
 
  LONGDOUBLE_TYPE d;
18627
 
  if( (*cnt)++ >= 16 ) return '0';
18628
 
  digit = (int)*val;
18629
 
  d = digit;
18630
 
  digit += '0';
18631
 
  *val = (*val - d)*10.0;
18632
 
  return (char)digit;
18633
 
}
18634
 
#endif /* SQLITE_OMIT_FLOATING_POINT */
18635
 
 
18636
 
/*
18637
 
** Append N space characters to the given string buffer.
18638
 
*/
18639
 
static void appendSpace(StrAccum *pAccum, int N){
18640
 
  static const char zSpaces[] = "                             ";
18641
 
  while( N>=(int)sizeof(zSpaces)-1 ){
18642
 
    sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
18643
 
    N -= sizeof(zSpaces)-1;
18644
 
  }
18645
 
  if( N>0 ){
18646
 
    sqlite3StrAccumAppend(pAccum, zSpaces, N);
18647
 
  }
18648
 
}
18649
 
 
18650
 
/*
18651
 
** On machines with a small stack size, you can redefine the
18652
 
** SQLITE_PRINT_BUF_SIZE to be less than 350.
18653
 
*/
18654
 
#ifndef SQLITE_PRINT_BUF_SIZE
18655
 
# if defined(SQLITE_SMALL_STACK)
18656
 
#   define SQLITE_PRINT_BUF_SIZE 50
18657
 
# else
18658
 
#   define SQLITE_PRINT_BUF_SIZE 350
18659
 
# endif
18660
 
#endif
18661
 
#define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
18662
 
 
18663
 
/*
18664
 
** The root program.  All variations call this core.
18665
 
**
18666
 
** INPUTS:
18667
 
**   func   This is a pointer to a function taking three arguments
18668
 
**            1. A pointer to anything.  Same as the "arg" parameter.
18669
 
**            2. A pointer to the list of characters to be output
18670
 
**               (Note, this list is NOT null terminated.)
18671
 
**            3. An integer number of characters to be output.
18672
 
**               (Note: This number might be zero.)
18673
 
**
18674
 
**   arg    This is the pointer to anything which will be passed as the
18675
 
**          first argument to "func".  Use it for whatever you like.
18676
 
**
18677
 
**   fmt    This is the format string, as in the usual print.
18678
 
**
18679
 
**   ap     This is a pointer to a list of arguments.  Same as in
18680
 
**          vfprint.
18681
 
**
18682
 
** OUTPUTS:
18683
 
**          The return value is the total number of characters sent to
18684
 
**          the function "func".  Returns -1 on a error.
18685
 
**
18686
 
** Note that the order in which automatic variables are declared below
18687
 
** seems to make a big difference in determining how fast this beast
18688
 
** will run.
18689
 
*/
18690
 
SQLITE_PRIVATE void sqlite3VXPrintf(
18691
 
  StrAccum *pAccum,                  /* Accumulate results here */
18692
 
  int useExtended,                   /* Allow extended %-conversions */
18693
 
  const char *fmt,                   /* Format string */
18694
 
  va_list ap                         /* arguments */
18695
 
){
18696
 
  int c;                     /* Next character in the format string */
18697
 
  char *bufpt;               /* Pointer to the conversion buffer */
18698
 
  int precision;             /* Precision of the current field */
18699
 
  int length;                /* Length of the field */
18700
 
  int idx;                   /* A general purpose loop counter */
18701
 
  int width;                 /* Width of the current field */
18702
 
  etByte flag_leftjustify;   /* True if "-" flag is present */
18703
 
  etByte flag_plussign;      /* True if "+" flag is present */
18704
 
  etByte flag_blanksign;     /* True if " " flag is present */
18705
 
  etByte flag_alternateform; /* True if "#" flag is present */
18706
 
  etByte flag_altform2;      /* True if "!" flag is present */
18707
 
  etByte flag_zeropad;       /* True if field width constant starts with zero */
18708
 
  etByte flag_long;          /* True if "l" flag is present */
18709
 
  etByte flag_longlong;      /* True if the "ll" flag is present */
18710
 
  etByte done;               /* Loop termination flag */
18711
 
  sqlite_uint64 longvalue;   /* Value for integer types */
18712
 
  LONGDOUBLE_TYPE realvalue; /* Value for real types */
18713
 
  const et_info *infop;      /* Pointer to the appropriate info structure */
18714
 
  char buf[etBUFSIZE];       /* Conversion buffer */
18715
 
  char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
18716
 
  etByte xtype = 0;          /* Conversion paradigm */
18717
 
  char *zExtra;              /* Extra memory used for etTCLESCAPE conversions */
18718
 
#ifndef SQLITE_OMIT_FLOATING_POINT
18719
 
  int  exp, e2;              /* exponent of real numbers */
18720
 
  double rounder;            /* Used for rounding floating point values */
18721
 
  etByte flag_dp;            /* True if decimal point should be shown */
18722
 
  etByte flag_rtz;           /* True if trailing zeros should be removed */
18723
 
  etByte flag_exp;           /* True to force display of the exponent */
18724
 
  int nsd;                   /* Number of significant digits returned */
18725
 
#endif
18726
 
 
18727
 
  length = 0;
18728
 
  bufpt = 0;
18729
 
  for(; (c=(*fmt))!=0; ++fmt){
18730
 
    if( c!='%' ){
18731
 
      int amt;
18732
 
      bufpt = (char *)fmt;
18733
 
      amt = 1;
18734
 
      while( (c=(*++fmt))!='%' && c!=0 ) amt++;
18735
 
      sqlite3StrAccumAppend(pAccum, bufpt, amt);
18736
 
      if( c==0 ) break;
18737
 
    }
18738
 
    if( (c=(*++fmt))==0 ){
18739
 
      sqlite3StrAccumAppend(pAccum, "%", 1);
18740
 
      break;
18741
 
    }
18742
 
    /* Find out what flags are present */
18743
 
    flag_leftjustify = flag_plussign = flag_blanksign = 
18744
 
     flag_alternateform = flag_altform2 = flag_zeropad = 0;
18745
 
    done = 0;
18746
 
    do{
18747
 
      switch( c ){
18748
 
        case '-':   flag_leftjustify = 1;     break;
18749
 
        case '+':   flag_plussign = 1;        break;
18750
 
        case ' ':   flag_blanksign = 1;       break;
18751
 
        case '#':   flag_alternateform = 1;   break;
18752
 
        case '!':   flag_altform2 = 1;        break;
18753
 
        case '0':   flag_zeropad = 1;         break;
18754
 
        default:    done = 1;                 break;
18755
 
      }
18756
 
    }while( !done && (c=(*++fmt))!=0 );
18757
 
    /* Get the field width */
18758
 
    width = 0;
18759
 
    if( c=='*' ){
18760
 
      width = va_arg(ap,int);
18761
 
      if( width<0 ){
18762
 
        flag_leftjustify = 1;
18763
 
        width = -width;
18764
 
      }
18765
 
      c = *++fmt;
18766
 
    }else{
18767
 
      while( c>='0' && c<='9' ){
18768
 
        width = width*10 + c - '0';
18769
 
        c = *++fmt;
18770
 
      }
18771
 
    }
18772
 
    if( width > etBUFSIZE-10 ){
18773
 
      width = etBUFSIZE-10;
18774
 
    }
18775
 
    /* Get the precision */
18776
 
    if( c=='.' ){
18777
 
      precision = 0;
18778
 
      c = *++fmt;
18779
 
      if( c=='*' ){
18780
 
        precision = va_arg(ap,int);
18781
 
        if( precision<0 ) precision = -precision;
18782
 
        c = *++fmt;
18783
 
      }else{
18784
 
        while( c>='0' && c<='9' ){
18785
 
          precision = precision*10 + c - '0';
18786
 
          c = *++fmt;
18787
 
        }
18788
 
      }
18789
 
    }else{
18790
 
      precision = -1;
18791
 
    }
18792
 
    /* Get the conversion type modifier */
18793
 
    if( c=='l' ){
18794
 
      flag_long = 1;
18795
 
      c = *++fmt;
18796
 
      if( c=='l' ){
18797
 
        flag_longlong = 1;
18798
 
        c = *++fmt;
18799
 
      }else{
18800
 
        flag_longlong = 0;
18801
 
      }
18802
 
    }else{
18803
 
      flag_long = flag_longlong = 0;
18804
 
    }
18805
 
    /* Fetch the info entry for the field */
18806
 
    infop = &fmtinfo[0];
18807
 
    xtype = etINVALID;
18808
 
    for(idx=0; idx<ArraySize(fmtinfo); idx++){
18809
 
      if( c==fmtinfo[idx].fmttype ){
18810
 
        infop = &fmtinfo[idx];
18811
 
        if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
18812
 
          xtype = infop->type;
18813
 
        }else{
18814
 
          return;
18815
 
        }
18816
 
        break;
18817
 
      }
18818
 
    }
18819
 
    zExtra = 0;
18820
 
 
18821
 
 
18822
 
    /* Limit the precision to prevent overflowing buf[] during conversion */
18823
 
    if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
18824
 
      precision = etBUFSIZE-40;
18825
 
    }
18826
 
 
18827
 
    /*
18828
 
    ** At this point, variables are initialized as follows:
18829
 
    **
18830
 
    **   flag_alternateform          TRUE if a '#' is present.
18831
 
    **   flag_altform2               TRUE if a '!' is present.
18832
 
    **   flag_plussign               TRUE if a '+' is present.
18833
 
    **   flag_leftjustify            TRUE if a '-' is present or if the
18834
 
    **                               field width was negative.
18835
 
    **   flag_zeropad                TRUE if the width began with 0.
18836
 
    **   flag_long                   TRUE if the letter 'l' (ell) prefixed
18837
 
    **                               the conversion character.
18838
 
    **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
18839
 
    **                               the conversion character.
18840
 
    **   flag_blanksign              TRUE if a ' ' is present.
18841
 
    **   width                       The specified field width.  This is
18842
 
    **                               always non-negative.  Zero is the default.
18843
 
    **   precision                   The specified precision.  The default
18844
 
    **                               is -1.
18845
 
    **   xtype                       The class of the conversion.
18846
 
    **   infop                       Pointer to the appropriate info struct.
18847
 
    */
18848
 
    switch( xtype ){
18849
 
      case etPOINTER:
18850
 
        flag_longlong = sizeof(char*)==sizeof(i64);
18851
 
        flag_long = sizeof(char*)==sizeof(long int);
18852
 
        /* Fall through into the next case */
18853
 
      case etORDINAL:
18854
 
      case etRADIX:
18855
 
        if( infop->flags & FLAG_SIGNED ){
18856
 
          i64 v;
18857
 
          if( flag_longlong ){
18858
 
            v = va_arg(ap,i64);
18859
 
          }else if( flag_long ){
18860
 
            v = va_arg(ap,long int);
18861
 
          }else{
18862
 
            v = va_arg(ap,int);
18863
 
          }
18864
 
          if( v<0 ){
18865
 
            if( v==SMALLEST_INT64 ){
18866
 
              longvalue = ((u64)1)<<63;
18867
 
            }else{
18868
 
              longvalue = -v;
18869
 
            }
18870
 
            prefix = '-';
18871
 
          }else{
18872
 
            longvalue = v;
18873
 
            if( flag_plussign )        prefix = '+';
18874
 
            else if( flag_blanksign )  prefix = ' ';
18875
 
            else                       prefix = 0;
18876
 
          }
18877
 
        }else{
18878
 
          if( flag_longlong ){
18879
 
            longvalue = va_arg(ap,u64);
18880
 
          }else if( flag_long ){
18881
 
            longvalue = va_arg(ap,unsigned long int);
18882
 
          }else{
18883
 
            longvalue = va_arg(ap,unsigned int);
18884
 
          }
18885
 
          prefix = 0;
18886
 
        }
18887
 
        if( longvalue==0 ) flag_alternateform = 0;
18888
 
        if( flag_zeropad && precision<width-(prefix!=0) ){
18889
 
          precision = width-(prefix!=0);
18890
 
        }
18891
 
        bufpt = &buf[etBUFSIZE-1];
18892
 
        if( xtype==etORDINAL ){
18893
 
          static const char zOrd[] = "thstndrd";
18894
 
          int x = (int)(longvalue % 10);
18895
 
          if( x>=4 || (longvalue/10)%10==1 ){
18896
 
            x = 0;
18897
 
          }
18898
 
          buf[etBUFSIZE-3] = zOrd[x*2];
18899
 
          buf[etBUFSIZE-2] = zOrd[x*2+1];
18900
 
          bufpt -= 2;
18901
 
        }
18902
 
        {
18903
 
          register const char *cset;      /* Use registers for speed */
18904
 
          register int base;
18905
 
          cset = &aDigits[infop->charset];
18906
 
          base = infop->base;
18907
 
          do{                                           /* Convert to ascii */
18908
 
            *(--bufpt) = cset[longvalue%base];
18909
 
            longvalue = longvalue/base;
18910
 
          }while( longvalue>0 );
18911
 
        }
18912
 
        length = (int)(&buf[etBUFSIZE-1]-bufpt);
18913
 
        for(idx=precision-length; idx>0; idx--){
18914
 
          *(--bufpt) = '0';                             /* Zero pad */
18915
 
        }
18916
 
        if( prefix ) *(--bufpt) = prefix;               /* Add sign */
18917
 
        if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
18918
 
          const char *pre;
18919
 
          char x;
18920
 
          pre = &aPrefix[infop->prefix];
18921
 
          for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
18922
 
        }
18923
 
        length = (int)(&buf[etBUFSIZE-1]-bufpt);
18924
 
        break;
18925
 
      case etFLOAT:
18926
 
      case etEXP:
18927
 
      case etGENERIC:
18928
 
        realvalue = va_arg(ap,double);
18929
 
#ifdef SQLITE_OMIT_FLOATING_POINT
18930
 
        length = 0;
18931
 
#else
18932
 
        if( precision<0 ) precision = 6;         /* Set default precision */
18933
 
        if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
18934
 
        if( realvalue<0.0 ){
18935
 
          realvalue = -realvalue;
18936
 
          prefix = '-';
18937
 
        }else{
18938
 
          if( flag_plussign )          prefix = '+';
18939
 
          else if( flag_blanksign )    prefix = ' ';
18940
 
          else                         prefix = 0;
18941
 
        }
18942
 
        if( xtype==etGENERIC && precision>0 ) precision--;
18943
 
#if 0
18944
 
        /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
18945
 
        for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
18946
 
#else
18947
 
        /* It makes more sense to use 0.5 */
18948
 
        for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
18949
 
#endif
18950
 
        if( xtype==etFLOAT ) realvalue += rounder;
18951
 
        /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
18952
 
        exp = 0;
18953
 
        if( sqlite3IsNaN((double)realvalue) ){
18954
 
          bufpt = "NaN";
18955
 
          length = 3;
18956
 
          break;
18957
 
        }
18958
 
        if( realvalue>0.0 ){
18959
 
          while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
18960
 
          while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
18961
 
          while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
18962
 
          while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
18963
 
          while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
18964
 
          if( exp>350 ){
18965
 
            if( prefix=='-' ){
18966
 
              bufpt = "-Inf";
18967
 
            }else if( prefix=='+' ){
18968
 
              bufpt = "+Inf";
18969
 
            }else{
18970
 
              bufpt = "Inf";
18971
 
            }
18972
 
            length = sqlite3Strlen30(bufpt);
18973
 
            break;
18974
 
          }
18975
 
        }
18976
 
        bufpt = buf;
18977
 
        /*
18978
 
        ** If the field type is etGENERIC, then convert to either etEXP
18979
 
        ** or etFLOAT, as appropriate.
18980
 
        */
18981
 
        flag_exp = xtype==etEXP;
18982
 
        if( xtype!=etFLOAT ){
18983
 
          realvalue += rounder;
18984
 
          if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
18985
 
        }
18986
 
        if( xtype==etGENERIC ){
18987
 
          flag_rtz = !flag_alternateform;
18988
 
          if( exp<-4 || exp>precision ){
18989
 
            xtype = etEXP;
18990
 
          }else{
18991
 
            precision = precision - exp;
18992
 
            xtype = etFLOAT;
18993
 
          }
18994
 
        }else{
18995
 
          flag_rtz = 0;
18996
 
        }
18997
 
        if( xtype==etEXP ){
18998
 
          e2 = 0;
18999
 
        }else{
19000
 
          e2 = exp;
19001
 
        }
19002
 
        nsd = 0;
19003
 
        flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
19004
 
        /* The sign in front of the number */
19005
 
        if( prefix ){
19006
 
          *(bufpt++) = prefix;
19007
 
        }
19008
 
        /* Digits prior to the decimal point */
19009
 
        if( e2<0 ){
19010
 
          *(bufpt++) = '0';
19011
 
        }else{
19012
 
          for(; e2>=0; e2--){
19013
 
            *(bufpt++) = et_getdigit(&realvalue,&nsd);
19014
 
          }
19015
 
        }
19016
 
        /* The decimal point */
19017
 
        if( flag_dp ){
19018
 
          *(bufpt++) = '.';
19019
 
        }
19020
 
        /* "0" digits after the decimal point but before the first
19021
 
        ** significant digit of the number */
19022
 
        for(e2++; e2<0; precision--, e2++){
19023
 
          assert( precision>0 );
19024
 
          *(bufpt++) = '0';
19025
 
        }
19026
 
        /* Significant digits after the decimal point */
19027
 
        while( (precision--)>0 ){
19028
 
          *(bufpt++) = et_getdigit(&realvalue,&nsd);
19029
 
        }
19030
 
        /* Remove trailing zeros and the "." if no digits follow the "." */
19031
 
        if( flag_rtz && flag_dp ){
19032
 
          while( bufpt[-1]=='0' ) *(--bufpt) = 0;
19033
 
          assert( bufpt>buf );
19034
 
          if( bufpt[-1]=='.' ){
19035
 
            if( flag_altform2 ){
19036
 
              *(bufpt++) = '0';
19037
 
            }else{
19038
 
              *(--bufpt) = 0;
19039
 
            }
19040
 
          }
19041
 
        }
19042
 
        /* Add the "eNNN" suffix */
19043
 
        if( flag_exp || xtype==etEXP ){
19044
 
          *(bufpt++) = aDigits[infop->charset];
19045
 
          if( exp<0 ){
19046
 
            *(bufpt++) = '-'; exp = -exp;
19047
 
          }else{
19048
 
            *(bufpt++) = '+';
19049
 
          }
19050
 
          if( exp>=100 ){
19051
 
            *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
19052
 
            exp %= 100;
19053
 
          }
19054
 
          *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
19055
 
          *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
19056
 
        }
19057
 
        *bufpt = 0;
19058
 
 
19059
 
        /* The converted number is in buf[] and zero terminated. Output it.
19060
 
        ** Note that the number is in the usual order, not reversed as with
19061
 
        ** integer conversions. */
19062
 
        length = (int)(bufpt-buf);
19063
 
        bufpt = buf;
19064
 
 
19065
 
        /* Special case:  Add leading zeros if the flag_zeropad flag is
19066
 
        ** set and we are not left justified */
19067
 
        if( flag_zeropad && !flag_leftjustify && length < width){
19068
 
          int i;
19069
 
          int nPad = width - length;
19070
 
          for(i=width; i>=nPad; i--){
19071
 
            bufpt[i] = bufpt[i-nPad];
19072
 
          }
19073
 
          i = prefix!=0;
19074
 
          while( nPad-- ) bufpt[i++] = '0';
19075
 
          length = width;
19076
 
        }
19077
 
#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
19078
 
        break;
19079
 
      case etSIZE:
19080
 
        *(va_arg(ap,int*)) = pAccum->nChar;
19081
 
        length = width = 0;
19082
 
        break;
19083
 
      case etPERCENT:
19084
 
        buf[0] = '%';
19085
 
        bufpt = buf;
19086
 
        length = 1;
19087
 
        break;
19088
 
      case etCHARX:
19089
 
        c = va_arg(ap,int);
19090
 
        buf[0] = (char)c;
19091
 
        if( precision>=0 ){
19092
 
          for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
19093
 
          length = precision;
19094
 
        }else{
19095
 
          length =1;
19096
 
        }
19097
 
        bufpt = buf;
19098
 
        break;
19099
 
      case etSTRING:
19100
 
      case etDYNSTRING:
19101
 
        bufpt = va_arg(ap,char*);
19102
 
        if( bufpt==0 ){
19103
 
          bufpt = "";
19104
 
        }else if( xtype==etDYNSTRING ){
19105
 
          zExtra = bufpt;
19106
 
        }
19107
 
        if( precision>=0 ){
19108
 
          for(length=0; length<precision && bufpt[length]; length++){}
19109
 
        }else{
19110
 
          length = sqlite3Strlen30(bufpt);
19111
 
        }
19112
 
        break;
19113
 
      case etSQLESCAPE:
19114
 
      case etSQLESCAPE2:
19115
 
      case etSQLESCAPE3: {
19116
 
        int i, j, k, n, isnull;
19117
 
        int needQuote;
19118
 
        char ch;
19119
 
        char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
19120
 
        char *escarg = va_arg(ap,char*);
19121
 
        isnull = escarg==0;
19122
 
        if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
19123
 
        k = precision;
19124
 
        for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
19125
 
          if( ch==q )  n++;
19126
 
        }
19127
 
        needQuote = !isnull && xtype==etSQLESCAPE2;
19128
 
        n += i + 1 + needQuote*2;
19129
 
        if( n>etBUFSIZE ){
19130
 
          bufpt = zExtra = sqlite3Malloc( n );
19131
 
          if( bufpt==0 ){
19132
 
            pAccum->mallocFailed = 1;
19133
 
            return;
19134
 
          }
19135
 
        }else{
19136
 
          bufpt = buf;
19137
 
        }
19138
 
        j = 0;
19139
 
        if( needQuote ) bufpt[j++] = q;
19140
 
        k = i;
19141
 
        for(i=0; i<k; i++){
19142
 
          bufpt[j++] = ch = escarg[i];
19143
 
          if( ch==q ) bufpt[j++] = ch;
19144
 
        }
19145
 
        if( needQuote ) bufpt[j++] = q;
19146
 
        bufpt[j] = 0;
19147
 
        length = j;
19148
 
        /* The precision in %q and %Q means how many input characters to
19149
 
        ** consume, not the length of the output...
19150
 
        ** if( precision>=0 && precision<length ) length = precision; */
19151
 
        break;
19152
 
      }
19153
 
      case etTOKEN: {
19154
 
        Token *pToken = va_arg(ap, Token*);
19155
 
        if( pToken ){
19156
 
          sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
19157
 
        }
19158
 
        length = width = 0;
19159
 
        break;
19160
 
      }
19161
 
      case etSRCLIST: {
19162
 
        SrcList *pSrc = va_arg(ap, SrcList*);
19163
 
        int k = va_arg(ap, int);
19164
 
        struct SrcList_item *pItem = &pSrc->a[k];
19165
 
        assert( k>=0 && k<pSrc->nSrc );
19166
 
        if( pItem->zDatabase ){
19167
 
          sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
19168
 
          sqlite3StrAccumAppend(pAccum, ".", 1);
19169
 
        }
19170
 
        sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
19171
 
        length = width = 0;
19172
 
        break;
19173
 
      }
19174
 
      default: {
19175
 
        assert( xtype==etINVALID );
19176
 
        return;
19177
 
      }
19178
 
    }/* End switch over the format type */
19179
 
    /*
19180
 
    ** The text of the conversion is pointed to by "bufpt" and is
19181
 
    ** "length" characters long.  The field width is "width".  Do
19182
 
    ** the output.
19183
 
    */
19184
 
    if( !flag_leftjustify ){
19185
 
      register int nspace;
19186
 
      nspace = width-length;
19187
 
      if( nspace>0 ){
19188
 
        appendSpace(pAccum, nspace);
19189
 
      }
19190
 
    }
19191
 
    if( length>0 ){
19192
 
      sqlite3StrAccumAppend(pAccum, bufpt, length);
19193
 
    }
19194
 
    if( flag_leftjustify ){
19195
 
      register int nspace;
19196
 
      nspace = width-length;
19197
 
      if( nspace>0 ){
19198
 
        appendSpace(pAccum, nspace);
19199
 
      }
19200
 
    }
19201
 
    if( zExtra ){
19202
 
      sqlite3_free(zExtra);
19203
 
    }
19204
 
  }/* End for loop over the format string */
19205
 
} /* End of function */
19206
 
 
19207
 
/*
19208
 
** Append N bytes of text from z to the StrAccum object.
19209
 
*/
19210
 
SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
19211
 
  assert( z!=0 || N==0 );
19212
 
  if( p->tooBig | p->mallocFailed ){
19213
 
    testcase(p->tooBig);
19214
 
    testcase(p->mallocFailed);
19215
 
    return;
19216
 
  }
19217
 
  if( N<0 ){
19218
 
    N = sqlite3Strlen30(z);
19219
 
  }
19220
 
  if( N==0 || NEVER(z==0) ){
19221
 
    return;
19222
 
  }
19223
 
  if( p->nChar+N >= p->nAlloc ){
19224
 
    char *zNew;
19225
 
    if( !p->useMalloc ){
19226
 
      p->tooBig = 1;
19227
 
      N = p->nAlloc - p->nChar - 1;
19228
 
      if( N<=0 ){
19229
 
        return;
19230
 
      }
19231
 
    }else{
19232
 
      char *zOld = (p->zText==p->zBase ? 0 : p->zText);
19233
 
      i64 szNew = p->nChar;
19234
 
      szNew += N + 1;
19235
 
      if( szNew > p->mxAlloc ){
19236
 
        sqlite3StrAccumReset(p);
19237
 
        p->tooBig = 1;
19238
 
        return;
19239
 
      }else{
19240
 
        p->nAlloc = (int)szNew;
19241
 
      }
19242
 
      if( p->useMalloc==1 ){
19243
 
        zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
19244
 
      }else{
19245
 
        zNew = sqlite3_realloc(zOld, p->nAlloc);
19246
 
      }
19247
 
      if( zNew ){
19248
 
        if( zOld==0 ) memcpy(zNew, p->zText, p->nChar);
19249
 
        p->zText = zNew;
19250
 
      }else{
19251
 
        p->mallocFailed = 1;
19252
 
        sqlite3StrAccumReset(p);
19253
 
        return;
19254
 
      }
19255
 
    }
19256
 
  }
19257
 
  memcpy(&p->zText[p->nChar], z, N);
19258
 
  p->nChar += N;
19259
 
}
19260
 
 
19261
 
/*
19262
 
** Finish off a string by making sure it is zero-terminated.
19263
 
** Return a pointer to the resulting string.  Return a NULL
19264
 
** pointer if any kind of error was encountered.
19265
 
*/
19266
 
SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
19267
 
  if( p->zText ){
19268
 
    p->zText[p->nChar] = 0;
19269
 
    if( p->useMalloc && p->zText==p->zBase ){
19270
 
      if( p->useMalloc==1 ){
19271
 
        p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
19272
 
      }else{
19273
 
        p->zText = sqlite3_malloc(p->nChar+1);
19274
 
      }
19275
 
      if( p->zText ){
19276
 
        memcpy(p->zText, p->zBase, p->nChar+1);
19277
 
      }else{
19278
 
        p->mallocFailed = 1;
19279
 
      }
19280
 
    }
19281
 
  }
19282
 
  return p->zText;
19283
 
}
19284
 
 
19285
 
/*
19286
 
** Reset an StrAccum string.  Reclaim all malloced memory.
19287
 
*/
19288
 
SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
19289
 
  if( p->zText!=p->zBase ){
19290
 
    if( p->useMalloc==1 ){
19291
 
      sqlite3DbFree(p->db, p->zText);
19292
 
    }else{
19293
 
      sqlite3_free(p->zText);
19294
 
    }
19295
 
  }
19296
 
  p->zText = 0;
19297
 
}
19298
 
 
19299
 
/*
19300
 
** Initialize a string accumulator
19301
 
*/
19302
 
SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
19303
 
  p->zText = p->zBase = zBase;
19304
 
  p->db = 0;
19305
 
  p->nChar = 0;
19306
 
  p->nAlloc = n;
19307
 
  p->mxAlloc = mx;
19308
 
  p->useMalloc = 1;
19309
 
  p->tooBig = 0;
19310
 
  p->mallocFailed = 0;
19311
 
}
19312
 
 
19313
 
/*
19314
 
** Print into memory obtained from sqliteMalloc().  Use the internal
19315
 
** %-conversion extensions.
19316
 
*/
19317
 
SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
19318
 
  char *z;
19319
 
  char zBase[SQLITE_PRINT_BUF_SIZE];
19320
 
  StrAccum acc;
19321
 
  assert( db!=0 );
19322
 
  sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
19323
 
                      db->aLimit[SQLITE_LIMIT_LENGTH]);
19324
 
  acc.db = db;
19325
 
  sqlite3VXPrintf(&acc, 1, zFormat, ap);
19326
 
  z = sqlite3StrAccumFinish(&acc);
19327
 
  if( acc.mallocFailed ){
19328
 
    db->mallocFailed = 1;
19329
 
  }
19330
 
  return z;
19331
 
}
19332
 
 
19333
 
/*
19334
 
** Print into memory obtained from sqliteMalloc().  Use the internal
19335
 
** %-conversion extensions.
19336
 
*/
19337
 
SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
19338
 
  va_list ap;
19339
 
  char *z;
19340
 
  va_start(ap, zFormat);
19341
 
  z = sqlite3VMPrintf(db, zFormat, ap);
19342
 
  va_end(ap);
19343
 
  return z;
19344
 
}
19345
 
 
19346
 
/*
19347
 
** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
19348
 
** the string and before returnning.  This routine is intended to be used
19349
 
** to modify an existing string.  For example:
19350
 
**
19351
 
**       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
19352
 
**
19353
 
*/
19354
 
SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
19355
 
  va_list ap;
19356
 
  char *z;
19357
 
  va_start(ap, zFormat);
19358
 
  z = sqlite3VMPrintf(db, zFormat, ap);
19359
 
  va_end(ap);
19360
 
  sqlite3DbFree(db, zStr);
19361
 
  return z;
19362
 
}
19363
 
 
19364
 
/*
19365
 
** Print into memory obtained from sqlite3_malloc().  Omit the internal
19366
 
** %-conversion extensions.
19367
 
*/
19368
 
SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
19369
 
  char *z;
19370
 
  char zBase[SQLITE_PRINT_BUF_SIZE];
19371
 
  StrAccum acc;
19372
 
#ifndef SQLITE_OMIT_AUTOINIT
19373
 
  if( sqlite3_initialize() ) return 0;
19374
 
#endif
19375
 
  sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
19376
 
  acc.useMalloc = 2;
19377
 
  sqlite3VXPrintf(&acc, 0, zFormat, ap);
19378
 
  z = sqlite3StrAccumFinish(&acc);
19379
 
  return z;
19380
 
}
19381
 
 
19382
 
/*
19383
 
** Print into memory obtained from sqlite3_malloc()().  Omit the internal
19384
 
** %-conversion extensions.
19385
 
*/
19386
 
SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
19387
 
  va_list ap;
19388
 
  char *z;
19389
 
#ifndef SQLITE_OMIT_AUTOINIT
19390
 
  if( sqlite3_initialize() ) return 0;
19391
 
#endif
19392
 
  va_start(ap, zFormat);
19393
 
  z = sqlite3_vmprintf(zFormat, ap);
19394
 
  va_end(ap);
19395
 
  return z;
19396
 
}
19397
 
 
19398
 
/*
19399
 
** sqlite3_snprintf() works like snprintf() except that it ignores the
19400
 
** current locale settings.  This is important for SQLite because we
19401
 
** are not able to use a "," as the decimal point in place of "." as
19402
 
** specified by some locales.
19403
 
**
19404
 
** Oops:  The first two arguments of sqlite3_snprintf() are backwards
19405
 
** from the snprintf() standard.  Unfortunately, it is too late to change
19406
 
** this without breaking compatibility, so we just have to live with the
19407
 
** mistake.
19408
 
**
19409
 
** sqlite3_vsnprintf() is the varargs version.
19410
 
*/
19411
 
SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
19412
 
  StrAccum acc;
19413
 
  if( n<=0 ) return zBuf;
19414
 
  sqlite3StrAccumInit(&acc, zBuf, n, 0);
19415
 
  acc.useMalloc = 0;
19416
 
  sqlite3VXPrintf(&acc, 0, zFormat, ap);
19417
 
  return sqlite3StrAccumFinish(&acc);
19418
 
}
19419
 
SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
19420
 
  char *z;
19421
 
  va_list ap;
19422
 
  va_start(ap,zFormat);
19423
 
  z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
19424
 
  va_end(ap);
19425
 
  return z;
19426
 
}
19427
 
 
19428
 
/*
19429
 
** This is the routine that actually formats the sqlite3_log() message.
19430
 
** We house it in a separate routine from sqlite3_log() to avoid using
19431
 
** stack space on small-stack systems when logging is disabled.
19432
 
**
19433
 
** sqlite3_log() must render into a static buffer.  It cannot dynamically
19434
 
** allocate memory because it might be called while the memory allocator
19435
 
** mutex is held.
19436
 
*/
19437
 
static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
19438
 
  StrAccum acc;                          /* String accumulator */
19439
 
  char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
19440
 
 
19441
 
  sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
19442
 
  acc.useMalloc = 0;
19443
 
  sqlite3VXPrintf(&acc, 0, zFormat, ap);
19444
 
  sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
19445
 
                           sqlite3StrAccumFinish(&acc));
19446
 
}
19447
 
 
19448
 
/*
19449
 
** Format and write a message to the log if logging is enabled.
19450
 
*/
19451
 
SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
19452
 
  va_list ap;                             /* Vararg list */
19453
 
  if( sqlite3GlobalConfig.xLog ){
19454
 
    va_start(ap, zFormat);
19455
 
    renderLogMsg(iErrCode, zFormat, ap);
19456
 
    va_end(ap);
19457
 
  }
19458
 
}
19459
 
 
19460
 
#if defined(SQLITE_DEBUG)
19461
 
/*
19462
 
** A version of printf() that understands %lld.  Used for debugging.
19463
 
** The printf() built into some versions of windows does not understand %lld
19464
 
** and segfaults if you give it a long long int.
19465
 
*/
19466
 
SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
19467
 
  va_list ap;
19468
 
  StrAccum acc;
19469
 
  char zBuf[500];
19470
 
  sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
19471
 
  acc.useMalloc = 0;
19472
 
  va_start(ap,zFormat);
19473
 
  sqlite3VXPrintf(&acc, 0, zFormat, ap);
19474
 
  va_end(ap);
19475
 
  sqlite3StrAccumFinish(&acc);
19476
 
  fprintf(stdout,"%s", zBuf);
19477
 
  fflush(stdout);
19478
 
}
19479
 
#endif
19480
 
 
19481
 
#ifndef SQLITE_OMIT_TRACE
19482
 
/*
19483
 
** variable-argument wrapper around sqlite3VXPrintf().
19484
 
*/
19485
 
SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
19486
 
  va_list ap;
19487
 
  va_start(ap,zFormat);
19488
 
  sqlite3VXPrintf(p, 1, zFormat, ap);
19489
 
  va_end(ap);
19490
 
}
19491
 
#endif
19492
 
 
19493
 
/************** End of printf.c **********************************************/
19494
 
/************** Begin file random.c ******************************************/
19495
 
/*
19496
 
** 2001 September 15
19497
 
**
19498
 
** The author disclaims copyright to this source code.  In place of
19499
 
** a legal notice, here is a blessing:
19500
 
**
19501
 
**    May you do good and not evil.
19502
 
**    May you find forgiveness for yourself and forgive others.
19503
 
**    May you share freely, never taking more than you give.
19504
 
**
19505
 
*************************************************************************
19506
 
** This file contains code to implement a pseudo-random number
19507
 
** generator (PRNG) for SQLite.
19508
 
**
19509
 
** Random numbers are used by some of the database backends in order
19510
 
** to generate random integer keys for tables or random filenames.
19511
 
*/
19512
 
 
19513
 
 
19514
 
/* All threads share a single random number generator.
19515
 
** This structure is the current state of the generator.
19516
 
*/
19517
 
static SQLITE_WSD struct sqlite3PrngType {
19518
 
  unsigned char isInit;          /* True if initialized */
19519
 
  unsigned char i, j;            /* State variables */
19520
 
  unsigned char s[256];          /* State variables */
19521
 
} sqlite3Prng;
19522
 
 
19523
 
/*
19524
 
** Get a single 8-bit random value from the RC4 PRNG.  The Mutex
19525
 
** must be held while executing this routine.
19526
 
**
19527
 
** Why not just use a library random generator like lrand48() for this?
19528
 
** Because the OP_NewRowid opcode in the VDBE depends on having a very
19529
 
** good source of random numbers.  The lrand48() library function may
19530
 
** well be good enough.  But maybe not.  Or maybe lrand48() has some
19531
 
** subtle problems on some systems that could cause problems.  It is hard
19532
 
** to know.  To minimize the risk of problems due to bad lrand48()
19533
 
** implementations, SQLite uses this random number generator based
19534
 
** on RC4, which we know works very well.
19535
 
**
19536
 
** (Later):  Actually, OP_NewRowid does not depend on a good source of
19537
 
** randomness any more.  But we will leave this code in all the same.
19538
 
*/
19539
 
static u8 randomByte(void){
19540
 
  unsigned char t;
19541
 
 
19542
 
 
19543
 
  /* The "wsdPrng" macro will resolve to the pseudo-random number generator
19544
 
  ** state vector.  If writable static data is unsupported on the target,
19545
 
  ** we have to locate the state vector at run-time.  In the more common
19546
 
  ** case where writable static data is supported, wsdPrng can refer directly
19547
 
  ** to the "sqlite3Prng" state vector declared above.
19548
 
  */
19549
 
#ifdef SQLITE_OMIT_WSD
19550
 
  struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
19551
 
# define wsdPrng p[0]
19552
 
#else
19553
 
# define wsdPrng sqlite3Prng
19554
 
#endif
19555
 
 
19556
 
 
19557
 
  /* Initialize the state of the random number generator once,
19558
 
  ** the first time this routine is called.  The seed value does
19559
 
  ** not need to contain a lot of randomness since we are not
19560
 
  ** trying to do secure encryption or anything like that...
19561
 
  **
19562
 
  ** Nothing in this file or anywhere else in SQLite does any kind of
19563
 
  ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
19564
 
  ** number generator) not as an encryption device.
19565
 
  */
19566
 
  if( !wsdPrng.isInit ){
19567
 
    int i;
19568
 
    char k[256];
19569
 
    wsdPrng.j = 0;
19570
 
    wsdPrng.i = 0;
19571
 
    sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
19572
 
    for(i=0; i<256; i++){
19573
 
      wsdPrng.s[i] = (u8)i;
19574
 
    }
19575
 
    for(i=0; i<256; i++){
19576
 
      wsdPrng.j += wsdPrng.s[i] + k[i];
19577
 
      t = wsdPrng.s[wsdPrng.j];
19578
 
      wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
19579
 
      wsdPrng.s[i] = t;
19580
 
    }
19581
 
    wsdPrng.isInit = 1;
19582
 
  }
19583
 
 
19584
 
  /* Generate and return single random byte
19585
 
  */
19586
 
  wsdPrng.i++;
19587
 
  t = wsdPrng.s[wsdPrng.i];
19588
 
  wsdPrng.j += t;
19589
 
  wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
19590
 
  wsdPrng.s[wsdPrng.j] = t;
19591
 
  t += wsdPrng.s[wsdPrng.i];
19592
 
  return wsdPrng.s[t];
19593
 
}
19594
 
 
19595
 
/*
19596
 
** Return N random bytes.
19597
 
*/
19598
 
SQLITE_API void sqlite3_randomness(int N, void *pBuf){
19599
 
  unsigned char *zBuf = pBuf;
19600
 
#if SQLITE_THREADSAFE
19601
 
  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
19602
 
#endif
19603
 
  sqlite3_mutex_enter(mutex);
19604
 
  while( N-- ){
19605
 
    *(zBuf++) = randomByte();
19606
 
  }
19607
 
  sqlite3_mutex_leave(mutex);
19608
 
}
19609
 
 
19610
 
#ifndef SQLITE_OMIT_BUILTIN_TEST
19611
 
/*
19612
 
** For testing purposes, we sometimes want to preserve the state of
19613
 
** PRNG and restore the PRNG to its saved state at a later time, or
19614
 
** to reset the PRNG to its initial state.  These routines accomplish
19615
 
** those tasks.
19616
 
**
19617
 
** The sqlite3_test_control() interface calls these routines to
19618
 
** control the PRNG.
19619
 
*/
19620
 
static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
19621
 
SQLITE_PRIVATE void sqlite3PrngSaveState(void){
19622
 
  memcpy(
19623
 
    &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
19624
 
    &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
19625
 
    sizeof(sqlite3Prng)
19626
 
  );
19627
 
}
19628
 
SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
19629
 
  memcpy(
19630
 
    &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
19631
 
    &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
19632
 
    sizeof(sqlite3Prng)
19633
 
  );
19634
 
}
19635
 
SQLITE_PRIVATE void sqlite3PrngResetState(void){
19636
 
  GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
19637
 
}
19638
 
#endif /* SQLITE_OMIT_BUILTIN_TEST */
19639
 
 
19640
 
/************** End of random.c **********************************************/
19641
 
/************** Begin file utf.c *********************************************/
19642
 
/*
19643
 
** 2004 April 13
19644
 
**
19645
 
** The author disclaims copyright to this source code.  In place of
19646
 
** a legal notice, here is a blessing:
19647
 
**
19648
 
**    May you do good and not evil.
19649
 
**    May you find forgiveness for yourself and forgive others.
19650
 
**    May you share freely, never taking more than you give.
19651
 
**
19652
 
*************************************************************************
19653
 
** This file contains routines used to translate between UTF-8, 
19654
 
** UTF-16, UTF-16BE, and UTF-16LE.
19655
 
**
19656
 
** Notes on UTF-8:
19657
 
**
19658
 
**   Byte-0    Byte-1    Byte-2    Byte-3    Value
19659
 
**  0xxxxxxx                                 00000000 00000000 0xxxxxxx
19660
 
**  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
19661
 
**  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
19662
 
**  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
19663
 
**
19664
 
**
19665
 
** Notes on UTF-16:  (with wwww+1==uuuuu)
19666
 
**
19667
 
**      Word-0               Word-1          Value
19668
 
**  110110ww wwzzzzyy   110111yy yyxxxxxx    000uuuuu zzzzyyyy yyxxxxxx
19669
 
**  zzzzyyyy yyxxxxxx                        00000000 zzzzyyyy yyxxxxxx
19670
 
**
19671
 
**
19672
 
** BOM or Byte Order Mark:
19673
 
**     0xff 0xfe   little-endian utf-16 follows
19674
 
**     0xfe 0xff   big-endian utf-16 follows
19675
 
**
19676
 
*/
19677
 
 
19678
 
#ifndef SQLITE_AMALGAMATION
19679
 
/*
19680
 
** The following constant value is used by the SQLITE_BIGENDIAN and
19681
 
** SQLITE_LITTLEENDIAN macros.
19682
 
*/
19683
 
SQLITE_PRIVATE const int sqlite3one = 1;
19684
 
#endif /* SQLITE_AMALGAMATION */
19685
 
 
19686
 
/*
19687
 
** This lookup table is used to help decode the first byte of
19688
 
** a multi-byte UTF8 character.
19689
 
*/
19690
 
static const unsigned char sqlite3Utf8Trans1[] = {
19691
 
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
19692
 
  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
19693
 
  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
19694
 
  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
19695
 
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
19696
 
  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
19697
 
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
19698
 
  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
19699
 
};
19700
 
 
19701
 
 
19702
 
#define WRITE_UTF8(zOut, c) {                          \
19703
 
  if( c<0x00080 ){                                     \
19704
 
    *zOut++ = (u8)(c&0xFF);                            \
19705
 
  }                                                    \
19706
 
  else if( c<0x00800 ){                                \
19707
 
    *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
19708
 
    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
19709
 
  }                                                    \
19710
 
  else if( c<0x10000 ){                                \
19711
 
    *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
19712
 
    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
19713
 
    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
19714
 
  }else{                                               \
19715
 
    *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
19716
 
    *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
19717
 
    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
19718
 
    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
19719
 
  }                                                    \
19720
 
}
19721
 
 
19722
 
#define WRITE_UTF16LE(zOut, c) {                                    \
19723
 
  if( c<=0xFFFF ){                                                  \
19724
 
    *zOut++ = (u8)(c&0x00FF);                                       \
19725
 
    *zOut++ = (u8)((c>>8)&0x00FF);                                  \
19726
 
  }else{                                                            \
19727
 
    *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
19728
 
    *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
19729
 
    *zOut++ = (u8)(c&0x00FF);                                       \
19730
 
    *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
19731
 
  }                                                                 \
19732
 
}
19733
 
 
19734
 
#define WRITE_UTF16BE(zOut, c) {                                    \
19735
 
  if( c<=0xFFFF ){                                                  \
19736
 
    *zOut++ = (u8)((c>>8)&0x00FF);                                  \
19737
 
    *zOut++ = (u8)(c&0x00FF);                                       \
19738
 
  }else{                                                            \
19739
 
    *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
19740
 
    *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
19741
 
    *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
19742
 
    *zOut++ = (u8)(c&0x00FF);                                       \
19743
 
  }                                                                 \
19744
 
}
19745
 
 
19746
 
#define READ_UTF16LE(zIn, TERM, c){                                   \
19747
 
  c = (*zIn++);                                                       \
19748
 
  c += ((*zIn++)<<8);                                                 \
19749
 
  if( c>=0xD800 && c<0xE000 && TERM ){                                \
19750
 
    int c2 = (*zIn++);                                                \
19751
 
    c2 += ((*zIn++)<<8);                                              \
19752
 
    c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
19753
 
  }                                                                   \
19754
 
}
19755
 
 
19756
 
#define READ_UTF16BE(zIn, TERM, c){                                   \
19757
 
  c = ((*zIn++)<<8);                                                  \
19758
 
  c += (*zIn++);                                                      \
19759
 
  if( c>=0xD800 && c<0xE000 && TERM ){                                \
19760
 
    int c2 = ((*zIn++)<<8);                                           \
19761
 
    c2 += (*zIn++);                                                   \
19762
 
    c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
19763
 
  }                                                                   \
19764
 
}
19765
 
 
19766
 
/*
19767
 
** Translate a single UTF-8 character.  Return the unicode value.
19768
 
**
19769
 
** During translation, assume that the byte that zTerm points
19770
 
** is a 0x00.
19771
 
**
19772
 
** Write a pointer to the next unread byte back into *pzNext.
19773
 
**
19774
 
** Notes On Invalid UTF-8:
19775
 
**
19776
 
**  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
19777
 
**     be encoded as a multi-byte character.  Any multi-byte character that
19778
 
**     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
19779
 
**
19780
 
**  *  This routine never allows a UTF16 surrogate value to be encoded.
19781
 
**     If a multi-byte character attempts to encode a value between
19782
 
**     0xd800 and 0xe000 then it is rendered as 0xfffd.
19783
 
**
19784
 
**  *  Bytes in the range of 0x80 through 0xbf which occur as the first
19785
 
**     byte of a character are interpreted as single-byte characters
19786
 
**     and rendered as themselves even though they are technically
19787
 
**     invalid characters.
19788
 
**
19789
 
**  *  This routine accepts an infinite number of different UTF8 encodings
19790
 
**     for unicode values 0x80 and greater.  It do not change over-length
19791
 
**     encodings to 0xfffd as some systems recommend.
19792
 
*/
19793
 
#define READ_UTF8(zIn, zTerm, c)                           \
19794
 
  c = *(zIn++);                                            \
19795
 
  if( c>=0xc0 ){                                           \
19796
 
    c = sqlite3Utf8Trans1[c-0xc0];                         \
19797
 
    while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
19798
 
      c = (c<<6) + (0x3f & *(zIn++));                      \
19799
 
    }                                                      \
19800
 
    if( c<0x80                                             \
19801
 
        || (c&0xFFFFF800)==0xD800                          \
19802
 
        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
19803
 
  }
19804
 
SQLITE_PRIVATE int sqlite3Utf8Read(
19805
 
  const unsigned char *zIn,       /* First byte of UTF-8 character */
19806
 
  const unsigned char **pzNext    /* Write first byte past UTF-8 char here */
19807
 
){
19808
 
  unsigned int c;
19809
 
 
19810
 
  /* Same as READ_UTF8() above but without the zTerm parameter.
19811
 
  ** For this routine, we assume the UTF8 string is always zero-terminated.
19812
 
  */
19813
 
  c = *(zIn++);
19814
 
  if( c>=0xc0 ){
19815
 
    c = sqlite3Utf8Trans1[c-0xc0];
19816
 
    while( (*zIn & 0xc0)==0x80 ){
19817
 
      c = (c<<6) + (0x3f & *(zIn++));
19818
 
    }
19819
 
    if( c<0x80
19820
 
        || (c&0xFFFFF800)==0xD800
19821
 
        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
19822
 
  }
19823
 
  *pzNext = zIn;
19824
 
  return c;
19825
 
}
19826
 
 
19827
 
 
19828
 
 
19829
 
 
19830
 
/*
19831
 
** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
19832
 
** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
19833
 
*/ 
19834
 
/* #define TRANSLATE_TRACE 1 */
19835
 
 
19836
 
#ifndef SQLITE_OMIT_UTF16
19837
 
/*
19838
 
** This routine transforms the internal text encoding used by pMem to
19839
 
** desiredEnc. It is an error if the string is already of the desired
19840
 
** encoding, or if *pMem does not contain a string value.
19841
 
*/
19842
 
SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
19843
 
  int len;                    /* Maximum length of output string in bytes */
19844
 
  unsigned char *zOut;                  /* Output buffer */
19845
 
  unsigned char *zIn;                   /* Input iterator */
19846
 
  unsigned char *zTerm;                 /* End of input */
19847
 
  unsigned char *z;                     /* Output iterator */
19848
 
  unsigned int c;
19849
 
 
19850
 
  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
19851
 
  assert( pMem->flags&MEM_Str );
19852
 
  assert( pMem->enc!=desiredEnc );
19853
 
  assert( pMem->enc!=0 );
19854
 
  assert( pMem->n>=0 );
19855
 
 
19856
 
#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
19857
 
  {
19858
 
    char zBuf[100];
19859
 
    sqlite3VdbeMemPrettyPrint(pMem, zBuf);
19860
 
    fprintf(stderr, "INPUT:  %s\n", zBuf);
19861
 
  }
19862
 
#endif
19863
 
 
19864
 
  /* If the translation is between UTF-16 little and big endian, then 
19865
 
  ** all that is required is to swap the byte order. This case is handled
19866
 
  ** differently from the others.
19867
 
  */
19868
 
  if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
19869
 
    u8 temp;
19870
 
    int rc;
19871
 
    rc = sqlite3VdbeMemMakeWriteable(pMem);
19872
 
    if( rc!=SQLITE_OK ){
19873
 
      assert( rc==SQLITE_NOMEM );
19874
 
      return SQLITE_NOMEM;
19875
 
    }
19876
 
    zIn = (u8*)pMem->z;
19877
 
    zTerm = &zIn[pMem->n&~1];
19878
 
    while( zIn<zTerm ){
19879
 
      temp = *zIn;
19880
 
      *zIn = *(zIn+1);
19881
 
      zIn++;
19882
 
      *zIn++ = temp;
19883
 
    }
19884
 
    pMem->enc = desiredEnc;
19885
 
    goto translate_out;
19886
 
  }
19887
 
 
19888
 
  /* Set len to the maximum number of bytes required in the output buffer. */
19889
 
  if( desiredEnc==SQLITE_UTF8 ){
19890
 
    /* When converting from UTF-16, the maximum growth results from
19891
 
    ** translating a 2-byte character to a 4-byte UTF-8 character.
19892
 
    ** A single byte is required for the output string
19893
 
    ** nul-terminator.
19894
 
    */
19895
 
    pMem->n &= ~1;
19896
 
    len = pMem->n * 2 + 1;
19897
 
  }else{
19898
 
    /* When converting from UTF-8 to UTF-16 the maximum growth is caused
19899
 
    ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
19900
 
    ** character. Two bytes are required in the output buffer for the
19901
 
    ** nul-terminator.
19902
 
    */
19903
 
    len = pMem->n * 2 + 2;
19904
 
  }
19905
 
 
19906
 
  /* Set zIn to point at the start of the input buffer and zTerm to point 1
19907
 
  ** byte past the end.
19908
 
  **
19909
 
  ** Variable zOut is set to point at the output buffer, space obtained
19910
 
  ** from sqlite3_malloc().
19911
 
  */
19912
 
  zIn = (u8*)pMem->z;
19913
 
  zTerm = &zIn[pMem->n];
19914
 
  zOut = sqlite3DbMallocRaw(pMem->db, len);
19915
 
  if( !zOut ){
19916
 
    return SQLITE_NOMEM;
19917
 
  }
19918
 
  z = zOut;
19919
 
 
19920
 
  if( pMem->enc==SQLITE_UTF8 ){
19921
 
    if( desiredEnc==SQLITE_UTF16LE ){
19922
 
      /* UTF-8 -> UTF-16 Little-endian */
19923
 
      while( zIn<zTerm ){
19924
 
        /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
19925
 
        READ_UTF8(zIn, zTerm, c);
19926
 
        WRITE_UTF16LE(z, c);
19927
 
      }
19928
 
    }else{
19929
 
      assert( desiredEnc==SQLITE_UTF16BE );
19930
 
      /* UTF-8 -> UTF-16 Big-endian */
19931
 
      while( zIn<zTerm ){
19932
 
        /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
19933
 
        READ_UTF8(zIn, zTerm, c);
19934
 
        WRITE_UTF16BE(z, c);
19935
 
      }
19936
 
    }
19937
 
    pMem->n = (int)(z - zOut);
19938
 
    *z++ = 0;
19939
 
  }else{
19940
 
    assert( desiredEnc==SQLITE_UTF8 );
19941
 
    if( pMem->enc==SQLITE_UTF16LE ){
19942
 
      /* UTF-16 Little-endian -> UTF-8 */
19943
 
      while( zIn<zTerm ){
19944
 
        READ_UTF16LE(zIn, zIn<zTerm, c); 
19945
 
        WRITE_UTF8(z, c);
19946
 
      }
19947
 
    }else{
19948
 
      /* UTF-16 Big-endian -> UTF-8 */
19949
 
      while( zIn<zTerm ){
19950
 
        READ_UTF16BE(zIn, zIn<zTerm, c); 
19951
 
        WRITE_UTF8(z, c);
19952
 
      }
19953
 
    }
19954
 
    pMem->n = (int)(z - zOut);
19955
 
  }
19956
 
  *z = 0;
19957
 
  assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
19958
 
 
19959
 
  sqlite3VdbeMemRelease(pMem);
19960
 
  pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
19961
 
  pMem->enc = desiredEnc;
19962
 
  pMem->flags |= (MEM_Term|MEM_Dyn);
19963
 
  pMem->z = (char*)zOut;
19964
 
  pMem->zMalloc = pMem->z;
19965
 
 
19966
 
translate_out:
19967
 
#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
19968
 
  {
19969
 
    char zBuf[100];
19970
 
    sqlite3VdbeMemPrettyPrint(pMem, zBuf);
19971
 
    fprintf(stderr, "OUTPUT: %s\n", zBuf);
19972
 
  }
19973
 
#endif
19974
 
  return SQLITE_OK;
19975
 
}
19976
 
 
19977
 
/*
19978
 
** This routine checks for a byte-order mark at the beginning of the 
19979
 
** UTF-16 string stored in *pMem. If one is present, it is removed and
19980
 
** the encoding of the Mem adjusted. This routine does not do any
19981
 
** byte-swapping, it just sets Mem.enc appropriately.
19982
 
**
19983
 
** The allocation (static, dynamic etc.) and encoding of the Mem may be
19984
 
** changed by this function.
19985
 
*/
19986
 
SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
19987
 
  int rc = SQLITE_OK;
19988
 
  u8 bom = 0;
19989
 
 
19990
 
  assert( pMem->n>=0 );
19991
 
  if( pMem->n>1 ){
19992
 
    u8 b1 = *(u8 *)pMem->z;
19993
 
    u8 b2 = *(((u8 *)pMem->z) + 1);
19994
 
    if( b1==0xFE && b2==0xFF ){
19995
 
      bom = SQLITE_UTF16BE;
19996
 
    }
19997
 
    if( b1==0xFF && b2==0xFE ){
19998
 
      bom = SQLITE_UTF16LE;
19999
 
    }
20000
 
  }
20001
 
  
20002
 
  if( bom ){
20003
 
    rc = sqlite3VdbeMemMakeWriteable(pMem);
20004
 
    if( rc==SQLITE_OK ){
20005
 
      pMem->n -= 2;
20006
 
      memmove(pMem->z, &pMem->z[2], pMem->n);
20007
 
      pMem->z[pMem->n] = '\0';
20008
 
      pMem->z[pMem->n+1] = '\0';
20009
 
      pMem->flags |= MEM_Term;
20010
 
      pMem->enc = bom;
20011
 
    }
20012
 
  }
20013
 
  return rc;
20014
 
}
20015
 
#endif /* SQLITE_OMIT_UTF16 */
20016
 
 
20017
 
/*
20018
 
** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
20019
 
** return the number of unicode characters in pZ up to (but not including)
20020
 
** the first 0x00 byte. If nByte is not less than zero, return the
20021
 
** number of unicode characters in the first nByte of pZ (or up to 
20022
 
** the first 0x00, whichever comes first).
20023
 
*/
20024
 
SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
20025
 
  int r = 0;
20026
 
  const u8 *z = (const u8*)zIn;
20027
 
  const u8 *zTerm;
20028
 
  if( nByte>=0 ){
20029
 
    zTerm = &z[nByte];
20030
 
  }else{
20031
 
    zTerm = (const u8*)(-1);
20032
 
  }
20033
 
  assert( z<=zTerm );
20034
 
  while( *z!=0 && z<zTerm ){
20035
 
    SQLITE_SKIP_UTF8(z);
20036
 
    r++;
20037
 
  }
20038
 
  return r;
20039
 
}
20040
 
 
20041
 
/* This test function is not currently used by the automated test-suite. 
20042
 
** Hence it is only available in debug builds.
20043
 
*/
20044
 
#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
20045
 
/*
20046
 
** Translate UTF-8 to UTF-8.
20047
 
**
20048
 
** This has the effect of making sure that the string is well-formed
20049
 
** UTF-8.  Miscoded characters are removed.
20050
 
**
20051
 
** The translation is done in-place and aborted if the output
20052
 
** overruns the input.
20053
 
*/
20054
 
SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
20055
 
  unsigned char *zOut = zIn;
20056
 
  unsigned char *zStart = zIn;
20057
 
  u32 c;
20058
 
 
20059
 
  while( zIn[0] && zOut<=zIn ){
20060
 
    c = sqlite3Utf8Read(zIn, (const u8**)&zIn);
20061
 
    if( c!=0xfffd ){
20062
 
      WRITE_UTF8(zOut, c);
20063
 
    }
20064
 
  }
20065
 
  *zOut = 0;
20066
 
  return (int)(zOut - zStart);
20067
 
}
20068
 
#endif
20069
 
 
20070
 
#ifndef SQLITE_OMIT_UTF16
20071
 
/*
20072
 
** Convert a UTF-16 string in the native encoding into a UTF-8 string.
20073
 
** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
20074
 
** be freed by the calling function.
20075
 
**
20076
 
** NULL is returned if there is an allocation error.
20077
 
*/
20078
 
SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
20079
 
  Mem m;
20080
 
  memset(&m, 0, sizeof(m));
20081
 
  m.db = db;
20082
 
  sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
20083
 
  sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
20084
 
  if( db->mallocFailed ){
20085
 
    sqlite3VdbeMemRelease(&m);
20086
 
    m.z = 0;
20087
 
  }
20088
 
  assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
20089
 
  assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
20090
 
  assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
20091
 
  assert( m.z || db->mallocFailed );
20092
 
  return m.z;
20093
 
}
20094
 
 
20095
 
/*
20096
 
** Convert a UTF-8 string to the UTF-16 encoding specified by parameter
20097
 
** enc. A pointer to the new string is returned, and the value of *pnOut
20098
 
** is set to the length of the returned string in bytes. The call should
20099
 
** arrange to call sqlite3DbFree() on the returned pointer when it is
20100
 
** no longer required.
20101
 
** 
20102
 
** If a malloc failure occurs, NULL is returned and the db.mallocFailed
20103
 
** flag set.
20104
 
*/
20105
 
#ifdef SQLITE_ENABLE_STAT2
20106
 
SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
20107
 
  Mem m;
20108
 
  memset(&m, 0, sizeof(m));
20109
 
  m.db = db;
20110
 
  sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
20111
 
  if( sqlite3VdbeMemTranslate(&m, enc) ){
20112
 
    assert( db->mallocFailed );
20113
 
    return 0;
20114
 
  }
20115
 
  assert( m.z==m.zMalloc );
20116
 
  *pnOut = m.n;
20117
 
  return m.z;
20118
 
}
20119
 
#endif
20120
 
 
20121
 
/*
20122
 
** zIn is a UTF-16 encoded unicode string at least nChar characters long.
20123
 
** Return the number of bytes in the first nChar unicode characters
20124
 
** in pZ.  nChar must be non-negative.
20125
 
*/
20126
 
SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
20127
 
  int c;
20128
 
  unsigned char const *z = zIn;
20129
 
  int n = 0;
20130
 
  
20131
 
#ifdef SQLITE_BIGENDIAN
20132
 
    while( n<nChar ){
20133
 
                const int term = 1;
20134
 
      READ_UTF16BE(z, term, c);
20135
 
      n++;
20136
 
    }
20137
 
#else
20138
 
    while( n<nChar ){
20139
 
                const int term = 1;
20140
 
                READ_UTF16LE(z, term, c);
20141
 
                n++;
20142
 
    }
20143
 
#endif
20144
 
  return (int)(z-(unsigned char const *)zIn);
20145
 
}
20146
 
 
20147
 
#if defined(SQLITE_TEST)
20148
 
/*
20149
 
** This routine is called from the TCL test function "translate_selftest".
20150
 
** It checks that the primitives for serializing and deserializing
20151
 
** characters in each encoding are inverses of each other.
20152
 
*/
20153
 
SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
20154
 
  unsigned int i, t;
20155
 
  unsigned char zBuf[20];
20156
 
  unsigned char *z;
20157
 
  int n;
20158
 
  unsigned int c;
20159
 
 
20160
 
  for(i=0; i<0x00110000; i++){
20161
 
    z = zBuf;
20162
 
    WRITE_UTF8(z, i);
20163
 
    n = (int)(z-zBuf);
20164
 
    assert( n>0 && n<=4 );
20165
 
    z[0] = 0;
20166
 
    z = zBuf;
20167
 
    c = sqlite3Utf8Read(z, (const u8**)&z);
20168
 
    t = i;
20169
 
    if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
20170
 
    if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
20171
 
    assert( c==t );
20172
 
    assert( (z-zBuf)==n );
20173
 
  }
20174
 
  for(i=0; i<0x00110000; i++){
20175
 
    if( i>=0xD800 && i<0xE000 ) continue;
20176
 
    z = zBuf;
20177
 
    WRITE_UTF16LE(z, i);
20178
 
    n = (int)(z-zBuf);
20179
 
    assert( n>0 && n<=4 );
20180
 
    z[0] = 0;
20181
 
    z = zBuf;
20182
 
    READ_UTF16LE(z, 1, c);
20183
 
    assert( c==i );
20184
 
    assert( (z-zBuf)==n );
20185
 
  }
20186
 
  for(i=0; i<0x00110000; i++){
20187
 
    if( i>=0xD800 && i<0xE000 ) continue;
20188
 
    z = zBuf;
20189
 
    WRITE_UTF16BE(z, i);
20190
 
    n = (int)(z-zBuf);
20191
 
    assert( n>0 && n<=4 );
20192
 
    z[0] = 0;
20193
 
    z = zBuf;
20194
 
    READ_UTF16BE(z, 1, c);
20195
 
    assert( c==i );
20196
 
    assert( (z-zBuf)==n );
20197
 
  }
20198
 
}
20199
 
#endif /* SQLITE_TEST */
20200
 
#endif /* SQLITE_OMIT_UTF16 */
20201
 
 
20202
 
/************** End of utf.c *************************************************/
20203
 
/************** Begin file util.c ********************************************/
20204
 
/*
20205
 
** 2001 September 15
20206
 
**
20207
 
** The author disclaims copyright to this source code.  In place of
20208
 
** a legal notice, here is a blessing:
20209
 
**
20210
 
**    May you do good and not evil.
20211
 
**    May you find forgiveness for yourself and forgive others.
20212
 
**    May you share freely, never taking more than you give.
20213
 
**
20214
 
*************************************************************************
20215
 
** Utility functions used throughout sqlite.
20216
 
**
20217
 
** This file contains functions for allocating memory, comparing
20218
 
** strings, and stuff like that.
20219
 
**
20220
 
*/
20221
 
#ifdef SQLITE_HAVE_ISNAN
20222
 
# include <math.h>
20223
 
#endif
20224
 
 
20225
 
/*
20226
 
** Routine needed to support the testcase() macro.
20227
 
*/
20228
 
#ifdef SQLITE_COVERAGE_TEST
20229
 
SQLITE_PRIVATE void sqlite3Coverage(int x){
20230
 
  static unsigned dummy = 0;
20231
 
  dummy += (unsigned)x;
20232
 
}
20233
 
#endif
20234
 
 
20235
 
#ifndef SQLITE_OMIT_FLOATING_POINT
20236
 
/*
20237
 
** Return true if the floating point value is Not a Number (NaN).
20238
 
**
20239
 
** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
20240
 
** Otherwise, we have our own implementation that works on most systems.
20241
 
*/
20242
 
SQLITE_PRIVATE int sqlite3IsNaN(double x){
20243
 
  int rc;   /* The value return */
20244
 
#if !defined(SQLITE_HAVE_ISNAN)
20245
 
  /*
20246
 
  ** Systems that support the isnan() library function should probably
20247
 
  ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have
20248
 
  ** found that many systems do not have a working isnan() function so
20249
 
  ** this implementation is provided as an alternative.
20250
 
  **
20251
 
  ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
20252
 
  ** On the other hand, the use of -ffast-math comes with the following
20253
 
  ** warning:
20254
 
  **
20255
 
  **      This option [-ffast-math] should never be turned on by any
20256
 
  **      -O option since it can result in incorrect output for programs
20257
 
  **      which depend on an exact implementation of IEEE or ISO 
20258
 
  **      rules/specifications for math functions.
20259
 
  **
20260
 
  ** Under MSVC, this NaN test may fail if compiled with a floating-
20261
 
  ** point precision mode other than /fp:precise.  From the MSDN 
20262
 
  ** documentation:
20263
 
  **
20264
 
  **      The compiler [with /fp:precise] will properly handle comparisons 
20265
 
  **      involving NaN. For example, x != x evaluates to true if x is NaN 
20266
 
  **      ...
20267
 
  */
20268
 
#ifdef __FAST_MATH__
20269
 
# error SQLite will not work correctly with the -ffast-math option of GCC.
20270
 
#endif
20271
 
  volatile double y = x;
20272
 
  volatile double z = y;
20273
 
  rc = (y!=z);
20274
 
#else  /* if defined(SQLITE_HAVE_ISNAN) */
20275
 
  rc = isnan(x);
20276
 
#endif /* SQLITE_HAVE_ISNAN */
20277
 
  testcase( rc );
20278
 
  return rc;
20279
 
}
20280
 
#endif /* SQLITE_OMIT_FLOATING_POINT */
20281
 
 
20282
 
/*
20283
 
** Compute a string length that is limited to what can be stored in
20284
 
** lower 30 bits of a 32-bit signed integer.
20285
 
**
20286
 
** The value returned will never be negative.  Nor will it ever be greater
20287
 
** than the actual length of the string.  For very long strings (greater
20288
 
** than 1GiB) the value returned might be less than the true string length.
20289
 
*/
20290
 
SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
20291
 
  const char *z2 = z;
20292
 
  if( z==0 ) return 0;
20293
 
  while( *z2 ){ z2++; }
20294
 
  return 0x3fffffff & (int)(z2 - z);
20295
 
}
20296
 
 
20297
 
/*
20298
 
** Set the most recent error code and error string for the sqlite
20299
 
** handle "db". The error code is set to "err_code".
20300
 
**
20301
 
** If it is not NULL, string zFormat specifies the format of the
20302
 
** error string in the style of the printf functions: The following
20303
 
** format characters are allowed:
20304
 
**
20305
 
**      %s      Insert a string
20306
 
**      %z      A string that should be freed after use
20307
 
**      %d      Insert an integer
20308
 
**      %T      Insert a token
20309
 
**      %S      Insert the first element of a SrcList
20310
 
**
20311
 
** zFormat and any string tokens that follow it are assumed to be
20312
 
** encoded in UTF-8.
20313
 
**
20314
 
** To clear the most recent error for sqlite handle "db", sqlite3Error
20315
 
** should be called with err_code set to SQLITE_OK and zFormat set
20316
 
** to NULL.
20317
 
*/
20318
 
SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
20319
 
  if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
20320
 
    db->errCode = err_code;
20321
 
    if( zFormat ){
20322
 
      char *z;
20323
 
      va_list ap;
20324
 
      va_start(ap, zFormat);
20325
 
      z = sqlite3VMPrintf(db, zFormat, ap);
20326
 
      va_end(ap);
20327
 
      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
20328
 
    }else{
20329
 
      sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
20330
 
    }
20331
 
  }
20332
 
}
20333
 
 
20334
 
/*
20335
 
** Add an error message to pParse->zErrMsg and increment pParse->nErr.
20336
 
** The following formatting characters are allowed:
20337
 
**
20338
 
**      %s      Insert a string
20339
 
**      %z      A string that should be freed after use
20340
 
**      %d      Insert an integer
20341
 
**      %T      Insert a token
20342
 
**      %S      Insert the first element of a SrcList
20343
 
**
20344
 
** This function should be used to report any error that occurs whilst
20345
 
** compiling an SQL statement (i.e. within sqlite3_prepare()). The
20346
 
** last thing the sqlite3_prepare() function does is copy the error
20347
 
** stored by this function into the database handle using sqlite3Error().
20348
 
** Function sqlite3Error() should be used during statement execution
20349
 
** (sqlite3_step() etc.).
20350
 
*/
20351
 
SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
20352
 
  char *zMsg;
20353
 
  va_list ap;
20354
 
  sqlite3 *db = pParse->db;
20355
 
  va_start(ap, zFormat);
20356
 
  zMsg = sqlite3VMPrintf(db, zFormat, ap);
20357
 
  va_end(ap);
20358
 
  if( db->suppressErr ){
20359
 
    sqlite3DbFree(db, zMsg);
20360
 
  }else{
20361
 
    pParse->nErr++;
20362
 
    sqlite3DbFree(db, pParse->zErrMsg);
20363
 
    pParse->zErrMsg = zMsg;
20364
 
    pParse->rc = SQLITE_ERROR;
20365
 
  }
20366
 
}
20367
 
 
20368
 
/*
20369
 
** Convert an SQL-style quoted string into a normal string by removing
20370
 
** the quote characters.  The conversion is done in-place.  If the
20371
 
** input does not begin with a quote character, then this routine
20372
 
** is a no-op.
20373
 
**
20374
 
** The input string must be zero-terminated.  A new zero-terminator
20375
 
** is added to the dequoted string.
20376
 
**
20377
 
** The return value is -1 if no dequoting occurs or the length of the
20378
 
** dequoted string, exclusive of the zero terminator, if dequoting does
20379
 
** occur.
20380
 
**
20381
 
** 2002-Feb-14: This routine is extended to remove MS-Access style
20382
 
** brackets from around identifers.  For example:  "[a-b-c]" becomes
20383
 
** "a-b-c".
20384
 
*/
20385
 
SQLITE_PRIVATE int sqlite3Dequote(char *z){
20386
 
  char quote;
20387
 
  int i, j;
20388
 
  if( z==0 ) return -1;
20389
 
  quote = z[0];
20390
 
  switch( quote ){
20391
 
    case '\'':  break;
20392
 
    case '"':   break;
20393
 
    case '`':   break;                /* For MySQL compatibility */
20394
 
    case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
20395
 
    default:    return -1;
20396
 
  }
20397
 
  for(i=1, j=0; ALWAYS(z[i]); i++){
20398
 
    if( z[i]==quote ){
20399
 
      if( z[i+1]==quote ){
20400
 
        z[j++] = quote;
20401
 
        i++;
20402
 
      }else{
20403
 
        break;
20404
 
      }
20405
 
    }else{
20406
 
      z[j++] = z[i];
20407
 
    }
20408
 
  }
20409
 
  z[j] = 0;
20410
 
  return j;
20411
 
}
20412
 
 
20413
 
/* Convenient short-hand */
20414
 
#define UpperToLower sqlite3UpperToLower
20415
 
 
20416
 
/*
20417
 
** Some systems have stricmp().  Others have strcasecmp().  Because
20418
 
** there is no consistency, we will define our own.
20419
 
**
20420
 
** IMPLEMENTATION-OF: R-20522-24639 The sqlite3_strnicmp() API allows
20421
 
** applications and extensions to compare the contents of two buffers
20422
 
** containing UTF-8 strings in a case-independent fashion, using the same
20423
 
** definition of case independence that SQLite uses internally when
20424
 
** comparing identifiers.
20425
 
*/
20426
 
SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
20427
 
  register unsigned char *a, *b;
20428
 
  a = (unsigned char *)zLeft;
20429
 
  b = (unsigned char *)zRight;
20430
 
  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
20431
 
  return UpperToLower[*a] - UpperToLower[*b];
20432
 
}
20433
 
SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
20434
 
  register unsigned char *a, *b;
20435
 
  a = (unsigned char *)zLeft;
20436
 
  b = (unsigned char *)zRight;
20437
 
  while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
20438
 
  return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
20439
 
}
20440
 
 
20441
 
/*
20442
 
** The string z[] is an text representation of a real number.
20443
 
** Convert this string to a double and write it into *pResult.
20444
 
**
20445
 
** The string z[] is length bytes in length (bytes, not characters) and
20446
 
** uses the encoding enc.  The string is not necessarily zero-terminated.
20447
 
**
20448
 
** Return TRUE if the result is a valid real number (or integer) and FALSE
20449
 
** if the string is empty or contains extraneous text.  Valid numbers
20450
 
** are in one of these formats:
20451
 
**
20452
 
**    [+-]digits[E[+-]digits]
20453
 
**    [+-]digits.[digits][E[+-]digits]
20454
 
**    [+-].digits[E[+-]digits]
20455
 
**
20456
 
** Leading and trailing whitespace is ignored for the purpose of determining
20457
 
** validity.
20458
 
**
20459
 
** If some prefix of the input string is a valid number, this routine
20460
 
** returns FALSE but it still converts the prefix and writes the result
20461
 
** into *pResult.
20462
 
*/
20463
 
SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
20464
 
#ifndef SQLITE_OMIT_FLOATING_POINT
20465
 
  int incr = (enc==SQLITE_UTF8?1:2);
20466
 
  const char *zEnd = z + length;
20467
 
  /* sign * significand * (10 ^ (esign * exponent)) */
20468
 
  int sign = 1;    /* sign of significand */
20469
 
  i64 s = 0;       /* significand */
20470
 
  int d = 0;       /* adjust exponent for shifting decimal point */
20471
 
  int esign = 1;   /* sign of exponent */
20472
 
  int e = 0;       /* exponent */
20473
 
  int eValid = 1;  /* True exponent is either not used or is well-formed */
20474
 
  double result;
20475
 
  int nDigits = 0;
20476
 
 
20477
 
  *pResult = 0.0;   /* Default return value, in case of an error */
20478
 
 
20479
 
  if( enc==SQLITE_UTF16BE ) z++;
20480
 
 
20481
 
  /* skip leading spaces */
20482
 
  while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
20483
 
  if( z>=zEnd ) return 0;
20484
 
 
20485
 
  /* get sign of significand */
20486
 
  if( *z=='-' ){
20487
 
    sign = -1;
20488
 
    z+=incr;
20489
 
  }else if( *z=='+' ){
20490
 
    z+=incr;
20491
 
  }
20492
 
 
20493
 
  /* skip leading zeroes */
20494
 
  while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
20495
 
 
20496
 
  /* copy max significant digits to significand */
20497
 
  while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
20498
 
    s = s*10 + (*z - '0');
20499
 
    z+=incr, nDigits++;
20500
 
  }
20501
 
 
20502
 
  /* skip non-significant significand digits
20503
 
  ** (increase exponent by d to shift decimal left) */
20504
 
  while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
20505
 
  if( z>=zEnd ) goto do_atof_calc;
20506
 
 
20507
 
  /* if decimal point is present */
20508
 
  if( *z=='.' ){
20509
 
    z+=incr;
20510
 
    /* copy digits from after decimal to significand
20511
 
    ** (decrease exponent by d to shift decimal right) */
20512
 
    while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
20513
 
      s = s*10 + (*z - '0');
20514
 
      z+=incr, nDigits++, d--;
20515
 
    }
20516
 
    /* skip non-significant digits */
20517
 
    while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
20518
 
  }
20519
 
  if( z>=zEnd ) goto do_atof_calc;
20520
 
 
20521
 
  /* if exponent is present */
20522
 
  if( *z=='e' || *z=='E' ){
20523
 
    z+=incr;
20524
 
    eValid = 0;
20525
 
    if( z>=zEnd ) goto do_atof_calc;
20526
 
    /* get sign of exponent */
20527
 
    if( *z=='-' ){
20528
 
      esign = -1;
20529
 
      z+=incr;
20530
 
    }else if( *z=='+' ){
20531
 
      z+=incr;
20532
 
    }
20533
 
    /* copy digits to exponent */
20534
 
    while( z<zEnd && sqlite3Isdigit(*z) ){
20535
 
      e = e*10 + (*z - '0');
20536
 
      z+=incr;
20537
 
      eValid = 1;
20538
 
    }
20539
 
  }
20540
 
 
20541
 
  /* skip trailing spaces */
20542
 
  if( nDigits && eValid ){
20543
 
    while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
20544
 
  }
20545
 
 
20546
 
do_atof_calc:
20547
 
  /* adjust exponent by d, and update sign */
20548
 
  e = (e*esign) + d;
20549
 
  if( e<0 ) {
20550
 
    esign = -1;
20551
 
    e *= -1;
20552
 
  } else {
20553
 
    esign = 1;
20554
 
  }
20555
 
 
20556
 
  /* if 0 significand */
20557
 
  if( !s ) {
20558
 
    /* In the IEEE 754 standard, zero is signed.
20559
 
    ** Add the sign if we've seen at least one digit */
20560
 
    result = (sign<0 && nDigits) ? -(double)0 : (double)0;
20561
 
  } else {
20562
 
    /* attempt to reduce exponent */
20563
 
    if( esign>0 ){
20564
 
      while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
20565
 
    }else{
20566
 
      while( !(s%10) && e>0 ) e--,s/=10;
20567
 
    }
20568
 
 
20569
 
    /* adjust the sign of significand */
20570
 
    s = sign<0 ? -s : s;
20571
 
 
20572
 
    /* if exponent, scale significand as appropriate
20573
 
    ** and store in result. */
20574
 
    if( e ){
20575
 
      double scale = 1.0;
20576
 
      /* attempt to handle extremely small/large numbers better */
20577
 
      if( e>307 && e<342 ){
20578
 
        while( e%308 ) { scale *= 1.0e+1; e -= 1; }
20579
 
        if( esign<0 ){
20580
 
          result = s / scale;
20581
 
          result /= 1.0e+308;
20582
 
        }else{
20583
 
          result = s * scale;
20584
 
          result *= 1.0e+308;
20585
 
        }
20586
 
      }else{
20587
 
        /* 1.0e+22 is the largest power of 10 than can be 
20588
 
        ** represented exactly. */
20589
 
        while( e%22 ) { scale *= 1.0e+1; e -= 1; }
20590
 
        while( e>0 ) { scale *= 1.0e+22; e -= 22; }
20591
 
        if( esign<0 ){
20592
 
          result = s / scale;
20593
 
        }else{
20594
 
          result = s * scale;
20595
 
        }
20596
 
      }
20597
 
    } else {
20598
 
      result = (double)s;
20599
 
    }
20600
 
  }
20601
 
 
20602
 
  /* store the result */
20603
 
  *pResult = result;
20604
 
 
20605
 
  /* return true if number and no extra non-whitespace chracters after */
20606
 
  return z>=zEnd && nDigits>0 && eValid;
20607
 
#else
20608
 
  return !sqlite3Atoi64(z, pResult, length, enc);
20609
 
#endif /* SQLITE_OMIT_FLOATING_POINT */
20610
 
}
20611
 
 
20612
 
/*
20613
 
** Compare the 19-character string zNum against the text representation
20614
 
** value 2^63:  9223372036854775808.  Return negative, zero, or positive
20615
 
** if zNum is less than, equal to, or greater than the string.
20616
 
** Note that zNum must contain exactly 19 characters.
20617
 
**
20618
 
** Unlike memcmp() this routine is guaranteed to return the difference
20619
 
** in the values of the last digit if the only difference is in the
20620
 
** last digit.  So, for example,
20621
 
**
20622
 
**      compare2pow63("9223372036854775800", 1)
20623
 
**
20624
 
** will return -8.
20625
 
*/
20626
 
static int compare2pow63(const char *zNum, int incr){
20627
 
  int c = 0;
20628
 
  int i;
20629
 
                    /* 012345678901234567 */
20630
 
  const char *pow63 = "922337203685477580";
20631
 
  for(i=0; c==0 && i<18; i++){
20632
 
    c = (zNum[i*incr]-pow63[i])*10;
20633
 
  }
20634
 
  if( c==0 ){
20635
 
    c = zNum[18*incr] - '8';
20636
 
    testcase( c==(-1) );
20637
 
    testcase( c==0 );
20638
 
    testcase( c==(+1) );
20639
 
  }
20640
 
  return c;
20641
 
}
20642
 
 
20643
 
 
20644
 
/*
20645
 
** Convert zNum to a 64-bit signed integer.
20646
 
**
20647
 
** If the zNum value is representable as a 64-bit twos-complement 
20648
 
** integer, then write that value into *pNum and return 0.
20649
 
**
20650
 
** If zNum is exactly 9223372036854665808, return 2.  This special
20651
 
** case is broken out because while 9223372036854665808 cannot be a 
20652
 
** signed 64-bit integer, its negative -9223372036854665808 can be.
20653
 
**
20654
 
** If zNum is too big for a 64-bit integer and is not
20655
 
** 9223372036854665808 then return 1.
20656
 
**
20657
 
** length is the number of bytes in the string (bytes, not characters).
20658
 
** The string is not necessarily zero-terminated.  The encoding is
20659
 
** given by enc.
20660
 
*/
20661
 
SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
20662
 
  int incr = (enc==SQLITE_UTF8?1:2);
20663
 
  u64 u = 0;
20664
 
  int neg = 0; /* assume positive */
20665
 
  int i;
20666
 
  int c = 0;
20667
 
  const char *zStart;
20668
 
  const char *zEnd = zNum + length;
20669
 
  if( enc==SQLITE_UTF16BE ) zNum++;
20670
 
  while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
20671
 
  if( zNum<zEnd ){
20672
 
    if( *zNum=='-' ){
20673
 
      neg = 1;
20674
 
      zNum+=incr;
20675
 
    }else if( *zNum=='+' ){
20676
 
      zNum+=incr;
20677
 
    }
20678
 
  }
20679
 
  zStart = zNum;
20680
 
  while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
20681
 
  for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
20682
 
    u = u*10 + c - '0';
20683
 
  }
20684
 
  if( u>LARGEST_INT64 ){
20685
 
    *pNum = SMALLEST_INT64;
20686
 
  }else if( neg ){
20687
 
    *pNum = -(i64)u;
20688
 
  }else{
20689
 
    *pNum = (i64)u;
20690
 
  }
20691
 
  testcase( i==18 );
20692
 
  testcase( i==19 );
20693
 
  testcase( i==20 );
20694
 
  if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){
20695
 
    /* zNum is empty or contains non-numeric text or is longer
20696
 
    ** than 19 digits (thus guaranteeing that it is too large) */
20697
 
    return 1;
20698
 
  }else if( i<19*incr ){
20699
 
    /* Less than 19 digits, so we know that it fits in 64 bits */
20700
 
    assert( u<=LARGEST_INT64 );
20701
 
    return 0;
20702
 
  }else{
20703
 
    /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
20704
 
    c = compare2pow63(zNum, incr);
20705
 
    if( c<0 ){
20706
 
      /* zNum is less than 9223372036854775808 so it fits */
20707
 
      assert( u<=LARGEST_INT64 );
20708
 
      return 0;
20709
 
    }else if( c>0 ){
20710
 
      /* zNum is greater than 9223372036854775808 so it overflows */
20711
 
      return 1;
20712
 
    }else{
20713
 
      /* zNum is exactly 9223372036854775808.  Fits if negative.  The
20714
 
      ** special case 2 overflow if positive */
20715
 
      assert( u-1==LARGEST_INT64 );
20716
 
      assert( (*pNum)==SMALLEST_INT64 );
20717
 
      return neg ? 0 : 2;
20718
 
    }
20719
 
  }
20720
 
}
20721
 
 
20722
 
/*
20723
 
** If zNum represents an integer that will fit in 32-bits, then set
20724
 
** *pValue to that integer and return true.  Otherwise return false.
20725
 
**
20726
 
** Any non-numeric characters that following zNum are ignored.
20727
 
** This is different from sqlite3Atoi64() which requires the
20728
 
** input number to be zero-terminated.
20729
 
*/
20730
 
SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
20731
 
  sqlite_int64 v = 0;
20732
 
  int i, c;
20733
 
  int neg = 0;
20734
 
  if( zNum[0]=='-' ){
20735
 
    neg = 1;
20736
 
    zNum++;
20737
 
  }else if( zNum[0]=='+' ){
20738
 
    zNum++;
20739
 
  }
20740
 
  while( zNum[0]=='0' ) zNum++;
20741
 
  for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
20742
 
    v = v*10 + c;
20743
 
  }
20744
 
 
20745
 
  /* The longest decimal representation of a 32 bit integer is 10 digits:
20746
 
  **
20747
 
  **             1234567890
20748
 
  **     2^31 -> 2147483648
20749
 
  */
20750
 
  testcase( i==10 );
20751
 
  if( i>10 ){
20752
 
    return 0;
20753
 
  }
20754
 
  testcase( v-neg==2147483647 );
20755
 
  if( v-neg>2147483647 ){
20756
 
    return 0;
20757
 
  }
20758
 
  if( neg ){
20759
 
    v = -v;
20760
 
  }
20761
 
  *pValue = (int)v;
20762
 
  return 1;
20763
 
}
20764
 
 
20765
 
/*
20766
 
** Return a 32-bit integer value extracted from a string.  If the
20767
 
** string is not an integer, just return 0.
20768
 
*/
20769
 
SQLITE_PRIVATE int sqlite3Atoi(const char *z){
20770
 
  int x = 0;
20771
 
  if( z ) sqlite3GetInt32(z, &x);
20772
 
  return x;
20773
 
}
20774
 
 
20775
 
/*
20776
 
** The variable-length integer encoding is as follows:
20777
 
**
20778
 
** KEY:
20779
 
**         A = 0xxxxxxx    7 bits of data and one flag bit
20780
 
**         B = 1xxxxxxx    7 bits of data and one flag bit
20781
 
**         C = xxxxxxxx    8 bits of data
20782
 
**
20783
 
**  7 bits - A
20784
 
** 14 bits - BA
20785
 
** 21 bits - BBA
20786
 
** 28 bits - BBBA
20787
 
** 35 bits - BBBBA
20788
 
** 42 bits - BBBBBA
20789
 
** 49 bits - BBBBBBA
20790
 
** 56 bits - BBBBBBBA
20791
 
** 64 bits - BBBBBBBBC
20792
 
*/
20793
 
 
20794
 
/*
20795
 
** Write a 64-bit variable-length integer to memory starting at p[0].
20796
 
** The length of data write will be between 1 and 9 bytes.  The number
20797
 
** of bytes written is returned.
20798
 
**
20799
 
** A variable-length integer consists of the lower 7 bits of each byte
20800
 
** for all bytes that have the 8th bit set and one byte with the 8th
20801
 
** bit clear.  Except, if we get to the 9th byte, it stores the full
20802
 
** 8 bits and is the last byte.
20803
 
*/
20804
 
SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
20805
 
  int i, j, n;
20806
 
  u8 buf[10];
20807
 
  if( v & (((u64)0xff000000)<<32) ){
20808
 
    p[8] = (u8)v;
20809
 
    v >>= 8;
20810
 
    for(i=7; i>=0; i--){
20811
 
      p[i] = (u8)((v & 0x7f) | 0x80);
20812
 
      v >>= 7;
20813
 
    }
20814
 
    return 9;
20815
 
  }    
20816
 
  n = 0;
20817
 
  do{
20818
 
    buf[n++] = (u8)((v & 0x7f) | 0x80);
20819
 
    v >>= 7;
20820
 
  }while( v!=0 );
20821
 
  buf[0] &= 0x7f;
20822
 
  assert( n<=9 );
20823
 
  for(i=0, j=n-1; j>=0; j--, i++){
20824
 
    p[i] = buf[j];
20825
 
  }
20826
 
  return n;
20827
 
}
20828
 
 
20829
 
/*
20830
 
** This routine is a faster version of sqlite3PutVarint() that only
20831
 
** works for 32-bit positive integers and which is optimized for
20832
 
** the common case of small integers.  A MACRO version, putVarint32,
20833
 
** is provided which inlines the single-byte case.  All code should use
20834
 
** the MACRO version as this function assumes the single-byte case has
20835
 
** already been handled.
20836
 
*/
20837
 
SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char *p, u32 v){
20838
 
#ifndef putVarint32
20839
 
  if( (v & ~0x7f)==0 ){
20840
 
    p[0] = v;
20841
 
    return 1;
20842
 
  }
20843
 
#endif
20844
 
  if( (v & ~0x3fff)==0 ){
20845
 
    p[0] = (u8)((v>>7) | 0x80);
20846
 
    p[1] = (u8)(v & 0x7f);
20847
 
    return 2;
20848
 
  }
20849
 
  return sqlite3PutVarint(p, v);
20850
 
}
20851
 
 
20852
 
/*
20853
 
** Bitmasks used by sqlite3GetVarint().  These precomputed constants
20854
 
** are defined here rather than simply putting the constant expressions
20855
 
** inline in order to work around bugs in the RVT compiler.
20856
 
**
20857
 
** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
20858
 
**
20859
 
** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
20860
 
*/
20861
 
#define SLOT_2_0     0x001fc07f
20862
 
#define SLOT_4_2_0   0xf01fc07f
20863
 
 
20864
 
 
20865
 
/*
20866
 
** Read a 64-bit variable-length integer from memory starting at p[0].
20867
 
** Return the number of bytes read.  The value is stored in *v.
20868
 
*/
20869
 
SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
20870
 
  u32 a,b,s;
20871
 
 
20872
 
  a = *p;
20873
 
  /* a: p0 (unmasked) */
20874
 
  if (!(a&0x80))
20875
 
  {
20876
 
    *v = a;
20877
 
    return 1;
20878
 
  }
20879
 
 
20880
 
  p++;
20881
 
  b = *p;
20882
 
  /* b: p1 (unmasked) */
20883
 
  if (!(b&0x80))
20884
 
  {
20885
 
    a &= 0x7f;
20886
 
    a = a<<7;
20887
 
    a |= b;
20888
 
    *v = a;
20889
 
    return 2;
20890
 
  }
20891
 
 
20892
 
  /* Verify that constants are precomputed correctly */
20893
 
  assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
20894
 
  assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
20895
 
 
20896
 
  p++;
20897
 
  a = a<<14;
20898
 
  a |= *p;
20899
 
  /* a: p0<<14 | p2 (unmasked) */
20900
 
  if (!(a&0x80))
20901
 
  {
20902
 
    a &= SLOT_2_0;
20903
 
    b &= 0x7f;
20904
 
    b = b<<7;
20905
 
    a |= b;
20906
 
    *v = a;
20907
 
    return 3;
20908
 
  }
20909
 
 
20910
 
  /* CSE1 from below */
20911
 
  a &= SLOT_2_0;
20912
 
  p++;
20913
 
  b = b<<14;
20914
 
  b |= *p;
20915
 
  /* b: p1<<14 | p3 (unmasked) */
20916
 
  if (!(b&0x80))
20917
 
  {
20918
 
    b &= SLOT_2_0;
20919
 
    /* moved CSE1 up */
20920
 
    /* a &= (0x7f<<14)|(0x7f); */
20921
 
    a = a<<7;
20922
 
    a |= b;
20923
 
    *v = a;
20924
 
    return 4;
20925
 
  }
20926
 
 
20927
 
  /* a: p0<<14 | p2 (masked) */
20928
 
  /* b: p1<<14 | p3 (unmasked) */
20929
 
  /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
20930
 
  /* moved CSE1 up */
20931
 
  /* a &= (0x7f<<14)|(0x7f); */
20932
 
  b &= SLOT_2_0;
20933
 
  s = a;
20934
 
  /* s: p0<<14 | p2 (masked) */
20935
 
 
20936
 
  p++;
20937
 
  a = a<<14;
20938
 
  a |= *p;
20939
 
  /* a: p0<<28 | p2<<14 | p4 (unmasked) */
20940
 
  if (!(a&0x80))
20941
 
  {
20942
 
    /* we can skip these cause they were (effectively) done above in calc'ing s */
20943
 
    /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
20944
 
    /* b &= (0x7f<<14)|(0x7f); */
20945
 
    b = b<<7;
20946
 
    a |= b;
20947
 
    s = s>>18;
20948
 
    *v = ((u64)s)<<32 | a;
20949
 
    return 5;
20950
 
  }
20951
 
 
20952
 
  /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
20953
 
  s = s<<7;
20954
 
  s |= b;
20955
 
  /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
20956
 
 
20957
 
  p++;
20958
 
  b = b<<14;
20959
 
  b |= *p;
20960
 
  /* b: p1<<28 | p3<<14 | p5 (unmasked) */
20961
 
  if (!(b&0x80))
20962
 
  {
20963
 
    /* we can skip this cause it was (effectively) done above in calc'ing s */
20964
 
    /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
20965
 
    a &= SLOT_2_0;
20966
 
    a = a<<7;
20967
 
    a |= b;
20968
 
    s = s>>18;
20969
 
    *v = ((u64)s)<<32 | a;
20970
 
    return 6;
20971
 
  }
20972
 
 
20973
 
  p++;
20974
 
  a = a<<14;
20975
 
  a |= *p;
20976
 
  /* a: p2<<28 | p4<<14 | p6 (unmasked) */
20977
 
  if (!(a&0x80))
20978
 
  {
20979
 
    a &= SLOT_4_2_0;
20980
 
    b &= SLOT_2_0;
20981
 
    b = b<<7;
20982
 
    a |= b;
20983
 
    s = s>>11;
20984
 
    *v = ((u64)s)<<32 | a;
20985
 
    return 7;
20986
 
  }
20987
 
 
20988
 
  /* CSE2 from below */
20989
 
  a &= SLOT_2_0;
20990
 
  p++;
20991
 
  b = b<<14;
20992
 
  b |= *p;
20993
 
  /* b: p3<<28 | p5<<14 | p7 (unmasked) */
20994
 
  if (!(b&0x80))
20995
 
  {
20996
 
    b &= SLOT_4_2_0;
20997
 
    /* moved CSE2 up */
20998
 
    /* a &= (0x7f<<14)|(0x7f); */
20999
 
    a = a<<7;
21000
 
    a |= b;
21001
 
    s = s>>4;
21002
 
    *v = ((u64)s)<<32 | a;
21003
 
    return 8;
21004
 
  }
21005
 
 
21006
 
  p++;
21007
 
  a = a<<15;
21008
 
  a |= *p;
21009
 
  /* a: p4<<29 | p6<<15 | p8 (unmasked) */
21010
 
 
21011
 
  /* moved CSE2 up */
21012
 
  /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
21013
 
  b &= SLOT_2_0;
21014
 
  b = b<<8;
21015
 
  a |= b;
21016
 
 
21017
 
  s = s<<4;
21018
 
  b = p[-4];
21019
 
  b &= 0x7f;
21020
 
  b = b>>3;
21021
 
  s |= b;
21022
 
 
21023
 
  *v = ((u64)s)<<32 | a;
21024
 
 
21025
 
  return 9;
21026
 
}
21027
 
 
21028
 
/*
21029
 
** Read a 32-bit variable-length integer from memory starting at p[0].
21030
 
** Return the number of bytes read.  The value is stored in *v.
21031
 
**
21032
 
** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
21033
 
** integer, then set *v to 0xffffffff.
21034
 
**
21035
 
** A MACRO version, getVarint32, is provided which inlines the 
21036
 
** single-byte case.  All code should use the MACRO version as 
21037
 
** this function assumes the single-byte case has already been handled.
21038
 
*/
21039
 
SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
21040
 
  u32 a,b;
21041
 
 
21042
 
  /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
21043
 
  ** by the getVarin32() macro */
21044
 
  a = *p;
21045
 
  /* a: p0 (unmasked) */
21046
 
#ifndef getVarint32
21047
 
  if (!(a&0x80))
21048
 
  {
21049
 
    /* Values between 0 and 127 */
21050
 
    *v = a;
21051
 
    return 1;
21052
 
  }
21053
 
#endif
21054
 
 
21055
 
  /* The 2-byte case */
21056
 
  p++;
21057
 
  b = *p;
21058
 
  /* b: p1 (unmasked) */
21059
 
  if (!(b&0x80))
21060
 
  {
21061
 
    /* Values between 128 and 16383 */
21062
 
    a &= 0x7f;
21063
 
    a = a<<7;
21064
 
    *v = a | b;
21065
 
    return 2;
21066
 
  }
21067
 
 
21068
 
  /* The 3-byte case */
21069
 
  p++;
21070
 
  a = a<<14;
21071
 
  a |= *p;
21072
 
  /* a: p0<<14 | p2 (unmasked) */
21073
 
  if (!(a&0x80))
21074
 
  {
21075
 
    /* Values between 16384 and 2097151 */
21076
 
    a &= (0x7f<<14)|(0x7f);
21077
 
    b &= 0x7f;
21078
 
    b = b<<7;
21079
 
    *v = a | b;
21080
 
    return 3;
21081
 
  }
21082
 
 
21083
 
  /* A 32-bit varint is used to store size information in btrees.
21084
 
  ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
21085
 
  ** A 3-byte varint is sufficient, for example, to record the size
21086
 
  ** of a 1048569-byte BLOB or string.
21087
 
  **
21088
 
  ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
21089
 
  ** rare larger cases can be handled by the slower 64-bit varint
21090
 
  ** routine.
21091
 
  */
21092
 
#if 1
21093
 
  {
21094
 
    u64 v64;
21095
 
    u8 n;
21096
 
 
21097
 
    p -= 2;
21098
 
    n = sqlite3GetVarint(p, &v64);
21099
 
    assert( n>3 && n<=9 );
21100
 
    if( (v64 & SQLITE_MAX_U32)!=v64 ){
21101
 
      *v = 0xffffffff;
21102
 
    }else{
21103
 
      *v = (u32)v64;
21104
 
    }
21105
 
    return n;
21106
 
  }
21107
 
 
21108
 
#else
21109
 
  /* For following code (kept for historical record only) shows an
21110
 
  ** unrolling for the 3- and 4-byte varint cases.  This code is
21111
 
  ** slightly faster, but it is also larger and much harder to test.
21112
 
  */
21113
 
  p++;
21114
 
  b = b<<14;
21115
 
  b |= *p;
21116
 
  /* b: p1<<14 | p3 (unmasked) */
21117
 
  if (!(b&0x80))
21118
 
  {
21119
 
    /* Values between 2097152 and 268435455 */
21120
 
    b &= (0x7f<<14)|(0x7f);
21121
 
    a &= (0x7f<<14)|(0x7f);
21122
 
    a = a<<7;
21123
 
    *v = a | b;
21124
 
    return 4;
21125
 
  }
21126
 
 
21127
 
  p++;
21128
 
  a = a<<14;
21129
 
  a |= *p;
21130
 
  /* a: p0<<28 | p2<<14 | p4 (unmasked) */
21131
 
  if (!(a&0x80))
21132
 
  {
21133
 
    /* Values  between 268435456 and 34359738367 */
21134
 
    a &= SLOT_4_2_0;
21135
 
    b &= SLOT_4_2_0;
21136
 
    b = b<<7;
21137
 
    *v = a | b;
21138
 
    return 5;
21139
 
  }
21140
 
 
21141
 
  /* We can only reach this point when reading a corrupt database
21142
 
  ** file.  In that case we are not in any hurry.  Use the (relatively
21143
 
  ** slow) general-purpose sqlite3GetVarint() routine to extract the
21144
 
  ** value. */
21145
 
  {
21146
 
    u64 v64;
21147
 
    u8 n;
21148
 
 
21149
 
    p -= 4;
21150
 
    n = sqlite3GetVarint(p, &v64);
21151
 
    assert( n>5 && n<=9 );
21152
 
    *v = (u32)v64;
21153
 
    return n;
21154
 
  }
21155
 
#endif
21156
 
}
21157
 
 
21158
 
/*
21159
 
** Return the number of bytes that will be needed to store the given
21160
 
** 64-bit integer.
21161
 
*/
21162
 
SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
21163
 
  int i = 0;
21164
 
  do{
21165
 
    i++;
21166
 
    v >>= 7;
21167
 
  }while( v!=0 && ALWAYS(i<9) );
21168
 
  return i;
21169
 
}
21170
 
 
21171
 
 
21172
 
/*
21173
 
** Read or write a four-byte big-endian integer value.
21174
 
*/
21175
 
SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
21176
 
  return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
21177
 
}
21178
 
SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
21179
 
  p[0] = (u8)(v>>24);
21180
 
  p[1] = (u8)(v>>16);
21181
 
  p[2] = (u8)(v>>8);
21182
 
  p[3] = (u8)v;
21183
 
}
21184
 
 
21185
 
 
21186
 
 
21187
 
#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
21188
 
/*
21189
 
** Translate a single byte of Hex into an integer.
21190
 
** This routine only works if h really is a valid hexadecimal
21191
 
** character:  0..9a..fA..F
21192
 
*/
21193
 
static u8 hexToInt(int h){
21194
 
  assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
21195
 
#ifdef SQLITE_ASCII
21196
 
  h += 9*(1&(h>>6));
21197
 
#endif
21198
 
#ifdef SQLITE_EBCDIC
21199
 
  h += 9*(1&~(h>>4));
21200
 
#endif
21201
 
  return (u8)(h & 0xf);
21202
 
}
21203
 
#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
21204
 
 
21205
 
#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
21206
 
/*
21207
 
** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
21208
 
** value.  Return a pointer to its binary value.  Space to hold the
21209
 
** binary value has been obtained from malloc and must be freed by
21210
 
** the calling routine.
21211
 
*/
21212
 
SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
21213
 
  char *zBlob;
21214
 
  int i;
21215
 
 
21216
 
  zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
21217
 
  n--;
21218
 
  if( zBlob ){
21219
 
    for(i=0; i<n; i+=2){
21220
 
      zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
21221
 
    }
21222
 
    zBlob[i/2] = 0;
21223
 
  }
21224
 
  return zBlob;
21225
 
}
21226
 
#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
21227
 
 
21228
 
/*
21229
 
** Log an error that is an API call on a connection pointer that should
21230
 
** not have been used.  The "type" of connection pointer is given as the
21231
 
** argument.  The zType is a word like "NULL" or "closed" or "invalid".
21232
 
*/
21233
 
static void logBadConnection(const char *zType){
21234
 
  sqlite3_log(SQLITE_MISUSE, 
21235
 
     "API call with %s database connection pointer",
21236
 
     zType
21237
 
  );
21238
 
}
21239
 
 
21240
 
/*
21241
 
** Check to make sure we have a valid db pointer.  This test is not
21242
 
** foolproof but it does provide some measure of protection against
21243
 
** misuse of the interface such as passing in db pointers that are
21244
 
** NULL or which have been previously closed.  If this routine returns
21245
 
** 1 it means that the db pointer is valid and 0 if it should not be
21246
 
** dereferenced for any reason.  The calling function should invoke
21247
 
** SQLITE_MISUSE immediately.
21248
 
**
21249
 
** sqlite3SafetyCheckOk() requires that the db pointer be valid for
21250
 
** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
21251
 
** open properly and is not fit for general use but which can be
21252
 
** used as an argument to sqlite3_errmsg() or sqlite3_close().
21253
 
*/
21254
 
SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
21255
 
  u32 magic;
21256
 
  if( db==0 ){
21257
 
    logBadConnection("NULL");
21258
 
    return 0;
21259
 
  }
21260
 
  magic = db->magic;
21261
 
  if( magic!=SQLITE_MAGIC_OPEN ){
21262
 
    if( sqlite3SafetyCheckSickOrOk(db) ){
21263
 
      testcase( sqlite3GlobalConfig.xLog!=0 );
21264
 
      logBadConnection("unopened");
21265
 
    }
21266
 
    return 0;
21267
 
  }else{
21268
 
    return 1;
21269
 
  }
21270
 
}
21271
 
SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
21272
 
  u32 magic;
21273
 
  magic = db->magic;
21274
 
  if( magic!=SQLITE_MAGIC_SICK &&
21275
 
      magic!=SQLITE_MAGIC_OPEN &&
21276
 
      magic!=SQLITE_MAGIC_BUSY ){
21277
 
    testcase( sqlite3GlobalConfig.xLog!=0 );
21278
 
    logBadConnection("invalid");
21279
 
    return 0;
21280
 
  }else{
21281
 
    return 1;
21282
 
  }
21283
 
}
21284
 
 
21285
 
/*
21286
 
** Attempt to add, substract, or multiply the 64-bit signed value iB against
21287
 
** the other 64-bit signed integer at *pA and store the result in *pA.
21288
 
** Return 0 on success.  Or if the operation would have resulted in an
21289
 
** overflow, leave *pA unchanged and return 1.
21290
 
*/
21291
 
SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
21292
 
  i64 iA = *pA;
21293
 
  testcase( iA==0 ); testcase( iA==1 );
21294
 
  testcase( iB==-1 ); testcase( iB==0 );
21295
 
  if( iB>=0 ){
21296
 
    testcase( iA>0 && LARGEST_INT64 - iA == iB );
21297
 
    testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
21298
 
    if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
21299
 
    *pA += iB;
21300
 
  }else{
21301
 
    testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
21302
 
    testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
21303
 
    if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
21304
 
    *pA += iB;
21305
 
  }
21306
 
  return 0; 
21307
 
}
21308
 
SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
21309
 
  testcase( iB==SMALLEST_INT64+1 );
21310
 
  if( iB==SMALLEST_INT64 ){
21311
 
    testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
21312
 
    if( (*pA)>=0 ) return 1;
21313
 
    *pA -= iB;
21314
 
    return 0;
21315
 
  }else{
21316
 
    return sqlite3AddInt64(pA, -iB);
21317
 
  }
21318
 
}
21319
 
#define TWOPOWER32 (((i64)1)<<32)
21320
 
#define TWOPOWER31 (((i64)1)<<31)
21321
 
SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
21322
 
  i64 iA = *pA;
21323
 
  i64 iA1, iA0, iB1, iB0, r;
21324
 
 
21325
 
  iA1 = iA/TWOPOWER32;
21326
 
  iA0 = iA % TWOPOWER32;
21327
 
  iB1 = iB/TWOPOWER32;
21328
 
  iB0 = iB % TWOPOWER32;
21329
 
  if( iA1*iB1 != 0 ) return 1;
21330
 
  assert( iA1*iB0==0 || iA0*iB1==0 );
21331
 
  r = iA1*iB0 + iA0*iB1;
21332
 
  testcase( r==(-TWOPOWER31)-1 );
21333
 
  testcase( r==(-TWOPOWER31) );
21334
 
  testcase( r==TWOPOWER31 );
21335
 
  testcase( r==TWOPOWER31-1 );
21336
 
  if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
21337
 
  r *= TWOPOWER32;
21338
 
  if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
21339
 
  *pA = r;
21340
 
  return 0;
21341
 
}
21342
 
 
21343
 
/*
21344
 
** Compute the absolute value of a 32-bit signed integer, of possible.  Or 
21345
 
** if the integer has a value of -2147483648, return +2147483647
21346
 
*/
21347
 
SQLITE_PRIVATE int sqlite3AbsInt32(int x){
21348
 
  if( x>=0 ) return x;
21349
 
  if( x==(int)0x80000000 ) return 0x7fffffff;
21350
 
  return -x;
21351
 
}
21352
 
 
21353
 
/************** End of util.c ************************************************/
21354
 
/************** Begin file hash.c ********************************************/
21355
 
/*
21356
 
** 2001 September 22
21357
 
**
21358
 
** The author disclaims copyright to this source code.  In place of
21359
 
** a legal notice, here is a blessing:
21360
 
**
21361
 
**    May you do good and not evil.
21362
 
**    May you find forgiveness for yourself and forgive others.
21363
 
**    May you share freely, never taking more than you give.
21364
 
**
21365
 
*************************************************************************
21366
 
** This is the implementation of generic hash-tables
21367
 
** used in SQLite.
21368
 
*/
21369
 
 
21370
 
/* Turn bulk memory into a hash table object by initializing the
21371
 
** fields of the Hash structure.
21372
 
**
21373
 
** "pNew" is a pointer to the hash table that is to be initialized.
21374
 
*/
21375
 
SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
21376
 
  assert( pNew!=0 );
21377
 
  pNew->first = 0;
21378
 
  pNew->count = 0;
21379
 
  pNew->htsize = 0;
21380
 
  pNew->ht = 0;
21381
 
}
21382
 
 
21383
 
/* Remove all entries from a hash table.  Reclaim all memory.
21384
 
** Call this routine to delete a hash table or to reset a hash table
21385
 
** to the empty state.
21386
 
*/
21387
 
SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
21388
 
  HashElem *elem;         /* For looping over all elements of the table */
21389
 
 
21390
 
  assert( pH!=0 );
21391
 
  elem = pH->first;
21392
 
  pH->first = 0;
21393
 
  sqlite3_free(pH->ht);
21394
 
  pH->ht = 0;
21395
 
  pH->htsize = 0;
21396
 
  while( elem ){
21397
 
    HashElem *next_elem = elem->next;
21398
 
    sqlite3_free(elem);
21399
 
    elem = next_elem;
21400
 
  }
21401
 
  pH->count = 0;
21402
 
}
21403
 
 
21404
 
/*
21405
 
** The hashing function.
21406
 
*/
21407
 
static unsigned int strHash(const char *z, int nKey){
21408
 
  int h = 0;
21409
 
  assert( nKey>=0 );
21410
 
  while( nKey > 0  ){
21411
 
    h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
21412
 
    nKey--;
21413
 
  }
21414
 
  return h;
21415
 
}
21416
 
 
21417
 
 
21418
 
/* Link pNew element into the hash table pH.  If pEntry!=0 then also
21419
 
** insert pNew into the pEntry hash bucket.
21420
 
*/
21421
 
static void insertElement(
21422
 
  Hash *pH,              /* The complete hash table */
21423
 
  struct _ht *pEntry,    /* The entry into which pNew is inserted */
21424
 
  HashElem *pNew         /* The element to be inserted */
21425
 
){
21426
 
  HashElem *pHead;       /* First element already in pEntry */
21427
 
  if( pEntry ){
21428
 
    pHead = pEntry->count ? pEntry->chain : 0;
21429
 
    pEntry->count++;
21430
 
    pEntry->chain = pNew;
21431
 
  }else{
21432
 
    pHead = 0;
21433
 
  }
21434
 
  if( pHead ){
21435
 
    pNew->next = pHead;
21436
 
    pNew->prev = pHead->prev;
21437
 
    if( pHead->prev ){ pHead->prev->next = pNew; }
21438
 
    else             { pH->first = pNew; }
21439
 
    pHead->prev = pNew;
21440
 
  }else{
21441
 
    pNew->next = pH->first;
21442
 
    if( pH->first ){ pH->first->prev = pNew; }
21443
 
    pNew->prev = 0;
21444
 
    pH->first = pNew;
21445
 
  }
21446
 
}
21447
 
 
21448
 
 
21449
 
/* Resize the hash table so that it cantains "new_size" buckets.
21450
 
**
21451
 
** The hash table might fail to resize if sqlite3_malloc() fails or
21452
 
** if the new size is the same as the prior size.
21453
 
** Return TRUE if the resize occurs and false if not.
21454
 
*/
21455
 
static int rehash(Hash *pH, unsigned int new_size){
21456
 
  struct _ht *new_ht;            /* The new hash table */
21457
 
  HashElem *elem, *next_elem;    /* For looping over existing elements */
21458
 
 
21459
 
#if SQLITE_MALLOC_SOFT_LIMIT>0
21460
 
  if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
21461
 
    new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
21462
 
  }
21463
 
  if( new_size==pH->htsize ) return 0;
21464
 
#endif
21465
 
 
21466
 
  /* The inability to allocates space for a larger hash table is
21467
 
  ** a performance hit but it is not a fatal error.  So mark the
21468
 
  ** allocation as a benign.
21469
 
  */
21470
 
  sqlite3BeginBenignMalloc();
21471
 
  new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
21472
 
  sqlite3EndBenignMalloc();
21473
 
 
21474
 
  if( new_ht==0 ) return 0;
21475
 
  sqlite3_free(pH->ht);
21476
 
  pH->ht = new_ht;
21477
 
  pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
21478
 
  memset(new_ht, 0, new_size*sizeof(struct _ht));
21479
 
  for(elem=pH->first, pH->first=0; elem; elem = next_elem){
21480
 
    unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
21481
 
    next_elem = elem->next;
21482
 
    insertElement(pH, &new_ht[h], elem);
21483
 
  }
21484
 
  return 1;
21485
 
}
21486
 
 
21487
 
/* This function (for internal use only) locates an element in an
21488
 
** hash table that matches the given key.  The hash for this key has
21489
 
** already been computed and is passed as the 4th parameter.
21490
 
*/
21491
 
static HashElem *findElementGivenHash(
21492
 
  const Hash *pH,     /* The pH to be searched */
21493
 
  const char *pKey,   /* The key we are searching for */
21494
 
  int nKey,           /* Bytes in key (not counting zero terminator) */
21495
 
  unsigned int h      /* The hash for this key. */
21496
 
){
21497
 
  HashElem *elem;                /* Used to loop thru the element list */
21498
 
  int count;                     /* Number of elements left to test */
21499
 
 
21500
 
  if( pH->ht ){
21501
 
    struct _ht *pEntry = &pH->ht[h];
21502
 
    elem = pEntry->chain;
21503
 
    count = pEntry->count;
21504
 
  }else{
21505
 
    elem = pH->first;
21506
 
    count = pH->count;
21507
 
  }
21508
 
  while( count-- && ALWAYS(elem) ){
21509
 
    if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){ 
21510
 
      return elem;
21511
 
    }
21512
 
    elem = elem->next;
21513
 
  }
21514
 
  return 0;
21515
 
}
21516
 
 
21517
 
/* Remove a single entry from the hash table given a pointer to that
21518
 
** element and a hash on the element's key.
21519
 
*/
21520
 
static void removeElementGivenHash(
21521
 
  Hash *pH,         /* The pH containing "elem" */
21522
 
  HashElem* elem,   /* The element to be removed from the pH */
21523
 
  unsigned int h    /* Hash value for the element */
21524
 
){
21525
 
  struct _ht *pEntry;
21526
 
  if( elem->prev ){
21527
 
    elem->prev->next = elem->next; 
21528
 
  }else{
21529
 
    pH->first = elem->next;
21530
 
  }
21531
 
  if( elem->next ){
21532
 
    elem->next->prev = elem->prev;
21533
 
  }
21534
 
  if( pH->ht ){
21535
 
    pEntry = &pH->ht[h];
21536
 
    if( pEntry->chain==elem ){
21537
 
      pEntry->chain = elem->next;
21538
 
    }
21539
 
    pEntry->count--;
21540
 
    assert( pEntry->count>=0 );
21541
 
  }
21542
 
  sqlite3_free( elem );
21543
 
  pH->count--;
21544
 
  if( pH->count<=0 ){
21545
 
    assert( pH->first==0 );
21546
 
    assert( pH->count==0 );
21547
 
    sqlite3HashClear(pH);
21548
 
  }
21549
 
}
21550
 
 
21551
 
/* Attempt to locate an element of the hash table pH with a key
21552
 
** that matches pKey,nKey.  Return the data for this element if it is
21553
 
** found, or NULL if there is no match.
21554
 
*/
21555
 
SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
21556
 
  HashElem *elem;    /* The element that matches key */
21557
 
  unsigned int h;    /* A hash on key */
21558
 
 
21559
 
  assert( pH!=0 );
21560
 
  assert( pKey!=0 );
21561
 
  assert( nKey>=0 );
21562
 
  if( pH->ht ){
21563
 
    h = strHash(pKey, nKey) % pH->htsize;
21564
 
  }else{
21565
 
    h = 0;
21566
 
  }
21567
 
  elem = findElementGivenHash(pH, pKey, nKey, h);
21568
 
  return elem ? elem->data : 0;
21569
 
}
21570
 
 
21571
 
/* Insert an element into the hash table pH.  The key is pKey,nKey
21572
 
** and the data is "data".
21573
 
**
21574
 
** If no element exists with a matching key, then a new
21575
 
** element is created and NULL is returned.
21576
 
**
21577
 
** If another element already exists with the same key, then the
21578
 
** new data replaces the old data and the old data is returned.
21579
 
** The key is not copied in this instance.  If a malloc fails, then
21580
 
** the new data is returned and the hash table is unchanged.
21581
 
**
21582
 
** If the "data" parameter to this function is NULL, then the
21583
 
** element corresponding to "key" is removed from the hash table.
21584
 
*/
21585
 
SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
21586
 
  unsigned int h;       /* the hash of the key modulo hash table size */
21587
 
  HashElem *elem;       /* Used to loop thru the element list */
21588
 
  HashElem *new_elem;   /* New element added to the pH */
21589
 
 
21590
 
  assert( pH!=0 );
21591
 
  assert( pKey!=0 );
21592
 
  assert( nKey>=0 );
21593
 
  if( pH->htsize ){
21594
 
    h = strHash(pKey, nKey) % pH->htsize;
21595
 
  }else{
21596
 
    h = 0;
21597
 
  }
21598
 
  elem = findElementGivenHash(pH,pKey,nKey,h);
21599
 
  if( elem ){
21600
 
    void *old_data = elem->data;
21601
 
    if( data==0 ){
21602
 
      removeElementGivenHash(pH,elem,h);
21603
 
    }else{
21604
 
      elem->data = data;
21605
 
      elem->pKey = pKey;
21606
 
      assert(nKey==elem->nKey);
21607
 
    }
21608
 
    return old_data;
21609
 
  }
21610
 
  if( data==0 ) return 0;
21611
 
  new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
21612
 
  if( new_elem==0 ) return data;
21613
 
  new_elem->pKey = pKey;
21614
 
  new_elem->nKey = nKey;
21615
 
  new_elem->data = data;
21616
 
  pH->count++;
21617
 
  if( pH->count>=10 && pH->count > 2*pH->htsize ){
21618
 
    if( rehash(pH, pH->count*2) ){
21619
 
      assert( pH->htsize>0 );
21620
 
      h = strHash(pKey, nKey) % pH->htsize;
21621
 
    }
21622
 
  }
21623
 
  if( pH->ht ){
21624
 
    insertElement(pH, &pH->ht[h], new_elem);
21625
 
  }else{
21626
 
    insertElement(pH, 0, new_elem);
21627
 
  }
21628
 
  return 0;
21629
 
}
21630
 
 
21631
 
/************** End of hash.c ************************************************/
21632
 
/************** Begin file opcodes.c *****************************************/
21633
 
/* Automatically generated.  Do not edit */
21634
 
/* See the mkopcodec.awk script for details. */
21635
 
#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
21636
 
SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
21637
 
 static const char *const azName[] = { "?",
21638
 
     /*   1 */ "Goto",
21639
 
     /*   2 */ "Gosub",
21640
 
     /*   3 */ "Return",
21641
 
     /*   4 */ "Yield",
21642
 
     /*   5 */ "HaltIfNull",
21643
 
     /*   6 */ "Halt",
21644
 
     /*   7 */ "Integer",
21645
 
     /*   8 */ "Int64",
21646
 
     /*   9 */ "String",
21647
 
     /*  10 */ "Null",
21648
 
     /*  11 */ "Blob",
21649
 
     /*  12 */ "Variable",
21650
 
     /*  13 */ "Move",
21651
 
     /*  14 */ "Copy",
21652
 
     /*  15 */ "SCopy",
21653
 
     /*  16 */ "ResultRow",
21654
 
     /*  17 */ "CollSeq",
21655
 
     /*  18 */ "Function",
21656
 
     /*  19 */ "Not",
21657
 
     /*  20 */ "AddImm",
21658
 
     /*  21 */ "MustBeInt",
21659
 
     /*  22 */ "RealAffinity",
21660
 
     /*  23 */ "Permutation",
21661
 
     /*  24 */ "Compare",
21662
 
     /*  25 */ "Jump",
21663
 
     /*  26 */ "If",
21664
 
     /*  27 */ "IfNot",
21665
 
     /*  28 */ "Column",
21666
 
     /*  29 */ "Affinity",
21667
 
     /*  30 */ "MakeRecord",
21668
 
     /*  31 */ "Count",
21669
 
     /*  32 */ "Savepoint",
21670
 
     /*  33 */ "AutoCommit",
21671
 
     /*  34 */ "Transaction",
21672
 
     /*  35 */ "ReadCookie",
21673
 
     /*  36 */ "SetCookie",
21674
 
     /*  37 */ "VerifyCookie",
21675
 
     /*  38 */ "OpenRead",
21676
 
     /*  39 */ "OpenWrite",
21677
 
     /*  40 */ "OpenAutoindex",
21678
 
     /*  41 */ "OpenEphemeral",
21679
 
     /*  42 */ "OpenPseudo",
21680
 
     /*  43 */ "Close",
21681
 
     /*  44 */ "SeekLt",
21682
 
     /*  45 */ "SeekLe",
21683
 
     /*  46 */ "SeekGe",
21684
 
     /*  47 */ "SeekGt",
21685
 
     /*  48 */ "Seek",
21686
 
     /*  49 */ "NotFound",
21687
 
     /*  50 */ "Found",
21688
 
     /*  51 */ "IsUnique",
21689
 
     /*  52 */ "NotExists",
21690
 
     /*  53 */ "Sequence",
21691
 
     /*  54 */ "NewRowid",
21692
 
     /*  55 */ "Insert",
21693
 
     /*  56 */ "InsertInt",
21694
 
     /*  57 */ "Delete",
21695
 
     /*  58 */ "ResetCount",
21696
 
     /*  59 */ "RowKey",
21697
 
     /*  60 */ "RowData",
21698
 
     /*  61 */ "Rowid",
21699
 
     /*  62 */ "NullRow",
21700
 
     /*  63 */ "Last",
21701
 
     /*  64 */ "Sort",
21702
 
     /*  65 */ "Rewind",
21703
 
     /*  66 */ "Prev",
21704
 
     /*  67 */ "Next",
21705
 
     /*  68 */ "Or",
21706
 
     /*  69 */ "And",
21707
 
     /*  70 */ "IdxInsert",
21708
 
     /*  71 */ "IdxDelete",
21709
 
     /*  72 */ "IdxRowid",
21710
 
     /*  73 */ "IsNull",
21711
 
     /*  74 */ "NotNull",
21712
 
     /*  75 */ "Ne",
21713
 
     /*  76 */ "Eq",
21714
 
     /*  77 */ "Gt",
21715
 
     /*  78 */ "Le",
21716
 
     /*  79 */ "Lt",
21717
 
     /*  80 */ "Ge",
21718
 
     /*  81 */ "IdxLT",
21719
 
     /*  82 */ "BitAnd",
21720
 
     /*  83 */ "BitOr",
21721
 
     /*  84 */ "ShiftLeft",
21722
 
     /*  85 */ "ShiftRight",
21723
 
     /*  86 */ "Add",
21724
 
     /*  87 */ "Subtract",
21725
 
     /*  88 */ "Multiply",
21726
 
     /*  89 */ "Divide",
21727
 
     /*  90 */ "Remainder",
21728
 
     /*  91 */ "Concat",
21729
 
     /*  92 */ "IdxGE",
21730
 
     /*  93 */ "BitNot",
21731
 
     /*  94 */ "String8",
21732
 
     /*  95 */ "Destroy",
21733
 
     /*  96 */ "Clear",
21734
 
     /*  97 */ "CreateIndex",
21735
 
     /*  98 */ "CreateTable",
21736
 
     /*  99 */ "ParseSchema",
21737
 
     /* 100 */ "LoadAnalysis",
21738
 
     /* 101 */ "DropTable",
21739
 
     /* 102 */ "DropIndex",
21740
 
     /* 103 */ "DropTrigger",
21741
 
     /* 104 */ "IntegrityCk",
21742
 
     /* 105 */ "RowSetAdd",
21743
 
     /* 106 */ "RowSetRead",
21744
 
     /* 107 */ "RowSetTest",
21745
 
     /* 108 */ "Program",
21746
 
     /* 109 */ "Param",
21747
 
     /* 110 */ "FkCounter",
21748
 
     /* 111 */ "FkIfZero",
21749
 
     /* 112 */ "MemMax",
21750
 
     /* 113 */ "IfPos",
21751
 
     /* 114 */ "IfNeg",
21752
 
     /* 115 */ "IfZero",
21753
 
     /* 116 */ "AggStep",
21754
 
     /* 117 */ "AggFinal",
21755
 
     /* 118 */ "Checkpoint",
21756
 
     /* 119 */ "JournalMode",
21757
 
     /* 120 */ "Vacuum",
21758
 
     /* 121 */ "IncrVacuum",
21759
 
     /* 122 */ "Expire",
21760
 
     /* 123 */ "TableLock",
21761
 
     /* 124 */ "VBegin",
21762
 
     /* 125 */ "VCreate",
21763
 
     /* 126 */ "VDestroy",
21764
 
     /* 127 */ "VOpen",
21765
 
     /* 128 */ "VFilter",
21766
 
     /* 129 */ "VColumn",
21767
 
     /* 130 */ "Real",
21768
 
     /* 131 */ "VNext",
21769
 
     /* 132 */ "VRename",
21770
 
     /* 133 */ "VUpdate",
21771
 
     /* 134 */ "Pagecount",
21772
 
     /* 135 */ "MaxPgcnt",
21773
 
     /* 136 */ "Trace",
21774
 
     /* 137 */ "Noop",
21775
 
     /* 138 */ "Explain",
21776
 
     /* 139 */ "NotUsed_139",
21777
 
     /* 140 */ "NotUsed_140",
21778
 
     /* 141 */ "ToText",
21779
 
     /* 142 */ "ToBlob",
21780
 
     /* 143 */ "ToNumeric",
21781
 
     /* 144 */ "ToInt",
21782
 
     /* 145 */ "ToReal",
21783
 
  };
21784
 
  return azName[i];
21785
 
}
21786
 
#endif
21787
 
 
21788
 
/************** End of opcodes.c *********************************************/
21789
 
/************** Begin file os_os2.c ******************************************/
21790
 
/*
21791
 
** 2006 Feb 14
21792
 
**
21793
 
** The author disclaims copyright to this source code.  In place of
21794
 
** a legal notice, here is a blessing:
21795
 
**
21796
 
**    May you do good and not evil.
21797
 
**    May you find forgiveness for yourself and forgive others.
21798
 
**    May you share freely, never taking more than you give.
21799
 
**
21800
 
******************************************************************************
21801
 
**
21802
 
** This file contains code that is specific to OS/2.
21803
 
*/
21804
 
 
21805
 
 
21806
 
#if SQLITE_OS_OS2
21807
 
 
21808
 
/*
21809
 
** A Note About Memory Allocation:
21810
 
**
21811
 
** This driver uses malloc()/free() directly rather than going through
21812
 
** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
21813
 
** are designed for use on embedded systems where memory is scarce and
21814
 
** malloc failures happen frequently.  OS/2 does not typically run on
21815
 
** embedded systems, and when it does the developers normally have bigger
21816
 
** problems to worry about than running out of memory.  So there is not
21817
 
** a compelling need to use the wrappers.
21818
 
**
21819
 
** But there is a good reason to not use the wrappers.  If we use the
21820
 
** wrappers then we will get simulated malloc() failures within this
21821
 
** driver.  And that causes all kinds of problems for our tests.  We
21822
 
** could enhance SQLite to deal with simulated malloc failures within
21823
 
** the OS driver, but the code to deal with those failure would not
21824
 
** be exercised on Linux (which does not need to malloc() in the driver)
21825
 
** and so we would have difficulty writing coverage tests for that
21826
 
** code.  Better to leave the code out, we think.
21827
 
**
21828
 
** The point of this discussion is as follows:  When creating a new
21829
 
** OS layer for an embedded system, if you use this file as an example,
21830
 
** avoid the use of malloc()/free().  Those routines work ok on OS/2
21831
 
** desktops but not so well in embedded systems.
21832
 
*/
21833
 
 
21834
 
/*
21835
 
** Macros used to determine whether or not to use threads.
21836
 
*/
21837
 
#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE
21838
 
# define SQLITE_OS2_THREADS 1
21839
 
#endif
21840
 
 
21841
 
/*
21842
 
** Include code that is common to all os_*.c files
21843
 
*/
21844
 
/************** Include os_common.h in the middle of os_os2.c ****************/
21845
 
/************** Begin file os_common.h ***************************************/
21846
 
/*
21847
 
** 2004 May 22
21848
 
**
21849
 
** The author disclaims copyright to this source code.  In place of
21850
 
** a legal notice, here is a blessing:
21851
 
**
21852
 
**    May you do good and not evil.
21853
 
**    May you find forgiveness for yourself and forgive others.
21854
 
**    May you share freely, never taking more than you give.
21855
 
**
21856
 
******************************************************************************
21857
 
**
21858
 
** This file contains macros and a little bit of code that is common to
21859
 
** all of the platform-specific files (os_*.c) and is #included into those
21860
 
** files.
21861
 
**
21862
 
** This file should be #included by the os_*.c files only.  It is not a
21863
 
** general purpose header file.
21864
 
*/
21865
 
#ifndef _OS_COMMON_H_
21866
 
#define _OS_COMMON_H_
21867
 
 
21868
 
/*
21869
 
** At least two bugs have slipped in because we changed the MEMORY_DEBUG
21870
 
** macro to SQLITE_DEBUG and some older makefiles have not yet made the
21871
 
** switch.  The following code should catch this problem at compile-time.
21872
 
*/
21873
 
#ifdef MEMORY_DEBUG
21874
 
# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
21875
 
#endif
21876
 
 
21877
 
#ifdef SQLITE_DEBUG
21878
 
SQLITE_PRIVATE int sqlite3OSTrace = 0;
21879
 
#define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
21880
 
#else
21881
 
#define OSTRACE(X)
21882
 
#endif
21883
 
 
21884
 
/*
21885
 
** Macros for performance tracing.  Normally turned off.  Only works
21886
 
** on i486 hardware.
21887
 
*/
21888
 
#ifdef SQLITE_PERFORMANCE_TRACE
21889
 
 
21890
 
/* 
21891
 
** hwtime.h contains inline assembler code for implementing 
21892
 
** high-performance timing routines.
21893
 
*/
21894
 
/************** Include hwtime.h in the middle of os_common.h ****************/
21895
 
/************** Begin file hwtime.h ******************************************/
21896
 
/*
21897
 
** 2008 May 27
21898
 
**
21899
 
** The author disclaims copyright to this source code.  In place of
21900
 
** a legal notice, here is a blessing:
21901
 
**
21902
 
**    May you do good and not evil.
21903
 
**    May you find forgiveness for yourself and forgive others.
21904
 
**    May you share freely, never taking more than you give.
21905
 
**
21906
 
******************************************************************************
21907
 
**
21908
 
** This file contains inline asm code for retrieving "high-performance"
21909
 
** counters for x86 class CPUs.
21910
 
*/
21911
 
#ifndef _HWTIME_H_
21912
 
#define _HWTIME_H_
21913
 
 
21914
 
/*
21915
 
** The following routine only works on pentium-class (or newer) processors.
21916
 
** It uses the RDTSC opcode to read the cycle count value out of the
21917
 
** processor and returns that value.  This can be used for high-res
21918
 
** profiling.
21919
 
*/
21920
 
#if (defined(__GNUC__) || defined(_MSC_VER)) && \
21921
 
      (defined(i386) || defined(__i386__) || defined(_M_IX86))
21922
 
 
21923
 
  #if defined(__GNUC__)
21924
 
 
21925
 
  __inline__ sqlite_uint64 sqlite3Hwtime(void){
21926
 
     unsigned int lo, hi;
21927
 
     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
21928
 
     return (sqlite_uint64)hi << 32 | lo;
21929
 
  }
21930
 
 
21931
 
  #elif defined(_MSC_VER)
21932
 
 
21933
 
  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
21934
 
     __asm {
21935
 
        rdtsc
21936
 
        ret       ; return value at EDX:EAX
21937
 
     }
21938
 
  }
21939
 
 
21940
 
  #endif
21941
 
 
21942
 
#elif (defined(__GNUC__) && defined(__x86_64__))
21943
 
 
21944
 
  __inline__ sqlite_uint64 sqlite3Hwtime(void){
21945
 
      unsigned long val;
21946
 
      __asm__ __volatile__ ("rdtsc" : "=A" (val));
21947
 
      return val;
21948
 
  }
21949
 
 
21950
 
#elif (defined(__GNUC__) && defined(__ppc__))
21951
 
 
21952
 
  __inline__ sqlite_uint64 sqlite3Hwtime(void){
21953
 
      unsigned long long retval;
21954
 
      unsigned long junk;
21955
 
      __asm__ __volatile__ ("\n\
21956
 
          1:      mftbu   %1\n\
21957
 
                  mftb    %L0\n\
21958
 
                  mftbu   %0\n\
21959
 
                  cmpw    %0,%1\n\
21960
 
                  bne     1b"
21961
 
                  : "=r" (retval), "=r" (junk));
21962
 
      return retval;
21963
 
  }
21964
 
 
21965
 
#else
21966
 
 
21967
 
  #error Need implementation of sqlite3Hwtime() for your platform.
21968
 
 
21969
 
  /*
21970
 
  ** To compile without implementing sqlite3Hwtime() for your platform,
21971
 
  ** you can remove the above #error and use the following
21972
 
  ** stub function.  You will lose timing support for many
21973
 
  ** of the debugging and testing utilities, but it should at
21974
 
  ** least compile and run.
21975
 
  */
21976
 
SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
21977
 
 
21978
 
#endif
21979
 
 
21980
 
#endif /* !defined(_HWTIME_H_) */
21981
 
 
21982
 
/************** End of hwtime.h **********************************************/
21983
 
/************** Continuing where we left off in os_common.h ******************/
21984
 
 
21985
 
static sqlite_uint64 g_start;
21986
 
static sqlite_uint64 g_elapsed;
21987
 
#define TIMER_START       g_start=sqlite3Hwtime()
21988
 
#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
21989
 
#define TIMER_ELAPSED     g_elapsed
21990
 
#else
21991
 
#define TIMER_START
21992
 
#define TIMER_END
21993
 
#define TIMER_ELAPSED     ((sqlite_uint64)0)
21994
 
#endif
21995
 
 
21996
 
/*
21997
 
** If we compile with the SQLITE_TEST macro set, then the following block
21998
 
** of code will give us the ability to simulate a disk I/O error.  This
21999
 
** is used for testing the I/O recovery logic.
22000
 
*/
22001
 
#ifdef SQLITE_TEST
22002
 
SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
22003
 
SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
22004
 
SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
22005
 
SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
22006
 
SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
22007
 
SQLITE_API int sqlite3_diskfull_pending = 0;
22008
 
SQLITE_API int sqlite3_diskfull = 0;
22009
 
#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
22010
 
#define SimulateIOError(CODE)  \
22011
 
  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
22012
 
       || sqlite3_io_error_pending-- == 1 )  \
22013
 
              { local_ioerr(); CODE; }
22014
 
static void local_ioerr(){
22015
 
  IOTRACE(("IOERR\n"));
22016
 
  sqlite3_io_error_hit++;
22017
 
  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
22018
 
}
22019
 
#define SimulateDiskfullError(CODE) \
22020
 
   if( sqlite3_diskfull_pending ){ \
22021
 
     if( sqlite3_diskfull_pending == 1 ){ \
22022
 
       local_ioerr(); \
22023
 
       sqlite3_diskfull = 1; \
22024
 
       sqlite3_io_error_hit = 1; \
22025
 
       CODE; \
22026
 
     }else{ \
22027
 
       sqlite3_diskfull_pending--; \
22028
 
     } \
22029
 
   }
22030
 
#else
22031
 
#define SimulateIOErrorBenign(X)
22032
 
#define SimulateIOError(A)
22033
 
#define SimulateDiskfullError(A)
22034
 
#endif
22035
 
 
22036
 
/*
22037
 
** When testing, keep a count of the number of open files.
22038
 
*/
22039
 
#ifdef SQLITE_TEST
22040
 
SQLITE_API int sqlite3_open_file_count = 0;
22041
 
#define OpenCounter(X)  sqlite3_open_file_count+=(X)
22042
 
#else
22043
 
#define OpenCounter(X)
22044
 
#endif
22045
 
 
22046
 
#endif /* !defined(_OS_COMMON_H_) */
22047
 
 
22048
 
/************** End of os_common.h *******************************************/
22049
 
/************** Continuing where we left off in os_os2.c *********************/
22050
 
 
22051
 
/* Forward references */
22052
 
typedef struct os2File os2File;         /* The file structure */
22053
 
typedef struct os2ShmNode os2ShmNode;   /* A shared descritive memory node */
22054
 
typedef struct os2ShmLink os2ShmLink;   /* A connection to shared-memory */
22055
 
 
22056
 
/*
22057
 
** The os2File structure is subclass of sqlite3_file specific for the OS/2
22058
 
** protability layer.
22059
 
*/
22060
 
struct os2File {
22061
 
  const sqlite3_io_methods *pMethod;  /* Always the first entry */
22062
 
  HFILE h;                  /* Handle for accessing the file */
22063
 
  int flags;                /* Flags provided to os2Open() */
22064
 
  int locktype;             /* Type of lock currently held on this file */
22065
 
  int szChunk;              /* Chunk size configured by FCNTL_CHUNK_SIZE */
22066
 
  char *zFullPathCp;        /* Full path name of this file */
22067
 
  os2ShmLink *pShmLink;     /* Instance of shared memory on this file */
22068
 
};
22069
 
 
22070
 
#define LOCK_TIMEOUT 10L /* the default locking timeout */
22071
 
 
22072
 
/*
22073
 
** Missing from some versions of the OS/2 toolkit -
22074
 
** used to allocate from high memory if possible
22075
 
*/
22076
 
#ifndef OBJ_ANY
22077
 
# define OBJ_ANY 0x00000400
22078
 
#endif
22079
 
 
22080
 
/*****************************************************************************
22081
 
** The next group of routines implement the I/O methods specified
22082
 
** by the sqlite3_io_methods object.
22083
 
******************************************************************************/
22084
 
 
22085
 
/*
22086
 
** Close a file.
22087
 
*/
22088
 
static int os2Close( sqlite3_file *id ){
22089
 
  APIRET rc;
22090
 
  os2File *pFile = (os2File*)id;
22091
 
 
22092
 
  assert( id!=0 );
22093
 
  OSTRACE(( "CLOSE %d (%s)\n", pFile->h, pFile->zFullPathCp ));
22094
 
 
22095
 
  rc = DosClose( pFile->h );
22096
 
 
22097
 
  if( pFile->flags & SQLITE_OPEN_DELETEONCLOSE )
22098
 
    DosForceDelete( (PSZ)pFile->zFullPathCp );
22099
 
 
22100
 
  free( pFile->zFullPathCp );
22101
 
  pFile->zFullPathCp = NULL;
22102
 
  pFile->locktype = NO_LOCK;
22103
 
  pFile->h = (HFILE)-1;
22104
 
  pFile->flags = 0;
22105
 
 
22106
 
  OpenCounter( -1 );
22107
 
  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
22108
 
}
22109
 
 
22110
 
/*
22111
 
** Read data from a file into a buffer.  Return SQLITE_OK if all
22112
 
** bytes were read successfully and SQLITE_IOERR if anything goes
22113
 
** wrong.
22114
 
*/
22115
 
static int os2Read(
22116
 
  sqlite3_file *id,               /* File to read from */
22117
 
  void *pBuf,                     /* Write content into this buffer */
22118
 
  int amt,                        /* Number of bytes to read */
22119
 
  sqlite3_int64 offset            /* Begin reading at this offset */
22120
 
){
22121
 
  ULONG fileLocation = 0L;
22122
 
  ULONG got;
22123
 
  os2File *pFile = (os2File*)id;
22124
 
  assert( id!=0 );
22125
 
  SimulateIOError( return SQLITE_IOERR_READ );
22126
 
  OSTRACE(( "READ %d lock=%d\n", pFile->h, pFile->locktype ));
22127
 
  if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
22128
 
    return SQLITE_IOERR;
22129
 
  }
22130
 
  if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
22131
 
    return SQLITE_IOERR_READ;
22132
 
  }
22133
 
  if( got == (ULONG)amt )
22134
 
    return SQLITE_OK;
22135
 
  else {
22136
 
    /* Unread portions of the input buffer must be zero-filled */
22137
 
    memset(&((char*)pBuf)[got], 0, amt-got);
22138
 
    return SQLITE_IOERR_SHORT_READ;
22139
 
  }
22140
 
}
22141
 
 
22142
 
/*
22143
 
** Write data from a buffer into a file.  Return SQLITE_OK on success
22144
 
** or some other error code on failure.
22145
 
*/
22146
 
static int os2Write(
22147
 
  sqlite3_file *id,               /* File to write into */
22148
 
  const void *pBuf,               /* The bytes to be written */
22149
 
  int amt,                        /* Number of bytes to write */
22150
 
  sqlite3_int64 offset            /* Offset into the file to begin writing at */
22151
 
){
22152
 
  ULONG fileLocation = 0L;
22153
 
  APIRET rc = NO_ERROR;
22154
 
  ULONG wrote;
22155
 
  os2File *pFile = (os2File*)id;
22156
 
  assert( id!=0 );
22157
 
  SimulateIOError( return SQLITE_IOERR_WRITE );
22158
 
  SimulateDiskfullError( return SQLITE_FULL );
22159
 
  OSTRACE(( "WRITE %d lock=%d\n", pFile->h, pFile->locktype ));
22160
 
  if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
22161
 
    return SQLITE_IOERR;
22162
 
  }
22163
 
  assert( amt>0 );
22164
 
  while( amt > 0 &&
22165
 
         ( rc = DosWrite( pFile->h, (PVOID)pBuf, amt, &wrote ) ) == NO_ERROR &&
22166
 
         wrote > 0
22167
 
  ){
22168
 
    amt -= wrote;
22169
 
    pBuf = &((char*)pBuf)[wrote];
22170
 
  }
22171
 
 
22172
 
  return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
22173
 
}
22174
 
 
22175
 
/*
22176
 
** Truncate an open file to a specified size
22177
 
*/
22178
 
static int os2Truncate( sqlite3_file *id, i64 nByte ){
22179
 
  APIRET rc;
22180
 
  os2File *pFile = (os2File*)id;
22181
 
  assert( id!=0 );
22182
 
  OSTRACE(( "TRUNCATE %d %lld\n", pFile->h, nByte ));
22183
 
  SimulateIOError( return SQLITE_IOERR_TRUNCATE );
22184
 
 
22185
 
  /* If the user has configured a chunk-size for this file, truncate the
22186
 
  ** file so that it consists of an integer number of chunks (i.e. the
22187
 
  ** actual file size after the operation may be larger than the requested
22188
 
  ** size).
22189
 
  */
22190
 
  if( pFile->szChunk ){
22191
 
    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
22192
 
  }
22193
 
  
22194
 
  rc = DosSetFileSize( pFile->h, nByte );
22195
 
  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
22196
 
}
22197
 
 
22198
 
#ifdef SQLITE_TEST
22199
 
/*
22200
 
** Count the number of fullsyncs and normal syncs.  This is used to test
22201
 
** that syncs and fullsyncs are occuring at the right times.
22202
 
*/
22203
 
SQLITE_API int sqlite3_sync_count = 0;
22204
 
SQLITE_API int sqlite3_fullsync_count = 0;
22205
 
#endif
22206
 
 
22207
 
/*
22208
 
** Make sure all writes to a particular file are committed to disk.
22209
 
*/
22210
 
static int os2Sync( sqlite3_file *id, int flags ){
22211
 
  os2File *pFile = (os2File*)id;
22212
 
  OSTRACE(( "SYNC %d lock=%d\n", pFile->h, pFile->locktype ));
22213
 
#ifdef SQLITE_TEST
22214
 
  if( flags & SQLITE_SYNC_FULL){
22215
 
    sqlite3_fullsync_count++;
22216
 
  }
22217
 
  sqlite3_sync_count++;
22218
 
#endif
22219
 
  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
22220
 
  ** no-op
22221
 
  */
22222
 
#ifdef SQLITE_NO_SYNC
22223
 
  UNUSED_PARAMETER(pFile);
22224
 
  return SQLITE_OK;
22225
 
#else
22226
 
  return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
22227
 
#endif
22228
 
}
22229
 
 
22230
 
/*
22231
 
** Determine the current size of a file in bytes
22232
 
*/
22233
 
static int os2FileSize( sqlite3_file *id, sqlite3_int64 *pSize ){
22234
 
  APIRET rc = NO_ERROR;
22235
 
  FILESTATUS3 fsts3FileInfo;
22236
 
  memset(&fsts3FileInfo, 0, sizeof(fsts3FileInfo));
22237
 
  assert( id!=0 );
22238
 
  SimulateIOError( return SQLITE_IOERR_FSTAT );
22239
 
  rc = DosQueryFileInfo( ((os2File*)id)->h, FIL_STANDARD, &fsts3FileInfo, sizeof(FILESTATUS3) );
22240
 
  if( rc == NO_ERROR ){
22241
 
    *pSize = fsts3FileInfo.cbFile;
22242
 
    return SQLITE_OK;
22243
 
  }else{
22244
 
    return SQLITE_IOERR_FSTAT;
22245
 
  }
22246
 
}
22247
 
 
22248
 
/*
22249
 
** Acquire a reader lock.
22250
 
*/
22251
 
static int getReadLock( os2File *pFile ){
22252
 
  FILELOCK  LockArea,
22253
 
            UnlockArea;
22254
 
  APIRET res;
22255
 
  memset(&LockArea, 0, sizeof(LockArea));
22256
 
  memset(&UnlockArea, 0, sizeof(UnlockArea));
22257
 
  LockArea.lOffset = SHARED_FIRST;
22258
 
  LockArea.lRange = SHARED_SIZE;
22259
 
  UnlockArea.lOffset = 0L;
22260
 
  UnlockArea.lRange = 0L;
22261
 
  res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
22262
 
  OSTRACE(( "GETREADLOCK %d res=%d\n", pFile->h, res ));
22263
 
  return res;
22264
 
}
22265
 
 
22266
 
/*
22267
 
** Undo a readlock
22268
 
*/
22269
 
static int unlockReadLock( os2File *id ){
22270
 
  FILELOCK  LockArea,
22271
 
            UnlockArea;
22272
 
  APIRET res;
22273
 
  memset(&LockArea, 0, sizeof(LockArea));
22274
 
  memset(&UnlockArea, 0, sizeof(UnlockArea));
22275
 
  LockArea.lOffset = 0L;
22276
 
  LockArea.lRange = 0L;
22277
 
  UnlockArea.lOffset = SHARED_FIRST;
22278
 
  UnlockArea.lRange = SHARED_SIZE;
22279
 
  res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
22280
 
  OSTRACE(( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res ));
22281
 
  return res;
22282
 
}
22283
 
 
22284
 
/*
22285
 
** Lock the file with the lock specified by parameter locktype - one
22286
 
** of the following:
22287
 
**
22288
 
**     (1) SHARED_LOCK
22289
 
**     (2) RESERVED_LOCK
22290
 
**     (3) PENDING_LOCK
22291
 
**     (4) EXCLUSIVE_LOCK
22292
 
**
22293
 
** Sometimes when requesting one lock state, additional lock states
22294
 
** are inserted in between.  The locking might fail on one of the later
22295
 
** transitions leaving the lock state different from what it started but
22296
 
** still short of its goal.  The following chart shows the allowed
22297
 
** transitions and the inserted intermediate states:
22298
 
**
22299
 
**    UNLOCKED -> SHARED
22300
 
**    SHARED -> RESERVED
22301
 
**    SHARED -> (PENDING) -> EXCLUSIVE
22302
 
**    RESERVED -> (PENDING) -> EXCLUSIVE
22303
 
**    PENDING -> EXCLUSIVE
22304
 
**
22305
 
** This routine will only increase a lock.  The os2Unlock() routine
22306
 
** erases all locks at once and returns us immediately to locking level 0.
22307
 
** It is not possible to lower the locking level one step at a time.  You
22308
 
** must go straight to locking level 0.
22309
 
*/
22310
 
static int os2Lock( sqlite3_file *id, int locktype ){
22311
 
  int rc = SQLITE_OK;       /* Return code from subroutines */
22312
 
  APIRET res = NO_ERROR;    /* Result of an OS/2 lock call */
22313
 
  int newLocktype;       /* Set pFile->locktype to this value before exiting */
22314
 
  int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
22315
 
  FILELOCK  LockArea,
22316
 
            UnlockArea;
22317
 
  os2File *pFile = (os2File*)id;
22318
 
  memset(&LockArea, 0, sizeof(LockArea));
22319
 
  memset(&UnlockArea, 0, sizeof(UnlockArea));
22320
 
  assert( pFile!=0 );
22321
 
  OSTRACE(( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype ));
22322
 
 
22323
 
  /* If there is already a lock of this type or more restrictive on the
22324
 
  ** os2File, do nothing. Don't use the end_lock: exit path, as
22325
 
  ** sqlite3_mutex_enter() hasn't been called yet.
22326
 
  */
22327
 
  if( pFile->locktype>=locktype ){
22328
 
    OSTRACE(( "LOCK %d %d ok (already held)\n", pFile->h, locktype ));
22329
 
    return SQLITE_OK;
22330
 
  }
22331
 
 
22332
 
  /* Make sure the locking sequence is correct
22333
 
  */
22334
 
  assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
22335
 
  assert( locktype!=PENDING_LOCK );
22336
 
  assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
22337
 
 
22338
 
  /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
22339
 
  ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
22340
 
  ** the PENDING_LOCK byte is temporary.
22341
 
  */
22342
 
  newLocktype = pFile->locktype;
22343
 
  if( pFile->locktype==NO_LOCK
22344
 
      || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
22345
 
  ){
22346
 
    LockArea.lOffset = PENDING_BYTE;
22347
 
    LockArea.lRange = 1L;
22348
 
    UnlockArea.lOffset = 0L;
22349
 
    UnlockArea.lRange = 0L;
22350
 
 
22351
 
    /* wait longer than LOCK_TIMEOUT here not to have to try multiple times */
22352
 
    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 100L, 0L );
22353
 
    if( res == NO_ERROR ){
22354
 
      gotPendingLock = 1;
22355
 
      OSTRACE(( "LOCK %d pending lock boolean set.  res=%d\n", pFile->h, res ));
22356
 
    }
22357
 
  }
22358
 
 
22359
 
  /* Acquire a shared lock
22360
 
  */
22361
 
  if( locktype==SHARED_LOCK && res == NO_ERROR ){
22362
 
    assert( pFile->locktype==NO_LOCK );
22363
 
    res = getReadLock(pFile);
22364
 
    if( res == NO_ERROR ){
22365
 
      newLocktype = SHARED_LOCK;
22366
 
    }
22367
 
    OSTRACE(( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res ));
22368
 
  }
22369
 
 
22370
 
  /* Acquire a RESERVED lock
22371
 
  */
22372
 
  if( locktype==RESERVED_LOCK && res == NO_ERROR ){
22373
 
    assert( pFile->locktype==SHARED_LOCK );
22374
 
    LockArea.lOffset = RESERVED_BYTE;
22375
 
    LockArea.lRange = 1L;
22376
 
    UnlockArea.lOffset = 0L;
22377
 
    UnlockArea.lRange = 0L;
22378
 
    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22379
 
    if( res == NO_ERROR ){
22380
 
      newLocktype = RESERVED_LOCK;
22381
 
    }
22382
 
    OSTRACE(( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res ));
22383
 
  }
22384
 
 
22385
 
  /* Acquire a PENDING lock
22386
 
  */
22387
 
  if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
22388
 
    newLocktype = PENDING_LOCK;
22389
 
    gotPendingLock = 0;
22390
 
    OSTRACE(( "LOCK %d acquire pending lock. pending lock boolean unset.\n",
22391
 
               pFile->h ));
22392
 
  }
22393
 
 
22394
 
  /* Acquire an EXCLUSIVE lock
22395
 
  */
22396
 
  if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
22397
 
    assert( pFile->locktype>=SHARED_LOCK );
22398
 
    res = unlockReadLock(pFile);
22399
 
    OSTRACE(( "unreadlock = %d\n", res ));
22400
 
    LockArea.lOffset = SHARED_FIRST;
22401
 
    LockArea.lRange = SHARED_SIZE;
22402
 
    UnlockArea.lOffset = 0L;
22403
 
    UnlockArea.lRange = 0L;
22404
 
    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22405
 
    if( res == NO_ERROR ){
22406
 
      newLocktype = EXCLUSIVE_LOCK;
22407
 
    }else{
22408
 
      OSTRACE(( "OS/2 error-code = %d\n", res ));
22409
 
      getReadLock(pFile);
22410
 
    }
22411
 
    OSTRACE(( "LOCK %d acquire exclusive lock.  res=%d\n", pFile->h, res ));
22412
 
  }
22413
 
 
22414
 
  /* If we are holding a PENDING lock that ought to be released, then
22415
 
  ** release it now.
22416
 
  */
22417
 
  if( gotPendingLock && locktype==SHARED_LOCK ){
22418
 
    int r;
22419
 
    LockArea.lOffset = 0L;
22420
 
    LockArea.lRange = 0L;
22421
 
    UnlockArea.lOffset = PENDING_BYTE;
22422
 
    UnlockArea.lRange = 1L;
22423
 
    r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22424
 
    OSTRACE(( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r ));
22425
 
  }
22426
 
 
22427
 
  /* Update the state of the lock has held in the file descriptor then
22428
 
  ** return the appropriate result code.
22429
 
  */
22430
 
  if( res == NO_ERROR ){
22431
 
    rc = SQLITE_OK;
22432
 
  }else{
22433
 
    OSTRACE(( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
22434
 
              locktype, newLocktype ));
22435
 
    rc = SQLITE_BUSY;
22436
 
  }
22437
 
  pFile->locktype = newLocktype;
22438
 
  OSTRACE(( "LOCK %d now %d\n", pFile->h, pFile->locktype ));
22439
 
  return rc;
22440
 
}
22441
 
 
22442
 
/*
22443
 
** This routine checks if there is a RESERVED lock held on the specified
22444
 
** file by this or any other process. If such a lock is held, return
22445
 
** non-zero, otherwise zero.
22446
 
*/
22447
 
static int os2CheckReservedLock( sqlite3_file *id, int *pOut ){
22448
 
  int r = 0;
22449
 
  os2File *pFile = (os2File*)id;
22450
 
  assert( pFile!=0 );
22451
 
  if( pFile->locktype>=RESERVED_LOCK ){
22452
 
    r = 1;
22453
 
    OSTRACE(( "TEST WR-LOCK %d %d (local)\n", pFile->h, r ));
22454
 
  }else{
22455
 
    FILELOCK  LockArea,
22456
 
              UnlockArea;
22457
 
    APIRET rc = NO_ERROR;
22458
 
    memset(&LockArea, 0, sizeof(LockArea));
22459
 
    memset(&UnlockArea, 0, sizeof(UnlockArea));
22460
 
    LockArea.lOffset = RESERVED_BYTE;
22461
 
    LockArea.lRange = 1L;
22462
 
    UnlockArea.lOffset = 0L;
22463
 
    UnlockArea.lRange = 0L;
22464
 
    rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22465
 
    OSTRACE(( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc ));
22466
 
    if( rc == NO_ERROR ){
22467
 
      APIRET rcu = NO_ERROR; /* return code for unlocking */
22468
 
      LockArea.lOffset = 0L;
22469
 
      LockArea.lRange = 0L;
22470
 
      UnlockArea.lOffset = RESERVED_BYTE;
22471
 
      UnlockArea.lRange = 1L;
22472
 
      rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22473
 
      OSTRACE(( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu ));
22474
 
    }
22475
 
    r = !(rc == NO_ERROR);
22476
 
    OSTRACE(( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r ));
22477
 
  }
22478
 
  *pOut = r;
22479
 
  return SQLITE_OK;
22480
 
}
22481
 
 
22482
 
/*
22483
 
** Lower the locking level on file descriptor id to locktype.  locktype
22484
 
** must be either NO_LOCK or SHARED_LOCK.
22485
 
**
22486
 
** If the locking level of the file descriptor is already at or below
22487
 
** the requested locking level, this routine is a no-op.
22488
 
**
22489
 
** It is not possible for this routine to fail if the second argument
22490
 
** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
22491
 
** might return SQLITE_IOERR;
22492
 
*/
22493
 
static int os2Unlock( sqlite3_file *id, int locktype ){
22494
 
  int type;
22495
 
  os2File *pFile = (os2File*)id;
22496
 
  APIRET rc = SQLITE_OK;
22497
 
  APIRET res = NO_ERROR;
22498
 
  FILELOCK  LockArea,
22499
 
            UnlockArea;
22500
 
  memset(&LockArea, 0, sizeof(LockArea));
22501
 
  memset(&UnlockArea, 0, sizeof(UnlockArea));
22502
 
  assert( pFile!=0 );
22503
 
  assert( locktype<=SHARED_LOCK );
22504
 
  OSTRACE(( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype ));
22505
 
  type = pFile->locktype;
22506
 
  if( type>=EXCLUSIVE_LOCK ){
22507
 
    LockArea.lOffset = 0L;
22508
 
    LockArea.lRange = 0L;
22509
 
    UnlockArea.lOffset = SHARED_FIRST;
22510
 
    UnlockArea.lRange = SHARED_SIZE;
22511
 
    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22512
 
    OSTRACE(( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res ));
22513
 
    if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
22514
 
      /* This should never happen.  We should always be able to
22515
 
      ** reacquire the read lock */
22516
 
      OSTRACE(( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype ));
22517
 
      rc = SQLITE_IOERR_UNLOCK;
22518
 
    }
22519
 
  }
22520
 
  if( type>=RESERVED_LOCK ){
22521
 
    LockArea.lOffset = 0L;
22522
 
    LockArea.lRange = 0L;
22523
 
    UnlockArea.lOffset = RESERVED_BYTE;
22524
 
    UnlockArea.lRange = 1L;
22525
 
    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22526
 
    OSTRACE(( "UNLOCK %d reserved res=%d\n", pFile->h, res ));
22527
 
  }
22528
 
  if( locktype==NO_LOCK && type>=SHARED_LOCK ){
22529
 
    res = unlockReadLock(pFile);
22530
 
    OSTRACE(( "UNLOCK %d is %d want %d res=%d\n",
22531
 
              pFile->h, type, locktype, res ));
22532
 
  }
22533
 
  if( type>=PENDING_LOCK ){
22534
 
    LockArea.lOffset = 0L;
22535
 
    LockArea.lRange = 0L;
22536
 
    UnlockArea.lOffset = PENDING_BYTE;
22537
 
    UnlockArea.lRange = 1L;
22538
 
    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22539
 
    OSTRACE(( "UNLOCK %d pending res=%d\n", pFile->h, res ));
22540
 
  }
22541
 
  pFile->locktype = locktype;
22542
 
  OSTRACE(( "UNLOCK %d now %d\n", pFile->h, pFile->locktype ));
22543
 
  return rc;
22544
 
}
22545
 
 
22546
 
/*
22547
 
** Control and query of the open file handle.
22548
 
*/
22549
 
static int os2FileControl(sqlite3_file *id, int op, void *pArg){
22550
 
  switch( op ){
22551
 
    case SQLITE_FCNTL_LOCKSTATE: {
22552
 
      *(int*)pArg = ((os2File*)id)->locktype;
22553
 
      OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
22554
 
                ((os2File*)id)->h, ((os2File*)id)->locktype ));
22555
 
      return SQLITE_OK;
22556
 
    }
22557
 
    case SQLITE_FCNTL_CHUNK_SIZE: {
22558
 
      ((os2File*)id)->szChunk = *(int*)pArg;
22559
 
      return SQLITE_OK;
22560
 
    }
22561
 
    case SQLITE_FCNTL_SIZE_HINT: {
22562
 
      sqlite3_int64 sz = *(sqlite3_int64*)pArg;
22563
 
      SimulateIOErrorBenign(1);
22564
 
      os2Truncate(id, sz);
22565
 
      SimulateIOErrorBenign(0);
22566
 
      return SQLITE_OK;
22567
 
    }
22568
 
    case SQLITE_FCNTL_SYNC_OMITTED: {
22569
 
      return SQLITE_OK;
22570
 
    }
22571
 
  }
22572
 
  return SQLITE_NOTFOUND;
22573
 
}
22574
 
 
22575
 
/*
22576
 
** Return the sector size in bytes of the underlying block device for
22577
 
** the specified file. This is almost always 512 bytes, but may be
22578
 
** larger for some devices.
22579
 
**
22580
 
** SQLite code assumes this function cannot fail. It also assumes that
22581
 
** if two files are created in the same file-system directory (i.e.
22582
 
** a database and its journal file) that the sector size will be the
22583
 
** same for both.
22584
 
*/
22585
 
static int os2SectorSize(sqlite3_file *id){
22586
 
  UNUSED_PARAMETER(id);
22587
 
  return SQLITE_DEFAULT_SECTOR_SIZE;
22588
 
}
22589
 
 
22590
 
/*
22591
 
** Return a vector of device characteristics.
22592
 
*/
22593
 
static int os2DeviceCharacteristics(sqlite3_file *id){
22594
 
  UNUSED_PARAMETER(id);
22595
 
  return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
22596
 
}
22597
 
 
22598
 
 
22599
 
/*
22600
 
** Character set conversion objects used by conversion routines.
22601
 
*/
22602
 
static UconvObject ucUtf8 = NULL; /* convert between UTF-8 and UCS-2 */
22603
 
static UconvObject uclCp = NULL;  /* convert between local codepage and UCS-2 */
22604
 
 
22605
 
/*
22606
 
** Helper function to initialize the conversion objects from and to UTF-8.
22607
 
*/
22608
 
static void initUconvObjects( void ){
22609
 
  if( UniCreateUconvObject( UTF_8, &ucUtf8 ) != ULS_SUCCESS )
22610
 
    ucUtf8 = NULL;
22611
 
  if ( UniCreateUconvObject( (UniChar *)L"@path=yes", &uclCp ) != ULS_SUCCESS )
22612
 
    uclCp = NULL;
22613
 
}
22614
 
 
22615
 
/*
22616
 
** Helper function to free the conversion objects from and to UTF-8.
22617
 
*/
22618
 
static void freeUconvObjects( void ){
22619
 
  if ( ucUtf8 )
22620
 
    UniFreeUconvObject( ucUtf8 );
22621
 
  if ( uclCp )
22622
 
    UniFreeUconvObject( uclCp );
22623
 
  ucUtf8 = NULL;
22624
 
  uclCp = NULL;
22625
 
}
22626
 
 
22627
 
/*
22628
 
** Helper function to convert UTF-8 filenames to local OS/2 codepage.
22629
 
** The two-step process: first convert the incoming UTF-8 string
22630
 
** into UCS-2 and then from UCS-2 to the current codepage.
22631
 
** The returned char pointer has to be freed.
22632
 
*/
22633
 
static char *convertUtf8PathToCp( const char *in ){
22634
 
  UniChar tempPath[CCHMAXPATH];
22635
 
  char *out = (char *)calloc( CCHMAXPATH, 1 );
22636
 
 
22637
 
  if( !out )
22638
 
    return NULL;
22639
 
 
22640
 
  if( !ucUtf8 || !uclCp )
22641
 
    initUconvObjects();
22642
 
 
22643
 
  /* determine string for the conversion of UTF-8 which is CP1208 */
22644
 
  if( UniStrToUcs( ucUtf8, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
22645
 
    return out; /* if conversion fails, return the empty string */
22646
 
 
22647
 
  /* conversion for current codepage which can be used for paths */
22648
 
  UniStrFromUcs( uclCp, out, tempPath, CCHMAXPATH );
22649
 
 
22650
 
  return out;
22651
 
}
22652
 
 
22653
 
/*
22654
 
** Helper function to convert filenames from local codepage to UTF-8.
22655
 
** The two-step process: first convert the incoming codepage-specific
22656
 
** string into UCS-2 and then from UCS-2 to the codepage of UTF-8.
22657
 
** The returned char pointer has to be freed.
22658
 
**
22659
 
** This function is non-static to be able to use this in shell.c and
22660
 
** similar applications that take command line arguments.
22661
 
*/
22662
 
char *convertCpPathToUtf8( const char *in ){
22663
 
  UniChar tempPath[CCHMAXPATH];
22664
 
  char *out = (char *)calloc( CCHMAXPATH, 1 );
22665
 
 
22666
 
  if( !out )
22667
 
    return NULL;
22668
 
 
22669
 
  if( !ucUtf8 || !uclCp )
22670
 
    initUconvObjects();
22671
 
 
22672
 
  /* conversion for current codepage which can be used for paths */
22673
 
  if( UniStrToUcs( uclCp, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
22674
 
    return out; /* if conversion fails, return the empty string */
22675
 
 
22676
 
  /* determine string for the conversion of UTF-8 which is CP1208 */
22677
 
  UniStrFromUcs( ucUtf8, out, tempPath, CCHMAXPATH );
22678
 
 
22679
 
  return out;
22680
 
}
22681
 
 
22682
 
 
22683
 
#ifndef SQLITE_OMIT_WAL
22684
 
 
22685
 
/*
22686
 
** Use main database file for interprocess locking. If un-defined
22687
 
** a separate file is created for this purpose. The file will be
22688
 
** used only to set file locks. There will be no data written to it.
22689
 
*/
22690
 
#define SQLITE_OS2_NO_WAL_LOCK_FILE     
22691
 
 
22692
 
#if 0
22693
 
static void _ERR_TRACE( const char *fmt, ... ) {
22694
 
  va_list  ap;
22695
 
  va_start(ap, fmt);
22696
 
  vfprintf(stderr, fmt, ap);
22697
 
  fflush(stderr);
22698
 
}
22699
 
#define ERR_TRACE(rc, msg)        \
22700
 
        if( (rc) != SQLITE_OK ) _ERR_TRACE msg;
22701
 
#else
22702
 
#define ERR_TRACE(rc, msg)
22703
 
#endif
22704
 
 
22705
 
/*
22706
 
** Helper functions to obtain and relinquish the global mutex. The
22707
 
** global mutex is used to protect os2ShmNodeList.
22708
 
**
22709
 
** Function os2ShmMutexHeld() is used to assert() that the global mutex 
22710
 
** is held when required. This function is only used as part of assert() 
22711
 
** statements. e.g.
22712
 
**
22713
 
**   os2ShmEnterMutex()
22714
 
**     assert( os2ShmMutexHeld() );
22715
 
**   os2ShmLeaveMutex()
22716
 
*/
22717
 
static void os2ShmEnterMutex(void){
22718
 
  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
22719
 
}
22720
 
static void os2ShmLeaveMutex(void){
22721
 
  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
22722
 
}
22723
 
#ifdef SQLITE_DEBUG
22724
 
static int os2ShmMutexHeld(void) {
22725
 
  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
22726
 
}
22727
 
int GetCurrentProcessId(void) {
22728
 
  PPIB pib;
22729
 
  DosGetInfoBlocks(NULL, &pib);
22730
 
  return (int)pib->pib_ulpid;
22731
 
}
22732
 
#endif
22733
 
 
22734
 
/*
22735
 
** Object used to represent a the shared memory area for a single log file.
22736
 
** When multiple threads all reference the same log-summary, each thread has
22737
 
** its own os2File object, but they all point to a single instance of this 
22738
 
** object.  In other words, each log-summary is opened only once per process.
22739
 
**
22740
 
** os2ShmMutexHeld() must be true when creating or destroying
22741
 
** this object or while reading or writing the following fields:
22742
 
**
22743
 
**      nRef
22744
 
**      pNext 
22745
 
**
22746
 
** The following fields are read-only after the object is created:
22747
 
** 
22748
 
**      szRegion
22749
 
**      hLockFile
22750
 
**      shmBaseName
22751
 
**
22752
 
** Either os2ShmNode.mutex must be held or os2ShmNode.nRef==0 and
22753
 
** os2ShmMutexHeld() is true when reading or writing any other field
22754
 
** in this structure.
22755
 
**
22756
 
*/
22757
 
struct os2ShmNode {
22758
 
  sqlite3_mutex *mutex;      /* Mutex to access this object */
22759
 
  os2ShmNode *pNext;         /* Next in list of all os2ShmNode objects */
22760
 
 
22761
 
  int szRegion;              /* Size of shared-memory regions */
22762
 
 
22763
 
  int nRegion;               /* Size of array apRegion */
22764
 
  void **apRegion;           /* Array of pointers to shared-memory regions */
22765
 
 
22766
 
  int nRef;                  /* Number of os2ShmLink objects pointing to this */
22767
 
  os2ShmLink *pFirst;        /* First os2ShmLink object pointing to this */
22768
 
 
22769
 
  HFILE hLockFile;           /* File used for inter-process memory locking */
22770
 
  char shmBaseName[1];       /* Name of the memory object !!! must last !!! */
22771
 
};
22772
 
 
22773
 
 
22774
 
/*
22775
 
** Structure used internally by this VFS to record the state of an
22776
 
** open shared memory connection.
22777
 
**
22778
 
** The following fields are initialized when this object is created and
22779
 
** are read-only thereafter:
22780
 
**
22781
 
**    os2Shm.pShmNode
22782
 
**    os2Shm.id
22783
 
**
22784
 
** All other fields are read/write.  The os2Shm.pShmNode->mutex must be held
22785
 
** while accessing any read/write fields.
22786
 
*/
22787
 
struct os2ShmLink {
22788
 
  os2ShmNode *pShmNode;      /* The underlying os2ShmNode object */
22789
 
  os2ShmLink *pNext;         /* Next os2Shm with the same os2ShmNode */
22790
 
  u32 sharedMask;            /* Mask of shared locks held */
22791
 
  u32 exclMask;              /* Mask of exclusive locks held */
22792
 
#ifdef SQLITE_DEBUG
22793
 
  u8 id;                     /* Id of this connection with its os2ShmNode */
22794
 
#endif
22795
 
};
22796
 
 
22797
 
 
22798
 
/*
22799
 
** A global list of all os2ShmNode objects.
22800
 
**
22801
 
** The os2ShmMutexHeld() must be true while reading or writing this list.
22802
 
*/
22803
 
static os2ShmNode *os2ShmNodeList = NULL;
22804
 
 
22805
 
/*
22806
 
** Constants used for locking
22807
 
*/
22808
 
#ifdef  SQLITE_OS2_NO_WAL_LOCK_FILE
22809
 
#define OS2_SHM_BASE   (PENDING_BYTE + 0x10000)         /* first lock byte */
22810
 
#else
22811
 
#define OS2_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
22812
 
#endif
22813
 
 
22814
 
#define OS2_SHM_DMS    (OS2_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
22815
 
 
22816
 
/*
22817
 
** Apply advisory locks for all n bytes beginning at ofst.
22818
 
*/
22819
 
#define _SHM_UNLCK  1   /* no lock */
22820
 
#define _SHM_RDLCK  2   /* shared lock, no wait */
22821
 
#define _SHM_WRLCK  3   /* exlusive lock, no wait */
22822
 
#define _SHM_WRLCK_WAIT 4 /* exclusive lock, wait */
22823
 
static int os2ShmSystemLock(
22824
 
  os2ShmNode *pNode,    /* Apply locks to this open shared-memory segment */
22825
 
  int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, _SHM_WRLCK or _SHM_WRLCK_WAIT */
22826
 
  int ofst,             /* Offset to first byte to be locked/unlocked */
22827
 
  int nByte             /* Number of bytes to lock or unlock */
22828
 
){
22829
 
  APIRET rc;
22830
 
  FILELOCK area;
22831
 
  ULONG mode, timeout;
22832
 
 
22833
 
  /* Access to the os2ShmNode object is serialized by the caller */
22834
 
  assert( sqlite3_mutex_held(pNode->mutex) || pNode->nRef==0 );
22835
 
 
22836
 
  mode = 1;     /* shared lock */
22837
 
  timeout = 0;  /* no wait */
22838
 
  area.lOffset = ofst;
22839
 
  area.lRange = nByte;
22840
 
 
22841
 
  switch( lockType ) {
22842
 
    case _SHM_WRLCK_WAIT:
22843
 
      timeout = (ULONG)-1;      /* wait forever */
22844
 
    case _SHM_WRLCK:
22845
 
      mode = 0;                 /* exclusive lock */
22846
 
    case _SHM_RDLCK:
22847
 
      rc = DosSetFileLocks(pNode->hLockFile, 
22848
 
                           NULL, &area, timeout, mode);
22849
 
      break;
22850
 
    /* case _SHM_UNLCK: */
22851
 
    default:
22852
 
      rc = DosSetFileLocks(pNode->hLockFile, 
22853
 
                           &area, NULL, 0, 0);
22854
 
      break;
22855
 
  }
22856
 
                          
22857
 
  OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n", 
22858
 
           pNode->hLockFile,
22859
 
           rc==SQLITE_OK ? "ok" : "failed",
22860
 
           lockType==_SHM_UNLCK ? "Unlock" : "Lock",
22861
 
           rc));
22862
 
 
22863
 
  ERR_TRACE(rc, ("os2ShmSystemLock: %d %s\n", rc, pNode->shmBaseName))
22864
 
 
22865
 
  return ( rc == 0 ) ?  SQLITE_OK : SQLITE_BUSY;
22866
 
}
22867
 
 
22868
 
/*
22869
 
** Find an os2ShmNode in global list or allocate a new one, if not found.
22870
 
**
22871
 
** This is not a VFS shared-memory method; it is a utility function called
22872
 
** by VFS shared-memory methods.
22873
 
*/
22874
 
static int os2OpenSharedMemory( os2File *fd, int szRegion ) {
22875
 
  os2ShmLink *pLink;
22876
 
  os2ShmNode *pNode;
22877
 
  int cbShmName, rc = SQLITE_OK;
22878
 
  char shmName[CCHMAXPATH + 30];
22879
 
#ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
22880
 
  ULONG action;
22881
 
#endif
22882
 
  
22883
 
  /* We need some additional space at the end to append the region number */
22884
 
  cbShmName = sprintf(shmName, "\\SHAREMEM\\%s", fd->zFullPathCp );
22885
 
  if( cbShmName >= CCHMAXPATH-8 )
22886
 
    return SQLITE_IOERR_SHMOPEN; 
22887
 
 
22888
 
  /* Replace colon in file name to form a valid shared memory name */
22889
 
  shmName[10+1] = '!';
22890
 
 
22891
 
  /* Allocate link object (we free it later in case of failure) */
22892
 
  pLink = sqlite3_malloc( sizeof(*pLink) );
22893
 
  if( !pLink )
22894
 
    return SQLITE_NOMEM;
22895
 
 
22896
 
  /* Access node list */
22897
 
  os2ShmEnterMutex();
22898
 
 
22899
 
  /* Find node by it's shared memory base name */
22900
 
  for( pNode = os2ShmNodeList; 
22901
 
       pNode && stricmp(shmName, pNode->shmBaseName) != 0; 
22902
 
       pNode = pNode->pNext )   ;
22903
 
 
22904
 
  /* Not found: allocate a new node */
22905
 
  if( !pNode ) {
22906
 
    pNode = sqlite3_malloc( sizeof(*pNode) + cbShmName );
22907
 
    if( pNode ) {
22908
 
      memset(pNode, 0, sizeof(*pNode) );
22909
 
      pNode->szRegion = szRegion;
22910
 
      pNode->hLockFile = (HFILE)-1;      
22911
 
      strcpy(pNode->shmBaseName, shmName);
22912
 
 
22913
 
#ifdef SQLITE_OS2_NO_WAL_LOCK_FILE
22914
 
      if( DosDupHandle(fd->h, &pNode->hLockFile) != 0 ) {
22915
 
#else
22916
 
      sprintf(shmName, "%s-lck", fd->zFullPathCp);
22917
 
      if( DosOpen((PSZ)shmName, &pNode->hLockFile, &action, 0, FILE_NORMAL, 
22918
 
                  OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
22919
 
                  OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE | 
22920
 
                  OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR,
22921
 
                  NULL) != 0 ) {
22922
 
#endif
22923
 
        sqlite3_free(pNode);  
22924
 
        rc = SQLITE_IOERR;
22925
 
      } else {
22926
 
        pNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
22927
 
        if( !pNode->mutex ) {
22928
 
          sqlite3_free(pNode);  
22929
 
          rc = SQLITE_NOMEM;
22930
 
        }
22931
 
      }   
22932
 
    } else {
22933
 
      rc = SQLITE_NOMEM;
22934
 
    }
22935
 
    
22936
 
    if( rc == SQLITE_OK ) {
22937
 
      pNode->pNext = os2ShmNodeList;
22938
 
      os2ShmNodeList = pNode;
22939
 
    } else {
22940
 
      pNode = NULL;
22941
 
    }
22942
 
  } else if( pNode->szRegion != szRegion ) {
22943
 
    rc = SQLITE_IOERR_SHMSIZE;
22944
 
    pNode = NULL;
22945
 
  }
22946
 
 
22947
 
  if( pNode ) {
22948
 
    sqlite3_mutex_enter(pNode->mutex);
22949
 
 
22950
 
    memset(pLink, 0, sizeof(*pLink));
22951
 
 
22952
 
    pLink->pShmNode = pNode;
22953
 
    pLink->pNext = pNode->pFirst;
22954
 
    pNode->pFirst = pLink;
22955
 
    pNode->nRef++;
22956
 
 
22957
 
    fd->pShmLink = pLink;
22958
 
 
22959
 
    sqlite3_mutex_leave(pNode->mutex);
22960
 
    
22961
 
  } else {
22962
 
    /* Error occured. Free our link object. */
22963
 
    sqlite3_free(pLink);  
22964
 
  }
22965
 
 
22966
 
  os2ShmLeaveMutex();
22967
 
 
22968
 
  ERR_TRACE(rc, ("os2OpenSharedMemory: %d  %s\n", rc, fd->zFullPathCp))  
22969
 
  
22970
 
  return rc;
22971
 
}
22972
 
 
22973
 
/*
22974
 
** Purge the os2ShmNodeList list of all entries with nRef==0.
22975
 
**
22976
 
** This is not a VFS shared-memory method; it is a utility function called
22977
 
** by VFS shared-memory methods.
22978
 
*/
22979
 
static void os2PurgeShmNodes( int deleteFlag ) {
22980
 
  os2ShmNode *pNode;
22981
 
  os2ShmNode **ppNode;
22982
 
 
22983
 
  os2ShmEnterMutex();
22984
 
  
22985
 
  ppNode = &os2ShmNodeList;
22986
 
 
22987
 
  while( *ppNode ) {
22988
 
    pNode = *ppNode;
22989
 
 
22990
 
    if( pNode->nRef == 0 ) {
22991
 
      *ppNode = pNode->pNext;   
22992
 
     
22993
 
      if( pNode->apRegion ) {
22994
 
        /* Prevent other processes from resizing the shared memory */
22995
 
        os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
22996
 
 
22997
 
        while( pNode->nRegion-- ) {
22998
 
#ifdef SQLITE_DEBUG
22999
 
          int rc = 
23000
 
#endif          
23001
 
          DosFreeMem(pNode->apRegion[pNode->nRegion]);
23002
 
 
23003
 
          OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
23004
 
                  (int)GetCurrentProcessId(), pNode->nRegion,
23005
 
                  rc == 0 ? "ok" : "failed"));
23006
 
        }
23007
 
 
23008
 
        /* Allow other processes to resize the shared memory */
23009
 
        os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
23010
 
 
23011
 
        sqlite3_free(pNode->apRegion);
23012
 
      }  
23013
 
 
23014
 
      DosClose(pNode->hLockFile);
23015
 
      
23016
 
#ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
23017
 
      if( deleteFlag ) {
23018
 
         char fileName[CCHMAXPATH];
23019
 
         /* Skip "\\SHAREMEM\\" */
23020
 
         sprintf(fileName, "%s-lck", pNode->shmBaseName + 10);
23021
 
         /* restore colon */
23022
 
         fileName[1] = ':';
23023
 
         
23024
 
         DosForceDelete(fileName); 
23025
 
      }
23026
 
#endif
23027
 
 
23028
 
      sqlite3_mutex_free(pNode->mutex);
23029
 
 
23030
 
      sqlite3_free(pNode);
23031
 
      
23032
 
    } else {
23033
 
      ppNode = &pNode->pNext;
23034
 
    }
23035
 
  } 
23036
 
 
23037
 
  os2ShmLeaveMutex();
23038
 
}
23039
 
 
23040
 
/*
23041
 
** This function is called to obtain a pointer to region iRegion of the
23042
 
** shared-memory associated with the database file id. Shared-memory regions
23043
 
** are numbered starting from zero. Each shared-memory region is szRegion
23044
 
** bytes in size.
23045
 
**
23046
 
** If an error occurs, an error code is returned and *pp is set to NULL.
23047
 
**
23048
 
** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
23049
 
** region has not been allocated (by any client, including one running in a
23050
 
** separate process), then *pp is set to NULL and SQLITE_OK returned. If
23051
 
** bExtend is non-zero and the requested shared-memory region has not yet
23052
 
** been allocated, it is allocated by this function.
23053
 
**
23054
 
** If the shared-memory region has already been allocated or is allocated by
23055
 
** this call as described above, then it is mapped into this processes
23056
 
** address space (if it is not already), *pp is set to point to the mapped
23057
 
** memory and SQLITE_OK returned.
23058
 
*/
23059
 
static int os2ShmMap(
23060
 
  sqlite3_file *id,               /* Handle open on database file */
23061
 
  int iRegion,                    /* Region to retrieve */
23062
 
  int szRegion,                   /* Size of regions */
23063
 
  int bExtend,                    /* True to extend block if necessary */
23064
 
  void volatile **pp              /* OUT: Mapped memory */
23065
 
){
23066
 
  PVOID pvTemp;
23067
 
  void **apRegion;
23068
 
  os2ShmNode *pNode;
23069
 
  int n, rc = SQLITE_OK;
23070
 
  char shmName[CCHMAXPATH];
23071
 
  os2File *pFile = (os2File*)id;
23072
 
  
23073
 
  *pp = NULL;
23074
 
 
23075
 
  if( !pFile->pShmLink )
23076
 
    rc = os2OpenSharedMemory( pFile, szRegion );
23077
 
  
23078
 
  if( rc == SQLITE_OK ) {
23079
 
    pNode = pFile->pShmLink->pShmNode ;
23080
 
    
23081
 
    sqlite3_mutex_enter(pNode->mutex);
23082
 
    
23083
 
    assert( szRegion==pNode->szRegion );
23084
 
 
23085
 
    /* Unmapped region ? */
23086
 
    if( iRegion >= pNode->nRegion ) {
23087
 
      /* Prevent other processes from resizing the shared memory */
23088
 
      os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
23089
 
 
23090
 
      apRegion = sqlite3_realloc(
23091
 
        pNode->apRegion, (iRegion + 1) * sizeof(apRegion[0]));
23092
 
 
23093
 
      if( apRegion ) {
23094
 
        pNode->apRegion = apRegion;
23095
 
 
23096
 
        while( pNode->nRegion <= iRegion ) {
23097
 
          sprintf(shmName, "%s-%u", 
23098
 
                  pNode->shmBaseName, pNode->nRegion);
23099
 
 
23100
 
          if( DosGetNamedSharedMem(&pvTemp, (PSZ)shmName, 
23101
 
                PAG_READ | PAG_WRITE) != NO_ERROR ) {
23102
 
            if( !bExtend )
23103
 
              break;
23104
 
 
23105
 
            if( DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
23106
 
                  PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_ANY) != NO_ERROR && 
23107
 
                DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
23108
 
                  PAG_READ | PAG_WRITE | PAG_COMMIT) != NO_ERROR ) { 
23109
 
              rc = SQLITE_NOMEM;
23110
 
              break;
23111
 
            }
23112
 
          }
23113
 
 
23114
 
          apRegion[pNode->nRegion++] = pvTemp;
23115
 
        }
23116
 
 
23117
 
        /* zero out remaining entries */ 
23118
 
        for( n = pNode->nRegion; n <= iRegion; n++ )
23119
 
          pNode->apRegion[n] = NULL;
23120
 
 
23121
 
        /* Return this region (maybe zero) */
23122
 
        *pp = pNode->apRegion[iRegion];
23123
 
      } else {
23124
 
        rc = SQLITE_NOMEM;
23125
 
      }
23126
 
 
23127
 
      /* Allow other processes to resize the shared memory */
23128
 
      os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
23129
 
      
23130
 
    } else {
23131
 
      /* Region has been mapped previously */
23132
 
      *pp = pNode->apRegion[iRegion];
23133
 
    }
23134
 
 
23135
 
    sqlite3_mutex_leave(pNode->mutex);
23136
 
  } 
23137
 
 
23138
 
  ERR_TRACE(rc, ("os2ShmMap: %s iRgn = %d, szRgn = %d, bExt = %d : %d\n", 
23139
 
                 pFile->zFullPathCp, iRegion, szRegion, bExtend, rc))
23140
 
          
23141
 
  return rc;
23142
 
}
23143
 
 
23144
 
/*
23145
 
** Close a connection to shared-memory.  Delete the underlying
23146
 
** storage if deleteFlag is true.
23147
 
**
23148
 
** If there is no shared memory associated with the connection then this
23149
 
** routine is a harmless no-op.
23150
 
*/
23151
 
static int os2ShmUnmap(
23152
 
  sqlite3_file *id,               /* The underlying database file */
23153
 
  int deleteFlag                  /* Delete shared-memory if true */
23154
 
){
23155
 
  os2File *pFile = (os2File*)id;
23156
 
  os2ShmLink *pLink = pFile->pShmLink;
23157
 
  
23158
 
  if( pLink ) {
23159
 
    int nRef = -1;
23160
 
    os2ShmLink **ppLink;
23161
 
    os2ShmNode *pNode = pLink->pShmNode;
23162
 
 
23163
 
    sqlite3_mutex_enter(pNode->mutex);
23164
 
    
23165
 
    for( ppLink = &pNode->pFirst;
23166
 
         *ppLink && *ppLink != pLink;
23167
 
         ppLink = &(*ppLink)->pNext )   ;
23168
 
         
23169
 
    assert(*ppLink);
23170
 
 
23171
 
    if( *ppLink ) {
23172
 
      *ppLink = pLink->pNext;
23173
 
      nRef = --pNode->nRef;
23174
 
    } else {
23175
 
      ERR_TRACE(1, ("os2ShmUnmap: link not found ! %s\n", 
23176
 
                    pNode->shmBaseName))
23177
 
    }
23178
 
    
23179
 
    pFile->pShmLink = NULL;
23180
 
    sqlite3_free(pLink);
23181
 
 
23182
 
    sqlite3_mutex_leave(pNode->mutex);
23183
 
    
23184
 
    if( nRef == 0 )
23185
 
      os2PurgeShmNodes( deleteFlag );
23186
 
  }
23187
 
 
23188
 
  return SQLITE_OK;
23189
 
}
23190
 
 
23191
 
/*
23192
 
** Change the lock state for a shared-memory segment.
23193
 
**
23194
 
** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
23195
 
** different here than in posix.  In xShmLock(), one can go from unlocked
23196
 
** to shared and back or from unlocked to exclusive and back.  But one may
23197
 
** not go from shared to exclusive or from exclusive to shared.
23198
 
*/
23199
 
static int os2ShmLock(
23200
 
  sqlite3_file *id,          /* Database file holding the shared memory */
23201
 
  int ofst,                  /* First lock to acquire or release */
23202
 
  int n,                     /* Number of locks to acquire or release */
23203
 
  int flags                  /* What to do with the lock */
23204
 
){
23205
 
  u32 mask;                             /* Mask of locks to take or release */
23206
 
  int rc = SQLITE_OK;                   /* Result code */
23207
 
  os2File *pFile = (os2File*)id;
23208
 
  os2ShmLink *p = pFile->pShmLink;      /* The shared memory being locked */
23209
 
  os2ShmLink *pX;                       /* For looping over all siblings */
23210
 
  os2ShmNode *pShmNode = p->pShmNode;   /* Our node */
23211
 
  
23212
 
  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
23213
 
  assert( n>=1 );
23214
 
  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
23215
 
       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
23216
 
       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
23217
 
       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
23218
 
  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
23219
 
 
23220
 
  mask = (u32)((1U<<(ofst+n)) - (1U<<ofst));
23221
 
  assert( n>1 || mask==(1<<ofst) );
23222
 
 
23223
 
 
23224
 
  sqlite3_mutex_enter(pShmNode->mutex);
23225
 
 
23226
 
  if( flags & SQLITE_SHM_UNLOCK ){
23227
 
    u32 allMask = 0; /* Mask of locks held by siblings */
23228
 
 
23229
 
    /* See if any siblings hold this same lock */
23230
 
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
23231
 
      if( pX==p ) continue;
23232
 
      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
23233
 
      allMask |= pX->sharedMask;
23234
 
    }
23235
 
 
23236
 
    /* Unlock the system-level locks */
23237
 
    if( (mask & allMask)==0 ){
23238
 
      rc = os2ShmSystemLock(pShmNode, _SHM_UNLCK, ofst+OS2_SHM_BASE, n);
23239
 
    }else{
23240
 
      rc = SQLITE_OK;
23241
 
    }
23242
 
 
23243
 
    /* Undo the local locks */
23244
 
    if( rc==SQLITE_OK ){
23245
 
      p->exclMask &= ~mask;
23246
 
      p->sharedMask &= ~mask;
23247
 
    } 
23248
 
  }else if( flags & SQLITE_SHM_SHARED ){
23249
 
    u32 allShared = 0;  /* Union of locks held by connections other than "p" */
23250
 
 
23251
 
    /* Find out which shared locks are already held by sibling connections.
23252
 
    ** If any sibling already holds an exclusive lock, go ahead and return
23253
 
    ** SQLITE_BUSY.
23254
 
    */
23255
 
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
23256
 
      if( (pX->exclMask & mask)!=0 ){
23257
 
        rc = SQLITE_BUSY;
23258
 
        break;
23259
 
      }
23260
 
      allShared |= pX->sharedMask;
23261
 
    }
23262
 
 
23263
 
    /* Get shared locks at the system level, if necessary */
23264
 
    if( rc==SQLITE_OK ){
23265
 
      if( (allShared & mask)==0 ){
23266
 
        rc = os2ShmSystemLock(pShmNode, _SHM_RDLCK, ofst+OS2_SHM_BASE, n);
23267
 
      }else{
23268
 
        rc = SQLITE_OK;
23269
 
      }
23270
 
    }
23271
 
 
23272
 
    /* Get the local shared locks */
23273
 
    if( rc==SQLITE_OK ){
23274
 
      p->sharedMask |= mask;
23275
 
    }
23276
 
  }else{
23277
 
    /* Make sure no sibling connections hold locks that will block this
23278
 
    ** lock.  If any do, return SQLITE_BUSY right away.
23279
 
    */
23280
 
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
23281
 
      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
23282
 
        rc = SQLITE_BUSY;
23283
 
        break;
23284
 
      }
23285
 
    }
23286
 
  
23287
 
    /* Get the exclusive locks at the system level.  Then if successful
23288
 
    ** also mark the local connection as being locked.
23289
 
    */
23290
 
    if( rc==SQLITE_OK ){
23291
 
      rc = os2ShmSystemLock(pShmNode, _SHM_WRLCK, ofst+OS2_SHM_BASE, n);
23292
 
      if( rc==SQLITE_OK ){
23293
 
        assert( (p->sharedMask & mask)==0 );
23294
 
        p->exclMask |= mask;
23295
 
      }
23296
 
    }
23297
 
  }
23298
 
 
23299
 
  sqlite3_mutex_leave(pShmNode->mutex);
23300
 
  
23301
 
  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
23302
 
           p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
23303
 
           rc ? "failed" : "ok"));
23304
 
 
23305
 
  ERR_TRACE(rc, ("os2ShmLock: ofst = %d, n = %d, flags = 0x%x -> %d \n", 
23306
 
                 ofst, n, flags, rc))
23307
 
                  
23308
 
  return rc; 
23309
 
}
23310
 
 
23311
 
/*
23312
 
** Implement a memory barrier or memory fence on shared memory.
23313
 
**
23314
 
** All loads and stores begun before the barrier must complete before
23315
 
** any load or store begun after the barrier.
23316
 
*/
23317
 
static void os2ShmBarrier(
23318
 
  sqlite3_file *id                /* Database file holding the shared memory */
23319
 
){
23320
 
  UNUSED_PARAMETER(id);
23321
 
  os2ShmEnterMutex();
23322
 
  os2ShmLeaveMutex();
23323
 
}
23324
 
 
23325
 
#else
23326
 
# define os2ShmMap     0
23327
 
# define os2ShmLock    0
23328
 
# define os2ShmBarrier 0
23329
 
# define os2ShmUnmap   0
23330
 
#endif /* #ifndef SQLITE_OMIT_WAL */
23331
 
 
23332
 
 
23333
 
/*
23334
 
** This vector defines all the methods that can operate on an
23335
 
** sqlite3_file for os2.
23336
 
*/
23337
 
static const sqlite3_io_methods os2IoMethod = {
23338
 
  2,                              /* iVersion */
23339
 
  os2Close,                       /* xClose */
23340
 
  os2Read,                        /* xRead */
23341
 
  os2Write,                       /* xWrite */
23342
 
  os2Truncate,                    /* xTruncate */
23343
 
  os2Sync,                        /* xSync */
23344
 
  os2FileSize,                    /* xFileSize */
23345
 
  os2Lock,                        /* xLock */
23346
 
  os2Unlock,                      /* xUnlock */
23347
 
  os2CheckReservedLock,           /* xCheckReservedLock */
23348
 
  os2FileControl,                 /* xFileControl */
23349
 
  os2SectorSize,                  /* xSectorSize */
23350
 
  os2DeviceCharacteristics,       /* xDeviceCharacteristics */
23351
 
  os2ShmMap,                      /* xShmMap */
23352
 
  os2ShmLock,                     /* xShmLock */
23353
 
  os2ShmBarrier,                  /* xShmBarrier */
23354
 
  os2ShmUnmap                     /* xShmUnmap */
23355
 
};
23356
 
 
23357
 
 
23358
 
/***************************************************************************
23359
 
** Here ends the I/O methods that form the sqlite3_io_methods object.
23360
 
**
23361
 
** The next block of code implements the VFS methods.
23362
 
****************************************************************************/
23363
 
 
23364
 
/*
23365
 
** Create a temporary file name in zBuf.  zBuf must be big enough to
23366
 
** hold at pVfs->mxPathname characters.
23367
 
*/
23368
 
static int getTempname(int nBuf, char *zBuf ){
23369
 
  static const char zChars[] =
23370
 
    "abcdefghijklmnopqrstuvwxyz"
23371
 
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
23372
 
    "0123456789";
23373
 
  int i, j;
23374
 
  PSZ zTempPathCp;      
23375
 
  char zTempPath[CCHMAXPATH];
23376
 
  ULONG ulDriveNum, ulDriveMap;
23377
 
  
23378
 
  /* It's odd to simulate an io-error here, but really this is just
23379
 
  ** using the io-error infrastructure to test that SQLite handles this
23380
 
  ** function failing. 
23381
 
  */
23382
 
  SimulateIOError( return SQLITE_IOERR );
23383
 
 
23384
 
  if( sqlite3_temp_directory ) {
23385
 
    sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", sqlite3_temp_directory);
23386
 
  } else if( DosScanEnv( (PSZ)"TEMP",   &zTempPathCp ) == NO_ERROR ||
23387
 
             DosScanEnv( (PSZ)"TMP",    &zTempPathCp ) == NO_ERROR ||
23388
 
             DosScanEnv( (PSZ)"TMPDIR", &zTempPathCp ) == NO_ERROR ) {
23389
 
    char *zTempPathUTF = convertCpPathToUtf8( (char *)zTempPathCp );
23390
 
    sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", zTempPathUTF);
23391
 
    free( zTempPathUTF );
23392
 
  } else if( DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap ) == NO_ERROR ) {
23393
 
    zTempPath[0] = (char)('A' + ulDriveNum - 1);
23394
 
    zTempPath[1] = ':'; 
23395
 
    zTempPath[2] = '\0'; 
23396
 
  } else {
23397
 
    zTempPath[0] = '\0'; 
23398
 
  }
23399
 
  
23400
 
  /* Strip off a trailing slashes or backslashes, otherwise we would get *
23401
 
   * multiple (back)slashes which causes DosOpen() to fail.              *
23402
 
   * Trailing spaces are not allowed, either.                            */
23403
 
  j = sqlite3Strlen30(zTempPath);
23404
 
  while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' || 
23405
 
                    zTempPath[j-1] == ' ' ) ){
23406
 
    j--;
23407
 
  }
23408
 
  zTempPath[j] = '\0';
23409
 
  
23410
 
  /* We use 20 bytes to randomize the name */
23411
 
  sqlite3_snprintf(nBuf-22, zBuf,
23412
 
                   "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
23413
 
  j = sqlite3Strlen30(zBuf);
23414
 
  sqlite3_randomness( 20, &zBuf[j] );
23415
 
  for( i = 0; i < 20; i++, j++ ){
23416
 
    zBuf[j] = zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
23417
 
  }
23418
 
  zBuf[j] = 0;
23419
 
 
23420
 
  OSTRACE(( "TEMP FILENAME: %s\n", zBuf ));
23421
 
  return SQLITE_OK;
23422
 
}
23423
 
 
23424
 
 
23425
 
/*
23426
 
** Turn a relative pathname into a full pathname.  Write the full
23427
 
** pathname into zFull[].  zFull[] will be at least pVfs->mxPathname
23428
 
** bytes in size.
23429
 
*/
23430
 
static int os2FullPathname(
23431
 
  sqlite3_vfs *pVfs,          /* Pointer to vfs object */
23432
 
  const char *zRelative,      /* Possibly relative input path */
23433
 
  int nFull,                  /* Size of output buffer in bytes */
23434
 
  char *zFull                 /* Output buffer */
23435
 
){
23436
 
  char *zRelativeCp = convertUtf8PathToCp( zRelative );
23437
 
  char zFullCp[CCHMAXPATH] = "\0";
23438
 
  char *zFullUTF;
23439
 
  APIRET rc = DosQueryPathInfo( (PSZ)zRelativeCp, FIL_QUERYFULLNAME, 
23440
 
                                zFullCp, CCHMAXPATH );
23441
 
  free( zRelativeCp );
23442
 
  zFullUTF = convertCpPathToUtf8( zFullCp );
23443
 
  sqlite3_snprintf( nFull, zFull, zFullUTF );
23444
 
  free( zFullUTF );
23445
 
  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
23446
 
}
23447
 
 
23448
 
 
23449
 
/*
23450
 
** Open a file.
23451
 
*/
23452
 
static int os2Open(
23453
 
  sqlite3_vfs *pVfs,            /* Not used */
23454
 
  const char *zName,            /* Name of the file (UTF-8) */
23455
 
  sqlite3_file *id,             /* Write the SQLite file handle here */
23456
 
  int flags,                    /* Open mode flags */
23457
 
  int *pOutFlags                /* Status return flags */
23458
 
){
23459
 
  HFILE h;
23460
 
  ULONG ulOpenFlags = 0;
23461
 
  ULONG ulOpenMode = 0;
23462
 
  ULONG ulAction = 0;
23463
 
  ULONG rc;
23464
 
  os2File *pFile = (os2File*)id;
23465
 
  const char *zUtf8Name = zName;
23466
 
  char *zNameCp;
23467
 
  char  zTmpname[CCHMAXPATH];
23468
 
 
23469
 
  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
23470
 
  int isCreate     = (flags & SQLITE_OPEN_CREATE);
23471
 
  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
23472
 
#ifndef NDEBUG
23473
 
  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
23474
 
  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
23475
 
  int eType        = (flags & 0xFFFFFF00);
23476
 
  int isOpenJournal = (isCreate && (
23477
 
        eType==SQLITE_OPEN_MASTER_JOURNAL 
23478
 
     || eType==SQLITE_OPEN_MAIN_JOURNAL 
23479
 
     || eType==SQLITE_OPEN_WAL
23480
 
  ));
23481
 
#endif
23482
 
 
23483
 
  UNUSED_PARAMETER(pVfs);
23484
 
  assert( id!=0 );
23485
 
 
23486
 
  /* Check the following statements are true: 
23487
 
  **
23488
 
  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
23489
 
  **   (b) if CREATE is set, then READWRITE must also be set, and
23490
 
  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
23491
 
  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
23492
 
  */
23493
 
  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
23494
 
  assert(isCreate==0 || isReadWrite);
23495
 
  assert(isExclusive==0 || isCreate);
23496
 
  assert(isDelete==0 || isCreate);
23497
 
 
23498
 
  /* The main DB, main journal, WAL file and master journal are never 
23499
 
  ** automatically deleted. Nor are they ever temporary files.  */
23500
 
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
23501
 
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
23502
 
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
23503
 
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
23504
 
 
23505
 
  /* Assert that the upper layer has set one of the "file-type" flags. */
23506
 
  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
23507
 
       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
23508
 
       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
23509
 
       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
23510
 
  );
23511
 
 
23512
 
  memset( pFile, 0, sizeof(*pFile) );
23513
 
  pFile->h = (HFILE)-1;
23514
 
 
23515
 
  /* If the second argument to this function is NULL, generate a 
23516
 
  ** temporary file name to use 
23517
 
  */
23518
 
  if( !zUtf8Name ){
23519
 
    assert(isDelete && !isOpenJournal);
23520
 
    rc = getTempname(CCHMAXPATH, zTmpname);
23521
 
    if( rc!=SQLITE_OK ){
23522
 
      return rc;
23523
 
    }
23524
 
    zUtf8Name = zTmpname;
23525
 
  }
23526
 
 
23527
 
  if( isReadWrite ){
23528
 
    ulOpenMode |= OPEN_ACCESS_READWRITE;
23529
 
  }else{
23530
 
    ulOpenMode |= OPEN_ACCESS_READONLY;
23531
 
  }
23532
 
 
23533
 
  /* Open in random access mode for possibly better speed.  Allow full
23534
 
  ** sharing because file locks will provide exclusive access when needed.
23535
 
  ** The handle should not be inherited by child processes and we don't 
23536
 
  ** want popups from the critical error handler.
23537
 
  */
23538
 
  ulOpenMode |= OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE | 
23539
 
                OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR;
23540
 
 
23541
 
  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is 
23542
 
  ** created. SQLite doesn't use it to indicate "exclusive access" 
23543
 
  ** as it is usually understood.
23544
 
  */
23545
 
  if( isExclusive ){
23546
 
    /* Creates a new file, only if it does not already exist. */
23547
 
    /* If the file exists, it fails. */
23548
 
    ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_FAIL_IF_EXISTS;
23549
 
  }else if( isCreate ){
23550
 
    /* Open existing file, or create if it doesn't exist */
23551
 
    ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
23552
 
  }else{
23553
 
    /* Opens a file, only if it exists. */
23554
 
    ulOpenFlags |= OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
23555
 
  }
23556
 
 
23557
 
  zNameCp = convertUtf8PathToCp( zUtf8Name );
23558
 
  rc = DosOpen( (PSZ)zNameCp,
23559
 
                &h,
23560
 
                &ulAction,
23561
 
                0L,
23562
 
                FILE_NORMAL,
23563
 
                ulOpenFlags,
23564
 
                ulOpenMode,
23565
 
                (PEAOP2)NULL );
23566
 
  free( zNameCp );
23567
 
 
23568
 
  if( rc != NO_ERROR ){
23569
 
    OSTRACE(( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
23570
 
              rc, zUtf8Name, ulAction, ulOpenFlags, ulOpenMode ));
23571
 
 
23572
 
    if( isReadWrite ){
23573
 
      return os2Open( pVfs, zName, id,
23574
 
                      ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
23575
 
                      pOutFlags );
23576
 
    }else{
23577
 
      return SQLITE_CANTOPEN;
23578
 
    }
23579
 
  }
23580
 
 
23581
 
  if( pOutFlags ){
23582
 
    *pOutFlags = isReadWrite ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
23583
 
  }
23584
 
 
23585
 
  os2FullPathname( pVfs, zUtf8Name, sizeof( zTmpname ), zTmpname );
23586
 
  pFile->zFullPathCp = convertUtf8PathToCp( zTmpname );
23587
 
  pFile->pMethod = &os2IoMethod;
23588
 
  pFile->flags = flags;
23589
 
  pFile->h = h;
23590
 
 
23591
 
  OpenCounter(+1);
23592
 
  OSTRACE(( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags ));
23593
 
  return SQLITE_OK;
23594
 
}
23595
 
 
23596
 
/*
23597
 
** Delete the named file.
23598
 
*/
23599
 
static int os2Delete(
23600
 
  sqlite3_vfs *pVfs,                     /* Not used on os2 */
23601
 
  const char *zFilename,                 /* Name of file to delete */
23602
 
  int syncDir                            /* Not used on os2 */
23603
 
){
23604
 
  APIRET rc;
23605
 
  char *zFilenameCp;
23606
 
  SimulateIOError( return SQLITE_IOERR_DELETE );
23607
 
  zFilenameCp = convertUtf8PathToCp( zFilename );
23608
 
  rc = DosDelete( (PSZ)zFilenameCp );
23609
 
  free( zFilenameCp );
23610
 
  OSTRACE(( "DELETE \"%s\"\n", zFilename ));
23611
 
  return (rc == NO_ERROR ||
23612
 
          rc == ERROR_FILE_NOT_FOUND ||
23613
 
          rc == ERROR_PATH_NOT_FOUND ) ? SQLITE_OK : SQLITE_IOERR_DELETE;
23614
 
}
23615
 
 
23616
 
/*
23617
 
** Check the existance and status of a file.
23618
 
*/
23619
 
static int os2Access(
23620
 
  sqlite3_vfs *pVfs,        /* Not used on os2 */
23621
 
  const char *zFilename,    /* Name of file to check */
23622
 
  int flags,                /* Type of test to make on this file */
23623
 
  int *pOut                 /* Write results here */
23624
 
){
23625
 
  APIRET rc;
23626
 
  FILESTATUS3 fsts3ConfigInfo;
23627
 
  char *zFilenameCp;
23628
 
 
23629
 
  UNUSED_PARAMETER(pVfs);
23630
 
  SimulateIOError( return SQLITE_IOERR_ACCESS; );
23631
 
  
23632
 
  zFilenameCp = convertUtf8PathToCp( zFilename );
23633
 
  rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
23634
 
                         &fsts3ConfigInfo, sizeof(FILESTATUS3) );
23635
 
  free( zFilenameCp );
23636
 
  OSTRACE(( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
23637
 
            fsts3ConfigInfo.attrFile, flags, rc ));
23638
 
 
23639
 
  switch( flags ){
23640
 
    case SQLITE_ACCESS_EXISTS:
23641
 
      /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
23642
 
      ** as if it does not exist.
23643
 
      */
23644
 
      if( fsts3ConfigInfo.cbFile == 0 ) 
23645
 
        rc = ERROR_FILE_NOT_FOUND;
23646
 
      break;
23647
 
    case SQLITE_ACCESS_READ:
23648
 
      break;
23649
 
    case SQLITE_ACCESS_READWRITE:
23650
 
      if( fsts3ConfigInfo.attrFile & FILE_READONLY )
23651
 
        rc = ERROR_ACCESS_DENIED;
23652
 
      break;
23653
 
    default:
23654
 
      rc = ERROR_FILE_NOT_FOUND;
23655
 
      assert( !"Invalid flags argument" );
23656
 
  }
23657
 
 
23658
 
  *pOut = (rc == NO_ERROR);
23659
 
  OSTRACE(( "ACCESS %s flags %d: rc=%d\n", zFilename, flags, *pOut ));
23660
 
 
23661
 
  return SQLITE_OK;
23662
 
}
23663
 
 
23664
 
 
23665
 
#ifndef SQLITE_OMIT_LOAD_EXTENSION
23666
 
/*
23667
 
** Interfaces for opening a shared library, finding entry points
23668
 
** within the shared library, and closing the shared library.
23669
 
*/
23670
 
/*
23671
 
** Interfaces for opening a shared library, finding entry points
23672
 
** within the shared library, and closing the shared library.
23673
 
*/
23674
 
static void *os2DlOpen(sqlite3_vfs *pVfs, const char *zFilename){
23675
 
  HMODULE hmod;
23676
 
  APIRET rc;
23677
 
  char *zFilenameCp = convertUtf8PathToCp(zFilename);
23678
 
  rc = DosLoadModule(NULL, 0, (PSZ)zFilenameCp, &hmod);
23679
 
  free(zFilenameCp);
23680
 
  return rc != NO_ERROR ? 0 : (void*)hmod;
23681
 
}
23682
 
/*
23683
 
** A no-op since the error code is returned on the DosLoadModule call.
23684
 
** os2Dlopen returns zero if DosLoadModule is not successful.
23685
 
*/
23686
 
static void os2DlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
23687
 
/* no-op */
23688
 
}
23689
 
static void (*os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
23690
 
  PFN pfn;
23691
 
  APIRET rc;
23692
 
  rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)zSymbol, &pfn);
23693
 
  if( rc != NO_ERROR ){
23694
 
    /* if the symbol itself was not found, search again for the same
23695
 
     * symbol with an extra underscore, that might be needed depending
23696
 
     * on the calling convention */
23697
 
    char _zSymbol[256] = "_";
23698
 
    strncat(_zSymbol, zSymbol, 254);
23699
 
    rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)_zSymbol, &pfn);
23700
 
  }
23701
 
  return rc != NO_ERROR ? 0 : (void(*)(void))pfn;
23702
 
}
23703
 
static void os2DlClose(sqlite3_vfs *pVfs, void *pHandle){
23704
 
  DosFreeModule((HMODULE)pHandle);
23705
 
}
23706
 
#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
23707
 
  #define os2DlOpen 0
23708
 
  #define os2DlError 0
23709
 
  #define os2DlSym 0
23710
 
  #define os2DlClose 0
23711
 
#endif
23712
 
 
23713
 
 
23714
 
/*
23715
 
** Write up to nBuf bytes of randomness into zBuf.
23716
 
*/
23717
 
static int os2Randomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf ){
23718
 
  int n = 0;
23719
 
#if defined(SQLITE_TEST)
23720
 
  n = nBuf;
23721
 
  memset(zBuf, 0, nBuf);
23722
 
#else
23723
 
  int i;                           
23724
 
  PPIB ppib;
23725
 
  PTIB ptib;
23726
 
  DATETIME dt; 
23727
 
  static unsigned c = 0;
23728
 
  /* Ordered by variation probability */
23729
 
  static ULONG svIdx[6] = { QSV_MS_COUNT, QSV_TIME_LOW,
23730
 
                            QSV_MAXPRMEM, QSV_MAXSHMEM,
23731
 
                            QSV_TOTAVAILMEM, QSV_TOTRESMEM };
23732
 
 
23733
 
  /* 8 bytes; timezone and weekday don't increase the randomness much */
23734
 
  if( (int)sizeof(dt)-3 <= nBuf - n ){
23735
 
    c += 0x0100;
23736
 
    DosGetDateTime(&dt);
23737
 
    dt.year = (USHORT)((dt.year - 1900) | c);
23738
 
    memcpy(&zBuf[n], &dt, sizeof(dt)-3);
23739
 
    n += sizeof(dt)-3;
23740
 
  }
23741
 
 
23742
 
  /* 4 bytes; PIDs and TIDs are 16 bit internally, so combine them */
23743
 
  if( (int)sizeof(ULONG) <= nBuf - n ){
23744
 
    DosGetInfoBlocks(&ptib, &ppib);
23745
 
    *(PULONG)&zBuf[n] = MAKELONG(ppib->pib_ulpid,
23746
 
                                 ptib->tib_ptib2->tib2_ultid);
23747
 
    n += sizeof(ULONG);
23748
 
  }
23749
 
 
23750
 
  /* Up to 6 * 4 bytes; variables depend on the system state */
23751
 
  for( i = 0; i < 6 && (int)sizeof(ULONG) <= nBuf - n; i++ ){
23752
 
    DosQuerySysInfo(svIdx[i], svIdx[i], 
23753
 
                    (PULONG)&zBuf[n], sizeof(ULONG));
23754
 
    n += sizeof(ULONG);
23755
 
  } 
23756
 
#endif
23757
 
 
23758
 
  return n;
23759
 
}
23760
 
 
23761
 
/*
23762
 
** Sleep for a little while.  Return the amount of time slept.
23763
 
** The argument is the number of microseconds we want to sleep.
23764
 
** The return value is the number of microseconds of sleep actually
23765
 
** requested from the underlying operating system, a number which
23766
 
** might be greater than or equal to the argument, but not less
23767
 
** than the argument.
23768
 
*/
23769
 
static int os2Sleep( sqlite3_vfs *pVfs, int microsec ){
23770
 
  DosSleep( (microsec/1000) );
23771
 
  return microsec;
23772
 
}
23773
 
 
23774
 
/*
23775
 
** The following variable, if set to a non-zero value, becomes the result
23776
 
** returned from sqlite3OsCurrentTime().  This is used for testing.
23777
 
*/
23778
 
#ifdef SQLITE_TEST
23779
 
SQLITE_API int sqlite3_current_time = 0;
23780
 
#endif
23781
 
 
23782
 
/*
23783
 
** Find the current time (in Universal Coordinated Time).  Write into *piNow
23784
 
** the current time and date as a Julian Day number times 86_400_000.  In
23785
 
** other words, write into *piNow the number of milliseconds since the Julian
23786
 
** epoch of noon in Greenwich on November 24, 4714 B.C according to the
23787
 
** proleptic Gregorian calendar.
23788
 
**
23789
 
** On success, return 0.  Return 1 if the time and date cannot be found.
23790
 
*/
23791
 
static int os2CurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
23792
 
#ifdef SQLITE_TEST
23793
 
  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
23794
 
#endif
23795
 
  int year, month, datepart, timepart;
23796
 
 
23797
 
  DATETIME dt;
23798
 
  DosGetDateTime( &dt );
23799
 
 
23800
 
  year = dt.year;
23801
 
  month = dt.month;
23802
 
 
23803
 
  /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html
23804
 
  ** http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c
23805
 
  ** Calculate the Julian days
23806
 
  */
23807
 
  datepart = (int)dt.day - 32076 +
23808
 
    1461*(year + 4800 + (month - 14)/12)/4 +
23809
 
    367*(month - 2 - (month - 14)/12*12)/12 -
23810
 
    3*((year + 4900 + (month - 14)/12)/100)/4;
23811
 
 
23812
 
  /* Time in milliseconds, hours to noon added */
23813
 
  timepart = 12*3600*1000 + dt.hundredths*10 + dt.seconds*1000 +
23814
 
    ((int)dt.minutes + dt.timezone)*60*1000 + dt.hours*3600*1000;
23815
 
 
23816
 
  *piNow = (sqlite3_int64)datepart*86400*1000 + timepart;
23817
 
   
23818
 
#ifdef SQLITE_TEST
23819
 
  if( sqlite3_current_time ){
23820
 
    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
23821
 
  }
23822
 
#endif
23823
 
 
23824
 
  UNUSED_PARAMETER(pVfs);
23825
 
  return 0;
23826
 
}
23827
 
 
23828
 
/*
23829
 
** Find the current time (in Universal Coordinated Time).  Write the
23830
 
** current time and date as a Julian Day number into *prNow and
23831
 
** return 0.  Return 1 if the time and date cannot be found.
23832
 
*/
23833
 
static int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){
23834
 
  int rc;
23835
 
  sqlite3_int64 i;
23836
 
  rc = os2CurrentTimeInt64(pVfs, &i);
23837
 
  if( !rc ){
23838
 
    *prNow = i/86400000.0;
23839
 
  }
23840
 
  return rc;
23841
 
}
23842
 
 
23843
 
/*
23844
 
** The idea is that this function works like a combination of
23845
 
** GetLastError() and FormatMessage() on windows (or errno and
23846
 
** strerror_r() on unix). After an error is returned by an OS
23847
 
** function, SQLite calls this function with zBuf pointing to
23848
 
** a buffer of nBuf bytes. The OS layer should populate the
23849
 
** buffer with a nul-terminated UTF-8 encoded error message
23850
 
** describing the last IO error to have occurred within the calling
23851
 
** thread.
23852
 
**
23853
 
** If the error message is too large for the supplied buffer,
23854
 
** it should be truncated. The return value of xGetLastError
23855
 
** is zero if the error message fits in the buffer, or non-zero
23856
 
** otherwise (if the message was truncated). If non-zero is returned,
23857
 
** then it is not necessary to include the nul-terminator character
23858
 
** in the output buffer.
23859
 
**
23860
 
** Not supplying an error message will have no adverse effect
23861
 
** on SQLite. It is fine to have an implementation that never
23862
 
** returns an error message:
23863
 
**
23864
 
**   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
23865
 
**     assert(zBuf[0]=='\0');
23866
 
**     return 0;
23867
 
**   }
23868
 
**
23869
 
** However if an error message is supplied, it will be incorporated
23870
 
** by sqlite into the error message available to the user using
23871
 
** sqlite3_errmsg(), possibly making IO errors easier to debug.
23872
 
*/
23873
 
static int os2GetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
23874
 
  assert(zBuf[0]=='\0');
23875
 
  return 0;
23876
 
}
23877
 
 
23878
 
/*
23879
 
** Initialize and deinitialize the operating system interface.
23880
 
*/
23881
 
SQLITE_API int sqlite3_os_init(void){
23882
 
  static sqlite3_vfs os2Vfs = {
23883
 
    3,                 /* iVersion */
23884
 
    sizeof(os2File),   /* szOsFile */
23885
 
    CCHMAXPATH,        /* mxPathname */
23886
 
    0,                 /* pNext */
23887
 
    "os2",             /* zName */
23888
 
    0,                 /* pAppData */
23889
 
 
23890
 
    os2Open,           /* xOpen */
23891
 
    os2Delete,         /* xDelete */
23892
 
    os2Access,         /* xAccess */
23893
 
    os2FullPathname,   /* xFullPathname */
23894
 
    os2DlOpen,         /* xDlOpen */
23895
 
    os2DlError,        /* xDlError */
23896
 
    os2DlSym,          /* xDlSym */
23897
 
    os2DlClose,        /* xDlClose */
23898
 
    os2Randomness,     /* xRandomness */
23899
 
    os2Sleep,          /* xSleep */
23900
 
    os2CurrentTime,    /* xCurrentTime */
23901
 
    os2GetLastError,   /* xGetLastError */
23902
 
    os2CurrentTimeInt64, /* xCurrentTimeInt64 */
23903
 
    0,                 /* xSetSystemCall */
23904
 
    0,                 /* xGetSystemCall */
23905
 
    0                  /* xNextSystemCall */
23906
 
  };
23907
 
  sqlite3_vfs_register(&os2Vfs, 1);
23908
 
  initUconvObjects();
23909
 
/*  sqlite3OSTrace = 1; */
23910
 
  return SQLITE_OK;
23911
 
}
23912
 
SQLITE_API int sqlite3_os_end(void){
23913
 
  freeUconvObjects();
23914
 
  return SQLITE_OK;
23915
 
}
23916
 
 
23917
 
#endif /* SQLITE_OS_OS2 */
23918
 
 
23919
 
/************** End of os_os2.c **********************************************/
23920
 
/************** Begin file os_unix.c *****************************************/
23921
 
/*
23922
 
** 2004 May 22
23923
 
**
23924
 
** The author disclaims copyright to this source code.  In place of
23925
 
** a legal notice, here is a blessing:
23926
 
**
23927
 
**    May you do good and not evil.
23928
 
**    May you find forgiveness for yourself and forgive others.
23929
 
**    May you share freely, never taking more than you give.
23930
 
**
23931
 
******************************************************************************
23932
 
**
23933
 
** This file contains the VFS implementation for unix-like operating systems
23934
 
** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
23935
 
**
23936
 
** There are actually several different VFS implementations in this file.
23937
 
** The differences are in the way that file locking is done.  The default
23938
 
** implementation uses Posix Advisory Locks.  Alternative implementations
23939
 
** use flock(), dot-files, various proprietary locking schemas, or simply
23940
 
** skip locking all together.
23941
 
**
23942
 
** This source file is organized into divisions where the logic for various
23943
 
** subfunctions is contained within the appropriate division.  PLEASE
23944
 
** KEEP THE STRUCTURE OF THIS FILE INTACT.  New code should be placed
23945
 
** in the correct division and should be clearly labeled.
23946
 
**
23947
 
** The layout of divisions is as follows:
23948
 
**
23949
 
**   *  General-purpose declarations and utility functions.
23950
 
**   *  Unique file ID logic used by VxWorks.
23951
 
**   *  Various locking primitive implementations (all except proxy locking):
23952
 
**      + for Posix Advisory Locks
23953
 
**      + for no-op locks
23954
 
**      + for dot-file locks
23955
 
**      + for flock() locking
23956
 
**      + for named semaphore locks (VxWorks only)
23957
 
**      + for AFP filesystem locks (MacOSX only)
23958
 
**   *  sqlite3_file methods not associated with locking.
23959
 
**   *  Definitions of sqlite3_io_methods objects for all locking
23960
 
**      methods plus "finder" functions for each locking method.
23961
 
**   *  sqlite3_vfs method implementations.
23962
 
**   *  Locking primitives for the proxy uber-locking-method. (MacOSX only)
23963
 
**   *  Definitions of sqlite3_vfs objects for all locking methods
23964
 
**      plus implementations of sqlite3_os_init() and sqlite3_os_end().
23965
 
*/
23966
 
#if SQLITE_OS_UNIX              /* This file is used on unix only */
23967
 
 
23968
 
/*
23969
 
** There are various methods for file locking used for concurrency
23970
 
** control:
23971
 
**
23972
 
**   1. POSIX locking (the default),
23973
 
**   2. No locking,
23974
 
**   3. Dot-file locking,
23975
 
**   4. flock() locking,
23976
 
**   5. AFP locking (OSX only),
23977
 
**   6. Named POSIX semaphores (VXWorks only),
23978
 
**   7. proxy locking. (OSX only)
23979
 
**
23980
 
** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
23981
 
** is defined to 1.  The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
23982
 
** selection of the appropriate locking style based on the filesystem
23983
 
** where the database is located.  
23984
 
*/
23985
 
#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
23986
 
#  if defined(__APPLE__)
23987
 
#    define SQLITE_ENABLE_LOCKING_STYLE 1
23988
 
#  else
23989
 
#    define SQLITE_ENABLE_LOCKING_STYLE 0
23990
 
#  endif
23991
 
#endif
23992
 
 
23993
 
/*
23994
 
** Define the OS_VXWORKS pre-processor macro to 1 if building on 
23995
 
** vxworks, or 0 otherwise.
23996
 
*/
23997
 
#ifndef OS_VXWORKS
23998
 
#  if defined(__RTP__) || defined(_WRS_KERNEL)
23999
 
#    define OS_VXWORKS 1
24000
 
#  else
24001
 
#    define OS_VXWORKS 0
24002
 
#  endif
24003
 
#endif
24004
 
 
24005
 
/*
24006
 
** These #defines should enable >2GB file support on Posix if the
24007
 
** underlying operating system supports it.  If the OS lacks
24008
 
** large file support, these should be no-ops.
24009
 
**
24010
 
** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
24011
 
** on the compiler command line.  This is necessary if you are compiling
24012
 
** on a recent machine (ex: RedHat 7.2) but you want your code to work
24013
 
** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
24014
 
** without this option, LFS is enable.  But LFS does not exist in the kernel
24015
 
** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
24016
 
** portability you should omit LFS.
24017
 
**
24018
 
** The previous paragraph was written in 2005.  (This paragraph is written
24019
 
** on 2008-11-28.) These days, all Linux kernels support large files, so
24020
 
** you should probably leave LFS enabled.  But some embedded platforms might
24021
 
** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
24022
 
*/
24023
 
#ifndef SQLITE_DISABLE_LFS
24024
 
# define _LARGE_FILE       1
24025
 
# ifndef _FILE_OFFSET_BITS
24026
 
#   define _FILE_OFFSET_BITS 64
24027
 
# endif
24028
 
# define _LARGEFILE_SOURCE 1
24029
 
#endif
24030
 
 
24031
 
/*
24032
 
** standard include files.
24033
 
*/
24034
 
#include <sys/types.h>
24035
 
#include <sys/stat.h>
24036
 
#include <fcntl.h>
24037
 
#include <unistd.h>
24038
 
#include <sys/time.h>
24039
 
#include <errno.h>
24040
 
#ifndef SQLITE_OMIT_WAL
24041
 
#include <sys/mman.h>
24042
 
#endif
24043
 
 
24044
 
#if SQLITE_ENABLE_LOCKING_STYLE
24045
 
# include <sys/ioctl.h>
24046
 
# if OS_VXWORKS
24047
 
#  include <semaphore.h>
24048
 
#  include <limits.h>
24049
 
# else
24050
 
#  include <sys/file.h>
24051
 
#  include <sys/param.h>
24052
 
# endif
24053
 
#endif /* SQLITE_ENABLE_LOCKING_STYLE */
24054
 
 
24055
 
#if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
24056
 
# include <sys/mount.h>
24057
 
#endif
24058
 
 
24059
 
/*
24060
 
** Allowed values of unixFile.fsFlags
24061
 
*/
24062
 
#define SQLITE_FSFLAGS_IS_MSDOS     0x1
24063
 
 
24064
 
/*
24065
 
** If we are to be thread-safe, include the pthreads header and define
24066
 
** the SQLITE_UNIX_THREADS macro.
24067
 
*/
24068
 
#if SQLITE_THREADSAFE
24069
 
# define SQLITE_UNIX_THREADS 1
24070
 
#endif
24071
 
 
24072
 
/*
24073
 
** Default permissions when creating a new file
24074
 
*/
24075
 
#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
24076
 
# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
24077
 
#endif
24078
 
 
24079
 
/*
24080
 
 ** Default permissions when creating auto proxy dir
24081
 
 */
24082
 
#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
24083
 
# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
24084
 
#endif
24085
 
 
24086
 
/*
24087
 
** Maximum supported path-length.
24088
 
*/
24089
 
#define MAX_PATHNAME 512
24090
 
 
24091
 
/*
24092
 
** Only set the lastErrno if the error code is a real error and not 
24093
 
** a normal expected return code of SQLITE_BUSY or SQLITE_OK
24094
 
*/
24095
 
#define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))
24096
 
 
24097
 
/* Forward references */
24098
 
typedef struct unixShm unixShm;               /* Connection shared memory */
24099
 
typedef struct unixShmNode unixShmNode;       /* Shared memory instance */
24100
 
typedef struct unixInodeInfo unixInodeInfo;   /* An i-node */
24101
 
typedef struct UnixUnusedFd UnixUnusedFd;     /* An unused file descriptor */
24102
 
 
24103
 
/*
24104
 
** Sometimes, after a file handle is closed by SQLite, the file descriptor
24105
 
** cannot be closed immediately. In these cases, instances of the following
24106
 
** structure are used to store the file descriptor while waiting for an
24107
 
** opportunity to either close or reuse it.
24108
 
*/
24109
 
struct UnixUnusedFd {
24110
 
  int fd;                   /* File descriptor to close */
24111
 
  int flags;                /* Flags this file descriptor was opened with */
24112
 
  UnixUnusedFd *pNext;      /* Next unused file descriptor on same file */
24113
 
};
24114
 
 
24115
 
/*
24116
 
** The unixFile structure is subclass of sqlite3_file specific to the unix
24117
 
** VFS implementations.
24118
 
*/
24119
 
typedef struct unixFile unixFile;
24120
 
struct unixFile {
24121
 
  sqlite3_io_methods const *pMethod;  /* Always the first entry */
24122
 
  unixInodeInfo *pInode;              /* Info about locks on this inode */
24123
 
  int h;                              /* The file descriptor */
24124
 
  int dirfd;                          /* File descriptor for the directory */
24125
 
  unsigned char eFileLock;            /* The type of lock held on this fd */
24126
 
  unsigned char ctrlFlags;            /* Behavioral bits.  UNIXFILE_* flags */
24127
 
  int lastErrno;                      /* The unix errno from last I/O error */
24128
 
  void *lockingContext;               /* Locking style specific state */
24129
 
  UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
24130
 
  const char *zPath;                  /* Name of the file */
24131
 
  unixShm *pShm;                      /* Shared memory segment information */
24132
 
  int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
24133
 
#if SQLITE_ENABLE_LOCKING_STYLE
24134
 
  int openFlags;                      /* The flags specified at open() */
24135
 
#endif
24136
 
#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
24137
 
  unsigned fsFlags;                   /* cached details from statfs() */
24138
 
#endif
24139
 
#if OS_VXWORKS
24140
 
  int isDelete;                       /* Delete on close if true */
24141
 
  struct vxworksFileId *pId;          /* Unique file ID */
24142
 
#endif
24143
 
#ifndef NDEBUG
24144
 
  /* The next group of variables are used to track whether or not the
24145
 
  ** transaction counter in bytes 24-27 of database files are updated
24146
 
  ** whenever any part of the database changes.  An assertion fault will
24147
 
  ** occur if a file is updated without also updating the transaction
24148
 
  ** counter.  This test is made to avoid new problems similar to the
24149
 
  ** one described by ticket #3584. 
24150
 
  */
24151
 
  unsigned char transCntrChng;   /* True if the transaction counter changed */
24152
 
  unsigned char dbUpdate;        /* True if any part of database file changed */
24153
 
  unsigned char inNormalWrite;   /* True if in a normal write operation */
24154
 
#endif
24155
 
#ifdef SQLITE_TEST
24156
 
  /* In test mode, increase the size of this structure a bit so that 
24157
 
  ** it is larger than the struct CrashFile defined in test6.c.
24158
 
  */
24159
 
  char aPadding[32];
24160
 
#endif
24161
 
};
24162
 
 
24163
 
/*
24164
 
** Allowed values for the unixFile.ctrlFlags bitmask:
24165
 
*/
24166
 
#define UNIXFILE_EXCL   0x01     /* Connections from one process only */
24167
 
#define UNIXFILE_RDONLY 0x02     /* Connection is read only */
24168
 
 
24169
 
/*
24170
 
** Include code that is common to all os_*.c files
24171
 
*/
24172
 
/************** Include os_common.h in the middle of os_unix.c ***************/
24173
 
/************** Begin file os_common.h ***************************************/
24174
 
/*
24175
 
** 2004 May 22
24176
 
**
24177
 
** The author disclaims copyright to this source code.  In place of
24178
 
** a legal notice, here is a blessing:
24179
 
**
24180
 
**    May you do good and not evil.
24181
 
**    May you find forgiveness for yourself and forgive others.
24182
 
**    May you share freely, never taking more than you give.
24183
 
**
24184
 
******************************************************************************
24185
 
**
24186
 
** This file contains macros and a little bit of code that is common to
24187
 
** all of the platform-specific files (os_*.c) and is #included into those
24188
 
** files.
24189
 
**
24190
 
** This file should be #included by the os_*.c files only.  It is not a
24191
 
** general purpose header file.
24192
 
*/
24193
 
#ifndef _OS_COMMON_H_
24194
 
#define _OS_COMMON_H_
24195
 
 
24196
 
/*
24197
 
** At least two bugs have slipped in because we changed the MEMORY_DEBUG
24198
 
** macro to SQLITE_DEBUG and some older makefiles have not yet made the
24199
 
** switch.  The following code should catch this problem at compile-time.
24200
 
*/
24201
 
#ifdef MEMORY_DEBUG
24202
 
# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
24203
 
#endif
24204
 
 
24205
 
#ifdef SQLITE_DEBUG
24206
 
SQLITE_PRIVATE int sqlite3OSTrace = 0;
24207
 
#define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
24208
 
#else
24209
 
#define OSTRACE(X)
24210
 
#endif
24211
 
 
24212
 
/*
24213
 
** Macros for performance tracing.  Normally turned off.  Only works
24214
 
** on i486 hardware.
24215
 
*/
24216
 
#ifdef SQLITE_PERFORMANCE_TRACE
24217
 
 
24218
 
/* 
24219
 
** hwtime.h contains inline assembler code for implementing 
24220
 
** high-performance timing routines.
24221
 
*/
24222
 
/************** Include hwtime.h in the middle of os_common.h ****************/
24223
 
/************** Begin file hwtime.h ******************************************/
24224
 
/*
24225
 
** 2008 May 27
24226
 
**
24227
 
** The author disclaims copyright to this source code.  In place of
24228
 
** a legal notice, here is a blessing:
24229
 
**
24230
 
**    May you do good and not evil.
24231
 
**    May you find forgiveness for yourself and forgive others.
24232
 
**    May you share freely, never taking more than you give.
24233
 
**
24234
 
******************************************************************************
24235
 
**
24236
 
** This file contains inline asm code for retrieving "high-performance"
24237
 
** counters for x86 class CPUs.
24238
 
*/
24239
 
#ifndef _HWTIME_H_
24240
 
#define _HWTIME_H_
24241
 
 
24242
 
/*
24243
 
** The following routine only works on pentium-class (or newer) processors.
24244
 
** It uses the RDTSC opcode to read the cycle count value out of the
24245
 
** processor and returns that value.  This can be used for high-res
24246
 
** profiling.
24247
 
*/
24248
 
#if (defined(__GNUC__) || defined(_MSC_VER)) && \
24249
 
      (defined(i386) || defined(__i386__) || defined(_M_IX86))
24250
 
 
24251
 
  #if defined(__GNUC__)
24252
 
 
24253
 
  __inline__ sqlite_uint64 sqlite3Hwtime(void){
24254
 
     unsigned int lo, hi;
24255
 
     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
24256
 
     return (sqlite_uint64)hi << 32 | lo;
24257
 
  }
24258
 
 
24259
 
  #elif defined(_MSC_VER)
24260
 
 
24261
 
  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
24262
 
     __asm {
24263
 
        rdtsc
24264
 
        ret       ; return value at EDX:EAX
24265
 
     }
24266
 
  }
24267
 
 
24268
 
  #endif
24269
 
 
24270
 
#elif (defined(__GNUC__) && defined(__x86_64__))
24271
 
 
24272
 
  __inline__ sqlite_uint64 sqlite3Hwtime(void){
24273
 
      unsigned long val;
24274
 
      __asm__ __volatile__ ("rdtsc" : "=A" (val));
24275
 
      return val;
24276
 
  }
24277
 
 
24278
 
#elif (defined(__GNUC__) && defined(__ppc__))
24279
 
 
24280
 
  __inline__ sqlite_uint64 sqlite3Hwtime(void){
24281
 
      unsigned long long retval;
24282
 
      unsigned long junk;
24283
 
      __asm__ __volatile__ ("\n\
24284
 
          1:      mftbu   %1\n\
24285
 
                  mftb    %L0\n\
24286
 
                  mftbu   %0\n\
24287
 
                  cmpw    %0,%1\n\
24288
 
                  bne     1b"
24289
 
                  : "=r" (retval), "=r" (junk));
24290
 
      return retval;
24291
 
  }
24292
 
 
24293
 
#else
24294
 
 
24295
 
  #error Need implementation of sqlite3Hwtime() for your platform.
24296
 
 
24297
 
  /*
24298
 
  ** To compile without implementing sqlite3Hwtime() for your platform,
24299
 
  ** you can remove the above #error and use the following
24300
 
  ** stub function.  You will lose timing support for many
24301
 
  ** of the debugging and testing utilities, but it should at
24302
 
  ** least compile and run.
24303
 
  */
24304
 
SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
24305
 
 
24306
 
#endif
24307
 
 
24308
 
#endif /* !defined(_HWTIME_H_) */
24309
 
 
24310
 
/************** End of hwtime.h **********************************************/
24311
 
/************** Continuing where we left off in os_common.h ******************/
24312
 
 
24313
 
static sqlite_uint64 g_start;
24314
 
static sqlite_uint64 g_elapsed;
24315
 
#define TIMER_START       g_start=sqlite3Hwtime()
24316
 
#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
24317
 
#define TIMER_ELAPSED     g_elapsed
24318
 
#else
24319
 
#define TIMER_START
24320
 
#define TIMER_END
24321
 
#define TIMER_ELAPSED     ((sqlite_uint64)0)
24322
 
#endif
24323
 
 
24324
 
/*
24325
 
** If we compile with the SQLITE_TEST macro set, then the following block
24326
 
** of code will give us the ability to simulate a disk I/O error.  This
24327
 
** is used for testing the I/O recovery logic.
24328
 
*/
24329
 
#ifdef SQLITE_TEST
24330
 
SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
24331
 
SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
24332
 
SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
24333
 
SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
24334
 
SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
24335
 
SQLITE_API int sqlite3_diskfull_pending = 0;
24336
 
SQLITE_API int sqlite3_diskfull = 0;
24337
 
#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
24338
 
#define SimulateIOError(CODE)  \
24339
 
  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
24340
 
       || sqlite3_io_error_pending-- == 1 )  \
24341
 
              { local_ioerr(); CODE; }
24342
 
static void local_ioerr(){
24343
 
  IOTRACE(("IOERR\n"));
24344
 
  sqlite3_io_error_hit++;
24345
 
  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
24346
 
}
24347
 
#define SimulateDiskfullError(CODE) \
24348
 
   if( sqlite3_diskfull_pending ){ \
24349
 
     if( sqlite3_diskfull_pending == 1 ){ \
24350
 
       local_ioerr(); \
24351
 
       sqlite3_diskfull = 1; \
24352
 
       sqlite3_io_error_hit = 1; \
24353
 
       CODE; \
24354
 
     }else{ \
24355
 
       sqlite3_diskfull_pending--; \
24356
 
     } \
24357
 
   }
24358
 
#else
24359
 
#define SimulateIOErrorBenign(X)
24360
 
#define SimulateIOError(A)
24361
 
#define SimulateDiskfullError(A)
24362
 
#endif
24363
 
 
24364
 
/*
24365
 
** When testing, keep a count of the number of open files.
24366
 
*/
24367
 
#ifdef SQLITE_TEST
24368
 
SQLITE_API int sqlite3_open_file_count = 0;
24369
 
#define OpenCounter(X)  sqlite3_open_file_count+=(X)
24370
 
#else
24371
 
#define OpenCounter(X)
24372
 
#endif
24373
 
 
24374
 
#endif /* !defined(_OS_COMMON_H_) */
24375
 
 
24376
 
/************** End of os_common.h *******************************************/
24377
 
/************** Continuing where we left off in os_unix.c ********************/
24378
 
 
24379
 
/*
24380
 
** Define various macros that are missing from some systems.
24381
 
*/
24382
 
#ifndef O_LARGEFILE
24383
 
# define O_LARGEFILE 0
24384
 
#endif
24385
 
#ifdef SQLITE_DISABLE_LFS
24386
 
# undef O_LARGEFILE
24387
 
# define O_LARGEFILE 0
24388
 
#endif
24389
 
#ifndef O_NOFOLLOW
24390
 
# define O_NOFOLLOW 0
24391
 
#endif
24392
 
#ifndef O_BINARY
24393
 
# define O_BINARY 0
24394
 
#endif
24395
 
 
24396
 
/*
24397
 
** The threadid macro resolves to the thread-id or to 0.  Used for
24398
 
** testing and debugging only.
24399
 
*/
24400
 
#if SQLITE_THREADSAFE
24401
 
#define threadid pthread_self()
24402
 
#else
24403
 
#define threadid 0
24404
 
#endif
24405
 
 
24406
 
/*
24407
 
** Many system calls are accessed through pointer-to-functions so that
24408
 
** they may be overridden at runtime to facilitate fault injection during
24409
 
** testing and sandboxing.  The following array holds the names and pointers
24410
 
** to all overrideable system calls.
24411
 
*/
24412
 
static struct unix_syscall {
24413
 
  const char *zName;            /* Name of the sytem call */
24414
 
  sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
24415
 
  sqlite3_syscall_ptr pDefault; /* Default value */
24416
 
} aSyscall[] = {
24417
 
  { "open",         (sqlite3_syscall_ptr)open,       0  },
24418
 
#define osOpen      ((int(*)(const char*,int,...))aSyscall[0].pCurrent)
24419
 
 
24420
 
  { "close",        (sqlite3_syscall_ptr)close,      0  },
24421
 
#define osClose     ((int(*)(int))aSyscall[1].pCurrent)
24422
 
 
24423
 
  { "access",       (sqlite3_syscall_ptr)access,     0  },
24424
 
#define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)
24425
 
 
24426
 
  { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
24427
 
#define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
24428
 
 
24429
 
  { "stat",         (sqlite3_syscall_ptr)stat,       0  },
24430
 
#define osStat      ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
24431
 
 
24432
 
/*
24433
 
** The DJGPP compiler environment looks mostly like Unix, but it
24434
 
** lacks the fcntl() system call.  So redefine fcntl() to be something
24435
 
** that always succeeds.  This means that locking does not occur under
24436
 
** DJGPP.  But it is DOS - what did you expect?
24437
 
*/
24438
 
#ifdef __DJGPP__
24439
 
  { "fstat",        0,                 0  },
24440
 
#define osFstat(a,b,c)    0
24441
 
#else     
24442
 
  { "fstat",        (sqlite3_syscall_ptr)fstat,      0  },
24443
 
#define osFstat     ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
24444
 
#endif
24445
 
 
24446
 
  { "ftruncate",    (sqlite3_syscall_ptr)ftruncate,  0  },
24447
 
#define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
24448
 
 
24449
 
  { "fcntl",        (sqlite3_syscall_ptr)fcntl,      0  },
24450
 
#define osFcntl     ((int(*)(int,int,...))aSyscall[7].pCurrent)
24451
 
 
24452
 
  { "read",         (sqlite3_syscall_ptr)read,       0  },
24453
 
#define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
24454
 
 
24455
 
#if defined(USE_PREAD) || defined(SQLITE_ENABLE_LOCKING_STYLE)
24456
 
  { "pread",        (sqlite3_syscall_ptr)pread,      0  },
24457
 
#else
24458
 
  { "pread",        (sqlite3_syscall_ptr)0,          0  },
24459
 
#endif
24460
 
#define osPread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
24461
 
 
24462
 
#if defined(USE_PREAD64)
24463
 
  { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
24464
 
#else
24465
 
  { "pread64",      (sqlite3_syscall_ptr)0,          0  },
24466
 
#endif
24467
 
#define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
24468
 
 
24469
 
  { "write",        (sqlite3_syscall_ptr)write,      0  },
24470
 
#define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
24471
 
 
24472
 
#if defined(USE_PREAD) || defined(SQLITE_ENABLE_LOCKING_STYLE)
24473
 
  { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
24474
 
#else
24475
 
  { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
24476
 
#endif
24477
 
#define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
24478
 
                    aSyscall[12].pCurrent)
24479
 
 
24480
 
#if defined(USE_PREAD64)
24481
 
  { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
24482
 
#else
24483
 
  { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
24484
 
#endif
24485
 
#define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
24486
 
                    aSyscall[13].pCurrent)
24487
 
 
24488
 
#if SQLITE_ENABLE_LOCKING_STYLE
24489
 
  { "fchmod",       (sqlite3_syscall_ptr)fchmod,     0  },
24490
 
#else
24491
 
  { "fchmod",       (sqlite3_syscall_ptr)0,          0  },
24492
 
#endif
24493
 
#define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
24494
 
 
24495
 
#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
24496
 
  { "fallocate",    (sqlite3_syscall_ptr)posix_fallocate,  0 },
24497
 
#else
24498
 
  { "fallocate",    (sqlite3_syscall_ptr)0,                0 },
24499
 
#endif
24500
 
#define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
24501
 
 
24502
 
}; /* End of the overrideable system calls */
24503
 
 
24504
 
/*
24505
 
** This is the xSetSystemCall() method of sqlite3_vfs for all of the
24506
 
** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
24507
 
** system call pointer, or SQLITE_NOTFOUND if there is no configurable
24508
 
** system call named zName.
24509
 
*/
24510
 
static int unixSetSystemCall(
24511
 
  sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
24512
 
  const char *zName,            /* Name of system call to override */
24513
 
  sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
24514
 
){
24515
 
  unsigned int i;
24516
 
  int rc = SQLITE_NOTFOUND;
24517
 
 
24518
 
  UNUSED_PARAMETER(pNotUsed);
24519
 
  if( zName==0 ){
24520
 
    /* If no zName is given, restore all system calls to their default
24521
 
    ** settings and return NULL
24522
 
    */
24523
 
    rc = SQLITE_OK;
24524
 
    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
24525
 
      if( aSyscall[i].pDefault ){
24526
 
        aSyscall[i].pCurrent = aSyscall[i].pDefault;
24527
 
      }
24528
 
    }
24529
 
  }else{
24530
 
    /* If zName is specified, operate on only the one system call
24531
 
    ** specified.
24532
 
    */
24533
 
    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
24534
 
      if( strcmp(zName, aSyscall[i].zName)==0 ){
24535
 
        if( aSyscall[i].pDefault==0 ){
24536
 
          aSyscall[i].pDefault = aSyscall[i].pCurrent;
24537
 
        }
24538
 
        rc = SQLITE_OK;
24539
 
        if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
24540
 
        aSyscall[i].pCurrent = pNewFunc;
24541
 
        break;
24542
 
      }
24543
 
    }
24544
 
  }
24545
 
  return rc;
24546
 
}
24547
 
 
24548
 
/*
24549
 
** Return the value of a system call.  Return NULL if zName is not a
24550
 
** recognized system call name.  NULL is also returned if the system call
24551
 
** is currently undefined.
24552
 
*/
24553
 
static sqlite3_syscall_ptr unixGetSystemCall(
24554
 
  sqlite3_vfs *pNotUsed,
24555
 
  const char *zName
24556
 
){
24557
 
  unsigned int i;
24558
 
 
24559
 
  UNUSED_PARAMETER(pNotUsed);
24560
 
  for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
24561
 
    if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
24562
 
  }
24563
 
  return 0;
24564
 
}
24565
 
 
24566
 
/*
24567
 
** Return the name of the first system call after zName.  If zName==NULL
24568
 
** then return the name of the first system call.  Return NULL if zName
24569
 
** is the last system call or if zName is not the name of a valid
24570
 
** system call.
24571
 
*/
24572
 
static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
24573
 
  int i = -1;
24574
 
 
24575
 
  UNUSED_PARAMETER(p);
24576
 
  if( zName ){
24577
 
    for(i=0; i<ArraySize(aSyscall)-1; i++){
24578
 
      if( strcmp(zName, aSyscall[i].zName)==0 ) break;
24579
 
    }
24580
 
  }
24581
 
  for(i++; i<ArraySize(aSyscall); i++){
24582
 
    if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
24583
 
  }
24584
 
  return 0;
24585
 
}
24586
 
 
24587
 
/*
24588
 
** Retry open() calls that fail due to EINTR
24589
 
*/
24590
 
static int robust_open(const char *z, int f, int m){
24591
 
  int rc;
24592
 
  do{ rc = osOpen(z,f,m); }while( rc<0 && errno==EINTR );
24593
 
  return rc;
24594
 
}
24595
 
 
24596
 
/*
24597
 
** Helper functions to obtain and relinquish the global mutex. The
24598
 
** global mutex is used to protect the unixInodeInfo and
24599
 
** vxworksFileId objects used by this file, all of which may be 
24600
 
** shared by multiple threads.
24601
 
**
24602
 
** Function unixMutexHeld() is used to assert() that the global mutex 
24603
 
** is held when required. This function is only used as part of assert() 
24604
 
** statements. e.g.
24605
 
**
24606
 
**   unixEnterMutex()
24607
 
**     assert( unixMutexHeld() );
24608
 
**   unixEnterLeave()
24609
 
*/
24610
 
static void unixEnterMutex(void){
24611
 
  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
24612
 
}
24613
 
static void unixLeaveMutex(void){
24614
 
  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
24615
 
}
24616
 
#ifdef SQLITE_DEBUG
24617
 
static int unixMutexHeld(void) {
24618
 
  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
24619
 
}
24620
 
#endif
24621
 
 
24622
 
 
24623
 
#ifdef SQLITE_DEBUG
24624
 
/*
24625
 
** Helper function for printing out trace information from debugging
24626
 
** binaries. This returns the string represetation of the supplied
24627
 
** integer lock-type.
24628
 
*/
24629
 
static const char *azFileLock(int eFileLock){
24630
 
  switch( eFileLock ){
24631
 
    case NO_LOCK: return "NONE";
24632
 
    case SHARED_LOCK: return "SHARED";
24633
 
    case RESERVED_LOCK: return "RESERVED";
24634
 
    case PENDING_LOCK: return "PENDING";
24635
 
    case EXCLUSIVE_LOCK: return "EXCLUSIVE";
24636
 
  }
24637
 
  return "ERROR";
24638
 
}
24639
 
#endif
24640
 
 
24641
 
#ifdef SQLITE_LOCK_TRACE
24642
 
/*
24643
 
** Print out information about all locking operations.
24644
 
**
24645
 
** This routine is used for troubleshooting locks on multithreaded
24646
 
** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
24647
 
** command-line option on the compiler.  This code is normally
24648
 
** turned off.
24649
 
*/
24650
 
static int lockTrace(int fd, int op, struct flock *p){
24651
 
  char *zOpName, *zType;
24652
 
  int s;
24653
 
  int savedErrno;
24654
 
  if( op==F_GETLK ){
24655
 
    zOpName = "GETLK";
24656
 
  }else if( op==F_SETLK ){
24657
 
    zOpName = "SETLK";
24658
 
  }else{
24659
 
    s = osFcntl(fd, op, p);
24660
 
    sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
24661
 
    return s;
24662
 
  }
24663
 
  if( p->l_type==F_RDLCK ){
24664
 
    zType = "RDLCK";
24665
 
  }else if( p->l_type==F_WRLCK ){
24666
 
    zType = "WRLCK";
24667
 
  }else if( p->l_type==F_UNLCK ){
24668
 
    zType = "UNLCK";
24669
 
  }else{
24670
 
    assert( 0 );
24671
 
  }
24672
 
  assert( p->l_whence==SEEK_SET );
24673
 
  s = osFcntl(fd, op, p);
24674
 
  savedErrno = errno;
24675
 
  sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
24676
 
     threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
24677
 
     (int)p->l_pid, s);
24678
 
  if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
24679
 
    struct flock l2;
24680
 
    l2 = *p;
24681
 
    osFcntl(fd, F_GETLK, &l2);
24682
 
    if( l2.l_type==F_RDLCK ){
24683
 
      zType = "RDLCK";
24684
 
    }else if( l2.l_type==F_WRLCK ){
24685
 
      zType = "WRLCK";
24686
 
    }else if( l2.l_type==F_UNLCK ){
24687
 
      zType = "UNLCK";
24688
 
    }else{
24689
 
      assert( 0 );
24690
 
    }
24691
 
    sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
24692
 
       zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
24693
 
  }
24694
 
  errno = savedErrno;
24695
 
  return s;
24696
 
}
24697
 
#undef osFcntl
24698
 
#define osFcntl lockTrace
24699
 
#endif /* SQLITE_LOCK_TRACE */
24700
 
 
24701
 
/*
24702
 
** Retry ftruncate() calls that fail due to EINTR
24703
 
*/
24704
 
static int robust_ftruncate(int h, sqlite3_int64 sz){
24705
 
  int rc;
24706
 
  do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
24707
 
  return rc;
24708
 
}
24709
 
 
24710
 
/*
24711
 
** This routine translates a standard POSIX errno code into something
24712
 
** useful to the clients of the sqlite3 functions.  Specifically, it is
24713
 
** intended to translate a variety of "try again" errors into SQLITE_BUSY
24714
 
** and a variety of "please close the file descriptor NOW" errors into 
24715
 
** SQLITE_IOERR
24716
 
** 
24717
 
** Errors during initialization of locks, or file system support for locks,
24718
 
** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
24719
 
*/
24720
 
static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
24721
 
  switch (posixError) {
24722
 
#if 0
24723
 
  /* At one point this code was not commented out. In theory, this branch
24724
 
  ** should never be hit, as this function should only be called after
24725
 
  ** a locking-related function (i.e. fcntl()) has returned non-zero with
24726
 
  ** the value of errno as the first argument. Since a system call has failed,
24727
 
  ** errno should be non-zero.
24728
 
  **
24729
 
  ** Despite this, if errno really is zero, we still don't want to return
24730
 
  ** SQLITE_OK. The system call failed, and *some* SQLite error should be
24731
 
  ** propagated back to the caller. Commenting this branch out means errno==0
24732
 
  ** will be handled by the "default:" case below.
24733
 
  */
24734
 
  case 0: 
24735
 
    return SQLITE_OK;
24736
 
#endif
24737
 
 
24738
 
  case EAGAIN:
24739
 
  case ETIMEDOUT:
24740
 
  case EBUSY:
24741
 
  case EINTR:
24742
 
  case ENOLCK:  
24743
 
    /* random NFS retry error, unless during file system support 
24744
 
     * introspection, in which it actually means what it says */
24745
 
    return SQLITE_BUSY;
24746
 
    
24747
 
  case EACCES: 
24748
 
    /* EACCES is like EAGAIN during locking operations, but not any other time*/
24749
 
    if( (sqliteIOErr == SQLITE_IOERR_LOCK) || 
24750
 
        (sqliteIOErr == SQLITE_IOERR_UNLOCK) || 
24751
 
        (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
24752
 
        (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
24753
 
      return SQLITE_BUSY;
24754
 
    }
24755
 
    /* else fall through */
24756
 
  case EPERM: 
24757
 
    return SQLITE_PERM;
24758
 
    
24759
 
  /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
24760
 
  ** this module never makes such a call. And the code in SQLite itself 
24761
 
  ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
24762
 
  ** this case is also commented out. If the system does set errno to EDEADLK,
24763
 
  ** the default SQLITE_IOERR_XXX code will be returned. */
24764
 
#if 0
24765
 
  case EDEADLK:
24766
 
    return SQLITE_IOERR_BLOCKED;
24767
 
#endif
24768
 
    
24769
 
#if EOPNOTSUPP!=ENOTSUP
24770
 
  case EOPNOTSUPP: 
24771
 
    /* something went terribly awry, unless during file system support 
24772
 
     * introspection, in which it actually means what it says */
24773
 
#endif
24774
 
#ifdef ENOTSUP
24775
 
  case ENOTSUP: 
24776
 
    /* invalid fd, unless during file system support introspection, in which 
24777
 
     * it actually means what it says */
24778
 
#endif
24779
 
  case EIO:
24780
 
  case EBADF:
24781
 
  case EINVAL:
24782
 
  case ENOTCONN:
24783
 
  case ENODEV:
24784
 
  case ENXIO:
24785
 
  case ENOENT:
24786
 
  case ESTALE:
24787
 
  case ENOSYS:
24788
 
    /* these should force the client to close the file and reconnect */
24789
 
    
24790
 
  default: 
24791
 
    return sqliteIOErr;
24792
 
  }
24793
 
}
24794
 
 
24795
 
 
24796
 
 
24797
 
/******************************************************************************
24798
 
****************** Begin Unique File ID Utility Used By VxWorks ***************
24799
 
**
24800
 
** On most versions of unix, we can get a unique ID for a file by concatenating
24801
 
** the device number and the inode number.  But this does not work on VxWorks.
24802
 
** On VxWorks, a unique file id must be based on the canonical filename.
24803
 
**
24804
 
** A pointer to an instance of the following structure can be used as a
24805
 
** unique file ID in VxWorks.  Each instance of this structure contains
24806
 
** a copy of the canonical filename.  There is also a reference count.  
24807
 
** The structure is reclaimed when the number of pointers to it drops to
24808
 
** zero.
24809
 
**
24810
 
** There are never very many files open at one time and lookups are not
24811
 
** a performance-critical path, so it is sufficient to put these
24812
 
** structures on a linked list.
24813
 
*/
24814
 
struct vxworksFileId {
24815
 
  struct vxworksFileId *pNext;  /* Next in a list of them all */
24816
 
  int nRef;                     /* Number of references to this one */
24817
 
  int nName;                    /* Length of the zCanonicalName[] string */
24818
 
  char *zCanonicalName;         /* Canonical filename */
24819
 
};
24820
 
 
24821
 
#if OS_VXWORKS
24822
 
/* 
24823
 
** All unique filenames are held on a linked list headed by this
24824
 
** variable:
24825
 
*/
24826
 
static struct vxworksFileId *vxworksFileList = 0;
24827
 
 
24828
 
/*
24829
 
** Simplify a filename into its canonical form
24830
 
** by making the following changes:
24831
 
**
24832
 
**  * removing any trailing and duplicate /
24833
 
**  * convert /./ into just /
24834
 
**  * convert /A/../ where A is any simple name into just /
24835
 
**
24836
 
** Changes are made in-place.  Return the new name length.
24837
 
**
24838
 
** The original filename is in z[0..n-1].  Return the number of
24839
 
** characters in the simplified name.
24840
 
*/
24841
 
static int vxworksSimplifyName(char *z, int n){
24842
 
  int i, j;
24843
 
  while( n>1 && z[n-1]=='/' ){ n--; }
24844
 
  for(i=j=0; i<n; i++){
24845
 
    if( z[i]=='/' ){
24846
 
      if( z[i+1]=='/' ) continue;
24847
 
      if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
24848
 
        i += 1;
24849
 
        continue;
24850
 
      }
24851
 
      if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
24852
 
        while( j>0 && z[j-1]!='/' ){ j--; }
24853
 
        if( j>0 ){ j--; }
24854
 
        i += 2;
24855
 
        continue;
24856
 
      }
24857
 
    }
24858
 
    z[j++] = z[i];
24859
 
  }
24860
 
  z[j] = 0;
24861
 
  return j;
24862
 
}
24863
 
 
24864
 
/*
24865
 
** Find a unique file ID for the given absolute pathname.  Return
24866
 
** a pointer to the vxworksFileId object.  This pointer is the unique
24867
 
** file ID.
24868
 
**
24869
 
** The nRef field of the vxworksFileId object is incremented before
24870
 
** the object is returned.  A new vxworksFileId object is created
24871
 
** and added to the global list if necessary.
24872
 
**
24873
 
** If a memory allocation error occurs, return NULL.
24874
 
*/
24875
 
static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
24876
 
  struct vxworksFileId *pNew;         /* search key and new file ID */
24877
 
  struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
24878
 
  int n;                              /* Length of zAbsoluteName string */
24879
 
 
24880
 
  assert( zAbsoluteName[0]=='/' );
24881
 
  n = (int)strlen(zAbsoluteName);
24882
 
  pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
24883
 
  if( pNew==0 ) return 0;
24884
 
  pNew->zCanonicalName = (char*)&pNew[1];
24885
 
  memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
24886
 
  n = vxworksSimplifyName(pNew->zCanonicalName, n);
24887
 
 
24888
 
  /* Search for an existing entry that matching the canonical name.
24889
 
  ** If found, increment the reference count and return a pointer to
24890
 
  ** the existing file ID.
24891
 
  */
24892
 
  unixEnterMutex();
24893
 
  for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
24894
 
    if( pCandidate->nName==n 
24895
 
     && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
24896
 
    ){
24897
 
       sqlite3_free(pNew);
24898
 
       pCandidate->nRef++;
24899
 
       unixLeaveMutex();
24900
 
       return pCandidate;
24901
 
    }
24902
 
  }
24903
 
 
24904
 
  /* No match was found.  We will make a new file ID */
24905
 
  pNew->nRef = 1;
24906
 
  pNew->nName = n;
24907
 
  pNew->pNext = vxworksFileList;
24908
 
  vxworksFileList = pNew;
24909
 
  unixLeaveMutex();
24910
 
  return pNew;
24911
 
}
24912
 
 
24913
 
/*
24914
 
** Decrement the reference count on a vxworksFileId object.  Free
24915
 
** the object when the reference count reaches zero.
24916
 
*/
24917
 
static void vxworksReleaseFileId(struct vxworksFileId *pId){
24918
 
  unixEnterMutex();
24919
 
  assert( pId->nRef>0 );
24920
 
  pId->nRef--;
24921
 
  if( pId->nRef==0 ){
24922
 
    struct vxworksFileId **pp;
24923
 
    for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
24924
 
    assert( *pp==pId );
24925
 
    *pp = pId->pNext;
24926
 
    sqlite3_free(pId);
24927
 
  }
24928
 
  unixLeaveMutex();
24929
 
}
24930
 
#endif /* OS_VXWORKS */
24931
 
/*************** End of Unique File ID Utility Used By VxWorks ****************
24932
 
******************************************************************************/
24933
 
 
24934
 
 
24935
 
/******************************************************************************
24936
 
*************************** Posix Advisory Locking ****************************
24937
 
**
24938
 
** POSIX advisory locks are broken by design.  ANSI STD 1003.1 (1996)
24939
 
** section 6.5.2.2 lines 483 through 490 specify that when a process
24940
 
** sets or clears a lock, that operation overrides any prior locks set
24941
 
** by the same process.  It does not explicitly say so, but this implies
24942
 
** that it overrides locks set by the same process using a different
24943
 
** file descriptor.  Consider this test case:
24944
 
**
24945
 
**       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
24946
 
**       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
24947
 
**
24948
 
** Suppose ./file1 and ./file2 are really the same file (because
24949
 
** one is a hard or symbolic link to the other) then if you set
24950
 
** an exclusive lock on fd1, then try to get an exclusive lock
24951
 
** on fd2, it works.  I would have expected the second lock to
24952
 
** fail since there was already a lock on the file due to fd1.
24953
 
** But not so.  Since both locks came from the same process, the
24954
 
** second overrides the first, even though they were on different
24955
 
** file descriptors opened on different file names.
24956
 
**
24957
 
** This means that we cannot use POSIX locks to synchronize file access
24958
 
** among competing threads of the same process.  POSIX locks will work fine
24959
 
** to synchronize access for threads in separate processes, but not
24960
 
** threads within the same process.
24961
 
**
24962
 
** To work around the problem, SQLite has to manage file locks internally
24963
 
** on its own.  Whenever a new database is opened, we have to find the
24964
 
** specific inode of the database file (the inode is determined by the
24965
 
** st_dev and st_ino fields of the stat structure that fstat() fills in)
24966
 
** and check for locks already existing on that inode.  When locks are
24967
 
** created or removed, we have to look at our own internal record of the
24968
 
** locks to see if another thread has previously set a lock on that same
24969
 
** inode.
24970
 
**
24971
 
** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
24972
 
** For VxWorks, we have to use the alternative unique ID system based on
24973
 
** canonical filename and implemented in the previous division.)
24974
 
**
24975
 
** The sqlite3_file structure for POSIX is no longer just an integer file
24976
 
** descriptor.  It is now a structure that holds the integer file
24977
 
** descriptor and a pointer to a structure that describes the internal
24978
 
** locks on the corresponding inode.  There is one locking structure
24979
 
** per inode, so if the same inode is opened twice, both unixFile structures
24980
 
** point to the same locking structure.  The locking structure keeps
24981
 
** a reference count (so we will know when to delete it) and a "cnt"
24982
 
** field that tells us its internal lock status.  cnt==0 means the
24983
 
** file is unlocked.  cnt==-1 means the file has an exclusive lock.
24984
 
** cnt>0 means there are cnt shared locks on the file.
24985
 
**
24986
 
** Any attempt to lock or unlock a file first checks the locking
24987
 
** structure.  The fcntl() system call is only invoked to set a 
24988
 
** POSIX lock if the internal lock structure transitions between
24989
 
** a locked and an unlocked state.
24990
 
**
24991
 
** But wait:  there are yet more problems with POSIX advisory locks.
24992
 
**
24993
 
** If you close a file descriptor that points to a file that has locks,
24994
 
** all locks on that file that are owned by the current process are
24995
 
** released.  To work around this problem, each unixInodeInfo object
24996
 
** maintains a count of the number of pending locks on tha inode.
24997
 
** When an attempt is made to close an unixFile, if there are
24998
 
** other unixFile open on the same inode that are holding locks, the call
24999
 
** to close() the file descriptor is deferred until all of the locks clear.
25000
 
** The unixInodeInfo structure keeps a list of file descriptors that need to
25001
 
** be closed and that list is walked (and cleared) when the last lock
25002
 
** clears.
25003
 
**
25004
 
** Yet another problem:  LinuxThreads do not play well with posix locks.
25005
 
**
25006
 
** Many older versions of linux use the LinuxThreads library which is
25007
 
** not posix compliant.  Under LinuxThreads, a lock created by thread
25008
 
** A cannot be modified or overridden by a different thread B.
25009
 
** Only thread A can modify the lock.  Locking behavior is correct
25010
 
** if the appliation uses the newer Native Posix Thread Library (NPTL)
25011
 
** on linux - with NPTL a lock created by thread A can override locks
25012
 
** in thread B.  But there is no way to know at compile-time which
25013
 
** threading library is being used.  So there is no way to know at
25014
 
** compile-time whether or not thread A can override locks on thread B.
25015
 
** One has to do a run-time check to discover the behavior of the
25016
 
** current process.
25017
 
**
25018
 
** SQLite used to support LinuxThreads.  But support for LinuxThreads
25019
 
** was dropped beginning with version 3.7.0.  SQLite will still work with
25020
 
** LinuxThreads provided that (1) there is no more than one connection 
25021
 
** per database file in the same process and (2) database connections
25022
 
** do not move across threads.
25023
 
*/
25024
 
 
25025
 
/*
25026
 
** An instance of the following structure serves as the key used
25027
 
** to locate a particular unixInodeInfo object.
25028
 
*/
25029
 
struct unixFileId {
25030
 
  dev_t dev;                  /* Device number */
25031
 
#if OS_VXWORKS
25032
 
  struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
25033
 
#else
25034
 
  ino_t ino;                  /* Inode number */
25035
 
#endif
25036
 
};
25037
 
 
25038
 
/*
25039
 
** An instance of the following structure is allocated for each open
25040
 
** inode.  Or, on LinuxThreads, there is one of these structures for
25041
 
** each inode opened by each thread.
25042
 
**
25043
 
** A single inode can have multiple file descriptors, so each unixFile
25044
 
** structure contains a pointer to an instance of this object and this
25045
 
** object keeps a count of the number of unixFile pointing to it.
25046
 
*/
25047
 
struct unixInodeInfo {
25048
 
  struct unixFileId fileId;       /* The lookup key */
25049
 
  int nShared;                    /* Number of SHARED locks held */
25050
 
  unsigned char eFileLock;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
25051
 
  unsigned char bProcessLock;     /* An exclusive process lock is held */
25052
 
  int nRef;                       /* Number of pointers to this structure */
25053
 
  unixShmNode *pShmNode;          /* Shared memory associated with this inode */
25054
 
  int nLock;                      /* Number of outstanding file locks */
25055
 
  UnixUnusedFd *pUnused;          /* Unused file descriptors to close */
25056
 
  unixInodeInfo *pNext;           /* List of all unixInodeInfo objects */
25057
 
  unixInodeInfo *pPrev;           /*    .... doubly linked */
25058
 
#if defined(SQLITE_ENABLE_LOCKING_STYLE)
25059
 
  unsigned long long sharedByte;  /* for AFP simulated shared lock */
25060
 
#endif
25061
 
#if OS_VXWORKS
25062
 
  sem_t *pSem;                    /* Named POSIX semaphore */
25063
 
  char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
25064
 
#endif
25065
 
};
25066
 
 
25067
 
/*
25068
 
** A lists of all unixInodeInfo objects.
25069
 
*/
25070
 
static unixInodeInfo *inodeList = 0;
25071
 
 
25072
 
/*
25073
 
**
25074
 
** This function - unixLogError_x(), is only ever called via the macro
25075
 
** unixLogError().
25076
 
**
25077
 
** It is invoked after an error occurs in an OS function and errno has been
25078
 
** set. It logs a message using sqlite3_log() containing the current value of
25079
 
** errno and, if possible, the human-readable equivalent from strerror() or
25080
 
** strerror_r().
25081
 
**
25082
 
** The first argument passed to the macro should be the error code that
25083
 
** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 
25084
 
** The two subsequent arguments should be the name of the OS function that
25085
 
** failed (e.g. "unlink", "open") and the the associated file-system path,
25086
 
** if any.
25087
 
*/
25088
 
#define unixLogError(a,b,c)     unixLogErrorAtLine(a,b,c,__LINE__)
25089
 
static int unixLogErrorAtLine(
25090
 
  int errcode,                    /* SQLite error code */
25091
 
  const char *zFunc,              /* Name of OS function that failed */
25092
 
  const char *zPath,              /* File path associated with error */
25093
 
  int iLine                       /* Source line number where error occurred */
25094
 
){
25095
 
  char *zErr;                     /* Message from strerror() or equivalent */
25096
 
  int iErrno = errno;             /* Saved syscall error number */
25097
 
 
25098
 
  /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
25099
 
  ** the strerror() function to obtain the human-readable error message
25100
 
  ** equivalent to errno. Otherwise, use strerror_r().
25101
 
  */ 
25102
 
#if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
25103
 
  char aErr[80];
25104
 
  memset(aErr, 0, sizeof(aErr));
25105
 
  zErr = aErr;
25106
 
 
25107
 
  /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
25108
 
  ** assume that the system provides the the GNU version of strerror_r() that 
25109
 
  ** returns a pointer to a buffer containing the error message. That pointer 
25110
 
  ** may point to aErr[], or it may point to some static storage somewhere. 
25111
 
  ** Otherwise, assume that the system provides the POSIX version of 
25112
 
  ** strerror_r(), which always writes an error message into aErr[].
25113
 
  **
25114
 
  ** If the code incorrectly assumes that it is the POSIX version that is
25115
 
  ** available, the error message will often be an empty string. Not a
25116
 
  ** huge problem. Incorrectly concluding that the GNU version is available 
25117
 
  ** could lead to a segfault though.
25118
 
  */
25119
 
#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
25120
 
  zErr = 
25121
 
# endif
25122
 
  strerror_r(iErrno, aErr, sizeof(aErr)-1);
25123
 
 
25124
 
#elif SQLITE_THREADSAFE
25125
 
  /* This is a threadsafe build, but strerror_r() is not available. */
25126
 
  zErr = "";
25127
 
#else
25128
 
  /* Non-threadsafe build, use strerror(). */
25129
 
  zErr = strerror(iErrno);
25130
 
#endif
25131
 
 
25132
 
  assert( errcode!=SQLITE_OK );
25133
 
  if( zPath==0 ) zPath = "";
25134
 
  sqlite3_log(errcode,
25135
 
      "os_unix.c:%d: (%d) %s(%s) - %s",
25136
 
      iLine, iErrno, zFunc, zPath, zErr
25137
 
  );
25138
 
 
25139
 
  return errcode;
25140
 
}
25141
 
 
25142
 
/*
25143
 
** Close a file descriptor.
25144
 
**
25145
 
** We assume that close() almost always works, since it is only in a
25146
 
** very sick application or on a very sick platform that it might fail.
25147
 
** If it does fail, simply leak the file descriptor, but do log the
25148
 
** error.
25149
 
**
25150
 
** Note that it is not safe to retry close() after EINTR since the
25151
 
** file descriptor might have already been reused by another thread.
25152
 
** So we don't even try to recover from an EINTR.  Just log the error
25153
 
** and move on.
25154
 
*/
25155
 
static void robust_close(unixFile *pFile, int h, int lineno){
25156
 
  if( osClose(h) ){
25157
 
    unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
25158
 
                       pFile ? pFile->zPath : 0, lineno);
25159
 
  }
25160
 
}
25161
 
 
25162
 
/*
25163
 
** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
25164
 
*/ 
25165
 
static void closePendingFds(unixFile *pFile){
25166
 
  unixInodeInfo *pInode = pFile->pInode;
25167
 
  UnixUnusedFd *p;
25168
 
  UnixUnusedFd *pNext;
25169
 
  for(p=pInode->pUnused; p; p=pNext){
25170
 
    pNext = p->pNext;
25171
 
    robust_close(pFile, p->fd, __LINE__);
25172
 
    sqlite3_free(p);
25173
 
  }
25174
 
  pInode->pUnused = 0;
25175
 
}
25176
 
 
25177
 
/*
25178
 
** Release a unixInodeInfo structure previously allocated by findInodeInfo().
25179
 
**
25180
 
** The mutex entered using the unixEnterMutex() function must be held
25181
 
** when this function is called.
25182
 
*/
25183
 
static void releaseInodeInfo(unixFile *pFile){
25184
 
  unixInodeInfo *pInode = pFile->pInode;
25185
 
  assert( unixMutexHeld() );
25186
 
  if( ALWAYS(pInode) ){
25187
 
    pInode->nRef--;
25188
 
    if( pInode->nRef==0 ){
25189
 
      assert( pInode->pShmNode==0 );
25190
 
      closePendingFds(pFile);
25191
 
      if( pInode->pPrev ){
25192
 
        assert( pInode->pPrev->pNext==pInode );
25193
 
        pInode->pPrev->pNext = pInode->pNext;
25194
 
      }else{
25195
 
        assert( inodeList==pInode );
25196
 
        inodeList = pInode->pNext;
25197
 
      }
25198
 
      if( pInode->pNext ){
25199
 
        assert( pInode->pNext->pPrev==pInode );
25200
 
        pInode->pNext->pPrev = pInode->pPrev;
25201
 
      }
25202
 
      sqlite3_free(pInode);
25203
 
    }
25204
 
  }
25205
 
}
25206
 
 
25207
 
/*
25208
 
** Given a file descriptor, locate the unixInodeInfo object that
25209
 
** describes that file descriptor.  Create a new one if necessary.  The
25210
 
** return value might be uninitialized if an error occurs.
25211
 
**
25212
 
** The mutex entered using the unixEnterMutex() function must be held
25213
 
** when this function is called.
25214
 
**
25215
 
** Return an appropriate error code.
25216
 
*/
25217
 
static int findInodeInfo(
25218
 
  unixFile *pFile,               /* Unix file with file desc used in the key */
25219
 
  unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
25220
 
){
25221
 
  int rc;                        /* System call return code */
25222
 
  int fd;                        /* The file descriptor for pFile */
25223
 
  struct unixFileId fileId;      /* Lookup key for the unixInodeInfo */
25224
 
  struct stat statbuf;           /* Low-level file information */
25225
 
  unixInodeInfo *pInode = 0;     /* Candidate unixInodeInfo object */
25226
 
 
25227
 
  assert( unixMutexHeld() );
25228
 
 
25229
 
  /* Get low-level information about the file that we can used to
25230
 
  ** create a unique name for the file.
25231
 
  */
25232
 
  fd = pFile->h;
25233
 
  rc = osFstat(fd, &statbuf);
25234
 
  if( rc!=0 ){
25235
 
    pFile->lastErrno = errno;
25236
 
#ifdef EOVERFLOW
25237
 
    if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
25238
 
#endif
25239
 
    return SQLITE_IOERR;
25240
 
  }
25241
 
 
25242
 
#ifdef __APPLE__
25243
 
  /* On OS X on an msdos filesystem, the inode number is reported
25244
 
  ** incorrectly for zero-size files.  See ticket #3260.  To work
25245
 
  ** around this problem (we consider it a bug in OS X, not SQLite)
25246
 
  ** we always increase the file size to 1 by writing a single byte
25247
 
  ** prior to accessing the inode number.  The one byte written is
25248
 
  ** an ASCII 'S' character which also happens to be the first byte
25249
 
  ** in the header of every SQLite database.  In this way, if there
25250
 
  ** is a race condition such that another thread has already populated
25251
 
  ** the first page of the database, no damage is done.
25252
 
  */
25253
 
  if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
25254
 
    do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
25255
 
    if( rc!=1 ){
25256
 
      pFile->lastErrno = errno;
25257
 
      return SQLITE_IOERR;
25258
 
    }
25259
 
    rc = osFstat(fd, &statbuf);
25260
 
    if( rc!=0 ){
25261
 
      pFile->lastErrno = errno;
25262
 
      return SQLITE_IOERR;
25263
 
    }
25264
 
  }
25265
 
#endif
25266
 
 
25267
 
  memset(&fileId, 0, sizeof(fileId));
25268
 
  fileId.dev = statbuf.st_dev;
25269
 
#if OS_VXWORKS
25270
 
  fileId.pId = pFile->pId;
25271
 
#else
25272
 
  fileId.ino = statbuf.st_ino;
25273
 
#endif
25274
 
  pInode = inodeList;
25275
 
  while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
25276
 
    pInode = pInode->pNext;
25277
 
  }
25278
 
  if( pInode==0 ){
25279
 
    pInode = sqlite3_malloc( sizeof(*pInode) );
25280
 
    if( pInode==0 ){
25281
 
      return SQLITE_NOMEM;
25282
 
    }
25283
 
    memset(pInode, 0, sizeof(*pInode));
25284
 
    memcpy(&pInode->fileId, &fileId, sizeof(fileId));
25285
 
    pInode->nRef = 1;
25286
 
    pInode->pNext = inodeList;
25287
 
    pInode->pPrev = 0;
25288
 
    if( inodeList ) inodeList->pPrev = pInode;
25289
 
    inodeList = pInode;
25290
 
  }else{
25291
 
    pInode->nRef++;
25292
 
  }
25293
 
  *ppInode = pInode;
25294
 
  return SQLITE_OK;
25295
 
}
25296
 
 
25297
 
 
25298
 
/*
25299
 
** This routine checks if there is a RESERVED lock held on the specified
25300
 
** file by this or any other process. If such a lock is held, set *pResOut
25301
 
** to a non-zero value otherwise *pResOut is set to zero.  The return value
25302
 
** is set to SQLITE_OK unless an I/O error occurs during lock checking.
25303
 
*/
25304
 
static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
25305
 
  int rc = SQLITE_OK;
25306
 
  int reserved = 0;
25307
 
  unixFile *pFile = (unixFile*)id;
25308
 
 
25309
 
  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
25310
 
 
25311
 
  assert( pFile );
25312
 
  unixEnterMutex(); /* Because pFile->pInode is shared across threads */
25313
 
 
25314
 
  /* Check if a thread in this process holds such a lock */
25315
 
  if( pFile->pInode->eFileLock>SHARED_LOCK ){
25316
 
    reserved = 1;
25317
 
  }
25318
 
 
25319
 
  /* Otherwise see if some other process holds it.
25320
 
  */
25321
 
#ifndef __DJGPP__
25322
 
  if( !reserved && !pFile->pInode->bProcessLock ){
25323
 
    struct flock lock;
25324
 
    lock.l_whence = SEEK_SET;
25325
 
    lock.l_start = RESERVED_BYTE;
25326
 
    lock.l_len = 1;
25327
 
    lock.l_type = F_WRLCK;
25328
 
    if( osFcntl(pFile->h, F_GETLK, &lock) ){
25329
 
      rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
25330
 
      pFile->lastErrno = errno;
25331
 
    } else if( lock.l_type!=F_UNLCK ){
25332
 
      reserved = 1;
25333
 
    }
25334
 
  }
25335
 
#endif
25336
 
  
25337
 
  unixLeaveMutex();
25338
 
  OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
25339
 
 
25340
 
  *pResOut = reserved;
25341
 
  return rc;
25342
 
}
25343
 
 
25344
 
/*
25345
 
** Attempt to set a system-lock on the file pFile.  The lock is 
25346
 
** described by pLock.
25347
 
**
25348
 
** If the pFile was opened read/write from unix-excl, then the only lock
25349
 
** ever obtained is an exclusive lock, and it is obtained exactly once
25350
 
** the first time any lock is attempted.  All subsequent system locking
25351
 
** operations become no-ops.  Locking operations still happen internally,
25352
 
** in order to coordinate access between separate database connections
25353
 
** within this process, but all of that is handled in memory and the
25354
 
** operating system does not participate.
25355
 
**
25356
 
** This function is a pass-through to fcntl(F_SETLK) if pFile is using
25357
 
** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
25358
 
** and is read-only.
25359
 
**
25360
 
** Zero is returned if the call completes successfully, or -1 if a call
25361
 
** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
25362
 
*/
25363
 
static int unixFileLock(unixFile *pFile, struct flock *pLock){
25364
 
  int rc;
25365
 
  unixInodeInfo *pInode = pFile->pInode;
25366
 
  assert( unixMutexHeld() );
25367
 
  assert( pInode!=0 );
25368
 
  if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock)
25369
 
   && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0)
25370
 
  ){
25371
 
    if( pInode->bProcessLock==0 ){
25372
 
      struct flock lock;
25373
 
      assert( pInode->nLock==0 );
25374
 
      lock.l_whence = SEEK_SET;
25375
 
      lock.l_start = SHARED_FIRST;
25376
 
      lock.l_len = SHARED_SIZE;
25377
 
      lock.l_type = F_WRLCK;
25378
 
      rc = osFcntl(pFile->h, F_SETLK, &lock);
25379
 
      if( rc<0 ) return rc;
25380
 
      pInode->bProcessLock = 1;
25381
 
      pInode->nLock++;
25382
 
    }else{
25383
 
      rc = 0;
25384
 
    }
25385
 
  }else{
25386
 
    rc = osFcntl(pFile->h, F_SETLK, pLock);
25387
 
  }
25388
 
  return rc;
25389
 
}
25390
 
 
25391
 
/*
25392
 
** Lock the file with the lock specified by parameter eFileLock - one
25393
 
** of the following:
25394
 
**
25395
 
**     (1) SHARED_LOCK
25396
 
**     (2) RESERVED_LOCK
25397
 
**     (3) PENDING_LOCK
25398
 
**     (4) EXCLUSIVE_LOCK
25399
 
**
25400
 
** Sometimes when requesting one lock state, additional lock states
25401
 
** are inserted in between.  The locking might fail on one of the later
25402
 
** transitions leaving the lock state different from what it started but
25403
 
** still short of its goal.  The following chart shows the allowed
25404
 
** transitions and the inserted intermediate states:
25405
 
**
25406
 
**    UNLOCKED -> SHARED
25407
 
**    SHARED -> RESERVED
25408
 
**    SHARED -> (PENDING) -> EXCLUSIVE
25409
 
**    RESERVED -> (PENDING) -> EXCLUSIVE
25410
 
**    PENDING -> EXCLUSIVE
25411
 
**
25412
 
** This routine will only increase a lock.  Use the sqlite3OsUnlock()
25413
 
** routine to lower a locking level.
25414
 
*/
25415
 
static int unixLock(sqlite3_file *id, int eFileLock){
25416
 
  /* The following describes the implementation of the various locks and
25417
 
  ** lock transitions in terms of the POSIX advisory shared and exclusive
25418
 
  ** lock primitives (called read-locks and write-locks below, to avoid
25419
 
  ** confusion with SQLite lock names). The algorithms are complicated
25420
 
  ** slightly in order to be compatible with windows systems simultaneously
25421
 
  ** accessing the same database file, in case that is ever required.
25422
 
  **
25423
 
  ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
25424
 
  ** byte', each single bytes at well known offsets, and the 'shared byte
25425
 
  ** range', a range of 510 bytes at a well known offset.
25426
 
  **
25427
 
  ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
25428
 
  ** byte'.  If this is successful, a random byte from the 'shared byte
25429
 
  ** range' is read-locked and the lock on the 'pending byte' released.
25430
 
  **
25431
 
  ** A process may only obtain a RESERVED lock after it has a SHARED lock.
25432
 
  ** A RESERVED lock is implemented by grabbing a write-lock on the
25433
 
  ** 'reserved byte'. 
25434
 
  **
25435
 
  ** A process may only obtain a PENDING lock after it has obtained a
25436
 
  ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
25437
 
  ** on the 'pending byte'. This ensures that no new SHARED locks can be
25438
 
  ** obtained, but existing SHARED locks are allowed to persist. A process
25439
 
  ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
25440
 
  ** This property is used by the algorithm for rolling back a journal file
25441
 
  ** after a crash.
25442
 
  **
25443
 
  ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
25444
 
  ** implemented by obtaining a write-lock on the entire 'shared byte
25445
 
  ** range'. Since all other locks require a read-lock on one of the bytes
25446
 
  ** within this range, this ensures that no other locks are held on the
25447
 
  ** database. 
25448
 
  **
25449
 
  ** The reason a single byte cannot be used instead of the 'shared byte
25450
 
  ** range' is that some versions of windows do not support read-locks. By
25451
 
  ** locking a random byte from a range, concurrent SHARED locks may exist
25452
 
  ** even if the locking primitive used is always a write-lock.
25453
 
  */
25454
 
  int rc = SQLITE_OK;
25455
 
  unixFile *pFile = (unixFile*)id;
25456
 
  unixInodeInfo *pInode = pFile->pInode;
25457
 
  struct flock lock;
25458
 
  int tErrno = 0;
25459
 
 
25460
 
  assert( pFile );
25461
 
  OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
25462
 
      azFileLock(eFileLock), azFileLock(pFile->eFileLock),
25463
 
      azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
25464
 
 
25465
 
  /* If there is already a lock of this type or more restrictive on the
25466
 
  ** unixFile, do nothing. Don't use the end_lock: exit path, as
25467
 
  ** unixEnterMutex() hasn't been called yet.
25468
 
  */
25469
 
  if( pFile->eFileLock>=eFileLock ){
25470
 
    OSTRACE(("LOCK    %d %s ok (already held) (unix)\n", pFile->h,
25471
 
            azFileLock(eFileLock)));
25472
 
    return SQLITE_OK;
25473
 
  }
25474
 
 
25475
 
  /* Make sure the locking sequence is correct.
25476
 
  **  (1) We never move from unlocked to anything higher than shared lock.
25477
 
  **  (2) SQLite never explicitly requests a pendig lock.
25478
 
  **  (3) A shared lock is always held when a reserve lock is requested.
25479
 
  */
25480
 
  assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
25481
 
  assert( eFileLock!=PENDING_LOCK );
25482
 
  assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
25483
 
 
25484
 
  /* This mutex is needed because pFile->pInode is shared across threads
25485
 
  */
25486
 
  unixEnterMutex();
25487
 
  pInode = pFile->pInode;
25488
 
 
25489
 
  /* If some thread using this PID has a lock via a different unixFile*
25490
 
  ** handle that precludes the requested lock, return BUSY.
25491
 
  */
25492
 
  if( (pFile->eFileLock!=pInode->eFileLock && 
25493
 
          (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
25494
 
  ){
25495
 
    rc = SQLITE_BUSY;
25496
 
    goto end_lock;
25497
 
  }
25498
 
 
25499
 
  /* If a SHARED lock is requested, and some thread using this PID already
25500
 
  ** has a SHARED or RESERVED lock, then increment reference counts and
25501
 
  ** return SQLITE_OK.
25502
 
  */
25503
 
  if( eFileLock==SHARED_LOCK && 
25504
 
      (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
25505
 
    assert( eFileLock==SHARED_LOCK );
25506
 
    assert( pFile->eFileLock==0 );
25507
 
    assert( pInode->nShared>0 );
25508
 
    pFile->eFileLock = SHARED_LOCK;
25509
 
    pInode->nShared++;
25510
 
    pInode->nLock++;
25511
 
    goto end_lock;
25512
 
  }
25513
 
 
25514
 
 
25515
 
  /* A PENDING lock is needed before acquiring a SHARED lock and before
25516
 
  ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
25517
 
  ** be released.
25518
 
  */
25519
 
  lock.l_len = 1L;
25520
 
  lock.l_whence = SEEK_SET;
25521
 
  if( eFileLock==SHARED_LOCK 
25522
 
      || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
25523
 
  ){
25524
 
    lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
25525
 
    lock.l_start = PENDING_BYTE;
25526
 
    if( unixFileLock(pFile, &lock) ){
25527
 
      tErrno = errno;
25528
 
      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25529
 
      if( rc!=SQLITE_BUSY ){
25530
 
        pFile->lastErrno = tErrno;
25531
 
      }
25532
 
      goto end_lock;
25533
 
    }
25534
 
  }
25535
 
 
25536
 
 
25537
 
  /* If control gets to this point, then actually go ahead and make
25538
 
  ** operating system calls for the specified lock.
25539
 
  */
25540
 
  if( eFileLock==SHARED_LOCK ){
25541
 
    assert( pInode->nShared==0 );
25542
 
    assert( pInode->eFileLock==0 );
25543
 
    assert( rc==SQLITE_OK );
25544
 
 
25545
 
    /* Now get the read-lock */
25546
 
    lock.l_start = SHARED_FIRST;
25547
 
    lock.l_len = SHARED_SIZE;
25548
 
    if( unixFileLock(pFile, &lock) ){
25549
 
      tErrno = errno;
25550
 
      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25551
 
    }
25552
 
 
25553
 
    /* Drop the temporary PENDING lock */
25554
 
    lock.l_start = PENDING_BYTE;
25555
 
    lock.l_len = 1L;
25556
 
    lock.l_type = F_UNLCK;
25557
 
    if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
25558
 
      /* This could happen with a network mount */
25559
 
      tErrno = errno;
25560
 
      rc = SQLITE_IOERR_UNLOCK; 
25561
 
    }
25562
 
 
25563
 
    if( rc ){
25564
 
      if( rc!=SQLITE_BUSY ){
25565
 
        pFile->lastErrno = tErrno;
25566
 
      }
25567
 
      goto end_lock;
25568
 
    }else{
25569
 
      pFile->eFileLock = SHARED_LOCK;
25570
 
      pInode->nLock++;
25571
 
      pInode->nShared = 1;
25572
 
    }
25573
 
  }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
25574
 
    /* We are trying for an exclusive lock but another thread in this
25575
 
    ** same process is still holding a shared lock. */
25576
 
    rc = SQLITE_BUSY;
25577
 
  }else{
25578
 
    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
25579
 
    ** assumed that there is a SHARED or greater lock on the file
25580
 
    ** already.
25581
 
    */
25582
 
    assert( 0!=pFile->eFileLock );
25583
 
    lock.l_type = F_WRLCK;
25584
 
 
25585
 
    assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
25586
 
    if( eFileLock==RESERVED_LOCK ){
25587
 
      lock.l_start = RESERVED_BYTE;
25588
 
      lock.l_len = 1L;
25589
 
    }else{
25590
 
      lock.l_start = SHARED_FIRST;
25591
 
      lock.l_len = SHARED_SIZE;
25592
 
    }
25593
 
 
25594
 
    if( unixFileLock(pFile, &lock) ){
25595
 
      tErrno = errno;
25596
 
      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25597
 
      if( rc!=SQLITE_BUSY ){
25598
 
        pFile->lastErrno = tErrno;
25599
 
      }
25600
 
    }
25601
 
  }
25602
 
  
25603
 
 
25604
 
#ifndef NDEBUG
25605
 
  /* Set up the transaction-counter change checking flags when
25606
 
  ** transitioning from a SHARED to a RESERVED lock.  The change
25607
 
  ** from SHARED to RESERVED marks the beginning of a normal
25608
 
  ** write operation (not a hot journal rollback).
25609
 
  */
25610
 
  if( rc==SQLITE_OK
25611
 
   && pFile->eFileLock<=SHARED_LOCK
25612
 
   && eFileLock==RESERVED_LOCK
25613
 
  ){
25614
 
    pFile->transCntrChng = 0;
25615
 
    pFile->dbUpdate = 0;
25616
 
    pFile->inNormalWrite = 1;
25617
 
  }
25618
 
#endif
25619
 
 
25620
 
 
25621
 
  if( rc==SQLITE_OK ){
25622
 
    pFile->eFileLock = eFileLock;
25623
 
    pInode->eFileLock = eFileLock;
25624
 
  }else if( eFileLock==EXCLUSIVE_LOCK ){
25625
 
    pFile->eFileLock = PENDING_LOCK;
25626
 
    pInode->eFileLock = PENDING_LOCK;
25627
 
  }
25628
 
 
25629
 
end_lock:
25630
 
  unixLeaveMutex();
25631
 
  OSTRACE(("LOCK    %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), 
25632
 
      rc==SQLITE_OK ? "ok" : "failed"));
25633
 
  return rc;
25634
 
}
25635
 
 
25636
 
/*
25637
 
** Add the file descriptor used by file handle pFile to the corresponding
25638
 
** pUnused list.
25639
 
*/
25640
 
static void setPendingFd(unixFile *pFile){
25641
 
  unixInodeInfo *pInode = pFile->pInode;
25642
 
  UnixUnusedFd *p = pFile->pUnused;
25643
 
  p->pNext = pInode->pUnused;
25644
 
  pInode->pUnused = p;
25645
 
  pFile->h = -1;
25646
 
  pFile->pUnused = 0;
25647
 
}
25648
 
 
25649
 
/*
25650
 
** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
25651
 
** must be either NO_LOCK or SHARED_LOCK.
25652
 
**
25653
 
** If the locking level of the file descriptor is already at or below
25654
 
** the requested locking level, this routine is a no-op.
25655
 
** 
25656
 
** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
25657
 
** the byte range is divided into 2 parts and the first part is unlocked then
25658
 
** set to a read lock, then the other part is simply unlocked.  This works 
25659
 
** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to 
25660
 
** remove the write lock on a region when a read lock is set.
25661
 
*/
25662
 
static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
25663
 
  unixFile *pFile = (unixFile*)id;
25664
 
  unixInodeInfo *pInode;
25665
 
  struct flock lock;
25666
 
  int rc = SQLITE_OK;
25667
 
  int h;
25668
 
 
25669
 
  assert( pFile );
25670
 
  OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
25671
 
      pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
25672
 
      getpid()));
25673
 
 
25674
 
  assert( eFileLock<=SHARED_LOCK );
25675
 
  if( pFile->eFileLock<=eFileLock ){
25676
 
    return SQLITE_OK;
25677
 
  }
25678
 
  unixEnterMutex();
25679
 
  h = pFile->h;
25680
 
  pInode = pFile->pInode;
25681
 
  assert( pInode->nShared!=0 );
25682
 
  if( pFile->eFileLock>SHARED_LOCK ){
25683
 
    assert( pInode->eFileLock==pFile->eFileLock );
25684
 
    SimulateIOErrorBenign(1);
25685
 
    SimulateIOError( h=(-1) )
25686
 
    SimulateIOErrorBenign(0);
25687
 
 
25688
 
#ifndef NDEBUG
25689
 
    /* When reducing a lock such that other processes can start
25690
 
    ** reading the database file again, make sure that the
25691
 
    ** transaction counter was updated if any part of the database
25692
 
    ** file changed.  If the transaction counter is not updated,
25693
 
    ** other connections to the same file might not realize that
25694
 
    ** the file has changed and hence might not know to flush their
25695
 
    ** cache.  The use of a stale cache can lead to database corruption.
25696
 
    */
25697
 
#if 0
25698
 
    assert( pFile->inNormalWrite==0
25699
 
         || pFile->dbUpdate==0
25700
 
         || pFile->transCntrChng==1 );
25701
 
#endif
25702
 
    pFile->inNormalWrite = 0;
25703
 
#endif
25704
 
 
25705
 
    /* downgrading to a shared lock on NFS involves clearing the write lock
25706
 
    ** before establishing the readlock - to avoid a race condition we downgrade
25707
 
    ** the lock in 2 blocks, so that part of the range will be covered by a 
25708
 
    ** write lock until the rest is covered by a read lock:
25709
 
    **  1:   [WWWWW]
25710
 
    **  2:   [....W]
25711
 
    **  3:   [RRRRW]
25712
 
    **  4:   [RRRR.]
25713
 
    */
25714
 
    if( eFileLock==SHARED_LOCK ){
25715
 
 
25716
 
#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
25717
 
      (void)handleNFSUnlock;
25718
 
      assert( handleNFSUnlock==0 );
25719
 
#endif
25720
 
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
25721
 
      if( handleNFSUnlock ){
25722
 
        int tErrno;               /* Error code from system call errors */
25723
 
        off_t divSize = SHARED_SIZE - 1;
25724
 
        
25725
 
        lock.l_type = F_UNLCK;
25726
 
        lock.l_whence = SEEK_SET;
25727
 
        lock.l_start = SHARED_FIRST;
25728
 
        lock.l_len = divSize;
25729
 
        if( unixFileLock(pFile, &lock)==(-1) ){
25730
 
          tErrno = errno;
25731
 
          rc = SQLITE_IOERR_UNLOCK;
25732
 
          if( IS_LOCK_ERROR(rc) ){
25733
 
            pFile->lastErrno = tErrno;
25734
 
          }
25735
 
          goto end_unlock;
25736
 
        }
25737
 
        lock.l_type = F_RDLCK;
25738
 
        lock.l_whence = SEEK_SET;
25739
 
        lock.l_start = SHARED_FIRST;
25740
 
        lock.l_len = divSize;
25741
 
        if( unixFileLock(pFile, &lock)==(-1) ){
25742
 
          tErrno = errno;
25743
 
          rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
25744
 
          if( IS_LOCK_ERROR(rc) ){
25745
 
            pFile->lastErrno = tErrno;
25746
 
          }
25747
 
          goto end_unlock;
25748
 
        }
25749
 
        lock.l_type = F_UNLCK;
25750
 
        lock.l_whence = SEEK_SET;
25751
 
        lock.l_start = SHARED_FIRST+divSize;
25752
 
        lock.l_len = SHARED_SIZE-divSize;
25753
 
        if( unixFileLock(pFile, &lock)==(-1) ){
25754
 
          tErrno = errno;
25755
 
          rc = SQLITE_IOERR_UNLOCK;
25756
 
          if( IS_LOCK_ERROR(rc) ){
25757
 
            pFile->lastErrno = tErrno;
25758
 
          }
25759
 
          goto end_unlock;
25760
 
        }
25761
 
      }else
25762
 
#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
25763
 
      {
25764
 
        lock.l_type = F_RDLCK;
25765
 
        lock.l_whence = SEEK_SET;
25766
 
        lock.l_start = SHARED_FIRST;
25767
 
        lock.l_len = SHARED_SIZE;
25768
 
        if( unixFileLock(pFile, &lock) ){
25769
 
          /* In theory, the call to unixFileLock() cannot fail because another
25770
 
          ** process is holding an incompatible lock. If it does, this 
25771
 
          ** indicates that the other process is not following the locking
25772
 
          ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
25773
 
          ** SQLITE_BUSY would confuse the upper layer (in practice it causes 
25774
 
          ** an assert to fail). */ 
25775
 
          rc = SQLITE_IOERR_RDLOCK;
25776
 
          pFile->lastErrno = errno;
25777
 
          goto end_unlock;
25778
 
        }
25779
 
      }
25780
 
    }
25781
 
    lock.l_type = F_UNLCK;
25782
 
    lock.l_whence = SEEK_SET;
25783
 
    lock.l_start = PENDING_BYTE;
25784
 
    lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
25785
 
    if( unixFileLock(pFile, &lock)==0 ){
25786
 
      pInode->eFileLock = SHARED_LOCK;
25787
 
    }else{
25788
 
      rc = SQLITE_IOERR_UNLOCK;
25789
 
      pFile->lastErrno = errno;
25790
 
      goto end_unlock;
25791
 
    }
25792
 
  }
25793
 
  if( eFileLock==NO_LOCK ){
25794
 
    /* Decrement the shared lock counter.  Release the lock using an
25795
 
    ** OS call only when all threads in this same process have released
25796
 
    ** the lock.
25797
 
    */
25798
 
    pInode->nShared--;
25799
 
    if( pInode->nShared==0 ){
25800
 
      lock.l_type = F_UNLCK;
25801
 
      lock.l_whence = SEEK_SET;
25802
 
      lock.l_start = lock.l_len = 0L;
25803
 
      SimulateIOErrorBenign(1);
25804
 
      SimulateIOError( h=(-1) )
25805
 
      SimulateIOErrorBenign(0);
25806
 
      if( unixFileLock(pFile, &lock)==0 ){
25807
 
        pInode->eFileLock = NO_LOCK;
25808
 
      }else{
25809
 
        rc = SQLITE_IOERR_UNLOCK;
25810
 
        pFile->lastErrno = errno;
25811
 
        pInode->eFileLock = NO_LOCK;
25812
 
        pFile->eFileLock = NO_LOCK;
25813
 
      }
25814
 
    }
25815
 
 
25816
 
    /* Decrement the count of locks against this same file.  When the
25817
 
    ** count reaches zero, close any other file descriptors whose close
25818
 
    ** was deferred because of outstanding locks.
25819
 
    */
25820
 
    pInode->nLock--;
25821
 
    assert( pInode->nLock>=0 );
25822
 
    if( pInode->nLock==0 ){
25823
 
      closePendingFds(pFile);
25824
 
    }
25825
 
  }
25826
 
        
25827
 
end_unlock:
25828
 
  unixLeaveMutex();
25829
 
  if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
25830
 
  return rc;
25831
 
}
25832
 
 
25833
 
/*
25834
 
** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
25835
 
** must be either NO_LOCK or SHARED_LOCK.
25836
 
**
25837
 
** If the locking level of the file descriptor is already at or below
25838
 
** the requested locking level, this routine is a no-op.
25839
 
*/
25840
 
static int unixUnlock(sqlite3_file *id, int eFileLock){
25841
 
  return posixUnlock(id, eFileLock, 0);
25842
 
}
25843
 
 
25844
 
/*
25845
 
** This function performs the parts of the "close file" operation 
25846
 
** common to all locking schemes. It closes the directory and file
25847
 
** handles, if they are valid, and sets all fields of the unixFile
25848
 
** structure to 0.
25849
 
**
25850
 
** It is *not* necessary to hold the mutex when this routine is called,
25851
 
** even on VxWorks.  A mutex will be acquired on VxWorks by the
25852
 
** vxworksReleaseFileId() routine.
25853
 
*/
25854
 
static int closeUnixFile(sqlite3_file *id){
25855
 
  unixFile *pFile = (unixFile*)id;
25856
 
  if( pFile->dirfd>=0 ){
25857
 
    robust_close(pFile, pFile->dirfd, __LINE__);
25858
 
    pFile->dirfd=-1;
25859
 
  }
25860
 
  if( pFile->h>=0 ){
25861
 
    robust_close(pFile, pFile->h, __LINE__);
25862
 
    pFile->h = -1;
25863
 
  }
25864
 
#if OS_VXWORKS
25865
 
  if( pFile->pId ){
25866
 
    if( pFile->isDelete ){
25867
 
      unlink(pFile->pId->zCanonicalName);
25868
 
    }
25869
 
    vxworksReleaseFileId(pFile->pId);
25870
 
    pFile->pId = 0;
25871
 
  }
25872
 
#endif
25873
 
  OSTRACE(("CLOSE   %-3d\n", pFile->h));
25874
 
  OpenCounter(-1);
25875
 
  sqlite3_free(pFile->pUnused);
25876
 
  memset(pFile, 0, sizeof(unixFile));
25877
 
  return SQLITE_OK;
25878
 
}
25879
 
 
25880
 
/*
25881
 
** Close a file.
25882
 
*/
25883
 
static int unixClose(sqlite3_file *id){
25884
 
  int rc = SQLITE_OK;
25885
 
  unixFile *pFile = (unixFile *)id;
25886
 
  unixUnlock(id, NO_LOCK);
25887
 
  unixEnterMutex();
25888
 
 
25889
 
  /* unixFile.pInode is always valid here. Otherwise, a different close
25890
 
  ** routine (e.g. nolockClose()) would be called instead.
25891
 
  */
25892
 
  assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
25893
 
  if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
25894
 
    /* If there are outstanding locks, do not actually close the file just
25895
 
    ** yet because that would clear those locks.  Instead, add the file
25896
 
    ** descriptor to pInode->pUnused list.  It will be automatically closed 
25897
 
    ** when the last lock is cleared.
25898
 
    */
25899
 
    setPendingFd(pFile);
25900
 
  }
25901
 
  releaseInodeInfo(pFile);
25902
 
  rc = closeUnixFile(id);
25903
 
  unixLeaveMutex();
25904
 
  return rc;
25905
 
}
25906
 
 
25907
 
/************** End of the posix advisory lock implementation *****************
25908
 
******************************************************************************/
25909
 
 
25910
 
/******************************************************************************
25911
 
****************************** No-op Locking **********************************
25912
 
**
25913
 
** Of the various locking implementations available, this is by far the
25914
 
** simplest:  locking is ignored.  No attempt is made to lock the database
25915
 
** file for reading or writing.
25916
 
**
25917
 
** This locking mode is appropriate for use on read-only databases
25918
 
** (ex: databases that are burned into CD-ROM, for example.)  It can
25919
 
** also be used if the application employs some external mechanism to
25920
 
** prevent simultaneous access of the same database by two or more
25921
 
** database connections.  But there is a serious risk of database
25922
 
** corruption if this locking mode is used in situations where multiple
25923
 
** database connections are accessing the same database file at the same
25924
 
** time and one or more of those connections are writing.
25925
 
*/
25926
 
 
25927
 
static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
25928
 
  UNUSED_PARAMETER(NotUsed);
25929
 
  *pResOut = 0;
25930
 
  return SQLITE_OK;
25931
 
}
25932
 
static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
25933
 
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
25934
 
  return SQLITE_OK;
25935
 
}
25936
 
static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
25937
 
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
25938
 
  return SQLITE_OK;
25939
 
}
25940
 
 
25941
 
/*
25942
 
** Close the file.
25943
 
*/
25944
 
static int nolockClose(sqlite3_file *id) {
25945
 
  return closeUnixFile(id);
25946
 
}
25947
 
 
25948
 
/******************* End of the no-op lock implementation *********************
25949
 
******************************************************************************/
25950
 
 
25951
 
/******************************************************************************
25952
 
************************* Begin dot-file Locking ******************************
25953
 
**
25954
 
** The dotfile locking implementation uses the existance of separate lock
25955
 
** files in order to control access to the database.  This works on just
25956
 
** about every filesystem imaginable.  But there are serious downsides:
25957
 
**
25958
 
**    (1)  There is zero concurrency.  A single reader blocks all other
25959
 
**         connections from reading or writing the database.
25960
 
**
25961
 
**    (2)  An application crash or power loss can leave stale lock files
25962
 
**         sitting around that need to be cleared manually.
25963
 
**
25964
 
** Nevertheless, a dotlock is an appropriate locking mode for use if no
25965
 
** other locking strategy is available.
25966
 
**
25967
 
** Dotfile locking works by creating a file in the same directory as the
25968
 
** database and with the same name but with a ".lock" extension added.
25969
 
** The existance of a lock file implies an EXCLUSIVE lock.  All other lock
25970
 
** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
25971
 
*/
25972
 
 
25973
 
/*
25974
 
** The file suffix added to the data base filename in order to create the
25975
 
** lock file.
25976
 
*/
25977
 
#define DOTLOCK_SUFFIX ".lock"
25978
 
 
25979
 
/*
25980
 
** This routine checks if there is a RESERVED lock held on the specified
25981
 
** file by this or any other process. If such a lock is held, set *pResOut
25982
 
** to a non-zero value otherwise *pResOut is set to zero.  The return value
25983
 
** is set to SQLITE_OK unless an I/O error occurs during lock checking.
25984
 
**
25985
 
** In dotfile locking, either a lock exists or it does not.  So in this
25986
 
** variation of CheckReservedLock(), *pResOut is set to true if any lock
25987
 
** is held on the file and false if the file is unlocked.
25988
 
*/
25989
 
static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
25990
 
  int rc = SQLITE_OK;
25991
 
  int reserved = 0;
25992
 
  unixFile *pFile = (unixFile*)id;
25993
 
 
25994
 
  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
25995
 
  
25996
 
  assert( pFile );
25997
 
 
25998
 
  /* Check if a thread in this process holds such a lock */
25999
 
  if( pFile->eFileLock>SHARED_LOCK ){
26000
 
    /* Either this connection or some other connection in the same process
26001
 
    ** holds a lock on the file.  No need to check further. */
26002
 
    reserved = 1;
26003
 
  }else{
26004
 
    /* The lock is held if and only if the lockfile exists */
26005
 
    const char *zLockFile = (const char*)pFile->lockingContext;
26006
 
    reserved = osAccess(zLockFile, 0)==0;
26007
 
  }
26008
 
  OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
26009
 
  *pResOut = reserved;
26010
 
  return rc;
26011
 
}
26012
 
 
26013
 
/*
26014
 
** Lock the file with the lock specified by parameter eFileLock - one
26015
 
** of the following:
26016
 
**
26017
 
**     (1) SHARED_LOCK
26018
 
**     (2) RESERVED_LOCK
26019
 
**     (3) PENDING_LOCK
26020
 
**     (4) EXCLUSIVE_LOCK
26021
 
**
26022
 
** Sometimes when requesting one lock state, additional lock states
26023
 
** are inserted in between.  The locking might fail on one of the later
26024
 
** transitions leaving the lock state different from what it started but
26025
 
** still short of its goal.  The following chart shows the allowed
26026
 
** transitions and the inserted intermediate states:
26027
 
**
26028
 
**    UNLOCKED -> SHARED
26029
 
**    SHARED -> RESERVED
26030
 
**    SHARED -> (PENDING) -> EXCLUSIVE
26031
 
**    RESERVED -> (PENDING) -> EXCLUSIVE
26032
 
**    PENDING -> EXCLUSIVE
26033
 
**
26034
 
** This routine will only increase a lock.  Use the sqlite3OsUnlock()
26035
 
** routine to lower a locking level.
26036
 
**
26037
 
** With dotfile locking, we really only support state (4): EXCLUSIVE.
26038
 
** But we track the other locking levels internally.
26039
 
*/
26040
 
static int dotlockLock(sqlite3_file *id, int eFileLock) {
26041
 
  unixFile *pFile = (unixFile*)id;
26042
 
  int fd;
26043
 
  char *zLockFile = (char *)pFile->lockingContext;
26044
 
  int rc = SQLITE_OK;
26045
 
 
26046
 
 
26047
 
  /* If we have any lock, then the lock file already exists.  All we have
26048
 
  ** to do is adjust our internal record of the lock level.
26049
 
  */
26050
 
  if( pFile->eFileLock > NO_LOCK ){
26051
 
    pFile->eFileLock = eFileLock;
26052
 
#if !OS_VXWORKS
26053
 
    /* Always update the timestamp on the old file */
26054
 
    utimes(zLockFile, NULL);
26055
 
#endif
26056
 
    return SQLITE_OK;
26057
 
  }
26058
 
  
26059
 
  /* grab an exclusive lock */
26060
 
  fd = robust_open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
26061
 
  if( fd<0 ){
26062
 
    /* failed to open/create the file, someone else may have stolen the lock */
26063
 
    int tErrno = errno;
26064
 
    if( EEXIST == tErrno ){
26065
 
      rc = SQLITE_BUSY;
26066
 
    } else {
26067
 
      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
26068
 
      if( IS_LOCK_ERROR(rc) ){
26069
 
        pFile->lastErrno = tErrno;
26070
 
      }
26071
 
    }
26072
 
    return rc;
26073
 
  } 
26074
 
  robust_close(pFile, fd, __LINE__);
26075
 
  
26076
 
  /* got it, set the type and return ok */
26077
 
  pFile->eFileLock = eFileLock;
26078
 
  return rc;
26079
 
}
26080
 
 
26081
 
/*
26082
 
** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26083
 
** must be either NO_LOCK or SHARED_LOCK.
26084
 
**
26085
 
** If the locking level of the file descriptor is already at or below
26086
 
** the requested locking level, this routine is a no-op.
26087
 
**
26088
 
** When the locking level reaches NO_LOCK, delete the lock file.
26089
 
*/
26090
 
static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
26091
 
  unixFile *pFile = (unixFile*)id;
26092
 
  char *zLockFile = (char *)pFile->lockingContext;
26093
 
 
26094
 
  assert( pFile );
26095
 
  OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
26096
 
           pFile->eFileLock, getpid()));
26097
 
  assert( eFileLock<=SHARED_LOCK );
26098
 
  
26099
 
  /* no-op if possible */
26100
 
  if( pFile->eFileLock==eFileLock ){
26101
 
    return SQLITE_OK;
26102
 
  }
26103
 
 
26104
 
  /* To downgrade to shared, simply update our internal notion of the
26105
 
  ** lock state.  No need to mess with the file on disk.
26106
 
  */
26107
 
  if( eFileLock==SHARED_LOCK ){
26108
 
    pFile->eFileLock = SHARED_LOCK;
26109
 
    return SQLITE_OK;
26110
 
  }
26111
 
  
26112
 
  /* To fully unlock the database, delete the lock file */
26113
 
  assert( eFileLock==NO_LOCK );
26114
 
  if( unlink(zLockFile) ){
26115
 
    int rc = 0;
26116
 
    int tErrno = errno;
26117
 
    if( ENOENT != tErrno ){
26118
 
      rc = SQLITE_IOERR_UNLOCK;
26119
 
    }
26120
 
    if( IS_LOCK_ERROR(rc) ){
26121
 
      pFile->lastErrno = tErrno;
26122
 
    }
26123
 
    return rc; 
26124
 
  }
26125
 
  pFile->eFileLock = NO_LOCK;
26126
 
  return SQLITE_OK;
26127
 
}
26128
 
 
26129
 
/*
26130
 
** Close a file.  Make sure the lock has been released before closing.
26131
 
*/
26132
 
static int dotlockClose(sqlite3_file *id) {
26133
 
  int rc;
26134
 
  if( id ){
26135
 
    unixFile *pFile = (unixFile*)id;
26136
 
    dotlockUnlock(id, NO_LOCK);
26137
 
    sqlite3_free(pFile->lockingContext);
26138
 
  }
26139
 
  rc = closeUnixFile(id);
26140
 
  return rc;
26141
 
}
26142
 
/****************** End of the dot-file lock implementation *******************
26143
 
******************************************************************************/
26144
 
 
26145
 
/******************************************************************************
26146
 
************************** Begin flock Locking ********************************
26147
 
**
26148
 
** Use the flock() system call to do file locking.
26149
 
**
26150
 
** flock() locking is like dot-file locking in that the various
26151
 
** fine-grain locking levels supported by SQLite are collapsed into
26152
 
** a single exclusive lock.  In other words, SHARED, RESERVED, and
26153
 
** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite
26154
 
** still works when you do this, but concurrency is reduced since
26155
 
** only a single process can be reading the database at a time.
26156
 
**
26157
 
** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
26158
 
** compiling for VXWORKS.
26159
 
*/
26160
 
#if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
26161
 
 
26162
 
/*
26163
 
** Retry flock() calls that fail with EINTR
26164
 
*/
26165
 
#ifdef EINTR
26166
 
static int robust_flock(int fd, int op){
26167
 
  int rc;
26168
 
  do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
26169
 
  return rc;
26170
 
}
26171
 
#else
26172
 
# define robust_flock(a,b) flock(a,b)
26173
 
#endif
26174
 
     
26175
 
 
26176
 
/*
26177
 
** This routine checks if there is a RESERVED lock held on the specified
26178
 
** file by this or any other process. If such a lock is held, set *pResOut
26179
 
** to a non-zero value otherwise *pResOut is set to zero.  The return value
26180
 
** is set to SQLITE_OK unless an I/O error occurs during lock checking.
26181
 
*/
26182
 
static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
26183
 
  int rc = SQLITE_OK;
26184
 
  int reserved = 0;
26185
 
  unixFile *pFile = (unixFile*)id;
26186
 
  
26187
 
  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
26188
 
  
26189
 
  assert( pFile );
26190
 
  
26191
 
  /* Check if a thread in this process holds such a lock */
26192
 
  if( pFile->eFileLock>SHARED_LOCK ){
26193
 
    reserved = 1;
26194
 
  }
26195
 
  
26196
 
  /* Otherwise see if some other process holds it. */
26197
 
  if( !reserved ){
26198
 
    /* attempt to get the lock */
26199
 
    int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
26200
 
    if( !lrc ){
26201
 
      /* got the lock, unlock it */
26202
 
      lrc = robust_flock(pFile->h, LOCK_UN);
26203
 
      if ( lrc ) {
26204
 
        int tErrno = errno;
26205
 
        /* unlock failed with an error */
26206
 
        lrc = SQLITE_IOERR_UNLOCK; 
26207
 
        if( IS_LOCK_ERROR(lrc) ){
26208
 
          pFile->lastErrno = tErrno;
26209
 
          rc = lrc;
26210
 
        }
26211
 
      }
26212
 
    } else {
26213
 
      int tErrno = errno;
26214
 
      reserved = 1;
26215
 
      /* someone else might have it reserved */
26216
 
      lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); 
26217
 
      if( IS_LOCK_ERROR(lrc) ){
26218
 
        pFile->lastErrno = tErrno;
26219
 
        rc = lrc;
26220
 
      }
26221
 
    }
26222
 
  }
26223
 
  OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
26224
 
 
26225
 
#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
26226
 
  if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
26227
 
    rc = SQLITE_OK;
26228
 
    reserved=1;
26229
 
  }
26230
 
#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
26231
 
  *pResOut = reserved;
26232
 
  return rc;
26233
 
}
26234
 
 
26235
 
/*
26236
 
** Lock the file with the lock specified by parameter eFileLock - one
26237
 
** of the following:
26238
 
**
26239
 
**     (1) SHARED_LOCK
26240
 
**     (2) RESERVED_LOCK
26241
 
**     (3) PENDING_LOCK
26242
 
**     (4) EXCLUSIVE_LOCK
26243
 
**
26244
 
** Sometimes when requesting one lock state, additional lock states
26245
 
** are inserted in between.  The locking might fail on one of the later
26246
 
** transitions leaving the lock state different from what it started but
26247
 
** still short of its goal.  The following chart shows the allowed
26248
 
** transitions and the inserted intermediate states:
26249
 
**
26250
 
**    UNLOCKED -> SHARED
26251
 
**    SHARED -> RESERVED
26252
 
**    SHARED -> (PENDING) -> EXCLUSIVE
26253
 
**    RESERVED -> (PENDING) -> EXCLUSIVE
26254
 
**    PENDING -> EXCLUSIVE
26255
 
**
26256
 
** flock() only really support EXCLUSIVE locks.  We track intermediate
26257
 
** lock states in the sqlite3_file structure, but all locks SHARED or
26258
 
** above are really EXCLUSIVE locks and exclude all other processes from
26259
 
** access the file.
26260
 
**
26261
 
** This routine will only increase a lock.  Use the sqlite3OsUnlock()
26262
 
** routine to lower a locking level.
26263
 
*/
26264
 
static int flockLock(sqlite3_file *id, int eFileLock) {
26265
 
  int rc = SQLITE_OK;
26266
 
  unixFile *pFile = (unixFile*)id;
26267
 
 
26268
 
  assert( pFile );
26269
 
 
26270
 
  /* if we already have a lock, it is exclusive.  
26271
 
  ** Just adjust level and punt on outta here. */
26272
 
  if (pFile->eFileLock > NO_LOCK) {
26273
 
    pFile->eFileLock = eFileLock;
26274
 
    return SQLITE_OK;
26275
 
  }
26276
 
  
26277
 
  /* grab an exclusive lock */
26278
 
  
26279
 
  if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
26280
 
    int tErrno = errno;
26281
 
    /* didn't get, must be busy */
26282
 
    rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
26283
 
    if( IS_LOCK_ERROR(rc) ){
26284
 
      pFile->lastErrno = tErrno;
26285
 
    }
26286
 
  } else {
26287
 
    /* got it, set the type and return ok */
26288
 
    pFile->eFileLock = eFileLock;
26289
 
  }
26290
 
  OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), 
26291
 
           rc==SQLITE_OK ? "ok" : "failed"));
26292
 
#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
26293
 
  if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
26294
 
    rc = SQLITE_BUSY;
26295
 
  }
26296
 
#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
26297
 
  return rc;
26298
 
}
26299
 
 
26300
 
 
26301
 
/*
26302
 
** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26303
 
** must be either NO_LOCK or SHARED_LOCK.
26304
 
**
26305
 
** If the locking level of the file descriptor is already at or below
26306
 
** the requested locking level, this routine is a no-op.
26307
 
*/
26308
 
static int flockUnlock(sqlite3_file *id, int eFileLock) {
26309
 
  unixFile *pFile = (unixFile*)id;
26310
 
  
26311
 
  assert( pFile );
26312
 
  OSTRACE(("UNLOCK  %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
26313
 
           pFile->eFileLock, getpid()));
26314
 
  assert( eFileLock<=SHARED_LOCK );
26315
 
  
26316
 
  /* no-op if possible */
26317
 
  if( pFile->eFileLock==eFileLock ){
26318
 
    return SQLITE_OK;
26319
 
  }
26320
 
  
26321
 
  /* shared can just be set because we always have an exclusive */
26322
 
  if (eFileLock==SHARED_LOCK) {
26323
 
    pFile->eFileLock = eFileLock;
26324
 
    return SQLITE_OK;
26325
 
  }
26326
 
  
26327
 
  /* no, really, unlock. */
26328
 
  if( robust_flock(pFile->h, LOCK_UN) ){
26329
 
#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
26330
 
    return SQLITE_OK;
26331
 
#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
26332
 
    return SQLITE_IOERR_UNLOCK;
26333
 
  }else{
26334
 
    pFile->eFileLock = NO_LOCK;
26335
 
    return SQLITE_OK;
26336
 
  }
26337
 
}
26338
 
 
26339
 
/*
26340
 
** Close a file.
26341
 
*/
26342
 
static int flockClose(sqlite3_file *id) {
26343
 
  if( id ){
26344
 
    flockUnlock(id, NO_LOCK);
26345
 
  }
26346
 
  return closeUnixFile(id);
26347
 
}
26348
 
 
26349
 
#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
26350
 
 
26351
 
/******************* End of the flock lock implementation *********************
26352
 
******************************************************************************/
26353
 
 
26354
 
/******************************************************************************
26355
 
************************ Begin Named Semaphore Locking ************************
26356
 
**
26357
 
** Named semaphore locking is only supported on VxWorks.
26358
 
**
26359
 
** Semaphore locking is like dot-lock and flock in that it really only
26360
 
** supports EXCLUSIVE locking.  Only a single process can read or write
26361
 
** the database file at a time.  This reduces potential concurrency, but
26362
 
** makes the lock implementation much easier.
26363
 
*/
26364
 
#if OS_VXWORKS
26365
 
 
26366
 
/*
26367
 
** This routine checks if there is a RESERVED lock held on the specified
26368
 
** file by this or any other process. If such a lock is held, set *pResOut
26369
 
** to a non-zero value otherwise *pResOut is set to zero.  The return value
26370
 
** is set to SQLITE_OK unless an I/O error occurs during lock checking.
26371
 
*/
26372
 
static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
26373
 
  int rc = SQLITE_OK;
26374
 
  int reserved = 0;
26375
 
  unixFile *pFile = (unixFile*)id;
26376
 
 
26377
 
  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
26378
 
  
26379
 
  assert( pFile );
26380
 
 
26381
 
  /* Check if a thread in this process holds such a lock */
26382
 
  if( pFile->eFileLock>SHARED_LOCK ){
26383
 
    reserved = 1;
26384
 
  }
26385
 
  
26386
 
  /* Otherwise see if some other process holds it. */
26387
 
  if( !reserved ){
26388
 
    sem_t *pSem = pFile->pInode->pSem;
26389
 
    struct stat statBuf;
26390
 
 
26391
 
    if( sem_trywait(pSem)==-1 ){
26392
 
      int tErrno = errno;
26393
 
      if( EAGAIN != tErrno ){
26394
 
        rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
26395
 
        pFile->lastErrno = tErrno;
26396
 
      } else {
26397
 
        /* someone else has the lock when we are in NO_LOCK */
26398
 
        reserved = (pFile->eFileLock < SHARED_LOCK);
26399
 
      }
26400
 
    }else{
26401
 
      /* we could have it if we want it */
26402
 
      sem_post(pSem);
26403
 
    }
26404
 
  }
26405
 
  OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
26406
 
 
26407
 
  *pResOut = reserved;
26408
 
  return rc;
26409
 
}
26410
 
 
26411
 
/*
26412
 
** Lock the file with the lock specified by parameter eFileLock - one
26413
 
** of the following:
26414
 
**
26415
 
**     (1) SHARED_LOCK
26416
 
**     (2) RESERVED_LOCK
26417
 
**     (3) PENDING_LOCK
26418
 
**     (4) EXCLUSIVE_LOCK
26419
 
**
26420
 
** Sometimes when requesting one lock state, additional lock states
26421
 
** are inserted in between.  The locking might fail on one of the later
26422
 
** transitions leaving the lock state different from what it started but
26423
 
** still short of its goal.  The following chart shows the allowed
26424
 
** transitions and the inserted intermediate states:
26425
 
**
26426
 
**    UNLOCKED -> SHARED
26427
 
**    SHARED -> RESERVED
26428
 
**    SHARED -> (PENDING) -> EXCLUSIVE
26429
 
**    RESERVED -> (PENDING) -> EXCLUSIVE
26430
 
**    PENDING -> EXCLUSIVE
26431
 
**
26432
 
** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate
26433
 
** lock states in the sqlite3_file structure, but all locks SHARED or
26434
 
** above are really EXCLUSIVE locks and exclude all other processes from
26435
 
** access the file.
26436
 
**
26437
 
** This routine will only increase a lock.  Use the sqlite3OsUnlock()
26438
 
** routine to lower a locking level.
26439
 
*/
26440
 
static int semLock(sqlite3_file *id, int eFileLock) {
26441
 
  unixFile *pFile = (unixFile*)id;
26442
 
  int fd;
26443
 
  sem_t *pSem = pFile->pInode->pSem;
26444
 
  int rc = SQLITE_OK;
26445
 
 
26446
 
  /* if we already have a lock, it is exclusive.  
26447
 
  ** Just adjust level and punt on outta here. */
26448
 
  if (pFile->eFileLock > NO_LOCK) {
26449
 
    pFile->eFileLock = eFileLock;
26450
 
    rc = SQLITE_OK;
26451
 
    goto sem_end_lock;
26452
 
  }
26453
 
  
26454
 
  /* lock semaphore now but bail out when already locked. */
26455
 
  if( sem_trywait(pSem)==-1 ){
26456
 
    rc = SQLITE_BUSY;
26457
 
    goto sem_end_lock;
26458
 
  }
26459
 
 
26460
 
  /* got it, set the type and return ok */
26461
 
  pFile->eFileLock = eFileLock;
26462
 
 
26463
 
 sem_end_lock:
26464
 
  return rc;
26465
 
}
26466
 
 
26467
 
/*
26468
 
** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26469
 
** must be either NO_LOCK or SHARED_LOCK.
26470
 
**
26471
 
** If the locking level of the file descriptor is already at or below
26472
 
** the requested locking level, this routine is a no-op.
26473
 
*/
26474
 
static int semUnlock(sqlite3_file *id, int eFileLock) {
26475
 
  unixFile *pFile = (unixFile*)id;
26476
 
  sem_t *pSem = pFile->pInode->pSem;
26477
 
 
26478
 
  assert( pFile );
26479
 
  assert( pSem );
26480
 
  OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
26481
 
           pFile->eFileLock, getpid()));
26482
 
  assert( eFileLock<=SHARED_LOCK );
26483
 
  
26484
 
  /* no-op if possible */
26485
 
  if( pFile->eFileLock==eFileLock ){
26486
 
    return SQLITE_OK;
26487
 
  }
26488
 
  
26489
 
  /* shared can just be set because we always have an exclusive */
26490
 
  if (eFileLock==SHARED_LOCK) {
26491
 
    pFile->eFileLock = eFileLock;
26492
 
    return SQLITE_OK;
26493
 
  }
26494
 
  
26495
 
  /* no, really unlock. */
26496
 
  if ( sem_post(pSem)==-1 ) {
26497
 
    int rc, tErrno = errno;
26498
 
    rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
26499
 
    if( IS_LOCK_ERROR(rc) ){
26500
 
      pFile->lastErrno = tErrno;
26501
 
    }
26502
 
    return rc; 
26503
 
  }
26504
 
  pFile->eFileLock = NO_LOCK;
26505
 
  return SQLITE_OK;
26506
 
}
26507
 
 
26508
 
/*
26509
 
 ** Close a file.
26510
 
 */
26511
 
static int semClose(sqlite3_file *id) {
26512
 
  if( id ){
26513
 
    unixFile *pFile = (unixFile*)id;
26514
 
    semUnlock(id, NO_LOCK);
26515
 
    assert( pFile );
26516
 
    unixEnterMutex();
26517
 
    releaseInodeInfo(pFile);
26518
 
    unixLeaveMutex();
26519
 
    closeUnixFile(id);
26520
 
  }
26521
 
  return SQLITE_OK;
26522
 
}
26523
 
 
26524
 
#endif /* OS_VXWORKS */
26525
 
/*
26526
 
** Named semaphore locking is only available on VxWorks.
26527
 
**
26528
 
*************** End of the named semaphore lock implementation ****************
26529
 
******************************************************************************/
26530
 
 
26531
 
 
26532
 
/******************************************************************************
26533
 
*************************** Begin AFP Locking *********************************
26534
 
**
26535
 
** AFP is the Apple Filing Protocol.  AFP is a network filesystem found
26536
 
** on Apple Macintosh computers - both OS9 and OSX.
26537
 
**
26538
 
** Third-party implementations of AFP are available.  But this code here
26539
 
** only works on OSX.
26540
 
*/
26541
 
 
26542
 
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
26543
 
/*
26544
 
** The afpLockingContext structure contains all afp lock specific state
26545
 
*/
26546
 
typedef struct afpLockingContext afpLockingContext;
26547
 
struct afpLockingContext {
26548
 
  int reserved;
26549
 
  const char *dbPath;             /* Name of the open file */
26550
 
};
26551
 
 
26552
 
struct ByteRangeLockPB2
26553
 
{
26554
 
  unsigned long long offset;        /* offset to first byte to lock */
26555
 
  unsigned long long length;        /* nbr of bytes to lock */
26556
 
  unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
26557
 
  unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
26558
 
  unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
26559
 
  int fd;                           /* file desc to assoc this lock with */
26560
 
};
26561
 
 
26562
 
#define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
26563
 
 
26564
 
/*
26565
 
** This is a utility for setting or clearing a bit-range lock on an
26566
 
** AFP filesystem.
26567
 
** 
26568
 
** Return SQLITE_OK on success, SQLITE_BUSY on failure.
26569
 
*/
26570
 
static int afpSetLock(
26571
 
  const char *path,              /* Name of the file to be locked or unlocked */
26572
 
  unixFile *pFile,               /* Open file descriptor on path */
26573
 
  unsigned long long offset,     /* First byte to be locked */
26574
 
  unsigned long long length,     /* Number of bytes to lock */
26575
 
  int setLockFlag                /* True to set lock.  False to clear lock */
26576
 
){
26577
 
  struct ByteRangeLockPB2 pb;
26578
 
  int err;
26579
 
  
26580
 
  pb.unLockFlag = setLockFlag ? 0 : 1;
26581
 
  pb.startEndFlag = 0;
26582
 
  pb.offset = offset;
26583
 
  pb.length = length; 
26584
 
  pb.fd = pFile->h;
26585
 
  
26586
 
  OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", 
26587
 
    (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
26588
 
    offset, length));
26589
 
  err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
26590
 
  if ( err==-1 ) {
26591
 
    int rc;
26592
 
    int tErrno = errno;
26593
 
    OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
26594
 
             path, tErrno, strerror(tErrno)));
26595
 
#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
26596
 
    rc = SQLITE_BUSY;
26597
 
#else
26598
 
    rc = sqliteErrorFromPosixError(tErrno,
26599
 
                    setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
26600
 
#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
26601
 
    if( IS_LOCK_ERROR(rc) ){
26602
 
      pFile->lastErrno = tErrno;
26603
 
    }
26604
 
    return rc;
26605
 
  } else {
26606
 
    return SQLITE_OK;
26607
 
  }
26608
 
}
26609
 
 
26610
 
/*
26611
 
** This routine checks if there is a RESERVED lock held on the specified
26612
 
** file by this or any other process. If such a lock is held, set *pResOut
26613
 
** to a non-zero value otherwise *pResOut is set to zero.  The return value
26614
 
** is set to SQLITE_OK unless an I/O error occurs during lock checking.
26615
 
*/
26616
 
static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
26617
 
  int rc = SQLITE_OK;
26618
 
  int reserved = 0;
26619
 
  unixFile *pFile = (unixFile*)id;
26620
 
  
26621
 
  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
26622
 
  
26623
 
  assert( pFile );
26624
 
  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
26625
 
  if( context->reserved ){
26626
 
    *pResOut = 1;
26627
 
    return SQLITE_OK;
26628
 
  }
26629
 
  unixEnterMutex(); /* Because pFile->pInode is shared across threads */
26630
 
  
26631
 
  /* Check if a thread in this process holds such a lock */
26632
 
  if( pFile->pInode->eFileLock>SHARED_LOCK ){
26633
 
    reserved = 1;
26634
 
  }
26635
 
  
26636
 
  /* Otherwise see if some other process holds it.
26637
 
   */
26638
 
  if( !reserved ){
26639
 
    /* lock the RESERVED byte */
26640
 
    int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);  
26641
 
    if( SQLITE_OK==lrc ){
26642
 
      /* if we succeeded in taking the reserved lock, unlock it to restore
26643
 
      ** the original state */
26644
 
      lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
26645
 
    } else {
26646
 
      /* if we failed to get the lock then someone else must have it */
26647
 
      reserved = 1;
26648
 
    }
26649
 
    if( IS_LOCK_ERROR(lrc) ){
26650
 
      rc=lrc;
26651
 
    }
26652
 
  }
26653
 
  
26654
 
  unixLeaveMutex();
26655
 
  OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
26656
 
  
26657
 
  *pResOut = reserved;
26658
 
  return rc;
26659
 
}
26660
 
 
26661
 
/*
26662
 
** Lock the file with the lock specified by parameter eFileLock - one
26663
 
** of the following:
26664
 
**
26665
 
**     (1) SHARED_LOCK
26666
 
**     (2) RESERVED_LOCK
26667
 
**     (3) PENDING_LOCK
26668
 
**     (4) EXCLUSIVE_LOCK
26669
 
**
26670
 
** Sometimes when requesting one lock state, additional lock states
26671
 
** are inserted in between.  The locking might fail on one of the later
26672
 
** transitions leaving the lock state different from what it started but
26673
 
** still short of its goal.  The following chart shows the allowed
26674
 
** transitions and the inserted intermediate states:
26675
 
**
26676
 
**    UNLOCKED -> SHARED
26677
 
**    SHARED -> RESERVED
26678
 
**    SHARED -> (PENDING) -> EXCLUSIVE
26679
 
**    RESERVED -> (PENDING) -> EXCLUSIVE
26680
 
**    PENDING -> EXCLUSIVE
26681
 
**
26682
 
** This routine will only increase a lock.  Use the sqlite3OsUnlock()
26683
 
** routine to lower a locking level.
26684
 
*/
26685
 
static int afpLock(sqlite3_file *id, int eFileLock){
26686
 
  int rc = SQLITE_OK;
26687
 
  unixFile *pFile = (unixFile*)id;
26688
 
  unixInodeInfo *pInode = pFile->pInode;
26689
 
  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
26690
 
  
26691
 
  assert( pFile );
26692
 
  OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
26693
 
           azFileLock(eFileLock), azFileLock(pFile->eFileLock),
26694
 
           azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
26695
 
 
26696
 
  /* If there is already a lock of this type or more restrictive on the
26697
 
  ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
26698
 
  ** unixEnterMutex() hasn't been called yet.
26699
 
  */
26700
 
  if( pFile->eFileLock>=eFileLock ){
26701
 
    OSTRACE(("LOCK    %d %s ok (already held) (afp)\n", pFile->h,
26702
 
           azFileLock(eFileLock)));
26703
 
    return SQLITE_OK;
26704
 
  }
26705
 
 
26706
 
  /* Make sure the locking sequence is correct
26707
 
  **  (1) We never move from unlocked to anything higher than shared lock.
26708
 
  **  (2) SQLite never explicitly requests a pendig lock.
26709
 
  **  (3) A shared lock is always held when a reserve lock is requested.
26710
 
  */
26711
 
  assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
26712
 
  assert( eFileLock!=PENDING_LOCK );
26713
 
  assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
26714
 
  
26715
 
  /* This mutex is needed because pFile->pInode is shared across threads
26716
 
  */
26717
 
  unixEnterMutex();
26718
 
  pInode = pFile->pInode;
26719
 
 
26720
 
  /* If some thread using this PID has a lock via a different unixFile*
26721
 
  ** handle that precludes the requested lock, return BUSY.
26722
 
  */
26723
 
  if( (pFile->eFileLock!=pInode->eFileLock && 
26724
 
       (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
26725
 
     ){
26726
 
    rc = SQLITE_BUSY;
26727
 
    goto afp_end_lock;
26728
 
  }
26729
 
  
26730
 
  /* If a SHARED lock is requested, and some thread using this PID already
26731
 
  ** has a SHARED or RESERVED lock, then increment reference counts and
26732
 
  ** return SQLITE_OK.
26733
 
  */
26734
 
  if( eFileLock==SHARED_LOCK && 
26735
 
     (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
26736
 
    assert( eFileLock==SHARED_LOCK );
26737
 
    assert( pFile->eFileLock==0 );
26738
 
    assert( pInode->nShared>0 );
26739
 
    pFile->eFileLock = SHARED_LOCK;
26740
 
    pInode->nShared++;
26741
 
    pInode->nLock++;
26742
 
    goto afp_end_lock;
26743
 
  }
26744
 
    
26745
 
  /* A PENDING lock is needed before acquiring a SHARED lock and before
26746
 
  ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
26747
 
  ** be released.
26748
 
  */
26749
 
  if( eFileLock==SHARED_LOCK 
26750
 
      || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
26751
 
  ){
26752
 
    int failed;
26753
 
    failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
26754
 
    if (failed) {
26755
 
      rc = failed;
26756
 
      goto afp_end_lock;
26757
 
    }
26758
 
  }
26759
 
  
26760
 
  /* If control gets to this point, then actually go ahead and make
26761
 
  ** operating system calls for the specified lock.
26762
 
  */
26763
 
  if( eFileLock==SHARED_LOCK ){
26764
 
    int lrc1, lrc2, lrc1Errno;
26765
 
    long lk, mask;
26766
 
    
26767
 
    assert( pInode->nShared==0 );
26768
 
    assert( pInode->eFileLock==0 );
26769
 
        
26770
 
    mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
26771
 
    /* Now get the read-lock SHARED_LOCK */
26772
 
    /* note that the quality of the randomness doesn't matter that much */
26773
 
    lk = random(); 
26774
 
    pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
26775
 
    lrc1 = afpSetLock(context->dbPath, pFile, 
26776
 
          SHARED_FIRST+pInode->sharedByte, 1, 1);
26777
 
    if( IS_LOCK_ERROR(lrc1) ){
26778
 
      lrc1Errno = pFile->lastErrno;
26779
 
    }
26780
 
    /* Drop the temporary PENDING lock */
26781
 
    lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
26782
 
    
26783
 
    if( IS_LOCK_ERROR(lrc1) ) {
26784
 
      pFile->lastErrno = lrc1Errno;
26785
 
      rc = lrc1;
26786
 
      goto afp_end_lock;
26787
 
    } else if( IS_LOCK_ERROR(lrc2) ){
26788
 
      rc = lrc2;
26789
 
      goto afp_end_lock;
26790
 
    } else if( lrc1 != SQLITE_OK ) {
26791
 
      rc = lrc1;
26792
 
    } else {
26793
 
      pFile->eFileLock = SHARED_LOCK;
26794
 
      pInode->nLock++;
26795
 
      pInode->nShared = 1;
26796
 
    }
26797
 
  }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
26798
 
    /* We are trying for an exclusive lock but another thread in this
26799
 
     ** same process is still holding a shared lock. */
26800
 
    rc = SQLITE_BUSY;
26801
 
  }else{
26802
 
    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
26803
 
    ** assumed that there is a SHARED or greater lock on the file
26804
 
    ** already.
26805
 
    */
26806
 
    int failed = 0;
26807
 
    assert( 0!=pFile->eFileLock );
26808
 
    if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
26809
 
        /* Acquire a RESERVED lock */
26810
 
        failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
26811
 
      if( !failed ){
26812
 
        context->reserved = 1;
26813
 
      }
26814
 
    }
26815
 
    if (!failed && eFileLock == EXCLUSIVE_LOCK) {
26816
 
      /* Acquire an EXCLUSIVE lock */
26817
 
        
26818
 
      /* Remove the shared lock before trying the range.  we'll need to 
26819
 
      ** reestablish the shared lock if we can't get the  afpUnlock
26820
 
      */
26821
 
      if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
26822
 
                         pInode->sharedByte, 1, 0)) ){
26823
 
        int failed2 = SQLITE_OK;
26824
 
        /* now attemmpt to get the exclusive lock range */
26825
 
        failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, 
26826
 
                               SHARED_SIZE, 1);
26827
 
        if( failed && (failed2 = afpSetLock(context->dbPath, pFile, 
26828
 
                       SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
26829
 
          /* Can't reestablish the shared lock.  Sqlite can't deal, this is
26830
 
          ** a critical I/O error
26831
 
          */
26832
 
          rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : 
26833
 
               SQLITE_IOERR_LOCK;
26834
 
          goto afp_end_lock;
26835
 
        } 
26836
 
      }else{
26837
 
        rc = failed; 
26838
 
      }
26839
 
    }
26840
 
    if( failed ){
26841
 
      rc = failed;
26842
 
    }
26843
 
  }
26844
 
  
26845
 
  if( rc==SQLITE_OK ){
26846
 
    pFile->eFileLock = eFileLock;
26847
 
    pInode->eFileLock = eFileLock;
26848
 
  }else if( eFileLock==EXCLUSIVE_LOCK ){
26849
 
    pFile->eFileLock = PENDING_LOCK;
26850
 
    pInode->eFileLock = PENDING_LOCK;
26851
 
  }
26852
 
  
26853
 
afp_end_lock:
26854
 
  unixLeaveMutex();
26855
 
  OSTRACE(("LOCK    %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), 
26856
 
         rc==SQLITE_OK ? "ok" : "failed"));
26857
 
  return rc;
26858
 
}
26859
 
 
26860
 
/*
26861
 
** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26862
 
** must be either NO_LOCK or SHARED_LOCK.
26863
 
**
26864
 
** If the locking level of the file descriptor is already at or below
26865
 
** the requested locking level, this routine is a no-op.
26866
 
*/
26867
 
static int afpUnlock(sqlite3_file *id, int eFileLock) {
26868
 
  int rc = SQLITE_OK;
26869
 
  unixFile *pFile = (unixFile*)id;
26870
 
  unixInodeInfo *pInode;
26871
 
  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
26872
 
  int skipShared = 0;
26873
 
#ifdef SQLITE_TEST
26874
 
  int h = pFile->h;
26875
 
#endif
26876
 
 
26877
 
  assert( pFile );
26878
 
  OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
26879
 
           pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
26880
 
           getpid()));
26881
 
 
26882
 
  assert( eFileLock<=SHARED_LOCK );
26883
 
  if( pFile->eFileLock<=eFileLock ){
26884
 
    return SQLITE_OK;
26885
 
  }
26886
 
  unixEnterMutex();
26887
 
  pInode = pFile->pInode;
26888
 
  assert( pInode->nShared!=0 );
26889
 
  if( pFile->eFileLock>SHARED_LOCK ){
26890
 
    assert( pInode->eFileLock==pFile->eFileLock );
26891
 
    SimulateIOErrorBenign(1);
26892
 
    SimulateIOError( h=(-1) )
26893
 
    SimulateIOErrorBenign(0);
26894
 
    
26895
 
#ifndef NDEBUG
26896
 
    /* When reducing a lock such that other processes can start
26897
 
    ** reading the database file again, make sure that the
26898
 
    ** transaction counter was updated if any part of the database
26899
 
    ** file changed.  If the transaction counter is not updated,
26900
 
    ** other connections to the same file might not realize that
26901
 
    ** the file has changed and hence might not know to flush their
26902
 
    ** cache.  The use of a stale cache can lead to database corruption.
26903
 
    */
26904
 
    assert( pFile->inNormalWrite==0
26905
 
           || pFile->dbUpdate==0
26906
 
           || pFile->transCntrChng==1 );
26907
 
    pFile->inNormalWrite = 0;
26908
 
#endif
26909
 
    
26910
 
    if( pFile->eFileLock==EXCLUSIVE_LOCK ){
26911
 
      rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
26912
 
      if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
26913
 
        /* only re-establish the shared lock if necessary */
26914
 
        int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
26915
 
        rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
26916
 
      } else {
26917
 
        skipShared = 1;
26918
 
      }
26919
 
    }
26920
 
    if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
26921
 
      rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
26922
 
    } 
26923
 
    if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
26924
 
      rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
26925
 
      if( !rc ){ 
26926
 
        context->reserved = 0; 
26927
 
      }
26928
 
    }
26929
 
    if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
26930
 
      pInode->eFileLock = SHARED_LOCK;
26931
 
    }
26932
 
  }
26933
 
  if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
26934
 
 
26935
 
    /* Decrement the shared lock counter.  Release the lock using an
26936
 
    ** OS call only when all threads in this same process have released
26937
 
    ** the lock.
26938
 
    */
26939
 
    unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
26940
 
    pInode->nShared--;
26941
 
    if( pInode->nShared==0 ){
26942
 
      SimulateIOErrorBenign(1);
26943
 
      SimulateIOError( h=(-1) )
26944
 
      SimulateIOErrorBenign(0);
26945
 
      if( !skipShared ){
26946
 
        rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
26947
 
      }
26948
 
      if( !rc ){
26949
 
        pInode->eFileLock = NO_LOCK;
26950
 
        pFile->eFileLock = NO_LOCK;
26951
 
      }
26952
 
    }
26953
 
    if( rc==SQLITE_OK ){
26954
 
      pInode->nLock--;
26955
 
      assert( pInode->nLock>=0 );
26956
 
      if( pInode->nLock==0 ){
26957
 
        closePendingFds(pFile);
26958
 
      }
26959
 
    }
26960
 
  }
26961
 
  
26962
 
  unixLeaveMutex();
26963
 
  if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
26964
 
  return rc;
26965
 
}
26966
 
 
26967
 
/*
26968
 
** Close a file & cleanup AFP specific locking context 
26969
 
*/
26970
 
static int afpClose(sqlite3_file *id) {
26971
 
  int rc = SQLITE_OK;
26972
 
  if( id ){
26973
 
    unixFile *pFile = (unixFile*)id;
26974
 
    afpUnlock(id, NO_LOCK);
26975
 
    unixEnterMutex();
26976
 
    if( pFile->pInode && pFile->pInode->nLock ){
26977
 
      /* If there are outstanding locks, do not actually close the file just
26978
 
      ** yet because that would clear those locks.  Instead, add the file
26979
 
      ** descriptor to pInode->aPending.  It will be automatically closed when
26980
 
      ** the last lock is cleared.
26981
 
      */
26982
 
      setPendingFd(pFile);
26983
 
    }
26984
 
    releaseInodeInfo(pFile);
26985
 
    sqlite3_free(pFile->lockingContext);
26986
 
    rc = closeUnixFile(id);
26987
 
    unixLeaveMutex();
26988
 
  }
26989
 
  return rc;
26990
 
}
26991
 
 
26992
 
#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
26993
 
/*
26994
 
** The code above is the AFP lock implementation.  The code is specific
26995
 
** to MacOSX and does not work on other unix platforms.  No alternative
26996
 
** is available.  If you don't compile for a mac, then the "unix-afp"
26997
 
** VFS is not available.
26998
 
**
26999
 
********************* End of the AFP lock implementation **********************
27000
 
******************************************************************************/
27001
 
 
27002
 
/******************************************************************************
27003
 
*************************** Begin NFS Locking ********************************/
27004
 
 
27005
 
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
27006
 
/*
27007
 
 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
27008
 
 ** must be either NO_LOCK or SHARED_LOCK.
27009
 
 **
27010
 
 ** If the locking level of the file descriptor is already at or below
27011
 
 ** the requested locking level, this routine is a no-op.
27012
 
 */
27013
 
static int nfsUnlock(sqlite3_file *id, int eFileLock){
27014
 
  return posixUnlock(id, eFileLock, 1);
27015
 
}
27016
 
 
27017
 
#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
27018
 
/*
27019
 
** The code above is the NFS lock implementation.  The code is specific
27020
 
** to MacOSX and does not work on other unix platforms.  No alternative
27021
 
** is available.  
27022
 
**
27023
 
********************* End of the NFS lock implementation **********************
27024
 
******************************************************************************/
27025
 
 
27026
 
/******************************************************************************
27027
 
**************** Non-locking sqlite3_file methods *****************************
27028
 
**
27029
 
** The next division contains implementations for all methods of the 
27030
 
** sqlite3_file object other than the locking methods.  The locking
27031
 
** methods were defined in divisions above (one locking method per
27032
 
** division).  Those methods that are common to all locking modes
27033
 
** are gather together into this division.
27034
 
*/
27035
 
 
27036
 
/*
27037
 
** Seek to the offset passed as the second argument, then read cnt 
27038
 
** bytes into pBuf. Return the number of bytes actually read.
27039
 
**
27040
 
** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
27041
 
** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
27042
 
** one system to another.  Since SQLite does not define USE_PREAD
27043
 
** any any form by default, we will not attempt to define _XOPEN_SOURCE.
27044
 
** See tickets #2741 and #2681.
27045
 
**
27046
 
** To avoid stomping the errno value on a failed read the lastErrno value
27047
 
** is set before returning.
27048
 
*/
27049
 
static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
27050
 
  int got;
27051
 
#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
27052
 
  i64 newOffset;
27053
 
#endif
27054
 
  TIMER_START;
27055
 
#if defined(USE_PREAD)
27056
 
  do{ got = osPread(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
27057
 
  SimulateIOError( got = -1 );
27058
 
#elif defined(USE_PREAD64)
27059
 
  do{ got = osPread64(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR);
27060
 
  SimulateIOError( got = -1 );
27061
 
#else
27062
 
  newOffset = lseek(id->h, offset, SEEK_SET);
27063
 
  SimulateIOError( newOffset-- );
27064
 
  if( newOffset!=offset ){
27065
 
    if( newOffset == -1 ){
27066
 
      ((unixFile*)id)->lastErrno = errno;
27067
 
    }else{
27068
 
      ((unixFile*)id)->lastErrno = 0;                   
27069
 
    }
27070
 
    return -1;
27071
 
  }
27072
 
  do{ got = osRead(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
27073
 
#endif
27074
 
  TIMER_END;
27075
 
  if( got<0 ){
27076
 
    ((unixFile*)id)->lastErrno = errno;
27077
 
  }
27078
 
  OSTRACE(("READ    %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
27079
 
  return got;
27080
 
}
27081
 
 
27082
 
/*
27083
 
** Read data from a file into a buffer.  Return SQLITE_OK if all
27084
 
** bytes were read successfully and SQLITE_IOERR if anything goes
27085
 
** wrong.
27086
 
*/
27087
 
static int unixRead(
27088
 
  sqlite3_file *id, 
27089
 
  void *pBuf, 
27090
 
  int amt,
27091
 
  sqlite3_int64 offset
27092
 
){
27093
 
  unixFile *pFile = (unixFile *)id;
27094
 
  int got;
27095
 
  assert( id );
27096
 
 
27097
 
  /* If this is a database file (not a journal, master-journal or temp
27098
 
  ** file), the bytes in the locking range should never be read or written. */
27099
 
#if 0
27100
 
  assert( pFile->pUnused==0
27101
 
       || offset>=PENDING_BYTE+512
27102
 
       || offset+amt<=PENDING_BYTE 
27103
 
  );
27104
 
#endif
27105
 
 
27106
 
  got = seekAndRead(pFile, offset, pBuf, amt);
27107
 
  if( got==amt ){
27108
 
    return SQLITE_OK;
27109
 
  }else if( got<0 ){
27110
 
    /* lastErrno set by seekAndRead */
27111
 
    return SQLITE_IOERR_READ;
27112
 
  }else{
27113
 
    pFile->lastErrno = 0; /* not a system error */
27114
 
    /* Unread parts of the buffer must be zero-filled */
27115
 
    memset(&((char*)pBuf)[got], 0, amt-got);
27116
 
    return SQLITE_IOERR_SHORT_READ;
27117
 
  }
27118
 
}
27119
 
 
27120
 
/*
27121
 
** Seek to the offset in id->offset then read cnt bytes into pBuf.
27122
 
** Return the number of bytes actually read.  Update the offset.
27123
 
**
27124
 
** To avoid stomping the errno value on a failed write the lastErrno value
27125
 
** is set before returning.
27126
 
*/
27127
 
static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
27128
 
  int got;
27129
 
#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
27130
 
  i64 newOffset;
27131
 
#endif
27132
 
  TIMER_START;
27133
 
#if defined(USE_PREAD)
27134
 
  do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
27135
 
#elif defined(USE_PREAD64)
27136
 
  do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
27137
 
#else
27138
 
  newOffset = lseek(id->h, offset, SEEK_SET);
27139
 
  SimulateIOError( newOffset-- );
27140
 
  if( newOffset!=offset ){
27141
 
    if( newOffset == -1 ){
27142
 
      ((unixFile*)id)->lastErrno = errno;
27143
 
    }else{
27144
 
      ((unixFile*)id)->lastErrno = 0;                   
27145
 
    }
27146
 
    return -1;
27147
 
  }
27148
 
  do{ got = osWrite(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
27149
 
#endif
27150
 
  TIMER_END;
27151
 
  if( got<0 ){
27152
 
    ((unixFile*)id)->lastErrno = errno;
27153
 
  }
27154
 
 
27155
 
  OSTRACE(("WRITE   %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
27156
 
  return got;
27157
 
}
27158
 
 
27159
 
 
27160
 
/*
27161
 
** Write data from a buffer into a file.  Return SQLITE_OK on success
27162
 
** or some other error code on failure.
27163
 
*/
27164
 
static int unixWrite(
27165
 
  sqlite3_file *id, 
27166
 
  const void *pBuf, 
27167
 
  int amt,
27168
 
  sqlite3_int64 offset 
27169
 
){
27170
 
  unixFile *pFile = (unixFile*)id;
27171
 
  int wrote = 0;
27172
 
  assert( id );
27173
 
  assert( amt>0 );
27174
 
 
27175
 
  /* If this is a database file (not a journal, master-journal or temp
27176
 
  ** file), the bytes in the locking range should never be read or written. */
27177
 
#if 0
27178
 
  assert( pFile->pUnused==0
27179
 
       || offset>=PENDING_BYTE+512
27180
 
       || offset+amt<=PENDING_BYTE 
27181
 
  );
27182
 
#endif
27183
 
 
27184
 
#ifndef NDEBUG
27185
 
  /* If we are doing a normal write to a database file (as opposed to
27186
 
  ** doing a hot-journal rollback or a write to some file other than a
27187
 
  ** normal database file) then record the fact that the database
27188
 
  ** has changed.  If the transaction counter is modified, record that
27189
 
  ** fact too.
27190
 
  */
27191
 
  if( pFile->inNormalWrite ){
27192
 
    pFile->dbUpdate = 1;  /* The database has been modified */
27193
 
    if( offset<=24 && offset+amt>=27 ){
27194
 
      int rc;
27195
 
      char oldCntr[4];
27196
 
      SimulateIOErrorBenign(1);
27197
 
      rc = seekAndRead(pFile, 24, oldCntr, 4);
27198
 
      SimulateIOErrorBenign(0);
27199
 
      if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
27200
 
        pFile->transCntrChng = 1;  /* The transaction counter has changed */
27201
 
      }
27202
 
    }
27203
 
  }
27204
 
#endif
27205
 
 
27206
 
  while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
27207
 
    amt -= wrote;
27208
 
    offset += wrote;
27209
 
    pBuf = &((char*)pBuf)[wrote];
27210
 
  }
27211
 
  SimulateIOError(( wrote=(-1), amt=1 ));
27212
 
  SimulateDiskfullError(( wrote=0, amt=1 ));
27213
 
 
27214
 
  if( amt>0 ){
27215
 
    if( wrote<0 ){
27216
 
      /* lastErrno set by seekAndWrite */
27217
 
      return SQLITE_IOERR_WRITE;
27218
 
    }else{
27219
 
      pFile->lastErrno = 0; /* not a system error */
27220
 
      return SQLITE_FULL;
27221
 
    }
27222
 
  }
27223
 
 
27224
 
  return SQLITE_OK;
27225
 
}
27226
 
 
27227
 
#ifdef SQLITE_TEST
27228
 
/*
27229
 
** Count the number of fullsyncs and normal syncs.  This is used to test
27230
 
** that syncs and fullsyncs are occurring at the right times.
27231
 
*/
27232
 
SQLITE_API int sqlite3_sync_count = 0;
27233
 
SQLITE_API int sqlite3_fullsync_count = 0;
27234
 
#endif
27235
 
 
27236
 
/*
27237
 
** We do not trust systems to provide a working fdatasync().  Some do.
27238
 
** Others do no.  To be safe, we will stick with the (slower) fsync().
27239
 
** If you know that your system does support fdatasync() correctly,
27240
 
** then simply compile with -Dfdatasync=fdatasync
27241
 
*/
27242
 
#if !defined(fdatasync) && !defined(__linux__)
27243
 
# define fdatasync fsync
27244
 
#endif
27245
 
 
27246
 
/*
27247
 
** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
27248
 
** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
27249
 
** only available on Mac OS X.  But that could change.
27250
 
*/
27251
 
#ifdef F_FULLFSYNC
27252
 
# define HAVE_FULLFSYNC 1
27253
 
#else
27254
 
# define HAVE_FULLFSYNC 0
27255
 
#endif
27256
 
 
27257
 
 
27258
 
/*
27259
 
** The fsync() system call does not work as advertised on many
27260
 
** unix systems.  The following procedure is an attempt to make
27261
 
** it work better.
27262
 
**
27263
 
** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
27264
 
** for testing when we want to run through the test suite quickly.
27265
 
** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
27266
 
** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
27267
 
** or power failure will likely corrupt the database file.
27268
 
**
27269
 
** SQLite sets the dataOnly flag if the size of the file is unchanged.
27270
 
** The idea behind dataOnly is that it should only write the file content
27271
 
** to disk, not the inode.  We only set dataOnly if the file size is 
27272
 
** unchanged since the file size is part of the inode.  However, 
27273
 
** Ted Ts'o tells us that fdatasync() will also write the inode if the
27274
 
** file size has changed.  The only real difference between fdatasync()
27275
 
** and fsync(), Ted tells us, is that fdatasync() will not flush the
27276
 
** inode if the mtime or owner or other inode attributes have changed.
27277
 
** We only care about the file size, not the other file attributes, so
27278
 
** as far as SQLite is concerned, an fdatasync() is always adequate.
27279
 
** So, we always use fdatasync() if it is available, regardless of
27280
 
** the value of the dataOnly flag.
27281
 
*/
27282
 
static int full_fsync(int fd, int fullSync, int dataOnly){
27283
 
  int rc;
27284
 
 
27285
 
  /* The following "ifdef/elif/else/" block has the same structure as
27286
 
  ** the one below. It is replicated here solely to avoid cluttering 
27287
 
  ** up the real code with the UNUSED_PARAMETER() macros.
27288
 
  */
27289
 
#ifdef SQLITE_NO_SYNC
27290
 
  UNUSED_PARAMETER(fd);
27291
 
  UNUSED_PARAMETER(fullSync);
27292
 
  UNUSED_PARAMETER(dataOnly);
27293
 
#elif HAVE_FULLFSYNC
27294
 
  UNUSED_PARAMETER(dataOnly);
27295
 
#else
27296
 
  UNUSED_PARAMETER(fullSync);
27297
 
  UNUSED_PARAMETER(dataOnly);
27298
 
#endif
27299
 
 
27300
 
  /* Record the number of times that we do a normal fsync() and 
27301
 
  ** FULLSYNC.  This is used during testing to verify that this procedure
27302
 
  ** gets called with the correct arguments.
27303
 
  */
27304
 
#ifdef SQLITE_TEST
27305
 
  if( fullSync ) sqlite3_fullsync_count++;
27306
 
  sqlite3_sync_count++;
27307
 
#endif
27308
 
 
27309
 
  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
27310
 
  ** no-op
27311
 
  */
27312
 
#ifdef SQLITE_NO_SYNC
27313
 
  rc = SQLITE_OK;
27314
 
#elif HAVE_FULLFSYNC
27315
 
  if( fullSync ){
27316
 
    rc = osFcntl(fd, F_FULLFSYNC, 0);
27317
 
  }else{
27318
 
    rc = 1;
27319
 
  }
27320
 
  /* If the FULLFSYNC failed, fall back to attempting an fsync().
27321
 
  ** It shouldn't be possible for fullfsync to fail on the local 
27322
 
  ** file system (on OSX), so failure indicates that FULLFSYNC
27323
 
  ** isn't supported for this file system. So, attempt an fsync 
27324
 
  ** and (for now) ignore the overhead of a superfluous fcntl call.  
27325
 
  ** It'd be better to detect fullfsync support once and avoid 
27326
 
  ** the fcntl call every time sync is called.
27327
 
  */
27328
 
  if( rc ) rc = fsync(fd);
27329
 
 
27330
 
#elif defined(__APPLE__)
27331
 
  /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
27332
 
  ** so currently we default to the macro that redefines fdatasync to fsync
27333
 
  */
27334
 
  rc = fsync(fd);
27335
 
#else 
27336
 
  rc = fdatasync(fd);
27337
 
#if OS_VXWORKS
27338
 
  if( rc==-1 && errno==ENOTSUP ){
27339
 
    rc = fsync(fd);
27340
 
  }
27341
 
#endif /* OS_VXWORKS */
27342
 
#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
27343
 
 
27344
 
  if( OS_VXWORKS && rc!= -1 ){
27345
 
    rc = 0;
27346
 
  }
27347
 
  return rc;
27348
 
}
27349
 
 
27350
 
/*
27351
 
** Make sure all writes to a particular file are committed to disk.
27352
 
**
27353
 
** If dataOnly==0 then both the file itself and its metadata (file
27354
 
** size, access time, etc) are synced.  If dataOnly!=0 then only the
27355
 
** file data is synced.
27356
 
**
27357
 
** Under Unix, also make sure that the directory entry for the file
27358
 
** has been created by fsync-ing the directory that contains the file.
27359
 
** If we do not do this and we encounter a power failure, the directory
27360
 
** entry for the journal might not exist after we reboot.  The next
27361
 
** SQLite to access the file will not know that the journal exists (because
27362
 
** the directory entry for the journal was never created) and the transaction
27363
 
** will not roll back - possibly leading to database corruption.
27364
 
*/
27365
 
static int unixSync(sqlite3_file *id, int flags){
27366
 
  int rc;
27367
 
  unixFile *pFile = (unixFile*)id;
27368
 
 
27369
 
  int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
27370
 
  int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
27371
 
 
27372
 
  /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
27373
 
  assert((flags&0x0F)==SQLITE_SYNC_NORMAL
27374
 
      || (flags&0x0F)==SQLITE_SYNC_FULL
27375
 
  );
27376
 
 
27377
 
  /* Unix cannot, but some systems may return SQLITE_FULL from here. This
27378
 
  ** line is to test that doing so does not cause any problems.
27379
 
  */
27380
 
  SimulateDiskfullError( return SQLITE_FULL );
27381
 
 
27382
 
  assert( pFile );
27383
 
  OSTRACE(("SYNC    %-3d\n", pFile->h));
27384
 
  rc = full_fsync(pFile->h, isFullsync, isDataOnly);
27385
 
  SimulateIOError( rc=1 );
27386
 
  if( rc ){
27387
 
    pFile->lastErrno = errno;
27388
 
    return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
27389
 
  }
27390
 
  if( pFile->dirfd>=0 ){
27391
 
    OSTRACE(("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
27392
 
            HAVE_FULLFSYNC, isFullsync));
27393
 
#ifndef SQLITE_DISABLE_DIRSYNC
27394
 
    /* The directory sync is only attempted if full_fsync is
27395
 
    ** turned off or unavailable.  If a full_fsync occurred above,
27396
 
    ** then the directory sync is superfluous.
27397
 
    */
27398
 
    if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
27399
 
       /*
27400
 
       ** We have received multiple reports of fsync() returning
27401
 
       ** errors when applied to directories on certain file systems.
27402
 
       ** A failed directory sync is not a big deal.  So it seems
27403
 
       ** better to ignore the error.  Ticket #1657
27404
 
       */
27405
 
       /* pFile->lastErrno = errno; */
27406
 
       /* return SQLITE_IOERR; */
27407
 
    }
27408
 
#endif
27409
 
    /* Only need to sync once, so close the  directory when we are done */
27410
 
    robust_close(pFile, pFile->dirfd, __LINE__);
27411
 
    pFile->dirfd = -1;
27412
 
  }
27413
 
  return rc;
27414
 
}
27415
 
 
27416
 
/*
27417
 
** Truncate an open file to a specified size
27418
 
*/
27419
 
static int unixTruncate(sqlite3_file *id, i64 nByte){
27420
 
  unixFile *pFile = (unixFile *)id;
27421
 
  int rc;
27422
 
  assert( pFile );
27423
 
  SimulateIOError( return SQLITE_IOERR_TRUNCATE );
27424
 
 
27425
 
  /* If the user has configured a chunk-size for this file, truncate the
27426
 
  ** file so that it consists of an integer number of chunks (i.e. the
27427
 
  ** actual file size after the operation may be larger than the requested
27428
 
  ** size).
27429
 
  */
27430
 
  if( pFile->szChunk ){
27431
 
    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
27432
 
  }
27433
 
 
27434
 
  rc = robust_ftruncate(pFile->h, (off_t)nByte);
27435
 
  if( rc ){
27436
 
    pFile->lastErrno = errno;
27437
 
    return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
27438
 
  }else{
27439
 
#ifndef NDEBUG
27440
 
    /* If we are doing a normal write to a database file (as opposed to
27441
 
    ** doing a hot-journal rollback or a write to some file other than a
27442
 
    ** normal database file) and we truncate the file to zero length,
27443
 
    ** that effectively updates the change counter.  This might happen
27444
 
    ** when restoring a database using the backup API from a zero-length
27445
 
    ** source.
27446
 
    */
27447
 
    if( pFile->inNormalWrite && nByte==0 ){
27448
 
      pFile->transCntrChng = 1;
27449
 
    }
27450
 
#endif
27451
 
 
27452
 
    return SQLITE_OK;
27453
 
  }
27454
 
}
27455
 
 
27456
 
/*
27457
 
** Determine the current size of a file in bytes
27458
 
*/
27459
 
static int unixFileSize(sqlite3_file *id, i64 *pSize){
27460
 
  int rc;
27461
 
  struct stat buf;
27462
 
  assert( id );
27463
 
  rc = osFstat(((unixFile*)id)->h, &buf);
27464
 
  SimulateIOError( rc=1 );
27465
 
  if( rc!=0 ){
27466
 
    ((unixFile*)id)->lastErrno = errno;
27467
 
    return SQLITE_IOERR_FSTAT;
27468
 
  }
27469
 
  *pSize = buf.st_size;
27470
 
 
27471
 
  /* When opening a zero-size database, the findInodeInfo() procedure
27472
 
  ** writes a single byte into that file in order to work around a bug
27473
 
  ** in the OS-X msdos filesystem.  In order to avoid problems with upper
27474
 
  ** layers, we need to report this file size as zero even though it is
27475
 
  ** really 1.   Ticket #3260.
27476
 
  */
27477
 
  if( *pSize==1 ) *pSize = 0;
27478
 
 
27479
 
 
27480
 
  return SQLITE_OK;
27481
 
}
27482
 
 
27483
 
#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
27484
 
/*
27485
 
** Handler for proxy-locking file-control verbs.  Defined below in the
27486
 
** proxying locking division.
27487
 
*/
27488
 
static int proxyFileControl(sqlite3_file*,int,void*);
27489
 
#endif
27490
 
 
27491
 
/* 
27492
 
** This function is called to handle the SQLITE_FCNTL_SIZE_HINT 
27493
 
** file-control operation.
27494
 
**
27495
 
** If the user has configured a chunk-size for this file, it could be
27496
 
** that the file needs to be extended at this point. Otherwise, the
27497
 
** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix.
27498
 
*/
27499
 
static int fcntlSizeHint(unixFile *pFile, i64 nByte){
27500
 
  if( pFile->szChunk ){
27501
 
    i64 nSize;                    /* Required file size */
27502
 
    struct stat buf;              /* Used to hold return values of fstat() */
27503
 
   
27504
 
    if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
27505
 
 
27506
 
    nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
27507
 
    if( nSize>(i64)buf.st_size ){
27508
 
 
27509
 
#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
27510
 
      /* The code below is handling the return value of osFallocate() 
27511
 
      ** correctly. posix_fallocate() is defined to "returns zero on success, 
27512
 
      ** or an error number on  failure". See the manpage for details. */
27513
 
      int err;
27514
 
      do{
27515
 
        err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
27516
 
      }while( err==EINTR );
27517
 
      if( err ) return SQLITE_IOERR_WRITE;
27518
 
#else
27519
 
      /* If the OS does not have posix_fallocate(), fake it. First use
27520
 
      ** ftruncate() to set the file size, then write a single byte to
27521
 
      ** the last byte in each block within the extended region. This
27522
 
      ** is the same technique used by glibc to implement posix_fallocate()
27523
 
      ** on systems that do not have a real fallocate() system call.
27524
 
      */
27525
 
      int nBlk = buf.st_blksize;  /* File-system block size */
27526
 
      i64 iWrite;                 /* Next offset to write to */
27527
 
 
27528
 
      if( robust_ftruncate(pFile->h, nSize) ){
27529
 
        pFile->lastErrno = errno;
27530
 
        return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
27531
 
      }
27532
 
      iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
27533
 
      while( iWrite<nSize ){
27534
 
        int nWrite = seekAndWrite(pFile, iWrite, "", 1);
27535
 
        if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
27536
 
        iWrite += nBlk;
27537
 
      }
27538
 
#endif
27539
 
    }
27540
 
  }
27541
 
 
27542
 
  return SQLITE_OK;
27543
 
}
27544
 
 
27545
 
/*
27546
 
** Information and control of an open file handle.
27547
 
*/
27548
 
static int unixFileControl(sqlite3_file *id, int op, void *pArg){
27549
 
  switch( op ){
27550
 
    case SQLITE_FCNTL_LOCKSTATE: {
27551
 
      *(int*)pArg = ((unixFile*)id)->eFileLock;
27552
 
      return SQLITE_OK;
27553
 
    }
27554
 
    case SQLITE_LAST_ERRNO: {
27555
 
      *(int*)pArg = ((unixFile*)id)->lastErrno;
27556
 
      return SQLITE_OK;
27557
 
    }
27558
 
    case SQLITE_FCNTL_CHUNK_SIZE: {
27559
 
      ((unixFile*)id)->szChunk = *(int *)pArg;
27560
 
      return SQLITE_OK;
27561
 
    }
27562
 
    case SQLITE_FCNTL_SIZE_HINT: {
27563
 
      return fcntlSizeHint((unixFile *)id, *(i64 *)pArg);
27564
 
    }
27565
 
#ifndef NDEBUG
27566
 
    /* The pager calls this method to signal that it has done
27567
 
    ** a rollback and that the database is therefore unchanged and
27568
 
    ** it hence it is OK for the transaction change counter to be
27569
 
    ** unchanged.
27570
 
    */
27571
 
    case SQLITE_FCNTL_DB_UNCHANGED: {
27572
 
      ((unixFile*)id)->dbUpdate = 0;
27573
 
      return SQLITE_OK;
27574
 
    }
27575
 
#endif
27576
 
#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
27577
 
    case SQLITE_SET_LOCKPROXYFILE:
27578
 
    case SQLITE_GET_LOCKPROXYFILE: {
27579
 
      return proxyFileControl(id,op,pArg);
27580
 
    }
27581
 
#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
27582
 
    case SQLITE_FCNTL_SYNC_OMITTED: {
27583
 
      return SQLITE_OK;  /* A no-op */
27584
 
    }
27585
 
  }
27586
 
  return SQLITE_NOTFOUND;
27587
 
}
27588
 
 
27589
 
/*
27590
 
** Return the sector size in bytes of the underlying block device for
27591
 
** the specified file. This is almost always 512 bytes, but may be
27592
 
** larger for some devices.
27593
 
**
27594
 
** SQLite code assumes this function cannot fail. It also assumes that
27595
 
** if two files are created in the same file-system directory (i.e.
27596
 
** a database and its journal file) that the sector size will be the
27597
 
** same for both.
27598
 
*/
27599
 
static int unixSectorSize(sqlite3_file *NotUsed){
27600
 
  UNUSED_PARAMETER(NotUsed);
27601
 
  return SQLITE_DEFAULT_SECTOR_SIZE;
27602
 
}
27603
 
 
27604
 
/*
27605
 
** Return the device characteristics for the file. This is always 0 for unix.
27606
 
*/
27607
 
static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
27608
 
  UNUSED_PARAMETER(NotUsed);
27609
 
  return 0;
27610
 
}
27611
 
 
27612
 
#ifndef SQLITE_OMIT_WAL
27613
 
 
27614
 
 
27615
 
/*
27616
 
** Object used to represent an shared memory buffer.  
27617
 
**
27618
 
** When multiple threads all reference the same wal-index, each thread
27619
 
** has its own unixShm object, but they all point to a single instance
27620
 
** of this unixShmNode object.  In other words, each wal-index is opened
27621
 
** only once per process.
27622
 
**
27623
 
** Each unixShmNode object is connected to a single unixInodeInfo object.
27624
 
** We could coalesce this object into unixInodeInfo, but that would mean
27625
 
** every open file that does not use shared memory (in other words, most
27626
 
** open files) would have to carry around this extra information.  So
27627
 
** the unixInodeInfo object contains a pointer to this unixShmNode object
27628
 
** and the unixShmNode object is created only when needed.
27629
 
**
27630
 
** unixMutexHeld() must be true when creating or destroying
27631
 
** this object or while reading or writing the following fields:
27632
 
**
27633
 
**      nRef
27634
 
**
27635
 
** The following fields are read-only after the object is created:
27636
 
** 
27637
 
**      fid
27638
 
**      zFilename
27639
 
**
27640
 
** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
27641
 
** unixMutexHeld() is true when reading or writing any other field
27642
 
** in this structure.
27643
 
*/
27644
 
struct unixShmNode {
27645
 
  unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
27646
 
  sqlite3_mutex *mutex;      /* Mutex to access this object */
27647
 
  char *zFilename;           /* Name of the mmapped file */
27648
 
  int h;                     /* Open file descriptor */
27649
 
  int szRegion;              /* Size of shared-memory regions */
27650
 
  int nRegion;               /* Size of array apRegion */
27651
 
  char **apRegion;           /* Array of mapped shared-memory regions */
27652
 
  int nRef;                  /* Number of unixShm objects pointing to this */
27653
 
  unixShm *pFirst;           /* All unixShm objects pointing to this */
27654
 
#ifdef SQLITE_DEBUG
27655
 
  u8 exclMask;               /* Mask of exclusive locks held */
27656
 
  u8 sharedMask;             /* Mask of shared locks held */
27657
 
  u8 nextShmId;              /* Next available unixShm.id value */
27658
 
#endif
27659
 
};
27660
 
 
27661
 
/*
27662
 
** Structure used internally by this VFS to record the state of an
27663
 
** open shared memory connection.
27664
 
**
27665
 
** The following fields are initialized when this object is created and
27666
 
** are read-only thereafter:
27667
 
**
27668
 
**    unixShm.pFile
27669
 
**    unixShm.id
27670
 
**
27671
 
** All other fields are read/write.  The unixShm.pFile->mutex must be held
27672
 
** while accessing any read/write fields.
27673
 
*/
27674
 
struct unixShm {
27675
 
  unixShmNode *pShmNode;     /* The underlying unixShmNode object */
27676
 
  unixShm *pNext;            /* Next unixShm with the same unixShmNode */
27677
 
  u8 hasMutex;               /* True if holding the unixShmNode mutex */
27678
 
  u16 sharedMask;            /* Mask of shared locks held */
27679
 
  u16 exclMask;              /* Mask of exclusive locks held */
27680
 
#ifdef SQLITE_DEBUG
27681
 
  u8 id;                     /* Id of this connection within its unixShmNode */
27682
 
#endif
27683
 
};
27684
 
 
27685
 
/*
27686
 
** Constants used for locking
27687
 
*/
27688
 
#define UNIX_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)         /* first lock byte */
27689
 
#define UNIX_SHM_DMS    (UNIX_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
27690
 
 
27691
 
/*
27692
 
** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
27693
 
**
27694
 
** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
27695
 
** otherwise.
27696
 
*/
27697
 
static int unixShmSystemLock(
27698
 
  unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
27699
 
  int lockType,          /* F_UNLCK, F_RDLCK, or F_WRLCK */
27700
 
  int ofst,              /* First byte of the locking range */
27701
 
  int n                  /* Number of bytes to lock */
27702
 
){
27703
 
  struct flock f;       /* The posix advisory locking structure */
27704
 
  int rc = SQLITE_OK;   /* Result code form fcntl() */
27705
 
 
27706
 
  /* Access to the unixShmNode object is serialized by the caller */
27707
 
  assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
27708
 
 
27709
 
  /* Shared locks never span more than one byte */
27710
 
  assert( n==1 || lockType!=F_RDLCK );
27711
 
 
27712
 
  /* Locks are within range */
27713
 
  assert( n>=1 && n<SQLITE_SHM_NLOCK );
27714
 
 
27715
 
  if( pShmNode->h>=0 ){
27716
 
    /* Initialize the locking parameters */
27717
 
    memset(&f, 0, sizeof(f));
27718
 
    f.l_type = lockType;
27719
 
    f.l_whence = SEEK_SET;
27720
 
    f.l_start = ofst;
27721
 
    f.l_len = n;
27722
 
 
27723
 
    rc = osFcntl(pShmNode->h, F_SETLK, &f);
27724
 
    rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
27725
 
  }
27726
 
 
27727
 
  /* Update the global lock state and do debug tracing */
27728
 
#ifdef SQLITE_DEBUG
27729
 
  { u16 mask;
27730
 
  OSTRACE(("SHM-LOCK "));
27731
 
  mask = (1<<(ofst+n)) - (1<<ofst);
27732
 
  if( rc==SQLITE_OK ){
27733
 
    if( lockType==F_UNLCK ){
27734
 
      OSTRACE(("unlock %d ok", ofst));
27735
 
      pShmNode->exclMask &= ~mask;
27736
 
      pShmNode->sharedMask &= ~mask;
27737
 
    }else if( lockType==F_RDLCK ){
27738
 
      OSTRACE(("read-lock %d ok", ofst));
27739
 
      pShmNode->exclMask &= ~mask;
27740
 
      pShmNode->sharedMask |= mask;
27741
 
    }else{
27742
 
      assert( lockType==F_WRLCK );
27743
 
      OSTRACE(("write-lock %d ok", ofst));
27744
 
      pShmNode->exclMask |= mask;
27745
 
      pShmNode->sharedMask &= ~mask;
27746
 
    }
27747
 
  }else{
27748
 
    if( lockType==F_UNLCK ){
27749
 
      OSTRACE(("unlock %d failed", ofst));
27750
 
    }else if( lockType==F_RDLCK ){
27751
 
      OSTRACE(("read-lock failed"));
27752
 
    }else{
27753
 
      assert( lockType==F_WRLCK );
27754
 
      OSTRACE(("write-lock %d failed", ofst));
27755
 
    }
27756
 
  }
27757
 
  OSTRACE((" - afterwards %03x,%03x\n",
27758
 
           pShmNode->sharedMask, pShmNode->exclMask));
27759
 
  }
27760
 
#endif
27761
 
 
27762
 
  return rc;        
27763
 
}
27764
 
 
27765
 
 
27766
 
/*
27767
 
** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
27768
 
**
27769
 
** This is not a VFS shared-memory method; it is a utility function called
27770
 
** by VFS shared-memory methods.
27771
 
*/
27772
 
static void unixShmPurge(unixFile *pFd){
27773
 
  unixShmNode *p = pFd->pInode->pShmNode;
27774
 
  assert( unixMutexHeld() );
27775
 
  if( p && p->nRef==0 ){
27776
 
    int i;
27777
 
    assert( p->pInode==pFd->pInode );
27778
 
    if( p->mutex ) sqlite3_mutex_free(p->mutex);
27779
 
    for(i=0; i<p->nRegion; i++){
27780
 
      if( p->h>=0 ){
27781
 
        munmap(p->apRegion[i], p->szRegion);
27782
 
      }else{
27783
 
        sqlite3_free(p->apRegion[i]);
27784
 
      }
27785
 
    }
27786
 
    sqlite3_free(p->apRegion);
27787
 
    if( p->h>=0 ){
27788
 
      robust_close(pFd, p->h, __LINE__);
27789
 
      p->h = -1;
27790
 
    }
27791
 
    p->pInode->pShmNode = 0;
27792
 
    sqlite3_free(p);
27793
 
  }
27794
 
}
27795
 
 
27796
 
/*
27797
 
** Open a shared-memory area associated with open database file pDbFd.  
27798
 
** This particular implementation uses mmapped files.
27799
 
**
27800
 
** The file used to implement shared-memory is in the same directory
27801
 
** as the open database file and has the same name as the open database
27802
 
** file with the "-shm" suffix added.  For example, if the database file
27803
 
** is "/home/user1/config.db" then the file that is created and mmapped
27804
 
** for shared memory will be called "/home/user1/config.db-shm".  
27805
 
**
27806
 
** Another approach to is to use files in /dev/shm or /dev/tmp or an
27807
 
** some other tmpfs mount. But if a file in a different directory
27808
 
** from the database file is used, then differing access permissions
27809
 
** or a chroot() might cause two different processes on the same
27810
 
** database to end up using different files for shared memory - 
27811
 
** meaning that their memory would not really be shared - resulting
27812
 
** in database corruption.  Nevertheless, this tmpfs file usage
27813
 
** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
27814
 
** or the equivalent.  The use of the SQLITE_SHM_DIRECTORY compile-time
27815
 
** option results in an incompatible build of SQLite;  builds of SQLite
27816
 
** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
27817
 
** same database file at the same time, database corruption will likely
27818
 
** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
27819
 
** "unsupported" and may go away in a future SQLite release.
27820
 
**
27821
 
** When opening a new shared-memory file, if no other instances of that
27822
 
** file are currently open, in this process or in other processes, then
27823
 
** the file must be truncated to zero length or have its header cleared.
27824
 
**
27825
 
** If the original database file (pDbFd) is using the "unix-excl" VFS
27826
 
** that means that an exclusive lock is held on the database file and
27827
 
** that no other processes are able to read or write the database.  In
27828
 
** that case, we do not really need shared memory.  No shared memory
27829
 
** file is created.  The shared memory will be simulated with heap memory.
27830
 
*/
27831
 
static int unixOpenSharedMemory(unixFile *pDbFd){
27832
 
  struct unixShm *p = 0;          /* The connection to be opened */
27833
 
  struct unixShmNode *pShmNode;   /* The underlying mmapped file */
27834
 
  int rc;                         /* Result code */
27835
 
  unixInodeInfo *pInode;          /* The inode of fd */
27836
 
  char *zShmFilename;             /* Name of the file used for SHM */
27837
 
  int nShmFilename;               /* Size of the SHM filename in bytes */
27838
 
 
27839
 
  /* Allocate space for the new unixShm object. */
27840
 
  p = sqlite3_malloc( sizeof(*p) );
27841
 
  if( p==0 ) return SQLITE_NOMEM;
27842
 
  memset(p, 0, sizeof(*p));
27843
 
  assert( pDbFd->pShm==0 );
27844
 
 
27845
 
  /* Check to see if a unixShmNode object already exists. Reuse an existing
27846
 
  ** one if present. Create a new one if necessary.
27847
 
  */
27848
 
  unixEnterMutex();
27849
 
  pInode = pDbFd->pInode;
27850
 
  pShmNode = pInode->pShmNode;
27851
 
  if( pShmNode==0 ){
27852
 
    struct stat sStat;                 /* fstat() info for database file */
27853
 
 
27854
 
    /* Call fstat() to figure out the permissions on the database file. If
27855
 
    ** a new *-shm file is created, an attempt will be made to create it
27856
 
    ** with the same permissions. The actual permissions the file is created
27857
 
    ** with are subject to the current umask setting.
27858
 
    */
27859
 
    if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
27860
 
      rc = SQLITE_IOERR_FSTAT;
27861
 
      goto shm_open_err;
27862
 
    }
27863
 
 
27864
 
#ifdef SQLITE_SHM_DIRECTORY
27865
 
    nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 30;
27866
 
#else
27867
 
    nShmFilename = 5 + (int)strlen(pDbFd->zPath);
27868
 
#endif
27869
 
    pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
27870
 
    if( pShmNode==0 ){
27871
 
      rc = SQLITE_NOMEM;
27872
 
      goto shm_open_err;
27873
 
    }
27874
 
    memset(pShmNode, 0, sizeof(*pShmNode));
27875
 
    zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
27876
 
#ifdef SQLITE_SHM_DIRECTORY
27877
 
    sqlite3_snprintf(nShmFilename, zShmFilename, 
27878
 
                     SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
27879
 
                     (u32)sStat.st_ino, (u32)sStat.st_dev);
27880
 
#else
27881
 
    sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath);
27882
 
#endif
27883
 
    pShmNode->h = -1;
27884
 
    pDbFd->pInode->pShmNode = pShmNode;
27885
 
    pShmNode->pInode = pDbFd->pInode;
27886
 
    pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
27887
 
    if( pShmNode->mutex==0 ){
27888
 
      rc = SQLITE_NOMEM;
27889
 
      goto shm_open_err;
27890
 
    }
27891
 
 
27892
 
    if( pInode->bProcessLock==0 ){
27893
 
      pShmNode->h = robust_open(zShmFilename, O_RDWR|O_CREAT,
27894
 
                               (sStat.st_mode & 0777));
27895
 
      if( pShmNode->h<0 ){
27896
 
        rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
27897
 
        goto shm_open_err;
27898
 
      }
27899
 
  
27900
 
      /* Check to see if another process is holding the dead-man switch.
27901
 
      ** If not, truncate the file to zero length. 
27902
 
      */
27903
 
      rc = SQLITE_OK;
27904
 
      if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
27905
 
        if( robust_ftruncate(pShmNode->h, 0) ){
27906
 
          rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
27907
 
        }
27908
 
      }
27909
 
      if( rc==SQLITE_OK ){
27910
 
        rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
27911
 
      }
27912
 
      if( rc ) goto shm_open_err;
27913
 
    }
27914
 
  }
27915
 
 
27916
 
  /* Make the new connection a child of the unixShmNode */
27917
 
  p->pShmNode = pShmNode;
27918
 
#ifdef SQLITE_DEBUG
27919
 
  p->id = pShmNode->nextShmId++;
27920
 
#endif
27921
 
  pShmNode->nRef++;
27922
 
  pDbFd->pShm = p;
27923
 
  unixLeaveMutex();
27924
 
 
27925
 
  /* The reference count on pShmNode has already been incremented under
27926
 
  ** the cover of the unixEnterMutex() mutex and the pointer from the
27927
 
  ** new (struct unixShm) object to the pShmNode has been set. All that is
27928
 
  ** left to do is to link the new object into the linked list starting
27929
 
  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
27930
 
  ** mutex.
27931
 
  */
27932
 
  sqlite3_mutex_enter(pShmNode->mutex);
27933
 
  p->pNext = pShmNode->pFirst;
27934
 
  pShmNode->pFirst = p;
27935
 
  sqlite3_mutex_leave(pShmNode->mutex);
27936
 
  return SQLITE_OK;
27937
 
 
27938
 
  /* Jump here on any error */
27939
 
shm_open_err:
27940
 
  unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
27941
 
  sqlite3_free(p);
27942
 
  unixLeaveMutex();
27943
 
  return rc;
27944
 
}
27945
 
 
27946
 
/*
27947
 
** This function is called to obtain a pointer to region iRegion of the 
27948
 
** shared-memory associated with the database file fd. Shared-memory regions 
27949
 
** are numbered starting from zero. Each shared-memory region is szRegion 
27950
 
** bytes in size.
27951
 
**
27952
 
** If an error occurs, an error code is returned and *pp is set to NULL.
27953
 
**
27954
 
** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
27955
 
** region has not been allocated (by any client, including one running in a
27956
 
** separate process), then *pp is set to NULL and SQLITE_OK returned. If 
27957
 
** bExtend is non-zero and the requested shared-memory region has not yet 
27958
 
** been allocated, it is allocated by this function.
27959
 
**
27960
 
** If the shared-memory region has already been allocated or is allocated by
27961
 
** this call as described above, then it is mapped into this processes 
27962
 
** address space (if it is not already), *pp is set to point to the mapped 
27963
 
** memory and SQLITE_OK returned.
27964
 
*/
27965
 
static int unixShmMap(
27966
 
  sqlite3_file *fd,               /* Handle open on database file */
27967
 
  int iRegion,                    /* Region to retrieve */
27968
 
  int szRegion,                   /* Size of regions */
27969
 
  int bExtend,                    /* True to extend file if necessary */
27970
 
  void volatile **pp              /* OUT: Mapped memory */
27971
 
){
27972
 
  unixFile *pDbFd = (unixFile*)fd;
27973
 
  unixShm *p;
27974
 
  unixShmNode *pShmNode;
27975
 
  int rc = SQLITE_OK;
27976
 
 
27977
 
  /* If the shared-memory file has not yet been opened, open it now. */
27978
 
  if( pDbFd->pShm==0 ){
27979
 
    rc = unixOpenSharedMemory(pDbFd);
27980
 
    if( rc!=SQLITE_OK ) return rc;
27981
 
  }
27982
 
 
27983
 
  p = pDbFd->pShm;
27984
 
  pShmNode = p->pShmNode;
27985
 
  sqlite3_mutex_enter(pShmNode->mutex);
27986
 
  assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
27987
 
  assert( pShmNode->pInode==pDbFd->pInode );
27988
 
  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
27989
 
  assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
27990
 
 
27991
 
  if( pShmNode->nRegion<=iRegion ){
27992
 
    char **apNew;                      /* New apRegion[] array */
27993
 
    int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
27994
 
    struct stat sStat;                 /* Used by fstat() */
27995
 
 
27996
 
    pShmNode->szRegion = szRegion;
27997
 
 
27998
 
    if( pShmNode->h>=0 ){
27999
 
      /* The requested region is not mapped into this processes address space.
28000
 
      ** Check to see if it has been allocated (i.e. if the wal-index file is
28001
 
      ** large enough to contain the requested region).
28002
 
      */
28003
 
      if( osFstat(pShmNode->h, &sStat) ){
28004
 
        rc = SQLITE_IOERR_SHMSIZE;
28005
 
        goto shmpage_out;
28006
 
      }
28007
 
  
28008
 
      if( sStat.st_size<nByte ){
28009
 
        /* The requested memory region does not exist. If bExtend is set to
28010
 
        ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
28011
 
        **
28012
 
        ** Alternatively, if bExtend is true, use ftruncate() to allocate
28013
 
        ** the requested memory region.
28014
 
        */
28015
 
        if( !bExtend ) goto shmpage_out;
28016
 
        if( robust_ftruncate(pShmNode->h, nByte) ){
28017
 
          rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate",
28018
 
                            pShmNode->zFilename);
28019
 
          goto shmpage_out;
28020
 
        }
28021
 
      }
28022
 
    }
28023
 
 
28024
 
    /* Map the requested memory region into this processes address space. */
28025
 
    apNew = (char **)sqlite3_realloc(
28026
 
        pShmNode->apRegion, (iRegion+1)*sizeof(char *)
28027
 
    );
28028
 
    if( !apNew ){
28029
 
      rc = SQLITE_IOERR_NOMEM;
28030
 
      goto shmpage_out;
28031
 
    }
28032
 
    pShmNode->apRegion = apNew;
28033
 
    while(pShmNode->nRegion<=iRegion){
28034
 
      void *pMem;
28035
 
      if( pShmNode->h>=0 ){
28036
 
        pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE, 
28037
 
            MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion
28038
 
        );
28039
 
        if( pMem==MAP_FAILED ){
28040
 
          rc = SQLITE_IOERR;
28041
 
          goto shmpage_out;
28042
 
        }
28043
 
      }else{
28044
 
        pMem = sqlite3_malloc(szRegion);
28045
 
        if( pMem==0 ){
28046
 
          rc = SQLITE_NOMEM;
28047
 
          goto shmpage_out;
28048
 
        }
28049
 
        memset(pMem, 0, szRegion);
28050
 
      }
28051
 
      pShmNode->apRegion[pShmNode->nRegion] = pMem;
28052
 
      pShmNode->nRegion++;
28053
 
    }
28054
 
  }
28055
 
 
28056
 
shmpage_out:
28057
 
  if( pShmNode->nRegion>iRegion ){
28058
 
    *pp = pShmNode->apRegion[iRegion];
28059
 
  }else{
28060
 
    *pp = 0;
28061
 
  }
28062
 
  sqlite3_mutex_leave(pShmNode->mutex);
28063
 
  return rc;
28064
 
}
28065
 
 
28066
 
/*
28067
 
** Change the lock state for a shared-memory segment.
28068
 
**
28069
 
** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
28070
 
** different here than in posix.  In xShmLock(), one can go from unlocked
28071
 
** to shared and back or from unlocked to exclusive and back.  But one may
28072
 
** not go from shared to exclusive or from exclusive to shared.
28073
 
*/
28074
 
static int unixShmLock(
28075
 
  sqlite3_file *fd,          /* Database file holding the shared memory */
28076
 
  int ofst,                  /* First lock to acquire or release */
28077
 
  int n,                     /* Number of locks to acquire or release */
28078
 
  int flags                  /* What to do with the lock */
28079
 
){
28080
 
  unixFile *pDbFd = (unixFile*)fd;      /* Connection holding shared memory */
28081
 
  unixShm *p = pDbFd->pShm;             /* The shared memory being locked */
28082
 
  unixShm *pX;                          /* For looping over all siblings */
28083
 
  unixShmNode *pShmNode = p->pShmNode;  /* The underlying file iNode */
28084
 
  int rc = SQLITE_OK;                   /* Result code */
28085
 
  u16 mask;                             /* Mask of locks to take or release */
28086
 
 
28087
 
  assert( pShmNode==pDbFd->pInode->pShmNode );
28088
 
  assert( pShmNode->pInode==pDbFd->pInode );
28089
 
  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
28090
 
  assert( n>=1 );
28091
 
  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
28092
 
       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
28093
 
       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
28094
 
       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
28095
 
  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
28096
 
  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
28097
 
  assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
28098
 
 
28099
 
  mask = (1<<(ofst+n)) - (1<<ofst);
28100
 
  assert( n>1 || mask==(1<<ofst) );
28101
 
  sqlite3_mutex_enter(pShmNode->mutex);
28102
 
  if( flags & SQLITE_SHM_UNLOCK ){
28103
 
    u16 allMask = 0; /* Mask of locks held by siblings */
28104
 
 
28105
 
    /* See if any siblings hold this same lock */
28106
 
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
28107
 
      if( pX==p ) continue;
28108
 
      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
28109
 
      allMask |= pX->sharedMask;
28110
 
    }
28111
 
 
28112
 
    /* Unlock the system-level locks */
28113
 
    if( (mask & allMask)==0 ){
28114
 
      rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
28115
 
    }else{
28116
 
      rc = SQLITE_OK;
28117
 
    }
28118
 
 
28119
 
    /* Undo the local locks */
28120
 
    if( rc==SQLITE_OK ){
28121
 
      p->exclMask &= ~mask;
28122
 
      p->sharedMask &= ~mask;
28123
 
    } 
28124
 
  }else if( flags & SQLITE_SHM_SHARED ){
28125
 
    u16 allShared = 0;  /* Union of locks held by connections other than "p" */
28126
 
 
28127
 
    /* Find out which shared locks are already held by sibling connections.
28128
 
    ** If any sibling already holds an exclusive lock, go ahead and return
28129
 
    ** SQLITE_BUSY.
28130
 
    */
28131
 
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
28132
 
      if( (pX->exclMask & mask)!=0 ){
28133
 
        rc = SQLITE_BUSY;
28134
 
        break;
28135
 
      }
28136
 
      allShared |= pX->sharedMask;
28137
 
    }
28138
 
 
28139
 
    /* Get shared locks at the system level, if necessary */
28140
 
    if( rc==SQLITE_OK ){
28141
 
      if( (allShared & mask)==0 ){
28142
 
        rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
28143
 
      }else{
28144
 
        rc = SQLITE_OK;
28145
 
      }
28146
 
    }
28147
 
 
28148
 
    /* Get the local shared locks */
28149
 
    if( rc==SQLITE_OK ){
28150
 
      p->sharedMask |= mask;
28151
 
    }
28152
 
  }else{
28153
 
    /* Make sure no sibling connections hold locks that will block this
28154
 
    ** lock.  If any do, return SQLITE_BUSY right away.
28155
 
    */
28156
 
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
28157
 
      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
28158
 
        rc = SQLITE_BUSY;
28159
 
        break;
28160
 
      }
28161
 
    }
28162
 
  
28163
 
    /* Get the exclusive locks at the system level.  Then if successful
28164
 
    ** also mark the local connection as being locked.
28165
 
    */
28166
 
    if( rc==SQLITE_OK ){
28167
 
      rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
28168
 
      if( rc==SQLITE_OK ){
28169
 
        assert( (p->sharedMask & mask)==0 );
28170
 
        p->exclMask |= mask;
28171
 
      }
28172
 
    }
28173
 
  }
28174
 
  sqlite3_mutex_leave(pShmNode->mutex);
28175
 
  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
28176
 
           p->id, getpid(), p->sharedMask, p->exclMask));
28177
 
  return rc;
28178
 
}
28179
 
 
28180
 
/*
28181
 
** Implement a memory barrier or memory fence on shared memory.  
28182
 
**
28183
 
** All loads and stores begun before the barrier must complete before
28184
 
** any load or store begun after the barrier.
28185
 
*/
28186
 
static void unixShmBarrier(
28187
 
  sqlite3_file *fd                /* Database file holding the shared memory */
28188
 
){
28189
 
  UNUSED_PARAMETER(fd);
28190
 
  unixEnterMutex();
28191
 
  unixLeaveMutex();
28192
 
}
28193
 
 
28194
 
/*
28195
 
** Close a connection to shared-memory.  Delete the underlying 
28196
 
** storage if deleteFlag is true.
28197
 
**
28198
 
** If there is no shared memory associated with the connection then this
28199
 
** routine is a harmless no-op.
28200
 
*/
28201
 
static int unixShmUnmap(
28202
 
  sqlite3_file *fd,               /* The underlying database file */
28203
 
  int deleteFlag                  /* Delete shared-memory if true */
28204
 
){
28205
 
  unixShm *p;                     /* The connection to be closed */
28206
 
  unixShmNode *pShmNode;          /* The underlying shared-memory file */
28207
 
  unixShm **pp;                   /* For looping over sibling connections */
28208
 
  unixFile *pDbFd;                /* The underlying database file */
28209
 
 
28210
 
  pDbFd = (unixFile*)fd;
28211
 
  p = pDbFd->pShm;
28212
 
  if( p==0 ) return SQLITE_OK;
28213
 
  pShmNode = p->pShmNode;
28214
 
 
28215
 
  assert( pShmNode==pDbFd->pInode->pShmNode );
28216
 
  assert( pShmNode->pInode==pDbFd->pInode );
28217
 
 
28218
 
  /* Remove connection p from the set of connections associated
28219
 
  ** with pShmNode */
28220
 
  sqlite3_mutex_enter(pShmNode->mutex);
28221
 
  for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
28222
 
  *pp = p->pNext;
28223
 
 
28224
 
  /* Free the connection p */
28225
 
  sqlite3_free(p);
28226
 
  pDbFd->pShm = 0;
28227
 
  sqlite3_mutex_leave(pShmNode->mutex);
28228
 
 
28229
 
  /* If pShmNode->nRef has reached 0, then close the underlying
28230
 
  ** shared-memory file, too */
28231
 
  unixEnterMutex();
28232
 
  assert( pShmNode->nRef>0 );
28233
 
  pShmNode->nRef--;
28234
 
  if( pShmNode->nRef==0 ){
28235
 
    if( deleteFlag && pShmNode->h>=0 ) unlink(pShmNode->zFilename);
28236
 
    unixShmPurge(pDbFd);
28237
 
  }
28238
 
  unixLeaveMutex();
28239
 
 
28240
 
  return SQLITE_OK;
28241
 
}
28242
 
 
28243
 
 
28244
 
#else
28245
 
# define unixShmMap     0
28246
 
# define unixShmLock    0
28247
 
# define unixShmBarrier 0
28248
 
# define unixShmUnmap   0
28249
 
#endif /* #ifndef SQLITE_OMIT_WAL */
28250
 
 
28251
 
/*
28252
 
** Here ends the implementation of all sqlite3_file methods.
28253
 
**
28254
 
********************** End sqlite3_file Methods *******************************
28255
 
******************************************************************************/
28256
 
 
28257
 
/*
28258
 
** This division contains definitions of sqlite3_io_methods objects that
28259
 
** implement various file locking strategies.  It also contains definitions
28260
 
** of "finder" functions.  A finder-function is used to locate the appropriate
28261
 
** sqlite3_io_methods object for a particular database file.  The pAppData
28262
 
** field of the sqlite3_vfs VFS objects are initialized to be pointers to
28263
 
** the correct finder-function for that VFS.
28264
 
**
28265
 
** Most finder functions return a pointer to a fixed sqlite3_io_methods
28266
 
** object.  The only interesting finder-function is autolockIoFinder, which
28267
 
** looks at the filesystem type and tries to guess the best locking
28268
 
** strategy from that.
28269
 
**
28270
 
** For finder-funtion F, two objects are created:
28271
 
**
28272
 
**    (1) The real finder-function named "FImpt()".
28273
 
**
28274
 
**    (2) A constant pointer to this function named just "F".
28275
 
**
28276
 
**
28277
 
** A pointer to the F pointer is used as the pAppData value for VFS
28278
 
** objects.  We have to do this instead of letting pAppData point
28279
 
** directly at the finder-function since C90 rules prevent a void*
28280
 
** from be cast into a function pointer.
28281
 
**
28282
 
**
28283
 
** Each instance of this macro generates two objects:
28284
 
**
28285
 
**   *  A constant sqlite3_io_methods object call METHOD that has locking
28286
 
**      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
28287
 
**
28288
 
**   *  An I/O method finder function called FINDER that returns a pointer
28289
 
**      to the METHOD object in the previous bullet.
28290
 
*/
28291
 
#define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK)      \
28292
 
static const sqlite3_io_methods METHOD = {                                   \
28293
 
   VERSION,                    /* iVersion */                                \
28294
 
   CLOSE,                      /* xClose */                                  \
28295
 
   unixRead,                   /* xRead */                                   \
28296
 
   unixWrite,                  /* xWrite */                                  \
28297
 
   unixTruncate,               /* xTruncate */                               \
28298
 
   unixSync,                   /* xSync */                                   \
28299
 
   unixFileSize,               /* xFileSize */                               \
28300
 
   LOCK,                       /* xLock */                                   \
28301
 
   UNLOCK,                     /* xUnlock */                                 \
28302
 
   CKLOCK,                     /* xCheckReservedLock */                      \
28303
 
   unixFileControl,            /* xFileControl */                            \
28304
 
   unixSectorSize,             /* xSectorSize */                             \
28305
 
   unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
28306
 
   unixShmMap,                 /* xShmMap */                                 \
28307
 
   unixShmLock,                /* xShmLock */                                \
28308
 
   unixShmBarrier,             /* xShmBarrier */                             \
28309
 
   unixShmUnmap                /* xShmUnmap */                               \
28310
 
};                                                                           \
28311
 
static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
28312
 
  UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
28313
 
  return &METHOD;                                                            \
28314
 
}                                                                            \
28315
 
static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
28316
 
    = FINDER##Impl;
28317
 
 
28318
 
/*
28319
 
** Here are all of the sqlite3_io_methods objects for each of the
28320
 
** locking strategies.  Functions that return pointers to these methods
28321
 
** are also created.
28322
 
*/
28323
 
IOMETHODS(
28324
 
  posixIoFinder,            /* Finder function name */
28325
 
  posixIoMethods,           /* sqlite3_io_methods object name */
28326
 
  2,                        /* shared memory is enabled */
28327
 
  unixClose,                /* xClose method */
28328
 
  unixLock,                 /* xLock method */
28329
 
  unixUnlock,               /* xUnlock method */
28330
 
  unixCheckReservedLock     /* xCheckReservedLock method */
28331
 
)
28332
 
IOMETHODS(
28333
 
  nolockIoFinder,           /* Finder function name */
28334
 
  nolockIoMethods,          /* sqlite3_io_methods object name */
28335
 
  1,                        /* shared memory is disabled */
28336
 
  nolockClose,              /* xClose method */
28337
 
  nolockLock,               /* xLock method */
28338
 
  nolockUnlock,             /* xUnlock method */
28339
 
  nolockCheckReservedLock   /* xCheckReservedLock method */
28340
 
)
28341
 
IOMETHODS(
28342
 
  dotlockIoFinder,          /* Finder function name */
28343
 
  dotlockIoMethods,         /* sqlite3_io_methods object name */
28344
 
  1,                        /* shared memory is disabled */
28345
 
  dotlockClose,             /* xClose method */
28346
 
  dotlockLock,              /* xLock method */
28347
 
  dotlockUnlock,            /* xUnlock method */
28348
 
  dotlockCheckReservedLock  /* xCheckReservedLock method */
28349
 
)
28350
 
 
28351
 
#if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
28352
 
IOMETHODS(
28353
 
  flockIoFinder,            /* Finder function name */
28354
 
  flockIoMethods,           /* sqlite3_io_methods object name */
28355
 
  1,                        /* shared memory is disabled */
28356
 
  flockClose,               /* xClose method */
28357
 
  flockLock,                /* xLock method */
28358
 
  flockUnlock,              /* xUnlock method */
28359
 
  flockCheckReservedLock    /* xCheckReservedLock method */
28360
 
)
28361
 
#endif
28362
 
 
28363
 
#if OS_VXWORKS
28364
 
IOMETHODS(
28365
 
  semIoFinder,              /* Finder function name */
28366
 
  semIoMethods,             /* sqlite3_io_methods object name */
28367
 
  1,                        /* shared memory is disabled */
28368
 
  semClose,                 /* xClose method */
28369
 
  semLock,                  /* xLock method */
28370
 
  semUnlock,                /* xUnlock method */
28371
 
  semCheckReservedLock      /* xCheckReservedLock method */
28372
 
)
28373
 
#endif
28374
 
 
28375
 
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28376
 
IOMETHODS(
28377
 
  afpIoFinder,              /* Finder function name */
28378
 
  afpIoMethods,             /* sqlite3_io_methods object name */
28379
 
  1,                        /* shared memory is disabled */
28380
 
  afpClose,                 /* xClose method */
28381
 
  afpLock,                  /* xLock method */
28382
 
  afpUnlock,                /* xUnlock method */
28383
 
  afpCheckReservedLock      /* xCheckReservedLock method */
28384
 
)
28385
 
#endif
28386
 
 
28387
 
/*
28388
 
** The proxy locking method is a "super-method" in the sense that it
28389
 
** opens secondary file descriptors for the conch and lock files and
28390
 
** it uses proxy, dot-file, AFP, and flock() locking methods on those
28391
 
** secondary files.  For this reason, the division that implements
28392
 
** proxy locking is located much further down in the file.  But we need
28393
 
** to go ahead and define the sqlite3_io_methods and finder function
28394
 
** for proxy locking here.  So we forward declare the I/O methods.
28395
 
*/
28396
 
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28397
 
static int proxyClose(sqlite3_file*);
28398
 
static int proxyLock(sqlite3_file*, int);
28399
 
static int proxyUnlock(sqlite3_file*, int);
28400
 
static int proxyCheckReservedLock(sqlite3_file*, int*);
28401
 
IOMETHODS(
28402
 
  proxyIoFinder,            /* Finder function name */
28403
 
  proxyIoMethods,           /* sqlite3_io_methods object name */
28404
 
  1,                        /* shared memory is disabled */
28405
 
  proxyClose,               /* xClose method */
28406
 
  proxyLock,                /* xLock method */
28407
 
  proxyUnlock,              /* xUnlock method */
28408
 
  proxyCheckReservedLock    /* xCheckReservedLock method */
28409
 
)
28410
 
#endif
28411
 
 
28412
 
/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
28413
 
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28414
 
IOMETHODS(
28415
 
  nfsIoFinder,               /* Finder function name */
28416
 
  nfsIoMethods,              /* sqlite3_io_methods object name */
28417
 
  1,                         /* shared memory is disabled */
28418
 
  unixClose,                 /* xClose method */
28419
 
  unixLock,                  /* xLock method */
28420
 
  nfsUnlock,                 /* xUnlock method */
28421
 
  unixCheckReservedLock      /* xCheckReservedLock method */
28422
 
)
28423
 
#endif
28424
 
 
28425
 
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28426
 
/* 
28427
 
** This "finder" function attempts to determine the best locking strategy 
28428
 
** for the database file "filePath".  It then returns the sqlite3_io_methods
28429
 
** object that implements that strategy.
28430
 
**
28431
 
** This is for MacOSX only.
28432
 
*/
28433
 
static const sqlite3_io_methods *autolockIoFinderImpl(
28434
 
  const char *filePath,    /* name of the database file */
28435
 
  unixFile *pNew           /* open file object for the database file */
28436
 
){
28437
 
  static const struct Mapping {
28438
 
    const char *zFilesystem;              /* Filesystem type name */
28439
 
    const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
28440
 
  } aMap[] = {
28441
 
    { "hfs",    &posixIoMethods },
28442
 
    { "ufs",    &posixIoMethods },
28443
 
    { "afpfs",  &afpIoMethods },
28444
 
    { "smbfs",  &afpIoMethods },
28445
 
    { "webdav", &nolockIoMethods },
28446
 
    { 0, 0 }
28447
 
  };
28448
 
  int i;
28449
 
  struct statfs fsInfo;
28450
 
  struct flock lockInfo;
28451
 
 
28452
 
  if( !filePath ){
28453
 
    /* If filePath==NULL that means we are dealing with a transient file
28454
 
    ** that does not need to be locked. */
28455
 
    return &nolockIoMethods;
28456
 
  }
28457
 
  if( statfs(filePath, &fsInfo) != -1 ){
28458
 
    if( fsInfo.f_flags & MNT_RDONLY ){
28459
 
      return &nolockIoMethods;
28460
 
    }
28461
 
    for(i=0; aMap[i].zFilesystem; i++){
28462
 
      if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
28463
 
        return aMap[i].pMethods;
28464
 
      }
28465
 
    }
28466
 
  }
28467
 
 
28468
 
  /* Default case. Handles, amongst others, "nfs".
28469
 
  ** Test byte-range lock using fcntl(). If the call succeeds, 
28470
 
  ** assume that the file-system supports POSIX style locks. 
28471
 
  */
28472
 
  lockInfo.l_len = 1;
28473
 
  lockInfo.l_start = 0;
28474
 
  lockInfo.l_whence = SEEK_SET;
28475
 
  lockInfo.l_type = F_RDLCK;
28476
 
  if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
28477
 
    if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
28478
 
      return &nfsIoMethods;
28479
 
    } else {
28480
 
      return &posixIoMethods;
28481
 
    }
28482
 
  }else{
28483
 
    return &dotlockIoMethods;
28484
 
  }
28485
 
}
28486
 
static const sqlite3_io_methods 
28487
 
  *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
28488
 
 
28489
 
#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
28490
 
 
28491
 
#if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE
28492
 
/* 
28493
 
** This "finder" function attempts to determine the best locking strategy 
28494
 
** for the database file "filePath".  It then returns the sqlite3_io_methods
28495
 
** object that implements that strategy.
28496
 
**
28497
 
** This is for VXWorks only.
28498
 
*/
28499
 
static const sqlite3_io_methods *autolockIoFinderImpl(
28500
 
  const char *filePath,    /* name of the database file */
28501
 
  unixFile *pNew           /* the open file object */
28502
 
){
28503
 
  struct flock lockInfo;
28504
 
 
28505
 
  if( !filePath ){
28506
 
    /* If filePath==NULL that means we are dealing with a transient file
28507
 
    ** that does not need to be locked. */
28508
 
    return &nolockIoMethods;
28509
 
  }
28510
 
 
28511
 
  /* Test if fcntl() is supported and use POSIX style locks.
28512
 
  ** Otherwise fall back to the named semaphore method.
28513
 
  */
28514
 
  lockInfo.l_len = 1;
28515
 
  lockInfo.l_start = 0;
28516
 
  lockInfo.l_whence = SEEK_SET;
28517
 
  lockInfo.l_type = F_RDLCK;
28518
 
  if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
28519
 
    return &posixIoMethods;
28520
 
  }else{
28521
 
    return &semIoMethods;
28522
 
  }
28523
 
}
28524
 
static const sqlite3_io_methods 
28525
 
  *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
28526
 
 
28527
 
#endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
28528
 
 
28529
 
/*
28530
 
** An abstract type for a pointer to a IO method finder function:
28531
 
*/
28532
 
typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
28533
 
 
28534
 
 
28535
 
/****************************************************************************
28536
 
**************************** sqlite3_vfs methods ****************************
28537
 
**
28538
 
** This division contains the implementation of methods on the
28539
 
** sqlite3_vfs object.
28540
 
*/
28541
 
 
28542
 
/*
28543
 
** Initialize the contents of the unixFile structure pointed to by pId.
28544
 
*/
28545
 
static int fillInUnixFile(
28546
 
  sqlite3_vfs *pVfs,      /* Pointer to vfs object */
28547
 
  int h,                  /* Open file descriptor of file being opened */
28548
 
  int dirfd,              /* Directory file descriptor */
28549
 
  sqlite3_file *pId,      /* Write to the unixFile structure here */
28550
 
  const char *zFilename,  /* Name of the file being opened */
28551
 
  int noLock,             /* Omit locking if true */
28552
 
  int isDelete,           /* Delete on close if true */
28553
 
  int isReadOnly          /* True if the file is opened read-only */
28554
 
){
28555
 
  const sqlite3_io_methods *pLockingStyle;
28556
 
  unixFile *pNew = (unixFile *)pId;
28557
 
  int rc = SQLITE_OK;
28558
 
 
28559
 
  assert( pNew->pInode==NULL );
28560
 
 
28561
 
  /* Parameter isDelete is only used on vxworks. Express this explicitly 
28562
 
  ** here to prevent compiler warnings about unused parameters.
28563
 
  */
28564
 
  UNUSED_PARAMETER(isDelete);
28565
 
 
28566
 
  /* Usually the path zFilename should not be a relative pathname. The
28567
 
  ** exception is when opening the proxy "conch" file in builds that
28568
 
  ** include the special Apple locking styles.
28569
 
  */
28570
 
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28571
 
  assert( zFilename==0 || zFilename[0]=='/' 
28572
 
    || pVfs->pAppData==(void*)&autolockIoFinder );
28573
 
#else
28574
 
  assert( zFilename==0 || zFilename[0]=='/' );
28575
 
#endif
28576
 
 
28577
 
  OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
28578
 
  pNew->h = h;
28579
 
  pNew->dirfd = dirfd;
28580
 
  pNew->zPath = zFilename;
28581
 
  if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
28582
 
    pNew->ctrlFlags = UNIXFILE_EXCL;
28583
 
  }else{
28584
 
    pNew->ctrlFlags = 0;
28585
 
  }
28586
 
  if( isReadOnly ){
28587
 
    pNew->ctrlFlags |= UNIXFILE_RDONLY;
28588
 
  }
28589
 
 
28590
 
#if OS_VXWORKS
28591
 
  pNew->pId = vxworksFindFileId(zFilename);
28592
 
  if( pNew->pId==0 ){
28593
 
    noLock = 1;
28594
 
    rc = SQLITE_NOMEM;
28595
 
  }
28596
 
#endif
28597
 
 
28598
 
  if( noLock ){
28599
 
    pLockingStyle = &nolockIoMethods;
28600
 
  }else{
28601
 
    pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
28602
 
#if SQLITE_ENABLE_LOCKING_STYLE
28603
 
    /* Cache zFilename in the locking context (AFP and dotlock override) for
28604
 
    ** proxyLock activation is possible (remote proxy is based on db name)
28605
 
    ** zFilename remains valid until file is closed, to support */
28606
 
    pNew->lockingContext = (void*)zFilename;
28607
 
#endif
28608
 
  }
28609
 
 
28610
 
  if( pLockingStyle == &posixIoMethods
28611
 
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28612
 
    || pLockingStyle == &nfsIoMethods
28613
 
#endif
28614
 
  ){
28615
 
    unixEnterMutex();
28616
 
    rc = findInodeInfo(pNew, &pNew->pInode);
28617
 
    if( rc!=SQLITE_OK ){
28618
 
      /* If an error occured in findInodeInfo(), close the file descriptor
28619
 
      ** immediately, before releasing the mutex. findInodeInfo() may fail
28620
 
      ** in two scenarios:
28621
 
      **
28622
 
      **   (a) A call to fstat() failed.
28623
 
      **   (b) A malloc failed.
28624
 
      **
28625
 
      ** Scenario (b) may only occur if the process is holding no other
28626
 
      ** file descriptors open on the same file. If there were other file
28627
 
      ** descriptors on this file, then no malloc would be required by
28628
 
      ** findInodeInfo(). If this is the case, it is quite safe to close
28629
 
      ** handle h - as it is guaranteed that no posix locks will be released
28630
 
      ** by doing so.
28631
 
      **
28632
 
      ** If scenario (a) caused the error then things are not so safe. The
28633
 
      ** implicit assumption here is that if fstat() fails, things are in
28634
 
      ** such bad shape that dropping a lock or two doesn't matter much.
28635
 
      */
28636
 
      robust_close(pNew, h, __LINE__);
28637
 
      h = -1;
28638
 
    }
28639
 
    unixLeaveMutex();
28640
 
  }
28641
 
 
28642
 
#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
28643
 
  else if( pLockingStyle == &afpIoMethods ){
28644
 
    /* AFP locking uses the file path so it needs to be included in
28645
 
    ** the afpLockingContext.
28646
 
    */
28647
 
    afpLockingContext *pCtx;
28648
 
    pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
28649
 
    if( pCtx==0 ){
28650
 
      rc = SQLITE_NOMEM;
28651
 
    }else{
28652
 
      /* NB: zFilename exists and remains valid until the file is closed
28653
 
      ** according to requirement F11141.  So we do not need to make a
28654
 
      ** copy of the filename. */
28655
 
      pCtx->dbPath = zFilename;
28656
 
      pCtx->reserved = 0;
28657
 
      srandomdev();
28658
 
      unixEnterMutex();
28659
 
      rc = findInodeInfo(pNew, &pNew->pInode);
28660
 
      if( rc!=SQLITE_OK ){
28661
 
        sqlite3_free(pNew->lockingContext);
28662
 
        robust_close(pNew, h, __LINE__);
28663
 
        h = -1;
28664
 
      }
28665
 
      unixLeaveMutex();        
28666
 
    }
28667
 
  }
28668
 
#endif
28669
 
 
28670
 
  else if( pLockingStyle == &dotlockIoMethods ){
28671
 
    /* Dotfile locking uses the file path so it needs to be included in
28672
 
    ** the dotlockLockingContext 
28673
 
    */
28674
 
    char *zLockFile;
28675
 
    int nFilename;
28676
 
    nFilename = (int)strlen(zFilename) + 6;
28677
 
    zLockFile = (char *)sqlite3_malloc(nFilename);
28678
 
    if( zLockFile==0 ){
28679
 
      rc = SQLITE_NOMEM;
28680
 
    }else{
28681
 
      sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
28682
 
    }
28683
 
    pNew->lockingContext = zLockFile;
28684
 
  }
28685
 
 
28686
 
#if OS_VXWORKS
28687
 
  else if( pLockingStyle == &semIoMethods ){
28688
 
    /* Named semaphore locking uses the file path so it needs to be
28689
 
    ** included in the semLockingContext
28690
 
    */
28691
 
    unixEnterMutex();
28692
 
    rc = findInodeInfo(pNew, &pNew->pInode);
28693
 
    if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
28694
 
      char *zSemName = pNew->pInode->aSemName;
28695
 
      int n;
28696
 
      sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
28697
 
                       pNew->pId->zCanonicalName);
28698
 
      for( n=1; zSemName[n]; n++ )
28699
 
        if( zSemName[n]=='/' ) zSemName[n] = '_';
28700
 
      pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
28701
 
      if( pNew->pInode->pSem == SEM_FAILED ){
28702
 
        rc = SQLITE_NOMEM;
28703
 
        pNew->pInode->aSemName[0] = '\0';
28704
 
      }
28705
 
    }
28706
 
    unixLeaveMutex();
28707
 
  }
28708
 
#endif
28709
 
  
28710
 
  pNew->lastErrno = 0;
28711
 
#if OS_VXWORKS
28712
 
  if( rc!=SQLITE_OK ){
28713
 
    if( h>=0 ) robust_close(pNew, h, __LINE__);
28714
 
    h = -1;
28715
 
    unlink(zFilename);
28716
 
    isDelete = 0;
28717
 
  }
28718
 
  pNew->isDelete = isDelete;
28719
 
#endif
28720
 
  if( rc!=SQLITE_OK ){
28721
 
    if( dirfd>=0 ) robust_close(pNew, dirfd, __LINE__);
28722
 
    if( h>=0 ) robust_close(pNew, h, __LINE__);
28723
 
  }else{
28724
 
    pNew->pMethod = pLockingStyle;
28725
 
    OpenCounter(+1);
28726
 
  }
28727
 
  return rc;
28728
 
}
28729
 
 
28730
 
/*
28731
 
** Open a file descriptor to the directory containing file zFilename.
28732
 
** If successful, *pFd is set to the opened file descriptor and
28733
 
** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
28734
 
** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
28735
 
** value.
28736
 
**
28737
 
** If SQLITE_OK is returned, the caller is responsible for closing
28738
 
** the file descriptor *pFd using close().
28739
 
*/
28740
 
static int openDirectory(const char *zFilename, int *pFd){
28741
 
  int ii;
28742
 
  int fd = -1;
28743
 
  char zDirname[MAX_PATHNAME+1];
28744
 
 
28745
 
  sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
28746
 
  for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
28747
 
  if( ii>0 ){
28748
 
    zDirname[ii] = '\0';
28749
 
    fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
28750
 
    if( fd>=0 ){
28751
 
#ifdef FD_CLOEXEC
28752
 
      osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
28753
 
#endif
28754
 
      OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
28755
 
    }
28756
 
  }
28757
 
  *pFd = fd;
28758
 
  return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
28759
 
}
28760
 
 
28761
 
/*
28762
 
** Return the name of a directory in which to put temporary files.
28763
 
** If no suitable temporary file directory can be found, return NULL.
28764
 
*/
28765
 
static const char *unixTempFileDir(void){
28766
 
  static const char *azDirs[] = {
28767
 
     0,
28768
 
     0,
28769
 
     "/var/tmp",
28770
 
     "/usr/tmp",
28771
 
     "/tmp",
28772
 
     0        /* List terminator */
28773
 
  };
28774
 
  unsigned int i;
28775
 
  struct stat buf;
28776
 
  const char *zDir = 0;
28777
 
 
28778
 
  azDirs[0] = sqlite3_temp_directory;
28779
 
  if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
28780
 
  for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
28781
 
    if( zDir==0 ) continue;
28782
 
    if( osStat(zDir, &buf) ) continue;
28783
 
    if( !S_ISDIR(buf.st_mode) ) continue;
28784
 
    if( osAccess(zDir, 07) ) continue;
28785
 
    break;
28786
 
  }
28787
 
  return zDir;
28788
 
}
28789
 
 
28790
 
/*
28791
 
** Create a temporary file name in zBuf.  zBuf must be allocated
28792
 
** by the calling process and must be big enough to hold at least
28793
 
** pVfs->mxPathname bytes.
28794
 
*/
28795
 
static int unixGetTempname(int nBuf, char *zBuf){
28796
 
  static const unsigned char zChars[] =
28797
 
    "abcdefghijklmnopqrstuvwxyz"
28798
 
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
28799
 
    "0123456789";
28800
 
  unsigned int i, j;
28801
 
  const char *zDir;
28802
 
 
28803
 
  /* It's odd to simulate an io-error here, but really this is just
28804
 
  ** using the io-error infrastructure to test that SQLite handles this
28805
 
  ** function failing. 
28806
 
  */
28807
 
  SimulateIOError( return SQLITE_IOERR );
28808
 
 
28809
 
  zDir = unixTempFileDir();
28810
 
  if( zDir==0 ) zDir = ".";
28811
 
 
28812
 
  /* Check that the output buffer is large enough for the temporary file 
28813
 
  ** name. If it is not, return SQLITE_ERROR.
28814
 
  */
28815
 
  if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
28816
 
    return SQLITE_ERROR;
28817
 
  }
28818
 
 
28819
 
  do{
28820
 
    sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
28821
 
    j = (int)strlen(zBuf);
28822
 
    sqlite3_randomness(15, &zBuf[j]);
28823
 
    for(i=0; i<15; i++, j++){
28824
 
      zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
28825
 
    }
28826
 
    zBuf[j] = 0;
28827
 
  }while( osAccess(zBuf,0)==0 );
28828
 
  return SQLITE_OK;
28829
 
}
28830
 
 
28831
 
#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
28832
 
/*
28833
 
** Routine to transform a unixFile into a proxy-locking unixFile.
28834
 
** Implementation in the proxy-lock division, but used by unixOpen()
28835
 
** if SQLITE_PREFER_PROXY_LOCKING is defined.
28836
 
*/
28837
 
static int proxyTransformUnixFile(unixFile*, const char*);
28838
 
#endif
28839
 
 
28840
 
/*
28841
 
** Search for an unused file descriptor that was opened on the database 
28842
 
** file (not a journal or master-journal file) identified by pathname
28843
 
** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
28844
 
** argument to this function.
28845
 
**
28846
 
** Such a file descriptor may exist if a database connection was closed
28847
 
** but the associated file descriptor could not be closed because some
28848
 
** other file descriptor open on the same file is holding a file-lock.
28849
 
** Refer to comments in the unixClose() function and the lengthy comment
28850
 
** describing "Posix Advisory Locking" at the start of this file for 
28851
 
** further details. Also, ticket #4018.
28852
 
**
28853
 
** If a suitable file descriptor is found, then it is returned. If no
28854
 
** such file descriptor is located, -1 is returned.
28855
 
*/
28856
 
static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
28857
 
  UnixUnusedFd *pUnused = 0;
28858
 
 
28859
 
  /* Do not search for an unused file descriptor on vxworks. Not because
28860
 
  ** vxworks would not benefit from the change (it might, we're not sure),
28861
 
  ** but because no way to test it is currently available. It is better 
28862
 
  ** not to risk breaking vxworks support for the sake of such an obscure 
28863
 
  ** feature.  */
28864
 
#if !OS_VXWORKS
28865
 
  struct stat sStat;                   /* Results of stat() call */
28866
 
 
28867
 
  /* A stat() call may fail for various reasons. If this happens, it is
28868
 
  ** almost certain that an open() call on the same path will also fail.
28869
 
  ** For this reason, if an error occurs in the stat() call here, it is
28870
 
  ** ignored and -1 is returned. The caller will try to open a new file
28871
 
  ** descriptor on the same path, fail, and return an error to SQLite.
28872
 
  **
28873
 
  ** Even if a subsequent open() call does succeed, the consequences of
28874
 
  ** not searching for a resusable file descriptor are not dire.  */
28875
 
  if( 0==stat(zPath, &sStat) ){
28876
 
    unixInodeInfo *pInode;
28877
 
 
28878
 
    unixEnterMutex();
28879
 
    pInode = inodeList;
28880
 
    while( pInode && (pInode->fileId.dev!=sStat.st_dev
28881
 
                     || pInode->fileId.ino!=sStat.st_ino) ){
28882
 
       pInode = pInode->pNext;
28883
 
    }
28884
 
    if( pInode ){
28885
 
      UnixUnusedFd **pp;
28886
 
      for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
28887
 
      pUnused = *pp;
28888
 
      if( pUnused ){
28889
 
        *pp = pUnused->pNext;
28890
 
      }
28891
 
    }
28892
 
    unixLeaveMutex();
28893
 
  }
28894
 
#endif    /* if !OS_VXWORKS */
28895
 
  return pUnused;
28896
 
}
28897
 
 
28898
 
/*
28899
 
** This function is called by unixOpen() to determine the unix permissions
28900
 
** to create new files with. If no error occurs, then SQLITE_OK is returned
28901
 
** and a value suitable for passing as the third argument to open(2) is
28902
 
** written to *pMode. If an IO error occurs, an SQLite error code is 
28903
 
** returned and the value of *pMode is not modified.
28904
 
**
28905
 
** If the file being opened is a temporary file, it is always created with
28906
 
** the octal permissions 0600 (read/writable by owner only). If the file
28907
 
** is a database or master journal file, it is created with the permissions 
28908
 
** mask SQLITE_DEFAULT_FILE_PERMISSIONS.
28909
 
**
28910
 
** Finally, if the file being opened is a WAL or regular journal file, then 
28911
 
** this function queries the file-system for the permissions on the 
28912
 
** corresponding database file and sets *pMode to this value. Whenever 
28913
 
** possible, WAL and journal files are created using the same permissions 
28914
 
** as the associated database file.
28915
 
*/
28916
 
static int findCreateFileMode(
28917
 
  const char *zPath,              /* Path of file (possibly) being created */
28918
 
  int flags,                      /* Flags passed as 4th argument to xOpen() */
28919
 
  mode_t *pMode                   /* OUT: Permissions to open file with */
28920
 
){
28921
 
  int rc = SQLITE_OK;             /* Return Code */
28922
 
  if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
28923
 
    char zDb[MAX_PATHNAME+1];     /* Database file path */
28924
 
    int nDb;                      /* Number of valid bytes in zDb */
28925
 
    struct stat sStat;            /* Output of stat() on database file */
28926
 
 
28927
 
    /* zPath is a path to a WAL or journal file. The following block derives
28928
 
    ** the path to the associated database file from zPath. This block handles
28929
 
    ** the following naming conventions:
28930
 
    **
28931
 
    **   "<path to db>-journal"
28932
 
    **   "<path to db>-wal"
28933
 
    **   "<path to db>-journal-NNNN"
28934
 
    **   "<path to db>-wal-NNNN"
28935
 
    **
28936
 
    ** where NNNN is a 4 digit decimal number. The NNNN naming schemes are 
28937
 
    ** used by the test_multiplex.c module.
28938
 
    */
28939
 
    nDb = sqlite3Strlen30(zPath) - 1; 
28940
 
    while( nDb>0 && zPath[nDb]!='l' ) nDb--;
28941
 
    nDb -= ((flags & SQLITE_OPEN_WAL) ? 3 : 7);
28942
 
    memcpy(zDb, zPath, nDb);
28943
 
    zDb[nDb] = '\0';
28944
 
 
28945
 
    if( 0==stat(zDb, &sStat) ){
28946
 
      *pMode = sStat.st_mode & 0777;
28947
 
    }else{
28948
 
      rc = SQLITE_IOERR_FSTAT;
28949
 
    }
28950
 
  }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
28951
 
    *pMode = 0600;
28952
 
  }else{
28953
 
    *pMode = SQLITE_DEFAULT_FILE_PERMISSIONS;
28954
 
  }
28955
 
  return rc;
28956
 
}
28957
 
 
28958
 
/*
28959
 
** Open the file zPath.
28960
 
** 
28961
 
** Previously, the SQLite OS layer used three functions in place of this
28962
 
** one:
28963
 
**
28964
 
**     sqlite3OsOpenReadWrite();
28965
 
**     sqlite3OsOpenReadOnly();
28966
 
**     sqlite3OsOpenExclusive();
28967
 
**
28968
 
** These calls correspond to the following combinations of flags:
28969
 
**
28970
 
**     ReadWrite() ->     (READWRITE | CREATE)
28971
 
**     ReadOnly()  ->     (READONLY) 
28972
 
**     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
28973
 
**
28974
 
** The old OpenExclusive() accepted a boolean argument - "delFlag". If
28975
 
** true, the file was configured to be automatically deleted when the
28976
 
** file handle closed. To achieve the same effect using this new 
28977
 
** interface, add the DELETEONCLOSE flag to those specified above for 
28978
 
** OpenExclusive().
28979
 
*/
28980
 
static int unixOpen(
28981
 
  sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
28982
 
  const char *zPath,           /* Pathname of file to be opened */
28983
 
  sqlite3_file *pFile,         /* The file descriptor to be filled in */
28984
 
  int flags,                   /* Input flags to control the opening */
28985
 
  int *pOutFlags               /* Output flags returned to SQLite core */
28986
 
){
28987
 
  unixFile *p = (unixFile *)pFile;
28988
 
  int fd = -1;                   /* File descriptor returned by open() */
28989
 
  int dirfd = -1;                /* Directory file descriptor */
28990
 
  int openFlags = 0;             /* Flags to pass to open() */
28991
 
  int eType = flags&0xFFFFFF00;  /* Type of file to open */
28992
 
  int noLock;                    /* True to omit locking primitives */
28993
 
  int rc = SQLITE_OK;            /* Function Return Code */
28994
 
 
28995
 
  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
28996
 
  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
28997
 
  int isCreate     = (flags & SQLITE_OPEN_CREATE);
28998
 
  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
28999
 
  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
29000
 
#if SQLITE_ENABLE_LOCKING_STYLE
29001
 
  int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
29002
 
#endif
29003
 
 
29004
 
  /* If creating a master or main-file journal, this function will open
29005
 
  ** a file-descriptor on the directory too. The first time unixSync()
29006
 
  ** is called the directory file descriptor will be fsync()ed and close()d.
29007
 
  */
29008
 
  int isOpenDirectory = (isCreate && (
29009
 
        eType==SQLITE_OPEN_MASTER_JOURNAL 
29010
 
     || eType==SQLITE_OPEN_MAIN_JOURNAL 
29011
 
     || eType==SQLITE_OPEN_WAL
29012
 
  ));
29013
 
 
29014
 
  /* If argument zPath is a NULL pointer, this function is required to open
29015
 
  ** a temporary file. Use this buffer to store the file name in.
29016
 
  */
29017
 
  char zTmpname[MAX_PATHNAME+1];
29018
 
  const char *zName = zPath;
29019
 
 
29020
 
  /* Check the following statements are true: 
29021
 
  **
29022
 
  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
29023
 
  **   (b) if CREATE is set, then READWRITE must also be set, and
29024
 
  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
29025
 
  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
29026
 
  */
29027
 
  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
29028
 
  assert(isCreate==0 || isReadWrite);
29029
 
  assert(isExclusive==0 || isCreate);
29030
 
  assert(isDelete==0 || isCreate);
29031
 
 
29032
 
  /* The main DB, main journal, WAL file and master journal are never 
29033
 
  ** automatically deleted. Nor are they ever temporary files.  */
29034
 
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
29035
 
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
29036
 
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
29037
 
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
29038
 
 
29039
 
  /* Assert that the upper layer has set one of the "file-type" flags. */
29040
 
  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
29041
 
       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
29042
 
       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
29043
 
       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
29044
 
  );
29045
 
 
29046
 
  memset(p, 0, sizeof(unixFile));
29047
 
 
29048
 
  if( eType==SQLITE_OPEN_MAIN_DB ){
29049
 
    UnixUnusedFd *pUnused;
29050
 
    pUnused = findReusableFd(zName, flags);
29051
 
    if( pUnused ){
29052
 
      fd = pUnused->fd;
29053
 
    }else{
29054
 
      pUnused = sqlite3_malloc(sizeof(*pUnused));
29055
 
      if( !pUnused ){
29056
 
        return SQLITE_NOMEM;
29057
 
      }
29058
 
    }
29059
 
    p->pUnused = pUnused;
29060
 
  }else if( !zName ){
29061
 
    /* If zName is NULL, the upper layer is requesting a temp file. */
29062
 
    assert(isDelete && !isOpenDirectory);
29063
 
    rc = unixGetTempname(MAX_PATHNAME+1, zTmpname);
29064
 
    if( rc!=SQLITE_OK ){
29065
 
      return rc;
29066
 
    }
29067
 
    zName = zTmpname;
29068
 
  }
29069
 
 
29070
 
  /* Determine the value of the flags parameter passed to POSIX function
29071
 
  ** open(). These must be calculated even if open() is not called, as
29072
 
  ** they may be stored as part of the file handle and used by the 
29073
 
  ** 'conch file' locking functions later on.  */
29074
 
  if( isReadonly )  openFlags |= O_RDONLY;
29075
 
  if( isReadWrite ) openFlags |= O_RDWR;
29076
 
  if( isCreate )    openFlags |= O_CREAT;
29077
 
  if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
29078
 
  openFlags |= (O_LARGEFILE|O_BINARY);
29079
 
 
29080
 
  if( fd<0 ){
29081
 
    mode_t openMode;              /* Permissions to create file with */
29082
 
    rc = findCreateFileMode(zName, flags, &openMode);
29083
 
    if( rc!=SQLITE_OK ){
29084
 
      assert( !p->pUnused );
29085
 
      assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
29086
 
      return rc;
29087
 
    }
29088
 
    fd = robust_open(zName, openFlags, openMode);
29089
 
    OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
29090
 
    if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
29091
 
      /* Failed to open the file for read/write access. Try read-only. */
29092
 
      flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
29093
 
      openFlags &= ~(O_RDWR|O_CREAT);
29094
 
      flags |= SQLITE_OPEN_READONLY;
29095
 
      openFlags |= O_RDONLY;
29096
 
      isReadonly = 1;
29097
 
      fd = robust_open(zName, openFlags, openMode);
29098
 
    }
29099
 
    if( fd<0 ){
29100
 
      rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
29101
 
      goto open_finished;
29102
 
    }
29103
 
  }
29104
 
  assert( fd>=0 );
29105
 
  if( pOutFlags ){
29106
 
    *pOutFlags = flags;
29107
 
  }
29108
 
 
29109
 
  if( p->pUnused ){
29110
 
    p->pUnused->fd = fd;
29111
 
    p->pUnused->flags = flags;
29112
 
  }
29113
 
 
29114
 
  if( isDelete ){
29115
 
#if OS_VXWORKS
29116
 
    zPath = zName;
29117
 
#else
29118
 
    unlink(zName);
29119
 
#endif
29120
 
  }
29121
 
#if SQLITE_ENABLE_LOCKING_STYLE
29122
 
  else{
29123
 
    p->openFlags = openFlags;
29124
 
  }
29125
 
#endif
29126
 
 
29127
 
  if( isOpenDirectory ){
29128
 
    rc = openDirectory(zPath, &dirfd);
29129
 
    if( rc!=SQLITE_OK ){
29130
 
      /* It is safe to close fd at this point, because it is guaranteed not
29131
 
      ** to be open on a database file. If it were open on a database file,
29132
 
      ** it would not be safe to close as this would release any locks held
29133
 
      ** on the file by this process.  */
29134
 
      assert( eType!=SQLITE_OPEN_MAIN_DB );
29135
 
      robust_close(p, fd, __LINE__);
29136
 
      goto open_finished;
29137
 
    }
29138
 
  }
29139
 
 
29140
 
#ifdef FD_CLOEXEC
29141
 
  osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
29142
 
#endif
29143
 
 
29144
 
  noLock = eType!=SQLITE_OPEN_MAIN_DB;
29145
 
 
29146
 
  
29147
 
#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29148
 
  struct statfs fsInfo;
29149
 
  if( fstatfs(fd, &fsInfo) == -1 ){
29150
 
    ((unixFile*)pFile)->lastErrno = errno;
29151
 
    if( dirfd>=0 ) robust_close(p, dirfd, __LINE__);
29152
 
    robust_close(p, fd, __LINE__);
29153
 
    return SQLITE_IOERR_ACCESS;
29154
 
  }
29155
 
  if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
29156
 
    ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
29157
 
  }
29158
 
#endif
29159
 
  
29160
 
#if SQLITE_ENABLE_LOCKING_STYLE
29161
 
#if SQLITE_PREFER_PROXY_LOCKING
29162
 
  isAutoProxy = 1;
29163
 
#endif
29164
 
  if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
29165
 
    char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
29166
 
    int useProxy = 0;
29167
 
 
29168
 
    /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means 
29169
 
    ** never use proxy, NULL means use proxy for non-local files only.  */
29170
 
    if( envforce!=NULL ){
29171
 
      useProxy = atoi(envforce)>0;
29172
 
    }else{
29173
 
      struct statfs fsInfo;
29174
 
      if( statfs(zPath, &fsInfo) == -1 ){
29175
 
        /* In theory, the close(fd) call is sub-optimal. If the file opened
29176
 
        ** with fd is a database file, and there are other connections open
29177
 
        ** on that file that are currently holding advisory locks on it,
29178
 
        ** then the call to close() will cancel those locks. In practice,
29179
 
        ** we're assuming that statfs() doesn't fail very often. At least
29180
 
        ** not while other file descriptors opened by the same process on
29181
 
        ** the same file are working.  */
29182
 
        p->lastErrno = errno;
29183
 
        if( dirfd>=0 ){
29184
 
          robust_close(p, dirfd, __LINE__);
29185
 
        }
29186
 
        robust_close(p, fd, __LINE__);
29187
 
        rc = SQLITE_IOERR_ACCESS;
29188
 
        goto open_finished;
29189
 
      }
29190
 
      useProxy = !(fsInfo.f_flags&MNT_LOCAL);
29191
 
    }
29192
 
    if( useProxy ){
29193
 
      rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
29194
 
                          isDelete, isReadonly);
29195
 
      if( rc==SQLITE_OK ){
29196
 
        rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
29197
 
        if( rc!=SQLITE_OK ){
29198
 
          /* Use unixClose to clean up the resources added in fillInUnixFile 
29199
 
          ** and clear all the structure's references.  Specifically, 
29200
 
          ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op 
29201
 
          */
29202
 
          unixClose(pFile);
29203
 
          return rc;
29204
 
        }
29205
 
      }
29206
 
      goto open_finished;
29207
 
    }
29208
 
  }
29209
 
#endif
29210
 
  
29211
 
  rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
29212
 
                      isDelete, isReadonly);
29213
 
open_finished:
29214
 
  if( rc!=SQLITE_OK ){
29215
 
    sqlite3_free(p->pUnused);
29216
 
  }
29217
 
  return rc;
29218
 
}
29219
 
 
29220
 
 
29221
 
/*
29222
 
** Delete the file at zPath. If the dirSync argument is true, fsync()
29223
 
** the directory after deleting the file.
29224
 
*/
29225
 
static int unixDelete(
29226
 
  sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */
29227
 
  const char *zPath,        /* Name of file to be deleted */
29228
 
  int dirSync               /* If true, fsync() directory after deleting file */
29229
 
){
29230
 
  int rc = SQLITE_OK;
29231
 
  UNUSED_PARAMETER(NotUsed);
29232
 
  SimulateIOError(return SQLITE_IOERR_DELETE);
29233
 
  if( unlink(zPath)==(-1) && errno!=ENOENT ){
29234
 
    return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
29235
 
  }
29236
 
#ifndef SQLITE_DISABLE_DIRSYNC
29237
 
  if( dirSync ){
29238
 
    int fd;
29239
 
    rc = openDirectory(zPath, &fd);
29240
 
    if( rc==SQLITE_OK ){
29241
 
#if OS_VXWORKS
29242
 
      if( fsync(fd)==-1 )
29243
 
#else
29244
 
      if( fsync(fd) )
29245
 
#endif
29246
 
      {
29247
 
        rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
29248
 
      }
29249
 
      robust_close(0, fd, __LINE__);
29250
 
    }
29251
 
  }
29252
 
#endif
29253
 
  return rc;
29254
 
}
29255
 
 
29256
 
/*
29257
 
** Test the existance of or access permissions of file zPath. The
29258
 
** test performed depends on the value of flags:
29259
 
**
29260
 
**     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
29261
 
**     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
29262
 
**     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
29263
 
**
29264
 
** Otherwise return 0.
29265
 
*/
29266
 
static int unixAccess(
29267
 
  sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */
29268
 
  const char *zPath,      /* Path of the file to examine */
29269
 
  int flags,              /* What do we want to learn about the zPath file? */
29270
 
  int *pResOut            /* Write result boolean here */
29271
 
){
29272
 
  int amode = 0;
29273
 
  UNUSED_PARAMETER(NotUsed);
29274
 
  SimulateIOError( return SQLITE_IOERR_ACCESS; );
29275
 
  switch( flags ){
29276
 
    case SQLITE_ACCESS_EXISTS:
29277
 
      amode = F_OK;
29278
 
      break;
29279
 
    case SQLITE_ACCESS_READWRITE:
29280
 
      amode = W_OK|R_OK;
29281
 
      break;
29282
 
    case SQLITE_ACCESS_READ:
29283
 
      amode = R_OK;
29284
 
      break;
29285
 
 
29286
 
    default:
29287
 
      assert(!"Invalid flags argument");
29288
 
  }
29289
 
  *pResOut = (osAccess(zPath, amode)==0);
29290
 
  if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
29291
 
    struct stat buf;
29292
 
    if( 0==stat(zPath, &buf) && buf.st_size==0 ){
29293
 
      *pResOut = 0;
29294
 
    }
29295
 
  }
29296
 
  return SQLITE_OK;
29297
 
}
29298
 
 
29299
 
 
29300
 
/*
29301
 
** Turn a relative pathname into a full pathname. The relative path
29302
 
** is stored as a nul-terminated string in the buffer pointed to by
29303
 
** zPath. 
29304
 
**
29305
 
** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes 
29306
 
** (in this case, MAX_PATHNAME bytes). The full-path is written to
29307
 
** this buffer before returning.
29308
 
*/
29309
 
static int unixFullPathname(
29310
 
  sqlite3_vfs *pVfs,            /* Pointer to vfs object */
29311
 
  const char *zPath,            /* Possibly relative input path */
29312
 
  int nOut,                     /* Size of output buffer in bytes */
29313
 
  char *zOut                    /* Output buffer */
29314
 
){
29315
 
 
29316
 
  /* It's odd to simulate an io-error here, but really this is just
29317
 
  ** using the io-error infrastructure to test that SQLite handles this
29318
 
  ** function failing. This function could fail if, for example, the
29319
 
  ** current working directory has been unlinked.
29320
 
  */
29321
 
  SimulateIOError( return SQLITE_ERROR );
29322
 
 
29323
 
  assert( pVfs->mxPathname==MAX_PATHNAME );
29324
 
  UNUSED_PARAMETER(pVfs);
29325
 
 
29326
 
  zOut[nOut-1] = '\0';
29327
 
  if( zPath[0]=='/' ){
29328
 
    sqlite3_snprintf(nOut, zOut, "%s", zPath);
29329
 
  }else{
29330
 
    int nCwd;
29331
 
    if( osGetcwd(zOut, nOut-1)==0 ){
29332
 
      return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
29333
 
    }
29334
 
    nCwd = (int)strlen(zOut);
29335
 
    sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
29336
 
  }
29337
 
  return SQLITE_OK;
29338
 
}
29339
 
 
29340
 
 
29341
 
#ifndef SQLITE_OMIT_LOAD_EXTENSION
29342
 
/*
29343
 
** Interfaces for opening a shared library, finding entry points
29344
 
** within the shared library, and closing the shared library.
29345
 
*/
29346
 
#include <dlfcn.h>
29347
 
static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
29348
 
  UNUSED_PARAMETER(NotUsed);
29349
 
  return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
29350
 
}
29351
 
 
29352
 
/*
29353
 
** SQLite calls this function immediately after a call to unixDlSym() or
29354
 
** unixDlOpen() fails (returns a null pointer). If a more detailed error
29355
 
** message is available, it is written to zBufOut. If no error message
29356
 
** is available, zBufOut is left unmodified and SQLite uses a default
29357
 
** error message.
29358
 
*/
29359
 
static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
29360
 
  const char *zErr;
29361
 
  UNUSED_PARAMETER(NotUsed);
29362
 
  unixEnterMutex();
29363
 
  zErr = dlerror();
29364
 
  if( zErr ){
29365
 
    sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
29366
 
  }
29367
 
  unixLeaveMutex();
29368
 
}
29369
 
static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
29370
 
  /* 
29371
 
  ** GCC with -pedantic-errors says that C90 does not allow a void* to be
29372
 
  ** cast into a pointer to a function.  And yet the library dlsym() routine
29373
 
  ** returns a void* which is really a pointer to a function.  So how do we
29374
 
  ** use dlsym() with -pedantic-errors?
29375
 
  **
29376
 
  ** Variable x below is defined to be a pointer to a function taking
29377
 
  ** parameters void* and const char* and returning a pointer to a function.
29378
 
  ** We initialize x by assigning it a pointer to the dlsym() function.
29379
 
  ** (That assignment requires a cast.)  Then we call the function that
29380
 
  ** x points to.  
29381
 
  **
29382
 
  ** This work-around is unlikely to work correctly on any system where
29383
 
  ** you really cannot cast a function pointer into void*.  But then, on the
29384
 
  ** other hand, dlsym() will not work on such a system either, so we have
29385
 
  ** not really lost anything.
29386
 
  */
29387
 
  void (*(*x)(void*,const char*))(void);
29388
 
  UNUSED_PARAMETER(NotUsed);
29389
 
  x = (void(*(*)(void*,const char*))(void))dlsym;
29390
 
  return (*x)(p, zSym);
29391
 
}
29392
 
static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
29393
 
  UNUSED_PARAMETER(NotUsed);
29394
 
  dlclose(pHandle);
29395
 
}
29396
 
#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
29397
 
  #define unixDlOpen  0
29398
 
  #define unixDlError 0
29399
 
  #define unixDlSym   0
29400
 
  #define unixDlClose 0
29401
 
#endif
29402
 
 
29403
 
/*
29404
 
** Write nBuf bytes of random data to the supplied buffer zBuf.
29405
 
*/
29406
 
static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
29407
 
  UNUSED_PARAMETER(NotUsed);
29408
 
  assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
29409
 
 
29410
 
  /* We have to initialize zBuf to prevent valgrind from reporting
29411
 
  ** errors.  The reports issued by valgrind are incorrect - we would
29412
 
  ** prefer that the randomness be increased by making use of the
29413
 
  ** uninitialized space in zBuf - but valgrind errors tend to worry
29414
 
  ** some users.  Rather than argue, it seems easier just to initialize
29415
 
  ** the whole array and silence valgrind, even if that means less randomness
29416
 
  ** in the random seed.
29417
 
  **
29418
 
  ** When testing, initializing zBuf[] to zero is all we do.  That means
29419
 
  ** that we always use the same random number sequence.  This makes the
29420
 
  ** tests repeatable.
29421
 
  */
29422
 
  memset(zBuf, 0, nBuf);
29423
 
#if !defined(SQLITE_TEST)
29424
 
  {
29425
 
    int pid, fd;
29426
 
    fd = robust_open("/dev/urandom", O_RDONLY, 0);
29427
 
    if( fd<0 ){
29428
 
      time_t t;
29429
 
      time(&t);
29430
 
      memcpy(zBuf, &t, sizeof(t));
29431
 
      pid = getpid();
29432
 
      memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
29433
 
      assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
29434
 
      nBuf = sizeof(t) + sizeof(pid);
29435
 
    }else{
29436
 
      do{ nBuf = osRead(fd, zBuf, nBuf); }while( nBuf<0 && errno==EINTR );
29437
 
      robust_close(0, fd, __LINE__);
29438
 
    }
29439
 
  }
29440
 
#endif
29441
 
  return nBuf;
29442
 
}
29443
 
 
29444
 
 
29445
 
/*
29446
 
** Sleep for a little while.  Return the amount of time slept.
29447
 
** The argument is the number of microseconds we want to sleep.
29448
 
** The return value is the number of microseconds of sleep actually
29449
 
** requested from the underlying operating system, a number which
29450
 
** might be greater than or equal to the argument, but not less
29451
 
** than the argument.
29452
 
*/
29453
 
static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
29454
 
#if OS_VXWORKS
29455
 
  struct timespec sp;
29456
 
 
29457
 
  sp.tv_sec = microseconds / 1000000;
29458
 
  sp.tv_nsec = (microseconds % 1000000) * 1000;
29459
 
  nanosleep(&sp, NULL);
29460
 
  UNUSED_PARAMETER(NotUsed);
29461
 
  return microseconds;
29462
 
#elif defined(HAVE_USLEEP) && HAVE_USLEEP
29463
 
  usleep(microseconds);
29464
 
  UNUSED_PARAMETER(NotUsed);
29465
 
  return microseconds;
29466
 
#else
29467
 
  int seconds = (microseconds+999999)/1000000;
29468
 
  sleep(seconds);
29469
 
  UNUSED_PARAMETER(NotUsed);
29470
 
  return seconds*1000000;
29471
 
#endif
29472
 
}
29473
 
 
29474
 
/*
29475
 
** The following variable, if set to a non-zero value, is interpreted as
29476
 
** the number of seconds since 1970 and is used to set the result of
29477
 
** sqlite3OsCurrentTime() during testing.
29478
 
*/
29479
 
#ifdef SQLITE_TEST
29480
 
SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
29481
 
#endif
29482
 
 
29483
 
/*
29484
 
** Find the current time (in Universal Coordinated Time).  Write into *piNow
29485
 
** the current time and date as a Julian Day number times 86_400_000.  In
29486
 
** other words, write into *piNow the number of milliseconds since the Julian
29487
 
** epoch of noon in Greenwich on November 24, 4714 B.C according to the
29488
 
** proleptic Gregorian calendar.
29489
 
**
29490
 
** On success, return 0.  Return 1 if the time and date cannot be found.
29491
 
*/
29492
 
static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
29493
 
  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
29494
 
#if defined(NO_GETTOD)
29495
 
  time_t t;
29496
 
  time(&t);
29497
 
  *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
29498
 
#elif OS_VXWORKS
29499
 
  struct timespec sNow;
29500
 
  clock_gettime(CLOCK_REALTIME, &sNow);
29501
 
  *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
29502
 
#else
29503
 
  struct timeval sNow;
29504
 
  gettimeofday(&sNow, 0);
29505
 
  *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
29506
 
#endif
29507
 
 
29508
 
#ifdef SQLITE_TEST
29509
 
  if( sqlite3_current_time ){
29510
 
    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
29511
 
  }
29512
 
#endif
29513
 
  UNUSED_PARAMETER(NotUsed);
29514
 
  return 0;
29515
 
}
29516
 
 
29517
 
/*
29518
 
** Find the current time (in Universal Coordinated Time).  Write the
29519
 
** current time and date as a Julian Day number into *prNow and
29520
 
** return 0.  Return 1 if the time and date cannot be found.
29521
 
*/
29522
 
static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
29523
 
  sqlite3_int64 i;
29524
 
  UNUSED_PARAMETER(NotUsed);
29525
 
  unixCurrentTimeInt64(0, &i);
29526
 
  *prNow = i/86400000.0;
29527
 
  return 0;
29528
 
}
29529
 
 
29530
 
/*
29531
 
** We added the xGetLastError() method with the intention of providing
29532
 
** better low-level error messages when operating-system problems come up
29533
 
** during SQLite operation.  But so far, none of that has been implemented
29534
 
** in the core.  So this routine is never called.  For now, it is merely
29535
 
** a place-holder.
29536
 
*/
29537
 
static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
29538
 
  UNUSED_PARAMETER(NotUsed);
29539
 
  UNUSED_PARAMETER(NotUsed2);
29540
 
  UNUSED_PARAMETER(NotUsed3);
29541
 
  return 0;
29542
 
}
29543
 
 
29544
 
 
29545
 
/*
29546
 
************************ End of sqlite3_vfs methods ***************************
29547
 
******************************************************************************/
29548
 
 
29549
 
/******************************************************************************
29550
 
************************** Begin Proxy Locking ********************************
29551
 
**
29552
 
** Proxy locking is a "uber-locking-method" in this sense:  It uses the
29553
 
** other locking methods on secondary lock files.  Proxy locking is a
29554
 
** meta-layer over top of the primitive locking implemented above.  For
29555
 
** this reason, the division that implements of proxy locking is deferred
29556
 
** until late in the file (here) after all of the other I/O methods have
29557
 
** been defined - so that the primitive locking methods are available
29558
 
** as services to help with the implementation of proxy locking.
29559
 
**
29560
 
****
29561
 
**
29562
 
** The default locking schemes in SQLite use byte-range locks on the
29563
 
** database file to coordinate safe, concurrent access by multiple readers
29564
 
** and writers [http://sqlite.org/lockingv3.html].  The five file locking
29565
 
** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
29566
 
** as POSIX read & write locks over fixed set of locations (via fsctl),
29567
 
** on AFP and SMB only exclusive byte-range locks are available via fsctl
29568
 
** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
29569
 
** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
29570
 
** address in the shared range is taken for a SHARED lock, the entire
29571
 
** shared range is taken for an EXCLUSIVE lock):
29572
 
**
29573
 
**      PENDING_BYTE        0x40000000                  
29574
 
**      RESERVED_BYTE       0x40000001
29575
 
**      SHARED_RANGE        0x40000002 -> 0x40000200
29576
 
**
29577
 
** This works well on the local file system, but shows a nearly 100x
29578
 
** slowdown in read performance on AFP because the AFP client disables
29579
 
** the read cache when byte-range locks are present.  Enabling the read
29580
 
** cache exposes a cache coherency problem that is present on all OS X
29581
 
** supported network file systems.  NFS and AFP both observe the
29582
 
** close-to-open semantics for ensuring cache coherency
29583
 
** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
29584
 
** address the requirements for concurrent database access by multiple
29585
 
** readers and writers
29586
 
** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
29587
 
**
29588
 
** To address the performance and cache coherency issues, proxy file locking
29589
 
** changes the way database access is controlled by limiting access to a
29590
 
** single host at a time and moving file locks off of the database file
29591
 
** and onto a proxy file on the local file system.  
29592
 
**
29593
 
**
29594
 
** Using proxy locks
29595
 
** -----------------
29596
 
**
29597
 
** C APIs
29598
 
**
29599
 
**  sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
29600
 
**                       <proxy_path> | ":auto:");
29601
 
**  sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
29602
 
**
29603
 
**
29604
 
** SQL pragmas
29605
 
**
29606
 
**  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
29607
 
**  PRAGMA [database.]lock_proxy_file
29608
 
**
29609
 
** Specifying ":auto:" means that if there is a conch file with a matching
29610
 
** host ID in it, the proxy path in the conch file will be used, otherwise
29611
 
** a proxy path based on the user's temp dir
29612
 
** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
29613
 
** actual proxy file name is generated from the name and path of the
29614
 
** database file.  For example:
29615
 
**
29616
 
**       For database path "/Users/me/foo.db" 
29617
 
**       The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
29618
 
**
29619
 
** Once a lock proxy is configured for a database connection, it can not
29620
 
** be removed, however it may be switched to a different proxy path via
29621
 
** the above APIs (assuming the conch file is not being held by another
29622
 
** connection or process). 
29623
 
**
29624
 
**
29625
 
** How proxy locking works
29626
 
** -----------------------
29627
 
**
29628
 
** Proxy file locking relies primarily on two new supporting files: 
29629
 
**
29630
 
**   *  conch file to limit access to the database file to a single host
29631
 
**      at a time
29632
 
**
29633
 
**   *  proxy file to act as a proxy for the advisory locks normally
29634
 
**      taken on the database
29635
 
**
29636
 
** The conch file - to use a proxy file, sqlite must first "hold the conch"
29637
 
** by taking an sqlite-style shared lock on the conch file, reading the
29638
 
** contents and comparing the host's unique host ID (see below) and lock
29639
 
** proxy path against the values stored in the conch.  The conch file is
29640
 
** stored in the same directory as the database file and the file name
29641
 
** is patterned after the database file name as ".<databasename>-conch".
29642
 
** If the conch file does not exist, or it's contents do not match the
29643
 
** host ID and/or proxy path, then the lock is escalated to an exclusive
29644
 
** lock and the conch file contents is updated with the host ID and proxy
29645
 
** path and the lock is downgraded to a shared lock again.  If the conch
29646
 
** is held by another process (with a shared lock), the exclusive lock
29647
 
** will fail and SQLITE_BUSY is returned.
29648
 
**
29649
 
** The proxy file - a single-byte file used for all advisory file locks
29650
 
** normally taken on the database file.   This allows for safe sharing
29651
 
** of the database file for multiple readers and writers on the same
29652
 
** host (the conch ensures that they all use the same local lock file).
29653
 
**
29654
 
** Requesting the lock proxy does not immediately take the conch, it is
29655
 
** only taken when the first request to lock database file is made.  
29656
 
** This matches the semantics of the traditional locking behavior, where
29657
 
** opening a connection to a database file does not take a lock on it.
29658
 
** The shared lock and an open file descriptor are maintained until 
29659
 
** the connection to the database is closed. 
29660
 
**
29661
 
** The proxy file and the lock file are never deleted so they only need
29662
 
** to be created the first time they are used.
29663
 
**
29664
 
** Configuration options
29665
 
** ---------------------
29666
 
**
29667
 
**  SQLITE_PREFER_PROXY_LOCKING
29668
 
**
29669
 
**       Database files accessed on non-local file systems are
29670
 
**       automatically configured for proxy locking, lock files are
29671
 
**       named automatically using the same logic as
29672
 
**       PRAGMA lock_proxy_file=":auto:"
29673
 
**    
29674
 
**  SQLITE_PROXY_DEBUG
29675
 
**
29676
 
**       Enables the logging of error messages during host id file
29677
 
**       retrieval and creation
29678
 
**
29679
 
**  LOCKPROXYDIR
29680
 
**
29681
 
**       Overrides the default directory used for lock proxy files that
29682
 
**       are named automatically via the ":auto:" setting
29683
 
**
29684
 
**  SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
29685
 
**
29686
 
**       Permissions to use when creating a directory for storing the
29687
 
**       lock proxy files, only used when LOCKPROXYDIR is not set.
29688
 
**    
29689
 
**    
29690
 
** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
29691
 
** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
29692
 
** force proxy locking to be used for every database file opened, and 0
29693
 
** will force automatic proxy locking to be disabled for all database
29694
 
** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
29695
 
** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
29696
 
*/
29697
 
 
29698
 
/*
29699
 
** Proxy locking is only available on MacOSX 
29700
 
*/
29701
 
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
29702
 
 
29703
 
/*
29704
 
** The proxyLockingContext has the path and file structures for the remote 
29705
 
** and local proxy files in it
29706
 
*/
29707
 
typedef struct proxyLockingContext proxyLockingContext;
29708
 
struct proxyLockingContext {
29709
 
  unixFile *conchFile;         /* Open conch file */
29710
 
  char *conchFilePath;         /* Name of the conch file */
29711
 
  unixFile *lockProxy;         /* Open proxy lock file */
29712
 
  char *lockProxyPath;         /* Name of the proxy lock file */
29713
 
  char *dbPath;                /* Name of the open file */
29714
 
  int conchHeld;               /* 1 if the conch is held, -1 if lockless */
29715
 
  void *oldLockingContext;     /* Original lockingcontext to restore on close */
29716
 
  sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */
29717
 
};
29718
 
 
29719
 
/* 
29720
 
** The proxy lock file path for the database at dbPath is written into lPath, 
29721
 
** which must point to valid, writable memory large enough for a maxLen length
29722
 
** file path. 
29723
 
*/
29724
 
static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
29725
 
  int len;
29726
 
  int dbLen;
29727
 
  int i;
29728
 
 
29729
 
#ifdef LOCKPROXYDIR
29730
 
  len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
29731
 
#else
29732
 
# ifdef _CS_DARWIN_USER_TEMP_DIR
29733
 
  {
29734
 
    if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
29735
 
      OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
29736
 
               lPath, errno, getpid()));
29737
 
      return SQLITE_IOERR_LOCK;
29738
 
    }
29739
 
    len = strlcat(lPath, "sqliteplocks", maxLen);    
29740
 
  }
29741
 
# else
29742
 
  len = strlcpy(lPath, "/tmp/", maxLen);
29743
 
# endif
29744
 
#endif
29745
 
 
29746
 
  if( lPath[len-1]!='/' ){
29747
 
    len = strlcat(lPath, "/", maxLen);
29748
 
  }
29749
 
  
29750
 
  /* transform the db path to a unique cache name */
29751
 
  dbLen = (int)strlen(dbPath);
29752
 
  for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
29753
 
    char c = dbPath[i];
29754
 
    lPath[i+len] = (c=='/')?'_':c;
29755
 
  }
29756
 
  lPath[i+len]='\0';
29757
 
  strlcat(lPath, ":auto:", maxLen);
29758
 
  OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, getpid()));
29759
 
  return SQLITE_OK;
29760
 
}
29761
 
 
29762
 
/* 
29763
 
 ** Creates the lock file and any missing directories in lockPath
29764
 
 */
29765
 
static int proxyCreateLockPath(const char *lockPath){
29766
 
  int i, len;
29767
 
  char buf[MAXPATHLEN];
29768
 
  int start = 0;
29769
 
  
29770
 
  assert(lockPath!=NULL);
29771
 
  /* try to create all the intermediate directories */
29772
 
  len = (int)strlen(lockPath);
29773
 
  buf[0] = lockPath[0];
29774
 
  for( i=1; i<len; i++ ){
29775
 
    if( lockPath[i] == '/' && (i - start > 0) ){
29776
 
      /* only mkdir if leaf dir != "." or "/" or ".." */
29777
 
      if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') 
29778
 
         || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
29779
 
        buf[i]='\0';
29780
 
        if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
29781
 
          int err=errno;
29782
 
          if( err!=EEXIST ) {
29783
 
            OSTRACE(("CREATELOCKPATH  FAILED creating %s, "
29784
 
                     "'%s' proxy lock path=%s pid=%d\n",
29785
 
                     buf, strerror(err), lockPath, getpid()));
29786
 
            return err;
29787
 
          }
29788
 
        }
29789
 
      }
29790
 
      start=i+1;
29791
 
    }
29792
 
    buf[i] = lockPath[i];
29793
 
  }
29794
 
  OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n", lockPath, getpid()));
29795
 
  return 0;
29796
 
}
29797
 
 
29798
 
/*
29799
 
** Create a new VFS file descriptor (stored in memory obtained from
29800
 
** sqlite3_malloc) and open the file named "path" in the file descriptor.
29801
 
**
29802
 
** The caller is responsible not only for closing the file descriptor
29803
 
** but also for freeing the memory associated with the file descriptor.
29804
 
*/
29805
 
static int proxyCreateUnixFile(
29806
 
    const char *path,        /* path for the new unixFile */
29807
 
    unixFile **ppFile,       /* unixFile created and returned by ref */
29808
 
    int islockfile           /* if non zero missing dirs will be created */
29809
 
) {
29810
 
  int fd = -1;
29811
 
  int dirfd = -1;
29812
 
  unixFile *pNew;
29813
 
  int rc = SQLITE_OK;
29814
 
  int openFlags = O_RDWR | O_CREAT;
29815
 
  sqlite3_vfs dummyVfs;
29816
 
  int terrno = 0;
29817
 
  UnixUnusedFd *pUnused = NULL;
29818
 
 
29819
 
  /* 1. first try to open/create the file
29820
 
  ** 2. if that fails, and this is a lock file (not-conch), try creating
29821
 
  ** the parent directories and then try again.
29822
 
  ** 3. if that fails, try to open the file read-only
29823
 
  ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
29824
 
  */
29825
 
  pUnused = findReusableFd(path, openFlags);
29826
 
  if( pUnused ){
29827
 
    fd = pUnused->fd;
29828
 
  }else{
29829
 
    pUnused = sqlite3_malloc(sizeof(*pUnused));
29830
 
    if( !pUnused ){
29831
 
      return SQLITE_NOMEM;
29832
 
    }
29833
 
  }
29834
 
  if( fd<0 ){
29835
 
    fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
29836
 
    terrno = errno;
29837
 
    if( fd<0 && errno==ENOENT && islockfile ){
29838
 
      if( proxyCreateLockPath(path) == SQLITE_OK ){
29839
 
        fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
29840
 
      }
29841
 
    }
29842
 
  }
29843
 
  if( fd<0 ){
29844
 
    openFlags = O_RDONLY;
29845
 
    fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
29846
 
    terrno = errno;
29847
 
  }
29848
 
  if( fd<0 ){
29849
 
    if( islockfile ){
29850
 
      return SQLITE_BUSY;
29851
 
    }
29852
 
    switch (terrno) {
29853
 
      case EACCES:
29854
 
        return SQLITE_PERM;
29855
 
      case EIO: 
29856
 
        return SQLITE_IOERR_LOCK; /* even though it is the conch */
29857
 
      default:
29858
 
        return SQLITE_CANTOPEN_BKPT;
29859
 
    }
29860
 
  }
29861
 
  
29862
 
  pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew));
29863
 
  if( pNew==NULL ){
29864
 
    rc = SQLITE_NOMEM;
29865
 
    goto end_create_proxy;
29866
 
  }
29867
 
  memset(pNew, 0, sizeof(unixFile));
29868
 
  pNew->openFlags = openFlags;
29869
 
  memset(&dummyVfs, 0, sizeof(dummyVfs));
29870
 
  dummyVfs.pAppData = (void*)&autolockIoFinder;
29871
 
  dummyVfs.zName = "dummy";
29872
 
  pUnused->fd = fd;
29873
 
  pUnused->flags = openFlags;
29874
 
  pNew->pUnused = pUnused;
29875
 
  
29876
 
  rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0, 0);
29877
 
  if( rc==SQLITE_OK ){
29878
 
    *ppFile = pNew;
29879
 
    return SQLITE_OK;
29880
 
  }
29881
 
end_create_proxy:    
29882
 
  robust_close(pNew, fd, __LINE__);
29883
 
  sqlite3_free(pNew);
29884
 
  sqlite3_free(pUnused);
29885
 
  return rc;
29886
 
}
29887
 
 
29888
 
#ifdef SQLITE_TEST
29889
 
/* simulate multiple hosts by creating unique hostid file paths */
29890
 
SQLITE_API int sqlite3_hostid_num = 0;
29891
 
#endif
29892
 
 
29893
 
#define PROXY_HOSTIDLEN    16  /* conch file host id length */
29894
 
 
29895
 
/* Not always defined in the headers as it ought to be */
29896
 
extern int gethostuuid(uuid_t id, const struct timespec *wait);
29897
 
 
29898
 
/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN 
29899
 
** bytes of writable memory.
29900
 
*/
29901
 
static int proxyGetHostID(unsigned char *pHostID, int *pError){
29902
 
  assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
29903
 
  memset(pHostID, 0, PROXY_HOSTIDLEN);
29904
 
#if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\
29905
 
               && __MAC_OS_X_VERSION_MIN_REQUIRED<1050
29906
 
  {
29907
 
    static const struct timespec timeout = {1, 0}; /* 1 sec timeout */
29908
 
    if( gethostuuid(pHostID, &timeout) ){
29909
 
      int err = errno;
29910
 
      if( pError ){
29911
 
        *pError = err;
29912
 
      }
29913
 
      return SQLITE_IOERR;
29914
 
    }
29915
 
  }
29916
 
#endif
29917
 
#ifdef SQLITE_TEST
29918
 
  /* simulate multiple hosts by creating unique hostid file paths */
29919
 
  if( sqlite3_hostid_num != 0){
29920
 
    pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
29921
 
  }
29922
 
#endif
29923
 
  
29924
 
  return SQLITE_OK;
29925
 
}
29926
 
 
29927
 
/* The conch file contains the header, host id and lock file path
29928
 
 */
29929
 
#define PROXY_CONCHVERSION 2   /* 1-byte header, 16-byte host id, path */
29930
 
#define PROXY_HEADERLEN    1   /* conch file header length */
29931
 
#define PROXY_PATHINDEX    (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
29932
 
#define PROXY_MAXCONCHLEN  (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
29933
 
 
29934
 
/* 
29935
 
** Takes an open conch file, copies the contents to a new path and then moves 
29936
 
** it back.  The newly created file's file descriptor is assigned to the
29937
 
** conch file structure and finally the original conch file descriptor is 
29938
 
** closed.  Returns zero if successful.
29939
 
*/
29940
 
static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
29941
 
  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
29942
 
  unixFile *conchFile = pCtx->conchFile;
29943
 
  char tPath[MAXPATHLEN];
29944
 
  char buf[PROXY_MAXCONCHLEN];
29945
 
  char *cPath = pCtx->conchFilePath;
29946
 
  size_t readLen = 0;
29947
 
  size_t pathLen = 0;
29948
 
  char errmsg[64] = "";
29949
 
  int fd = -1;
29950
 
  int rc = -1;
29951
 
  UNUSED_PARAMETER(myHostID);
29952
 
 
29953
 
  /* create a new path by replace the trailing '-conch' with '-break' */
29954
 
  pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
29955
 
  if( pathLen>MAXPATHLEN || pathLen<6 || 
29956
 
     (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
29957
 
    sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
29958
 
    goto end_breaklock;
29959
 
  }
29960
 
  /* read the conch content */
29961
 
  readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
29962
 
  if( readLen<PROXY_PATHINDEX ){
29963
 
    sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
29964
 
    goto end_breaklock;
29965
 
  }
29966
 
  /* write it out to the temporary break file */
29967
 
  fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL),
29968
 
                   SQLITE_DEFAULT_FILE_PERMISSIONS);
29969
 
  if( fd<0 ){
29970
 
    sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
29971
 
    goto end_breaklock;
29972
 
  }
29973
 
  if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
29974
 
    sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
29975
 
    goto end_breaklock;
29976
 
  }
29977
 
  if( rename(tPath, cPath) ){
29978
 
    sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
29979
 
    goto end_breaklock;
29980
 
  }
29981
 
  rc = 0;
29982
 
  fprintf(stderr, "broke stale lock on %s\n", cPath);
29983
 
  robust_close(pFile, conchFile->h, __LINE__);
29984
 
  conchFile->h = fd;
29985
 
  conchFile->openFlags = O_RDWR | O_CREAT;
29986
 
 
29987
 
end_breaklock:
29988
 
  if( rc ){
29989
 
    if( fd>=0 ){
29990
 
      unlink(tPath);
29991
 
      robust_close(pFile, fd, __LINE__);
29992
 
    }
29993
 
    fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
29994
 
  }
29995
 
  return rc;
29996
 
}
29997
 
 
29998
 
/* Take the requested lock on the conch file and break a stale lock if the 
29999
 
** host id matches.
30000
 
*/
30001
 
static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
30002
 
  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
30003
 
  unixFile *conchFile = pCtx->conchFile;
30004
 
  int rc = SQLITE_OK;
30005
 
  int nTries = 0;
30006
 
  struct timespec conchModTime;
30007
 
  
30008
 
  do {
30009
 
    rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
30010
 
    nTries ++;
30011
 
    if( rc==SQLITE_BUSY ){
30012
 
      /* If the lock failed (busy):
30013
 
       * 1st try: get the mod time of the conch, wait 0.5s and try again. 
30014
 
       * 2nd try: fail if the mod time changed or host id is different, wait 
30015
 
       *           10 sec and try again
30016
 
       * 3rd try: break the lock unless the mod time has changed.
30017
 
       */
30018
 
      struct stat buf;
30019
 
      if( osFstat(conchFile->h, &buf) ){
30020
 
        pFile->lastErrno = errno;
30021
 
        return SQLITE_IOERR_LOCK;
30022
 
      }
30023
 
      
30024
 
      if( nTries==1 ){
30025
 
        conchModTime = buf.st_mtimespec;
30026
 
        usleep(500000); /* wait 0.5 sec and try the lock again*/
30027
 
        continue;  
30028
 
      }
30029
 
 
30030
 
      assert( nTries>1 );
30031
 
      if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || 
30032
 
         conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
30033
 
        return SQLITE_BUSY;
30034
 
      }
30035
 
      
30036
 
      if( nTries==2 ){  
30037
 
        char tBuf[PROXY_MAXCONCHLEN];
30038
 
        int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
30039
 
        if( len<0 ){
30040
 
          pFile->lastErrno = errno;
30041
 
          return SQLITE_IOERR_LOCK;
30042
 
        }
30043
 
        if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
30044
 
          /* don't break the lock if the host id doesn't match */
30045
 
          if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
30046
 
            return SQLITE_BUSY;
30047
 
          }
30048
 
        }else{
30049
 
          /* don't break the lock on short read or a version mismatch */
30050
 
          return SQLITE_BUSY;
30051
 
        }
30052
 
        usleep(10000000); /* wait 10 sec and try the lock again */
30053
 
        continue; 
30054
 
      }
30055
 
      
30056
 
      assert( nTries==3 );
30057
 
      if( 0==proxyBreakConchLock(pFile, myHostID) ){
30058
 
        rc = SQLITE_OK;
30059
 
        if( lockType==EXCLUSIVE_LOCK ){
30060
 
          rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);          
30061
 
        }
30062
 
        if( !rc ){
30063
 
          rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
30064
 
        }
30065
 
      }
30066
 
    }
30067
 
  } while( rc==SQLITE_BUSY && nTries<3 );
30068
 
  
30069
 
  return rc;
30070
 
}
30071
 
 
30072
 
/* Takes the conch by taking a shared lock and read the contents conch, if 
30073
 
** lockPath is non-NULL, the host ID and lock file path must match.  A NULL 
30074
 
** lockPath means that the lockPath in the conch file will be used if the 
30075
 
** host IDs match, or a new lock path will be generated automatically 
30076
 
** and written to the conch file.
30077
 
*/
30078
 
static int proxyTakeConch(unixFile *pFile){
30079
 
  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
30080
 
  
30081
 
  if( pCtx->conchHeld!=0 ){
30082
 
    return SQLITE_OK;
30083
 
  }else{
30084
 
    unixFile *conchFile = pCtx->conchFile;
30085
 
    uuid_t myHostID;
30086
 
    int pError = 0;
30087
 
    char readBuf[PROXY_MAXCONCHLEN];
30088
 
    char lockPath[MAXPATHLEN];
30089
 
    char *tempLockPath = NULL;
30090
 
    int rc = SQLITE_OK;
30091
 
    int createConch = 0;
30092
 
    int hostIdMatch = 0;
30093
 
    int readLen = 0;
30094
 
    int tryOldLockPath = 0;
30095
 
    int forceNewLockPath = 0;
30096
 
    
30097
 
    OSTRACE(("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
30098
 
             (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()));
30099
 
 
30100
 
    rc = proxyGetHostID(myHostID, &pError);
30101
 
    if( (rc&0xff)==SQLITE_IOERR ){
30102
 
      pFile->lastErrno = pError;
30103
 
      goto end_takeconch;
30104
 
    }
30105
 
    rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
30106
 
    if( rc!=SQLITE_OK ){
30107
 
      goto end_takeconch;
30108
 
    }
30109
 
    /* read the existing conch file */
30110
 
    readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
30111
 
    if( readLen<0 ){
30112
 
      /* I/O error: lastErrno set by seekAndRead */
30113
 
      pFile->lastErrno = conchFile->lastErrno;
30114
 
      rc = SQLITE_IOERR_READ;
30115
 
      goto end_takeconch;
30116
 
    }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || 
30117
 
             readBuf[0]!=(char)PROXY_CONCHVERSION ){
30118
 
      /* a short read or version format mismatch means we need to create a new 
30119
 
      ** conch file. 
30120
 
      */
30121
 
      createConch = 1;
30122
 
    }
30123
 
    /* if the host id matches and the lock path already exists in the conch
30124
 
    ** we'll try to use the path there, if we can't open that path, we'll 
30125
 
    ** retry with a new auto-generated path 
30126
 
    */
30127
 
    do { /* in case we need to try again for an :auto: named lock file */
30128
 
 
30129
 
      if( !createConch && !forceNewLockPath ){
30130
 
        hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, 
30131
 
                                  PROXY_HOSTIDLEN);
30132
 
        /* if the conch has data compare the contents */
30133
 
        if( !pCtx->lockProxyPath ){
30134
 
          /* for auto-named local lock file, just check the host ID and we'll
30135
 
           ** use the local lock file path that's already in there
30136
 
           */
30137
 
          if( hostIdMatch ){
30138
 
            size_t pathLen = (readLen - PROXY_PATHINDEX);
30139
 
            
30140
 
            if( pathLen>=MAXPATHLEN ){
30141
 
              pathLen=MAXPATHLEN-1;
30142
 
            }
30143
 
            memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
30144
 
            lockPath[pathLen] = 0;
30145
 
            tempLockPath = lockPath;
30146
 
            tryOldLockPath = 1;
30147
 
            /* create a copy of the lock path if the conch is taken */
30148
 
            goto end_takeconch;
30149
 
          }
30150
 
        }else if( hostIdMatch
30151
 
               && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
30152
 
                           readLen-PROXY_PATHINDEX)
30153
 
        ){
30154
 
          /* conch host and lock path match */
30155
 
          goto end_takeconch; 
30156
 
        }
30157
 
      }
30158
 
      
30159
 
      /* if the conch isn't writable and doesn't match, we can't take it */
30160
 
      if( (conchFile->openFlags&O_RDWR) == 0 ){
30161
 
        rc = SQLITE_BUSY;
30162
 
        goto end_takeconch;
30163
 
      }
30164
 
      
30165
 
      /* either the conch didn't match or we need to create a new one */
30166
 
      if( !pCtx->lockProxyPath ){
30167
 
        proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
30168
 
        tempLockPath = lockPath;
30169
 
        /* create a copy of the lock path _only_ if the conch is taken */
30170
 
      }
30171
 
      
30172
 
      /* update conch with host and path (this will fail if other process
30173
 
      ** has a shared lock already), if the host id matches, use the big
30174
 
      ** stick.
30175
 
      */
30176
 
      futimes(conchFile->h, NULL);
30177
 
      if( hostIdMatch && !createConch ){
30178
 
        if( conchFile->pInode && conchFile->pInode->nShared>1 ){
30179
 
          /* We are trying for an exclusive lock but another thread in this
30180
 
           ** same process is still holding a shared lock. */
30181
 
          rc = SQLITE_BUSY;
30182
 
        } else {          
30183
 
          rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
30184
 
        }
30185
 
      }else{
30186
 
        rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
30187
 
      }
30188
 
      if( rc==SQLITE_OK ){
30189
 
        char writeBuffer[PROXY_MAXCONCHLEN];
30190
 
        int writeSize = 0;
30191
 
        
30192
 
        writeBuffer[0] = (char)PROXY_CONCHVERSION;
30193
 
        memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
30194
 
        if( pCtx->lockProxyPath!=NULL ){
30195
 
          strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN);
30196
 
        }else{
30197
 
          strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
30198
 
        }
30199
 
        writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
30200
 
        robust_ftruncate(conchFile->h, writeSize);
30201
 
        rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
30202
 
        fsync(conchFile->h);
30203
 
        /* If we created a new conch file (not just updated the contents of a 
30204
 
         ** valid conch file), try to match the permissions of the database 
30205
 
         */
30206
 
        if( rc==SQLITE_OK && createConch ){
30207
 
          struct stat buf;
30208
 
          int err = osFstat(pFile->h, &buf);
30209
 
          if( err==0 ){
30210
 
            mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
30211
 
                                        S_IROTH|S_IWOTH);
30212
 
            /* try to match the database file R/W permissions, ignore failure */
30213
 
#ifndef SQLITE_PROXY_DEBUG
30214
 
            osFchmod(conchFile->h, cmode);
30215
 
#else
30216
 
            do{
30217
 
              rc = osFchmod(conchFile->h, cmode);
30218
 
            }while( rc==(-1) && errno==EINTR );
30219
 
            if( rc!=0 ){
30220
 
              int code = errno;
30221
 
              fprintf(stderr, "fchmod %o FAILED with %d %s\n",
30222
 
                      cmode, code, strerror(code));
30223
 
            } else {
30224
 
              fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
30225
 
            }
30226
 
          }else{
30227
 
            int code = errno;
30228
 
            fprintf(stderr, "STAT FAILED[%d] with %d %s\n", 
30229
 
                    err, code, strerror(code));
30230
 
#endif
30231
 
          }
30232
 
        }
30233
 
      }
30234
 
      conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
30235
 
      
30236
 
    end_takeconch:
30237
 
      OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
30238
 
      if( rc==SQLITE_OK && pFile->openFlags ){
30239
 
        if( pFile->h>=0 ){
30240
 
          robust_close(pFile, pFile->h, __LINE__);
30241
 
        }
30242
 
        pFile->h = -1;
30243
 
        int fd = robust_open(pCtx->dbPath, pFile->openFlags,
30244
 
                      SQLITE_DEFAULT_FILE_PERMISSIONS);
30245
 
        OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
30246
 
        if( fd>=0 ){
30247
 
          pFile->h = fd;
30248
 
        }else{
30249
 
          rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
30250
 
           during locking */
30251
 
        }
30252
 
      }
30253
 
      if( rc==SQLITE_OK && !pCtx->lockProxy ){
30254
 
        char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
30255
 
        rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
30256
 
        if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
30257
 
          /* we couldn't create the proxy lock file with the old lock file path
30258
 
           ** so try again via auto-naming 
30259
 
           */
30260
 
          forceNewLockPath = 1;
30261
 
          tryOldLockPath = 0;
30262
 
          continue; /* go back to the do {} while start point, try again */
30263
 
        }
30264
 
      }
30265
 
      if( rc==SQLITE_OK ){
30266
 
        /* Need to make a copy of path if we extracted the value
30267
 
         ** from the conch file or the path was allocated on the stack
30268
 
         */
30269
 
        if( tempLockPath ){
30270
 
          pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
30271
 
          if( !pCtx->lockProxyPath ){
30272
 
            rc = SQLITE_NOMEM;
30273
 
          }
30274
 
        }
30275
 
      }
30276
 
      if( rc==SQLITE_OK ){
30277
 
        pCtx->conchHeld = 1;
30278
 
        
30279
 
        if( pCtx->lockProxy->pMethod == &afpIoMethods ){
30280
 
          afpLockingContext *afpCtx;
30281
 
          afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
30282
 
          afpCtx->dbPath = pCtx->lockProxyPath;
30283
 
        }
30284
 
      } else {
30285
 
        conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
30286
 
      }
30287
 
      OSTRACE(("TAKECONCH  %d %s\n", conchFile->h,
30288
 
               rc==SQLITE_OK?"ok":"failed"));
30289
 
      return rc;
30290
 
    } while (1); /* in case we need to retry the :auto: lock file - 
30291
 
                 ** we should never get here except via the 'continue' call. */
30292
 
  }
30293
 
}
30294
 
 
30295
 
/*
30296
 
** If pFile holds a lock on a conch file, then release that lock.
30297
 
*/
30298
 
static int proxyReleaseConch(unixFile *pFile){
30299
 
  int rc = SQLITE_OK;         /* Subroutine return code */
30300
 
  proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
30301
 
  unixFile *conchFile;        /* Name of the conch file */
30302
 
 
30303
 
  pCtx = (proxyLockingContext *)pFile->lockingContext;
30304
 
  conchFile = pCtx->conchFile;
30305
 
  OSTRACE(("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
30306
 
           (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), 
30307
 
           getpid()));
30308
 
  if( pCtx->conchHeld>0 ){
30309
 
    rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
30310
 
  }
30311
 
  pCtx->conchHeld = 0;
30312
 
  OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
30313
 
           (rc==SQLITE_OK ? "ok" : "failed")));
30314
 
  return rc;
30315
 
}
30316
 
 
30317
 
/*
30318
 
** Given the name of a database file, compute the name of its conch file.
30319
 
** Store the conch filename in memory obtained from sqlite3_malloc().
30320
 
** Make *pConchPath point to the new name.  Return SQLITE_OK on success
30321
 
** or SQLITE_NOMEM if unable to obtain memory.
30322
 
**
30323
 
** The caller is responsible for ensuring that the allocated memory
30324
 
** space is eventually freed.
30325
 
**
30326
 
** *pConchPath is set to NULL if a memory allocation error occurs.
30327
 
*/
30328
 
static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
30329
 
  int i;                        /* Loop counter */
30330
 
  int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
30331
 
  char *conchPath;              /* buffer in which to construct conch name */
30332
 
 
30333
 
  /* Allocate space for the conch filename and initialize the name to
30334
 
  ** the name of the original database file. */  
30335
 
  *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
30336
 
  if( conchPath==0 ){
30337
 
    return SQLITE_NOMEM;
30338
 
  }
30339
 
  memcpy(conchPath, dbPath, len+1);
30340
 
  
30341
 
  /* now insert a "." before the last / character */
30342
 
  for( i=(len-1); i>=0; i-- ){
30343
 
    if( conchPath[i]=='/' ){
30344
 
      i++;
30345
 
      break;
30346
 
    }
30347
 
  }
30348
 
  conchPath[i]='.';
30349
 
  while ( i<len ){
30350
 
    conchPath[i+1]=dbPath[i];
30351
 
    i++;
30352
 
  }
30353
 
 
30354
 
  /* append the "-conch" suffix to the file */
30355
 
  memcpy(&conchPath[i+1], "-conch", 7);
30356
 
  assert( (int)strlen(conchPath) == len+7 );
30357
 
 
30358
 
  return SQLITE_OK;
30359
 
}
30360
 
 
30361
 
 
30362
 
/* Takes a fully configured proxy locking-style unix file and switches
30363
 
** the local lock file path 
30364
 
*/
30365
 
static int switchLockProxyPath(unixFile *pFile, const char *path) {
30366
 
  proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
30367
 
  char *oldPath = pCtx->lockProxyPath;
30368
 
  int rc = SQLITE_OK;
30369
 
 
30370
 
  if( pFile->eFileLock!=NO_LOCK ){
30371
 
    return SQLITE_BUSY;
30372
 
  }  
30373
 
 
30374
 
  /* nothing to do if the path is NULL, :auto: or matches the existing path */
30375
 
  if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
30376
 
    (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
30377
 
    return SQLITE_OK;
30378
 
  }else{
30379
 
    unixFile *lockProxy = pCtx->lockProxy;
30380
 
    pCtx->lockProxy=NULL;
30381
 
    pCtx->conchHeld = 0;
30382
 
    if( lockProxy!=NULL ){
30383
 
      rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
30384
 
      if( rc ) return rc;
30385
 
      sqlite3_free(lockProxy);
30386
 
    }
30387
 
    sqlite3_free(oldPath);
30388
 
    pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
30389
 
  }
30390
 
  
30391
 
  return rc;
30392
 
}
30393
 
 
30394
 
/*
30395
 
** pFile is a file that has been opened by a prior xOpen call.  dbPath
30396
 
** is a string buffer at least MAXPATHLEN+1 characters in size.
30397
 
**
30398
 
** This routine find the filename associated with pFile and writes it
30399
 
** int dbPath.
30400
 
*/
30401
 
static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
30402
 
#if defined(__APPLE__)
30403
 
  if( pFile->pMethod == &afpIoMethods ){
30404
 
    /* afp style keeps a reference to the db path in the filePath field 
30405
 
    ** of the struct */
30406
 
    assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
30407
 
    strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN);
30408
 
  } else
30409
 
#endif
30410
 
  if( pFile->pMethod == &dotlockIoMethods ){
30411
 
    /* dot lock style uses the locking context to store the dot lock
30412
 
    ** file path */
30413
 
    int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
30414
 
    memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
30415
 
  }else{
30416
 
    /* all other styles use the locking context to store the db file path */
30417
 
    assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
30418
 
    strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
30419
 
  }
30420
 
  return SQLITE_OK;
30421
 
}
30422
 
 
30423
 
/*
30424
 
** Takes an already filled in unix file and alters it so all file locking 
30425
 
** will be performed on the local proxy lock file.  The following fields
30426
 
** are preserved in the locking context so that they can be restored and 
30427
 
** the unix structure properly cleaned up at close time:
30428
 
**  ->lockingContext
30429
 
**  ->pMethod
30430
 
*/
30431
 
static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
30432
 
  proxyLockingContext *pCtx;
30433
 
  char dbPath[MAXPATHLEN+1];       /* Name of the database file */
30434
 
  char *lockPath=NULL;
30435
 
  int rc = SQLITE_OK;
30436
 
  
30437
 
  if( pFile->eFileLock!=NO_LOCK ){
30438
 
    return SQLITE_BUSY;
30439
 
  }
30440
 
  proxyGetDbPathForUnixFile(pFile, dbPath);
30441
 
  if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
30442
 
    lockPath=NULL;
30443
 
  }else{
30444
 
    lockPath=(char *)path;
30445
 
  }
30446
 
  
30447
 
  OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
30448
 
           (lockPath ? lockPath : ":auto:"), getpid()));
30449
 
 
30450
 
  pCtx = sqlite3_malloc( sizeof(*pCtx) );
30451
 
  if( pCtx==0 ){
30452
 
    return SQLITE_NOMEM;
30453
 
  }
30454
 
  memset(pCtx, 0, sizeof(*pCtx));
30455
 
 
30456
 
  rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
30457
 
  if( rc==SQLITE_OK ){
30458
 
    rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
30459
 
    if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
30460
 
      /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
30461
 
      ** (c) the file system is read-only, then enable no-locking access.
30462
 
      ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
30463
 
      ** that openFlags will have only one of O_RDONLY or O_RDWR.
30464
 
      */
30465
 
      struct statfs fsInfo;
30466
 
      struct stat conchInfo;
30467
 
      int goLockless = 0;
30468
 
 
30469
 
      if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
30470
 
        int err = errno;
30471
 
        if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
30472
 
          goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
30473
 
        }
30474
 
      }
30475
 
      if( goLockless ){
30476
 
        pCtx->conchHeld = -1; /* read only FS/ lockless */
30477
 
        rc = SQLITE_OK;
30478
 
      }
30479
 
    }
30480
 
  }  
30481
 
  if( rc==SQLITE_OK && lockPath ){
30482
 
    pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
30483
 
  }
30484
 
 
30485
 
  if( rc==SQLITE_OK ){
30486
 
    pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
30487
 
    if( pCtx->dbPath==NULL ){
30488
 
      rc = SQLITE_NOMEM;
30489
 
    }
30490
 
  }
30491
 
  if( rc==SQLITE_OK ){
30492
 
    /* all memory is allocated, proxys are created and assigned, 
30493
 
    ** switch the locking context and pMethod then return.
30494
 
    */
30495
 
    pCtx->oldLockingContext = pFile->lockingContext;
30496
 
    pFile->lockingContext = pCtx;
30497
 
    pCtx->pOldMethod = pFile->pMethod;
30498
 
    pFile->pMethod = &proxyIoMethods;
30499
 
  }else{
30500
 
    if( pCtx->conchFile ){ 
30501
 
      pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
30502
 
      sqlite3_free(pCtx->conchFile);
30503
 
    }
30504
 
    sqlite3DbFree(0, pCtx->lockProxyPath);
30505
 
    sqlite3_free(pCtx->conchFilePath); 
30506
 
    sqlite3_free(pCtx);
30507
 
  }
30508
 
  OSTRACE(("TRANSPROXY  %d %s\n", pFile->h,
30509
 
           (rc==SQLITE_OK ? "ok" : "failed")));
30510
 
  return rc;
30511
 
}
30512
 
 
30513
 
 
30514
 
/*
30515
 
** This routine handles sqlite3_file_control() calls that are specific
30516
 
** to proxy locking.
30517
 
*/
30518
 
static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
30519
 
  switch( op ){
30520
 
    case SQLITE_GET_LOCKPROXYFILE: {
30521
 
      unixFile *pFile = (unixFile*)id;
30522
 
      if( pFile->pMethod == &proxyIoMethods ){
30523
 
        proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
30524
 
        proxyTakeConch(pFile);
30525
 
        if( pCtx->lockProxyPath ){
30526
 
          *(const char **)pArg = pCtx->lockProxyPath;
30527
 
        }else{
30528
 
          *(const char **)pArg = ":auto: (not held)";
30529
 
        }
30530
 
      } else {
30531
 
        *(const char **)pArg = NULL;
30532
 
      }
30533
 
      return SQLITE_OK;
30534
 
    }
30535
 
    case SQLITE_SET_LOCKPROXYFILE: {
30536
 
      unixFile *pFile = (unixFile*)id;
30537
 
      int rc = SQLITE_OK;
30538
 
      int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
30539
 
      if( pArg==NULL || (const char *)pArg==0 ){
30540
 
        if( isProxyStyle ){
30541
 
          /* turn off proxy locking - not supported */
30542
 
          rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
30543
 
        }else{
30544
 
          /* turn off proxy locking - already off - NOOP */
30545
 
          rc = SQLITE_OK;
30546
 
        }
30547
 
      }else{
30548
 
        const char *proxyPath = (const char *)pArg;
30549
 
        if( isProxyStyle ){
30550
 
          proxyLockingContext *pCtx = 
30551
 
            (proxyLockingContext*)pFile->lockingContext;
30552
 
          if( !strcmp(pArg, ":auto:") 
30553
 
           || (pCtx->lockProxyPath &&
30554
 
               !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
30555
 
          ){
30556
 
            rc = SQLITE_OK;
30557
 
          }else{
30558
 
            rc = switchLockProxyPath(pFile, proxyPath);
30559
 
          }
30560
 
        }else{
30561
 
          /* turn on proxy file locking */
30562
 
          rc = proxyTransformUnixFile(pFile, proxyPath);
30563
 
        }
30564
 
      }
30565
 
      return rc;
30566
 
    }
30567
 
    default: {
30568
 
      assert( 0 );  /* The call assures that only valid opcodes are sent */
30569
 
    }
30570
 
  }
30571
 
  /*NOTREACHED*/
30572
 
  return SQLITE_ERROR;
30573
 
}
30574
 
 
30575
 
/*
30576
 
** Within this division (the proxying locking implementation) the procedures
30577
 
** above this point are all utilities.  The lock-related methods of the
30578
 
** proxy-locking sqlite3_io_method object follow.
30579
 
*/
30580
 
 
30581
 
 
30582
 
/*
30583
 
** This routine checks if there is a RESERVED lock held on the specified
30584
 
** file by this or any other process. If such a lock is held, set *pResOut
30585
 
** to a non-zero value otherwise *pResOut is set to zero.  The return value
30586
 
** is set to SQLITE_OK unless an I/O error occurs during lock checking.
30587
 
*/
30588
 
static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
30589
 
  unixFile *pFile = (unixFile*)id;
30590
 
  int rc = proxyTakeConch(pFile);
30591
 
  if( rc==SQLITE_OK ){
30592
 
    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30593
 
    if( pCtx->conchHeld>0 ){
30594
 
      unixFile *proxy = pCtx->lockProxy;
30595
 
      return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
30596
 
    }else{ /* conchHeld < 0 is lockless */
30597
 
      pResOut=0;
30598
 
    }
30599
 
  }
30600
 
  return rc;
30601
 
}
30602
 
 
30603
 
/*
30604
 
** Lock the file with the lock specified by parameter eFileLock - one
30605
 
** of the following:
30606
 
**
30607
 
**     (1) SHARED_LOCK
30608
 
**     (2) RESERVED_LOCK
30609
 
**     (3) PENDING_LOCK
30610
 
**     (4) EXCLUSIVE_LOCK
30611
 
**
30612
 
** Sometimes when requesting one lock state, additional lock states
30613
 
** are inserted in between.  The locking might fail on one of the later
30614
 
** transitions leaving the lock state different from what it started but
30615
 
** still short of its goal.  The following chart shows the allowed
30616
 
** transitions and the inserted intermediate states:
30617
 
**
30618
 
**    UNLOCKED -> SHARED
30619
 
**    SHARED -> RESERVED
30620
 
**    SHARED -> (PENDING) -> EXCLUSIVE
30621
 
**    RESERVED -> (PENDING) -> EXCLUSIVE
30622
 
**    PENDING -> EXCLUSIVE
30623
 
**
30624
 
** This routine will only increase a lock.  Use the sqlite3OsUnlock()
30625
 
** routine to lower a locking level.
30626
 
*/
30627
 
static int proxyLock(sqlite3_file *id, int eFileLock) {
30628
 
  unixFile *pFile = (unixFile*)id;
30629
 
  int rc = proxyTakeConch(pFile);
30630
 
  if( rc==SQLITE_OK ){
30631
 
    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30632
 
    if( pCtx->conchHeld>0 ){
30633
 
      unixFile *proxy = pCtx->lockProxy;
30634
 
      rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
30635
 
      pFile->eFileLock = proxy->eFileLock;
30636
 
    }else{
30637
 
      /* conchHeld < 0 is lockless */
30638
 
    }
30639
 
  }
30640
 
  return rc;
30641
 
}
30642
 
 
30643
 
 
30644
 
/*
30645
 
** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
30646
 
** must be either NO_LOCK or SHARED_LOCK.
30647
 
**
30648
 
** If the locking level of the file descriptor is already at or below
30649
 
** the requested locking level, this routine is a no-op.
30650
 
*/
30651
 
static int proxyUnlock(sqlite3_file *id, int eFileLock) {
30652
 
  unixFile *pFile = (unixFile*)id;
30653
 
  int rc = proxyTakeConch(pFile);
30654
 
  if( rc==SQLITE_OK ){
30655
 
    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30656
 
    if( pCtx->conchHeld>0 ){
30657
 
      unixFile *proxy = pCtx->lockProxy;
30658
 
      rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
30659
 
      pFile->eFileLock = proxy->eFileLock;
30660
 
    }else{
30661
 
      /* conchHeld < 0 is lockless */
30662
 
    }
30663
 
  }
30664
 
  return rc;
30665
 
}
30666
 
 
30667
 
/*
30668
 
** Close a file that uses proxy locks.
30669
 
*/
30670
 
static int proxyClose(sqlite3_file *id) {
30671
 
  if( id ){
30672
 
    unixFile *pFile = (unixFile*)id;
30673
 
    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30674
 
    unixFile *lockProxy = pCtx->lockProxy;
30675
 
    unixFile *conchFile = pCtx->conchFile;
30676
 
    int rc = SQLITE_OK;
30677
 
    
30678
 
    if( lockProxy ){
30679
 
      rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
30680
 
      if( rc ) return rc;
30681
 
      rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
30682
 
      if( rc ) return rc;
30683
 
      sqlite3_free(lockProxy);
30684
 
      pCtx->lockProxy = 0;
30685
 
    }
30686
 
    if( conchFile ){
30687
 
      if( pCtx->conchHeld ){
30688
 
        rc = proxyReleaseConch(pFile);
30689
 
        if( rc ) return rc;
30690
 
      }
30691
 
      rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
30692
 
      if( rc ) return rc;
30693
 
      sqlite3_free(conchFile);
30694
 
    }
30695
 
    sqlite3DbFree(0, pCtx->lockProxyPath);
30696
 
    sqlite3_free(pCtx->conchFilePath);
30697
 
    sqlite3DbFree(0, pCtx->dbPath);
30698
 
    /* restore the original locking context and pMethod then close it */
30699
 
    pFile->lockingContext = pCtx->oldLockingContext;
30700
 
    pFile->pMethod = pCtx->pOldMethod;
30701
 
    sqlite3_free(pCtx);
30702
 
    return pFile->pMethod->xClose(id);
30703
 
  }
30704
 
  return SQLITE_OK;
30705
 
}
30706
 
 
30707
 
 
30708
 
 
30709
 
#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
30710
 
/*
30711
 
** The proxy locking style is intended for use with AFP filesystems.
30712
 
** And since AFP is only supported on MacOSX, the proxy locking is also
30713
 
** restricted to MacOSX.
30714
 
** 
30715
 
**
30716
 
******************* End of the proxy lock implementation **********************
30717
 
******************************************************************************/
30718
 
 
30719
 
/*
30720
 
** Initialize the operating system interface.
30721
 
**
30722
 
** This routine registers all VFS implementations for unix-like operating
30723
 
** systems.  This routine, and the sqlite3_os_end() routine that follows,
30724
 
** should be the only routines in this file that are visible from other
30725
 
** files.
30726
 
**
30727
 
** This routine is called once during SQLite initialization and by a
30728
 
** single thread.  The memory allocation and mutex subsystems have not
30729
 
** necessarily been initialized when this routine is called, and so they
30730
 
** should not be used.
30731
 
*/
30732
 
SQLITE_API int sqlite3_os_init(void){ 
30733
 
  /* 
30734
 
  ** The following macro defines an initializer for an sqlite3_vfs object.
30735
 
  ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer
30736
 
  ** to the "finder" function.  (pAppData is a pointer to a pointer because
30737
 
  ** silly C90 rules prohibit a void* from being cast to a function pointer
30738
 
  ** and so we have to go through the intermediate pointer to avoid problems
30739
 
  ** when compiling with -pedantic-errors on GCC.)
30740
 
  **
30741
 
  ** The FINDER parameter to this macro is the name of the pointer to the
30742
 
  ** finder-function.  The finder-function returns a pointer to the
30743
 
  ** sqlite_io_methods object that implements the desired locking
30744
 
  ** behaviors.  See the division above that contains the IOMETHODS
30745
 
  ** macro for addition information on finder-functions.
30746
 
  **
30747
 
  ** Most finders simply return a pointer to a fixed sqlite3_io_methods
30748
 
  ** object.  But the "autolockIoFinder" available on MacOSX does a little
30749
 
  ** more than that; it looks at the filesystem type that hosts the 
30750
 
  ** database file and tries to choose an locking method appropriate for
30751
 
  ** that filesystem time.
30752
 
  */
30753
 
  #define UNIXVFS(VFSNAME, FINDER) {                        \
30754
 
    3,                    /* iVersion */                    \
30755
 
    sizeof(unixFile),     /* szOsFile */                    \
30756
 
    MAX_PATHNAME,         /* mxPathname */                  \
30757
 
    0,                    /* pNext */                       \
30758
 
    VFSNAME,              /* zName */                       \
30759
 
    (void*)&FINDER,       /* pAppData */                    \
30760
 
    unixOpen,             /* xOpen */                       \
30761
 
    unixDelete,           /* xDelete */                     \
30762
 
    unixAccess,           /* xAccess */                     \
30763
 
    unixFullPathname,     /* xFullPathname */               \
30764
 
    unixDlOpen,           /* xDlOpen */                     \
30765
 
    unixDlError,          /* xDlError */                    \
30766
 
    unixDlSym,            /* xDlSym */                      \
30767
 
    unixDlClose,          /* xDlClose */                    \
30768
 
    unixRandomness,       /* xRandomness */                 \
30769
 
    unixSleep,            /* xSleep */                      \
30770
 
    unixCurrentTime,      /* xCurrentTime */                \
30771
 
    unixGetLastError,     /* xGetLastError */               \
30772
 
    unixCurrentTimeInt64, /* xCurrentTimeInt64 */           \
30773
 
    unixSetSystemCall,    /* xSetSystemCall */              \
30774
 
    unixGetSystemCall,    /* xGetSystemCall */              \
30775
 
    unixNextSystemCall,   /* xNextSystemCall */             \
30776
 
  }
30777
 
 
30778
 
  /*
30779
 
  ** All default VFSes for unix are contained in the following array.
30780
 
  **
30781
 
  ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
30782
 
  ** by the SQLite core when the VFS is registered.  So the following
30783
 
  ** array cannot be const.
30784
 
  */
30785
 
  static sqlite3_vfs aVfs[] = {
30786
 
#if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__))
30787
 
    UNIXVFS("unix",          autolockIoFinder ),
30788
 
#else
30789
 
    UNIXVFS("unix",          posixIoFinder ),
30790
 
#endif
30791
 
    UNIXVFS("unix-none",     nolockIoFinder ),
30792
 
    UNIXVFS("unix-dotfile",  dotlockIoFinder ),
30793
 
    UNIXVFS("unix-excl",     posixIoFinder ),
30794
 
#if OS_VXWORKS
30795
 
    UNIXVFS("unix-namedsem", semIoFinder ),
30796
 
#endif
30797
 
#if SQLITE_ENABLE_LOCKING_STYLE
30798
 
    UNIXVFS("unix-posix",    posixIoFinder ),
30799
 
#if !OS_VXWORKS
30800
 
    UNIXVFS("unix-flock",    flockIoFinder ),
30801
 
#endif
30802
 
#endif
30803
 
#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
30804
 
    UNIXVFS("unix-afp",      afpIoFinder ),
30805
 
    UNIXVFS("unix-nfs",      nfsIoFinder ),
30806
 
    UNIXVFS("unix-proxy",    proxyIoFinder ),
30807
 
#endif
30808
 
  };
30809
 
  unsigned int i;          /* Loop counter */
30810
 
 
30811
 
  /* Double-check that the aSyscall[] array has been constructed
30812
 
  ** correctly.  See ticket [bb3a86e890c8e96ab] */
30813
 
  assert( ArraySize(aSyscall)==16 );
30814
 
 
30815
 
  /* Register all VFSes defined in the aVfs[] array */
30816
 
  for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
30817
 
    sqlite3_vfs_register(&aVfs[i], i==0);
30818
 
  }
30819
 
  return SQLITE_OK; 
30820
 
}
30821
 
 
30822
 
/*
30823
 
** Shutdown the operating system interface.
30824
 
**
30825
 
** Some operating systems might need to do some cleanup in this routine,
30826
 
** to release dynamically allocated objects.  But not on unix.
30827
 
** This routine is a no-op for unix.
30828
 
*/
30829
 
SQLITE_API int sqlite3_os_end(void){ 
30830
 
  return SQLITE_OK; 
30831
 
}
30832
 
 
30833
 
#endif /* SQLITE_OS_UNIX */
30834
 
 
30835
 
/************** End of os_unix.c *********************************************/
30836
 
/************** Begin file os_win.c ******************************************/
30837
 
/*
30838
 
** 2004 May 22
30839
 
**
30840
 
** The author disclaims copyright to this source code.  In place of
30841
 
** a legal notice, here is a blessing:
30842
 
**
30843
 
**    May you do good and not evil.
30844
 
**    May you find forgiveness for yourself and forgive others.
30845
 
**    May you share freely, never taking more than you give.
30846
 
**
30847
 
******************************************************************************
30848
 
**
30849
 
** This file contains code that is specific to windows.
30850
 
*/
30851
 
#if SQLITE_OS_WIN               /* This file is used for windows only */
30852
 
 
30853
 
 
30854
 
/*
30855
 
** A Note About Memory Allocation:
30856
 
**
30857
 
** This driver uses malloc()/free() directly rather than going through
30858
 
** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
30859
 
** are designed for use on embedded systems where memory is scarce and
30860
 
** malloc failures happen frequently.  Win32 does not typically run on
30861
 
** embedded systems, and when it does the developers normally have bigger
30862
 
** problems to worry about than running out of memory.  So there is not
30863
 
** a compelling need to use the wrappers.
30864
 
**
30865
 
** But there is a good reason to not use the wrappers.  If we use the
30866
 
** wrappers then we will get simulated malloc() failures within this
30867
 
** driver.  And that causes all kinds of problems for our tests.  We
30868
 
** could enhance SQLite to deal with simulated malloc failures within
30869
 
** the OS driver, but the code to deal with those failure would not
30870
 
** be exercised on Linux (which does not need to malloc() in the driver)
30871
 
** and so we would have difficulty writing coverage tests for that
30872
 
** code.  Better to leave the code out, we think.
30873
 
**
30874
 
** The point of this discussion is as follows:  When creating a new
30875
 
** OS layer for an embedded system, if you use this file as an example,
30876
 
** avoid the use of malloc()/free().  Those routines work ok on windows
30877
 
** desktops but not so well in embedded systems.
30878
 
*/
30879
 
 
30880
 
#include <winbase.h>
30881
 
 
30882
 
#ifdef __CYGWIN__
30883
 
# include <sys/cygwin.h>
30884
 
#endif
30885
 
 
30886
 
/*
30887
 
** Macros used to determine whether or not to use threads.
30888
 
*/
30889
 
#if defined(THREADSAFE) && THREADSAFE
30890
 
# define SQLITE_W32_THREADS 1
30891
 
#endif
30892
 
 
30893
 
/*
30894
 
** Include code that is common to all os_*.c files
30895
 
*/
30896
 
/************** Include os_common.h in the middle of os_win.c ****************/
30897
 
/************** Begin file os_common.h ***************************************/
30898
 
/*
30899
 
** 2004 May 22
30900
 
**
30901
 
** The author disclaims copyright to this source code.  In place of
30902
 
** a legal notice, here is a blessing:
30903
 
**
30904
 
**    May you do good and not evil.
30905
 
**    May you find forgiveness for yourself and forgive others.
30906
 
**    May you share freely, never taking more than you give.
30907
 
**
30908
 
******************************************************************************
30909
 
**
30910
 
** This file contains macros and a little bit of code that is common to
30911
 
** all of the platform-specific files (os_*.c) and is #included into those
30912
 
** files.
30913
 
**
30914
 
** This file should be #included by the os_*.c files only.  It is not a
30915
 
** general purpose header file.
30916
 
*/
30917
 
#ifndef _OS_COMMON_H_
30918
 
#define _OS_COMMON_H_
30919
 
 
30920
 
/*
30921
 
** At least two bugs have slipped in because we changed the MEMORY_DEBUG
30922
 
** macro to SQLITE_DEBUG and some older makefiles have not yet made the
30923
 
** switch.  The following code should catch this problem at compile-time.
30924
 
*/
30925
 
#ifdef MEMORY_DEBUG
30926
 
# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
30927
 
#endif
30928
 
 
30929
 
#ifdef SQLITE_DEBUG
30930
 
SQLITE_PRIVATE int sqlite3OSTrace = 0;
30931
 
#define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
30932
 
#else
30933
 
#define OSTRACE(X)
30934
 
#endif
30935
 
 
30936
 
/*
30937
 
** Macros for performance tracing.  Normally turned off.  Only works
30938
 
** on i486 hardware.
30939
 
*/
30940
 
#ifdef SQLITE_PERFORMANCE_TRACE
30941
 
 
30942
 
/* 
30943
 
** hwtime.h contains inline assembler code for implementing 
30944
 
** high-performance timing routines.
30945
 
*/
30946
 
/************** Include hwtime.h in the middle of os_common.h ****************/
30947
 
/************** Begin file hwtime.h ******************************************/
30948
 
/*
30949
 
** 2008 May 27
30950
 
**
30951
 
** The author disclaims copyright to this source code.  In place of
30952
 
** a legal notice, here is a blessing:
30953
 
**
30954
 
**    May you do good and not evil.
30955
 
**    May you find forgiveness for yourself and forgive others.
30956
 
**    May you share freely, never taking more than you give.
30957
 
**
30958
 
******************************************************************************
30959
 
**
30960
 
** This file contains inline asm code for retrieving "high-performance"
30961
 
** counters for x86 class CPUs.
30962
 
*/
30963
 
#ifndef _HWTIME_H_
30964
 
#define _HWTIME_H_
30965
 
 
30966
 
/*
30967
 
** The following routine only works on pentium-class (or newer) processors.
30968
 
** It uses the RDTSC opcode to read the cycle count value out of the
30969
 
** processor and returns that value.  This can be used for high-res
30970
 
** profiling.
30971
 
*/
30972
 
#if (defined(__GNUC__) || defined(_MSC_VER)) && \
30973
 
      (defined(i386) || defined(__i386__) || defined(_M_IX86))
30974
 
 
30975
 
  #if defined(__GNUC__)
30976
 
 
30977
 
  __inline__ sqlite_uint64 sqlite3Hwtime(void){
30978
 
     unsigned int lo, hi;
30979
 
     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
30980
 
     return (sqlite_uint64)hi << 32 | lo;
30981
 
  }
30982
 
 
30983
 
  #elif defined(_MSC_VER)
30984
 
 
30985
 
  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
30986
 
     __asm {
30987
 
        rdtsc
30988
 
        ret       ; return value at EDX:EAX
30989
 
     }
30990
 
  }
30991
 
 
30992
 
  #endif
30993
 
 
30994
 
#elif (defined(__GNUC__) && defined(__x86_64__))
30995
 
 
30996
 
  __inline__ sqlite_uint64 sqlite3Hwtime(void){
30997
 
      unsigned long val;
30998
 
      __asm__ __volatile__ ("rdtsc" : "=A" (val));
30999
 
      return val;
31000
 
  }
31001
 
 
31002
 
#elif (defined(__GNUC__) && defined(__ppc__))
31003
 
 
31004
 
  __inline__ sqlite_uint64 sqlite3Hwtime(void){
31005
 
      unsigned long long retval;
31006
 
      unsigned long junk;
31007
 
      __asm__ __volatile__ ("\n\
31008
 
          1:      mftbu   %1\n\
31009
 
                  mftb    %L0\n\
31010
 
                  mftbu   %0\n\
31011
 
                  cmpw    %0,%1\n\
31012
 
                  bne     1b"
31013
 
                  : "=r" (retval), "=r" (junk));
31014
 
      return retval;
31015
 
  }
31016
 
 
31017
 
#else
31018
 
 
31019
 
  #error Need implementation of sqlite3Hwtime() for your platform.
31020
 
 
31021
 
  /*
31022
 
  ** To compile without implementing sqlite3Hwtime() for your platform,
31023
 
  ** you can remove the above #error and use the following
31024
 
  ** stub function.  You will lose timing support for many
31025
 
  ** of the debugging and testing utilities, but it should at
31026
 
  ** least compile and run.
31027
 
  */
31028
 
SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
31029
 
 
31030
 
#endif
31031
 
 
31032
 
#endif /* !defined(_HWTIME_H_) */
31033
 
 
31034
 
/************** End of hwtime.h **********************************************/
31035
 
/************** Continuing where we left off in os_common.h ******************/
31036
 
 
31037
 
static sqlite_uint64 g_start;
31038
 
static sqlite_uint64 g_elapsed;
31039
 
#define TIMER_START       g_start=sqlite3Hwtime()
31040
 
#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
31041
 
#define TIMER_ELAPSED     g_elapsed
31042
 
#else
31043
 
#define TIMER_START
31044
 
#define TIMER_END
31045
 
#define TIMER_ELAPSED     ((sqlite_uint64)0)
31046
 
#endif
31047
 
 
31048
 
/*
31049
 
** If we compile with the SQLITE_TEST macro set, then the following block
31050
 
** of code will give us the ability to simulate a disk I/O error.  This
31051
 
** is used for testing the I/O recovery logic.
31052
 
*/
31053
 
#ifdef SQLITE_TEST
31054
 
SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
31055
 
SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
31056
 
SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
31057
 
SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
31058
 
SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
31059
 
SQLITE_API int sqlite3_diskfull_pending = 0;
31060
 
SQLITE_API int sqlite3_diskfull = 0;
31061
 
#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
31062
 
#define SimulateIOError(CODE)  \
31063
 
  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
31064
 
       || sqlite3_io_error_pending-- == 1 )  \
31065
 
              { local_ioerr(); CODE; }
31066
 
static void local_ioerr(){
31067
 
  IOTRACE(("IOERR\n"));
31068
 
  sqlite3_io_error_hit++;
31069
 
  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
31070
 
}
31071
 
#define SimulateDiskfullError(CODE) \
31072
 
   if( sqlite3_diskfull_pending ){ \
31073
 
     if( sqlite3_diskfull_pending == 1 ){ \
31074
 
       local_ioerr(); \
31075
 
       sqlite3_diskfull = 1; \
31076
 
       sqlite3_io_error_hit = 1; \
31077
 
       CODE; \
31078
 
     }else{ \
31079
 
       sqlite3_diskfull_pending--; \
31080
 
     } \
31081
 
   }
31082
 
#else
31083
 
#define SimulateIOErrorBenign(X)
31084
 
#define SimulateIOError(A)
31085
 
#define SimulateDiskfullError(A)
31086
 
#endif
31087
 
 
31088
 
/*
31089
 
** When testing, keep a count of the number of open files.
31090
 
*/
31091
 
#ifdef SQLITE_TEST
31092
 
SQLITE_API int sqlite3_open_file_count = 0;
31093
 
#define OpenCounter(X)  sqlite3_open_file_count+=(X)
31094
 
#else
31095
 
#define OpenCounter(X)
31096
 
#endif
31097
 
 
31098
 
#endif /* !defined(_OS_COMMON_H_) */
31099
 
 
31100
 
/************** End of os_common.h *******************************************/
31101
 
/************** Continuing where we left off in os_win.c *********************/
31102
 
 
31103
 
/*
31104
 
** Some microsoft compilers lack this definition.
31105
 
*/
31106
 
#ifndef INVALID_FILE_ATTRIBUTES
31107
 
# define INVALID_FILE_ATTRIBUTES ((DWORD)-1) 
31108
 
#endif
31109
 
 
31110
 
/*
31111
 
** Determine if we are dealing with WindowsCE - which has a much
31112
 
** reduced API.
31113
 
*/
31114
 
#if SQLITE_OS_WINCE
31115
 
# define AreFileApisANSI() 1
31116
 
# define FormatMessageW(a,b,c,d,e,f,g) 0
31117
 
#endif
31118
 
 
31119
 
/* Forward references */
31120
 
typedef struct winShm winShm;           /* A connection to shared-memory */
31121
 
typedef struct winShmNode winShmNode;   /* A region of shared-memory */
31122
 
 
31123
 
/*
31124
 
** WinCE lacks native support for file locking so we have to fake it
31125
 
** with some code of our own.
31126
 
*/
31127
 
#if SQLITE_OS_WINCE
31128
 
typedef struct winceLock {
31129
 
  int nReaders;       /* Number of reader locks obtained */
31130
 
  BOOL bPending;      /* Indicates a pending lock has been obtained */
31131
 
  BOOL bReserved;     /* Indicates a reserved lock has been obtained */
31132
 
  BOOL bExclusive;    /* Indicates an exclusive lock has been obtained */
31133
 
} winceLock;
31134
 
#endif
31135
 
 
31136
 
/*
31137
 
** The winFile structure is a subclass of sqlite3_file* specific to the win32
31138
 
** portability layer.
31139
 
*/
31140
 
typedef struct winFile winFile;
31141
 
struct winFile {
31142
 
  const sqlite3_io_methods *pMethod; /*** Must be first ***/
31143
 
  sqlite3_vfs *pVfs;      /* The VFS used to open this file */
31144
 
  HANDLE h;               /* Handle for accessing the file */
31145
 
  unsigned char locktype; /* Type of lock currently held on this file */
31146
 
  short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
31147
 
  DWORD lastErrno;        /* The Windows errno from the last I/O error */
31148
 
  DWORD sectorSize;       /* Sector size of the device file is on */
31149
 
  winShm *pShm;           /* Instance of shared memory on this file */
31150
 
  const char *zPath;      /* Full pathname of this file */
31151
 
  int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
31152
 
#if SQLITE_OS_WINCE
31153
 
  WCHAR *zDeleteOnClose;  /* Name of file to delete when closing */
31154
 
  HANDLE hMutex;          /* Mutex used to control access to shared lock */  
31155
 
  HANDLE hShared;         /* Shared memory segment used for locking */
31156
 
  winceLock local;        /* Locks obtained by this instance of winFile */
31157
 
  winceLock *shared;      /* Global shared lock memory for the file  */
31158
 
#endif
31159
 
};
31160
 
 
31161
 
/*
31162
 
** Forward prototypes.
31163
 
*/
31164
 
static int getSectorSize(
31165
 
    sqlite3_vfs *pVfs,
31166
 
    const char *zRelative     /* UTF-8 file name */
31167
 
);
31168
 
 
31169
 
/*
31170
 
** The following variable is (normally) set once and never changes
31171
 
** thereafter.  It records whether the operating system is Win95
31172
 
** or WinNT.
31173
 
**
31174
 
** 0:   Operating system unknown.
31175
 
** 1:   Operating system is Win95.
31176
 
** 2:   Operating system is WinNT.
31177
 
**
31178
 
** In order to facilitate testing on a WinNT system, the test fixture
31179
 
** can manually set this value to 1 to emulate Win98 behavior.
31180
 
*/
31181
 
#ifdef SQLITE_TEST
31182
 
SQLITE_API int sqlite3_os_type = 0;
31183
 
#else
31184
 
static int sqlite3_os_type = 0;
31185
 
#endif
31186
 
 
31187
 
/*
31188
 
** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
31189
 
** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
31190
 
**
31191
 
** Here is an interesting observation:  Win95, Win98, and WinME lack
31192
 
** the LockFileEx() API.  But we can still statically link against that
31193
 
** API as long as we don't call it when running Win95/98/ME.  A call to
31194
 
** this routine is used to determine if the host is Win95/98/ME or
31195
 
** WinNT/2K/XP so that we will know whether or not we can safely call
31196
 
** the LockFileEx() API.
31197
 
*/
31198
 
#if SQLITE_OS_WINCE
31199
 
# define isNT()  (1)
31200
 
#else
31201
 
  static int isNT(void){
31202
 
    if( sqlite3_os_type==0 ){
31203
 
      OSVERSIONINFO sInfo;
31204
 
      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
31205
 
      GetVersionEx(&sInfo);
31206
 
      sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
31207
 
    }
31208
 
    return sqlite3_os_type==2;
31209
 
  }
31210
 
#endif /* SQLITE_OS_WINCE */
31211
 
 
31212
 
/*
31213
 
** Convert a UTF-8 string to microsoft unicode (UTF-16?). 
31214
 
**
31215
 
** Space to hold the returned string is obtained from malloc.
31216
 
*/
31217
 
static WCHAR *utf8ToUnicode(const char *zFilename){
31218
 
  int nChar;
31219
 
  WCHAR *zWideFilename;
31220
 
 
31221
 
  nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
31222
 
  zWideFilename = malloc( nChar*sizeof(zWideFilename[0]) );
31223
 
  if( zWideFilename==0 ){
31224
 
    return 0;
31225
 
  }
31226
 
  nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar);
31227
 
  if( nChar==0 ){
31228
 
    free(zWideFilename);
31229
 
    zWideFilename = 0;
31230
 
  }
31231
 
  return zWideFilename;
31232
 
}
31233
 
 
31234
 
/*
31235
 
** Convert microsoft unicode to UTF-8.  Space to hold the returned string is
31236
 
** obtained from malloc().
31237
 
*/
31238
 
static char *unicodeToUtf8(const WCHAR *zWideFilename){
31239
 
  int nByte;
31240
 
  char *zFilename;
31241
 
 
31242
 
  nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
31243
 
  zFilename = malloc( nByte );
31244
 
  if( zFilename==0 ){
31245
 
    return 0;
31246
 
  }
31247
 
  nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
31248
 
                              0, 0);
31249
 
  if( nByte == 0 ){
31250
 
    free(zFilename);
31251
 
    zFilename = 0;
31252
 
  }
31253
 
  return zFilename;
31254
 
}
31255
 
 
31256
 
/*
31257
 
** Convert an ansi string to microsoft unicode, based on the
31258
 
** current codepage settings for file apis.
31259
 
** 
31260
 
** Space to hold the returned string is obtained
31261
 
** from malloc.
31262
 
*/
31263
 
static WCHAR *mbcsToUnicode(const char *zFilename){
31264
 
  int nByte;
31265
 
  WCHAR *zMbcsFilename;
31266
 
  int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
31267
 
 
31268
 
  nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR);
31269
 
  zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) );
31270
 
  if( zMbcsFilename==0 ){
31271
 
    return 0;
31272
 
  }
31273
 
  nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte);
31274
 
  if( nByte==0 ){
31275
 
    free(zMbcsFilename);
31276
 
    zMbcsFilename = 0;
31277
 
  }
31278
 
  return zMbcsFilename;
31279
 
}
31280
 
 
31281
 
/*
31282
 
** Convert microsoft unicode to multibyte character string, based on the
31283
 
** user's Ansi codepage.
31284
 
**
31285
 
** Space to hold the returned string is obtained from
31286
 
** malloc().
31287
 
*/
31288
 
static char *unicodeToMbcs(const WCHAR *zWideFilename){
31289
 
  int nByte;
31290
 
  char *zFilename;
31291
 
  int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
31292
 
 
31293
 
  nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
31294
 
  zFilename = malloc( nByte );
31295
 
  if( zFilename==0 ){
31296
 
    return 0;
31297
 
  }
31298
 
  nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte,
31299
 
                              0, 0);
31300
 
  if( nByte == 0 ){
31301
 
    free(zFilename);
31302
 
    zFilename = 0;
31303
 
  }
31304
 
  return zFilename;
31305
 
}
31306
 
 
31307
 
/*
31308
 
** Convert multibyte character string to UTF-8.  Space to hold the
31309
 
** returned string is obtained from malloc().
31310
 
*/
31311
 
SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
31312
 
  char *zFilenameUtf8;
31313
 
  WCHAR *zTmpWide;
31314
 
 
31315
 
  zTmpWide = mbcsToUnicode(zFilename);
31316
 
  if( zTmpWide==0 ){
31317
 
    return 0;
31318
 
  }
31319
 
  zFilenameUtf8 = unicodeToUtf8(zTmpWide);
31320
 
  free(zTmpWide);
31321
 
  return zFilenameUtf8;
31322
 
}
31323
 
 
31324
 
/*
31325
 
** Convert UTF-8 to multibyte character string.  Space to hold the 
31326
 
** returned string is obtained from malloc().
31327
 
*/
31328
 
static char *utf8ToMbcs(const char *zFilename){
31329
 
  char *zFilenameMbcs;
31330
 
  WCHAR *zTmpWide;
31331
 
 
31332
 
  zTmpWide = utf8ToUnicode(zFilename);
31333
 
  if( zTmpWide==0 ){
31334
 
    return 0;
31335
 
  }
31336
 
  zFilenameMbcs = unicodeToMbcs(zTmpWide);
31337
 
  free(zTmpWide);
31338
 
  return zFilenameMbcs;
31339
 
}
31340
 
 
31341
 
#if SQLITE_OS_WINCE
31342
 
/*************************************************************************
31343
 
** This section contains code for WinCE only.
31344
 
*/
31345
 
/*
31346
 
** WindowsCE does not have a localtime() function.  So create a
31347
 
** substitute.
31348
 
*/
31349
 
struct tm *__cdecl localtime(const time_t *t)
31350
 
{
31351
 
  static struct tm y;
31352
 
  FILETIME uTm, lTm;
31353
 
  SYSTEMTIME pTm;
31354
 
  sqlite3_int64 t64;
31355
 
  t64 = *t;
31356
 
  t64 = (t64 + 11644473600)*10000000;
31357
 
  uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
31358
 
  uTm.dwHighDateTime= (DWORD)(t64 >> 32);
31359
 
  FileTimeToLocalFileTime(&uTm,&lTm);
31360
 
  FileTimeToSystemTime(&lTm,&pTm);
31361
 
  y.tm_year = pTm.wYear - 1900;
31362
 
  y.tm_mon = pTm.wMonth - 1;
31363
 
  y.tm_wday = pTm.wDayOfWeek;
31364
 
  y.tm_mday = pTm.wDay;
31365
 
  y.tm_hour = pTm.wHour;
31366
 
  y.tm_min = pTm.wMinute;
31367
 
  y.tm_sec = pTm.wSecond;
31368
 
  return &y;
31369
 
}
31370
 
 
31371
 
/* This will never be called, but defined to make the code compile */
31372
 
#define GetTempPathA(a,b)
31373
 
 
31374
 
#define LockFile(a,b,c,d,e)       winceLockFile(&a, b, c, d, e)
31375
 
#define UnlockFile(a,b,c,d,e)     winceUnlockFile(&a, b, c, d, e)
31376
 
#define LockFileEx(a,b,c,d,e,f)   winceLockFileEx(&a, b, c, d, e, f)
31377
 
 
31378
 
#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
31379
 
 
31380
 
/*
31381
 
** Acquire a lock on the handle h
31382
 
*/
31383
 
static void winceMutexAcquire(HANDLE h){
31384
 
   DWORD dwErr;
31385
 
   do {
31386
 
     dwErr = WaitForSingleObject(h, INFINITE);
31387
 
   } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
31388
 
}
31389
 
/*
31390
 
** Release a lock acquired by winceMutexAcquire()
31391
 
*/
31392
 
#define winceMutexRelease(h) ReleaseMutex(h)
31393
 
 
31394
 
/*
31395
 
** Create the mutex and shared memory used for locking in the file
31396
 
** descriptor pFile
31397
 
*/
31398
 
static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
31399
 
  WCHAR *zTok;
31400
 
  WCHAR *zName = utf8ToUnicode(zFilename);
31401
 
  BOOL bInit = TRUE;
31402
 
 
31403
 
  /* Initialize the local lockdata */
31404
 
  ZeroMemory(&pFile->local, sizeof(pFile->local));
31405
 
 
31406
 
  /* Replace the backslashes from the filename and lowercase it
31407
 
  ** to derive a mutex name. */
31408
 
  zTok = CharLowerW(zName);
31409
 
  for (;*zTok;zTok++){
31410
 
    if (*zTok == '\\') *zTok = '_';
31411
 
  }
31412
 
 
31413
 
  /* Create/open the named mutex */
31414
 
  pFile->hMutex = CreateMutexW(NULL, FALSE, zName);
31415
 
  if (!pFile->hMutex){
31416
 
    pFile->lastErrno = GetLastError();
31417
 
    free(zName);
31418
 
    return FALSE;
31419
 
  }
31420
 
 
31421
 
  /* Acquire the mutex before continuing */
31422
 
  winceMutexAcquire(pFile->hMutex);
31423
 
  
31424
 
  /* Since the names of named mutexes, semaphores, file mappings etc are 
31425
 
  ** case-sensitive, take advantage of that by uppercasing the mutex name
31426
 
  ** and using that as the shared filemapping name.
31427
 
  */
31428
 
  CharUpperW(zName);
31429
 
  pFile->hShared = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
31430
 
                                       PAGE_READWRITE, 0, sizeof(winceLock),
31431
 
                                       zName);  
31432
 
 
31433
 
  /* Set a flag that indicates we're the first to create the memory so it 
31434
 
  ** must be zero-initialized */
31435
 
  if (GetLastError() == ERROR_ALREADY_EXISTS){
31436
 
    bInit = FALSE;
31437
 
  }
31438
 
 
31439
 
  free(zName);
31440
 
 
31441
 
  /* If we succeeded in making the shared memory handle, map it. */
31442
 
  if (pFile->hShared){
31443
 
    pFile->shared = (winceLock*)MapViewOfFile(pFile->hShared, 
31444
 
             FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
31445
 
    /* If mapping failed, close the shared memory handle and erase it */
31446
 
    if (!pFile->shared){
31447
 
      pFile->lastErrno = GetLastError();
31448
 
      CloseHandle(pFile->hShared);
31449
 
      pFile->hShared = NULL;
31450
 
    }
31451
 
  }
31452
 
 
31453
 
  /* If shared memory could not be created, then close the mutex and fail */
31454
 
  if (pFile->hShared == NULL){
31455
 
    winceMutexRelease(pFile->hMutex);
31456
 
    CloseHandle(pFile->hMutex);
31457
 
    pFile->hMutex = NULL;
31458
 
    return FALSE;
31459
 
  }
31460
 
  
31461
 
  /* Initialize the shared memory if we're supposed to */
31462
 
  if (bInit) {
31463
 
    ZeroMemory(pFile->shared, sizeof(winceLock));
31464
 
  }
31465
 
 
31466
 
  winceMutexRelease(pFile->hMutex);
31467
 
  return TRUE;
31468
 
}
31469
 
 
31470
 
/*
31471
 
** Destroy the part of winFile that deals with wince locks
31472
 
*/
31473
 
static void winceDestroyLock(winFile *pFile){
31474
 
  if (pFile->hMutex){
31475
 
    /* Acquire the mutex */
31476
 
    winceMutexAcquire(pFile->hMutex);
31477
 
 
31478
 
    /* The following blocks should probably assert in debug mode, but they
31479
 
       are to cleanup in case any locks remained open */
31480
 
    if (pFile->local.nReaders){
31481
 
      pFile->shared->nReaders --;
31482
 
    }
31483
 
    if (pFile->local.bReserved){
31484
 
      pFile->shared->bReserved = FALSE;
31485
 
    }
31486
 
    if (pFile->local.bPending){
31487
 
      pFile->shared->bPending = FALSE;
31488
 
    }
31489
 
    if (pFile->local.bExclusive){
31490
 
      pFile->shared->bExclusive = FALSE;
31491
 
    }
31492
 
 
31493
 
    /* De-reference and close our copy of the shared memory handle */
31494
 
    UnmapViewOfFile(pFile->shared);
31495
 
    CloseHandle(pFile->hShared);
31496
 
 
31497
 
    /* Done with the mutex */
31498
 
    winceMutexRelease(pFile->hMutex);    
31499
 
    CloseHandle(pFile->hMutex);
31500
 
    pFile->hMutex = NULL;
31501
 
  }
31502
 
}
31503
 
 
31504
 
/* 
31505
 
** An implementation of the LockFile() API of windows for wince
31506
 
*/
31507
 
static BOOL winceLockFile(
31508
 
  HANDLE *phFile,
31509
 
  DWORD dwFileOffsetLow,
31510
 
  DWORD dwFileOffsetHigh,
31511
 
  DWORD nNumberOfBytesToLockLow,
31512
 
  DWORD nNumberOfBytesToLockHigh
31513
 
){
31514
 
  winFile *pFile = HANDLE_TO_WINFILE(phFile);
31515
 
  BOOL bReturn = FALSE;
31516
 
 
31517
 
  UNUSED_PARAMETER(dwFileOffsetHigh);
31518
 
  UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
31519
 
 
31520
 
  if (!pFile->hMutex) return TRUE;
31521
 
  winceMutexAcquire(pFile->hMutex);
31522
 
 
31523
 
  /* Wanting an exclusive lock? */
31524
 
  if (dwFileOffsetLow == (DWORD)SHARED_FIRST
31525
 
       && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
31526
 
    if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
31527
 
       pFile->shared->bExclusive = TRUE;
31528
 
       pFile->local.bExclusive = TRUE;
31529
 
       bReturn = TRUE;
31530
 
    }
31531
 
  }
31532
 
 
31533
 
  /* Want a read-only lock? */
31534
 
  else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
31535
 
           nNumberOfBytesToLockLow == 1){
31536
 
    if (pFile->shared->bExclusive == 0){
31537
 
      pFile->local.nReaders ++;
31538
 
      if (pFile->local.nReaders == 1){
31539
 
        pFile->shared->nReaders ++;
31540
 
      }
31541
 
      bReturn = TRUE;
31542
 
    }
31543
 
  }
31544
 
 
31545
 
  /* Want a pending lock? */
31546
 
  else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToLockLow == 1){
31547
 
    /* If no pending lock has been acquired, then acquire it */
31548
 
    if (pFile->shared->bPending == 0) {
31549
 
      pFile->shared->bPending = TRUE;
31550
 
      pFile->local.bPending = TRUE;
31551
 
      bReturn = TRUE;
31552
 
    }
31553
 
  }
31554
 
 
31555
 
  /* Want a reserved lock? */
31556
 
  else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToLockLow == 1){
31557
 
    if (pFile->shared->bReserved == 0) {
31558
 
      pFile->shared->bReserved = TRUE;
31559
 
      pFile->local.bReserved = TRUE;
31560
 
      bReturn = TRUE;
31561
 
    }
31562
 
  }
31563
 
 
31564
 
  winceMutexRelease(pFile->hMutex);
31565
 
  return bReturn;
31566
 
}
31567
 
 
31568
 
/*
31569
 
** An implementation of the UnlockFile API of windows for wince
31570
 
*/
31571
 
static BOOL winceUnlockFile(
31572
 
  HANDLE *phFile,
31573
 
  DWORD dwFileOffsetLow,
31574
 
  DWORD dwFileOffsetHigh,
31575
 
  DWORD nNumberOfBytesToUnlockLow,
31576
 
  DWORD nNumberOfBytesToUnlockHigh
31577
 
){
31578
 
  winFile *pFile = HANDLE_TO_WINFILE(phFile);
31579
 
  BOOL bReturn = FALSE;
31580
 
 
31581
 
  UNUSED_PARAMETER(dwFileOffsetHigh);
31582
 
  UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
31583
 
 
31584
 
  if (!pFile->hMutex) return TRUE;
31585
 
  winceMutexAcquire(pFile->hMutex);
31586
 
 
31587
 
  /* Releasing a reader lock or an exclusive lock */
31588
 
  if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
31589
 
    /* Did we have an exclusive lock? */
31590
 
    if (pFile->local.bExclusive){
31591
 
      assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
31592
 
      pFile->local.bExclusive = FALSE;
31593
 
      pFile->shared->bExclusive = FALSE;
31594
 
      bReturn = TRUE;
31595
 
    }
31596
 
 
31597
 
    /* Did we just have a reader lock? */
31598
 
    else if (pFile->local.nReaders){
31599
 
      assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE || nNumberOfBytesToUnlockLow == 1);
31600
 
      pFile->local.nReaders --;
31601
 
      if (pFile->local.nReaders == 0)
31602
 
      {
31603
 
        pFile->shared->nReaders --;
31604
 
      }
31605
 
      bReturn = TRUE;
31606
 
    }
31607
 
  }
31608
 
 
31609
 
  /* Releasing a pending lock */
31610
 
  else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){
31611
 
    if (pFile->local.bPending){
31612
 
      pFile->local.bPending = FALSE;
31613
 
      pFile->shared->bPending = FALSE;
31614
 
      bReturn = TRUE;
31615
 
    }
31616
 
  }
31617
 
  /* Releasing a reserved lock */
31618
 
  else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){
31619
 
    if (pFile->local.bReserved) {
31620
 
      pFile->local.bReserved = FALSE;
31621
 
      pFile->shared->bReserved = FALSE;
31622
 
      bReturn = TRUE;
31623
 
    }
31624
 
  }
31625
 
 
31626
 
  winceMutexRelease(pFile->hMutex);
31627
 
  return bReturn;
31628
 
}
31629
 
 
31630
 
/*
31631
 
** An implementation of the LockFileEx() API of windows for wince
31632
 
*/
31633
 
static BOOL winceLockFileEx(
31634
 
  HANDLE *phFile,
31635
 
  DWORD dwFlags,
31636
 
  DWORD dwReserved,
31637
 
  DWORD nNumberOfBytesToLockLow,
31638
 
  DWORD nNumberOfBytesToLockHigh,
31639
 
  LPOVERLAPPED lpOverlapped
31640
 
){
31641
 
  UNUSED_PARAMETER(dwReserved);
31642
 
  UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
31643
 
 
31644
 
  /* If the caller wants a shared read lock, forward this call
31645
 
  ** to winceLockFile */
31646
 
  if (lpOverlapped->Offset == (DWORD)SHARED_FIRST &&
31647
 
      dwFlags == 1 &&
31648
 
      nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
31649
 
    return winceLockFile(phFile, SHARED_FIRST, 0, 1, 0);
31650
 
  }
31651
 
  return FALSE;
31652
 
}
31653
 
/*
31654
 
** End of the special code for wince
31655
 
*****************************************************************************/
31656
 
#endif /* SQLITE_OS_WINCE */
31657
 
 
31658
 
/*****************************************************************************
31659
 
** The next group of routines implement the I/O methods specified
31660
 
** by the sqlite3_io_methods object.
31661
 
******************************************************************************/
31662
 
 
31663
 
/*
31664
 
** Some microsoft compilers lack this definition.
31665
 
*/
31666
 
#ifndef INVALID_SET_FILE_POINTER
31667
 
# define INVALID_SET_FILE_POINTER ((DWORD)-1)
31668
 
#endif
31669
 
 
31670
 
/*
31671
 
** Move the current position of the file handle passed as the first 
31672
 
** argument to offset iOffset within the file. If successful, return 0. 
31673
 
** Otherwise, set pFile->lastErrno and return non-zero.
31674
 
*/
31675
 
static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
31676
 
  LONG upperBits;                 /* Most sig. 32 bits of new offset */
31677
 
  LONG lowerBits;                 /* Least sig. 32 bits of new offset */
31678
 
  DWORD dwRet;                    /* Value returned by SetFilePointer() */
31679
 
 
31680
 
  upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
31681
 
  lowerBits = (LONG)(iOffset & 0xffffffff);
31682
 
 
31683
 
  /* API oddity: If successful, SetFilePointer() returns a dword 
31684
 
  ** containing the lower 32-bits of the new file-offset. Or, if it fails,
31685
 
  ** it returns INVALID_SET_FILE_POINTER. However according to MSDN, 
31686
 
  ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine 
31687
 
  ** whether an error has actually occured, it is also necessary to call 
31688
 
  ** GetLastError().
31689
 
  */
31690
 
  dwRet = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
31691
 
  if( (dwRet==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR) ){
31692
 
    pFile->lastErrno = GetLastError();
31693
 
    return 1;
31694
 
  }
31695
 
 
31696
 
  return 0;
31697
 
}
31698
 
 
31699
 
/*
31700
 
** Close a file.
31701
 
**
31702
 
** It is reported that an attempt to close a handle might sometimes
31703
 
** fail.  This is a very unreasonable result, but windows is notorious
31704
 
** for being unreasonable so I do not doubt that it might happen.  If
31705
 
** the close fails, we pause for 100 milliseconds and try again.  As
31706
 
** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
31707
 
** giving up and returning an error.
31708
 
*/
31709
 
#define MX_CLOSE_ATTEMPT 3
31710
 
static int winClose(sqlite3_file *id){
31711
 
  int rc, cnt = 0;
31712
 
  winFile *pFile = (winFile*)id;
31713
 
 
31714
 
  assert( id!=0 );
31715
 
  assert( pFile->pShm==0 );
31716
 
  OSTRACE(("CLOSE %d\n", pFile->h));
31717
 
  do{
31718
 
    rc = CloseHandle(pFile->h);
31719
 
    /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
31720
 
  }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
31721
 
#if SQLITE_OS_WINCE
31722
 
#define WINCE_DELETION_ATTEMPTS 3
31723
 
  winceDestroyLock(pFile);
31724
 
  if( pFile->zDeleteOnClose ){
31725
 
    int cnt = 0;
31726
 
    while(
31727
 
           DeleteFileW(pFile->zDeleteOnClose)==0
31728
 
        && GetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff 
31729
 
        && cnt++ < WINCE_DELETION_ATTEMPTS
31730
 
    ){
31731
 
       Sleep(100);  /* Wait a little before trying again */
31732
 
    }
31733
 
    free(pFile->zDeleteOnClose);
31734
 
  }
31735
 
#endif
31736
 
  OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
31737
 
  OpenCounter(-1);
31738
 
  return rc ? SQLITE_OK : SQLITE_IOERR;
31739
 
}
31740
 
 
31741
 
/*
31742
 
** Read data from a file into a buffer.  Return SQLITE_OK if all
31743
 
** bytes were read successfully and SQLITE_IOERR if anything goes
31744
 
** wrong.
31745
 
*/
31746
 
static int winRead(
31747
 
  sqlite3_file *id,          /* File to read from */
31748
 
  void *pBuf,                /* Write content into this buffer */
31749
 
  int amt,                   /* Number of bytes to read */
31750
 
  sqlite3_int64 offset       /* Begin reading at this offset */
31751
 
){
31752
 
  winFile *pFile = (winFile*)id;  /* file handle */
31753
 
  DWORD nRead;                    /* Number of bytes actually read from file */
31754
 
 
31755
 
  assert( id!=0 );
31756
 
  SimulateIOError(return SQLITE_IOERR_READ);
31757
 
  OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
31758
 
 
31759
 
  if( seekWinFile(pFile, offset) ){
31760
 
    return SQLITE_FULL;
31761
 
  }
31762
 
  if( !ReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
31763
 
    pFile->lastErrno = GetLastError();
31764
 
    return SQLITE_IOERR_READ;
31765
 
  }
31766
 
  if( nRead<(DWORD)amt ){
31767
 
    /* Unread parts of the buffer must be zero-filled */
31768
 
    memset(&((char*)pBuf)[nRead], 0, amt-nRead);
31769
 
    return SQLITE_IOERR_SHORT_READ;
31770
 
  }
31771
 
 
31772
 
  return SQLITE_OK;
31773
 
}
31774
 
 
31775
 
/*
31776
 
** Write data from a buffer into a file.  Return SQLITE_OK on success
31777
 
** or some other error code on failure.
31778
 
*/
31779
 
static int winWrite(
31780
 
  sqlite3_file *id,               /* File to write into */
31781
 
  const void *pBuf,               /* The bytes to be written */
31782
 
  int amt,                        /* Number of bytes to write */
31783
 
  sqlite3_int64 offset            /* Offset into the file to begin writing at */
31784
 
){
31785
 
  int rc;                         /* True if error has occured, else false */
31786
 
  winFile *pFile = (winFile*)id;  /* File handle */
31787
 
 
31788
 
  assert( amt>0 );
31789
 
  assert( pFile );
31790
 
  SimulateIOError(return SQLITE_IOERR_WRITE);
31791
 
  SimulateDiskfullError(return SQLITE_FULL);
31792
 
 
31793
 
  OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
31794
 
 
31795
 
  rc = seekWinFile(pFile, offset);
31796
 
  if( rc==0 ){
31797
 
    u8 *aRem = (u8 *)pBuf;        /* Data yet to be written */
31798
 
    int nRem = amt;               /* Number of bytes yet to be written */
31799
 
    DWORD nWrite;                 /* Bytes written by each WriteFile() call */
31800
 
 
31801
 
    while( nRem>0 && WriteFile(pFile->h, aRem, nRem, &nWrite, 0) && nWrite>0 ){
31802
 
      aRem += nWrite;
31803
 
      nRem -= nWrite;
31804
 
    }
31805
 
    if( nRem>0 ){
31806
 
      pFile->lastErrno = GetLastError();
31807
 
      rc = 1;
31808
 
    }
31809
 
  }
31810
 
 
31811
 
  if( rc ){
31812
 
    if( pFile->lastErrno==ERROR_HANDLE_DISK_FULL ){
31813
 
      return SQLITE_FULL;
31814
 
    }
31815
 
    return SQLITE_IOERR_WRITE;
31816
 
  }
31817
 
  return SQLITE_OK;
31818
 
}
31819
 
 
31820
 
/*
31821
 
** Truncate an open file to a specified size
31822
 
*/
31823
 
static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
31824
 
  winFile *pFile = (winFile*)id;  /* File handle object */
31825
 
  int rc = SQLITE_OK;             /* Return code for this function */
31826
 
 
31827
 
  assert( pFile );
31828
 
 
31829
 
  OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
31830
 
  SimulateIOError(return SQLITE_IOERR_TRUNCATE);
31831
 
 
31832
 
  /* If the user has configured a chunk-size for this file, truncate the
31833
 
  ** file so that it consists of an integer number of chunks (i.e. the
31834
 
  ** actual file size after the operation may be larger than the requested
31835
 
  ** size).
31836
 
  */
31837
 
  if( pFile->szChunk ){
31838
 
    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
31839
 
  }
31840
 
 
31841
 
  /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
31842
 
  if( seekWinFile(pFile, nByte) ){
31843
 
    rc = SQLITE_IOERR_TRUNCATE;
31844
 
  }else if( 0==SetEndOfFile(pFile->h) ){
31845
 
    pFile->lastErrno = GetLastError();
31846
 
    rc = SQLITE_IOERR_TRUNCATE;
31847
 
  }
31848
 
 
31849
 
  OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
31850
 
  return rc;
31851
 
}
31852
 
 
31853
 
#ifdef SQLITE_TEST
31854
 
/*
31855
 
** Count the number of fullsyncs and normal syncs.  This is used to test
31856
 
** that syncs and fullsyncs are occuring at the right times.
31857
 
*/
31858
 
SQLITE_API int sqlite3_sync_count = 0;
31859
 
SQLITE_API int sqlite3_fullsync_count = 0;
31860
 
#endif
31861
 
 
31862
 
/*
31863
 
** Make sure all writes to a particular file are committed to disk.
31864
 
*/
31865
 
static int winSync(sqlite3_file *id, int flags){
31866
 
#if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || defined(SQLITE_DEBUG)
31867
 
  winFile *pFile = (winFile*)id;
31868
 
#else
31869
 
  UNUSED_PARAMETER(id);
31870
 
#endif
31871
 
 
31872
 
  assert( pFile );
31873
 
  /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
31874
 
  assert((flags&0x0F)==SQLITE_SYNC_NORMAL
31875
 
      || (flags&0x0F)==SQLITE_SYNC_FULL
31876
 
  );
31877
 
 
31878
 
  OSTRACE(("SYNC %d lock=%d\n", pFile->h, pFile->locktype));
31879
 
 
31880
 
#ifndef SQLITE_TEST
31881
 
  UNUSED_PARAMETER(flags);
31882
 
#else
31883
 
  if( flags & SQLITE_SYNC_FULL ){
31884
 
    sqlite3_fullsync_count++;
31885
 
  }
31886
 
  sqlite3_sync_count++;
31887
 
#endif
31888
 
 
31889
 
  /* Unix cannot, but some systems may return SQLITE_FULL from here. This
31890
 
  ** line is to test that doing so does not cause any problems.
31891
 
  */
31892
 
  SimulateDiskfullError( return SQLITE_FULL );
31893
 
  SimulateIOError( return SQLITE_IOERR; );
31894
 
 
31895
 
  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
31896
 
  ** no-op
31897
 
  */
31898
 
#ifdef SQLITE_NO_SYNC
31899
 
  return SQLITE_OK;
31900
 
#else
31901
 
  if( FlushFileBuffers(pFile->h) ){
31902
 
    return SQLITE_OK;
31903
 
  }else{
31904
 
    pFile->lastErrno = GetLastError();
31905
 
    return SQLITE_IOERR;
31906
 
  }
31907
 
#endif
31908
 
}
31909
 
 
31910
 
/*
31911
 
** Determine the current size of a file in bytes
31912
 
*/
31913
 
static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
31914
 
  DWORD upperBits;
31915
 
  DWORD lowerBits;
31916
 
  winFile *pFile = (winFile*)id;
31917
 
  DWORD error;
31918
 
 
31919
 
  assert( id!=0 );
31920
 
  SimulateIOError(return SQLITE_IOERR_FSTAT);
31921
 
  lowerBits = GetFileSize(pFile->h, &upperBits);
31922
 
  if(   (lowerBits == INVALID_FILE_SIZE)
31923
 
     && ((error = GetLastError()) != NO_ERROR) )
31924
 
  {
31925
 
    pFile->lastErrno = error;
31926
 
    return SQLITE_IOERR_FSTAT;
31927
 
  }
31928
 
  *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
31929
 
  return SQLITE_OK;
31930
 
}
31931
 
 
31932
 
/*
31933
 
** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
31934
 
*/
31935
 
#ifndef LOCKFILE_FAIL_IMMEDIATELY
31936
 
# define LOCKFILE_FAIL_IMMEDIATELY 1
31937
 
#endif
31938
 
 
31939
 
/*
31940
 
** Acquire a reader lock.
31941
 
** Different API routines are called depending on whether or not this
31942
 
** is Win95 or WinNT.
31943
 
*/
31944
 
static int getReadLock(winFile *pFile){
31945
 
  int res;
31946
 
  if( isNT() ){
31947
 
    OVERLAPPED ovlp;
31948
 
    ovlp.Offset = SHARED_FIRST;
31949
 
    ovlp.OffsetHigh = 0;
31950
 
    ovlp.hEvent = 0;
31951
 
    res = LockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY,
31952
 
                     0, SHARED_SIZE, 0, &ovlp);
31953
 
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
31954
 
*/
31955
 
#if SQLITE_OS_WINCE==0
31956
 
  }else{
31957
 
    int lk;
31958
 
    sqlite3_randomness(sizeof(lk), &lk);
31959
 
    pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
31960
 
    res = LockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
31961
 
#endif
31962
 
  }
31963
 
  if( res == 0 ){
31964
 
    pFile->lastErrno = GetLastError();
31965
 
  }
31966
 
  return res;
31967
 
}
31968
 
 
31969
 
/*
31970
 
** Undo a readlock
31971
 
*/
31972
 
static int unlockReadLock(winFile *pFile){
31973
 
  int res;
31974
 
  if( isNT() ){
31975
 
    res = UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
31976
 
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
31977
 
*/
31978
 
#if SQLITE_OS_WINCE==0
31979
 
  }else{
31980
 
    res = UnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0);
31981
 
#endif
31982
 
  }
31983
 
  if( res == 0 ){
31984
 
    pFile->lastErrno = GetLastError();
31985
 
  }
31986
 
  return res;
31987
 
}
31988
 
 
31989
 
/*
31990
 
** Lock the file with the lock specified by parameter locktype - one
31991
 
** of the following:
31992
 
**
31993
 
**     (1) SHARED_LOCK
31994
 
**     (2) RESERVED_LOCK
31995
 
**     (3) PENDING_LOCK
31996
 
**     (4) EXCLUSIVE_LOCK
31997
 
**
31998
 
** Sometimes when requesting one lock state, additional lock states
31999
 
** are inserted in between.  The locking might fail on one of the later
32000
 
** transitions leaving the lock state different from what it started but
32001
 
** still short of its goal.  The following chart shows the allowed
32002
 
** transitions and the inserted intermediate states:
32003
 
**
32004
 
**    UNLOCKED -> SHARED
32005
 
**    SHARED -> RESERVED
32006
 
**    SHARED -> (PENDING) -> EXCLUSIVE
32007
 
**    RESERVED -> (PENDING) -> EXCLUSIVE
32008
 
**    PENDING -> EXCLUSIVE
32009
 
**
32010
 
** This routine will only increase a lock.  The winUnlock() routine
32011
 
** erases all locks at once and returns us immediately to locking level 0.
32012
 
** It is not possible to lower the locking level one step at a time.  You
32013
 
** must go straight to locking level 0.
32014
 
*/
32015
 
static int winLock(sqlite3_file *id, int locktype){
32016
 
  int rc = SQLITE_OK;    /* Return code from subroutines */
32017
 
  int res = 1;           /* Result of a windows lock call */
32018
 
  int newLocktype;       /* Set pFile->locktype to this value before exiting */
32019
 
  int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
32020
 
  winFile *pFile = (winFile*)id;
32021
 
  DWORD error = NO_ERROR;
32022
 
 
32023
 
  assert( id!=0 );
32024
 
  OSTRACE(("LOCK %d %d was %d(%d)\n",
32025
 
           pFile->h, locktype, pFile->locktype, pFile->sharedLockByte));
32026
 
 
32027
 
  /* If there is already a lock of this type or more restrictive on the
32028
 
  ** OsFile, do nothing. Don't use the end_lock: exit path, as
32029
 
  ** sqlite3OsEnterMutex() hasn't been called yet.
32030
 
  */
32031
 
  if( pFile->locktype>=locktype ){
32032
 
    return SQLITE_OK;
32033
 
  }
32034
 
 
32035
 
  /* Make sure the locking sequence is correct
32036
 
  */
32037
 
  assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
32038
 
  assert( locktype!=PENDING_LOCK );
32039
 
  assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
32040
 
 
32041
 
  /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
32042
 
  ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
32043
 
  ** the PENDING_LOCK byte is temporary.
32044
 
  */
32045
 
  newLocktype = pFile->locktype;
32046
 
  if(   (pFile->locktype==NO_LOCK)
32047
 
     || (   (locktype==EXCLUSIVE_LOCK)
32048
 
         && (pFile->locktype==RESERVED_LOCK))
32049
 
  ){
32050
 
    int cnt = 3;
32051
 
    while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
32052
 
      /* Try 3 times to get the pending lock.  The pending lock might be
32053
 
      ** held by another reader process who will release it momentarily.
32054
 
      */
32055
 
      OSTRACE(("could not get a PENDING lock. cnt=%d\n", cnt));
32056
 
      Sleep(1);
32057
 
    }
32058
 
    gotPendingLock = res;
32059
 
    if( !res ){
32060
 
      error = GetLastError();
32061
 
    }
32062
 
  }
32063
 
 
32064
 
  /* Acquire a shared lock
32065
 
  */
32066
 
  if( locktype==SHARED_LOCK && res ){
32067
 
    assert( pFile->locktype==NO_LOCK );
32068
 
    res = getReadLock(pFile);
32069
 
    if( res ){
32070
 
      newLocktype = SHARED_LOCK;
32071
 
    }else{
32072
 
      error = GetLastError();
32073
 
    }
32074
 
  }
32075
 
 
32076
 
  /* Acquire a RESERVED lock
32077
 
  */
32078
 
  if( locktype==RESERVED_LOCK && res ){
32079
 
    assert( pFile->locktype==SHARED_LOCK );
32080
 
    res = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
32081
 
    if( res ){
32082
 
      newLocktype = RESERVED_LOCK;
32083
 
    }else{
32084
 
      error = GetLastError();
32085
 
    }
32086
 
  }
32087
 
 
32088
 
  /* Acquire a PENDING lock
32089
 
  */
32090
 
  if( locktype==EXCLUSIVE_LOCK && res ){
32091
 
    newLocktype = PENDING_LOCK;
32092
 
    gotPendingLock = 0;
32093
 
  }
32094
 
 
32095
 
  /* Acquire an EXCLUSIVE lock
32096
 
  */
32097
 
  if( locktype==EXCLUSIVE_LOCK && res ){
32098
 
    assert( pFile->locktype>=SHARED_LOCK );
32099
 
    res = unlockReadLock(pFile);
32100
 
    OSTRACE(("unreadlock = %d\n", res));
32101
 
    res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
32102
 
    if( res ){
32103
 
      newLocktype = EXCLUSIVE_LOCK;
32104
 
    }else{
32105
 
      error = GetLastError();
32106
 
      OSTRACE(("error-code = %d\n", error));
32107
 
      getReadLock(pFile);
32108
 
    }
32109
 
  }
32110
 
 
32111
 
  /* If we are holding a PENDING lock that ought to be released, then
32112
 
  ** release it now.
32113
 
  */
32114
 
  if( gotPendingLock && locktype==SHARED_LOCK ){
32115
 
    UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
32116
 
  }
32117
 
 
32118
 
  /* Update the state of the lock has held in the file descriptor then
32119
 
  ** return the appropriate result code.
32120
 
  */
32121
 
  if( res ){
32122
 
    rc = SQLITE_OK;
32123
 
  }else{
32124
 
    OSTRACE(("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
32125
 
           locktype, newLocktype));
32126
 
    pFile->lastErrno = error;
32127
 
    rc = SQLITE_BUSY;
32128
 
  }
32129
 
  pFile->locktype = (u8)newLocktype;
32130
 
  return rc;
32131
 
}
32132
 
 
32133
 
/*
32134
 
** This routine checks if there is a RESERVED lock held on the specified
32135
 
** file by this or any other process. If such a lock is held, return
32136
 
** non-zero, otherwise zero.
32137
 
*/
32138
 
static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
32139
 
  int rc;
32140
 
  winFile *pFile = (winFile*)id;
32141
 
 
32142
 
  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
32143
 
 
32144
 
  assert( id!=0 );
32145
 
  if( pFile->locktype>=RESERVED_LOCK ){
32146
 
    rc = 1;
32147
 
    OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
32148
 
  }else{
32149
 
    rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
32150
 
    if( rc ){
32151
 
      UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
32152
 
    }
32153
 
    rc = !rc;
32154
 
    OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
32155
 
  }
32156
 
  *pResOut = rc;
32157
 
  return SQLITE_OK;
32158
 
}
32159
 
 
32160
 
/*
32161
 
** Lower the locking level on file descriptor id to locktype.  locktype
32162
 
** must be either NO_LOCK or SHARED_LOCK.
32163
 
**
32164
 
** If the locking level of the file descriptor is already at or below
32165
 
** the requested locking level, this routine is a no-op.
32166
 
**
32167
 
** It is not possible for this routine to fail if the second argument
32168
 
** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
32169
 
** might return SQLITE_IOERR;
32170
 
*/
32171
 
static int winUnlock(sqlite3_file *id, int locktype){
32172
 
  int type;
32173
 
  winFile *pFile = (winFile*)id;
32174
 
  int rc = SQLITE_OK;
32175
 
  assert( pFile!=0 );
32176
 
  assert( locktype<=SHARED_LOCK );
32177
 
  OSTRACE(("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
32178
 
          pFile->locktype, pFile->sharedLockByte));
32179
 
  type = pFile->locktype;
32180
 
  if( type>=EXCLUSIVE_LOCK ){
32181
 
    UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
32182
 
    if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
32183
 
      /* This should never happen.  We should always be able to
32184
 
      ** reacquire the read lock */
32185
 
      rc = SQLITE_IOERR_UNLOCK;
32186
 
    }
32187
 
  }
32188
 
  if( type>=RESERVED_LOCK ){
32189
 
    UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
32190
 
  }
32191
 
  if( locktype==NO_LOCK && type>=SHARED_LOCK ){
32192
 
    unlockReadLock(pFile);
32193
 
  }
32194
 
  if( type>=PENDING_LOCK ){
32195
 
    UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
32196
 
  }
32197
 
  pFile->locktype = (u8)locktype;
32198
 
  return rc;
32199
 
}
32200
 
 
32201
 
/*
32202
 
** Control and query of the open file handle.
32203
 
*/
32204
 
static int winFileControl(sqlite3_file *id, int op, void *pArg){
32205
 
  switch( op ){
32206
 
    case SQLITE_FCNTL_LOCKSTATE: {
32207
 
      *(int*)pArg = ((winFile*)id)->locktype;
32208
 
      return SQLITE_OK;
32209
 
    }
32210
 
    case SQLITE_LAST_ERRNO: {
32211
 
      *(int*)pArg = (int)((winFile*)id)->lastErrno;
32212
 
      return SQLITE_OK;
32213
 
    }
32214
 
    case SQLITE_FCNTL_CHUNK_SIZE: {
32215
 
      ((winFile*)id)->szChunk = *(int *)pArg;
32216
 
      return SQLITE_OK;
32217
 
    }
32218
 
    case SQLITE_FCNTL_SIZE_HINT: {
32219
 
      sqlite3_int64 sz = *(sqlite3_int64*)pArg;
32220
 
      SimulateIOErrorBenign(1);
32221
 
      winTruncate(id, sz);
32222
 
      SimulateIOErrorBenign(0);
32223
 
      return SQLITE_OK;
32224
 
    }
32225
 
    case SQLITE_FCNTL_SYNC_OMITTED: {
32226
 
      return SQLITE_OK;
32227
 
    }
32228
 
  }
32229
 
  return SQLITE_NOTFOUND;
32230
 
}
32231
 
 
32232
 
/*
32233
 
** Return the sector size in bytes of the underlying block device for
32234
 
** the specified file. This is almost always 512 bytes, but may be
32235
 
** larger for some devices.
32236
 
**
32237
 
** SQLite code assumes this function cannot fail. It also assumes that
32238
 
** if two files are created in the same file-system directory (i.e.
32239
 
** a database and its journal file) that the sector size will be the
32240
 
** same for both.
32241
 
*/
32242
 
static int winSectorSize(sqlite3_file *id){
32243
 
  assert( id!=0 );
32244
 
  return (int)(((winFile*)id)->sectorSize);
32245
 
}
32246
 
 
32247
 
/*
32248
 
** Return a vector of device characteristics.
32249
 
*/
32250
 
static int winDeviceCharacteristics(sqlite3_file *id){
32251
 
  UNUSED_PARAMETER(id);
32252
 
  return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
32253
 
}
32254
 
 
32255
 
#ifndef SQLITE_OMIT_WAL
32256
 
 
32257
 
/* 
32258
 
** Windows will only let you create file view mappings
32259
 
** on allocation size granularity boundaries.
32260
 
** During sqlite3_os_init() we do a GetSystemInfo()
32261
 
** to get the granularity size.
32262
 
*/
32263
 
SYSTEM_INFO winSysInfo;
32264
 
 
32265
 
/*
32266
 
** Helper functions to obtain and relinquish the global mutex. The
32267
 
** global mutex is used to protect the winLockInfo objects used by 
32268
 
** this file, all of which may be shared by multiple threads.
32269
 
**
32270
 
** Function winShmMutexHeld() is used to assert() that the global mutex 
32271
 
** is held when required. This function is only used as part of assert() 
32272
 
** statements. e.g.
32273
 
**
32274
 
**   winShmEnterMutex()
32275
 
**     assert( winShmMutexHeld() );
32276
 
**   winShmLeaveMutex()
32277
 
*/
32278
 
static void winShmEnterMutex(void){
32279
 
  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
32280
 
}
32281
 
static void winShmLeaveMutex(void){
32282
 
  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
32283
 
}
32284
 
#ifdef SQLITE_DEBUG
32285
 
static int winShmMutexHeld(void) {
32286
 
  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
32287
 
}
32288
 
#endif
32289
 
 
32290
 
/*
32291
 
** Object used to represent a single file opened and mmapped to provide
32292
 
** shared memory.  When multiple threads all reference the same
32293
 
** log-summary, each thread has its own winFile object, but they all
32294
 
** point to a single instance of this object.  In other words, each
32295
 
** log-summary is opened only once per process.
32296
 
**
32297
 
** winShmMutexHeld() must be true when creating or destroying
32298
 
** this object or while reading or writing the following fields:
32299
 
**
32300
 
**      nRef
32301
 
**      pNext 
32302
 
**
32303
 
** The following fields are read-only after the object is created:
32304
 
** 
32305
 
**      fid
32306
 
**      zFilename
32307
 
**
32308
 
** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
32309
 
** winShmMutexHeld() is true when reading or writing any other field
32310
 
** in this structure.
32311
 
**
32312
 
*/
32313
 
struct winShmNode {
32314
 
  sqlite3_mutex *mutex;      /* Mutex to access this object */
32315
 
  char *zFilename;           /* Name of the file */
32316
 
  winFile hFile;             /* File handle from winOpen */
32317
 
 
32318
 
  int szRegion;              /* Size of shared-memory regions */
32319
 
  int nRegion;               /* Size of array apRegion */
32320
 
  struct ShmRegion {
32321
 
    HANDLE hMap;             /* File handle from CreateFileMapping */
32322
 
    void *pMap;
32323
 
  } *aRegion;
32324
 
  DWORD lastErrno;           /* The Windows errno from the last I/O error */
32325
 
 
32326
 
  int nRef;                  /* Number of winShm objects pointing to this */
32327
 
  winShm *pFirst;            /* All winShm objects pointing to this */
32328
 
  winShmNode *pNext;         /* Next in list of all winShmNode objects */
32329
 
#ifdef SQLITE_DEBUG
32330
 
  u8 nextShmId;              /* Next available winShm.id value */
32331
 
#endif
32332
 
};
32333
 
 
32334
 
/*
32335
 
** A global array of all winShmNode objects.
32336
 
**
32337
 
** The winShmMutexHeld() must be true while reading or writing this list.
32338
 
*/
32339
 
static winShmNode *winShmNodeList = 0;
32340
 
 
32341
 
/*
32342
 
** Structure used internally by this VFS to record the state of an
32343
 
** open shared memory connection.
32344
 
**
32345
 
** The following fields are initialized when this object is created and
32346
 
** are read-only thereafter:
32347
 
**
32348
 
**    winShm.pShmNode
32349
 
**    winShm.id
32350
 
**
32351
 
** All other fields are read/write.  The winShm.pShmNode->mutex must be held
32352
 
** while accessing any read/write fields.
32353
 
*/
32354
 
struct winShm {
32355
 
  winShmNode *pShmNode;      /* The underlying winShmNode object */
32356
 
  winShm *pNext;             /* Next winShm with the same winShmNode */
32357
 
  u8 hasMutex;               /* True if holding the winShmNode mutex */
32358
 
  u16 sharedMask;            /* Mask of shared locks held */
32359
 
  u16 exclMask;              /* Mask of exclusive locks held */
32360
 
#ifdef SQLITE_DEBUG
32361
 
  u8 id;                     /* Id of this connection with its winShmNode */
32362
 
#endif
32363
 
};
32364
 
 
32365
 
/*
32366
 
** Constants used for locking
32367
 
*/
32368
 
#define WIN_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
32369
 
#define WIN_SHM_DMS    (WIN_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
32370
 
 
32371
 
/*
32372
 
** Apply advisory locks for all n bytes beginning at ofst.
32373
 
*/
32374
 
#define _SHM_UNLCK  1
32375
 
#define _SHM_RDLCK  2
32376
 
#define _SHM_WRLCK  3
32377
 
static int winShmSystemLock(
32378
 
  winShmNode *pFile,    /* Apply locks to this open shared-memory segment */
32379
 
  int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
32380
 
  int ofst,             /* Offset to first byte to be locked/unlocked */
32381
 
  int nByte             /* Number of bytes to lock or unlock */
32382
 
){
32383
 
  OVERLAPPED ovlp;
32384
 
  DWORD dwFlags;
32385
 
  int rc = 0;           /* Result code form Lock/UnlockFileEx() */
32386
 
 
32387
 
  /* Access to the winShmNode object is serialized by the caller */
32388
 
  assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
32389
 
 
32390
 
  /* Initialize the locking parameters */
32391
 
  dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
32392
 
  if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
32393
 
 
32394
 
  memset(&ovlp, 0, sizeof(OVERLAPPED));
32395
 
  ovlp.Offset = ofst;
32396
 
 
32397
 
  /* Release/Acquire the system-level lock */
32398
 
  if( lockType==_SHM_UNLCK ){
32399
 
    rc = UnlockFileEx(pFile->hFile.h, 0, nByte, 0, &ovlp);
32400
 
  }else{
32401
 
    rc = LockFileEx(pFile->hFile.h, dwFlags, 0, nByte, 0, &ovlp);
32402
 
  }
32403
 
  
32404
 
  if( rc!= 0 ){
32405
 
    rc = SQLITE_OK;
32406
 
  }else{
32407
 
    pFile->lastErrno =  GetLastError();
32408
 
    rc = SQLITE_BUSY;
32409
 
  }
32410
 
 
32411
 
  OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n", 
32412
 
           pFile->hFile.h,
32413
 
           rc==SQLITE_OK ? "ok" : "failed",
32414
 
           lockType==_SHM_UNLCK ? "UnlockFileEx" : "LockFileEx",
32415
 
           pFile->lastErrno));
32416
 
 
32417
 
  return rc;
32418
 
}
32419
 
 
32420
 
/* Forward references to VFS methods */
32421
 
static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
32422
 
static int winDelete(sqlite3_vfs *,const char*,int);
32423
 
 
32424
 
/*
32425
 
** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
32426
 
**
32427
 
** This is not a VFS shared-memory method; it is a utility function called
32428
 
** by VFS shared-memory methods.
32429
 
*/
32430
 
static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
32431
 
  winShmNode **pp;
32432
 
  winShmNode *p;
32433
 
  BOOL bRc;
32434
 
  assert( winShmMutexHeld() );
32435
 
  pp = &winShmNodeList;
32436
 
  while( (p = *pp)!=0 ){
32437
 
    if( p->nRef==0 ){
32438
 
      int i;
32439
 
      if( p->mutex ) sqlite3_mutex_free(p->mutex);
32440
 
      for(i=0; i<p->nRegion; i++){
32441
 
        bRc = UnmapViewOfFile(p->aRegion[i].pMap);
32442
 
        OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
32443
 
                 (int)GetCurrentProcessId(), i,
32444
 
                 bRc ? "ok" : "failed"));
32445
 
        bRc = CloseHandle(p->aRegion[i].hMap);
32446
 
        OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n",
32447
 
                 (int)GetCurrentProcessId(), i,
32448
 
                 bRc ? "ok" : "failed"));
32449
 
      }
32450
 
      if( p->hFile.h != INVALID_HANDLE_VALUE ){
32451
 
        SimulateIOErrorBenign(1);
32452
 
        winClose((sqlite3_file *)&p->hFile);
32453
 
        SimulateIOErrorBenign(0);
32454
 
      }
32455
 
      if( deleteFlag ){
32456
 
        SimulateIOErrorBenign(1);
32457
 
        winDelete(pVfs, p->zFilename, 0);
32458
 
        SimulateIOErrorBenign(0);
32459
 
      }
32460
 
      *pp = p->pNext;
32461
 
      sqlite3_free(p->aRegion);
32462
 
      sqlite3_free(p);
32463
 
    }else{
32464
 
      pp = &p->pNext;
32465
 
    }
32466
 
  }
32467
 
}
32468
 
 
32469
 
/*
32470
 
** Open the shared-memory area associated with database file pDbFd.
32471
 
**
32472
 
** When opening a new shared-memory file, if no other instances of that
32473
 
** file are currently open, in this process or in other processes, then
32474
 
** the file must be truncated to zero length or have its header cleared.
32475
 
*/
32476
 
static int winOpenSharedMemory(winFile *pDbFd){
32477
 
  struct winShm *p;                  /* The connection to be opened */
32478
 
  struct winShmNode *pShmNode = 0;   /* The underlying mmapped file */
32479
 
  int rc;                            /* Result code */
32480
 
  struct winShmNode *pNew;           /* Newly allocated winShmNode */
32481
 
  int nName;                         /* Size of zName in bytes */
32482
 
 
32483
 
  assert( pDbFd->pShm==0 );    /* Not previously opened */
32484
 
 
32485
 
  /* Allocate space for the new sqlite3_shm object.  Also speculatively
32486
 
  ** allocate space for a new winShmNode and filename.
32487
 
  */
32488
 
  p = sqlite3_malloc( sizeof(*p) );
32489
 
  if( p==0 ) return SQLITE_NOMEM;
32490
 
  memset(p, 0, sizeof(*p));
32491
 
  nName = sqlite3Strlen30(pDbFd->zPath);
32492
 
  pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 15 );
32493
 
  if( pNew==0 ){
32494
 
    sqlite3_free(p);
32495
 
    return SQLITE_NOMEM;
32496
 
  }
32497
 
  memset(pNew, 0, sizeof(*pNew));
32498
 
  pNew->zFilename = (char*)&pNew[1];
32499
 
  sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
32500
 
 
32501
 
  /* Look to see if there is an existing winShmNode that can be used.
32502
 
  ** If no matching winShmNode currently exists, create a new one.
32503
 
  */
32504
 
  winShmEnterMutex();
32505
 
  for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
32506
 
    /* TBD need to come up with better match here.  Perhaps
32507
 
    ** use FILE_ID_BOTH_DIR_INFO Structure.
32508
 
    */
32509
 
    if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
32510
 
  }
32511
 
  if( pShmNode ){
32512
 
    sqlite3_free(pNew);
32513
 
  }else{
32514
 
    pShmNode = pNew;
32515
 
    pNew = 0;
32516
 
    ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
32517
 
    pShmNode->pNext = winShmNodeList;
32518
 
    winShmNodeList = pShmNode;
32519
 
 
32520
 
    pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
32521
 
    if( pShmNode->mutex==0 ){
32522
 
      rc = SQLITE_NOMEM;
32523
 
      goto shm_open_err;
32524
 
    }
32525
 
 
32526
 
    rc = winOpen(pDbFd->pVfs,
32527
 
                 pShmNode->zFilename,             /* Name of the file (UTF-8) */
32528
 
                 (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
32529
 
                 SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
32530
 
                 0);
32531
 
    if( SQLITE_OK!=rc ){
32532
 
      rc = SQLITE_CANTOPEN_BKPT;
32533
 
      goto shm_open_err;
32534
 
    }
32535
 
 
32536
 
    /* Check to see if another process is holding the dead-man switch.
32537
 
    ** If not, truncate the file to zero length. 
32538
 
    */
32539
 
    if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
32540
 
      rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
32541
 
      if( rc!=SQLITE_OK ){
32542
 
        rc = SQLITE_IOERR_SHMOPEN;
32543
 
      }
32544
 
    }
32545
 
    if( rc==SQLITE_OK ){
32546
 
      winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
32547
 
      rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
32548
 
    }
32549
 
    if( rc ) goto shm_open_err;
32550
 
  }
32551
 
 
32552
 
  /* Make the new connection a child of the winShmNode */
32553
 
  p->pShmNode = pShmNode;
32554
 
#ifdef SQLITE_DEBUG
32555
 
  p->id = pShmNode->nextShmId++;
32556
 
#endif
32557
 
  pShmNode->nRef++;
32558
 
  pDbFd->pShm = p;
32559
 
  winShmLeaveMutex();
32560
 
 
32561
 
  /* The reference count on pShmNode has already been incremented under
32562
 
  ** the cover of the winShmEnterMutex() mutex and the pointer from the
32563
 
  ** new (struct winShm) object to the pShmNode has been set. All that is
32564
 
  ** left to do is to link the new object into the linked list starting
32565
 
  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
32566
 
  ** mutex.
32567
 
  */
32568
 
  sqlite3_mutex_enter(pShmNode->mutex);
32569
 
  p->pNext = pShmNode->pFirst;
32570
 
  pShmNode->pFirst = p;
32571
 
  sqlite3_mutex_leave(pShmNode->mutex);
32572
 
  return SQLITE_OK;
32573
 
 
32574
 
  /* Jump here on any error */
32575
 
shm_open_err:
32576
 
  winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
32577
 
  winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
32578
 
  sqlite3_free(p);
32579
 
  sqlite3_free(pNew);
32580
 
  winShmLeaveMutex();
32581
 
  return rc;
32582
 
}
32583
 
 
32584
 
/*
32585
 
** Close a connection to shared-memory.  Delete the underlying 
32586
 
** storage if deleteFlag is true.
32587
 
*/
32588
 
static int winShmUnmap(
32589
 
  sqlite3_file *fd,          /* Database holding shared memory */
32590
 
  int deleteFlag             /* Delete after closing if true */
32591
 
){
32592
 
  winFile *pDbFd;       /* Database holding shared-memory */
32593
 
  winShm *p;            /* The connection to be closed */
32594
 
  winShmNode *pShmNode; /* The underlying shared-memory file */
32595
 
  winShm **pp;          /* For looping over sibling connections */
32596
 
 
32597
 
  pDbFd = (winFile*)fd;
32598
 
  p = pDbFd->pShm;
32599
 
  if( p==0 ) return SQLITE_OK;
32600
 
  pShmNode = p->pShmNode;
32601
 
 
32602
 
  /* Remove connection p from the set of connections associated
32603
 
  ** with pShmNode */
32604
 
  sqlite3_mutex_enter(pShmNode->mutex);
32605
 
  for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
32606
 
  *pp = p->pNext;
32607
 
 
32608
 
  /* Free the connection p */
32609
 
  sqlite3_free(p);
32610
 
  pDbFd->pShm = 0;
32611
 
  sqlite3_mutex_leave(pShmNode->mutex);
32612
 
 
32613
 
  /* If pShmNode->nRef has reached 0, then close the underlying
32614
 
  ** shared-memory file, too */
32615
 
  winShmEnterMutex();
32616
 
  assert( pShmNode->nRef>0 );
32617
 
  pShmNode->nRef--;
32618
 
  if( pShmNode->nRef==0 ){
32619
 
    winShmPurge(pDbFd->pVfs, deleteFlag);
32620
 
  }
32621
 
  winShmLeaveMutex();
32622
 
 
32623
 
  return SQLITE_OK;
32624
 
}
32625
 
 
32626
 
/*
32627
 
** Change the lock state for a shared-memory segment.
32628
 
*/
32629
 
static int winShmLock(
32630
 
  sqlite3_file *fd,          /* Database file holding the shared memory */
32631
 
  int ofst,                  /* First lock to acquire or release */
32632
 
  int n,                     /* Number of locks to acquire or release */
32633
 
  int flags                  /* What to do with the lock */
32634
 
){
32635
 
  winFile *pDbFd = (winFile*)fd;        /* Connection holding shared memory */
32636
 
  winShm *p = pDbFd->pShm;              /* The shared memory being locked */
32637
 
  winShm *pX;                           /* For looping over all siblings */
32638
 
  winShmNode *pShmNode = p->pShmNode;
32639
 
  int rc = SQLITE_OK;                   /* Result code */
32640
 
  u16 mask;                             /* Mask of locks to take or release */
32641
 
 
32642
 
  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
32643
 
  assert( n>=1 );
32644
 
  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
32645
 
       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
32646
 
       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
32647
 
       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
32648
 
  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
32649
 
 
32650
 
  mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
32651
 
  assert( n>1 || mask==(1<<ofst) );
32652
 
  sqlite3_mutex_enter(pShmNode->mutex);
32653
 
  if( flags & SQLITE_SHM_UNLOCK ){
32654
 
    u16 allMask = 0; /* Mask of locks held by siblings */
32655
 
 
32656
 
    /* See if any siblings hold this same lock */
32657
 
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
32658
 
      if( pX==p ) continue;
32659
 
      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
32660
 
      allMask |= pX->sharedMask;
32661
 
    }
32662
 
 
32663
 
    /* Unlock the system-level locks */
32664
 
    if( (mask & allMask)==0 ){
32665
 
      rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
32666
 
    }else{
32667
 
      rc = SQLITE_OK;
32668
 
    }
32669
 
 
32670
 
    /* Undo the local locks */
32671
 
    if( rc==SQLITE_OK ){
32672
 
      p->exclMask &= ~mask;
32673
 
      p->sharedMask &= ~mask;
32674
 
    } 
32675
 
  }else if( flags & SQLITE_SHM_SHARED ){
32676
 
    u16 allShared = 0;  /* Union of locks held by connections other than "p" */
32677
 
 
32678
 
    /* Find out which shared locks are already held by sibling connections.
32679
 
    ** If any sibling already holds an exclusive lock, go ahead and return
32680
 
    ** SQLITE_BUSY.
32681
 
    */
32682
 
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
32683
 
      if( (pX->exclMask & mask)!=0 ){
32684
 
        rc = SQLITE_BUSY;
32685
 
        break;
32686
 
      }
32687
 
      allShared |= pX->sharedMask;
32688
 
    }
32689
 
 
32690
 
    /* Get shared locks at the system level, if necessary */
32691
 
    if( rc==SQLITE_OK ){
32692
 
      if( (allShared & mask)==0 ){
32693
 
        rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
32694
 
      }else{
32695
 
        rc = SQLITE_OK;
32696
 
      }
32697
 
    }
32698
 
 
32699
 
    /* Get the local shared locks */
32700
 
    if( rc==SQLITE_OK ){
32701
 
      p->sharedMask |= mask;
32702
 
    }
32703
 
  }else{
32704
 
    /* Make sure no sibling connections hold locks that will block this
32705
 
    ** lock.  If any do, return SQLITE_BUSY right away.
32706
 
    */
32707
 
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
32708
 
      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
32709
 
        rc = SQLITE_BUSY;
32710
 
        break;
32711
 
      }
32712
 
    }
32713
 
  
32714
 
    /* Get the exclusive locks at the system level.  Then if successful
32715
 
    ** also mark the local connection as being locked.
32716
 
    */
32717
 
    if( rc==SQLITE_OK ){
32718
 
      rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
32719
 
      if( rc==SQLITE_OK ){
32720
 
        assert( (p->sharedMask & mask)==0 );
32721
 
        p->exclMask |= mask;
32722
 
      }
32723
 
    }
32724
 
  }
32725
 
  sqlite3_mutex_leave(pShmNode->mutex);
32726
 
  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
32727
 
           p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
32728
 
           rc ? "failed" : "ok"));
32729
 
  return rc;
32730
 
}
32731
 
 
32732
 
/*
32733
 
** Implement a memory barrier or memory fence on shared memory.  
32734
 
**
32735
 
** All loads and stores begun before the barrier must complete before
32736
 
** any load or store begun after the barrier.
32737
 
*/
32738
 
static void winShmBarrier(
32739
 
  sqlite3_file *fd          /* Database holding the shared memory */
32740
 
){
32741
 
  UNUSED_PARAMETER(fd);
32742
 
  /* MemoryBarrier(); // does not work -- do not know why not */
32743
 
  winShmEnterMutex();
32744
 
  winShmLeaveMutex();
32745
 
}
32746
 
 
32747
 
/*
32748
 
** This function is called to obtain a pointer to region iRegion of the 
32749
 
** shared-memory associated with the database file fd. Shared-memory regions 
32750
 
** are numbered starting from zero. Each shared-memory region is szRegion 
32751
 
** bytes in size.
32752
 
**
32753
 
** If an error occurs, an error code is returned and *pp is set to NULL.
32754
 
**
32755
 
** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
32756
 
** region has not been allocated (by any client, including one running in a
32757
 
** separate process), then *pp is set to NULL and SQLITE_OK returned. If 
32758
 
** isWrite is non-zero and the requested shared-memory region has not yet 
32759
 
** been allocated, it is allocated by this function.
32760
 
**
32761
 
** If the shared-memory region has already been allocated or is allocated by
32762
 
** this call as described above, then it is mapped into this processes 
32763
 
** address space (if it is not already), *pp is set to point to the mapped 
32764
 
** memory and SQLITE_OK returned.
32765
 
*/
32766
 
static int winShmMap(
32767
 
  sqlite3_file *fd,               /* Handle open on database file */
32768
 
  int iRegion,                    /* Region to retrieve */
32769
 
  int szRegion,                   /* Size of regions */
32770
 
  int isWrite,                    /* True to extend file if necessary */
32771
 
  void volatile **pp              /* OUT: Mapped memory */
32772
 
){
32773
 
  winFile *pDbFd = (winFile*)fd;
32774
 
  winShm *p = pDbFd->pShm;
32775
 
  winShmNode *pShmNode;
32776
 
  int rc = SQLITE_OK;
32777
 
 
32778
 
  if( !p ){
32779
 
    rc = winOpenSharedMemory(pDbFd);
32780
 
    if( rc!=SQLITE_OK ) return rc;
32781
 
    p = pDbFd->pShm;
32782
 
  }
32783
 
  pShmNode = p->pShmNode;
32784
 
 
32785
 
  sqlite3_mutex_enter(pShmNode->mutex);
32786
 
  assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
32787
 
 
32788
 
  if( pShmNode->nRegion<=iRegion ){
32789
 
    struct ShmRegion *apNew;           /* New aRegion[] array */
32790
 
    int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
32791
 
    sqlite3_int64 sz;                  /* Current size of wal-index file */
32792
 
 
32793
 
    pShmNode->szRegion = szRegion;
32794
 
 
32795
 
    /* The requested region is not mapped into this processes address space.
32796
 
    ** Check to see if it has been allocated (i.e. if the wal-index file is
32797
 
    ** large enough to contain the requested region).
32798
 
    */
32799
 
    rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
32800
 
    if( rc!=SQLITE_OK ){
32801
 
      rc = SQLITE_IOERR_SHMSIZE;
32802
 
      goto shmpage_out;
32803
 
    }
32804
 
 
32805
 
    if( sz<nByte ){
32806
 
      /* The requested memory region does not exist. If isWrite is set to
32807
 
      ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
32808
 
      **
32809
 
      ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
32810
 
      ** the requested memory region.
32811
 
      */
32812
 
      if( !isWrite ) goto shmpage_out;
32813
 
      rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
32814
 
      if( rc!=SQLITE_OK ){
32815
 
        rc = SQLITE_IOERR_SHMSIZE;
32816
 
        goto shmpage_out;
32817
 
      }
32818
 
    }
32819
 
 
32820
 
    /* Map the requested memory region into this processes address space. */
32821
 
    apNew = (struct ShmRegion *)sqlite3_realloc(
32822
 
        pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
32823
 
    );
32824
 
    if( !apNew ){
32825
 
      rc = SQLITE_IOERR_NOMEM;
32826
 
      goto shmpage_out;
32827
 
    }
32828
 
    pShmNode->aRegion = apNew;
32829
 
 
32830
 
    while( pShmNode->nRegion<=iRegion ){
32831
 
      HANDLE hMap;                /* file-mapping handle */
32832
 
      void *pMap = 0;             /* Mapped memory region */
32833
 
     
32834
 
      hMap = CreateFileMapping(pShmNode->hFile.h, 
32835
 
          NULL, PAGE_READWRITE, 0, nByte, NULL
32836
 
      );
32837
 
      OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
32838
 
               (int)GetCurrentProcessId(), pShmNode->nRegion, nByte,
32839
 
               hMap ? "ok" : "failed"));
32840
 
      if( hMap ){
32841
 
        int iOffset = pShmNode->nRegion*szRegion;
32842
 
        int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
32843
 
        pMap = MapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
32844
 
            0, iOffset - iOffsetShift, szRegion + iOffsetShift
32845
 
        );
32846
 
        OSTRACE(("SHM-MAP pid-%d map region=%d offset=%d size=%d %s\n",
32847
 
                 (int)GetCurrentProcessId(), pShmNode->nRegion, iOffset, szRegion,
32848
 
                 pMap ? "ok" : "failed"));
32849
 
      }
32850
 
      if( !pMap ){
32851
 
        pShmNode->lastErrno = GetLastError();
32852
 
        rc = SQLITE_IOERR;
32853
 
        if( hMap ) CloseHandle(hMap);
32854
 
        goto shmpage_out;
32855
 
      }
32856
 
 
32857
 
      pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
32858
 
      pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
32859
 
      pShmNode->nRegion++;
32860
 
    }
32861
 
  }
32862
 
 
32863
 
shmpage_out:
32864
 
  if( pShmNode->nRegion>iRegion ){
32865
 
    int iOffset = iRegion*szRegion;
32866
 
    int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
32867
 
    char *p = (char *)pShmNode->aRegion[iRegion].pMap;
32868
 
    *pp = (void *)&p[iOffsetShift];
32869
 
  }else{
32870
 
    *pp = 0;
32871
 
  }
32872
 
  sqlite3_mutex_leave(pShmNode->mutex);
32873
 
  return rc;
32874
 
}
32875
 
 
32876
 
#else
32877
 
# define winShmMap     0
32878
 
# define winShmLock    0
32879
 
# define winShmBarrier 0
32880
 
# define winShmUnmap   0
32881
 
#endif /* #ifndef SQLITE_OMIT_WAL */
32882
 
 
32883
 
/*
32884
 
** Here ends the implementation of all sqlite3_file methods.
32885
 
**
32886
 
********************** End sqlite3_file Methods *******************************
32887
 
******************************************************************************/
32888
 
 
32889
 
/*
32890
 
** This vector defines all the methods that can operate on an
32891
 
** sqlite3_file for win32.
32892
 
*/
32893
 
static const sqlite3_io_methods winIoMethod = {
32894
 
  2,                              /* iVersion */
32895
 
  winClose,                       /* xClose */
32896
 
  winRead,                        /* xRead */
32897
 
  winWrite,                       /* xWrite */
32898
 
  winTruncate,                    /* xTruncate */
32899
 
  winSync,                        /* xSync */
32900
 
  winFileSize,                    /* xFileSize */
32901
 
  winLock,                        /* xLock */
32902
 
  winUnlock,                      /* xUnlock */
32903
 
  winCheckReservedLock,           /* xCheckReservedLock */
32904
 
  winFileControl,                 /* xFileControl */
32905
 
  winSectorSize,                  /* xSectorSize */
32906
 
  winDeviceCharacteristics,       /* xDeviceCharacteristics */
32907
 
  winShmMap,                      /* xShmMap */
32908
 
  winShmLock,                     /* xShmLock */
32909
 
  winShmBarrier,                  /* xShmBarrier */
32910
 
  winShmUnmap                     /* xShmUnmap */
32911
 
};
32912
 
 
32913
 
/****************************************************************************
32914
 
**************************** sqlite3_vfs methods ****************************
32915
 
**
32916
 
** This division contains the implementation of methods on the
32917
 
** sqlite3_vfs object.
32918
 
*/
32919
 
 
32920
 
/*
32921
 
** Convert a UTF-8 filename into whatever form the underlying
32922
 
** operating system wants filenames in.  Space to hold the result
32923
 
** is obtained from malloc and must be freed by the calling
32924
 
** function.
32925
 
*/
32926
 
static void *convertUtf8Filename(const char *zFilename){
32927
 
  void *zConverted = 0;
32928
 
  if( isNT() ){
32929
 
    zConverted = utf8ToUnicode(zFilename);
32930
 
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
32931
 
*/
32932
 
#if SQLITE_OS_WINCE==0
32933
 
  }else{
32934
 
    zConverted = utf8ToMbcs(zFilename);
32935
 
#endif
32936
 
  }
32937
 
  /* caller will handle out of memory */
32938
 
  return zConverted;
32939
 
}
32940
 
 
32941
 
/*
32942
 
** Create a temporary file name in zBuf.  zBuf must be big enough to
32943
 
** hold at pVfs->mxPathname characters.
32944
 
*/
32945
 
static int getTempname(int nBuf, char *zBuf){
32946
 
  static char zChars[] =
32947
 
    "abcdefghijklmnopqrstuvwxyz"
32948
 
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
32949
 
    "0123456789";
32950
 
  size_t i, j;
32951
 
  char zTempPath[MAX_PATH+1];
32952
 
 
32953
 
  /* It's odd to simulate an io-error here, but really this is just
32954
 
  ** using the io-error infrastructure to test that SQLite handles this
32955
 
  ** function failing. 
32956
 
  */
32957
 
  SimulateIOError( return SQLITE_IOERR );
32958
 
 
32959
 
  if( sqlite3_temp_directory ){
32960
 
    sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
32961
 
  }else if( isNT() ){
32962
 
    char *zMulti;
32963
 
    WCHAR zWidePath[MAX_PATH];
32964
 
    GetTempPathW(MAX_PATH-30, zWidePath);
32965
 
    zMulti = unicodeToUtf8(zWidePath);
32966
 
    if( zMulti ){
32967
 
      sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
32968
 
      free(zMulti);
32969
 
    }else{
32970
 
      return SQLITE_NOMEM;
32971
 
    }
32972
 
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
32973
 
** Since the ASCII version of these Windows API do not exist for WINCE,
32974
 
** it's important to not reference them for WINCE builds.
32975
 
*/
32976
 
#if SQLITE_OS_WINCE==0
32977
 
  }else{
32978
 
    char *zUtf8;
32979
 
    char zMbcsPath[MAX_PATH];
32980
 
    GetTempPathA(MAX_PATH-30, zMbcsPath);
32981
 
    zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
32982
 
    if( zUtf8 ){
32983
 
      sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
32984
 
      free(zUtf8);
32985
 
    }else{
32986
 
      return SQLITE_NOMEM;
32987
 
    }
32988
 
#endif
32989
 
  }
32990
 
 
32991
 
  /* Check that the output buffer is large enough for the temporary file 
32992
 
  ** name. If it is not, return SQLITE_ERROR.
32993
 
  */
32994
 
  if( (sqlite3Strlen30(zTempPath) + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){
32995
 
    return SQLITE_ERROR;
32996
 
  }
32997
 
 
32998
 
  for(i=sqlite3Strlen30(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
32999
 
  zTempPath[i] = 0;
33000
 
 
33001
 
  sqlite3_snprintf(nBuf-17, zBuf,
33002
 
                   "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
33003
 
  j = sqlite3Strlen30(zBuf);
33004
 
  sqlite3_randomness(15, &zBuf[j]);
33005
 
  for(i=0; i<15; i++, j++){
33006
 
    zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
33007
 
  }
33008
 
  zBuf[j] = 0;
33009
 
 
33010
 
  OSTRACE(("TEMP FILENAME: %s\n", zBuf));
33011
 
  return SQLITE_OK; 
33012
 
}
33013
 
 
33014
 
/*
33015
 
** The return value of getLastErrorMsg
33016
 
** is zero if the error message fits in the buffer, or non-zero
33017
 
** otherwise (if the message was truncated).
33018
 
*/
33019
 
static int getLastErrorMsg(int nBuf, char *zBuf){
33020
 
  /* FormatMessage returns 0 on failure.  Otherwise it
33021
 
  ** returns the number of TCHARs written to the output
33022
 
  ** buffer, excluding the terminating null char.
33023
 
  */
33024
 
  DWORD error = GetLastError();
33025
 
  DWORD dwLen = 0;
33026
 
  char *zOut = 0;
33027
 
 
33028
 
  if( isNT() ){
33029
 
    WCHAR *zTempWide = NULL;
33030
 
    dwLen = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
33031
 
                           NULL,
33032
 
                           error,
33033
 
                           0,
33034
 
                           (LPWSTR) &zTempWide,
33035
 
                           0,
33036
 
                           0);
33037
 
    if( dwLen > 0 ){
33038
 
      /* allocate a buffer and convert to UTF8 */
33039
 
      zOut = unicodeToUtf8(zTempWide);
33040
 
      /* free the system buffer allocated by FormatMessage */
33041
 
      LocalFree(zTempWide);
33042
 
    }
33043
 
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
33044
 
** Since the ASCII version of these Windows API do not exist for WINCE,
33045
 
** it's important to not reference them for WINCE builds.
33046
 
*/
33047
 
#if SQLITE_OS_WINCE==0
33048
 
  }else{
33049
 
    char *zTemp = NULL;
33050
 
    dwLen = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
33051
 
                           NULL,
33052
 
                           error,
33053
 
                           0,
33054
 
                           (LPSTR) &zTemp,
33055
 
                           0,
33056
 
                           0);
33057
 
    if( dwLen > 0 ){
33058
 
      /* allocate a buffer and convert to UTF8 */
33059
 
      zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
33060
 
      /* free the system buffer allocated by FormatMessage */
33061
 
      LocalFree(zTemp);
33062
 
    }
33063
 
#endif
33064
 
  }
33065
 
  if( 0 == dwLen ){
33066
 
    sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", error, error);
33067
 
  }else{
33068
 
    /* copy a maximum of nBuf chars to output buffer */
33069
 
    sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
33070
 
    /* free the UTF8 buffer */
33071
 
    free(zOut);
33072
 
  }
33073
 
  return 0;
33074
 
}
33075
 
 
33076
 
/*
33077
 
** Open a file.
33078
 
*/
33079
 
static int winOpen(
33080
 
  sqlite3_vfs *pVfs,        /* Not used */
33081
 
  const char *zName,        /* Name of the file (UTF-8) */
33082
 
  sqlite3_file *id,         /* Write the SQLite file handle here */
33083
 
  int flags,                /* Open mode flags */
33084
 
  int *pOutFlags            /* Status return flags */
33085
 
){
33086
 
  HANDLE h;
33087
 
  DWORD dwDesiredAccess;
33088
 
  DWORD dwShareMode;
33089
 
  DWORD dwCreationDisposition;
33090
 
  DWORD dwFlagsAndAttributes = 0;
33091
 
#if SQLITE_OS_WINCE
33092
 
  int isTemp = 0;
33093
 
#endif
33094
 
  winFile *pFile = (winFile*)id;
33095
 
  void *zConverted;              /* Filename in OS encoding */
33096
 
  const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
33097
 
 
33098
 
  /* If argument zPath is a NULL pointer, this function is required to open
33099
 
  ** a temporary file. Use this buffer to store the file name in.
33100
 
  */
33101
 
  char zTmpname[MAX_PATH+1];     /* Buffer used to create temp filename */
33102
 
 
33103
 
  int rc = SQLITE_OK;            /* Function Return Code */
33104
 
#if !defined(NDEBUG) || SQLITE_OS_WINCE
33105
 
  int eType = flags&0xFFFFFF00;  /* Type of file to open */
33106
 
#endif
33107
 
 
33108
 
  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
33109
 
  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
33110
 
  int isCreate     = (flags & SQLITE_OPEN_CREATE);
33111
 
#ifndef NDEBUG
33112
 
  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
33113
 
#endif
33114
 
  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
33115
 
 
33116
 
#ifndef NDEBUG
33117
 
  int isOpenJournal = (isCreate && (
33118
 
        eType==SQLITE_OPEN_MASTER_JOURNAL 
33119
 
     || eType==SQLITE_OPEN_MAIN_JOURNAL 
33120
 
     || eType==SQLITE_OPEN_WAL
33121
 
  ));
33122
 
#endif
33123
 
 
33124
 
  /* Check the following statements are true: 
33125
 
  **
33126
 
  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
33127
 
  **   (b) if CREATE is set, then READWRITE must also be set, and
33128
 
  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
33129
 
  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
33130
 
  */
33131
 
  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
33132
 
  assert(isCreate==0 || isReadWrite);
33133
 
  assert(isExclusive==0 || isCreate);
33134
 
  assert(isDelete==0 || isCreate);
33135
 
 
33136
 
  /* The main DB, main journal, WAL file and master journal are never 
33137
 
  ** automatically deleted. Nor are they ever temporary files.  */
33138
 
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
33139
 
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
33140
 
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
33141
 
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
33142
 
 
33143
 
  /* Assert that the upper layer has set one of the "file-type" flags. */
33144
 
  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
33145
 
       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
33146
 
       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
33147
 
       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
33148
 
  );
33149
 
 
33150
 
  assert( id!=0 );
33151
 
  UNUSED_PARAMETER(pVfs);
33152
 
 
33153
 
  pFile->h = INVALID_HANDLE_VALUE;
33154
 
 
33155
 
  /* If the second argument to this function is NULL, generate a 
33156
 
  ** temporary file name to use 
33157
 
  */
33158
 
  if( !zUtf8Name ){
33159
 
    assert(isDelete && !isOpenJournal);
33160
 
    rc = getTempname(MAX_PATH+1, zTmpname);
33161
 
    if( rc!=SQLITE_OK ){
33162
 
      return rc;
33163
 
    }
33164
 
    zUtf8Name = zTmpname;
33165
 
  }
33166
 
 
33167
 
  /* Convert the filename to the system encoding. */
33168
 
  zConverted = convertUtf8Filename(zUtf8Name);
33169
 
  if( zConverted==0 ){
33170
 
    return SQLITE_NOMEM;
33171
 
  }
33172
 
 
33173
 
  if( isReadWrite ){
33174
 
    dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
33175
 
  }else{
33176
 
    dwDesiredAccess = GENERIC_READ;
33177
 
  }
33178
 
 
33179
 
  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is 
33180
 
  ** created. SQLite doesn't use it to indicate "exclusive access" 
33181
 
  ** as it is usually understood.
33182
 
  */
33183
 
  if( isExclusive ){
33184
 
    /* Creates a new file, only if it does not already exist. */
33185
 
    /* If the file exists, it fails. */
33186
 
    dwCreationDisposition = CREATE_NEW;
33187
 
  }else if( isCreate ){
33188
 
    /* Open existing file, or create if it doesn't exist */
33189
 
    dwCreationDisposition = OPEN_ALWAYS;
33190
 
  }else{
33191
 
    /* Opens a file, only if it exists. */
33192
 
    dwCreationDisposition = OPEN_EXISTING;
33193
 
  }
33194
 
 
33195
 
  dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
33196
 
 
33197
 
  if( isDelete ){
33198
 
#if SQLITE_OS_WINCE
33199
 
    dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
33200
 
    isTemp = 1;
33201
 
#else
33202
 
    dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
33203
 
                               | FILE_ATTRIBUTE_HIDDEN
33204
 
                               | FILE_FLAG_DELETE_ON_CLOSE;
33205
 
#endif
33206
 
  }else{
33207
 
    dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
33208
 
  }
33209
 
  /* Reports from the internet are that performance is always
33210
 
  ** better if FILE_FLAG_RANDOM_ACCESS is used.  Ticket #2699. */
33211
 
#if SQLITE_OS_WINCE
33212
 
  dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
33213
 
#endif
33214
 
 
33215
 
  if( isNT() ){
33216
 
    h = CreateFileW((WCHAR*)zConverted,
33217
 
       dwDesiredAccess,
33218
 
       dwShareMode,
33219
 
       NULL,
33220
 
       dwCreationDisposition,
33221
 
       dwFlagsAndAttributes,
33222
 
       NULL
33223
 
    );
33224
 
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
33225
 
** Since the ASCII version of these Windows API do not exist for WINCE,
33226
 
** it's important to not reference them for WINCE builds.
33227
 
*/
33228
 
#if SQLITE_OS_WINCE==0
33229
 
  }else{
33230
 
    h = CreateFileA((char*)zConverted,
33231
 
       dwDesiredAccess,
33232
 
       dwShareMode,
33233
 
       NULL,
33234
 
       dwCreationDisposition,
33235
 
       dwFlagsAndAttributes,
33236
 
       NULL
33237
 
    );
33238
 
#endif
33239
 
  }
33240
 
 
33241
 
  OSTRACE(("OPEN %d %s 0x%lx %s\n", 
33242
 
           h, zName, dwDesiredAccess, 
33243
 
           h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
33244
 
 
33245
 
  if( h==INVALID_HANDLE_VALUE ){
33246
 
    pFile->lastErrno = GetLastError();
33247
 
    free(zConverted);
33248
 
    if( isReadWrite ){
33249
 
      return winOpen(pVfs, zName, id, 
33250
 
             ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
33251
 
    }else{
33252
 
      return SQLITE_CANTOPEN_BKPT;
33253
 
    }
33254
 
  }
33255
 
 
33256
 
  if( pOutFlags ){
33257
 
    if( isReadWrite ){
33258
 
      *pOutFlags = SQLITE_OPEN_READWRITE;
33259
 
    }else{
33260
 
      *pOutFlags = SQLITE_OPEN_READONLY;
33261
 
    }
33262
 
  }
33263
 
 
33264
 
  memset(pFile, 0, sizeof(*pFile));
33265
 
  pFile->pMethod = &winIoMethod;
33266
 
  pFile->h = h;
33267
 
  pFile->lastErrno = NO_ERROR;
33268
 
  pFile->pVfs = pVfs;
33269
 
  pFile->pShm = 0;
33270
 
  pFile->zPath = zName;
33271
 
  pFile->sectorSize = getSectorSize(pVfs, zUtf8Name);
33272
 
 
33273
 
#if SQLITE_OS_WINCE
33274
 
  if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
33275
 
       && !winceCreateLock(zName, pFile)
33276
 
  ){
33277
 
    CloseHandle(h);
33278
 
    free(zConverted);
33279
 
    return SQLITE_CANTOPEN_BKPT;
33280
 
  }
33281
 
  if( isTemp ){
33282
 
    pFile->zDeleteOnClose = zConverted;
33283
 
  }else
33284
 
#endif
33285
 
  {
33286
 
    free(zConverted);
33287
 
  }
33288
 
 
33289
 
  OpenCounter(+1);
33290
 
  return rc;
33291
 
}
33292
 
 
33293
 
/*
33294
 
** Delete the named file.
33295
 
**
33296
 
** Note that windows does not allow a file to be deleted if some other
33297
 
** process has it open.  Sometimes a virus scanner or indexing program
33298
 
** will open a journal file shortly after it is created in order to do
33299
 
** whatever it does.  While this other process is holding the
33300
 
** file open, we will be unable to delete it.  To work around this
33301
 
** problem, we delay 100 milliseconds and try to delete again.  Up
33302
 
** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
33303
 
** up and returning an error.
33304
 
*/
33305
 
#define MX_DELETION_ATTEMPTS 5
33306
 
static int winDelete(
33307
 
  sqlite3_vfs *pVfs,          /* Not used on win32 */
33308
 
  const char *zFilename,      /* Name of file to delete */
33309
 
  int syncDir                 /* Not used on win32 */
33310
 
){
33311
 
  int cnt = 0;
33312
 
  DWORD rc;
33313
 
  DWORD error = 0;
33314
 
  void *zConverted;
33315
 
  UNUSED_PARAMETER(pVfs);
33316
 
  UNUSED_PARAMETER(syncDir);
33317
 
 
33318
 
  SimulateIOError(return SQLITE_IOERR_DELETE);
33319
 
  zConverted = convertUtf8Filename(zFilename);
33320
 
  if( zConverted==0 ){
33321
 
    return SQLITE_NOMEM;
33322
 
  }
33323
 
  if( isNT() ){
33324
 
    do{
33325
 
      DeleteFileW(zConverted);
33326
 
    }while(   (   ((rc = GetFileAttributesW(zConverted)) != INVALID_FILE_ATTRIBUTES)
33327
 
               || ((error = GetLastError()) == ERROR_ACCESS_DENIED))
33328
 
           && (++cnt < MX_DELETION_ATTEMPTS)
33329
 
           && (Sleep(100), 1) );
33330
 
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
33331
 
** Since the ASCII version of these Windows API do not exist for WINCE,
33332
 
** it's important to not reference them for WINCE builds.
33333
 
*/
33334
 
#if SQLITE_OS_WINCE==0
33335
 
  }else{
33336
 
    do{
33337
 
      DeleteFileA(zConverted);
33338
 
    }while(   (   ((rc = GetFileAttributesA(zConverted)) != INVALID_FILE_ATTRIBUTES)
33339
 
               || ((error = GetLastError()) == ERROR_ACCESS_DENIED))
33340
 
           && (++cnt < MX_DELETION_ATTEMPTS)
33341
 
           && (Sleep(100), 1) );
33342
 
#endif
33343
 
  }
33344
 
  free(zConverted);
33345
 
  OSTRACE(("DELETE \"%s\" %s\n", zFilename,
33346
 
       ( (rc==INVALID_FILE_ATTRIBUTES) && (error==ERROR_FILE_NOT_FOUND)) ?
33347
 
         "ok" : "failed" ));
33348
 
 
33349
 
  return (   (rc == INVALID_FILE_ATTRIBUTES) 
33350
 
          && (error == ERROR_FILE_NOT_FOUND)) ? SQLITE_OK : SQLITE_IOERR_DELETE;
33351
 
}
33352
 
 
33353
 
/*
33354
 
** Check the existance and status of a file.
33355
 
*/
33356
 
static int winAccess(
33357
 
  sqlite3_vfs *pVfs,         /* Not used on win32 */
33358
 
  const char *zFilename,     /* Name of file to check */
33359
 
  int flags,                 /* Type of test to make on this file */
33360
 
  int *pResOut               /* OUT: Result */
33361
 
){
33362
 
  DWORD attr;
33363
 
  int rc = 0;
33364
 
  void *zConverted;
33365
 
  UNUSED_PARAMETER(pVfs);
33366
 
 
33367
 
  SimulateIOError( return SQLITE_IOERR_ACCESS; );
33368
 
  zConverted = convertUtf8Filename(zFilename);
33369
 
  if( zConverted==0 ){
33370
 
    return SQLITE_NOMEM;
33371
 
  }
33372
 
  if( isNT() ){
33373
 
    WIN32_FILE_ATTRIBUTE_DATA sAttrData;
33374
 
    memset(&sAttrData, 0, sizeof(sAttrData));
33375
 
    if( GetFileAttributesExW((WCHAR*)zConverted,
33376
 
                             GetFileExInfoStandard, 
33377
 
                             &sAttrData) ){
33378
 
      /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
33379
 
      ** as if it does not exist.
33380
 
      */
33381
 
      if(    flags==SQLITE_ACCESS_EXISTS
33382
 
          && sAttrData.nFileSizeHigh==0 
33383
 
          && sAttrData.nFileSizeLow==0 ){
33384
 
        attr = INVALID_FILE_ATTRIBUTES;
33385
 
      }else{
33386
 
        attr = sAttrData.dwFileAttributes;
33387
 
      }
33388
 
    }else{
33389
 
      if( GetLastError()!=ERROR_FILE_NOT_FOUND ){
33390
 
        free(zConverted);
33391
 
        return SQLITE_IOERR_ACCESS;
33392
 
      }else{
33393
 
        attr = INVALID_FILE_ATTRIBUTES;
33394
 
      }
33395
 
    }
33396
 
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
33397
 
** Since the ASCII version of these Windows API do not exist for WINCE,
33398
 
** it's important to not reference them for WINCE builds.
33399
 
*/
33400
 
#if SQLITE_OS_WINCE==0
33401
 
  }else{
33402
 
    attr = GetFileAttributesA((char*)zConverted);
33403
 
#endif
33404
 
  }
33405
 
  free(zConverted);
33406
 
  switch( flags ){
33407
 
    case SQLITE_ACCESS_READ:
33408
 
    case SQLITE_ACCESS_EXISTS:
33409
 
      rc = attr!=INVALID_FILE_ATTRIBUTES;
33410
 
      break;
33411
 
    case SQLITE_ACCESS_READWRITE:
33412
 
      rc = (attr & FILE_ATTRIBUTE_READONLY)==0;
33413
 
      break;
33414
 
    default:
33415
 
      assert(!"Invalid flags argument");
33416
 
  }
33417
 
  *pResOut = rc;
33418
 
  return SQLITE_OK;
33419
 
}
33420
 
 
33421
 
 
33422
 
/*
33423
 
** Turn a relative pathname into a full pathname.  Write the full
33424
 
** pathname into zOut[].  zOut[] will be at least pVfs->mxPathname
33425
 
** bytes in size.
33426
 
*/
33427
 
static int winFullPathname(
33428
 
  sqlite3_vfs *pVfs,            /* Pointer to vfs object */
33429
 
  const char *zRelative,        /* Possibly relative input path */
33430
 
  int nFull,                    /* Size of output buffer in bytes */
33431
 
  char *zFull                   /* Output buffer */
33432
 
){
33433
 
  
33434
 
#if defined(__CYGWIN__)
33435
 
  SimulateIOError( return SQLITE_ERROR );
33436
 
  UNUSED_PARAMETER(nFull);
33437
 
  cygwin_conv_to_full_win32_path(zRelative, zFull);
33438
 
  return SQLITE_OK;
33439
 
#endif
33440
 
 
33441
 
#if SQLITE_OS_WINCE
33442
 
  SimulateIOError( return SQLITE_ERROR );
33443
 
  UNUSED_PARAMETER(nFull);
33444
 
  /* WinCE has no concept of a relative pathname, or so I am told. */
33445
 
  sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zRelative);
33446
 
  return SQLITE_OK;
33447
 
#endif
33448
 
 
33449
 
#if !SQLITE_OS_WINCE && !defined(__CYGWIN__)
33450
 
  int nByte;
33451
 
  void *zConverted;
33452
 
  char *zOut;
33453
 
 
33454
 
  /* It's odd to simulate an io-error here, but really this is just
33455
 
  ** using the io-error infrastructure to test that SQLite handles this
33456
 
  ** function failing. This function could fail if, for example, the
33457
 
  ** current working directory has been unlinked.
33458
 
  */
33459
 
  SimulateIOError( return SQLITE_ERROR );
33460
 
  UNUSED_PARAMETER(nFull);
33461
 
  zConverted = convertUtf8Filename(zRelative);
33462
 
  if( isNT() ){
33463
 
    WCHAR *zTemp;
33464
 
    nByte = GetFullPathNameW((WCHAR*)zConverted, 0, 0, 0) + 3;
33465
 
    zTemp = malloc( nByte*sizeof(zTemp[0]) );
33466
 
    if( zTemp==0 ){
33467
 
      free(zConverted);
33468
 
      return SQLITE_NOMEM;
33469
 
    }
33470
 
    GetFullPathNameW((WCHAR*)zConverted, nByte, zTemp, 0);
33471
 
    free(zConverted);
33472
 
    zOut = unicodeToUtf8(zTemp);
33473
 
    free(zTemp);
33474
 
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
33475
 
** Since the ASCII version of these Windows API do not exist for WINCE,
33476
 
** it's important to not reference them for WINCE builds.
33477
 
*/
33478
 
#if SQLITE_OS_WINCE==0
33479
 
  }else{
33480
 
    char *zTemp;
33481
 
    nByte = GetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
33482
 
    zTemp = malloc( nByte*sizeof(zTemp[0]) );
33483
 
    if( zTemp==0 ){
33484
 
      free(zConverted);
33485
 
      return SQLITE_NOMEM;
33486
 
    }
33487
 
    GetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
33488
 
    free(zConverted);
33489
 
    zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
33490
 
    free(zTemp);
33491
 
#endif
33492
 
  }
33493
 
  if( zOut ){
33494
 
    sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
33495
 
    free(zOut);
33496
 
    return SQLITE_OK;
33497
 
  }else{
33498
 
    return SQLITE_NOMEM;
33499
 
  }
33500
 
#endif
33501
 
}
33502
 
 
33503
 
/*
33504
 
** Get the sector size of the device used to store
33505
 
** file.
33506
 
*/
33507
 
static int getSectorSize(
33508
 
    sqlite3_vfs *pVfs,
33509
 
    const char *zRelative     /* UTF-8 file name */
33510
 
){
33511
 
  DWORD bytesPerSector = SQLITE_DEFAULT_SECTOR_SIZE;
33512
 
  /* GetDiskFreeSpace is not supported under WINCE */
33513
 
#if SQLITE_OS_WINCE
33514
 
  UNUSED_PARAMETER(pVfs);
33515
 
  UNUSED_PARAMETER(zRelative);
33516
 
#else
33517
 
  char zFullpath[MAX_PATH+1];
33518
 
  int rc;
33519
 
  DWORD dwRet = 0;
33520
 
  DWORD dwDummy;
33521
 
 
33522
 
  /*
33523
 
  ** We need to get the full path name of the file
33524
 
  ** to get the drive letter to look up the sector
33525
 
  ** size.
33526
 
  */
33527
 
  SimulateIOErrorBenign(1);
33528
 
  rc = winFullPathname(pVfs, zRelative, MAX_PATH, zFullpath);
33529
 
  SimulateIOErrorBenign(0);
33530
 
  if( rc == SQLITE_OK )
33531
 
  {
33532
 
    void *zConverted = convertUtf8Filename(zFullpath);
33533
 
    if( zConverted ){
33534
 
      if( isNT() ){
33535
 
        /* trim path to just drive reference */
33536
 
        WCHAR *p = zConverted;
33537
 
        for(;*p;p++){
33538
 
          if( *p == '\\' ){
33539
 
            *p = '\0';
33540
 
            break;
33541
 
          }
33542
 
        }
33543
 
        dwRet = GetDiskFreeSpaceW((WCHAR*)zConverted,
33544
 
                                  &dwDummy,
33545
 
                                  &bytesPerSector,
33546
 
                                  &dwDummy,
33547
 
                                  &dwDummy);
33548
 
      }else{
33549
 
        /* trim path to just drive reference */
33550
 
        char *p = (char *)zConverted;
33551
 
        for(;*p;p++){
33552
 
          if( *p == '\\' ){
33553
 
            *p = '\0';
33554
 
            break;
33555
 
          }
33556
 
        }
33557
 
        dwRet = GetDiskFreeSpaceA((char*)zConverted,
33558
 
                                  &dwDummy,
33559
 
                                  &bytesPerSector,
33560
 
                                  &dwDummy,
33561
 
                                  &dwDummy);
33562
 
      }
33563
 
      free(zConverted);
33564
 
    }
33565
 
    if( !dwRet ){
33566
 
      bytesPerSector = SQLITE_DEFAULT_SECTOR_SIZE;
33567
 
    }
33568
 
  }
33569
 
#endif
33570
 
  return (int) bytesPerSector; 
33571
 
}
33572
 
 
33573
 
#ifndef SQLITE_OMIT_LOAD_EXTENSION
33574
 
/*
33575
 
** Interfaces for opening a shared library, finding entry points
33576
 
** within the shared library, and closing the shared library.
33577
 
*/
33578
 
/*
33579
 
** Interfaces for opening a shared library, finding entry points
33580
 
** within the shared library, and closing the shared library.
33581
 
*/
33582
 
static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
33583
 
  HANDLE h;
33584
 
  void *zConverted = convertUtf8Filename(zFilename);
33585
 
  UNUSED_PARAMETER(pVfs);
33586
 
  if( zConverted==0 ){
33587
 
    return 0;
33588
 
  }
33589
 
  if( isNT() ){
33590
 
    h = LoadLibraryW((WCHAR*)zConverted);
33591
 
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
33592
 
** Since the ASCII version of these Windows API do not exist for WINCE,
33593
 
** it's important to not reference them for WINCE builds.
33594
 
*/
33595
 
#if SQLITE_OS_WINCE==0
33596
 
  }else{
33597
 
    h = LoadLibraryA((char*)zConverted);
33598
 
#endif
33599
 
  }
33600
 
  free(zConverted);
33601
 
  return (void*)h;
33602
 
}
33603
 
static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
33604
 
  UNUSED_PARAMETER(pVfs);
33605
 
  getLastErrorMsg(nBuf, zBufOut);
33606
 
}
33607
 
void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
33608
 
  UNUSED_PARAMETER(pVfs);
33609
 
#if SQLITE_OS_WINCE
33610
 
  /* The GetProcAddressA() routine is only available on wince. */
33611
 
  return (void(*)(void))GetProcAddressA((HANDLE)pHandle, zSymbol);
33612
 
#else
33613
 
  /* All other windows platforms expect GetProcAddress() to take
33614
 
  ** an Ansi string regardless of the _UNICODE setting */
33615
 
  return (void(*)(void))GetProcAddress((HANDLE)pHandle, zSymbol);
33616
 
#endif
33617
 
}
33618
 
void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
33619
 
  UNUSED_PARAMETER(pVfs);
33620
 
  FreeLibrary((HANDLE)pHandle);
33621
 
}
33622
 
#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
33623
 
  #define winDlOpen  0
33624
 
  #define winDlError 0
33625
 
  #define winDlSym   0
33626
 
  #define winDlClose 0
33627
 
#endif
33628
 
 
33629
 
 
33630
 
/*
33631
 
** Write up to nBuf bytes of randomness into zBuf.
33632
 
*/
33633
 
static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
33634
 
  int n = 0;
33635
 
  UNUSED_PARAMETER(pVfs);
33636
 
#if defined(SQLITE_TEST)
33637
 
  n = nBuf;
33638
 
  memset(zBuf, 0, nBuf);
33639
 
#else
33640
 
  if( sizeof(SYSTEMTIME)<=nBuf-n ){
33641
 
    SYSTEMTIME x;
33642
 
    GetSystemTime(&x);
33643
 
    memcpy(&zBuf[n], &x, sizeof(x));
33644
 
    n += sizeof(x);
33645
 
  }
33646
 
  if( sizeof(DWORD)<=nBuf-n ){
33647
 
    DWORD pid = GetCurrentProcessId();
33648
 
    memcpy(&zBuf[n], &pid, sizeof(pid));
33649
 
    n += sizeof(pid);
33650
 
  }
33651
 
  if( sizeof(DWORD)<=nBuf-n ){
33652
 
    DWORD cnt = GetTickCount();
33653
 
    memcpy(&zBuf[n], &cnt, sizeof(cnt));
33654
 
    n += sizeof(cnt);
33655
 
  }
33656
 
  if( sizeof(LARGE_INTEGER)<=nBuf-n ){
33657
 
    LARGE_INTEGER i;
33658
 
    QueryPerformanceCounter(&i);
33659
 
    memcpy(&zBuf[n], &i, sizeof(i));
33660
 
    n += sizeof(i);
33661
 
  }
33662
 
#endif
33663
 
  return n;
33664
 
}
33665
 
 
33666
 
 
33667
 
/*
33668
 
** Sleep for a little while.  Return the amount of time slept.
33669
 
*/
33670
 
static int winSleep(sqlite3_vfs *pVfs, int microsec){
33671
 
  Sleep((microsec+999)/1000);
33672
 
  UNUSED_PARAMETER(pVfs);
33673
 
  return ((microsec+999)/1000)*1000;
33674
 
}
33675
 
 
33676
 
/*
33677
 
** The following variable, if set to a non-zero value, is interpreted as
33678
 
** the number of seconds since 1970 and is used to set the result of
33679
 
** sqlite3OsCurrentTime() during testing.
33680
 
*/
33681
 
#ifdef SQLITE_TEST
33682
 
SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
33683
 
#endif
33684
 
 
33685
 
/*
33686
 
** Find the current time (in Universal Coordinated Time).  Write into *piNow
33687
 
** the current time and date as a Julian Day number times 86_400_000.  In
33688
 
** other words, write into *piNow the number of milliseconds since the Julian
33689
 
** epoch of noon in Greenwich on November 24, 4714 B.C according to the
33690
 
** proleptic Gregorian calendar.
33691
 
**
33692
 
** On success, return 0.  Return 1 if the time and date cannot be found.
33693
 
*/
33694
 
static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
33695
 
  /* FILETIME structure is a 64-bit value representing the number of 
33696
 
     100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). 
33697
 
  */
33698
 
  FILETIME ft;
33699
 
  static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
33700
 
#ifdef SQLITE_TEST
33701
 
  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
33702
 
#endif
33703
 
  /* 2^32 - to avoid use of LL and warnings in gcc */
33704
 
  static const sqlite3_int64 max32BitValue = 
33705
 
      (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
33706
 
 
33707
 
#if SQLITE_OS_WINCE
33708
 
  SYSTEMTIME time;
33709
 
  GetSystemTime(&time);
33710
 
  /* if SystemTimeToFileTime() fails, it returns zero. */
33711
 
  if (!SystemTimeToFileTime(&time,&ft)){
33712
 
    return 1;
33713
 
  }
33714
 
#else
33715
 
  GetSystemTimeAsFileTime( &ft );
33716
 
#endif
33717
 
 
33718
 
  *piNow = winFiletimeEpoch +
33719
 
            ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + 
33720
 
               (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
33721
 
 
33722
 
#ifdef SQLITE_TEST
33723
 
  if( sqlite3_current_time ){
33724
 
    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
33725
 
  }
33726
 
#endif
33727
 
  UNUSED_PARAMETER(pVfs);
33728
 
  return 0;
33729
 
}
33730
 
 
33731
 
/*
33732
 
** Find the current time (in Universal Coordinated Time).  Write the
33733
 
** current time and date as a Julian Day number into *prNow and
33734
 
** return 0.  Return 1 if the time and date cannot be found.
33735
 
*/
33736
 
int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
33737
 
  int rc;
33738
 
  sqlite3_int64 i;
33739
 
  rc = winCurrentTimeInt64(pVfs, &i);
33740
 
  if( !rc ){
33741
 
    *prNow = i/86400000.0;
33742
 
  }
33743
 
  return rc;
33744
 
}
33745
 
 
33746
 
/*
33747
 
** The idea is that this function works like a combination of
33748
 
** GetLastError() and FormatMessage() on windows (or errno and
33749
 
** strerror_r() on unix). After an error is returned by an OS
33750
 
** function, SQLite calls this function with zBuf pointing to
33751
 
** a buffer of nBuf bytes. The OS layer should populate the
33752
 
** buffer with a nul-terminated UTF-8 encoded error message
33753
 
** describing the last IO error to have occurred within the calling
33754
 
** thread.
33755
 
**
33756
 
** If the error message is too large for the supplied buffer,
33757
 
** it should be truncated. The return value of xGetLastError
33758
 
** is zero if the error message fits in the buffer, or non-zero
33759
 
** otherwise (if the message was truncated). If non-zero is returned,
33760
 
** then it is not necessary to include the nul-terminator character
33761
 
** in the output buffer.
33762
 
**
33763
 
** Not supplying an error message will have no adverse effect
33764
 
** on SQLite. It is fine to have an implementation that never
33765
 
** returns an error message:
33766
 
**
33767
 
**   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
33768
 
**     assert(zBuf[0]=='\0');
33769
 
**     return 0;
33770
 
**   }
33771
 
**
33772
 
** However if an error message is supplied, it will be incorporated
33773
 
** by sqlite into the error message available to the user using
33774
 
** sqlite3_errmsg(), possibly making IO errors easier to debug.
33775
 
*/
33776
 
static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
33777
 
  UNUSED_PARAMETER(pVfs);
33778
 
  return getLastErrorMsg(nBuf, zBuf);
33779
 
}
33780
 
 
33781
 
 
33782
 
 
33783
 
/*
33784
 
** Initialize and deinitialize the operating system interface.
33785
 
*/
33786
 
SQLITE_API int sqlite3_os_init(void){
33787
 
  static sqlite3_vfs winVfs = {
33788
 
    3,                   /* iVersion */
33789
 
    sizeof(winFile),     /* szOsFile */
33790
 
    MAX_PATH,            /* mxPathname */
33791
 
    0,                   /* pNext */
33792
 
    "win32",             /* zName */
33793
 
    0,                   /* pAppData */
33794
 
    winOpen,             /* xOpen */
33795
 
    winDelete,           /* xDelete */
33796
 
    winAccess,           /* xAccess */
33797
 
    winFullPathname,     /* xFullPathname */
33798
 
    winDlOpen,           /* xDlOpen */
33799
 
    winDlError,          /* xDlError */
33800
 
    winDlSym,            /* xDlSym */
33801
 
    winDlClose,          /* xDlClose */
33802
 
    winRandomness,       /* xRandomness */
33803
 
    winSleep,            /* xSleep */
33804
 
    winCurrentTime,      /* xCurrentTime */
33805
 
    winGetLastError,     /* xGetLastError */
33806
 
    winCurrentTimeInt64, /* xCurrentTimeInt64 */
33807
 
    0,                   /* xSetSystemCall */
33808
 
    0,                   /* xGetSystemCall */
33809
 
    0,                   /* xNextSystemCall */
33810
 
  };
33811
 
 
33812
 
#ifndef SQLITE_OMIT_WAL
33813
 
  /* get memory map allocation granularity */
33814
 
  memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
33815
 
  GetSystemInfo(&winSysInfo);
33816
 
  assert(winSysInfo.dwAllocationGranularity > 0);
33817
 
#endif
33818
 
 
33819
 
  sqlite3_vfs_register(&winVfs, 1);
33820
 
  return SQLITE_OK; 
33821
 
}
33822
 
SQLITE_API int sqlite3_os_end(void){ 
33823
 
  return SQLITE_OK;
33824
 
}
33825
 
 
33826
 
#endif /* SQLITE_OS_WIN */
33827
 
 
33828
 
/************** End of os_win.c **********************************************/
33829
 
/************** Begin file bitvec.c ******************************************/
33830
 
/*
33831
 
** 2008 February 16
33832
 
**
33833
 
** The author disclaims copyright to this source code.  In place of
33834
 
** a legal notice, here is a blessing:
33835
 
**
33836
 
**    May you do good and not evil.
33837
 
**    May you find forgiveness for yourself and forgive others.
33838
 
**    May you share freely, never taking more than you give.
33839
 
**
33840
 
*************************************************************************
33841
 
** This file implements an object that represents a fixed-length
33842
 
** bitmap.  Bits are numbered starting with 1.
33843
 
**
33844
 
** A bitmap is used to record which pages of a database file have been
33845
 
** journalled during a transaction, or which pages have the "dont-write"
33846
 
** property.  Usually only a few pages are meet either condition.
33847
 
** So the bitmap is usually sparse and has low cardinality.
33848
 
** But sometimes (for example when during a DROP of a large table) most
33849
 
** or all of the pages in a database can get journalled.  In those cases, 
33850
 
** the bitmap becomes dense with high cardinality.  The algorithm needs 
33851
 
** to handle both cases well.
33852
 
**
33853
 
** The size of the bitmap is fixed when the object is created.
33854
 
**
33855
 
** All bits are clear when the bitmap is created.  Individual bits
33856
 
** may be set or cleared one at a time.
33857
 
**
33858
 
** Test operations are about 100 times more common that set operations.
33859
 
** Clear operations are exceedingly rare.  There are usually between
33860
 
** 5 and 500 set operations per Bitvec object, though the number of sets can
33861
 
** sometimes grow into tens of thousands or larger.  The size of the
33862
 
** Bitvec object is the number of pages in the database file at the
33863
 
** start of a transaction, and is thus usually less than a few thousand,
33864
 
** but can be as large as 2 billion for a really big database.
33865
 
*/
33866
 
 
33867
 
/* Size of the Bitvec structure in bytes. */
33868
 
#define BITVEC_SZ        512
33869
 
 
33870
 
/* Round the union size down to the nearest pointer boundary, since that's how 
33871
 
** it will be aligned within the Bitvec struct. */
33872
 
#define BITVEC_USIZE     (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
33873
 
 
33874
 
/* Type of the array "element" for the bitmap representation. 
33875
 
** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. 
33876
 
** Setting this to the "natural word" size of your CPU may improve
33877
 
** performance. */
33878
 
#define BITVEC_TELEM     u8
33879
 
/* Size, in bits, of the bitmap element. */
33880
 
#define BITVEC_SZELEM    8
33881
 
/* Number of elements in a bitmap array. */
33882
 
#define BITVEC_NELEM     (BITVEC_USIZE/sizeof(BITVEC_TELEM))
33883
 
/* Number of bits in the bitmap array. */
33884
 
#define BITVEC_NBIT      (BITVEC_NELEM*BITVEC_SZELEM)
33885
 
 
33886
 
/* Number of u32 values in hash table. */
33887
 
#define BITVEC_NINT      (BITVEC_USIZE/sizeof(u32))
33888
 
/* Maximum number of entries in hash table before 
33889
 
** sub-dividing and re-hashing. */
33890
 
#define BITVEC_MXHASH    (BITVEC_NINT/2)
33891
 
/* Hashing function for the aHash representation.
33892
 
** Empirical testing showed that the *37 multiplier 
33893
 
** (an arbitrary prime)in the hash function provided 
33894
 
** no fewer collisions than the no-op *1. */
33895
 
#define BITVEC_HASH(X)   (((X)*1)%BITVEC_NINT)
33896
 
 
33897
 
#define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
33898
 
 
33899
 
 
33900
 
/*
33901
 
** A bitmap is an instance of the following structure.
33902
 
**
33903
 
** This bitmap records the existance of zero or more bits
33904
 
** with values between 1 and iSize, inclusive.
33905
 
**
33906
 
** There are three possible representations of the bitmap.
33907
 
** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
33908
 
** bitmap.  The least significant bit is bit 1.
33909
 
**
33910
 
** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
33911
 
** a hash table that will hold up to BITVEC_MXHASH distinct values.
33912
 
**
33913
 
** Otherwise, the value i is redirected into one of BITVEC_NPTR
33914
 
** sub-bitmaps pointed to by Bitvec.u.apSub[].  Each subbitmap
33915
 
** handles up to iDivisor separate values of i.  apSub[0] holds
33916
 
** values between 1 and iDivisor.  apSub[1] holds values between
33917
 
** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
33918
 
** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
33919
 
** to hold deal with values between 1 and iDivisor.
33920
 
*/
33921
 
struct Bitvec {
33922
 
  u32 iSize;      /* Maximum bit index.  Max iSize is 4,294,967,296. */
33923
 
  u32 nSet;       /* Number of bits that are set - only valid for aHash
33924
 
                  ** element.  Max is BITVEC_NINT.  For BITVEC_SZ of 512,
33925
 
                  ** this would be 125. */
33926
 
  u32 iDivisor;   /* Number of bits handled by each apSub[] entry. */
33927
 
                  /* Should >=0 for apSub element. */
33928
 
                  /* Max iDivisor is max(u32) / BITVEC_NPTR + 1.  */
33929
 
                  /* For a BITVEC_SZ of 512, this would be 34,359,739. */
33930
 
  union {
33931
 
    BITVEC_TELEM aBitmap[BITVEC_NELEM];    /* Bitmap representation */
33932
 
    u32 aHash[BITVEC_NINT];      /* Hash table representation */
33933
 
    Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
33934
 
  } u;
33935
 
};
33936
 
 
33937
 
/*
33938
 
** Create a new bitmap object able to handle bits between 0 and iSize,
33939
 
** inclusive.  Return a pointer to the new object.  Return NULL if 
33940
 
** malloc fails.
33941
 
*/
33942
 
SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
33943
 
  Bitvec *p;
33944
 
  assert( sizeof(*p)==BITVEC_SZ );
33945
 
  p = sqlite3MallocZero( sizeof(*p) );
33946
 
  if( p ){
33947
 
    p->iSize = iSize;
33948
 
  }
33949
 
  return p;
33950
 
}
33951
 
 
33952
 
/*
33953
 
** Check to see if the i-th bit is set.  Return true or false.
33954
 
** If p is NULL (if the bitmap has not been created) or if
33955
 
** i is out of range, then return false.
33956
 
*/
33957
 
SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
33958
 
  if( p==0 ) return 0;
33959
 
  if( i>p->iSize || i==0 ) return 0;
33960
 
  i--;
33961
 
  while( p->iDivisor ){
33962
 
    u32 bin = i/p->iDivisor;
33963
 
    i = i%p->iDivisor;
33964
 
    p = p->u.apSub[bin];
33965
 
    if (!p) {
33966
 
      return 0;
33967
 
    }
33968
 
  }
33969
 
  if( p->iSize<=BITVEC_NBIT ){
33970
 
    return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
33971
 
  } else{
33972
 
    u32 h = BITVEC_HASH(i++);
33973
 
    while( p->u.aHash[h] ){
33974
 
      if( p->u.aHash[h]==i ) return 1;
33975
 
      h = (h+1) % BITVEC_NINT;
33976
 
    }
33977
 
    return 0;
33978
 
  }
33979
 
}
33980
 
 
33981
 
/*
33982
 
** Set the i-th bit.  Return 0 on success and an error code if
33983
 
** anything goes wrong.
33984
 
**
33985
 
** This routine might cause sub-bitmaps to be allocated.  Failing
33986
 
** to get the memory needed to hold the sub-bitmap is the only
33987
 
** that can go wrong with an insert, assuming p and i are valid.
33988
 
**
33989
 
** The calling function must ensure that p is a valid Bitvec object
33990
 
** and that the value for "i" is within range of the Bitvec object.
33991
 
** Otherwise the behavior is undefined.
33992
 
*/
33993
 
SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
33994
 
  u32 h;
33995
 
  if( p==0 ) return SQLITE_OK;
33996
 
  assert( i>0 );
33997
 
  assert( i<=p->iSize );
33998
 
  i--;
33999
 
  while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
34000
 
    u32 bin = i/p->iDivisor;
34001
 
    i = i%p->iDivisor;
34002
 
    if( p->u.apSub[bin]==0 ){
34003
 
      p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
34004
 
      if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
34005
 
    }
34006
 
    p = p->u.apSub[bin];
34007
 
  }
34008
 
  if( p->iSize<=BITVEC_NBIT ){
34009
 
    p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
34010
 
    return SQLITE_OK;
34011
 
  }
34012
 
  h = BITVEC_HASH(i++);
34013
 
  /* if there wasn't a hash collision, and this doesn't */
34014
 
  /* completely fill the hash, then just add it without */
34015
 
  /* worring about sub-dividing and re-hashing. */
34016
 
  if( !p->u.aHash[h] ){
34017
 
    if (p->nSet<(BITVEC_NINT-1)) {
34018
 
      goto bitvec_set_end;
34019
 
    } else {
34020
 
      goto bitvec_set_rehash;
34021
 
    }
34022
 
  }
34023
 
  /* there was a collision, check to see if it's already */
34024
 
  /* in hash, if not, try to find a spot for it */
34025
 
  do {
34026
 
    if( p->u.aHash[h]==i ) return SQLITE_OK;
34027
 
    h++;
34028
 
    if( h>=BITVEC_NINT ) h = 0;
34029
 
  } while( p->u.aHash[h] );
34030
 
  /* we didn't find it in the hash.  h points to the first */
34031
 
  /* available free spot. check to see if this is going to */
34032
 
  /* make our hash too "full".  */
34033
 
bitvec_set_rehash:
34034
 
  if( p->nSet>=BITVEC_MXHASH ){
34035
 
    unsigned int j;
34036
 
    int rc;
34037
 
    u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
34038
 
    if( aiValues==0 ){
34039
 
      return SQLITE_NOMEM;
34040
 
    }else{
34041
 
      memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
34042
 
      memset(p->u.apSub, 0, sizeof(p->u.apSub));
34043
 
      p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
34044
 
      rc = sqlite3BitvecSet(p, i);
34045
 
      for(j=0; j<BITVEC_NINT; j++){
34046
 
        if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
34047
 
      }
34048
 
      sqlite3StackFree(0, aiValues);
34049
 
      return rc;
34050
 
    }
34051
 
  }
34052
 
bitvec_set_end:
34053
 
  p->nSet++;
34054
 
  p->u.aHash[h] = i;
34055
 
  return SQLITE_OK;
34056
 
}
34057
 
 
34058
 
/*
34059
 
** Clear the i-th bit.
34060
 
**
34061
 
** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
34062
 
** that BitvecClear can use to rebuilt its hash table.
34063
 
*/
34064
 
SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
34065
 
  if( p==0 ) return;
34066
 
  assert( i>0 );
34067
 
  i--;
34068
 
  while( p->iDivisor ){
34069
 
    u32 bin = i/p->iDivisor;
34070
 
    i = i%p->iDivisor;
34071
 
    p = p->u.apSub[bin];
34072
 
    if (!p) {
34073
 
      return;
34074
 
    }
34075
 
  }
34076
 
  if( p->iSize<=BITVEC_NBIT ){
34077
 
    p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
34078
 
  }else{
34079
 
    unsigned int j;
34080
 
    u32 *aiValues = pBuf;
34081
 
    memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
34082
 
    memset(p->u.aHash, 0, sizeof(p->u.aHash));
34083
 
    p->nSet = 0;
34084
 
    for(j=0; j<BITVEC_NINT; j++){
34085
 
      if( aiValues[j] && aiValues[j]!=(i+1) ){
34086
 
        u32 h = BITVEC_HASH(aiValues[j]-1);
34087
 
        p->nSet++;
34088
 
        while( p->u.aHash[h] ){
34089
 
          h++;
34090
 
          if( h>=BITVEC_NINT ) h = 0;
34091
 
        }
34092
 
        p->u.aHash[h] = aiValues[j];
34093
 
      }
34094
 
    }
34095
 
  }
34096
 
}
34097
 
 
34098
 
/*
34099
 
** Destroy a bitmap object.  Reclaim all memory used.
34100
 
*/
34101
 
SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
34102
 
  if( p==0 ) return;
34103
 
  if( p->iDivisor ){
34104
 
    unsigned int i;
34105
 
    for(i=0; i<BITVEC_NPTR; i++){
34106
 
      sqlite3BitvecDestroy(p->u.apSub[i]);
34107
 
    }
34108
 
  }
34109
 
  sqlite3_free(p);
34110
 
}
34111
 
 
34112
 
/*
34113
 
** Return the value of the iSize parameter specified when Bitvec *p
34114
 
** was created.
34115
 
*/
34116
 
SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
34117
 
  return p->iSize;
34118
 
}
34119
 
 
34120
 
#ifndef SQLITE_OMIT_BUILTIN_TEST
34121
 
/*
34122
 
** Let V[] be an array of unsigned characters sufficient to hold
34123
 
** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
34124
 
** Then the following macros can be used to set, clear, or test
34125
 
** individual bits within V.
34126
 
*/
34127
 
#define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
34128
 
#define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
34129
 
#define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
34130
 
 
34131
 
/*
34132
 
** This routine runs an extensive test of the Bitvec code.
34133
 
**
34134
 
** The input is an array of integers that acts as a program
34135
 
** to test the Bitvec.  The integers are opcodes followed
34136
 
** by 0, 1, or 3 operands, depending on the opcode.  Another
34137
 
** opcode follows immediately after the last operand.
34138
 
**
34139
 
** There are 6 opcodes numbered from 0 through 5.  0 is the
34140
 
** "halt" opcode and causes the test to end.
34141
 
**
34142
 
**    0          Halt and return the number of errors
34143
 
**    1 N S X    Set N bits beginning with S and incrementing by X
34144
 
**    2 N S X    Clear N bits beginning with S and incrementing by X
34145
 
**    3 N        Set N randomly chosen bits
34146
 
**    4 N        Clear N randomly chosen bits
34147
 
**    5 N S X    Set N bits from S increment X in array only, not in bitvec
34148
 
**
34149
 
** The opcodes 1 through 4 perform set and clear operations are performed
34150
 
** on both a Bitvec object and on a linear array of bits obtained from malloc.
34151
 
** Opcode 5 works on the linear array only, not on the Bitvec.
34152
 
** Opcode 5 is used to deliberately induce a fault in order to
34153
 
** confirm that error detection works.
34154
 
**
34155
 
** At the conclusion of the test the linear array is compared
34156
 
** against the Bitvec object.  If there are any differences,
34157
 
** an error is returned.  If they are the same, zero is returned.
34158
 
**
34159
 
** If a memory allocation error occurs, return -1.
34160
 
*/
34161
 
SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
34162
 
  Bitvec *pBitvec = 0;
34163
 
  unsigned char *pV = 0;
34164
 
  int rc = -1;
34165
 
  int i, nx, pc, op;
34166
 
  void *pTmpSpace;
34167
 
 
34168
 
  /* Allocate the Bitvec to be tested and a linear array of
34169
 
  ** bits to act as the reference */
34170
 
  pBitvec = sqlite3BitvecCreate( sz );
34171
 
  pV = sqlite3_malloc( (sz+7)/8 + 1 );
34172
 
  pTmpSpace = sqlite3_malloc(BITVEC_SZ);
34173
 
  if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
34174
 
  memset(pV, 0, (sz+7)/8 + 1);
34175
 
 
34176
 
  /* NULL pBitvec tests */
34177
 
  sqlite3BitvecSet(0, 1);
34178
 
  sqlite3BitvecClear(0, 1, pTmpSpace);
34179
 
 
34180
 
  /* Run the program */
34181
 
  pc = 0;
34182
 
  while( (op = aOp[pc])!=0 ){
34183
 
    switch( op ){
34184
 
      case 1:
34185
 
      case 2:
34186
 
      case 5: {
34187
 
        nx = 4;
34188
 
        i = aOp[pc+2] - 1;
34189
 
        aOp[pc+2] += aOp[pc+3];
34190
 
        break;
34191
 
      }
34192
 
      case 3:
34193
 
      case 4: 
34194
 
      default: {
34195
 
        nx = 2;
34196
 
        sqlite3_randomness(sizeof(i), &i);
34197
 
        break;
34198
 
      }
34199
 
    }
34200
 
    if( (--aOp[pc+1]) > 0 ) nx = 0;
34201
 
    pc += nx;
34202
 
    i = (i & 0x7fffffff)%sz;
34203
 
    if( (op & 1)!=0 ){
34204
 
      SETBIT(pV, (i+1));
34205
 
      if( op!=5 ){
34206
 
        if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
34207
 
      }
34208
 
    }else{
34209
 
      CLEARBIT(pV, (i+1));
34210
 
      sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
34211
 
    }
34212
 
  }
34213
 
 
34214
 
  /* Test to make sure the linear array exactly matches the
34215
 
  ** Bitvec object.  Start with the assumption that they do
34216
 
  ** match (rc==0).  Change rc to non-zero if a discrepancy
34217
 
  ** is found.
34218
 
  */
34219
 
  rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
34220
 
          + sqlite3BitvecTest(pBitvec, 0)
34221
 
          + (sqlite3BitvecSize(pBitvec) - sz);
34222
 
  for(i=1; i<=sz; i++){
34223
 
    if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
34224
 
      rc = i;
34225
 
      break;
34226
 
    }
34227
 
  }
34228
 
 
34229
 
  /* Free allocated structure */
34230
 
bitvec_end:
34231
 
  sqlite3_free(pTmpSpace);
34232
 
  sqlite3_free(pV);
34233
 
  sqlite3BitvecDestroy(pBitvec);
34234
 
  return rc;
34235
 
}
34236
 
#endif /* SQLITE_OMIT_BUILTIN_TEST */
34237
 
 
34238
 
/************** End of bitvec.c **********************************************/
34239
 
/************** Begin file pcache.c ******************************************/
34240
 
/*
34241
 
** 2008 August 05
34242
 
**
34243
 
** The author disclaims copyright to this source code.  In place of
34244
 
** a legal notice, here is a blessing:
34245
 
**
34246
 
**    May you do good and not evil.
34247
 
**    May you find forgiveness for yourself and forgive others.
34248
 
**    May you share freely, never taking more than you give.
34249
 
**
34250
 
*************************************************************************
34251
 
** This file implements that page cache.
34252
 
*/
34253
 
 
34254
 
/*
34255
 
** A complete page cache is an instance of this structure.
34256
 
*/
34257
 
struct PCache {
34258
 
  PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
34259
 
  PgHdr *pSynced;                     /* Last synced page in dirty page list */
34260
 
  int nRef;                           /* Number of referenced pages */
34261
 
  int nMax;                           /* Configured cache size */
34262
 
  int szPage;                         /* Size of every page in this cache */
34263
 
  int szExtra;                        /* Size of extra space for each page */
34264
 
  int bPurgeable;                     /* True if pages are on backing store */
34265
 
  int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
34266
 
  void *pStress;                      /* Argument to xStress */
34267
 
  sqlite3_pcache *pCache;             /* Pluggable cache module */
34268
 
  PgHdr *pPage1;                      /* Reference to page 1 */
34269
 
};
34270
 
 
34271
 
/*
34272
 
** Some of the assert() macros in this code are too expensive to run
34273
 
** even during normal debugging.  Use them only rarely on long-running
34274
 
** tests.  Enable the expensive asserts using the
34275
 
** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
34276
 
*/
34277
 
#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
34278
 
# define expensive_assert(X)  assert(X)
34279
 
#else
34280
 
# define expensive_assert(X)
34281
 
#endif
34282
 
 
34283
 
/********************************** Linked List Management ********************/
34284
 
 
34285
 
#if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
34286
 
/*
34287
 
** Check that the pCache->pSynced variable is set correctly. If it
34288
 
** is not, either fail an assert or return zero. Otherwise, return
34289
 
** non-zero. This is only used in debugging builds, as follows:
34290
 
**
34291
 
**   expensive_assert( pcacheCheckSynced(pCache) );
34292
 
*/
34293
 
static int pcacheCheckSynced(PCache *pCache){
34294
 
  PgHdr *p;
34295
 
  for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
34296
 
    assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
34297
 
  }
34298
 
  return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
34299
 
}
34300
 
#endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
34301
 
 
34302
 
/*
34303
 
** Remove page pPage from the list of dirty pages.
34304
 
*/
34305
 
static void pcacheRemoveFromDirtyList(PgHdr *pPage){
34306
 
  PCache *p = pPage->pCache;
34307
 
 
34308
 
  assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
34309
 
  assert( pPage->pDirtyPrev || pPage==p->pDirty );
34310
 
 
34311
 
  /* Update the PCache1.pSynced variable if necessary. */
34312
 
  if( p->pSynced==pPage ){
34313
 
    PgHdr *pSynced = pPage->pDirtyPrev;
34314
 
    while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
34315
 
      pSynced = pSynced->pDirtyPrev;
34316
 
    }
34317
 
    p->pSynced = pSynced;
34318
 
  }
34319
 
 
34320
 
  if( pPage->pDirtyNext ){
34321
 
    pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
34322
 
  }else{
34323
 
    assert( pPage==p->pDirtyTail );
34324
 
    p->pDirtyTail = pPage->pDirtyPrev;
34325
 
  }
34326
 
  if( pPage->pDirtyPrev ){
34327
 
    pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
34328
 
  }else{
34329
 
    assert( pPage==p->pDirty );
34330
 
    p->pDirty = pPage->pDirtyNext;
34331
 
  }
34332
 
  pPage->pDirtyNext = 0;
34333
 
  pPage->pDirtyPrev = 0;
34334
 
 
34335
 
  expensive_assert( pcacheCheckSynced(p) );
34336
 
}
34337
 
 
34338
 
/*
34339
 
** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
34340
 
** pPage).
34341
 
*/
34342
 
static void pcacheAddToDirtyList(PgHdr *pPage){
34343
 
  PCache *p = pPage->pCache;
34344
 
 
34345
 
  assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
34346
 
 
34347
 
  pPage->pDirtyNext = p->pDirty;
34348
 
  if( pPage->pDirtyNext ){
34349
 
    assert( pPage->pDirtyNext->pDirtyPrev==0 );
34350
 
    pPage->pDirtyNext->pDirtyPrev = pPage;
34351
 
  }
34352
 
  p->pDirty = pPage;
34353
 
  if( !p->pDirtyTail ){
34354
 
    p->pDirtyTail = pPage;
34355
 
  }
34356
 
  if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
34357
 
    p->pSynced = pPage;
34358
 
  }
34359
 
  expensive_assert( pcacheCheckSynced(p) );
34360
 
}
34361
 
 
34362
 
/*
34363
 
** Wrapper around the pluggable caches xUnpin method. If the cache is
34364
 
** being used for an in-memory database, this function is a no-op.
34365
 
*/
34366
 
static void pcacheUnpin(PgHdr *p){
34367
 
  PCache *pCache = p->pCache;
34368
 
  if( pCache->bPurgeable ){
34369
 
    if( p->pgno==1 ){
34370
 
      pCache->pPage1 = 0;
34371
 
    }
34372
 
    sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 0);
34373
 
  }
34374
 
}
34375
 
 
34376
 
/*************************************************** General Interfaces ******
34377
 
**
34378
 
** Initialize and shutdown the page cache subsystem. Neither of these 
34379
 
** functions are threadsafe.
34380
 
*/
34381
 
SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
34382
 
  if( sqlite3GlobalConfig.pcache.xInit==0 ){
34383
 
    /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
34384
 
    ** built-in default page cache is used instead of the application defined
34385
 
    ** page cache. */
34386
 
    sqlite3PCacheSetDefault();
34387
 
  }
34388
 
  return sqlite3GlobalConfig.pcache.xInit(sqlite3GlobalConfig.pcache.pArg);
34389
 
}
34390
 
SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
34391
 
  if( sqlite3GlobalConfig.pcache.xShutdown ){
34392
 
    /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
34393
 
    sqlite3GlobalConfig.pcache.xShutdown(sqlite3GlobalConfig.pcache.pArg);
34394
 
  }
34395
 
}
34396
 
 
34397
 
/*
34398
 
** Return the size in bytes of a PCache object.
34399
 
*/
34400
 
SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
34401
 
 
34402
 
/*
34403
 
** Create a new PCache object. Storage space to hold the object
34404
 
** has already been allocated and is passed in as the p pointer. 
34405
 
** The caller discovers how much space needs to be allocated by 
34406
 
** calling sqlite3PcacheSize().
34407
 
*/
34408
 
SQLITE_PRIVATE void sqlite3PcacheOpen(
34409
 
  int szPage,                  /* Size of every page */
34410
 
  int szExtra,                 /* Extra space associated with each page */
34411
 
  int bPurgeable,              /* True if pages are on backing store */
34412
 
  int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
34413
 
  void *pStress,               /* Argument to xStress */
34414
 
  PCache *p                    /* Preallocated space for the PCache */
34415
 
){
34416
 
  memset(p, 0, sizeof(PCache));
34417
 
  p->szPage = szPage;
34418
 
  p->szExtra = szExtra;
34419
 
  p->bPurgeable = bPurgeable;
34420
 
  p->xStress = xStress;
34421
 
  p->pStress = pStress;
34422
 
  p->nMax = 100;
34423
 
}
34424
 
 
34425
 
/*
34426
 
** Change the page size for PCache object. The caller must ensure that there
34427
 
** are no outstanding page references when this function is called.
34428
 
*/
34429
 
SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
34430
 
  assert( pCache->nRef==0 && pCache->pDirty==0 );
34431
 
  if( pCache->pCache ){
34432
 
    sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
34433
 
    pCache->pCache = 0;
34434
 
    pCache->pPage1 = 0;
34435
 
  }
34436
 
  pCache->szPage = szPage;
34437
 
}
34438
 
 
34439
 
/*
34440
 
** Try to obtain a page from the cache.
34441
 
*/
34442
 
SQLITE_PRIVATE int sqlite3PcacheFetch(
34443
 
  PCache *pCache,       /* Obtain the page from this cache */
34444
 
  Pgno pgno,            /* Page number to obtain */
34445
 
  int createFlag,       /* If true, create page if it does not exist already */
34446
 
  PgHdr **ppPage        /* Write the page here */
34447
 
){
34448
 
  PgHdr *pPage = 0;
34449
 
  int eCreate;
34450
 
 
34451
 
  assert( pCache!=0 );
34452
 
  assert( createFlag==1 || createFlag==0 );
34453
 
  assert( pgno>0 );
34454
 
 
34455
 
  /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
34456
 
  ** allocate it now.
34457
 
  */
34458
 
  if( !pCache->pCache && createFlag ){
34459
 
    sqlite3_pcache *p;
34460
 
    int nByte;
34461
 
    nByte = pCache->szPage + pCache->szExtra + sizeof(PgHdr);
34462
 
    p = sqlite3GlobalConfig.pcache.xCreate(nByte, pCache->bPurgeable);
34463
 
    if( !p ){
34464
 
      return SQLITE_NOMEM;
34465
 
    }
34466
 
    sqlite3GlobalConfig.pcache.xCachesize(p, pCache->nMax);
34467
 
    pCache->pCache = p;
34468
 
  }
34469
 
 
34470
 
  eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
34471
 
  if( pCache->pCache ){
34472
 
    pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, eCreate);
34473
 
  }
34474
 
 
34475
 
  if( !pPage && eCreate==1 ){
34476
 
    PgHdr *pPg;
34477
 
 
34478
 
    /* Find a dirty page to write-out and recycle. First try to find a 
34479
 
    ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
34480
 
    ** cleared), but if that is not possible settle for any other 
34481
 
    ** unreferenced dirty page.
34482
 
    */
34483
 
    expensive_assert( pcacheCheckSynced(pCache) );
34484
 
    for(pPg=pCache->pSynced; 
34485
 
        pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
34486
 
        pPg=pPg->pDirtyPrev
34487
 
    );
34488
 
    pCache->pSynced = pPg;
34489
 
    if( !pPg ){
34490
 
      for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
34491
 
    }
34492
 
    if( pPg ){
34493
 
      int rc;
34494
 
      rc = pCache->xStress(pCache->pStress, pPg);
34495
 
      if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
34496
 
        return rc;
34497
 
      }
34498
 
    }
34499
 
 
34500
 
    pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);
34501
 
  }
34502
 
 
34503
 
  if( pPage ){
34504
 
    if( !pPage->pData ){
34505
 
      memset(pPage, 0, sizeof(PgHdr));
34506
 
      pPage->pData = (void *)&pPage[1];
34507
 
      pPage->pExtra = (void*)&((char *)pPage->pData)[pCache->szPage];
34508
 
      memset(pPage->pExtra, 0, pCache->szExtra);
34509
 
      pPage->pCache = pCache;
34510
 
      pPage->pgno = pgno;
34511
 
    }
34512
 
    assert( pPage->pCache==pCache );
34513
 
    assert( pPage->pgno==pgno );
34514
 
    assert( pPage->pData==(void *)&pPage[1] );
34515
 
    assert( pPage->pExtra==(void *)&((char *)&pPage[1])[pCache->szPage] );
34516
 
 
34517
 
    if( 0==pPage->nRef ){
34518
 
      pCache->nRef++;
34519
 
    }
34520
 
    pPage->nRef++;
34521
 
    if( pgno==1 ){
34522
 
      pCache->pPage1 = pPage;
34523
 
    }
34524
 
  }
34525
 
  *ppPage = pPage;
34526
 
  return (pPage==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
34527
 
}
34528
 
 
34529
 
/*
34530
 
** Decrement the reference count on a page. If the page is clean and the
34531
 
** reference count drops to 0, then it is made elible for recycling.
34532
 
*/
34533
 
SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){
34534
 
  assert( p->nRef>0 );
34535
 
  p->nRef--;
34536
 
  if( p->nRef==0 ){
34537
 
    PCache *pCache = p->pCache;
34538
 
    pCache->nRef--;
34539
 
    if( (p->flags&PGHDR_DIRTY)==0 ){
34540
 
      pcacheUnpin(p);
34541
 
    }else{
34542
 
      /* Move the page to the head of the dirty list. */
34543
 
      pcacheRemoveFromDirtyList(p);
34544
 
      pcacheAddToDirtyList(p);
34545
 
    }
34546
 
  }
34547
 
}
34548
 
 
34549
 
/*
34550
 
** Increase the reference count of a supplied page by 1.
34551
 
*/
34552
 
SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
34553
 
  assert(p->nRef>0);
34554
 
  p->nRef++;
34555
 
}
34556
 
 
34557
 
/*
34558
 
** Drop a page from the cache. There must be exactly one reference to the
34559
 
** page. This function deletes that reference, so after it returns the
34560
 
** page pointed to by p is invalid.
34561
 
*/
34562
 
SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
34563
 
  PCache *pCache;
34564
 
  assert( p->nRef==1 );
34565
 
  if( p->flags&PGHDR_DIRTY ){
34566
 
    pcacheRemoveFromDirtyList(p);
34567
 
  }
34568
 
  pCache = p->pCache;
34569
 
  pCache->nRef--;
34570
 
  if( p->pgno==1 ){
34571
 
    pCache->pPage1 = 0;
34572
 
  }
34573
 
  sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 1);
34574
 
}
34575
 
 
34576
 
/*
34577
 
** Make sure the page is marked as dirty. If it isn't dirty already,
34578
 
** make it so.
34579
 
*/
34580
 
SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
34581
 
  p->flags &= ~PGHDR_DONT_WRITE;
34582
 
  assert( p->nRef>0 );
34583
 
  if( 0==(p->flags & PGHDR_DIRTY) ){
34584
 
    p->flags |= PGHDR_DIRTY;
34585
 
    pcacheAddToDirtyList( p);
34586
 
  }
34587
 
}
34588
 
 
34589
 
/*
34590
 
** Make sure the page is marked as clean. If it isn't clean already,
34591
 
** make it so.
34592
 
*/
34593
 
SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
34594
 
  if( (p->flags & PGHDR_DIRTY) ){
34595
 
    pcacheRemoveFromDirtyList(p);
34596
 
    p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
34597
 
    if( p->nRef==0 ){
34598
 
      pcacheUnpin(p);
34599
 
    }
34600
 
  }
34601
 
}
34602
 
 
34603
 
/*
34604
 
** Make every page in the cache clean.
34605
 
*/
34606
 
SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
34607
 
  PgHdr *p;
34608
 
  while( (p = pCache->pDirty)!=0 ){
34609
 
    sqlite3PcacheMakeClean(p);
34610
 
  }
34611
 
}
34612
 
 
34613
 
/*
34614
 
** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
34615
 
*/
34616
 
SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
34617
 
  PgHdr *p;
34618
 
  for(p=pCache->pDirty; p; p=p->pDirtyNext){
34619
 
    p->flags &= ~PGHDR_NEED_SYNC;
34620
 
  }
34621
 
  pCache->pSynced = pCache->pDirtyTail;
34622
 
}
34623
 
 
34624
 
/*
34625
 
** Change the page number of page p to newPgno. 
34626
 
*/
34627
 
SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
34628
 
  PCache *pCache = p->pCache;
34629
 
  assert( p->nRef>0 );
34630
 
  assert( newPgno>0 );
34631
 
  sqlite3GlobalConfig.pcache.xRekey(pCache->pCache, p, p->pgno, newPgno);
34632
 
  p->pgno = newPgno;
34633
 
  if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
34634
 
    pcacheRemoveFromDirtyList(p);
34635
 
    pcacheAddToDirtyList(p);
34636
 
  }
34637
 
}
34638
 
 
34639
 
/*
34640
 
** Drop every cache entry whose page number is greater than "pgno". The
34641
 
** caller must ensure that there are no outstanding references to any pages
34642
 
** other than page 1 with a page number greater than pgno.
34643
 
**
34644
 
** If there is a reference to page 1 and the pgno parameter passed to this
34645
 
** function is 0, then the data area associated with page 1 is zeroed, but
34646
 
** the page object is not dropped.
34647
 
*/
34648
 
SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
34649
 
  if( pCache->pCache ){
34650
 
    PgHdr *p;
34651
 
    PgHdr *pNext;
34652
 
    for(p=pCache->pDirty; p; p=pNext){
34653
 
      pNext = p->pDirtyNext;
34654
 
      /* This routine never gets call with a positive pgno except right
34655
 
      ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
34656
 
      ** it must be that pgno==0.
34657
 
      */
34658
 
      assert( p->pgno>0 );
34659
 
      if( ALWAYS(p->pgno>pgno) ){
34660
 
        assert( p->flags&PGHDR_DIRTY );
34661
 
        sqlite3PcacheMakeClean(p);
34662
 
      }
34663
 
    }
34664
 
    if( pgno==0 && pCache->pPage1 ){
34665
 
      memset(pCache->pPage1->pData, 0, pCache->szPage);
34666
 
      pgno = 1;
34667
 
    }
34668
 
    sqlite3GlobalConfig.pcache.xTruncate(pCache->pCache, pgno+1);
34669
 
  }
34670
 
}
34671
 
 
34672
 
/*
34673
 
** Close a cache.
34674
 
*/
34675
 
SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
34676
 
  if( pCache->pCache ){
34677
 
    sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
34678
 
  }
34679
 
}
34680
 
 
34681
 
/* 
34682
 
** Discard the contents of the cache.
34683
 
*/
34684
 
SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
34685
 
  sqlite3PcacheTruncate(pCache, 0);
34686
 
}
34687
 
 
34688
 
/*
34689
 
** Merge two lists of pages connected by pDirty and in pgno order.
34690
 
** Do not both fixing the pDirtyPrev pointers.
34691
 
*/
34692
 
static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
34693
 
  PgHdr result, *pTail;
34694
 
  pTail = &result;
34695
 
  while( pA && pB ){
34696
 
    if( pA->pgno<pB->pgno ){
34697
 
      pTail->pDirty = pA;
34698
 
      pTail = pA;
34699
 
      pA = pA->pDirty;
34700
 
    }else{
34701
 
      pTail->pDirty = pB;
34702
 
      pTail = pB;
34703
 
      pB = pB->pDirty;
34704
 
    }
34705
 
  }
34706
 
  if( pA ){
34707
 
    pTail->pDirty = pA;
34708
 
  }else if( pB ){
34709
 
    pTail->pDirty = pB;
34710
 
  }else{
34711
 
    pTail->pDirty = 0;
34712
 
  }
34713
 
  return result.pDirty;
34714
 
}
34715
 
 
34716
 
/*
34717
 
** Sort the list of pages in accending order by pgno.  Pages are
34718
 
** connected by pDirty pointers.  The pDirtyPrev pointers are
34719
 
** corrupted by this sort.
34720
 
**
34721
 
** Since there cannot be more than 2^31 distinct pages in a database,
34722
 
** there cannot be more than 31 buckets required by the merge sorter.
34723
 
** One extra bucket is added to catch overflow in case something
34724
 
** ever changes to make the previous sentence incorrect.
34725
 
*/
34726
 
#define N_SORT_BUCKET  32
34727
 
static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
34728
 
  PgHdr *a[N_SORT_BUCKET], *p;
34729
 
  int i;
34730
 
  memset(a, 0, sizeof(a));
34731
 
  while( pIn ){
34732
 
    p = pIn;
34733
 
    pIn = p->pDirty;
34734
 
    p->pDirty = 0;
34735
 
    for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
34736
 
      if( a[i]==0 ){
34737
 
        a[i] = p;
34738
 
        break;
34739
 
      }else{
34740
 
        p = pcacheMergeDirtyList(a[i], p);
34741
 
        a[i] = 0;
34742
 
      }
34743
 
    }
34744
 
    if( NEVER(i==N_SORT_BUCKET-1) ){
34745
 
      /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
34746
 
      ** the input list.  But that is impossible.
34747
 
      */
34748
 
      a[i] = pcacheMergeDirtyList(a[i], p);
34749
 
    }
34750
 
  }
34751
 
  p = a[0];
34752
 
  for(i=1; i<N_SORT_BUCKET; i++){
34753
 
    p = pcacheMergeDirtyList(p, a[i]);
34754
 
  }
34755
 
  return p;
34756
 
}
34757
 
 
34758
 
/*
34759
 
** Return a list of all dirty pages in the cache, sorted by page number.
34760
 
*/
34761
 
SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
34762
 
  PgHdr *p;
34763
 
  for(p=pCache->pDirty; p; p=p->pDirtyNext){
34764
 
    p->pDirty = p->pDirtyNext;
34765
 
  }
34766
 
  return pcacheSortDirtyList(pCache->pDirty);
34767
 
}
34768
 
 
34769
 
/* 
34770
 
** Return the total number of referenced pages held by the cache.
34771
 
*/
34772
 
SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
34773
 
  return pCache->nRef;
34774
 
}
34775
 
 
34776
 
/*
34777
 
** Return the number of references to the page supplied as an argument.
34778
 
*/
34779
 
SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
34780
 
  return p->nRef;
34781
 
}
34782
 
 
34783
 
/* 
34784
 
** Return the total number of pages in the cache.
34785
 
*/
34786
 
SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
34787
 
  int nPage = 0;
34788
 
  if( pCache->pCache ){
34789
 
    nPage = sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache);
34790
 
  }
34791
 
  return nPage;
34792
 
}
34793
 
 
34794
 
#ifdef SQLITE_TEST
34795
 
/*
34796
 
** Get the suggested cache-size value.
34797
 
*/
34798
 
SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
34799
 
  return pCache->nMax;
34800
 
}
34801
 
#endif
34802
 
 
34803
 
/*
34804
 
** Set the suggested cache-size value.
34805
 
*/
34806
 
SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
34807
 
  pCache->nMax = mxPage;
34808
 
  if( pCache->pCache ){
34809
 
    sqlite3GlobalConfig.pcache.xCachesize(pCache->pCache, mxPage);
34810
 
  }
34811
 
}
34812
 
 
34813
 
#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
34814
 
/*
34815
 
** For all dirty pages currently in the cache, invoke the specified
34816
 
** callback. This is only used if the SQLITE_CHECK_PAGES macro is
34817
 
** defined.
34818
 
*/
34819
 
SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
34820
 
  PgHdr *pDirty;
34821
 
  for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
34822
 
    xIter(pDirty);
34823
 
  }
34824
 
}
34825
 
#endif
34826
 
 
34827
 
/************** End of pcache.c **********************************************/
34828
 
/************** Begin file pcache1.c *****************************************/
34829
 
/*
34830
 
** 2008 November 05
34831
 
**
34832
 
** The author disclaims copyright to this source code.  In place of
34833
 
** a legal notice, here is a blessing:
34834
 
**
34835
 
**    May you do good and not evil.
34836
 
**    May you find forgiveness for yourself and forgive others.
34837
 
**    May you share freely, never taking more than you give.
34838
 
**
34839
 
*************************************************************************
34840
 
**
34841
 
** This file implements the default page cache implementation (the
34842
 
** sqlite3_pcache interface). It also contains part of the implementation
34843
 
** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
34844
 
** If the default page cache implementation is overriden, then neither of
34845
 
** these two features are available.
34846
 
*/
34847
 
 
34848
 
 
34849
 
typedef struct PCache1 PCache1;
34850
 
typedef struct PgHdr1 PgHdr1;
34851
 
typedef struct PgFreeslot PgFreeslot;
34852
 
typedef struct PGroup PGroup;
34853
 
 
34854
 
/* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
34855
 
** of one or more PCaches that are able to recycle each others unpinned
34856
 
** pages when they are under memory pressure.  A PGroup is an instance of
34857
 
** the following object.
34858
 
**
34859
 
** This page cache implementation works in one of two modes:
34860
 
**
34861
 
**   (1)  Every PCache is the sole member of its own PGroup.  There is
34862
 
**        one PGroup per PCache.
34863
 
**
34864
 
**   (2)  There is a single global PGroup that all PCaches are a member
34865
 
**        of.
34866
 
**
34867
 
** Mode 1 uses more memory (since PCache instances are not able to rob
34868
 
** unused pages from other PCaches) but it also operates without a mutex,
34869
 
** and is therefore often faster.  Mode 2 requires a mutex in order to be
34870
 
** threadsafe, but is able recycle pages more efficient.
34871
 
**
34872
 
** For mode (1), PGroup.mutex is NULL.  For mode (2) there is only a single
34873
 
** PGroup which is the pcache1.grp global variable and its mutex is
34874
 
** SQLITE_MUTEX_STATIC_LRU.
34875
 
*/
34876
 
struct PGroup {
34877
 
  sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
34878
 
  int nMaxPage;                  /* Sum of nMax for purgeable caches */
34879
 
  int nMinPage;                  /* Sum of nMin for purgeable caches */
34880
 
  int mxPinned;                  /* nMaxpage + 10 - nMinPage */
34881
 
  int nCurrentPage;              /* Number of purgeable pages allocated */
34882
 
  PgHdr1 *pLruHead, *pLruTail;   /* LRU list of unpinned pages */
34883
 
};
34884
 
 
34885
 
/* Each page cache is an instance of the following object.  Every
34886
 
** open database file (including each in-memory database and each
34887
 
** temporary or transient database) has a single page cache which
34888
 
** is an instance of this object.
34889
 
**
34890
 
** Pointers to structures of this type are cast and returned as 
34891
 
** opaque sqlite3_pcache* handles.
34892
 
*/
34893
 
struct PCache1 {
34894
 
  /* Cache configuration parameters. Page size (szPage) and the purgeable
34895
 
  ** flag (bPurgeable) are set when the cache is created. nMax may be 
34896
 
  ** modified at any time by a call to the pcache1CacheSize() method.
34897
 
  ** The PGroup mutex must be held when accessing nMax.
34898
 
  */
34899
 
  PGroup *pGroup;                     /* PGroup this cache belongs to */
34900
 
  int szPage;                         /* Size of allocated pages in bytes */
34901
 
  int bPurgeable;                     /* True if cache is purgeable */
34902
 
  unsigned int nMin;                  /* Minimum number of pages reserved */
34903
 
  unsigned int nMax;                  /* Configured "cache_size" value */
34904
 
  unsigned int n90pct;                /* nMax*9/10 */
34905
 
 
34906
 
  /* Hash table of all pages. The following variables may only be accessed
34907
 
  ** when the accessor is holding the PGroup mutex.
34908
 
  */
34909
 
  unsigned int nRecyclable;           /* Number of pages in the LRU list */
34910
 
  unsigned int nPage;                 /* Total number of pages in apHash */
34911
 
  unsigned int nHash;                 /* Number of slots in apHash[] */
34912
 
  PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
34913
 
 
34914
 
  unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
34915
 
};
34916
 
 
34917
 
/*
34918
 
** Each cache entry is represented by an instance of the following 
34919
 
** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated 
34920
 
** directly before this structure in memory (see the PGHDR1_TO_PAGE() 
34921
 
** macro below).
34922
 
*/
34923
 
struct PgHdr1 {
34924
 
  unsigned int iKey;             /* Key value (page number) */
34925
 
  PgHdr1 *pNext;                 /* Next in hash table chain */
34926
 
  PCache1 *pCache;               /* Cache that currently owns this page */
34927
 
  PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
34928
 
  PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
34929
 
};
34930
 
 
34931
 
/*
34932
 
** Free slots in the allocator used to divide up the buffer provided using
34933
 
** the SQLITE_CONFIG_PAGECACHE mechanism.
34934
 
*/
34935
 
struct PgFreeslot {
34936
 
  PgFreeslot *pNext;  /* Next free slot */
34937
 
};
34938
 
 
34939
 
/*
34940
 
** Global data used by this cache.
34941
 
*/
34942
 
static SQLITE_WSD struct PCacheGlobal {
34943
 
  PGroup grp;                    /* The global PGroup for mode (2) */
34944
 
 
34945
 
  /* Variables related to SQLITE_CONFIG_PAGECACHE settings.  The
34946
 
  ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
34947
 
  ** fixed at sqlite3_initialize() time and do not require mutex protection.
34948
 
  ** The nFreeSlot and pFree values do require mutex protection.
34949
 
  */
34950
 
  int isInit;                    /* True if initialized */
34951
 
  int szSlot;                    /* Size of each free slot */
34952
 
  int nSlot;                     /* The number of pcache slots */
34953
 
  int nReserve;                  /* Try to keep nFreeSlot above this */
34954
 
  void *pStart, *pEnd;           /* Bounds of pagecache malloc range */
34955
 
  /* Above requires no mutex.  Use mutex below for variable that follow. */
34956
 
  sqlite3_mutex *mutex;          /* Mutex for accessing the following: */
34957
 
  int nFreeSlot;                 /* Number of unused pcache slots */
34958
 
  PgFreeslot *pFree;             /* Free page blocks */
34959
 
  /* The following value requires a mutex to change.  We skip the mutex on
34960
 
  ** reading because (1) most platforms read a 32-bit integer atomically and
34961
 
  ** (2) even if an incorrect value is read, no great harm is done since this
34962
 
  ** is really just an optimization. */
34963
 
  int bUnderPressure;            /* True if low on PAGECACHE memory */
34964
 
} pcache1_g;
34965
 
 
34966
 
/*
34967
 
** All code in this file should access the global structure above via the
34968
 
** alias "pcache1". This ensures that the WSD emulation is used when
34969
 
** compiling for systems that do not support real WSD.
34970
 
*/
34971
 
#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
34972
 
 
34973
 
/*
34974
 
** When a PgHdr1 structure is allocated, the associated PCache1.szPage
34975
 
** bytes of data are located directly before it in memory (i.e. the total
34976
 
** size of the allocation is sizeof(PgHdr1)+PCache1.szPage byte). The
34977
 
** PGHDR1_TO_PAGE() macro takes a pointer to a PgHdr1 structure as
34978
 
** an argument and returns a pointer to the associated block of szPage
34979
 
** bytes. The PAGE_TO_PGHDR1() macro does the opposite: its argument is
34980
 
** a pointer to a block of szPage bytes of data and the return value is
34981
 
** a pointer to the associated PgHdr1 structure.
34982
 
**
34983
 
**   assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
34984
 
*/
34985
 
#define PGHDR1_TO_PAGE(p)    (void*)(((char*)p) - p->pCache->szPage)
34986
 
#define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
34987
 
 
34988
 
/*
34989
 
** Macros to enter and leave the PCache LRU mutex.
34990
 
*/
34991
 
#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
34992
 
#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
34993
 
 
34994
 
/******************************************************************************/
34995
 
/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
34996
 
 
34997
 
/*
34998
 
** This function is called during initialization if a static buffer is 
34999
 
** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
35000
 
** verb to sqlite3_config(). Parameter pBuf points to an allocation large
35001
 
** enough to contain 'n' buffers of 'sz' bytes each.
35002
 
**
35003
 
** This routine is called from sqlite3_initialize() and so it is guaranteed
35004
 
** to be serialized already.  There is no need for further mutexing.
35005
 
*/
35006
 
SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
35007
 
  if( pcache1.isInit ){
35008
 
    PgFreeslot *p;
35009
 
    sz = ROUNDDOWN8(sz);
35010
 
    pcache1.szSlot = sz;
35011
 
    pcache1.nSlot = pcache1.nFreeSlot = n;
35012
 
    pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
35013
 
    pcache1.pStart = pBuf;
35014
 
    pcache1.pFree = 0;
35015
 
    pcache1.bUnderPressure = 0;
35016
 
    while( n-- ){
35017
 
      p = (PgFreeslot*)pBuf;
35018
 
      p->pNext = pcache1.pFree;
35019
 
      pcache1.pFree = p;
35020
 
      pBuf = (void*)&((char*)pBuf)[sz];
35021
 
    }
35022
 
    pcache1.pEnd = pBuf;
35023
 
  }
35024
 
}
35025
 
 
35026
 
/*
35027
 
** Malloc function used within this file to allocate space from the buffer
35028
 
** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no 
35029
 
** such buffer exists or there is no space left in it, this function falls 
35030
 
** back to sqlite3Malloc().
35031
 
**
35032
 
** Multiple threads can run this routine at the same time.  Global variables
35033
 
** in pcache1 need to be protected via mutex.
35034
 
*/
35035
 
static void *pcache1Alloc(int nByte){
35036
 
  void *p = 0;
35037
 
  assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
35038
 
  sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
35039
 
  if( nByte<=pcache1.szSlot ){
35040
 
    sqlite3_mutex_enter(pcache1.mutex);
35041
 
    p = (PgHdr1 *)pcache1.pFree;
35042
 
    if( p ){
35043
 
      pcache1.pFree = pcache1.pFree->pNext;
35044
 
      pcache1.nFreeSlot--;
35045
 
      pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
35046
 
      assert( pcache1.nFreeSlot>=0 );
35047
 
      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
35048
 
    }
35049
 
    sqlite3_mutex_leave(pcache1.mutex);
35050
 
  }
35051
 
  if( p==0 ){
35052
 
    /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
35053
 
    ** it from sqlite3Malloc instead.
35054
 
    */
35055
 
    p = sqlite3Malloc(nByte);
35056
 
    if( p ){
35057
 
      int sz = sqlite3MallocSize(p);
35058
 
      sqlite3_mutex_enter(pcache1.mutex);
35059
 
      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
35060
 
      sqlite3_mutex_leave(pcache1.mutex);
35061
 
    }
35062
 
    sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
35063
 
  }
35064
 
  return p;
35065
 
}
35066
 
 
35067
 
/*
35068
 
** Free an allocated buffer obtained from pcache1Alloc().
35069
 
*/
35070
 
static void pcache1Free(void *p){
35071
 
  if( p==0 ) return;
35072
 
  if( p>=pcache1.pStart && p<pcache1.pEnd ){
35073
 
    PgFreeslot *pSlot;
35074
 
    sqlite3_mutex_enter(pcache1.mutex);
35075
 
    sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
35076
 
    pSlot = (PgFreeslot*)p;
35077
 
    pSlot->pNext = pcache1.pFree;
35078
 
    pcache1.pFree = pSlot;
35079
 
    pcache1.nFreeSlot++;
35080
 
    pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
35081
 
    assert( pcache1.nFreeSlot<=pcache1.nSlot );
35082
 
    sqlite3_mutex_leave(pcache1.mutex);
35083
 
  }else{
35084
 
    int iSize;
35085
 
    assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
35086
 
    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
35087
 
    iSize = sqlite3MallocSize(p);
35088
 
    sqlite3_mutex_enter(pcache1.mutex);
35089
 
    sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
35090
 
    sqlite3_mutex_leave(pcache1.mutex);
35091
 
    sqlite3_free(p);
35092
 
  }
35093
 
}
35094
 
 
35095
 
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
35096
 
/*
35097
 
** Return the size of a pcache allocation
35098
 
*/
35099
 
static int pcache1MemSize(void *p){
35100
 
  if( p>=pcache1.pStart && p<pcache1.pEnd ){
35101
 
    return pcache1.szSlot;
35102
 
  }else{
35103
 
    int iSize;
35104
 
    assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
35105
 
    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
35106
 
    iSize = sqlite3MallocSize(p);
35107
 
    sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
35108
 
    return iSize;
35109
 
  }
35110
 
}
35111
 
#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
35112
 
 
35113
 
/*
35114
 
** Allocate a new page object initially associated with cache pCache.
35115
 
*/
35116
 
static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
35117
 
  int nByte = sizeof(PgHdr1) + pCache->szPage;
35118
 
  void *pPg = pcache1Alloc(nByte);
35119
 
  PgHdr1 *p;
35120
 
  if( pPg ){
35121
 
    p = PAGE_TO_PGHDR1(pCache, pPg);
35122
 
    if( pCache->bPurgeable ){
35123
 
      pCache->pGroup->nCurrentPage++;
35124
 
    }
35125
 
  }else{
35126
 
    p = 0;
35127
 
  }
35128
 
  return p;
35129
 
}
35130
 
 
35131
 
/*
35132
 
** Free a page object allocated by pcache1AllocPage().
35133
 
**
35134
 
** The pointer is allowed to be NULL, which is prudent.  But it turns out
35135
 
** that the current implementation happens to never call this routine
35136
 
** with a NULL pointer, so we mark the NULL test with ALWAYS().
35137
 
*/
35138
 
static void pcache1FreePage(PgHdr1 *p){
35139
 
  if( ALWAYS(p) ){
35140
 
    PCache1 *pCache = p->pCache;
35141
 
    if( pCache->bPurgeable ){
35142
 
      pCache->pGroup->nCurrentPage--;
35143
 
    }
35144
 
    pcache1Free(PGHDR1_TO_PAGE(p));
35145
 
  }
35146
 
}
35147
 
 
35148
 
/*
35149
 
** Malloc function used by SQLite to obtain space from the buffer configured
35150
 
** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
35151
 
** exists, this function falls back to sqlite3Malloc().
35152
 
*/
35153
 
SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
35154
 
  return pcache1Alloc(sz);
35155
 
}
35156
 
 
35157
 
/*
35158
 
** Free an allocated buffer obtained from sqlite3PageMalloc().
35159
 
*/
35160
 
SQLITE_PRIVATE void sqlite3PageFree(void *p){
35161
 
  pcache1Free(p);
35162
 
}
35163
 
 
35164
 
 
35165
 
/*
35166
 
** Return true if it desirable to avoid allocating a new page cache
35167
 
** entry.
35168
 
**
35169
 
** If memory was allocated specifically to the page cache using
35170
 
** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
35171
 
** it is desirable to avoid allocating a new page cache entry because
35172
 
** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
35173
 
** for all page cache needs and we should not need to spill the
35174
 
** allocation onto the heap.
35175
 
**
35176
 
** Or, the heap is used for all page cache memory put the heap is
35177
 
** under memory pressure, then again it is desirable to avoid
35178
 
** allocating a new page cache entry in order to avoid stressing
35179
 
** the heap even further.
35180
 
*/
35181
 
static int pcache1UnderMemoryPressure(PCache1 *pCache){
35182
 
  if( pcache1.nSlot && pCache->szPage<=pcache1.szSlot ){
35183
 
    return pcache1.bUnderPressure;
35184
 
  }else{
35185
 
    return sqlite3HeapNearlyFull();
35186
 
  }
35187
 
}
35188
 
 
35189
 
/******************************************************************************/
35190
 
/******** General Implementation Functions ************************************/
35191
 
 
35192
 
/*
35193
 
** This function is used to resize the hash table used by the cache passed
35194
 
** as the first argument.
35195
 
**
35196
 
** The PCache mutex must be held when this function is called.
35197
 
*/
35198
 
static int pcache1ResizeHash(PCache1 *p){
35199
 
  PgHdr1 **apNew;
35200
 
  unsigned int nNew;
35201
 
  unsigned int i;
35202
 
 
35203
 
  assert( sqlite3_mutex_held(p->pGroup->mutex) );
35204
 
 
35205
 
  nNew = p->nHash*2;
35206
 
  if( nNew<256 ){
35207
 
    nNew = 256;
35208
 
  }
35209
 
 
35210
 
  pcache1LeaveMutex(p->pGroup);
35211
 
  if( p->nHash ){ sqlite3BeginBenignMalloc(); }
35212
 
  apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
35213
 
  if( p->nHash ){ sqlite3EndBenignMalloc(); }
35214
 
  pcache1EnterMutex(p->pGroup);
35215
 
  if( apNew ){
35216
 
    memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
35217
 
    for(i=0; i<p->nHash; i++){
35218
 
      PgHdr1 *pPage;
35219
 
      PgHdr1 *pNext = p->apHash[i];
35220
 
      while( (pPage = pNext)!=0 ){
35221
 
        unsigned int h = pPage->iKey % nNew;
35222
 
        pNext = pPage->pNext;
35223
 
        pPage->pNext = apNew[h];
35224
 
        apNew[h] = pPage;
35225
 
      }
35226
 
    }
35227
 
    sqlite3_free(p->apHash);
35228
 
    p->apHash = apNew;
35229
 
    p->nHash = nNew;
35230
 
  }
35231
 
 
35232
 
  return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
35233
 
}
35234
 
 
35235
 
/*
35236
 
** This function is used internally to remove the page pPage from the 
35237
 
** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
35238
 
** LRU list, then this function is a no-op.
35239
 
**
35240
 
** The PGroup mutex must be held when this function is called.
35241
 
**
35242
 
** If pPage is NULL then this routine is a no-op.
35243
 
*/
35244
 
static void pcache1PinPage(PgHdr1 *pPage){
35245
 
  PCache1 *pCache;
35246
 
  PGroup *pGroup;
35247
 
 
35248
 
  if( pPage==0 ) return;
35249
 
  pCache = pPage->pCache;
35250
 
  pGroup = pCache->pGroup;
35251
 
  assert( sqlite3_mutex_held(pGroup->mutex) );
35252
 
  if( pPage->pLruNext || pPage==pGroup->pLruTail ){
35253
 
    if( pPage->pLruPrev ){
35254
 
      pPage->pLruPrev->pLruNext = pPage->pLruNext;
35255
 
    }
35256
 
    if( pPage->pLruNext ){
35257
 
      pPage->pLruNext->pLruPrev = pPage->pLruPrev;
35258
 
    }
35259
 
    if( pGroup->pLruHead==pPage ){
35260
 
      pGroup->pLruHead = pPage->pLruNext;
35261
 
    }
35262
 
    if( pGroup->pLruTail==pPage ){
35263
 
      pGroup->pLruTail = pPage->pLruPrev;
35264
 
    }
35265
 
    pPage->pLruNext = 0;
35266
 
    pPage->pLruPrev = 0;
35267
 
    pPage->pCache->nRecyclable--;
35268
 
  }
35269
 
}
35270
 
 
35271
 
 
35272
 
/*
35273
 
** Remove the page supplied as an argument from the hash table 
35274
 
** (PCache1.apHash structure) that it is currently stored in.
35275
 
**
35276
 
** The PGroup mutex must be held when this function is called.
35277
 
*/
35278
 
static void pcache1RemoveFromHash(PgHdr1 *pPage){
35279
 
  unsigned int h;
35280
 
  PCache1 *pCache = pPage->pCache;
35281
 
  PgHdr1 **pp;
35282
 
 
35283
 
  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
35284
 
  h = pPage->iKey % pCache->nHash;
35285
 
  for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
35286
 
  *pp = (*pp)->pNext;
35287
 
 
35288
 
  pCache->nPage--;
35289
 
}
35290
 
 
35291
 
/*
35292
 
** If there are currently more than nMaxPage pages allocated, try
35293
 
** to recycle pages to reduce the number allocated to nMaxPage.
35294
 
*/
35295
 
static void pcache1EnforceMaxPage(PGroup *pGroup){
35296
 
  assert( sqlite3_mutex_held(pGroup->mutex) );
35297
 
  while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
35298
 
    PgHdr1 *p = pGroup->pLruTail;
35299
 
    assert( p->pCache->pGroup==pGroup );
35300
 
    pcache1PinPage(p);
35301
 
    pcache1RemoveFromHash(p);
35302
 
    pcache1FreePage(p);
35303
 
  }
35304
 
}
35305
 
 
35306
 
/*
35307
 
** Discard all pages from cache pCache with a page number (key value) 
35308
 
** greater than or equal to iLimit. Any pinned pages that meet this 
35309
 
** criteria are unpinned before they are discarded.
35310
 
**
35311
 
** The PCache mutex must be held when this function is called.
35312
 
*/
35313
 
static void pcache1TruncateUnsafe(
35314
 
  PCache1 *pCache,             /* The cache to truncate */
35315
 
  unsigned int iLimit          /* Drop pages with this pgno or larger */
35316
 
){
35317
 
  TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
35318
 
  unsigned int h;
35319
 
  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
35320
 
  for(h=0; h<pCache->nHash; h++){
35321
 
    PgHdr1 **pp = &pCache->apHash[h]; 
35322
 
    PgHdr1 *pPage;
35323
 
    while( (pPage = *pp)!=0 ){
35324
 
      if( pPage->iKey>=iLimit ){
35325
 
        pCache->nPage--;
35326
 
        *pp = pPage->pNext;
35327
 
        pcache1PinPage(pPage);
35328
 
        pcache1FreePage(pPage);
35329
 
      }else{
35330
 
        pp = &pPage->pNext;
35331
 
        TESTONLY( nPage++; )
35332
 
      }
35333
 
    }
35334
 
  }
35335
 
  assert( pCache->nPage==nPage );
35336
 
}
35337
 
 
35338
 
/******************************************************************************/
35339
 
/******** sqlite3_pcache Methods **********************************************/
35340
 
 
35341
 
/*
35342
 
** Implementation of the sqlite3_pcache.xInit method.
35343
 
*/
35344
 
static int pcache1Init(void *NotUsed){
35345
 
  UNUSED_PARAMETER(NotUsed);
35346
 
  assert( pcache1.isInit==0 );
35347
 
  memset(&pcache1, 0, sizeof(pcache1));
35348
 
  if( sqlite3GlobalConfig.bCoreMutex ){
35349
 
    pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
35350
 
    pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
35351
 
  }
35352
 
  pcache1.grp.mxPinned = 10;
35353
 
  pcache1.isInit = 1;
35354
 
  return SQLITE_OK;
35355
 
}
35356
 
 
35357
 
/*
35358
 
** Implementation of the sqlite3_pcache.xShutdown method.
35359
 
** Note that the static mutex allocated in xInit does 
35360
 
** not need to be freed.
35361
 
*/
35362
 
static void pcache1Shutdown(void *NotUsed){
35363
 
  UNUSED_PARAMETER(NotUsed);
35364
 
  assert( pcache1.isInit!=0 );
35365
 
  memset(&pcache1, 0, sizeof(pcache1));
35366
 
}
35367
 
 
35368
 
/*
35369
 
** Implementation of the sqlite3_pcache.xCreate method.
35370
 
**
35371
 
** Allocate a new cache.
35372
 
*/
35373
 
static sqlite3_pcache *pcache1Create(int szPage, int bPurgeable){
35374
 
  PCache1 *pCache;      /* The newly created page cache */
35375
 
  PGroup *pGroup;       /* The group the new page cache will belong to */
35376
 
  int sz;               /* Bytes of memory required to allocate the new cache */
35377
 
 
35378
 
  /*
35379
 
  ** The seperateCache variable is true if each PCache has its own private
35380
 
  ** PGroup.  In other words, separateCache is true for mode (1) where no
35381
 
  ** mutexing is required.
35382
 
  **
35383
 
  **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
35384
 
  **
35385
 
  **   *  Always use a unified cache in single-threaded applications
35386
 
  **
35387
 
  **   *  Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
35388
 
  **      use separate caches (mode-1)
35389
 
  */
35390
 
#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
35391
 
  const int separateCache = 0;
35392
 
#else
35393
 
  int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
35394
 
#endif
35395
 
 
35396
 
  sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
35397
 
  pCache = (PCache1 *)sqlite3_malloc(sz);
35398
 
  if( pCache ){
35399
 
    memset(pCache, 0, sz);
35400
 
    if( separateCache ){
35401
 
      pGroup = (PGroup*)&pCache[1];
35402
 
      pGroup->mxPinned = 10;
35403
 
    }else{
35404
 
      pGroup = &pcache1_g.grp;
35405
 
    }
35406
 
    pCache->pGroup = pGroup;
35407
 
    pCache->szPage = szPage;
35408
 
    pCache->bPurgeable = (bPurgeable ? 1 : 0);
35409
 
    if( bPurgeable ){
35410
 
      pCache->nMin = 10;
35411
 
      pcache1EnterMutex(pGroup);
35412
 
      pGroup->nMinPage += pCache->nMin;
35413
 
      pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
35414
 
      pcache1LeaveMutex(pGroup);
35415
 
    }
35416
 
  }
35417
 
  return (sqlite3_pcache *)pCache;
35418
 
}
35419
 
 
35420
 
/*
35421
 
** Implementation of the sqlite3_pcache.xCachesize method. 
35422
 
**
35423
 
** Configure the cache_size limit for a cache.
35424
 
*/
35425
 
static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
35426
 
  PCache1 *pCache = (PCache1 *)p;
35427
 
  if( pCache->bPurgeable ){
35428
 
    PGroup *pGroup = pCache->pGroup;
35429
 
    pcache1EnterMutex(pGroup);
35430
 
    pGroup->nMaxPage += (nMax - pCache->nMax);
35431
 
    pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
35432
 
    pCache->nMax = nMax;
35433
 
    pCache->n90pct = pCache->nMax*9/10;
35434
 
    pcache1EnforceMaxPage(pGroup);
35435
 
    pcache1LeaveMutex(pGroup);
35436
 
  }
35437
 
}
35438
 
 
35439
 
/*
35440
 
** Implementation of the sqlite3_pcache.xPagecount method. 
35441
 
*/
35442
 
static int pcache1Pagecount(sqlite3_pcache *p){
35443
 
  int n;
35444
 
  PCache1 *pCache = (PCache1*)p;
35445
 
  pcache1EnterMutex(pCache->pGroup);
35446
 
  n = pCache->nPage;
35447
 
  pcache1LeaveMutex(pCache->pGroup);
35448
 
  return n;
35449
 
}
35450
 
 
35451
 
/*
35452
 
** Implementation of the sqlite3_pcache.xFetch method. 
35453
 
**
35454
 
** Fetch a page by key value.
35455
 
**
35456
 
** Whether or not a new page may be allocated by this function depends on
35457
 
** the value of the createFlag argument.  0 means do not allocate a new
35458
 
** page.  1 means allocate a new page if space is easily available.  2 
35459
 
** means to try really hard to allocate a new page.
35460
 
**
35461
 
** For a non-purgeable cache (a cache used as the storage for an in-memory
35462
 
** database) there is really no difference between createFlag 1 and 2.  So
35463
 
** the calling function (pcache.c) will never have a createFlag of 1 on
35464
 
** a non-purgable cache.
35465
 
**
35466
 
** There are three different approaches to obtaining space for a page,
35467
 
** depending on the value of parameter createFlag (which may be 0, 1 or 2).
35468
 
**
35469
 
**   1. Regardless of the value of createFlag, the cache is searched for a 
35470
 
**      copy of the requested page. If one is found, it is returned.
35471
 
**
35472
 
**   2. If createFlag==0 and the page is not already in the cache, NULL is
35473
 
**      returned.
35474
 
**
35475
 
**   3. If createFlag is 1, and the page is not already in the cache, then
35476
 
**      return NULL (do not allocate a new page) if any of the following
35477
 
**      conditions are true:
35478
 
**
35479
 
**       (a) the number of pages pinned by the cache is greater than
35480
 
**           PCache1.nMax, or
35481
 
**
35482
 
**       (b) the number of pages pinned by the cache is greater than
35483
 
**           the sum of nMax for all purgeable caches, less the sum of 
35484
 
**           nMin for all other purgeable caches, or
35485
 
**
35486
 
**   4. If none of the first three conditions apply and the cache is marked
35487
 
**      as purgeable, and if one of the following is true:
35488
 
**
35489
 
**       (a) The number of pages allocated for the cache is already 
35490
 
**           PCache1.nMax, or
35491
 
**
35492
 
**       (b) The number of pages allocated for all purgeable caches is
35493
 
**           already equal to or greater than the sum of nMax for all
35494
 
**           purgeable caches,
35495
 
**
35496
 
**       (c) The system is under memory pressure and wants to avoid
35497
 
**           unnecessary pages cache entry allocations
35498
 
**
35499
 
**      then attempt to recycle a page from the LRU list. If it is the right
35500
 
**      size, return the recycled buffer. Otherwise, free the buffer and
35501
 
**      proceed to step 5. 
35502
 
**
35503
 
**   5. Otherwise, allocate and return a new page buffer.
35504
 
*/
35505
 
static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
35506
 
  int nPinned;
35507
 
  PCache1 *pCache = (PCache1 *)p;
35508
 
  PGroup *pGroup;
35509
 
  PgHdr1 *pPage = 0;
35510
 
 
35511
 
  assert( pCache->bPurgeable || createFlag!=1 );
35512
 
  assert( pCache->bPurgeable || pCache->nMin==0 );
35513
 
  assert( pCache->bPurgeable==0 || pCache->nMin==10 );
35514
 
  assert( pCache->nMin==0 || pCache->bPurgeable );
35515
 
  pcache1EnterMutex(pGroup = pCache->pGroup);
35516
 
 
35517
 
  /* Step 1: Search the hash table for an existing entry. */
35518
 
  if( pCache->nHash>0 ){
35519
 
    unsigned int h = iKey % pCache->nHash;
35520
 
    for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
35521
 
  }
35522
 
 
35523
 
  /* Step 2: Abort if no existing page is found and createFlag is 0 */
35524
 
  if( pPage || createFlag==0 ){
35525
 
    pcache1PinPage(pPage);
35526
 
    goto fetch_out;
35527
 
  }
35528
 
 
35529
 
  /* The pGroup local variable will normally be initialized by the
35530
 
  ** pcache1EnterMutex() macro above.  But if SQLITE_MUTEX_OMIT is defined,
35531
 
  ** then pcache1EnterMutex() is a no-op, so we have to initialize the
35532
 
  ** local variable here.  Delaying the initialization of pGroup is an
35533
 
  ** optimization:  The common case is to exit the module before reaching
35534
 
  ** this point.
35535
 
  */
35536
 
#ifdef SQLITE_MUTEX_OMIT
35537
 
  pGroup = pCache->pGroup;
35538
 
#endif
35539
 
 
35540
 
 
35541
 
  /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
35542
 
  nPinned = pCache->nPage - pCache->nRecyclable;
35543
 
  assert( nPinned>=0 );
35544
 
  assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
35545
 
  assert( pCache->n90pct == pCache->nMax*9/10 );
35546
 
  if( createFlag==1 && (
35547
 
        nPinned>=pGroup->mxPinned
35548
 
     || nPinned>=(int)pCache->n90pct
35549
 
     || pcache1UnderMemoryPressure(pCache)
35550
 
  )){
35551
 
    goto fetch_out;
35552
 
  }
35553
 
 
35554
 
  if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
35555
 
    goto fetch_out;
35556
 
  }
35557
 
 
35558
 
  /* Step 4. Try to recycle a page. */
35559
 
  if( pCache->bPurgeable && pGroup->pLruTail && (
35560
 
         (pCache->nPage+1>=pCache->nMax)
35561
 
      || pGroup->nCurrentPage>=pGroup->nMaxPage
35562
 
      || pcache1UnderMemoryPressure(pCache)
35563
 
  )){
35564
 
    PCache1 *pOtherCache;
35565
 
    pPage = pGroup->pLruTail;
35566
 
    pcache1RemoveFromHash(pPage);
35567
 
    pcache1PinPage(pPage);
35568
 
    if( (pOtherCache = pPage->pCache)->szPage!=pCache->szPage ){
35569
 
      pcache1FreePage(pPage);
35570
 
      pPage = 0;
35571
 
    }else{
35572
 
      pGroup->nCurrentPage -= 
35573
 
               (pOtherCache->bPurgeable - pCache->bPurgeable);
35574
 
    }
35575
 
  }
35576
 
 
35577
 
  /* Step 5. If a usable page buffer has still not been found, 
35578
 
  ** attempt to allocate a new one. 
35579
 
  */
35580
 
  if( !pPage ){
35581
 
    if( createFlag==1 ) sqlite3BeginBenignMalloc();
35582
 
    pcache1LeaveMutex(pGroup);
35583
 
    pPage = pcache1AllocPage(pCache);
35584
 
    pcache1EnterMutex(pGroup);
35585
 
    if( createFlag==1 ) sqlite3EndBenignMalloc();
35586
 
  }
35587
 
 
35588
 
  if( pPage ){
35589
 
    unsigned int h = iKey % pCache->nHash;
35590
 
    pCache->nPage++;
35591
 
    pPage->iKey = iKey;
35592
 
    pPage->pNext = pCache->apHash[h];
35593
 
    pPage->pCache = pCache;
35594
 
    pPage->pLruPrev = 0;
35595
 
    pPage->pLruNext = 0;
35596
 
    *(void **)(PGHDR1_TO_PAGE(pPage)) = 0;
35597
 
    pCache->apHash[h] = pPage;
35598
 
  }
35599
 
 
35600
 
fetch_out:
35601
 
  if( pPage && iKey>pCache->iMaxKey ){
35602
 
    pCache->iMaxKey = iKey;
35603
 
  }
35604
 
  pcache1LeaveMutex(pGroup);
35605
 
  return (pPage ? PGHDR1_TO_PAGE(pPage) : 0);
35606
 
}
35607
 
 
35608
 
 
35609
 
/*
35610
 
** Implementation of the sqlite3_pcache.xUnpin method.
35611
 
**
35612
 
** Mark a page as unpinned (eligible for asynchronous recycling).
35613
 
*/
35614
 
static void pcache1Unpin(sqlite3_pcache *p, void *pPg, int reuseUnlikely){
35615
 
  PCache1 *pCache = (PCache1 *)p;
35616
 
  PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
35617
 
  PGroup *pGroup = pCache->pGroup;
35618
 
 
35619
 
  assert( pPage->pCache==pCache );
35620
 
  pcache1EnterMutex(pGroup);
35621
 
 
35622
 
  /* It is an error to call this function if the page is already 
35623
 
  ** part of the PGroup LRU list.
35624
 
  */
35625
 
  assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
35626
 
  assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
35627
 
 
35628
 
  if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
35629
 
    pcache1RemoveFromHash(pPage);
35630
 
    pcache1FreePage(pPage);
35631
 
  }else{
35632
 
    /* Add the page to the PGroup LRU list. */
35633
 
    if( pGroup->pLruHead ){
35634
 
      pGroup->pLruHead->pLruPrev = pPage;
35635
 
      pPage->pLruNext = pGroup->pLruHead;
35636
 
      pGroup->pLruHead = pPage;
35637
 
    }else{
35638
 
      pGroup->pLruTail = pPage;
35639
 
      pGroup->pLruHead = pPage;
35640
 
    }
35641
 
    pCache->nRecyclable++;
35642
 
  }
35643
 
 
35644
 
  pcache1LeaveMutex(pCache->pGroup);
35645
 
}
35646
 
 
35647
 
/*
35648
 
** Implementation of the sqlite3_pcache.xRekey method. 
35649
 
*/
35650
 
static void pcache1Rekey(
35651
 
  sqlite3_pcache *p,
35652
 
  void *pPg,
35653
 
  unsigned int iOld,
35654
 
  unsigned int iNew
35655
 
){
35656
 
  PCache1 *pCache = (PCache1 *)p;
35657
 
  PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
35658
 
  PgHdr1 **pp;
35659
 
  unsigned int h; 
35660
 
  assert( pPage->iKey==iOld );
35661
 
  assert( pPage->pCache==pCache );
35662
 
 
35663
 
  pcache1EnterMutex(pCache->pGroup);
35664
 
 
35665
 
  h = iOld%pCache->nHash;
35666
 
  pp = &pCache->apHash[h];
35667
 
  while( (*pp)!=pPage ){
35668
 
    pp = &(*pp)->pNext;
35669
 
  }
35670
 
  *pp = pPage->pNext;
35671
 
 
35672
 
  h = iNew%pCache->nHash;
35673
 
  pPage->iKey = iNew;
35674
 
  pPage->pNext = pCache->apHash[h];
35675
 
  pCache->apHash[h] = pPage;
35676
 
  if( iNew>pCache->iMaxKey ){
35677
 
    pCache->iMaxKey = iNew;
35678
 
  }
35679
 
 
35680
 
  pcache1LeaveMutex(pCache->pGroup);
35681
 
}
35682
 
 
35683
 
/*
35684
 
** Implementation of the sqlite3_pcache.xTruncate method. 
35685
 
**
35686
 
** Discard all unpinned pages in the cache with a page number equal to
35687
 
** or greater than parameter iLimit. Any pinned pages with a page number
35688
 
** equal to or greater than iLimit are implicitly unpinned.
35689
 
*/
35690
 
static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
35691
 
  PCache1 *pCache = (PCache1 *)p;
35692
 
  pcache1EnterMutex(pCache->pGroup);
35693
 
  if( iLimit<=pCache->iMaxKey ){
35694
 
    pcache1TruncateUnsafe(pCache, iLimit);
35695
 
    pCache->iMaxKey = iLimit-1;
35696
 
  }
35697
 
  pcache1LeaveMutex(pCache->pGroup);
35698
 
}
35699
 
 
35700
 
/*
35701
 
** Implementation of the sqlite3_pcache.xDestroy method. 
35702
 
**
35703
 
** Destroy a cache allocated using pcache1Create().
35704
 
*/
35705
 
static void pcache1Destroy(sqlite3_pcache *p){
35706
 
  PCache1 *pCache = (PCache1 *)p;
35707
 
  PGroup *pGroup = pCache->pGroup;
35708
 
  assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
35709
 
  pcache1EnterMutex(pGroup);
35710
 
  pcache1TruncateUnsafe(pCache, 0);
35711
 
  pGroup->nMaxPage -= pCache->nMax;
35712
 
  pGroup->nMinPage -= pCache->nMin;
35713
 
  pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
35714
 
  pcache1EnforceMaxPage(pGroup);
35715
 
  pcache1LeaveMutex(pGroup);
35716
 
  sqlite3_free(pCache->apHash);
35717
 
  sqlite3_free(pCache);
35718
 
}
35719
 
 
35720
 
/*
35721
 
** This function is called during initialization (sqlite3_initialize()) to
35722
 
** install the default pluggable cache module, assuming the user has not
35723
 
** already provided an alternative.
35724
 
*/
35725
 
SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
35726
 
  static const sqlite3_pcache_methods defaultMethods = {
35727
 
    0,                       /* pArg */
35728
 
    pcache1Init,             /* xInit */
35729
 
    pcache1Shutdown,         /* xShutdown */
35730
 
    pcache1Create,           /* xCreate */
35731
 
    pcache1Cachesize,        /* xCachesize */
35732
 
    pcache1Pagecount,        /* xPagecount */
35733
 
    pcache1Fetch,            /* xFetch */
35734
 
    pcache1Unpin,            /* xUnpin */
35735
 
    pcache1Rekey,            /* xRekey */
35736
 
    pcache1Truncate,         /* xTruncate */
35737
 
    pcache1Destroy           /* xDestroy */
35738
 
  };
35739
 
  sqlite3_config(SQLITE_CONFIG_PCACHE, &defaultMethods);
35740
 
}
35741
 
 
35742
 
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
35743
 
/*
35744
 
** This function is called to free superfluous dynamically allocated memory
35745
 
** held by the pager system. Memory in use by any SQLite pager allocated
35746
 
** by the current thread may be sqlite3_free()ed.
35747
 
**
35748
 
** nReq is the number of bytes of memory required. Once this much has
35749
 
** been released, the function returns. The return value is the total number 
35750
 
** of bytes of memory released.
35751
 
*/
35752
 
SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
35753
 
  int nFree = 0;
35754
 
  assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
35755
 
  assert( sqlite3_mutex_notheld(pcache1.mutex) );
35756
 
  if( pcache1.pStart==0 ){
35757
 
    PgHdr1 *p;
35758
 
    pcache1EnterMutex(&pcache1.grp);
35759
 
    while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
35760
 
      nFree += pcache1MemSize(PGHDR1_TO_PAGE(p));
35761
 
      pcache1PinPage(p);
35762
 
      pcache1RemoveFromHash(p);
35763
 
      pcache1FreePage(p);
35764
 
    }
35765
 
    pcache1LeaveMutex(&pcache1.grp);
35766
 
  }
35767
 
  return nFree;
35768
 
}
35769
 
#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
35770
 
 
35771
 
#ifdef SQLITE_TEST
35772
 
/*
35773
 
** This function is used by test procedures to inspect the internal state
35774
 
** of the global cache.
35775
 
*/
35776
 
SQLITE_PRIVATE void sqlite3PcacheStats(
35777
 
  int *pnCurrent,      /* OUT: Total number of pages cached */
35778
 
  int *pnMax,          /* OUT: Global maximum cache size */
35779
 
  int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
35780
 
  int *pnRecyclable    /* OUT: Total number of pages available for recycling */
35781
 
){
35782
 
  PgHdr1 *p;
35783
 
  int nRecyclable = 0;
35784
 
  for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
35785
 
    nRecyclable++;
35786
 
  }
35787
 
  *pnCurrent = pcache1.grp.nCurrentPage;
35788
 
  *pnMax = pcache1.grp.nMaxPage;
35789
 
  *pnMin = pcache1.grp.nMinPage;
35790
 
  *pnRecyclable = nRecyclable;
35791
 
}
35792
 
#endif
35793
 
 
35794
 
/************** End of pcache1.c *********************************************/
35795
 
/************** Begin file rowset.c ******************************************/
35796
 
/*
35797
 
** 2008 December 3
35798
 
**
35799
 
** The author disclaims copyright to this source code.  In place of
35800
 
** a legal notice, here is a blessing:
35801
 
**
35802
 
**    May you do good and not evil.
35803
 
**    May you find forgiveness for yourself and forgive others.
35804
 
**    May you share freely, never taking more than you give.
35805
 
**
35806
 
*************************************************************************
35807
 
**
35808
 
** This module implements an object we call a "RowSet".
35809
 
**
35810
 
** The RowSet object is a collection of rowids.  Rowids
35811
 
** are inserted into the RowSet in an arbitrary order.  Inserts
35812
 
** can be intermixed with tests to see if a given rowid has been
35813
 
** previously inserted into the RowSet.
35814
 
**
35815
 
** After all inserts are finished, it is possible to extract the
35816
 
** elements of the RowSet in sorted order.  Once this extraction
35817
 
** process has started, no new elements may be inserted.
35818
 
**
35819
 
** Hence, the primitive operations for a RowSet are:
35820
 
**
35821
 
**    CREATE
35822
 
**    INSERT
35823
 
**    TEST
35824
 
**    SMALLEST
35825
 
**    DESTROY
35826
 
**
35827
 
** The CREATE and DESTROY primitives are the constructor and destructor,
35828
 
** obviously.  The INSERT primitive adds a new element to the RowSet.
35829
 
** TEST checks to see if an element is already in the RowSet.  SMALLEST
35830
 
** extracts the least value from the RowSet.
35831
 
**
35832
 
** The INSERT primitive might allocate additional memory.  Memory is
35833
 
** allocated in chunks so most INSERTs do no allocation.  There is an 
35834
 
** upper bound on the size of allocated memory.  No memory is freed
35835
 
** until DESTROY.
35836
 
**
35837
 
** The TEST primitive includes a "batch" number.  The TEST primitive
35838
 
** will only see elements that were inserted before the last change
35839
 
** in the batch number.  In other words, if an INSERT occurs between
35840
 
** two TESTs where the TESTs have the same batch nubmer, then the
35841
 
** value added by the INSERT will not be visible to the second TEST.
35842
 
** The initial batch number is zero, so if the very first TEST contains
35843
 
** a non-zero batch number, it will see all prior INSERTs.
35844
 
**
35845
 
** No INSERTs may occurs after a SMALLEST.  An assertion will fail if
35846
 
** that is attempted.
35847
 
**
35848
 
** The cost of an INSERT is roughly constant.  (Sometime new memory
35849
 
** has to be allocated on an INSERT.)  The cost of a TEST with a new
35850
 
** batch number is O(NlogN) where N is the number of elements in the RowSet.
35851
 
** The cost of a TEST using the same batch number is O(logN).  The cost
35852
 
** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
35853
 
** primitives are constant time.  The cost of DESTROY is O(N).
35854
 
**
35855
 
** There is an added cost of O(N) when switching between TEST and
35856
 
** SMALLEST primitives.
35857
 
*/
35858
 
 
35859
 
 
35860
 
/*
35861
 
** Target size for allocation chunks.
35862
 
*/
35863
 
#define ROWSET_ALLOCATION_SIZE 1024
35864
 
 
35865
 
/*
35866
 
** The number of rowset entries per allocation chunk.
35867
 
*/
35868
 
#define ROWSET_ENTRY_PER_CHUNK  \
35869
 
                       ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
35870
 
 
35871
 
/*
35872
 
** Each entry in a RowSet is an instance of the following object.
35873
 
*/
35874
 
struct RowSetEntry {            
35875
 
  i64 v;                        /* ROWID value for this entry */
35876
 
  struct RowSetEntry *pRight;   /* Right subtree (larger entries) or list */
35877
 
  struct RowSetEntry *pLeft;    /* Left subtree (smaller entries) */
35878
 
};
35879
 
 
35880
 
/*
35881
 
** RowSetEntry objects are allocated in large chunks (instances of the
35882
 
** following structure) to reduce memory allocation overhead.  The
35883
 
** chunks are kept on a linked list so that they can be deallocated
35884
 
** when the RowSet is destroyed.
35885
 
*/
35886
 
struct RowSetChunk {
35887
 
  struct RowSetChunk *pNextChunk;        /* Next chunk on list of them all */
35888
 
  struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
35889
 
};
35890
 
 
35891
 
/*
35892
 
** A RowSet in an instance of the following structure.
35893
 
**
35894
 
** A typedef of this structure if found in sqliteInt.h.
35895
 
*/
35896
 
struct RowSet {
35897
 
  struct RowSetChunk *pChunk;    /* List of all chunk allocations */
35898
 
  sqlite3 *db;                   /* The database connection */
35899
 
  struct RowSetEntry *pEntry;    /* List of entries using pRight */
35900
 
  struct RowSetEntry *pLast;     /* Last entry on the pEntry list */
35901
 
  struct RowSetEntry *pFresh;    /* Source of new entry objects */
35902
 
  struct RowSetEntry *pTree;     /* Binary tree of entries */
35903
 
  u16 nFresh;                    /* Number of objects on pFresh */
35904
 
  u8 isSorted;                   /* True if pEntry is sorted */
35905
 
  u8 iBatch;                     /* Current insert batch */
35906
 
};
35907
 
 
35908
 
/*
35909
 
** Turn bulk memory into a RowSet object.  N bytes of memory
35910
 
** are available at pSpace.  The db pointer is used as a memory context
35911
 
** for any subsequent allocations that need to occur.
35912
 
** Return a pointer to the new RowSet object.
35913
 
**
35914
 
** It must be the case that N is sufficient to make a Rowset.  If not
35915
 
** an assertion fault occurs.
35916
 
** 
35917
 
** If N is larger than the minimum, use the surplus as an initial
35918
 
** allocation of entries available to be filled.
35919
 
*/
35920
 
SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){
35921
 
  RowSet *p;
35922
 
  assert( N >= ROUND8(sizeof(*p)) );
35923
 
  p = pSpace;
35924
 
  p->pChunk = 0;
35925
 
  p->db = db;
35926
 
  p->pEntry = 0;
35927
 
  p->pLast = 0;
35928
 
  p->pTree = 0;
35929
 
  p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
35930
 
  p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
35931
 
  p->isSorted = 1;
35932
 
  p->iBatch = 0;
35933
 
  return p;
35934
 
}
35935
 
 
35936
 
/*
35937
 
** Deallocate all chunks from a RowSet.  This frees all memory that
35938
 
** the RowSet has allocated over its lifetime.  This routine is
35939
 
** the destructor for the RowSet.
35940
 
*/
35941
 
SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){
35942
 
  struct RowSetChunk *pChunk, *pNextChunk;
35943
 
  for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
35944
 
    pNextChunk = pChunk->pNextChunk;
35945
 
    sqlite3DbFree(p->db, pChunk);
35946
 
  }
35947
 
  p->pChunk = 0;
35948
 
  p->nFresh = 0;
35949
 
  p->pEntry = 0;
35950
 
  p->pLast = 0;
35951
 
  p->pTree = 0;
35952
 
  p->isSorted = 1;
35953
 
}
35954
 
 
35955
 
/*
35956
 
** Insert a new value into a RowSet.
35957
 
**
35958
 
** The mallocFailed flag of the database connection is set if a
35959
 
** memory allocation fails.
35960
 
*/
35961
 
SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
35962
 
  struct RowSetEntry *pEntry;  /* The new entry */
35963
 
  struct RowSetEntry *pLast;   /* The last prior entry */
35964
 
  assert( p!=0 );
35965
 
  if( p->nFresh==0 ){
35966
 
    struct RowSetChunk *pNew;
35967
 
    pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
35968
 
    if( pNew==0 ){
35969
 
      return;
35970
 
    }
35971
 
    pNew->pNextChunk = p->pChunk;
35972
 
    p->pChunk = pNew;
35973
 
    p->pFresh = pNew->aEntry;
35974
 
    p->nFresh = ROWSET_ENTRY_PER_CHUNK;
35975
 
  }
35976
 
  pEntry = p->pFresh++;
35977
 
  p->nFresh--;
35978
 
  pEntry->v = rowid;
35979
 
  pEntry->pRight = 0;
35980
 
  pLast = p->pLast;
35981
 
  if( pLast ){
35982
 
    if( p->isSorted && rowid<=pLast->v ){
35983
 
      p->isSorted = 0;
35984
 
    }
35985
 
    pLast->pRight = pEntry;
35986
 
  }else{
35987
 
    assert( p->pEntry==0 ); /* Fires if INSERT after SMALLEST */
35988
 
    p->pEntry = pEntry;
35989
 
  }
35990
 
  p->pLast = pEntry;
35991
 
}
35992
 
 
35993
 
/*
35994
 
** Merge two lists of RowSetEntry objects.  Remove duplicates.
35995
 
**
35996
 
** The input lists are connected via pRight pointers and are 
35997
 
** assumed to each already be in sorted order.
35998
 
*/
35999
 
static struct RowSetEntry *rowSetMerge(
36000
 
  struct RowSetEntry *pA,    /* First sorted list to be merged */
36001
 
  struct RowSetEntry *pB     /* Second sorted list to be merged */
36002
 
){
36003
 
  struct RowSetEntry head;
36004
 
  struct RowSetEntry *pTail;
36005
 
 
36006
 
  pTail = &head;
36007
 
  while( pA && pB ){
36008
 
    assert( pA->pRight==0 || pA->v<=pA->pRight->v );
36009
 
    assert( pB->pRight==0 || pB->v<=pB->pRight->v );
36010
 
    if( pA->v<pB->v ){
36011
 
      pTail->pRight = pA;
36012
 
      pA = pA->pRight;
36013
 
      pTail = pTail->pRight;
36014
 
    }else if( pB->v<pA->v ){
36015
 
      pTail->pRight = pB;
36016
 
      pB = pB->pRight;
36017
 
      pTail = pTail->pRight;
36018
 
    }else{
36019
 
      pA = pA->pRight;
36020
 
    }
36021
 
  }
36022
 
  if( pA ){
36023
 
    assert( pA->pRight==0 || pA->v<=pA->pRight->v );
36024
 
    pTail->pRight = pA;
36025
 
  }else{
36026
 
    assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
36027
 
    pTail->pRight = pB;
36028
 
  }
36029
 
  return head.pRight;
36030
 
}
36031
 
 
36032
 
/*
36033
 
** Sort all elements on the pEntry list of the RowSet into ascending order.
36034
 
*/ 
36035
 
static void rowSetSort(RowSet *p){
36036
 
  unsigned int i;
36037
 
  struct RowSetEntry *pEntry;
36038
 
  struct RowSetEntry *aBucket[40];
36039
 
 
36040
 
  assert( p->isSorted==0 );
36041
 
  memset(aBucket, 0, sizeof(aBucket));
36042
 
  while( p->pEntry ){
36043
 
    pEntry = p->pEntry;
36044
 
    p->pEntry = pEntry->pRight;
36045
 
    pEntry->pRight = 0;
36046
 
    for(i=0; aBucket[i]; i++){
36047
 
      pEntry = rowSetMerge(aBucket[i], pEntry);
36048
 
      aBucket[i] = 0;
36049
 
    }
36050
 
    aBucket[i] = pEntry;
36051
 
  }
36052
 
  pEntry = 0;
36053
 
  for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
36054
 
    pEntry = rowSetMerge(pEntry, aBucket[i]);
36055
 
  }
36056
 
  p->pEntry = pEntry;
36057
 
  p->pLast = 0;
36058
 
  p->isSorted = 1;
36059
 
}
36060
 
 
36061
 
 
36062
 
/*
36063
 
** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
36064
 
** Convert this tree into a linked list connected by the pRight pointers
36065
 
** and return pointers to the first and last elements of the new list.
36066
 
*/
36067
 
static void rowSetTreeToList(
36068
 
  struct RowSetEntry *pIn,         /* Root of the input tree */
36069
 
  struct RowSetEntry **ppFirst,    /* Write head of the output list here */
36070
 
  struct RowSetEntry **ppLast      /* Write tail of the output list here */
36071
 
){
36072
 
  assert( pIn!=0 );
36073
 
  if( pIn->pLeft ){
36074
 
    struct RowSetEntry *p;
36075
 
    rowSetTreeToList(pIn->pLeft, ppFirst, &p);
36076
 
    p->pRight = pIn;
36077
 
  }else{
36078
 
    *ppFirst = pIn;
36079
 
  }
36080
 
  if( pIn->pRight ){
36081
 
    rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
36082
 
  }else{
36083
 
    *ppLast = pIn;
36084
 
  }
36085
 
  assert( (*ppLast)->pRight==0 );
36086
 
}
36087
 
 
36088
 
 
36089
 
/*
36090
 
** Convert a sorted list of elements (connected by pRight) into a binary
36091
 
** tree with depth of iDepth.  A depth of 1 means the tree contains a single
36092
 
** node taken from the head of *ppList.  A depth of 2 means a tree with
36093
 
** three nodes.  And so forth.
36094
 
**
36095
 
** Use as many entries from the input list as required and update the
36096
 
** *ppList to point to the unused elements of the list.  If the input
36097
 
** list contains too few elements, then construct an incomplete tree
36098
 
** and leave *ppList set to NULL.
36099
 
**
36100
 
** Return a pointer to the root of the constructed binary tree.
36101
 
*/
36102
 
static struct RowSetEntry *rowSetNDeepTree(
36103
 
  struct RowSetEntry **ppList,
36104
 
  int iDepth
36105
 
){
36106
 
  struct RowSetEntry *p;         /* Root of the new tree */
36107
 
  struct RowSetEntry *pLeft;     /* Left subtree */
36108
 
  if( *ppList==0 ){
36109
 
    return 0;
36110
 
  }
36111
 
  if( iDepth==1 ){
36112
 
    p = *ppList;
36113
 
    *ppList = p->pRight;
36114
 
    p->pLeft = p->pRight = 0;
36115
 
    return p;
36116
 
  }
36117
 
  pLeft = rowSetNDeepTree(ppList, iDepth-1);
36118
 
  p = *ppList;
36119
 
  if( p==0 ){
36120
 
    return pLeft;
36121
 
  }
36122
 
  p->pLeft = pLeft;
36123
 
  *ppList = p->pRight;
36124
 
  p->pRight = rowSetNDeepTree(ppList, iDepth-1);
36125
 
  return p;
36126
 
}
36127
 
 
36128
 
/*
36129
 
** Convert a sorted list of elements into a binary tree. Make the tree
36130
 
** as deep as it needs to be in order to contain the entire list.
36131
 
*/
36132
 
static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
36133
 
  int iDepth;           /* Depth of the tree so far */
36134
 
  struct RowSetEntry *p;       /* Current tree root */
36135
 
  struct RowSetEntry *pLeft;   /* Left subtree */
36136
 
 
36137
 
  assert( pList!=0 );
36138
 
  p = pList;
36139
 
  pList = p->pRight;
36140
 
  p->pLeft = p->pRight = 0;
36141
 
  for(iDepth=1; pList; iDepth++){
36142
 
    pLeft = p;
36143
 
    p = pList;
36144
 
    pList = p->pRight;
36145
 
    p->pLeft = pLeft;
36146
 
    p->pRight = rowSetNDeepTree(&pList, iDepth);
36147
 
  }
36148
 
  return p;
36149
 
}
36150
 
 
36151
 
/*
36152
 
** Convert the list in p->pEntry into a sorted list if it is not
36153
 
** sorted already.  If there is a binary tree on p->pTree, then
36154
 
** convert it into a list too and merge it into the p->pEntry list.
36155
 
*/
36156
 
static void rowSetToList(RowSet *p){
36157
 
  if( !p->isSorted ){
36158
 
    rowSetSort(p);
36159
 
  }
36160
 
  if( p->pTree ){
36161
 
    struct RowSetEntry *pHead, *pTail;
36162
 
    rowSetTreeToList(p->pTree, &pHead, &pTail);
36163
 
    p->pTree = 0;
36164
 
    p->pEntry = rowSetMerge(p->pEntry, pHead);
36165
 
  }
36166
 
}
36167
 
 
36168
 
/*
36169
 
** Extract the smallest element from the RowSet.
36170
 
** Write the element into *pRowid.  Return 1 on success.  Return
36171
 
** 0 if the RowSet is already empty.
36172
 
**
36173
 
** After this routine has been called, the sqlite3RowSetInsert()
36174
 
** routine may not be called again.  
36175
 
*/
36176
 
SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
36177
 
  rowSetToList(p);
36178
 
  if( p->pEntry ){
36179
 
    *pRowid = p->pEntry->v;
36180
 
    p->pEntry = p->pEntry->pRight;
36181
 
    if( p->pEntry==0 ){
36182
 
      sqlite3RowSetClear(p);
36183
 
    }
36184
 
    return 1;
36185
 
  }else{
36186
 
    return 0;
36187
 
  }
36188
 
}
36189
 
 
36190
 
/*
36191
 
** Check to see if element iRowid was inserted into the the rowset as
36192
 
** part of any insert batch prior to iBatch.  Return 1 or 0.
36193
 
*/
36194
 
SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
36195
 
  struct RowSetEntry *p;
36196
 
  if( iBatch!=pRowSet->iBatch ){
36197
 
    if( pRowSet->pEntry ){
36198
 
      rowSetToList(pRowSet);
36199
 
      pRowSet->pTree = rowSetListToTree(pRowSet->pEntry);
36200
 
      pRowSet->pEntry = 0;
36201
 
      pRowSet->pLast = 0;
36202
 
    }
36203
 
    pRowSet->iBatch = iBatch;
36204
 
  }
36205
 
  p = pRowSet->pTree;
36206
 
  while( p ){
36207
 
    if( p->v<iRowid ){
36208
 
      p = p->pRight;
36209
 
    }else if( p->v>iRowid ){
36210
 
      p = p->pLeft;
36211
 
    }else{
36212
 
      return 1;
36213
 
    }
36214
 
  }
36215
 
  return 0;
36216
 
}
36217
 
 
36218
 
/************** End of rowset.c **********************************************/
36219
 
/************** Begin file pager.c *******************************************/
36220
 
/*
36221
 
** 2001 September 15
36222
 
**
36223
 
** The author disclaims copyright to this source code.  In place of
36224
 
** a legal notice, here is a blessing:
36225
 
**
36226
 
**    May you do good and not evil.
36227
 
**    May you find forgiveness for yourself and forgive others.
36228
 
**    May you share freely, never taking more than you give.
36229
 
**
36230
 
*************************************************************************
36231
 
** This is the implementation of the page cache subsystem or "pager".
36232
 
** 
36233
 
** The pager is used to access a database disk file.  It implements
36234
 
** atomic commit and rollback through the use of a journal file that
36235
 
** is separate from the database file.  The pager also implements file
36236
 
** locking to prevent two processes from writing the same database
36237
 
** file simultaneously, or one process from reading the database while
36238
 
** another is writing.
36239
 
*/
36240
 
#ifndef SQLITE_OMIT_DISKIO
36241
 
/************** Include wal.h in the middle of pager.c ***********************/
36242
 
/************** Begin file wal.h *********************************************/
36243
 
/*
36244
 
** 2010 February 1
36245
 
**
36246
 
** The author disclaims copyright to this source code.  In place of
36247
 
** a legal notice, here is a blessing:
36248
 
**
36249
 
**    May you do good and not evil.
36250
 
**    May you find forgiveness for yourself and forgive others.
36251
 
**    May you share freely, never taking more than you give.
36252
 
**
36253
 
*************************************************************************
36254
 
** This header file defines the interface to the write-ahead logging 
36255
 
** system. Refer to the comments below and the header comment attached to 
36256
 
** the implementation of each function in log.c for further details.
36257
 
*/
36258
 
 
36259
 
#ifndef _WAL_H_
36260
 
#define _WAL_H_
36261
 
 
36262
 
 
36263
 
#ifdef SQLITE_OMIT_WAL
36264
 
# define sqlite3WalOpen(x,y,z)                   0
36265
 
# define sqlite3WalClose(w,x,y,z)                0
36266
 
# define sqlite3WalBeginReadTransaction(y,z)     0
36267
 
# define sqlite3WalEndReadTransaction(z)
36268
 
# define sqlite3WalRead(v,w,x,y,z)               0
36269
 
# define sqlite3WalDbsize(y)                     0
36270
 
# define sqlite3WalBeginWriteTransaction(y)      0
36271
 
# define sqlite3WalEndWriteTransaction(x)        0
36272
 
# define sqlite3WalUndo(x,y,z)                   0
36273
 
# define sqlite3WalSavepoint(y,z)
36274
 
# define sqlite3WalSavepointUndo(y,z)            0
36275
 
# define sqlite3WalFrames(u,v,w,x,y,z)           0
36276
 
# define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
36277
 
# define sqlite3WalCallback(z)                   0
36278
 
# define sqlite3WalExclusiveMode(y,z)            0
36279
 
# define sqlite3WalHeapMemory(z)                 0
36280
 
#else
36281
 
 
36282
 
#define WAL_SAVEPOINT_NDATA 4
36283
 
 
36284
 
/* Connection to a write-ahead log (WAL) file. 
36285
 
** There is one object of this type for each pager. 
36286
 
*/
36287
 
typedef struct Wal Wal;
36288
 
 
36289
 
/* Open and close a connection to a write-ahead log. */
36290
 
SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *zName, int, Wal**);
36291
 
SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *);
36292
 
 
36293
 
/* Used by readers to open (lock) and close (unlock) a snapshot.  A 
36294
 
** snapshot is like a read-transaction.  It is the state of the database
36295
 
** at an instant in time.  sqlite3WalOpenSnapshot gets a read lock and
36296
 
** preserves the current state even if the other threads or processes
36297
 
** write to or checkpoint the WAL.  sqlite3WalCloseSnapshot() closes the
36298
 
** transaction and releases the lock.
36299
 
*/
36300
 
SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
36301
 
SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
36302
 
 
36303
 
/* Read a page from the write-ahead log, if it is present. */
36304
 
SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut);
36305
 
 
36306
 
/* If the WAL is not empty, return the size of the database. */
36307
 
SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
36308
 
 
36309
 
/* Obtain or release the WRITER lock. */
36310
 
SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
36311
 
SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
36312
 
 
36313
 
/* Undo any frames written (but not committed) to the log */
36314
 
SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
36315
 
 
36316
 
/* Return an integer that records the current (uncommitted) write
36317
 
** position in the WAL */
36318
 
SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
36319
 
 
36320
 
/* Move the write position of the WAL back to iFrame.  Called in
36321
 
** response to a ROLLBACK TO command. */
36322
 
SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
36323
 
 
36324
 
/* Write a frame or frames to the log. */
36325
 
SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
36326
 
 
36327
 
/* Copy pages from the log to the database file */ 
36328
 
SQLITE_PRIVATE int sqlite3WalCheckpoint(
36329
 
  Wal *pWal,                      /* Write-ahead log connection */
36330
 
  int eMode,                      /* One of PASSIVE, FULL and RESTART */
36331
 
  int (*xBusy)(void*),            /* Function to call when busy */
36332
 
  void *pBusyArg,                 /* Context argument for xBusyHandler */
36333
 
  int sync_flags,                 /* Flags to sync db file with (or 0) */
36334
 
  int nBuf,                       /* Size of buffer nBuf */
36335
 
  u8 *zBuf,                       /* Temporary buffer to use */
36336
 
  int *pnLog,                     /* OUT: Number of frames in WAL */
36337
 
  int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
36338
 
);
36339
 
 
36340
 
/* Return the value to pass to a sqlite3_wal_hook callback, the
36341
 
** number of frames in the WAL at the point of the last commit since
36342
 
** sqlite3WalCallback() was called.  If no commits have occurred since
36343
 
** the last call, then return 0.
36344
 
*/
36345
 
SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
36346
 
 
36347
 
/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
36348
 
** by the pager layer on the database file.
36349
 
*/
36350
 
SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
36351
 
 
36352
 
/* Return true if the argument is non-NULL and the WAL module is using
36353
 
** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
36354
 
** WAL module is using shared-memory, return false. 
36355
 
*/
36356
 
SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
36357
 
 
36358
 
#endif /* ifndef SQLITE_OMIT_WAL */
36359
 
#endif /* _WAL_H_ */
36360
 
 
36361
 
/************** End of wal.h *************************************************/
36362
 
/************** Continuing where we left off in pager.c **********************/
36363
 
 
36364
 
 
36365
 
/******************* NOTES ON THE DESIGN OF THE PAGER ************************
36366
 
**
36367
 
** This comment block describes invariants that hold when using a rollback
36368
 
** journal.  These invariants do not apply for journal_mode=WAL,
36369
 
** journal_mode=MEMORY, or journal_mode=OFF.
36370
 
**
36371
 
** Within this comment block, a page is deemed to have been synced
36372
 
** automatically as soon as it is written when PRAGMA synchronous=OFF.
36373
 
** Otherwise, the page is not synced until the xSync method of the VFS
36374
 
** is called successfully on the file containing the page.
36375
 
**
36376
 
** Definition:  A page of the database file is said to be "overwriteable" if
36377
 
** one or more of the following are true about the page:
36378
 
** 
36379
 
**     (a)  The original content of the page as it was at the beginning of
36380
 
**          the transaction has been written into the rollback journal and
36381
 
**          synced.
36382
 
** 
36383
 
**     (b)  The page was a freelist leaf page at the start of the transaction.
36384
 
** 
36385
 
**     (c)  The page number is greater than the largest page that existed in
36386
 
**          the database file at the start of the transaction.
36387
 
** 
36388
 
** (1) A page of the database file is never overwritten unless one of the
36389
 
**     following are true:
36390
 
** 
36391
 
**     (a) The page and all other pages on the same sector are overwriteable.
36392
 
** 
36393
 
**     (b) The atomic page write optimization is enabled, and the entire
36394
 
**         transaction other than the update of the transaction sequence
36395
 
**         number consists of a single page change.
36396
 
** 
36397
 
** (2) The content of a page written into the rollback journal exactly matches
36398
 
**     both the content in the database when the rollback journal was written
36399
 
**     and the content in the database at the beginning of the current
36400
 
**     transaction.
36401
 
** 
36402
 
** (3) Writes to the database file are an integer multiple of the page size
36403
 
**     in length and are aligned on a page boundary.
36404
 
** 
36405
 
** (4) Reads from the database file are either aligned on a page boundary and
36406
 
**     an integer multiple of the page size in length or are taken from the
36407
 
**     first 100 bytes of the database file.
36408
 
** 
36409
 
** (5) All writes to the database file are synced prior to the rollback journal
36410
 
**     being deleted, truncated, or zeroed.
36411
 
** 
36412
 
** (6) If a master journal file is used, then all writes to the database file
36413
 
**     are synced prior to the master journal being deleted.
36414
 
** 
36415
 
** Definition: Two databases (or the same database at two points it time)
36416
 
** are said to be "logically equivalent" if they give the same answer to
36417
 
** all queries.  Note in particular the the content of freelist leaf
36418
 
** pages can be changed arbitarily without effecting the logical equivalence
36419
 
** of the database.
36420
 
** 
36421
 
** (7) At any time, if any subset, including the empty set and the total set,
36422
 
**     of the unsynced changes to a rollback journal are removed and the 
36423
 
**     journal is rolled back, the resulting database file will be logical
36424
 
**     equivalent to the database file at the beginning of the transaction.
36425
 
** 
36426
 
** (8) When a transaction is rolled back, the xTruncate method of the VFS
36427
 
**     is called to restore the database file to the same size it was at
36428
 
**     the beginning of the transaction.  (In some VFSes, the xTruncate
36429
 
**     method is a no-op, but that does not change the fact the SQLite will
36430
 
**     invoke it.)
36431
 
** 
36432
 
** (9) Whenever the database file is modified, at least one bit in the range
36433
 
**     of bytes from 24 through 39 inclusive will be changed prior to releasing
36434
 
**     the EXCLUSIVE lock, thus signaling other connections on the same
36435
 
**     database to flush their caches.
36436
 
**
36437
 
** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
36438
 
**      than one billion transactions.
36439
 
**
36440
 
** (11) A database file is well-formed at the beginning and at the conclusion
36441
 
**      of every transaction.
36442
 
**
36443
 
** (12) An EXCLUSIVE lock is held on the database file when writing to
36444
 
**      the database file.
36445
 
**
36446
 
** (13) A SHARED lock is held on the database file while reading any
36447
 
**      content out of the database file.
36448
 
**
36449
 
******************************************************************************/
36450
 
 
36451
 
/*
36452
 
** Macros for troubleshooting.  Normally turned off
36453
 
*/
36454
 
#if 0
36455
 
int sqlite3PagerTrace=1;  /* True to enable tracing */
36456
 
#define sqlite3DebugPrintf printf
36457
 
#define PAGERTRACE(X)     if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
36458
 
#else
36459
 
#define PAGERTRACE(X)
36460
 
#endif
36461
 
 
36462
 
/*
36463
 
** The following two macros are used within the PAGERTRACE() macros above
36464
 
** to print out file-descriptors. 
36465
 
**
36466
 
** PAGERID() takes a pointer to a Pager struct as its argument. The
36467
 
** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
36468
 
** struct as its argument.
36469
 
*/
36470
 
#define PAGERID(p) ((int)(p->fd))
36471
 
#define FILEHANDLEID(fd) ((int)fd)
36472
 
 
36473
 
/*
36474
 
** The Pager.eState variable stores the current 'state' of a pager. A
36475
 
** pager may be in any one of the seven states shown in the following
36476
 
** state diagram.
36477
 
**
36478
 
**                            OPEN <------+------+
36479
 
**                              |         |      |
36480
 
**                              V         |      |
36481
 
**               +---------> READER-------+      |
36482
 
**               |              |                |
36483
 
**               |              V                |
36484
 
**               |<-------WRITER_LOCKED------> ERROR
36485
 
**               |              |                ^  
36486
 
**               |              V                |
36487
 
**               |<------WRITER_CACHEMOD-------->|
36488
 
**               |              |                |
36489
 
**               |              V                |
36490
 
**               |<-------WRITER_DBMOD---------->|
36491
 
**               |              |                |
36492
 
**               |              V                |
36493
 
**               +<------WRITER_FINISHED-------->+
36494
 
**
36495
 
**
36496
 
** List of state transitions and the C [function] that performs each:
36497
 
** 
36498
 
**   OPEN              -> READER              [sqlite3PagerSharedLock]
36499
 
**   READER            -> OPEN                [pager_unlock]
36500
 
**
36501
 
**   READER            -> WRITER_LOCKED       [sqlite3PagerBegin]
36502
 
**   WRITER_LOCKED     -> WRITER_CACHEMOD     [pager_open_journal]
36503
 
**   WRITER_CACHEMOD   -> WRITER_DBMOD        [syncJournal]
36504
 
**   WRITER_DBMOD      -> WRITER_FINISHED     [sqlite3PagerCommitPhaseOne]
36505
 
**   WRITER_***        -> READER              [pager_end_transaction]
36506
 
**
36507
 
**   WRITER_***        -> ERROR               [pager_error]
36508
 
**   ERROR             -> OPEN                [pager_unlock]
36509
 
** 
36510
 
**
36511
 
**  OPEN:
36512
 
**
36513
 
**    The pager starts up in this state. Nothing is guaranteed in this
36514
 
**    state - the file may or may not be locked and the database size is
36515
 
**    unknown. The database may not be read or written.
36516
 
**
36517
 
**    * No read or write transaction is active.
36518
 
**    * Any lock, or no lock at all, may be held on the database file.
36519
 
**    * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
36520
 
**
36521
 
**  READER:
36522
 
**
36523
 
**    In this state all the requirements for reading the database in 
36524
 
**    rollback (non-WAL) mode are met. Unless the pager is (or recently
36525
 
**    was) in exclusive-locking mode, a user-level read transaction is 
36526
 
**    open. The database size is known in this state.
36527
 
**
36528
 
**    A connection running with locking_mode=normal enters this state when
36529
 
**    it opens a read-transaction on the database and returns to state
36530
 
**    OPEN after the read-transaction is completed. However a connection
36531
 
**    running in locking_mode=exclusive (including temp databases) remains in
36532
 
**    this state even after the read-transaction is closed. The only way
36533
 
**    a locking_mode=exclusive connection can transition from READER to OPEN
36534
 
**    is via the ERROR state (see below).
36535
 
** 
36536
 
**    * A read transaction may be active (but a write-transaction cannot).
36537
 
**    * A SHARED or greater lock is held on the database file.
36538
 
**    * The dbSize variable may be trusted (even if a user-level read 
36539
 
**      transaction is not active). The dbOrigSize and dbFileSize variables
36540
 
**      may not be trusted at this point.
36541
 
**    * If the database is a WAL database, then the WAL connection is open.
36542
 
**    * Even if a read-transaction is not open, it is guaranteed that 
36543
 
**      there is no hot-journal in the file-system.
36544
 
**
36545
 
**  WRITER_LOCKED:
36546
 
**
36547
 
**    The pager moves to this state from READER when a write-transaction
36548
 
**    is first opened on the database. In WRITER_LOCKED state, all locks 
36549
 
**    required to start a write-transaction are held, but no actual 
36550
 
**    modifications to the cache or database have taken place.
36551
 
**
36552
 
**    In rollback mode, a RESERVED or (if the transaction was opened with 
36553
 
**    BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
36554
 
**    moving to this state, but the journal file is not written to or opened 
36555
 
**    to in this state. If the transaction is committed or rolled back while 
36556
 
**    in WRITER_LOCKED state, all that is required is to unlock the database 
36557
 
**    file.
36558
 
**
36559
 
**    IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
36560
 
**    If the connection is running with locking_mode=exclusive, an attempt
36561
 
**    is made to obtain an EXCLUSIVE lock on the database file.
36562
 
**
36563
 
**    * A write transaction is active.
36564
 
**    * If the connection is open in rollback-mode, a RESERVED or greater 
36565
 
**      lock is held on the database file.
36566
 
**    * If the connection is open in WAL-mode, a WAL write transaction
36567
 
**      is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
36568
 
**      called).
36569
 
**    * The dbSize, dbOrigSize and dbFileSize variables are all valid.
36570
 
**    * The contents of the pager cache have not been modified.
36571
 
**    * The journal file may or may not be open.
36572
 
**    * Nothing (not even the first header) has been written to the journal.
36573
 
**
36574
 
**  WRITER_CACHEMOD:
36575
 
**
36576
 
**    A pager moves from WRITER_LOCKED state to this state when a page is
36577
 
**    first modified by the upper layer. In rollback mode the journal file
36578
 
**    is opened (if it is not already open) and a header written to the
36579
 
**    start of it. The database file on disk has not been modified.
36580
 
**
36581
 
**    * A write transaction is active.
36582
 
**    * A RESERVED or greater lock is held on the database file.
36583
 
**    * The journal file is open and the first header has been written 
36584
 
**      to it, but the header has not been synced to disk.
36585
 
**    * The contents of the page cache have been modified.
36586
 
**
36587
 
**  WRITER_DBMOD:
36588
 
**
36589
 
**    The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
36590
 
**    when it modifies the contents of the database file. WAL connections
36591
 
**    never enter this state (since they do not modify the database file,
36592
 
**    just the log file).
36593
 
**
36594
 
**    * A write transaction is active.
36595
 
**    * An EXCLUSIVE or greater lock is held on the database file.
36596
 
**    * The journal file is open and the first header has been written 
36597
 
**      and synced to disk.
36598
 
**    * The contents of the page cache have been modified (and possibly
36599
 
**      written to disk).
36600
 
**
36601
 
**  WRITER_FINISHED:
36602
 
**
36603
 
**    It is not possible for a WAL connection to enter this state.
36604
 
**
36605
 
**    A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
36606
 
**    state after the entire transaction has been successfully written into the
36607
 
**    database file. In this state the transaction may be committed simply
36608
 
**    by finalizing the journal file. Once in WRITER_FINISHED state, it is 
36609
 
**    not possible to modify the database further. At this point, the upper 
36610
 
**    layer must either commit or rollback the transaction.
36611
 
**
36612
 
**    * A write transaction is active.
36613
 
**    * An EXCLUSIVE or greater lock is held on the database file.
36614
 
**    * All writing and syncing of journal and database data has finished.
36615
 
**      If no error occured, all that remains is to finalize the journal to
36616
 
**      commit the transaction. If an error did occur, the caller will need
36617
 
**      to rollback the transaction. 
36618
 
**
36619
 
**  ERROR:
36620
 
**
36621
 
**    The ERROR state is entered when an IO or disk-full error (including
36622
 
**    SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it 
36623
 
**    difficult to be sure that the in-memory pager state (cache contents, 
36624
 
**    db size etc.) are consistent with the contents of the file-system.
36625
 
**
36626
 
**    Temporary pager files may enter the ERROR state, but in-memory pagers
36627
 
**    cannot.
36628
 
**
36629
 
**    For example, if an IO error occurs while performing a rollback, 
36630
 
**    the contents of the page-cache may be left in an inconsistent state.
36631
 
**    At this point it would be dangerous to change back to READER state
36632
 
**    (as usually happens after a rollback). Any subsequent readers might
36633
 
**    report database corruption (due to the inconsistent cache), and if
36634
 
**    they upgrade to writers, they may inadvertently corrupt the database
36635
 
**    file. To avoid this hazard, the pager switches into the ERROR state
36636
 
**    instead of READER following such an error.
36637
 
**
36638
 
**    Once it has entered the ERROR state, any attempt to use the pager
36639
 
**    to read or write data returns an error. Eventually, once all 
36640
 
**    outstanding transactions have been abandoned, the pager is able to
36641
 
**    transition back to OPEN state, discarding the contents of the 
36642
 
**    page-cache and any other in-memory state at the same time. Everything
36643
 
**    is reloaded from disk (and, if necessary, hot-journal rollback peformed)
36644
 
**    when a read-transaction is next opened on the pager (transitioning
36645
 
**    the pager into READER state). At that point the system has recovered 
36646
 
**    from the error.
36647
 
**
36648
 
**    Specifically, the pager jumps into the ERROR state if:
36649
 
**
36650
 
**      1. An error occurs while attempting a rollback. This happens in
36651
 
**         function sqlite3PagerRollback().
36652
 
**
36653
 
**      2. An error occurs while attempting to finalize a journal file
36654
 
**         following a commit in function sqlite3PagerCommitPhaseTwo().
36655
 
**
36656
 
**      3. An error occurs while attempting to write to the journal or
36657
 
**         database file in function pagerStress() in order to free up
36658
 
**         memory.
36659
 
**
36660
 
**    In other cases, the error is returned to the b-tree layer. The b-tree
36661
 
**    layer then attempts a rollback operation. If the error condition 
36662
 
**    persists, the pager enters the ERROR state via condition (1) above.
36663
 
**
36664
 
**    Condition (3) is necessary because it can be triggered by a read-only
36665
 
**    statement executed within a transaction. In this case, if the error
36666
 
**    code were simply returned to the user, the b-tree layer would not
36667
 
**    automatically attempt a rollback, as it assumes that an error in a
36668
 
**    read-only statement cannot leave the pager in an internally inconsistent 
36669
 
**    state.
36670
 
**
36671
 
**    * The Pager.errCode variable is set to something other than SQLITE_OK.
36672
 
**    * There are one or more outstanding references to pages (after the
36673
 
**      last reference is dropped the pager should move back to OPEN state).
36674
 
**    * The pager is not an in-memory pager.
36675
 
**    
36676
 
**
36677
 
** Notes:
36678
 
**
36679
 
**   * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
36680
 
**     connection is open in WAL mode. A WAL connection is always in one
36681
 
**     of the first four states.
36682
 
**
36683
 
**   * Normally, a connection open in exclusive mode is never in PAGER_OPEN
36684
 
**     state. There are two exceptions: immediately after exclusive-mode has
36685
 
**     been turned on (and before any read or write transactions are 
36686
 
**     executed), and when the pager is leaving the "error state".
36687
 
**
36688
 
**   * See also: assert_pager_state().
36689
 
*/
36690
 
#define PAGER_OPEN                  0
36691
 
#define PAGER_READER                1
36692
 
#define PAGER_WRITER_LOCKED         2
36693
 
#define PAGER_WRITER_CACHEMOD       3
36694
 
#define PAGER_WRITER_DBMOD          4
36695
 
#define PAGER_WRITER_FINISHED       5
36696
 
#define PAGER_ERROR                 6
36697
 
 
36698
 
/*
36699
 
** The Pager.eLock variable is almost always set to one of the 
36700
 
** following locking-states, according to the lock currently held on
36701
 
** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
36702
 
** This variable is kept up to date as locks are taken and released by
36703
 
** the pagerLockDb() and pagerUnlockDb() wrappers.
36704
 
**
36705
 
** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
36706
 
** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
36707
 
** the operation was successful. In these circumstances pagerLockDb() and
36708
 
** pagerUnlockDb() take a conservative approach - eLock is always updated
36709
 
** when unlocking the file, and only updated when locking the file if the
36710
 
** VFS call is successful. This way, the Pager.eLock variable may be set
36711
 
** to a less exclusive (lower) value than the lock that is actually held
36712
 
** at the system level, but it is never set to a more exclusive value.
36713
 
**
36714
 
** This is usually safe. If an xUnlock fails or appears to fail, there may 
36715
 
** be a few redundant xLock() calls or a lock may be held for longer than
36716
 
** required, but nothing really goes wrong.
36717
 
**
36718
 
** The exception is when the database file is unlocked as the pager moves
36719
 
** from ERROR to OPEN state. At this point there may be a hot-journal file 
36720
 
** in the file-system that needs to be rolled back (as part of a OPEN->SHARED
36721
 
** transition, by the same pager or any other). If the call to xUnlock()
36722
 
** fails at this point and the pager is left holding an EXCLUSIVE lock, this
36723
 
** can confuse the call to xCheckReservedLock() call made later as part
36724
 
** of hot-journal detection.
36725
 
**
36726
 
** xCheckReservedLock() is defined as returning true "if there is a RESERVED 
36727
 
** lock held by this process or any others". So xCheckReservedLock may 
36728
 
** return true because the caller itself is holding an EXCLUSIVE lock (but
36729
 
** doesn't know it because of a previous error in xUnlock). If this happens
36730
 
** a hot-journal may be mistaken for a journal being created by an active
36731
 
** transaction in another process, causing SQLite to read from the database
36732
 
** without rolling it back.
36733
 
**
36734
 
** To work around this, if a call to xUnlock() fails when unlocking the
36735
 
** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
36736
 
** is only changed back to a real locking state after a successful call
36737
 
** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
36738
 
** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK 
36739
 
** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
36740
 
** lock on the database file before attempting to roll it back. See function
36741
 
** PagerSharedLock() for more detail.
36742
 
**
36743
 
** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in 
36744
 
** PAGER_OPEN state.
36745
 
*/
36746
 
#define UNKNOWN_LOCK                (EXCLUSIVE_LOCK+1)
36747
 
 
36748
 
/*
36749
 
** A macro used for invoking the codec if there is one
36750
 
*/
36751
 
#ifdef SQLITE_HAS_CODEC
36752
 
# define CODEC1(P,D,N,X,E) \
36753
 
    if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
36754
 
# define CODEC2(P,D,N,X,E,O) \
36755
 
    if( P->xCodec==0 ){ O=(char*)D; }else \
36756
 
    if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
36757
 
#else
36758
 
# define CODEC1(P,D,N,X,E)   /* NO-OP */
36759
 
# define CODEC2(P,D,N,X,E,O) O=(char*)D
36760
 
#endif
36761
 
 
36762
 
/*
36763
 
** The maximum allowed sector size. 64KiB. If the xSectorsize() method 
36764
 
** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
36765
 
** This could conceivably cause corruption following a power failure on
36766
 
** such a system. This is currently an undocumented limit.
36767
 
*/
36768
 
#define MAX_SECTOR_SIZE 0x10000
36769
 
 
36770
 
/*
36771
 
** An instance of the following structure is allocated for each active
36772
 
** savepoint and statement transaction in the system. All such structures
36773
 
** are stored in the Pager.aSavepoint[] array, which is allocated and
36774
 
** resized using sqlite3Realloc().
36775
 
**
36776
 
** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
36777
 
** set to 0. If a journal-header is written into the main journal while
36778
 
** the savepoint is active, then iHdrOffset is set to the byte offset 
36779
 
** immediately following the last journal record written into the main
36780
 
** journal before the journal-header. This is required during savepoint
36781
 
** rollback (see pagerPlaybackSavepoint()).
36782
 
*/
36783
 
typedef struct PagerSavepoint PagerSavepoint;
36784
 
struct PagerSavepoint {
36785
 
  i64 iOffset;                 /* Starting offset in main journal */
36786
 
  i64 iHdrOffset;              /* See above */
36787
 
  Bitvec *pInSavepoint;        /* Set of pages in this savepoint */
36788
 
  Pgno nOrig;                  /* Original number of pages in file */
36789
 
  Pgno iSubRec;                /* Index of first record in sub-journal */
36790
 
#ifndef SQLITE_OMIT_WAL
36791
 
  u32 aWalData[WAL_SAVEPOINT_NDATA];        /* WAL savepoint context */
36792
 
#endif
36793
 
};
36794
 
 
36795
 
/*
36796
 
** A open page cache is an instance of struct Pager. A description of
36797
 
** some of the more important member variables follows:
36798
 
**
36799
 
** eState
36800
 
**
36801
 
**   The current 'state' of the pager object. See the comment and state
36802
 
**   diagram above for a description of the pager state.
36803
 
**
36804
 
** eLock
36805
 
**
36806
 
**   For a real on-disk database, the current lock held on the database file -
36807
 
**   NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
36808
 
**
36809
 
**   For a temporary or in-memory database (neither of which require any
36810
 
**   locks), this variable is always set to EXCLUSIVE_LOCK. Since such
36811
 
**   databases always have Pager.exclusiveMode==1, this tricks the pager
36812
 
**   logic into thinking that it already has all the locks it will ever
36813
 
**   need (and no reason to release them).
36814
 
**
36815
 
**   In some (obscure) circumstances, this variable may also be set to
36816
 
**   UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
36817
 
**   details.
36818
 
**
36819
 
** changeCountDone
36820
 
**
36821
 
**   This boolean variable is used to make sure that the change-counter 
36822
 
**   (the 4-byte header field at byte offset 24 of the database file) is 
36823
 
**   not updated more often than necessary. 
36824
 
**
36825
 
**   It is set to true when the change-counter field is updated, which 
36826
 
**   can only happen if an exclusive lock is held on the database file.
36827
 
**   It is cleared (set to false) whenever an exclusive lock is 
36828
 
**   relinquished on the database file. Each time a transaction is committed,
36829
 
**   The changeCountDone flag is inspected. If it is true, the work of
36830
 
**   updating the change-counter is omitted for the current transaction.
36831
 
**
36832
 
**   This mechanism means that when running in exclusive mode, a connection 
36833
 
**   need only update the change-counter once, for the first transaction
36834
 
**   committed.
36835
 
**
36836
 
** setMaster
36837
 
**
36838
 
**   When PagerCommitPhaseOne() is called to commit a transaction, it may
36839
 
**   (or may not) specify a master-journal name to be written into the 
36840
 
**   journal file before it is synced to disk.
36841
 
**
36842
 
**   Whether or not a journal file contains a master-journal pointer affects 
36843
 
**   the way in which the journal file is finalized after the transaction is 
36844
 
**   committed or rolled back when running in "journal_mode=PERSIST" mode.
36845
 
**   If a journal file does not contain a master-journal pointer, it is
36846
 
**   finalized by overwriting the first journal header with zeroes. If
36847
 
**   it does contain a master-journal pointer the journal file is finalized 
36848
 
**   by truncating it to zero bytes, just as if the connection were 
36849
 
**   running in "journal_mode=truncate" mode.
36850
 
**
36851
 
**   Journal files that contain master journal pointers cannot be finalized
36852
 
**   simply by overwriting the first journal-header with zeroes, as the
36853
 
**   master journal pointer could interfere with hot-journal rollback of any
36854
 
**   subsequently interrupted transaction that reuses the journal file.
36855
 
**
36856
 
**   The flag is cleared as soon as the journal file is finalized (either
36857
 
**   by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
36858
 
**   journal file from being successfully finalized, the setMaster flag
36859
 
**   is cleared anyway (and the pager will move to ERROR state).
36860
 
**
36861
 
** doNotSpill, doNotSyncSpill
36862
 
**
36863
 
**   These two boolean variables control the behaviour of cache-spills
36864
 
**   (calls made by the pcache module to the pagerStress() routine to
36865
 
**   write cached data to the file-system in order to free up memory).
36866
 
**
36867
 
**   When doNotSpill is non-zero, writing to the database from pagerStress()
36868
 
**   is disabled altogether. This is done in a very obscure case that
36869
 
**   comes up during savepoint rollback that requires the pcache module
36870
 
**   to allocate a new page to prevent the journal file from being written
36871
 
**   while it is being traversed by code in pager_playback().
36872
 
** 
36873
 
**   If doNotSyncSpill is non-zero, writing to the database from pagerStress()
36874
 
**   is permitted, but syncing the journal file is not. This flag is set
36875
 
**   by sqlite3PagerWrite() when the file-system sector-size is larger than
36876
 
**   the database page-size in order to prevent a journal sync from happening 
36877
 
**   in between the journalling of two pages on the same sector. 
36878
 
**
36879
 
** subjInMemory
36880
 
**
36881
 
**   This is a boolean variable. If true, then any required sub-journal
36882
 
**   is opened as an in-memory journal file. If false, then in-memory
36883
 
**   sub-journals are only used for in-memory pager files.
36884
 
**
36885
 
**   This variable is updated by the upper layer each time a new 
36886
 
**   write-transaction is opened.
36887
 
**
36888
 
** dbSize, dbOrigSize, dbFileSize
36889
 
**
36890
 
**   Variable dbSize is set to the number of pages in the database file.
36891
 
**   It is valid in PAGER_READER and higher states (all states except for
36892
 
**   OPEN and ERROR). 
36893
 
**
36894
 
**   dbSize is set based on the size of the database file, which may be 
36895
 
**   larger than the size of the database (the value stored at offset
36896
 
**   28 of the database header by the btree). If the size of the file
36897
 
**   is not an integer multiple of the page-size, the value stored in
36898
 
**   dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
36899
 
**   Except, any file that is greater than 0 bytes in size is considered
36900
 
**   to have at least one page. (i.e. a 1KB file with 2K page-size leads
36901
 
**   to dbSize==1).
36902
 
**
36903
 
**   During a write-transaction, if pages with page-numbers greater than
36904
 
**   dbSize are modified in the cache, dbSize is updated accordingly.
36905
 
**   Similarly, if the database is truncated using PagerTruncateImage(), 
36906
 
**   dbSize is updated.
36907
 
**
36908
 
**   Variables dbOrigSize and dbFileSize are valid in states 
36909
 
**   PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
36910
 
**   variable at the start of the transaction. It is used during rollback,
36911
 
**   and to determine whether or not pages need to be journalled before
36912
 
**   being modified.
36913
 
**
36914
 
**   Throughout a write-transaction, dbFileSize contains the size of
36915
 
**   the file on disk in pages. It is set to a copy of dbSize when the
36916
 
**   write-transaction is first opened, and updated when VFS calls are made
36917
 
**   to write or truncate the database file on disk. 
36918
 
**
36919
 
**   The only reason the dbFileSize variable is required is to suppress 
36920
 
**   unnecessary calls to xTruncate() after committing a transaction. If, 
36921
 
**   when a transaction is committed, the dbFileSize variable indicates 
36922
 
**   that the database file is larger than the database image (Pager.dbSize), 
36923
 
**   pager_truncate() is called. The pager_truncate() call uses xFilesize()
36924
 
**   to measure the database file on disk, and then truncates it if required.
36925
 
**   dbFileSize is not used when rolling back a transaction. In this case
36926
 
**   pager_truncate() is called unconditionally (which means there may be
36927
 
**   a call to xFilesize() that is not strictly required). In either case,
36928
 
**   pager_truncate() may cause the file to become smaller or larger.
36929
 
**
36930
 
** dbHintSize
36931
 
**
36932
 
**   The dbHintSize variable is used to limit the number of calls made to
36933
 
**   the VFS xFileControl(FCNTL_SIZE_HINT) method. 
36934
 
**
36935
 
**   dbHintSize is set to a copy of the dbSize variable when a
36936
 
**   write-transaction is opened (at the same time as dbFileSize and
36937
 
**   dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
36938
 
**   dbHintSize is increased to the number of pages that correspond to the
36939
 
**   size-hint passed to the method call. See pager_write_pagelist() for 
36940
 
**   details.
36941
 
**
36942
 
** errCode
36943
 
**
36944
 
**   The Pager.errCode variable is only ever used in PAGER_ERROR state. It
36945
 
**   is set to zero in all other states. In PAGER_ERROR state, Pager.errCode 
36946
 
**   is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX 
36947
 
**   sub-codes.
36948
 
*/
36949
 
struct Pager {
36950
 
  sqlite3_vfs *pVfs;          /* OS functions to use for IO */
36951
 
  u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
36952
 
  u8 journalMode;             /* One of the PAGER_JOURNALMODE_* values */
36953
 
  u8 useJournal;              /* Use a rollback journal on this file */
36954
 
  u8 noReadlock;              /* Do not bother to obtain readlocks */
36955
 
  u8 noSync;                  /* Do not sync the journal if true */
36956
 
  u8 fullSync;                /* Do extra syncs of the journal for robustness */
36957
 
  u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
36958
 
  u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
36959
 
  u8 tempFile;                /* zFilename is a temporary file */
36960
 
  u8 readOnly;                /* True for a read-only database */
36961
 
  u8 memDb;                   /* True to inhibit all file I/O */
36962
 
 
36963
 
  /**************************************************************************
36964
 
  ** The following block contains those class members that change during
36965
 
  ** routine opertion.  Class members not in this block are either fixed
36966
 
  ** when the pager is first created or else only change when there is a
36967
 
  ** significant mode change (such as changing the page_size, locking_mode,
36968
 
  ** or the journal_mode).  From another view, these class members describe
36969
 
  ** the "state" of the pager, while other class members describe the
36970
 
  ** "configuration" of the pager.
36971
 
  */
36972
 
  u8 eState;                  /* Pager state (OPEN, READER, WRITER_LOCKED..) */
36973
 
  u8 eLock;                   /* Current lock held on database file */
36974
 
  u8 changeCountDone;         /* Set after incrementing the change-counter */
36975
 
  u8 setMaster;               /* True if a m-j name has been written to jrnl */
36976
 
  u8 doNotSpill;              /* Do not spill the cache when non-zero */
36977
 
  u8 doNotSyncSpill;          /* Do not do a spill that requires jrnl sync */
36978
 
  u8 subjInMemory;            /* True to use in-memory sub-journals */
36979
 
  Pgno dbSize;                /* Number of pages in the database */
36980
 
  Pgno dbOrigSize;            /* dbSize before the current transaction */
36981
 
  Pgno dbFileSize;            /* Number of pages in the database file */
36982
 
  Pgno dbHintSize;            /* Value passed to FCNTL_SIZE_HINT call */
36983
 
  int errCode;                /* One of several kinds of errors */
36984
 
  int nRec;                   /* Pages journalled since last j-header written */
36985
 
  u32 cksumInit;              /* Quasi-random value added to every checksum */
36986
 
  u32 nSubRec;                /* Number of records written to sub-journal */
36987
 
  Bitvec *pInJournal;         /* One bit for each page in the database file */
36988
 
  sqlite3_file *fd;           /* File descriptor for database */
36989
 
  sqlite3_file *jfd;          /* File descriptor for main journal */
36990
 
  sqlite3_file *sjfd;         /* File descriptor for sub-journal */
36991
 
  i64 journalOff;             /* Current write offset in the journal file */
36992
 
  i64 journalHdr;             /* Byte offset to previous journal header */
36993
 
  sqlite3_backup *pBackup;    /* Pointer to list of ongoing backup processes */
36994
 
  PagerSavepoint *aSavepoint; /* Array of active savepoints */
36995
 
  int nSavepoint;             /* Number of elements in aSavepoint[] */
36996
 
  char dbFileVers[16];        /* Changes whenever database file changes */
36997
 
  /*
36998
 
  ** End of the routinely-changing class members
36999
 
  ***************************************************************************/
37000
 
 
37001
 
  u16 nExtra;                 /* Add this many bytes to each in-memory page */
37002
 
  i16 nReserve;               /* Number of unused bytes at end of each page */
37003
 
  u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
37004
 
  u32 sectorSize;             /* Assumed sector size during rollback */
37005
 
  int pageSize;               /* Number of bytes in a page */
37006
 
  Pgno mxPgno;                /* Maximum allowed size of the database */
37007
 
  i64 journalSizeLimit;       /* Size limit for persistent journal files */
37008
 
  char *zFilename;            /* Name of the database file */
37009
 
  char *zJournal;             /* Name of the journal file */
37010
 
  int (*xBusyHandler)(void*); /* Function to call when busy */
37011
 
  void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
37012
 
#ifdef SQLITE_TEST
37013
 
  int nHit, nMiss;            /* Cache hits and missing */
37014
 
  int nRead, nWrite;          /* Database pages read/written */
37015
 
#endif
37016
 
  void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
37017
 
#ifdef SQLITE_HAS_CODEC
37018
 
  void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
37019
 
  void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
37020
 
  void (*xCodecFree)(void*);             /* Destructor for the codec */
37021
 
  void *pCodec;               /* First argument to xCodec... methods */
37022
 
#endif
37023
 
  char *pTmpSpace;            /* Pager.pageSize bytes of space for tmp use */
37024
 
  PCache *pPCache;            /* Pointer to page cache object */
37025
 
#ifndef SQLITE_OMIT_WAL
37026
 
  Wal *pWal;                  /* Write-ahead log used by "journal_mode=wal" */
37027
 
  char *zWal;                 /* File name for write-ahead log */
37028
 
#endif
37029
 
};
37030
 
 
37031
 
/*
37032
 
** The following global variables hold counters used for
37033
 
** testing purposes only.  These variables do not exist in
37034
 
** a non-testing build.  These variables are not thread-safe.
37035
 
*/
37036
 
#ifdef SQLITE_TEST
37037
 
SQLITE_API int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
37038
 
SQLITE_API int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
37039
 
SQLITE_API int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
37040
 
# define PAGER_INCR(v)  v++
37041
 
#else
37042
 
# define PAGER_INCR(v)
37043
 
#endif
37044
 
 
37045
 
 
37046
 
 
37047
 
/*
37048
 
** Journal files begin with the following magic string.  The data
37049
 
** was obtained from /dev/random.  It is used only as a sanity check.
37050
 
**
37051
 
** Since version 2.8.0, the journal format contains additional sanity
37052
 
** checking information.  If the power fails while the journal is being
37053
 
** written, semi-random garbage data might appear in the journal
37054
 
** file after power is restored.  If an attempt is then made
37055
 
** to roll the journal back, the database could be corrupted.  The additional
37056
 
** sanity checking data is an attempt to discover the garbage in the
37057
 
** journal and ignore it.
37058
 
**
37059
 
** The sanity checking information for the new journal format consists
37060
 
** of a 32-bit checksum on each page of data.  The checksum covers both
37061
 
** the page number and the pPager->pageSize bytes of data for the page.
37062
 
** This cksum is initialized to a 32-bit random value that appears in the
37063
 
** journal file right after the header.  The random initializer is important,
37064
 
** because garbage data that appears at the end of a journal is likely
37065
 
** data that was once in other files that have now been deleted.  If the
37066
 
** garbage data came from an obsolete journal file, the checksums might
37067
 
** be correct.  But by initializing the checksum to random value which
37068
 
** is different for every journal, we minimize that risk.
37069
 
*/
37070
 
static const unsigned char aJournalMagic[] = {
37071
 
  0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
37072
 
};
37073
 
 
37074
 
/*
37075
 
** The size of the of each page record in the journal is given by
37076
 
** the following macro.
37077
 
*/
37078
 
#define JOURNAL_PG_SZ(pPager)  ((pPager->pageSize) + 8)
37079
 
 
37080
 
/*
37081
 
** The journal header size for this pager. This is usually the same 
37082
 
** size as a single disk sector. See also setSectorSize().
37083
 
*/
37084
 
#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
37085
 
 
37086
 
/*
37087
 
** The macro MEMDB is true if we are dealing with an in-memory database.
37088
 
** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
37089
 
** the value of MEMDB will be a constant and the compiler will optimize
37090
 
** out code that would never execute.
37091
 
*/
37092
 
#ifdef SQLITE_OMIT_MEMORYDB
37093
 
# define MEMDB 0
37094
 
#else
37095
 
# define MEMDB pPager->memDb
37096
 
#endif
37097
 
 
37098
 
/*
37099
 
** The maximum legal page number is (2^31 - 1).
37100
 
*/
37101
 
#define PAGER_MAX_PGNO 2147483647
37102
 
 
37103
 
/*
37104
 
** The argument to this macro is a file descriptor (type sqlite3_file*).
37105
 
** Return 0 if it is not open, or non-zero (but not 1) if it is.
37106
 
**
37107
 
** This is so that expressions can be written as:
37108
 
**
37109
 
**   if( isOpen(pPager->jfd) ){ ...
37110
 
**
37111
 
** instead of
37112
 
**
37113
 
**   if( pPager->jfd->pMethods ){ ...
37114
 
*/
37115
 
#define isOpen(pFd) ((pFd)->pMethods)
37116
 
 
37117
 
/*
37118
 
** Return true if this pager uses a write-ahead log instead of the usual
37119
 
** rollback journal. Otherwise false.
37120
 
*/
37121
 
#ifndef SQLITE_OMIT_WAL
37122
 
static int pagerUseWal(Pager *pPager){
37123
 
  return (pPager->pWal!=0);
37124
 
}
37125
 
#else
37126
 
# define pagerUseWal(x) 0
37127
 
# define pagerRollbackWal(x) 0
37128
 
# define pagerWalFrames(v,w,x,y,z) 0
37129
 
# define pagerOpenWalIfPresent(z) SQLITE_OK
37130
 
# define pagerBeginReadTransaction(z) SQLITE_OK
37131
 
#endif
37132
 
 
37133
 
#ifndef NDEBUG 
37134
 
/*
37135
 
** Usage:
37136
 
**
37137
 
**   assert( assert_pager_state(pPager) );
37138
 
**
37139
 
** This function runs many asserts to try to find inconsistencies in
37140
 
** the internal state of the Pager object.
37141
 
*/
37142
 
static int assert_pager_state(Pager *p){
37143
 
  Pager *pPager = p;
37144
 
 
37145
 
  /* State must be valid. */
37146
 
  assert( p->eState==PAGER_OPEN
37147
 
       || p->eState==PAGER_READER
37148
 
       || p->eState==PAGER_WRITER_LOCKED
37149
 
       || p->eState==PAGER_WRITER_CACHEMOD
37150
 
       || p->eState==PAGER_WRITER_DBMOD
37151
 
       || p->eState==PAGER_WRITER_FINISHED
37152
 
       || p->eState==PAGER_ERROR
37153
 
  );
37154
 
 
37155
 
  /* Regardless of the current state, a temp-file connection always behaves
37156
 
  ** as if it has an exclusive lock on the database file. It never updates
37157
 
  ** the change-counter field, so the changeCountDone flag is always set.
37158
 
  */
37159
 
  assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
37160
 
  assert( p->tempFile==0 || pPager->changeCountDone );
37161
 
 
37162
 
  /* If the useJournal flag is clear, the journal-mode must be "OFF". 
37163
 
  ** And if the journal-mode is "OFF", the journal file must not be open.
37164
 
  */
37165
 
  assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
37166
 
  assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
37167
 
 
37168
 
  /* Check that MEMDB implies noSync. And an in-memory journal. Since 
37169
 
  ** this means an in-memory pager performs no IO at all, it cannot encounter 
37170
 
  ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing 
37171
 
  ** a journal file. (although the in-memory journal implementation may 
37172
 
  ** return SQLITE_IOERR_NOMEM while the journal file is being written). It 
37173
 
  ** is therefore not possible for an in-memory pager to enter the ERROR 
37174
 
  ** state.
37175
 
  */
37176
 
  if( MEMDB ){
37177
 
    assert( p->noSync );
37178
 
    assert( p->journalMode==PAGER_JOURNALMODE_OFF 
37179
 
         || p->journalMode==PAGER_JOURNALMODE_MEMORY 
37180
 
    );
37181
 
    assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
37182
 
    assert( pagerUseWal(p)==0 );
37183
 
  }
37184
 
 
37185
 
  /* If changeCountDone is set, a RESERVED lock or greater must be held
37186
 
  ** on the file.
37187
 
  */
37188
 
  assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
37189
 
  assert( p->eLock!=PENDING_LOCK );
37190
 
 
37191
 
  switch( p->eState ){
37192
 
    case PAGER_OPEN:
37193
 
      assert( !MEMDB );
37194
 
      assert( pPager->errCode==SQLITE_OK );
37195
 
      assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
37196
 
      break;
37197
 
 
37198
 
    case PAGER_READER:
37199
 
      assert( pPager->errCode==SQLITE_OK );
37200
 
      assert( p->eLock!=UNKNOWN_LOCK );
37201
 
      assert( p->eLock>=SHARED_LOCK || p->noReadlock );
37202
 
      break;
37203
 
 
37204
 
    case PAGER_WRITER_LOCKED:
37205
 
      assert( p->eLock!=UNKNOWN_LOCK );
37206
 
      assert( pPager->errCode==SQLITE_OK );
37207
 
      if( !pagerUseWal(pPager) ){
37208
 
        assert( p->eLock>=RESERVED_LOCK );
37209
 
      }
37210
 
      assert( pPager->dbSize==pPager->dbOrigSize );
37211
 
      assert( pPager->dbOrigSize==pPager->dbFileSize );
37212
 
      assert( pPager->dbOrigSize==pPager->dbHintSize );
37213
 
      assert( pPager->setMaster==0 );
37214
 
      break;
37215
 
 
37216
 
    case PAGER_WRITER_CACHEMOD:
37217
 
      assert( p->eLock!=UNKNOWN_LOCK );
37218
 
      assert( pPager->errCode==SQLITE_OK );
37219
 
      if( !pagerUseWal(pPager) ){
37220
 
        /* It is possible that if journal_mode=wal here that neither the
37221
 
        ** journal file nor the WAL file are open. This happens during
37222
 
        ** a rollback transaction that switches from journal_mode=off
37223
 
        ** to journal_mode=wal.
37224
 
        */
37225
 
        assert( p->eLock>=RESERVED_LOCK );
37226
 
        assert( isOpen(p->jfd) 
37227
 
             || p->journalMode==PAGER_JOURNALMODE_OFF 
37228
 
             || p->journalMode==PAGER_JOURNALMODE_WAL 
37229
 
        );
37230
 
      }
37231
 
      assert( pPager->dbOrigSize==pPager->dbFileSize );
37232
 
      assert( pPager->dbOrigSize==pPager->dbHintSize );
37233
 
      break;
37234
 
 
37235
 
    case PAGER_WRITER_DBMOD:
37236
 
      assert( p->eLock==EXCLUSIVE_LOCK );
37237
 
      assert( pPager->errCode==SQLITE_OK );
37238
 
      assert( !pagerUseWal(pPager) );
37239
 
      assert( p->eLock>=EXCLUSIVE_LOCK );
37240
 
      assert( isOpen(p->jfd) 
37241
 
           || p->journalMode==PAGER_JOURNALMODE_OFF 
37242
 
           || p->journalMode==PAGER_JOURNALMODE_WAL 
37243
 
      );
37244
 
      assert( pPager->dbOrigSize<=pPager->dbHintSize );
37245
 
      break;
37246
 
 
37247
 
    case PAGER_WRITER_FINISHED:
37248
 
      assert( p->eLock==EXCLUSIVE_LOCK );
37249
 
      assert( pPager->errCode==SQLITE_OK );
37250
 
      assert( !pagerUseWal(pPager) );
37251
 
      assert( isOpen(p->jfd) 
37252
 
           || p->journalMode==PAGER_JOURNALMODE_OFF 
37253
 
           || p->journalMode==PAGER_JOURNALMODE_WAL 
37254
 
      );
37255
 
      break;
37256
 
 
37257
 
    case PAGER_ERROR:
37258
 
      /* There must be at least one outstanding reference to the pager if
37259
 
      ** in ERROR state. Otherwise the pager should have already dropped
37260
 
      ** back to OPEN state.
37261
 
      */
37262
 
      assert( pPager->errCode!=SQLITE_OK );
37263
 
      assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
37264
 
      break;
37265
 
  }
37266
 
 
37267
 
  return 1;
37268
 
}
37269
 
#endif /* ifndef NDEBUG */
37270
 
 
37271
 
#ifdef SQLITE_DEBUG 
37272
 
/*
37273
 
** Return a pointer to a human readable string in a static buffer
37274
 
** containing the state of the Pager object passed as an argument. This
37275
 
** is intended to be used within debuggers. For example, as an alternative
37276
 
** to "print *pPager" in gdb:
37277
 
**
37278
 
** (gdb) printf "%s", print_pager_state(pPager)
37279
 
*/
37280
 
static char *print_pager_state(Pager *p){
37281
 
  static char zRet[1024];
37282
 
 
37283
 
  sqlite3_snprintf(1024, zRet,
37284
 
      "Filename:      %s\n"
37285
 
      "State:         %s errCode=%d\n"
37286
 
      "Lock:          %s\n"
37287
 
      "Locking mode:  locking_mode=%s\n"
37288
 
      "Journal mode:  journal_mode=%s\n"
37289
 
      "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
37290
 
      "Journal:       journalOff=%lld journalHdr=%lld\n"
37291
 
      "Size:          dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
37292
 
      , p->zFilename
37293
 
      , p->eState==PAGER_OPEN            ? "OPEN" :
37294
 
        p->eState==PAGER_READER          ? "READER" :
37295
 
        p->eState==PAGER_WRITER_LOCKED   ? "WRITER_LOCKED" :
37296
 
        p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
37297
 
        p->eState==PAGER_WRITER_DBMOD    ? "WRITER_DBMOD" :
37298
 
        p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
37299
 
        p->eState==PAGER_ERROR           ? "ERROR" : "?error?"
37300
 
      , (int)p->errCode
37301
 
      , p->eLock==NO_LOCK         ? "NO_LOCK" :
37302
 
        p->eLock==RESERVED_LOCK   ? "RESERVED" :
37303
 
        p->eLock==EXCLUSIVE_LOCK  ? "EXCLUSIVE" :
37304
 
        p->eLock==SHARED_LOCK     ? "SHARED" :
37305
 
        p->eLock==UNKNOWN_LOCK    ? "UNKNOWN" : "?error?"
37306
 
      , p->exclusiveMode ? "exclusive" : "normal"
37307
 
      , p->journalMode==PAGER_JOURNALMODE_MEMORY   ? "memory" :
37308
 
        p->journalMode==PAGER_JOURNALMODE_OFF      ? "off" :
37309
 
        p->journalMode==PAGER_JOURNALMODE_DELETE   ? "delete" :
37310
 
        p->journalMode==PAGER_JOURNALMODE_PERSIST  ? "persist" :
37311
 
        p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
37312
 
        p->journalMode==PAGER_JOURNALMODE_WAL      ? "wal" : "?error?"
37313
 
      , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
37314
 
      , p->journalOff, p->journalHdr
37315
 
      , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
37316
 
  );
37317
 
 
37318
 
  return zRet;
37319
 
}
37320
 
#endif
37321
 
 
37322
 
/*
37323
 
** Return true if it is necessary to write page *pPg into the sub-journal.
37324
 
** A page needs to be written into the sub-journal if there exists one
37325
 
** or more open savepoints for which:
37326
 
**
37327
 
**   * The page-number is less than or equal to PagerSavepoint.nOrig, and
37328
 
**   * The bit corresponding to the page-number is not set in
37329
 
**     PagerSavepoint.pInSavepoint.
37330
 
*/
37331
 
static int subjRequiresPage(PgHdr *pPg){
37332
 
  Pgno pgno = pPg->pgno;
37333
 
  Pager *pPager = pPg->pPager;
37334
 
  int i;
37335
 
  for(i=0; i<pPager->nSavepoint; i++){
37336
 
    PagerSavepoint *p = &pPager->aSavepoint[i];
37337
 
    if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
37338
 
      return 1;
37339
 
    }
37340
 
  }
37341
 
  return 0;
37342
 
}
37343
 
 
37344
 
/*
37345
 
** Return true if the page is already in the journal file.
37346
 
*/
37347
 
static int pageInJournal(PgHdr *pPg){
37348
 
  return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
37349
 
}
37350
 
 
37351
 
/*
37352
 
** Read a 32-bit integer from the given file descriptor.  Store the integer
37353
 
** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
37354
 
** error code is something goes wrong.
37355
 
**
37356
 
** All values are stored on disk as big-endian.
37357
 
*/
37358
 
static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
37359
 
  unsigned char ac[4];
37360
 
  int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
37361
 
  if( rc==SQLITE_OK ){
37362
 
    *pRes = sqlite3Get4byte(ac);
37363
 
  }
37364
 
  return rc;
37365
 
}
37366
 
 
37367
 
/*
37368
 
** Write a 32-bit integer into a string buffer in big-endian byte order.
37369
 
*/
37370
 
#define put32bits(A,B)  sqlite3Put4byte((u8*)A,B)
37371
 
 
37372
 
 
37373
 
/*
37374
 
** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
37375
 
** on success or an error code is something goes wrong.
37376
 
*/
37377
 
static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
37378
 
  char ac[4];
37379
 
  put32bits(ac, val);
37380
 
  return sqlite3OsWrite(fd, ac, 4, offset);
37381
 
}
37382
 
 
37383
 
/*
37384
 
** Unlock the database file to level eLock, which must be either NO_LOCK
37385
 
** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
37386
 
** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
37387
 
**
37388
 
** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
37389
 
** called, do not modify it. See the comment above the #define of 
37390
 
** UNKNOWN_LOCK for an explanation of this.
37391
 
*/
37392
 
static int pagerUnlockDb(Pager *pPager, int eLock){
37393
 
  int rc = SQLITE_OK;
37394
 
 
37395
 
  assert( !pPager->exclusiveMode || pPager->eLock==eLock );
37396
 
  assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
37397
 
  assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
37398
 
  if( isOpen(pPager->fd) ){
37399
 
    assert( pPager->eLock>=eLock );
37400
 
    rc = sqlite3OsUnlock(pPager->fd, eLock);
37401
 
    if( pPager->eLock!=UNKNOWN_LOCK ){
37402
 
      pPager->eLock = (u8)eLock;
37403
 
    }
37404
 
    IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
37405
 
  }
37406
 
  return rc;
37407
 
}
37408
 
 
37409
 
/*
37410
 
** Lock the database file to level eLock, which must be either SHARED_LOCK,
37411
 
** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
37412
 
** Pager.eLock variable to the new locking state. 
37413
 
**
37414
 
** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is 
37415
 
** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK. 
37416
 
** See the comment above the #define of UNKNOWN_LOCK for an explanation 
37417
 
** of this.
37418
 
*/
37419
 
static int pagerLockDb(Pager *pPager, int eLock){
37420
 
  int rc = SQLITE_OK;
37421
 
 
37422
 
  assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
37423
 
  if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
37424
 
    rc = sqlite3OsLock(pPager->fd, eLock);
37425
 
    if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
37426
 
      pPager->eLock = (u8)eLock;
37427
 
      IOTRACE(("LOCK %p %d\n", pPager, eLock))
37428
 
    }
37429
 
  }
37430
 
  return rc;
37431
 
}
37432
 
 
37433
 
/*
37434
 
** This function determines whether or not the atomic-write optimization
37435
 
** can be used with this pager. The optimization can be used if:
37436
 
**
37437
 
**  (a) the value returned by OsDeviceCharacteristics() indicates that
37438
 
**      a database page may be written atomically, and
37439
 
**  (b) the value returned by OsSectorSize() is less than or equal
37440
 
**      to the page size.
37441
 
**
37442
 
** The optimization is also always enabled for temporary files. It is
37443
 
** an error to call this function if pPager is opened on an in-memory
37444
 
** database.
37445
 
**
37446
 
** If the optimization cannot be used, 0 is returned. If it can be used,
37447
 
** then the value returned is the size of the journal file when it
37448
 
** contains rollback data for exactly one page.
37449
 
*/
37450
 
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
37451
 
static int jrnlBufferSize(Pager *pPager){
37452
 
  assert( !MEMDB );
37453
 
  if( !pPager->tempFile ){
37454
 
    int dc;                           /* Device characteristics */
37455
 
    int nSector;                      /* Sector size */
37456
 
    int szPage;                       /* Page size */
37457
 
 
37458
 
    assert( isOpen(pPager->fd) );
37459
 
    dc = sqlite3OsDeviceCharacteristics(pPager->fd);
37460
 
    nSector = pPager->sectorSize;
37461
 
    szPage = pPager->pageSize;
37462
 
 
37463
 
    assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
37464
 
    assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
37465
 
    if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
37466
 
      return 0;
37467
 
    }
37468
 
  }
37469
 
 
37470
 
  return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
37471
 
}
37472
 
#endif
37473
 
 
37474
 
/*
37475
 
** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
37476
 
** on the cache using a hash function.  This is used for testing
37477
 
** and debugging only.
37478
 
*/
37479
 
#ifdef SQLITE_CHECK_PAGES
37480
 
/*
37481
 
** Return a 32-bit hash of the page data for pPage.
37482
 
*/
37483
 
static u32 pager_datahash(int nByte, unsigned char *pData){
37484
 
  u32 hash = 0;
37485
 
  int i;
37486
 
  for(i=0; i<nByte; i++){
37487
 
    hash = (hash*1039) + pData[i];
37488
 
  }
37489
 
  return hash;
37490
 
}
37491
 
static u32 pager_pagehash(PgHdr *pPage){
37492
 
  return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
37493
 
}
37494
 
static void pager_set_pagehash(PgHdr *pPage){
37495
 
  pPage->pageHash = pager_pagehash(pPage);
37496
 
}
37497
 
 
37498
 
/*
37499
 
** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
37500
 
** is defined, and NDEBUG is not defined, an assert() statement checks
37501
 
** that the page is either dirty or still matches the calculated page-hash.
37502
 
*/
37503
 
#define CHECK_PAGE(x) checkPage(x)
37504
 
static void checkPage(PgHdr *pPg){
37505
 
  Pager *pPager = pPg->pPager;
37506
 
  assert( pPager->eState!=PAGER_ERROR );
37507
 
  assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
37508
 
}
37509
 
 
37510
 
#else
37511
 
#define pager_datahash(X,Y)  0
37512
 
#define pager_pagehash(X)  0
37513
 
#define pager_set_pagehash(X)
37514
 
#define CHECK_PAGE(x)
37515
 
#endif  /* SQLITE_CHECK_PAGES */
37516
 
 
37517
 
/*
37518
 
** When this is called the journal file for pager pPager must be open.
37519
 
** This function attempts to read a master journal file name from the 
37520
 
** end of the file and, if successful, copies it into memory supplied 
37521
 
** by the caller. See comments above writeMasterJournal() for the format
37522
 
** used to store a master journal file name at the end of a journal file.
37523
 
**
37524
 
** zMaster must point to a buffer of at least nMaster bytes allocated by
37525
 
** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
37526
 
** enough space to write the master journal name). If the master journal
37527
 
** name in the journal is longer than nMaster bytes (including a
37528
 
** nul-terminator), then this is handled as if no master journal name
37529
 
** were present in the journal.
37530
 
**
37531
 
** If a master journal file name is present at the end of the journal
37532
 
** file, then it is copied into the buffer pointed to by zMaster. A
37533
 
** nul-terminator byte is appended to the buffer following the master
37534
 
** journal file name.
37535
 
**
37536
 
** If it is determined that no master journal file name is present 
37537
 
** zMaster[0] is set to 0 and SQLITE_OK returned.
37538
 
**
37539
 
** If an error occurs while reading from the journal file, an SQLite
37540
 
** error code is returned.
37541
 
*/
37542
 
static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
37543
 
  int rc;                    /* Return code */
37544
 
  u32 len;                   /* Length in bytes of master journal name */
37545
 
  i64 szJ;                   /* Total size in bytes of journal file pJrnl */
37546
 
  u32 cksum;                 /* MJ checksum value read from journal */
37547
 
  u32 u;                     /* Unsigned loop counter */
37548
 
  unsigned char aMagic[8];   /* A buffer to hold the magic header */
37549
 
  zMaster[0] = '\0';
37550
 
 
37551
 
  if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
37552
 
   || szJ<16
37553
 
   || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
37554
 
   || len>=nMaster 
37555
 
   || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
37556
 
   || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
37557
 
   || memcmp(aMagic, aJournalMagic, 8)
37558
 
   || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
37559
 
  ){
37560
 
    return rc;
37561
 
  }
37562
 
 
37563
 
  /* See if the checksum matches the master journal name */
37564
 
  for(u=0; u<len; u++){
37565
 
    cksum -= zMaster[u];
37566
 
  }
37567
 
  if( cksum ){
37568
 
    /* If the checksum doesn't add up, then one or more of the disk sectors
37569
 
    ** containing the master journal filename is corrupted. This means
37570
 
    ** definitely roll back, so just return SQLITE_OK and report a (nul)
37571
 
    ** master-journal filename.
37572
 
    */
37573
 
    len = 0;
37574
 
  }
37575
 
  zMaster[len] = '\0';
37576
 
   
37577
 
  return SQLITE_OK;
37578
 
}
37579
 
 
37580
 
/*
37581
 
** Return the offset of the sector boundary at or immediately 
37582
 
** following the value in pPager->journalOff, assuming a sector 
37583
 
** size of pPager->sectorSize bytes.
37584
 
**
37585
 
** i.e for a sector size of 512:
37586
 
**
37587
 
**   Pager.journalOff          Return value
37588
 
**   ---------------------------------------
37589
 
**   0                         0
37590
 
**   512                       512
37591
 
**   100                       512
37592
 
**   2000                      2048
37593
 
** 
37594
 
*/
37595
 
static i64 journalHdrOffset(Pager *pPager){
37596
 
  i64 offset = 0;
37597
 
  i64 c = pPager->journalOff;
37598
 
  if( c ){
37599
 
    offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
37600
 
  }
37601
 
  assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
37602
 
  assert( offset>=c );
37603
 
  assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
37604
 
  return offset;
37605
 
}
37606
 
 
37607
 
/*
37608
 
** The journal file must be open when this function is called.
37609
 
**
37610
 
** This function is a no-op if the journal file has not been written to
37611
 
** within the current transaction (i.e. if Pager.journalOff==0).
37612
 
**
37613
 
** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
37614
 
** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
37615
 
** zero the 28-byte header at the start of the journal file. In either case, 
37616
 
** if the pager is not in no-sync mode, sync the journal file immediately 
37617
 
** after writing or truncating it.
37618
 
**
37619
 
** If Pager.journalSizeLimit is set to a positive, non-zero value, and
37620
 
** following the truncation or zeroing described above the size of the 
37621
 
** journal file in bytes is larger than this value, then truncate the
37622
 
** journal file to Pager.journalSizeLimit bytes. The journal file does
37623
 
** not need to be synced following this operation.
37624
 
**
37625
 
** If an IO error occurs, abandon processing and return the IO error code.
37626
 
** Otherwise, return SQLITE_OK.
37627
 
*/
37628
 
static int zeroJournalHdr(Pager *pPager, int doTruncate){
37629
 
  int rc = SQLITE_OK;                               /* Return code */
37630
 
  assert( isOpen(pPager->jfd) );
37631
 
  if( pPager->journalOff ){
37632
 
    const i64 iLimit = pPager->journalSizeLimit;    /* Local cache of jsl */
37633
 
 
37634
 
    IOTRACE(("JZEROHDR %p\n", pPager))
37635
 
    if( doTruncate || iLimit==0 ){
37636
 
      rc = sqlite3OsTruncate(pPager->jfd, 0);
37637
 
    }else{
37638
 
      static const char zeroHdr[28] = {0};
37639
 
      rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
37640
 
    }
37641
 
    if( rc==SQLITE_OK && !pPager->noSync ){
37642
 
      rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
37643
 
    }
37644
 
 
37645
 
    /* At this point the transaction is committed but the write lock 
37646
 
    ** is still held on the file. If there is a size limit configured for 
37647
 
    ** the persistent journal and the journal file currently consumes more
37648
 
    ** space than that limit allows for, truncate it now. There is no need
37649
 
    ** to sync the file following this operation.
37650
 
    */
37651
 
    if( rc==SQLITE_OK && iLimit>0 ){
37652
 
      i64 sz;
37653
 
      rc = sqlite3OsFileSize(pPager->jfd, &sz);
37654
 
      if( rc==SQLITE_OK && sz>iLimit ){
37655
 
        rc = sqlite3OsTruncate(pPager->jfd, iLimit);
37656
 
      }
37657
 
    }
37658
 
  }
37659
 
  return rc;
37660
 
}
37661
 
 
37662
 
/*
37663
 
** The journal file must be open when this routine is called. A journal
37664
 
** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
37665
 
** current location.
37666
 
**
37667
 
** The format for the journal header is as follows:
37668
 
** - 8 bytes: Magic identifying journal format.
37669
 
** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
37670
 
** - 4 bytes: Random number used for page hash.
37671
 
** - 4 bytes: Initial database page count.
37672
 
** - 4 bytes: Sector size used by the process that wrote this journal.
37673
 
** - 4 bytes: Database page size.
37674
 
** 
37675
 
** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
37676
 
*/
37677
 
static int writeJournalHdr(Pager *pPager){
37678
 
  int rc = SQLITE_OK;                 /* Return code */
37679
 
  char *zHeader = pPager->pTmpSpace;  /* Temporary space used to build header */
37680
 
  u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
37681
 
  u32 nWrite;                         /* Bytes of header sector written */
37682
 
  int ii;                             /* Loop counter */
37683
 
 
37684
 
  assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
37685
 
 
37686
 
  if( nHeader>JOURNAL_HDR_SZ(pPager) ){
37687
 
    nHeader = JOURNAL_HDR_SZ(pPager);
37688
 
  }
37689
 
 
37690
 
  /* If there are active savepoints and any of them were created 
37691
 
  ** since the most recent journal header was written, update the 
37692
 
  ** PagerSavepoint.iHdrOffset fields now.
37693
 
  */
37694
 
  for(ii=0; ii<pPager->nSavepoint; ii++){
37695
 
    if( pPager->aSavepoint[ii].iHdrOffset==0 ){
37696
 
      pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
37697
 
    }
37698
 
  }
37699
 
 
37700
 
  pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
37701
 
 
37702
 
  /* 
37703
 
  ** Write the nRec Field - the number of page records that follow this
37704
 
  ** journal header. Normally, zero is written to this value at this time.
37705
 
  ** After the records are added to the journal (and the journal synced, 
37706
 
  ** if in full-sync mode), the zero is overwritten with the true number
37707
 
  ** of records (see syncJournal()).
37708
 
  **
37709
 
  ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
37710
 
  ** reading the journal this value tells SQLite to assume that the
37711
 
  ** rest of the journal file contains valid page records. This assumption
37712
 
  ** is dangerous, as if a failure occurred whilst writing to the journal
37713
 
  ** file it may contain some garbage data. There are two scenarios
37714
 
  ** where this risk can be ignored:
37715
 
  **
37716
 
  **   * When the pager is in no-sync mode. Corruption can follow a
37717
 
  **     power failure in this case anyway.
37718
 
  **
37719
 
  **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
37720
 
  **     that garbage data is never appended to the journal file.
37721
 
  */
37722
 
  assert( isOpen(pPager->fd) || pPager->noSync );
37723
 
  if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
37724
 
   || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) 
37725
 
  ){
37726
 
    memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
37727
 
    put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
37728
 
  }else{
37729
 
    memset(zHeader, 0, sizeof(aJournalMagic)+4);
37730
 
  }
37731
 
 
37732
 
  /* The random check-hash initialiser */ 
37733
 
  sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
37734
 
  put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
37735
 
  /* The initial database size */
37736
 
  put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
37737
 
  /* The assumed sector size for this process */
37738
 
  put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
37739
 
 
37740
 
  /* The page size */
37741
 
  put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
37742
 
 
37743
 
  /* Initializing the tail of the buffer is not necessary.  Everything
37744
 
  ** works find if the following memset() is omitted.  But initializing
37745
 
  ** the memory prevents valgrind from complaining, so we are willing to
37746
 
  ** take the performance hit.
37747
 
  */
37748
 
  memset(&zHeader[sizeof(aJournalMagic)+20], 0,
37749
 
         nHeader-(sizeof(aJournalMagic)+20));
37750
 
 
37751
 
  /* In theory, it is only necessary to write the 28 bytes that the 
37752
 
  ** journal header consumes to the journal file here. Then increment the 
37753
 
  ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next 
37754
 
  ** record is written to the following sector (leaving a gap in the file
37755
 
  ** that will be implicitly filled in by the OS).
37756
 
  **
37757
 
  ** However it has been discovered that on some systems this pattern can 
37758
 
  ** be significantly slower than contiguously writing data to the file,
37759
 
  ** even if that means explicitly writing data to the block of 
37760
 
  ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
37761
 
  ** is done. 
37762
 
  **
37763
 
  ** The loop is required here in case the sector-size is larger than the 
37764
 
  ** database page size. Since the zHeader buffer is only Pager.pageSize
37765
 
  ** bytes in size, more than one call to sqlite3OsWrite() may be required
37766
 
  ** to populate the entire journal header sector.
37767
 
  */ 
37768
 
  for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
37769
 
    IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
37770
 
    rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
37771
 
    assert( pPager->journalHdr <= pPager->journalOff );
37772
 
    pPager->journalOff += nHeader;
37773
 
  }
37774
 
 
37775
 
  return rc;
37776
 
}
37777
 
 
37778
 
/*
37779
 
** The journal file must be open when this is called. A journal header file
37780
 
** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
37781
 
** file. The current location in the journal file is given by
37782
 
** pPager->journalOff. See comments above function writeJournalHdr() for
37783
 
** a description of the journal header format.
37784
 
**
37785
 
** If the header is read successfully, *pNRec is set to the number of
37786
 
** page records following this header and *pDbSize is set to the size of the
37787
 
** database before the transaction began, in pages. Also, pPager->cksumInit
37788
 
** is set to the value read from the journal header. SQLITE_OK is returned
37789
 
** in this case.
37790
 
**
37791
 
** If the journal header file appears to be corrupted, SQLITE_DONE is
37792
 
** returned and *pNRec and *PDbSize are undefined.  If JOURNAL_HDR_SZ bytes
37793
 
** cannot be read from the journal file an error code is returned.
37794
 
*/
37795
 
static int readJournalHdr(
37796
 
  Pager *pPager,               /* Pager object */
37797
 
  int isHot,
37798
 
  i64 journalSize,             /* Size of the open journal file in bytes */
37799
 
  u32 *pNRec,                  /* OUT: Value read from the nRec field */
37800
 
  u32 *pDbSize                 /* OUT: Value of original database size field */
37801
 
){
37802
 
  int rc;                      /* Return code */
37803
 
  unsigned char aMagic[8];     /* A buffer to hold the magic header */
37804
 
  i64 iHdrOff;                 /* Offset of journal header being read */
37805
 
 
37806
 
  assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
37807
 
 
37808
 
  /* Advance Pager.journalOff to the start of the next sector. If the
37809
 
  ** journal file is too small for there to be a header stored at this
37810
 
  ** point, return SQLITE_DONE.
37811
 
  */
37812
 
  pPager->journalOff = journalHdrOffset(pPager);
37813
 
  if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
37814
 
    return SQLITE_DONE;
37815
 
  }
37816
 
  iHdrOff = pPager->journalOff;
37817
 
 
37818
 
  /* Read in the first 8 bytes of the journal header. If they do not match
37819
 
  ** the  magic string found at the start of each journal header, return
37820
 
  ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
37821
 
  ** proceed.
37822
 
  */
37823
 
  if( isHot || iHdrOff!=pPager->journalHdr ){
37824
 
    rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
37825
 
    if( rc ){
37826
 
      return rc;
37827
 
    }
37828
 
    if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
37829
 
      return SQLITE_DONE;
37830
 
    }
37831
 
  }
37832
 
 
37833
 
  /* Read the first three 32-bit fields of the journal header: The nRec
37834
 
  ** field, the checksum-initializer and the database size at the start
37835
 
  ** of the transaction. Return an error code if anything goes wrong.
37836
 
  */
37837
 
  if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
37838
 
   || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
37839
 
   || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
37840
 
  ){
37841
 
    return rc;
37842
 
  }
37843
 
 
37844
 
  if( pPager->journalOff==0 ){
37845
 
    u32 iPageSize;               /* Page-size field of journal header */
37846
 
    u32 iSectorSize;             /* Sector-size field of journal header */
37847
 
 
37848
 
    /* Read the page-size and sector-size journal header fields. */
37849
 
    if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
37850
 
     || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
37851
 
    ){
37852
 
      return rc;
37853
 
    }
37854
 
 
37855
 
    /* Versions of SQLite prior to 3.5.8 set the page-size field of the
37856
 
    ** journal header to zero. In this case, assume that the Pager.pageSize
37857
 
    ** variable is already set to the correct page size.
37858
 
    */
37859
 
    if( iPageSize==0 ){
37860
 
      iPageSize = pPager->pageSize;
37861
 
    }
37862
 
 
37863
 
    /* Check that the values read from the page-size and sector-size fields
37864
 
    ** are within range. To be 'in range', both values need to be a power
37865
 
    ** of two greater than or equal to 512 or 32, and not greater than their 
37866
 
    ** respective compile time maximum limits.
37867
 
    */
37868
 
    if( iPageSize<512                  || iSectorSize<32
37869
 
     || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
37870
 
     || ((iPageSize-1)&iPageSize)!=0   || ((iSectorSize-1)&iSectorSize)!=0 
37871
 
    ){
37872
 
      /* If the either the page-size or sector-size in the journal-header is 
37873
 
      ** invalid, then the process that wrote the journal-header must have 
37874
 
      ** crashed before the header was synced. In this case stop reading 
37875
 
      ** the journal file here.
37876
 
      */
37877
 
      return SQLITE_DONE;
37878
 
    }
37879
 
 
37880
 
    /* Update the page-size to match the value read from the journal. 
37881
 
    ** Use a testcase() macro to make sure that malloc failure within 
37882
 
    ** PagerSetPagesize() is tested.
37883
 
    */
37884
 
    rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
37885
 
    testcase( rc!=SQLITE_OK );
37886
 
 
37887
 
    /* Update the assumed sector-size to match the value used by 
37888
 
    ** the process that created this journal. If this journal was
37889
 
    ** created by a process other than this one, then this routine
37890
 
    ** is being called from within pager_playback(). The local value
37891
 
    ** of Pager.sectorSize is restored at the end of that routine.
37892
 
    */
37893
 
    pPager->sectorSize = iSectorSize;
37894
 
  }
37895
 
 
37896
 
  pPager->journalOff += JOURNAL_HDR_SZ(pPager);
37897
 
  return rc;
37898
 
}
37899
 
 
37900
 
 
37901
 
/*
37902
 
** Write the supplied master journal name into the journal file for pager
37903
 
** pPager at the current location. The master journal name must be the last
37904
 
** thing written to a journal file. If the pager is in full-sync mode, the
37905
 
** journal file descriptor is advanced to the next sector boundary before
37906
 
** anything is written. The format is:
37907
 
**
37908
 
**   + 4 bytes: PAGER_MJ_PGNO.
37909
 
**   + N bytes: Master journal filename in utf-8.
37910
 
**   + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
37911
 
**   + 4 bytes: Master journal name checksum.
37912
 
**   + 8 bytes: aJournalMagic[].
37913
 
**
37914
 
** The master journal page checksum is the sum of the bytes in the master
37915
 
** journal name, where each byte is interpreted as a signed 8-bit integer.
37916
 
**
37917
 
** If zMaster is a NULL pointer (occurs for a single database transaction), 
37918
 
** this call is a no-op.
37919
 
*/
37920
 
static int writeMasterJournal(Pager *pPager, const char *zMaster){
37921
 
  int rc;                          /* Return code */
37922
 
  int nMaster;                     /* Length of string zMaster */
37923
 
  i64 iHdrOff;                     /* Offset of header in journal file */
37924
 
  i64 jrnlSize;                    /* Size of journal file on disk */
37925
 
  u32 cksum = 0;                   /* Checksum of string zMaster */
37926
 
 
37927
 
  assert( pPager->setMaster==0 );
37928
 
  assert( !pagerUseWal(pPager) );
37929
 
 
37930
 
  if( !zMaster 
37931
 
   || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
37932
 
   || pPager->journalMode==PAGER_JOURNALMODE_OFF 
37933
 
  ){
37934
 
    return SQLITE_OK;
37935
 
  }
37936
 
  pPager->setMaster = 1;
37937
 
  assert( isOpen(pPager->jfd) );
37938
 
  assert( pPager->journalHdr <= pPager->journalOff );
37939
 
 
37940
 
  /* Calculate the length in bytes and the checksum of zMaster */
37941
 
  for(nMaster=0; zMaster[nMaster]; nMaster++){
37942
 
    cksum += zMaster[nMaster];
37943
 
  }
37944
 
 
37945
 
  /* If in full-sync mode, advance to the next disk sector before writing
37946
 
  ** the master journal name. This is in case the previous page written to
37947
 
  ** the journal has already been synced.
37948
 
  */
37949
 
  if( pPager->fullSync ){
37950
 
    pPager->journalOff = journalHdrOffset(pPager);
37951
 
  }
37952
 
  iHdrOff = pPager->journalOff;
37953
 
 
37954
 
  /* Write the master journal data to the end of the journal file. If
37955
 
  ** an error occurs, return the error code to the caller.
37956
 
  */
37957
 
  if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
37958
 
   || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
37959
 
   || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
37960
 
   || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
37961
 
   || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
37962
 
  ){
37963
 
    return rc;
37964
 
  }
37965
 
  pPager->journalOff += (nMaster+20);
37966
 
 
37967
 
  /* If the pager is in peristent-journal mode, then the physical 
37968
 
  ** journal-file may extend past the end of the master-journal name
37969
 
  ** and 8 bytes of magic data just written to the file. This is 
37970
 
  ** dangerous because the code to rollback a hot-journal file
37971
 
  ** will not be able to find the master-journal name to determine 
37972
 
  ** whether or not the journal is hot. 
37973
 
  **
37974
 
  ** Easiest thing to do in this scenario is to truncate the journal 
37975
 
  ** file to the required size.
37976
 
  */ 
37977
 
  if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
37978
 
   && jrnlSize>pPager->journalOff
37979
 
  ){
37980
 
    rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
37981
 
  }
37982
 
  return rc;
37983
 
}
37984
 
 
37985
 
/*
37986
 
** Find a page in the hash table given its page number. Return
37987
 
** a pointer to the page or NULL if the requested page is not 
37988
 
** already in memory.
37989
 
*/
37990
 
static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
37991
 
  PgHdr *p;                         /* Return value */
37992
 
 
37993
 
  /* It is not possible for a call to PcacheFetch() with createFlag==0 to
37994
 
  ** fail, since no attempt to allocate dynamic memory will be made.
37995
 
  */
37996
 
  (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
37997
 
  return p;
37998
 
}
37999
 
 
38000
 
/*
38001
 
** Discard the entire contents of the in-memory page-cache.
38002
 
*/
38003
 
static void pager_reset(Pager *pPager){
38004
 
  sqlite3BackupRestart(pPager->pBackup);
38005
 
  sqlite3PcacheClear(pPager->pPCache);
38006
 
}
38007
 
 
38008
 
/*
38009
 
** Free all structures in the Pager.aSavepoint[] array and set both
38010
 
** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
38011
 
** if it is open and the pager is not in exclusive mode.
38012
 
*/
38013
 
static void releaseAllSavepoints(Pager *pPager){
38014
 
  int ii;               /* Iterator for looping through Pager.aSavepoint */
38015
 
  for(ii=0; ii<pPager->nSavepoint; ii++){
38016
 
    sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
38017
 
  }
38018
 
  if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
38019
 
    sqlite3OsClose(pPager->sjfd);
38020
 
  }
38021
 
  sqlite3_free(pPager->aSavepoint);
38022
 
  pPager->aSavepoint = 0;
38023
 
  pPager->nSavepoint = 0;
38024
 
  pPager->nSubRec = 0;
38025
 
}
38026
 
 
38027
 
/*
38028
 
** Set the bit number pgno in the PagerSavepoint.pInSavepoint 
38029
 
** bitvecs of all open savepoints. Return SQLITE_OK if successful
38030
 
** or SQLITE_NOMEM if a malloc failure occurs.
38031
 
*/
38032
 
static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
38033
 
  int ii;                   /* Loop counter */
38034
 
  int rc = SQLITE_OK;       /* Result code */
38035
 
 
38036
 
  for(ii=0; ii<pPager->nSavepoint; ii++){
38037
 
    PagerSavepoint *p = &pPager->aSavepoint[ii];
38038
 
    if( pgno<=p->nOrig ){
38039
 
      rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
38040
 
      testcase( rc==SQLITE_NOMEM );
38041
 
      assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
38042
 
    }
38043
 
  }
38044
 
  return rc;
38045
 
}
38046
 
 
38047
 
/*
38048
 
** This function is a no-op if the pager is in exclusive mode and not
38049
 
** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
38050
 
** state.
38051
 
**
38052
 
** If the pager is not in exclusive-access mode, the database file is
38053
 
** completely unlocked. If the file is unlocked and the file-system does
38054
 
** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
38055
 
** closed (if it is open).
38056
 
**
38057
 
** If the pager is in ERROR state when this function is called, the 
38058
 
** contents of the pager cache are discarded before switching back to 
38059
 
** the OPEN state. Regardless of whether the pager is in exclusive-mode
38060
 
** or not, any journal file left in the file-system will be treated
38061
 
** as a hot-journal and rolled back the next time a read-transaction
38062
 
** is opened (by this or by any other connection).
38063
 
*/
38064
 
static void pager_unlock(Pager *pPager){
38065
 
 
38066
 
  assert( pPager->eState==PAGER_READER 
38067
 
       || pPager->eState==PAGER_OPEN 
38068
 
       || pPager->eState==PAGER_ERROR 
38069
 
  );
38070
 
 
38071
 
  sqlite3BitvecDestroy(pPager->pInJournal);
38072
 
  pPager->pInJournal = 0;
38073
 
  releaseAllSavepoints(pPager);
38074
 
 
38075
 
  if( pagerUseWal(pPager) ){
38076
 
    assert( !isOpen(pPager->jfd) );
38077
 
    sqlite3WalEndReadTransaction(pPager->pWal);
38078
 
    pPager->eState = PAGER_OPEN;
38079
 
  }else if( !pPager->exclusiveMode ){
38080
 
    int rc;                       /* Error code returned by pagerUnlockDb() */
38081
 
    int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
38082
 
 
38083
 
    /* If the operating system support deletion of open files, then
38084
 
    ** close the journal file when dropping the database lock.  Otherwise
38085
 
    ** another connection with journal_mode=delete might delete the file
38086
 
    ** out from under us.
38087
 
    */
38088
 
    assert( (PAGER_JOURNALMODE_MEMORY   & 5)!=1 );
38089
 
    assert( (PAGER_JOURNALMODE_OFF      & 5)!=1 );
38090
 
    assert( (PAGER_JOURNALMODE_WAL      & 5)!=1 );
38091
 
    assert( (PAGER_JOURNALMODE_DELETE   & 5)!=1 );
38092
 
    assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
38093
 
    assert( (PAGER_JOURNALMODE_PERSIST  & 5)==1 );
38094
 
    if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
38095
 
     || 1!=(pPager->journalMode & 5)
38096
 
    ){
38097
 
      sqlite3OsClose(pPager->jfd);
38098
 
    }
38099
 
 
38100
 
    /* If the pager is in the ERROR state and the call to unlock the database
38101
 
    ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
38102
 
    ** above the #define for UNKNOWN_LOCK for an explanation of why this
38103
 
    ** is necessary.
38104
 
    */
38105
 
    rc = pagerUnlockDb(pPager, NO_LOCK);
38106
 
    if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
38107
 
      pPager->eLock = UNKNOWN_LOCK;
38108
 
    }
38109
 
 
38110
 
    /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
38111
 
    ** without clearing the error code. This is intentional - the error
38112
 
    ** code is cleared and the cache reset in the block below.
38113
 
    */
38114
 
    assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
38115
 
    pPager->changeCountDone = 0;
38116
 
    pPager->eState = PAGER_OPEN;
38117
 
  }
38118
 
 
38119
 
  /* If Pager.errCode is set, the contents of the pager cache cannot be
38120
 
  ** trusted. Now that there are no outstanding references to the pager,
38121
 
  ** it can safely move back to PAGER_OPEN state. This happens in both
38122
 
  ** normal and exclusive-locking mode.
38123
 
  */
38124
 
  if( pPager->errCode ){
38125
 
    assert( !MEMDB );
38126
 
    pager_reset(pPager);
38127
 
    pPager->changeCountDone = pPager->tempFile;
38128
 
    pPager->eState = PAGER_OPEN;
38129
 
    pPager->errCode = SQLITE_OK;
38130
 
  }
38131
 
 
38132
 
  pPager->journalOff = 0;
38133
 
  pPager->journalHdr = 0;
38134
 
  pPager->setMaster = 0;
38135
 
}
38136
 
 
38137
 
/*
38138
 
** This function is called whenever an IOERR or FULL error that requires
38139
 
** the pager to transition into the ERROR state may ahve occurred.
38140
 
** The first argument is a pointer to the pager structure, the second 
38141
 
** the error-code about to be returned by a pager API function. The 
38142
 
** value returned is a copy of the second argument to this function. 
38143
 
**
38144
 
** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
38145
 
** IOERR sub-codes, the pager enters the ERROR state and the error code
38146
 
** is stored in Pager.errCode. While the pager remains in the ERROR state,
38147
 
** all major API calls on the Pager will immediately return Pager.errCode.
38148
 
**
38149
 
** The ERROR state indicates that the contents of the pager-cache 
38150
 
** cannot be trusted. This state can be cleared by completely discarding 
38151
 
** the contents of the pager-cache. If a transaction was active when
38152
 
** the persistent error occurred, then the rollback journal may need
38153
 
** to be replayed to restore the contents of the database file (as if
38154
 
** it were a hot-journal).
38155
 
*/
38156
 
static int pager_error(Pager *pPager, int rc){
38157
 
  int rc2 = rc & 0xff;
38158
 
  assert( rc==SQLITE_OK || !MEMDB );
38159
 
  assert(
38160
 
       pPager->errCode==SQLITE_FULL ||
38161
 
       pPager->errCode==SQLITE_OK ||
38162
 
       (pPager->errCode & 0xff)==SQLITE_IOERR
38163
 
  );
38164
 
  if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
38165
 
    pPager->errCode = rc;
38166
 
    pPager->eState = PAGER_ERROR;
38167
 
  }
38168
 
  return rc;
38169
 
}
38170
 
 
38171
 
/*
38172
 
** This routine ends a transaction. A transaction is usually ended by 
38173
 
** either a COMMIT or a ROLLBACK operation. This routine may be called 
38174
 
** after rollback of a hot-journal, or if an error occurs while opening
38175
 
** the journal file or writing the very first journal-header of a
38176
 
** database transaction.
38177
 
** 
38178
 
** This routine is never called in PAGER_ERROR state. If it is called
38179
 
** in PAGER_NONE or PAGER_SHARED state and the lock held is less
38180
 
** exclusive than a RESERVED lock, it is a no-op.
38181
 
**
38182
 
** Otherwise, any active savepoints are released.
38183
 
**
38184
 
** If the journal file is open, then it is "finalized". Once a journal 
38185
 
** file has been finalized it is not possible to use it to roll back a 
38186
 
** transaction. Nor will it be considered to be a hot-journal by this
38187
 
** or any other database connection. Exactly how a journal is finalized
38188
 
** depends on whether or not the pager is running in exclusive mode and
38189
 
** the current journal-mode (Pager.journalMode value), as follows:
38190
 
**
38191
 
**   journalMode==MEMORY
38192
 
**     Journal file descriptor is simply closed. This destroys an 
38193
 
**     in-memory journal.
38194
 
**
38195
 
**   journalMode==TRUNCATE
38196
 
**     Journal file is truncated to zero bytes in size.
38197
 
**
38198
 
**   journalMode==PERSIST
38199
 
**     The first 28 bytes of the journal file are zeroed. This invalidates
38200
 
**     the first journal header in the file, and hence the entire journal
38201
 
**     file. An invalid journal file cannot be rolled back.
38202
 
**
38203
 
**   journalMode==DELETE
38204
 
**     The journal file is closed and deleted using sqlite3OsDelete().
38205
 
**
38206
 
**     If the pager is running in exclusive mode, this method of finalizing
38207
 
**     the journal file is never used. Instead, if the journalMode is
38208
 
**     DELETE and the pager is in exclusive mode, the method described under
38209
 
**     journalMode==PERSIST is used instead.
38210
 
**
38211
 
** After the journal is finalized, the pager moves to PAGER_READER state.
38212
 
** If running in non-exclusive rollback mode, the lock on the file is 
38213
 
** downgraded to a SHARED_LOCK.
38214
 
**
38215
 
** SQLITE_OK is returned if no error occurs. If an error occurs during
38216
 
** any of the IO operations to finalize the journal file or unlock the
38217
 
** database then the IO error code is returned to the user. If the 
38218
 
** operation to finalize the journal file fails, then the code still
38219
 
** tries to unlock the database file if not in exclusive mode. If the
38220
 
** unlock operation fails as well, then the first error code related
38221
 
** to the first error encountered (the journal finalization one) is
38222
 
** returned.
38223
 
*/
38224
 
static int pager_end_transaction(Pager *pPager, int hasMaster){
38225
 
  int rc = SQLITE_OK;      /* Error code from journal finalization operation */
38226
 
  int rc2 = SQLITE_OK;     /* Error code from db file unlock operation */
38227
 
 
38228
 
  /* Do nothing if the pager does not have an open write transaction
38229
 
  ** or at least a RESERVED lock. This function may be called when there
38230
 
  ** is no write-transaction active but a RESERVED or greater lock is
38231
 
  ** held under two circumstances:
38232
 
  **
38233
 
  **   1. After a successful hot-journal rollback, it is called with
38234
 
  **      eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
38235
 
  **
38236
 
  **   2. If a connection with locking_mode=exclusive holding an EXCLUSIVE 
38237
 
  **      lock switches back to locking_mode=normal and then executes a
38238
 
  **      read-transaction, this function is called with eState==PAGER_READER 
38239
 
  **      and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
38240
 
  */
38241
 
  assert( assert_pager_state(pPager) );
38242
 
  assert( pPager->eState!=PAGER_ERROR );
38243
 
  if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
38244
 
    return SQLITE_OK;
38245
 
  }
38246
 
 
38247
 
  releaseAllSavepoints(pPager);
38248
 
  assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
38249
 
  if( isOpen(pPager->jfd) ){
38250
 
    assert( !pagerUseWal(pPager) );
38251
 
 
38252
 
    /* Finalize the journal file. */
38253
 
    if( sqlite3IsMemJournal(pPager->jfd) ){
38254
 
      assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
38255
 
      sqlite3OsClose(pPager->jfd);
38256
 
    }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
38257
 
      if( pPager->journalOff==0 ){
38258
 
        rc = SQLITE_OK;
38259
 
      }else{
38260
 
        rc = sqlite3OsTruncate(pPager->jfd, 0);
38261
 
      }
38262
 
      pPager->journalOff = 0;
38263
 
    }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
38264
 
      || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
38265
 
    ){
38266
 
      rc = zeroJournalHdr(pPager, hasMaster);
38267
 
      pPager->journalOff = 0;
38268
 
    }else{
38269
 
      /* This branch may be executed with Pager.journalMode==MEMORY if
38270
 
      ** a hot-journal was just rolled back. In this case the journal
38271
 
      ** file should be closed and deleted. If this connection writes to
38272
 
      ** the database file, it will do so using an in-memory journal. 
38273
 
      */
38274
 
      assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE 
38275
 
           || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
38276
 
           || pPager->journalMode==PAGER_JOURNALMODE_WAL 
38277
 
      );
38278
 
      sqlite3OsClose(pPager->jfd);
38279
 
      if( !pPager->tempFile ){
38280
 
        rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
38281
 
      }
38282
 
    }
38283
 
  }
38284
 
 
38285
 
#ifdef SQLITE_CHECK_PAGES
38286
 
  sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
38287
 
  if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
38288
 
    PgHdr *p = pager_lookup(pPager, 1);
38289
 
    if( p ){
38290
 
      p->pageHash = 0;
38291
 
      sqlite3PagerUnref(p);
38292
 
    }
38293
 
  }
38294
 
#endif
38295
 
 
38296
 
  sqlite3BitvecDestroy(pPager->pInJournal);
38297
 
  pPager->pInJournal = 0;
38298
 
  pPager->nRec = 0;
38299
 
  sqlite3PcacheCleanAll(pPager->pPCache);
38300
 
  sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
38301
 
 
38302
 
  if( pagerUseWal(pPager) ){
38303
 
    /* Drop the WAL write-lock, if any. Also, if the connection was in 
38304
 
    ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE 
38305
 
    ** lock held on the database file.
38306
 
    */
38307
 
    rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
38308
 
    assert( rc2==SQLITE_OK );
38309
 
  }
38310
 
  if( !pPager->exclusiveMode 
38311
 
   && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
38312
 
  ){
38313
 
    rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
38314
 
    pPager->changeCountDone = 0;
38315
 
  }
38316
 
  pPager->eState = PAGER_READER;
38317
 
  pPager->setMaster = 0;
38318
 
 
38319
 
  return (rc==SQLITE_OK?rc2:rc);
38320
 
}
38321
 
 
38322
 
/*
38323
 
** Execute a rollback if a transaction is active and unlock the 
38324
 
** database file. 
38325
 
**
38326
 
** If the pager has already entered the ERROR state, do not attempt 
38327
 
** the rollback at this time. Instead, pager_unlock() is called. The
38328
 
** call to pager_unlock() will discard all in-memory pages, unlock
38329
 
** the database file and move the pager back to OPEN state. If this 
38330
 
** means that there is a hot-journal left in the file-system, the next 
38331
 
** connection to obtain a shared lock on the pager (which may be this one) 
38332
 
** will roll it back.
38333
 
**
38334
 
** If the pager has not already entered the ERROR state, but an IO or
38335
 
** malloc error occurs during a rollback, then this will itself cause 
38336
 
** the pager to enter the ERROR state. Which will be cleared by the
38337
 
** call to pager_unlock(), as described above.
38338
 
*/
38339
 
static void pagerUnlockAndRollback(Pager *pPager){
38340
 
  if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
38341
 
    assert( assert_pager_state(pPager) );
38342
 
    if( pPager->eState>=PAGER_WRITER_LOCKED ){
38343
 
      sqlite3BeginBenignMalloc();
38344
 
      sqlite3PagerRollback(pPager);
38345
 
      sqlite3EndBenignMalloc();
38346
 
    }else if( !pPager->exclusiveMode ){
38347
 
      assert( pPager->eState==PAGER_READER );
38348
 
      pager_end_transaction(pPager, 0);
38349
 
    }
38350
 
  }
38351
 
  pager_unlock(pPager);
38352
 
}
38353
 
 
38354
 
/*
38355
 
** Parameter aData must point to a buffer of pPager->pageSize bytes
38356
 
** of data. Compute and return a checksum based ont the contents of the 
38357
 
** page of data and the current value of pPager->cksumInit.
38358
 
**
38359
 
** This is not a real checksum. It is really just the sum of the 
38360
 
** random initial value (pPager->cksumInit) and every 200th byte
38361
 
** of the page data, starting with byte offset (pPager->pageSize%200).
38362
 
** Each byte is interpreted as an 8-bit unsigned integer.
38363
 
**
38364
 
** Changing the formula used to compute this checksum results in an
38365
 
** incompatible journal file format.
38366
 
**
38367
 
** If journal corruption occurs due to a power failure, the most likely 
38368
 
** scenario is that one end or the other of the record will be changed. 
38369
 
** It is much less likely that the two ends of the journal record will be
38370
 
** correct and the middle be corrupt.  Thus, this "checksum" scheme,
38371
 
** though fast and simple, catches the mostly likely kind of corruption.
38372
 
*/
38373
 
static u32 pager_cksum(Pager *pPager, const u8 *aData){
38374
 
  u32 cksum = pPager->cksumInit;         /* Checksum value to return */
38375
 
  int i = pPager->pageSize-200;          /* Loop counter */
38376
 
  while( i>0 ){
38377
 
    cksum += aData[i];
38378
 
    i -= 200;
38379
 
  }
38380
 
  return cksum;
38381
 
}
38382
 
 
38383
 
/*
38384
 
** Report the current page size and number of reserved bytes back
38385
 
** to the codec.
38386
 
*/
38387
 
#ifdef SQLITE_HAS_CODEC
38388
 
static void pagerReportSize(Pager *pPager){
38389
 
  if( pPager->xCodecSizeChng ){
38390
 
    pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
38391
 
                           (int)pPager->nReserve);
38392
 
  }
38393
 
}
38394
 
#else
38395
 
# define pagerReportSize(X)     /* No-op if we do not support a codec */
38396
 
#endif
38397
 
 
38398
 
/*
38399
 
** Read a single page from either the journal file (if isMainJrnl==1) or
38400
 
** from the sub-journal (if isMainJrnl==0) and playback that page.
38401
 
** The page begins at offset *pOffset into the file. The *pOffset
38402
 
** value is increased to the start of the next page in the journal.
38403
 
**
38404
 
** The main rollback journal uses checksums - the statement journal does 
38405
 
** not.
38406
 
**
38407
 
** If the page number of the page record read from the (sub-)journal file
38408
 
** is greater than the current value of Pager.dbSize, then playback is
38409
 
** skipped and SQLITE_OK is returned.
38410
 
**
38411
 
** If pDone is not NULL, then it is a record of pages that have already
38412
 
** been played back.  If the page at *pOffset has already been played back
38413
 
** (if the corresponding pDone bit is set) then skip the playback.
38414
 
** Make sure the pDone bit corresponding to the *pOffset page is set
38415
 
** prior to returning.
38416
 
**
38417
 
** If the page record is successfully read from the (sub-)journal file
38418
 
** and played back, then SQLITE_OK is returned. If an IO error occurs
38419
 
** while reading the record from the (sub-)journal file or while writing
38420
 
** to the database file, then the IO error code is returned. If data
38421
 
** is successfully read from the (sub-)journal file but appears to be
38422
 
** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
38423
 
** two circumstances:
38424
 
** 
38425
 
**   * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
38426
 
**   * If the record is being rolled back from the main journal file
38427
 
**     and the checksum field does not match the record content.
38428
 
**
38429
 
** Neither of these two scenarios are possible during a savepoint rollback.
38430
 
**
38431
 
** If this is a savepoint rollback, then memory may have to be dynamically
38432
 
** allocated by this function. If this is the case and an allocation fails,
38433
 
** SQLITE_NOMEM is returned.
38434
 
*/
38435
 
static int pager_playback_one_page(
38436
 
  Pager *pPager,                /* The pager being played back */
38437
 
  i64 *pOffset,                 /* Offset of record to playback */
38438
 
  Bitvec *pDone,                /* Bitvec of pages already played back */
38439
 
  int isMainJrnl,               /* 1 -> main journal. 0 -> sub-journal. */
38440
 
  int isSavepnt                 /* True for a savepoint rollback */
38441
 
){
38442
 
  int rc;
38443
 
  PgHdr *pPg;                   /* An existing page in the cache */
38444
 
  Pgno pgno;                    /* The page number of a page in journal */
38445
 
  u32 cksum;                    /* Checksum used for sanity checking */
38446
 
  char *aData;                  /* Temporary storage for the page */
38447
 
  sqlite3_file *jfd;            /* The file descriptor for the journal file */
38448
 
  int isSynced;                 /* True if journal page is synced */
38449
 
 
38450
 
  assert( (isMainJrnl&~1)==0 );      /* isMainJrnl is 0 or 1 */
38451
 
  assert( (isSavepnt&~1)==0 );       /* isSavepnt is 0 or 1 */
38452
 
  assert( isMainJrnl || pDone );     /* pDone always used on sub-journals */
38453
 
  assert( isSavepnt || pDone==0 );   /* pDone never used on non-savepoint */
38454
 
 
38455
 
  aData = pPager->pTmpSpace;
38456
 
  assert( aData );         /* Temp storage must have already been allocated */
38457
 
  assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
38458
 
 
38459
 
  /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction 
38460
 
  ** or savepoint rollback done at the request of the caller) or this is
38461
 
  ** a hot-journal rollback. If it is a hot-journal rollback, the pager
38462
 
  ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
38463
 
  ** only reads from the main journal, not the sub-journal.
38464
 
  */
38465
 
  assert( pPager->eState>=PAGER_WRITER_CACHEMOD
38466
 
       || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
38467
 
  );
38468
 
  assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
38469
 
 
38470
 
  /* Read the page number and page data from the journal or sub-journal
38471
 
  ** file. Return an error code to the caller if an IO error occurs.
38472
 
  */
38473
 
  jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
38474
 
  rc = read32bits(jfd, *pOffset, &pgno);
38475
 
  if( rc!=SQLITE_OK ) return rc;
38476
 
  rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
38477
 
  if( rc!=SQLITE_OK ) return rc;
38478
 
  *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
38479
 
 
38480
 
  /* Sanity checking on the page.  This is more important that I originally
38481
 
  ** thought.  If a power failure occurs while the journal is being written,
38482
 
  ** it could cause invalid data to be written into the journal.  We need to
38483
 
  ** detect this invalid data (with high probability) and ignore it.
38484
 
  */
38485
 
  if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
38486
 
    assert( !isSavepnt );
38487
 
    return SQLITE_DONE;
38488
 
  }
38489
 
  if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
38490
 
    return SQLITE_OK;
38491
 
  }
38492
 
  if( isMainJrnl ){
38493
 
    rc = read32bits(jfd, (*pOffset)-4, &cksum);
38494
 
    if( rc ) return rc;
38495
 
    if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
38496
 
      return SQLITE_DONE;
38497
 
    }
38498
 
  }
38499
 
 
38500
 
  /* If this page has already been played by before during the current
38501
 
  ** rollback, then don't bother to play it back again.
38502
 
  */
38503
 
  if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
38504
 
    return rc;
38505
 
  }
38506
 
 
38507
 
  /* When playing back page 1, restore the nReserve setting
38508
 
  */
38509
 
  if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
38510
 
    pPager->nReserve = ((u8*)aData)[20];
38511
 
    pagerReportSize(pPager);
38512
 
  }
38513
 
 
38514
 
  /* If the pager is in CACHEMOD state, then there must be a copy of this
38515
 
  ** page in the pager cache. In this case just update the pager cache,
38516
 
  ** not the database file. The page is left marked dirty in this case.
38517
 
  **
38518
 
  ** An exception to the above rule: If the database is in no-sync mode
38519
 
  ** and a page is moved during an incremental vacuum then the page may
38520
 
  ** not be in the pager cache. Later: if a malloc() or IO error occurs
38521
 
  ** during a Movepage() call, then the page may not be in the cache
38522
 
  ** either. So the condition described in the above paragraph is not
38523
 
  ** assert()able.
38524
 
  **
38525
 
  ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
38526
 
  ** pager cache if it exists and the main file. The page is then marked 
38527
 
  ** not dirty. Since this code is only executed in PAGER_OPEN state for
38528
 
  ** a hot-journal rollback, it is guaranteed that the page-cache is empty
38529
 
  ** if the pager is in OPEN state.
38530
 
  **
38531
 
  ** Ticket #1171:  The statement journal might contain page content that is
38532
 
  ** different from the page content at the start of the transaction.
38533
 
  ** This occurs when a page is changed prior to the start of a statement
38534
 
  ** then changed again within the statement.  When rolling back such a
38535
 
  ** statement we must not write to the original database unless we know
38536
 
  ** for certain that original page contents are synced into the main rollback
38537
 
  ** journal.  Otherwise, a power loss might leave modified data in the
38538
 
  ** database file without an entry in the rollback journal that can
38539
 
  ** restore the database to its original form.  Two conditions must be
38540
 
  ** met before writing to the database files. (1) the database must be
38541
 
  ** locked.  (2) we know that the original page content is fully synced
38542
 
  ** in the main journal either because the page is not in cache or else
38543
 
  ** the page is marked as needSync==0.
38544
 
  **
38545
 
  ** 2008-04-14:  When attempting to vacuum a corrupt database file, it
38546
 
  ** is possible to fail a statement on a database that does not yet exist.
38547
 
  ** Do not attempt to write if database file has never been opened.
38548
 
  */
38549
 
  if( pagerUseWal(pPager) ){
38550
 
    pPg = 0;
38551
 
  }else{
38552
 
    pPg = pager_lookup(pPager, pgno);
38553
 
  }
38554
 
  assert( pPg || !MEMDB );
38555
 
  assert( pPager->eState!=PAGER_OPEN || pPg==0 );
38556
 
  PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
38557
 
           PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
38558
 
           (isMainJrnl?"main-journal":"sub-journal")
38559
 
  ));
38560
 
  if( isMainJrnl ){
38561
 
    isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
38562
 
  }else{
38563
 
    isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
38564
 
  }
38565
 
  if( isOpen(pPager->fd)
38566
 
   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
38567
 
   && isSynced
38568
 
  ){
38569
 
    i64 ofst = (pgno-1)*(i64)pPager->pageSize;
38570
 
    testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
38571
 
    assert( !pagerUseWal(pPager) );
38572
 
    rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
38573
 
    if( pgno>pPager->dbFileSize ){
38574
 
      pPager->dbFileSize = pgno;
38575
 
    }
38576
 
    if( pPager->pBackup ){
38577
 
      CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
38578
 
      sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
38579
 
      CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
38580
 
    }
38581
 
  }else if( !isMainJrnl && pPg==0 ){
38582
 
    /* If this is a rollback of a savepoint and data was not written to
38583
 
    ** the database and the page is not in-memory, there is a potential
38584
 
    ** problem. When the page is next fetched by the b-tree layer, it 
38585
 
    ** will be read from the database file, which may or may not be 
38586
 
    ** current. 
38587
 
    **
38588
 
    ** There are a couple of different ways this can happen. All are quite
38589
 
    ** obscure. When running in synchronous mode, this can only happen 
38590
 
    ** if the page is on the free-list at the start of the transaction, then
38591
 
    ** populated, then moved using sqlite3PagerMovepage().
38592
 
    **
38593
 
    ** The solution is to add an in-memory page to the cache containing
38594
 
    ** the data just read from the sub-journal. Mark the page as dirty 
38595
 
    ** and if the pager requires a journal-sync, then mark the page as 
38596
 
    ** requiring a journal-sync before it is written.
38597
 
    */
38598
 
    assert( isSavepnt );
38599
 
    assert( pPager->doNotSpill==0 );
38600
 
    pPager->doNotSpill++;
38601
 
    rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
38602
 
    assert( pPager->doNotSpill==1 );
38603
 
    pPager->doNotSpill--;
38604
 
    if( rc!=SQLITE_OK ) return rc;
38605
 
    pPg->flags &= ~PGHDR_NEED_READ;
38606
 
    sqlite3PcacheMakeDirty(pPg);
38607
 
  }
38608
 
  if( pPg ){
38609
 
    /* No page should ever be explicitly rolled back that is in use, except
38610
 
    ** for page 1 which is held in use in order to keep the lock on the
38611
 
    ** database active. However such a page may be rolled back as a result
38612
 
    ** of an internal error resulting in an automatic call to
38613
 
    ** sqlite3PagerRollback().
38614
 
    */
38615
 
    void *pData;
38616
 
    pData = pPg->pData;
38617
 
    memcpy(pData, (u8*)aData, pPager->pageSize);
38618
 
    pPager->xReiniter(pPg);
38619
 
    if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
38620
 
      /* If the contents of this page were just restored from the main 
38621
 
      ** journal file, then its content must be as they were when the 
38622
 
      ** transaction was first opened. In this case we can mark the page
38623
 
      ** as clean, since there will be no need to write it out to the
38624
 
      ** database.
38625
 
      **
38626
 
      ** There is one exception to this rule. If the page is being rolled
38627
 
      ** back as part of a savepoint (or statement) rollback from an 
38628
 
      ** unsynced portion of the main journal file, then it is not safe
38629
 
      ** to mark the page as clean. This is because marking the page as
38630
 
      ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
38631
 
      ** already in the journal file (recorded in Pager.pInJournal) and
38632
 
      ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
38633
 
      ** again within this transaction, it will be marked as dirty but
38634
 
      ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
38635
 
      ** be written out into the database file before its journal file
38636
 
      ** segment is synced. If a crash occurs during or following this,
38637
 
      ** database corruption may ensue.
38638
 
      */
38639
 
      assert( !pagerUseWal(pPager) );
38640
 
      sqlite3PcacheMakeClean(pPg);
38641
 
    }
38642
 
    pager_set_pagehash(pPg);
38643
 
 
38644
 
    /* If this was page 1, then restore the value of Pager.dbFileVers.
38645
 
    ** Do this before any decoding. */
38646
 
    if( pgno==1 ){
38647
 
      memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
38648
 
    }
38649
 
 
38650
 
    /* Decode the page just read from disk */
38651
 
    CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
38652
 
    sqlite3PcacheRelease(pPg);
38653
 
  }
38654
 
  return rc;
38655
 
}
38656
 
 
38657
 
/*
38658
 
** Parameter zMaster is the name of a master journal file. A single journal
38659
 
** file that referred to the master journal file has just been rolled back.
38660
 
** This routine checks if it is possible to delete the master journal file,
38661
 
** and does so if it is.
38662
 
**
38663
 
** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not 
38664
 
** available for use within this function.
38665
 
**
38666
 
** When a master journal file is created, it is populated with the names 
38667
 
** of all of its child journals, one after another, formatted as utf-8 
38668
 
** encoded text. The end of each child journal file is marked with a 
38669
 
** nul-terminator byte (0x00). i.e. the entire contents of a master journal
38670
 
** file for a transaction involving two databases might be:
38671
 
**
38672
 
**   "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
38673
 
**
38674
 
** A master journal file may only be deleted once all of its child 
38675
 
** journals have been rolled back.
38676
 
**
38677
 
** This function reads the contents of the master-journal file into 
38678
 
** memory and loops through each of the child journal names. For
38679
 
** each child journal, it checks if:
38680
 
**
38681
 
**   * if the child journal exists, and if so
38682
 
**   * if the child journal contains a reference to master journal 
38683
 
**     file zMaster
38684
 
**
38685
 
** If a child journal can be found that matches both of the criteria
38686
 
** above, this function returns without doing anything. Otherwise, if
38687
 
** no such child journal can be found, file zMaster is deleted from
38688
 
** the file-system using sqlite3OsDelete().
38689
 
**
38690
 
** If an IO error within this function, an error code is returned. This
38691
 
** function allocates memory by calling sqlite3Malloc(). If an allocation
38692
 
** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors 
38693
 
** occur, SQLITE_OK is returned.
38694
 
**
38695
 
** TODO: This function allocates a single block of memory to load
38696
 
** the entire contents of the master journal file. This could be
38697
 
** a couple of kilobytes or so - potentially larger than the page 
38698
 
** size.
38699
 
*/
38700
 
static int pager_delmaster(Pager *pPager, const char *zMaster){
38701
 
  sqlite3_vfs *pVfs = pPager->pVfs;
38702
 
  int rc;                   /* Return code */
38703
 
  sqlite3_file *pMaster;    /* Malloc'd master-journal file descriptor */
38704
 
  sqlite3_file *pJournal;   /* Malloc'd child-journal file descriptor */
38705
 
  char *zMasterJournal = 0; /* Contents of master journal file */
38706
 
  i64 nMasterJournal;       /* Size of master journal file */
38707
 
  char *zJournal;           /* Pointer to one journal within MJ file */
38708
 
  char *zMasterPtr;         /* Space to hold MJ filename from a journal file */
38709
 
  int nMasterPtr;           /* Amount of space allocated to zMasterPtr[] */
38710
 
 
38711
 
  /* Allocate space for both the pJournal and pMaster file descriptors.
38712
 
  ** If successful, open the master journal file for reading.
38713
 
  */
38714
 
  pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
38715
 
  pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
38716
 
  if( !pMaster ){
38717
 
    rc = SQLITE_NOMEM;
38718
 
  }else{
38719
 
    const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
38720
 
    rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
38721
 
  }
38722
 
  if( rc!=SQLITE_OK ) goto delmaster_out;
38723
 
 
38724
 
  /* Load the entire master journal file into space obtained from
38725
 
  ** sqlite3_malloc() and pointed to by zMasterJournal.   Also obtain
38726
 
  ** sufficient space (in zMasterPtr) to hold the names of master
38727
 
  ** journal files extracted from regular rollback-journals.
38728
 
  */
38729
 
  rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
38730
 
  if( rc!=SQLITE_OK ) goto delmaster_out;
38731
 
  nMasterPtr = pVfs->mxPathname+1;
38732
 
  zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
38733
 
  if( !zMasterJournal ){
38734
 
    rc = SQLITE_NOMEM;
38735
 
    goto delmaster_out;
38736
 
  }
38737
 
  zMasterPtr = &zMasterJournal[nMasterJournal+1];
38738
 
  rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
38739
 
  if( rc!=SQLITE_OK ) goto delmaster_out;
38740
 
  zMasterJournal[nMasterJournal] = 0;
38741
 
 
38742
 
  zJournal = zMasterJournal;
38743
 
  while( (zJournal-zMasterJournal)<nMasterJournal ){
38744
 
    int exists;
38745
 
    rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
38746
 
    if( rc!=SQLITE_OK ){
38747
 
      goto delmaster_out;
38748
 
    }
38749
 
    if( exists ){
38750
 
      /* One of the journals pointed to by the master journal exists.
38751
 
      ** Open it and check if it points at the master journal. If
38752
 
      ** so, return without deleting the master journal file.
38753
 
      */
38754
 
      int c;
38755
 
      int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
38756
 
      rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
38757
 
      if( rc!=SQLITE_OK ){
38758
 
        goto delmaster_out;
38759
 
      }
38760
 
 
38761
 
      rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
38762
 
      sqlite3OsClose(pJournal);
38763
 
      if( rc!=SQLITE_OK ){
38764
 
        goto delmaster_out;
38765
 
      }
38766
 
 
38767
 
      c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
38768
 
      if( c ){
38769
 
        /* We have a match. Do not delete the master journal file. */
38770
 
        goto delmaster_out;
38771
 
      }
38772
 
    }
38773
 
    zJournal += (sqlite3Strlen30(zJournal)+1);
38774
 
  }
38775
 
 
38776
 
  sqlite3OsClose(pMaster);
38777
 
  rc = sqlite3OsDelete(pVfs, zMaster, 0);
38778
 
 
38779
 
delmaster_out:
38780
 
  sqlite3_free(zMasterJournal);
38781
 
  if( pMaster ){
38782
 
    sqlite3OsClose(pMaster);
38783
 
    assert( !isOpen(pJournal) );
38784
 
    sqlite3_free(pMaster);
38785
 
  }
38786
 
  return rc;
38787
 
}
38788
 
 
38789
 
 
38790
 
/*
38791
 
** This function is used to change the actual size of the database 
38792
 
** file in the file-system. This only happens when committing a transaction,
38793
 
** or rolling back a transaction (including rolling back a hot-journal).
38794
 
**
38795
 
** If the main database file is not open, or the pager is not in either
38796
 
** DBMOD or OPEN state, this function is a no-op. Otherwise, the size 
38797
 
** of the file is changed to nPage pages (nPage*pPager->pageSize bytes). 
38798
 
** If the file on disk is currently larger than nPage pages, then use the VFS
38799
 
** xTruncate() method to truncate it.
38800
 
**
38801
 
** Or, it might might be the case that the file on disk is smaller than 
38802
 
** nPage pages. Some operating system implementations can get confused if 
38803
 
** you try to truncate a file to some size that is larger than it 
38804
 
** currently is, so detect this case and write a single zero byte to 
38805
 
** the end of the new file instead.
38806
 
**
38807
 
** If successful, return SQLITE_OK. If an IO error occurs while modifying
38808
 
** the database file, return the error code to the caller.
38809
 
*/
38810
 
static int pager_truncate(Pager *pPager, Pgno nPage){
38811
 
  int rc = SQLITE_OK;
38812
 
  assert( pPager->eState!=PAGER_ERROR );
38813
 
  assert( pPager->eState!=PAGER_READER );
38814
 
  
38815
 
  if( isOpen(pPager->fd) 
38816
 
   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) 
38817
 
  ){
38818
 
    i64 currentSize, newSize;
38819
 
    int szPage = pPager->pageSize;
38820
 
    assert( pPager->eLock==EXCLUSIVE_LOCK );
38821
 
    /* TODO: Is it safe to use Pager.dbFileSize here? */
38822
 
    rc = sqlite3OsFileSize(pPager->fd, &currentSize);
38823
 
    newSize = szPage*(i64)nPage;
38824
 
    if( rc==SQLITE_OK && currentSize!=newSize ){
38825
 
      if( currentSize>newSize ){
38826
 
        rc = sqlite3OsTruncate(pPager->fd, newSize);
38827
 
      }else{
38828
 
        char *pTmp = pPager->pTmpSpace;
38829
 
        memset(pTmp, 0, szPage);
38830
 
        testcase( (newSize-szPage) <  currentSize );
38831
 
        testcase( (newSize-szPage) == currentSize );
38832
 
        testcase( (newSize-szPage) >  currentSize );
38833
 
        rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
38834
 
      }
38835
 
      if( rc==SQLITE_OK ){
38836
 
        pPager->dbFileSize = nPage;
38837
 
      }
38838
 
    }
38839
 
  }
38840
 
  return rc;
38841
 
}
38842
 
 
38843
 
/*
38844
 
** Set the value of the Pager.sectorSize variable for the given
38845
 
** pager based on the value returned by the xSectorSize method
38846
 
** of the open database file. The sector size will be used used 
38847
 
** to determine the size and alignment of journal header and 
38848
 
** master journal pointers within created journal files.
38849
 
**
38850
 
** For temporary files the effective sector size is always 512 bytes.
38851
 
**
38852
 
** Otherwise, for non-temporary files, the effective sector size is
38853
 
** the value returned by the xSectorSize() method rounded up to 32 if
38854
 
** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
38855
 
** is greater than MAX_SECTOR_SIZE.
38856
 
*/
38857
 
static void setSectorSize(Pager *pPager){
38858
 
  assert( isOpen(pPager->fd) || pPager->tempFile );
38859
 
 
38860
 
  if( !pPager->tempFile ){
38861
 
    /* Sector size doesn't matter for temporary files. Also, the file
38862
 
    ** may not have been opened yet, in which case the OsSectorSize()
38863
 
    ** call will segfault.
38864
 
    */
38865
 
    pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
38866
 
  }
38867
 
  if( pPager->sectorSize<32 ){
38868
 
    pPager->sectorSize = 512;
38869
 
  }
38870
 
  if( pPager->sectorSize>MAX_SECTOR_SIZE ){
38871
 
    assert( MAX_SECTOR_SIZE>=512 );
38872
 
    pPager->sectorSize = MAX_SECTOR_SIZE;
38873
 
  }
38874
 
}
38875
 
 
38876
 
/*
38877
 
** Playback the journal and thus restore the database file to
38878
 
** the state it was in before we started making changes.  
38879
 
**
38880
 
** The journal file format is as follows: 
38881
 
**
38882
 
**  (1)  8 byte prefix.  A copy of aJournalMagic[].
38883
 
**  (2)  4 byte big-endian integer which is the number of valid page records
38884
 
**       in the journal.  If this value is 0xffffffff, then compute the
38885
 
**       number of page records from the journal size.
38886
 
**  (3)  4 byte big-endian integer which is the initial value for the 
38887
 
**       sanity checksum.
38888
 
**  (4)  4 byte integer which is the number of pages to truncate the
38889
 
**       database to during a rollback.
38890
 
**  (5)  4 byte big-endian integer which is the sector size.  The header
38891
 
**       is this many bytes in size.
38892
 
**  (6)  4 byte big-endian integer which is the page size.
38893
 
**  (7)  zero padding out to the next sector size.
38894
 
**  (8)  Zero or more pages instances, each as follows:
38895
 
**        +  4 byte page number.
38896
 
**        +  pPager->pageSize bytes of data.
38897
 
**        +  4 byte checksum
38898
 
**
38899
 
** When we speak of the journal header, we mean the first 7 items above.
38900
 
** Each entry in the journal is an instance of the 8th item.
38901
 
**
38902
 
** Call the value from the second bullet "nRec".  nRec is the number of
38903
 
** valid page entries in the journal.  In most cases, you can compute the
38904
 
** value of nRec from the size of the journal file.  But if a power
38905
 
** failure occurred while the journal was being written, it could be the
38906
 
** case that the size of the journal file had already been increased but
38907
 
** the extra entries had not yet made it safely to disk.  In such a case,
38908
 
** the value of nRec computed from the file size would be too large.  For
38909
 
** that reason, we always use the nRec value in the header.
38910
 
**
38911
 
** If the nRec value is 0xffffffff it means that nRec should be computed
38912
 
** from the file size.  This value is used when the user selects the
38913
 
** no-sync option for the journal.  A power failure could lead to corruption
38914
 
** in this case.  But for things like temporary table (which will be
38915
 
** deleted when the power is restored) we don't care.  
38916
 
**
38917
 
** If the file opened as the journal file is not a well-formed
38918
 
** journal file then all pages up to the first corrupted page are rolled
38919
 
** back (or no pages if the journal header is corrupted). The journal file
38920
 
** is then deleted and SQLITE_OK returned, just as if no corruption had
38921
 
** been encountered.
38922
 
**
38923
 
** If an I/O or malloc() error occurs, the journal-file is not deleted
38924
 
** and an error code is returned.
38925
 
**
38926
 
** The isHot parameter indicates that we are trying to rollback a journal
38927
 
** that might be a hot journal.  Or, it could be that the journal is 
38928
 
** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
38929
 
** If the journal really is hot, reset the pager cache prior rolling
38930
 
** back any content.  If the journal is merely persistent, no reset is
38931
 
** needed.
38932
 
*/
38933
 
static int pager_playback(Pager *pPager, int isHot){
38934
 
  sqlite3_vfs *pVfs = pPager->pVfs;
38935
 
  i64 szJ;                 /* Size of the journal file in bytes */
38936
 
  u32 nRec;                /* Number of Records in the journal */
38937
 
  u32 u;                   /* Unsigned loop counter */
38938
 
  Pgno mxPg = 0;           /* Size of the original file in pages */
38939
 
  int rc;                  /* Result code of a subroutine */
38940
 
  int res = 1;             /* Value returned by sqlite3OsAccess() */
38941
 
  char *zMaster = 0;       /* Name of master journal file if any */
38942
 
  int needPagerReset;      /* True to reset page prior to first page rollback */
38943
 
 
38944
 
  /* Figure out how many records are in the journal.  Abort early if
38945
 
  ** the journal is empty.
38946
 
  */
38947
 
  assert( isOpen(pPager->jfd) );
38948
 
  rc = sqlite3OsFileSize(pPager->jfd, &szJ);
38949
 
  if( rc!=SQLITE_OK ){
38950
 
    goto end_playback;
38951
 
  }
38952
 
 
38953
 
  /* Read the master journal name from the journal, if it is present.
38954
 
  ** If a master journal file name is specified, but the file is not
38955
 
  ** present on disk, then the journal is not hot and does not need to be
38956
 
  ** played back.
38957
 
  **
38958
 
  ** TODO: Technically the following is an error because it assumes that
38959
 
  ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
38960
 
  ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
38961
 
  **  mxPathname is 512, which is the same as the minimum allowable value
38962
 
  ** for pageSize.
38963
 
  */
38964
 
  zMaster = pPager->pTmpSpace;
38965
 
  rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
38966
 
  if( rc==SQLITE_OK && zMaster[0] ){
38967
 
    rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
38968
 
  }
38969
 
  zMaster = 0;
38970
 
  if( rc!=SQLITE_OK || !res ){
38971
 
    goto end_playback;
38972
 
  }
38973
 
  pPager->journalOff = 0;
38974
 
  needPagerReset = isHot;
38975
 
 
38976
 
  /* This loop terminates either when a readJournalHdr() or 
38977
 
  ** pager_playback_one_page() call returns SQLITE_DONE or an IO error 
38978
 
  ** occurs. 
38979
 
  */
38980
 
  for(;;) {
38981
 
    /* Read the next journal header from the journal file.  If there are
38982
 
    ** not enough bytes left in the journal file for a complete header, or
38983
 
    ** it is corrupted, then a process must have failed while writing it.
38984
 
    ** This indicates nothing more needs to be rolled back.
38985
 
    */
38986
 
    rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
38987
 
    if( rc!=SQLITE_OK ){ 
38988
 
      if( rc==SQLITE_DONE ){
38989
 
        rc = SQLITE_OK;
38990
 
      }
38991
 
      goto end_playback;
38992
 
    }
38993
 
 
38994
 
    /* If nRec is 0xffffffff, then this journal was created by a process
38995
 
    ** working in no-sync mode. This means that the rest of the journal
38996
 
    ** file consists of pages, there are no more journal headers. Compute
38997
 
    ** the value of nRec based on this assumption.
38998
 
    */
38999
 
    if( nRec==0xffffffff ){
39000
 
      assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
39001
 
      nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
39002
 
    }
39003
 
 
39004
 
    /* If nRec is 0 and this rollback is of a transaction created by this
39005
 
    ** process and if this is the final header in the journal, then it means
39006
 
    ** that this part of the journal was being filled but has not yet been
39007
 
    ** synced to disk.  Compute the number of pages based on the remaining
39008
 
    ** size of the file.
39009
 
    **
39010
 
    ** The third term of the test was added to fix ticket #2565.
39011
 
    ** When rolling back a hot journal, nRec==0 always means that the next
39012
 
    ** chunk of the journal contains zero pages to be rolled back.  But
39013
 
    ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
39014
 
    ** the journal, it means that the journal might contain additional
39015
 
    ** pages that need to be rolled back and that the number of pages 
39016
 
    ** should be computed based on the journal file size.
39017
 
    */
39018
 
    if( nRec==0 && !isHot &&
39019
 
        pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
39020
 
      nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
39021
 
    }
39022
 
 
39023
 
    /* If this is the first header read from the journal, truncate the
39024
 
    ** database file back to its original size.
39025
 
    */
39026
 
    if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
39027
 
      rc = pager_truncate(pPager, mxPg);
39028
 
      if( rc!=SQLITE_OK ){
39029
 
        goto end_playback;
39030
 
      }
39031
 
      pPager->dbSize = mxPg;
39032
 
    }
39033
 
 
39034
 
    /* Copy original pages out of the journal and back into the 
39035
 
    ** database file and/or page cache.
39036
 
    */
39037
 
    for(u=0; u<nRec; u++){
39038
 
      if( needPagerReset ){
39039
 
        pager_reset(pPager);
39040
 
        needPagerReset = 0;
39041
 
      }
39042
 
      rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
39043
 
      if( rc!=SQLITE_OK ){
39044
 
        if( rc==SQLITE_DONE ){
39045
 
          rc = SQLITE_OK;
39046
 
          pPager->journalOff = szJ;
39047
 
          break;
39048
 
        }else if( rc==SQLITE_IOERR_SHORT_READ ){
39049
 
          /* If the journal has been truncated, simply stop reading and
39050
 
          ** processing the journal. This might happen if the journal was
39051
 
          ** not completely written and synced prior to a crash.  In that
39052
 
          ** case, the database should have never been written in the
39053
 
          ** first place so it is OK to simply abandon the rollback. */
39054
 
          rc = SQLITE_OK;
39055
 
          goto end_playback;
39056
 
        }else{
39057
 
          /* If we are unable to rollback, quit and return the error
39058
 
          ** code.  This will cause the pager to enter the error state
39059
 
          ** so that no further harm will be done.  Perhaps the next
39060
 
          ** process to come along will be able to rollback the database.
39061
 
          */
39062
 
          goto end_playback;
39063
 
        }
39064
 
      }
39065
 
    }
39066
 
  }
39067
 
  /*NOTREACHED*/
39068
 
  assert( 0 );
39069
 
 
39070
 
end_playback:
39071
 
  /* Following a rollback, the database file should be back in its original
39072
 
  ** state prior to the start of the transaction, so invoke the
39073
 
  ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
39074
 
  ** assertion that the transaction counter was modified.
39075
 
  */
39076
 
  assert(
39077
 
    pPager->fd->pMethods==0 ||
39078
 
    sqlite3OsFileControl(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0)>=SQLITE_OK
39079
 
  );
39080
 
 
39081
 
  /* If this playback is happening automatically as a result of an IO or 
39082
 
  ** malloc error that occurred after the change-counter was updated but 
39083
 
  ** before the transaction was committed, then the change-counter 
39084
 
  ** modification may just have been reverted. If this happens in exclusive 
39085
 
  ** mode, then subsequent transactions performed by the connection will not
39086
 
  ** update the change-counter at all. This may lead to cache inconsistency
39087
 
  ** problems for other processes at some point in the future. So, just
39088
 
  ** in case this has happened, clear the changeCountDone flag now.
39089
 
  */
39090
 
  pPager->changeCountDone = pPager->tempFile;
39091
 
 
39092
 
  if( rc==SQLITE_OK ){
39093
 
    zMaster = pPager->pTmpSpace;
39094
 
    rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
39095
 
    testcase( rc!=SQLITE_OK );
39096
 
  }
39097
 
  if( rc==SQLITE_OK
39098
 
   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
39099
 
  ){
39100
 
    rc = sqlite3PagerSync(pPager);
39101
 
  }
39102
 
  if( rc==SQLITE_OK ){
39103
 
    rc = pager_end_transaction(pPager, zMaster[0]!='\0');
39104
 
    testcase( rc!=SQLITE_OK );
39105
 
  }
39106
 
  if( rc==SQLITE_OK && zMaster[0] && res ){
39107
 
    /* If there was a master journal and this routine will return success,
39108
 
    ** see if it is possible to delete the master journal.
39109
 
    */
39110
 
    rc = pager_delmaster(pPager, zMaster);
39111
 
    testcase( rc!=SQLITE_OK );
39112
 
  }
39113
 
 
39114
 
  /* The Pager.sectorSize variable may have been updated while rolling
39115
 
  ** back a journal created by a process with a different sector size
39116
 
  ** value. Reset it to the correct value for this process.
39117
 
  */
39118
 
  setSectorSize(pPager);
39119
 
  return rc;
39120
 
}
39121
 
 
39122
 
 
39123
 
/*
39124
 
** Read the content for page pPg out of the database file and into 
39125
 
** pPg->pData. A shared lock or greater must be held on the database
39126
 
** file before this function is called.
39127
 
**
39128
 
** If page 1 is read, then the value of Pager.dbFileVers[] is set to
39129
 
** the value read from the database file.
39130
 
**
39131
 
** If an IO error occurs, then the IO error is returned to the caller.
39132
 
** Otherwise, SQLITE_OK is returned.
39133
 
*/
39134
 
static int readDbPage(PgHdr *pPg){
39135
 
  Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
39136
 
  Pgno pgno = pPg->pgno;       /* Page number to read */
39137
 
  int rc = SQLITE_OK;          /* Return code */
39138
 
  int isInWal = 0;             /* True if page is in log file */
39139
 
  int pgsz = pPager->pageSize; /* Number of bytes to read */
39140
 
 
39141
 
  assert( pPager->eState>=PAGER_READER && !MEMDB );
39142
 
  assert( isOpen(pPager->fd) );
39143
 
 
39144
 
  if( NEVER(!isOpen(pPager->fd)) ){
39145
 
    assert( pPager->tempFile );
39146
 
    memset(pPg->pData, 0, pPager->pageSize);
39147
 
    return SQLITE_OK;
39148
 
  }
39149
 
 
39150
 
  if( pagerUseWal(pPager) ){
39151
 
    /* Try to pull the page from the write-ahead log. */
39152
 
    rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData);
39153
 
  }
39154
 
  if( rc==SQLITE_OK && !isInWal ){
39155
 
    i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
39156
 
    rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
39157
 
    if( rc==SQLITE_IOERR_SHORT_READ ){
39158
 
      rc = SQLITE_OK;
39159
 
    }
39160
 
  }
39161
 
 
39162
 
  if( pgno==1 ){
39163
 
    if( rc ){
39164
 
      /* If the read is unsuccessful, set the dbFileVers[] to something
39165
 
      ** that will never be a valid file version.  dbFileVers[] is a copy
39166
 
      ** of bytes 24..39 of the database.  Bytes 28..31 should always be
39167
 
      ** zero or the size of the database in page. Bytes 32..35 and 35..39
39168
 
      ** should be page numbers which are never 0xffffffff.  So filling
39169
 
      ** pPager->dbFileVers[] with all 0xff bytes should suffice.
39170
 
      **
39171
 
      ** For an encrypted database, the situation is more complex:  bytes
39172
 
      ** 24..39 of the database are white noise.  But the probability of
39173
 
      ** white noising equaling 16 bytes of 0xff is vanishingly small so
39174
 
      ** we should still be ok.
39175
 
      */
39176
 
      memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
39177
 
    }else{
39178
 
      u8 *dbFileVers = &((u8*)pPg->pData)[24];
39179
 
      memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
39180
 
    }
39181
 
  }
39182
 
  CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
39183
 
 
39184
 
  PAGER_INCR(sqlite3_pager_readdb_count);
39185
 
  PAGER_INCR(pPager->nRead);
39186
 
  IOTRACE(("PGIN %p %d\n", pPager, pgno));
39187
 
  PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
39188
 
               PAGERID(pPager), pgno, pager_pagehash(pPg)));
39189
 
 
39190
 
  return rc;
39191
 
}
39192
 
 
39193
 
/*
39194
 
** Update the value of the change-counter at offsets 24 and 92 in
39195
 
** the header and the sqlite version number at offset 96.
39196
 
**
39197
 
** This is an unconditional update.  See also the pager_incr_changecounter()
39198
 
** routine which only updates the change-counter if the update is actually
39199
 
** needed, as determined by the pPager->changeCountDone state variable.
39200
 
*/
39201
 
static void pager_write_changecounter(PgHdr *pPg){
39202
 
  u32 change_counter;
39203
 
 
39204
 
  /* Increment the value just read and write it back to byte 24. */
39205
 
  change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
39206
 
  put32bits(((char*)pPg->pData)+24, change_counter);
39207
 
 
39208
 
  /* Also store the SQLite version number in bytes 96..99 and in
39209
 
  ** bytes 92..95 store the change counter for which the version number
39210
 
  ** is valid. */
39211
 
  put32bits(((char*)pPg->pData)+92, change_counter);
39212
 
  put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
39213
 
}
39214
 
 
39215
 
#ifndef SQLITE_OMIT_WAL
39216
 
/*
39217
 
** This function is invoked once for each page that has already been 
39218
 
** written into the log file when a WAL transaction is rolled back.
39219
 
** Parameter iPg is the page number of said page. The pCtx argument 
39220
 
** is actually a pointer to the Pager structure.
39221
 
**
39222
 
** If page iPg is present in the cache, and has no outstanding references,
39223
 
** it is discarded. Otherwise, if there are one or more outstanding
39224
 
** references, the page content is reloaded from the database. If the
39225
 
** attempt to reload content from the database is required and fails, 
39226
 
** return an SQLite error code. Otherwise, SQLITE_OK.
39227
 
*/
39228
 
static int pagerUndoCallback(void *pCtx, Pgno iPg){
39229
 
  int rc = SQLITE_OK;
39230
 
  Pager *pPager = (Pager *)pCtx;
39231
 
  PgHdr *pPg;
39232
 
 
39233
 
  pPg = sqlite3PagerLookup(pPager, iPg);
39234
 
  if( pPg ){
39235
 
    if( sqlite3PcachePageRefcount(pPg)==1 ){
39236
 
      sqlite3PcacheDrop(pPg);
39237
 
    }else{
39238
 
      rc = readDbPage(pPg);
39239
 
      if( rc==SQLITE_OK ){
39240
 
        pPager->xReiniter(pPg);
39241
 
      }
39242
 
      sqlite3PagerUnref(pPg);
39243
 
    }
39244
 
  }
39245
 
 
39246
 
  /* Normally, if a transaction is rolled back, any backup processes are
39247
 
  ** updated as data is copied out of the rollback journal and into the
39248
 
  ** database. This is not generally possible with a WAL database, as
39249
 
  ** rollback involves simply truncating the log file. Therefore, if one
39250
 
  ** or more frames have already been written to the log (and therefore 
39251
 
  ** also copied into the backup databases) as part of this transaction,
39252
 
  ** the backups must be restarted.
39253
 
  */
39254
 
  sqlite3BackupRestart(pPager->pBackup);
39255
 
 
39256
 
  return rc;
39257
 
}
39258
 
 
39259
 
/*
39260
 
** This function is called to rollback a transaction on a WAL database.
39261
 
*/
39262
 
static int pagerRollbackWal(Pager *pPager){
39263
 
  int rc;                         /* Return Code */
39264
 
  PgHdr *pList;                   /* List of dirty pages to revert */
39265
 
 
39266
 
  /* For all pages in the cache that are currently dirty or have already
39267
 
  ** been written (but not committed) to the log file, do one of the 
39268
 
  ** following:
39269
 
  **
39270
 
  **   + Discard the cached page (if refcount==0), or
39271
 
  **   + Reload page content from the database (if refcount>0).
39272
 
  */
39273
 
  pPager->dbSize = pPager->dbOrigSize;
39274
 
  rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
39275
 
  pList = sqlite3PcacheDirtyList(pPager->pPCache);
39276
 
  while( pList && rc==SQLITE_OK ){
39277
 
    PgHdr *pNext = pList->pDirty;
39278
 
    rc = pagerUndoCallback((void *)pPager, pList->pgno);
39279
 
    pList = pNext;
39280
 
  }
39281
 
 
39282
 
  return rc;
39283
 
}
39284
 
 
39285
 
/*
39286
 
** This function is a wrapper around sqlite3WalFrames(). As well as logging
39287
 
** the contents of the list of pages headed by pList (connected by pDirty),
39288
 
** this function notifies any active backup processes that the pages have
39289
 
** changed. 
39290
 
**
39291
 
** The list of pages passed into this routine is always sorted by page number.
39292
 
** Hence, if page 1 appears anywhere on the list, it will be the first page.
39293
 
*/ 
39294
 
static int pagerWalFrames(
39295
 
  Pager *pPager,                  /* Pager object */
39296
 
  PgHdr *pList,                   /* List of frames to log */
39297
 
  Pgno nTruncate,                 /* Database size after this commit */
39298
 
  int isCommit,                   /* True if this is a commit */
39299
 
  int syncFlags                   /* Flags to pass to OsSync() (or 0) */
39300
 
){
39301
 
  int rc;                         /* Return code */
39302
 
#if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
39303
 
  PgHdr *p;                       /* For looping over pages */
39304
 
#endif
39305
 
 
39306
 
  assert( pPager->pWal );
39307
 
#ifdef SQLITE_DEBUG
39308
 
  /* Verify that the page list is in accending order */
39309
 
  for(p=pList; p && p->pDirty; p=p->pDirty){
39310
 
    assert( p->pgno < p->pDirty->pgno );
39311
 
  }
39312
 
#endif
39313
 
 
39314
 
  if( isCommit ){
39315
 
    /* If a WAL transaction is being committed, there is no point in writing
39316
 
    ** any pages with page numbers greater than nTruncate into the WAL file.
39317
 
    ** They will never be read by any client. So remove them from the pDirty
39318
 
    ** list here. */
39319
 
    PgHdr *p;
39320
 
    PgHdr **ppNext = &pList;
39321
 
    for(p=pList; (*ppNext = p) != NULL; p=p->pDirty){
39322
 
      if( p->pgno<=nTruncate ) ppNext = &p->pDirty;
39323
 
    }
39324
 
    assert( pList );
39325
 
  }
39326
 
 
39327
 
  if( pList->pgno==1 ) pager_write_changecounter(pList);
39328
 
  rc = sqlite3WalFrames(pPager->pWal, 
39329
 
      pPager->pageSize, pList, nTruncate, isCommit, syncFlags
39330
 
  );
39331
 
  if( rc==SQLITE_OK && pPager->pBackup ){
39332
 
    PgHdr *p;
39333
 
    for(p=pList; p; p=p->pDirty){
39334
 
      sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
39335
 
    }
39336
 
  }
39337
 
 
39338
 
#ifdef SQLITE_CHECK_PAGES
39339
 
  pList = sqlite3PcacheDirtyList(pPager->pPCache);
39340
 
  for(p=pList; p; p=p->pDirty){
39341
 
    pager_set_pagehash(p);
39342
 
  }
39343
 
#endif
39344
 
 
39345
 
  return rc;
39346
 
}
39347
 
 
39348
 
/*
39349
 
** Begin a read transaction on the WAL.
39350
 
**
39351
 
** This routine used to be called "pagerOpenSnapshot()" because it essentially
39352
 
** makes a snapshot of the database at the current point in time and preserves
39353
 
** that snapshot for use by the reader in spite of concurrently changes by
39354
 
** other writers or checkpointers.
39355
 
*/
39356
 
static int pagerBeginReadTransaction(Pager *pPager){
39357
 
  int rc;                         /* Return code */
39358
 
  int changed = 0;                /* True if cache must be reset */
39359
 
 
39360
 
  assert( pagerUseWal(pPager) );
39361
 
  assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
39362
 
 
39363
 
  /* sqlite3WalEndReadTransaction() was not called for the previous
39364
 
  ** transaction in locking_mode=EXCLUSIVE.  So call it now.  If we
39365
 
  ** are in locking_mode=NORMAL and EndRead() was previously called,
39366
 
  ** the duplicate call is harmless.
39367
 
  */
39368
 
  sqlite3WalEndReadTransaction(pPager->pWal);
39369
 
 
39370
 
  rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
39371
 
  if( rc!=SQLITE_OK || changed ){
39372
 
    pager_reset(pPager);
39373
 
  }
39374
 
 
39375
 
  return rc;
39376
 
}
39377
 
#endif
39378
 
 
39379
 
/*
39380
 
** This function is called as part of the transition from PAGER_OPEN
39381
 
** to PAGER_READER state to determine the size of the database file
39382
 
** in pages (assuming the page size currently stored in Pager.pageSize).
39383
 
**
39384
 
** If no error occurs, SQLITE_OK is returned and the size of the database
39385
 
** in pages is stored in *pnPage. Otherwise, an error code (perhaps
39386
 
** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
39387
 
*/
39388
 
static int pagerPagecount(Pager *pPager, Pgno *pnPage){
39389
 
  Pgno nPage;                     /* Value to return via *pnPage */
39390
 
 
39391
 
  /* Query the WAL sub-system for the database size. The WalDbsize()
39392
 
  ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
39393
 
  ** if the database size is not available. The database size is not
39394
 
  ** available from the WAL sub-system if the log file is empty or
39395
 
  ** contains no valid committed transactions.
39396
 
  */
39397
 
  assert( pPager->eState==PAGER_OPEN );
39398
 
  assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
39399
 
  nPage = sqlite3WalDbsize(pPager->pWal);
39400
 
 
39401
 
  /* If the database size was not available from the WAL sub-system,
39402
 
  ** determine it based on the size of the database file. If the size
39403
 
  ** of the database file is not an integer multiple of the page-size,
39404
 
  ** round down to the nearest page. Except, any file larger than 0
39405
 
  ** bytes in size is considered to contain at least one page.
39406
 
  */
39407
 
  if( nPage==0 ){
39408
 
    i64 n = 0;                    /* Size of db file in bytes */
39409
 
    assert( isOpen(pPager->fd) || pPager->tempFile );
39410
 
    if( isOpen(pPager->fd) ){
39411
 
      int rc = sqlite3OsFileSize(pPager->fd, &n);
39412
 
      if( rc!=SQLITE_OK ){
39413
 
        return rc;
39414
 
      }
39415
 
    }
39416
 
    nPage = (Pgno)(n / pPager->pageSize);
39417
 
    if( nPage==0 && n>0 ){
39418
 
      nPage = 1;
39419
 
    }
39420
 
  }
39421
 
 
39422
 
  /* If the current number of pages in the file is greater than the
39423
 
  ** configured maximum pager number, increase the allowed limit so
39424
 
  ** that the file can be read.
39425
 
  */
39426
 
  if( nPage>pPager->mxPgno ){
39427
 
    pPager->mxPgno = (Pgno)nPage;
39428
 
  }
39429
 
 
39430
 
  *pnPage = nPage;
39431
 
  return SQLITE_OK;
39432
 
}
39433
 
 
39434
 
#ifndef SQLITE_OMIT_WAL
39435
 
/*
39436
 
** Check if the *-wal file that corresponds to the database opened by pPager
39437
 
** exists if the database is not empy, or verify that the *-wal file does
39438
 
** not exist (by deleting it) if the database file is empty.
39439
 
**
39440
 
** If the database is not empty and the *-wal file exists, open the pager
39441
 
** in WAL mode.  If the database is empty or if no *-wal file exists and
39442
 
** if no error occurs, make sure Pager.journalMode is not set to
39443
 
** PAGER_JOURNALMODE_WAL.
39444
 
**
39445
 
** Return SQLITE_OK or an error code.
39446
 
**
39447
 
** The caller must hold a SHARED lock on the database file to call this
39448
 
** function. Because an EXCLUSIVE lock on the db file is required to delete 
39449
 
** a WAL on a none-empty database, this ensures there is no race condition 
39450
 
** between the xAccess() below and an xDelete() being executed by some 
39451
 
** other connection.
39452
 
*/
39453
 
static int pagerOpenWalIfPresent(Pager *pPager){
39454
 
  int rc = SQLITE_OK;
39455
 
  assert( pPager->eState==PAGER_OPEN );
39456
 
  assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
39457
 
 
39458
 
  if( !pPager->tempFile ){
39459
 
    int isWal;                    /* True if WAL file exists */
39460
 
    Pgno nPage;                   /* Size of the database file */
39461
 
 
39462
 
    rc = pagerPagecount(pPager, &nPage);
39463
 
    if( rc ) return rc;
39464
 
    if( nPage==0 ){
39465
 
      rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
39466
 
      isWal = 0;
39467
 
    }else{
39468
 
      rc = sqlite3OsAccess(
39469
 
          pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
39470
 
      );
39471
 
    }
39472
 
    if( rc==SQLITE_OK ){
39473
 
      if( isWal ){
39474
 
        testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
39475
 
        rc = sqlite3PagerOpenWal(pPager, 0);
39476
 
      }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
39477
 
        pPager->journalMode = PAGER_JOURNALMODE_DELETE;
39478
 
      }
39479
 
    }
39480
 
  }
39481
 
  return rc;
39482
 
}
39483
 
#endif
39484
 
 
39485
 
/*
39486
 
** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
39487
 
** the entire master journal file. The case pSavepoint==NULL occurs when 
39488
 
** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction 
39489
 
** savepoint.
39490
 
**
39491
 
** When pSavepoint is not NULL (meaning a non-transaction savepoint is 
39492
 
** being rolled back), then the rollback consists of up to three stages,
39493
 
** performed in the order specified:
39494
 
**
39495
 
**   * Pages are played back from the main journal starting at byte
39496
 
**     offset PagerSavepoint.iOffset and continuing to 
39497
 
**     PagerSavepoint.iHdrOffset, or to the end of the main journal
39498
 
**     file if PagerSavepoint.iHdrOffset is zero.
39499
 
**
39500
 
**   * If PagerSavepoint.iHdrOffset is not zero, then pages are played
39501
 
**     back starting from the journal header immediately following 
39502
 
**     PagerSavepoint.iHdrOffset to the end of the main journal file.
39503
 
**
39504
 
**   * Pages are then played back from the sub-journal file, starting
39505
 
**     with the PagerSavepoint.iSubRec and continuing to the end of
39506
 
**     the journal file.
39507
 
**
39508
 
** Throughout the rollback process, each time a page is rolled back, the
39509
 
** corresponding bit is set in a bitvec structure (variable pDone in the
39510
 
** implementation below). This is used to ensure that a page is only
39511
 
** rolled back the first time it is encountered in either journal.
39512
 
**
39513
 
** If pSavepoint is NULL, then pages are only played back from the main
39514
 
** journal file. There is no need for a bitvec in this case.
39515
 
**
39516
 
** In either case, before playback commences the Pager.dbSize variable
39517
 
** is reset to the value that it held at the start of the savepoint 
39518
 
** (or transaction). No page with a page-number greater than this value
39519
 
** is played back. If one is encountered it is simply skipped.
39520
 
*/
39521
 
static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
39522
 
  i64 szJ;                 /* Effective size of the main journal */
39523
 
  i64 iHdrOff;             /* End of first segment of main-journal records */
39524
 
  int rc = SQLITE_OK;      /* Return code */
39525
 
  Bitvec *pDone = 0;       /* Bitvec to ensure pages played back only once */
39526
 
 
39527
 
  assert( pPager->eState!=PAGER_ERROR );
39528
 
  assert( pPager->eState>=PAGER_WRITER_LOCKED );
39529
 
 
39530
 
  /* Allocate a bitvec to use to store the set of pages rolled back */
39531
 
  if( pSavepoint ){
39532
 
    pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
39533
 
    if( !pDone ){
39534
 
      return SQLITE_NOMEM;
39535
 
    }
39536
 
  }
39537
 
 
39538
 
  /* Set the database size back to the value it was before the savepoint 
39539
 
  ** being reverted was opened.
39540
 
  */
39541
 
  pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
39542
 
  pPager->changeCountDone = pPager->tempFile;
39543
 
 
39544
 
  if( !pSavepoint && pagerUseWal(pPager) ){
39545
 
    return pagerRollbackWal(pPager);
39546
 
  }
39547
 
 
39548
 
  /* Use pPager->journalOff as the effective size of the main rollback
39549
 
  ** journal.  The actual file might be larger than this in
39550
 
  ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST.  But anything
39551
 
  ** past pPager->journalOff is off-limits to us.
39552
 
  */
39553
 
  szJ = pPager->journalOff;
39554
 
  assert( pagerUseWal(pPager)==0 || szJ==0 );
39555
 
 
39556
 
  /* Begin by rolling back records from the main journal starting at
39557
 
  ** PagerSavepoint.iOffset and continuing to the next journal header.
39558
 
  ** There might be records in the main journal that have a page number
39559
 
  ** greater than the current database size (pPager->dbSize) but those
39560
 
  ** will be skipped automatically.  Pages are added to pDone as they
39561
 
  ** are played back.
39562
 
  */
39563
 
  if( pSavepoint && !pagerUseWal(pPager) ){
39564
 
    iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
39565
 
    pPager->journalOff = pSavepoint->iOffset;
39566
 
    while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
39567
 
      rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
39568
 
    }
39569
 
    assert( rc!=SQLITE_DONE );
39570
 
  }else{
39571
 
    pPager->journalOff = 0;
39572
 
  }
39573
 
 
39574
 
  /* Continue rolling back records out of the main journal starting at
39575
 
  ** the first journal header seen and continuing until the effective end
39576
 
  ** of the main journal file.  Continue to skip out-of-range pages and
39577
 
  ** continue adding pages rolled back to pDone.
39578
 
  */
39579
 
  while( rc==SQLITE_OK && pPager->journalOff<szJ ){
39580
 
    u32 ii;            /* Loop counter */
39581
 
    u32 nJRec = 0;     /* Number of Journal Records */
39582
 
    u32 dummy;
39583
 
    rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
39584
 
    assert( rc!=SQLITE_DONE );
39585
 
 
39586
 
    /*
39587
 
    ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
39588
 
    ** test is related to ticket #2565.  See the discussion in the
39589
 
    ** pager_playback() function for additional information.
39590
 
    */
39591
 
    if( nJRec==0 
39592
 
     && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
39593
 
    ){
39594
 
      nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
39595
 
    }
39596
 
    for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
39597
 
      rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
39598
 
    }
39599
 
    assert( rc!=SQLITE_DONE );
39600
 
  }
39601
 
  assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
39602
 
 
39603
 
  /* Finally,  rollback pages from the sub-journal.  Page that were
39604
 
  ** previously rolled back out of the main journal (and are hence in pDone)
39605
 
  ** will be skipped.  Out-of-range pages are also skipped.
39606
 
  */
39607
 
  if( pSavepoint ){
39608
 
    u32 ii;            /* Loop counter */
39609
 
    i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
39610
 
 
39611
 
    if( pagerUseWal(pPager) ){
39612
 
      rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
39613
 
    }
39614
 
    for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
39615
 
      assert( offset==ii*(4+pPager->pageSize) );
39616
 
      rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
39617
 
    }
39618
 
    assert( rc!=SQLITE_DONE );
39619
 
  }
39620
 
 
39621
 
  sqlite3BitvecDestroy(pDone);
39622
 
  if( rc==SQLITE_OK ){
39623
 
    pPager->journalOff = szJ;
39624
 
  }
39625
 
 
39626
 
  return rc;
39627
 
}
39628
 
 
39629
 
/*
39630
 
** Change the maximum number of in-memory pages that are allowed.
39631
 
*/
39632
 
SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
39633
 
  sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
39634
 
}
39635
 
 
39636
 
/*
39637
 
** Adjust the robustness of the database to damage due to OS crashes
39638
 
** or power failures by changing the number of syncs()s when writing
39639
 
** the rollback journal.  There are three levels:
39640
 
**
39641
 
**    OFF       sqlite3OsSync() is never called.  This is the default
39642
 
**              for temporary and transient files.
39643
 
**
39644
 
**    NORMAL    The journal is synced once before writes begin on the
39645
 
**              database.  This is normally adequate protection, but
39646
 
**              it is theoretically possible, though very unlikely,
39647
 
**              that an inopertune power failure could leave the journal
39648
 
**              in a state which would cause damage to the database
39649
 
**              when it is rolled back.
39650
 
**
39651
 
**    FULL      The journal is synced twice before writes begin on the
39652
 
**              database (with some additional information - the nRec field
39653
 
**              of the journal header - being written in between the two
39654
 
**              syncs).  If we assume that writing a
39655
 
**              single disk sector is atomic, then this mode provides
39656
 
**              assurance that the journal will not be corrupted to the
39657
 
**              point of causing damage to the database during rollback.
39658
 
**
39659
 
** The above is for a rollback-journal mode.  For WAL mode, OFF continues
39660
 
** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
39661
 
** prior to the start of checkpoint and that the database file is synced
39662
 
** at the conclusion of the checkpoint if the entire content of the WAL
39663
 
** was written back into the database.  But no sync operations occur for
39664
 
** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
39665
 
** file is synced following each commit operation, in addition to the
39666
 
** syncs associated with NORMAL.
39667
 
**
39668
 
** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
39669
 
** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
39670
 
** using fcntl(F_FULLFSYNC).  SQLITE_SYNC_NORMAL means to do an
39671
 
** ordinary fsync() call.  There is no difference between SQLITE_SYNC_FULL
39672
 
** and SQLITE_SYNC_NORMAL on platforms other than MacOSX.  But the
39673
 
** synchronous=FULL versus synchronous=NORMAL setting determines when
39674
 
** the xSync primitive is called and is relevant to all platforms.
39675
 
**
39676
 
** Numeric values associated with these states are OFF==1, NORMAL=2,
39677
 
** and FULL=3.
39678
 
*/
39679
 
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
39680
 
SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(
39681
 
  Pager *pPager,        /* The pager to set safety level for */
39682
 
  int level,            /* PRAGMA synchronous.  1=OFF, 2=NORMAL, 3=FULL */  
39683
 
  int bFullFsync,       /* PRAGMA fullfsync */
39684
 
  int bCkptFullFsync    /* PRAGMA checkpoint_fullfsync */
39685
 
){
39686
 
  assert( level>=1 && level<=3 );
39687
 
  pPager->noSync =  (level==1 || pPager->tempFile) ?1:0;
39688
 
  pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
39689
 
  if( pPager->noSync ){
39690
 
    pPager->syncFlags = 0;
39691
 
    pPager->ckptSyncFlags = 0;
39692
 
  }else if( bFullFsync ){
39693
 
    pPager->syncFlags = SQLITE_SYNC_FULL;
39694
 
    pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
39695
 
  }else if( bCkptFullFsync ){
39696
 
    pPager->syncFlags = SQLITE_SYNC_NORMAL;
39697
 
    pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
39698
 
  }else{
39699
 
    pPager->syncFlags = SQLITE_SYNC_NORMAL;
39700
 
    pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
39701
 
  }
39702
 
}
39703
 
#endif
39704
 
 
39705
 
/*
39706
 
** The following global variable is incremented whenever the library
39707
 
** attempts to open a temporary file.  This information is used for
39708
 
** testing and analysis only.  
39709
 
*/
39710
 
#ifdef SQLITE_TEST
39711
 
SQLITE_API int sqlite3_opentemp_count = 0;
39712
 
#endif
39713
 
 
39714
 
/*
39715
 
** Open a temporary file.
39716
 
**
39717
 
** Write the file descriptor into *pFile. Return SQLITE_OK on success 
39718
 
** or some other error code if we fail. The OS will automatically 
39719
 
** delete the temporary file when it is closed.
39720
 
**
39721
 
** The flags passed to the VFS layer xOpen() call are those specified
39722
 
** by parameter vfsFlags ORed with the following:
39723
 
**
39724
 
**     SQLITE_OPEN_READWRITE
39725
 
**     SQLITE_OPEN_CREATE
39726
 
**     SQLITE_OPEN_EXCLUSIVE
39727
 
**     SQLITE_OPEN_DELETEONCLOSE
39728
 
*/
39729
 
static int pagerOpentemp(
39730
 
  Pager *pPager,        /* The pager object */
39731
 
  sqlite3_file *pFile,  /* Write the file descriptor here */
39732
 
  int vfsFlags          /* Flags passed through to the VFS */
39733
 
){
39734
 
  int rc;               /* Return code */
39735
 
 
39736
 
#ifdef SQLITE_TEST
39737
 
  sqlite3_opentemp_count++;  /* Used for testing and analysis only */
39738
 
#endif
39739
 
 
39740
 
  vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
39741
 
            SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
39742
 
  rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
39743
 
  assert( rc!=SQLITE_OK || isOpen(pFile) );
39744
 
  return rc;
39745
 
}
39746
 
 
39747
 
/*
39748
 
** Set the busy handler function.
39749
 
**
39750
 
** The pager invokes the busy-handler if sqlite3OsLock() returns 
39751
 
** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
39752
 
** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE 
39753
 
** lock. It does *not* invoke the busy handler when upgrading from
39754
 
** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
39755
 
** (which occurs during hot-journal rollback). Summary:
39756
 
**
39757
 
**   Transition                        | Invokes xBusyHandler
39758
 
**   --------------------------------------------------------
39759
 
**   NO_LOCK       -> SHARED_LOCK      | Yes
39760
 
**   SHARED_LOCK   -> RESERVED_LOCK    | No
39761
 
**   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
39762
 
**   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
39763
 
**
39764
 
** If the busy-handler callback returns non-zero, the lock is 
39765
 
** retried. If it returns zero, then the SQLITE_BUSY error is
39766
 
** returned to the caller of the pager API function.
39767
 
*/
39768
 
SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
39769
 
  Pager *pPager,                       /* Pager object */
39770
 
  int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
39771
 
  void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
39772
 
){  
39773
 
  pPager->xBusyHandler = xBusyHandler;
39774
 
  pPager->pBusyHandlerArg = pBusyHandlerArg;
39775
 
}
39776
 
 
39777
 
/*
39778
 
** Change the page size used by the Pager object. The new page size 
39779
 
** is passed in *pPageSize.
39780
 
**
39781
 
** If the pager is in the error state when this function is called, it
39782
 
** is a no-op. The value returned is the error state error code (i.e. 
39783
 
** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
39784
 
**
39785
 
** Otherwise, if all of the following are true:
39786
 
**
39787
 
**   * the new page size (value of *pPageSize) is valid (a power 
39788
 
**     of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
39789
 
**
39790
 
**   * there are no outstanding page references, and
39791
 
**
39792
 
**   * the database is either not an in-memory database or it is
39793
 
**     an in-memory database that currently consists of zero pages.
39794
 
**
39795
 
** then the pager object page size is set to *pPageSize.
39796
 
**
39797
 
** If the page size is changed, then this function uses sqlite3PagerMalloc() 
39798
 
** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt 
39799
 
** fails, SQLITE_NOMEM is returned and the page size remains unchanged. 
39800
 
** In all other cases, SQLITE_OK is returned.
39801
 
**
39802
 
** If the page size is not changed, either because one of the enumerated
39803
 
** conditions above is not true, the pager was in error state when this
39804
 
** function was called, or because the memory allocation attempt failed, 
39805
 
** then *pPageSize is set to the old, retained page size before returning.
39806
 
*/
39807
 
SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
39808
 
  int rc = SQLITE_OK;
39809
 
 
39810
 
  /* It is not possible to do a full assert_pager_state() here, as this
39811
 
  ** function may be called from within PagerOpen(), before the state
39812
 
  ** of the Pager object is internally consistent.
39813
 
  **
39814
 
  ** At one point this function returned an error if the pager was in 
39815
 
  ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
39816
 
  ** there is at least one outstanding page reference, this function
39817
 
  ** is a no-op for that case anyhow.
39818
 
  */
39819
 
 
39820
 
  u32 pageSize = *pPageSize;
39821
 
  assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
39822
 
  if( (pPager->memDb==0 || pPager->dbSize==0)
39823
 
   && sqlite3PcacheRefCount(pPager->pPCache)==0 
39824
 
   && pageSize && pageSize!=(u32)pPager->pageSize 
39825
 
  ){
39826
 
    char *pNew = NULL;             /* New temp space */
39827
 
    i64 nByte = 0;
39828
 
 
39829
 
    if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
39830
 
      rc = sqlite3OsFileSize(pPager->fd, &nByte);
39831
 
    }
39832
 
    if( rc==SQLITE_OK ){
39833
 
      pNew = (char *)sqlite3PageMalloc(pageSize);
39834
 
      if( !pNew ) rc = SQLITE_NOMEM;
39835
 
    }
39836
 
 
39837
 
    if( rc==SQLITE_OK ){
39838
 
      pager_reset(pPager);
39839
 
      pPager->dbSize = (Pgno)(nByte/pageSize);
39840
 
      pPager->pageSize = pageSize;
39841
 
      sqlite3PageFree(pPager->pTmpSpace);
39842
 
      pPager->pTmpSpace = pNew;
39843
 
      sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
39844
 
    }
39845
 
  }
39846
 
 
39847
 
  *pPageSize = pPager->pageSize;
39848
 
  if( rc==SQLITE_OK ){
39849
 
    if( nReserve<0 ) nReserve = pPager->nReserve;
39850
 
    assert( nReserve>=0 && nReserve<1000 );
39851
 
    pPager->nReserve = (i16)nReserve;
39852
 
    pagerReportSize(pPager);
39853
 
  }
39854
 
  return rc;
39855
 
}
39856
 
 
39857
 
/*
39858
 
** Return a pointer to the "temporary page" buffer held internally
39859
 
** by the pager.  This is a buffer that is big enough to hold the
39860
 
** entire content of a database page.  This buffer is used internally
39861
 
** during rollback and will be overwritten whenever a rollback
39862
 
** occurs.  But other modules are free to use it too, as long as
39863
 
** no rollbacks are happening.
39864
 
*/
39865
 
SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
39866
 
  return pPager->pTmpSpace;
39867
 
}
39868
 
 
39869
 
/*
39870
 
** Attempt to set the maximum database page count if mxPage is positive. 
39871
 
** Make no changes if mxPage is zero or negative.  And never reduce the
39872
 
** maximum page count below the current size of the database.
39873
 
**
39874
 
** Regardless of mxPage, return the current maximum page count.
39875
 
*/
39876
 
SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
39877
 
  if( mxPage>0 ){
39878
 
    pPager->mxPgno = mxPage;
39879
 
  }
39880
 
  assert( pPager->eState!=PAGER_OPEN );      /* Called only by OP_MaxPgcnt */
39881
 
  assert( pPager->mxPgno>=pPager->dbSize );  /* OP_MaxPgcnt enforces this */
39882
 
  return pPager->mxPgno;
39883
 
}
39884
 
 
39885
 
/*
39886
 
** The following set of routines are used to disable the simulated
39887
 
** I/O error mechanism.  These routines are used to avoid simulated
39888
 
** errors in places where we do not care about errors.
39889
 
**
39890
 
** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
39891
 
** and generate no code.
39892
 
*/
39893
 
#ifdef SQLITE_TEST
39894
 
SQLITE_API extern int sqlite3_io_error_pending;
39895
 
SQLITE_API extern int sqlite3_io_error_hit;
39896
 
static int saved_cnt;
39897
 
void disable_simulated_io_errors(void){
39898
 
  saved_cnt = sqlite3_io_error_pending;
39899
 
  sqlite3_io_error_pending = -1;
39900
 
}
39901
 
void enable_simulated_io_errors(void){
39902
 
  sqlite3_io_error_pending = saved_cnt;
39903
 
}
39904
 
#else
39905
 
# define disable_simulated_io_errors()
39906
 
# define enable_simulated_io_errors()
39907
 
#endif
39908
 
 
39909
 
/*
39910
 
** Read the first N bytes from the beginning of the file into memory
39911
 
** that pDest points to. 
39912
 
**
39913
 
** If the pager was opened on a transient file (zFilename==""), or
39914
 
** opened on a file less than N bytes in size, the output buffer is
39915
 
** zeroed and SQLITE_OK returned. The rationale for this is that this 
39916
 
** function is used to read database headers, and a new transient or
39917
 
** zero sized database has a header than consists entirely of zeroes.
39918
 
**
39919
 
** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
39920
 
** the error code is returned to the caller and the contents of the
39921
 
** output buffer undefined.
39922
 
*/
39923
 
SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
39924
 
  int rc = SQLITE_OK;
39925
 
  memset(pDest, 0, N);
39926
 
  assert( isOpen(pPager->fd) || pPager->tempFile );
39927
 
 
39928
 
  /* This routine is only called by btree immediately after creating
39929
 
  ** the Pager object.  There has not been an opportunity to transition
39930
 
  ** to WAL mode yet.
39931
 
  */
39932
 
  assert( !pagerUseWal(pPager) );
39933
 
 
39934
 
  if( isOpen(pPager->fd) ){
39935
 
    IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
39936
 
    rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
39937
 
    if( rc==SQLITE_IOERR_SHORT_READ ){
39938
 
      rc = SQLITE_OK;
39939
 
    }
39940
 
  }
39941
 
  return rc;
39942
 
}
39943
 
 
39944
 
/*
39945
 
** This function may only be called when a read-transaction is open on
39946
 
** the pager. It returns the total number of pages in the database.
39947
 
**
39948
 
** However, if the file is between 1 and <page-size> bytes in size, then 
39949
 
** this is considered a 1 page file.
39950
 
*/
39951
 
SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
39952
 
  assert( pPager->eState>=PAGER_READER );
39953
 
  assert( pPager->eState!=PAGER_WRITER_FINISHED );
39954
 
  *pnPage = (int)pPager->dbSize;
39955
 
}
39956
 
 
39957
 
 
39958
 
/*
39959
 
** Try to obtain a lock of type locktype on the database file. If
39960
 
** a similar or greater lock is already held, this function is a no-op
39961
 
** (returning SQLITE_OK immediately).
39962
 
**
39963
 
** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke 
39964
 
** the busy callback if the lock is currently not available. Repeat 
39965
 
** until the busy callback returns false or until the attempt to 
39966
 
** obtain the lock succeeds.
39967
 
**
39968
 
** Return SQLITE_OK on success and an error code if we cannot obtain
39969
 
** the lock. If the lock is obtained successfully, set the Pager.state 
39970
 
** variable to locktype before returning.
39971
 
*/
39972
 
static int pager_wait_on_lock(Pager *pPager, int locktype){
39973
 
  int rc;                              /* Return code */
39974
 
 
39975
 
  /* Check that this is either a no-op (because the requested lock is 
39976
 
  ** already held, or one of the transistions that the busy-handler
39977
 
  ** may be invoked during, according to the comment above
39978
 
  ** sqlite3PagerSetBusyhandler().
39979
 
  */
39980
 
  assert( (pPager->eLock>=locktype)
39981
 
       || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
39982
 
       || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
39983
 
  );
39984
 
 
39985
 
  do {
39986
 
    rc = pagerLockDb(pPager, locktype);
39987
 
  }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
39988
 
  return rc;
39989
 
}
39990
 
 
39991
 
/*
39992
 
** Function assertTruncateConstraint(pPager) checks that one of the 
39993
 
** following is true for all dirty pages currently in the page-cache:
39994
 
**
39995
 
**   a) The page number is less than or equal to the size of the 
39996
 
**      current database image, in pages, OR
39997
 
**
39998
 
**   b) if the page content were written at this time, it would not
39999
 
**      be necessary to write the current content out to the sub-journal
40000
 
**      (as determined by function subjRequiresPage()).
40001
 
**
40002
 
** If the condition asserted by this function were not true, and the
40003
 
** dirty page were to be discarded from the cache via the pagerStress()
40004
 
** routine, pagerStress() would not write the current page content to
40005
 
** the database file. If a savepoint transaction were rolled back after
40006
 
** this happened, the correct behaviour would be to restore the current
40007
 
** content of the page. However, since this content is not present in either
40008
 
** the database file or the portion of the rollback journal and 
40009
 
** sub-journal rolled back the content could not be restored and the
40010
 
** database image would become corrupt. It is therefore fortunate that 
40011
 
** this circumstance cannot arise.
40012
 
*/
40013
 
#if defined(SQLITE_DEBUG)
40014
 
static void assertTruncateConstraintCb(PgHdr *pPg){
40015
 
  assert( pPg->flags&PGHDR_DIRTY );
40016
 
  assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
40017
 
}
40018
 
static void assertTruncateConstraint(Pager *pPager){
40019
 
  sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
40020
 
}
40021
 
#else
40022
 
# define assertTruncateConstraint(pPager)
40023
 
#endif
40024
 
 
40025
 
/*
40026
 
** Truncate the in-memory database file image to nPage pages. This 
40027
 
** function does not actually modify the database file on disk. It 
40028
 
** just sets the internal state of the pager object so that the 
40029
 
** truncation will be done when the current transaction is committed.
40030
 
*/
40031
 
SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
40032
 
  assert( pPager->dbSize>=nPage );
40033
 
  assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
40034
 
  pPager->dbSize = nPage;
40035
 
  assertTruncateConstraint(pPager);
40036
 
}
40037
 
 
40038
 
 
40039
 
/*
40040
 
** This function is called before attempting a hot-journal rollback. It
40041
 
** syncs the journal file to disk, then sets pPager->journalHdr to the
40042
 
** size of the journal file so that the pager_playback() routine knows
40043
 
** that the entire journal file has been synced.
40044
 
**
40045
 
** Syncing a hot-journal to disk before attempting to roll it back ensures 
40046
 
** that if a power-failure occurs during the rollback, the process that
40047
 
** attempts rollback following system recovery sees the same journal
40048
 
** content as this process.
40049
 
**
40050
 
** If everything goes as planned, SQLITE_OK is returned. Otherwise, 
40051
 
** an SQLite error code.
40052
 
*/
40053
 
static int pagerSyncHotJournal(Pager *pPager){
40054
 
  int rc = SQLITE_OK;
40055
 
  if( !pPager->noSync ){
40056
 
    rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
40057
 
  }
40058
 
  if( rc==SQLITE_OK ){
40059
 
    rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
40060
 
  }
40061
 
  return rc;
40062
 
}
40063
 
 
40064
 
/*
40065
 
** Shutdown the page cache.  Free all memory and close all files.
40066
 
**
40067
 
** If a transaction was in progress when this routine is called, that
40068
 
** transaction is rolled back.  All outstanding pages are invalidated
40069
 
** and their memory is freed.  Any attempt to use a page associated
40070
 
** with this page cache after this function returns will likely
40071
 
** result in a coredump.
40072
 
**
40073
 
** This function always succeeds. If a transaction is active an attempt
40074
 
** is made to roll it back. If an error occurs during the rollback 
40075
 
** a hot journal may be left in the filesystem but no error is returned
40076
 
** to the caller.
40077
 
*/
40078
 
SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
40079
 
  u8 *pTmp = (u8 *)pPager->pTmpSpace;
40080
 
 
40081
 
  disable_simulated_io_errors();
40082
 
  sqlite3BeginBenignMalloc();
40083
 
  /* pPager->errCode = 0; */
40084
 
  pPager->exclusiveMode = 0;
40085
 
#ifndef SQLITE_OMIT_WAL
40086
 
  sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
40087
 
  pPager->pWal = 0;
40088
 
#endif
40089
 
  pager_reset(pPager);
40090
 
  if( MEMDB ){
40091
 
    pager_unlock(pPager);
40092
 
  }else{
40093
 
    /* If it is open, sync the journal file before calling UnlockAndRollback.
40094
 
    ** If this is not done, then an unsynced portion of the open journal 
40095
 
    ** file may be played back into the database. If a power failure occurs 
40096
 
    ** while this is happening, the database could become corrupt.
40097
 
    **
40098
 
    ** If an error occurs while trying to sync the journal, shift the pager
40099
 
    ** into the ERROR state. This causes UnlockAndRollback to unlock the
40100
 
    ** database and close the journal file without attempting to roll it
40101
 
    ** back or finalize it. The next database user will have to do hot-journal
40102
 
    ** rollback before accessing the database file.
40103
 
    */
40104
 
    if( isOpen(pPager->jfd) ){
40105
 
      pager_error(pPager, pagerSyncHotJournal(pPager));
40106
 
    }
40107
 
    pagerUnlockAndRollback(pPager);
40108
 
  }
40109
 
  sqlite3EndBenignMalloc();
40110
 
  enable_simulated_io_errors();
40111
 
  PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
40112
 
  IOTRACE(("CLOSE %p\n", pPager))
40113
 
  sqlite3OsClose(pPager->jfd);
40114
 
  sqlite3OsClose(pPager->fd);
40115
 
  sqlite3PageFree(pTmp);
40116
 
  sqlite3PcacheClose(pPager->pPCache);
40117
 
 
40118
 
#ifdef SQLITE_HAS_CODEC
40119
 
  if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
40120
 
#endif
40121
 
 
40122
 
  assert( !pPager->aSavepoint && !pPager->pInJournal );
40123
 
  assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
40124
 
 
40125
 
  sqlite3_free(pPager);
40126
 
  return SQLITE_OK;
40127
 
}
40128
 
 
40129
 
#if !defined(NDEBUG) || defined(SQLITE_TEST)
40130
 
/*
40131
 
** Return the page number for page pPg.
40132
 
*/
40133
 
SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
40134
 
  return pPg->pgno;
40135
 
}
40136
 
#endif
40137
 
 
40138
 
/*
40139
 
** Increment the reference count for page pPg.
40140
 
*/
40141
 
SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
40142
 
  sqlite3PcacheRef(pPg);
40143
 
}
40144
 
 
40145
 
/*
40146
 
** Sync the journal. In other words, make sure all the pages that have
40147
 
** been written to the journal have actually reached the surface of the
40148
 
** disk and can be restored in the event of a hot-journal rollback.
40149
 
**
40150
 
** If the Pager.noSync flag is set, then this function is a no-op.
40151
 
** Otherwise, the actions required depend on the journal-mode and the 
40152
 
** device characteristics of the the file-system, as follows:
40153
 
**
40154
 
**   * If the journal file is an in-memory journal file, no action need
40155
 
**     be taken.
40156
 
**
40157
 
**   * Otherwise, if the device does not support the SAFE_APPEND property,
40158
 
**     then the nRec field of the most recently written journal header
40159
 
**     is updated to contain the number of journal records that have
40160
 
**     been written following it. If the pager is operating in full-sync
40161
 
**     mode, then the journal file is synced before this field is updated.
40162
 
**
40163
 
**   * If the device does not support the SEQUENTIAL property, then 
40164
 
**     journal file is synced.
40165
 
**
40166
 
** Or, in pseudo-code:
40167
 
**
40168
 
**   if( NOT <in-memory journal> ){
40169
 
**     if( NOT SAFE_APPEND ){
40170
 
**       if( <full-sync mode> ) xSync(<journal file>);
40171
 
**       <update nRec field>
40172
 
**     } 
40173
 
**     if( NOT SEQUENTIAL ) xSync(<journal file>);
40174
 
**   }
40175
 
**
40176
 
** If successful, this routine clears the PGHDR_NEED_SYNC flag of every 
40177
 
** page currently held in memory before returning SQLITE_OK. If an IO
40178
 
** error is encountered, then the IO error code is returned to the caller.
40179
 
*/
40180
 
static int syncJournal(Pager *pPager, int newHdr){
40181
 
  int rc;                         /* Return code */
40182
 
 
40183
 
  assert( pPager->eState==PAGER_WRITER_CACHEMOD
40184
 
       || pPager->eState==PAGER_WRITER_DBMOD
40185
 
  );
40186
 
  assert( assert_pager_state(pPager) );
40187
 
  assert( !pagerUseWal(pPager) );
40188
 
 
40189
 
  rc = sqlite3PagerExclusiveLock(pPager);
40190
 
  if( rc!=SQLITE_OK ) return rc;
40191
 
 
40192
 
  if( !pPager->noSync ){
40193
 
    assert( !pPager->tempFile );
40194
 
    if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
40195
 
      const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
40196
 
      assert( isOpen(pPager->jfd) );
40197
 
 
40198
 
      if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
40199
 
        /* This block deals with an obscure problem. If the last connection
40200
 
        ** that wrote to this database was operating in persistent-journal
40201
 
        ** mode, then the journal file may at this point actually be larger
40202
 
        ** than Pager.journalOff bytes. If the next thing in the journal
40203
 
        ** file happens to be a journal-header (written as part of the
40204
 
        ** previous connection's transaction), and a crash or power-failure 
40205
 
        ** occurs after nRec is updated but before this connection writes 
40206
 
        ** anything else to the journal file (or commits/rolls back its 
40207
 
        ** transaction), then SQLite may become confused when doing the 
40208
 
        ** hot-journal rollback following recovery. It may roll back all
40209
 
        ** of this connections data, then proceed to rolling back the old,
40210
 
        ** out-of-date data that follows it. Database corruption.
40211
 
        **
40212
 
        ** To work around this, if the journal file does appear to contain
40213
 
        ** a valid header following Pager.journalOff, then write a 0x00
40214
 
        ** byte to the start of it to prevent it from being recognized.
40215
 
        **
40216
 
        ** Variable iNextHdrOffset is set to the offset at which this
40217
 
        ** problematic header will occur, if it exists. aMagic is used 
40218
 
        ** as a temporary buffer to inspect the first couple of bytes of
40219
 
        ** the potential journal header.
40220
 
        */
40221
 
        i64 iNextHdrOffset;
40222
 
        u8 aMagic[8];
40223
 
        u8 zHeader[sizeof(aJournalMagic)+4];
40224
 
 
40225
 
        memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
40226
 
        put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
40227
 
 
40228
 
        iNextHdrOffset = journalHdrOffset(pPager);
40229
 
        rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
40230
 
        if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
40231
 
          static const u8 zerobyte = 0;
40232
 
          rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
40233
 
        }
40234
 
        if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
40235
 
          return rc;
40236
 
        }
40237
 
 
40238
 
        /* Write the nRec value into the journal file header. If in
40239
 
        ** full-synchronous mode, sync the journal first. This ensures that
40240
 
        ** all data has really hit the disk before nRec is updated to mark
40241
 
        ** it as a candidate for rollback.
40242
 
        **
40243
 
        ** This is not required if the persistent media supports the
40244
 
        ** SAFE_APPEND property. Because in this case it is not possible 
40245
 
        ** for garbage data to be appended to the file, the nRec field
40246
 
        ** is populated with 0xFFFFFFFF when the journal header is written
40247
 
        ** and never needs to be updated.
40248
 
        */
40249
 
        if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
40250
 
          PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
40251
 
          IOTRACE(("JSYNC %p\n", pPager))
40252
 
          rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
40253
 
          if( rc!=SQLITE_OK ) return rc;
40254
 
        }
40255
 
        IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
40256
 
        rc = sqlite3OsWrite(
40257
 
            pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
40258
 
        );
40259
 
        if( rc!=SQLITE_OK ) return rc;
40260
 
      }
40261
 
      if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
40262
 
        PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
40263
 
        IOTRACE(("JSYNC %p\n", pPager))
40264
 
        rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags| 
40265
 
          (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
40266
 
        );
40267
 
        if( rc!=SQLITE_OK ) return rc;
40268
 
      }
40269
 
 
40270
 
      pPager->journalHdr = pPager->journalOff;
40271
 
      if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
40272
 
        pPager->nRec = 0;
40273
 
        rc = writeJournalHdr(pPager);
40274
 
        if( rc!=SQLITE_OK ) return rc;
40275
 
      }
40276
 
    }else{
40277
 
      pPager->journalHdr = pPager->journalOff;
40278
 
    }
40279
 
  }
40280
 
 
40281
 
  /* Unless the pager is in noSync mode, the journal file was just 
40282
 
  ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on 
40283
 
  ** all pages.
40284
 
  */
40285
 
  sqlite3PcacheClearSyncFlags(pPager->pPCache);
40286
 
  pPager->eState = PAGER_WRITER_DBMOD;
40287
 
  assert( assert_pager_state(pPager) );
40288
 
  return SQLITE_OK;
40289
 
}
40290
 
 
40291
 
/*
40292
 
** The argument is the first in a linked list of dirty pages connected
40293
 
** by the PgHdr.pDirty pointer. This function writes each one of the
40294
 
** in-memory pages in the list to the database file. The argument may
40295
 
** be NULL, representing an empty list. In this case this function is
40296
 
** a no-op.
40297
 
**
40298
 
** The pager must hold at least a RESERVED lock when this function
40299
 
** is called. Before writing anything to the database file, this lock
40300
 
** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
40301
 
** SQLITE_BUSY is returned and no data is written to the database file.
40302
 
** 
40303
 
** If the pager is a temp-file pager and the actual file-system file
40304
 
** is not yet open, it is created and opened before any data is 
40305
 
** written out.
40306
 
**
40307
 
** Once the lock has been upgraded and, if necessary, the file opened,
40308
 
** the pages are written out to the database file in list order. Writing
40309
 
** a page is skipped if it meets either of the following criteria:
40310
 
**
40311
 
**   * The page number is greater than Pager.dbSize, or
40312
 
**   * The PGHDR_DONT_WRITE flag is set on the page.
40313
 
**
40314
 
** If writing out a page causes the database file to grow, Pager.dbFileSize
40315
 
** is updated accordingly. If page 1 is written out, then the value cached
40316
 
** in Pager.dbFileVers[] is updated to match the new value stored in
40317
 
** the database file.
40318
 
**
40319
 
** If everything is successful, SQLITE_OK is returned. If an IO error 
40320
 
** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
40321
 
** be obtained, SQLITE_BUSY is returned.
40322
 
*/
40323
 
static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
40324
 
  int rc = SQLITE_OK;                  /* Return code */
40325
 
 
40326
 
  /* This function is only called for rollback pagers in WRITER_DBMOD state. */
40327
 
  assert( !pagerUseWal(pPager) );
40328
 
  assert( pPager->eState==PAGER_WRITER_DBMOD );
40329
 
  assert( pPager->eLock==EXCLUSIVE_LOCK );
40330
 
 
40331
 
  /* If the file is a temp-file has not yet been opened, open it now. It
40332
 
  ** is not possible for rc to be other than SQLITE_OK if this branch
40333
 
  ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
40334
 
  */
40335
 
  if( !isOpen(pPager->fd) ){
40336
 
    assert( pPager->tempFile && rc==SQLITE_OK );
40337
 
    rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
40338
 
  }
40339
 
 
40340
 
  /* Before the first write, give the VFS a hint of what the final
40341
 
  ** file size will be.
40342
 
  */
40343
 
  assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
40344
 
  if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
40345
 
    sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
40346
 
    sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
40347
 
    pPager->dbHintSize = pPager->dbSize;
40348
 
  }
40349
 
 
40350
 
  while( rc==SQLITE_OK && pList ){
40351
 
    Pgno pgno = pList->pgno;
40352
 
 
40353
 
    /* If there are dirty pages in the page cache with page numbers greater
40354
 
    ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
40355
 
    ** make the file smaller (presumably by auto-vacuum code). Do not write
40356
 
    ** any such pages to the file.
40357
 
    **
40358
 
    ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
40359
 
    ** set (set by sqlite3PagerDontWrite()).
40360
 
    */
40361
 
    if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
40362
 
      i64 offset = (pgno-1)*(i64)pPager->pageSize;   /* Offset to write */
40363
 
      char *pData;                                   /* Data to write */    
40364
 
 
40365
 
      assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
40366
 
      if( pList->pgno==1 ) pager_write_changecounter(pList);
40367
 
 
40368
 
      /* Encode the database */
40369
 
      CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
40370
 
 
40371
 
      /* Write out the page data. */
40372
 
      rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
40373
 
 
40374
 
      /* If page 1 was just written, update Pager.dbFileVers to match
40375
 
      ** the value now stored in the database file. If writing this 
40376
 
      ** page caused the database file to grow, update dbFileSize. 
40377
 
      */
40378
 
      if( pgno==1 ){
40379
 
        memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
40380
 
      }
40381
 
      if( pgno>pPager->dbFileSize ){
40382
 
        pPager->dbFileSize = pgno;
40383
 
      }
40384
 
 
40385
 
      /* Update any backup objects copying the contents of this pager. */
40386
 
      sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
40387
 
 
40388
 
      PAGERTRACE(("STORE %d page %d hash(%08x)\n",
40389
 
                   PAGERID(pPager), pgno, pager_pagehash(pList)));
40390
 
      IOTRACE(("PGOUT %p %d\n", pPager, pgno));
40391
 
      PAGER_INCR(sqlite3_pager_writedb_count);
40392
 
      PAGER_INCR(pPager->nWrite);
40393
 
    }else{
40394
 
      PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
40395
 
    }
40396
 
    pager_set_pagehash(pList);
40397
 
    pList = pList->pDirty;
40398
 
  }
40399
 
 
40400
 
  return rc;
40401
 
}
40402
 
 
40403
 
/*
40404
 
** Ensure that the sub-journal file is open. If it is already open, this 
40405
 
** function is a no-op.
40406
 
**
40407
 
** SQLITE_OK is returned if everything goes according to plan. An 
40408
 
** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen() 
40409
 
** fails.
40410
 
*/
40411
 
static int openSubJournal(Pager *pPager){
40412
 
  int rc = SQLITE_OK;
40413
 
  if( !isOpen(pPager->sjfd) ){
40414
 
    if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
40415
 
      sqlite3MemJournalOpen(pPager->sjfd);
40416
 
    }else{
40417
 
      rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
40418
 
    }
40419
 
  }
40420
 
  return rc;
40421
 
}
40422
 
 
40423
 
/*
40424
 
** Append a record of the current state of page pPg to the sub-journal. 
40425
 
** It is the callers responsibility to use subjRequiresPage() to check 
40426
 
** that it is really required before calling this function.
40427
 
**
40428
 
** If successful, set the bit corresponding to pPg->pgno in the bitvecs
40429
 
** for all open savepoints before returning.
40430
 
**
40431
 
** This function returns SQLITE_OK if everything is successful, an IO
40432
 
** error code if the attempt to write to the sub-journal fails, or 
40433
 
** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
40434
 
** bitvec.
40435
 
*/
40436
 
static int subjournalPage(PgHdr *pPg){
40437
 
  int rc = SQLITE_OK;
40438
 
  Pager *pPager = pPg->pPager;
40439
 
  if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
40440
 
 
40441
 
    /* Open the sub-journal, if it has not already been opened */
40442
 
    assert( pPager->useJournal );
40443
 
    assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
40444
 
    assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
40445
 
    assert( pagerUseWal(pPager) 
40446
 
         || pageInJournal(pPg) 
40447
 
         || pPg->pgno>pPager->dbOrigSize 
40448
 
    );
40449
 
    rc = openSubJournal(pPager);
40450
 
 
40451
 
    /* If the sub-journal was opened successfully (or was already open),
40452
 
    ** write the journal record into the file.  */
40453
 
    if( rc==SQLITE_OK ){
40454
 
      void *pData = pPg->pData;
40455
 
      i64 offset = pPager->nSubRec*(4+pPager->pageSize);
40456
 
      char *pData2;
40457
 
  
40458
 
      CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
40459
 
      PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
40460
 
      rc = write32bits(pPager->sjfd, offset, pPg->pgno);
40461
 
      if( rc==SQLITE_OK ){
40462
 
        rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
40463
 
      }
40464
 
    }
40465
 
  }
40466
 
  if( rc==SQLITE_OK ){
40467
 
    pPager->nSubRec++;
40468
 
    assert( pPager->nSavepoint>0 );
40469
 
    rc = addToSavepointBitvecs(pPager, pPg->pgno);
40470
 
  }
40471
 
  return rc;
40472
 
}
40473
 
 
40474
 
/*
40475
 
** This function is called by the pcache layer when it has reached some
40476
 
** soft memory limit. The first argument is a pointer to a Pager object
40477
 
** (cast as a void*). The pager is always 'purgeable' (not an in-memory
40478
 
** database). The second argument is a reference to a page that is 
40479
 
** currently dirty but has no outstanding references. The page
40480
 
** is always associated with the Pager object passed as the first 
40481
 
** argument.
40482
 
**
40483
 
** The job of this function is to make pPg clean by writing its contents
40484
 
** out to the database file, if possible. This may involve syncing the
40485
 
** journal file. 
40486
 
**
40487
 
** If successful, sqlite3PcacheMakeClean() is called on the page and
40488
 
** SQLITE_OK returned. If an IO error occurs while trying to make the
40489
 
** page clean, the IO error code is returned. If the page cannot be
40490
 
** made clean for some other reason, but no error occurs, then SQLITE_OK
40491
 
** is returned by sqlite3PcacheMakeClean() is not called.
40492
 
*/
40493
 
static int pagerStress(void *p, PgHdr *pPg){
40494
 
  Pager *pPager = (Pager *)p;
40495
 
  int rc = SQLITE_OK;
40496
 
 
40497
 
  assert( pPg->pPager==pPager );
40498
 
  assert( pPg->flags&PGHDR_DIRTY );
40499
 
 
40500
 
  /* The doNotSyncSpill flag is set during times when doing a sync of
40501
 
  ** journal (and adding a new header) is not allowed.  This occurs
40502
 
  ** during calls to sqlite3PagerWrite() while trying to journal multiple
40503
 
  ** pages belonging to the same sector.
40504
 
  **
40505
 
  ** The doNotSpill flag inhibits all cache spilling regardless of whether
40506
 
  ** or not a sync is required.  This is set during a rollback.
40507
 
  **
40508
 
  ** Spilling is also prohibited when in an error state since that could
40509
 
  ** lead to database corruption.   In the current implementaton it 
40510
 
  ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
40511
 
  ** while in the error state, hence it is impossible for this routine to
40512
 
  ** be called in the error state.  Nevertheless, we include a NEVER()
40513
 
  ** test for the error state as a safeguard against future changes.
40514
 
  */
40515
 
  if( NEVER(pPager->errCode) ) return SQLITE_OK;
40516
 
  if( pPager->doNotSpill ) return SQLITE_OK;
40517
 
  if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
40518
 
    return SQLITE_OK;
40519
 
  }
40520
 
 
40521
 
  pPg->pDirty = 0;
40522
 
  if( pagerUseWal(pPager) ){
40523
 
    /* Write a single frame for this page to the log. */
40524
 
    if( subjRequiresPage(pPg) ){ 
40525
 
      rc = subjournalPage(pPg); 
40526
 
    }
40527
 
    if( rc==SQLITE_OK ){
40528
 
      rc = pagerWalFrames(pPager, pPg, 0, 0, 0);
40529
 
    }
40530
 
  }else{
40531
 
  
40532
 
    /* Sync the journal file if required. */
40533
 
    if( pPg->flags&PGHDR_NEED_SYNC 
40534
 
     || pPager->eState==PAGER_WRITER_CACHEMOD
40535
 
    ){
40536
 
      rc = syncJournal(pPager, 1);
40537
 
    }
40538
 
  
40539
 
    /* If the page number of this page is larger than the current size of
40540
 
    ** the database image, it may need to be written to the sub-journal.
40541
 
    ** This is because the call to pager_write_pagelist() below will not
40542
 
    ** actually write data to the file in this case.
40543
 
    **
40544
 
    ** Consider the following sequence of events:
40545
 
    **
40546
 
    **   BEGIN;
40547
 
    **     <journal page X>
40548
 
    **     <modify page X>
40549
 
    **     SAVEPOINT sp;
40550
 
    **       <shrink database file to Y pages>
40551
 
    **       pagerStress(page X)
40552
 
    **     ROLLBACK TO sp;
40553
 
    **
40554
 
    ** If (X>Y), then when pagerStress is called page X will not be written
40555
 
    ** out to the database file, but will be dropped from the cache. Then,
40556
 
    ** following the "ROLLBACK TO sp" statement, reading page X will read
40557
 
    ** data from the database file. This will be the copy of page X as it
40558
 
    ** was when the transaction started, not as it was when "SAVEPOINT sp"
40559
 
    ** was executed.
40560
 
    **
40561
 
    ** The solution is to write the current data for page X into the 
40562
 
    ** sub-journal file now (if it is not already there), so that it will
40563
 
    ** be restored to its current value when the "ROLLBACK TO sp" is 
40564
 
    ** executed.
40565
 
    */
40566
 
    if( NEVER(
40567
 
        rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
40568
 
    ) ){
40569
 
      rc = subjournalPage(pPg);
40570
 
    }
40571
 
  
40572
 
    /* Write the contents of the page out to the database file. */
40573
 
    if( rc==SQLITE_OK ){
40574
 
      assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
40575
 
      rc = pager_write_pagelist(pPager, pPg);
40576
 
    }
40577
 
  }
40578
 
 
40579
 
  /* Mark the page as clean. */
40580
 
  if( rc==SQLITE_OK ){
40581
 
    PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
40582
 
    sqlite3PcacheMakeClean(pPg);
40583
 
  }
40584
 
 
40585
 
  return pager_error(pPager, rc); 
40586
 
}
40587
 
 
40588
 
 
40589
 
/*
40590
 
** Allocate and initialize a new Pager object and put a pointer to it
40591
 
** in *ppPager. The pager should eventually be freed by passing it
40592
 
** to sqlite3PagerClose().
40593
 
**
40594
 
** The zFilename argument is the path to the database file to open.
40595
 
** If zFilename is NULL then a randomly-named temporary file is created
40596
 
** and used as the file to be cached. Temporary files are be deleted
40597
 
** automatically when they are closed. If zFilename is ":memory:" then 
40598
 
** all information is held in cache. It is never written to disk. 
40599
 
** This can be used to implement an in-memory database.
40600
 
**
40601
 
** The nExtra parameter specifies the number of bytes of space allocated
40602
 
** along with each page reference. This space is available to the user
40603
 
** via the sqlite3PagerGetExtra() API.
40604
 
**
40605
 
** The flags argument is used to specify properties that affect the
40606
 
** operation of the pager. It should be passed some bitwise combination
40607
 
** of the PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK flags.
40608
 
**
40609
 
** The vfsFlags parameter is a bitmask to pass to the flags parameter
40610
 
** of the xOpen() method of the supplied VFS when opening files. 
40611
 
**
40612
 
** If the pager object is allocated and the specified file opened 
40613
 
** successfully, SQLITE_OK is returned and *ppPager set to point to
40614
 
** the new pager object. If an error occurs, *ppPager is set to NULL
40615
 
** and error code returned. This function may return SQLITE_NOMEM
40616
 
** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or 
40617
 
** various SQLITE_IO_XXX errors.
40618
 
*/
40619
 
SQLITE_PRIVATE int sqlite3PagerOpen(
40620
 
  sqlite3_vfs *pVfs,       /* The virtual file system to use */
40621
 
  Pager **ppPager,         /* OUT: Return the Pager structure here */
40622
 
  const char *zFilename,   /* Name of the database file to open */
40623
 
  int nExtra,              /* Extra bytes append to each in-memory page */
40624
 
  int flags,               /* flags controlling this file */
40625
 
  int vfsFlags,            /* flags passed through to sqlite3_vfs.xOpen() */
40626
 
  void (*xReinit)(DbPage*) /* Function to reinitialize pages */
40627
 
){
40628
 
  u8 *pPtr;
40629
 
  Pager *pPager = 0;       /* Pager object to allocate and return */
40630
 
  int rc = SQLITE_OK;      /* Return code */
40631
 
  int tempFile = 0;        /* True for temp files (incl. in-memory files) */
40632
 
  int memDb = 0;           /* True if this is an in-memory file */
40633
 
  int readOnly = 0;        /* True if this is a read-only file */
40634
 
  int journalFileSize;     /* Bytes to allocate for each journal fd */
40635
 
  char *zPathname = 0;     /* Full path to database file */
40636
 
  int nPathname = 0;       /* Number of bytes in zPathname */
40637
 
  int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
40638
 
  int noReadlock = (flags & PAGER_NO_READLOCK)!=0;  /* True to omit read-lock */
40639
 
  int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
40640
 
  u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
40641
 
 
40642
 
  /* Figure out how much space is required for each journal file-handle
40643
 
  ** (there are two of them, the main journal and the sub-journal). This
40644
 
  ** is the maximum space required for an in-memory journal file handle 
40645
 
  ** and a regular journal file-handle. Note that a "regular journal-handle"
40646
 
  ** may be a wrapper capable of caching the first portion of the journal
40647
 
  ** file in memory to implement the atomic-write optimization (see 
40648
 
  ** source file journal.c).
40649
 
  */
40650
 
  if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
40651
 
    journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
40652
 
  }else{
40653
 
    journalFileSize = ROUND8(sqlite3MemJournalSize());
40654
 
  }
40655
 
 
40656
 
  /* Set the output variable to NULL in case an error occurs. */
40657
 
  *ppPager = 0;
40658
 
 
40659
 
#ifndef SQLITE_OMIT_MEMORYDB
40660
 
  if( flags & PAGER_MEMORY ){
40661
 
    memDb = 1;
40662
 
    zFilename = 0;
40663
 
  }
40664
 
#endif
40665
 
 
40666
 
  /* Compute and store the full pathname in an allocated buffer pointed
40667
 
  ** to by zPathname, length nPathname. Or, if this is a temporary file,
40668
 
  ** leave both nPathname and zPathname set to 0.
40669
 
  */
40670
 
  if( zFilename && zFilename[0] ){
40671
 
    nPathname = pVfs->mxPathname+1;
40672
 
    zPathname = sqlite3Malloc(nPathname*2);
40673
 
    if( zPathname==0 ){
40674
 
      return SQLITE_NOMEM;
40675
 
    }
40676
 
    zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
40677
 
    rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
40678
 
    nPathname = sqlite3Strlen30(zPathname);
40679
 
    if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
40680
 
      /* This branch is taken when the journal path required by
40681
 
      ** the database being opened will be more than pVfs->mxPathname
40682
 
      ** bytes in length. This means the database cannot be opened,
40683
 
      ** as it will not be possible to open the journal file or even
40684
 
      ** check for a hot-journal before reading.
40685
 
      */
40686
 
      rc = SQLITE_CANTOPEN_BKPT;
40687
 
    }
40688
 
    if( rc!=SQLITE_OK ){
40689
 
      sqlite3_free(zPathname);
40690
 
      return rc;
40691
 
    }
40692
 
  }
40693
 
 
40694
 
  /* Allocate memory for the Pager structure, PCache object, the
40695
 
  ** three file descriptors, the database file name and the journal 
40696
 
  ** file name. The layout in memory is as follows:
40697
 
  **
40698
 
  **     Pager object                    (sizeof(Pager) bytes)
40699
 
  **     PCache object                   (sqlite3PcacheSize() bytes)
40700
 
  **     Database file handle            (pVfs->szOsFile bytes)
40701
 
  **     Sub-journal file handle         (journalFileSize bytes)
40702
 
  **     Main journal file handle        (journalFileSize bytes)
40703
 
  **     Database file name              (nPathname+1 bytes)
40704
 
  **     Journal file name               (nPathname+8+1 bytes)
40705
 
  */
40706
 
  pPtr = (u8 *)sqlite3MallocZero(
40707
 
    ROUND8(sizeof(*pPager)) +      /* Pager structure */
40708
 
    ROUND8(pcacheSize) +           /* PCache object */
40709
 
    ROUND8(pVfs->szOsFile) +       /* The main db file */
40710
 
    journalFileSize * 2 +          /* The two journal files */ 
40711
 
    nPathname + 1 +                /* zFilename */
40712
 
    nPathname + 8 + 1              /* zJournal */
40713
 
#ifndef SQLITE_OMIT_WAL
40714
 
    + nPathname + 4 + 1              /* zWal */
40715
 
#endif
40716
 
  );
40717
 
  assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
40718
 
  if( !pPtr ){
40719
 
    sqlite3_free(zPathname);
40720
 
    return SQLITE_NOMEM;
40721
 
  }
40722
 
  pPager =              (Pager*)(pPtr);
40723
 
  pPager->pPCache =    (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
40724
 
  pPager->fd =   (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
40725
 
  pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
40726
 
  pPager->jfd =  (sqlite3_file*)(pPtr += journalFileSize);
40727
 
  pPager->zFilename =    (char*)(pPtr += journalFileSize);
40728
 
  assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
40729
 
 
40730
 
  /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
40731
 
  if( zPathname ){
40732
 
    assert( nPathname>0 );
40733
 
    pPager->zJournal =   (char*)(pPtr += nPathname + 1);
40734
 
    memcpy(pPager->zFilename, zPathname, nPathname);
40735
 
    memcpy(pPager->zJournal, zPathname, nPathname);
40736
 
    memcpy(&pPager->zJournal[nPathname], "-journal", 8);
40737
 
#ifndef SQLITE_OMIT_WAL
40738
 
    pPager->zWal = &pPager->zJournal[nPathname+8+1];
40739
 
    memcpy(pPager->zWal, zPathname, nPathname);
40740
 
    memcpy(&pPager->zWal[nPathname], "-wal", 4);
40741
 
#endif
40742
 
    sqlite3_free(zPathname);
40743
 
  }
40744
 
  pPager->pVfs = pVfs;
40745
 
  pPager->vfsFlags = vfsFlags;
40746
 
 
40747
 
  /* Open the pager file.
40748
 
  */
40749
 
  if( zFilename && zFilename[0] ){
40750
 
    int fout = 0;                    /* VFS flags returned by xOpen() */
40751
 
    rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
40752
 
    assert( !memDb );
40753
 
    readOnly = (fout&SQLITE_OPEN_READONLY);
40754
 
 
40755
 
    /* If the file was successfully opened for read/write access,
40756
 
    ** choose a default page size in case we have to create the
40757
 
    ** database file. The default page size is the maximum of:
40758
 
    **
40759
 
    **    + SQLITE_DEFAULT_PAGE_SIZE,
40760
 
    **    + The value returned by sqlite3OsSectorSize()
40761
 
    **    + The largest page size that can be written atomically.
40762
 
    */
40763
 
    if( rc==SQLITE_OK && !readOnly ){
40764
 
      setSectorSize(pPager);
40765
 
      assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
40766
 
      if( szPageDflt<pPager->sectorSize ){
40767
 
        if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
40768
 
          szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
40769
 
        }else{
40770
 
          szPageDflt = (u32)pPager->sectorSize;
40771
 
        }
40772
 
      }
40773
 
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
40774
 
      {
40775
 
        int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
40776
 
        int ii;
40777
 
        assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
40778
 
        assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
40779
 
        assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
40780
 
        for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
40781
 
          if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
40782
 
            szPageDflt = ii;
40783
 
          }
40784
 
        }
40785
 
      }
40786
 
#endif
40787
 
    }
40788
 
  }else{
40789
 
    /* If a temporary file is requested, it is not opened immediately.
40790
 
    ** In this case we accept the default page size and delay actually
40791
 
    ** opening the file until the first call to OsWrite().
40792
 
    **
40793
 
    ** This branch is also run for an in-memory database. An in-memory
40794
 
    ** database is the same as a temp-file that is never written out to
40795
 
    ** disk and uses an in-memory rollback journal.
40796
 
    */ 
40797
 
    tempFile = 1;
40798
 
    pPager->eState = PAGER_READER;
40799
 
    pPager->eLock = EXCLUSIVE_LOCK;
40800
 
    readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
40801
 
  }
40802
 
 
40803
 
  /* The following call to PagerSetPagesize() serves to set the value of 
40804
 
  ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
40805
 
  */
40806
 
  if( rc==SQLITE_OK ){
40807
 
    assert( pPager->memDb==0 );
40808
 
    rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
40809
 
    testcase( rc!=SQLITE_OK );
40810
 
  }
40811
 
 
40812
 
  /* If an error occurred in either of the blocks above, free the 
40813
 
  ** Pager structure and close the file.
40814
 
  */
40815
 
  if( rc!=SQLITE_OK ){
40816
 
    assert( !pPager->pTmpSpace );
40817
 
    sqlite3OsClose(pPager->fd);
40818
 
    sqlite3_free(pPager);
40819
 
    return rc;
40820
 
  }
40821
 
 
40822
 
  /* Initialize the PCache object. */
40823
 
  assert( nExtra<1000 );
40824
 
  nExtra = ROUND8(nExtra);
40825
 
  sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
40826
 
                    !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
40827
 
 
40828
 
  PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
40829
 
  IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
40830
 
 
40831
 
  pPager->useJournal = (u8)useJournal;
40832
 
  pPager->noReadlock = (noReadlock && readOnly) ?1:0;
40833
 
  /* pPager->stmtOpen = 0; */
40834
 
  /* pPager->stmtInUse = 0; */
40835
 
  /* pPager->nRef = 0; */
40836
 
  /* pPager->stmtSize = 0; */
40837
 
  /* pPager->stmtJSize = 0; */
40838
 
  /* pPager->nPage = 0; */
40839
 
  pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
40840
 
  /* pPager->state = PAGER_UNLOCK; */
40841
 
#if 0
40842
 
  assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
40843
 
#endif
40844
 
  /* pPager->errMask = 0; */
40845
 
  pPager->tempFile = (u8)tempFile;
40846
 
  assert( tempFile==PAGER_LOCKINGMODE_NORMAL 
40847
 
          || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
40848
 
  assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
40849
 
  pPager->exclusiveMode = (u8)tempFile; 
40850
 
  pPager->changeCountDone = pPager->tempFile;
40851
 
  pPager->memDb = (u8)memDb;
40852
 
  pPager->readOnly = (u8)readOnly;
40853
 
  assert( useJournal || pPager->tempFile );
40854
 
  pPager->noSync = pPager->tempFile;
40855
 
  pPager->fullSync = pPager->noSync ?0:1;
40856
 
  pPager->syncFlags = pPager->noSync ? 0 : SQLITE_SYNC_NORMAL;
40857
 
  pPager->ckptSyncFlags = pPager->syncFlags;
40858
 
  /* pPager->pFirst = 0; */
40859
 
  /* pPager->pFirstSynced = 0; */
40860
 
  /* pPager->pLast = 0; */
40861
 
  pPager->nExtra = (u16)nExtra;
40862
 
  pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
40863
 
  assert( isOpen(pPager->fd) || tempFile );
40864
 
  setSectorSize(pPager);
40865
 
  if( !useJournal ){
40866
 
    pPager->journalMode = PAGER_JOURNALMODE_OFF;
40867
 
  }else if( memDb ){
40868
 
    pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
40869
 
  }
40870
 
  /* pPager->xBusyHandler = 0; */
40871
 
  /* pPager->pBusyHandlerArg = 0; */
40872
 
  pPager->xReiniter = xReinit;
40873
 
  /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
40874
 
 
40875
 
  *ppPager = pPager;
40876
 
  return SQLITE_OK;
40877
 
}
40878
 
 
40879
 
 
40880
 
 
40881
 
/*
40882
 
** This function is called after transitioning from PAGER_UNLOCK to
40883
 
** PAGER_SHARED state. It tests if there is a hot journal present in
40884
 
** the file-system for the given pager. A hot journal is one that 
40885
 
** needs to be played back. According to this function, a hot-journal
40886
 
** file exists if the following criteria are met:
40887
 
**
40888
 
**   * The journal file exists in the file system, and
40889
 
**   * No process holds a RESERVED or greater lock on the database file, and
40890
 
**   * The database file itself is greater than 0 bytes in size, and
40891
 
**   * The first byte of the journal file exists and is not 0x00.
40892
 
**
40893
 
** If the current size of the database file is 0 but a journal file
40894
 
** exists, that is probably an old journal left over from a prior
40895
 
** database with the same name. In this case the journal file is
40896
 
** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
40897
 
** is returned.
40898
 
**
40899
 
** This routine does not check if there is a master journal filename
40900
 
** at the end of the file. If there is, and that master journal file
40901
 
** does not exist, then the journal file is not really hot. In this
40902
 
** case this routine will return a false-positive. The pager_playback()
40903
 
** routine will discover that the journal file is not really hot and 
40904
 
** will not roll it back. 
40905
 
**
40906
 
** If a hot-journal file is found to exist, *pExists is set to 1 and 
40907
 
** SQLITE_OK returned. If no hot-journal file is present, *pExists is
40908
 
** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
40909
 
** to determine whether or not a hot-journal file exists, the IO error
40910
 
** code is returned and the value of *pExists is undefined.
40911
 
*/
40912
 
static int hasHotJournal(Pager *pPager, int *pExists){
40913
 
  sqlite3_vfs * const pVfs = pPager->pVfs;
40914
 
  int rc = SQLITE_OK;           /* Return code */
40915
 
  int exists = 1;               /* True if a journal file is present */
40916
 
  int jrnlOpen = !!isOpen(pPager->jfd);
40917
 
 
40918
 
  assert( pPager->useJournal );
40919
 
  assert( isOpen(pPager->fd) );
40920
 
  assert( pPager->eState==PAGER_OPEN );
40921
 
 
40922
 
  assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
40923
 
    SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
40924
 
  ));
40925
 
 
40926
 
  *pExists = 0;
40927
 
  if( !jrnlOpen ){
40928
 
    rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
40929
 
  }
40930
 
  if( rc==SQLITE_OK && exists ){
40931
 
    int locked = 0;             /* True if some process holds a RESERVED lock */
40932
 
 
40933
 
    /* Race condition here:  Another process might have been holding the
40934
 
    ** the RESERVED lock and have a journal open at the sqlite3OsAccess() 
40935
 
    ** call above, but then delete the journal and drop the lock before
40936
 
    ** we get to the following sqlite3OsCheckReservedLock() call.  If that
40937
 
    ** is the case, this routine might think there is a hot journal when
40938
 
    ** in fact there is none.  This results in a false-positive which will
40939
 
    ** be dealt with by the playback routine.  Ticket #3883.
40940
 
    */
40941
 
    rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
40942
 
    if( rc==SQLITE_OK && !locked ){
40943
 
      Pgno nPage;                 /* Number of pages in database file */
40944
 
 
40945
 
      /* Check the size of the database file. If it consists of 0 pages,
40946
 
      ** then delete the journal file. See the header comment above for 
40947
 
      ** the reasoning here.  Delete the obsolete journal file under
40948
 
      ** a RESERVED lock to avoid race conditions and to avoid violating
40949
 
      ** [H33020].
40950
 
      */
40951
 
      rc = pagerPagecount(pPager, &nPage);
40952
 
      if( rc==SQLITE_OK ){
40953
 
        if( nPage==0 ){
40954
 
          sqlite3BeginBenignMalloc();
40955
 
          if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
40956
 
            sqlite3OsDelete(pVfs, pPager->zJournal, 0);
40957
 
            if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
40958
 
          }
40959
 
          sqlite3EndBenignMalloc();
40960
 
        }else{
40961
 
          /* The journal file exists and no other connection has a reserved
40962
 
          ** or greater lock on the database file. Now check that there is
40963
 
          ** at least one non-zero bytes at the start of the journal file.
40964
 
          ** If there is, then we consider this journal to be hot. If not, 
40965
 
          ** it can be ignored.
40966
 
          */
40967
 
          if( !jrnlOpen ){
40968
 
            int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
40969
 
            rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
40970
 
          }
40971
 
          if( rc==SQLITE_OK ){
40972
 
            u8 first = 0;
40973
 
            rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
40974
 
            if( rc==SQLITE_IOERR_SHORT_READ ){
40975
 
              rc = SQLITE_OK;
40976
 
            }
40977
 
            if( !jrnlOpen ){
40978
 
              sqlite3OsClose(pPager->jfd);
40979
 
            }
40980
 
            *pExists = (first!=0);
40981
 
          }else if( rc==SQLITE_CANTOPEN ){
40982
 
            /* If we cannot open the rollback journal file in order to see if
40983
 
            ** its has a zero header, that might be due to an I/O error, or
40984
 
            ** it might be due to the race condition described above and in
40985
 
            ** ticket #3883.  Either way, assume that the journal is hot.
40986
 
            ** This might be a false positive.  But if it is, then the
40987
 
            ** automatic journal playback and recovery mechanism will deal
40988
 
            ** with it under an EXCLUSIVE lock where we do not need to
40989
 
            ** worry so much with race conditions.
40990
 
            */
40991
 
            *pExists = 1;
40992
 
            rc = SQLITE_OK;
40993
 
          }
40994
 
        }
40995
 
      }
40996
 
    }
40997
 
  }
40998
 
 
40999
 
  return rc;
41000
 
}
41001
 
 
41002
 
/*
41003
 
** This function is called to obtain a shared lock on the database file.
41004
 
** It is illegal to call sqlite3PagerAcquire() until after this function
41005
 
** has been successfully called. If a shared-lock is already held when
41006
 
** this function is called, it is a no-op.
41007
 
**
41008
 
** The following operations are also performed by this function.
41009
 
**
41010
 
**   1) If the pager is currently in PAGER_OPEN state (no lock held
41011
 
**      on the database file), then an attempt is made to obtain a
41012
 
**      SHARED lock on the database file. Immediately after obtaining
41013
 
**      the SHARED lock, the file-system is checked for a hot-journal,
41014
 
**      which is played back if present. Following any hot-journal 
41015
 
**      rollback, the contents of the cache are validated by checking
41016
 
**      the 'change-counter' field of the database file header and
41017
 
**      discarded if they are found to be invalid.
41018
 
**
41019
 
**   2) If the pager is running in exclusive-mode, and there are currently
41020
 
**      no outstanding references to any pages, and is in the error state,
41021
 
**      then an attempt is made to clear the error state by discarding
41022
 
**      the contents of the page cache and rolling back any open journal
41023
 
**      file.
41024
 
**
41025
 
** If everything is successful, SQLITE_OK is returned. If an IO error 
41026
 
** occurs while locking the database, checking for a hot-journal file or 
41027
 
** rolling back a journal file, the IO error code is returned.
41028
 
*/
41029
 
SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
41030
 
  int rc = SQLITE_OK;                /* Return code */
41031
 
 
41032
 
  /* This routine is only called from b-tree and only when there are no
41033
 
  ** outstanding pages. This implies that the pager state should either
41034
 
  ** be OPEN or READER. READER is only possible if the pager is or was in 
41035
 
  ** exclusive access mode.
41036
 
  */
41037
 
  assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
41038
 
  assert( assert_pager_state(pPager) );
41039
 
  assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
41040
 
  if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
41041
 
 
41042
 
  if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
41043
 
    int bHotJournal = 1;          /* True if there exists a hot journal-file */
41044
 
 
41045
 
    assert( !MEMDB );
41046
 
    assert( pPager->noReadlock==0 || pPager->readOnly );
41047
 
 
41048
 
    if( pPager->noReadlock==0 ){
41049
 
      rc = pager_wait_on_lock(pPager, SHARED_LOCK);
41050
 
      if( rc!=SQLITE_OK ){
41051
 
        assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
41052
 
        goto failed;
41053
 
      }
41054
 
    }
41055
 
 
41056
 
    /* If a journal file exists, and there is no RESERVED lock on the
41057
 
    ** database file, then it either needs to be played back or deleted.
41058
 
    */
41059
 
    if( pPager->eLock<=SHARED_LOCK ){
41060
 
      rc = hasHotJournal(pPager, &bHotJournal);
41061
 
    }
41062
 
    if( rc!=SQLITE_OK ){
41063
 
      goto failed;
41064
 
    }
41065
 
    if( bHotJournal ){
41066
 
      /* Get an EXCLUSIVE lock on the database file. At this point it is
41067
 
      ** important that a RESERVED lock is not obtained on the way to the
41068
 
      ** EXCLUSIVE lock. If it were, another process might open the
41069
 
      ** database file, detect the RESERVED lock, and conclude that the
41070
 
      ** database is safe to read while this process is still rolling the 
41071
 
      ** hot-journal back.
41072
 
      ** 
41073
 
      ** Because the intermediate RESERVED lock is not requested, any
41074
 
      ** other process attempting to access the database file will get to 
41075
 
      ** this point in the code and fail to obtain its own EXCLUSIVE lock 
41076
 
      ** on the database file.
41077
 
      **
41078
 
      ** Unless the pager is in locking_mode=exclusive mode, the lock is
41079
 
      ** downgraded to SHARED_LOCK before this function returns.
41080
 
      */
41081
 
      rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
41082
 
      if( rc!=SQLITE_OK ){
41083
 
        goto failed;
41084
 
      }
41085
 
 
41086
 
      /* If it is not already open and the file exists on disk, open the 
41087
 
      ** journal for read/write access. Write access is required because 
41088
 
      ** in exclusive-access mode the file descriptor will be kept open 
41089
 
      ** and possibly used for a transaction later on. Also, write-access 
41090
 
      ** is usually required to finalize the journal in journal_mode=persist 
41091
 
      ** mode (and also for journal_mode=truncate on some systems).
41092
 
      **
41093
 
      ** If the journal does not exist, it usually means that some 
41094
 
      ** other connection managed to get in and roll it back before 
41095
 
      ** this connection obtained the exclusive lock above. Or, it 
41096
 
      ** may mean that the pager was in the error-state when this
41097
 
      ** function was called and the journal file does not exist.
41098
 
      */
41099
 
      if( !isOpen(pPager->jfd) ){
41100
 
        sqlite3_vfs * const pVfs = pPager->pVfs;
41101
 
        int bExists;              /* True if journal file exists */
41102
 
        rc = sqlite3OsAccess(
41103
 
            pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
41104
 
        if( rc==SQLITE_OK && bExists ){
41105
 
          int fout = 0;
41106
 
          int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
41107
 
          assert( !pPager->tempFile );
41108
 
          rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
41109
 
          assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
41110
 
          if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
41111
 
            rc = SQLITE_CANTOPEN_BKPT;
41112
 
            sqlite3OsClose(pPager->jfd);
41113
 
          }
41114
 
        }
41115
 
      }
41116
 
 
41117
 
      /* Playback and delete the journal.  Drop the database write
41118
 
      ** lock and reacquire the read lock. Purge the cache before
41119
 
      ** playing back the hot-journal so that we don't end up with
41120
 
      ** an inconsistent cache.  Sync the hot journal before playing
41121
 
      ** it back since the process that crashed and left the hot journal
41122
 
      ** probably did not sync it and we are required to always sync
41123
 
      ** the journal before playing it back.
41124
 
      */
41125
 
      if( isOpen(pPager->jfd) ){
41126
 
        assert( rc==SQLITE_OK );
41127
 
        rc = pagerSyncHotJournal(pPager);
41128
 
        if( rc==SQLITE_OK ){
41129
 
          rc = pager_playback(pPager, 1);
41130
 
          pPager->eState = PAGER_OPEN;
41131
 
        }
41132
 
      }else if( !pPager->exclusiveMode ){
41133
 
        pagerUnlockDb(pPager, SHARED_LOCK);
41134
 
      }
41135
 
 
41136
 
      if( rc!=SQLITE_OK ){
41137
 
        /* This branch is taken if an error occurs while trying to open
41138
 
        ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
41139
 
        ** pager_unlock() routine will be called before returning to unlock
41140
 
        ** the file. If the unlock attempt fails, then Pager.eLock must be
41141
 
        ** set to UNKNOWN_LOCK (see the comment above the #define for 
41142
 
        ** UNKNOWN_LOCK above for an explanation). 
41143
 
        **
41144
 
        ** In order to get pager_unlock() to do this, set Pager.eState to
41145
 
        ** PAGER_ERROR now. This is not actually counted as a transition
41146
 
        ** to ERROR state in the state diagram at the top of this file,
41147
 
        ** since we know that the same call to pager_unlock() will very
41148
 
        ** shortly transition the pager object to the OPEN state. Calling
41149
 
        ** assert_pager_state() would fail now, as it should not be possible
41150
 
        ** to be in ERROR state when there are zero outstanding page 
41151
 
        ** references.
41152
 
        */
41153
 
        pager_error(pPager, rc);
41154
 
        goto failed;
41155
 
      }
41156
 
 
41157
 
      assert( pPager->eState==PAGER_OPEN );
41158
 
      assert( (pPager->eLock==SHARED_LOCK)
41159
 
           || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
41160
 
      );
41161
 
    }
41162
 
 
41163
 
    if( !pPager->tempFile 
41164
 
     && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0) 
41165
 
    ){
41166
 
      /* The shared-lock has just been acquired on the database file
41167
 
      ** and there are already pages in the cache (from a previous
41168
 
      ** read or write transaction).  Check to see if the database
41169
 
      ** has been modified.  If the database has changed, flush the
41170
 
      ** cache.
41171
 
      **
41172
 
      ** Database changes is detected by looking at 15 bytes beginning
41173
 
      ** at offset 24 into the file.  The first 4 of these 16 bytes are
41174
 
      ** a 32-bit counter that is incremented with each change.  The
41175
 
      ** other bytes change randomly with each file change when
41176
 
      ** a codec is in use.
41177
 
      ** 
41178
 
      ** There is a vanishingly small chance that a change will not be 
41179
 
      ** detected.  The chance of an undetected change is so small that
41180
 
      ** it can be neglected.
41181
 
      */
41182
 
      Pgno nPage = 0;
41183
 
      char dbFileVers[sizeof(pPager->dbFileVers)];
41184
 
 
41185
 
      rc = pagerPagecount(pPager, &nPage);
41186
 
      if( rc ) goto failed;
41187
 
 
41188
 
      if( nPage>0 ){
41189
 
        IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
41190
 
        rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
41191
 
        if( rc!=SQLITE_OK ){
41192
 
          goto failed;
41193
 
        }
41194
 
      }else{
41195
 
        memset(dbFileVers, 0, sizeof(dbFileVers));
41196
 
      }
41197
 
 
41198
 
      if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
41199
 
        pager_reset(pPager);
41200
 
      }
41201
 
    }
41202
 
 
41203
 
    /* If there is a WAL file in the file-system, open this database in WAL
41204
 
    ** mode. Otherwise, the following function call is a no-op.
41205
 
    */
41206
 
    rc = pagerOpenWalIfPresent(pPager);
41207
 
#ifndef SQLITE_OMIT_WAL
41208
 
    assert( pPager->pWal==0 || rc==SQLITE_OK );
41209
 
#endif
41210
 
  }
41211
 
 
41212
 
  if( pagerUseWal(pPager) ){
41213
 
    assert( rc==SQLITE_OK );
41214
 
    rc = pagerBeginReadTransaction(pPager);
41215
 
  }
41216
 
 
41217
 
  if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
41218
 
    rc = pagerPagecount(pPager, &pPager->dbSize);
41219
 
  }
41220
 
 
41221
 
 failed:
41222
 
  if( rc!=SQLITE_OK ){
41223
 
    assert( !MEMDB );
41224
 
    pager_unlock(pPager);
41225
 
    assert( pPager->eState==PAGER_OPEN );
41226
 
  }else{
41227
 
    pPager->eState = PAGER_READER;
41228
 
  }
41229
 
  return rc;
41230
 
}
41231
 
 
41232
 
/*
41233
 
** If the reference count has reached zero, rollback any active
41234
 
** transaction and unlock the pager.
41235
 
**
41236
 
** Except, in locking_mode=EXCLUSIVE when there is nothing to in
41237
 
** the rollback journal, the unlock is not performed and there is
41238
 
** nothing to rollback, so this routine is a no-op.
41239
 
*/ 
41240
 
static void pagerUnlockIfUnused(Pager *pPager){
41241
 
  if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
41242
 
    pagerUnlockAndRollback(pPager);
41243
 
  }
41244
 
}
41245
 
 
41246
 
/*
41247
 
** Acquire a reference to page number pgno in pager pPager (a page
41248
 
** reference has type DbPage*). If the requested reference is 
41249
 
** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
41250
 
**
41251
 
** If the requested page is already in the cache, it is returned. 
41252
 
** Otherwise, a new page object is allocated and populated with data
41253
 
** read from the database file. In some cases, the pcache module may
41254
 
** choose not to allocate a new page object and may reuse an existing
41255
 
** object with no outstanding references.
41256
 
**
41257
 
** The extra data appended to a page is always initialized to zeros the 
41258
 
** first time a page is loaded into memory. If the page requested is 
41259
 
** already in the cache when this function is called, then the extra
41260
 
** data is left as it was when the page object was last used.
41261
 
**
41262
 
** If the database image is smaller than the requested page or if a 
41263
 
** non-zero value is passed as the noContent parameter and the 
41264
 
** requested page is not already stored in the cache, then no 
41265
 
** actual disk read occurs. In this case the memory image of the 
41266
 
** page is initialized to all zeros. 
41267
 
**
41268
 
** If noContent is true, it means that we do not care about the contents
41269
 
** of the page. This occurs in two seperate scenarios:
41270
 
**
41271
 
**   a) When reading a free-list leaf page from the database, and
41272
 
**
41273
 
**   b) When a savepoint is being rolled back and we need to load
41274
 
**      a new page into the cache to be filled with the data read
41275
 
**      from the savepoint journal.
41276
 
**
41277
 
** If noContent is true, then the data returned is zeroed instead of
41278
 
** being read from the database. Additionally, the bits corresponding
41279
 
** to pgno in Pager.pInJournal (bitvec of pages already written to the
41280
 
** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
41281
 
** savepoints are set. This means if the page is made writable at any
41282
 
** point in the future, using a call to sqlite3PagerWrite(), its contents
41283
 
** will not be journaled. This saves IO.
41284
 
**
41285
 
** The acquisition might fail for several reasons.  In all cases,
41286
 
** an appropriate error code is returned and *ppPage is set to NULL.
41287
 
**
41288
 
** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
41289
 
** to find a page in the in-memory cache first.  If the page is not already
41290
 
** in memory, this routine goes to disk to read it in whereas Lookup()
41291
 
** just returns 0.  This routine acquires a read-lock the first time it
41292
 
** has to go to disk, and could also playback an old journal if necessary.
41293
 
** Since Lookup() never goes to disk, it never has to deal with locks
41294
 
** or journal files.
41295
 
*/
41296
 
SQLITE_PRIVATE int sqlite3PagerAcquire(
41297
 
  Pager *pPager,      /* The pager open on the database file */
41298
 
  Pgno pgno,          /* Page number to fetch */
41299
 
  DbPage **ppPage,    /* Write a pointer to the page here */
41300
 
  int noContent       /* Do not bother reading content from disk if true */
41301
 
){
41302
 
  int rc;
41303
 
  PgHdr *pPg;
41304
 
 
41305
 
  assert( pPager->eState>=PAGER_READER );
41306
 
  assert( assert_pager_state(pPager) );
41307
 
 
41308
 
  if( pgno==0 ){
41309
 
    return SQLITE_CORRUPT_BKPT;
41310
 
  }
41311
 
 
41312
 
  /* If the pager is in the error state, return an error immediately. 
41313
 
  ** Otherwise, request the page from the PCache layer. */
41314
 
  if( pPager->errCode!=SQLITE_OK ){
41315
 
    rc = pPager->errCode;
41316
 
  }else{
41317
 
    rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
41318
 
  }
41319
 
 
41320
 
  if( rc!=SQLITE_OK ){
41321
 
    /* Either the call to sqlite3PcacheFetch() returned an error or the
41322
 
    ** pager was already in the error-state when this function was called.
41323
 
    ** Set pPg to 0 and jump to the exception handler.  */
41324
 
    pPg = 0;
41325
 
    goto pager_acquire_err;
41326
 
  }
41327
 
  assert( (*ppPage)->pgno==pgno );
41328
 
  assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
41329
 
 
41330
 
  if( (*ppPage)->pPager && !noContent ){
41331
 
    /* In this case the pcache already contains an initialized copy of
41332
 
    ** the page. Return without further ado.  */
41333
 
    assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
41334
 
    PAGER_INCR(pPager->nHit);
41335
 
    return SQLITE_OK;
41336
 
 
41337
 
  }else{
41338
 
    /* The pager cache has created a new page. Its content needs to 
41339
 
    ** be initialized.  */
41340
 
 
41341
 
    PAGER_INCR(pPager->nMiss);
41342
 
    pPg = *ppPage;
41343
 
    pPg->pPager = pPager;
41344
 
 
41345
 
    /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
41346
 
    ** number greater than this, or the unused locking-page, is requested. */
41347
 
    if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
41348
 
      rc = SQLITE_CORRUPT_BKPT;
41349
 
      goto pager_acquire_err;
41350
 
    }
41351
 
 
41352
 
    if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
41353
 
      if( pgno>pPager->mxPgno ){
41354
 
        rc = SQLITE_FULL;
41355
 
        goto pager_acquire_err;
41356
 
      }
41357
 
      if( noContent ){
41358
 
        /* Failure to set the bits in the InJournal bit-vectors is benign.
41359
 
        ** It merely means that we might do some extra work to journal a 
41360
 
        ** page that does not need to be journaled.  Nevertheless, be sure 
41361
 
        ** to test the case where a malloc error occurs while trying to set 
41362
 
        ** a bit in a bit vector.
41363
 
        */
41364
 
        sqlite3BeginBenignMalloc();
41365
 
        if( pgno<=pPager->dbOrigSize ){
41366
 
          TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
41367
 
          testcase( rc==SQLITE_NOMEM );
41368
 
        }
41369
 
        TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
41370
 
        testcase( rc==SQLITE_NOMEM );
41371
 
        sqlite3EndBenignMalloc();
41372
 
      }
41373
 
      memset(pPg->pData, 0, pPager->pageSize);
41374
 
      IOTRACE(("ZERO %p %d\n", pPager, pgno));
41375
 
    }else{
41376
 
      assert( pPg->pPager==pPager );
41377
 
      rc = readDbPage(pPg);
41378
 
      if( rc!=SQLITE_OK ){
41379
 
        goto pager_acquire_err;
41380
 
      }
41381
 
    }
41382
 
    pager_set_pagehash(pPg);
41383
 
  }
41384
 
 
41385
 
  return SQLITE_OK;
41386
 
 
41387
 
pager_acquire_err:
41388
 
  assert( rc!=SQLITE_OK );
41389
 
  if( pPg ){
41390
 
    sqlite3PcacheDrop(pPg);
41391
 
  }
41392
 
  pagerUnlockIfUnused(pPager);
41393
 
 
41394
 
  *ppPage = 0;
41395
 
  return rc;
41396
 
}
41397
 
 
41398
 
/*
41399
 
** Acquire a page if it is already in the in-memory cache.  Do
41400
 
** not read the page from disk.  Return a pointer to the page,
41401
 
** or 0 if the page is not in cache. 
41402
 
**
41403
 
** See also sqlite3PagerGet().  The difference between this routine
41404
 
** and sqlite3PagerGet() is that _get() will go to the disk and read
41405
 
** in the page if the page is not already in cache.  This routine
41406
 
** returns NULL if the page is not in cache or if a disk I/O error 
41407
 
** has ever happened.
41408
 
*/
41409
 
SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
41410
 
  PgHdr *pPg = 0;
41411
 
  assert( pPager!=0 );
41412
 
  assert( pgno!=0 );
41413
 
  assert( pPager->pPCache!=0 );
41414
 
  assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
41415
 
  sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
41416
 
  return pPg;
41417
 
}
41418
 
 
41419
 
/*
41420
 
** Release a page reference.
41421
 
**
41422
 
** If the number of references to the page drop to zero, then the
41423
 
** page is added to the LRU list.  When all references to all pages
41424
 
** are released, a rollback occurs and the lock on the database is
41425
 
** removed.
41426
 
*/
41427
 
SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
41428
 
  if( pPg ){
41429
 
    Pager *pPager = pPg->pPager;
41430
 
    sqlite3PcacheRelease(pPg);
41431
 
    pagerUnlockIfUnused(pPager);
41432
 
  }
41433
 
}
41434
 
 
41435
 
/*
41436
 
** This function is called at the start of every write transaction.
41437
 
** There must already be a RESERVED or EXCLUSIVE lock on the database 
41438
 
** file when this routine is called.
41439
 
**
41440
 
** Open the journal file for pager pPager and write a journal header
41441
 
** to the start of it. If there are active savepoints, open the sub-journal
41442
 
** as well. This function is only used when the journal file is being 
41443
 
** opened to write a rollback log for a transaction. It is not used 
41444
 
** when opening a hot journal file to roll it back.
41445
 
**
41446
 
** If the journal file is already open (as it may be in exclusive mode),
41447
 
** then this function just writes a journal header to the start of the
41448
 
** already open file. 
41449
 
**
41450
 
** Whether or not the journal file is opened by this function, the
41451
 
** Pager.pInJournal bitvec structure is allocated.
41452
 
**
41453
 
** Return SQLITE_OK if everything is successful. Otherwise, return 
41454
 
** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or 
41455
 
** an IO error code if opening or writing the journal file fails.
41456
 
*/
41457
 
static int pager_open_journal(Pager *pPager){
41458
 
  int rc = SQLITE_OK;                        /* Return code */
41459
 
  sqlite3_vfs * const pVfs = pPager->pVfs;   /* Local cache of vfs pointer */
41460
 
 
41461
 
  assert( pPager->eState==PAGER_WRITER_LOCKED );
41462
 
  assert( assert_pager_state(pPager) );
41463
 
  assert( pPager->pInJournal==0 );
41464
 
  
41465
 
  /* If already in the error state, this function is a no-op.  But on
41466
 
  ** the other hand, this routine is never called if we are already in
41467
 
  ** an error state. */
41468
 
  if( NEVER(pPager->errCode) ) return pPager->errCode;
41469
 
 
41470
 
  if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
41471
 
    pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
41472
 
    if( pPager->pInJournal==0 ){
41473
 
      return SQLITE_NOMEM;
41474
 
    }
41475
 
  
41476
 
    /* Open the journal file if it is not already open. */
41477
 
    if( !isOpen(pPager->jfd) ){
41478
 
      if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
41479
 
        sqlite3MemJournalOpen(pPager->jfd);
41480
 
      }else{
41481
 
        const int flags =                   /* VFS flags to open journal file */
41482
 
          SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
41483
 
          (pPager->tempFile ? 
41484
 
            (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
41485
 
            (SQLITE_OPEN_MAIN_JOURNAL)
41486
 
          );
41487
 
  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
41488
 
        rc = sqlite3JournalOpen(
41489
 
            pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
41490
 
        );
41491
 
  #else
41492
 
        rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
41493
 
  #endif
41494
 
      }
41495
 
      assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
41496
 
    }
41497
 
  
41498
 
  
41499
 
    /* Write the first journal header to the journal file and open 
41500
 
    ** the sub-journal if necessary.
41501
 
    */
41502
 
    if( rc==SQLITE_OK ){
41503
 
      /* TODO: Check if all of these are really required. */
41504
 
      pPager->nRec = 0;
41505
 
      pPager->journalOff = 0;
41506
 
      pPager->setMaster = 0;
41507
 
      pPager->journalHdr = 0;
41508
 
      rc = writeJournalHdr(pPager);
41509
 
    }
41510
 
  }
41511
 
 
41512
 
  if( rc!=SQLITE_OK ){
41513
 
    sqlite3BitvecDestroy(pPager->pInJournal);
41514
 
    pPager->pInJournal = 0;
41515
 
  }else{
41516
 
    assert( pPager->eState==PAGER_WRITER_LOCKED );
41517
 
    pPager->eState = PAGER_WRITER_CACHEMOD;
41518
 
  }
41519
 
 
41520
 
  return rc;
41521
 
}
41522
 
 
41523
 
/*
41524
 
** Begin a write-transaction on the specified pager object. If a 
41525
 
** write-transaction has already been opened, this function is a no-op.
41526
 
**
41527
 
** If the exFlag argument is false, then acquire at least a RESERVED
41528
 
** lock on the database file. If exFlag is true, then acquire at least
41529
 
** an EXCLUSIVE lock. If such a lock is already held, no locking 
41530
 
** functions need be called.
41531
 
**
41532
 
** If the subjInMemory argument is non-zero, then any sub-journal opened
41533
 
** within this transaction will be opened as an in-memory file. This
41534
 
** has no effect if the sub-journal is already opened (as it may be when
41535
 
** running in exclusive mode) or if the transaction does not require a
41536
 
** sub-journal. If the subjInMemory argument is zero, then any required
41537
 
** sub-journal is implemented in-memory if pPager is an in-memory database, 
41538
 
** or using a temporary file otherwise.
41539
 
*/
41540
 
SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
41541
 
  int rc = SQLITE_OK;
41542
 
 
41543
 
  if( pPager->errCode ) return pPager->errCode;
41544
 
  assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
41545
 
  pPager->subjInMemory = (u8)subjInMemory;
41546
 
 
41547
 
  if( ALWAYS(pPager->eState==PAGER_READER) ){
41548
 
    assert( pPager->pInJournal==0 );
41549
 
 
41550
 
    if( pagerUseWal(pPager) ){
41551
 
      /* If the pager is configured to use locking_mode=exclusive, and an
41552
 
      ** exclusive lock on the database is not already held, obtain it now.
41553
 
      */
41554
 
      if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
41555
 
        rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
41556
 
        if( rc!=SQLITE_OK ){
41557
 
          return rc;
41558
 
        }
41559
 
        sqlite3WalExclusiveMode(pPager->pWal, 1);
41560
 
      }
41561
 
 
41562
 
      /* Grab the write lock on the log file. If successful, upgrade to
41563
 
      ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
41564
 
      ** The busy-handler is not invoked if another connection already
41565
 
      ** holds the write-lock. If possible, the upper layer will call it.
41566
 
      */
41567
 
      rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
41568
 
    }else{
41569
 
      /* Obtain a RESERVED lock on the database file. If the exFlag parameter
41570
 
      ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
41571
 
      ** busy-handler callback can be used when upgrading to the EXCLUSIVE
41572
 
      ** lock, but not when obtaining the RESERVED lock.
41573
 
      */
41574
 
      rc = pagerLockDb(pPager, RESERVED_LOCK);
41575
 
      if( rc==SQLITE_OK && exFlag ){
41576
 
        rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
41577
 
      }
41578
 
    }
41579
 
 
41580
 
    if( rc==SQLITE_OK ){
41581
 
      /* Change to WRITER_LOCKED state.
41582
 
      **
41583
 
      ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
41584
 
      ** when it has an open transaction, but never to DBMOD or FINISHED.
41585
 
      ** This is because in those states the code to roll back savepoint 
41586
 
      ** transactions may copy data from the sub-journal into the database 
41587
 
      ** file as well as into the page cache. Which would be incorrect in 
41588
 
      ** WAL mode.
41589
 
      */
41590
 
      pPager->eState = PAGER_WRITER_LOCKED;
41591
 
      pPager->dbHintSize = pPager->dbSize;
41592
 
      pPager->dbFileSize = pPager->dbSize;
41593
 
      pPager->dbOrigSize = pPager->dbSize;
41594
 
      pPager->journalOff = 0;
41595
 
    }
41596
 
 
41597
 
    assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
41598
 
    assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
41599
 
    assert( assert_pager_state(pPager) );
41600
 
  }
41601
 
 
41602
 
  PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
41603
 
  return rc;
41604
 
}
41605
 
 
41606
 
/*
41607
 
** Mark a single data page as writeable. The page is written into the 
41608
 
** main journal or sub-journal as required. If the page is written into
41609
 
** one of the journals, the corresponding bit is set in the 
41610
 
** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
41611
 
** of any open savepoints as appropriate.
41612
 
*/
41613
 
static int pager_write(PgHdr *pPg){
41614
 
  void *pData = pPg->pData;
41615
 
  Pager *pPager = pPg->pPager;
41616
 
  int rc = SQLITE_OK;
41617
 
 
41618
 
  /* This routine is not called unless a write-transaction has already 
41619
 
  ** been started. The journal file may or may not be open at this point.
41620
 
  ** It is never called in the ERROR state.
41621
 
  */
41622
 
  assert( pPager->eState==PAGER_WRITER_LOCKED
41623
 
       || pPager->eState==PAGER_WRITER_CACHEMOD
41624
 
       || pPager->eState==PAGER_WRITER_DBMOD
41625
 
  );
41626
 
  assert( assert_pager_state(pPager) );
41627
 
 
41628
 
  /* If an error has been previously detected, report the same error
41629
 
  ** again. This should not happen, but the check provides robustness. */
41630
 
  if( NEVER(pPager->errCode) )  return pPager->errCode;
41631
 
 
41632
 
  /* Higher-level routines never call this function if database is not
41633
 
  ** writable.  But check anyway, just for robustness. */
41634
 
  if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
41635
 
 
41636
 
  CHECK_PAGE(pPg);
41637
 
 
41638
 
  /* The journal file needs to be opened. Higher level routines have already
41639
 
  ** obtained the necessary locks to begin the write-transaction, but the
41640
 
  ** rollback journal might not yet be open. Open it now if this is the case.
41641
 
  **
41642
 
  ** This is done before calling sqlite3PcacheMakeDirty() on the page. 
41643
 
  ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
41644
 
  ** an error might occur and the pager would end up in WRITER_LOCKED state
41645
 
  ** with pages marked as dirty in the cache.
41646
 
  */
41647
 
  if( pPager->eState==PAGER_WRITER_LOCKED ){
41648
 
    rc = pager_open_journal(pPager);
41649
 
    if( rc!=SQLITE_OK ) return rc;
41650
 
  }
41651
 
  assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
41652
 
  assert( assert_pager_state(pPager) );
41653
 
 
41654
 
  /* Mark the page as dirty.  If the page has already been written
41655
 
  ** to the journal then we can return right away.
41656
 
  */
41657
 
  sqlite3PcacheMakeDirty(pPg);
41658
 
  if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
41659
 
    assert( !pagerUseWal(pPager) );
41660
 
  }else{
41661
 
  
41662
 
    /* The transaction journal now exists and we have a RESERVED or an
41663
 
    ** EXCLUSIVE lock on the main database file.  Write the current page to
41664
 
    ** the transaction journal if it is not there already.
41665
 
    */
41666
 
    if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
41667
 
      assert( pagerUseWal(pPager)==0 );
41668
 
      if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
41669
 
        u32 cksum;
41670
 
        char *pData2;
41671
 
        i64 iOff = pPager->journalOff;
41672
 
 
41673
 
        /* We should never write to the journal file the page that
41674
 
        ** contains the database locks.  The following assert verifies
41675
 
        ** that we do not. */
41676
 
        assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
41677
 
 
41678
 
        assert( pPager->journalHdr<=pPager->journalOff );
41679
 
        CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
41680
 
        cksum = pager_cksum(pPager, (u8*)pData2);
41681
 
 
41682
 
        /* Even if an IO or diskfull error occurs while journalling the
41683
 
        ** page in the block above, set the need-sync flag for the page.
41684
 
        ** Otherwise, when the transaction is rolled back, the logic in
41685
 
        ** playback_one_page() will think that the page needs to be restored
41686
 
        ** in the database file. And if an IO error occurs while doing so,
41687
 
        ** then corruption may follow.
41688
 
        */
41689
 
        pPg->flags |= PGHDR_NEED_SYNC;
41690
 
 
41691
 
        rc = write32bits(pPager->jfd, iOff, pPg->pgno);
41692
 
        if( rc!=SQLITE_OK ) return rc;
41693
 
        rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
41694
 
        if( rc!=SQLITE_OK ) return rc;
41695
 
        rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
41696
 
        if( rc!=SQLITE_OK ) return rc;
41697
 
 
41698
 
        IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, 
41699
 
                 pPager->journalOff, pPager->pageSize));
41700
 
        PAGER_INCR(sqlite3_pager_writej_count);
41701
 
        PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
41702
 
             PAGERID(pPager), pPg->pgno, 
41703
 
             ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
41704
 
 
41705
 
        pPager->journalOff += 8 + pPager->pageSize;
41706
 
        pPager->nRec++;
41707
 
        assert( pPager->pInJournal!=0 );
41708
 
        rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
41709
 
        testcase( rc==SQLITE_NOMEM );
41710
 
        assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
41711
 
        rc |= addToSavepointBitvecs(pPager, pPg->pgno);
41712
 
        if( rc!=SQLITE_OK ){
41713
 
          assert( rc==SQLITE_NOMEM );
41714
 
          return rc;
41715
 
        }
41716
 
      }else{
41717
 
        if( pPager->eState!=PAGER_WRITER_DBMOD ){
41718
 
          pPg->flags |= PGHDR_NEED_SYNC;
41719
 
        }
41720
 
        PAGERTRACE(("APPEND %d page %d needSync=%d\n",
41721
 
                PAGERID(pPager), pPg->pgno,
41722
 
               ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
41723
 
      }
41724
 
    }
41725
 
  
41726
 
    /* If the statement journal is open and the page is not in it,
41727
 
    ** then write the current page to the statement journal.  Note that
41728
 
    ** the statement journal format differs from the standard journal format
41729
 
    ** in that it omits the checksums and the header.
41730
 
    */
41731
 
    if( subjRequiresPage(pPg) ){
41732
 
      rc = subjournalPage(pPg);
41733
 
    }
41734
 
  }
41735
 
 
41736
 
  /* Update the database size and return.
41737
 
  */
41738
 
  if( pPager->dbSize<pPg->pgno ){
41739
 
    pPager->dbSize = pPg->pgno;
41740
 
  }
41741
 
  return rc;
41742
 
}
41743
 
 
41744
 
/*
41745
 
** Mark a data page as writeable. This routine must be called before 
41746
 
** making changes to a page. The caller must check the return value 
41747
 
** of this function and be careful not to change any page data unless 
41748
 
** this routine returns SQLITE_OK.
41749
 
**
41750
 
** The difference between this function and pager_write() is that this
41751
 
** function also deals with the special case where 2 or more pages
41752
 
** fit on a single disk sector. In this case all co-resident pages
41753
 
** must have been written to the journal file before returning.
41754
 
**
41755
 
** If an error occurs, SQLITE_NOMEM or an IO error code is returned
41756
 
** as appropriate. Otherwise, SQLITE_OK.
41757
 
*/
41758
 
SQLITE_PRIVATE int sqlite3PagerWrite(DbPage *pDbPage){
41759
 
  int rc = SQLITE_OK;
41760
 
 
41761
 
  PgHdr *pPg = pDbPage;
41762
 
  Pager *pPager = pPg->pPager;
41763
 
  Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
41764
 
 
41765
 
  assert( pPager->eState>=PAGER_WRITER_LOCKED );
41766
 
  assert( pPager->eState!=PAGER_ERROR );
41767
 
  assert( assert_pager_state(pPager) );
41768
 
 
41769
 
  if( nPagePerSector>1 ){
41770
 
    Pgno nPageCount;          /* Total number of pages in database file */
41771
 
    Pgno pg1;                 /* First page of the sector pPg is located on. */
41772
 
    int nPage = 0;            /* Number of pages starting at pg1 to journal */
41773
 
    int ii;                   /* Loop counter */
41774
 
    int needSync = 0;         /* True if any page has PGHDR_NEED_SYNC */
41775
 
 
41776
 
    /* Set the doNotSyncSpill flag to 1. This is because we cannot allow
41777
 
    ** a journal header to be written between the pages journaled by
41778
 
    ** this function.
41779
 
    */
41780
 
    assert( !MEMDB );
41781
 
    assert( pPager->doNotSyncSpill==0 );
41782
 
    pPager->doNotSyncSpill++;
41783
 
 
41784
 
    /* This trick assumes that both the page-size and sector-size are
41785
 
    ** an integer power of 2. It sets variable pg1 to the identifier
41786
 
    ** of the first page of the sector pPg is located on.
41787
 
    */
41788
 
    pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
41789
 
 
41790
 
    nPageCount = pPager->dbSize;
41791
 
    if( pPg->pgno>nPageCount ){
41792
 
      nPage = (pPg->pgno - pg1)+1;
41793
 
    }else if( (pg1+nPagePerSector-1)>nPageCount ){
41794
 
      nPage = nPageCount+1-pg1;
41795
 
    }else{
41796
 
      nPage = nPagePerSector;
41797
 
    }
41798
 
    assert(nPage>0);
41799
 
    assert(pg1<=pPg->pgno);
41800
 
    assert((pg1+nPage)>pPg->pgno);
41801
 
 
41802
 
    for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
41803
 
      Pgno pg = pg1+ii;
41804
 
      PgHdr *pPage;
41805
 
      if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
41806
 
        if( pg!=PAGER_MJ_PGNO(pPager) ){
41807
 
          rc = sqlite3PagerGet(pPager, pg, &pPage);
41808
 
          if( rc==SQLITE_OK ){
41809
 
            rc = pager_write(pPage);
41810
 
            if( pPage->flags&PGHDR_NEED_SYNC ){
41811
 
              needSync = 1;
41812
 
            }
41813
 
            sqlite3PagerUnref(pPage);
41814
 
          }
41815
 
        }
41816
 
      }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
41817
 
        if( pPage->flags&PGHDR_NEED_SYNC ){
41818
 
          needSync = 1;
41819
 
        }
41820
 
        sqlite3PagerUnref(pPage);
41821
 
      }
41822
 
    }
41823
 
 
41824
 
    /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages 
41825
 
    ** starting at pg1, then it needs to be set for all of them. Because
41826
 
    ** writing to any of these nPage pages may damage the others, the
41827
 
    ** journal file must contain sync()ed copies of all of them
41828
 
    ** before any of them can be written out to the database file.
41829
 
    */
41830
 
    if( rc==SQLITE_OK && needSync ){
41831
 
      assert( !MEMDB );
41832
 
      for(ii=0; ii<nPage; ii++){
41833
 
        PgHdr *pPage = pager_lookup(pPager, pg1+ii);
41834
 
        if( pPage ){
41835
 
          pPage->flags |= PGHDR_NEED_SYNC;
41836
 
          sqlite3PagerUnref(pPage);
41837
 
        }
41838
 
      }
41839
 
    }
41840
 
 
41841
 
    assert( pPager->doNotSyncSpill==1 );
41842
 
    pPager->doNotSyncSpill--;
41843
 
  }else{
41844
 
    rc = pager_write(pDbPage);
41845
 
  }
41846
 
  return rc;
41847
 
}
41848
 
 
41849
 
/*
41850
 
** Return TRUE if the page given in the argument was previously passed
41851
 
** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
41852
 
** to change the content of the page.
41853
 
*/
41854
 
#ifndef NDEBUG
41855
 
SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
41856
 
  return pPg->flags&PGHDR_DIRTY;
41857
 
}
41858
 
#endif
41859
 
 
41860
 
/*
41861
 
** A call to this routine tells the pager that it is not necessary to
41862
 
** write the information on page pPg back to the disk, even though
41863
 
** that page might be marked as dirty.  This happens, for example, when
41864
 
** the page has been added as a leaf of the freelist and so its
41865
 
** content no longer matters.
41866
 
**
41867
 
** The overlying software layer calls this routine when all of the data
41868
 
** on the given page is unused. The pager marks the page as clean so
41869
 
** that it does not get written to disk.
41870
 
**
41871
 
** Tests show that this optimization can quadruple the speed of large 
41872
 
** DELETE operations.
41873
 
*/
41874
 
SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
41875
 
  Pager *pPager = pPg->pPager;
41876
 
  if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
41877
 
    PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
41878
 
    IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
41879
 
    pPg->flags |= PGHDR_DONT_WRITE;
41880
 
    pager_set_pagehash(pPg);
41881
 
  }
41882
 
}
41883
 
 
41884
 
/*
41885
 
** This routine is called to increment the value of the database file 
41886
 
** change-counter, stored as a 4-byte big-endian integer starting at 
41887
 
** byte offset 24 of the pager file.  The secondary change counter at
41888
 
** 92 is also updated, as is the SQLite version number at offset 96.
41889
 
**
41890
 
** But this only happens if the pPager->changeCountDone flag is false.
41891
 
** To avoid excess churning of page 1, the update only happens once.
41892
 
** See also the pager_write_changecounter() routine that does an 
41893
 
** unconditional update of the change counters.
41894
 
**
41895
 
** If the isDirectMode flag is zero, then this is done by calling 
41896
 
** sqlite3PagerWrite() on page 1, then modifying the contents of the
41897
 
** page data. In this case the file will be updated when the current
41898
 
** transaction is committed.
41899
 
**
41900
 
** The isDirectMode flag may only be non-zero if the library was compiled
41901
 
** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
41902
 
** if isDirect is non-zero, then the database file is updated directly
41903
 
** by writing an updated version of page 1 using a call to the 
41904
 
** sqlite3OsWrite() function.
41905
 
*/
41906
 
static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
41907
 
  int rc = SQLITE_OK;
41908
 
 
41909
 
  assert( pPager->eState==PAGER_WRITER_CACHEMOD
41910
 
       || pPager->eState==PAGER_WRITER_DBMOD
41911
 
  );
41912
 
  assert( assert_pager_state(pPager) );
41913
 
 
41914
 
  /* Declare and initialize constant integer 'isDirect'. If the
41915
 
  ** atomic-write optimization is enabled in this build, then isDirect
41916
 
  ** is initialized to the value passed as the isDirectMode parameter
41917
 
  ** to this function. Otherwise, it is always set to zero.
41918
 
  **
41919
 
  ** The idea is that if the atomic-write optimization is not
41920
 
  ** enabled at compile time, the compiler can omit the tests of
41921
 
  ** 'isDirect' below, as well as the block enclosed in the
41922
 
  ** "if( isDirect )" condition.
41923
 
  */
41924
 
#ifndef SQLITE_ENABLE_ATOMIC_WRITE
41925
 
# define DIRECT_MODE 0
41926
 
  assert( isDirectMode==0 );
41927
 
  UNUSED_PARAMETER(isDirectMode);
41928
 
#else
41929
 
# define DIRECT_MODE isDirectMode
41930
 
#endif
41931
 
 
41932
 
  if( !pPager->changeCountDone && pPager->dbSize>0 ){
41933
 
    PgHdr *pPgHdr;                /* Reference to page 1 */
41934
 
 
41935
 
    assert( !pPager->tempFile && isOpen(pPager->fd) );
41936
 
 
41937
 
    /* Open page 1 of the file for writing. */
41938
 
    rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
41939
 
    assert( pPgHdr==0 || rc==SQLITE_OK );
41940
 
 
41941
 
    /* If page one was fetched successfully, and this function is not
41942
 
    ** operating in direct-mode, make page 1 writable.  When not in 
41943
 
    ** direct mode, page 1 is always held in cache and hence the PagerGet()
41944
 
    ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
41945
 
    */
41946
 
#if( !DIRECT_MODE)
41947
 
         if(ALWAYS(rc==SQLITE_OK) ){
41948
 
      rc = sqlite3PagerWrite(pPgHdr);
41949
 
    }
41950
 
#endif
41951
 
 
41952
 
    if( rc==SQLITE_OK ){
41953
 
      /* Actually do the update of the change counter */
41954
 
      pager_write_changecounter(pPgHdr);
41955
 
 
41956
 
      /* If running in direct mode, write the contents of page 1 to the file. */
41957
 
                #if( DIRECT_MODE )
41958
 
        const void *zBuf;
41959
 
        assert( pPager->dbFileSize>0 );
41960
 
        CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
41961
 
        if( rc==SQLITE_OK ){
41962
 
          rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
41963
 
        }
41964
 
        if( rc==SQLITE_OK ){
41965
 
          pPager->changeCountDone = 1;
41966
 
        }
41967
 
                #else
41968
 
        pPager->changeCountDone = 1;
41969
 
                #endif
41970
 
    }
41971
 
 
41972
 
    /* Release the page reference. */
41973
 
    sqlite3PagerUnref(pPgHdr);
41974
 
  }
41975
 
  return rc;
41976
 
}
41977
 
 
41978
 
/*
41979
 
** Sync the database file to disk. This is a no-op for in-memory databases
41980
 
** or pages with the Pager.noSync flag set.
41981
 
**
41982
 
** If successful, or if called on a pager for which it is a no-op, this
41983
 
** function returns SQLITE_OK. Otherwise, an IO error code is returned.
41984
 
*/
41985
 
SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager){
41986
 
  int rc = SQLITE_OK;
41987
 
  if( !pPager->noSync ){
41988
 
    assert( !MEMDB );
41989
 
    rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
41990
 
  }else if( isOpen(pPager->fd) ){
41991
 
    assert( !MEMDB );
41992
 
    sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, (void *)&rc);
41993
 
  }
41994
 
  return rc;
41995
 
}
41996
 
 
41997
 
/*
41998
 
** This function may only be called while a write-transaction is active in
41999
 
** rollback. If the connection is in WAL mode, this call is a no-op. 
42000
 
** Otherwise, if the connection does not already have an EXCLUSIVE lock on 
42001
 
** the database file, an attempt is made to obtain one.
42002
 
**
42003
 
** If the EXCLUSIVE lock is already held or the attempt to obtain it is
42004
 
** successful, or the connection is in WAL mode, SQLITE_OK is returned.
42005
 
** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is 
42006
 
** returned.
42007
 
*/
42008
 
SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
42009
 
  int rc = SQLITE_OK;
42010
 
  assert( pPager->eState==PAGER_WRITER_CACHEMOD 
42011
 
       || pPager->eState==PAGER_WRITER_DBMOD 
42012
 
       || pPager->eState==PAGER_WRITER_LOCKED 
42013
 
  );
42014
 
  assert( assert_pager_state(pPager) );
42015
 
  if( 0==pagerUseWal(pPager) ){
42016
 
    rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
42017
 
  }
42018
 
  return rc;
42019
 
}
42020
 
 
42021
 
/*
42022
 
** Sync the database file for the pager pPager. zMaster points to the name
42023
 
** of a master journal file that should be written into the individual
42024
 
** journal file. zMaster may be NULL, which is interpreted as no master
42025
 
** journal (a single database transaction).
42026
 
**
42027
 
** This routine ensures that:
42028
 
**
42029
 
**   * The database file change-counter is updated,
42030
 
**   * the journal is synced (unless the atomic-write optimization is used),
42031
 
**   * all dirty pages are written to the database file, 
42032
 
**   * the database file is truncated (if required), and
42033
 
**   * the database file synced. 
42034
 
**
42035
 
** The only thing that remains to commit the transaction is to finalize 
42036
 
** (delete, truncate or zero the first part of) the journal file (or 
42037
 
** delete the master journal file if specified).
42038
 
**
42039
 
** Note that if zMaster==NULL, this does not overwrite a previous value
42040
 
** passed to an sqlite3PagerCommitPhaseOne() call.
42041
 
**
42042
 
** If the final parameter - noSync - is true, then the database file itself
42043
 
** is not synced. The caller must call sqlite3PagerSync() directly to
42044
 
** sync the database file before calling CommitPhaseTwo() to delete the
42045
 
** journal file in this case.
42046
 
*/
42047
 
SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
42048
 
  Pager *pPager,                  /* Pager object */
42049
 
  const char *zMaster,            /* If not NULL, the master journal name */
42050
 
  int noSync                      /* True to omit the xSync on the db file */
42051
 
){
42052
 
  int rc = SQLITE_OK;             /* Return code */
42053
 
 
42054
 
  assert( pPager->eState==PAGER_WRITER_LOCKED
42055
 
       || pPager->eState==PAGER_WRITER_CACHEMOD
42056
 
       || pPager->eState==PAGER_WRITER_DBMOD
42057
 
       || pPager->eState==PAGER_ERROR
42058
 
  );
42059
 
  assert( assert_pager_state(pPager) );
42060
 
 
42061
 
  /* If a prior error occurred, report that error again. */
42062
 
  if( NEVER(pPager->errCode) ) return pPager->errCode;
42063
 
 
42064
 
  PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", 
42065
 
      pPager->zFilename, zMaster, pPager->dbSize));
42066
 
 
42067
 
  /* If no database changes have been made, return early. */
42068
 
  if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
42069
 
 
42070
 
  if( MEMDB ){
42071
 
    /* If this is an in-memory db, or no pages have been written to, or this
42072
 
    ** function has already been called, it is mostly a no-op.  However, any
42073
 
    ** backup in progress needs to be restarted.
42074
 
    */
42075
 
    sqlite3BackupRestart(pPager->pBackup);
42076
 
  }else{
42077
 
    if( pagerUseWal(pPager) ){
42078
 
      PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
42079
 
      if( pList ){
42080
 
        rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1, 
42081
 
            (pPager->fullSync ? pPager->syncFlags : 0)
42082
 
        );
42083
 
      }
42084
 
      if( rc==SQLITE_OK ){
42085
 
        sqlite3PcacheCleanAll(pPager->pPCache);
42086
 
      }
42087
 
    }else{
42088
 
      /* The following block updates the change-counter. Exactly how it
42089
 
      ** does this depends on whether or not the atomic-update optimization
42090
 
      ** was enabled at compile time, and if this transaction meets the 
42091
 
      ** runtime criteria to use the operation: 
42092
 
      **
42093
 
      **    * The file-system supports the atomic-write property for
42094
 
      **      blocks of size page-size, and 
42095
 
      **    * This commit is not part of a multi-file transaction, and
42096
 
      **    * Exactly one page has been modified and store in the journal file.
42097
 
      **
42098
 
      ** If the optimization was not enabled at compile time, then the
42099
 
      ** pager_incr_changecounter() function is called to update the change
42100
 
      ** counter in 'indirect-mode'. If the optimization is compiled in but
42101
 
      ** is not applicable to this transaction, call sqlite3JournalCreate()
42102
 
      ** to make sure the journal file has actually been created, then call
42103
 
      ** pager_incr_changecounter() to update the change-counter in indirect
42104
 
      ** mode. 
42105
 
      **
42106
 
      ** Otherwise, if the optimization is both enabled and applicable,
42107
 
      ** then call pager_incr_changecounter() to update the change-counter
42108
 
      ** in 'direct' mode. In this case the journal file will never be
42109
 
      ** created for this transaction.
42110
 
      */
42111
 
  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
42112
 
      PgHdr *pPg;
42113
 
      assert( isOpen(pPager->jfd) 
42114
 
           || pPager->journalMode==PAGER_JOURNALMODE_OFF 
42115
 
           || pPager->journalMode==PAGER_JOURNALMODE_WAL 
42116
 
      );
42117
 
      if( !zMaster && isOpen(pPager->jfd) 
42118
 
       && pPager->journalOff==jrnlBufferSize(pPager) 
42119
 
       && pPager->dbSize>=pPager->dbOrigSize
42120
 
       && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
42121
 
      ){
42122
 
        /* Update the db file change counter via the direct-write method. The 
42123
 
        ** following call will modify the in-memory representation of page 1 
42124
 
        ** to include the updated change counter and then write page 1 
42125
 
        ** directly to the database file. Because of the atomic-write 
42126
 
        ** property of the host file-system, this is safe.
42127
 
        */
42128
 
        rc = pager_incr_changecounter(pPager, 1);
42129
 
      }else{
42130
 
        rc = sqlite3JournalCreate(pPager->jfd);
42131
 
        if( rc==SQLITE_OK ){
42132
 
          rc = pager_incr_changecounter(pPager, 0);
42133
 
        }
42134
 
      }
42135
 
  #else
42136
 
      rc = pager_incr_changecounter(pPager, 0);
42137
 
  #endif
42138
 
      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
42139
 
  
42140
 
      /* If this transaction has made the database smaller, then all pages
42141
 
      ** being discarded by the truncation must be written to the journal
42142
 
      ** file. This can only happen in auto-vacuum mode.
42143
 
      **
42144
 
      ** Before reading the pages with page numbers larger than the 
42145
 
      ** current value of Pager.dbSize, set dbSize back to the value
42146
 
      ** that it took at the start of the transaction. Otherwise, the
42147
 
      ** calls to sqlite3PagerGet() return zeroed pages instead of 
42148
 
      ** reading data from the database file.
42149
 
      */
42150
 
  #ifndef SQLITE_OMIT_AUTOVACUUM
42151
 
      if( pPager->dbSize<pPager->dbOrigSize 
42152
 
       && pPager->journalMode!=PAGER_JOURNALMODE_OFF
42153
 
      ){
42154
 
        Pgno i;                                   /* Iterator variable */
42155
 
        const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
42156
 
        const Pgno dbSize = pPager->dbSize;       /* Database image size */ 
42157
 
        pPager->dbSize = pPager->dbOrigSize;
42158
 
        for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
42159
 
          if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
42160
 
            PgHdr *pPage;             /* Page to journal */
42161
 
            rc = sqlite3PagerGet(pPager, i, &pPage);
42162
 
            if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
42163
 
            rc = sqlite3PagerWrite(pPage);
42164
 
            sqlite3PagerUnref(pPage);
42165
 
            if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
42166
 
          }
42167
 
        }
42168
 
        pPager->dbSize = dbSize;
42169
 
      } 
42170
 
  #endif
42171
 
  
42172
 
      /* Write the master journal name into the journal file. If a master 
42173
 
      ** journal file name has already been written to the journal file, 
42174
 
      ** or if zMaster is NULL (no master journal), then this call is a no-op.
42175
 
      */
42176
 
      rc = writeMasterJournal(pPager, zMaster);
42177
 
      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
42178
 
  
42179
 
      /* Sync the journal file and write all dirty pages to the database.
42180
 
      ** If the atomic-update optimization is being used, this sync will not 
42181
 
      ** create the journal file or perform any real IO.
42182
 
      **
42183
 
      ** Because the change-counter page was just modified, unless the
42184
 
      ** atomic-update optimization is used it is almost certain that the
42185
 
      ** journal requires a sync here. However, in locking_mode=exclusive
42186
 
      ** on a system under memory pressure it is just possible that this is 
42187
 
      ** not the case. In this case it is likely enough that the redundant
42188
 
      ** xSync() call will be changed to a no-op by the OS anyhow. 
42189
 
      */
42190
 
      rc = syncJournal(pPager, 0);
42191
 
      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
42192
 
  
42193
 
      rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
42194
 
      if( rc!=SQLITE_OK ){
42195
 
        assert( rc!=SQLITE_IOERR_BLOCKED );
42196
 
        goto commit_phase_one_exit;
42197
 
      }
42198
 
      sqlite3PcacheCleanAll(pPager->pPCache);
42199
 
  
42200
 
      /* If the file on disk is not the same size as the database image,
42201
 
      ** then use pager_truncate to grow or shrink the file here.
42202
 
      */
42203
 
      if( pPager->dbSize!=pPager->dbFileSize ){
42204
 
        Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
42205
 
        assert( pPager->eState==PAGER_WRITER_DBMOD );
42206
 
        rc = pager_truncate(pPager, nNew);
42207
 
        if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
42208
 
      }
42209
 
  
42210
 
      /* Finally, sync the database file. */
42211
 
      if( !noSync ){
42212
 
        rc = sqlite3PagerSync(pPager);
42213
 
      }
42214
 
      IOTRACE(("DBSYNC %p\n", pPager))
42215
 
    }
42216
 
  }
42217
 
 
42218
 
commit_phase_one_exit:
42219
 
  if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
42220
 
    pPager->eState = PAGER_WRITER_FINISHED;
42221
 
  }
42222
 
  return rc;
42223
 
}
42224
 
 
42225
 
 
42226
 
/*
42227
 
** When this function is called, the database file has been completely
42228
 
** updated to reflect the changes made by the current transaction and
42229
 
** synced to disk. The journal file still exists in the file-system 
42230
 
** though, and if a failure occurs at this point it will eventually
42231
 
** be used as a hot-journal and the current transaction rolled back.
42232
 
**
42233
 
** This function finalizes the journal file, either by deleting, 
42234
 
** truncating or partially zeroing it, so that it cannot be used 
42235
 
** for hot-journal rollback. Once this is done the transaction is
42236
 
** irrevocably committed.
42237
 
**
42238
 
** If an error occurs, an IO error code is returned and the pager
42239
 
** moves into the error state. Otherwise, SQLITE_OK is returned.
42240
 
*/
42241
 
SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
42242
 
  int rc = SQLITE_OK;                  /* Return code */
42243
 
 
42244
 
  /* This routine should not be called if a prior error has occurred.
42245
 
  ** But if (due to a coding error elsewhere in the system) it does get
42246
 
  ** called, just return the same error code without doing anything. */
42247
 
  if( NEVER(pPager->errCode) ) return pPager->errCode;
42248
 
 
42249
 
  assert( pPager->eState==PAGER_WRITER_LOCKED
42250
 
       || pPager->eState==PAGER_WRITER_FINISHED
42251
 
       || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
42252
 
  );
42253
 
  assert( assert_pager_state(pPager) );
42254
 
 
42255
 
  /* An optimization. If the database was not actually modified during
42256
 
  ** this transaction, the pager is running in exclusive-mode and is
42257
 
  ** using persistent journals, then this function is a no-op.
42258
 
  **
42259
 
  ** The start of the journal file currently contains a single journal 
42260
 
  ** header with the nRec field set to 0. If such a journal is used as
42261
 
  ** a hot-journal during hot-journal rollback, 0 changes will be made
42262
 
  ** to the database file. So there is no need to zero the journal 
42263
 
  ** header. Since the pager is in exclusive mode, there is no need
42264
 
  ** to drop any locks either.
42265
 
  */
42266
 
  if( pPager->eState==PAGER_WRITER_LOCKED 
42267
 
   && pPager->exclusiveMode 
42268
 
   && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
42269
 
  ){
42270
 
    assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
42271
 
    pPager->eState = PAGER_READER;
42272
 
    return SQLITE_OK;
42273
 
  }
42274
 
 
42275
 
  PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
42276
 
  rc = pager_end_transaction(pPager, pPager->setMaster);
42277
 
  return pager_error(pPager, rc);
42278
 
}
42279
 
 
42280
 
/*
42281
 
** If a write transaction is open, then all changes made within the 
42282
 
** transaction are reverted and the current write-transaction is closed.
42283
 
** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
42284
 
** state if an error occurs.
42285
 
**
42286
 
** If the pager is already in PAGER_ERROR state when this function is called,
42287
 
** it returns Pager.errCode immediately. No work is performed in this case.
42288
 
**
42289
 
** Otherwise, in rollback mode, this function performs two functions:
42290
 
**
42291
 
**   1) It rolls back the journal file, restoring all database file and 
42292
 
**      in-memory cache pages to the state they were in when the transaction
42293
 
**      was opened, and
42294
 
**
42295
 
**   2) It finalizes the journal file, so that it is not used for hot
42296
 
**      rollback at any point in the future.
42297
 
**
42298
 
** Finalization of the journal file (task 2) is only performed if the 
42299
 
** rollback is successful.
42300
 
**
42301
 
** In WAL mode, all cache-entries containing data modified within the
42302
 
** current transaction are either expelled from the cache or reverted to
42303
 
** their pre-transaction state by re-reading data from the database or
42304
 
** WAL files. The WAL transaction is then closed.
42305
 
*/
42306
 
SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
42307
 
  int rc = SQLITE_OK;                  /* Return code */
42308
 
  PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
42309
 
 
42310
 
  /* PagerRollback() is a no-op if called in READER or OPEN state. If
42311
 
  ** the pager is already in the ERROR state, the rollback is not 
42312
 
  ** attempted here. Instead, the error code is returned to the caller.
42313
 
  */
42314
 
  assert( assert_pager_state(pPager) );
42315
 
  if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
42316
 
  if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
42317
 
 
42318
 
  if( pagerUseWal(pPager) ){
42319
 
    int rc2;
42320
 
    rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
42321
 
    rc2 = pager_end_transaction(pPager, pPager->setMaster);
42322
 
    if( rc==SQLITE_OK ) rc = rc2;
42323
 
  }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
42324
 
    int eState = pPager->eState;
42325
 
    rc = pager_end_transaction(pPager, 0);
42326
 
    if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
42327
 
      /* This can happen using journal_mode=off. Move the pager to the error 
42328
 
      ** state to indicate that the contents of the cache may not be trusted.
42329
 
      ** Any active readers will get SQLITE_ABORT.
42330
 
      */
42331
 
      pPager->errCode = SQLITE_ABORT;
42332
 
      pPager->eState = PAGER_ERROR;
42333
 
      return rc;
42334
 
    }
42335
 
  }else{
42336
 
    rc = pager_playback(pPager, 0);
42337
 
  }
42338
 
 
42339
 
  assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
42340
 
  assert( rc==SQLITE_OK || rc==SQLITE_FULL || (rc&0xFF)==SQLITE_IOERR );
42341
 
 
42342
 
  /* If an error occurs during a ROLLBACK, we can no longer trust the pager
42343
 
  ** cache. So call pager_error() on the way out to make any error persistent.
42344
 
  */
42345
 
  return pager_error(pPager, rc);
42346
 
}
42347
 
 
42348
 
/*
42349
 
** Return TRUE if the database file is opened read-only.  Return FALSE
42350
 
** if the database is (in theory) writable.
42351
 
*/
42352
 
SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
42353
 
  return pPager->readOnly;
42354
 
}
42355
 
 
42356
 
/*
42357
 
** Return the number of references to the pager.
42358
 
*/
42359
 
SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
42360
 
  return sqlite3PcacheRefCount(pPager->pPCache);
42361
 
}
42362
 
 
42363
 
/*
42364
 
** Return the approximate number of bytes of memory currently
42365
 
** used by the pager and its associated cache.
42366
 
*/
42367
 
SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
42368
 
  int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
42369
 
                                     + 5*sizeof(void*);
42370
 
  return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
42371
 
           + sqlite3MallocSize(pPager)
42372
 
           + pPager->pageSize;
42373
 
}
42374
 
 
42375
 
/*
42376
 
** Return the number of references to the specified page.
42377
 
*/
42378
 
SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
42379
 
  return sqlite3PcachePageRefcount(pPage);
42380
 
}
42381
 
 
42382
 
#ifdef SQLITE_TEST
42383
 
/*
42384
 
** This routine is used for testing and analysis only.
42385
 
*/
42386
 
SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
42387
 
  static int a[11];
42388
 
  a[0] = sqlite3PcacheRefCount(pPager->pPCache);
42389
 
  a[1] = sqlite3PcachePagecount(pPager->pPCache);
42390
 
  a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
42391
 
  a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
42392
 
  a[4] = pPager->eState;
42393
 
  a[5] = pPager->errCode;
42394
 
  a[6] = pPager->nHit;
42395
 
  a[7] = pPager->nMiss;
42396
 
  a[8] = 0;  /* Used to be pPager->nOvfl */
42397
 
  a[9] = pPager->nRead;
42398
 
  a[10] = pPager->nWrite;
42399
 
  return a;
42400
 
}
42401
 
#endif
42402
 
 
42403
 
/*
42404
 
** Return true if this is an in-memory pager.
42405
 
*/
42406
 
SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
42407
 
  return MEMDB;
42408
 
}
42409
 
 
42410
 
/*
42411
 
** Check that there are at least nSavepoint savepoints open. If there are
42412
 
** currently less than nSavepoints open, then open one or more savepoints
42413
 
** to make up the difference. If the number of savepoints is already
42414
 
** equal to nSavepoint, then this function is a no-op.
42415
 
**
42416
 
** If a memory allocation fails, SQLITE_NOMEM is returned. If an error 
42417
 
** occurs while opening the sub-journal file, then an IO error code is
42418
 
** returned. Otherwise, SQLITE_OK.
42419
 
*/
42420
 
SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
42421
 
  int rc = SQLITE_OK;                       /* Return code */
42422
 
  int nCurrent = pPager->nSavepoint;        /* Current number of savepoints */
42423
 
 
42424
 
  assert( pPager->eState>=PAGER_WRITER_LOCKED );
42425
 
  assert( assert_pager_state(pPager) );
42426
 
 
42427
 
  if( nSavepoint>nCurrent && pPager->useJournal ){
42428
 
    int ii;                                 /* Iterator variable */
42429
 
    PagerSavepoint *aNew;                   /* New Pager.aSavepoint array */
42430
 
 
42431
 
    /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
42432
 
    ** if the allocation fails. Otherwise, zero the new portion in case a 
42433
 
    ** malloc failure occurs while populating it in the for(...) loop below.
42434
 
    */
42435
 
    aNew = (PagerSavepoint *)sqlite3Realloc(
42436
 
        pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
42437
 
    );
42438
 
    if( !aNew ){
42439
 
      return SQLITE_NOMEM;
42440
 
    }
42441
 
    memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
42442
 
    pPager->aSavepoint = aNew;
42443
 
 
42444
 
    /* Populate the PagerSavepoint structures just allocated. */
42445
 
    for(ii=nCurrent; ii<nSavepoint; ii++){
42446
 
      aNew[ii].nOrig = pPager->dbSize;
42447
 
      if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
42448
 
        aNew[ii].iOffset = pPager->journalOff;
42449
 
      }else{
42450
 
        aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
42451
 
      }
42452
 
      aNew[ii].iSubRec = pPager->nSubRec;
42453
 
      aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
42454
 
      if( !aNew[ii].pInSavepoint ){
42455
 
        return SQLITE_NOMEM;
42456
 
      }
42457
 
      if( pagerUseWal(pPager) ){
42458
 
        sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
42459
 
      }
42460
 
      pPager->nSavepoint = ii+1;
42461
 
    }
42462
 
    assert( pPager->nSavepoint==nSavepoint );
42463
 
    assertTruncateConstraint(pPager);
42464
 
  }
42465
 
 
42466
 
  return rc;
42467
 
}
42468
 
 
42469
 
/*
42470
 
** This function is called to rollback or release (commit) a savepoint.
42471
 
** The savepoint to release or rollback need not be the most recently 
42472
 
** created savepoint.
42473
 
**
42474
 
** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
42475
 
** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
42476
 
** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
42477
 
** that have occurred since the specified savepoint was created.
42478
 
**
42479
 
** The savepoint to rollback or release is identified by parameter 
42480
 
** iSavepoint. A value of 0 means to operate on the outermost savepoint
42481
 
** (the first created). A value of (Pager.nSavepoint-1) means operate
42482
 
** on the most recently created savepoint. If iSavepoint is greater than
42483
 
** (Pager.nSavepoint-1), then this function is a no-op.
42484
 
**
42485
 
** If a negative value is passed to this function, then the current
42486
 
** transaction is rolled back. This is different to calling 
42487
 
** sqlite3PagerRollback() because this function does not terminate
42488
 
** the transaction or unlock the database, it just restores the 
42489
 
** contents of the database to its original state. 
42490
 
**
42491
 
** In any case, all savepoints with an index greater than iSavepoint 
42492
 
** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
42493
 
** then savepoint iSavepoint is also destroyed.
42494
 
**
42495
 
** This function may return SQLITE_NOMEM if a memory allocation fails,
42496
 
** or an IO error code if an IO error occurs while rolling back a 
42497
 
** savepoint. If no errors occur, SQLITE_OK is returned.
42498
 
*/ 
42499
 
SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
42500
 
  int rc = pPager->errCode;       /* Return code */
42501
 
 
42502
 
  assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
42503
 
  assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
42504
 
 
42505
 
  if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
42506
 
    int ii;            /* Iterator variable */
42507
 
    int nNew;          /* Number of remaining savepoints after this op. */
42508
 
 
42509
 
    /* Figure out how many savepoints will still be active after this
42510
 
    ** operation. Store this value in nNew. Then free resources associated 
42511
 
    ** with any savepoints that are destroyed by this operation.
42512
 
    */
42513
 
    nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
42514
 
    for(ii=nNew; ii<pPager->nSavepoint; ii++){
42515
 
      sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
42516
 
    }
42517
 
    pPager->nSavepoint = nNew;
42518
 
 
42519
 
    /* If this is a release of the outermost savepoint, truncate 
42520
 
    ** the sub-journal to zero bytes in size. */
42521
 
    if( op==SAVEPOINT_RELEASE ){
42522
 
      if( nNew==0 && isOpen(pPager->sjfd) ){
42523
 
        /* Only truncate if it is an in-memory sub-journal. */
42524
 
        if( sqlite3IsMemJournal(pPager->sjfd) ){
42525
 
          rc = sqlite3OsTruncate(pPager->sjfd, 0);
42526
 
          assert( rc==SQLITE_OK );
42527
 
        }
42528
 
        pPager->nSubRec = 0;
42529
 
      }
42530
 
    }
42531
 
    /* Else this is a rollback operation, playback the specified savepoint.
42532
 
    ** If this is a temp-file, it is possible that the journal file has
42533
 
    ** not yet been opened. In this case there have been no changes to
42534
 
    ** the database file, so the playback operation can be skipped.
42535
 
    */
42536
 
    else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
42537
 
      PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
42538
 
      rc = pagerPlaybackSavepoint(pPager, pSavepoint);
42539
 
      assert(rc!=SQLITE_DONE);
42540
 
    }
42541
 
  }
42542
 
 
42543
 
  return rc;
42544
 
}
42545
 
 
42546
 
/*
42547
 
** Return the full pathname of the database file.
42548
 
*/
42549
 
SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager){
42550
 
  return pPager->zFilename;
42551
 
}
42552
 
 
42553
 
/*
42554
 
** Return the VFS structure for the pager.
42555
 
*/
42556
 
SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
42557
 
  return pPager->pVfs;
42558
 
}
42559
 
 
42560
 
/*
42561
 
** Return the file handle for the database file associated
42562
 
** with the pager.  This might return NULL if the file has
42563
 
** not yet been opened.
42564
 
*/
42565
 
SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
42566
 
  return pPager->fd;
42567
 
}
42568
 
 
42569
 
/*
42570
 
** Return the full pathname of the journal file.
42571
 
*/
42572
 
SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
42573
 
  return pPager->zJournal;
42574
 
}
42575
 
 
42576
 
/*
42577
 
** Return true if fsync() calls are disabled for this pager.  Return FALSE
42578
 
** if fsync()s are executed normally.
42579
 
*/
42580
 
SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
42581
 
  return pPager->noSync;
42582
 
}
42583
 
 
42584
 
#ifdef SQLITE_HAS_CODEC
42585
 
/*
42586
 
** Set or retrieve the codec for this pager
42587
 
*/
42588
 
SQLITE_PRIVATE void sqlite3PagerSetCodec(
42589
 
  Pager *pPager,
42590
 
  void *(*xCodec)(void*,void*,Pgno,int),
42591
 
  void (*xCodecSizeChng)(void*,int,int),
42592
 
  void (*xCodecFree)(void*),
42593
 
  void *pCodec
42594
 
){
42595
 
  if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
42596
 
  pPager->xCodec = pPager->memDb ? 0 : xCodec;
42597
 
  pPager->xCodecSizeChng = xCodecSizeChng;
42598
 
  pPager->xCodecFree = xCodecFree;
42599
 
  pPager->pCodec = pCodec;
42600
 
  pagerReportSize(pPager);
42601
 
}
42602
 
SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
42603
 
  return pPager->pCodec;
42604
 
}
42605
 
#endif
42606
 
 
42607
 
#ifndef SQLITE_OMIT_AUTOVACUUM
42608
 
/*
42609
 
** Move the page pPg to location pgno in the file.
42610
 
**
42611
 
** There must be no references to the page previously located at
42612
 
** pgno (which we call pPgOld) though that page is allowed to be
42613
 
** in cache.  If the page previously located at pgno is not already
42614
 
** in the rollback journal, it is not put there by by this routine.
42615
 
**
42616
 
** References to the page pPg remain valid. Updating any
42617
 
** meta-data associated with pPg (i.e. data stored in the nExtra bytes
42618
 
** allocated along with the page) is the responsibility of the caller.
42619
 
**
42620
 
** A transaction must be active when this routine is called. It used to be
42621
 
** required that a statement transaction was not active, but this restriction
42622
 
** has been removed (CREATE INDEX needs to move a page when a statement
42623
 
** transaction is active).
42624
 
**
42625
 
** If the fourth argument, isCommit, is non-zero, then this page is being
42626
 
** moved as part of a database reorganization just before the transaction 
42627
 
** is being committed. In this case, it is guaranteed that the database page 
42628
 
** pPg refers to will not be written to again within this transaction.
42629
 
**
42630
 
** This function may return SQLITE_NOMEM or an IO error code if an error
42631
 
** occurs. Otherwise, it returns SQLITE_OK.
42632
 
*/
42633
 
SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
42634
 
  PgHdr *pPgOld;               /* The page being overwritten. */
42635
 
  Pgno needSyncPgno = 0;       /* Old value of pPg->pgno, if sync is required */
42636
 
  int rc;                      /* Return code */
42637
 
  Pgno origPgno;               /* The original page number */
42638
 
 
42639
 
  assert( pPg->nRef>0 );
42640
 
  assert( pPager->eState==PAGER_WRITER_CACHEMOD
42641
 
       || pPager->eState==PAGER_WRITER_DBMOD
42642
 
  );
42643
 
  assert( assert_pager_state(pPager) );
42644
 
 
42645
 
  /* In order to be able to rollback, an in-memory database must journal
42646
 
  ** the page we are moving from.
42647
 
  */
42648
 
  if( MEMDB ){
42649
 
    rc = sqlite3PagerWrite(pPg);
42650
 
    if( rc ) return rc;
42651
 
  }
42652
 
 
42653
 
  /* If the page being moved is dirty and has not been saved by the latest
42654
 
  ** savepoint, then save the current contents of the page into the 
42655
 
  ** sub-journal now. This is required to handle the following scenario:
42656
 
  **
42657
 
  **   BEGIN;
42658
 
  **     <journal page X, then modify it in memory>
42659
 
  **     SAVEPOINT one;
42660
 
  **       <Move page X to location Y>
42661
 
  **     ROLLBACK TO one;
42662
 
  **
42663
 
  ** If page X were not written to the sub-journal here, it would not
42664
 
  ** be possible to restore its contents when the "ROLLBACK TO one"
42665
 
  ** statement were is processed.
42666
 
  **
42667
 
  ** subjournalPage() may need to allocate space to store pPg->pgno into
42668
 
  ** one or more savepoint bitvecs. This is the reason this function
42669
 
  ** may return SQLITE_NOMEM.
42670
 
  */
42671
 
  if( pPg->flags&PGHDR_DIRTY
42672
 
   && subjRequiresPage(pPg)
42673
 
   && SQLITE_OK!=(rc = subjournalPage(pPg))
42674
 
  ){
42675
 
    return rc;
42676
 
  }
42677
 
 
42678
 
  PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n", 
42679
 
      PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
42680
 
  IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
42681
 
 
42682
 
  /* If the journal needs to be sync()ed before page pPg->pgno can
42683
 
  ** be written to, store pPg->pgno in local variable needSyncPgno.
42684
 
  **
42685
 
  ** If the isCommit flag is set, there is no need to remember that
42686
 
  ** the journal needs to be sync()ed before database page pPg->pgno 
42687
 
  ** can be written to. The caller has already promised not to write to it.
42688
 
  */
42689
 
  if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
42690
 
    needSyncPgno = pPg->pgno;
42691
 
    assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
42692
 
    assert( pPg->flags&PGHDR_DIRTY );
42693
 
  }
42694
 
 
42695
 
  /* If the cache contains a page with page-number pgno, remove it
42696
 
  ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for 
42697
 
  ** page pgno before the 'move' operation, it needs to be retained 
42698
 
  ** for the page moved there.
42699
 
  */
42700
 
  pPg->flags &= ~PGHDR_NEED_SYNC;
42701
 
  pPgOld = pager_lookup(pPager, pgno);
42702
 
  assert( !pPgOld || pPgOld->nRef==1 );
42703
 
  if( pPgOld ){
42704
 
    pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
42705
 
    if( MEMDB ){
42706
 
      /* Do not discard pages from an in-memory database since we might
42707
 
      ** need to rollback later.  Just move the page out of the way. */
42708
 
      sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
42709
 
    }else{
42710
 
      sqlite3PcacheDrop(pPgOld);
42711
 
    }
42712
 
  }
42713
 
 
42714
 
  origPgno = pPg->pgno;
42715
 
  sqlite3PcacheMove(pPg, pgno);
42716
 
  sqlite3PcacheMakeDirty(pPg);
42717
 
 
42718
 
  /* For an in-memory database, make sure the original page continues
42719
 
  ** to exist, in case the transaction needs to roll back.  Use pPgOld
42720
 
  ** as the original page since it has already been allocated.
42721
 
  */
42722
 
  if( MEMDB ){
42723
 
    assert( pPgOld );
42724
 
    sqlite3PcacheMove(pPgOld, origPgno);
42725
 
    sqlite3PagerUnref(pPgOld);
42726
 
  }
42727
 
 
42728
 
  if( needSyncPgno ){
42729
 
    /* If needSyncPgno is non-zero, then the journal file needs to be 
42730
 
    ** sync()ed before any data is written to database file page needSyncPgno.
42731
 
    ** Currently, no such page exists in the page-cache and the 
42732
 
    ** "is journaled" bitvec flag has been set. This needs to be remedied by
42733
 
    ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
42734
 
    ** flag.
42735
 
    **
42736
 
    ** If the attempt to load the page into the page-cache fails, (due
42737
 
    ** to a malloc() or IO failure), clear the bit in the pInJournal[]
42738
 
    ** array. Otherwise, if the page is loaded and written again in
42739
 
    ** this transaction, it may be written to the database file before
42740
 
    ** it is synced into the journal file. This way, it may end up in
42741
 
    ** the journal file twice, but that is not a problem.
42742
 
    */
42743
 
    PgHdr *pPgHdr;
42744
 
    rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
42745
 
    if( rc!=SQLITE_OK ){
42746
 
      if( needSyncPgno<=pPager->dbOrigSize ){
42747
 
        assert( pPager->pTmpSpace!=0 );
42748
 
        sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
42749
 
      }
42750
 
      return rc;
42751
 
    }
42752
 
    pPgHdr->flags |= PGHDR_NEED_SYNC;
42753
 
    sqlite3PcacheMakeDirty(pPgHdr);
42754
 
    sqlite3PagerUnref(pPgHdr);
42755
 
  }
42756
 
 
42757
 
  return SQLITE_OK;
42758
 
}
42759
 
#endif
42760
 
 
42761
 
/*
42762
 
** Return a pointer to the data for the specified page.
42763
 
*/
42764
 
SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
42765
 
  assert( pPg->nRef>0 || pPg->pPager->memDb );
42766
 
  return pPg->pData;
42767
 
}
42768
 
 
42769
 
/*
42770
 
** Return a pointer to the Pager.nExtra bytes of "extra" space 
42771
 
** allocated along with the specified page.
42772
 
*/
42773
 
SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
42774
 
  return pPg->pExtra;
42775
 
}
42776
 
 
42777
 
/*
42778
 
** Get/set the locking-mode for this pager. Parameter eMode must be one
42779
 
** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or 
42780
 
** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
42781
 
** the locking-mode is set to the value specified.
42782
 
**
42783
 
** The returned value is either PAGER_LOCKINGMODE_NORMAL or
42784
 
** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
42785
 
** locking-mode.
42786
 
*/
42787
 
SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
42788
 
  assert( eMode==PAGER_LOCKINGMODE_QUERY
42789
 
            || eMode==PAGER_LOCKINGMODE_NORMAL
42790
 
            || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
42791
 
  assert( PAGER_LOCKINGMODE_QUERY<0 );
42792
 
  assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
42793
 
  assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
42794
 
  if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
42795
 
    pPager->exclusiveMode = (u8)eMode;
42796
 
  }
42797
 
  return (int)pPager->exclusiveMode;
42798
 
}
42799
 
 
42800
 
/*
42801
 
** Set the journal-mode for this pager. Parameter eMode must be one of:
42802
 
**
42803
 
**    PAGER_JOURNALMODE_DELETE
42804
 
**    PAGER_JOURNALMODE_TRUNCATE
42805
 
**    PAGER_JOURNALMODE_PERSIST
42806
 
**    PAGER_JOURNALMODE_OFF
42807
 
**    PAGER_JOURNALMODE_MEMORY
42808
 
**    PAGER_JOURNALMODE_WAL
42809
 
**
42810
 
** The journalmode is set to the value specified if the change is allowed.
42811
 
** The change may be disallowed for the following reasons:
42812
 
**
42813
 
**   *  An in-memory database can only have its journal_mode set to _OFF
42814
 
**      or _MEMORY.
42815
 
**
42816
 
**   *  Temporary databases cannot have _WAL journalmode.
42817
 
**
42818
 
** The returned indicate the current (possibly updated) journal-mode.
42819
 
*/
42820
 
SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
42821
 
  u8 eOld = pPager->journalMode;    /* Prior journalmode */
42822
 
 
42823
 
#ifdef SQLITE_DEBUG
42824
 
  /* The print_pager_state() routine is intended to be used by the debugger
42825
 
  ** only.  We invoke it once here to suppress a compiler warning. */
42826
 
  print_pager_state(pPager);
42827
 
#endif
42828
 
 
42829
 
 
42830
 
  /* The eMode parameter is always valid */
42831
 
  assert(      eMode==PAGER_JOURNALMODE_DELETE
42832
 
            || eMode==PAGER_JOURNALMODE_TRUNCATE
42833
 
            || eMode==PAGER_JOURNALMODE_PERSIST
42834
 
            || eMode==PAGER_JOURNALMODE_OFF 
42835
 
            || eMode==PAGER_JOURNALMODE_WAL 
42836
 
            || eMode==PAGER_JOURNALMODE_MEMORY );
42837
 
 
42838
 
  /* This routine is only called from the OP_JournalMode opcode, and
42839
 
  ** the logic there will never allow a temporary file to be changed
42840
 
  ** to WAL mode.
42841
 
  */
42842
 
  assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
42843
 
 
42844
 
  /* Do allow the journalmode of an in-memory database to be set to
42845
 
  ** anything other than MEMORY or OFF
42846
 
  */
42847
 
  if( MEMDB ){
42848
 
    assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
42849
 
    if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
42850
 
      eMode = eOld;
42851
 
    }
42852
 
  }
42853
 
 
42854
 
  if( eMode!=eOld ){
42855
 
 
42856
 
    /* Change the journal mode. */
42857
 
    assert( pPager->eState!=PAGER_ERROR );
42858
 
    pPager->journalMode = (u8)eMode;
42859
 
 
42860
 
    /* When transistioning from TRUNCATE or PERSIST to any other journal
42861
 
    ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
42862
 
    ** delete the journal file.
42863
 
    */
42864
 
    assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
42865
 
    assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
42866
 
    assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
42867
 
    assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
42868
 
    assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
42869
 
    assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
42870
 
 
42871
 
    assert( isOpen(pPager->fd) || pPager->exclusiveMode );
42872
 
    if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
42873
 
 
42874
 
      /* In this case we would like to delete the journal file. If it is
42875
 
      ** not possible, then that is not a problem. Deleting the journal file
42876
 
      ** here is an optimization only.
42877
 
      **
42878
 
      ** Before deleting the journal file, obtain a RESERVED lock on the
42879
 
      ** database file. This ensures that the journal file is not deleted
42880
 
      ** while it is in use by some other client.
42881
 
      */
42882
 
      sqlite3OsClose(pPager->jfd);
42883
 
      if( pPager->eLock>=RESERVED_LOCK ){
42884
 
        sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
42885
 
      }else{
42886
 
        int rc = SQLITE_OK;
42887
 
        int state = pPager->eState;
42888
 
        assert( state==PAGER_OPEN || state==PAGER_READER );
42889
 
        if( state==PAGER_OPEN ){
42890
 
          rc = sqlite3PagerSharedLock(pPager);
42891
 
        }
42892
 
        if( pPager->eState==PAGER_READER ){
42893
 
          assert( rc==SQLITE_OK );
42894
 
          rc = pagerLockDb(pPager, RESERVED_LOCK);
42895
 
        }
42896
 
        if( rc==SQLITE_OK ){
42897
 
          sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
42898
 
        }
42899
 
        if( rc==SQLITE_OK && state==PAGER_READER ){
42900
 
          pagerUnlockDb(pPager, SHARED_LOCK);
42901
 
        }else if( state==PAGER_OPEN ){
42902
 
          pager_unlock(pPager);
42903
 
        }
42904
 
        assert( state==pPager->eState );
42905
 
      }
42906
 
    }
42907
 
  }
42908
 
 
42909
 
  /* Return the new journal mode */
42910
 
  return (int)pPager->journalMode;
42911
 
}
42912
 
 
42913
 
/*
42914
 
** Return the current journal mode.
42915
 
*/
42916
 
SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
42917
 
  return (int)pPager->journalMode;
42918
 
}
42919
 
 
42920
 
/*
42921
 
** Return TRUE if the pager is in a state where it is OK to change the
42922
 
** journalmode.  Journalmode changes can only happen when the database
42923
 
** is unmodified.
42924
 
*/
42925
 
SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
42926
 
  assert( assert_pager_state(pPager) );
42927
 
  if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
42928
 
  if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
42929
 
  return 1;
42930
 
}
42931
 
 
42932
 
/*
42933
 
** Get/set the size-limit used for persistent journal files.
42934
 
**
42935
 
** Setting the size limit to -1 means no limit is enforced.
42936
 
** An attempt to set a limit smaller than -1 is a no-op.
42937
 
*/
42938
 
SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
42939
 
  if( iLimit>=-1 ){
42940
 
    pPager->journalSizeLimit = iLimit;
42941
 
  }
42942
 
  return pPager->journalSizeLimit;
42943
 
}
42944
 
 
42945
 
/*
42946
 
** Return a pointer to the pPager->pBackup variable. The backup module
42947
 
** in backup.c maintains the content of this variable. This module
42948
 
** uses it opaquely as an argument to sqlite3BackupRestart() and
42949
 
** sqlite3BackupUpdate() only.
42950
 
*/
42951
 
SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
42952
 
  return &pPager->pBackup;
42953
 
}
42954
 
 
42955
 
#ifndef SQLITE_OMIT_WAL
42956
 
/*
42957
 
** This function is called when the user invokes "PRAGMA wal_checkpoint",
42958
 
** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
42959
 
** or wal_blocking_checkpoint() API functions.
42960
 
**
42961
 
** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
42962
 
*/
42963
 
SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
42964
 
  int rc = SQLITE_OK;
42965
 
  if( pPager->pWal ){
42966
 
    rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
42967
 
        pPager->xBusyHandler, pPager->pBusyHandlerArg,
42968
 
        pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
42969
 
        pnLog, pnCkpt
42970
 
    );
42971
 
  }
42972
 
  return rc;
42973
 
}
42974
 
 
42975
 
SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
42976
 
  return sqlite3WalCallback(pPager->pWal);
42977
 
}
42978
 
 
42979
 
/*
42980
 
** Return true if the underlying VFS for the given pager supports the
42981
 
** primitives necessary for write-ahead logging.
42982
 
*/
42983
 
SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
42984
 
  const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
42985
 
  return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
42986
 
}
42987
 
 
42988
 
/*
42989
 
** Attempt to take an exclusive lock on the database file. If a PENDING lock
42990
 
** is obtained instead, immediately release it.
42991
 
*/
42992
 
static int pagerExclusiveLock(Pager *pPager){
42993
 
  int rc;                         /* Return code */
42994
 
 
42995
 
  assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
42996
 
  rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
42997
 
  if( rc!=SQLITE_OK ){
42998
 
    /* If the attempt to grab the exclusive lock failed, release the 
42999
 
    ** pending lock that may have been obtained instead.  */
43000
 
    pagerUnlockDb(pPager, SHARED_LOCK);
43001
 
  }
43002
 
 
43003
 
  return rc;
43004
 
}
43005
 
 
43006
 
/*
43007
 
** Call sqlite3WalOpen() to open the WAL handle. If the pager is in 
43008
 
** exclusive-locking mode when this function is called, take an EXCLUSIVE
43009
 
** lock on the database file and use heap-memory to store the wal-index
43010
 
** in. Otherwise, use the normal shared-memory.
43011
 
*/
43012
 
static int pagerOpenWal(Pager *pPager){
43013
 
  int rc = SQLITE_OK;
43014
 
 
43015
 
  assert( pPager->pWal==0 && pPager->tempFile==0 );
43016
 
  assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK || pPager->noReadlock);
43017
 
 
43018
 
  /* If the pager is already in exclusive-mode, the WAL module will use 
43019
 
  ** heap-memory for the wal-index instead of the VFS shared-memory 
43020
 
  ** implementation. Take the exclusive lock now, before opening the WAL
43021
 
  ** file, to make sure this is safe.
43022
 
  */
43023
 
  if( pPager->exclusiveMode ){
43024
 
    rc = pagerExclusiveLock(pPager);
43025
 
  }
43026
 
 
43027
 
  /* Open the connection to the log file. If this operation fails, 
43028
 
  ** (e.g. due to malloc() failure), return an error code.
43029
 
  */
43030
 
  if( rc==SQLITE_OK ){
43031
 
    rc = sqlite3WalOpen(pPager->pVfs, 
43032
 
        pPager->fd, pPager->zWal, pPager->exclusiveMode, &pPager->pWal
43033
 
    );
43034
 
  }
43035
 
 
43036
 
  return rc;
43037
 
}
43038
 
 
43039
 
 
43040
 
/*
43041
 
** The caller must be holding a SHARED lock on the database file to call
43042
 
** this function.
43043
 
**
43044
 
** If the pager passed as the first argument is open on a real database
43045
 
** file (not a temp file or an in-memory database), and the WAL file
43046
 
** is not already open, make an attempt to open it now. If successful,
43047
 
** return SQLITE_OK. If an error occurs or the VFS used by the pager does 
43048
 
** not support the xShmXXX() methods, return an error code. *pbOpen is
43049
 
** not modified in either case.
43050
 
**
43051
 
** If the pager is open on a temp-file (or in-memory database), or if
43052
 
** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
43053
 
** without doing anything.
43054
 
*/
43055
 
SQLITE_PRIVATE int sqlite3PagerOpenWal(
43056
 
  Pager *pPager,                  /* Pager object */
43057
 
  int *pbOpen                     /* OUT: Set to true if call is a no-op */
43058
 
){
43059
 
  int rc = SQLITE_OK;             /* Return code */
43060
 
 
43061
 
  assert( assert_pager_state(pPager) );
43062
 
  assert( pPager->eState==PAGER_OPEN   || pbOpen );
43063
 
  assert( pPager->eState==PAGER_READER || !pbOpen );
43064
 
  assert( pbOpen==0 || *pbOpen==0 );
43065
 
  assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
43066
 
 
43067
 
  if( !pPager->tempFile && !pPager->pWal ){
43068
 
    if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
43069
 
 
43070
 
    /* Close any rollback journal previously open */
43071
 
    sqlite3OsClose(pPager->jfd);
43072
 
 
43073
 
    rc = pagerOpenWal(pPager);
43074
 
    if( rc==SQLITE_OK ){
43075
 
      pPager->journalMode = PAGER_JOURNALMODE_WAL;
43076
 
      pPager->eState = PAGER_OPEN;
43077
 
    }
43078
 
  }else{
43079
 
    *pbOpen = 1;
43080
 
  }
43081
 
 
43082
 
  return rc;
43083
 
}
43084
 
 
43085
 
/*
43086
 
** This function is called to close the connection to the log file prior
43087
 
** to switching from WAL to rollback mode.
43088
 
**
43089
 
** Before closing the log file, this function attempts to take an 
43090
 
** EXCLUSIVE lock on the database file. If this cannot be obtained, an
43091
 
** error (SQLITE_BUSY) is returned and the log connection is not closed.
43092
 
** If successful, the EXCLUSIVE lock is not released before returning.
43093
 
*/
43094
 
SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
43095
 
  int rc = SQLITE_OK;
43096
 
 
43097
 
  assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
43098
 
 
43099
 
  /* If the log file is not already open, but does exist in the file-system,
43100
 
  ** it may need to be checkpointed before the connection can switch to
43101
 
  ** rollback mode. Open it now so this can happen.
43102
 
  */
43103
 
  if( !pPager->pWal ){
43104
 
    int logexists = 0;
43105
 
    rc = pagerLockDb(pPager, SHARED_LOCK);
43106
 
    if( rc==SQLITE_OK ){
43107
 
      rc = sqlite3OsAccess(
43108
 
          pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
43109
 
      );
43110
 
    }
43111
 
    if( rc==SQLITE_OK && logexists ){
43112
 
      rc = pagerOpenWal(pPager);
43113
 
    }
43114
 
  }
43115
 
    
43116
 
  /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
43117
 
  ** the database file, the log and log-summary files will be deleted.
43118
 
  */
43119
 
  if( rc==SQLITE_OK && pPager->pWal ){
43120
 
    rc = pagerExclusiveLock(pPager);
43121
 
    if( rc==SQLITE_OK ){
43122
 
      rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
43123
 
                           pPager->pageSize, (u8*)pPager->pTmpSpace);
43124
 
      pPager->pWal = 0;
43125
 
    }
43126
 
  }
43127
 
  return rc;
43128
 
}
43129
 
 
43130
 
#ifdef SQLITE_HAS_CODEC
43131
 
/*
43132
 
** This function is called by the wal module when writing page content
43133
 
** into the log file.
43134
 
**
43135
 
** This function returns a pointer to a buffer containing the encrypted
43136
 
** page content. If a malloc fails, this function may return NULL.
43137
 
*/
43138
 
SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
43139
 
  void *aData = 0;
43140
 
  CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
43141
 
  return aData;
43142
 
}
43143
 
#endif /* SQLITE_HAS_CODEC */
43144
 
 
43145
 
#endif /* !SQLITE_OMIT_WAL */
43146
 
 
43147
 
#endif /* SQLITE_OMIT_DISKIO */
43148
 
 
43149
 
/************** End of pager.c ***********************************************/
43150
 
/************** Begin file wal.c *********************************************/
43151
 
/*
43152
 
** 2010 February 1
43153
 
**
43154
 
** The author disclaims copyright to this source code.  In place of
43155
 
** a legal notice, here is a blessing:
43156
 
**
43157
 
**    May you do good and not evil.
43158
 
**    May you find forgiveness for yourself and forgive others.
43159
 
**    May you share freely, never taking more than you give.
43160
 
**
43161
 
*************************************************************************
43162
 
**
43163
 
** This file contains the implementation of a write-ahead log (WAL) used in 
43164
 
** "journal_mode=WAL" mode.
43165
 
**
43166
 
** WRITE-AHEAD LOG (WAL) FILE FORMAT
43167
 
**
43168
 
** A WAL file consists of a header followed by zero or more "frames".
43169
 
** Each frame records the revised content of a single page from the
43170
 
** database file.  All changes to the database are recorded by writing
43171
 
** frames into the WAL.  Transactions commit when a frame is written that
43172
 
** contains a commit marker.  A single WAL can and usually does record 
43173
 
** multiple transactions.  Periodically, the content of the WAL is
43174
 
** transferred back into the database file in an operation called a
43175
 
** "checkpoint".
43176
 
**
43177
 
** A single WAL file can be used multiple times.  In other words, the
43178
 
** WAL can fill up with frames and then be checkpointed and then new
43179
 
** frames can overwrite the old ones.  A WAL always grows from beginning
43180
 
** toward the end.  Checksums and counters attached to each frame are
43181
 
** used to determine which frames within the WAL are valid and which
43182
 
** are leftovers from prior checkpoints.
43183
 
**
43184
 
** The WAL header is 32 bytes in size and consists of the following eight
43185
 
** big-endian 32-bit unsigned integer values:
43186
 
**
43187
 
**     0: Magic number.  0x377f0682 or 0x377f0683
43188
 
**     4: File format version.  Currently 3007000
43189
 
**     8: Database page size.  Example: 1024
43190
 
**    12: Checkpoint sequence number
43191
 
**    16: Salt-1, random integer incremented with each checkpoint
43192
 
**    20: Salt-2, a different random integer changing with each ckpt
43193
 
**    24: Checksum-1 (first part of checksum for first 24 bytes of header).
43194
 
**    28: Checksum-2 (second part of checksum for first 24 bytes of header).
43195
 
**
43196
 
** Immediately following the wal-header are zero or more frames. Each
43197
 
** frame consists of a 24-byte frame-header followed by a <page-size> bytes
43198
 
** of page data. The frame-header is six big-endian 32-bit unsigned 
43199
 
** integer values, as follows:
43200
 
**
43201
 
**     0: Page number.
43202
 
**     4: For commit records, the size of the database image in pages 
43203
 
**        after the commit. For all other records, zero.
43204
 
**     8: Salt-1 (copied from the header)
43205
 
**    12: Salt-2 (copied from the header)
43206
 
**    16: Checksum-1.
43207
 
**    20: Checksum-2.
43208
 
**
43209
 
** A frame is considered valid if and only if the following conditions are
43210
 
** true:
43211
 
**
43212
 
**    (1) The salt-1 and salt-2 values in the frame-header match
43213
 
**        salt values in the wal-header
43214
 
**
43215
 
**    (2) The checksum values in the final 8 bytes of the frame-header
43216
 
**        exactly match the checksum computed consecutively on the
43217
 
**        WAL header and the first 8 bytes and the content of all frames
43218
 
**        up to and including the current frame.
43219
 
**
43220
 
** The checksum is computed using 32-bit big-endian integers if the
43221
 
** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
43222
 
** is computed using little-endian if the magic number is 0x377f0682.
43223
 
** The checksum values are always stored in the frame header in a
43224
 
** big-endian format regardless of which byte order is used to compute
43225
 
** the checksum.  The checksum is computed by interpreting the input as
43226
 
** an even number of unsigned 32-bit integers: x[0] through x[N].  The
43227
 
** algorithm used for the checksum is as follows:
43228
 
** 
43229
 
**   for i from 0 to n-1 step 2:
43230
 
**     s0 += x[i] + s1;
43231
 
**     s1 += x[i+1] + s0;
43232
 
**   endfor
43233
 
**
43234
 
** Note that s0 and s1 are both weighted checksums using fibonacci weights
43235
 
** in reverse order (the largest fibonacci weight occurs on the first element
43236
 
** of the sequence being summed.)  The s1 value spans all 32-bit 
43237
 
** terms of the sequence whereas s0 omits the final term.
43238
 
**
43239
 
** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
43240
 
** WAL is transferred into the database, then the database is VFS.xSync-ed.
43241
 
** The VFS.xSync operations serve as write barriers - all writes launched
43242
 
** before the xSync must complete before any write that launches after the
43243
 
** xSync begins.
43244
 
**
43245
 
** After each checkpoint, the salt-1 value is incremented and the salt-2
43246
 
** value is randomized.  This prevents old and new frames in the WAL from
43247
 
** being considered valid at the same time and being checkpointing together
43248
 
** following a crash.
43249
 
**
43250
 
** READER ALGORITHM
43251
 
**
43252
 
** To read a page from the database (call it page number P), a reader
43253
 
** first checks the WAL to see if it contains page P.  If so, then the
43254
 
** last valid instance of page P that is a followed by a commit frame
43255
 
** or is a commit frame itself becomes the value read.  If the WAL
43256
 
** contains no copies of page P that are valid and which are a commit
43257
 
** frame or are followed by a commit frame, then page P is read from
43258
 
** the database file.
43259
 
**
43260
 
** To start a read transaction, the reader records the index of the last
43261
 
** valid frame in the WAL.  The reader uses this recorded "mxFrame" value
43262
 
** for all subsequent read operations.  New transactions can be appended
43263
 
** to the WAL, but as long as the reader uses its original mxFrame value
43264
 
** and ignores the newly appended content, it will see a consistent snapshot
43265
 
** of the database from a single point in time.  This technique allows
43266
 
** multiple concurrent readers to view different versions of the database
43267
 
** content simultaneously.
43268
 
**
43269
 
** The reader algorithm in the previous paragraphs works correctly, but 
43270
 
** because frames for page P can appear anywhere within the WAL, the
43271
 
** reader has to scan the entire WAL looking for page P frames.  If the
43272
 
** WAL is large (multiple megabytes is typical) that scan can be slow,
43273
 
** and read performance suffers.  To overcome this problem, a separate
43274
 
** data structure called the wal-index is maintained to expedite the
43275
 
** search for frames of a particular page.
43276
 
** 
43277
 
** WAL-INDEX FORMAT
43278
 
**
43279
 
** Conceptually, the wal-index is shared memory, though VFS implementations
43280
 
** might choose to implement the wal-index using a mmapped file.  Because
43281
 
** the wal-index is shared memory, SQLite does not support journal_mode=WAL 
43282
 
** on a network filesystem.  All users of the database must be able to
43283
 
** share memory.
43284
 
**
43285
 
** The wal-index is transient.  After a crash, the wal-index can (and should
43286
 
** be) reconstructed from the original WAL file.  In fact, the VFS is required
43287
 
** to either truncate or zero the header of the wal-index when the last
43288
 
** connection to it closes.  Because the wal-index is transient, it can
43289
 
** use an architecture-specific format; it does not have to be cross-platform.
43290
 
** Hence, unlike the database and WAL file formats which store all values
43291
 
** as big endian, the wal-index can store multi-byte values in the native
43292
 
** byte order of the host computer.
43293
 
**
43294
 
** The purpose of the wal-index is to answer this question quickly:  Given
43295
 
** a page number P, return the index of the last frame for page P in the WAL,
43296
 
** or return NULL if there are no frames for page P in the WAL.
43297
 
**
43298
 
** The wal-index consists of a header region, followed by an one or
43299
 
** more index blocks.  
43300
 
**
43301
 
** The wal-index header contains the total number of frames within the WAL
43302
 
** in the the mxFrame field.  
43303
 
**
43304
 
** Each index block except for the first contains information on 
43305
 
** HASHTABLE_NPAGE frames. The first index block contains information on
43306
 
** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and 
43307
 
** HASHTABLE_NPAGE are selected so that together the wal-index header and
43308
 
** first index block are the same size as all other index blocks in the
43309
 
** wal-index.
43310
 
**
43311
 
** Each index block contains two sections, a page-mapping that contains the
43312
 
** database page number associated with each wal frame, and a hash-table 
43313
 
** that allows readers to query an index block for a specific page number.
43314
 
** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
43315
 
** for the first index block) 32-bit page numbers. The first entry in the 
43316
 
** first index-block contains the database page number corresponding to the
43317
 
** first frame in the WAL file. The first entry in the second index block
43318
 
** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
43319
 
** the log, and so on.
43320
 
**
43321
 
** The last index block in a wal-index usually contains less than the full
43322
 
** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
43323
 
** depending on the contents of the WAL file. This does not change the
43324
 
** allocated size of the page-mapping array - the page-mapping array merely
43325
 
** contains unused entries.
43326
 
**
43327
 
** Even without using the hash table, the last frame for page P
43328
 
** can be found by scanning the page-mapping sections of each index block
43329
 
** starting with the last index block and moving toward the first, and
43330
 
** within each index block, starting at the end and moving toward the
43331
 
** beginning.  The first entry that equals P corresponds to the frame
43332
 
** holding the content for that page.
43333
 
**
43334
 
** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
43335
 
** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
43336
 
** hash table for each page number in the mapping section, so the hash 
43337
 
** table is never more than half full.  The expected number of collisions 
43338
 
** prior to finding a match is 1.  Each entry of the hash table is an
43339
 
** 1-based index of an entry in the mapping section of the same
43340
 
** index block.   Let K be the 1-based index of the largest entry in
43341
 
** the mapping section.  (For index blocks other than the last, K will
43342
 
** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
43343
 
** K will be (mxFrame%HASHTABLE_NPAGE).)  Unused slots of the hash table
43344
 
** contain a value of 0.
43345
 
**
43346
 
** To look for page P in the hash table, first compute a hash iKey on
43347
 
** P as follows:
43348
 
**
43349
 
**      iKey = (P * 383) % HASHTABLE_NSLOT
43350
 
**
43351
 
** Then start scanning entries of the hash table, starting with iKey
43352
 
** (wrapping around to the beginning when the end of the hash table is
43353
 
** reached) until an unused hash slot is found. Let the first unused slot
43354
 
** be at index iUnused.  (iUnused might be less than iKey if there was
43355
 
** wrap-around.) Because the hash table is never more than half full,
43356
 
** the search is guaranteed to eventually hit an unused entry.  Let 
43357
 
** iMax be the value between iKey and iUnused, closest to iUnused,
43358
 
** where aHash[iMax]==P.  If there is no iMax entry (if there exists
43359
 
** no hash slot such that aHash[i]==p) then page P is not in the
43360
 
** current index block.  Otherwise the iMax-th mapping entry of the
43361
 
** current index block corresponds to the last entry that references 
43362
 
** page P.
43363
 
**
43364
 
** A hash search begins with the last index block and moves toward the
43365
 
** first index block, looking for entries corresponding to page P.  On
43366
 
** average, only two or three slots in each index block need to be
43367
 
** examined in order to either find the last entry for page P, or to
43368
 
** establish that no such entry exists in the block.  Each index block
43369
 
** holds over 4000 entries.  So two or three index blocks are sufficient
43370
 
** to cover a typical 10 megabyte WAL file, assuming 1K pages.  8 or 10
43371
 
** comparisons (on average) suffice to either locate a frame in the
43372
 
** WAL or to establish that the frame does not exist in the WAL.  This
43373
 
** is much faster than scanning the entire 10MB WAL.
43374
 
**
43375
 
** Note that entries are added in order of increasing K.  Hence, one
43376
 
** reader might be using some value K0 and a second reader that started
43377
 
** at a later time (after additional transactions were added to the WAL
43378
 
** and to the wal-index) might be using a different value K1, where K1>K0.
43379
 
** Both readers can use the same hash table and mapping section to get
43380
 
** the correct result.  There may be entries in the hash table with
43381
 
** K>K0 but to the first reader, those entries will appear to be unused
43382
 
** slots in the hash table and so the first reader will get an answer as
43383
 
** if no values greater than K0 had ever been inserted into the hash table
43384
 
** in the first place - which is what reader one wants.  Meanwhile, the
43385
 
** second reader using K1 will see additional values that were inserted
43386
 
** later, which is exactly what reader two wants.  
43387
 
**
43388
 
** When a rollback occurs, the value of K is decreased. Hash table entries
43389
 
** that correspond to frames greater than the new K value are removed
43390
 
** from the hash table at this point.
43391
 
*/
43392
 
#ifndef SQLITE_OMIT_WAL
43393
 
 
43394
 
 
43395
 
/*
43396
 
** Trace output macros
43397
 
*/
43398
 
#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
43399
 
SQLITE_PRIVATE int sqlite3WalTrace = 0;
43400
 
# define WALTRACE(X)  if(sqlite3WalTrace) sqlite3DebugPrintf X
43401
 
#else
43402
 
# define WALTRACE(X)
43403
 
#endif
43404
 
 
43405
 
/*
43406
 
** The maximum (and only) versions of the wal and wal-index formats
43407
 
** that may be interpreted by this version of SQLite.
43408
 
**
43409
 
** If a client begins recovering a WAL file and finds that (a) the checksum
43410
 
** values in the wal-header are correct and (b) the version field is not
43411
 
** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
43412
 
**
43413
 
** Similarly, if a client successfully reads a wal-index header (i.e. the 
43414
 
** checksum test is successful) and finds that the version field is not
43415
 
** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
43416
 
** returns SQLITE_CANTOPEN.
43417
 
*/
43418
 
#define WAL_MAX_VERSION      3007000
43419
 
#define WALINDEX_MAX_VERSION 3007000
43420
 
 
43421
 
/*
43422
 
** Indices of various locking bytes.   WAL_NREADER is the number
43423
 
** of available reader locks and should be at least 3.
43424
 
*/
43425
 
#define WAL_WRITE_LOCK         0
43426
 
#define WAL_ALL_BUT_WRITE      1
43427
 
#define WAL_CKPT_LOCK          1
43428
 
#define WAL_RECOVER_LOCK       2
43429
 
#define WAL_READ_LOCK(I)       (3+(I))
43430
 
#define WAL_NREADER            (SQLITE_SHM_NLOCK-3)
43431
 
 
43432
 
 
43433
 
/* Object declarations */
43434
 
typedef struct WalIndexHdr WalIndexHdr;
43435
 
typedef struct WalIterator WalIterator;
43436
 
typedef struct WalCkptInfo WalCkptInfo;
43437
 
 
43438
 
 
43439
 
/*
43440
 
** The following object holds a copy of the wal-index header content.
43441
 
**
43442
 
** The actual header in the wal-index consists of two copies of this
43443
 
** object.
43444
 
**
43445
 
** The szPage value can be any power of 2 between 512 and 32768, inclusive.
43446
 
** Or it can be 1 to represent a 65536-byte page.  The latter case was
43447
 
** added in 3.7.1 when support for 64K pages was added.  
43448
 
*/
43449
 
struct WalIndexHdr {
43450
 
  u32 iVersion;                   /* Wal-index version */
43451
 
  u32 unused;                     /* Unused (padding) field */
43452
 
  u32 iChange;                    /* Counter incremented each transaction */
43453
 
  u8 isInit;                      /* 1 when initialized */
43454
 
  u8 bigEndCksum;                 /* True if checksums in WAL are big-endian */
43455
 
  u16 szPage;                     /* Database page size in bytes. 1==64K */
43456
 
  u32 mxFrame;                    /* Index of last valid frame in the WAL */
43457
 
  u32 nPage;                      /* Size of database in pages */
43458
 
  u32 aFrameCksum[2];             /* Checksum of last frame in log */
43459
 
  u32 aSalt[2];                   /* Two salt values copied from WAL header */
43460
 
  u32 aCksum[2];                  /* Checksum over all prior fields */
43461
 
};
43462
 
 
43463
 
/*
43464
 
** A copy of the following object occurs in the wal-index immediately
43465
 
** following the second copy of the WalIndexHdr.  This object stores
43466
 
** information used by checkpoint.
43467
 
**
43468
 
** nBackfill is the number of frames in the WAL that have been written
43469
 
** back into the database. (We call the act of moving content from WAL to
43470
 
** database "backfilling".)  The nBackfill number is never greater than
43471
 
** WalIndexHdr.mxFrame.  nBackfill can only be increased by threads
43472
 
** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
43473
 
** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
43474
 
** mxFrame back to zero when the WAL is reset.
43475
 
**
43476
 
** There is one entry in aReadMark[] for each reader lock.  If a reader
43477
 
** holds read-lock K, then the value in aReadMark[K] is no greater than
43478
 
** the mxFrame for that reader.  The value READMARK_NOT_USED (0xffffffff)
43479
 
** for any aReadMark[] means that entry is unused.  aReadMark[0] is 
43480
 
** a special case; its value is never used and it exists as a place-holder
43481
 
** to avoid having to offset aReadMark[] indexs by one.  Readers holding
43482
 
** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
43483
 
** directly from the database.
43484
 
**
43485
 
** The value of aReadMark[K] may only be changed by a thread that
43486
 
** is holding an exclusive lock on WAL_READ_LOCK(K).  Thus, the value of
43487
 
** aReadMark[K] cannot changed while there is a reader is using that mark
43488
 
** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
43489
 
**
43490
 
** The checkpointer may only transfer frames from WAL to database where
43491
 
** the frame numbers are less than or equal to every aReadMark[] that is
43492
 
** in use (that is, every aReadMark[j] for which there is a corresponding
43493
 
** WAL_READ_LOCK(j)).  New readers (usually) pick the aReadMark[] with the
43494
 
** largest value and will increase an unused aReadMark[] to mxFrame if there
43495
 
** is not already an aReadMark[] equal to mxFrame.  The exception to the
43496
 
** previous sentence is when nBackfill equals mxFrame (meaning that everything
43497
 
** in the WAL has been backfilled into the database) then new readers
43498
 
** will choose aReadMark[0] which has value 0 and hence such reader will
43499
 
** get all their all content directly from the database file and ignore 
43500
 
** the WAL.
43501
 
**
43502
 
** Writers normally append new frames to the end of the WAL.  However,
43503
 
** if nBackfill equals mxFrame (meaning that all WAL content has been
43504
 
** written back into the database) and if no readers are using the WAL
43505
 
** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
43506
 
** the writer will first "reset" the WAL back to the beginning and start
43507
 
** writing new content beginning at frame 1.
43508
 
**
43509
 
** We assume that 32-bit loads are atomic and so no locks are needed in
43510
 
** order to read from any aReadMark[] entries.
43511
 
*/
43512
 
struct WalCkptInfo {
43513
 
  u32 nBackfill;                  /* Number of WAL frames backfilled into DB */
43514
 
  u32 aReadMark[WAL_NREADER];     /* Reader marks */
43515
 
};
43516
 
#define READMARK_NOT_USED  0xffffffff
43517
 
 
43518
 
 
43519
 
/* A block of WALINDEX_LOCK_RESERVED bytes beginning at
43520
 
** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
43521
 
** only support mandatory file-locks, we do not read or write data
43522
 
** from the region of the file on which locks are applied.
43523
 
*/
43524
 
#define WALINDEX_LOCK_OFFSET   (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo))
43525
 
#define WALINDEX_LOCK_RESERVED 16
43526
 
#define WALINDEX_HDR_SIZE      (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
43527
 
 
43528
 
/* Size of header before each frame in wal */
43529
 
#define WAL_FRAME_HDRSIZE 24
43530
 
 
43531
 
/* Size of write ahead log header, including checksum. */
43532
 
/* #define WAL_HDRSIZE 24 */
43533
 
#define WAL_HDRSIZE 32
43534
 
 
43535
 
/* WAL magic value. Either this value, or the same value with the least
43536
 
** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
43537
 
** big-endian format in the first 4 bytes of a WAL file.
43538
 
**
43539
 
** If the LSB is set, then the checksums for each frame within the WAL
43540
 
** file are calculated by treating all data as an array of 32-bit 
43541
 
** big-endian words. Otherwise, they are calculated by interpreting 
43542
 
** all data as 32-bit little-endian words.
43543
 
*/
43544
 
#define WAL_MAGIC 0x377f0682
43545
 
 
43546
 
/*
43547
 
** Return the offset of frame iFrame in the write-ahead log file, 
43548
 
** assuming a database page size of szPage bytes. The offset returned
43549
 
** is to the start of the write-ahead log frame-header.
43550
 
*/
43551
 
#define walFrameOffset(iFrame, szPage) (                               \
43552
 
  WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE)         \
43553
 
)
43554
 
 
43555
 
/*
43556
 
** An open write-ahead log file is represented by an instance of the
43557
 
** following object.
43558
 
*/
43559
 
struct Wal {
43560
 
  sqlite3_vfs *pVfs;         /* The VFS used to create pDbFd */
43561
 
  sqlite3_file *pDbFd;       /* File handle for the database file */
43562
 
  sqlite3_file *pWalFd;      /* File handle for WAL file */
43563
 
  u32 iCallback;             /* Value to pass to log callback (or 0) */
43564
 
  int nWiData;               /* Size of array apWiData */
43565
 
  volatile u32 **apWiData;   /* Pointer to wal-index content in memory */
43566
 
  u32 szPage;                /* Database page size */
43567
 
  i16 readLock;              /* Which read lock is being held.  -1 for none */
43568
 
  u8 exclusiveMode;          /* Non-zero if connection is in exclusive mode */
43569
 
  u8 writeLock;              /* True if in a write transaction */
43570
 
  u8 ckptLock;               /* True if holding a checkpoint lock */
43571
 
  u8 readOnly;               /* True if the WAL file is open read-only */
43572
 
  WalIndexHdr hdr;           /* Wal-index header for current transaction */
43573
 
  const char *zWalName;      /* Name of WAL file */
43574
 
  u32 nCkpt;                 /* Checkpoint sequence counter in the wal-header */
43575
 
#ifdef SQLITE_DEBUG
43576
 
  u8 lockError;              /* True if a locking error has occurred */
43577
 
#endif
43578
 
};
43579
 
 
43580
 
/*
43581
 
** Candidate values for Wal.exclusiveMode.
43582
 
*/
43583
 
#define WAL_NORMAL_MODE     0
43584
 
#define WAL_EXCLUSIVE_MODE  1     
43585
 
#define WAL_HEAPMEMORY_MODE 2
43586
 
 
43587
 
/*
43588
 
** Each page of the wal-index mapping contains a hash-table made up of
43589
 
** an array of HASHTABLE_NSLOT elements of the following type.
43590
 
*/
43591
 
typedef u16 ht_slot;
43592
 
 
43593
 
/*
43594
 
** This structure is used to implement an iterator that loops through
43595
 
** all frames in the WAL in database page order. Where two or more frames
43596
 
** correspond to the same database page, the iterator visits only the 
43597
 
** frame most recently written to the WAL (in other words, the frame with
43598
 
** the largest index).
43599
 
**
43600
 
** The internals of this structure are only accessed by:
43601
 
**
43602
 
**   walIteratorInit() - Create a new iterator,
43603
 
**   walIteratorNext() - Step an iterator,
43604
 
**   walIteratorFree() - Free an iterator.
43605
 
**
43606
 
** This functionality is used by the checkpoint code (see walCheckpoint()).
43607
 
*/
43608
 
struct WalIterator {
43609
 
  int iPrior;                     /* Last result returned from the iterator */
43610
 
  int nSegment;                   /* Number of entries in aSegment[] */
43611
 
  struct WalSegment {
43612
 
    int iNext;                    /* Next slot in aIndex[] not yet returned */
43613
 
    ht_slot *aIndex;              /* i0, i1, i2... such that aPgno[iN] ascend */
43614
 
    u32 *aPgno;                   /* Array of page numbers. */
43615
 
    int nEntry;                   /* Nr. of entries in aPgno[] and aIndex[] */
43616
 
    int iZero;                    /* Frame number associated with aPgno[0] */
43617
 
  } aSegment[1];                  /* One for every 32KB page in the wal-index */
43618
 
};
43619
 
 
43620
 
/*
43621
 
** Define the parameters of the hash tables in the wal-index file. There
43622
 
** is a hash-table following every HASHTABLE_NPAGE page numbers in the
43623
 
** wal-index.
43624
 
**
43625
 
** Changing any of these constants will alter the wal-index format and
43626
 
** create incompatibilities.
43627
 
*/
43628
 
#define HASHTABLE_NPAGE      4096                 /* Must be power of 2 */
43629
 
#define HASHTABLE_HASH_1     383                  /* Should be prime */
43630
 
#define HASHTABLE_NSLOT      (HASHTABLE_NPAGE*2)  /* Must be a power of 2 */
43631
 
 
43632
 
/* 
43633
 
** The block of page numbers associated with the first hash-table in a
43634
 
** wal-index is smaller than usual. This is so that there is a complete
43635
 
** hash-table on each aligned 32KB page of the wal-index.
43636
 
*/
43637
 
#define HASHTABLE_NPAGE_ONE  (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
43638
 
 
43639
 
/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
43640
 
#define WALINDEX_PGSZ   (                                         \
43641
 
    sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
43642
 
)
43643
 
 
43644
 
/*
43645
 
** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
43646
 
** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
43647
 
** numbered from zero.
43648
 
**
43649
 
** If this call is successful, *ppPage is set to point to the wal-index
43650
 
** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
43651
 
** then an SQLite error code is returned and *ppPage is set to 0.
43652
 
*/
43653
 
static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
43654
 
  int rc = SQLITE_OK;
43655
 
 
43656
 
  /* Enlarge the pWal->apWiData[] array if required */
43657
 
  if( pWal->nWiData<=iPage ){
43658
 
    int nByte = sizeof(u32*)*(iPage+1);
43659
 
    volatile u32 **apNew;
43660
 
    apNew = (volatile u32 **)sqlite3_realloc((void *)pWal->apWiData, nByte);
43661
 
    if( !apNew ){
43662
 
      *ppPage = 0;
43663
 
      return SQLITE_NOMEM;
43664
 
    }
43665
 
    memset((void*)&apNew[pWal->nWiData], 0,
43666
 
           sizeof(u32*)*(iPage+1-pWal->nWiData));
43667
 
    pWal->apWiData = apNew;
43668
 
    pWal->nWiData = iPage+1;
43669
 
  }
43670
 
 
43671
 
  /* Request a pointer to the required page from the VFS */
43672
 
  if( pWal->apWiData[iPage]==0 ){
43673
 
    if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
43674
 
      pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
43675
 
      if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM;
43676
 
    }else{
43677
 
      rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
43678
 
          pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
43679
 
      );
43680
 
    }
43681
 
  }
43682
 
 
43683
 
  *ppPage = pWal->apWiData[iPage];
43684
 
  assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
43685
 
  return rc;
43686
 
}
43687
 
 
43688
 
/*
43689
 
** Return a pointer to the WalCkptInfo structure in the wal-index.
43690
 
*/
43691
 
static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
43692
 
  assert( pWal->nWiData>0 && pWal->apWiData[0] );
43693
 
  return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
43694
 
}
43695
 
 
43696
 
/*
43697
 
** Return a pointer to the WalIndexHdr structure in the wal-index.
43698
 
*/
43699
 
static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
43700
 
  assert( pWal->nWiData>0 && pWal->apWiData[0] );
43701
 
  return (volatile WalIndexHdr*)pWal->apWiData[0];
43702
 
}
43703
 
 
43704
 
/*
43705
 
** The argument to this macro must be of type u32. On a little-endian
43706
 
** architecture, it returns the u32 value that results from interpreting
43707
 
** the 4 bytes as a big-endian value. On a big-endian architecture, it
43708
 
** returns the value that would be produced by intepreting the 4 bytes
43709
 
** of the input value as a little-endian integer.
43710
 
*/
43711
 
#define BYTESWAP32(x) ( \
43712
 
    (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8)  \
43713
 
  + (((x)&0x00FF0000)>>8)  + (((x)&0xFF000000)>>24) \
43714
 
)
43715
 
 
43716
 
/*
43717
 
** Generate or extend an 8 byte checksum based on the data in 
43718
 
** array aByte[] and the initial values of aIn[0] and aIn[1] (or
43719
 
** initial values of 0 and 0 if aIn==NULL).
43720
 
**
43721
 
** The checksum is written back into aOut[] before returning.
43722
 
**
43723
 
** nByte must be a positive multiple of 8.
43724
 
*/
43725
 
static void walChecksumBytes(
43726
 
  int nativeCksum, /* True for native byte-order, false for non-native */
43727
 
  u8 *a,           /* Content to be checksummed */
43728
 
  int nByte,       /* Bytes of content in a[].  Must be a multiple of 8. */
43729
 
  const u32 *aIn,  /* Initial checksum value input */
43730
 
  u32 *aOut        /* OUT: Final checksum value output */
43731
 
){
43732
 
  u32 s1, s2;
43733
 
  u32 *aData = (u32 *)a;
43734
 
  u32 *aEnd = (u32 *)&a[nByte];
43735
 
 
43736
 
  if( aIn ){
43737
 
    s1 = aIn[0];
43738
 
    s2 = aIn[1];
43739
 
  }else{
43740
 
    s1 = s2 = 0;
43741
 
  }
43742
 
 
43743
 
  assert( nByte>=8 );
43744
 
  assert( (nByte&0x00000007)==0 );
43745
 
 
43746
 
  if( nativeCksum ){
43747
 
    do {
43748
 
      s1 += *aData++ + s2;
43749
 
      s2 += *aData++ + s1;
43750
 
    }while( aData<aEnd );
43751
 
  }else{
43752
 
    do {
43753
 
      s1 += BYTESWAP32(aData[0]) + s2;
43754
 
      s2 += BYTESWAP32(aData[1]) + s1;
43755
 
      aData += 2;
43756
 
    }while( aData<aEnd );
43757
 
  }
43758
 
 
43759
 
  aOut[0] = s1;
43760
 
  aOut[1] = s2;
43761
 
}
43762
 
 
43763
 
static void walShmBarrier(Wal *pWal){
43764
 
  if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
43765
 
    sqlite3OsShmBarrier(pWal->pDbFd);
43766
 
  }
43767
 
}
43768
 
 
43769
 
/*
43770
 
** Write the header information in pWal->hdr into the wal-index.
43771
 
**
43772
 
** The checksum on pWal->hdr is updated before it is written.
43773
 
*/
43774
 
static void walIndexWriteHdr(Wal *pWal){
43775
 
  volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
43776
 
  const int nCksum = offsetof(WalIndexHdr, aCksum);
43777
 
 
43778
 
  assert( pWal->writeLock );
43779
 
  pWal->hdr.isInit = 1;
43780
 
  pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
43781
 
  walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
43782
 
  memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
43783
 
  walShmBarrier(pWal);
43784
 
  memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
43785
 
}
43786
 
 
43787
 
/*
43788
 
** This function encodes a single frame header and writes it to a buffer
43789
 
** supplied by the caller. A frame-header is made up of a series of 
43790
 
** 4-byte big-endian integers, as follows:
43791
 
**
43792
 
**     0: Page number.
43793
 
**     4: For commit records, the size of the database image in pages 
43794
 
**        after the commit. For all other records, zero.
43795
 
**     8: Salt-1 (copied from the wal-header)
43796
 
**    12: Salt-2 (copied from the wal-header)
43797
 
**    16: Checksum-1.
43798
 
**    20: Checksum-2.
43799
 
*/
43800
 
static void walEncodeFrame(
43801
 
  Wal *pWal,                      /* The write-ahead log */
43802
 
  u32 iPage,                      /* Database page number for frame */
43803
 
  u32 nTruncate,                  /* New db size (or 0 for non-commit frames) */
43804
 
  u8 *aData,                      /* Pointer to page data */
43805
 
  u8 *aFrame                      /* OUT: Write encoded frame here */
43806
 
){
43807
 
  int nativeCksum;                /* True for native byte-order checksums */
43808
 
  u32 *aCksum = pWal->hdr.aFrameCksum;
43809
 
  assert( WAL_FRAME_HDRSIZE==24 );
43810
 
  sqlite3Put4byte(&aFrame[0], iPage);
43811
 
  sqlite3Put4byte(&aFrame[4], nTruncate);
43812
 
  memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
43813
 
 
43814
 
  nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
43815
 
  walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
43816
 
  walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
43817
 
 
43818
 
  sqlite3Put4byte(&aFrame[16], aCksum[0]);
43819
 
  sqlite3Put4byte(&aFrame[20], aCksum[1]);
43820
 
}
43821
 
 
43822
 
/*
43823
 
** Check to see if the frame with header in aFrame[] and content
43824
 
** in aData[] is valid.  If it is a valid frame, fill *piPage and
43825
 
** *pnTruncate and return true.  Return if the frame is not valid.
43826
 
*/
43827
 
static int walDecodeFrame(
43828
 
  Wal *pWal,                      /* The write-ahead log */
43829
 
  u32 *piPage,                    /* OUT: Database page number for frame */
43830
 
  u32 *pnTruncate,                /* OUT: New db size (or 0 if not commit) */
43831
 
  u8 *aData,                      /* Pointer to page data (for checksum) */
43832
 
  u8 *aFrame                      /* Frame data */
43833
 
){
43834
 
  int nativeCksum;                /* True for native byte-order checksums */
43835
 
  u32 *aCksum = pWal->hdr.aFrameCksum;
43836
 
  u32 pgno;                       /* Page number of the frame */
43837
 
  assert( WAL_FRAME_HDRSIZE==24 );
43838
 
 
43839
 
  /* A frame is only valid if the salt values in the frame-header
43840
 
  ** match the salt values in the wal-header. 
43841
 
  */
43842
 
  if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
43843
 
    return 0;
43844
 
  }
43845
 
 
43846
 
  /* A frame is only valid if the page number is creater than zero.
43847
 
  */
43848
 
  pgno = sqlite3Get4byte(&aFrame[0]);
43849
 
  if( pgno==0 ){
43850
 
    return 0;
43851
 
  }
43852
 
 
43853
 
  /* A frame is only valid if a checksum of the WAL header,
43854
 
  ** all prior frams, the first 16 bytes of this frame-header, 
43855
 
  ** and the frame-data matches the checksum in the last 8 
43856
 
  ** bytes of this frame-header.
43857
 
  */
43858
 
  nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
43859
 
  walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
43860
 
  walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
43861
 
  if( aCksum[0]!=sqlite3Get4byte(&aFrame[16]) 
43862
 
   || aCksum[1]!=sqlite3Get4byte(&aFrame[20]) 
43863
 
  ){
43864
 
    /* Checksum failed. */
43865
 
    return 0;
43866
 
  }
43867
 
 
43868
 
  /* If we reach this point, the frame is valid.  Return the page number
43869
 
  ** and the new database size.
43870
 
  */
43871
 
  *piPage = pgno;
43872
 
  *pnTruncate = sqlite3Get4byte(&aFrame[4]);
43873
 
  return 1;
43874
 
}
43875
 
 
43876
 
 
43877
 
#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
43878
 
/*
43879
 
** Names of locks.  This routine is used to provide debugging output and is not
43880
 
** a part of an ordinary build.
43881
 
*/
43882
 
static const char *walLockName(int lockIdx){
43883
 
  if( lockIdx==WAL_WRITE_LOCK ){
43884
 
    return "WRITE-LOCK";
43885
 
  }else if( lockIdx==WAL_CKPT_LOCK ){
43886
 
    return "CKPT-LOCK";
43887
 
  }else if( lockIdx==WAL_RECOVER_LOCK ){
43888
 
    return "RECOVER-LOCK";
43889
 
  }else{
43890
 
    static char zName[15];
43891
 
    sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
43892
 
                     lockIdx-WAL_READ_LOCK(0));
43893
 
    return zName;
43894
 
  }
43895
 
}
43896
 
#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
43897
 
    
43898
 
 
43899
 
/*
43900
 
** Set or release locks on the WAL.  Locks are either shared or exclusive.
43901
 
** A lock cannot be moved directly between shared and exclusive - it must go
43902
 
** through the unlocked state first.
43903
 
**
43904
 
** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
43905
 
*/
43906
 
static int walLockShared(Wal *pWal, int lockIdx){
43907
 
  int rc;
43908
 
  if( pWal->exclusiveMode ) return SQLITE_OK;
43909
 
  rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
43910
 
                        SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
43911
 
  WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
43912
 
            walLockName(lockIdx), rc ? "failed" : "ok"));
43913
 
  VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
43914
 
  return rc;
43915
 
}
43916
 
static void walUnlockShared(Wal *pWal, int lockIdx){
43917
 
  if( pWal->exclusiveMode ) return;
43918
 
  (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
43919
 
                         SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
43920
 
  WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
43921
 
}
43922
 
static int walLockExclusive(Wal *pWal, int lockIdx, int n){
43923
 
  int rc;
43924
 
  if( pWal->exclusiveMode ) return SQLITE_OK;
43925
 
  rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
43926
 
                        SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
43927
 
  WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
43928
 
            walLockName(lockIdx), n, rc ? "failed" : "ok"));
43929
 
  VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
43930
 
  return rc;
43931
 
}
43932
 
static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
43933
 
  if( pWal->exclusiveMode ) return;
43934
 
  (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
43935
 
                         SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
43936
 
  WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
43937
 
             walLockName(lockIdx), n));
43938
 
}
43939
 
 
43940
 
/*
43941
 
** Compute a hash on a page number.  The resulting hash value must land
43942
 
** between 0 and (HASHTABLE_NSLOT-1).  The walHashNext() function advances
43943
 
** the hash to the next value in the event of a collision.
43944
 
*/
43945
 
static int walHash(u32 iPage){
43946
 
  assert( iPage>0 );
43947
 
  assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
43948
 
  return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
43949
 
}
43950
 
static int walNextHash(int iPriorHash){
43951
 
  return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
43952
 
}
43953
 
 
43954
 
/* 
43955
 
** Return pointers to the hash table and page number array stored on
43956
 
** page iHash of the wal-index. The wal-index is broken into 32KB pages
43957
 
** numbered starting from 0.
43958
 
**
43959
 
** Set output variable *paHash to point to the start of the hash table
43960
 
** in the wal-index file. Set *piZero to one less than the frame 
43961
 
** number of the first frame indexed by this hash table. If a
43962
 
** slot in the hash table is set to N, it refers to frame number 
43963
 
** (*piZero+N) in the log.
43964
 
**
43965
 
** Finally, set *paPgno so that *paPgno[1] is the page number of the
43966
 
** first frame indexed by the hash table, frame (*piZero+1).
43967
 
*/
43968
 
static int walHashGet(
43969
 
  Wal *pWal,                      /* WAL handle */
43970
 
  int iHash,                      /* Find the iHash'th table */
43971
 
  volatile ht_slot **paHash,      /* OUT: Pointer to hash index */
43972
 
  volatile u32 **paPgno,          /* OUT: Pointer to page number array */
43973
 
  u32 *piZero                     /* OUT: Frame associated with *paPgno[0] */
43974
 
){
43975
 
  int rc;                         /* Return code */
43976
 
  volatile u32 *aPgno;
43977
 
 
43978
 
  rc = walIndexPage(pWal, iHash, &aPgno);
43979
 
  assert( rc==SQLITE_OK || iHash>0 );
43980
 
 
43981
 
  if( rc==SQLITE_OK ){
43982
 
    u32 iZero;
43983
 
    volatile ht_slot *aHash;
43984
 
 
43985
 
    aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE];
43986
 
    if( iHash==0 ){
43987
 
      aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
43988
 
      iZero = 0;
43989
 
    }else{
43990
 
      iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
43991
 
    }
43992
 
  
43993
 
    *paPgno = &aPgno[-1];
43994
 
    *paHash = aHash;
43995
 
    *piZero = iZero;
43996
 
  }
43997
 
  return rc;
43998
 
}
43999
 
 
44000
 
/*
44001
 
** Return the number of the wal-index page that contains the hash-table
44002
 
** and page-number array that contain entries corresponding to WAL frame
44003
 
** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages 
44004
 
** are numbered starting from 0.
44005
 
*/
44006
 
static int walFramePage(u32 iFrame){
44007
 
  int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
44008
 
  assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
44009
 
       && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
44010
 
       && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
44011
 
       && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
44012
 
       && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
44013
 
  );
44014
 
  return iHash;
44015
 
}
44016
 
 
44017
 
/*
44018
 
** Return the page number associated with frame iFrame in this WAL.
44019
 
*/
44020
 
static u32 walFramePgno(Wal *pWal, u32 iFrame){
44021
 
  int iHash = walFramePage(iFrame);
44022
 
  if( iHash==0 ){
44023
 
    return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
44024
 
  }
44025
 
  return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
44026
 
}
44027
 
 
44028
 
/*
44029
 
** Remove entries from the hash table that point to WAL slots greater
44030
 
** than pWal->hdr.mxFrame.
44031
 
**
44032
 
** This function is called whenever pWal->hdr.mxFrame is decreased due
44033
 
** to a rollback or savepoint.
44034
 
**
44035
 
** At most only the hash table containing pWal->hdr.mxFrame needs to be
44036
 
** updated.  Any later hash tables will be automatically cleared when
44037
 
** pWal->hdr.mxFrame advances to the point where those hash tables are
44038
 
** actually needed.
44039
 
*/
44040
 
static void walCleanupHash(Wal *pWal){
44041
 
  volatile ht_slot *aHash = 0;    /* Pointer to hash table to clear */
44042
 
  volatile u32 *aPgno = 0;        /* Page number array for hash table */
44043
 
  u32 iZero = 0;                  /* frame == (aHash[x]+iZero) */
44044
 
  int iLimit = 0;                 /* Zero values greater than this */
44045
 
  int nByte;                      /* Number of bytes to zero in aPgno[] */
44046
 
  int i;                          /* Used to iterate through aHash[] */
44047
 
 
44048
 
  assert( pWal->writeLock );
44049
 
  testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
44050
 
  testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
44051
 
  testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
44052
 
 
44053
 
  if( pWal->hdr.mxFrame==0 ) return;
44054
 
 
44055
 
  /* Obtain pointers to the hash-table and page-number array containing 
44056
 
  ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
44057
 
  ** that the page said hash-table and array reside on is already mapped.
44058
 
  */
44059
 
  assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
44060
 
  assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
44061
 
  walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
44062
 
 
44063
 
  /* Zero all hash-table entries that correspond to frame numbers greater
44064
 
  ** than pWal->hdr.mxFrame.
44065
 
  */
44066
 
  iLimit = pWal->hdr.mxFrame - iZero;
44067
 
  assert( iLimit>0 );
44068
 
  for(i=0; i<HASHTABLE_NSLOT; i++){
44069
 
    if( aHash[i]>iLimit ){
44070
 
      aHash[i] = 0;
44071
 
    }
44072
 
  }
44073
 
  
44074
 
  /* Zero the entries in the aPgno array that correspond to frames with
44075
 
  ** frame numbers greater than pWal->hdr.mxFrame. 
44076
 
  */
44077
 
  nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]);
44078
 
  memset((void *)&aPgno[iLimit+1], 0, nByte);
44079
 
 
44080
 
#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
44081
 
  /* Verify that the every entry in the mapping region is still reachable
44082
 
  ** via the hash table even after the cleanup.
44083
 
  */
44084
 
  if( iLimit ){
44085
 
    int i;           /* Loop counter */
44086
 
    int iKey;        /* Hash key */
44087
 
    for(i=1; i<=iLimit; i++){
44088
 
      for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
44089
 
        if( aHash[iKey]==i ) break;
44090
 
      }
44091
 
      assert( aHash[iKey]==i );
44092
 
    }
44093
 
  }
44094
 
#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
44095
 
}
44096
 
 
44097
 
 
44098
 
/*
44099
 
** Set an entry in the wal-index that will map database page number
44100
 
** pPage into WAL frame iFrame.
44101
 
*/
44102
 
static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
44103
 
  int rc;                         /* Return code */
44104
 
  u32 iZero = 0;                  /* One less than frame number of aPgno[1] */
44105
 
  volatile u32 *aPgno = 0;        /* Page number array */
44106
 
  volatile ht_slot *aHash = 0;    /* Hash table */
44107
 
 
44108
 
  rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);
44109
 
 
44110
 
  /* Assuming the wal-index file was successfully mapped, populate the
44111
 
  ** page number array and hash table entry.
44112
 
  */
44113
 
  if( rc==SQLITE_OK ){
44114
 
    int iKey;                     /* Hash table key */
44115
 
    int idx;                      /* Value to write to hash-table slot */
44116
 
    int nCollide;                 /* Number of hash collisions */
44117
 
 
44118
 
    idx = iFrame - iZero;
44119
 
    assert( idx <= HASHTABLE_NSLOT/2 + 1 );
44120
 
    
44121
 
    /* If this is the first entry to be added to this hash-table, zero the
44122
 
    ** entire hash table and aPgno[] array before proceding. 
44123
 
    */
44124
 
    if( idx==1 ){
44125
 
      int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
44126
 
      memset((void*)&aPgno[1], 0, nByte);
44127
 
    }
44128
 
 
44129
 
    /* If the entry in aPgno[] is already set, then the previous writer
44130
 
    ** must have exited unexpectedly in the middle of a transaction (after
44131
 
    ** writing one or more dirty pages to the WAL to free up memory). 
44132
 
    ** Remove the remnants of that writers uncommitted transaction from 
44133
 
    ** the hash-table before writing any new entries.
44134
 
    */
44135
 
    if( aPgno[idx] ){
44136
 
      walCleanupHash(pWal);
44137
 
      assert( !aPgno[idx] );
44138
 
    }
44139
 
 
44140
 
    /* Write the aPgno[] array entry and the hash-table slot. */
44141
 
    nCollide = idx;
44142
 
    for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
44143
 
      if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
44144
 
    }
44145
 
    aPgno[idx] = iPage;
44146
 
    aHash[iKey] = (ht_slot)idx;
44147
 
 
44148
 
#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
44149
 
    /* Verify that the number of entries in the hash table exactly equals
44150
 
    ** the number of entries in the mapping region.
44151
 
    */
44152
 
    {
44153
 
      int i;           /* Loop counter */
44154
 
      int nEntry = 0;  /* Number of entries in the hash table */
44155
 
      for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
44156
 
      assert( nEntry==idx );
44157
 
    }
44158
 
 
44159
 
    /* Verify that the every entry in the mapping region is reachable
44160
 
    ** via the hash table.  This turns out to be a really, really expensive
44161
 
    ** thing to check, so only do this occasionally - not on every
44162
 
    ** iteration.
44163
 
    */
44164
 
    if( (idx&0x3ff)==0 ){
44165
 
      int i;           /* Loop counter */
44166
 
      for(i=1; i<=idx; i++){
44167
 
        for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
44168
 
          if( aHash[iKey]==i ) break;
44169
 
        }
44170
 
        assert( aHash[iKey]==i );
44171
 
      }
44172
 
    }
44173
 
#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
44174
 
  }
44175
 
 
44176
 
 
44177
 
  return rc;
44178
 
}
44179
 
 
44180
 
 
44181
 
/*
44182
 
** Recover the wal-index by reading the write-ahead log file. 
44183
 
**
44184
 
** This routine first tries to establish an exclusive lock on the
44185
 
** wal-index to prevent other threads/processes from doing anything
44186
 
** with the WAL or wal-index while recovery is running.  The
44187
 
** WAL_RECOVER_LOCK is also held so that other threads will know
44188
 
** that this thread is running recovery.  If unable to establish
44189
 
** the necessary locks, this routine returns SQLITE_BUSY.
44190
 
*/
44191
 
static int walIndexRecover(Wal *pWal){
44192
 
  int rc;                         /* Return Code */
44193
 
  i64 nSize;                      /* Size of log file */
44194
 
  u32 aFrameCksum[2] = {0, 0};
44195
 
  int iLock;                      /* Lock offset to lock for checkpoint */
44196
 
  int nLock;                      /* Number of locks to hold */
44197
 
 
44198
 
  /* Obtain an exclusive lock on all byte in the locking range not already
44199
 
  ** locked by the caller. The caller is guaranteed to have locked the
44200
 
  ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
44201
 
  ** If successful, the same bytes that are locked here are unlocked before
44202
 
  ** this function returns.
44203
 
  */
44204
 
  assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
44205
 
  assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
44206
 
  assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
44207
 
  assert( pWal->writeLock );
44208
 
  iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
44209
 
  nLock = SQLITE_SHM_NLOCK - iLock;
44210
 
  rc = walLockExclusive(pWal, iLock, nLock);
44211
 
  if( rc ){
44212
 
    return rc;
44213
 
  }
44214
 
  WALTRACE(("WAL%p: recovery begin...\n", pWal));
44215
 
 
44216
 
  memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
44217
 
 
44218
 
  rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
44219
 
  if( rc!=SQLITE_OK ){
44220
 
    goto recovery_error;
44221
 
  }
44222
 
 
44223
 
  if( nSize>WAL_HDRSIZE ){
44224
 
    u8 aBuf[WAL_HDRSIZE];         /* Buffer to load WAL header into */
44225
 
    u8 *aFrame = 0;               /* Malloc'd buffer to load entire frame */
44226
 
    int szFrame;                  /* Number of bytes in buffer aFrame[] */
44227
 
    u8 *aData;                    /* Pointer to data part of aFrame buffer */
44228
 
    int iFrame;                   /* Index of last frame read */
44229
 
    i64 iOffset;                  /* Next offset to read from log file */
44230
 
    int szPage;                   /* Page size according to the log */
44231
 
    u32 magic;                    /* Magic value read from WAL header */
44232
 
    u32 version;                  /* Magic value read from WAL header */
44233
 
 
44234
 
    /* Read in the WAL header. */
44235
 
    rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
44236
 
    if( rc!=SQLITE_OK ){
44237
 
      goto recovery_error;
44238
 
    }
44239
 
 
44240
 
    /* If the database page size is not a power of two, or is greater than
44241
 
    ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid 
44242
 
    ** data. Similarly, if the 'magic' value is invalid, ignore the whole
44243
 
    ** WAL file.
44244
 
    */
44245
 
    magic = sqlite3Get4byte(&aBuf[0]);
44246
 
    szPage = sqlite3Get4byte(&aBuf[8]);
44247
 
    if( (magic&0xFFFFFFFE)!=WAL_MAGIC 
44248
 
     || szPage&(szPage-1) 
44249
 
     || szPage>SQLITE_MAX_PAGE_SIZE 
44250
 
     || szPage<512 
44251
 
    ){
44252
 
      goto finished;
44253
 
    }
44254
 
    pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
44255
 
    pWal->szPage = szPage;
44256
 
    pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
44257
 
    memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
44258
 
 
44259
 
    /* Verify that the WAL header checksum is correct */
44260
 
    walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN, 
44261
 
        aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
44262
 
    );
44263
 
    if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
44264
 
     || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
44265
 
    ){
44266
 
      goto finished;
44267
 
    }
44268
 
 
44269
 
    /* Verify that the version number on the WAL format is one that
44270
 
    ** are able to understand */
44271
 
    version = sqlite3Get4byte(&aBuf[4]);
44272
 
    if( version!=WAL_MAX_VERSION ){
44273
 
      rc = SQLITE_CANTOPEN_BKPT;
44274
 
      goto finished;
44275
 
    }
44276
 
 
44277
 
    /* Malloc a buffer to read frames into. */
44278
 
    szFrame = szPage + WAL_FRAME_HDRSIZE;
44279
 
    aFrame = (u8 *)sqlite3_malloc(szFrame);
44280
 
    if( !aFrame ){
44281
 
      rc = SQLITE_NOMEM;
44282
 
      goto recovery_error;
44283
 
    }
44284
 
    aData = &aFrame[WAL_FRAME_HDRSIZE];
44285
 
 
44286
 
    /* Read all frames from the log file. */
44287
 
    iFrame = 0;
44288
 
    for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
44289
 
      u32 pgno;                   /* Database page number for frame */
44290
 
      u32 nTruncate;              /* dbsize field from frame header */
44291
 
      int isValid;                /* True if this frame is valid */
44292
 
 
44293
 
      /* Read and decode the next log frame. */
44294
 
      rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
44295
 
      if( rc!=SQLITE_OK ) break;
44296
 
      isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
44297
 
      if( !isValid ) break;
44298
 
      rc = walIndexAppend(pWal, ++iFrame, pgno);
44299
 
      if( rc!=SQLITE_OK ) break;
44300
 
 
44301
 
      /* If nTruncate is non-zero, this is a commit record. */
44302
 
      if( nTruncate ){
44303
 
        pWal->hdr.mxFrame = iFrame;
44304
 
        pWal->hdr.nPage = nTruncate;
44305
 
        pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
44306
 
        testcase( szPage<=32768 );
44307
 
        testcase( szPage>=65536 );
44308
 
        aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
44309
 
        aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
44310
 
      }
44311
 
    }
44312
 
 
44313
 
    sqlite3_free(aFrame);
44314
 
  }
44315
 
 
44316
 
finished:
44317
 
  if( rc==SQLITE_OK ){
44318
 
    volatile WalCkptInfo *pInfo;
44319
 
    int i;
44320
 
    pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
44321
 
    pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
44322
 
    walIndexWriteHdr(pWal);
44323
 
 
44324
 
    /* Reset the checkpoint-header. This is safe because this thread is 
44325
 
    ** currently holding locks that exclude all other readers, writers and
44326
 
    ** checkpointers.
44327
 
    */
44328
 
    pInfo = walCkptInfo(pWal);
44329
 
    pInfo->nBackfill = 0;
44330
 
    pInfo->aReadMark[0] = 0;
44331
 
    for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
44332
 
 
44333
 
    /* If more than one frame was recovered from the log file, report an
44334
 
    ** event via sqlite3_log(). This is to help with identifying performance
44335
 
    ** problems caused by applications routinely shutting down without
44336
 
    ** checkpointing the log file.
44337
 
    */
44338
 
    if( pWal->hdr.nPage ){
44339
 
      sqlite3_log(SQLITE_OK, "Recovered %d frames from WAL file %s",
44340
 
          pWal->hdr.nPage, pWal->zWalName
44341
 
      );
44342
 
    }
44343
 
  }
44344
 
 
44345
 
recovery_error:
44346
 
  WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
44347
 
  walUnlockExclusive(pWal, iLock, nLock);
44348
 
  return rc;
44349
 
}
44350
 
 
44351
 
/*
44352
 
** Close an open wal-index.
44353
 
*/
44354
 
static void walIndexClose(Wal *pWal, int isDelete){
44355
 
  if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
44356
 
    int i;
44357
 
    for(i=0; i<pWal->nWiData; i++){
44358
 
      sqlite3_free((void *)pWal->apWiData[i]);
44359
 
      pWal->apWiData[i] = 0;
44360
 
    }
44361
 
  }else{
44362
 
    sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
44363
 
  }
44364
 
}
44365
 
 
44366
 
/* 
44367
 
** Open a connection to the WAL file zWalName. The database file must 
44368
 
** already be opened on connection pDbFd. The buffer that zWalName points
44369
 
** to must remain valid for the lifetime of the returned Wal* handle.
44370
 
**
44371
 
** A SHARED lock should be held on the database file when this function
44372
 
** is called. The purpose of this SHARED lock is to prevent any other
44373
 
** client from unlinking the WAL or wal-index file. If another process
44374
 
** were to do this just after this client opened one of these files, the
44375
 
** system would be badly broken.
44376
 
**
44377
 
** If the log file is successfully opened, SQLITE_OK is returned and 
44378
 
** *ppWal is set to point to a new WAL handle. If an error occurs,
44379
 
** an SQLite error code is returned and *ppWal is left unmodified.
44380
 
*/
44381
 
SQLITE_PRIVATE int sqlite3WalOpen(
44382
 
  sqlite3_vfs *pVfs,              /* vfs module to open wal and wal-index */
44383
 
  sqlite3_file *pDbFd,            /* The open database file */
44384
 
  const char *zWalName,           /* Name of the WAL file */
44385
 
  int bNoShm,                     /* True to run in heap-memory mode */
44386
 
  Wal **ppWal                     /* OUT: Allocated Wal handle */
44387
 
){
44388
 
  int rc;                         /* Return Code */
44389
 
  Wal *pRet;                      /* Object to allocate and return */
44390
 
  int flags;                      /* Flags passed to OsOpen() */
44391
 
 
44392
 
  assert( zWalName && zWalName[0] );
44393
 
  assert( pDbFd );
44394
 
 
44395
 
  /* In the amalgamation, the os_unix.c and os_win.c source files come before
44396
 
  ** this source file.  Verify that the #defines of the locking byte offsets
44397
 
  ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
44398
 
  */
44399
 
#ifdef WIN_SHM_BASE
44400
 
  assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
44401
 
#endif
44402
 
#ifdef UNIX_SHM_BASE
44403
 
  assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
44404
 
#endif
44405
 
 
44406
 
 
44407
 
  /* Allocate an instance of struct Wal to return. */
44408
 
  *ppWal = 0;
44409
 
  pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
44410
 
  if( !pRet ){
44411
 
    return SQLITE_NOMEM;
44412
 
  }
44413
 
 
44414
 
  pRet->pVfs = pVfs;
44415
 
  pRet->pWalFd = (sqlite3_file *)&pRet[1];
44416
 
  pRet->pDbFd = pDbFd;
44417
 
  pRet->readLock = -1;
44418
 
  pRet->zWalName = zWalName;
44419
 
  pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
44420
 
 
44421
 
  /* Open file handle on the write-ahead log file. */
44422
 
  flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
44423
 
  rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
44424
 
  if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
44425
 
    pRet->readOnly = 1;
44426
 
  }
44427
 
 
44428
 
  if( rc!=SQLITE_OK ){
44429
 
    walIndexClose(pRet, 0);
44430
 
    sqlite3OsClose(pRet->pWalFd);
44431
 
    sqlite3_free(pRet);
44432
 
  }else{
44433
 
    *ppWal = pRet;
44434
 
    WALTRACE(("WAL%d: opened\n", pRet));
44435
 
  }
44436
 
  return rc;
44437
 
}
44438
 
 
44439
 
/*
44440
 
** Find the smallest page number out of all pages held in the WAL that
44441
 
** has not been returned by any prior invocation of this method on the
44442
 
** same WalIterator object.   Write into *piFrame the frame index where
44443
 
** that page was last written into the WAL.  Write into *piPage the page
44444
 
** number.
44445
 
**
44446
 
** Return 0 on success.  If there are no pages in the WAL with a page
44447
 
** number larger than *piPage, then return 1.
44448
 
*/
44449
 
static int walIteratorNext(
44450
 
  WalIterator *p,               /* Iterator */
44451
 
  u32 *piPage,                  /* OUT: The page number of the next page */
44452
 
  u32 *piFrame                  /* OUT: Wal frame index of next page */
44453
 
){
44454
 
  u32 iMin;                     /* Result pgno must be greater than iMin */
44455
 
  u32 iRet = 0xFFFFFFFF;        /* 0xffffffff is never a valid page number */
44456
 
  int i;                        /* For looping through segments */
44457
 
 
44458
 
  iMin = p->iPrior;
44459
 
  assert( iMin<0xffffffff );
44460
 
  for(i=p->nSegment-1; i>=0; i--){
44461
 
    struct WalSegment *pSegment = &p->aSegment[i];
44462
 
    while( pSegment->iNext<pSegment->nEntry ){
44463
 
      u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
44464
 
      if( iPg>iMin ){
44465
 
        if( iPg<iRet ){
44466
 
          iRet = iPg;
44467
 
          *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
44468
 
        }
44469
 
        break;
44470
 
      }
44471
 
      pSegment->iNext++;
44472
 
    }
44473
 
  }
44474
 
 
44475
 
  *piPage = p->iPrior = iRet;
44476
 
  return (iRet==0xFFFFFFFF);
44477
 
}
44478
 
 
44479
 
/*
44480
 
** This function merges two sorted lists into a single sorted list.
44481
 
**
44482
 
** aLeft[] and aRight[] are arrays of indices.  The sort key is
44483
 
** aContent[aLeft[]] and aContent[aRight[]].  Upon entry, the following
44484
 
** is guaranteed for all J<K:
44485
 
**
44486
 
**        aContent[aLeft[J]] < aContent[aLeft[K]]
44487
 
**        aContent[aRight[J]] < aContent[aRight[K]]
44488
 
**
44489
 
** This routine overwrites aRight[] with a new (probably longer) sequence
44490
 
** of indices such that the aRight[] contains every index that appears in
44491
 
** either aLeft[] or the old aRight[] and such that the second condition
44492
 
** above is still met.
44493
 
**
44494
 
** The aContent[aLeft[X]] values will be unique for all X.  And the
44495
 
** aContent[aRight[X]] values will be unique too.  But there might be
44496
 
** one or more combinations of X and Y such that
44497
 
**
44498
 
**      aLeft[X]!=aRight[Y]  &&  aContent[aLeft[X]] == aContent[aRight[Y]]
44499
 
**
44500
 
** When that happens, omit the aLeft[X] and use the aRight[Y] index.
44501
 
*/
44502
 
static void walMerge(
44503
 
  const u32 *aContent,            /* Pages in wal - keys for the sort */
44504
 
  ht_slot *aLeft,                 /* IN: Left hand input list */
44505
 
  int nLeft,                      /* IN: Elements in array *paLeft */
44506
 
  ht_slot **paRight,              /* IN/OUT: Right hand input list */
44507
 
  int *pnRight,                   /* IN/OUT: Elements in *paRight */
44508
 
  ht_slot *aTmp                   /* Temporary buffer */
44509
 
){
44510
 
  int iLeft = 0;                  /* Current index in aLeft */
44511
 
  int iRight = 0;                 /* Current index in aRight */
44512
 
  int iOut = 0;                   /* Current index in output buffer */
44513
 
  int nRight = *pnRight;
44514
 
  ht_slot *aRight = *paRight;
44515
 
 
44516
 
  assert( nLeft>0 && nRight>0 );
44517
 
  while( iRight<nRight || iLeft<nLeft ){
44518
 
    ht_slot logpage;
44519
 
    Pgno dbpage;
44520
 
 
44521
 
    if( (iLeft<nLeft) 
44522
 
     && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
44523
 
    ){
44524
 
      logpage = aLeft[iLeft++];
44525
 
    }else{
44526
 
      logpage = aRight[iRight++];
44527
 
    }
44528
 
    dbpage = aContent[logpage];
44529
 
 
44530
 
    aTmp[iOut++] = logpage;
44531
 
    if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
44532
 
 
44533
 
    assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
44534
 
    assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
44535
 
  }
44536
 
 
44537
 
  *paRight = aLeft;
44538
 
  *pnRight = iOut;
44539
 
  memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
44540
 
}
44541
 
 
44542
 
/*
44543
 
** Sort the elements in list aList using aContent[] as the sort key.
44544
 
** Remove elements with duplicate keys, preferring to keep the
44545
 
** larger aList[] values.
44546
 
**
44547
 
** The aList[] entries are indices into aContent[].  The values in
44548
 
** aList[] are to be sorted so that for all J<K:
44549
 
**
44550
 
**      aContent[aList[J]] < aContent[aList[K]]
44551
 
**
44552
 
** For any X and Y such that
44553
 
**
44554
 
**      aContent[aList[X]] == aContent[aList[Y]]
44555
 
**
44556
 
** Keep the larger of the two values aList[X] and aList[Y] and discard
44557
 
** the smaller.
44558
 
*/
44559
 
static void walMergesort(
44560
 
  const u32 *aContent,            /* Pages in wal */
44561
 
  ht_slot *aBuffer,               /* Buffer of at least *pnList items to use */
44562
 
  ht_slot *aList,                 /* IN/OUT: List to sort */
44563
 
  int *pnList                     /* IN/OUT: Number of elements in aList[] */
44564
 
){
44565
 
  struct Sublist {
44566
 
    int nList;                    /* Number of elements in aList */
44567
 
    ht_slot *aList;               /* Pointer to sub-list content */
44568
 
  };
44569
 
 
44570
 
  const int nList = *pnList;      /* Size of input list */
44571
 
  int nMerge = 0;                 /* Number of elements in list aMerge */
44572
 
  ht_slot *aMerge = 0;            /* List to be merged */
44573
 
  int iList;                      /* Index into input list */
44574
 
  int iSub = 0;                   /* Index into aSub array */
44575
 
  struct Sublist aSub[13];        /* Array of sub-lists */
44576
 
 
44577
 
  memset(aSub, 0, sizeof(aSub));
44578
 
  assert( nList<=HASHTABLE_NPAGE && nList>0 );
44579
 
  assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
44580
 
 
44581
 
  for(iList=0; iList<nList; iList++){
44582
 
    nMerge = 1;
44583
 
    aMerge = &aList[iList];
44584
 
    for(iSub=0; iList & (1<<iSub); iSub++){
44585
 
      struct Sublist *p = &aSub[iSub];
44586
 
      assert( p->aList && p->nList<=(1<<iSub) );
44587
 
      assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
44588
 
      walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
44589
 
    }
44590
 
    aSub[iSub].aList = aMerge;
44591
 
    aSub[iSub].nList = nMerge;
44592
 
  }
44593
 
 
44594
 
  for(iSub++; iSub<ArraySize(aSub); iSub++){
44595
 
    if( nList & (1<<iSub) ){
44596
 
      struct Sublist *p = &aSub[iSub];
44597
 
      assert( p->nList<=(1<<iSub) );
44598
 
      assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
44599
 
      walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
44600
 
    }
44601
 
  }
44602
 
  assert( aMerge==aList );
44603
 
  *pnList = nMerge;
44604
 
 
44605
 
#ifdef SQLITE_DEBUG
44606
 
  {
44607
 
    int i;
44608
 
    for(i=1; i<*pnList; i++){
44609
 
      assert( aContent[aList[i]] > aContent[aList[i-1]] );
44610
 
    }
44611
 
  }
44612
 
#endif
44613
 
}
44614
 
 
44615
 
/* 
44616
 
** Free an iterator allocated by walIteratorInit().
44617
 
*/
44618
 
static void walIteratorFree(WalIterator *p){
44619
 
  sqlite3ScratchFree(p);
44620
 
}
44621
 
 
44622
 
/*
44623
 
** Construct a WalInterator object that can be used to loop over all 
44624
 
** pages in the WAL in ascending order. The caller must hold the checkpoint
44625
 
** lock.
44626
 
**
44627
 
** On success, make *pp point to the newly allocated WalInterator object
44628
 
** return SQLITE_OK. Otherwise, return an error code. If this routine
44629
 
** returns an error, the value of *pp is undefined.
44630
 
**
44631
 
** The calling routine should invoke walIteratorFree() to destroy the
44632
 
** WalIterator object when it has finished with it.
44633
 
*/
44634
 
static int walIteratorInit(Wal *pWal, WalIterator **pp){
44635
 
  WalIterator *p;                 /* Return value */
44636
 
  int nSegment;                   /* Number of segments to merge */
44637
 
  u32 iLast;                      /* Last frame in log */
44638
 
  int nByte;                      /* Number of bytes to allocate */
44639
 
  int i;                          /* Iterator variable */
44640
 
  ht_slot *aTmp;                  /* Temp space used by merge-sort */
44641
 
  int rc = SQLITE_OK;             /* Return Code */
44642
 
 
44643
 
  /* This routine only runs while holding the checkpoint lock. And
44644
 
  ** it only runs if there is actually content in the log (mxFrame>0).
44645
 
  */
44646
 
  assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
44647
 
  iLast = pWal->hdr.mxFrame;
44648
 
 
44649
 
  /* Allocate space for the WalIterator object. */
44650
 
  nSegment = walFramePage(iLast) + 1;
44651
 
  nByte = sizeof(WalIterator) 
44652
 
        + (nSegment-1)*sizeof(struct WalSegment)
44653
 
        + iLast*sizeof(ht_slot);
44654
 
  p = (WalIterator *)sqlite3ScratchMalloc(nByte);
44655
 
  if( !p ){
44656
 
    return SQLITE_NOMEM;
44657
 
  }
44658
 
  memset(p, 0, nByte);
44659
 
  p->nSegment = nSegment;
44660
 
 
44661
 
  /* Allocate temporary space used by the merge-sort routine. This block
44662
 
  ** of memory will be freed before this function returns.
44663
 
  */
44664
 
  aTmp = (ht_slot *)sqlite3ScratchMalloc(
44665
 
      sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
44666
 
  );
44667
 
  if( !aTmp ){
44668
 
    rc = SQLITE_NOMEM;
44669
 
  }
44670
 
 
44671
 
  for(i=0; rc==SQLITE_OK && i<nSegment; i++){
44672
 
    volatile ht_slot *aHash;
44673
 
    u32 iZero;
44674
 
    volatile u32 *aPgno;
44675
 
 
44676
 
    rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
44677
 
    if( rc==SQLITE_OK ){
44678
 
      int j;                      /* Counter variable */
44679
 
      int nEntry;                 /* Number of entries in this segment */
44680
 
      ht_slot *aIndex;            /* Sorted index for this segment */
44681
 
 
44682
 
      aPgno++;
44683
 
      if( (i+1)==nSegment ){
44684
 
        nEntry = (int)(iLast - iZero);
44685
 
      }else{
44686
 
        nEntry = (int)((u32*)aHash - (u32*)aPgno);
44687
 
      }
44688
 
      aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
44689
 
      iZero++;
44690
 
  
44691
 
      for(j=0; j<nEntry; j++){
44692
 
        aIndex[j] = (ht_slot)j;
44693
 
      }
44694
 
      walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry);
44695
 
      p->aSegment[i].iZero = iZero;
44696
 
      p->aSegment[i].nEntry = nEntry;
44697
 
      p->aSegment[i].aIndex = aIndex;
44698
 
      p->aSegment[i].aPgno = (u32 *)aPgno;
44699
 
    }
44700
 
  }
44701
 
  sqlite3ScratchFree(aTmp);
44702
 
 
44703
 
  if( rc!=SQLITE_OK ){
44704
 
    walIteratorFree(p);
44705
 
  }
44706
 
  *pp = p;
44707
 
  return rc;
44708
 
}
44709
 
 
44710
 
/*
44711
 
** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
44712
 
** n. If the attempt fails and parameter xBusy is not NULL, then it is a
44713
 
** busy-handler function. Invoke it and retry the lock until either the
44714
 
** lock is successfully obtained or the busy-handler returns 0.
44715
 
*/
44716
 
static int walBusyLock(
44717
 
  Wal *pWal,                      /* WAL connection */
44718
 
  int (*xBusy)(void*),            /* Function to call when busy */
44719
 
  void *pBusyArg,                 /* Context argument for xBusyHandler */
44720
 
  int lockIdx,                    /* Offset of first byte to lock */
44721
 
  int n                           /* Number of bytes to lock */
44722
 
){
44723
 
  int rc;
44724
 
  do {
44725
 
    rc = walLockExclusive(pWal, lockIdx, n);
44726
 
  }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
44727
 
  return rc;
44728
 
}
44729
 
 
44730
 
/*
44731
 
** The cache of the wal-index header must be valid to call this function.
44732
 
** Return the page-size in bytes used by the database.
44733
 
*/
44734
 
static int walPagesize(Wal *pWal){
44735
 
  return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
44736
 
}
44737
 
 
44738
 
/*
44739
 
** Copy as much content as we can from the WAL back into the database file
44740
 
** in response to an sqlite3_wal_checkpoint() request or the equivalent.
44741
 
**
44742
 
** The amount of information copies from WAL to database might be limited
44743
 
** by active readers.  This routine will never overwrite a database page
44744
 
** that a concurrent reader might be using.
44745
 
**
44746
 
** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
44747
 
** SQLite is in WAL-mode in synchronous=NORMAL.  That means that if 
44748
 
** checkpoints are always run by a background thread or background 
44749
 
** process, foreground threads will never block on a lengthy fsync call.
44750
 
**
44751
 
** Fsync is called on the WAL before writing content out of the WAL and
44752
 
** into the database.  This ensures that if the new content is persistent
44753
 
** in the WAL and can be recovered following a power-loss or hard reset.
44754
 
**
44755
 
** Fsync is also called on the database file if (and only if) the entire
44756
 
** WAL content is copied into the database file.  This second fsync makes
44757
 
** it safe to delete the WAL since the new content will persist in the
44758
 
** database file.
44759
 
**
44760
 
** This routine uses and updates the nBackfill field of the wal-index header.
44761
 
** This is the only routine tha will increase the value of nBackfill.  
44762
 
** (A WAL reset or recovery will revert nBackfill to zero, but not increase
44763
 
** its value.)
44764
 
**
44765
 
** The caller must be holding sufficient locks to ensure that no other
44766
 
** checkpoint is running (in any other thread or process) at the same
44767
 
** time.
44768
 
*/
44769
 
static int walCheckpoint(
44770
 
  Wal *pWal,                      /* Wal connection */
44771
 
  int eMode,                      /* One of PASSIVE, FULL or RESTART */
44772
 
  int (*xBusyCall)(void*),        /* Function to call when busy */
44773
 
  void *pBusyArg,                 /* Context argument for xBusyHandler */
44774
 
  int sync_flags,                 /* Flags for OsSync() (or 0) */
44775
 
  u8 *zBuf                        /* Temporary buffer to use */
44776
 
){
44777
 
  int rc;                         /* Return code */
44778
 
  int szPage;                     /* Database page-size */
44779
 
  WalIterator *pIter = 0;         /* Wal iterator context */
44780
 
  u32 iDbpage = 0;                /* Next database page to write */
44781
 
  u32 iFrame = 0;                 /* Wal frame containing data for iDbpage */
44782
 
  u32 mxSafeFrame;                /* Max frame that can be backfilled */
44783
 
  u32 mxPage;                     /* Max database page to write */
44784
 
  int i;                          /* Loop counter */
44785
 
  volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
44786
 
  int (*xBusy)(void*) = 0;        /* Function to call when waiting for locks */
44787
 
 
44788
 
  szPage = walPagesize(pWal);
44789
 
  testcase( szPage<=32768 );
44790
 
  testcase( szPage>=65536 );
44791
 
  pInfo = walCkptInfo(pWal);
44792
 
  if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;
44793
 
 
44794
 
  /* Allocate the iterator */
44795
 
  rc = walIteratorInit(pWal, &pIter);
44796
 
  if( rc!=SQLITE_OK ){
44797
 
    return rc;
44798
 
  }
44799
 
  assert( pIter );
44800
 
 
44801
 
  if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall;
44802
 
 
44803
 
  /* Compute in mxSafeFrame the index of the last frame of the WAL that is
44804
 
  ** safe to write into the database.  Frames beyond mxSafeFrame might
44805
 
  ** overwrite database pages that are in use by active readers and thus
44806
 
  ** cannot be backfilled from the WAL.
44807
 
  */
44808
 
  mxSafeFrame = pWal->hdr.mxFrame;
44809
 
  mxPage = pWal->hdr.nPage;
44810
 
  for(i=1; i<WAL_NREADER; i++){
44811
 
    u32 y = pInfo->aReadMark[i];
44812
 
    if( mxSafeFrame>y ){
44813
 
      assert( y<=pWal->hdr.mxFrame );
44814
 
      rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
44815
 
      if( rc==SQLITE_OK ){
44816
 
        pInfo->aReadMark[i] = READMARK_NOT_USED;
44817
 
        walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
44818
 
      }else if( rc==SQLITE_BUSY ){
44819
 
        mxSafeFrame = y;
44820
 
        xBusy = 0;
44821
 
      }else{
44822
 
        goto walcheckpoint_out;
44823
 
      }
44824
 
    }
44825
 
  }
44826
 
 
44827
 
  if( pInfo->nBackfill<mxSafeFrame
44828
 
   && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0), 1))==SQLITE_OK
44829
 
  ){
44830
 
    i64 nSize;                    /* Current size of database file */
44831
 
    u32 nBackfill = pInfo->nBackfill;
44832
 
 
44833
 
    /* Sync the WAL to disk */
44834
 
    if( sync_flags ){
44835
 
      rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
44836
 
    }
44837
 
 
44838
 
    /* If the database file may grow as a result of this checkpoint, hint
44839
 
    ** about the eventual size of the db file to the VFS layer. 
44840
 
    */
44841
 
    if( rc==SQLITE_OK ){
44842
 
      i64 nReq = ((i64)mxPage * szPage);
44843
 
      rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
44844
 
      if( rc==SQLITE_OK && nSize<nReq ){
44845
 
        sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
44846
 
      }
44847
 
    }
44848
 
 
44849
 
    /* Iterate through the contents of the WAL, copying data to the db file. */
44850
 
    while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
44851
 
      i64 iOffset;
44852
 
      assert( walFramePgno(pWal, iFrame)==iDbpage );
44853
 
      if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue;
44854
 
      iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
44855
 
      /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
44856
 
      rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
44857
 
      if( rc!=SQLITE_OK ) break;
44858
 
      iOffset = (iDbpage-1)*(i64)szPage;
44859
 
      testcase( IS_BIG_INT(iOffset) );
44860
 
      rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
44861
 
      if( rc!=SQLITE_OK ) break;
44862
 
    }
44863
 
 
44864
 
    /* If work was actually accomplished... */
44865
 
    if( rc==SQLITE_OK ){
44866
 
      if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
44867
 
        i64 szDb = pWal->hdr.nPage*(i64)szPage;
44868
 
        testcase( IS_BIG_INT(szDb) );
44869
 
        rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
44870
 
        if( rc==SQLITE_OK && sync_flags ){
44871
 
          rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
44872
 
        }
44873
 
      }
44874
 
      if( rc==SQLITE_OK ){
44875
 
        pInfo->nBackfill = mxSafeFrame;
44876
 
      }
44877
 
    }
44878
 
 
44879
 
    /* Release the reader lock held while backfilling */
44880
 
    walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
44881
 
  }
44882
 
 
44883
 
  if( rc==SQLITE_BUSY ){
44884
 
    /* Reset the return code so as not to report a checkpoint failure
44885
 
    ** just because there are active readers.  */
44886
 
    rc = SQLITE_OK;
44887
 
  }
44888
 
 
44889
 
  /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal
44890
 
  ** file has been copied into the database file, then block until all
44891
 
  ** readers have finished using the wal file. This ensures that the next
44892
 
  ** process to write to the database restarts the wal file.
44893
 
  */
44894
 
  if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
44895
 
    assert( pWal->writeLock );
44896
 
    if( pInfo->nBackfill<pWal->hdr.mxFrame ){
44897
 
      rc = SQLITE_BUSY;
44898
 
    }else if( eMode==SQLITE_CHECKPOINT_RESTART ){
44899
 
      assert( mxSafeFrame==pWal->hdr.mxFrame );
44900
 
      rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
44901
 
      if( rc==SQLITE_OK ){
44902
 
        walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
44903
 
      }
44904
 
    }
44905
 
  }
44906
 
 
44907
 
 walcheckpoint_out:
44908
 
  walIteratorFree(pIter);
44909
 
  return rc;
44910
 
}
44911
 
 
44912
 
/*
44913
 
** Close a connection to a log file.
44914
 
*/
44915
 
SQLITE_PRIVATE int sqlite3WalClose(
44916
 
  Wal *pWal,                      /* Wal to close */
44917
 
  int sync_flags,                 /* Flags to pass to OsSync() (or 0) */
44918
 
  int nBuf,
44919
 
  u8 *zBuf                        /* Buffer of at least nBuf bytes */
44920
 
){
44921
 
  int rc = SQLITE_OK;
44922
 
  if( pWal ){
44923
 
    int isDelete = 0;             /* True to unlink wal and wal-index files */
44924
 
 
44925
 
    /* If an EXCLUSIVE lock can be obtained on the database file (using the
44926
 
    ** ordinary, rollback-mode locking methods, this guarantees that the
44927
 
    ** connection associated with this log file is the only connection to
44928
 
    ** the database. In this case checkpoint the database and unlink both
44929
 
    ** the wal and wal-index files.
44930
 
    **
44931
 
    ** The EXCLUSIVE lock is not released before returning.
44932
 
    */
44933
 
    rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
44934
 
    if( rc==SQLITE_OK ){
44935
 
      if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
44936
 
        pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
44937
 
      }
44938
 
      rc = sqlite3WalCheckpoint(
44939
 
          pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
44940
 
      );
44941
 
      if( rc==SQLITE_OK ){
44942
 
        isDelete = 1;
44943
 
      }
44944
 
    }
44945
 
 
44946
 
    walIndexClose(pWal, isDelete);
44947
 
    sqlite3OsClose(pWal->pWalFd);
44948
 
    if( isDelete ){
44949
 
      sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
44950
 
    }
44951
 
    WALTRACE(("WAL%p: closed\n", pWal));
44952
 
    sqlite3_free((void *)pWal->apWiData);
44953
 
    sqlite3_free(pWal);
44954
 
  }
44955
 
  return rc;
44956
 
}
44957
 
 
44958
 
/*
44959
 
** Try to read the wal-index header.  Return 0 on success and 1 if
44960
 
** there is a problem.
44961
 
**
44962
 
** The wal-index is in shared memory.  Another thread or process might
44963
 
** be writing the header at the same time this procedure is trying to
44964
 
** read it, which might result in inconsistency.  A dirty read is detected
44965
 
** by verifying that both copies of the header are the same and also by
44966
 
** a checksum on the header.
44967
 
**
44968
 
** If and only if the read is consistent and the header is different from
44969
 
** pWal->hdr, then pWal->hdr is updated to the content of the new header
44970
 
** and *pChanged is set to 1.
44971
 
**
44972
 
** If the checksum cannot be verified return non-zero. If the header
44973
 
** is read successfully and the checksum verified, return zero.
44974
 
*/
44975
 
static int walIndexTryHdr(Wal *pWal, int *pChanged){
44976
 
  u32 aCksum[2];                  /* Checksum on the header content */
44977
 
  WalIndexHdr h1, h2;             /* Two copies of the header content */
44978
 
  WalIndexHdr volatile *aHdr;     /* Header in shared memory */
44979
 
 
44980
 
  /* The first page of the wal-index must be mapped at this point. */
44981
 
  assert( pWal->nWiData>0 && pWal->apWiData[0] );
44982
 
 
44983
 
  /* Read the header. This might happen concurrently with a write to the
44984
 
  ** same area of shared memory on a different CPU in a SMP,
44985
 
  ** meaning it is possible that an inconsistent snapshot is read
44986
 
  ** from the file. If this happens, return non-zero.
44987
 
  **
44988
 
  ** There are two copies of the header at the beginning of the wal-index.
44989
 
  ** When reading, read [0] first then [1].  Writes are in the reverse order.
44990
 
  ** Memory barriers are used to prevent the compiler or the hardware from
44991
 
  ** reordering the reads and writes.
44992
 
  */
44993
 
  aHdr = walIndexHdr(pWal);
44994
 
  memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
44995
 
  walShmBarrier(pWal);
44996
 
  memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
44997
 
 
44998
 
  if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
44999
 
    return 1;   /* Dirty read */
45000
 
  }  
45001
 
  if( h1.isInit==0 ){
45002
 
    return 1;   /* Malformed header - probably all zeros */
45003
 
  }
45004
 
  walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
45005
 
  if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
45006
 
    return 1;   /* Checksum does not match */
45007
 
  }
45008
 
 
45009
 
  if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
45010
 
    *pChanged = 1;
45011
 
    memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
45012
 
    pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
45013
 
    testcase( pWal->szPage<=32768 );
45014
 
    testcase( pWal->szPage>=65536 );
45015
 
  }
45016
 
 
45017
 
  /* The header was successfully read. Return zero. */
45018
 
  return 0;
45019
 
}
45020
 
 
45021
 
/*
45022
 
** Read the wal-index header from the wal-index and into pWal->hdr.
45023
 
** If the wal-header appears to be corrupt, try to reconstruct the
45024
 
** wal-index from the WAL before returning.
45025
 
**
45026
 
** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
45027
 
** changed by this opertion.  If pWal->hdr is unchanged, set *pChanged
45028
 
** to 0.
45029
 
**
45030
 
** If the wal-index header is successfully read, return SQLITE_OK. 
45031
 
** Otherwise an SQLite error code.
45032
 
*/
45033
 
static int walIndexReadHdr(Wal *pWal, int *pChanged){
45034
 
  int rc;                         /* Return code */
45035
 
  int badHdr;                     /* True if a header read failed */
45036
 
  volatile u32 *page0;            /* Chunk of wal-index containing header */
45037
 
 
45038
 
  /* Ensure that page 0 of the wal-index (the page that contains the 
45039
 
  ** wal-index header) is mapped. Return early if an error occurs here.
45040
 
  */
45041
 
  assert( pChanged );
45042
 
  rc = walIndexPage(pWal, 0, &page0);
45043
 
  if( rc!=SQLITE_OK ){
45044
 
    return rc;
45045
 
  };
45046
 
  assert( page0 || pWal->writeLock==0 );
45047
 
 
45048
 
  /* If the first page of the wal-index has been mapped, try to read the
45049
 
  ** wal-index header immediately, without holding any lock. This usually
45050
 
  ** works, but may fail if the wal-index header is corrupt or currently 
45051
 
  ** being modified by another thread or process.
45052
 
  */
45053
 
  badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
45054
 
 
45055
 
  /* If the first attempt failed, it might have been due to a race
45056
 
  ** with a writer.  So get a WRITE lock and try again.
45057
 
  */
45058
 
  assert( badHdr==0 || pWal->writeLock==0 );
45059
 
  if( badHdr && SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
45060
 
    pWal->writeLock = 1;
45061
 
    if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
45062
 
      badHdr = walIndexTryHdr(pWal, pChanged);
45063
 
      if( badHdr ){
45064
 
        /* If the wal-index header is still malformed even while holding
45065
 
        ** a WRITE lock, it can only mean that the header is corrupted and
45066
 
        ** needs to be reconstructed.  So run recovery to do exactly that.
45067
 
        */
45068
 
        rc = walIndexRecover(pWal);
45069
 
        *pChanged = 1;
45070
 
      }
45071
 
    }
45072
 
    pWal->writeLock = 0;
45073
 
    walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
45074
 
  }
45075
 
 
45076
 
  /* If the header is read successfully, check the version number to make
45077
 
  ** sure the wal-index was not constructed with some future format that
45078
 
  ** this version of SQLite cannot understand.
45079
 
  */
45080
 
  if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
45081
 
    rc = SQLITE_CANTOPEN_BKPT;
45082
 
  }
45083
 
 
45084
 
  return rc;
45085
 
}
45086
 
 
45087
 
/*
45088
 
** This is the value that walTryBeginRead returns when it needs to
45089
 
** be retried.
45090
 
*/
45091
 
#define WAL_RETRY  (-1)
45092
 
 
45093
 
/*
45094
 
** Attempt to start a read transaction.  This might fail due to a race or
45095
 
** other transient condition.  When that happens, it returns WAL_RETRY to
45096
 
** indicate to the caller that it is safe to retry immediately.
45097
 
**
45098
 
** On success return SQLITE_OK.  On a permanent failure (such an
45099
 
** I/O error or an SQLITE_BUSY because another process is running
45100
 
** recovery) return a positive error code.
45101
 
**
45102
 
** The useWal parameter is true to force the use of the WAL and disable
45103
 
** the case where the WAL is bypassed because it has been completely
45104
 
** checkpointed.  If useWal==0 then this routine calls walIndexReadHdr() 
45105
 
** to make a copy of the wal-index header into pWal->hdr.  If the 
45106
 
** wal-index header has changed, *pChanged is set to 1 (as an indication 
45107
 
** to the caller that the local paget cache is obsolete and needs to be 
45108
 
** flushed.)  When useWal==1, the wal-index header is assumed to already
45109
 
** be loaded and the pChanged parameter is unused.
45110
 
**
45111
 
** The caller must set the cnt parameter to the number of prior calls to
45112
 
** this routine during the current read attempt that returned WAL_RETRY.
45113
 
** This routine will start taking more aggressive measures to clear the
45114
 
** race conditions after multiple WAL_RETRY returns, and after an excessive
45115
 
** number of errors will ultimately return SQLITE_PROTOCOL.  The
45116
 
** SQLITE_PROTOCOL return indicates that some other process has gone rogue
45117
 
** and is not honoring the locking protocol.  There is a vanishingly small
45118
 
** chance that SQLITE_PROTOCOL could be returned because of a run of really
45119
 
** bad luck when there is lots of contention for the wal-index, but that
45120
 
** possibility is so small that it can be safely neglected, we believe.
45121
 
**
45122
 
** On success, this routine obtains a read lock on 
45123
 
** WAL_READ_LOCK(pWal->readLock).  The pWal->readLock integer is
45124
 
** in the range 0 <= pWal->readLock < WAL_NREADER.  If pWal->readLock==(-1)
45125
 
** that means the Wal does not hold any read lock.  The reader must not
45126
 
** access any database page that is modified by a WAL frame up to and
45127
 
** including frame number aReadMark[pWal->readLock].  The reader will
45128
 
** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
45129
 
** Or if pWal->readLock==0, then the reader will ignore the WAL
45130
 
** completely and get all content directly from the database file.
45131
 
** If the useWal parameter is 1 then the WAL will never be ignored and
45132
 
** this routine will always set pWal->readLock>0 on success.
45133
 
** When the read transaction is completed, the caller must release the
45134
 
** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
45135
 
**
45136
 
** This routine uses the nBackfill and aReadMark[] fields of the header
45137
 
** to select a particular WAL_READ_LOCK() that strives to let the
45138
 
** checkpoint process do as much work as possible.  This routine might
45139
 
** update values of the aReadMark[] array in the header, but if it does
45140
 
** so it takes care to hold an exclusive lock on the corresponding
45141
 
** WAL_READ_LOCK() while changing values.
45142
 
*/
45143
 
static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
45144
 
  volatile WalCkptInfo *pInfo;    /* Checkpoint information in wal-index */
45145
 
  u32 mxReadMark;                 /* Largest aReadMark[] value */
45146
 
  int mxI;                        /* Index of largest aReadMark[] value */
45147
 
  int i;                          /* Loop counter */
45148
 
  int rc = SQLITE_OK;             /* Return code  */
45149
 
 
45150
 
  assert( pWal->readLock<0 );     /* Not currently locked */
45151
 
 
45152
 
  /* Take steps to avoid spinning forever if there is a protocol error.
45153
 
  **
45154
 
  ** Circumstances that cause a RETRY should only last for the briefest
45155
 
  ** instances of time.  No I/O or other system calls are done while the
45156
 
  ** locks are held, so the locks should not be held for very long. But 
45157
 
  ** if we are unlucky, another process that is holding a lock might get
45158
 
  ** paged out or take a page-fault that is time-consuming to resolve, 
45159
 
  ** during the few nanoseconds that it is holding the lock.  In that case,
45160
 
  ** it might take longer than normal for the lock to free.
45161
 
  **
45162
 
  ** After 5 RETRYs, we begin calling sqlite3OsSleep().  The first few
45163
 
  ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
45164
 
  ** is more of a scheduler yield than an actual delay.  But on the 10th
45165
 
  ** an subsequent retries, the delays start becoming longer and longer, 
45166
 
  ** so that on the 100th (and last) RETRY we delay for 21 milliseconds.
45167
 
  ** The total delay time before giving up is less than 1 second.
45168
 
  */
45169
 
  if( cnt>5 ){
45170
 
    int nDelay = 1;                      /* Pause time in microseconds */
45171
 
    if( cnt>100 ){
45172
 
      VVA_ONLY( pWal->lockError = 1; )
45173
 
      return SQLITE_PROTOCOL;
45174
 
    }
45175
 
    if( cnt>=10 ) nDelay = (cnt-9)*238;  /* Max delay 21ms. Total delay 996ms */
45176
 
    sqlite3OsSleep(pWal->pVfs, nDelay);
45177
 
  }
45178
 
 
45179
 
  if( !useWal ){
45180
 
    rc = walIndexReadHdr(pWal, pChanged);
45181
 
    if( rc==SQLITE_BUSY ){
45182
 
      /* If there is not a recovery running in another thread or process
45183
 
      ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
45184
 
      ** be running, convert BUSY to BUSY_RECOVERY.  There is a race here
45185
 
      ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
45186
 
      ** would be technically correct.  But the race is benign since with
45187
 
      ** WAL_RETRY this routine will be called again and will probably be
45188
 
      ** right on the second iteration.
45189
 
      */
45190
 
      if( pWal->apWiData[0]==0 ){
45191
 
        /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
45192
 
        ** We assume this is a transient condition, so return WAL_RETRY. The
45193
 
        ** xShmMap() implementation used by the default unix and win32 VFS 
45194
 
        ** modules may return SQLITE_BUSY due to a race condition in the 
45195
 
        ** code that determines whether or not the shared-memory region 
45196
 
        ** must be zeroed before the requested page is returned.
45197
 
        */
45198
 
        rc = WAL_RETRY;
45199
 
      }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
45200
 
        walUnlockShared(pWal, WAL_RECOVER_LOCK);
45201
 
        rc = WAL_RETRY;
45202
 
      }else if( rc==SQLITE_BUSY ){
45203
 
        rc = SQLITE_BUSY_RECOVERY;
45204
 
      }
45205
 
    }
45206
 
    if( rc!=SQLITE_OK ){
45207
 
      return rc;
45208
 
    }
45209
 
  }
45210
 
 
45211
 
  pInfo = walCkptInfo(pWal);
45212
 
  if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
45213
 
    /* The WAL has been completely backfilled (or it is empty).
45214
 
    ** and can be safely ignored.
45215
 
    */
45216
 
    rc = walLockShared(pWal, WAL_READ_LOCK(0));
45217
 
    walShmBarrier(pWal);
45218
 
    if( rc==SQLITE_OK ){
45219
 
      if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
45220
 
        /* It is not safe to allow the reader to continue here if frames
45221
 
        ** may have been appended to the log before READ_LOCK(0) was obtained.
45222
 
        ** When holding READ_LOCK(0), the reader ignores the entire log file,
45223
 
        ** which implies that the database file contains a trustworthy
45224
 
        ** snapshoT. Since holding READ_LOCK(0) prevents a checkpoint from
45225
 
        ** happening, this is usually correct.
45226
 
        **
45227
 
        ** However, if frames have been appended to the log (or if the log 
45228
 
        ** is wrapped and written for that matter) before the READ_LOCK(0)
45229
 
        ** is obtained, that is not necessarily true. A checkpointer may
45230
 
        ** have started to backfill the appended frames but crashed before
45231
 
        ** it finished. Leaving a corrupt image in the database file.
45232
 
        */
45233
 
        walUnlockShared(pWal, WAL_READ_LOCK(0));
45234
 
        return WAL_RETRY;
45235
 
      }
45236
 
      pWal->readLock = 0;
45237
 
      return SQLITE_OK;
45238
 
    }else if( rc!=SQLITE_BUSY ){
45239
 
      return rc;
45240
 
    }
45241
 
  }
45242
 
 
45243
 
  /* If we get this far, it means that the reader will want to use
45244
 
  ** the WAL to get at content from recent commits.  The job now is
45245
 
  ** to select one of the aReadMark[] entries that is closest to
45246
 
  ** but not exceeding pWal->hdr.mxFrame and lock that entry.
45247
 
  */
45248
 
  mxReadMark = 0;
45249
 
  mxI = 0;
45250
 
  for(i=1; i<WAL_NREADER; i++){
45251
 
    u32 thisMark = pInfo->aReadMark[i];
45252
 
    if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
45253
 
      assert( thisMark!=READMARK_NOT_USED );
45254
 
      mxReadMark = thisMark;
45255
 
      mxI = i;
45256
 
    }
45257
 
  }
45258
 
  /* There was once an "if" here. The extra "{" is to preserve indentation. */
45259
 
  {
45260
 
    if( mxReadMark < pWal->hdr.mxFrame || mxI==0 ){
45261
 
      for(i=1; i<WAL_NREADER; i++){
45262
 
        rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
45263
 
        if( rc==SQLITE_OK ){
45264
 
          mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
45265
 
          mxI = i;
45266
 
          walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
45267
 
          break;
45268
 
        }else if( rc!=SQLITE_BUSY ){
45269
 
          return rc;
45270
 
        }
45271
 
      }
45272
 
    }
45273
 
    if( mxI==0 ){
45274
 
      assert( rc==SQLITE_BUSY );
45275
 
      return WAL_RETRY;
45276
 
    }
45277
 
 
45278
 
    rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
45279
 
    if( rc ){
45280
 
      return rc==SQLITE_BUSY ? WAL_RETRY : rc;
45281
 
    }
45282
 
    /* Now that the read-lock has been obtained, check that neither the
45283
 
    ** value in the aReadMark[] array or the contents of the wal-index
45284
 
    ** header have changed.
45285
 
    **
45286
 
    ** It is necessary to check that the wal-index header did not change
45287
 
    ** between the time it was read and when the shared-lock was obtained
45288
 
    ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
45289
 
    ** that the log file may have been wrapped by a writer, or that frames
45290
 
    ** that occur later in the log than pWal->hdr.mxFrame may have been
45291
 
    ** copied into the database by a checkpointer. If either of these things
45292
 
    ** happened, then reading the database with the current value of
45293
 
    ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
45294
 
    ** instead.
45295
 
    **
45296
 
    ** This does not guarantee that the copy of the wal-index header is up to
45297
 
    ** date before proceeding. That would not be possible without somehow
45298
 
    ** blocking writers. It only guarantees that a dangerous checkpoint or 
45299
 
    ** log-wrap (either of which would require an exclusive lock on
45300
 
    ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid.
45301
 
    */
45302
 
    walShmBarrier(pWal);
45303
 
    if( pInfo->aReadMark[mxI]!=mxReadMark
45304
 
     || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
45305
 
    ){
45306
 
      walUnlockShared(pWal, WAL_READ_LOCK(mxI));
45307
 
      return WAL_RETRY;
45308
 
    }else{
45309
 
      assert( mxReadMark<=pWal->hdr.mxFrame );
45310
 
      pWal->readLock = (i16)mxI;
45311
 
    }
45312
 
  }
45313
 
  return rc;
45314
 
}
45315
 
 
45316
 
/*
45317
 
** Begin a read transaction on the database.
45318
 
**
45319
 
** This routine used to be called sqlite3OpenSnapshot() and with good reason:
45320
 
** it takes a snapshot of the state of the WAL and wal-index for the current
45321
 
** instant in time.  The current thread will continue to use this snapshot.
45322
 
** Other threads might append new content to the WAL and wal-index but
45323
 
** that extra content is ignored by the current thread.
45324
 
**
45325
 
** If the database contents have changes since the previous read
45326
 
** transaction, then *pChanged is set to 1 before returning.  The
45327
 
** Pager layer will use this to know that is cache is stale and
45328
 
** needs to be flushed.
45329
 
*/
45330
 
SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
45331
 
  int rc;                         /* Return code */
45332
 
  int cnt = 0;                    /* Number of TryBeginRead attempts */
45333
 
 
45334
 
  do{
45335
 
    rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
45336
 
  }while( rc==WAL_RETRY );
45337
 
  testcase( (rc&0xff)==SQLITE_BUSY );
45338
 
  testcase( (rc&0xff)==SQLITE_IOERR );
45339
 
  testcase( rc==SQLITE_PROTOCOL );
45340
 
  testcase( rc==SQLITE_OK );
45341
 
  return rc;
45342
 
}
45343
 
 
45344
 
/*
45345
 
** Finish with a read transaction.  All this does is release the
45346
 
** read-lock.
45347
 
*/
45348
 
SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
45349
 
  sqlite3WalEndWriteTransaction(pWal);
45350
 
  if( pWal->readLock>=0 ){
45351
 
    walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
45352
 
    pWal->readLock = -1;
45353
 
  }
45354
 
}
45355
 
 
45356
 
/*
45357
 
** Read a page from the WAL, if it is present in the WAL and if the 
45358
 
** current read transaction is configured to use the WAL.  
45359
 
**
45360
 
** The *pInWal is set to 1 if the requested page is in the WAL and
45361
 
** has been loaded.  Or *pInWal is set to 0 if the page was not in 
45362
 
** the WAL and needs to be read out of the database.
45363
 
*/
45364
 
SQLITE_PRIVATE int sqlite3WalRead(
45365
 
  Wal *pWal,                      /* WAL handle */
45366
 
  Pgno pgno,                      /* Database page number to read data for */
45367
 
  int *pInWal,                    /* OUT: True if data is read from WAL */
45368
 
  int nOut,                       /* Size of buffer pOut in bytes */
45369
 
  u8 *pOut                        /* Buffer to write page data to */
45370
 
){
45371
 
  u32 iRead = 0;                  /* If !=0, WAL frame to return data from */
45372
 
  u32 iLast = pWal->hdr.mxFrame;  /* Last page in WAL for this reader */
45373
 
  int iHash;                      /* Used to loop through N hash tables */
45374
 
 
45375
 
  /* This routine is only be called from within a read transaction. */
45376
 
  assert( pWal->readLock>=0 || pWal->lockError );
45377
 
 
45378
 
  /* If the "last page" field of the wal-index header snapshot is 0, then
45379
 
  ** no data will be read from the wal under any circumstances. Return early
45380
 
  ** in this case as an optimization.  Likewise, if pWal->readLock==0, 
45381
 
  ** then the WAL is ignored by the reader so return early, as if the 
45382
 
  ** WAL were empty.
45383
 
  */
45384
 
  if( iLast==0 || pWal->readLock==0 ){
45385
 
    *pInWal = 0;
45386
 
    return SQLITE_OK;
45387
 
  }
45388
 
 
45389
 
  /* Search the hash table or tables for an entry matching page number
45390
 
  ** pgno. Each iteration of the following for() loop searches one
45391
 
  ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
45392
 
  **
45393
 
  ** This code might run concurrently to the code in walIndexAppend()
45394
 
  ** that adds entries to the wal-index (and possibly to this hash 
45395
 
  ** table). This means the value just read from the hash 
45396
 
  ** slot (aHash[iKey]) may have been added before or after the 
45397
 
  ** current read transaction was opened. Values added after the
45398
 
  ** read transaction was opened may have been written incorrectly -
45399
 
  ** i.e. these slots may contain garbage data. However, we assume
45400
 
  ** that any slots written before the current read transaction was
45401
 
  ** opened remain unmodified.
45402
 
  **
45403
 
  ** For the reasons above, the if(...) condition featured in the inner
45404
 
  ** loop of the following block is more stringent that would be required 
45405
 
  ** if we had exclusive access to the hash-table:
45406
 
  **
45407
 
  **   (aPgno[iFrame]==pgno): 
45408
 
  **     This condition filters out normal hash-table collisions.
45409
 
  **
45410
 
  **   (iFrame<=iLast): 
45411
 
  **     This condition filters out entries that were added to the hash
45412
 
  **     table after the current read-transaction had started.
45413
 
  */
45414
 
  for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){
45415
 
    volatile ht_slot *aHash;      /* Pointer to hash table */
45416
 
    volatile u32 *aPgno;          /* Pointer to array of page numbers */
45417
 
    u32 iZero;                    /* Frame number corresponding to aPgno[0] */
45418
 
    int iKey;                     /* Hash slot index */
45419
 
    int nCollide;                 /* Number of hash collisions remaining */
45420
 
    int rc;                       /* Error code */
45421
 
 
45422
 
    rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
45423
 
    if( rc!=SQLITE_OK ){
45424
 
      return rc;
45425
 
    }
45426
 
    nCollide = HASHTABLE_NSLOT;
45427
 
    for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
45428
 
      u32 iFrame = aHash[iKey] + iZero;
45429
 
      if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
45430
 
        assert( iFrame>iRead );
45431
 
        iRead = iFrame;
45432
 
      }
45433
 
      if( (nCollide--)==0 ){
45434
 
        return SQLITE_CORRUPT_BKPT;
45435
 
      }
45436
 
    }
45437
 
  }
45438
 
 
45439
 
#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
45440
 
  /* If expensive assert() statements are available, do a linear search
45441
 
  ** of the wal-index file content. Make sure the results agree with the
45442
 
  ** result obtained using the hash indexes above.  */
45443
 
  {
45444
 
    u32 iRead2 = 0;
45445
 
    u32 iTest;
45446
 
    for(iTest=iLast; iTest>0; iTest--){
45447
 
      if( walFramePgno(pWal, iTest)==pgno ){
45448
 
        iRead2 = iTest;
45449
 
        break;
45450
 
      }
45451
 
    }
45452
 
    assert( iRead==iRead2 );
45453
 
  }
45454
 
#endif
45455
 
 
45456
 
  /* If iRead is non-zero, then it is the log frame number that contains the
45457
 
  ** required page. Read and return data from the log file.
45458
 
  */
45459
 
  if( iRead ){
45460
 
    int sz;
45461
 
    i64 iOffset;
45462
 
    sz = pWal->hdr.szPage;
45463
 
    sz = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
45464
 
    testcase( sz<=32768 );
45465
 
    testcase( sz>=65536 );
45466
 
    iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
45467
 
    *pInWal = 1;
45468
 
    /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
45469
 
    return sqlite3OsRead(pWal->pWalFd, pOut, nOut, iOffset);
45470
 
  }
45471
 
 
45472
 
  *pInWal = 0;
45473
 
  return SQLITE_OK;
45474
 
}
45475
 
 
45476
 
 
45477
 
/* 
45478
 
** Return the size of the database in pages (or zero, if unknown).
45479
 
*/
45480
 
SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
45481
 
  if( pWal && ALWAYS(pWal->readLock>=0) ){
45482
 
    return pWal->hdr.nPage;
45483
 
  }
45484
 
  return 0;
45485
 
}
45486
 
 
45487
 
 
45488
 
/* 
45489
 
** This function starts a write transaction on the WAL.
45490
 
**
45491
 
** A read transaction must have already been started by a prior call
45492
 
** to sqlite3WalBeginReadTransaction().
45493
 
**
45494
 
** If another thread or process has written into the database since
45495
 
** the read transaction was started, then it is not possible for this
45496
 
** thread to write as doing so would cause a fork.  So this routine
45497
 
** returns SQLITE_BUSY in that case and no write transaction is started.
45498
 
**
45499
 
** There can only be a single writer active at a time.
45500
 
*/
45501
 
SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
45502
 
  int rc;
45503
 
 
45504
 
  /* Cannot start a write transaction without first holding a read
45505
 
  ** transaction. */
45506
 
  assert( pWal->readLock>=0 );
45507
 
 
45508
 
  if( pWal->readOnly ){
45509
 
    return SQLITE_READONLY;
45510
 
  }
45511
 
 
45512
 
  /* Only one writer allowed at a time.  Get the write lock.  Return
45513
 
  ** SQLITE_BUSY if unable.
45514
 
  */
45515
 
  rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
45516
 
  if( rc ){
45517
 
    return rc;
45518
 
  }
45519
 
  pWal->writeLock = 1;
45520
 
 
45521
 
  /* If another connection has written to the database file since the
45522
 
  ** time the read transaction on this connection was started, then
45523
 
  ** the write is disallowed.
45524
 
  */
45525
 
  if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
45526
 
    walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
45527
 
    pWal->writeLock = 0;
45528
 
    rc = SQLITE_BUSY;
45529
 
  }
45530
 
 
45531
 
  return rc;
45532
 
}
45533
 
 
45534
 
/*
45535
 
** End a write transaction.  The commit has already been done.  This
45536
 
** routine merely releases the lock.
45537
 
*/
45538
 
SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
45539
 
  if( pWal->writeLock ){
45540
 
    walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
45541
 
    pWal->writeLock = 0;
45542
 
  }
45543
 
  return SQLITE_OK;
45544
 
}
45545
 
 
45546
 
/*
45547
 
** If any data has been written (but not committed) to the log file, this
45548
 
** function moves the write-pointer back to the start of the transaction.
45549
 
**
45550
 
** Additionally, the callback function is invoked for each frame written
45551
 
** to the WAL since the start of the transaction. If the callback returns
45552
 
** other than SQLITE_OK, it is not invoked again and the error code is
45553
 
** returned to the caller.
45554
 
**
45555
 
** Otherwise, if the callback function does not return an error, this
45556
 
** function returns SQLITE_OK.
45557
 
*/
45558
 
SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
45559
 
  int rc = SQLITE_OK;
45560
 
  if( ALWAYS(pWal->writeLock) ){
45561
 
    Pgno iMax = pWal->hdr.mxFrame;
45562
 
    Pgno iFrame;
45563
 
  
45564
 
    /* Restore the clients cache of the wal-index header to the state it
45565
 
    ** was in before the client began writing to the database. 
45566
 
    */
45567
 
    memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
45568
 
 
45569
 
    for(iFrame=pWal->hdr.mxFrame+1; 
45570
 
        ALWAYS(rc==SQLITE_OK) && iFrame<=iMax; 
45571
 
        iFrame++
45572
 
    ){
45573
 
      /* This call cannot fail. Unless the page for which the page number
45574
 
      ** is passed as the second argument is (a) in the cache and 
45575
 
      ** (b) has an outstanding reference, then xUndo is either a no-op
45576
 
      ** (if (a) is false) or simply expels the page from the cache (if (b)
45577
 
      ** is false).
45578
 
      **
45579
 
      ** If the upper layer is doing a rollback, it is guaranteed that there
45580
 
      ** are no outstanding references to any page other than page 1. And
45581
 
      ** page 1 is never written to the log until the transaction is
45582
 
      ** committed. As a result, the call to xUndo may not fail.
45583
 
      */
45584
 
      assert( walFramePgno(pWal, iFrame)!=1 );
45585
 
      rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
45586
 
    }
45587
 
    walCleanupHash(pWal);
45588
 
  }
45589
 
  assert( rc==SQLITE_OK );
45590
 
  return rc;
45591
 
}
45592
 
 
45593
 
/* 
45594
 
** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32 
45595
 
** values. This function populates the array with values required to 
45596
 
** "rollback" the write position of the WAL handle back to the current 
45597
 
** point in the event of a savepoint rollback (via WalSavepointUndo()).
45598
 
*/
45599
 
SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
45600
 
  assert( pWal->writeLock );
45601
 
  aWalData[0] = pWal->hdr.mxFrame;
45602
 
  aWalData[1] = pWal->hdr.aFrameCksum[0];
45603
 
  aWalData[2] = pWal->hdr.aFrameCksum[1];
45604
 
  aWalData[3] = pWal->nCkpt;
45605
 
}
45606
 
 
45607
 
/* 
45608
 
** Move the write position of the WAL back to the point identified by
45609
 
** the values in the aWalData[] array. aWalData must point to an array
45610
 
** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
45611
 
** by a call to WalSavepoint().
45612
 
*/
45613
 
SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
45614
 
  int rc = SQLITE_OK;
45615
 
 
45616
 
  assert( pWal->writeLock );
45617
 
  assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
45618
 
 
45619
 
  if( aWalData[3]!=pWal->nCkpt ){
45620
 
    /* This savepoint was opened immediately after the write-transaction
45621
 
    ** was started. Right after that, the writer decided to wrap around
45622
 
    ** to the start of the log. Update the savepoint values to match.
45623
 
    */
45624
 
    aWalData[0] = 0;
45625
 
    aWalData[3] = pWal->nCkpt;
45626
 
  }
45627
 
 
45628
 
  if( aWalData[0]<pWal->hdr.mxFrame ){
45629
 
    pWal->hdr.mxFrame = aWalData[0];
45630
 
    pWal->hdr.aFrameCksum[0] = aWalData[1];
45631
 
    pWal->hdr.aFrameCksum[1] = aWalData[2];
45632
 
    walCleanupHash(pWal);
45633
 
  }
45634
 
 
45635
 
  return rc;
45636
 
}
45637
 
 
45638
 
/*
45639
 
** This function is called just before writing a set of frames to the log
45640
 
** file (see sqlite3WalFrames()). It checks to see if, instead of appending
45641
 
** to the current log file, it is possible to overwrite the start of the
45642
 
** existing log file with the new frames (i.e. "reset" the log). If so,
45643
 
** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
45644
 
** unchanged.
45645
 
**
45646
 
** SQLITE_OK is returned if no error is encountered (regardless of whether
45647
 
** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
45648
 
** if an error occurs.
45649
 
*/
45650
 
static int walRestartLog(Wal *pWal){
45651
 
  int rc = SQLITE_OK;
45652
 
  int cnt;
45653
 
 
45654
 
  if( pWal->readLock==0 ){
45655
 
    volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
45656
 
    assert( pInfo->nBackfill==pWal->hdr.mxFrame );
45657
 
    if( pInfo->nBackfill>0 ){
45658
 
      u32 salt1;
45659
 
      sqlite3_randomness(4, &salt1);
45660
 
      rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
45661
 
      if( rc==SQLITE_OK ){
45662
 
        /* If all readers are using WAL_READ_LOCK(0) (in other words if no
45663
 
        ** readers are currently using the WAL), then the transactions
45664
 
        ** frames will overwrite the start of the existing log. Update the
45665
 
        ** wal-index header to reflect this.
45666
 
        **
45667
 
        ** In theory it would be Ok to update the cache of the header only
45668
 
        ** at this point. But updating the actual wal-index header is also
45669
 
        ** safe and means there is no special case for sqlite3WalUndo()
45670
 
        ** to handle if this transaction is rolled back.
45671
 
        */
45672
 
        int i;                    /* Loop counter */
45673
 
        u32 *aSalt = pWal->hdr.aSalt;       /* Big-endian salt values */
45674
 
        pWal->nCkpt++;
45675
 
        pWal->hdr.mxFrame = 0;
45676
 
        sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
45677
 
        aSalt[1] = salt1;
45678
 
        walIndexWriteHdr(pWal);
45679
 
        pInfo->nBackfill = 0;
45680
 
        for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
45681
 
        assert( pInfo->aReadMark[0]==0 );
45682
 
        walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
45683
 
      }else if( rc!=SQLITE_BUSY ){
45684
 
        return rc;
45685
 
      }
45686
 
    }
45687
 
    walUnlockShared(pWal, WAL_READ_LOCK(0));
45688
 
    pWal->readLock = -1;
45689
 
    cnt = 0;
45690
 
    do{
45691
 
      int notUsed;
45692
 
      rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
45693
 
    }while( rc==WAL_RETRY );
45694
 
    assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
45695
 
    testcase( (rc&0xff)==SQLITE_IOERR );
45696
 
    testcase( rc==SQLITE_PROTOCOL );
45697
 
    testcase( rc==SQLITE_OK );
45698
 
  }
45699
 
  return rc;
45700
 
}
45701
 
 
45702
 
/* 
45703
 
** Write a set of frames to the log. The caller must hold the write-lock
45704
 
** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
45705
 
*/
45706
 
SQLITE_PRIVATE int sqlite3WalFrames(
45707
 
  Wal *pWal,                      /* Wal handle to write to */
45708
 
  int szPage,                     /* Database page-size in bytes */
45709
 
  PgHdr *pList,                   /* List of dirty pages to write */
45710
 
  Pgno nTruncate,                 /* Database size after this commit */
45711
 
  int isCommit,                   /* True if this is a commit */
45712
 
  int sync_flags                  /* Flags to pass to OsSync() (or 0) */
45713
 
){
45714
 
  int rc;                         /* Used to catch return codes */
45715
 
  u32 iFrame;                     /* Next frame address */
45716
 
  u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-header in */
45717
 
  PgHdr *p;                       /* Iterator to run through pList with. */
45718
 
  PgHdr *pLast = 0;               /* Last frame in list */
45719
 
  int nLast = 0;                  /* Number of extra copies of last page */
45720
 
 
45721
 
  assert( pList );
45722
 
  assert( pWal->writeLock );
45723
 
 
45724
 
#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
45725
 
  { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
45726
 
    WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
45727
 
              pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
45728
 
  }
45729
 
#endif
45730
 
 
45731
 
  /* See if it is possible to write these frames into the start of the
45732
 
  ** log file, instead of appending to it at pWal->hdr.mxFrame.
45733
 
  */
45734
 
  if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
45735
 
    return rc;
45736
 
  }
45737
 
 
45738
 
  /* If this is the first frame written into the log, write the WAL
45739
 
  ** header to the start of the WAL file. See comments at the top of
45740
 
  ** this source file for a description of the WAL header format.
45741
 
  */
45742
 
  iFrame = pWal->hdr.mxFrame;
45743
 
  if( iFrame==0 ){
45744
 
    u8 aWalHdr[WAL_HDRSIZE];      /* Buffer to assemble wal-header in */
45745
 
    u32 aCksum[2];                /* Checksum for wal-header */
45746
 
 
45747
 
    sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
45748
 
    sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
45749
 
    sqlite3Put4byte(&aWalHdr[8], szPage);
45750
 
    sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
45751
 
    sqlite3_randomness(8, pWal->hdr.aSalt);
45752
 
    memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
45753
 
    walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
45754
 
    sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
45755
 
    sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
45756
 
    
45757
 
    pWal->szPage = szPage;
45758
 
    pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
45759
 
    pWal->hdr.aFrameCksum[0] = aCksum[0];
45760
 
    pWal->hdr.aFrameCksum[1] = aCksum[1];
45761
 
 
45762
 
    rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
45763
 
    WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
45764
 
    if( rc!=SQLITE_OK ){
45765
 
      return rc;
45766
 
    }
45767
 
  }
45768
 
  assert( (int)pWal->szPage==szPage );
45769
 
 
45770
 
  /* Write the log file. */
45771
 
  for(p=pList; p; p=p->pDirty){
45772
 
    u32 nDbsize;                  /* Db-size field for frame header */
45773
 
    i64 iOffset;                  /* Write offset in log file */
45774
 
    void *pData;
45775
 
   
45776
 
    iOffset = walFrameOffset(++iFrame, szPage);
45777
 
    /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
45778
 
    
45779
 
    /* Populate and write the frame header */
45780
 
    nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0;
45781
 
#if defined(SQLITE_HAS_CODEC)
45782
 
    if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM;
45783
 
#else
45784
 
    pData = p->pData;
45785
 
#endif
45786
 
    walEncodeFrame(pWal, p->pgno, nDbsize, pData, aFrame);
45787
 
    rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
45788
 
    if( rc!=SQLITE_OK ){
45789
 
      return rc;
45790
 
    }
45791
 
 
45792
 
    /* Write the page data */
45793
 
    rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset+sizeof(aFrame));
45794
 
    if( rc!=SQLITE_OK ){
45795
 
      return rc;
45796
 
    }
45797
 
    pLast = p;
45798
 
  }
45799
 
 
45800
 
  /* Sync the log file if the 'isSync' flag was specified. */
45801
 
  if( sync_flags ){
45802
 
    i64 iSegment = sqlite3OsSectorSize(pWal->pWalFd);
45803
 
    i64 iOffset = walFrameOffset(iFrame+1, szPage);
45804
 
 
45805
 
    assert( isCommit );
45806
 
    assert( iSegment>0 );
45807
 
 
45808
 
    iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment);
45809
 
    while( iOffset<iSegment ){
45810
 
      void *pData;
45811
 
#if defined(SQLITE_HAS_CODEC)
45812
 
      if( (pData = sqlite3PagerCodec(pLast))==0 ) return SQLITE_NOMEM;
45813
 
#else
45814
 
      pData = pLast->pData;
45815
 
#endif
45816
 
      walEncodeFrame(pWal, pLast->pgno, nTruncate, pData, aFrame);
45817
 
      /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
45818
 
      rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
45819
 
      if( rc!=SQLITE_OK ){
45820
 
        return rc;
45821
 
      }
45822
 
      iOffset += WAL_FRAME_HDRSIZE;
45823
 
      rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset); 
45824
 
      if( rc!=SQLITE_OK ){
45825
 
        return rc;
45826
 
      }
45827
 
      nLast++;
45828
 
      iOffset += szPage;
45829
 
    }
45830
 
 
45831
 
    rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
45832
 
  }
45833
 
 
45834
 
  /* Append data to the wal-index. It is not necessary to lock the 
45835
 
  ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
45836
 
  ** guarantees that there are no other writers, and no data that may
45837
 
  ** be in use by existing readers is being overwritten.
45838
 
  */
45839
 
  iFrame = pWal->hdr.mxFrame;
45840
 
  for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
45841
 
    iFrame++;
45842
 
    rc = walIndexAppend(pWal, iFrame, p->pgno);
45843
 
  }
45844
 
  while( nLast>0 && rc==SQLITE_OK ){
45845
 
    iFrame++;
45846
 
    nLast--;
45847
 
    rc = walIndexAppend(pWal, iFrame, pLast->pgno);
45848
 
  }
45849
 
 
45850
 
  if( rc==SQLITE_OK ){
45851
 
    /* Update the private copy of the header. */
45852
 
    pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
45853
 
    testcase( szPage<=32768 );
45854
 
    testcase( szPage>=65536 );
45855
 
    pWal->hdr.mxFrame = iFrame;
45856
 
    if( isCommit ){
45857
 
      pWal->hdr.iChange++;
45858
 
      pWal->hdr.nPage = nTruncate;
45859
 
    }
45860
 
    /* If this is a commit, update the wal-index header too. */
45861
 
    if( isCommit ){
45862
 
      walIndexWriteHdr(pWal);
45863
 
      pWal->iCallback = iFrame;
45864
 
    }
45865
 
  }
45866
 
 
45867
 
  WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
45868
 
  return rc;
45869
 
}
45870
 
 
45871
 
/* 
45872
 
** This routine is called to implement sqlite3_wal_checkpoint() and
45873
 
** related interfaces.
45874
 
**
45875
 
** Obtain a CHECKPOINT lock and then backfill as much information as
45876
 
** we can from WAL into the database.
45877
 
**
45878
 
** If parameter xBusy is not NULL, it is a pointer to a busy-handler
45879
 
** callback. In this case this function runs a blocking checkpoint.
45880
 
*/
45881
 
SQLITE_PRIVATE int sqlite3WalCheckpoint(
45882
 
  Wal *pWal,                      /* Wal connection */
45883
 
  int eMode,                      /* PASSIVE, FULL or RESTART */
45884
 
  int (*xBusy)(void*),            /* Function to call when busy */
45885
 
  void *pBusyArg,                 /* Context argument for xBusyHandler */
45886
 
  int sync_flags,                 /* Flags to sync db file with (or 0) */
45887
 
  int nBuf,                       /* Size of temporary buffer */
45888
 
  u8 *zBuf,                       /* Temporary buffer to use */
45889
 
  int *pnLog,                     /* OUT: Number of frames in WAL */
45890
 
  int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
45891
 
){
45892
 
  int rc;                         /* Return code */
45893
 
  int isChanged = 0;              /* True if a new wal-index header is loaded */
45894
 
  int eMode2 = eMode;             /* Mode to pass to walCheckpoint() */
45895
 
 
45896
 
  assert( pWal->ckptLock==0 );
45897
 
  assert( pWal->writeLock==0 );
45898
 
 
45899
 
  WALTRACE(("WAL%p: checkpoint begins\n", pWal));
45900
 
  rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
45901
 
  if( rc ){
45902
 
    /* Usually this is SQLITE_BUSY meaning that another thread or process
45903
 
    ** is already running a checkpoint, or maybe a recovery.  But it might
45904
 
    ** also be SQLITE_IOERR. */
45905
 
    return rc;
45906
 
  }
45907
 
  pWal->ckptLock = 1;
45908
 
 
45909
 
  /* If this is a blocking-checkpoint, then obtain the write-lock as well
45910
 
  ** to prevent any writers from running while the checkpoint is underway.
45911
 
  ** This has to be done before the call to walIndexReadHdr() below.
45912
 
  **
45913
 
  ** If the writer lock cannot be obtained, then a passive checkpoint is
45914
 
  ** run instead. Since the checkpointer is not holding the writer lock,
45915
 
  ** there is no point in blocking waiting for any readers. Assuming no 
45916
 
  ** other error occurs, this function will return SQLITE_BUSY to the caller.
45917
 
  */
45918
 
  if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
45919
 
    rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
45920
 
    if( rc==SQLITE_OK ){
45921
 
      pWal->writeLock = 1;
45922
 
    }else if( rc==SQLITE_BUSY ){
45923
 
      eMode2 = SQLITE_CHECKPOINT_PASSIVE;
45924
 
      rc = SQLITE_OK;
45925
 
    }
45926
 
  }
45927
 
 
45928
 
  /* Read the wal-index header. */
45929
 
  if( rc==SQLITE_OK ){
45930
 
    rc = walIndexReadHdr(pWal, &isChanged);
45931
 
  }
45932
 
 
45933
 
  /* Copy data from the log to the database file. */
45934
 
  if( rc==SQLITE_OK ){
45935
 
    if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
45936
 
      rc = SQLITE_CORRUPT_BKPT;
45937
 
    }else{
45938
 
      rc = walCheckpoint(pWal, eMode2, xBusy, pBusyArg, sync_flags, zBuf);
45939
 
    }
45940
 
 
45941
 
    /* If no error occurred, set the output variables. */
45942
 
    if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
45943
 
      if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
45944
 
      if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
45945
 
    }
45946
 
  }
45947
 
 
45948
 
  if( isChanged ){
45949
 
    /* If a new wal-index header was loaded before the checkpoint was 
45950
 
    ** performed, then the pager-cache associated with pWal is now
45951
 
    ** out of date. So zero the cached wal-index header to ensure that
45952
 
    ** next time the pager opens a snapshot on this database it knows that
45953
 
    ** the cache needs to be reset.
45954
 
    */
45955
 
    memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
45956
 
  }
45957
 
 
45958
 
  /* Release the locks. */
45959
 
  sqlite3WalEndWriteTransaction(pWal);
45960
 
  walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
45961
 
  pWal->ckptLock = 0;
45962
 
  WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
45963
 
  return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
45964
 
}
45965
 
 
45966
 
/* Return the value to pass to a sqlite3_wal_hook callback, the
45967
 
** number of frames in the WAL at the point of the last commit since
45968
 
** sqlite3WalCallback() was called.  If no commits have occurred since
45969
 
** the last call, then return 0.
45970
 
*/
45971
 
SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
45972
 
  u32 ret = 0;
45973
 
  if( pWal ){
45974
 
    ret = pWal->iCallback;
45975
 
    pWal->iCallback = 0;
45976
 
  }
45977
 
  return (int)ret;
45978
 
}
45979
 
 
45980
 
/*
45981
 
** This function is called to change the WAL subsystem into or out
45982
 
** of locking_mode=EXCLUSIVE.
45983
 
**
45984
 
** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
45985
 
** into locking_mode=NORMAL.  This means that we must acquire a lock
45986
 
** on the pWal->readLock byte.  If the WAL is already in locking_mode=NORMAL
45987
 
** or if the acquisition of the lock fails, then return 0.  If the
45988
 
** transition out of exclusive-mode is successful, return 1.  This
45989
 
** operation must occur while the pager is still holding the exclusive
45990
 
** lock on the main database file.
45991
 
**
45992
 
** If op is one, then change from locking_mode=NORMAL into 
45993
 
** locking_mode=EXCLUSIVE.  This means that the pWal->readLock must
45994
 
** be released.  Return 1 if the transition is made and 0 if the
45995
 
** WAL is already in exclusive-locking mode - meaning that this
45996
 
** routine is a no-op.  The pager must already hold the exclusive lock
45997
 
** on the main database file before invoking this operation.
45998
 
**
45999
 
** If op is negative, then do a dry-run of the op==1 case but do
46000
 
** not actually change anything. The pager uses this to see if it
46001
 
** should acquire the database exclusive lock prior to invoking
46002
 
** the op==1 case.
46003
 
*/
46004
 
SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
46005
 
  int rc;
46006
 
  assert( pWal->writeLock==0 );
46007
 
  assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
46008
 
 
46009
 
  /* pWal->readLock is usually set, but might be -1 if there was a 
46010
 
  ** prior error while attempting to acquire are read-lock. This cannot 
46011
 
  ** happen if the connection is actually in exclusive mode (as no xShmLock
46012
 
  ** locks are taken in this case). Nor should the pager attempt to
46013
 
  ** upgrade to exclusive-mode following such an error.
46014
 
  */
46015
 
  assert( pWal->readLock>=0 || pWal->lockError );
46016
 
  assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
46017
 
 
46018
 
  if( op==0 ){
46019
 
    if( pWal->exclusiveMode ){
46020
 
      pWal->exclusiveMode = 0;
46021
 
      if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
46022
 
        pWal->exclusiveMode = 1;
46023
 
      }
46024
 
      rc = pWal->exclusiveMode==0;
46025
 
    }else{
46026
 
      /* Already in locking_mode=NORMAL */
46027
 
      rc = 0;
46028
 
    }
46029
 
  }else if( op>0 ){
46030
 
    assert( pWal->exclusiveMode==0 );
46031
 
    assert( pWal->readLock>=0 );
46032
 
    walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
46033
 
    pWal->exclusiveMode = 1;
46034
 
    rc = 1;
46035
 
  }else{
46036
 
    rc = pWal->exclusiveMode==0;
46037
 
  }
46038
 
  return rc;
46039
 
}
46040
 
 
46041
 
/* 
46042
 
** Return true if the argument is non-NULL and the WAL module is using
46043
 
** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
46044
 
** WAL module is using shared-memory, return false. 
46045
 
*/
46046
 
SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
46047
 
  return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
46048
 
}
46049
 
 
46050
 
#endif /* #ifndef SQLITE_OMIT_WAL */
46051
 
 
46052
 
/************** End of wal.c *************************************************/
46053
 
/************** Begin file btmutex.c *****************************************/
46054
 
/*
46055
 
** 2007 August 27
46056
 
**
46057
 
** The author disclaims copyright to this source code.  In place of
46058
 
** a legal notice, here is a blessing:
46059
 
**
46060
 
**    May you do good and not evil.
46061
 
**    May you find forgiveness for yourself and forgive others.
46062
 
**    May you share freely, never taking more than you give.
46063
 
**
46064
 
*************************************************************************
46065
 
**
46066
 
** This file contains code used to implement mutexes on Btree objects.
46067
 
** This code really belongs in btree.c.  But btree.c is getting too
46068
 
** big and we want to break it down some.  This packaged seemed like
46069
 
** a good breakout.
46070
 
*/
46071
 
/************** Include btreeInt.h in the middle of btmutex.c ****************/
46072
 
/************** Begin file btreeInt.h ****************************************/
46073
 
/*
46074
 
** 2004 April 6
46075
 
**
46076
 
** The author disclaims copyright to this source code.  In place of
46077
 
** a legal notice, here is a blessing:
46078
 
**
46079
 
**    May you do good and not evil.
46080
 
**    May you find forgiveness for yourself and forgive others.
46081
 
**    May you share freely, never taking more than you give.
46082
 
**
46083
 
*************************************************************************
46084
 
** This file implements a external (disk-based) database using BTrees.
46085
 
** For a detailed discussion of BTrees, refer to
46086
 
**
46087
 
**     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
46088
 
**     "Sorting And Searching", pages 473-480. Addison-Wesley
46089
 
**     Publishing Company, Reading, Massachusetts.
46090
 
**
46091
 
** The basic idea is that each page of the file contains N database
46092
 
** entries and N+1 pointers to subpages.
46093
 
**
46094
 
**   ----------------------------------------------------------------
46095
 
**   |  Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
46096
 
**   ----------------------------------------------------------------
46097
 
**
46098
 
** All of the keys on the page that Ptr(0) points to have values less
46099
 
** than Key(0).  All of the keys on page Ptr(1) and its subpages have
46100
 
** values greater than Key(0) and less than Key(1).  All of the keys
46101
 
** on Ptr(N) and its subpages have values greater than Key(N-1).  And
46102
 
** so forth.
46103
 
**
46104
 
** Finding a particular key requires reading O(log(M)) pages from the 
46105
 
** disk where M is the number of entries in the tree.
46106
 
**
46107
 
** In this implementation, a single file can hold one or more separate 
46108
 
** BTrees.  Each BTree is identified by the index of its root page.  The
46109
 
** key and data for any entry are combined to form the "payload".  A
46110
 
** fixed amount of payload can be carried directly on the database
46111
 
** page.  If the payload is larger than the preset amount then surplus
46112
 
** bytes are stored on overflow pages.  The payload for an entry
46113
 
** and the preceding pointer are combined to form a "Cell".  Each 
46114
 
** page has a small header which contains the Ptr(N) pointer and other
46115
 
** information such as the size of key and data.
46116
 
**
46117
 
** FORMAT DETAILS
46118
 
**
46119
 
** The file is divided into pages.  The first page is called page 1,
46120
 
** the second is page 2, and so forth.  A page number of zero indicates
46121
 
** "no such page".  The page size can be any power of 2 between 512 and 65536.
46122
 
** Each page can be either a btree page, a freelist page, an overflow
46123
 
** page, or a pointer-map page.
46124
 
**
46125
 
** The first page is always a btree page.  The first 100 bytes of the first
46126
 
** page contain a special header (the "file header") that describes the file.
46127
 
** The format of the file header is as follows:
46128
 
**
46129
 
**   OFFSET   SIZE    DESCRIPTION
46130
 
**      0      16     Header string: "SQLite format 3\000"
46131
 
**     16       2     Page size in bytes.  
46132
 
**     18       1     File format write version
46133
 
**     19       1     File format read version
46134
 
**     20       1     Bytes of unused space at the end of each page
46135
 
**     21       1     Max embedded payload fraction
46136
 
**     22       1     Min embedded payload fraction
46137
 
**     23       1     Min leaf payload fraction
46138
 
**     24       4     File change counter
46139
 
**     28       4     Reserved for future use
46140
 
**     32       4     First freelist page
46141
 
**     36       4     Number of freelist pages in the file
46142
 
**     40      60     15 4-byte meta values passed to higher layers
46143
 
**
46144
 
**     40       4     Schema cookie
46145
 
**     44       4     File format of schema layer
46146
 
**     48       4     Size of page cache
46147
 
**     52       4     Largest root-page (auto/incr_vacuum)
46148
 
**     56       4     1=UTF-8 2=UTF16le 3=UTF16be
46149
 
**     60       4     User version
46150
 
**     64       4     Incremental vacuum mode
46151
 
**     68       4     unused
46152
 
**     72       4     unused
46153
 
**     76       4     unused
46154
 
**
46155
 
** All of the integer values are big-endian (most significant byte first).
46156
 
**
46157
 
** The file change counter is incremented when the database is changed
46158
 
** This counter allows other processes to know when the file has changed
46159
 
** and thus when they need to flush their cache.
46160
 
**
46161
 
** The max embedded payload fraction is the amount of the total usable
46162
 
** space in a page that can be consumed by a single cell for standard
46163
 
** B-tree (non-LEAFDATA) tables.  A value of 255 means 100%.  The default
46164
 
** is to limit the maximum cell size so that at least 4 cells will fit
46165
 
** on one page.  Thus the default max embedded payload fraction is 64.
46166
 
**
46167
 
** If the payload for a cell is larger than the max payload, then extra
46168
 
** payload is spilled to overflow pages.  Once an overflow page is allocated,
46169
 
** as many bytes as possible are moved into the overflow pages without letting
46170
 
** the cell size drop below the min embedded payload fraction.
46171
 
**
46172
 
** The min leaf payload fraction is like the min embedded payload fraction
46173
 
** except that it applies to leaf nodes in a LEAFDATA tree.  The maximum
46174
 
** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
46175
 
** not specified in the header.
46176
 
**
46177
 
** Each btree pages is divided into three sections:  The header, the
46178
 
** cell pointer array, and the cell content area.  Page 1 also has a 100-byte
46179
 
** file header that occurs before the page header.
46180
 
**
46181
 
**      |----------------|
46182
 
**      | file header    |   100 bytes.  Page 1 only.
46183
 
**      |----------------|
46184
 
**      | page header    |   8 bytes for leaves.  12 bytes for interior nodes
46185
 
**      |----------------|
46186
 
**      | cell pointer   |   |  2 bytes per cell.  Sorted order.
46187
 
**      | array          |   |  Grows downward
46188
 
**      |                |   v
46189
 
**      |----------------|
46190
 
**      | unallocated    |
46191
 
**      | space          |
46192
 
**      |----------------|   ^  Grows upwards
46193
 
**      | cell content   |   |  Arbitrary order interspersed with freeblocks.
46194
 
**      | area           |   |  and free space fragments.
46195
 
**      |----------------|
46196
 
**
46197
 
** The page headers looks like this:
46198
 
**
46199
 
**   OFFSET   SIZE     DESCRIPTION
46200
 
**      0       1      Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
46201
 
**      1       2      byte offset to the first freeblock
46202
 
**      3       2      number of cells on this page
46203
 
**      5       2      first byte of the cell content area
46204
 
**      7       1      number of fragmented free bytes
46205
 
**      8       4      Right child (the Ptr(N) value).  Omitted on leaves.
46206
 
**
46207
 
** The flags define the format of this btree page.  The leaf flag means that
46208
 
** this page has no children.  The zerodata flag means that this page carries
46209
 
** only keys and no data.  The intkey flag means that the key is a integer
46210
 
** which is stored in the key size entry of the cell header rather than in
46211
 
** the payload area.
46212
 
**
46213
 
** The cell pointer array begins on the first byte after the page header.
46214
 
** The cell pointer array contains zero or more 2-byte numbers which are
46215
 
** offsets from the beginning of the page to the cell content in the cell
46216
 
** content area.  The cell pointers occur in sorted order.  The system strives
46217
 
** to keep free space after the last cell pointer so that new cells can
46218
 
** be easily added without having to defragment the page.
46219
 
**
46220
 
** Cell content is stored at the very end of the page and grows toward the
46221
 
** beginning of the page.
46222
 
**
46223
 
** Unused space within the cell content area is collected into a linked list of
46224
 
** freeblocks.  Each freeblock is at least 4 bytes in size.  The byte offset
46225
 
** to the first freeblock is given in the header.  Freeblocks occur in
46226
 
** increasing order.  Because a freeblock must be at least 4 bytes in size,
46227
 
** any group of 3 or fewer unused bytes in the cell content area cannot
46228
 
** exist on the freeblock chain.  A group of 3 or fewer free bytes is called
46229
 
** a fragment.  The total number of bytes in all fragments is recorded.
46230
 
** in the page header at offset 7.
46231
 
**
46232
 
**    SIZE    DESCRIPTION
46233
 
**      2     Byte offset of the next freeblock
46234
 
**      2     Bytes in this freeblock
46235
 
**
46236
 
** Cells are of variable length.  Cells are stored in the cell content area at
46237
 
** the end of the page.  Pointers to the cells are in the cell pointer array
46238
 
** that immediately follows the page header.  Cells is not necessarily
46239
 
** contiguous or in order, but cell pointers are contiguous and in order.
46240
 
**
46241
 
** Cell content makes use of variable length integers.  A variable
46242
 
** length integer is 1 to 9 bytes where the lower 7 bits of each 
46243
 
** byte are used.  The integer consists of all bytes that have bit 8 set and
46244
 
** the first byte with bit 8 clear.  The most significant byte of the integer
46245
 
** appears first.  A variable-length integer may not be more than 9 bytes long.
46246
 
** As a special case, all 8 bytes of the 9th byte are used as data.  This
46247
 
** allows a 64-bit integer to be encoded in 9 bytes.
46248
 
**
46249
 
**    0x00                      becomes  0x00000000
46250
 
**    0x7f                      becomes  0x0000007f
46251
 
**    0x81 0x00                 becomes  0x00000080
46252
 
**    0x82 0x00                 becomes  0x00000100
46253
 
**    0x80 0x7f                 becomes  0x0000007f
46254
 
**    0x8a 0x91 0xd1 0xac 0x78  becomes  0x12345678
46255
 
**    0x81 0x81 0x81 0x81 0x01  becomes  0x10204081
46256
 
**
46257
 
** Variable length integers are used for rowids and to hold the number of
46258
 
** bytes of key and data in a btree cell.
46259
 
**
46260
 
** The content of a cell looks like this:
46261
 
**
46262
 
**    SIZE    DESCRIPTION
46263
 
**      4     Page number of the left child. Omitted if leaf flag is set.
46264
 
**     var    Number of bytes of data. Omitted if the zerodata flag is set.
46265
 
**     var    Number of bytes of key. Or the key itself if intkey flag is set.
46266
 
**      *     Payload
46267
 
**      4     First page of the overflow chain.  Omitted if no overflow
46268
 
**
46269
 
** Overflow pages form a linked list.  Each page except the last is completely
46270
 
** filled with data (pagesize - 4 bytes).  The last page can have as little
46271
 
** as 1 byte of data.
46272
 
**
46273
 
**    SIZE    DESCRIPTION
46274
 
**      4     Page number of next overflow page
46275
 
**      *     Data
46276
 
**
46277
 
** Freelist pages come in two subtypes: trunk pages and leaf pages.  The
46278
 
** file header points to the first in a linked list of trunk page.  Each trunk
46279
 
** page points to multiple leaf pages.  The content of a leaf page is
46280
 
** unspecified.  A trunk page looks like this:
46281
 
**
46282
 
**    SIZE    DESCRIPTION
46283
 
**      4     Page number of next trunk page
46284
 
**      4     Number of leaf pointers on this page
46285
 
**      *     zero or more pages numbers of leaves
46286
 
*/
46287
 
 
46288
 
 
46289
 
/* The following value is the maximum cell size assuming a maximum page
46290
 
** size give above.
46291
 
*/
46292
 
#define MX_CELL_SIZE(pBt)  ((int)(pBt->pageSize-8))
46293
 
 
46294
 
/* The maximum number of cells on a single page of the database.  This
46295
 
** assumes a minimum cell size of 6 bytes  (4 bytes for the cell itself
46296
 
** plus 2 bytes for the index to the cell in the page header).  Such
46297
 
** small cells will be rare, but they are possible.
46298
 
*/
46299
 
#define MX_CELL(pBt) ((pBt->pageSize-8)/6)
46300
 
 
46301
 
/* Forward declarations */
46302
 
typedef struct MemPage MemPage;
46303
 
typedef struct BtLock BtLock;
46304
 
 
46305
 
/*
46306
 
** This is a magic string that appears at the beginning of every
46307
 
** SQLite database in order to identify the file as a real database.
46308
 
**
46309
 
** You can change this value at compile-time by specifying a
46310
 
** -DSQLITE_FILE_HEADER="..." on the compiler command-line.  The
46311
 
** header must be exactly 16 bytes including the zero-terminator so
46312
 
** the string itself should be 15 characters long.  If you change
46313
 
** the header, then your custom library will not be able to read 
46314
 
** databases generated by the standard tools and the standard tools
46315
 
** will not be able to read databases created by your custom library.
46316
 
*/
46317
 
#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
46318
 
#  define SQLITE_FILE_HEADER "SQLite format 3"
46319
 
#endif
46320
 
 
46321
 
/*
46322
 
** Page type flags.  An ORed combination of these flags appear as the
46323
 
** first byte of on-disk image of every BTree page.
46324
 
*/
46325
 
#define PTF_INTKEY    0x01
46326
 
#define PTF_ZERODATA  0x02
46327
 
#define PTF_LEAFDATA  0x04
46328
 
#define PTF_LEAF      0x08
46329
 
 
46330
 
/*
46331
 
** As each page of the file is loaded into memory, an instance of the following
46332
 
** structure is appended and initialized to zero.  This structure stores
46333
 
** information about the page that is decoded from the raw file page.
46334
 
**
46335
 
** The pParent field points back to the parent page.  This allows us to
46336
 
** walk up the BTree from any leaf to the root.  Care must be taken to
46337
 
** unref() the parent page pointer when this page is no longer referenced.
46338
 
** The pageDestructor() routine handles that chore.
46339
 
**
46340
 
** Access to all fields of this structure is controlled by the mutex
46341
 
** stored in MemPage.pBt->mutex.
46342
 
*/
46343
 
struct MemPage {
46344
 
  u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
46345
 
  u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
46346
 
  u8 intKey;           /* True if intkey flag is set */
46347
 
  u8 leaf;             /* True if leaf flag is set */
46348
 
  u8 hasData;          /* True if this page stores data */
46349
 
  u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
46350
 
  u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
46351
 
  u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
46352
 
  u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
46353
 
  u16 cellOffset;      /* Index in aData of first cell pointer */
46354
 
  u16 nFree;           /* Number of free bytes on the page */
46355
 
  u16 nCell;           /* Number of cells on this page, local and ovfl */
46356
 
  u16 maskPage;        /* Mask for page offset */
46357
 
  struct _OvflCell {   /* Cells that will not fit on aData[] */
46358
 
    u8 *pCell;          /* Pointers to the body of the overflow cell */
46359
 
    u16 idx;            /* Insert this cell before idx-th non-overflow cell */
46360
 
  } aOvfl[5];
46361
 
  BtShared *pBt;       /* Pointer to BtShared that this page is part of */
46362
 
  u8 *aData;           /* Pointer to disk image of the page data */
46363
 
  DbPage *pDbPage;     /* Pager page handle */
46364
 
  Pgno pgno;           /* Page number for this page */
46365
 
};
46366
 
 
46367
 
/*
46368
 
** The in-memory image of a disk page has the auxiliary information appended
46369
 
** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
46370
 
** that extra information.
46371
 
*/
46372
 
#define EXTRA_SIZE sizeof(MemPage)
46373
 
 
46374
 
/*
46375
 
** A linked list of the following structures is stored at BtShared.pLock.
46376
 
** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor 
46377
 
** is opened on the table with root page BtShared.iTable. Locks are removed
46378
 
** from this list when a transaction is committed or rolled back, or when
46379
 
** a btree handle is closed.
46380
 
*/
46381
 
struct BtLock {
46382
 
  Btree *pBtree;        /* Btree handle holding this lock */
46383
 
  Pgno iTable;          /* Root page of table */
46384
 
  u8 eLock;             /* READ_LOCK or WRITE_LOCK */
46385
 
  BtLock *pNext;        /* Next in BtShared.pLock list */
46386
 
};
46387
 
 
46388
 
/* Candidate values for BtLock.eLock */
46389
 
#define READ_LOCK     1
46390
 
#define WRITE_LOCK    2
46391
 
 
46392
 
/* A Btree handle
46393
 
**
46394
 
** A database connection contains a pointer to an instance of
46395
 
** this object for every database file that it has open.  This structure
46396
 
** is opaque to the database connection.  The database connection cannot
46397
 
** see the internals of this structure and only deals with pointers to
46398
 
** this structure.
46399
 
**
46400
 
** For some database files, the same underlying database cache might be 
46401
 
** shared between multiple connections.  In that case, each connection
46402
 
** has it own instance of this object.  But each instance of this object
46403
 
** points to the same BtShared object.  The database cache and the
46404
 
** schema associated with the database file are all contained within
46405
 
** the BtShared object.
46406
 
**
46407
 
** All fields in this structure are accessed under sqlite3.mutex.
46408
 
** The pBt pointer itself may not be changed while there exists cursors 
46409
 
** in the referenced BtShared that point back to this Btree since those
46410
 
** cursors have to go through this Btree to find their BtShared and
46411
 
** they often do so without holding sqlite3.mutex.
46412
 
*/
46413
 
struct Btree {
46414
 
  sqlite3 *db;       /* The database connection holding this btree */
46415
 
  BtShared *pBt;     /* Sharable content of this btree */
46416
 
  u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
46417
 
  u8 sharable;       /* True if we can share pBt with another db */
46418
 
  u8 locked;         /* True if db currently has pBt locked */
46419
 
  int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
46420
 
  int nBackup;       /* Number of backup operations reading this btree */
46421
 
  Btree *pNext;      /* List of other sharable Btrees from the same db */
46422
 
  Btree *pPrev;      /* Back pointer of the same list */
46423
 
#ifndef SQLITE_OMIT_SHARED_CACHE
46424
 
  BtLock lock;       /* Object used to lock page 1 */
46425
 
#endif
46426
 
};
46427
 
 
46428
 
/*
46429
 
** Btree.inTrans may take one of the following values.
46430
 
**
46431
 
** If the shared-data extension is enabled, there may be multiple users
46432
 
** of the Btree structure. At most one of these may open a write transaction,
46433
 
** but any number may have active read transactions.
46434
 
*/
46435
 
#define TRANS_NONE  0
46436
 
#define TRANS_READ  1
46437
 
#define TRANS_WRITE 2
46438
 
 
46439
 
/*
46440
 
** An instance of this object represents a single database file.
46441
 
** 
46442
 
** A single database file can be in use as the same time by two
46443
 
** or more database connections.  When two or more connections are
46444
 
** sharing the same database file, each connection has it own
46445
 
** private Btree object for the file and each of those Btrees points
46446
 
** to this one BtShared object.  BtShared.nRef is the number of
46447
 
** connections currently sharing this database file.
46448
 
**
46449
 
** Fields in this structure are accessed under the BtShared.mutex
46450
 
** mutex, except for nRef and pNext which are accessed under the
46451
 
** global SQLITE_MUTEX_STATIC_MASTER mutex.  The pPager field
46452
 
** may not be modified once it is initially set as long as nRef>0.
46453
 
** The pSchema field may be set once under BtShared.mutex and
46454
 
** thereafter is unchanged as long as nRef>0.
46455
 
**
46456
 
** isPending:
46457
 
**
46458
 
**   If a BtShared client fails to obtain a write-lock on a database
46459
 
**   table (because there exists one or more read-locks on the table),
46460
 
**   the shared-cache enters 'pending-lock' state and isPending is
46461
 
**   set to true.
46462
 
**
46463
 
**   The shared-cache leaves the 'pending lock' state when either of
46464
 
**   the following occur:
46465
 
**
46466
 
**     1) The current writer (BtShared.pWriter) concludes its transaction, OR
46467
 
**     2) The number of locks held by other connections drops to zero.
46468
 
**
46469
 
**   while in the 'pending-lock' state, no connection may start a new
46470
 
**   transaction.
46471
 
**
46472
 
**   This feature is included to help prevent writer-starvation.
46473
 
*/
46474
 
struct BtShared {
46475
 
  Pager *pPager;        /* The page cache */
46476
 
  sqlite3 *db;          /* Database connection currently using this Btree */
46477
 
  BtCursor *pCursor;    /* A list of all open cursors */
46478
 
  MemPage *pPage1;      /* First page of the database */
46479
 
  u8 readOnly;          /* True if the underlying file is readonly */
46480
 
  u8 pageSizeFixed;     /* True if the page size can no longer be changed */
46481
 
  u8 secureDelete;      /* True if secure_delete is enabled */
46482
 
  u8 initiallyEmpty;    /* Database is empty at start of transaction */
46483
 
  u8 openFlags;         /* Flags to sqlite3BtreeOpen() */
46484
 
#ifndef SQLITE_OMIT_AUTOVACUUM
46485
 
  u8 autoVacuum;        /* True if auto-vacuum is enabled */
46486
 
  u8 incrVacuum;        /* True if incr-vacuum is enabled */
46487
 
#endif
46488
 
  u8 inTransaction;     /* Transaction state */
46489
 
  u8 doNotUseWAL;       /* If true, do not open write-ahead-log file */
46490
 
  u16 maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
46491
 
  u16 minLocal;         /* Minimum local payload in non-LEAFDATA tables */
46492
 
  u16 maxLeaf;          /* Maximum local payload in a LEAFDATA table */
46493
 
  u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
46494
 
  u32 pageSize;         /* Total number of bytes on a page */
46495
 
  u32 usableSize;       /* Number of usable bytes on each page */
46496
 
  int nTransaction;     /* Number of open transactions (read + write) */
46497
 
  u32 nPage;            /* Number of pages in the database */
46498
 
  void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
46499
 
  void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
46500
 
  sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
46501
 
  Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
46502
 
#ifndef SQLITE_OMIT_SHARED_CACHE
46503
 
  int nRef;             /* Number of references to this structure */
46504
 
  BtShared *pNext;      /* Next on a list of sharable BtShared structs */
46505
 
  BtLock *pLock;        /* List of locks held on this shared-btree struct */
46506
 
  Btree *pWriter;       /* Btree with currently open write transaction */
46507
 
  u8 isExclusive;       /* True if pWriter has an EXCLUSIVE lock on the db */
46508
 
  u8 isPending;         /* If waiting for read-locks to clear */
46509
 
#endif
46510
 
  u8 *pTmpSpace;        /* BtShared.pageSize bytes of space for tmp use */
46511
 
};
46512
 
 
46513
 
/*
46514
 
** An instance of the following structure is used to hold information
46515
 
** about a cell.  The parseCellPtr() function fills in this structure
46516
 
** based on information extract from the raw disk page.
46517
 
*/
46518
 
typedef struct CellInfo CellInfo;
46519
 
struct CellInfo {
46520
 
  i64 nKey;      /* The key for INTKEY tables, or number of bytes in key */
46521
 
  u8 *pCell;     /* Pointer to the start of cell content */
46522
 
  u32 nData;     /* Number of bytes of data */
46523
 
  u32 nPayload;  /* Total amount of payload */
46524
 
  u16 nHeader;   /* Size of the cell content header in bytes */
46525
 
  u16 nLocal;    /* Amount of payload held locally */
46526
 
  u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
46527
 
  u16 nSize;     /* Size of the cell content on the main b-tree page */
46528
 
};
46529
 
 
46530
 
/*
46531
 
** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
46532
 
** this will be declared corrupt. This value is calculated based on a
46533
 
** maximum database size of 2^31 pages a minimum fanout of 2 for a
46534
 
** root-node and 3 for all other internal nodes.
46535
 
**
46536
 
** If a tree that appears to be taller than this is encountered, it is
46537
 
** assumed that the database is corrupt.
46538
 
*/
46539
 
#define BTCURSOR_MAX_DEPTH 20
46540
 
 
46541
 
/*
46542
 
** A cursor is a pointer to a particular entry within a particular
46543
 
** b-tree within a database file.
46544
 
**
46545
 
** The entry is identified by its MemPage and the index in
46546
 
** MemPage.aCell[] of the entry.
46547
 
**
46548
 
** A single database file can shared by two more database connections,
46549
 
** but cursors cannot be shared.  Each cursor is associated with a
46550
 
** particular database connection identified BtCursor.pBtree.db.
46551
 
**
46552
 
** Fields in this structure are accessed under the BtShared.mutex
46553
 
** found at self->pBt->mutex. 
46554
 
*/
46555
 
struct BtCursor {
46556
 
  Btree *pBtree;            /* The Btree to which this cursor belongs */
46557
 
  BtShared *pBt;            /* The BtShared this cursor points to */
46558
 
  BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
46559
 
  struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
46560
 
  Pgno pgnoRoot;            /* The root page of this tree */
46561
 
  sqlite3_int64 cachedRowid; /* Next rowid cache.  0 means not valid */
46562
 
  CellInfo info;            /* A parse of the cell we are pointing at */
46563
 
  i64 nKey;        /* Size of pKey, or last integer key */
46564
 
  void *pKey;      /* Saved key that was cursor's last known position */
46565
 
  int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
46566
 
  u8 wrFlag;                /* True if writable */
46567
 
  u8 atLast;                /* Cursor pointing to the last entry */
46568
 
  u8 validNKey;             /* True if info.nKey is valid */
46569
 
  u8 eState;                /* One of the CURSOR_XXX constants (see below) */
46570
 
#ifndef SQLITE_OMIT_INCRBLOB
46571
 
  Pgno *aOverflow;          /* Cache of overflow page locations */
46572
 
  u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
46573
 
#endif
46574
 
  i16 iPage;                            /* Index of current page in apPage */
46575
 
  u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
46576
 
  MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
46577
 
};
46578
 
 
46579
 
/*
46580
 
** Potential values for BtCursor.eState.
46581
 
**
46582
 
** CURSOR_VALID:
46583
 
**   Cursor points to a valid entry. getPayload() etc. may be called.
46584
 
**
46585
 
** CURSOR_INVALID:
46586
 
**   Cursor does not point to a valid entry. This can happen (for example) 
46587
 
**   because the table is empty or because BtreeCursorFirst() has not been
46588
 
**   called.
46589
 
**
46590
 
** CURSOR_REQUIRESEEK:
46591
 
**   The table that this cursor was opened on still exists, but has been 
46592
 
**   modified since the cursor was last used. The cursor position is saved
46593
 
**   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in 
46594
 
**   this state, restoreCursorPosition() can be called to attempt to
46595
 
**   seek the cursor to the saved position.
46596
 
**
46597
 
** CURSOR_FAULT:
46598
 
**   A unrecoverable error (an I/O error or a malloc failure) has occurred
46599
 
**   on a different connection that shares the BtShared cache with this
46600
 
**   cursor.  The error has left the cache in an inconsistent state.
46601
 
**   Do nothing else with this cursor.  Any attempt to use the cursor
46602
 
**   should return the error code stored in BtCursor.skip
46603
 
*/
46604
 
#define CURSOR_INVALID           0
46605
 
#define CURSOR_VALID             1
46606
 
#define CURSOR_REQUIRESEEK       2
46607
 
#define CURSOR_FAULT             3
46608
 
 
46609
 
/* 
46610
 
** The database page the PENDING_BYTE occupies. This page is never used.
46611
 
*/
46612
 
# define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
46613
 
 
46614
 
/*
46615
 
** These macros define the location of the pointer-map entry for a 
46616
 
** database page. The first argument to each is the number of usable
46617
 
** bytes on each page of the database (often 1024). The second is the
46618
 
** page number to look up in the pointer map.
46619
 
**
46620
 
** PTRMAP_PAGENO returns the database page number of the pointer-map
46621
 
** page that stores the required pointer. PTRMAP_PTROFFSET returns
46622
 
** the offset of the requested map entry.
46623
 
**
46624
 
** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
46625
 
** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
46626
 
** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
46627
 
** this test.
46628
 
*/
46629
 
#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
46630
 
#define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
46631
 
#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
46632
 
 
46633
 
/*
46634
 
** The pointer map is a lookup table that identifies the parent page for
46635
 
** each child page in the database file.  The parent page is the page that
46636
 
** contains a pointer to the child.  Every page in the database contains
46637
 
** 0 or 1 parent pages.  (In this context 'database page' refers
46638
 
** to any page that is not part of the pointer map itself.)  Each pointer map
46639
 
** entry consists of a single byte 'type' and a 4 byte parent page number.
46640
 
** The PTRMAP_XXX identifiers below are the valid types.
46641
 
**
46642
 
** The purpose of the pointer map is to facility moving pages from one
46643
 
** position in the file to another as part of autovacuum.  When a page
46644
 
** is moved, the pointer in its parent must be updated to point to the
46645
 
** new location.  The pointer map is used to locate the parent page quickly.
46646
 
**
46647
 
** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
46648
 
**                  used in this case.
46649
 
**
46650
 
** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number 
46651
 
**                  is not used in this case.
46652
 
**
46653
 
** PTRMAP_OVERFLOW1: The database page is the first page in a list of 
46654
 
**                   overflow pages. The page number identifies the page that
46655
 
**                   contains the cell with a pointer to this overflow page.
46656
 
**
46657
 
** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
46658
 
**                   overflow pages. The page-number identifies the previous
46659
 
**                   page in the overflow page list.
46660
 
**
46661
 
** PTRMAP_BTREE: The database page is a non-root btree page. The page number
46662
 
**               identifies the parent page in the btree.
46663
 
*/
46664
 
#define PTRMAP_ROOTPAGE 1
46665
 
#define PTRMAP_FREEPAGE 2
46666
 
#define PTRMAP_OVERFLOW1 3
46667
 
#define PTRMAP_OVERFLOW2 4
46668
 
#define PTRMAP_BTREE 5
46669
 
 
46670
 
/* A bunch of assert() statements to check the transaction state variables
46671
 
** of handle p (type Btree*) are internally consistent.
46672
 
*/
46673
 
#define btreeIntegrity(p) \
46674
 
  assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
46675
 
  assert( p->pBt->inTransaction>=p->inTrans ); 
46676
 
 
46677
 
 
46678
 
/*
46679
 
** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
46680
 
** if the database supports auto-vacuum or not. Because it is used
46681
 
** within an expression that is an argument to another macro 
46682
 
** (sqliteMallocRaw), it is not possible to use conditional compilation.
46683
 
** So, this macro is defined instead.
46684
 
*/
46685
 
#ifndef SQLITE_OMIT_AUTOVACUUM
46686
 
#define ISAUTOVACUUM (pBt->autoVacuum)
46687
 
#else
46688
 
#define ISAUTOVACUUM 0
46689
 
#endif
46690
 
 
46691
 
 
46692
 
/*
46693
 
** This structure is passed around through all the sanity checking routines
46694
 
** in order to keep track of some global state information.
46695
 
*/
46696
 
typedef struct IntegrityCk IntegrityCk;
46697
 
struct IntegrityCk {
46698
 
  BtShared *pBt;    /* The tree being checked out */
46699
 
  Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
46700
 
  Pgno nPage;       /* Number of pages in the database */
46701
 
  int *anRef;       /* Number of times each page is referenced */
46702
 
  int mxErr;        /* Stop accumulating errors when this reaches zero */
46703
 
  int nErr;         /* Number of messages written to zErrMsg so far */
46704
 
  int mallocFailed; /* A memory allocation error has occurred */
46705
 
  StrAccum errMsg;  /* Accumulate the error message text here */
46706
 
};
46707
 
 
46708
 
/*
46709
 
** Read or write a two- and four-byte big-endian integer values.
46710
 
*/
46711
 
#define get2byte(x)   ((x)[0]<<8 | (x)[1])
46712
 
#define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
46713
 
#define get4byte sqlite3Get4byte
46714
 
#define put4byte sqlite3Put4byte
46715
 
 
46716
 
/************** End of btreeInt.h ********************************************/
46717
 
/************** Continuing where we left off in btmutex.c ********************/
46718
 
#ifndef SQLITE_OMIT_SHARED_CACHE
46719
 
#if SQLITE_THREADSAFE
46720
 
 
46721
 
/*
46722
 
** Obtain the BtShared mutex associated with B-Tree handle p. Also,
46723
 
** set BtShared.db to the database handle associated with p and the
46724
 
** p->locked boolean to true.
46725
 
*/
46726
 
static void lockBtreeMutex(Btree *p){
46727
 
  assert( p->locked==0 );
46728
 
  assert( sqlite3_mutex_notheld(p->pBt->mutex) );
46729
 
  assert( sqlite3_mutex_held(p->db->mutex) );
46730
 
 
46731
 
  sqlite3_mutex_enter(p->pBt->mutex);
46732
 
  p->pBt->db = p->db;
46733
 
  p->locked = 1;
46734
 
}
46735
 
 
46736
 
/*
46737
 
** Release the BtShared mutex associated with B-Tree handle p and
46738
 
** clear the p->locked boolean.
46739
 
*/
46740
 
static void unlockBtreeMutex(Btree *p){
46741
 
  BtShared *pBt = p->pBt;
46742
 
  assert( p->locked==1 );
46743
 
  assert( sqlite3_mutex_held(pBt->mutex) );
46744
 
  assert( sqlite3_mutex_held(p->db->mutex) );
46745
 
  assert( p->db==pBt->db );
46746
 
 
46747
 
  sqlite3_mutex_leave(pBt->mutex);
46748
 
  p->locked = 0;
46749
 
}
46750
 
 
46751
 
/*
46752
 
** Enter a mutex on the given BTree object.
46753
 
**
46754
 
** If the object is not sharable, then no mutex is ever required
46755
 
** and this routine is a no-op.  The underlying mutex is non-recursive.
46756
 
** But we keep a reference count in Btree.wantToLock so the behavior
46757
 
** of this interface is recursive.
46758
 
**
46759
 
** To avoid deadlocks, multiple Btrees are locked in the same order
46760
 
** by all database connections.  The p->pNext is a list of other
46761
 
** Btrees belonging to the same database connection as the p Btree
46762
 
** which need to be locked after p.  If we cannot get a lock on
46763
 
** p, then first unlock all of the others on p->pNext, then wait
46764
 
** for the lock to become available on p, then relock all of the
46765
 
** subsequent Btrees that desire a lock.
46766
 
*/
46767
 
SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
46768
 
  Btree *pLater;
46769
 
 
46770
 
  /* Some basic sanity checking on the Btree.  The list of Btrees
46771
 
  ** connected by pNext and pPrev should be in sorted order by
46772
 
  ** Btree.pBt value. All elements of the list should belong to
46773
 
  ** the same connection. Only shared Btrees are on the list. */
46774
 
  assert( p->pNext==0 || p->pNext->pBt>p->pBt );
46775
 
  assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
46776
 
  assert( p->pNext==0 || p->pNext->db==p->db );
46777
 
  assert( p->pPrev==0 || p->pPrev->db==p->db );
46778
 
  assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
46779
 
 
46780
 
  /* Check for locking consistency */
46781
 
  assert( !p->locked || p->wantToLock>0 );
46782
 
  assert( p->sharable || p->wantToLock==0 );
46783
 
 
46784
 
  /* We should already hold a lock on the database connection */
46785
 
  assert( sqlite3_mutex_held(p->db->mutex) );
46786
 
 
46787
 
  /* Unless the database is sharable and unlocked, then BtShared.db
46788
 
  ** should already be set correctly. */
46789
 
  assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
46790
 
 
46791
 
  if( !p->sharable ) return;
46792
 
  p->wantToLock++;
46793
 
  if( p->locked ) return;
46794
 
 
46795
 
  /* In most cases, we should be able to acquire the lock we
46796
 
  ** want without having to go throught the ascending lock
46797
 
  ** procedure that follows.  Just be sure not to block.
46798
 
  */
46799
 
  if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
46800
 
    p->pBt->db = p->db;
46801
 
    p->locked = 1;
46802
 
    return;
46803
 
  }
46804
 
 
46805
 
  /* To avoid deadlock, first release all locks with a larger
46806
 
  ** BtShared address.  Then acquire our lock.  Then reacquire
46807
 
  ** the other BtShared locks that we used to hold in ascending
46808
 
  ** order.
46809
 
  */
46810
 
  for(pLater=p->pNext; pLater; pLater=pLater->pNext){
46811
 
    assert( pLater->sharable );
46812
 
    assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
46813
 
    assert( !pLater->locked || pLater->wantToLock>0 );
46814
 
    if( pLater->locked ){
46815
 
      unlockBtreeMutex(pLater);
46816
 
    }
46817
 
  }
46818
 
  lockBtreeMutex(p);
46819
 
  for(pLater=p->pNext; pLater; pLater=pLater->pNext){
46820
 
    if( pLater->wantToLock ){
46821
 
      lockBtreeMutex(pLater);
46822
 
    }
46823
 
  }
46824
 
}
46825
 
 
46826
 
/*
46827
 
** Exit the recursive mutex on a Btree.
46828
 
*/
46829
 
SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
46830
 
  if( p->sharable ){
46831
 
    assert( p->wantToLock>0 );
46832
 
    p->wantToLock--;
46833
 
    if( p->wantToLock==0 ){
46834
 
      unlockBtreeMutex(p);
46835
 
    }
46836
 
  }
46837
 
}
46838
 
 
46839
 
#ifndef NDEBUG
46840
 
/*
46841
 
** Return true if the BtShared mutex is held on the btree, or if the
46842
 
** B-Tree is not marked as sharable.
46843
 
**
46844
 
** This routine is used only from within assert() statements.
46845
 
*/
46846
 
SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
46847
 
  assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
46848
 
  assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
46849
 
  assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
46850
 
  assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
46851
 
 
46852
 
  return (p->sharable==0 || p->locked);
46853
 
}
46854
 
#endif
46855
 
 
46856
 
 
46857
 
#ifndef SQLITE_OMIT_INCRBLOB
46858
 
/*
46859
 
** Enter and leave a mutex on a Btree given a cursor owned by that
46860
 
** Btree.  These entry points are used by incremental I/O and can be
46861
 
** omitted if that module is not used.
46862
 
*/
46863
 
SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
46864
 
  sqlite3BtreeEnter(pCur->pBtree);
46865
 
}
46866
 
SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
46867
 
  sqlite3BtreeLeave(pCur->pBtree);
46868
 
}
46869
 
#endif /* SQLITE_OMIT_INCRBLOB */
46870
 
 
46871
 
 
46872
 
/*
46873
 
** Enter the mutex on every Btree associated with a database
46874
 
** connection.  This is needed (for example) prior to parsing
46875
 
** a statement since we will be comparing table and column names
46876
 
** against all schemas and we do not want those schemas being
46877
 
** reset out from under us.
46878
 
**
46879
 
** There is a corresponding leave-all procedures.
46880
 
**
46881
 
** Enter the mutexes in accending order by BtShared pointer address
46882
 
** to avoid the possibility of deadlock when two threads with
46883
 
** two or more btrees in common both try to lock all their btrees
46884
 
** at the same instant.
46885
 
*/
46886
 
SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
46887
 
  int i;
46888
 
  Btree *p;
46889
 
  assert( sqlite3_mutex_held(db->mutex) );
46890
 
  for(i=0; i<db->nDb; i++){
46891
 
    p = db->aDb[i].pBt;
46892
 
    if( p ) sqlite3BtreeEnter(p);
46893
 
  }
46894
 
}
46895
 
SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
46896
 
  int i;
46897
 
  Btree *p;
46898
 
  assert( sqlite3_mutex_held(db->mutex) );
46899
 
  for(i=0; i<db->nDb; i++){
46900
 
    p = db->aDb[i].pBt;
46901
 
    if( p ) sqlite3BtreeLeave(p);
46902
 
  }
46903
 
}
46904
 
 
46905
 
/*
46906
 
** Return true if a particular Btree requires a lock.  Return FALSE if
46907
 
** no lock is ever required since it is not sharable.
46908
 
*/
46909
 
SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
46910
 
  return p->sharable;
46911
 
}
46912
 
 
46913
 
#ifndef NDEBUG
46914
 
/*
46915
 
** Return true if the current thread holds the database connection
46916
 
** mutex and all required BtShared mutexes.
46917
 
**
46918
 
** This routine is used inside assert() statements only.
46919
 
*/
46920
 
SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
46921
 
  int i;
46922
 
  if( !sqlite3_mutex_held(db->mutex) ){
46923
 
    return 0;
46924
 
  }
46925
 
  for(i=0; i<db->nDb; i++){
46926
 
    Btree *p;
46927
 
    p = db->aDb[i].pBt;
46928
 
    if( p && p->sharable &&
46929
 
         (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
46930
 
      return 0;
46931
 
    }
46932
 
  }
46933
 
  return 1;
46934
 
}
46935
 
#endif /* NDEBUG */
46936
 
 
46937
 
#ifndef NDEBUG
46938
 
/*
46939
 
** Return true if the correct mutexes are held for accessing the
46940
 
** db->aDb[iDb].pSchema structure.  The mutexes required for schema
46941
 
** access are:
46942
 
**
46943
 
**   (1) The mutex on db
46944
 
**   (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
46945
 
**
46946
 
** If pSchema is not NULL, then iDb is computed from pSchema and
46947
 
** db using sqlite3SchemaToIndex().
46948
 
*/
46949
 
SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
46950
 
  Btree *p;
46951
 
  assert( db!=0 );
46952
 
  if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
46953
 
  assert( iDb>=0 && iDb<db->nDb );
46954
 
  if( !sqlite3_mutex_held(db->mutex) ) return 0;
46955
 
  if( iDb==1 ) return 1;
46956
 
  p = db->aDb[iDb].pBt;
46957
 
  assert( p!=0 );
46958
 
  return p->sharable==0 || p->locked==1;
46959
 
}
46960
 
#endif /* NDEBUG */
46961
 
 
46962
 
#else /* SQLITE_THREADSAFE>0 above.  SQLITE_THREADSAFE==0 below */
46963
 
/*
46964
 
** The following are special cases for mutex enter routines for use
46965
 
** in single threaded applications that use shared cache.  Except for
46966
 
** these two routines, all mutex operations are no-ops in that case and
46967
 
** are null #defines in btree.h.
46968
 
**
46969
 
** If shared cache is disabled, then all btree mutex routines, including
46970
 
** the ones below, are no-ops and are null #defines in btree.h.
46971
 
*/
46972
 
 
46973
 
SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
46974
 
  p->pBt->db = p->db;
46975
 
}
46976
 
SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
46977
 
  int i;
46978
 
  for(i=0; i<db->nDb; i++){
46979
 
    Btree *p = db->aDb[i].pBt;
46980
 
    if( p ){
46981
 
      p->pBt->db = p->db;
46982
 
    }
46983
 
  }
46984
 
}
46985
 
#endif /* if SQLITE_THREADSAFE */
46986
 
#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
46987
 
 
46988
 
/************** End of btmutex.c *********************************************/
46989
 
/************** Begin file btree.c *******************************************/
46990
 
/*
46991
 
** 2004 April 6
46992
 
**
46993
 
** The author disclaims copyright to this source code.  In place of
46994
 
** a legal notice, here is a blessing:
46995
 
**
46996
 
**    May you do good and not evil.
46997
 
**    May you find forgiveness for yourself and forgive others.
46998
 
**    May you share freely, never taking more than you give.
46999
 
**
47000
 
*************************************************************************
47001
 
** This file implements a external (disk-based) database using BTrees.
47002
 
** See the header comment on "btreeInt.h" for additional information.
47003
 
** Including a description of file format and an overview of operation.
47004
 
*/
47005
 
 
47006
 
/*
47007
 
** The header string that appears at the beginning of every
47008
 
** SQLite database.
47009
 
*/
47010
 
static const char zMagicHeader[] = SQLITE_FILE_HEADER;
47011
 
 
47012
 
/*
47013
 
** Set this global variable to 1 to enable tracing using the TRACE
47014
 
** macro.
47015
 
*/
47016
 
#if 0
47017
 
int sqlite3BtreeTrace=1;  /* True to enable tracing */
47018
 
# define TRACE(X)  if(sqlite3BtreeTrace){printf X;fflush(stdout);}
47019
 
#else
47020
 
# define TRACE(X)
47021
 
#endif
47022
 
 
47023
 
/*
47024
 
** Extract a 2-byte big-endian integer from an array of unsigned bytes.
47025
 
** But if the value is zero, make it 65536.
47026
 
**
47027
 
** This routine is used to extract the "offset to cell content area" value
47028
 
** from the header of a btree page.  If the page size is 65536 and the page
47029
 
** is empty, the offset should be 65536, but the 2-byte value stores zero.
47030
 
** This routine makes the necessary adjustment to 65536.
47031
 
*/
47032
 
#define get2byteNotZero(X)  (((((int)get2byte(X))-1)&0xffff)+1)
47033
 
 
47034
 
#ifndef SQLITE_OMIT_SHARED_CACHE
47035
 
/*
47036
 
** A list of BtShared objects that are eligible for participation
47037
 
** in shared cache.  This variable has file scope during normal builds,
47038
 
** but the test harness needs to access it so we make it global for 
47039
 
** test builds.
47040
 
**
47041
 
** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
47042
 
*/
47043
 
#ifdef SQLITE_TEST
47044
 
SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
47045
 
#else
47046
 
static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
47047
 
#endif
47048
 
#endif /* SQLITE_OMIT_SHARED_CACHE */
47049
 
 
47050
 
#ifndef SQLITE_OMIT_SHARED_CACHE
47051
 
/*
47052
 
** Enable or disable the shared pager and schema features.
47053
 
**
47054
 
** This routine has no effect on existing database connections.
47055
 
** The shared cache setting effects only future calls to
47056
 
** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
47057
 
*/
47058
 
SQLITE_API int sqlite3_enable_shared_cache(int enable){
47059
 
  sqlite3GlobalConfig.sharedCacheEnabled = enable;
47060
 
  return SQLITE_OK;
47061
 
}
47062
 
#endif
47063
 
 
47064
 
 
47065
 
 
47066
 
#ifdef SQLITE_OMIT_SHARED_CACHE
47067
 
  /*
47068
 
  ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
47069
 
  ** and clearAllSharedCacheTableLocks()
47070
 
  ** manipulate entries in the BtShared.pLock linked list used to store
47071
 
  ** shared-cache table level locks. If the library is compiled with the
47072
 
  ** shared-cache feature disabled, then there is only ever one user
47073
 
  ** of each BtShared structure and so this locking is not necessary. 
47074
 
  ** So define the lock related functions as no-ops.
47075
 
  */
47076
 
  #define querySharedCacheTableLock(a,b,c) SQLITE_OK
47077
 
  #define setSharedCacheTableLock(a,b,c) SQLITE_OK
47078
 
  #define clearAllSharedCacheTableLocks(a)
47079
 
  #define downgradeAllSharedCacheTableLocks(a)
47080
 
  #define hasSharedCacheTableLock(a,b,c,d) 1
47081
 
  #define hasReadConflicts(a, b) 0
47082
 
#endif
47083
 
 
47084
 
#ifndef SQLITE_OMIT_SHARED_CACHE
47085
 
 
47086
 
#ifdef SQLITE_DEBUG
47087
 
/*
47088
 
**** This function is only used as part of an assert() statement. ***
47089
 
**
47090
 
** Check to see if pBtree holds the required locks to read or write to the 
47091
 
** table with root page iRoot.   Return 1 if it does and 0 if not.
47092
 
**
47093
 
** For example, when writing to a table with root-page iRoot via 
47094
 
** Btree connection pBtree:
47095
 
**
47096
 
**    assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
47097
 
**
47098
 
** When writing to an index that resides in a sharable database, the 
47099
 
** caller should have first obtained a lock specifying the root page of
47100
 
** the corresponding table. This makes things a bit more complicated,
47101
 
** as this module treats each table as a separate structure. To determine
47102
 
** the table corresponding to the index being written, this
47103
 
** function has to search through the database schema.
47104
 
**
47105
 
** Instead of a lock on the table/index rooted at page iRoot, the caller may
47106
 
** hold a write-lock on the schema table (root page 1). This is also
47107
 
** acceptable.
47108
 
*/
47109
 
static int hasSharedCacheTableLock(
47110
 
  Btree *pBtree,         /* Handle that must hold lock */
47111
 
  Pgno iRoot,            /* Root page of b-tree */
47112
 
  int isIndex,           /* True if iRoot is the root of an index b-tree */
47113
 
  int eLockType          /* Required lock type (READ_LOCK or WRITE_LOCK) */
47114
 
){
47115
 
  Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
47116
 
  Pgno iTab = 0;
47117
 
  BtLock *pLock;
47118
 
 
47119
 
  /* If this database is not shareable, or if the client is reading
47120
 
  ** and has the read-uncommitted flag set, then no lock is required. 
47121
 
  ** Return true immediately.
47122
 
  */
47123
 
  if( (pBtree->sharable==0)
47124
 
   || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted))
47125
 
  ){
47126
 
    return 1;
47127
 
  }
47128
 
 
47129
 
  /* If the client is reading  or writing an index and the schema is
47130
 
  ** not loaded, then it is too difficult to actually check to see if
47131
 
  ** the correct locks are held.  So do not bother - just return true.
47132
 
  ** This case does not come up very often anyhow.
47133
 
  */
47134
 
  if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
47135
 
    return 1;
47136
 
  }
47137
 
 
47138
 
  /* Figure out the root-page that the lock should be held on. For table
47139
 
  ** b-trees, this is just the root page of the b-tree being read or
47140
 
  ** written. For index b-trees, it is the root page of the associated
47141
 
  ** table.  */
47142
 
  if( isIndex ){
47143
 
    HashElem *p;
47144
 
    for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
47145
 
      Index *pIdx = (Index *)sqliteHashData(p);
47146
 
      if( pIdx->tnum==(int)iRoot ){
47147
 
        iTab = pIdx->pTable->tnum;
47148
 
      }
47149
 
    }
47150
 
  }else{
47151
 
    iTab = iRoot;
47152
 
  }
47153
 
 
47154
 
  /* Search for the required lock. Either a write-lock on root-page iTab, a 
47155
 
  ** write-lock on the schema table, or (if the client is reading) a
47156
 
  ** read-lock on iTab will suffice. Return 1 if any of these are found.  */
47157
 
  for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
47158
 
    if( pLock->pBtree==pBtree 
47159
 
     && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
47160
 
     && pLock->eLock>=eLockType 
47161
 
    ){
47162
 
      return 1;
47163
 
    }
47164
 
  }
47165
 
 
47166
 
  /* Failed to find the required lock. */
47167
 
  return 0;
47168
 
}
47169
 
#endif /* SQLITE_DEBUG */
47170
 
 
47171
 
#ifdef SQLITE_DEBUG
47172
 
/*
47173
 
**** This function may be used as part of assert() statements only. ****
47174
 
**
47175
 
** Return true if it would be illegal for pBtree to write into the
47176
 
** table or index rooted at iRoot because other shared connections are
47177
 
** simultaneously reading that same table or index.
47178
 
**
47179
 
** It is illegal for pBtree to write if some other Btree object that
47180
 
** shares the same BtShared object is currently reading or writing
47181
 
** the iRoot table.  Except, if the other Btree object has the
47182
 
** read-uncommitted flag set, then it is OK for the other object to
47183
 
** have a read cursor.
47184
 
**
47185
 
** For example, before writing to any part of the table or index
47186
 
** rooted at page iRoot, one should call:
47187
 
**
47188
 
**    assert( !hasReadConflicts(pBtree, iRoot) );
47189
 
*/
47190
 
static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
47191
 
  BtCursor *p;
47192
 
  for(p=pBtree->pBt->pCursor; p; p=p->pNext){
47193
 
    if( p->pgnoRoot==iRoot 
47194
 
     && p->pBtree!=pBtree
47195
 
     && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted)
47196
 
    ){
47197
 
      return 1;
47198
 
    }
47199
 
  }
47200
 
  return 0;
47201
 
}
47202
 
#endif    /* #ifdef SQLITE_DEBUG */
47203
 
 
47204
 
/*
47205
 
** Query to see if Btree handle p may obtain a lock of type eLock 
47206
 
** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
47207
 
** SQLITE_OK if the lock may be obtained (by calling
47208
 
** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
47209
 
*/
47210
 
static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
47211
 
  BtShared *pBt = p->pBt;
47212
 
  BtLock *pIter;
47213
 
 
47214
 
  assert( sqlite3BtreeHoldsMutex(p) );
47215
 
  assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
47216
 
  assert( p->db!=0 );
47217
 
  assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
47218
 
  
47219
 
  /* If requesting a write-lock, then the Btree must have an open write
47220
 
  ** transaction on this file. And, obviously, for this to be so there 
47221
 
  ** must be an open write transaction on the file itself.
47222
 
  */
47223
 
  assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
47224
 
  assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
47225
 
  
47226
 
  /* This routine is a no-op if the shared-cache is not enabled */
47227
 
  if( !p->sharable ){
47228
 
    return SQLITE_OK;
47229
 
  }
47230
 
 
47231
 
  /* If some other connection is holding an exclusive lock, the
47232
 
  ** requested lock may not be obtained.
47233
 
  */
47234
 
  if( pBt->pWriter!=p && pBt->isExclusive ){
47235
 
    sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
47236
 
    return SQLITE_LOCKED_SHAREDCACHE;
47237
 
  }
47238
 
 
47239
 
  for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
47240
 
    /* The condition (pIter->eLock!=eLock) in the following if(...) 
47241
 
    ** statement is a simplification of:
47242
 
    **
47243
 
    **   (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
47244
 
    **
47245
 
    ** since we know that if eLock==WRITE_LOCK, then no other connection
47246
 
    ** may hold a WRITE_LOCK on any table in this file (since there can
47247
 
    ** only be a single writer).
47248
 
    */
47249
 
    assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
47250
 
    assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
47251
 
    if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
47252
 
      sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
47253
 
      if( eLock==WRITE_LOCK ){
47254
 
        assert( p==pBt->pWriter );
47255
 
        pBt->isPending = 1;
47256
 
      }
47257
 
      return SQLITE_LOCKED_SHAREDCACHE;
47258
 
    }
47259
 
  }
47260
 
  return SQLITE_OK;
47261
 
}
47262
 
#endif /* !SQLITE_OMIT_SHARED_CACHE */
47263
 
 
47264
 
#ifndef SQLITE_OMIT_SHARED_CACHE
47265
 
/*
47266
 
** Add a lock on the table with root-page iTable to the shared-btree used
47267
 
** by Btree handle p. Parameter eLock must be either READ_LOCK or 
47268
 
** WRITE_LOCK.
47269
 
**
47270
 
** This function assumes the following:
47271
 
**
47272
 
**   (a) The specified Btree object p is connected to a sharable
47273
 
**       database (one with the BtShared.sharable flag set), and
47274
 
**
47275
 
**   (b) No other Btree objects hold a lock that conflicts
47276
 
**       with the requested lock (i.e. querySharedCacheTableLock() has
47277
 
**       already been called and returned SQLITE_OK).
47278
 
**
47279
 
** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM 
47280
 
** is returned if a malloc attempt fails.
47281
 
*/
47282
 
static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
47283
 
  BtShared *pBt = p->pBt;
47284
 
  BtLock *pLock = 0;
47285
 
  BtLock *pIter;
47286
 
 
47287
 
  assert( sqlite3BtreeHoldsMutex(p) );
47288
 
  assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
47289
 
  assert( p->db!=0 );
47290
 
 
47291
 
  /* A connection with the read-uncommitted flag set will never try to
47292
 
  ** obtain a read-lock using this function. The only read-lock obtained
47293
 
  ** by a connection in read-uncommitted mode is on the sqlite_master 
47294
 
  ** table, and that lock is obtained in BtreeBeginTrans().  */
47295
 
  assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
47296
 
 
47297
 
  /* This function should only be called on a sharable b-tree after it 
47298
 
  ** has been determined that no other b-tree holds a conflicting lock.  */
47299
 
  assert( p->sharable );
47300
 
  assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
47301
 
 
47302
 
  /* First search the list for an existing lock on this table. */
47303
 
  for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
47304
 
    if( pIter->iTable==iTable && pIter->pBtree==p ){
47305
 
      pLock = pIter;
47306
 
      break;
47307
 
    }
47308
 
  }
47309
 
 
47310
 
  /* If the above search did not find a BtLock struct associating Btree p
47311
 
  ** with table iTable, allocate one and link it into the list.
47312
 
  */
47313
 
  if( !pLock ){
47314
 
    pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
47315
 
    if( !pLock ){
47316
 
      return SQLITE_NOMEM;
47317
 
    }
47318
 
    pLock->iTable = iTable;
47319
 
    pLock->pBtree = p;
47320
 
    pLock->pNext = pBt->pLock;
47321
 
    pBt->pLock = pLock;
47322
 
  }
47323
 
 
47324
 
  /* Set the BtLock.eLock variable to the maximum of the current lock
47325
 
  ** and the requested lock. This means if a write-lock was already held
47326
 
  ** and a read-lock requested, we don't incorrectly downgrade the lock.
47327
 
  */
47328
 
  assert( WRITE_LOCK>READ_LOCK );
47329
 
  if( eLock>pLock->eLock ){
47330
 
    pLock->eLock = eLock;
47331
 
  }
47332
 
 
47333
 
  return SQLITE_OK;
47334
 
}
47335
 
#endif /* !SQLITE_OMIT_SHARED_CACHE */
47336
 
 
47337
 
#ifndef SQLITE_OMIT_SHARED_CACHE
47338
 
/*
47339
 
** Release all the table locks (locks obtained via calls to
47340
 
** the setSharedCacheTableLock() procedure) held by Btree object p.
47341
 
**
47342
 
** This function assumes that Btree p has an open read or write 
47343
 
** transaction. If it does not, then the BtShared.isPending variable
47344
 
** may be incorrectly cleared.
47345
 
*/
47346
 
static void clearAllSharedCacheTableLocks(Btree *p){
47347
 
  BtShared *pBt = p->pBt;
47348
 
  BtLock **ppIter = &pBt->pLock;
47349
 
 
47350
 
  assert( sqlite3BtreeHoldsMutex(p) );
47351
 
  assert( p->sharable || 0==*ppIter );
47352
 
  assert( p->inTrans>0 );
47353
 
 
47354
 
  while( *ppIter ){
47355
 
    BtLock *pLock = *ppIter;
47356
 
    assert( pBt->isExclusive==0 || pBt->pWriter==pLock->pBtree );
47357
 
    assert( pLock->pBtree->inTrans>=pLock->eLock );
47358
 
    if( pLock->pBtree==p ){
47359
 
      *ppIter = pLock->pNext;
47360
 
      assert( pLock->iTable!=1 || pLock==&p->lock );
47361
 
      if( pLock->iTable!=1 ){
47362
 
        sqlite3_free(pLock);
47363
 
      }
47364
 
    }else{
47365
 
      ppIter = &pLock->pNext;
47366
 
    }
47367
 
  }
47368
 
 
47369
 
  assert( pBt->isPending==0 || pBt->pWriter );
47370
 
  if( pBt->pWriter==p ){
47371
 
    pBt->pWriter = 0;
47372
 
    pBt->isExclusive = 0;
47373
 
    pBt->isPending = 0;
47374
 
  }else if( pBt->nTransaction==2 ){
47375
 
    /* This function is called when Btree p is concluding its 
47376
 
    ** transaction. If there currently exists a writer, and p is not
47377
 
    ** that writer, then the number of locks held by connections other
47378
 
    ** than the writer must be about to drop to zero. In this case
47379
 
    ** set the isPending flag to 0.
47380
 
    **
47381
 
    ** If there is not currently a writer, then BtShared.isPending must
47382
 
    ** be zero already. So this next line is harmless in that case.
47383
 
    */
47384
 
    pBt->isPending = 0;
47385
 
  }
47386
 
}
47387
 
 
47388
 
/*
47389
 
** This function changes all write-locks held by Btree p into read-locks.
47390
 
*/
47391
 
static void downgradeAllSharedCacheTableLocks(Btree *p){
47392
 
  BtShared *pBt = p->pBt;
47393
 
  if( pBt->pWriter==p ){
47394
 
    BtLock *pLock;
47395
 
    pBt->pWriter = 0;
47396
 
    pBt->isExclusive = 0;
47397
 
    pBt->isPending = 0;
47398
 
    for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
47399
 
      assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
47400
 
      pLock->eLock = READ_LOCK;
47401
 
    }
47402
 
  }
47403
 
}
47404
 
 
47405
 
#endif /* SQLITE_OMIT_SHARED_CACHE */
47406
 
 
47407
 
static void releasePage(MemPage *pPage);  /* Forward reference */
47408
 
 
47409
 
/*
47410
 
***** This routine is used inside of assert() only ****
47411
 
**
47412
 
** Verify that the cursor holds the mutex on its BtShared
47413
 
*/
47414
 
#ifdef SQLITE_DEBUG
47415
 
static int cursorHoldsMutex(BtCursor *p){
47416
 
  return sqlite3_mutex_held(p->pBt->mutex);
47417
 
}
47418
 
#endif
47419
 
 
47420
 
 
47421
 
#ifndef SQLITE_OMIT_INCRBLOB
47422
 
/*
47423
 
** Invalidate the overflow page-list cache for cursor pCur, if any.
47424
 
*/
47425
 
static void invalidateOverflowCache(BtCursor *pCur){
47426
 
  assert( cursorHoldsMutex(pCur) );
47427
 
  sqlite3_free(pCur->aOverflow);
47428
 
  pCur->aOverflow = 0;
47429
 
}
47430
 
 
47431
 
/*
47432
 
** Invalidate the overflow page-list cache for all cursors opened
47433
 
** on the shared btree structure pBt.
47434
 
*/
47435
 
static void invalidateAllOverflowCache(BtShared *pBt){
47436
 
  BtCursor *p;
47437
 
  assert( sqlite3_mutex_held(pBt->mutex) );
47438
 
  for(p=pBt->pCursor; p; p=p->pNext){
47439
 
    invalidateOverflowCache(p);
47440
 
  }
47441
 
}
47442
 
 
47443
 
/*
47444
 
** This function is called before modifying the contents of a table
47445
 
** to invalidate any incrblob cursors that are open on the
47446
 
** row or one of the rows being modified.
47447
 
**
47448
 
** If argument isClearTable is true, then the entire contents of the
47449
 
** table is about to be deleted. In this case invalidate all incrblob
47450
 
** cursors open on any row within the table with root-page pgnoRoot.
47451
 
**
47452
 
** Otherwise, if argument isClearTable is false, then the row with
47453
 
** rowid iRow is being replaced or deleted. In this case invalidate
47454
 
** only those incrblob cursors open on that specific row.
47455
 
*/
47456
 
static void invalidateIncrblobCursors(
47457
 
  Btree *pBtree,          /* The database file to check */
47458
 
  i64 iRow,               /* The rowid that might be changing */
47459
 
  int isClearTable        /* True if all rows are being deleted */
47460
 
){
47461
 
  BtCursor *p;
47462
 
  BtShared *pBt = pBtree->pBt;
47463
 
  assert( sqlite3BtreeHoldsMutex(pBtree) );
47464
 
  for(p=pBt->pCursor; p; p=p->pNext){
47465
 
    if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){
47466
 
      p->eState = CURSOR_INVALID;
47467
 
    }
47468
 
  }
47469
 
}
47470
 
 
47471
 
#else
47472
 
  /* Stub functions when INCRBLOB is omitted */
47473
 
  #define invalidateOverflowCache(x)
47474
 
  #define invalidateAllOverflowCache(x)
47475
 
  #define invalidateIncrblobCursors(x,y,z)
47476
 
#endif /* SQLITE_OMIT_INCRBLOB */
47477
 
 
47478
 
/*
47479
 
** Set bit pgno of the BtShared.pHasContent bitvec. This is called 
47480
 
** when a page that previously contained data becomes a free-list leaf 
47481
 
** page.
47482
 
**
47483
 
** The BtShared.pHasContent bitvec exists to work around an obscure
47484
 
** bug caused by the interaction of two useful IO optimizations surrounding
47485
 
** free-list leaf pages:
47486
 
**
47487
 
**   1) When all data is deleted from a page and the page becomes
47488
 
**      a free-list leaf page, the page is not written to the database
47489
 
**      (as free-list leaf pages contain no meaningful data). Sometimes
47490
 
**      such a page is not even journalled (as it will not be modified,
47491
 
**      why bother journalling it?).
47492
 
**
47493
 
**   2) When a free-list leaf page is reused, its content is not read
47494
 
**      from the database or written to the journal file (why should it
47495
 
**      be, if it is not at all meaningful?).
47496
 
**
47497
 
** By themselves, these optimizations work fine and provide a handy
47498
 
** performance boost to bulk delete or insert operations. However, if
47499
 
** a page is moved to the free-list and then reused within the same
47500
 
** transaction, a problem comes up. If the page is not journalled when
47501
 
** it is moved to the free-list and it is also not journalled when it
47502
 
** is extracted from the free-list and reused, then the original data
47503
 
** may be lost. In the event of a rollback, it may not be possible
47504
 
** to restore the database to its original configuration.
47505
 
**
47506
 
** The solution is the BtShared.pHasContent bitvec. Whenever a page is 
47507
 
** moved to become a free-list leaf page, the corresponding bit is
47508
 
** set in the bitvec. Whenever a leaf page is extracted from the free-list,
47509
 
** optimization 2 above is omitted if the corresponding bit is already
47510
 
** set in BtShared.pHasContent. The contents of the bitvec are cleared
47511
 
** at the end of every transaction.
47512
 
*/
47513
 
static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
47514
 
  int rc = SQLITE_OK;
47515
 
  if( !pBt->pHasContent ){
47516
 
    assert( pgno<=pBt->nPage );
47517
 
    pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
47518
 
    if( !pBt->pHasContent ){
47519
 
      rc = SQLITE_NOMEM;
47520
 
    }
47521
 
  }
47522
 
  if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
47523
 
    rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
47524
 
  }
47525
 
  return rc;
47526
 
}
47527
 
 
47528
 
/*
47529
 
** Query the BtShared.pHasContent vector.
47530
 
**
47531
 
** This function is called when a free-list leaf page is removed from the
47532
 
** free-list for reuse. It returns false if it is safe to retrieve the
47533
 
** page from the pager layer with the 'no-content' flag set. True otherwise.
47534
 
*/
47535
 
static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
47536
 
  Bitvec *p = pBt->pHasContent;
47537
 
  return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
47538
 
}
47539
 
 
47540
 
/*
47541
 
** Clear (destroy) the BtShared.pHasContent bitvec. This should be
47542
 
** invoked at the conclusion of each write-transaction.
47543
 
*/
47544
 
static void btreeClearHasContent(BtShared *pBt){
47545
 
  sqlite3BitvecDestroy(pBt->pHasContent);
47546
 
  pBt->pHasContent = 0;
47547
 
}
47548
 
 
47549
 
/*
47550
 
** Save the current cursor position in the variables BtCursor.nKey 
47551
 
** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
47552
 
**
47553
 
** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
47554
 
** prior to calling this routine.  
47555
 
*/
47556
 
static int saveCursorPosition(BtCursor *pCur){
47557
 
  int rc;
47558
 
 
47559
 
  assert( CURSOR_VALID==pCur->eState );
47560
 
  assert( 0==pCur->pKey );
47561
 
  assert( cursorHoldsMutex(pCur) );
47562
 
 
47563
 
  rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
47564
 
  assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
47565
 
 
47566
 
  /* If this is an intKey table, then the above call to BtreeKeySize()
47567
 
  ** stores the integer key in pCur->nKey. In this case this value is
47568
 
  ** all that is required. Otherwise, if pCur is not open on an intKey
47569
 
  ** table, then malloc space for and store the pCur->nKey bytes of key 
47570
 
  ** data.
47571
 
  */
47572
 
  if( 0==pCur->apPage[0]->intKey ){
47573
 
    void *pKey = sqlite3Malloc( (int)pCur->nKey );
47574
 
    if( pKey ){
47575
 
      rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
47576
 
      if( rc==SQLITE_OK ){
47577
 
        pCur->pKey = pKey;
47578
 
      }else{
47579
 
        sqlite3_free(pKey);
47580
 
      }
47581
 
    }else{
47582
 
      rc = SQLITE_NOMEM;
47583
 
    }
47584
 
  }
47585
 
  assert( !pCur->apPage[0]->intKey || !pCur->pKey );
47586
 
 
47587
 
  if( rc==SQLITE_OK ){
47588
 
    int i;
47589
 
    for(i=0; i<=pCur->iPage; i++){
47590
 
      releasePage(pCur->apPage[i]);
47591
 
      pCur->apPage[i] = 0;
47592
 
    }
47593
 
    pCur->iPage = -1;
47594
 
    pCur->eState = CURSOR_REQUIRESEEK;
47595
 
  }
47596
 
 
47597
 
  invalidateOverflowCache(pCur);
47598
 
  return rc;
47599
 
}
47600
 
 
47601
 
/*
47602
 
** Save the positions of all cursors (except pExcept) that are open on
47603
 
** the table  with root-page iRoot. Usually, this is called just before cursor
47604
 
** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
47605
 
*/
47606
 
static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
47607
 
  BtCursor *p;
47608
 
  assert( sqlite3_mutex_held(pBt->mutex) );
47609
 
  assert( pExcept==0 || pExcept->pBt==pBt );
47610
 
  for(p=pBt->pCursor; p; p=p->pNext){
47611
 
    if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && 
47612
 
        p->eState==CURSOR_VALID ){
47613
 
      int rc = saveCursorPosition(p);
47614
 
      if( SQLITE_OK!=rc ){
47615
 
        return rc;
47616
 
      }
47617
 
    }
47618
 
  }
47619
 
  return SQLITE_OK;
47620
 
}
47621
 
 
47622
 
/*
47623
 
** Clear the current cursor position.
47624
 
*/
47625
 
SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
47626
 
  assert( cursorHoldsMutex(pCur) );
47627
 
  sqlite3_free(pCur->pKey);
47628
 
  pCur->pKey = 0;
47629
 
  pCur->eState = CURSOR_INVALID;
47630
 
}
47631
 
 
47632
 
/*
47633
 
** In this version of BtreeMoveto, pKey is a packed index record
47634
 
** such as is generated by the OP_MakeRecord opcode.  Unpack the
47635
 
** record and then call BtreeMovetoUnpacked() to do the work.
47636
 
*/
47637
 
static int btreeMoveto(
47638
 
  BtCursor *pCur,     /* Cursor open on the btree to be searched */
47639
 
  const void *pKey,   /* Packed key if the btree is an index */
47640
 
  i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
47641
 
  int bias,           /* Bias search to the high end */
47642
 
  int *pRes           /* Write search results here */
47643
 
){
47644
 
  int rc;                    /* Status code */
47645
 
  UnpackedRecord *pIdxKey;   /* Unpacked index key */
47646
 
  char aSpace[150];          /* Temp space for pIdxKey - to avoid a malloc */
47647
 
 
47648
 
  if( pKey ){
47649
 
    assert( nKey==(i64)(int)nKey );
47650
 
    pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey,
47651
 
                                      aSpace, sizeof(aSpace));
47652
 
    if( pIdxKey==0 ) return SQLITE_NOMEM;
47653
 
  }else{
47654
 
    pIdxKey = 0;
47655
 
  }
47656
 
  rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
47657
 
  if( pKey ){
47658
 
    sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
47659
 
  }
47660
 
  return rc;
47661
 
}
47662
 
 
47663
 
/*
47664
 
** Restore the cursor to the position it was in (or as close to as possible)
47665
 
** when saveCursorPosition() was called. Note that this call deletes the 
47666
 
** saved position info stored by saveCursorPosition(), so there can be
47667
 
** at most one effective restoreCursorPosition() call after each 
47668
 
** saveCursorPosition().
47669
 
*/
47670
 
static int btreeRestoreCursorPosition(BtCursor *pCur){
47671
 
  int rc;
47672
 
  assert( cursorHoldsMutex(pCur) );
47673
 
  assert( pCur->eState>=CURSOR_REQUIRESEEK );
47674
 
  if( pCur->eState==CURSOR_FAULT ){
47675
 
    return pCur->skipNext;
47676
 
  }
47677
 
  pCur->eState = CURSOR_INVALID;
47678
 
  rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
47679
 
  if( rc==SQLITE_OK ){
47680
 
    sqlite3_free(pCur->pKey);
47681
 
    pCur->pKey = 0;
47682
 
    assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
47683
 
  }
47684
 
  return rc;
47685
 
}
47686
 
 
47687
 
#define restoreCursorPosition(p) \
47688
 
  (p->eState>=CURSOR_REQUIRESEEK ? \
47689
 
         btreeRestoreCursorPosition(p) : \
47690
 
         SQLITE_OK)
47691
 
 
47692
 
/*
47693
 
** Determine whether or not a cursor has moved from the position it
47694
 
** was last placed at.  Cursors can move when the row they are pointing
47695
 
** at is deleted out from under them.
47696
 
**
47697
 
** This routine returns an error code if something goes wrong.  The
47698
 
** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
47699
 
*/
47700
 
SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
47701
 
  int rc;
47702
 
 
47703
 
  rc = restoreCursorPosition(pCur);
47704
 
  if( rc ){
47705
 
    *pHasMoved = 1;
47706
 
    return rc;
47707
 
  }
47708
 
  if( pCur->eState!=CURSOR_VALID || pCur->skipNext!=0 ){
47709
 
    *pHasMoved = 1;
47710
 
  }else{
47711
 
    *pHasMoved = 0;
47712
 
  }
47713
 
  return SQLITE_OK;
47714
 
}
47715
 
 
47716
 
#ifndef SQLITE_OMIT_AUTOVACUUM
47717
 
/*
47718
 
** Given a page number of a regular database page, return the page
47719
 
** number for the pointer-map page that contains the entry for the
47720
 
** input page number.
47721
 
**
47722
 
** Return 0 (not a valid page) for pgno==1 since there is
47723
 
** no pointer map associated with page 1.  The integrity_check logic
47724
 
** requires that ptrmapPageno(*,1)!=1.
47725
 
*/
47726
 
static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
47727
 
  int nPagesPerMapPage;
47728
 
  Pgno iPtrMap, ret;
47729
 
  assert( sqlite3_mutex_held(pBt->mutex) );
47730
 
  if( pgno<2 ) return 0;
47731
 
  nPagesPerMapPage = (pBt->usableSize/5)+1;
47732
 
  iPtrMap = (pgno-2)/nPagesPerMapPage;
47733
 
  ret = (iPtrMap*nPagesPerMapPage) + 2; 
47734
 
  if( ret==PENDING_BYTE_PAGE(pBt) ){
47735
 
    ret++;
47736
 
  }
47737
 
  return ret;
47738
 
}
47739
 
 
47740
 
/*
47741
 
** Write an entry into the pointer map.
47742
 
**
47743
 
** This routine updates the pointer map entry for page number 'key'
47744
 
** so that it maps to type 'eType' and parent page number 'pgno'.
47745
 
**
47746
 
** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
47747
 
** a no-op.  If an error occurs, the appropriate error code is written
47748
 
** into *pRC.
47749
 
*/
47750
 
static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
47751
 
  DbPage *pDbPage;  /* The pointer map page */
47752
 
  u8 *pPtrmap;      /* The pointer map data */
47753
 
  Pgno iPtrmap;     /* The pointer map page number */
47754
 
  int offset;       /* Offset in pointer map page */
47755
 
  int rc;           /* Return code from subfunctions */
47756
 
 
47757
 
  if( *pRC ) return;
47758
 
 
47759
 
  assert( sqlite3_mutex_held(pBt->mutex) );
47760
 
  /* The master-journal page number must never be used as a pointer map page */
47761
 
  assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
47762
 
 
47763
 
  assert( pBt->autoVacuum );
47764
 
  if( key==0 ){
47765
 
    *pRC = SQLITE_CORRUPT_BKPT;
47766
 
    return;
47767
 
  }
47768
 
  iPtrmap = PTRMAP_PAGENO(pBt, key);
47769
 
  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
47770
 
  if( rc!=SQLITE_OK ){
47771
 
    *pRC = rc;
47772
 
    return;
47773
 
  }
47774
 
  offset = PTRMAP_PTROFFSET(iPtrmap, key);
47775
 
  if( offset<0 ){
47776
 
    *pRC = SQLITE_CORRUPT_BKPT;
47777
 
    goto ptrmap_exit;
47778
 
  }
47779
 
  pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
47780
 
 
47781
 
  if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
47782
 
    TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
47783
 
    *pRC= rc = sqlite3PagerWrite(pDbPage);
47784
 
    if( rc==SQLITE_OK ){
47785
 
      pPtrmap[offset] = eType;
47786
 
      put4byte(&pPtrmap[offset+1], parent);
47787
 
    }
47788
 
  }
47789
 
 
47790
 
ptrmap_exit:
47791
 
  sqlite3PagerUnref(pDbPage);
47792
 
}
47793
 
 
47794
 
/*
47795
 
** Read an entry from the pointer map.
47796
 
**
47797
 
** This routine retrieves the pointer map entry for page 'key', writing
47798
 
** the type and parent page number to *pEType and *pPgno respectively.
47799
 
** An error code is returned if something goes wrong, otherwise SQLITE_OK.
47800
 
*/
47801
 
static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
47802
 
  DbPage *pDbPage;   /* The pointer map page */
47803
 
  int iPtrmap;       /* Pointer map page index */
47804
 
  u8 *pPtrmap;       /* Pointer map page data */
47805
 
  int offset;        /* Offset of entry in pointer map */
47806
 
  int rc;
47807
 
 
47808
 
  assert( sqlite3_mutex_held(pBt->mutex) );
47809
 
 
47810
 
  iPtrmap = PTRMAP_PAGENO(pBt, key);
47811
 
  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
47812
 
  if( rc!=0 ){
47813
 
    return rc;
47814
 
  }
47815
 
  pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
47816
 
 
47817
 
  offset = PTRMAP_PTROFFSET(iPtrmap, key);
47818
 
  assert( pEType!=0 );
47819
 
  *pEType = pPtrmap[offset];
47820
 
  if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
47821
 
 
47822
 
  sqlite3PagerUnref(pDbPage);
47823
 
  if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
47824
 
  return SQLITE_OK;
47825
 
}
47826
 
 
47827
 
#else /* if defined SQLITE_OMIT_AUTOVACUUM */
47828
 
  #define ptrmapPut(w,x,y,z,rc)
47829
 
  #define ptrmapGet(w,x,y,z) SQLITE_OK
47830
 
  #define ptrmapPutOvflPtr(x, y, rc)
47831
 
#endif
47832
 
 
47833
 
/*
47834
 
** Given a btree page and a cell index (0 means the first cell on
47835
 
** the page, 1 means the second cell, and so forth) return a pointer
47836
 
** to the cell content.
47837
 
**
47838
 
** This routine works only for pages that do not contain overflow cells.
47839
 
*/
47840
 
#define findCell(P,I) \
47841
 
  ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)])))
47842
 
 
47843
 
/*
47844
 
** This a more complex version of findCell() that works for
47845
 
** pages that do contain overflow cells.
47846
 
*/
47847
 
static u8 *findOverflowCell(MemPage *pPage, int iCell){
47848
 
  int i;
47849
 
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
47850
 
  for(i=pPage->nOverflow-1; i>=0; i--){
47851
 
    int k;
47852
 
    struct _OvflCell *pOvfl;
47853
 
    pOvfl = &pPage->aOvfl[i];
47854
 
    k = pOvfl->idx;
47855
 
    if( k<=iCell ){
47856
 
      if( k==iCell ){
47857
 
        return pOvfl->pCell;
47858
 
      }
47859
 
      iCell--;
47860
 
    }
47861
 
  }
47862
 
  return findCell(pPage, iCell);
47863
 
}
47864
 
 
47865
 
/*
47866
 
** Parse a cell content block and fill in the CellInfo structure.  There
47867
 
** are two versions of this function.  btreeParseCell() takes a 
47868
 
** cell index as the second argument and btreeParseCellPtr() 
47869
 
** takes a pointer to the body of the cell as its second argument.
47870
 
**
47871
 
** Within this file, the parseCell() macro can be called instead of
47872
 
** btreeParseCellPtr(). Using some compilers, this will be faster.
47873
 
*/
47874
 
static void btreeParseCellPtr(
47875
 
  MemPage *pPage,         /* Page containing the cell */
47876
 
  u8 *pCell,              /* Pointer to the cell text. */
47877
 
  CellInfo *pInfo         /* Fill in this structure */
47878
 
){
47879
 
  u16 n;                  /* Number bytes in cell content header */
47880
 
  u32 nPayload;           /* Number of bytes of cell payload */
47881
 
 
47882
 
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
47883
 
 
47884
 
  pInfo->pCell = pCell;
47885
 
  assert( pPage->leaf==0 || pPage->leaf==1 );
47886
 
  n = pPage->childPtrSize;
47887
 
  assert( n==4-4*pPage->leaf );
47888
 
  if( pPage->intKey ){
47889
 
    if( pPage->hasData ){
47890
 
      n += getVarint32(&pCell[n], nPayload);
47891
 
    }else{
47892
 
      nPayload = 0;
47893
 
    }
47894
 
    n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
47895
 
    pInfo->nData = nPayload;
47896
 
  }else{
47897
 
    pInfo->nData = 0;
47898
 
    n += getVarint32(&pCell[n], nPayload);
47899
 
    pInfo->nKey = nPayload;
47900
 
  }
47901
 
  pInfo->nPayload = nPayload;
47902
 
  pInfo->nHeader = n;
47903
 
  testcase( nPayload==pPage->maxLocal );
47904
 
  testcase( nPayload==pPage->maxLocal+1 );
47905
 
  if( likely(nPayload<=pPage->maxLocal) ){
47906
 
    /* This is the (easy) common case where the entire payload fits
47907
 
    ** on the local page.  No overflow is required.
47908
 
    */
47909
 
    if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4;
47910
 
    pInfo->nLocal = (u16)nPayload;
47911
 
    pInfo->iOverflow = 0;
47912
 
  }else{
47913
 
    /* If the payload will not fit completely on the local page, we have
47914
 
    ** to decide how much to store locally and how much to spill onto
47915
 
    ** overflow pages.  The strategy is to minimize the amount of unused
47916
 
    ** space on overflow pages while keeping the amount of local storage
47917
 
    ** in between minLocal and maxLocal.
47918
 
    **
47919
 
    ** Warning:  changing the way overflow payload is distributed in any
47920
 
    ** way will result in an incompatible file format.
47921
 
    */
47922
 
    int minLocal;  /* Minimum amount of payload held locally */
47923
 
    int maxLocal;  /* Maximum amount of payload held locally */
47924
 
    int surplus;   /* Overflow payload available for local storage */
47925
 
 
47926
 
    minLocal = pPage->minLocal;
47927
 
    maxLocal = pPage->maxLocal;
47928
 
    surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
47929
 
    testcase( surplus==maxLocal );
47930
 
    testcase( surplus==maxLocal+1 );
47931
 
    if( surplus <= maxLocal ){
47932
 
      pInfo->nLocal = (u16)surplus;
47933
 
    }else{
47934
 
      pInfo->nLocal = (u16)minLocal;
47935
 
    }
47936
 
    pInfo->iOverflow = (u16)(pInfo->nLocal + n);
47937
 
    pInfo->nSize = pInfo->iOverflow + 4;
47938
 
  }
47939
 
}
47940
 
#define parseCell(pPage, iCell, pInfo) \
47941
 
  btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
47942
 
static void btreeParseCell(
47943
 
  MemPage *pPage,         /* Page containing the cell */
47944
 
  int iCell,              /* The cell index.  First cell is 0 */
47945
 
  CellInfo *pInfo         /* Fill in this structure */
47946
 
){
47947
 
  parseCell(pPage, iCell, pInfo);
47948
 
}
47949
 
 
47950
 
/*
47951
 
** Compute the total number of bytes that a Cell needs in the cell
47952
 
** data area of the btree-page.  The return number includes the cell
47953
 
** data header and the local payload, but not any overflow page or
47954
 
** the space used by the cell pointer.
47955
 
*/
47956
 
static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
47957
 
  u8 *pIter = &pCell[pPage->childPtrSize];
47958
 
  u32 nSize;
47959
 
 
47960
 
#ifdef SQLITE_DEBUG
47961
 
  /* The value returned by this function should always be the same as
47962
 
  ** the (CellInfo.nSize) value found by doing a full parse of the
47963
 
  ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
47964
 
  ** this function verifies that this invariant is not violated. */
47965
 
  CellInfo debuginfo;
47966
 
  btreeParseCellPtr(pPage, pCell, &debuginfo);
47967
 
#endif
47968
 
 
47969
 
  if( pPage->intKey ){
47970
 
    u8 *pEnd;
47971
 
    if( pPage->hasData ){
47972
 
      pIter += getVarint32(pIter, nSize);
47973
 
    }else{
47974
 
      nSize = 0;
47975
 
    }
47976
 
 
47977
 
    /* pIter now points at the 64-bit integer key value, a variable length 
47978
 
    ** integer. The following block moves pIter to point at the first byte
47979
 
    ** past the end of the key value. */
47980
 
    pEnd = &pIter[9];
47981
 
    while( (*pIter++)&0x80 && pIter<pEnd );
47982
 
  }else{
47983
 
    pIter += getVarint32(pIter, nSize);
47984
 
  }
47985
 
 
47986
 
  testcase( nSize==pPage->maxLocal );
47987
 
  testcase( nSize==pPage->maxLocal+1 );
47988
 
  if( nSize>pPage->maxLocal ){
47989
 
    int minLocal = pPage->minLocal;
47990
 
    nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
47991
 
    testcase( nSize==pPage->maxLocal );
47992
 
    testcase( nSize==pPage->maxLocal+1 );
47993
 
    if( nSize>pPage->maxLocal ){
47994
 
      nSize = minLocal;
47995
 
    }
47996
 
    nSize += 4;
47997
 
  }
47998
 
  nSize += (u32)(pIter - pCell);
47999
 
 
48000
 
  /* The minimum size of any cell is 4 bytes. */
48001
 
  if( nSize<4 ){
48002
 
    nSize = 4;
48003
 
  }
48004
 
 
48005
 
  assert( nSize==debuginfo.nSize );
48006
 
  return (u16)nSize;
48007
 
}
48008
 
 
48009
 
#ifdef SQLITE_DEBUG
48010
 
/* This variation on cellSizePtr() is used inside of assert() statements
48011
 
** only. */
48012
 
static u16 cellSize(MemPage *pPage, int iCell){
48013
 
  return cellSizePtr(pPage, findCell(pPage, iCell));
48014
 
}
48015
 
#endif
48016
 
 
48017
 
#ifndef SQLITE_OMIT_AUTOVACUUM
48018
 
/*
48019
 
** If the cell pCell, part of page pPage contains a pointer
48020
 
** to an overflow page, insert an entry into the pointer-map
48021
 
** for the overflow page.
48022
 
*/
48023
 
static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
48024
 
  CellInfo info;
48025
 
  if( *pRC ) return;
48026
 
  assert( pCell!=0 );
48027
 
  btreeParseCellPtr(pPage, pCell, &info);
48028
 
  assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
48029
 
  if( info.iOverflow ){
48030
 
    Pgno ovfl = get4byte(&pCell[info.iOverflow]);
48031
 
    ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
48032
 
  }
48033
 
}
48034
 
#endif
48035
 
 
48036
 
 
48037
 
/*
48038
 
** Defragment the page given.  All Cells are moved to the
48039
 
** end of the page and all free space is collected into one
48040
 
** big FreeBlk that occurs in between the header and cell
48041
 
** pointer array and the cell content area.
48042
 
*/
48043
 
static int defragmentPage(MemPage *pPage){
48044
 
  int i;                     /* Loop counter */
48045
 
  int pc;                    /* Address of a i-th cell */
48046
 
  int hdr;                   /* Offset to the page header */
48047
 
  int size;                  /* Size of a cell */
48048
 
  int usableSize;            /* Number of usable bytes on a page */
48049
 
  int cellOffset;            /* Offset to the cell pointer array */
48050
 
  int cbrk;                  /* Offset to the cell content area */
48051
 
  int nCell;                 /* Number of cells on the page */
48052
 
  unsigned char *data;       /* The page data */
48053
 
  unsigned char *temp;       /* Temp area for cell content */
48054
 
  int iCellFirst;            /* First allowable cell index */
48055
 
  int iCellLast;             /* Last possible cell index */
48056
 
 
48057
 
 
48058
 
  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
48059
 
  assert( pPage->pBt!=0 );
48060
 
  assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
48061
 
  assert( pPage->nOverflow==0 );
48062
 
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
48063
 
  temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
48064
 
  data = pPage->aData;
48065
 
  hdr = pPage->hdrOffset;
48066
 
  cellOffset = pPage->cellOffset;
48067
 
  nCell = pPage->nCell;
48068
 
  assert( nCell==get2byte(&data[hdr+3]) );
48069
 
  usableSize = pPage->pBt->usableSize;
48070
 
  cbrk = get2byte(&data[hdr+5]);
48071
 
  memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
48072
 
  cbrk = usableSize;
48073
 
  iCellFirst = cellOffset + 2*nCell;
48074
 
  iCellLast = usableSize - 4;
48075
 
  for(i=0; i<nCell; i++){
48076
 
    u8 *pAddr;     /* The i-th cell pointer */
48077
 
    pAddr = &data[cellOffset + i*2];
48078
 
    pc = get2byte(pAddr);
48079
 
    testcase( pc==iCellFirst );
48080
 
    testcase( pc==iCellLast );
48081
 
#if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
48082
 
    /* These conditions have already been verified in btreeInitPage()
48083
 
    ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined 
48084
 
    */
48085
 
    if( pc<iCellFirst || pc>iCellLast ){
48086
 
      return SQLITE_CORRUPT_BKPT;
48087
 
    }
48088
 
#endif
48089
 
    assert( pc>=iCellFirst && pc<=iCellLast );
48090
 
    size = cellSizePtr(pPage, &temp[pc]);
48091
 
    cbrk -= size;
48092
 
#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
48093
 
    if( cbrk<iCellFirst ){
48094
 
      return SQLITE_CORRUPT_BKPT;
48095
 
    }
48096
 
#else
48097
 
    if( cbrk<iCellFirst || pc+size>usableSize ){
48098
 
      return SQLITE_CORRUPT_BKPT;
48099
 
    }
48100
 
#endif
48101
 
    assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
48102
 
    testcase( cbrk+size==usableSize );
48103
 
    testcase( pc+size==usableSize );
48104
 
    memcpy(&data[cbrk], &temp[pc], size);
48105
 
    put2byte(pAddr, cbrk);
48106
 
  }
48107
 
  assert( cbrk>=iCellFirst );
48108
 
  put2byte(&data[hdr+5], cbrk);
48109
 
  data[hdr+1] = 0;
48110
 
  data[hdr+2] = 0;
48111
 
  data[hdr+7] = 0;
48112
 
  memset(&data[iCellFirst], 0, cbrk-iCellFirst);
48113
 
  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
48114
 
  if( cbrk-iCellFirst!=pPage->nFree ){
48115
 
    return SQLITE_CORRUPT_BKPT;
48116
 
  }
48117
 
  return SQLITE_OK;
48118
 
}
48119
 
 
48120
 
/*
48121
 
** Allocate nByte bytes of space from within the B-Tree page passed
48122
 
** as the first argument. Write into *pIdx the index into pPage->aData[]
48123
 
** of the first byte of allocated space. Return either SQLITE_OK or
48124
 
** an error code (usually SQLITE_CORRUPT).
48125
 
**
48126
 
** The caller guarantees that there is sufficient space to make the
48127
 
** allocation.  This routine might need to defragment in order to bring
48128
 
** all the space together, however.  This routine will avoid using
48129
 
** the first two bytes past the cell pointer area since presumably this
48130
 
** allocation is being made in order to insert a new cell, so we will
48131
 
** also end up needing a new cell pointer.
48132
 
*/
48133
 
static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
48134
 
  const int hdr = pPage->hdrOffset;    /* Local cache of pPage->hdrOffset */
48135
 
  u8 * const data = pPage->aData;      /* Local cache of pPage->aData */
48136
 
  int nFrag;                           /* Number of fragmented bytes on pPage */
48137
 
  int top;                             /* First byte of cell content area */
48138
 
  int gap;        /* First byte of gap between cell pointers and cell content */
48139
 
  int rc;         /* Integer return code */
48140
 
  int usableSize; /* Usable size of the page */
48141
 
  
48142
 
  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
48143
 
  assert( pPage->pBt );
48144
 
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
48145
 
  assert( nByte>=0 );  /* Minimum cell size is 4 */
48146
 
  assert( pPage->nFree>=nByte );
48147
 
  assert( pPage->nOverflow==0 );
48148
 
  usableSize = pPage->pBt->usableSize;
48149
 
  assert( nByte < usableSize-8 );
48150
 
 
48151
 
  nFrag = data[hdr+7];
48152
 
  assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
48153
 
  gap = pPage->cellOffset + 2*pPage->nCell;
48154
 
  top = get2byteNotZero(&data[hdr+5]);
48155
 
  if( gap>top ) return SQLITE_CORRUPT_BKPT;
48156
 
  testcase( gap+2==top );
48157
 
  testcase( gap+1==top );
48158
 
  testcase( gap==top );
48159
 
 
48160
 
  if( nFrag>=60 ){
48161
 
    /* Always defragment highly fragmented pages */
48162
 
    rc = defragmentPage(pPage);
48163
 
    if( rc ) return rc;
48164
 
    top = get2byteNotZero(&data[hdr+5]);
48165
 
  }else if( gap+2<=top ){
48166
 
    /* Search the freelist looking for a free slot big enough to satisfy 
48167
 
    ** the request. The allocation is made from the first free slot in 
48168
 
    ** the list that is large enough to accomadate it.
48169
 
    */
48170
 
    int pc, addr;
48171
 
    for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
48172
 
      int size;            /* Size of the free slot */
48173
 
      if( pc>usableSize-4 || pc<addr+4 ){
48174
 
        return SQLITE_CORRUPT_BKPT;
48175
 
      }
48176
 
      size = get2byte(&data[pc+2]);
48177
 
      if( size>=nByte ){
48178
 
        int x = size - nByte;
48179
 
        testcase( x==4 );
48180
 
        testcase( x==3 );
48181
 
        if( x<4 ){
48182
 
          /* Remove the slot from the free-list. Update the number of
48183
 
          ** fragmented bytes within the page. */
48184
 
          memcpy(&data[addr], &data[pc], 2);
48185
 
          data[hdr+7] = (u8)(nFrag + x);
48186
 
        }else if( size+pc > usableSize ){
48187
 
          return SQLITE_CORRUPT_BKPT;
48188
 
        }else{
48189
 
          /* The slot remains on the free-list. Reduce its size to account
48190
 
          ** for the portion used by the new allocation. */
48191
 
          put2byte(&data[pc+2], x);
48192
 
        }
48193
 
        *pIdx = pc + x;
48194
 
        return SQLITE_OK;
48195
 
      }
48196
 
    }
48197
 
  }
48198
 
 
48199
 
  /* Check to make sure there is enough space in the gap to satisfy
48200
 
  ** the allocation.  If not, defragment.
48201
 
  */
48202
 
  testcase( gap+2+nByte==top );
48203
 
  if( gap+2+nByte>top ){
48204
 
    rc = defragmentPage(pPage);
48205
 
    if( rc ) return rc;
48206
 
    top = get2byteNotZero(&data[hdr+5]);
48207
 
    assert( gap+nByte<=top );
48208
 
  }
48209
 
 
48210
 
 
48211
 
  /* Allocate memory from the gap in between the cell pointer array
48212
 
  ** and the cell content area.  The btreeInitPage() call has already
48213
 
  ** validated the freelist.  Given that the freelist is valid, there
48214
 
  ** is no way that the allocation can extend off the end of the page.
48215
 
  ** The assert() below verifies the previous sentence.
48216
 
  */
48217
 
  top -= nByte;
48218
 
  put2byte(&data[hdr+5], top);
48219
 
  assert( top+nByte <= (int)pPage->pBt->usableSize );
48220
 
  *pIdx = top;
48221
 
  return SQLITE_OK;
48222
 
}
48223
 
 
48224
 
/*
48225
 
** Return a section of the pPage->aData to the freelist.
48226
 
** The first byte of the new free block is pPage->aDisk[start]
48227
 
** and the size of the block is "size" bytes.
48228
 
**
48229
 
** Most of the effort here is involved in coalesing adjacent
48230
 
** free blocks into a single big free block.
48231
 
*/
48232
 
static int freeSpace(MemPage *pPage, int start, int size){
48233
 
  int addr, pbegin, hdr;
48234
 
  int iLast;                        /* Largest possible freeblock offset */
48235
 
  unsigned char *data = pPage->aData;
48236
 
 
48237
 
  assert( pPage->pBt!=0 );
48238
 
  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
48239
 
  assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
48240
 
  assert( (start + size) <= (int)pPage->pBt->usableSize );
48241
 
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
48242
 
  assert( size>=0 );   /* Minimum cell size is 4 */
48243
 
 
48244
 
  if( pPage->pBt->secureDelete ){
48245
 
    /* Overwrite deleted information with zeros when the secure_delete
48246
 
    ** option is enabled */
48247
 
    memset(&data[start], 0, size);
48248
 
  }
48249
 
 
48250
 
  /* Add the space back into the linked list of freeblocks.  Note that
48251
 
  ** even though the freeblock list was checked by btreeInitPage(),
48252
 
  ** btreeInitPage() did not detect overlapping cells or
48253
 
  ** freeblocks that overlapped cells.   Nor does it detect when the
48254
 
  ** cell content area exceeds the value in the page header.  If these
48255
 
  ** situations arise, then subsequent insert operations might corrupt
48256
 
  ** the freelist.  So we do need to check for corruption while scanning
48257
 
  ** the freelist.
48258
 
  */
48259
 
  hdr = pPage->hdrOffset;
48260
 
  addr = hdr + 1;
48261
 
  iLast = pPage->pBt->usableSize - 4;
48262
 
  assert( start<=iLast );
48263
 
  while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
48264
 
    if( pbegin<addr+4 ){
48265
 
      return SQLITE_CORRUPT_BKPT;
48266
 
    }
48267
 
    addr = pbegin;
48268
 
  }
48269
 
  if( pbegin>iLast ){
48270
 
    return SQLITE_CORRUPT_BKPT;
48271
 
  }
48272
 
  assert( pbegin>addr || pbegin==0 );
48273
 
  put2byte(&data[addr], start);
48274
 
  put2byte(&data[start], pbegin);
48275
 
  put2byte(&data[start+2], size);
48276
 
  pPage->nFree = pPage->nFree + (u16)size;
48277
 
 
48278
 
  /* Coalesce adjacent free blocks */
48279
 
  addr = hdr + 1;
48280
 
  while( (pbegin = get2byte(&data[addr]))>0 ){
48281
 
    int pnext, psize, x;
48282
 
    assert( pbegin>addr );
48283
 
    assert( pbegin <= (int)pPage->pBt->usableSize-4 );
48284
 
    pnext = get2byte(&data[pbegin]);
48285
 
    psize = get2byte(&data[pbegin+2]);
48286
 
    if( pbegin + psize + 3 >= pnext && pnext>0 ){
48287
 
      int frag = pnext - (pbegin+psize);
48288
 
      if( (frag<0) || (frag>(int)data[hdr+7]) ){
48289
 
        return SQLITE_CORRUPT_BKPT;
48290
 
      }
48291
 
      data[hdr+7] -= (u8)frag;
48292
 
      x = get2byte(&data[pnext]);
48293
 
      put2byte(&data[pbegin], x);
48294
 
      x = pnext + get2byte(&data[pnext+2]) - pbegin;
48295
 
      put2byte(&data[pbegin+2], x);
48296
 
    }else{
48297
 
      addr = pbegin;
48298
 
    }
48299
 
  }
48300
 
 
48301
 
  /* If the cell content area begins with a freeblock, remove it. */
48302
 
  if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
48303
 
    int top;
48304
 
    pbegin = get2byte(&data[hdr+1]);
48305
 
    memcpy(&data[hdr+1], &data[pbegin], 2);
48306
 
    top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]);
48307
 
    put2byte(&data[hdr+5], top);
48308
 
  }
48309
 
  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
48310
 
  return SQLITE_OK;
48311
 
}
48312
 
 
48313
 
/*
48314
 
** Decode the flags byte (the first byte of the header) for a page
48315
 
** and initialize fields of the MemPage structure accordingly.
48316
 
**
48317
 
** Only the following combinations are supported.  Anything different
48318
 
** indicates a corrupt database files:
48319
 
**
48320
 
**         PTF_ZERODATA
48321
 
**         PTF_ZERODATA | PTF_LEAF
48322
 
**         PTF_LEAFDATA | PTF_INTKEY
48323
 
**         PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
48324
 
*/
48325
 
static int decodeFlags(MemPage *pPage, int flagByte){
48326
 
  BtShared *pBt;     /* A copy of pPage->pBt */
48327
 
 
48328
 
  assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
48329
 
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
48330
 
  pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
48331
 
  flagByte &= ~PTF_LEAF;
48332
 
  pPage->childPtrSize = 4-4*pPage->leaf;
48333
 
  pBt = pPage->pBt;
48334
 
  if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
48335
 
    pPage->intKey = 1;
48336
 
    pPage->hasData = pPage->leaf;
48337
 
    pPage->maxLocal = pBt->maxLeaf;
48338
 
    pPage->minLocal = pBt->minLeaf;
48339
 
  }else if( flagByte==PTF_ZERODATA ){
48340
 
    pPage->intKey = 0;
48341
 
    pPage->hasData = 0;
48342
 
    pPage->maxLocal = pBt->maxLocal;
48343
 
    pPage->minLocal = pBt->minLocal;
48344
 
  }else{
48345
 
    return SQLITE_CORRUPT_BKPT;
48346
 
  }
48347
 
  return SQLITE_OK;
48348
 
}
48349
 
 
48350
 
/*
48351
 
** Initialize the auxiliary information for a disk block.
48352
 
**
48353
 
** Return SQLITE_OK on success.  If we see that the page does
48354
 
** not contain a well-formed database page, then return 
48355
 
** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
48356
 
** guarantee that the page is well-formed.  It only shows that
48357
 
** we failed to detect any corruption.
48358
 
*/
48359
 
static int btreeInitPage(MemPage *pPage){
48360
 
 
48361
 
  assert( pPage->pBt!=0 );
48362
 
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
48363
 
  assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
48364
 
  assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
48365
 
  assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
48366
 
 
48367
 
  if( !pPage->isInit ){
48368
 
    u16 pc;            /* Address of a freeblock within pPage->aData[] */
48369
 
    u8 hdr;            /* Offset to beginning of page header */
48370
 
    u8 *data;          /* Equal to pPage->aData */
48371
 
    BtShared *pBt;        /* The main btree structure */
48372
 
    int usableSize;    /* Amount of usable space on each page */
48373
 
    u16 cellOffset;    /* Offset from start of page to first cell pointer */
48374
 
    int nFree;         /* Number of unused bytes on the page */
48375
 
    int top;           /* First byte of the cell content area */
48376
 
    int iCellFirst;    /* First allowable cell or freeblock offset */
48377
 
    int iCellLast;     /* Last possible cell or freeblock offset */
48378
 
 
48379
 
    pBt = pPage->pBt;
48380
 
 
48381
 
    hdr = pPage->hdrOffset;
48382
 
    data = pPage->aData;
48383
 
    if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
48384
 
    assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
48385
 
    pPage->maskPage = (u16)(pBt->pageSize - 1);
48386
 
    pPage->nOverflow = 0;
48387
 
    usableSize = pBt->usableSize;
48388
 
    pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
48389
 
    top = get2byteNotZero(&data[hdr+5]);
48390
 
    pPage->nCell = get2byte(&data[hdr+3]);
48391
 
    if( pPage->nCell>MX_CELL(pBt) ){
48392
 
      /* To many cells for a single page.  The page must be corrupt */
48393
 
      return SQLITE_CORRUPT_BKPT;
48394
 
    }
48395
 
    testcase( pPage->nCell==MX_CELL(pBt) );
48396
 
 
48397
 
    /* A malformed database page might cause us to read past the end
48398
 
    ** of page when parsing a cell.  
48399
 
    **
48400
 
    ** The following block of code checks early to see if a cell extends
48401
 
    ** past the end of a page boundary and causes SQLITE_CORRUPT to be 
48402
 
    ** returned if it does.
48403
 
    */
48404
 
    iCellFirst = cellOffset + 2*pPage->nCell;
48405
 
    iCellLast = usableSize - 4;
48406
 
#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
48407
 
    {
48408
 
      int i;            /* Index into the cell pointer array */
48409
 
      int sz;           /* Size of a cell */
48410
 
 
48411
 
      if( !pPage->leaf ) iCellLast--;
48412
 
      for(i=0; i<pPage->nCell; i++){
48413
 
        pc = get2byte(&data[cellOffset+i*2]);
48414
 
        testcase( pc==iCellFirst );
48415
 
        testcase( pc==iCellLast );
48416
 
        if( pc<iCellFirst || pc>iCellLast ){
48417
 
          return SQLITE_CORRUPT_BKPT;
48418
 
        }
48419
 
        sz = cellSizePtr(pPage, &data[pc]);
48420
 
        testcase( pc+sz==usableSize );
48421
 
        if( pc+sz>usableSize ){
48422
 
          return SQLITE_CORRUPT_BKPT;
48423
 
        }
48424
 
      }
48425
 
      if( !pPage->leaf ) iCellLast++;
48426
 
    }  
48427
 
#endif
48428
 
 
48429
 
    /* Compute the total free space on the page */
48430
 
    pc = get2byte(&data[hdr+1]);
48431
 
    nFree = data[hdr+7] + top;
48432
 
    while( pc>0 ){
48433
 
      u16 next, size;
48434
 
      if( pc<iCellFirst || pc>iCellLast ){
48435
 
        /* Start of free block is off the page */
48436
 
        return SQLITE_CORRUPT_BKPT; 
48437
 
      }
48438
 
      next = get2byte(&data[pc]);
48439
 
      size = get2byte(&data[pc+2]);
48440
 
      if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
48441
 
        /* Free blocks must be in ascending order. And the last byte of
48442
 
        ** the free-block must lie on the database page.  */
48443
 
        return SQLITE_CORRUPT_BKPT; 
48444
 
      }
48445
 
      nFree = nFree + size;
48446
 
      pc = next;
48447
 
    }
48448
 
 
48449
 
    /* At this point, nFree contains the sum of the offset to the start
48450
 
    ** of the cell-content area plus the number of free bytes within
48451
 
    ** the cell-content area. If this is greater than the usable-size
48452
 
    ** of the page, then the page must be corrupted. This check also
48453
 
    ** serves to verify that the offset to the start of the cell-content
48454
 
    ** area, according to the page header, lies within the page.
48455
 
    */
48456
 
    if( nFree>usableSize ){
48457
 
      return SQLITE_CORRUPT_BKPT; 
48458
 
    }
48459
 
    pPage->nFree = (u16)(nFree - iCellFirst);
48460
 
    pPage->isInit = 1;
48461
 
  }
48462
 
  return SQLITE_OK;
48463
 
}
48464
 
 
48465
 
/*
48466
 
** Set up a raw page so that it looks like a database page holding
48467
 
** no entries.
48468
 
*/
48469
 
static void zeroPage(MemPage *pPage, int flags){
48470
 
  unsigned char *data = pPage->aData;
48471
 
  BtShared *pBt = pPage->pBt;
48472
 
  u8 hdr = pPage->hdrOffset;
48473
 
  u16 first;
48474
 
 
48475
 
  assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
48476
 
  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
48477
 
  assert( sqlite3PagerGetData(pPage->pDbPage) == data );
48478
 
  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
48479
 
  assert( sqlite3_mutex_held(pBt->mutex) );
48480
 
  if( pBt->secureDelete ){
48481
 
    memset(&data[hdr], 0, pBt->usableSize - hdr);
48482
 
  }
48483
 
  data[hdr] = (char)flags;
48484
 
  first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
48485
 
  memset(&data[hdr+1], 0, 4);
48486
 
  data[hdr+7] = 0;
48487
 
  put2byte(&data[hdr+5], pBt->usableSize);
48488
 
  pPage->nFree = (u16)(pBt->usableSize - first);
48489
 
  decodeFlags(pPage, flags);
48490
 
  pPage->hdrOffset = hdr;
48491
 
  pPage->cellOffset = first;
48492
 
  pPage->nOverflow = 0;
48493
 
  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
48494
 
  pPage->maskPage = (u16)(pBt->pageSize - 1);
48495
 
  pPage->nCell = 0;
48496
 
  pPage->isInit = 1;
48497
 
}
48498
 
 
48499
 
 
48500
 
/*
48501
 
** Convert a DbPage obtained from the pager into a MemPage used by
48502
 
** the btree layer.
48503
 
*/
48504
 
static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
48505
 
  MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
48506
 
  pPage->aData = sqlite3PagerGetData(pDbPage);
48507
 
  pPage->pDbPage = pDbPage;
48508
 
  pPage->pBt = pBt;
48509
 
  pPage->pgno = pgno;
48510
 
  pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
48511
 
  return pPage; 
48512
 
}
48513
 
 
48514
 
/*
48515
 
** Get a page from the pager.  Initialize the MemPage.pBt and
48516
 
** MemPage.aData elements if needed.
48517
 
**
48518
 
** If the noContent flag is set, it means that we do not care about
48519
 
** the content of the page at this time.  So do not go to the disk
48520
 
** to fetch the content.  Just fill in the content with zeros for now.
48521
 
** If in the future we call sqlite3PagerWrite() on this page, that
48522
 
** means we have started to be concerned about content and the disk
48523
 
** read should occur at that point.
48524
 
*/
48525
 
static int btreeGetPage(
48526
 
  BtShared *pBt,       /* The btree */
48527
 
  Pgno pgno,           /* Number of the page to fetch */
48528
 
  MemPage **ppPage,    /* Return the page in this parameter */
48529
 
  int noContent        /* Do not load page content if true */
48530
 
){
48531
 
  int rc;
48532
 
  DbPage *pDbPage;
48533
 
 
48534
 
  assert( sqlite3_mutex_held(pBt->mutex) );
48535
 
  rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
48536
 
  if( rc ) return rc;
48537
 
  *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
48538
 
  return SQLITE_OK;
48539
 
}
48540
 
 
48541
 
/*
48542
 
** Retrieve a page from the pager cache. If the requested page is not
48543
 
** already in the pager cache return NULL. Initialize the MemPage.pBt and
48544
 
** MemPage.aData elements if needed.
48545
 
*/
48546
 
static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
48547
 
  DbPage *pDbPage;
48548
 
  assert( sqlite3_mutex_held(pBt->mutex) );
48549
 
  pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
48550
 
  if( pDbPage ){
48551
 
    return btreePageFromDbPage(pDbPage, pgno, pBt);
48552
 
  }
48553
 
  return 0;
48554
 
}
48555
 
 
48556
 
/*
48557
 
** Return the size of the database file in pages. If there is any kind of
48558
 
** error, return ((unsigned int)-1).
48559
 
*/
48560
 
static Pgno btreePagecount(BtShared *pBt){
48561
 
  return pBt->nPage;
48562
 
}
48563
 
SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
48564
 
  assert( sqlite3BtreeHoldsMutex(p) );
48565
 
  assert( ((p->pBt->nPage)&0x8000000)==0 );
48566
 
  return (int)btreePagecount(p->pBt);
48567
 
}
48568
 
 
48569
 
/*
48570
 
** Get a page from the pager and initialize it.  This routine is just a
48571
 
** convenience wrapper around separate calls to btreeGetPage() and 
48572
 
** btreeInitPage().
48573
 
**
48574
 
** If an error occurs, then the value *ppPage is set to is undefined. It
48575
 
** may remain unchanged, or it may be set to an invalid value.
48576
 
*/
48577
 
static int getAndInitPage(
48578
 
  BtShared *pBt,          /* The database file */
48579
 
  Pgno pgno,           /* Number of the page to get */
48580
 
  MemPage **ppPage     /* Write the page pointer here */
48581
 
){
48582
 
  int rc;
48583
 
  assert( sqlite3_mutex_held(pBt->mutex) );
48584
 
 
48585
 
  if( pgno>btreePagecount(pBt) ){
48586
 
    rc = SQLITE_CORRUPT_BKPT;
48587
 
  }else{
48588
 
    rc = btreeGetPage(pBt, pgno, ppPage, 0);
48589
 
    if( rc==SQLITE_OK ){
48590
 
      rc = btreeInitPage(*ppPage);
48591
 
      if( rc!=SQLITE_OK ){
48592
 
        releasePage(*ppPage);
48593
 
      }
48594
 
    }
48595
 
  }
48596
 
 
48597
 
  testcase( pgno==0 );
48598
 
  assert( pgno!=0 || rc==SQLITE_CORRUPT );
48599
 
  return rc;
48600
 
}
48601
 
 
48602
 
/*
48603
 
** Release a MemPage.  This should be called once for each prior
48604
 
** call to btreeGetPage.
48605
 
*/
48606
 
static void releasePage(MemPage *pPage){
48607
 
  if( pPage ){
48608
 
    assert( pPage->aData );
48609
 
    assert( pPage->pBt );
48610
 
    assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
48611
 
    assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
48612
 
    assert( sqlite3_mutex_held(pPage->pBt->mutex) );
48613
 
    sqlite3PagerUnref(pPage->pDbPage);
48614
 
  }
48615
 
}
48616
 
 
48617
 
/*
48618
 
** During a rollback, when the pager reloads information into the cache
48619
 
** so that the cache is restored to its original state at the start of
48620
 
** the transaction, for each page restored this routine is called.
48621
 
**
48622
 
** This routine needs to reset the extra data section at the end of the
48623
 
** page to agree with the restored data.
48624
 
*/
48625
 
static void pageReinit(DbPage *pData){
48626
 
  MemPage *pPage;
48627
 
  pPage = (MemPage *)sqlite3PagerGetExtra(pData);
48628
 
  assert( sqlite3PagerPageRefcount(pData)>0 );
48629
 
  if( pPage->isInit ){
48630
 
    assert( sqlite3_mutex_held(pPage->pBt->mutex) );
48631
 
    pPage->isInit = 0;
48632
 
    if( sqlite3PagerPageRefcount(pData)>1 ){
48633
 
      /* pPage might not be a btree page;  it might be an overflow page
48634
 
      ** or ptrmap page or a free page.  In those cases, the following
48635
 
      ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
48636
 
      ** But no harm is done by this.  And it is very important that
48637
 
      ** btreeInitPage() be called on every btree page so we make
48638
 
      ** the call for every page that comes in for re-initing. */
48639
 
      btreeInitPage(pPage);
48640
 
    }
48641
 
  }
48642
 
}
48643
 
 
48644
 
/*
48645
 
** Invoke the busy handler for a btree.
48646
 
*/
48647
 
static int btreeInvokeBusyHandler(void *pArg){
48648
 
  BtShared *pBt = (BtShared*)pArg;
48649
 
  assert( pBt->db );
48650
 
  assert( sqlite3_mutex_held(pBt->db->mutex) );
48651
 
  return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
48652
 
}
48653
 
 
48654
 
/*
48655
 
** Open a database file.
48656
 
** 
48657
 
** zFilename is the name of the database file.  If zFilename is NULL
48658
 
** then an ephemeral database is created.  The ephemeral database might
48659
 
** be exclusively in memory, or it might use a disk-based memory cache.
48660
 
** Either way, the ephemeral database will be automatically deleted 
48661
 
** when sqlite3BtreeClose() is called.
48662
 
**
48663
 
** If zFilename is ":memory:" then an in-memory database is created
48664
 
** that is automatically destroyed when it is closed.
48665
 
**
48666
 
** The "flags" parameter is a bitmask that might contain bits
48667
 
** BTREE_OMIT_JOURNAL and/or BTREE_NO_READLOCK.  The BTREE_NO_READLOCK
48668
 
** bit is also set if the SQLITE_NoReadlock flags is set in db->flags.
48669
 
** These flags are passed through into sqlite3PagerOpen() and must
48670
 
** be the same values as PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK.
48671
 
**
48672
 
** If the database is already opened in the same database connection
48673
 
** and we are in shared cache mode, then the open will fail with an
48674
 
** SQLITE_CONSTRAINT error.  We cannot allow two or more BtShared
48675
 
** objects in the same database connection since doing so will lead
48676
 
** to problems with locking.
48677
 
*/
48678
 
SQLITE_PRIVATE int sqlite3BtreeOpen(
48679
 
  const char *zFilename,  /* Name of the file containing the BTree database */
48680
 
  sqlite3 *db,            /* Associated database handle */
48681
 
  Btree **ppBtree,        /* Pointer to new Btree object written here */
48682
 
  int flags,              /* Options */
48683
 
  int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
48684
 
){
48685
 
  sqlite3_vfs *pVfs;             /* The VFS to use for this btree */
48686
 
  BtShared *pBt = 0;             /* Shared part of btree structure */
48687
 
  Btree *p;                      /* Handle to return */
48688
 
  sqlite3_mutex *mutexOpen = 0;  /* Prevents a race condition. Ticket #3537 */
48689
 
  int rc = SQLITE_OK;            /* Result code from this function */
48690
 
  u8 nReserve;                   /* Byte of unused space on each page */
48691
 
  unsigned char zDbHeader[100];  /* Database header content */
48692
 
 
48693
 
  /* True if opening an ephemeral, temporary database */
48694
 
  const int isTempDb = zFilename==0 || zFilename[0]==0;
48695
 
 
48696
 
  /* Set the variable isMemdb to true for an in-memory database, or 
48697
 
  ** false for a file-based database.
48698
 
  */
48699
 
#ifdef SQLITE_OMIT_MEMORYDB
48700
 
  const int isMemdb = 0;
48701
 
#else
48702
 
  const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
48703
 
                       || (isTempDb && sqlite3TempInMemory(db));
48704
 
#endif
48705
 
 
48706
 
  assert( db!=0 );
48707
 
  assert( sqlite3_mutex_held(db->mutex) );
48708
 
  assert( (flags&0xff)==flags );   /* flags fit in 8 bits */
48709
 
 
48710
 
  /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
48711
 
  assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
48712
 
 
48713
 
  /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
48714
 
  assert( (flags & BTREE_SINGLE)==0 || isTempDb );
48715
 
 
48716
 
  if( db->flags & SQLITE_NoReadlock ){
48717
 
    flags |= BTREE_NO_READLOCK;
48718
 
  }
48719
 
  if( isMemdb ){
48720
 
    flags |= BTREE_MEMORY;
48721
 
  }
48722
 
  if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
48723
 
    vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
48724
 
  }
48725
 
  pVfs = db->pVfs;
48726
 
  p = sqlite3MallocZero(sizeof(Btree));
48727
 
  if( !p ){
48728
 
    return SQLITE_NOMEM;
48729
 
  }
48730
 
  p->inTrans = TRANS_NONE;
48731
 
  p->db = db;
48732
 
#ifndef SQLITE_OMIT_SHARED_CACHE
48733
 
  p->lock.pBtree = p;
48734
 
  p->lock.iTable = 1;
48735
 
#endif
48736
 
 
48737
 
#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
48738
 
  /*
48739
 
  ** If this Btree is a candidate for shared cache, try to find an
48740
 
  ** existing BtShared object that we can share with
48741
 
  */
48742
 
  if( isMemdb==0 && isTempDb==0 ){
48743
 
    if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
48744
 
      int nFullPathname = pVfs->mxPathname+1;
48745
 
      char *zFullPathname = sqlite3Malloc(nFullPathname);
48746
 
      sqlite3_mutex *mutexShared;
48747
 
      p->sharable = 1;
48748
 
      if( !zFullPathname ){
48749
 
        sqlite3_free(p);
48750
 
        return SQLITE_NOMEM;
48751
 
      }
48752
 
      sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
48753
 
      mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
48754
 
      sqlite3_mutex_enter(mutexOpen);
48755
 
      mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
48756
 
      sqlite3_mutex_enter(mutexShared);
48757
 
      for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
48758
 
        assert( pBt->nRef>0 );
48759
 
        if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
48760
 
                 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
48761
 
          int iDb;
48762
 
          for(iDb=db->nDb-1; iDb>=0; iDb--){
48763
 
            Btree *pExisting = db->aDb[iDb].pBt;
48764
 
            if( pExisting && pExisting->pBt==pBt ){
48765
 
              sqlite3_mutex_leave(mutexShared);
48766
 
              sqlite3_mutex_leave(mutexOpen);
48767
 
              sqlite3_free(zFullPathname);
48768
 
              sqlite3_free(p);
48769
 
              return SQLITE_CONSTRAINT;
48770
 
            }
48771
 
          }
48772
 
          p->pBt = pBt;
48773
 
          pBt->nRef++;
48774
 
          break;
48775
 
        }
48776
 
      }
48777
 
      sqlite3_mutex_leave(mutexShared);
48778
 
      sqlite3_free(zFullPathname);
48779
 
    }
48780
 
#ifdef SQLITE_DEBUG
48781
 
    else{
48782
 
      /* In debug mode, we mark all persistent databases as sharable
48783
 
      ** even when they are not.  This exercises the locking code and
48784
 
      ** gives more opportunity for asserts(sqlite3_mutex_held())
48785
 
      ** statements to find locking problems.
48786
 
      */
48787
 
      p->sharable = 1;
48788
 
    }
48789
 
#endif
48790
 
  }
48791
 
#endif
48792
 
  if( pBt==0 ){
48793
 
    /*
48794
 
    ** The following asserts make sure that structures used by the btree are
48795
 
    ** the right size.  This is to guard against size changes that result
48796
 
    ** when compiling on a different architecture.
48797
 
    */
48798
 
    assert( sizeof(i64)==8 || sizeof(i64)==4 );
48799
 
    assert( sizeof(u64)==8 || sizeof(u64)==4 );
48800
 
    assert( sizeof(u32)==4 );
48801
 
    assert( sizeof(u16)==2 );
48802
 
    assert( sizeof(Pgno)==4 );
48803
 
  
48804
 
    pBt = sqlite3MallocZero( sizeof(*pBt) );
48805
 
    if( pBt==0 ){
48806
 
      rc = SQLITE_NOMEM;
48807
 
      goto btree_open_out;
48808
 
    }
48809
 
    rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
48810
 
                          EXTRA_SIZE, flags, vfsFlags, pageReinit);
48811
 
    if( rc==SQLITE_OK ){
48812
 
      rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
48813
 
    }
48814
 
    if( rc!=SQLITE_OK ){
48815
 
      goto btree_open_out;
48816
 
    }
48817
 
    pBt->openFlags = (u8)flags;
48818
 
    pBt->db = db;
48819
 
    sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
48820
 
    p->pBt = pBt;
48821
 
  
48822
 
    pBt->pCursor = 0;
48823
 
    pBt->pPage1 = 0;
48824
 
    pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
48825
 
#ifdef SQLITE_SECURE_DELETE
48826
 
    pBt->secureDelete = 1;
48827
 
#endif
48828
 
    pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
48829
 
    if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
48830
 
         || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
48831
 
      pBt->pageSize = 0;
48832
 
#ifndef SQLITE_OMIT_AUTOVACUUM
48833
 
      /* If the magic name ":memory:" will create an in-memory database, then
48834
 
      ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
48835
 
      ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
48836
 
      ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
48837
 
      ** regular file-name. In this case the auto-vacuum applies as per normal.
48838
 
      */
48839
 
      if( zFilename && !isMemdb ){
48840
 
        pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
48841
 
        pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
48842
 
      }
48843
 
#endif
48844
 
      nReserve = 0;
48845
 
    }else{
48846
 
      nReserve = zDbHeader[20];
48847
 
      pBt->pageSizeFixed = 1;
48848
 
#ifndef SQLITE_OMIT_AUTOVACUUM
48849
 
      pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
48850
 
      pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
48851
 
#endif
48852
 
    }
48853
 
    rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
48854
 
    if( rc ) goto btree_open_out;
48855
 
    pBt->usableSize = pBt->pageSize - nReserve;
48856
 
    assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
48857
 
   
48858
 
#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
48859
 
    /* Add the new BtShared object to the linked list sharable BtShareds.
48860
 
    */
48861
 
    if( p->sharable ){
48862
 
      sqlite3_mutex *mutexShared;
48863
 
      pBt->nRef = 1;
48864
 
      mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
48865
 
           #if (SQLITE_THREADSAFE)
48866
 
                if (sqlite3GlobalConfig.bCoreMutex ){
48867
 
        pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
48868
 
        if( pBt->mutex==0 ){
48869
 
          rc = SQLITE_NOMEM;
48870
 
          db->mallocFailed = 0;
48871
 
          goto btree_open_out;
48872
 
        }
48873
 
      }
48874
 
                #endif
48875
 
      sqlite3_mutex_enter(mutexShared);
48876
 
      pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
48877
 
      GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
48878
 
      sqlite3_mutex_leave(mutexShared);
48879
 
    }
48880
 
#endif
48881
 
  }
48882
 
 
48883
 
#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
48884
 
  /* If the new Btree uses a sharable pBtShared, then link the new
48885
 
  ** Btree into the list of all sharable Btrees for the same connection.
48886
 
  ** The list is kept in ascending order by pBt address.
48887
 
  */
48888
 
  if( p->sharable ){
48889
 
    int i;
48890
 
    Btree *pSib;
48891
 
    for(i=0; i<db->nDb; i++){
48892
 
      if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
48893
 
        while( pSib->pPrev ){ pSib = pSib->pPrev; }
48894
 
        if( p->pBt<pSib->pBt ){
48895
 
          p->pNext = pSib;
48896
 
          p->pPrev = 0;
48897
 
          pSib->pPrev = p;
48898
 
        }else{
48899
 
          while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
48900
 
            pSib = pSib->pNext;
48901
 
          }
48902
 
          p->pNext = pSib->pNext;
48903
 
          p->pPrev = pSib;
48904
 
          if( p->pNext ){
48905
 
            p->pNext->pPrev = p;
48906
 
          }
48907
 
          pSib->pNext = p;
48908
 
        }
48909
 
        break;
48910
 
      }
48911
 
    }
48912
 
  }
48913
 
#endif
48914
 
  *ppBtree = p;
48915
 
 
48916
 
btree_open_out:
48917
 
  if( rc!=SQLITE_OK ){
48918
 
    if( pBt && pBt->pPager ){
48919
 
      sqlite3PagerClose(pBt->pPager);
48920
 
    }
48921
 
    sqlite3_free(pBt);
48922
 
    sqlite3_free(p);
48923
 
    *ppBtree = 0;
48924
 
  }else{
48925
 
    /* If the B-Tree was successfully opened, set the pager-cache size to the
48926
 
    ** default value. Except, when opening on an existing shared pager-cache,
48927
 
    ** do not change the pager-cache size.
48928
 
    */
48929
 
    if( sqlite3BtreeSchema(p, 0, 0)==0 ){
48930
 
      sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
48931
 
    }
48932
 
  }
48933
 
  if( mutexOpen ){
48934
 
    assert( sqlite3_mutex_held(mutexOpen) );
48935
 
    sqlite3_mutex_leave(mutexOpen);
48936
 
  }
48937
 
  return rc;
48938
 
}
48939
 
 
48940
 
/*
48941
 
** Decrement the BtShared.nRef counter.  When it reaches zero,
48942
 
** remove the BtShared structure from the sharing list.  Return
48943
 
** true if the BtShared.nRef counter reaches zero and return
48944
 
** false if it is still positive.
48945
 
*/
48946
 
static int removeFromSharingList(BtShared *pBt){
48947
 
#ifndef SQLITE_OMIT_SHARED_CACHE
48948
 
  sqlite3_mutex *pMaster;
48949
 
  BtShared *pList;
48950
 
  int removed = 0;
48951
 
 
48952
 
  assert( sqlite3_mutex_notheld(pBt->mutex) );
48953
 
  pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
48954
 
  sqlite3_mutex_enter(pMaster);
48955
 
  pBt->nRef--;
48956
 
  if( pBt->nRef<=0 ){
48957
 
    if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
48958
 
      GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
48959
 
    }else{
48960
 
      pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
48961
 
      while( ALWAYS(pList) && pList->pNext!=pBt ){
48962
 
        pList=pList->pNext;
48963
 
      }
48964
 
      if( ALWAYS(pList) ){
48965
 
        pList->pNext = pBt->pNext;
48966
 
      }
48967
 
    }
48968
 
         #if( SQLITE_THREADSAFE )
48969
 
      sqlite3_mutex_free(pBt->mutex);
48970
 
         #endif
48971
 
    removed = 1;
48972
 
  }
48973
 
  sqlite3_mutex_leave(pMaster);
48974
 
  return removed;
48975
 
#else
48976
 
  return 1;
48977
 
#endif
48978
 
}
48979
 
 
48980
 
/*
48981
 
** Make sure pBt->pTmpSpace points to an allocation of 
48982
 
** MX_CELL_SIZE(pBt) bytes.
48983
 
*/
48984
 
static void allocateTempSpace(BtShared *pBt){
48985
 
  if( !pBt->pTmpSpace ){
48986
 
    pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
48987
 
  }
48988
 
}
48989
 
 
48990
 
/*
48991
 
** Free the pBt->pTmpSpace allocation
48992
 
*/
48993
 
static void freeTempSpace(BtShared *pBt){
48994
 
  sqlite3PageFree( pBt->pTmpSpace);
48995
 
  pBt->pTmpSpace = 0;
48996
 
}
48997
 
 
48998
 
/*
48999
 
** Close an open database and invalidate all cursors.
49000
 
*/
49001
 
SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
49002
 
  BtShared *pBt = p->pBt;
49003
 
  BtCursor *pCur;
49004
 
 
49005
 
  /* Close all cursors opened via this handle.  */
49006
 
  assert( sqlite3_mutex_held(p->db->mutex) );
49007
 
  sqlite3BtreeEnter(p);
49008
 
  pCur = pBt->pCursor;
49009
 
  while( pCur ){
49010
 
    BtCursor *pTmp = pCur;
49011
 
    pCur = pCur->pNext;
49012
 
    if( pTmp->pBtree==p ){
49013
 
      sqlite3BtreeCloseCursor(pTmp);
49014
 
    }
49015
 
  }
49016
 
 
49017
 
  /* Rollback any active transaction and free the handle structure.
49018
 
  ** The call to sqlite3BtreeRollback() drops any table-locks held by
49019
 
  ** this handle.
49020
 
  */
49021
 
  sqlite3BtreeRollback(p);
49022
 
  sqlite3BtreeLeave(p);
49023
 
 
49024
 
  /* If there are still other outstanding references to the shared-btree
49025
 
  ** structure, return now. The remainder of this procedure cleans 
49026
 
  ** up the shared-btree.
49027
 
  */
49028
 
  assert( p->wantToLock==0 && p->locked==0 );
49029
 
  if( !p->sharable || removeFromSharingList(pBt) ){
49030
 
    /* The pBt is no longer on the sharing list, so we can access
49031
 
    ** it without having to hold the mutex.
49032
 
    **
49033
 
    ** Clean out and delete the BtShared object.
49034
 
    */
49035
 
    assert( !pBt->pCursor );
49036
 
    sqlite3PagerClose(pBt->pPager);
49037
 
    if( pBt->xFreeSchema && pBt->pSchema ){
49038
 
      pBt->xFreeSchema(pBt->pSchema);
49039
 
    }
49040
 
    sqlite3DbFree(0, pBt->pSchema);
49041
 
    freeTempSpace(pBt);
49042
 
    sqlite3_free(pBt);
49043
 
  }
49044
 
 
49045
 
#ifndef SQLITE_OMIT_SHARED_CACHE
49046
 
  assert( p->wantToLock==0 );
49047
 
  assert( p->locked==0 );
49048
 
  if( p->pPrev ) p->pPrev->pNext = p->pNext;
49049
 
  if( p->pNext ) p->pNext->pPrev = p->pPrev;
49050
 
#endif
49051
 
 
49052
 
  sqlite3_free(p);
49053
 
  return SQLITE_OK;
49054
 
}
49055
 
 
49056
 
/*
49057
 
** Change the limit on the number of pages allowed in the cache.
49058
 
**
49059
 
** The maximum number of cache pages is set to the absolute
49060
 
** value of mxPage.  If mxPage is negative, the pager will
49061
 
** operate asynchronously - it will not stop to do fsync()s
49062
 
** to insure data is written to the disk surface before
49063
 
** continuing.  Transactions still work if synchronous is off,
49064
 
** and the database cannot be corrupted if this program
49065
 
** crashes.  But if the operating system crashes or there is
49066
 
** an abrupt power failure when synchronous is off, the database
49067
 
** could be left in an inconsistent and unrecoverable state.
49068
 
** Synchronous is on by default so database corruption is not
49069
 
** normally a worry.
49070
 
*/
49071
 
SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
49072
 
  BtShared *pBt = p->pBt;
49073
 
  assert( sqlite3_mutex_held(p->db->mutex) );
49074
 
  sqlite3BtreeEnter(p);
49075
 
  sqlite3PagerSetCachesize(pBt->pPager, mxPage);
49076
 
  sqlite3BtreeLeave(p);
49077
 
  return SQLITE_OK;
49078
 
}
49079
 
 
49080
 
/*
49081
 
** Change the way data is synced to disk in order to increase or decrease
49082
 
** how well the database resists damage due to OS crashes and power
49083
 
** failures.  Level 1 is the same as asynchronous (no syncs() occur and
49084
 
** there is a high probability of damage)  Level 2 is the default.  There
49085
 
** is a very low but non-zero probability of damage.  Level 3 reduces the
49086
 
** probability of damage to near zero but with a write performance reduction.
49087
 
*/
49088
 
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
49089
 
SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(
49090
 
  Btree *p,              /* The btree to set the safety level on */
49091
 
  int level,             /* PRAGMA synchronous.  1=OFF, 2=NORMAL, 3=FULL */
49092
 
  int fullSync,          /* PRAGMA fullfsync. */
49093
 
  int ckptFullSync       /* PRAGMA checkpoint_fullfync */
49094
 
){
49095
 
  BtShared *pBt = p->pBt;
49096
 
  assert( sqlite3_mutex_held(p->db->mutex) );
49097
 
  assert( level>=1 && level<=3 );
49098
 
  sqlite3BtreeEnter(p);
49099
 
  sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync, ckptFullSync);
49100
 
  sqlite3BtreeLeave(p);
49101
 
  return SQLITE_OK;
49102
 
}
49103
 
#endif
49104
 
 
49105
 
/*
49106
 
** Return TRUE if the given btree is set to safety level 1.  In other
49107
 
** words, return TRUE if no sync() occurs on the disk files.
49108
 
*/
49109
 
SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
49110
 
  BtShared *pBt = p->pBt;
49111
 
  int rc;
49112
 
  assert( sqlite3_mutex_held(p->db->mutex) );  
49113
 
  sqlite3BtreeEnter(p);
49114
 
  assert( pBt && pBt->pPager );
49115
 
  rc = sqlite3PagerNosync(pBt->pPager);
49116
 
  sqlite3BtreeLeave(p);
49117
 
  return rc;
49118
 
}
49119
 
 
49120
 
/*
49121
 
** Change the default pages size and the number of reserved bytes per page.
49122
 
** Or, if the page size has already been fixed, return SQLITE_READONLY 
49123
 
** without changing anything.
49124
 
**
49125
 
** The page size must be a power of 2 between 512 and 65536.  If the page
49126
 
** size supplied does not meet this constraint then the page size is not
49127
 
** changed.
49128
 
**
49129
 
** Page sizes are constrained to be a power of two so that the region
49130
 
** of the database file used for locking (beginning at PENDING_BYTE,
49131
 
** the first byte past the 1GB boundary, 0x40000000) needs to occur
49132
 
** at the beginning of a page.
49133
 
**
49134
 
** If parameter nReserve is less than zero, then the number of reserved
49135
 
** bytes per page is left unchanged.
49136
 
**
49137
 
** If the iFix!=0 then the pageSizeFixed flag is set so that the page size
49138
 
** and autovacuum mode can no longer be changed.
49139
 
*/
49140
 
SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
49141
 
  int rc = SQLITE_OK;
49142
 
  BtShared *pBt = p->pBt;
49143
 
  assert( nReserve>=-1 && nReserve<=255 );
49144
 
  sqlite3BtreeEnter(p);
49145
 
  if( pBt->pageSizeFixed ){
49146
 
    sqlite3BtreeLeave(p);
49147
 
    return SQLITE_READONLY;
49148
 
  }
49149
 
  if( nReserve<0 ){
49150
 
    nReserve = pBt->pageSize - pBt->usableSize;
49151
 
  }
49152
 
  assert( nReserve>=0 && nReserve<=255 );
49153
 
  if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
49154
 
        ((pageSize-1)&pageSize)==0 ){
49155
 
    assert( (pageSize & 7)==0 );
49156
 
    assert( !pBt->pPage1 && !pBt->pCursor );
49157
 
    pBt->pageSize = (u32)pageSize;
49158
 
    freeTempSpace(pBt);
49159
 
  }
49160
 
  rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
49161
 
  pBt->usableSize = pBt->pageSize - (u16)nReserve;
49162
 
  if( iFix ) pBt->pageSizeFixed = 1;
49163
 
  sqlite3BtreeLeave(p);
49164
 
  return rc;
49165
 
}
49166
 
 
49167
 
/*
49168
 
** Return the currently defined page size
49169
 
*/
49170
 
SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
49171
 
  return p->pBt->pageSize;
49172
 
}
49173
 
 
49174
 
#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
49175
 
/*
49176
 
** Return the number of bytes of space at the end of every page that
49177
 
** are intentually left unused.  This is the "reserved" space that is
49178
 
** sometimes used by extensions.
49179
 
*/
49180
 
SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree *p){
49181
 
  int n;
49182
 
  sqlite3BtreeEnter(p);
49183
 
  n = p->pBt->pageSize - p->pBt->usableSize;
49184
 
  sqlite3BtreeLeave(p);
49185
 
  return n;
49186
 
}
49187
 
 
49188
 
/*
49189
 
** Set the maximum page count for a database if mxPage is positive.
49190
 
** No changes are made if mxPage is 0 or negative.
49191
 
** Regardless of the value of mxPage, return the maximum page count.
49192
 
*/
49193
 
SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
49194
 
  int n;
49195
 
  sqlite3BtreeEnter(p);
49196
 
  n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
49197
 
  sqlite3BtreeLeave(p);
49198
 
  return n;
49199
 
}
49200
 
 
49201
 
/*
49202
 
** Set the secureDelete flag if newFlag is 0 or 1.  If newFlag is -1,
49203
 
** then make no changes.  Always return the value of the secureDelete
49204
 
** setting after the change.
49205
 
*/
49206
 
SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
49207
 
  int b;
49208
 
  if( p==0 ) return 0;
49209
 
  sqlite3BtreeEnter(p);
49210
 
  if( newFlag>=0 ){
49211
 
    p->pBt->secureDelete = (newFlag!=0) ? 1 : 0;
49212
 
  } 
49213
 
  b = p->pBt->secureDelete;
49214
 
  sqlite3BtreeLeave(p);
49215
 
  return b;
49216
 
}
49217
 
#endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
49218
 
 
49219
 
/*
49220
 
** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
49221
 
** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
49222
 
** is disabled. The default value for the auto-vacuum property is 
49223
 
** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
49224
 
*/
49225
 
SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
49226
 
#ifdef SQLITE_OMIT_AUTOVACUUM
49227
 
  return SQLITE_READONLY;
49228
 
#else
49229
 
  BtShared *pBt = p->pBt;
49230
 
  int rc = SQLITE_OK;
49231
 
  u8 av = (u8)autoVacuum;
49232
 
 
49233
 
  sqlite3BtreeEnter(p);
49234
 
  if( pBt->pageSizeFixed && (av ?1:0)!=pBt->autoVacuum ){
49235
 
    rc = SQLITE_READONLY;
49236
 
  }else{
49237
 
    pBt->autoVacuum = av ?1:0;
49238
 
    pBt->incrVacuum = av==2 ?1:0;
49239
 
  }
49240
 
  sqlite3BtreeLeave(p);
49241
 
  return rc;
49242
 
#endif
49243
 
}
49244
 
 
49245
 
/*
49246
 
** Return the value of the 'auto-vacuum' property. If auto-vacuum is 
49247
 
** enabled 1 is returned. Otherwise 0.
49248
 
*/
49249
 
SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
49250
 
#ifdef SQLITE_OMIT_AUTOVACUUM
49251
 
  return BTREE_AUTOVACUUM_NONE;
49252
 
#else
49253
 
  int rc;
49254
 
  sqlite3BtreeEnter(p);
49255
 
  rc = (
49256
 
    (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
49257
 
    (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
49258
 
    BTREE_AUTOVACUUM_INCR
49259
 
  );
49260
 
  sqlite3BtreeLeave(p);
49261
 
  return rc;
49262
 
#endif
49263
 
}
49264
 
 
49265
 
 
49266
 
/*
49267
 
** Get a reference to pPage1 of the database file.  This will
49268
 
** also acquire a readlock on that file.
49269
 
**
49270
 
** SQLITE_OK is returned on success.  If the file is not a
49271
 
** well-formed database file, then SQLITE_CORRUPT is returned.
49272
 
** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
49273
 
** is returned if we run out of memory. 
49274
 
*/
49275
 
static int lockBtree(BtShared *pBt){
49276
 
  int rc;              /* Result code from subfunctions */
49277
 
  MemPage *pPage1;     /* Page 1 of the database file */
49278
 
  int nPage;           /* Number of pages in the database */
49279
 
  int nPageFile = 0;   /* Number of pages in the database file */
49280
 
  int nPageHeader;     /* Number of pages in the database according to hdr */
49281
 
 
49282
 
  assert( sqlite3_mutex_held(pBt->mutex) );
49283
 
  assert( pBt->pPage1==0 );
49284
 
  rc = sqlite3PagerSharedLock(pBt->pPager);
49285
 
  if( rc!=SQLITE_OK ) return rc;
49286
 
  rc = btreeGetPage(pBt, 1, &pPage1, 0);
49287
 
  if( rc!=SQLITE_OK ) return rc;
49288
 
 
49289
 
  /* Do some checking to help insure the file we opened really is
49290
 
  ** a valid database file. 
49291
 
  */
49292
 
  nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
49293
 
  sqlite3PagerPagecount(pBt->pPager, &nPageFile);
49294
 
  if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
49295
 
    nPage = nPageFile;
49296
 
  }
49297
 
  if( nPage>0 ){
49298
 
    u32 pageSize;
49299
 
    u32 usableSize;
49300
 
    u8 *page1 = pPage1->aData;
49301
 
    rc = SQLITE_NOTADB;
49302
 
    if( memcmp(page1, zMagicHeader, 16)!=0 ){
49303
 
      goto page1_init_failed;
49304
 
    }
49305
 
 
49306
 
#ifdef SQLITE_OMIT_WAL
49307
 
    if( page1[18]>1 ){
49308
 
      pBt->readOnly = 1;
49309
 
    }
49310
 
    if( page1[19]>1 ){
49311
 
      goto page1_init_failed;
49312
 
    }
49313
 
#else
49314
 
    if( page1[18]>2 ){
49315
 
      pBt->readOnly = 1;
49316
 
    }
49317
 
    if( page1[19]>2 ){
49318
 
      goto page1_init_failed;
49319
 
    }
49320
 
 
49321
 
    /* If the write version is set to 2, this database should be accessed
49322
 
    ** in WAL mode. If the log is not already open, open it now. Then 
49323
 
    ** return SQLITE_OK and return without populating BtShared.pPage1.
49324
 
    ** The caller detects this and calls this function again. This is
49325
 
    ** required as the version of page 1 currently in the page1 buffer
49326
 
    ** may not be the latest version - there may be a newer one in the log
49327
 
    ** file.
49328
 
    */
49329
 
    if( page1[19]==2 && pBt->doNotUseWAL==0 ){
49330
 
      int isOpen = 0;
49331
 
      rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
49332
 
      if( rc!=SQLITE_OK ){
49333
 
        goto page1_init_failed;
49334
 
      }else if( isOpen==0 ){
49335
 
        releasePage(pPage1);
49336
 
        return SQLITE_OK;
49337
 
      }
49338
 
      rc = SQLITE_NOTADB;
49339
 
    }
49340
 
#endif
49341
 
 
49342
 
    /* The maximum embedded fraction must be exactly 25%.  And the minimum
49343
 
    ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
49344
 
    ** The original design allowed these amounts to vary, but as of
49345
 
    ** version 3.6.0, we require them to be fixed.
49346
 
    */
49347
 
    if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
49348
 
      goto page1_init_failed;
49349
 
    }
49350
 
    pageSize = (page1[16]<<8) | (page1[17]<<16);
49351
 
    if( ((pageSize-1)&pageSize)!=0
49352
 
     || pageSize>SQLITE_MAX_PAGE_SIZE 
49353
 
     || pageSize<=256 
49354
 
    ){
49355
 
      goto page1_init_failed;
49356
 
    }
49357
 
    assert( (pageSize & 7)==0 );
49358
 
    usableSize = pageSize - page1[20];
49359
 
    if( (u32)pageSize!=pBt->pageSize ){
49360
 
      /* After reading the first page of the database assuming a page size
49361
 
      ** of BtShared.pageSize, we have discovered that the page-size is
49362
 
      ** actually pageSize. Unlock the database, leave pBt->pPage1 at
49363
 
      ** zero and return SQLITE_OK. The caller will call this function
49364
 
      ** again with the correct page-size.
49365
 
      */
49366
 
      releasePage(pPage1);
49367
 
      pBt->usableSize = usableSize;
49368
 
      pBt->pageSize = pageSize;
49369
 
      freeTempSpace(pBt);
49370
 
      rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
49371
 
                                   pageSize-usableSize);
49372
 
      return rc;
49373
 
    }
49374
 
    if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
49375
 
      rc = SQLITE_CORRUPT_BKPT;
49376
 
      goto page1_init_failed;
49377
 
    }
49378
 
    if( usableSize<480 ){
49379
 
      goto page1_init_failed;
49380
 
    }
49381
 
    pBt->pageSize = pageSize;
49382
 
    pBt->usableSize = usableSize;
49383
 
#ifndef SQLITE_OMIT_AUTOVACUUM
49384
 
    pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
49385
 
    pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
49386
 
#endif
49387
 
  }
49388
 
 
49389
 
  /* maxLocal is the maximum amount of payload to store locally for
49390
 
  ** a cell.  Make sure it is small enough so that at least minFanout
49391
 
  ** cells can will fit on one page.  We assume a 10-byte page header.
49392
 
  ** Besides the payload, the cell must store:
49393
 
  **     2-byte pointer to the cell
49394
 
  **     4-byte child pointer
49395
 
  **     9-byte nKey value
49396
 
  **     4-byte nData value
49397
 
  **     4-byte overflow page pointer
49398
 
  ** So a cell consists of a 2-byte pointer, a header which is as much as
49399
 
  ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
49400
 
  ** page pointer.
49401
 
  */
49402
 
  pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
49403
 
  pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
49404
 
  pBt->maxLeaf = (u16)(pBt->usableSize - 35);
49405
 
  pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
49406
 
  assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
49407
 
  pBt->pPage1 = pPage1;
49408
 
  pBt->nPage = nPage;
49409
 
  return SQLITE_OK;
49410
 
 
49411
 
page1_init_failed:
49412
 
  releasePage(pPage1);
49413
 
  pBt->pPage1 = 0;
49414
 
  return rc;
49415
 
}
49416
 
 
49417
 
/*
49418
 
** If there are no outstanding cursors and we are not in the middle
49419
 
** of a transaction but there is a read lock on the database, then
49420
 
** this routine unrefs the first page of the database file which 
49421
 
** has the effect of releasing the read lock.
49422
 
**
49423
 
** If there is a transaction in progress, this routine is a no-op.
49424
 
*/
49425
 
static void unlockBtreeIfUnused(BtShared *pBt){
49426
 
  assert( sqlite3_mutex_held(pBt->mutex) );
49427
 
  assert( pBt->pCursor==0 || pBt->inTransaction>TRANS_NONE );
49428
 
  if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
49429
 
    assert( pBt->pPage1->aData );
49430
 
    assert( sqlite3PagerRefcount(pBt->pPager)==1 );
49431
 
    assert( pBt->pPage1->aData );
49432
 
    releasePage(pBt->pPage1);
49433
 
    pBt->pPage1 = 0;
49434
 
  }
49435
 
}
49436
 
 
49437
 
/*
49438
 
** If pBt points to an empty file then convert that empty file
49439
 
** into a new empty database by initializing the first page of
49440
 
** the database.
49441
 
*/
49442
 
static int newDatabase(BtShared *pBt){
49443
 
  MemPage *pP1;
49444
 
  unsigned char *data;
49445
 
  int rc;
49446
 
 
49447
 
  assert( sqlite3_mutex_held(pBt->mutex) );
49448
 
  if( pBt->nPage>0 ){
49449
 
    return SQLITE_OK;
49450
 
  }
49451
 
  pP1 = pBt->pPage1;
49452
 
  assert( pP1!=0 );
49453
 
  data = pP1->aData;
49454
 
  rc = sqlite3PagerWrite(pP1->pDbPage);
49455
 
  if( rc ) return rc;
49456
 
  memcpy(data, zMagicHeader, sizeof(zMagicHeader));
49457
 
  assert( sizeof(zMagicHeader)==16 );
49458
 
  data[16] = (u8)((pBt->pageSize>>8)&0xff);
49459
 
  data[17] = (u8)((pBt->pageSize>>16)&0xff);
49460
 
  data[18] = 1;
49461
 
  data[19] = 1;
49462
 
  assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
49463
 
  data[20] = (u8)(pBt->pageSize - pBt->usableSize);
49464
 
  data[21] = 64;
49465
 
  data[22] = 32;
49466
 
  data[23] = 32;
49467
 
  memset(&data[24], 0, 100-24);
49468
 
  zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
49469
 
  pBt->pageSizeFixed = 1;
49470
 
#ifndef SQLITE_OMIT_AUTOVACUUM
49471
 
  assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
49472
 
  assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
49473
 
  put4byte(&data[36 + 4*4], pBt->autoVacuum);
49474
 
  put4byte(&data[36 + 7*4], pBt->incrVacuum);
49475
 
#endif
49476
 
  pBt->nPage = 1;
49477
 
  data[31] = 1;
49478
 
  return SQLITE_OK;
49479
 
}
49480
 
 
49481
 
/*
49482
 
** Attempt to start a new transaction. A write-transaction
49483
 
** is started if the second argument is nonzero, otherwise a read-
49484
 
** transaction.  If the second argument is 2 or more and exclusive
49485
 
** transaction is started, meaning that no other process is allowed
49486
 
** to access the database.  A preexisting transaction may not be
49487
 
** upgraded to exclusive by calling this routine a second time - the
49488
 
** exclusivity flag only works for a new transaction.
49489
 
**
49490
 
** A write-transaction must be started before attempting any 
49491
 
** changes to the database.  None of the following routines 
49492
 
** will work unless a transaction is started first:
49493
 
**
49494
 
**      sqlite3BtreeCreateTable()
49495
 
**      sqlite3BtreeCreateIndex()
49496
 
**      sqlite3BtreeClearTable()
49497
 
**      sqlite3BtreeDropTable()
49498
 
**      sqlite3BtreeInsert()
49499
 
**      sqlite3BtreeDelete()
49500
 
**      sqlite3BtreeUpdateMeta()
49501
 
**
49502
 
** If an initial attempt to acquire the lock fails because of lock contention
49503
 
** and the database was previously unlocked, then invoke the busy handler
49504
 
** if there is one.  But if there was previously a read-lock, do not
49505
 
** invoke the busy handler - just return SQLITE_BUSY.  SQLITE_BUSY is 
49506
 
** returned when there is already a read-lock in order to avoid a deadlock.
49507
 
**
49508
 
** Suppose there are two processes A and B.  A has a read lock and B has
49509
 
** a reserved lock.  B tries to promote to exclusive but is blocked because
49510
 
** of A's read lock.  A tries to promote to reserved but is blocked by B.
49511
 
** One or the other of the two processes must give way or there can be
49512
 
** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
49513
 
** when A already has a read lock, we encourage A to give up and let B
49514
 
** proceed.
49515
 
*/
49516
 
SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
49517
 
  sqlite3 *pBlock = 0;
49518
 
  BtShared *pBt = p->pBt;
49519
 
  int rc = SQLITE_OK;
49520
 
 
49521
 
  sqlite3BtreeEnter(p);
49522
 
  btreeIntegrity(p);
49523
 
 
49524
 
  /* If the btree is already in a write-transaction, or it
49525
 
  ** is already in a read-transaction and a read-transaction
49526
 
  ** is requested, this is a no-op.
49527
 
  */
49528
 
  if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
49529
 
    goto trans_begun;
49530
 
  }
49531
 
 
49532
 
  /* Write transactions are not possible on a read-only database */
49533
 
  if( pBt->readOnly && wrflag ){
49534
 
    rc = SQLITE_READONLY;
49535
 
    goto trans_begun;
49536
 
  }
49537
 
 
49538
 
#ifndef SQLITE_OMIT_SHARED_CACHE
49539
 
  /* If another database handle has already opened a write transaction 
49540
 
  ** on this shared-btree structure and a second write transaction is
49541
 
  ** requested, return SQLITE_LOCKED.
49542
 
  */
49543
 
  if( (wrflag && pBt->inTransaction==TRANS_WRITE) || pBt->isPending ){
49544
 
    pBlock = pBt->pWriter->db;
49545
 
  }else if( wrflag>1 ){
49546
 
    BtLock *pIter;
49547
 
    for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
49548
 
      if( pIter->pBtree!=p ){
49549
 
        pBlock = pIter->pBtree->db;
49550
 
        break;
49551
 
      }
49552
 
    }
49553
 
  }
49554
 
  if( pBlock ){
49555
 
    sqlite3ConnectionBlocked(p->db, pBlock);
49556
 
    rc = SQLITE_LOCKED_SHAREDCACHE;
49557
 
    goto trans_begun;
49558
 
  }
49559
 
#endif
49560
 
 
49561
 
  /* Any read-only or read-write transaction implies a read-lock on 
49562
 
  ** page 1. So if some other shared-cache client already has a write-lock 
49563
 
  ** on page 1, the transaction cannot be opened. */
49564
 
  rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
49565
 
  if( SQLITE_OK!=rc ) goto trans_begun;
49566
 
 
49567
 
  pBt->initiallyEmpty = (u8)(pBt->nPage==0);
49568
 
  do {
49569
 
    /* Call lockBtree() until either pBt->pPage1 is populated or
49570
 
    ** lockBtree() returns something other than SQLITE_OK. lockBtree()
49571
 
    ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
49572
 
    ** reading page 1 it discovers that the page-size of the database 
49573
 
    ** file is not pBt->pageSize. In this case lockBtree() will update
49574
 
    ** pBt->pageSize to the page-size of the file on disk.
49575
 
    */
49576
 
    while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
49577
 
 
49578
 
    if( rc==SQLITE_OK && wrflag ){
49579
 
      if( pBt->readOnly ){
49580
 
        rc = SQLITE_READONLY;
49581
 
      }else{
49582
 
        rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
49583
 
        if( rc==SQLITE_OK ){
49584
 
          rc = newDatabase(pBt);
49585
 
        }
49586
 
      }
49587
 
    }
49588
 
  
49589
 
    if( rc!=SQLITE_OK ){
49590
 
      unlockBtreeIfUnused(pBt);
49591
 
    }
49592
 
  }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
49593
 
          btreeInvokeBusyHandler(pBt) );
49594
 
 
49595
 
  if( rc==SQLITE_OK ){
49596
 
    if( p->inTrans==TRANS_NONE ){
49597
 
      pBt->nTransaction++;
49598
 
#ifndef SQLITE_OMIT_SHARED_CACHE
49599
 
      if( p->sharable ){
49600
 
        assert( p->lock.pBtree==p && p->lock.iTable==1 );
49601
 
        p->lock.eLock = READ_LOCK;
49602
 
        p->lock.pNext = pBt->pLock;
49603
 
        pBt->pLock = &p->lock;
49604
 
      }
49605
 
#endif
49606
 
    }
49607
 
    p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
49608
 
    if( p->inTrans>pBt->inTransaction ){
49609
 
      pBt->inTransaction = p->inTrans;
49610
 
    }
49611
 
    if( wrflag ){
49612
 
      MemPage *pPage1 = pBt->pPage1;
49613
 
#ifndef SQLITE_OMIT_SHARED_CACHE
49614
 
      assert( !pBt->pWriter );
49615
 
      pBt->pWriter = p;
49616
 
      pBt->isExclusive = (u8)(wrflag>1);
49617
 
#endif
49618
 
 
49619
 
      /* If the db-size header field is incorrect (as it may be if an old
49620
 
      ** client has been writing the database file), update it now. Doing
49621
 
      ** this sooner rather than later means the database size can safely 
49622
 
      ** re-read the database size from page 1 if a savepoint or transaction
49623
 
      ** rollback occurs within the transaction.
49624
 
      */
49625
 
      if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
49626
 
        rc = sqlite3PagerWrite(pPage1->pDbPage);
49627
 
        if( rc==SQLITE_OK ){
49628
 
          put4byte(&pPage1->aData[28], pBt->nPage);
49629
 
        }
49630
 
      }
49631
 
    }
49632
 
  }
49633
 
 
49634
 
 
49635
 
trans_begun:
49636
 
  if( rc==SQLITE_OK && wrflag ){
49637
 
    /* This call makes sure that the pager has the correct number of
49638
 
    ** open savepoints. If the second parameter is greater than 0 and
49639
 
    ** the sub-journal is not already open, then it will be opened here.
49640
 
    */
49641
 
    rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
49642
 
  }
49643
 
 
49644
 
  btreeIntegrity(p);
49645
 
  sqlite3BtreeLeave(p);
49646
 
  return rc;
49647
 
}
49648
 
 
49649
 
#ifndef SQLITE_OMIT_AUTOVACUUM
49650
 
 
49651
 
/*
49652
 
** Set the pointer-map entries for all children of page pPage. Also, if
49653
 
** pPage contains cells that point to overflow pages, set the pointer
49654
 
** map entries for the overflow pages as well.
49655
 
*/
49656
 
static int setChildPtrmaps(MemPage *pPage){
49657
 
  int i;                             /* Counter variable */
49658
 
  int nCell;                         /* Number of cells in page pPage */
49659
 
  int rc;                            /* Return code */
49660
 
  BtShared *pBt = pPage->pBt;
49661
 
  u8 isInitOrig = pPage->isInit;
49662
 
  Pgno pgno = pPage->pgno;
49663
 
 
49664
 
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49665
 
  rc = btreeInitPage(pPage);
49666
 
  if( rc!=SQLITE_OK ){
49667
 
    goto set_child_ptrmaps_out;
49668
 
  }
49669
 
  nCell = pPage->nCell;
49670
 
 
49671
 
  for(i=0; i<nCell; i++){
49672
 
    u8 *pCell = findCell(pPage, i);
49673
 
 
49674
 
    ptrmapPutOvflPtr(pPage, pCell, &rc);
49675
 
 
49676
 
    if( !pPage->leaf ){
49677
 
      Pgno childPgno = get4byte(pCell);
49678
 
      ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
49679
 
    }
49680
 
  }
49681
 
 
49682
 
  if( !pPage->leaf ){
49683
 
    Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
49684
 
    ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
49685
 
  }
49686
 
 
49687
 
set_child_ptrmaps_out:
49688
 
  pPage->isInit = isInitOrig;
49689
 
  return rc;
49690
 
}
49691
 
 
49692
 
/*
49693
 
** Somewhere on pPage is a pointer to page iFrom.  Modify this pointer so
49694
 
** that it points to iTo. Parameter eType describes the type of pointer to
49695
 
** be modified, as  follows:
49696
 
**
49697
 
** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child 
49698
 
**                   page of pPage.
49699
 
**
49700
 
** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
49701
 
**                   page pointed to by one of the cells on pPage.
49702
 
**
49703
 
** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
49704
 
**                   overflow page in the list.
49705
 
*/
49706
 
static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
49707
 
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49708
 
  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
49709
 
  if( eType==PTRMAP_OVERFLOW2 ){
49710
 
    /* The pointer is always the first 4 bytes of the page in this case.  */
49711
 
    if( get4byte(pPage->aData)!=iFrom ){
49712
 
      return SQLITE_CORRUPT_BKPT;
49713
 
    }
49714
 
    put4byte(pPage->aData, iTo);
49715
 
  }else{
49716
 
    u8 isInitOrig = pPage->isInit;
49717
 
    int i;
49718
 
    int nCell;
49719
 
 
49720
 
    btreeInitPage(pPage);
49721
 
    nCell = pPage->nCell;
49722
 
 
49723
 
    for(i=0; i<nCell; i++){
49724
 
      u8 *pCell = findCell(pPage, i);
49725
 
      if( eType==PTRMAP_OVERFLOW1 ){
49726
 
        CellInfo info;
49727
 
        btreeParseCellPtr(pPage, pCell, &info);
49728
 
        if( info.iOverflow ){
49729
 
          if( iFrom==get4byte(&pCell[info.iOverflow]) ){
49730
 
            put4byte(&pCell[info.iOverflow], iTo);
49731
 
            break;
49732
 
          }
49733
 
        }
49734
 
      }else{
49735
 
        if( get4byte(pCell)==iFrom ){
49736
 
          put4byte(pCell, iTo);
49737
 
          break;
49738
 
        }
49739
 
      }
49740
 
    }
49741
 
  
49742
 
    if( i==nCell ){
49743
 
      if( eType!=PTRMAP_BTREE || 
49744
 
          get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
49745
 
        return SQLITE_CORRUPT_BKPT;
49746
 
      }
49747
 
      put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
49748
 
    }
49749
 
 
49750
 
    pPage->isInit = isInitOrig;
49751
 
  }
49752
 
  return SQLITE_OK;
49753
 
}
49754
 
 
49755
 
 
49756
 
/*
49757
 
** Move the open database page pDbPage to location iFreePage in the 
49758
 
** database. The pDbPage reference remains valid.
49759
 
**
49760
 
** The isCommit flag indicates that there is no need to remember that
49761
 
** the journal needs to be sync()ed before database page pDbPage->pgno 
49762
 
** can be written to. The caller has already promised not to write to that
49763
 
** page.
49764
 
*/
49765
 
static int relocatePage(
49766
 
  BtShared *pBt,           /* Btree */
49767
 
  MemPage *pDbPage,        /* Open page to move */
49768
 
  u8 eType,                /* Pointer map 'type' entry for pDbPage */
49769
 
  Pgno iPtrPage,           /* Pointer map 'page-no' entry for pDbPage */
49770
 
  Pgno iFreePage,          /* The location to move pDbPage to */
49771
 
  int isCommit             /* isCommit flag passed to sqlite3PagerMovepage */
49772
 
){
49773
 
  MemPage *pPtrPage;   /* The page that contains a pointer to pDbPage */
49774
 
  Pgno iDbPage = pDbPage->pgno;
49775
 
  Pager *pPager = pBt->pPager;
49776
 
  int rc;
49777
 
 
49778
 
  assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || 
49779
 
      eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
49780
 
  assert( sqlite3_mutex_held(pBt->mutex) );
49781
 
  assert( pDbPage->pBt==pBt );
49782
 
 
49783
 
  /* Move page iDbPage from its current location to page number iFreePage */
49784
 
  TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", 
49785
 
      iDbPage, iFreePage, iPtrPage, eType));
49786
 
  rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
49787
 
  if( rc!=SQLITE_OK ){
49788
 
    return rc;
49789
 
  }
49790
 
  pDbPage->pgno = iFreePage;
49791
 
 
49792
 
  /* If pDbPage was a btree-page, then it may have child pages and/or cells
49793
 
  ** that point to overflow pages. The pointer map entries for all these
49794
 
  ** pages need to be changed.
49795
 
  **
49796
 
  ** If pDbPage is an overflow page, then the first 4 bytes may store a
49797
 
  ** pointer to a subsequent overflow page. If this is the case, then
49798
 
  ** the pointer map needs to be updated for the subsequent overflow page.
49799
 
  */
49800
 
  if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
49801
 
    rc = setChildPtrmaps(pDbPage);
49802
 
    if( rc!=SQLITE_OK ){
49803
 
      return rc;
49804
 
    }
49805
 
  }else{
49806
 
    Pgno nextOvfl = get4byte(pDbPage->aData);
49807
 
    if( nextOvfl!=0 ){
49808
 
      ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
49809
 
      if( rc!=SQLITE_OK ){
49810
 
        return rc;
49811
 
      }
49812
 
    }
49813
 
  }
49814
 
 
49815
 
  /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
49816
 
  ** that it points at iFreePage. Also fix the pointer map entry for
49817
 
  ** iPtrPage.
49818
 
  */
49819
 
  if( eType!=PTRMAP_ROOTPAGE ){
49820
 
    rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
49821
 
    if( rc!=SQLITE_OK ){
49822
 
      return rc;
49823
 
    }
49824
 
    rc = sqlite3PagerWrite(pPtrPage->pDbPage);
49825
 
    if( rc!=SQLITE_OK ){
49826
 
      releasePage(pPtrPage);
49827
 
      return rc;
49828
 
    }
49829
 
    rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
49830
 
    releasePage(pPtrPage);
49831
 
    if( rc==SQLITE_OK ){
49832
 
      ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
49833
 
    }
49834
 
  }
49835
 
  return rc;
49836
 
}
49837
 
 
49838
 
/* Forward declaration required by incrVacuumStep(). */
49839
 
static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
49840
 
 
49841
 
/*
49842
 
** Perform a single step of an incremental-vacuum. If successful,
49843
 
** return SQLITE_OK. If there is no work to do (and therefore no
49844
 
** point in calling this function again), return SQLITE_DONE.
49845
 
**
49846
 
** More specificly, this function attempts to re-organize the 
49847
 
** database so that the last page of the file currently in use
49848
 
** is no longer in use.
49849
 
**
49850
 
** If the nFin parameter is non-zero, this function assumes
49851
 
** that the caller will keep calling incrVacuumStep() until
49852
 
** it returns SQLITE_DONE or an error, and that nFin is the
49853
 
** number of pages the database file will contain after this 
49854
 
** process is complete.  If nFin is zero, it is assumed that
49855
 
** incrVacuumStep() will be called a finite amount of times
49856
 
** which may or may not empty the freelist.  A full autovacuum
49857
 
** has nFin>0.  A "PRAGMA incremental_vacuum" has nFin==0.
49858
 
*/
49859
 
static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){
49860
 
  Pgno nFreeList;           /* Number of pages still on the free-list */
49861
 
  int rc;
49862
 
 
49863
 
  assert( sqlite3_mutex_held(pBt->mutex) );
49864
 
  assert( iLastPg>nFin );
49865
 
 
49866
 
  if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
49867
 
    u8 eType;
49868
 
    Pgno iPtrPage;
49869
 
 
49870
 
    nFreeList = get4byte(&pBt->pPage1->aData[36]);
49871
 
    if( nFreeList==0 ){
49872
 
      return SQLITE_DONE;
49873
 
    }
49874
 
 
49875
 
    rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
49876
 
    if( rc!=SQLITE_OK ){
49877
 
      return rc;
49878
 
    }
49879
 
    if( eType==PTRMAP_ROOTPAGE ){
49880
 
      return SQLITE_CORRUPT_BKPT;
49881
 
    }
49882
 
 
49883
 
    if( eType==PTRMAP_FREEPAGE ){
49884
 
      if( nFin==0 ){
49885
 
        /* Remove the page from the files free-list. This is not required
49886
 
        ** if nFin is non-zero. In that case, the free-list will be
49887
 
        ** truncated to zero after this function returns, so it doesn't 
49888
 
        ** matter if it still contains some garbage entries.
49889
 
        */
49890
 
        Pgno iFreePg;
49891
 
        MemPage *pFreePg;
49892
 
        rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
49893
 
        if( rc!=SQLITE_OK ){
49894
 
          return rc;
49895
 
        }
49896
 
        assert( iFreePg==iLastPg );
49897
 
        releasePage(pFreePg);
49898
 
      }
49899
 
    } else {
49900
 
      Pgno iFreePg;             /* Index of free page to move pLastPg to */
49901
 
      MemPage *pLastPg;
49902
 
 
49903
 
      rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
49904
 
      if( rc!=SQLITE_OK ){
49905
 
        return rc;
49906
 
      }
49907
 
 
49908
 
      /* If nFin is zero, this loop runs exactly once and page pLastPg
49909
 
      ** is swapped with the first free page pulled off the free list.
49910
 
      **
49911
 
      ** On the other hand, if nFin is greater than zero, then keep
49912
 
      ** looping until a free-page located within the first nFin pages
49913
 
      ** of the file is found.
49914
 
      */
49915
 
      do {
49916
 
        MemPage *pFreePg;
49917
 
        rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
49918
 
        if( rc!=SQLITE_OK ){
49919
 
          releasePage(pLastPg);
49920
 
          return rc;
49921
 
        }
49922
 
        releasePage(pFreePg);
49923
 
      }while( nFin!=0 && iFreePg>nFin );
49924
 
      assert( iFreePg<iLastPg );
49925
 
      
49926
 
      rc = sqlite3PagerWrite(pLastPg->pDbPage);
49927
 
      if( rc==SQLITE_OK ){
49928
 
        rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0);
49929
 
      }
49930
 
      releasePage(pLastPg);
49931
 
      if( rc!=SQLITE_OK ){
49932
 
        return rc;
49933
 
      }
49934
 
    }
49935
 
  }
49936
 
 
49937
 
  if( nFin==0 ){
49938
 
    iLastPg--;
49939
 
    while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){
49940
 
      if( PTRMAP_ISPAGE(pBt, iLastPg) ){
49941
 
        MemPage *pPg;
49942
 
        rc = btreeGetPage(pBt, iLastPg, &pPg, 0);
49943
 
        if( rc!=SQLITE_OK ){
49944
 
          return rc;
49945
 
        }
49946
 
        rc = sqlite3PagerWrite(pPg->pDbPage);
49947
 
        releasePage(pPg);
49948
 
        if( rc!=SQLITE_OK ){
49949
 
          return rc;
49950
 
        }
49951
 
      }
49952
 
      iLastPg--;
49953
 
    }
49954
 
    sqlite3PagerTruncateImage(pBt->pPager, iLastPg);
49955
 
    pBt->nPage = iLastPg;
49956
 
  }
49957
 
  return SQLITE_OK;
49958
 
}
49959
 
 
49960
 
/*
49961
 
** A write-transaction must be opened before calling this function.
49962
 
** It performs a single unit of work towards an incremental vacuum.
49963
 
**
49964
 
** If the incremental vacuum is finished after this function has run,
49965
 
** SQLITE_DONE is returned. If it is not finished, but no error occurred,
49966
 
** SQLITE_OK is returned. Otherwise an SQLite error code. 
49967
 
*/
49968
 
SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
49969
 
  int rc;
49970
 
  BtShared *pBt = p->pBt;
49971
 
 
49972
 
  sqlite3BtreeEnter(p);
49973
 
  assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
49974
 
  if( !pBt->autoVacuum ){
49975
 
    rc = SQLITE_DONE;
49976
 
  }else{
49977
 
    invalidateAllOverflowCache(pBt);
49978
 
    rc = incrVacuumStep(pBt, 0, btreePagecount(pBt));
49979
 
    if( rc==SQLITE_OK ){
49980
 
      rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
49981
 
      put4byte(&pBt->pPage1->aData[28], pBt->nPage);
49982
 
    }
49983
 
  }
49984
 
  sqlite3BtreeLeave(p);
49985
 
  return rc;
49986
 
}
49987
 
 
49988
 
/*
49989
 
** This routine is called prior to sqlite3PagerCommit when a transaction
49990
 
** is commited for an auto-vacuum database.
49991
 
**
49992
 
** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
49993
 
** the database file should be truncated to during the commit process. 
49994
 
** i.e. the database has been reorganized so that only the first *pnTrunc
49995
 
** pages are in use.
49996
 
*/
49997
 
static int autoVacuumCommit(BtShared *pBt){
49998
 
  int rc = SQLITE_OK;
49999
 
  Pager *pPager = pBt->pPager;
50000
 
  VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
50001
 
 
50002
 
  assert( sqlite3_mutex_held(pBt->mutex) );
50003
 
  invalidateAllOverflowCache(pBt);
50004
 
  assert(pBt->autoVacuum);
50005
 
  if( !pBt->incrVacuum ){
50006
 
    Pgno nFin;         /* Number of pages in database after autovacuuming */
50007
 
    Pgno nFree;        /* Number of pages on the freelist initially */
50008
 
    Pgno nPtrmap;      /* Number of PtrMap pages to be freed */
50009
 
    Pgno iFree;        /* The next page to be freed */
50010
 
    int nEntry;        /* Number of entries on one ptrmap page */
50011
 
    Pgno nOrig;        /* Database size before freeing */
50012
 
 
50013
 
    nOrig = btreePagecount(pBt);
50014
 
    if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
50015
 
      /* It is not possible to create a database for which the final page
50016
 
      ** is either a pointer-map page or the pending-byte page. If one
50017
 
      ** is encountered, this indicates corruption.
50018
 
      */
50019
 
      return SQLITE_CORRUPT_BKPT;
50020
 
    }
50021
 
 
50022
 
    nFree = get4byte(&pBt->pPage1->aData[36]);
50023
 
    nEntry = pBt->usableSize/5;
50024
 
    nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
50025
 
    nFin = nOrig - nFree - nPtrmap;
50026
 
    if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
50027
 
      nFin--;
50028
 
    }
50029
 
    while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
50030
 
      nFin--;
50031
 
    }
50032
 
    if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
50033
 
 
50034
 
    for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
50035
 
      rc = incrVacuumStep(pBt, nFin, iFree);
50036
 
    }
50037
 
    if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
50038
 
      rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
50039
 
      put4byte(&pBt->pPage1->aData[32], 0);
50040
 
      put4byte(&pBt->pPage1->aData[36], 0);
50041
 
      put4byte(&pBt->pPage1->aData[28], nFin);
50042
 
      sqlite3PagerTruncateImage(pBt->pPager, nFin);
50043
 
      pBt->nPage = nFin;
50044
 
    }
50045
 
    if( rc!=SQLITE_OK ){
50046
 
      sqlite3PagerRollback(pPager);
50047
 
    }
50048
 
  }
50049
 
 
50050
 
  assert( nRef==sqlite3PagerRefcount(pPager) );
50051
 
  return rc;
50052
 
}
50053
 
 
50054
 
#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
50055
 
# define setChildPtrmaps(x) SQLITE_OK
50056
 
#endif
50057
 
 
50058
 
/*
50059
 
** This routine does the first phase of a two-phase commit.  This routine
50060
 
** causes a rollback journal to be created (if it does not already exist)
50061
 
** and populated with enough information so that if a power loss occurs
50062
 
** the database can be restored to its original state by playing back
50063
 
** the journal.  Then the contents of the journal are flushed out to
50064
 
** the disk.  After the journal is safely on oxide, the changes to the
50065
 
** database are written into the database file and flushed to oxide.
50066
 
** At the end of this call, the rollback journal still exists on the
50067
 
** disk and we are still holding all locks, so the transaction has not
50068
 
** committed.  See sqlite3BtreeCommitPhaseTwo() for the second phase of the
50069
 
** commit process.
50070
 
**
50071
 
** This call is a no-op if no write-transaction is currently active on pBt.
50072
 
**
50073
 
** Otherwise, sync the database file for the btree pBt. zMaster points to
50074
 
** the name of a master journal file that should be written into the
50075
 
** individual journal file, or is NULL, indicating no master journal file 
50076
 
** (single database transaction).
50077
 
**
50078
 
** When this is called, the master journal should already have been
50079
 
** created, populated with this journal pointer and synced to disk.
50080
 
**
50081
 
** Once this is routine has returned, the only thing required to commit
50082
 
** the write-transaction for this database file is to delete the journal.
50083
 
*/
50084
 
SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
50085
 
  int rc = SQLITE_OK;
50086
 
  if( p->inTrans==TRANS_WRITE ){
50087
 
    BtShared *pBt = p->pBt;
50088
 
    sqlite3BtreeEnter(p);
50089
 
#ifndef SQLITE_OMIT_AUTOVACUUM
50090
 
    if( pBt->autoVacuum ){
50091
 
      rc = autoVacuumCommit(pBt);
50092
 
      if( rc!=SQLITE_OK ){
50093
 
        sqlite3BtreeLeave(p);
50094
 
        return rc;
50095
 
      }
50096
 
    }
50097
 
#endif
50098
 
    rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
50099
 
    sqlite3BtreeLeave(p);
50100
 
  }
50101
 
  return rc;
50102
 
}
50103
 
 
50104
 
/*
50105
 
** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
50106
 
** at the conclusion of a transaction.
50107
 
*/
50108
 
static void btreeEndTransaction(Btree *p){
50109
 
  BtShared *pBt = p->pBt;
50110
 
  assert( sqlite3BtreeHoldsMutex(p) );
50111
 
 
50112
 
  btreeClearHasContent(pBt);
50113
 
  if( p->inTrans>TRANS_NONE && p->db->activeVdbeCnt>1 ){
50114
 
    /* If there are other active statements that belong to this database
50115
 
    ** handle, downgrade to a read-only transaction. The other statements
50116
 
    ** may still be reading from the database.  */
50117
 
    downgradeAllSharedCacheTableLocks(p);
50118
 
    p->inTrans = TRANS_READ;
50119
 
  }else{
50120
 
    /* If the handle had any kind of transaction open, decrement the 
50121
 
    ** transaction count of the shared btree. If the transaction count 
50122
 
    ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
50123
 
    ** call below will unlock the pager.  */
50124
 
    if( p->inTrans!=TRANS_NONE ){
50125
 
      clearAllSharedCacheTableLocks(p);
50126
 
      pBt->nTransaction--;
50127
 
      if( 0==pBt->nTransaction ){
50128
 
        pBt->inTransaction = TRANS_NONE;
50129
 
      }
50130
 
    }
50131
 
 
50132
 
    /* Set the current transaction state to TRANS_NONE and unlock the 
50133
 
    ** pager if this call closed the only read or write transaction.  */
50134
 
    p->inTrans = TRANS_NONE;
50135
 
    unlockBtreeIfUnused(pBt);
50136
 
  }
50137
 
 
50138
 
  btreeIntegrity(p);
50139
 
}
50140
 
 
50141
 
/*
50142
 
** Commit the transaction currently in progress.
50143
 
**
50144
 
** This routine implements the second phase of a 2-phase commit.  The
50145
 
** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
50146
 
** be invoked prior to calling this routine.  The sqlite3BtreeCommitPhaseOne()
50147
 
** routine did all the work of writing information out to disk and flushing the
50148
 
** contents so that they are written onto the disk platter.  All this
50149
 
** routine has to do is delete or truncate or zero the header in the
50150
 
** the rollback journal (which causes the transaction to commit) and
50151
 
** drop locks.
50152
 
**
50153
 
** Normally, if an error occurs while the pager layer is attempting to 
50154
 
** finalize the underlying journal file, this function returns an error and
50155
 
** the upper layer will attempt a rollback. However, if the second argument
50156
 
** is non-zero then this b-tree transaction is part of a multi-file 
50157
 
** transaction. In this case, the transaction has already been committed 
50158
 
** (by deleting a master journal file) and the caller will ignore this 
50159
 
** functions return code. So, even if an error occurs in the pager layer,
50160
 
** reset the b-tree objects internal state to indicate that the write
50161
 
** transaction has been closed. This is quite safe, as the pager will have
50162
 
** transitioned to the error state.
50163
 
**
50164
 
** This will release the write lock on the database file.  If there
50165
 
** are no active cursors, it also releases the read lock.
50166
 
*/
50167
 
SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
50168
 
 
50169
 
  if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
50170
 
  sqlite3BtreeEnter(p);
50171
 
  btreeIntegrity(p);
50172
 
 
50173
 
  /* If the handle has a write-transaction open, commit the shared-btrees 
50174
 
  ** transaction and set the shared state to TRANS_READ.
50175
 
  */
50176
 
  if( p->inTrans==TRANS_WRITE ){
50177
 
    int rc;
50178
 
    BtShared *pBt = p->pBt;
50179
 
    assert( pBt->inTransaction==TRANS_WRITE );
50180
 
    assert( pBt->nTransaction>0 );
50181
 
    rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
50182
 
    if( rc!=SQLITE_OK && bCleanup==0 ){
50183
 
      sqlite3BtreeLeave(p);
50184
 
      return rc;
50185
 
    }
50186
 
    pBt->inTransaction = TRANS_READ;
50187
 
  }
50188
 
 
50189
 
  btreeEndTransaction(p);
50190
 
  sqlite3BtreeLeave(p);
50191
 
  return SQLITE_OK;
50192
 
}
50193
 
 
50194
 
/*
50195
 
** Do both phases of a commit.
50196
 
*/
50197
 
SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
50198
 
  int rc;
50199
 
  sqlite3BtreeEnter(p);
50200
 
  rc = sqlite3BtreeCommitPhaseOne(p, 0);
50201
 
  if( rc==SQLITE_OK ){
50202
 
    rc = sqlite3BtreeCommitPhaseTwo(p, 0);
50203
 
  }
50204
 
  sqlite3BtreeLeave(p);
50205
 
  return rc;
50206
 
}
50207
 
 
50208
 
#ifndef NDEBUG
50209
 
/*
50210
 
** Return the number of write-cursors open on this handle. This is for use
50211
 
** in assert() expressions, so it is only compiled if NDEBUG is not
50212
 
** defined.
50213
 
**
50214
 
** For the purposes of this routine, a write-cursor is any cursor that
50215
 
** is capable of writing to the databse.  That means the cursor was
50216
 
** originally opened for writing and the cursor has not be disabled
50217
 
** by having its state changed to CURSOR_FAULT.
50218
 
*/
50219
 
static int countWriteCursors(BtShared *pBt){
50220
 
  BtCursor *pCur;
50221
 
  int r = 0;
50222
 
  for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
50223
 
    if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; 
50224
 
  }
50225
 
  return r;
50226
 
}
50227
 
#endif
50228
 
 
50229
 
/*
50230
 
** This routine sets the state to CURSOR_FAULT and the error
50231
 
** code to errCode for every cursor on BtShared that pBtree
50232
 
** references.
50233
 
**
50234
 
** Every cursor is tripped, including cursors that belong
50235
 
** to other database connections that happen to be sharing
50236
 
** the cache with pBtree.
50237
 
**
50238
 
** This routine gets called when a rollback occurs.
50239
 
** All cursors using the same cache must be tripped
50240
 
** to prevent them from trying to use the btree after
50241
 
** the rollback.  The rollback may have deleted tables
50242
 
** or moved root pages, so it is not sufficient to
50243
 
** save the state of the cursor.  The cursor must be
50244
 
** invalidated.
50245
 
*/
50246
 
SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
50247
 
  BtCursor *p;
50248
 
  sqlite3BtreeEnter(pBtree);
50249
 
  for(p=pBtree->pBt->pCursor; p; p=p->pNext){
50250
 
    int i;
50251
 
    sqlite3BtreeClearCursor(p);
50252
 
    p->eState = CURSOR_FAULT;
50253
 
    p->skipNext = errCode;
50254
 
    for(i=0; i<=p->iPage; i++){
50255
 
      releasePage(p->apPage[i]);
50256
 
      p->apPage[i] = 0;
50257
 
    }
50258
 
  }
50259
 
  sqlite3BtreeLeave(pBtree);
50260
 
}
50261
 
 
50262
 
/*
50263
 
** Rollback the transaction in progress.  All cursors will be
50264
 
** invalided by this operation.  Any attempt to use a cursor
50265
 
** that was open at the beginning of this operation will result
50266
 
** in an error.
50267
 
**
50268
 
** This will release the write lock on the database file.  If there
50269
 
** are no active cursors, it also releases the read lock.
50270
 
*/
50271
 
SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p){
50272
 
  int rc;
50273
 
  BtShared *pBt = p->pBt;
50274
 
  MemPage *pPage1;
50275
 
 
50276
 
  sqlite3BtreeEnter(p);
50277
 
  rc = saveAllCursors(pBt, 0, 0);
50278
 
#ifndef SQLITE_OMIT_SHARED_CACHE
50279
 
  if( rc!=SQLITE_OK ){
50280
 
    /* This is a horrible situation. An IO or malloc() error occurred whilst
50281
 
    ** trying to save cursor positions. If this is an automatic rollback (as
50282
 
    ** the result of a constraint, malloc() failure or IO error) then 
50283
 
    ** the cache may be internally inconsistent (not contain valid trees) so
50284
 
    ** we cannot simply return the error to the caller. Instead, abort 
50285
 
    ** all queries that may be using any of the cursors that failed to save.
50286
 
    */
50287
 
    sqlite3BtreeTripAllCursors(p, rc);
50288
 
  }
50289
 
#endif
50290
 
  btreeIntegrity(p);
50291
 
 
50292
 
  if( p->inTrans==TRANS_WRITE ){
50293
 
    int rc2;
50294
 
 
50295
 
    assert( TRANS_WRITE==pBt->inTransaction );
50296
 
    rc2 = sqlite3PagerRollback(pBt->pPager);
50297
 
    if( rc2!=SQLITE_OK ){
50298
 
      rc = rc2;
50299
 
    }
50300
 
 
50301
 
    /* The rollback may have destroyed the pPage1->aData value.  So
50302
 
    ** call btreeGetPage() on page 1 again to make
50303
 
    ** sure pPage1->aData is set correctly. */
50304
 
    if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
50305
 
      int nPage = get4byte(28+(u8*)pPage1->aData);
50306
 
      testcase( nPage==0 );
50307
 
      if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
50308
 
      testcase( pBt->nPage!=nPage );
50309
 
      pBt->nPage = nPage;
50310
 
      releasePage(pPage1);
50311
 
    }
50312
 
    assert( countWriteCursors(pBt)==0 );
50313
 
    pBt->inTransaction = TRANS_READ;
50314
 
  }
50315
 
 
50316
 
  btreeEndTransaction(p);
50317
 
  sqlite3BtreeLeave(p);
50318
 
  return rc;
50319
 
}
50320
 
 
50321
 
/*
50322
 
** Start a statement subtransaction. The subtransaction can can be rolled
50323
 
** back independently of the main transaction. You must start a transaction 
50324
 
** before starting a subtransaction. The subtransaction is ended automatically 
50325
 
** if the main transaction commits or rolls back.
50326
 
**
50327
 
** Statement subtransactions are used around individual SQL statements
50328
 
** that are contained within a BEGIN...COMMIT block.  If a constraint
50329
 
** error occurs within the statement, the effect of that one statement
50330
 
** can be rolled back without having to rollback the entire transaction.
50331
 
**
50332
 
** A statement sub-transaction is implemented as an anonymous savepoint. The
50333
 
** value passed as the second parameter is the total number of savepoints,
50334
 
** including the new anonymous savepoint, open on the B-Tree. i.e. if there
50335
 
** are no active savepoints and no other statement-transactions open,
50336
 
** iStatement is 1. This anonymous savepoint can be released or rolled back
50337
 
** using the sqlite3BtreeSavepoint() function.
50338
 
*/
50339
 
SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
50340
 
  int rc;
50341
 
  BtShared *pBt = p->pBt;
50342
 
  sqlite3BtreeEnter(p);
50343
 
  assert( p->inTrans==TRANS_WRITE );
50344
 
  assert( pBt->readOnly==0 );
50345
 
  assert( iStatement>0 );
50346
 
  assert( iStatement>p->db->nSavepoint );
50347
 
  assert( pBt->inTransaction==TRANS_WRITE );
50348
 
  /* At the pager level, a statement transaction is a savepoint with
50349
 
  ** an index greater than all savepoints created explicitly using
50350
 
  ** SQL statements. It is illegal to open, release or rollback any
50351
 
  ** such savepoints while the statement transaction savepoint is active.
50352
 
  */
50353
 
  rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
50354
 
  sqlite3BtreeLeave(p);
50355
 
  return rc;
50356
 
}
50357
 
 
50358
 
/*
50359
 
** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
50360
 
** or SAVEPOINT_RELEASE. This function either releases or rolls back the
50361
 
** savepoint identified by parameter iSavepoint, depending on the value 
50362
 
** of op.
50363
 
**
50364
 
** Normally, iSavepoint is greater than or equal to zero. However, if op is
50365
 
** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the 
50366
 
** contents of the entire transaction are rolled back. This is different
50367
 
** from a normal transaction rollback, as no locks are released and the
50368
 
** transaction remains open.
50369
 
*/
50370
 
SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
50371
 
  int rc = SQLITE_OK;
50372
 
  if( p && p->inTrans==TRANS_WRITE ){
50373
 
    BtShared *pBt = p->pBt;
50374
 
    assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
50375
 
    assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
50376
 
    sqlite3BtreeEnter(p);
50377
 
    rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
50378
 
    if( rc==SQLITE_OK ){
50379
 
      if( iSavepoint<0 && pBt->initiallyEmpty ) pBt->nPage = 0;
50380
 
      rc = newDatabase(pBt);
50381
 
      pBt->nPage = get4byte(28 + pBt->pPage1->aData);
50382
 
 
50383
 
      /* The database size was written into the offset 28 of the header
50384
 
      ** when the transaction started, so we know that the value at offset
50385
 
      ** 28 is nonzero. */
50386
 
      assert( pBt->nPage>0 );
50387
 
    }
50388
 
    sqlite3BtreeLeave(p);
50389
 
  }
50390
 
  return rc;
50391
 
}
50392
 
 
50393
 
/*
50394
 
** Create a new cursor for the BTree whose root is on the page
50395
 
** iTable. If a read-only cursor is requested, it is assumed that
50396
 
** the caller already has at least a read-only transaction open
50397
 
** on the database already. If a write-cursor is requested, then
50398
 
** the caller is assumed to have an open write transaction.
50399
 
**
50400
 
** If wrFlag==0, then the cursor can only be used for reading.
50401
 
** If wrFlag==1, then the cursor can be used for reading or for
50402
 
** writing if other conditions for writing are also met.  These
50403
 
** are the conditions that must be met in order for writing to
50404
 
** be allowed:
50405
 
**
50406
 
** 1:  The cursor must have been opened with wrFlag==1
50407
 
**
50408
 
** 2:  Other database connections that share the same pager cache
50409
 
**     but which are not in the READ_UNCOMMITTED state may not have
50410
 
**     cursors open with wrFlag==0 on the same table.  Otherwise
50411
 
**     the changes made by this write cursor would be visible to
50412
 
**     the read cursors in the other database connection.
50413
 
**
50414
 
** 3:  The database must be writable (not on read-only media)
50415
 
**
50416
 
** 4:  There must be an active transaction.
50417
 
**
50418
 
** No checking is done to make sure that page iTable really is the
50419
 
** root page of a b-tree.  If it is not, then the cursor acquired
50420
 
** will not work correctly.
50421
 
**
50422
 
** It is assumed that the sqlite3BtreeCursorZero() has been called
50423
 
** on pCur to initialize the memory space prior to invoking this routine.
50424
 
*/
50425
 
static int btreeCursor(
50426
 
  Btree *p,                              /* The btree */
50427
 
  int iTable,                            /* Root page of table to open */
50428
 
  int wrFlag,                            /* 1 to write. 0 read-only */
50429
 
  struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
50430
 
  BtCursor *pCur                         /* Space for new cursor */
50431
 
){
50432
 
  BtShared *pBt = p->pBt;                /* Shared b-tree handle */
50433
 
 
50434
 
  assert( sqlite3BtreeHoldsMutex(p) );
50435
 
  assert( wrFlag==0 || wrFlag==1 );
50436
 
 
50437
 
  /* The following assert statements verify that if this is a sharable 
50438
 
  ** b-tree database, the connection is holding the required table locks, 
50439
 
  ** and that no other connection has any open cursor that conflicts with 
50440
 
  ** this lock.  */
50441
 
  assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) );
50442
 
  assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
50443
 
 
50444
 
  /* Assert that the caller has opened the required transaction. */
50445
 
  assert( p->inTrans>TRANS_NONE );
50446
 
  assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
50447
 
  assert( pBt->pPage1 && pBt->pPage1->aData );
50448
 
 
50449
 
  if( NEVER(wrFlag && pBt->readOnly) ){
50450
 
    return SQLITE_READONLY;
50451
 
  }
50452
 
  if( iTable==1 && btreePagecount(pBt)==0 ){
50453
 
    return SQLITE_EMPTY;
50454
 
  }
50455
 
 
50456
 
  /* Now that no other errors can occur, finish filling in the BtCursor
50457
 
  ** variables and link the cursor into the BtShared list.  */
50458
 
  pCur->pgnoRoot = (Pgno)iTable;
50459
 
  pCur->iPage = -1;
50460
 
  pCur->pKeyInfo = pKeyInfo;
50461
 
  pCur->pBtree = p;
50462
 
  pCur->pBt = pBt;
50463
 
  pCur->wrFlag = (u8)wrFlag;
50464
 
  pCur->pNext = pBt->pCursor;
50465
 
  if( pCur->pNext ){
50466
 
    pCur->pNext->pPrev = pCur;
50467
 
  }
50468
 
  pBt->pCursor = pCur;
50469
 
  pCur->eState = CURSOR_INVALID;
50470
 
  pCur->cachedRowid = 0;
50471
 
  return SQLITE_OK;
50472
 
}
50473
 
SQLITE_PRIVATE int sqlite3BtreeCursor(
50474
 
  Btree *p,                                   /* The btree */
50475
 
  int iTable,                                 /* Root page of table to open */
50476
 
  int wrFlag,                                 /* 1 to write. 0 read-only */
50477
 
  struct KeyInfo *pKeyInfo,                   /* First arg to xCompare() */
50478
 
  BtCursor *pCur                              /* Write new cursor here */
50479
 
){
50480
 
  int rc;
50481
 
  sqlite3BtreeEnter(p);
50482
 
  rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
50483
 
  sqlite3BtreeLeave(p);
50484
 
  return rc;
50485
 
}
50486
 
 
50487
 
/*
50488
 
** Return the size of a BtCursor object in bytes.
50489
 
**
50490
 
** This interfaces is needed so that users of cursors can preallocate
50491
 
** sufficient storage to hold a cursor.  The BtCursor object is opaque
50492
 
** to users so they cannot do the sizeof() themselves - they must call
50493
 
** this routine.
50494
 
*/
50495
 
SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
50496
 
  return ROUND8(sizeof(BtCursor));
50497
 
}
50498
 
 
50499
 
/*
50500
 
** Initialize memory that will be converted into a BtCursor object.
50501
 
**
50502
 
** The simple approach here would be to memset() the entire object
50503
 
** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
50504
 
** do not need to be zeroed and they are large, so we can save a lot
50505
 
** of run-time by skipping the initialization of those elements.
50506
 
*/
50507
 
SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
50508
 
  memset(p, 0, offsetof(BtCursor, iPage));
50509
 
}
50510
 
 
50511
 
/*
50512
 
** Set the cached rowid value of every cursor in the same database file
50513
 
** as pCur and having the same root page number as pCur.  The value is
50514
 
** set to iRowid.
50515
 
**
50516
 
** Only positive rowid values are considered valid for this cache.
50517
 
** The cache is initialized to zero, indicating an invalid cache.
50518
 
** A btree will work fine with zero or negative rowids.  We just cannot
50519
 
** cache zero or negative rowids, which means tables that use zero or
50520
 
** negative rowids might run a little slower.  But in practice, zero
50521
 
** or negative rowids are very uncommon so this should not be a problem.
50522
 
*/
50523
 
SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor *pCur, sqlite3_int64 iRowid){
50524
 
  BtCursor *p;
50525
 
  for(p=pCur->pBt->pCursor; p; p=p->pNext){
50526
 
    if( p->pgnoRoot==pCur->pgnoRoot ) p->cachedRowid = iRowid;
50527
 
  }
50528
 
  assert( pCur->cachedRowid==iRowid );
50529
 
}
50530
 
 
50531
 
/*
50532
 
** Return the cached rowid for the given cursor.  A negative or zero
50533
 
** return value indicates that the rowid cache is invalid and should be
50534
 
** ignored.  If the rowid cache has never before been set, then a
50535
 
** zero is returned.
50536
 
*/
50537
 
SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor *pCur){
50538
 
  return pCur->cachedRowid;
50539
 
}
50540
 
 
50541
 
/*
50542
 
** Close a cursor.  The read lock on the database file is released
50543
 
** when the last cursor is closed.
50544
 
*/
50545
 
SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
50546
 
  Btree *pBtree = pCur->pBtree;
50547
 
  if( pBtree ){
50548
 
    int i;
50549
 
    BtShared *pBt = pCur->pBt;
50550
 
    sqlite3BtreeEnter(pBtree);
50551
 
    sqlite3BtreeClearCursor(pCur);
50552
 
    if( pCur->pPrev ){
50553
 
      pCur->pPrev->pNext = pCur->pNext;
50554
 
    }else{
50555
 
      pBt->pCursor = pCur->pNext;
50556
 
    }
50557
 
    if( pCur->pNext ){
50558
 
      pCur->pNext->pPrev = pCur->pPrev;
50559
 
    }
50560
 
    for(i=0; i<=pCur->iPage; i++){
50561
 
      releasePage(pCur->apPage[i]);
50562
 
    }
50563
 
    unlockBtreeIfUnused(pBt);
50564
 
    invalidateOverflowCache(pCur);
50565
 
    /* sqlite3_free(pCur); */
50566
 
    sqlite3BtreeLeave(pBtree);
50567
 
  }
50568
 
  return SQLITE_OK;
50569
 
}
50570
 
 
50571
 
/*
50572
 
** Make sure the BtCursor* given in the argument has a valid
50573
 
** BtCursor.info structure.  If it is not already valid, call
50574
 
** btreeParseCell() to fill it in.
50575
 
**
50576
 
** BtCursor.info is a cache of the information in the current cell.
50577
 
** Using this cache reduces the number of calls to btreeParseCell().
50578
 
**
50579
 
** 2007-06-25:  There is a bug in some versions of MSVC that cause the
50580
 
** compiler to crash when getCellInfo() is implemented as a macro.
50581
 
** But there is a measureable speed advantage to using the macro on gcc
50582
 
** (when less compiler optimizations like -Os or -O0 are used and the
50583
 
** compiler is not doing agressive inlining.)  So we use a real function
50584
 
** for MSVC and a macro for everything else.  Ticket #2457.
50585
 
*/
50586
 
#ifndef NDEBUG
50587
 
  static void assertCellInfo(BtCursor *pCur){
50588
 
    CellInfo info;
50589
 
    int iPage = pCur->iPage;
50590
 
    memset(&info, 0, sizeof(info));
50591
 
    btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
50592
 
    assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
50593
 
  }
50594
 
#else
50595
 
  #define assertCellInfo(x)
50596
 
#endif
50597
 
#ifdef _MSC_VER
50598
 
  /* Use a real function in MSVC to work around bugs in that compiler. */
50599
 
  static void getCellInfo(BtCursor *pCur){
50600
 
    if( pCur->info.nSize==0 ){
50601
 
      int iPage = pCur->iPage;
50602
 
      btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
50603
 
      pCur->validNKey = 1;
50604
 
    }else{
50605
 
      assertCellInfo(pCur);
50606
 
    }
50607
 
  }
50608
 
#else /* if not _MSC_VER */
50609
 
  /* Use a macro in all other compilers so that the function is inlined */
50610
 
#define getCellInfo(pCur)                                                      \
50611
 
  if( pCur->info.nSize==0 ){                                                   \
50612
 
    int iPage = pCur->iPage;                                                   \
50613
 
    btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
50614
 
    pCur->validNKey = 1;                                                       \
50615
 
  }else{                                                                       \
50616
 
    assertCellInfo(pCur);                                                      \
50617
 
  }
50618
 
#endif /* _MSC_VER */
50619
 
 
50620
 
#ifndef NDEBUG  /* The next routine used only within assert() statements */
50621
 
/*
50622
 
** Return true if the given BtCursor is valid.  A valid cursor is one
50623
 
** that is currently pointing to a row in a (non-empty) table.
50624
 
** This is a verification routine is used only within assert() statements.
50625
 
*/
50626
 
SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
50627
 
  return pCur && pCur->eState==CURSOR_VALID;
50628
 
}
50629
 
#endif /* NDEBUG */
50630
 
 
50631
 
/*
50632
 
** Set *pSize to the size of the buffer needed to hold the value of
50633
 
** the key for the current entry.  If the cursor is not pointing
50634
 
** to a valid entry, *pSize is set to 0. 
50635
 
**
50636
 
** For a table with the INTKEY flag set, this routine returns the key
50637
 
** itself, not the number of bytes in the key.
50638
 
**
50639
 
** The caller must position the cursor prior to invoking this routine.
50640
 
** 
50641
 
** This routine cannot fail.  It always returns SQLITE_OK.  
50642
 
*/
50643
 
SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
50644
 
  assert( cursorHoldsMutex(pCur) );
50645
 
  assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
50646
 
  if( pCur->eState!=CURSOR_VALID ){
50647
 
    *pSize = 0;
50648
 
  }else{
50649
 
    getCellInfo(pCur);
50650
 
    *pSize = pCur->info.nKey;
50651
 
  }
50652
 
  return SQLITE_OK;
50653
 
}
50654
 
 
50655
 
/*
50656
 
** Set *pSize to the number of bytes of data in the entry the
50657
 
** cursor currently points to.
50658
 
**
50659
 
** The caller must guarantee that the cursor is pointing to a non-NULL
50660
 
** valid entry.  In other words, the calling procedure must guarantee
50661
 
** that the cursor has Cursor.eState==CURSOR_VALID.
50662
 
**
50663
 
** Failure is not possible.  This function always returns SQLITE_OK.
50664
 
** It might just as well be a procedure (returning void) but we continue
50665
 
** to return an integer result code for historical reasons.
50666
 
*/
50667
 
SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
50668
 
  assert( cursorHoldsMutex(pCur) );
50669
 
  assert( pCur->eState==CURSOR_VALID );
50670
 
  getCellInfo(pCur);
50671
 
  *pSize = pCur->info.nData;
50672
 
  return SQLITE_OK;
50673
 
}
50674
 
 
50675
 
/*
50676
 
** Given the page number of an overflow page in the database (parameter
50677
 
** ovfl), this function finds the page number of the next page in the 
50678
 
** linked list of overflow pages. If possible, it uses the auto-vacuum
50679
 
** pointer-map data instead of reading the content of page ovfl to do so. 
50680
 
**
50681
 
** If an error occurs an SQLite error code is returned. Otherwise:
50682
 
**
50683
 
** The page number of the next overflow page in the linked list is 
50684
 
** written to *pPgnoNext. If page ovfl is the last page in its linked 
50685
 
** list, *pPgnoNext is set to zero. 
50686
 
**
50687
 
** If ppPage is not NULL, and a reference to the MemPage object corresponding
50688
 
** to page number pOvfl was obtained, then *ppPage is set to point to that
50689
 
** reference. It is the responsibility of the caller to call releasePage()
50690
 
** on *ppPage to free the reference. In no reference was obtained (because
50691
 
** the pointer-map was used to obtain the value for *pPgnoNext), then
50692
 
** *ppPage is set to zero.
50693
 
*/
50694
 
static int getOverflowPage(
50695
 
  BtShared *pBt,               /* The database file */
50696
 
  Pgno ovfl,                   /* Current overflow page number */
50697
 
  MemPage **ppPage,            /* OUT: MemPage handle (may be NULL) */
50698
 
  Pgno *pPgnoNext              /* OUT: Next overflow page number */
50699
 
){
50700
 
  Pgno next = 0;
50701
 
  MemPage *pPage = 0;
50702
 
  int rc = SQLITE_OK;
50703
 
 
50704
 
  assert( sqlite3_mutex_held(pBt->mutex) );
50705
 
  assert(pPgnoNext);
50706
 
 
50707
 
#ifndef SQLITE_OMIT_AUTOVACUUM
50708
 
  /* Try to find the next page in the overflow list using the
50709
 
  ** autovacuum pointer-map pages. Guess that the next page in 
50710
 
  ** the overflow list is page number (ovfl+1). If that guess turns 
50711
 
  ** out to be wrong, fall back to loading the data of page 
50712
 
  ** number ovfl to determine the next page number.
50713
 
  */
50714
 
  if( pBt->autoVacuum ){
50715
 
    Pgno pgno;
50716
 
    Pgno iGuess = ovfl+1;
50717
 
    u8 eType;
50718
 
 
50719
 
    while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
50720
 
      iGuess++;
50721
 
    }
50722
 
 
50723
 
    if( iGuess<=btreePagecount(pBt) ){
50724
 
      rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
50725
 
      if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
50726
 
        next = iGuess;
50727
 
        rc = SQLITE_DONE;
50728
 
      }
50729
 
    }
50730
 
  }
50731
 
#endif
50732
 
 
50733
 
  assert( next==0 || rc==SQLITE_DONE );
50734
 
  if( rc==SQLITE_OK ){
50735
 
    rc = btreeGetPage(pBt, ovfl, &pPage, 0);
50736
 
    assert( rc==SQLITE_OK || pPage==0 );
50737
 
    if( rc==SQLITE_OK ){
50738
 
      next = get4byte(pPage->aData);
50739
 
    }
50740
 
  }
50741
 
 
50742
 
  *pPgnoNext = next;
50743
 
  if( ppPage ){
50744
 
    *ppPage = pPage;
50745
 
  }else{
50746
 
    releasePage(pPage);
50747
 
  }
50748
 
  return (rc==SQLITE_DONE ? SQLITE_OK : rc);
50749
 
}
50750
 
 
50751
 
/*
50752
 
** Copy data from a buffer to a page, or from a page to a buffer.
50753
 
**
50754
 
** pPayload is a pointer to data stored on database page pDbPage.
50755
 
** If argument eOp is false, then nByte bytes of data are copied
50756
 
** from pPayload to the buffer pointed at by pBuf. If eOp is true,
50757
 
** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
50758
 
** of data are copied from the buffer pBuf to pPayload.
50759
 
**
50760
 
** SQLITE_OK is returned on success, otherwise an error code.
50761
 
*/
50762
 
static int copyPayload(
50763
 
  void *pPayload,           /* Pointer to page data */
50764
 
  void *pBuf,               /* Pointer to buffer */
50765
 
  int nByte,                /* Number of bytes to copy */
50766
 
  int eOp,                  /* 0 -> copy from page, 1 -> copy to page */
50767
 
  DbPage *pDbPage           /* Page containing pPayload */
50768
 
){
50769
 
  if( eOp ){
50770
 
    /* Copy data from buffer to page (a write operation) */
50771
 
    int rc = sqlite3PagerWrite(pDbPage);
50772
 
    if( rc!=SQLITE_OK ){
50773
 
      return rc;
50774
 
    }
50775
 
    memcpy(pPayload, pBuf, nByte);
50776
 
  }else{
50777
 
    /* Copy data from page to buffer (a read operation) */
50778
 
    memcpy(pBuf, pPayload, nByte);
50779
 
  }
50780
 
  return SQLITE_OK;
50781
 
}
50782
 
 
50783
 
/*
50784
 
** This function is used to read or overwrite payload information
50785
 
** for the entry that the pCur cursor is pointing to. If the eOp
50786
 
** parameter is 0, this is a read operation (data copied into
50787
 
** buffer pBuf). If it is non-zero, a write (data copied from
50788
 
** buffer pBuf).
50789
 
**
50790
 
** A total of "amt" bytes are read or written beginning at "offset".
50791
 
** Data is read to or from the buffer pBuf.
50792
 
**
50793
 
** The content being read or written might appear on the main page
50794
 
** or be scattered out on multiple overflow pages.
50795
 
**
50796
 
** If the BtCursor.isIncrblobHandle flag is set, and the current
50797
 
** cursor entry uses one or more overflow pages, this function
50798
 
** allocates space for and lazily popluates the overflow page-list 
50799
 
** cache array (BtCursor.aOverflow). Subsequent calls use this
50800
 
** cache to make seeking to the supplied offset more efficient.
50801
 
**
50802
 
** Once an overflow page-list cache has been allocated, it may be
50803
 
** invalidated if some other cursor writes to the same table, or if
50804
 
** the cursor is moved to a different row. Additionally, in auto-vacuum
50805
 
** mode, the following events may invalidate an overflow page-list cache.
50806
 
**
50807
 
**   * An incremental vacuum,
50808
 
**   * A commit in auto_vacuum="full" mode,
50809
 
**   * Creating a table (may require moving an overflow page).
50810
 
*/
50811
 
static int accessPayload(
50812
 
  BtCursor *pCur,      /* Cursor pointing to entry to read from */
50813
 
  u32 offset,          /* Begin reading this far into payload */
50814
 
  u32 amt,             /* Read this many bytes */
50815
 
  unsigned char *pBuf, /* Write the bytes into this buffer */ 
50816
 
  int eOp              /* zero to read. non-zero to write. */
50817
 
){
50818
 
  unsigned char *aPayload;
50819
 
  int rc = SQLITE_OK;
50820
 
  u32 nKey;
50821
 
  int iIdx = 0;
50822
 
  MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
50823
 
  BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
50824
 
 
50825
 
  assert( pPage );
50826
 
  assert( pCur->eState==CURSOR_VALID );
50827
 
  assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
50828
 
  assert( cursorHoldsMutex(pCur) );
50829
 
 
50830
 
  getCellInfo(pCur);
50831
 
  aPayload = pCur->info.pCell + pCur->info.nHeader;
50832
 
  nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
50833
 
 
50834
 
  if( NEVER(offset+amt > nKey+pCur->info.nData) 
50835
 
   || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
50836
 
  ){
50837
 
    /* Trying to read or write past the end of the data is an error */
50838
 
    return SQLITE_CORRUPT_BKPT;
50839
 
  }
50840
 
 
50841
 
  /* Check if data must be read/written to/from the btree page itself. */
50842
 
  if( offset<pCur->info.nLocal ){
50843
 
    int a = amt;
50844
 
    if( a+offset>pCur->info.nLocal ){
50845
 
      a = pCur->info.nLocal - offset;
50846
 
    }
50847
 
    rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
50848
 
    offset = 0;
50849
 
    pBuf += a;
50850
 
    amt -= a;
50851
 
  }else{
50852
 
    offset -= pCur->info.nLocal;
50853
 
  }
50854
 
 
50855
 
  if( rc==SQLITE_OK && amt>0 ){
50856
 
    const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
50857
 
    Pgno nextPage;
50858
 
 
50859
 
    nextPage = get4byte(&aPayload[pCur->info.nLocal]);
50860
 
 
50861
 
#ifndef SQLITE_OMIT_INCRBLOB
50862
 
    /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
50863
 
    ** has not been allocated, allocate it now. The array is sized at
50864
 
    ** one entry for each overflow page in the overflow chain. The
50865
 
    ** page number of the first overflow page is stored in aOverflow[0],
50866
 
    ** etc. A value of 0 in the aOverflow[] array means "not yet known"
50867
 
    ** (the cache is lazily populated).
50868
 
    */
50869
 
    if( pCur->isIncrblobHandle && !pCur->aOverflow ){
50870
 
      int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
50871
 
      pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
50872
 
      /* nOvfl is always positive.  If it were zero, fetchPayload would have
50873
 
      ** been used instead of this routine. */
50874
 
      if( ALWAYS(nOvfl) && !pCur->aOverflow ){
50875
 
        rc = SQLITE_NOMEM;
50876
 
      }
50877
 
    }
50878
 
 
50879
 
    /* If the overflow page-list cache has been allocated and the
50880
 
    ** entry for the first required overflow page is valid, skip
50881
 
    ** directly to it.
50882
 
    */
50883
 
    if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
50884
 
      iIdx = (offset/ovflSize);
50885
 
      nextPage = pCur->aOverflow[iIdx];
50886
 
      offset = (offset%ovflSize);
50887
 
    }
50888
 
#endif
50889
 
 
50890
 
    for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
50891
 
 
50892
 
#ifndef SQLITE_OMIT_INCRBLOB
50893
 
      /* If required, populate the overflow page-list cache. */
50894
 
      if( pCur->aOverflow ){
50895
 
        assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
50896
 
        pCur->aOverflow[iIdx] = nextPage;
50897
 
      }
50898
 
#endif
50899
 
 
50900
 
      if( offset>=ovflSize ){
50901
 
        /* The only reason to read this page is to obtain the page
50902
 
        ** number for the next page in the overflow chain. The page
50903
 
        ** data is not required. So first try to lookup the overflow
50904
 
        ** page-list cache, if any, then fall back to the getOverflowPage()
50905
 
        ** function.
50906
 
        */
50907
 
#ifndef SQLITE_OMIT_INCRBLOB
50908
 
        if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
50909
 
          nextPage = pCur->aOverflow[iIdx+1];
50910
 
        } else 
50911
 
#endif
50912
 
          rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
50913
 
        offset -= ovflSize;
50914
 
      }else{
50915
 
        /* Need to read this page properly. It contains some of the
50916
 
        ** range of data that is being read (eOp==0) or written (eOp!=0).
50917
 
        */
50918
 
        DbPage *pDbPage;
50919
 
        int a = amt;
50920
 
        rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
50921
 
        if( rc==SQLITE_OK ){
50922
 
          aPayload = sqlite3PagerGetData(pDbPage);
50923
 
          nextPage = get4byte(aPayload);
50924
 
          if( a + offset > ovflSize ){
50925
 
            a = ovflSize - offset;
50926
 
          }
50927
 
          rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
50928
 
          sqlite3PagerUnref(pDbPage);
50929
 
          offset = 0;
50930
 
          amt -= a;
50931
 
          pBuf += a;
50932
 
        }
50933
 
      }
50934
 
    }
50935
 
  }
50936
 
 
50937
 
  if( rc==SQLITE_OK && amt>0 ){
50938
 
    return SQLITE_CORRUPT_BKPT;
50939
 
  }
50940
 
  return rc;
50941
 
}
50942
 
 
50943
 
/*
50944
 
** Read part of the key associated with cursor pCur.  Exactly
50945
 
** "amt" bytes will be transfered into pBuf[].  The transfer
50946
 
** begins at "offset".
50947
 
**
50948
 
** The caller must ensure that pCur is pointing to a valid row
50949
 
** in the table.
50950
 
**
50951
 
** Return SQLITE_OK on success or an error code if anything goes
50952
 
** wrong.  An error is returned if "offset+amt" is larger than
50953
 
** the available payload.
50954
 
*/
50955
 
SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
50956
 
  assert( cursorHoldsMutex(pCur) );
50957
 
  assert( pCur->eState==CURSOR_VALID );
50958
 
  assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
50959
 
  assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
50960
 
  return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
50961
 
}
50962
 
 
50963
 
/*
50964
 
** Read part of the data associated with cursor pCur.  Exactly
50965
 
** "amt" bytes will be transfered into pBuf[].  The transfer
50966
 
** begins at "offset".
50967
 
**
50968
 
** Return SQLITE_OK on success or an error code if anything goes
50969
 
** wrong.  An error is returned if "offset+amt" is larger than
50970
 
** the available payload.
50971
 
*/
50972
 
SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
50973
 
  int rc;
50974
 
 
50975
 
#ifndef SQLITE_OMIT_INCRBLOB
50976
 
  if ( pCur->eState==CURSOR_INVALID ){
50977
 
    return SQLITE_ABORT;
50978
 
  }
50979
 
#endif
50980
 
 
50981
 
  assert( cursorHoldsMutex(pCur) );
50982
 
  rc = restoreCursorPosition(pCur);
50983
 
  if( rc==SQLITE_OK ){
50984
 
    assert( pCur->eState==CURSOR_VALID );
50985
 
    assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
50986
 
    assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
50987
 
    rc = accessPayload(pCur, offset, amt, pBuf, 0);
50988
 
  }
50989
 
  return rc;
50990
 
}
50991
 
 
50992
 
/*
50993
 
** Return a pointer to payload information from the entry that the 
50994
 
** pCur cursor is pointing to.  The pointer is to the beginning of
50995
 
** the key if skipKey==0 and it points to the beginning of data if
50996
 
** skipKey==1.  The number of bytes of available key/data is written
50997
 
** into *pAmt.  If *pAmt==0, then the value returned will not be
50998
 
** a valid pointer.
50999
 
**
51000
 
** This routine is an optimization.  It is common for the entire key
51001
 
** and data to fit on the local page and for there to be no overflow
51002
 
** pages.  When that is so, this routine can be used to access the
51003
 
** key and data without making a copy.  If the key and/or data spills
51004
 
** onto overflow pages, then accessPayload() must be used to reassemble
51005
 
** the key/data and copy it into a preallocated buffer.
51006
 
**
51007
 
** The pointer returned by this routine looks directly into the cached
51008
 
** page of the database.  The data might change or move the next time
51009
 
** any btree routine is called.
51010
 
*/
51011
 
static const unsigned char *fetchPayload(
51012
 
  BtCursor *pCur,      /* Cursor pointing to entry to read from */
51013
 
  int *pAmt,           /* Write the number of available bytes here */
51014
 
  int skipKey          /* read beginning at data if this is true */
51015
 
){
51016
 
  unsigned char *aPayload;
51017
 
  MemPage *pPage;
51018
 
  u32 nKey;
51019
 
  u32 nLocal;
51020
 
 
51021
 
  assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
51022
 
  assert( pCur->eState==CURSOR_VALID );
51023
 
  assert( cursorHoldsMutex(pCur) );
51024
 
  pPage = pCur->apPage[pCur->iPage];
51025
 
  assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
51026
 
  if( NEVER(pCur->info.nSize==0) ){
51027
 
    btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
51028
 
                   &pCur->info);
51029
 
  }
51030
 
  aPayload = pCur->info.pCell;
51031
 
  aPayload += pCur->info.nHeader;
51032
 
  if( pPage->intKey ){
51033
 
    nKey = 0;
51034
 
  }else{
51035
 
    nKey = (int)pCur->info.nKey;
51036
 
  }
51037
 
  if( skipKey ){
51038
 
    aPayload += nKey;
51039
 
    nLocal = pCur->info.nLocal - nKey;
51040
 
  }else{
51041
 
    nLocal = pCur->info.nLocal;
51042
 
    assert( nLocal<=nKey );
51043
 
  }
51044
 
  *pAmt = nLocal;
51045
 
  return aPayload;
51046
 
}
51047
 
 
51048
 
 
51049
 
/*
51050
 
** For the entry that cursor pCur is point to, return as
51051
 
** many bytes of the key or data as are available on the local
51052
 
** b-tree page.  Write the number of available bytes into *pAmt.
51053
 
**
51054
 
** The pointer returned is ephemeral.  The key/data may move
51055
 
** or be destroyed on the next call to any Btree routine,
51056
 
** including calls from other threads against the same cache.
51057
 
** Hence, a mutex on the BtShared should be held prior to calling
51058
 
** this routine.
51059
 
**
51060
 
** These routines is used to get quick access to key and data
51061
 
** in the common case where no overflow pages are used.
51062
 
*/
51063
 
SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
51064
 
  const void *p = 0;
51065
 
  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
51066
 
  assert( cursorHoldsMutex(pCur) );
51067
 
  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
51068
 
    p = (const void*)fetchPayload(pCur, pAmt, 0);
51069
 
  }
51070
 
  return p;
51071
 
}
51072
 
SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
51073
 
  const void *p = 0;
51074
 
  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
51075
 
  assert( cursorHoldsMutex(pCur) );
51076
 
  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
51077
 
    p = (const void*)fetchPayload(pCur, pAmt, 1);
51078
 
  }
51079
 
  return p;
51080
 
}
51081
 
 
51082
 
 
51083
 
/*
51084
 
** Move the cursor down to a new child page.  The newPgno argument is the
51085
 
** page number of the child page to move to.
51086
 
**
51087
 
** This function returns SQLITE_CORRUPT if the page-header flags field of
51088
 
** the new child page does not match the flags field of the parent (i.e.
51089
 
** if an intkey page appears to be the parent of a non-intkey page, or
51090
 
** vice-versa).
51091
 
*/
51092
 
static int moveToChild(BtCursor *pCur, u32 newPgno){
51093
 
  int rc;
51094
 
  int i = pCur->iPage;
51095
 
  MemPage *pNewPage;
51096
 
  BtShared *pBt = pCur->pBt;
51097
 
 
51098
 
  assert( cursorHoldsMutex(pCur) );
51099
 
  assert( pCur->eState==CURSOR_VALID );
51100
 
  assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
51101
 
  if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
51102
 
    return SQLITE_CORRUPT_BKPT;
51103
 
  }
51104
 
  rc = getAndInitPage(pBt, newPgno, &pNewPage);
51105
 
  if( rc ) return rc;
51106
 
  pCur->apPage[i+1] = pNewPage;
51107
 
  pCur->aiIdx[i+1] = 0;
51108
 
  pCur->iPage++;
51109
 
 
51110
 
  pCur->info.nSize = 0;
51111
 
  pCur->validNKey = 0;
51112
 
  if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
51113
 
    return SQLITE_CORRUPT_BKPT;
51114
 
  }
51115
 
  return SQLITE_OK;
51116
 
}
51117
 
 
51118
 
#ifndef NDEBUG
51119
 
/*
51120
 
** Page pParent is an internal (non-leaf) tree page. This function 
51121
 
** asserts that page number iChild is the left-child if the iIdx'th
51122
 
** cell in page pParent. Or, if iIdx is equal to the total number of
51123
 
** cells in pParent, that page number iChild is the right-child of
51124
 
** the page.
51125
 
*/
51126
 
static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
51127
 
  assert( iIdx<=pParent->nCell );
51128
 
  if( iIdx==pParent->nCell ){
51129
 
    assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
51130
 
  }else{
51131
 
    assert( get4byte(findCell(pParent, iIdx))==iChild );
51132
 
  }
51133
 
}
51134
 
#else
51135
 
#  define assertParentIndex(x,y,z) 
51136
 
#endif
51137
 
 
51138
 
/*
51139
 
** Move the cursor up to the parent page.
51140
 
**
51141
 
** pCur->idx is set to the cell index that contains the pointer
51142
 
** to the page we are coming from.  If we are coming from the
51143
 
** right-most child page then pCur->idx is set to one more than
51144
 
** the largest cell index.
51145
 
*/
51146
 
static void moveToParent(BtCursor *pCur){
51147
 
  assert( cursorHoldsMutex(pCur) );
51148
 
  assert( pCur->eState==CURSOR_VALID );
51149
 
  assert( pCur->iPage>0 );
51150
 
  assert( pCur->apPage[pCur->iPage] );
51151
 
  assertParentIndex(
51152
 
    pCur->apPage[pCur->iPage-1], 
51153
 
    pCur->aiIdx[pCur->iPage-1], 
51154
 
    pCur->apPage[pCur->iPage]->pgno
51155
 
  );
51156
 
  releasePage(pCur->apPage[pCur->iPage]);
51157
 
  pCur->iPage--;
51158
 
  pCur->info.nSize = 0;
51159
 
  pCur->validNKey = 0;
51160
 
}
51161
 
 
51162
 
/*
51163
 
** Move the cursor to point to the root page of its b-tree structure.
51164
 
**
51165
 
** If the table has a virtual root page, then the cursor is moved to point
51166
 
** to the virtual root page instead of the actual root page. A table has a
51167
 
** virtual root page when the actual root page contains no cells and a 
51168
 
** single child page. This can only happen with the table rooted at page 1.
51169
 
**
51170
 
** If the b-tree structure is empty, the cursor state is set to 
51171
 
** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
51172
 
** cell located on the root (or virtual root) page and the cursor state
51173
 
** is set to CURSOR_VALID.
51174
 
**
51175
 
** If this function returns successfully, it may be assumed that the
51176
 
** page-header flags indicate that the [virtual] root-page is the expected 
51177
 
** kind of b-tree page (i.e. if when opening the cursor the caller did not
51178
 
** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
51179
 
** indicating a table b-tree, or if the caller did specify a KeyInfo 
51180
 
** structure the flags byte is set to 0x02 or 0x0A, indicating an index
51181
 
** b-tree).
51182
 
*/
51183
 
static int moveToRoot(BtCursor *pCur){
51184
 
  MemPage *pRoot;
51185
 
  int rc = SQLITE_OK;
51186
 
  Btree *p = pCur->pBtree;
51187
 
  BtShared *pBt = p->pBt;
51188
 
 
51189
 
  assert( cursorHoldsMutex(pCur) );
51190
 
  assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
51191
 
  assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
51192
 
  assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
51193
 
  if( pCur->eState>=CURSOR_REQUIRESEEK ){
51194
 
    if( pCur->eState==CURSOR_FAULT ){
51195
 
      assert( pCur->skipNext!=SQLITE_OK );
51196
 
      return pCur->skipNext;
51197
 
    }
51198
 
    sqlite3BtreeClearCursor(pCur);
51199
 
  }
51200
 
 
51201
 
  if( pCur->iPage>=0 ){
51202
 
    int i;
51203
 
    for(i=1; i<=pCur->iPage; i++){
51204
 
      releasePage(pCur->apPage[i]);
51205
 
    }
51206
 
    pCur->iPage = 0;
51207
 
  }else{
51208
 
    rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
51209
 
    if( rc!=SQLITE_OK ){
51210
 
      pCur->eState = CURSOR_INVALID;
51211
 
      return rc;
51212
 
    }
51213
 
    pCur->iPage = 0;
51214
 
 
51215
 
    /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
51216
 
    ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
51217
 
    ** NULL, the caller expects a table b-tree. If this is not the case,
51218
 
    ** return an SQLITE_CORRUPT error.  */
51219
 
    assert( pCur->apPage[0]->intKey==1 || pCur->apPage[0]->intKey==0 );
51220
 
    if( (pCur->pKeyInfo==0)!=pCur->apPage[0]->intKey ){
51221
 
      return SQLITE_CORRUPT_BKPT;
51222
 
    }
51223
 
  }
51224
 
 
51225
 
  /* Assert that the root page is of the correct type. This must be the
51226
 
  ** case as the call to this function that loaded the root-page (either
51227
 
  ** this call or a previous invocation) would have detected corruption 
51228
 
  ** if the assumption were not true, and it is not possible for the flags 
51229
 
  ** byte to have been modified while this cursor is holding a reference
51230
 
  ** to the page.  */
51231
 
  pRoot = pCur->apPage[0];
51232
 
  assert( pRoot->pgno==pCur->pgnoRoot );
51233
 
  assert( pRoot->isInit && (pCur->pKeyInfo==0)==pRoot->intKey );
51234
 
 
51235
 
  pCur->aiIdx[0] = 0;
51236
 
  pCur->info.nSize = 0;
51237
 
  pCur->atLast = 0;
51238
 
  pCur->validNKey = 0;
51239
 
 
51240
 
  if( pRoot->nCell==0 && !pRoot->leaf ){
51241
 
    Pgno subpage;
51242
 
    if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
51243
 
    subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
51244
 
    pCur->eState = CURSOR_VALID;
51245
 
    rc = moveToChild(pCur, subpage);
51246
 
  }else{
51247
 
    pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
51248
 
  }
51249
 
  return rc;
51250
 
}
51251
 
 
51252
 
/*
51253
 
** Move the cursor down to the left-most leaf entry beneath the
51254
 
** entry to which it is currently pointing.
51255
 
**
51256
 
** The left-most leaf is the one with the smallest key - the first
51257
 
** in ascending order.
51258
 
*/
51259
 
static int moveToLeftmost(BtCursor *pCur){
51260
 
  Pgno pgno;
51261
 
  int rc = SQLITE_OK;
51262
 
  MemPage *pPage;
51263
 
 
51264
 
  assert( cursorHoldsMutex(pCur) );
51265
 
  assert( pCur->eState==CURSOR_VALID );
51266
 
  while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
51267
 
    assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
51268
 
    pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
51269
 
    rc = moveToChild(pCur, pgno);
51270
 
  }
51271
 
  return rc;
51272
 
}
51273
 
 
51274
 
/*
51275
 
** Move the cursor down to the right-most leaf entry beneath the
51276
 
** page to which it is currently pointing.  Notice the difference
51277
 
** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
51278
 
** finds the left-most entry beneath the *entry* whereas moveToRightmost()
51279
 
** finds the right-most entry beneath the *page*.
51280
 
**
51281
 
** The right-most entry is the one with the largest key - the last
51282
 
** key in ascending order.
51283
 
*/
51284
 
static int moveToRightmost(BtCursor *pCur){
51285
 
  Pgno pgno;
51286
 
  int rc = SQLITE_OK;
51287
 
  MemPage *pPage = 0;
51288
 
 
51289
 
  assert( cursorHoldsMutex(pCur) );
51290
 
  assert( pCur->eState==CURSOR_VALID );
51291
 
  while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
51292
 
    pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
51293
 
    pCur->aiIdx[pCur->iPage] = pPage->nCell;
51294
 
    rc = moveToChild(pCur, pgno);
51295
 
  }
51296
 
  if( rc==SQLITE_OK ){
51297
 
    pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
51298
 
    pCur->info.nSize = 0;
51299
 
    pCur->validNKey = 0;
51300
 
  }
51301
 
  return rc;
51302
 
}
51303
 
 
51304
 
/* Move the cursor to the first entry in the table.  Return SQLITE_OK
51305
 
** on success.  Set *pRes to 0 if the cursor actually points to something
51306
 
** or set *pRes to 1 if the table is empty.
51307
 
*/
51308
 
SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
51309
 
  int rc;
51310
 
 
51311
 
  assert( cursorHoldsMutex(pCur) );
51312
 
  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
51313
 
  rc = moveToRoot(pCur);
51314
 
  if( rc==SQLITE_OK ){
51315
 
    if( pCur->eState==CURSOR_INVALID ){
51316
 
      assert( pCur->apPage[pCur->iPage]->nCell==0 );
51317
 
      *pRes = 1;
51318
 
    }else{
51319
 
      assert( pCur->apPage[pCur->iPage]->nCell>0 );
51320
 
      *pRes = 0;
51321
 
      rc = moveToLeftmost(pCur);
51322
 
    }
51323
 
  }
51324
 
  return rc;
51325
 
}
51326
 
 
51327
 
/* Move the cursor to the last entry in the table.  Return SQLITE_OK
51328
 
** on success.  Set *pRes to 0 if the cursor actually points to something
51329
 
** or set *pRes to 1 if the table is empty.
51330
 
*/
51331
 
SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
51332
 
  int rc;
51333
 
 
51334
 
  assert( cursorHoldsMutex(pCur) );
51335
 
  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
51336
 
 
51337
 
  /* If the cursor already points to the last entry, this is a no-op. */
51338
 
  if( CURSOR_VALID==pCur->eState && pCur->atLast ){
51339
 
#ifdef SQLITE_DEBUG
51340
 
    /* This block serves to assert() that the cursor really does point 
51341
 
    ** to the last entry in the b-tree. */
51342
 
    int ii;
51343
 
    for(ii=0; ii<pCur->iPage; ii++){
51344
 
      assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
51345
 
    }
51346
 
    assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
51347
 
    assert( pCur->apPage[pCur->iPage]->leaf );
51348
 
#endif
51349
 
    return SQLITE_OK;
51350
 
  }
51351
 
 
51352
 
  rc = moveToRoot(pCur);
51353
 
  if( rc==SQLITE_OK ){
51354
 
    if( CURSOR_INVALID==pCur->eState ){
51355
 
      assert( pCur->apPage[pCur->iPage]->nCell==0 );
51356
 
      *pRes = 1;
51357
 
    }else{
51358
 
      assert( pCur->eState==CURSOR_VALID );
51359
 
      *pRes = 0;
51360
 
      rc = moveToRightmost(pCur);
51361
 
      pCur->atLast = rc==SQLITE_OK ?1:0;
51362
 
    }
51363
 
  }
51364
 
  return rc;
51365
 
}
51366
 
 
51367
 
/* Move the cursor so that it points to an entry near the key 
51368
 
** specified by pIdxKey or intKey.   Return a success code.
51369
 
**
51370
 
** For INTKEY tables, the intKey parameter is used.  pIdxKey 
51371
 
** must be NULL.  For index tables, pIdxKey is used and intKey
51372
 
** is ignored.
51373
 
**
51374
 
** If an exact match is not found, then the cursor is always
51375
 
** left pointing at a leaf page which would hold the entry if it
51376
 
** were present.  The cursor might point to an entry that comes
51377
 
** before or after the key.
51378
 
**
51379
 
** An integer is written into *pRes which is the result of
51380
 
** comparing the key with the entry to which the cursor is 
51381
 
** pointing.  The meaning of the integer written into
51382
 
** *pRes is as follows:
51383
 
**
51384
 
**     *pRes<0      The cursor is left pointing at an entry that
51385
 
**                  is smaller than intKey/pIdxKey or if the table is empty
51386
 
**                  and the cursor is therefore left point to nothing.
51387
 
**
51388
 
**     *pRes==0     The cursor is left pointing at an entry that
51389
 
**                  exactly matches intKey/pIdxKey.
51390
 
**
51391
 
**     *pRes>0      The cursor is left pointing at an entry that
51392
 
**                  is larger than intKey/pIdxKey.
51393
 
**
51394
 
*/
51395
 
SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
51396
 
  BtCursor *pCur,          /* The cursor to be moved */
51397
 
  UnpackedRecord *pIdxKey, /* Unpacked index key */
51398
 
  i64 intKey,              /* The table key */
51399
 
  int biasRight,           /* If true, bias the search to the high end */
51400
 
  int *pRes                /* Write search results here */
51401
 
){
51402
 
  int rc;
51403
 
 
51404
 
  assert( cursorHoldsMutex(pCur) );
51405
 
  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
51406
 
  assert( pRes );
51407
 
  assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
51408
 
 
51409
 
  /* If the cursor is already positioned at the point we are trying
51410
 
  ** to move to, then just return without doing any work */
51411
 
  if( pCur->eState==CURSOR_VALID && pCur->validNKey 
51412
 
   && pCur->apPage[0]->intKey 
51413
 
  ){
51414
 
    if( pCur->info.nKey==intKey ){
51415
 
      *pRes = 0;
51416
 
      return SQLITE_OK;
51417
 
    }
51418
 
    if( pCur->atLast && pCur->info.nKey<intKey ){
51419
 
      *pRes = -1;
51420
 
      return SQLITE_OK;
51421
 
    }
51422
 
  }
51423
 
 
51424
 
  rc = moveToRoot(pCur);
51425
 
  if( rc ){
51426
 
    return rc;
51427
 
  }
51428
 
  assert( pCur->apPage[pCur->iPage] );
51429
 
  assert( pCur->apPage[pCur->iPage]->isInit );
51430
 
  assert( pCur->apPage[pCur->iPage]->nCell>0 || pCur->eState==CURSOR_INVALID );
51431
 
  if( pCur->eState==CURSOR_INVALID ){
51432
 
    *pRes = -1;
51433
 
    assert( pCur->apPage[pCur->iPage]->nCell==0 );
51434
 
    return SQLITE_OK;
51435
 
  }
51436
 
  assert( pCur->apPage[0]->intKey || pIdxKey );
51437
 
  for(;;){
51438
 
    int lwr, upr;
51439
 
    Pgno chldPg;
51440
 
    MemPage *pPage = pCur->apPage[pCur->iPage];
51441
 
    int c;
51442
 
 
51443
 
    /* pPage->nCell must be greater than zero. If this is the root-page
51444
 
    ** the cursor would have been INVALID above and this for(;;) loop
51445
 
    ** not run. If this is not the root-page, then the moveToChild() routine
51446
 
    ** would have already detected db corruption. Similarly, pPage must
51447
 
    ** be the right kind (index or table) of b-tree page. Otherwise
51448
 
    ** a moveToChild() or moveToRoot() call would have detected corruption.  */
51449
 
    assert( pPage->nCell>0 );
51450
 
    assert( pPage->intKey==(pIdxKey==0) );
51451
 
    lwr = 0;
51452
 
    upr = pPage->nCell-1;
51453
 
    if( biasRight ){
51454
 
      pCur->aiIdx[pCur->iPage] = (u16)upr;
51455
 
    }else{
51456
 
      pCur->aiIdx[pCur->iPage] = (u16)((upr+lwr)/2);
51457
 
    }
51458
 
    for(;;){
51459
 
      int idx = pCur->aiIdx[pCur->iPage]; /* Index of current cell in pPage */
51460
 
      u8 *pCell;                          /* Pointer to current cell in pPage */
51461
 
 
51462
 
      pCur->info.nSize = 0;
51463
 
      pCell = findCell(pPage, idx) + pPage->childPtrSize;
51464
 
      if( pPage->intKey ){
51465
 
        i64 nCellKey;
51466
 
        if( pPage->hasData ){
51467
 
          u32 dummy;
51468
 
          pCell += getVarint32(pCell, dummy);
51469
 
        }
51470
 
        getVarint(pCell, (u64*)&nCellKey);
51471
 
        if( nCellKey==intKey ){
51472
 
          c = 0;
51473
 
        }else if( nCellKey<intKey ){
51474
 
          c = -1;
51475
 
        }else{
51476
 
          assert( nCellKey>intKey );
51477
 
          c = +1;
51478
 
        }
51479
 
        pCur->validNKey = 1;
51480
 
        pCur->info.nKey = nCellKey;
51481
 
      }else{
51482
 
        /* The maximum supported page-size is 65536 bytes. This means that
51483
 
        ** the maximum number of record bytes stored on an index B-Tree
51484
 
        ** page is less than 16384 bytes and may be stored as a 2-byte
51485
 
        ** varint. This information is used to attempt to avoid parsing 
51486
 
        ** the entire cell by checking for the cases where the record is 
51487
 
        ** stored entirely within the b-tree page by inspecting the first 
51488
 
        ** 2 bytes of the cell.
51489
 
        */
51490
 
        int nCell = pCell[0];
51491
 
        if( !(nCell & 0x80) && nCell<=pPage->maxLocal ){
51492
 
          /* This branch runs if the record-size field of the cell is a
51493
 
          ** single byte varint and the record fits entirely on the main
51494
 
          ** b-tree page.  */
51495
 
          c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
51496
 
        }else if( !(pCell[1] & 0x80) 
51497
 
          && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
51498
 
        ){
51499
 
          /* The record-size field is a 2 byte varint and the record 
51500
 
          ** fits entirely on the main b-tree page.  */
51501
 
          c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
51502
 
        }else{
51503
 
          /* The record flows over onto one or more overflow pages. In
51504
 
          ** this case the whole cell needs to be parsed, a buffer allocated
51505
 
          ** and accessPayload() used to retrieve the record into the
51506
 
          ** buffer before VdbeRecordCompare() can be called. */
51507
 
          void *pCellKey;
51508
 
          u8 * const pCellBody = pCell - pPage->childPtrSize;
51509
 
          btreeParseCellPtr(pPage, pCellBody, &pCur->info);
51510
 
          nCell = (int)pCur->info.nKey;
51511
 
          pCellKey = sqlite3Malloc( nCell );
51512
 
          if( pCellKey==0 ){
51513
 
            rc = SQLITE_NOMEM;
51514
 
            goto moveto_finish;
51515
 
          }
51516
 
          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
51517
 
          if( rc ){
51518
 
            sqlite3_free(pCellKey);
51519
 
            goto moveto_finish;
51520
 
          }
51521
 
          c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
51522
 
          sqlite3_free(pCellKey);
51523
 
        }
51524
 
      }
51525
 
      if( c==0 ){
51526
 
        if( pPage->intKey && !pPage->leaf ){
51527
 
          lwr = idx;
51528
 
          upr = lwr - 1;
51529
 
          break;
51530
 
        }else{
51531
 
          *pRes = 0;
51532
 
          rc = SQLITE_OK;
51533
 
          goto moveto_finish;
51534
 
        }
51535
 
      }
51536
 
      if( c<0 ){
51537
 
        lwr = idx+1;
51538
 
      }else{
51539
 
        upr = idx-1;
51540
 
      }
51541
 
      if( lwr>upr ){
51542
 
        break;
51543
 
      }
51544
 
      pCur->aiIdx[pCur->iPage] = (u16)((lwr+upr)/2);
51545
 
    }
51546
 
    assert( lwr==upr+1 );
51547
 
    assert( pPage->isInit );
51548
 
    if( pPage->leaf ){
51549
 
      chldPg = 0;
51550
 
    }else if( lwr>=pPage->nCell ){
51551
 
      chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
51552
 
    }else{
51553
 
      chldPg = get4byte(findCell(pPage, lwr));
51554
 
    }
51555
 
    if( chldPg==0 ){
51556
 
      assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
51557
 
      *pRes = c;
51558
 
      rc = SQLITE_OK;
51559
 
      goto moveto_finish;
51560
 
    }
51561
 
    pCur->aiIdx[pCur->iPage] = (u16)lwr;
51562
 
    pCur->info.nSize = 0;
51563
 
    pCur->validNKey = 0;
51564
 
    rc = moveToChild(pCur, chldPg);
51565
 
    if( rc ) goto moveto_finish;
51566
 
  }
51567
 
moveto_finish:
51568
 
  return rc;
51569
 
}
51570
 
 
51571
 
 
51572
 
/*
51573
 
** Return TRUE if the cursor is not pointing at an entry of the table.
51574
 
**
51575
 
** TRUE will be returned after a call to sqlite3BtreeNext() moves
51576
 
** past the last entry in the table or sqlite3BtreePrev() moves past
51577
 
** the first entry.  TRUE is also returned if the table is empty.
51578
 
*/
51579
 
SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
51580
 
  /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
51581
 
  ** have been deleted? This API will need to change to return an error code
51582
 
  ** as well as the boolean result value.
51583
 
  */
51584
 
  return (CURSOR_VALID!=pCur->eState);
51585
 
}
51586
 
 
51587
 
/*
51588
 
** Advance the cursor to the next entry in the database.  If
51589
 
** successful then set *pRes=0.  If the cursor
51590
 
** was already pointing to the last entry in the database before
51591
 
** this routine was called, then set *pRes=1.
51592
 
*/
51593
 
SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
51594
 
  int rc;
51595
 
  int idx;
51596
 
  MemPage *pPage;
51597
 
 
51598
 
  assert( cursorHoldsMutex(pCur) );
51599
 
  rc = restoreCursorPosition(pCur);
51600
 
  if( rc!=SQLITE_OK ){
51601
 
    return rc;
51602
 
  }
51603
 
  assert( pRes!=0 );
51604
 
  if( CURSOR_INVALID==pCur->eState ){
51605
 
    *pRes = 1;
51606
 
    return SQLITE_OK;
51607
 
  }
51608
 
  if( pCur->skipNext>0 ){
51609
 
    pCur->skipNext = 0;
51610
 
    *pRes = 0;
51611
 
    return SQLITE_OK;
51612
 
  }
51613
 
  pCur->skipNext = 0;
51614
 
 
51615
 
  pPage = pCur->apPage[pCur->iPage];
51616
 
  idx = ++pCur->aiIdx[pCur->iPage];
51617
 
  assert( pPage->isInit );
51618
 
  assert( idx<=pPage->nCell );
51619
 
 
51620
 
  pCur->info.nSize = 0;
51621
 
  pCur->validNKey = 0;
51622
 
  if( idx>=pPage->nCell ){
51623
 
    if( !pPage->leaf ){
51624
 
      rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
51625
 
      if( rc ) return rc;
51626
 
      rc = moveToLeftmost(pCur);
51627
 
      *pRes = 0;
51628
 
      return rc;
51629
 
    }
51630
 
    do{
51631
 
      if( pCur->iPage==0 ){
51632
 
        *pRes = 1;
51633
 
        pCur->eState = CURSOR_INVALID;
51634
 
        return SQLITE_OK;
51635
 
      }
51636
 
      moveToParent(pCur);
51637
 
      pPage = pCur->apPage[pCur->iPage];
51638
 
    }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
51639
 
    *pRes = 0;
51640
 
    if( pPage->intKey ){
51641
 
      rc = sqlite3BtreeNext(pCur, pRes);
51642
 
    }else{
51643
 
      rc = SQLITE_OK;
51644
 
    }
51645
 
    return rc;
51646
 
  }
51647
 
  *pRes = 0;
51648
 
  if( pPage->leaf ){
51649
 
    return SQLITE_OK;
51650
 
  }
51651
 
  rc = moveToLeftmost(pCur);
51652
 
  return rc;
51653
 
}
51654
 
 
51655
 
 
51656
 
/*
51657
 
** Step the cursor to the back to the previous entry in the database.  If
51658
 
** successful then set *pRes=0.  If the cursor
51659
 
** was already pointing to the first entry in the database before
51660
 
** this routine was called, then set *pRes=1.
51661
 
*/
51662
 
SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
51663
 
  int rc;
51664
 
  MemPage *pPage;
51665
 
 
51666
 
  assert( cursorHoldsMutex(pCur) );
51667
 
  rc = restoreCursorPosition(pCur);
51668
 
  if( rc!=SQLITE_OK ){
51669
 
    return rc;
51670
 
  }
51671
 
  pCur->atLast = 0;
51672
 
  if( CURSOR_INVALID==pCur->eState ){
51673
 
    *pRes = 1;
51674
 
    return SQLITE_OK;
51675
 
  }
51676
 
  if( pCur->skipNext<0 ){
51677
 
    pCur->skipNext = 0;
51678
 
    *pRes = 0;
51679
 
    return SQLITE_OK;
51680
 
  }
51681
 
  pCur->skipNext = 0;
51682
 
 
51683
 
  pPage = pCur->apPage[pCur->iPage];
51684
 
  assert( pPage->isInit );
51685
 
  if( !pPage->leaf ){
51686
 
    int idx = pCur->aiIdx[pCur->iPage];
51687
 
    rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
51688
 
    if( rc ){
51689
 
      return rc;
51690
 
    }
51691
 
    rc = moveToRightmost(pCur);
51692
 
  }else{
51693
 
    while( pCur->aiIdx[pCur->iPage]==0 ){
51694
 
      if( pCur->iPage==0 ){
51695
 
        pCur->eState = CURSOR_INVALID;
51696
 
        *pRes = 1;
51697
 
        return SQLITE_OK;
51698
 
      }
51699
 
      moveToParent(pCur);
51700
 
    }
51701
 
    pCur->info.nSize = 0;
51702
 
    pCur->validNKey = 0;
51703
 
 
51704
 
    pCur->aiIdx[pCur->iPage]--;
51705
 
    pPage = pCur->apPage[pCur->iPage];
51706
 
    if( pPage->intKey && !pPage->leaf ){
51707
 
      rc = sqlite3BtreePrevious(pCur, pRes);
51708
 
    }else{
51709
 
      rc = SQLITE_OK;
51710
 
    }
51711
 
  }
51712
 
  *pRes = 0;
51713
 
  return rc;
51714
 
}
51715
 
 
51716
 
/*
51717
 
** Allocate a new page from the database file.
51718
 
**
51719
 
** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
51720
 
** has already been called on the new page.)  The new page has also
51721
 
** been referenced and the calling routine is responsible for calling
51722
 
** sqlite3PagerUnref() on the new page when it is done.
51723
 
**
51724
 
** SQLITE_OK is returned on success.  Any other return value indicates
51725
 
** an error.  *ppPage and *pPgno are undefined in the event of an error.
51726
 
** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
51727
 
**
51728
 
** If the "nearby" parameter is not 0, then a (feeble) effort is made to 
51729
 
** locate a page close to the page number "nearby".  This can be used in an
51730
 
** attempt to keep related pages close to each other in the database file,
51731
 
** which in turn can make database access faster.
51732
 
**
51733
 
** If the "exact" parameter is not 0, and the page-number nearby exists 
51734
 
** anywhere on the free-list, then it is guarenteed to be returned. This
51735
 
** is only used by auto-vacuum databases when allocating a new table.
51736
 
*/
51737
 
static int allocateBtreePage(
51738
 
  BtShared *pBt, 
51739
 
  MemPage **ppPage, 
51740
 
  Pgno *pPgno, 
51741
 
  Pgno nearby,
51742
 
  u8 exact
51743
 
){
51744
 
  MemPage *pPage1;
51745
 
  int rc;
51746
 
  u32 n;     /* Number of pages on the freelist */
51747
 
  u32 k;     /* Number of leaves on the trunk of the freelist */
51748
 
  MemPage *pTrunk = 0;
51749
 
  MemPage *pPrevTrunk = 0;
51750
 
  Pgno mxPage;     /* Total size of the database file */
51751
 
 
51752
 
  assert( sqlite3_mutex_held(pBt->mutex) );
51753
 
  pPage1 = pBt->pPage1;
51754
 
  mxPage = btreePagecount(pBt);
51755
 
  n = get4byte(&pPage1->aData[36]);
51756
 
  testcase( n==mxPage-1 );
51757
 
  if( n>=mxPage ){
51758
 
    return SQLITE_CORRUPT_BKPT;
51759
 
  }
51760
 
  if( n>0 ){
51761
 
    /* There are pages on the freelist.  Reuse one of those pages. */
51762
 
    Pgno iTrunk;
51763
 
    u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
51764
 
    
51765
 
    /* If the 'exact' parameter was true and a query of the pointer-map
51766
 
    ** shows that the page 'nearby' is somewhere on the free-list, then
51767
 
    ** the entire-list will be searched for that page.
51768
 
    */
51769
 
#ifndef SQLITE_OMIT_AUTOVACUUM
51770
 
    if( exact && nearby<=mxPage ){
51771
 
      u8 eType;
51772
 
      assert( nearby>0 );
51773
 
      assert( pBt->autoVacuum );
51774
 
      rc = ptrmapGet(pBt, nearby, &eType, 0);
51775
 
      if( rc ) return rc;
51776
 
      if( eType==PTRMAP_FREEPAGE ){
51777
 
        searchList = 1;
51778
 
      }
51779
 
      *pPgno = nearby;
51780
 
    }
51781
 
#endif
51782
 
 
51783
 
    /* Decrement the free-list count by 1. Set iTrunk to the index of the
51784
 
    ** first free-list trunk page. iPrevTrunk is initially 1.
51785
 
    */
51786
 
    rc = sqlite3PagerWrite(pPage1->pDbPage);
51787
 
    if( rc ) return rc;
51788
 
    put4byte(&pPage1->aData[36], n-1);
51789
 
 
51790
 
    /* The code within this loop is run only once if the 'searchList' variable
51791
 
    ** is not true. Otherwise, it runs once for each trunk-page on the
51792
 
    ** free-list until the page 'nearby' is located.
51793
 
    */
51794
 
    do {
51795
 
      pPrevTrunk = pTrunk;
51796
 
      if( pPrevTrunk ){
51797
 
        iTrunk = get4byte(&pPrevTrunk->aData[0]);
51798
 
      }else{
51799
 
        iTrunk = get4byte(&pPage1->aData[32]);
51800
 
      }
51801
 
      testcase( iTrunk==mxPage );
51802
 
      if( iTrunk>mxPage ){
51803
 
        rc = SQLITE_CORRUPT_BKPT;
51804
 
      }else{
51805
 
        rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
51806
 
      }
51807
 
      if( rc ){
51808
 
        pTrunk = 0;
51809
 
        goto end_allocate_page;
51810
 
      }
51811
 
 
51812
 
      k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */
51813
 
      if( k==0 && !searchList ){
51814
 
        /* The trunk has no leaves and the list is not being searched. 
51815
 
        ** So extract the trunk page itself and use it as the newly 
51816
 
        ** allocated page */
51817
 
        assert( pPrevTrunk==0 );
51818
 
        rc = sqlite3PagerWrite(pTrunk->pDbPage);
51819
 
        if( rc ){
51820
 
          goto end_allocate_page;
51821
 
        }
51822
 
        *pPgno = iTrunk;
51823
 
        memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
51824
 
        *ppPage = pTrunk;
51825
 
        pTrunk = 0;
51826
 
        TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
51827
 
      }else if( k>(u32)(pBt->usableSize/4 - 2) ){
51828
 
        /* Value of k is out of range.  Database corruption */
51829
 
        rc = SQLITE_CORRUPT_BKPT;
51830
 
        goto end_allocate_page;
51831
 
#ifndef SQLITE_OMIT_AUTOVACUUM
51832
 
      }else if( searchList && nearby==iTrunk ){
51833
 
        /* The list is being searched and this trunk page is the page
51834
 
        ** to allocate, regardless of whether it has leaves.
51835
 
        */
51836
 
        assert( *pPgno==iTrunk );
51837
 
        *ppPage = pTrunk;
51838
 
        searchList = 0;
51839
 
        rc = sqlite3PagerWrite(pTrunk->pDbPage);
51840
 
        if( rc ){
51841
 
          goto end_allocate_page;
51842
 
        }
51843
 
        if( k==0 ){
51844
 
          if( !pPrevTrunk ){
51845
 
            memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
51846
 
          }else{
51847
 
            rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
51848
 
            if( rc!=SQLITE_OK ){
51849
 
              goto end_allocate_page;
51850
 
            }
51851
 
            memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
51852
 
          }
51853
 
        }else{
51854
 
          /* The trunk page is required by the caller but it contains 
51855
 
          ** pointers to free-list leaves. The first leaf becomes a trunk
51856
 
          ** page in this case.
51857
 
          */
51858
 
          MemPage *pNewTrunk;
51859
 
          Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
51860
 
          if( iNewTrunk>mxPage ){ 
51861
 
            rc = SQLITE_CORRUPT_BKPT;
51862
 
            goto end_allocate_page;
51863
 
          }
51864
 
          testcase( iNewTrunk==mxPage );
51865
 
          rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
51866
 
          if( rc!=SQLITE_OK ){
51867
 
            goto end_allocate_page;
51868
 
          }
51869
 
          rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
51870
 
          if( rc!=SQLITE_OK ){
51871
 
            releasePage(pNewTrunk);
51872
 
            goto end_allocate_page;
51873
 
          }
51874
 
          memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
51875
 
          put4byte(&pNewTrunk->aData[4], k-1);
51876
 
          memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
51877
 
          releasePage(pNewTrunk);
51878
 
          if( !pPrevTrunk ){
51879
 
            assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
51880
 
            put4byte(&pPage1->aData[32], iNewTrunk);
51881
 
          }else{
51882
 
            rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
51883
 
            if( rc ){
51884
 
              goto end_allocate_page;
51885
 
            }
51886
 
            put4byte(&pPrevTrunk->aData[0], iNewTrunk);
51887
 
          }
51888
 
        }
51889
 
        pTrunk = 0;
51890
 
        TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
51891
 
#endif
51892
 
      }else if( k>0 ){
51893
 
        /* Extract a leaf from the trunk */
51894
 
        u32 closest;
51895
 
        Pgno iPage;
51896
 
        unsigned char *aData = pTrunk->aData;
51897
 
        if( nearby>0 ){
51898
 
          u32 i;
51899
 
          int dist;
51900
 
          closest = 0;
51901
 
          dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
51902
 
          for(i=1; i<k; i++){
51903
 
            int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
51904
 
            if( d2<dist ){
51905
 
              closest = i;
51906
 
              dist = d2;
51907
 
            }
51908
 
          }
51909
 
        }else{
51910
 
          closest = 0;
51911
 
        }
51912
 
 
51913
 
        iPage = get4byte(&aData[8+closest*4]);
51914
 
        testcase( iPage==mxPage );
51915
 
        if( iPage>mxPage ){
51916
 
          rc = SQLITE_CORRUPT_BKPT;
51917
 
          goto end_allocate_page;
51918
 
        }
51919
 
        testcase( iPage==mxPage );
51920
 
        if( !searchList || iPage==nearby ){
51921
 
          int noContent;
51922
 
          *pPgno = iPage;
51923
 
          TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
51924
 
                 ": %d more free pages\n",
51925
 
                 *pPgno, closest+1, k, pTrunk->pgno, n-1));
51926
 
          rc = sqlite3PagerWrite(pTrunk->pDbPage);
51927
 
          if( rc ) goto end_allocate_page;
51928
 
          if( closest<k-1 ){
51929
 
            memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
51930
 
          }
51931
 
          put4byte(&aData[4], k-1);
51932
 
          noContent = !btreeGetHasContent(pBt, *pPgno);
51933
 
          rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
51934
 
          if( rc==SQLITE_OK ){
51935
 
            rc = sqlite3PagerWrite((*ppPage)->pDbPage);
51936
 
            if( rc!=SQLITE_OK ){
51937
 
              releasePage(*ppPage);
51938
 
            }
51939
 
          }
51940
 
          searchList = 0;
51941
 
        }
51942
 
      }
51943
 
      releasePage(pPrevTrunk);
51944
 
      pPrevTrunk = 0;
51945
 
    }while( searchList );
51946
 
  }else{
51947
 
    /* There are no pages on the freelist, so create a new page at the
51948
 
    ** end of the file */
51949
 
    rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
51950
 
    if( rc ) return rc;
51951
 
    pBt->nPage++;
51952
 
    if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
51953
 
 
51954
 
#ifndef SQLITE_OMIT_AUTOVACUUM
51955
 
    if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
51956
 
      /* If *pPgno refers to a pointer-map page, allocate two new pages
51957
 
      ** at the end of the file instead of one. The first allocated page
51958
 
      ** becomes a new pointer-map page, the second is used by the caller.
51959
 
      */
51960
 
      MemPage *pPg = 0;
51961
 
      TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
51962
 
      assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
51963
 
      rc = btreeGetPage(pBt, pBt->nPage, &pPg, 1);
51964
 
      if( rc==SQLITE_OK ){
51965
 
        rc = sqlite3PagerWrite(pPg->pDbPage);
51966
 
        releasePage(pPg);
51967
 
      }
51968
 
      if( rc ) return rc;
51969
 
      pBt->nPage++;
51970
 
      if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
51971
 
    }
51972
 
#endif
51973
 
    put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
51974
 
    *pPgno = pBt->nPage;
51975
 
 
51976
 
    assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
51977
 
    rc = btreeGetPage(pBt, *pPgno, ppPage, 1);
51978
 
    if( rc ) return rc;
51979
 
    rc = sqlite3PagerWrite((*ppPage)->pDbPage);
51980
 
    if( rc!=SQLITE_OK ){
51981
 
      releasePage(*ppPage);
51982
 
    }
51983
 
    TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
51984
 
  }
51985
 
 
51986
 
  assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
51987
 
 
51988
 
end_allocate_page:
51989
 
  releasePage(pTrunk);
51990
 
  releasePage(pPrevTrunk);
51991
 
  if( rc==SQLITE_OK ){
51992
 
    if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
51993
 
      releasePage(*ppPage);
51994
 
      return SQLITE_CORRUPT_BKPT;
51995
 
    }
51996
 
    (*ppPage)->isInit = 0;
51997
 
  }else{
51998
 
    *ppPage = 0;
51999
 
  }
52000
 
  assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
52001
 
  return rc;
52002
 
}
52003
 
 
52004
 
/*
52005
 
** This function is used to add page iPage to the database file free-list. 
52006
 
** It is assumed that the page is not already a part of the free-list.
52007
 
**
52008
 
** The value passed as the second argument to this function is optional.
52009
 
** If the caller happens to have a pointer to the MemPage object 
52010
 
** corresponding to page iPage handy, it may pass it as the second value. 
52011
 
** Otherwise, it may pass NULL.
52012
 
**
52013
 
** If a pointer to a MemPage object is passed as the second argument,
52014
 
** its reference count is not altered by this function.
52015
 
*/
52016
 
static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
52017
 
  MemPage *pTrunk = 0;                /* Free-list trunk page */
52018
 
  Pgno iTrunk = 0;                    /* Page number of free-list trunk page */ 
52019
 
  MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
52020
 
  MemPage *pPage;                     /* Page being freed. May be NULL. */
52021
 
  int rc;                             /* Return Code */
52022
 
  int nFree;                          /* Initial number of pages on free-list */
52023
 
 
52024
 
  assert( sqlite3_mutex_held(pBt->mutex) );
52025
 
  assert( iPage>1 );
52026
 
  assert( !pMemPage || pMemPage->pgno==iPage );
52027
 
 
52028
 
  if( pMemPage ){
52029
 
    pPage = pMemPage;
52030
 
    sqlite3PagerRef(pPage->pDbPage);
52031
 
  }else{
52032
 
    pPage = btreePageLookup(pBt, iPage);
52033
 
  }
52034
 
 
52035
 
  /* Increment the free page count on pPage1 */
52036
 
  rc = sqlite3PagerWrite(pPage1->pDbPage);
52037
 
  if( rc ) goto freepage_out;
52038
 
  nFree = get4byte(&pPage1->aData[36]);
52039
 
  put4byte(&pPage1->aData[36], nFree+1);
52040
 
 
52041
 
  if( pBt->secureDelete ){
52042
 
    /* If the secure_delete option is enabled, then
52043
 
    ** always fully overwrite deleted information with zeros.
52044
 
    */
52045
 
    if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
52046
 
     ||            ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
52047
 
    ){
52048
 
      goto freepage_out;
52049
 
    }
52050
 
    memset(pPage->aData, 0, pPage->pBt->pageSize);
52051
 
  }
52052
 
 
52053
 
  /* If the database supports auto-vacuum, write an entry in the pointer-map
52054
 
  ** to indicate that the page is free.
52055
 
  */
52056
 
  if( ISAUTOVACUUM ){
52057
 
    ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
52058
 
    if( rc ) goto freepage_out;
52059
 
  }
52060
 
 
52061
 
  /* Now manipulate the actual database free-list structure. There are two
52062
 
  ** possibilities. If the free-list is currently empty, or if the first
52063
 
  ** trunk page in the free-list is full, then this page will become a
52064
 
  ** new free-list trunk page. Otherwise, it will become a leaf of the
52065
 
  ** first trunk page in the current free-list. This block tests if it
52066
 
  ** is possible to add the page as a new free-list leaf.
52067
 
  */
52068
 
  if( nFree!=0 ){
52069
 
    u32 nLeaf;                /* Initial number of leaf cells on trunk page */
52070
 
 
52071
 
    iTrunk = get4byte(&pPage1->aData[32]);
52072
 
    rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
52073
 
    if( rc!=SQLITE_OK ){
52074
 
      goto freepage_out;
52075
 
    }
52076
 
 
52077
 
    nLeaf = get4byte(&pTrunk->aData[4]);
52078
 
    assert( pBt->usableSize>32 );
52079
 
    if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
52080
 
      rc = SQLITE_CORRUPT_BKPT;
52081
 
      goto freepage_out;
52082
 
    }
52083
 
    if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
52084
 
      /* In this case there is room on the trunk page to insert the page
52085
 
      ** being freed as a new leaf.
52086
 
      **
52087
 
      ** Note that the trunk page is not really full until it contains
52088
 
      ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
52089
 
      ** coded.  But due to a coding error in versions of SQLite prior to
52090
 
      ** 3.6.0, databases with freelist trunk pages holding more than
52091
 
      ** usableSize/4 - 8 entries will be reported as corrupt.  In order
52092
 
      ** to maintain backwards compatibility with older versions of SQLite,
52093
 
      ** we will continue to restrict the number of entries to usableSize/4 - 8
52094
 
      ** for now.  At some point in the future (once everyone has upgraded
52095
 
      ** to 3.6.0 or later) we should consider fixing the conditional above
52096
 
      ** to read "usableSize/4-2" instead of "usableSize/4-8".
52097
 
      */
52098
 
      rc = sqlite3PagerWrite(pTrunk->pDbPage);
52099
 
      if( rc==SQLITE_OK ){
52100
 
        put4byte(&pTrunk->aData[4], nLeaf+1);
52101
 
        put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
52102
 
        if( pPage && !pBt->secureDelete ){
52103
 
          sqlite3PagerDontWrite(pPage->pDbPage);
52104
 
        }
52105
 
        rc = btreeSetHasContent(pBt, iPage);
52106
 
      }
52107
 
      TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
52108
 
      goto freepage_out;
52109
 
    }
52110
 
  }
52111
 
 
52112
 
  /* If control flows to this point, then it was not possible to add the
52113
 
  ** the page being freed as a leaf page of the first trunk in the free-list.
52114
 
  ** Possibly because the free-list is empty, or possibly because the 
52115
 
  ** first trunk in the free-list is full. Either way, the page being freed
52116
 
  ** will become the new first trunk page in the free-list.
52117
 
  */
52118
 
  if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
52119
 
    goto freepage_out;
52120
 
  }
52121
 
  rc = sqlite3PagerWrite(pPage->pDbPage);
52122
 
  if( rc!=SQLITE_OK ){
52123
 
    goto freepage_out;
52124
 
  }
52125
 
  put4byte(pPage->aData, iTrunk);
52126
 
  put4byte(&pPage->aData[4], 0);
52127
 
  put4byte(&pPage1->aData[32], iPage);
52128
 
  TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
52129
 
 
52130
 
freepage_out:
52131
 
  if( pPage ){
52132
 
    pPage->isInit = 0;
52133
 
  }
52134
 
  releasePage(pPage);
52135
 
  releasePage(pTrunk);
52136
 
  return rc;
52137
 
}
52138
 
static void freePage(MemPage *pPage, int *pRC){
52139
 
  if( (*pRC)==SQLITE_OK ){
52140
 
    *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
52141
 
  }
52142
 
}
52143
 
 
52144
 
/*
52145
 
** Free any overflow pages associated with the given Cell.
52146
 
*/
52147
 
static int clearCell(MemPage *pPage, unsigned char *pCell){
52148
 
  BtShared *pBt = pPage->pBt;
52149
 
  CellInfo info;
52150
 
  Pgno ovflPgno;
52151
 
  int rc;
52152
 
  int nOvfl;
52153
 
  u32 ovflPageSize;
52154
 
 
52155
 
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52156
 
  btreeParseCellPtr(pPage, pCell, &info);
52157
 
  if( info.iOverflow==0 ){
52158
 
    return SQLITE_OK;  /* No overflow pages. Return without doing anything */
52159
 
  }
52160
 
  ovflPgno = get4byte(&pCell[info.iOverflow]);
52161
 
  assert( pBt->usableSize > 4 );
52162
 
  ovflPageSize = pBt->usableSize - 4;
52163
 
  nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
52164
 
  assert( ovflPgno==0 || nOvfl>0 );
52165
 
  while( nOvfl-- ){
52166
 
    Pgno iNext = 0;
52167
 
    MemPage *pOvfl = 0;
52168
 
    if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
52169
 
      /* 0 is not a legal page number and page 1 cannot be an 
52170
 
      ** overflow page. Therefore if ovflPgno<2 or past the end of the 
52171
 
      ** file the database must be corrupt. */
52172
 
      return SQLITE_CORRUPT_BKPT;
52173
 
    }
52174
 
    if( nOvfl ){
52175
 
      rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
52176
 
      if( rc ) return rc;
52177
 
    }
52178
 
 
52179
 
    if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
52180
 
     && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
52181
 
    ){
52182
 
      /* There is no reason any cursor should have an outstanding reference 
52183
 
      ** to an overflow page belonging to a cell that is being deleted/updated.
52184
 
      ** So if there exists more than one reference to this page, then it 
52185
 
      ** must not really be an overflow page and the database must be corrupt. 
52186
 
      ** It is helpful to detect this before calling freePage2(), as 
52187
 
      ** freePage2() may zero the page contents if secure-delete mode is
52188
 
      ** enabled. If this 'overflow' page happens to be a page that the
52189
 
      ** caller is iterating through or using in some other way, this
52190
 
      ** can be problematic.
52191
 
      */
52192
 
      rc = SQLITE_CORRUPT_BKPT;
52193
 
    }else{
52194
 
      rc = freePage2(pBt, pOvfl, ovflPgno);
52195
 
    }
52196
 
 
52197
 
    if( pOvfl ){
52198
 
      sqlite3PagerUnref(pOvfl->pDbPage);
52199
 
    }
52200
 
    if( rc ) return rc;
52201
 
    ovflPgno = iNext;
52202
 
  }
52203
 
  return SQLITE_OK;
52204
 
}
52205
 
 
52206
 
/*
52207
 
** Create the byte sequence used to represent a cell on page pPage
52208
 
** and write that byte sequence into pCell[].  Overflow pages are
52209
 
** allocated and filled in as necessary.  The calling procedure
52210
 
** is responsible for making sure sufficient space has been allocated
52211
 
** for pCell[].
52212
 
**
52213
 
** Note that pCell does not necessary need to point to the pPage->aData
52214
 
** area.  pCell might point to some temporary storage.  The cell will
52215
 
** be constructed in this temporary area then copied into pPage->aData
52216
 
** later.
52217
 
*/
52218
 
static int fillInCell(
52219
 
  MemPage *pPage,                /* The page that contains the cell */
52220
 
  unsigned char *pCell,          /* Complete text of the cell */
52221
 
  const void *pKey, i64 nKey,    /* The key */
52222
 
  const void *pData,int nData,   /* The data */
52223
 
  int nZero,                     /* Extra zero bytes to append to pData */
52224
 
  int *pnSize                    /* Write cell size here */
52225
 
){
52226
 
  int nPayload;
52227
 
  const u8 *pSrc;
52228
 
  int nSrc, n, rc;
52229
 
  int spaceLeft;
52230
 
  MemPage *pOvfl = 0;
52231
 
  MemPage *pToRelease = 0;
52232
 
  unsigned char *pPrior;
52233
 
  unsigned char *pPayload;
52234
 
  BtShared *pBt = pPage->pBt;
52235
 
  Pgno pgnoOvfl = 0;
52236
 
  int nHeader;
52237
 
  CellInfo info;
52238
 
 
52239
 
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52240
 
 
52241
 
  /* pPage is not necessarily writeable since pCell might be auxiliary
52242
 
  ** buffer space that is separate from the pPage buffer area */
52243
 
  assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
52244
 
            || sqlite3PagerIswriteable(pPage->pDbPage) );
52245
 
 
52246
 
  /* Fill in the header. */
52247
 
  nHeader = 0;
52248
 
  if( !pPage->leaf ){
52249
 
    nHeader += 4;
52250
 
  }
52251
 
  if( pPage->hasData ){
52252
 
    nHeader += putVarint(&pCell[nHeader], nData+nZero);
52253
 
  }else{
52254
 
    nData = nZero = 0;
52255
 
  }
52256
 
  nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
52257
 
  btreeParseCellPtr(pPage, pCell, &info);
52258
 
  assert( info.nHeader==nHeader );
52259
 
  assert( info.nKey==nKey );
52260
 
  assert( info.nData==(u32)(nData+nZero) );
52261
 
  
52262
 
  /* Fill in the payload */
52263
 
  nPayload = nData + nZero;
52264
 
  if( pPage->intKey ){
52265
 
    pSrc = pData;
52266
 
    nSrc = nData;
52267
 
    nData = 0;
52268
 
  }else{ 
52269
 
    if( NEVER(nKey>0x7fffffff || pKey==0) ){
52270
 
      return SQLITE_CORRUPT_BKPT;
52271
 
    }
52272
 
    nPayload += (int)nKey;
52273
 
    pSrc = pKey;
52274
 
    nSrc = (int)nKey;
52275
 
  }
52276
 
  *pnSize = info.nSize;
52277
 
  spaceLeft = info.nLocal;
52278
 
  pPayload = &pCell[nHeader];
52279
 
  pPrior = &pCell[info.iOverflow];
52280
 
 
52281
 
  while( nPayload>0 ){
52282
 
    if( spaceLeft==0 ){
52283
 
#ifndef SQLITE_OMIT_AUTOVACUUM
52284
 
      Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
52285
 
      if( pBt->autoVacuum ){
52286
 
        do{
52287
 
          pgnoOvfl++;
52288
 
        } while( 
52289
 
          PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) 
52290
 
        );
52291
 
      }
52292
 
#endif
52293
 
      rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
52294
 
#ifndef SQLITE_OMIT_AUTOVACUUM
52295
 
      /* If the database supports auto-vacuum, and the second or subsequent
52296
 
      ** overflow page is being allocated, add an entry to the pointer-map
52297
 
      ** for that page now. 
52298
 
      **
52299
 
      ** If this is the first overflow page, then write a partial entry 
52300
 
      ** to the pointer-map. If we write nothing to this pointer-map slot,
52301
 
      ** then the optimistic overflow chain processing in clearCell()
52302
 
      ** may misinterpret the uninitialised values and delete the
52303
 
      ** wrong pages from the database.
52304
 
      */
52305
 
      if( pBt->autoVacuum && rc==SQLITE_OK ){
52306
 
        u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
52307
 
        ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
52308
 
        if( rc ){
52309
 
          releasePage(pOvfl);
52310
 
        }
52311
 
      }
52312
 
#endif
52313
 
      if( rc ){
52314
 
        releasePage(pToRelease);
52315
 
        return rc;
52316
 
      }
52317
 
 
52318
 
      /* If pToRelease is not zero than pPrior points into the data area
52319
 
      ** of pToRelease.  Make sure pToRelease is still writeable. */
52320
 
      assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
52321
 
 
52322
 
      /* If pPrior is part of the data area of pPage, then make sure pPage
52323
 
      ** is still writeable */
52324
 
      assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
52325
 
            || sqlite3PagerIswriteable(pPage->pDbPage) );
52326
 
 
52327
 
      put4byte(pPrior, pgnoOvfl);
52328
 
      releasePage(pToRelease);
52329
 
      pToRelease = pOvfl;
52330
 
      pPrior = pOvfl->aData;
52331
 
      put4byte(pPrior, 0);
52332
 
      pPayload = &pOvfl->aData[4];
52333
 
      spaceLeft = pBt->usableSize - 4;
52334
 
    }
52335
 
    n = nPayload;
52336
 
    if( n>spaceLeft ) n = spaceLeft;
52337
 
 
52338
 
    /* If pToRelease is not zero than pPayload points into the data area
52339
 
    ** of pToRelease.  Make sure pToRelease is still writeable. */
52340
 
    assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
52341
 
 
52342
 
    /* If pPayload is part of the data area of pPage, then make sure pPage
52343
 
    ** is still writeable */
52344
 
    assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
52345
 
            || sqlite3PagerIswriteable(pPage->pDbPage) );
52346
 
 
52347
 
    if( nSrc>0 ){
52348
 
      if( n>nSrc ) n = nSrc;
52349
 
      assert( pSrc );
52350
 
      memcpy(pPayload, pSrc, n);
52351
 
    }else{
52352
 
      memset(pPayload, 0, n);
52353
 
    }
52354
 
    nPayload -= n;
52355
 
    pPayload += n;
52356
 
    pSrc += n;
52357
 
    nSrc -= n;
52358
 
    spaceLeft -= n;
52359
 
    if( nSrc==0 ){
52360
 
      nSrc = nData;
52361
 
      pSrc = pData;
52362
 
    }
52363
 
  }
52364
 
  releasePage(pToRelease);
52365
 
  return SQLITE_OK;
52366
 
}
52367
 
 
52368
 
/*
52369
 
** Remove the i-th cell from pPage.  This routine effects pPage only.
52370
 
** The cell content is not freed or deallocated.  It is assumed that
52371
 
** the cell content has been copied someplace else.  This routine just
52372
 
** removes the reference to the cell from pPage.
52373
 
**
52374
 
** "sz" must be the number of bytes in the cell.
52375
 
*/
52376
 
static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
52377
 
  int i;          /* Loop counter */
52378
 
  u32 pc;         /* Offset to cell content of cell being deleted */
52379
 
  u8 *data;       /* pPage->aData */
52380
 
  u8 *ptr;        /* Used to move bytes around within data[] */
52381
 
  int rc;         /* The return code */
52382
 
  int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
52383
 
 
52384
 
  if( *pRC ) return;
52385
 
 
52386
 
  assert( idx>=0 && idx<pPage->nCell );
52387
 
  assert( sz==cellSize(pPage, idx) );
52388
 
  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
52389
 
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52390
 
  data = pPage->aData;
52391
 
  ptr = &data[pPage->cellOffset + 2*idx];
52392
 
  pc = get2byte(ptr);
52393
 
  hdr = pPage->hdrOffset;
52394
 
  testcase( pc==get2byte(&data[hdr+5]) );
52395
 
  testcase( pc+sz==pPage->pBt->usableSize );
52396
 
  if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
52397
 
    *pRC = SQLITE_CORRUPT_BKPT;
52398
 
    return;
52399
 
  }
52400
 
  rc = freeSpace(pPage, pc, sz);
52401
 
  if( rc ){
52402
 
    *pRC = rc;
52403
 
    return;
52404
 
  }
52405
 
  for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
52406
 
    ptr[0] = ptr[2];
52407
 
    ptr[1] = ptr[3];
52408
 
  }
52409
 
  pPage->nCell--;
52410
 
  put2byte(&data[hdr+3], pPage->nCell);
52411
 
  pPage->nFree += 2;
52412
 
}
52413
 
 
52414
 
/*
52415
 
** Insert a new cell on pPage at cell index "i".  pCell points to the
52416
 
** content of the cell.
52417
 
**
52418
 
** If the cell content will fit on the page, then put it there.  If it
52419
 
** will not fit, then make a copy of the cell content into pTemp if
52420
 
** pTemp is not null.  Regardless of pTemp, allocate a new entry
52421
 
** in pPage->aOvfl[] and make it point to the cell content (either
52422
 
** in pTemp or the original pCell) and also record its index. 
52423
 
** Allocating a new entry in pPage->aCell[] implies that 
52424
 
** pPage->nOverflow is incremented.
52425
 
**
52426
 
** If nSkip is non-zero, then do not copy the first nSkip bytes of the
52427
 
** cell. The caller will overwrite them after this function returns. If
52428
 
** nSkip is non-zero, then pCell may not point to an invalid memory location 
52429
 
** (but pCell+nSkip is always valid).
52430
 
*/
52431
 
static void insertCell(
52432
 
  MemPage *pPage,   /* Page into which we are copying */
52433
 
  int i,            /* New cell becomes the i-th cell of the page */
52434
 
  u8 *pCell,        /* Content of the new cell */
52435
 
  int sz,           /* Bytes of content in pCell */
52436
 
  u8 *pTemp,        /* Temp storage space for pCell, if needed */
52437
 
  Pgno iChild,      /* If non-zero, replace first 4 bytes with this value */
52438
 
  int *pRC          /* Read and write return code from here */
52439
 
){
52440
 
  int idx = 0;      /* Where to write new cell content in data[] */
52441
 
  int j;            /* Loop counter */
52442
 
  int end;          /* First byte past the last cell pointer in data[] */
52443
 
  int ins;          /* Index in data[] where new cell pointer is inserted */
52444
 
  int cellOffset;   /* Address of first cell pointer in data[] */
52445
 
  u8 *data;         /* The content of the whole page */
52446
 
  u8 *ptr;          /* Used for moving information around in data[] */
52447
 
 
52448
 
  int nSkip = (iChild ? 4 : 0);
52449
 
 
52450
 
  if( *pRC ) return;
52451
 
 
52452
 
  assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
52453
 
  assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
52454
 
  assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) );
52455
 
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52456
 
  /* The cell should normally be sized correctly.  However, when moving a
52457
 
  ** malformed cell from a leaf page to an interior page, if the cell size
52458
 
  ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
52459
 
  ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
52460
 
  ** the term after the || in the following assert(). */
52461
 
  assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
52462
 
  if( pPage->nOverflow || sz+2>pPage->nFree ){
52463
 
    if( pTemp ){
52464
 
      memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
52465
 
      pCell = pTemp;
52466
 
    }
52467
 
    if( iChild ){
52468
 
      put4byte(pCell, iChild);
52469
 
    }
52470
 
    j = pPage->nOverflow++;
52471
 
    assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) );
52472
 
    pPage->aOvfl[j].pCell = pCell;
52473
 
    pPage->aOvfl[j].idx = (u16)i;
52474
 
  }else{
52475
 
    int rc = sqlite3PagerWrite(pPage->pDbPage);
52476
 
    if( rc!=SQLITE_OK ){
52477
 
      *pRC = rc;
52478
 
      return;
52479
 
    }
52480
 
    assert( sqlite3PagerIswriteable(pPage->pDbPage) );
52481
 
    data = pPage->aData;
52482
 
    cellOffset = pPage->cellOffset;
52483
 
    end = cellOffset + 2*pPage->nCell;
52484
 
    ins = cellOffset + 2*i;
52485
 
    rc = allocateSpace(pPage, sz, &idx);
52486
 
    if( rc ){ *pRC = rc; return; }
52487
 
    /* The allocateSpace() routine guarantees the following two properties
52488
 
    ** if it returns success */
52489
 
    assert( idx >= end+2 );
52490
 
    assert( idx+sz <= (int)pPage->pBt->usableSize );
52491
 
    pPage->nCell++;
52492
 
    pPage->nFree -= (u16)(2 + sz);
52493
 
    memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
52494
 
    if( iChild ){
52495
 
      put4byte(&data[idx], iChild);
52496
 
    }
52497
 
    for(j=end, ptr=&data[j]; j>ins; j-=2, ptr-=2){
52498
 
      ptr[0] = ptr[-2];
52499
 
      ptr[1] = ptr[-1];
52500
 
    }
52501
 
    put2byte(&data[ins], idx);
52502
 
    put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
52503
 
#ifndef SQLITE_OMIT_AUTOVACUUM
52504
 
    if( pPage->pBt->autoVacuum ){
52505
 
      /* The cell may contain a pointer to an overflow page. If so, write
52506
 
      ** the entry for the overflow page into the pointer map.
52507
 
      */
52508
 
      ptrmapPutOvflPtr(pPage, pCell, pRC);
52509
 
    }
52510
 
#endif
52511
 
  }
52512
 
}
52513
 
 
52514
 
/*
52515
 
** Add a list of cells to a page.  The page should be initially empty.
52516
 
** The cells are guaranteed to fit on the page.
52517
 
*/
52518
 
static void assemblePage(
52519
 
  MemPage *pPage,   /* The page to be assemblied */
52520
 
  int nCell,        /* The number of cells to add to this page */
52521
 
  u8 **apCell,      /* Pointers to cell bodies */
52522
 
  u16 *aSize        /* Sizes of the cells */
52523
 
){
52524
 
  int i;            /* Loop counter */
52525
 
  u8 *pCellptr;     /* Address of next cell pointer */
52526
 
  int cellbody;     /* Address of next cell body */
52527
 
  u8 * const data = pPage->aData;             /* Pointer to data for pPage */
52528
 
  const int hdr = pPage->hdrOffset;           /* Offset of header on pPage */
52529
 
  const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
52530
 
 
52531
 
  assert( pPage->nOverflow==0 );
52532
 
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52533
 
  assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt)
52534
 
            && (int)MX_CELL(pPage->pBt)<=10921);
52535
 
  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
52536
 
 
52537
 
  /* Check that the page has just been zeroed by zeroPage() */
52538
 
  assert( pPage->nCell==0 );
52539
 
  assert( get2byteNotZero(&data[hdr+5])==nUsable );
52540
 
 
52541
 
  pCellptr = &data[pPage->cellOffset + nCell*2];
52542
 
  cellbody = nUsable;
52543
 
  for(i=nCell-1; i>=0; i--){
52544
 
    pCellptr -= 2;
52545
 
    cellbody -= aSize[i];
52546
 
    put2byte(pCellptr, cellbody);
52547
 
    memcpy(&data[cellbody], apCell[i], aSize[i]);
52548
 
  }
52549
 
  put2byte(&data[hdr+3], nCell);
52550
 
  put2byte(&data[hdr+5], cellbody);
52551
 
  pPage->nFree -= (u16)(nCell*2 + nUsable - cellbody);
52552
 
  pPage->nCell = (u16)nCell;
52553
 
}
52554
 
 
52555
 
/*
52556
 
** The following parameters determine how many adjacent pages get involved
52557
 
** in a balancing operation.  NN is the number of neighbors on either side
52558
 
** of the page that participate in the balancing operation.  NB is the
52559
 
** total number of pages that participate, including the target page and
52560
 
** NN neighbors on either side.
52561
 
**
52562
 
** The minimum value of NN is 1 (of course).  Increasing NN above 1
52563
 
** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
52564
 
** in exchange for a larger degradation in INSERT and UPDATE performance.
52565
 
** The value of NN appears to give the best results overall.
52566
 
*/
52567
 
#define NN 1             /* Number of neighbors on either side of pPage */
52568
 
#define NB (NN*2+1)      /* Total pages involved in the balance */
52569
 
 
52570
 
 
52571
 
#ifndef SQLITE_OMIT_QUICKBALANCE
52572
 
/*
52573
 
** This version of balance() handles the common special case where
52574
 
** a new entry is being inserted on the extreme right-end of the
52575
 
** tree, in other words, when the new entry will become the largest
52576
 
** entry in the tree.
52577
 
**
52578
 
** Instead of trying to balance the 3 right-most leaf pages, just add
52579
 
** a new page to the right-hand side and put the one new entry in
52580
 
** that page.  This leaves the right side of the tree somewhat
52581
 
** unbalanced.  But odds are that we will be inserting new entries
52582
 
** at the end soon afterwards so the nearly empty page will quickly
52583
 
** fill up.  On average.
52584
 
**
52585
 
** pPage is the leaf page which is the right-most page in the tree.
52586
 
** pParent is its parent.  pPage must have a single overflow entry
52587
 
** which is also the right-most entry on the page.
52588
 
**
52589
 
** The pSpace buffer is used to store a temporary copy of the divider
52590
 
** cell that will be inserted into pParent. Such a cell consists of a 4
52591
 
** byte page number followed by a variable length integer. In other
52592
 
** words, at most 13 bytes. Hence the pSpace buffer must be at
52593
 
** least 13 bytes in size.
52594
 
*/
52595
 
static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
52596
 
  BtShared *const pBt = pPage->pBt;    /* B-Tree Database */
52597
 
  MemPage *pNew;                       /* Newly allocated page */
52598
 
  int rc;                              /* Return Code */
52599
 
  Pgno pgnoNew;                        /* Page number of pNew */
52600
 
 
52601
 
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52602
 
  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
52603
 
  assert( pPage->nOverflow==1 );
52604
 
 
52605
 
  /* This error condition is now caught prior to reaching this function */
52606
 
  if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT;
52607
 
 
52608
 
  /* Allocate a new page. This page will become the right-sibling of 
52609
 
  ** pPage. Make the parent page writable, so that the new divider cell
52610
 
  ** may be inserted. If both these operations are successful, proceed.
52611
 
  */
52612
 
  rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
52613
 
 
52614
 
  if( rc==SQLITE_OK ){
52615
 
 
52616
 
    u8 *pOut = &pSpace[4];
52617
 
    u8 *pCell = pPage->aOvfl[0].pCell;
52618
 
    u16 szCell = cellSizePtr(pPage, pCell);
52619
 
    u8 *pStop;
52620
 
 
52621
 
    assert( sqlite3PagerIswriteable(pNew->pDbPage) );
52622
 
    assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
52623
 
    zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
52624
 
    assemblePage(pNew, 1, &pCell, &szCell);
52625
 
 
52626
 
    /* If this is an auto-vacuum database, update the pointer map
52627
 
    ** with entries for the new page, and any pointer from the 
52628
 
    ** cell on the page to an overflow page. If either of these
52629
 
    ** operations fails, the return code is set, but the contents
52630
 
    ** of the parent page are still manipulated by thh code below.
52631
 
    ** That is Ok, at this point the parent page is guaranteed to
52632
 
    ** be marked as dirty. Returning an error code will cause a
52633
 
    ** rollback, undoing any changes made to the parent page.
52634
 
    */
52635
 
    if( ISAUTOVACUUM ){
52636
 
      ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
52637
 
      if( szCell>pNew->minLocal ){
52638
 
        ptrmapPutOvflPtr(pNew, pCell, &rc);
52639
 
      }
52640
 
    }
52641
 
  
52642
 
    /* Create a divider cell to insert into pParent. The divider cell
52643
 
    ** consists of a 4-byte page number (the page number of pPage) and
52644
 
    ** a variable length key value (which must be the same value as the
52645
 
    ** largest key on pPage).
52646
 
    **
52647
 
    ** To find the largest key value on pPage, first find the right-most 
52648
 
    ** cell on pPage. The first two fields of this cell are the 
52649
 
    ** record-length (a variable length integer at most 32-bits in size)
52650
 
    ** and the key value (a variable length integer, may have any value).
52651
 
    ** The first of the while(...) loops below skips over the record-length
52652
 
    ** field. The second while(...) loop copies the key value from the
52653
 
    ** cell on pPage into the pSpace buffer.
52654
 
    */
52655
 
    pCell = findCell(pPage, pPage->nCell-1);
52656
 
    pStop = &pCell[9];
52657
 
    while( (*(pCell++)&0x80) && pCell<pStop );
52658
 
    pStop = &pCell[9];
52659
 
    while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
52660
 
 
52661
 
    /* Insert the new divider cell into pParent. */
52662
 
    insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
52663
 
               0, pPage->pgno, &rc);
52664
 
 
52665
 
    /* Set the right-child pointer of pParent to point to the new page. */
52666
 
    put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
52667
 
  
52668
 
    /* Release the reference to the new page. */
52669
 
    releasePage(pNew);
52670
 
  }
52671
 
 
52672
 
  return rc;
52673
 
}
52674
 
#endif /* SQLITE_OMIT_QUICKBALANCE */
52675
 
 
52676
 
#if 0
52677
 
/*
52678
 
** This function does not contribute anything to the operation of SQLite.
52679
 
** it is sometimes activated temporarily while debugging code responsible 
52680
 
** for setting pointer-map entries.
52681
 
*/
52682
 
static int ptrmapCheckPages(MemPage **apPage, int nPage){
52683
 
  int i, j;
52684
 
  for(i=0; i<nPage; i++){
52685
 
    Pgno n;
52686
 
    u8 e;
52687
 
    MemPage *pPage = apPage[i];
52688
 
    BtShared *pBt = pPage->pBt;
52689
 
    assert( pPage->isInit );
52690
 
 
52691
 
    for(j=0; j<pPage->nCell; j++){
52692
 
      CellInfo info;
52693
 
      u8 *z;
52694
 
     
52695
 
      z = findCell(pPage, j);
52696
 
      btreeParseCellPtr(pPage, z, &info);
52697
 
      if( info.iOverflow ){
52698
 
        Pgno ovfl = get4byte(&z[info.iOverflow]);
52699
 
        ptrmapGet(pBt, ovfl, &e, &n);
52700
 
        assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
52701
 
      }
52702
 
      if( !pPage->leaf ){
52703
 
        Pgno child = get4byte(z);
52704
 
        ptrmapGet(pBt, child, &e, &n);
52705
 
        assert( n==pPage->pgno && e==PTRMAP_BTREE );
52706
 
      }
52707
 
    }
52708
 
    if( !pPage->leaf ){
52709
 
      Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
52710
 
      ptrmapGet(pBt, child, &e, &n);
52711
 
      assert( n==pPage->pgno && e==PTRMAP_BTREE );
52712
 
    }
52713
 
  }
52714
 
  return 1;
52715
 
}
52716
 
#endif
52717
 
 
52718
 
/*
52719
 
** This function is used to copy the contents of the b-tree node stored 
52720
 
** on page pFrom to page pTo. If page pFrom was not a leaf page, then
52721
 
** the pointer-map entries for each child page are updated so that the
52722
 
** parent page stored in the pointer map is page pTo. If pFrom contained
52723
 
** any cells with overflow page pointers, then the corresponding pointer
52724
 
** map entries are also updated so that the parent page is page pTo.
52725
 
**
52726
 
** If pFrom is currently carrying any overflow cells (entries in the
52727
 
** MemPage.aOvfl[] array), they are not copied to pTo. 
52728
 
**
52729
 
** Before returning, page pTo is reinitialized using btreeInitPage().
52730
 
**
52731
 
** The performance of this function is not critical. It is only used by 
52732
 
** the balance_shallower() and balance_deeper() procedures, neither of
52733
 
** which are called often under normal circumstances.
52734
 
*/
52735
 
static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
52736
 
  if( (*pRC)==SQLITE_OK ){
52737
 
    BtShared * const pBt = pFrom->pBt;
52738
 
    u8 * const aFrom = pFrom->aData;
52739
 
    u8 * const aTo = pTo->aData;
52740
 
    int const iFromHdr = pFrom->hdrOffset;
52741
 
    int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
52742
 
    int rc;
52743
 
    int iData;
52744
 
  
52745
 
  
52746
 
    assert( pFrom->isInit );
52747
 
    assert( pFrom->nFree>=iToHdr );
52748
 
    assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
52749
 
  
52750
 
    /* Copy the b-tree node content from page pFrom to page pTo. */
52751
 
    iData = get2byte(&aFrom[iFromHdr+5]);
52752
 
    memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
52753
 
    memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
52754
 
  
52755
 
    /* Reinitialize page pTo so that the contents of the MemPage structure
52756
 
    ** match the new data. The initialization of pTo can actually fail under
52757
 
    ** fairly obscure circumstances, even though it is a copy of initialized 
52758
 
    ** page pFrom.
52759
 
    */
52760
 
    pTo->isInit = 0;
52761
 
    rc = btreeInitPage(pTo);
52762
 
    if( rc!=SQLITE_OK ){
52763
 
      *pRC = rc;
52764
 
      return;
52765
 
    }
52766
 
  
52767
 
    /* If this is an auto-vacuum database, update the pointer-map entries
52768
 
    ** for any b-tree or overflow pages that pTo now contains the pointers to.
52769
 
    */
52770
 
    if( ISAUTOVACUUM ){
52771
 
      *pRC = setChildPtrmaps(pTo);
52772
 
    }
52773
 
  }
52774
 
}
52775
 
 
52776
 
/*
52777
 
** This routine redistributes cells on the iParentIdx'th child of pParent
52778
 
** (hereafter "the page") and up to 2 siblings so that all pages have about the
52779
 
** same amount of free space. Usually a single sibling on either side of the
52780
 
** page are used in the balancing, though both siblings might come from one
52781
 
** side if the page is the first or last child of its parent. If the page 
52782
 
** has fewer than 2 siblings (something which can only happen if the page
52783
 
** is a root page or a child of a root page) then all available siblings
52784
 
** participate in the balancing.
52785
 
**
52786
 
** The number of siblings of the page might be increased or decreased by 
52787
 
** one or two in an effort to keep pages nearly full but not over full. 
52788
 
**
52789
 
** Note that when this routine is called, some of the cells on the page
52790
 
** might not actually be stored in MemPage.aData[]. This can happen
52791
 
** if the page is overfull. This routine ensures that all cells allocated
52792
 
** to the page and its siblings fit into MemPage.aData[] before returning.
52793
 
**
52794
 
** In the course of balancing the page and its siblings, cells may be
52795
 
** inserted into or removed from the parent page (pParent). Doing so
52796
 
** may cause the parent page to become overfull or underfull. If this
52797
 
** happens, it is the responsibility of the caller to invoke the correct
52798
 
** balancing routine to fix this problem (see the balance() routine). 
52799
 
**
52800
 
** If this routine fails for any reason, it might leave the database
52801
 
** in a corrupted state. So if this routine fails, the database should
52802
 
** be rolled back.
52803
 
**
52804
 
** The third argument to this function, aOvflSpace, is a pointer to a
52805
 
** buffer big enough to hold one page. If while inserting cells into the parent
52806
 
** page (pParent) the parent page becomes overfull, this buffer is
52807
 
** used to store the parent's overflow cells. Because this function inserts
52808
 
** a maximum of four divider cells into the parent page, and the maximum
52809
 
** size of a cell stored within an internal node is always less than 1/4
52810
 
** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
52811
 
** enough for all overflow cells.
52812
 
**
52813
 
** If aOvflSpace is set to a null pointer, this function returns 
52814
 
** SQLITE_NOMEM.
52815
 
*/
52816
 
static int balance_nonroot(
52817
 
  MemPage *pParent,               /* Parent page of siblings being balanced */
52818
 
  int iParentIdx,                 /* Index of "the page" in pParent */
52819
 
  u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
52820
 
  int isRoot                      /* True if pParent is a root-page */
52821
 
){
52822
 
  BtShared *pBt;               /* The whole database */
52823
 
  int nCell = 0;               /* Number of cells in apCell[] */
52824
 
  int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
52825
 
  int nNew = 0;                /* Number of pages in apNew[] */
52826
 
  int nOld;                    /* Number of pages in apOld[] */
52827
 
  int i, j, k;                 /* Loop counters */
52828
 
  int nxDiv;                   /* Next divider slot in pParent->aCell[] */
52829
 
  int rc = SQLITE_OK;          /* The return code */
52830
 
  u16 leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
52831
 
  int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
52832
 
  int usableSpace;             /* Bytes in pPage beyond the header */
52833
 
  int pageFlags;               /* Value of pPage->aData[0] */
52834
 
  int subtotal;                /* Subtotal of bytes in cells on one page */
52835
 
  int iSpace1 = 0;             /* First unused byte of aSpace1[] */
52836
 
  int iOvflSpace = 0;          /* First unused byte of aOvflSpace[] */
52837
 
  int szScratch;               /* Size of scratch memory requested */
52838
 
  MemPage *apOld[NB];          /* pPage and up to two siblings */
52839
 
  MemPage *apCopy[NB];         /* Private copies of apOld[] pages */
52840
 
  MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
52841
 
  u8 *pRight;                  /* Location in parent of right-sibling pointer */
52842
 
  u8 *apDiv[NB-1];             /* Divider cells in pParent */
52843
 
  int cntNew[NB+2];            /* Index in aCell[] of cell after i-th page */
52844
 
  int szNew[NB+2];             /* Combined size of cells place on i-th page */
52845
 
  u8 **apCell = 0;             /* All cells begin balanced */
52846
 
  u16 *szCell;                 /* Local size of all cells in apCell[] */
52847
 
  u8 *aSpace1;                 /* Space for copies of dividers cells */
52848
 
  Pgno pgno;                   /* Temp var to store a page number in */
52849
 
 
52850
 
  pBt = pParent->pBt;
52851
 
  assert( sqlite3_mutex_held(pBt->mutex) );
52852
 
  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
52853
 
 
52854
 
#if 0
52855
 
  TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
52856
 
#endif
52857
 
 
52858
 
  /* At this point pParent may have at most one overflow cell. And if
52859
 
  ** this overflow cell is present, it must be the cell with 
52860
 
  ** index iParentIdx. This scenario comes about when this function
52861
 
  ** is called (indirectly) from sqlite3BtreeDelete().
52862
 
  */
52863
 
  assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
52864
 
  assert( pParent->nOverflow==0 || pParent->aOvfl[0].idx==iParentIdx );
52865
 
 
52866
 
  if( !aOvflSpace ){
52867
 
    return SQLITE_NOMEM;
52868
 
  }
52869
 
 
52870
 
  /* Find the sibling pages to balance. Also locate the cells in pParent 
52871
 
  ** that divide the siblings. An attempt is made to find NN siblings on 
52872
 
  ** either side of pPage. More siblings are taken from one side, however, 
52873
 
  ** if there are fewer than NN siblings on the other side. If pParent
52874
 
  ** has NB or fewer children then all children of pParent are taken.  
52875
 
  **
52876
 
  ** This loop also drops the divider cells from the parent page. This
52877
 
  ** way, the remainder of the function does not have to deal with any
52878
 
  ** overflow cells in the parent page, since if any existed they will
52879
 
  ** have already been removed.
52880
 
  */
52881
 
  i = pParent->nOverflow + pParent->nCell;
52882
 
  if( i<2 ){
52883
 
    nxDiv = 0;
52884
 
    nOld = i+1;
52885
 
  }else{
52886
 
    nOld = 3;
52887
 
    if( iParentIdx==0 ){                 
52888
 
      nxDiv = 0;
52889
 
    }else if( iParentIdx==i ){
52890
 
      nxDiv = i-2;
52891
 
    }else{
52892
 
      nxDiv = iParentIdx-1;
52893
 
    }
52894
 
    i = 2;
52895
 
  }
52896
 
  if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
52897
 
    pRight = &pParent->aData[pParent->hdrOffset+8];
52898
 
  }else{
52899
 
    pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
52900
 
  }
52901
 
  pgno = get4byte(pRight);
52902
 
  for(;;) {
52903
 
    rc = getAndInitPage(pBt, pgno, &apOld[i]);
52904
 
    if( rc ){
52905
 
      memset(apOld, 0, (i+1)*sizeof(MemPage*));
52906
 
      goto balance_cleanup;
52907
 
    }
52908
 
    nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
52909
 
    if( (i--)==0 ) break;
52910
 
 
52911
 
    if( i+nxDiv==pParent->aOvfl[0].idx && pParent->nOverflow ){
52912
 
      apDiv[i] = pParent->aOvfl[0].pCell;
52913
 
      pgno = get4byte(apDiv[i]);
52914
 
      szNew[i] = cellSizePtr(pParent, apDiv[i]);
52915
 
      pParent->nOverflow = 0;
52916
 
    }else{
52917
 
      apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
52918
 
      pgno = get4byte(apDiv[i]);
52919
 
      szNew[i] = cellSizePtr(pParent, apDiv[i]);
52920
 
 
52921
 
      /* Drop the cell from the parent page. apDiv[i] still points to
52922
 
      ** the cell within the parent, even though it has been dropped.
52923
 
      ** This is safe because dropping a cell only overwrites the first
52924
 
      ** four bytes of it, and this function does not need the first
52925
 
      ** four bytes of the divider cell. So the pointer is safe to use
52926
 
      ** later on.  
52927
 
      **
52928
 
      ** Unless SQLite is compiled in secure-delete mode. In this case,
52929
 
      ** the dropCell() routine will overwrite the entire cell with zeroes.
52930
 
      ** In this case, temporarily copy the cell into the aOvflSpace[]
52931
 
      ** buffer. It will be copied out again as soon as the aSpace[] buffer
52932
 
      ** is allocated.  */
52933
 
      if( pBt->secureDelete ){
52934
 
        int iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
52935
 
        if( (iOff+szNew[i])>(int)pBt->usableSize ){
52936
 
          rc = SQLITE_CORRUPT_BKPT;
52937
 
          memset(apOld, 0, (i+1)*sizeof(MemPage*));
52938
 
          goto balance_cleanup;
52939
 
        }else{
52940
 
          memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
52941
 
          apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
52942
 
        }
52943
 
      }
52944
 
      dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
52945
 
    }
52946
 
  }
52947
 
 
52948
 
  /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
52949
 
  ** alignment */
52950
 
  nMaxCells = (nMaxCells + 3)&~3;
52951
 
 
52952
 
  /*
52953
 
  ** Allocate space for memory structures
52954
 
  */
52955
 
  k = pBt->pageSize + ROUND8(sizeof(MemPage));
52956
 
  szScratch =
52957
 
       nMaxCells*sizeof(u8*)                       /* apCell */
52958
 
     + nMaxCells*sizeof(u16)                       /* szCell */
52959
 
     + pBt->pageSize                               /* aSpace1 */
52960
 
     + k*nOld;                                     /* Page copies (apCopy) */
52961
 
  apCell = sqlite3ScratchMalloc( szScratch ); 
52962
 
  if( apCell==0 ){
52963
 
    rc = SQLITE_NOMEM;
52964
 
    goto balance_cleanup;
52965
 
  }
52966
 
  szCell = (u16*)&apCell[nMaxCells];
52967
 
  aSpace1 = (u8*)&szCell[nMaxCells];
52968
 
  assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
52969
 
 
52970
 
  /*
52971
 
  ** Load pointers to all cells on sibling pages and the divider cells
52972
 
  ** into the local apCell[] array.  Make copies of the divider cells
52973
 
  ** into space obtained from aSpace1[] and remove the the divider Cells
52974
 
  ** from pParent.
52975
 
  **
52976
 
  ** If the siblings are on leaf pages, then the child pointers of the
52977
 
  ** divider cells are stripped from the cells before they are copied
52978
 
  ** into aSpace1[].  In this way, all cells in apCell[] are without
52979
 
  ** child pointers.  If siblings are not leaves, then all cell in
52980
 
  ** apCell[] include child pointers.  Either way, all cells in apCell[]
52981
 
  ** are alike.
52982
 
  **
52983
 
  ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
52984
 
  **       leafData:  1 if pPage holds key+data and pParent holds only keys.
52985
 
  */
52986
 
  leafCorrection = apOld[0]->leaf*4;
52987
 
  leafData = apOld[0]->hasData;
52988
 
  for(i=0; i<nOld; i++){
52989
 
    int limit;
52990
 
    
52991
 
    /* Before doing anything else, take a copy of the i'th original sibling
52992
 
    ** The rest of this function will use data from the copies rather
52993
 
    ** that the original pages since the original pages will be in the
52994
 
    ** process of being overwritten.  */
52995
 
    MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
52996
 
    memcpy(pOld, apOld[i], sizeof(MemPage));
52997
 
    pOld->aData = (void*)&pOld[1];
52998
 
    memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
52999
 
 
53000
 
    limit = pOld->nCell+pOld->nOverflow;
53001
 
    for(j=0; j<limit; j++){
53002
 
      assert( nCell<nMaxCells );
53003
 
      apCell[nCell] = findOverflowCell(pOld, j);
53004
 
      szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
53005
 
      nCell++;
53006
 
    }
53007
 
    if( i<nOld-1 && !leafData){
53008
 
      u16 sz = (u16)szNew[i];
53009
 
      u8 *pTemp;
53010
 
      assert( nCell<nMaxCells );
53011
 
      szCell[nCell] = sz;
53012
 
      pTemp = &aSpace1[iSpace1];
53013
 
      iSpace1 += sz;
53014
 
      assert( sz<=pBt->maxLocal+23 );
53015
 
      assert( iSpace1 <= (int)pBt->pageSize );
53016
 
      memcpy(pTemp, apDiv[i], sz);
53017
 
      apCell[nCell] = pTemp+leafCorrection;
53018
 
      assert( leafCorrection==0 || leafCorrection==4 );
53019
 
      szCell[nCell] = szCell[nCell] - leafCorrection;
53020
 
      if( !pOld->leaf ){
53021
 
        assert( leafCorrection==0 );
53022
 
        assert( pOld->hdrOffset==0 );
53023
 
        /* The right pointer of the child page pOld becomes the left
53024
 
        ** pointer of the divider cell */
53025
 
        memcpy(apCell[nCell], &pOld->aData[8], 4);
53026
 
      }else{
53027
 
        assert( leafCorrection==4 );
53028
 
        if( szCell[nCell]<4 ){
53029
 
          /* Do not allow any cells smaller than 4 bytes. */
53030
 
          szCell[nCell] = 4;
53031
 
        }
53032
 
      }
53033
 
      nCell++;
53034
 
    }
53035
 
  }
53036
 
 
53037
 
  /*
53038
 
  ** Figure out the number of pages needed to hold all nCell cells.
53039
 
  ** Store this number in "k".  Also compute szNew[] which is the total
53040
 
  ** size of all cells on the i-th page and cntNew[] which is the index
53041
 
  ** in apCell[] of the cell that divides page i from page i+1.  
53042
 
  ** cntNew[k] should equal nCell.
53043
 
  **
53044
 
  ** Values computed by this block:
53045
 
  **
53046
 
  **           k: The total number of sibling pages
53047
 
  **    szNew[i]: Spaced used on the i-th sibling page.
53048
 
  **   cntNew[i]: Index in apCell[] and szCell[] for the first cell to
53049
 
  **              the right of the i-th sibling page.
53050
 
  ** usableSpace: Number of bytes of space available on each sibling.
53051
 
  ** 
53052
 
  */
53053
 
  usableSpace = pBt->usableSize - 12 + leafCorrection;
53054
 
  for(subtotal=k=i=0; i<nCell; i++){
53055
 
    assert( i<nMaxCells );
53056
 
    subtotal += szCell[i] + 2;
53057
 
    if( subtotal > usableSpace ){
53058
 
      szNew[k] = subtotal - szCell[i];
53059
 
      cntNew[k] = i;
53060
 
      if( leafData ){ i--; }
53061
 
      subtotal = 0;
53062
 
      k++;
53063
 
      if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
53064
 
    }
53065
 
  }
53066
 
  szNew[k] = subtotal;
53067
 
  cntNew[k] = nCell;
53068
 
  k++;
53069
 
 
53070
 
  /*
53071
 
  ** The packing computed by the previous block is biased toward the siblings
53072
 
  ** on the left side.  The left siblings are always nearly full, while the
53073
 
  ** right-most sibling might be nearly empty.  This block of code attempts
53074
 
  ** to adjust the packing of siblings to get a better balance.
53075
 
  **
53076
 
  ** This adjustment is more than an optimization.  The packing above might
53077
 
  ** be so out of balance as to be illegal.  For example, the right-most
53078
 
  ** sibling might be completely empty.  This adjustment is not optional.
53079
 
  */
53080
 
  for(i=k-1; i>0; i--){
53081
 
    int szRight = szNew[i];  /* Size of sibling on the right */
53082
 
    int szLeft = szNew[i-1]; /* Size of sibling on the left */
53083
 
    int r;              /* Index of right-most cell in left sibling */
53084
 
    int d;              /* Index of first cell to the left of right sibling */
53085
 
 
53086
 
    r = cntNew[i-1] - 1;
53087
 
    d = r + 1 - leafData;
53088
 
    assert( d<nMaxCells );
53089
 
    assert( r<nMaxCells );
53090
 
    while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
53091
 
      szRight += szCell[d] + 2;
53092
 
      szLeft -= szCell[r] + 2;
53093
 
      cntNew[i-1]--;
53094
 
      r = cntNew[i-1] - 1;
53095
 
      d = r + 1 - leafData;
53096
 
    }
53097
 
    szNew[i] = szRight;
53098
 
    szNew[i-1] = szLeft;
53099
 
  }
53100
 
 
53101
 
  /* Either we found one or more cells (cntnew[0])>0) or pPage is
53102
 
  ** a virtual root page.  A virtual root page is when the real root
53103
 
  ** page is page 1 and we are the only child of that page.
53104
 
  */
53105
 
  assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
53106
 
 
53107
 
  TRACE(("BALANCE: old: %d %d %d  ",
53108
 
    apOld[0]->pgno, 
53109
 
    nOld>=2 ? apOld[1]->pgno : 0,
53110
 
    nOld>=3 ? apOld[2]->pgno : 0
53111
 
  ));
53112
 
 
53113
 
  /*
53114
 
  ** Allocate k new pages.  Reuse old pages where possible.
53115
 
  */
53116
 
  if( apOld[0]->pgno<=1 ){
53117
 
    rc = SQLITE_CORRUPT_BKPT;
53118
 
    goto balance_cleanup;
53119
 
  }
53120
 
  pageFlags = apOld[0]->aData[0];
53121
 
  for(i=0; i<k; i++){
53122
 
    MemPage *pNew;
53123
 
    if( i<nOld ){
53124
 
      pNew = apNew[i] = apOld[i];
53125
 
      apOld[i] = 0;
53126
 
      rc = sqlite3PagerWrite(pNew->pDbPage);
53127
 
      nNew++;
53128
 
      if( rc ) goto balance_cleanup;
53129
 
    }else{
53130
 
      assert( i>0 );
53131
 
      rc = allocateBtreePage(pBt, &pNew, &pgno, pgno, 0);
53132
 
      if( rc ) goto balance_cleanup;
53133
 
      apNew[i] = pNew;
53134
 
      nNew++;
53135
 
 
53136
 
      /* Set the pointer-map entry for the new sibling page. */
53137
 
      if( ISAUTOVACUUM ){
53138
 
        ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
53139
 
        if( rc!=SQLITE_OK ){
53140
 
          goto balance_cleanup;
53141
 
        }
53142
 
      }
53143
 
    }
53144
 
  }
53145
 
 
53146
 
  /* Free any old pages that were not reused as new pages.
53147
 
  */
53148
 
  while( i<nOld ){
53149
 
    freePage(apOld[i], &rc);
53150
 
    if( rc ) goto balance_cleanup;
53151
 
    releasePage(apOld[i]);
53152
 
    apOld[i] = 0;
53153
 
    i++;
53154
 
  }
53155
 
 
53156
 
  /*
53157
 
  ** Put the new pages in accending order.  This helps to
53158
 
  ** keep entries in the disk file in order so that a scan
53159
 
  ** of the table is a linear scan through the file.  That
53160
 
  ** in turn helps the operating system to deliver pages
53161
 
  ** from the disk more rapidly.
53162
 
  **
53163
 
  ** An O(n^2) insertion sort algorithm is used, but since
53164
 
  ** n is never more than NB (a small constant), that should
53165
 
  ** not be a problem.
53166
 
  **
53167
 
  ** When NB==3, this one optimization makes the database
53168
 
  ** about 25% faster for large insertions and deletions.
53169
 
  */
53170
 
  for(i=0; i<k-1; i++){
53171
 
    int minV = apNew[i]->pgno;
53172
 
    int minI = i;
53173
 
    for(j=i+1; j<k; j++){
53174
 
      if( apNew[j]->pgno<(unsigned)minV ){
53175
 
        minI = j;
53176
 
        minV = apNew[j]->pgno;
53177
 
      }
53178
 
    }
53179
 
    if( minI>i ){
53180
 
      MemPage *pT;
53181
 
      pT = apNew[i];
53182
 
      apNew[i] = apNew[minI];
53183
 
      apNew[minI] = pT;
53184
 
    }
53185
 
  }
53186
 
  TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
53187
 
    apNew[0]->pgno, szNew[0],
53188
 
    nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
53189
 
    nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
53190
 
    nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
53191
 
    nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0));
53192
 
 
53193
 
  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
53194
 
  put4byte(pRight, apNew[nNew-1]->pgno);
53195
 
 
53196
 
  /*
53197
 
  ** Evenly distribute the data in apCell[] across the new pages.
53198
 
  ** Insert divider cells into pParent as necessary.
53199
 
  */
53200
 
  j = 0;
53201
 
  for(i=0; i<nNew; i++){
53202
 
    /* Assemble the new sibling page. */
53203
 
    MemPage *pNew = apNew[i];
53204
 
    assert( j<nMaxCells );
53205
 
    zeroPage(pNew, pageFlags);
53206
 
    assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
53207
 
    assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
53208
 
    assert( pNew->nOverflow==0 );
53209
 
 
53210
 
    j = cntNew[i];
53211
 
 
53212
 
    /* If the sibling page assembled above was not the right-most sibling,
53213
 
    ** insert a divider cell into the parent page.
53214
 
    */
53215
 
    assert( i<nNew-1 || j==nCell );
53216
 
    if( j<nCell ){
53217
 
      u8 *pCell;
53218
 
      u8 *pTemp;
53219
 
      int sz;
53220
 
 
53221
 
      assert( j<nMaxCells );
53222
 
      pCell = apCell[j];
53223
 
      sz = szCell[j] + leafCorrection;
53224
 
      pTemp = &aOvflSpace[iOvflSpace];
53225
 
      if( !pNew->leaf ){
53226
 
        memcpy(&pNew->aData[8], pCell, 4);
53227
 
      }else if( leafData ){
53228
 
        /* If the tree is a leaf-data tree, and the siblings are leaves, 
53229
 
        ** then there is no divider cell in apCell[]. Instead, the divider 
53230
 
        ** cell consists of the integer key for the right-most cell of 
53231
 
        ** the sibling-page assembled above only.
53232
 
        */
53233
 
        CellInfo info;
53234
 
        j--;
53235
 
        btreeParseCellPtr(pNew, apCell[j], &info);
53236
 
        pCell = pTemp;
53237
 
        sz = 4 + putVarint(&pCell[4], info.nKey);
53238
 
        pTemp = 0;
53239
 
      }else{
53240
 
        pCell -= 4;
53241
 
        /* Obscure case for non-leaf-data trees: If the cell at pCell was
53242
 
        ** previously stored on a leaf node, and its reported size was 4
53243
 
        ** bytes, then it may actually be smaller than this 
53244
 
        ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
53245
 
        ** any cell). But it is important to pass the correct size to 
53246
 
        ** insertCell(), so reparse the cell now.
53247
 
        **
53248
 
        ** Note that this can never happen in an SQLite data file, as all
53249
 
        ** cells are at least 4 bytes. It only happens in b-trees used
53250
 
        ** to evaluate "IN (SELECT ...)" and similar clauses.
53251
 
        */
53252
 
        if( szCell[j]==4 ){
53253
 
          assert(leafCorrection==4);
53254
 
          sz = cellSizePtr(pParent, pCell);
53255
 
        }
53256
 
      }
53257
 
      iOvflSpace += sz;
53258
 
      assert( sz<=pBt->maxLocal+23 );
53259
 
      assert( iOvflSpace <= (int)pBt->pageSize );
53260
 
      insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc);
53261
 
      if( rc!=SQLITE_OK ) goto balance_cleanup;
53262
 
      assert( sqlite3PagerIswriteable(pParent->pDbPage) );
53263
 
 
53264
 
      j++;
53265
 
      nxDiv++;
53266
 
    }
53267
 
  }
53268
 
  assert( j==nCell );
53269
 
  assert( nOld>0 );
53270
 
  assert( nNew>0 );
53271
 
  if( (pageFlags & PTF_LEAF)==0 ){
53272
 
    u8 *zChild = &apCopy[nOld-1]->aData[8];
53273
 
    memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
53274
 
  }
53275
 
 
53276
 
  if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
53277
 
    /* The root page of the b-tree now contains no cells. The only sibling
53278
 
    ** page is the right-child of the parent. Copy the contents of the
53279
 
    ** child page into the parent, decreasing the overall height of the
53280
 
    ** b-tree structure by one. This is described as the "balance-shallower"
53281
 
    ** sub-algorithm in some documentation.
53282
 
    **
53283
 
    ** If this is an auto-vacuum database, the call to copyNodeContent() 
53284
 
    ** sets all pointer-map entries corresponding to database image pages 
53285
 
    ** for which the pointer is stored within the content being copied.
53286
 
    **
53287
 
    ** The second assert below verifies that the child page is defragmented
53288
 
    ** (it must be, as it was just reconstructed using assemblePage()). This
53289
 
    ** is important if the parent page happens to be page 1 of the database
53290
 
    ** image.  */
53291
 
    assert( nNew==1 );
53292
 
    assert( apNew[0]->nFree == 
53293
 
        (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) 
53294
 
    );
53295
 
    copyNodeContent(apNew[0], pParent, &rc);
53296
 
    freePage(apNew[0], &rc);
53297
 
  }else if( ISAUTOVACUUM ){
53298
 
    /* Fix the pointer-map entries for all the cells that were shifted around. 
53299
 
    ** There are several different types of pointer-map entries that need to
53300
 
    ** be dealt with by this routine. Some of these have been set already, but
53301
 
    ** many have not. The following is a summary:
53302
 
    **
53303
 
    **   1) The entries associated with new sibling pages that were not
53304
 
    **      siblings when this function was called. These have already
53305
 
    **      been set. We don't need to worry about old siblings that were
53306
 
    **      moved to the free-list - the freePage() code has taken care
53307
 
    **      of those.
53308
 
    **
53309
 
    **   2) The pointer-map entries associated with the first overflow
53310
 
    **      page in any overflow chains used by new divider cells. These 
53311
 
    **      have also already been taken care of by the insertCell() code.
53312
 
    **
53313
 
    **   3) If the sibling pages are not leaves, then the child pages of
53314
 
    **      cells stored on the sibling pages may need to be updated.
53315
 
    **
53316
 
    **   4) If the sibling pages are not internal intkey nodes, then any
53317
 
    **      overflow pages used by these cells may need to be updated
53318
 
    **      (internal intkey nodes never contain pointers to overflow pages).
53319
 
    **
53320
 
    **   5) If the sibling pages are not leaves, then the pointer-map
53321
 
    **      entries for the right-child pages of each sibling may need
53322
 
    **      to be updated.
53323
 
    **
53324
 
    ** Cases 1 and 2 are dealt with above by other code. The next
53325
 
    ** block deals with cases 3 and 4 and the one after that, case 5. Since
53326
 
    ** setting a pointer map entry is a relatively expensive operation, this
53327
 
    ** code only sets pointer map entries for child or overflow pages that have
53328
 
    ** actually moved between pages.  */
53329
 
    MemPage *pNew = apNew[0];
53330
 
    MemPage *pOld = apCopy[0];
53331
 
    int nOverflow = pOld->nOverflow;
53332
 
    int iNextOld = pOld->nCell + nOverflow;
53333
 
    int iOverflow = (nOverflow ? pOld->aOvfl[0].idx : -1);
53334
 
    j = 0;                             /* Current 'old' sibling page */
53335
 
    k = 0;                             /* Current 'new' sibling page */
53336
 
    for(i=0; i<nCell; i++){
53337
 
      int isDivider = 0;
53338
 
      while( i==iNextOld ){
53339
 
        /* Cell i is the cell immediately following the last cell on old
53340
 
        ** sibling page j. If the siblings are not leaf pages of an
53341
 
        ** intkey b-tree, then cell i was a divider cell. */
53342
 
        pOld = apCopy[++j];
53343
 
        iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
53344
 
        if( pOld->nOverflow ){
53345
 
          nOverflow = pOld->nOverflow;
53346
 
          iOverflow = i + !leafData + pOld->aOvfl[0].idx;
53347
 
        }
53348
 
        isDivider = !leafData;  
53349
 
      }
53350
 
 
53351
 
      assert(nOverflow>0 || iOverflow<i );
53352
 
      assert(nOverflow<2 || pOld->aOvfl[0].idx==pOld->aOvfl[1].idx-1);
53353
 
      assert(nOverflow<3 || pOld->aOvfl[1].idx==pOld->aOvfl[2].idx-1);
53354
 
      if( i==iOverflow ){
53355
 
        isDivider = 1;
53356
 
        if( (--nOverflow)>0 ){
53357
 
          iOverflow++;
53358
 
        }
53359
 
      }
53360
 
 
53361
 
      if( i==cntNew[k] ){
53362
 
        /* Cell i is the cell immediately following the last cell on new
53363
 
        ** sibling page k. If the siblings are not leaf pages of an
53364
 
        ** intkey b-tree, then cell i is a divider cell.  */
53365
 
        pNew = apNew[++k];
53366
 
        if( !leafData ) continue;
53367
 
      }
53368
 
      assert( j<nOld );
53369
 
      assert( k<nNew );
53370
 
 
53371
 
      /* If the cell was originally divider cell (and is not now) or
53372
 
      ** an overflow cell, or if the cell was located on a different sibling
53373
 
      ** page before the balancing, then the pointer map entries associated
53374
 
      ** with any child or overflow pages need to be updated.  */
53375
 
      if( isDivider || pOld->pgno!=pNew->pgno ){
53376
 
        if( !leafCorrection ){
53377
 
          ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc);
53378
 
        }
53379
 
        if( szCell[i]>pNew->minLocal ){
53380
 
          ptrmapPutOvflPtr(pNew, apCell[i], &rc);
53381
 
        }
53382
 
      }
53383
 
    }
53384
 
 
53385
 
    if( !leafCorrection ){
53386
 
      for(i=0; i<nNew; i++){
53387
 
        u32 key = get4byte(&apNew[i]->aData[8]);
53388
 
        ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
53389
 
      }
53390
 
    }
53391
 
 
53392
 
#if 0
53393
 
    /* The ptrmapCheckPages() contains assert() statements that verify that
53394
 
    ** all pointer map pages are set correctly. This is helpful while 
53395
 
    ** debugging. This is usually disabled because a corrupt database may
53396
 
    ** cause an assert() statement to fail.  */
53397
 
    ptrmapCheckPages(apNew, nNew);
53398
 
    ptrmapCheckPages(&pParent, 1);
53399
 
#endif
53400
 
  }
53401
 
 
53402
 
  assert( pParent->isInit );
53403
 
  TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
53404
 
          nOld, nNew, nCell));
53405
 
 
53406
 
  /*
53407
 
  ** Cleanup before returning.
53408
 
  */
53409
 
balance_cleanup:
53410
 
  sqlite3ScratchFree(apCell);
53411
 
  for(i=0; i<nOld; i++){
53412
 
    releasePage(apOld[i]);
53413
 
  }
53414
 
  for(i=0; i<nNew; i++){
53415
 
    releasePage(apNew[i]);
53416
 
  }
53417
 
 
53418
 
  return rc;
53419
 
}
53420
 
 
53421
 
 
53422
 
/*
53423
 
** This function is called when the root page of a b-tree structure is
53424
 
** overfull (has one or more overflow pages).
53425
 
**
53426
 
** A new child page is allocated and the contents of the current root
53427
 
** page, including overflow cells, are copied into the child. The root
53428
 
** page is then overwritten to make it an empty page with the right-child 
53429
 
** pointer pointing to the new page.
53430
 
**
53431
 
** Before returning, all pointer-map entries corresponding to pages 
53432
 
** that the new child-page now contains pointers to are updated. The
53433
 
** entry corresponding to the new right-child pointer of the root
53434
 
** page is also updated.
53435
 
**
53436
 
** If successful, *ppChild is set to contain a reference to the child 
53437
 
** page and SQLITE_OK is returned. In this case the caller is required
53438
 
** to call releasePage() on *ppChild exactly once. If an error occurs,
53439
 
** an error code is returned and *ppChild is set to 0.
53440
 
*/
53441
 
static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
53442
 
  int rc;                        /* Return value from subprocedures */
53443
 
  MemPage *pChild = 0;           /* Pointer to a new child page */
53444
 
  Pgno pgnoChild = 0;            /* Page number of the new child page */
53445
 
  BtShared *pBt = pRoot->pBt;    /* The BTree */
53446
 
 
53447
 
  assert( pRoot->nOverflow>0 );
53448
 
  assert( sqlite3_mutex_held(pBt->mutex) );
53449
 
 
53450
 
  /* Make pRoot, the root page of the b-tree, writable. Allocate a new 
53451
 
  ** page that will become the new right-child of pPage. Copy the contents
53452
 
  ** of the node stored on pRoot into the new child page.
53453
 
  */
53454
 
  rc = sqlite3PagerWrite(pRoot->pDbPage);
53455
 
  if( rc==SQLITE_OK ){
53456
 
    rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
53457
 
    copyNodeContent(pRoot, pChild, &rc);
53458
 
    if( ISAUTOVACUUM ){
53459
 
      ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
53460
 
    }
53461
 
  }
53462
 
  if( rc ){
53463
 
    *ppChild = 0;
53464
 
    releasePage(pChild);
53465
 
    return rc;
53466
 
  }
53467
 
  assert( sqlite3PagerIswriteable(pChild->pDbPage) );
53468
 
  assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
53469
 
  assert( pChild->nCell==pRoot->nCell );
53470
 
 
53471
 
  TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
53472
 
 
53473
 
  /* Copy the overflow cells from pRoot to pChild */
53474
 
  memcpy(pChild->aOvfl, pRoot->aOvfl, pRoot->nOverflow*sizeof(pRoot->aOvfl[0]));
53475
 
  pChild->nOverflow = pRoot->nOverflow;
53476
 
 
53477
 
  /* Zero the contents of pRoot. Then install pChild as the right-child. */
53478
 
  zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
53479
 
  put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
53480
 
 
53481
 
  *ppChild = pChild;
53482
 
  return SQLITE_OK;
53483
 
}
53484
 
 
53485
 
/*
53486
 
** The page that pCur currently points to has just been modified in
53487
 
** some way. This function figures out if this modification means the
53488
 
** tree needs to be balanced, and if so calls the appropriate balancing 
53489
 
** routine. Balancing routines are:
53490
 
**
53491
 
**   balance_quick()
53492
 
**   balance_deeper()
53493
 
**   balance_nonroot()
53494
 
*/
53495
 
static int balance(BtCursor *pCur){
53496
 
  int rc = SQLITE_OK;
53497
 
  const int nMin = pCur->pBt->usableSize * 2 / 3;
53498
 
  u8 aBalanceQuickSpace[13];
53499
 
  u8 *pFree = 0;
53500
 
 
53501
 
  TESTONLY( int balance_quick_called = 0 );
53502
 
  TESTONLY( int balance_deeper_called = 0 );
53503
 
 
53504
 
  do {
53505
 
    int iPage = pCur->iPage;
53506
 
    MemPage *pPage = pCur->apPage[iPage];
53507
 
 
53508
 
    if( iPage==0 ){
53509
 
      if( pPage->nOverflow ){
53510
 
        /* The root page of the b-tree is overfull. In this case call the
53511
 
        ** balance_deeper() function to create a new child for the root-page
53512
 
        ** and copy the current contents of the root-page to it. The
53513
 
        ** next iteration of the do-loop will balance the child page.
53514
 
        */ 
53515
 
        assert( (balance_deeper_called++)==0 );
53516
 
        rc = balance_deeper(pPage, &pCur->apPage[1]);
53517
 
        if( rc==SQLITE_OK ){
53518
 
          pCur->iPage = 1;
53519
 
          pCur->aiIdx[0] = 0;
53520
 
          pCur->aiIdx[1] = 0;
53521
 
          assert( pCur->apPage[1]->nOverflow );
53522
 
        }
53523
 
      }else{
53524
 
        break;
53525
 
      }
53526
 
    }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
53527
 
      break;
53528
 
    }else{
53529
 
      MemPage * const pParent = pCur->apPage[iPage-1];
53530
 
      int const iIdx = pCur->aiIdx[iPage-1];
53531
 
 
53532
 
      rc = sqlite3PagerWrite(pParent->pDbPage);
53533
 
      if( rc==SQLITE_OK ){
53534
 
#ifndef SQLITE_OMIT_QUICKBALANCE
53535
 
        if( pPage->hasData
53536
 
         && pPage->nOverflow==1
53537
 
         && pPage->aOvfl[0].idx==pPage->nCell
53538
 
         && pParent->pgno!=1
53539
 
         && pParent->nCell==iIdx
53540
 
        ){
53541
 
          /* Call balance_quick() to create a new sibling of pPage on which
53542
 
          ** to store the overflow cell. balance_quick() inserts a new cell
53543
 
          ** into pParent, which may cause pParent overflow. If this
53544
 
          ** happens, the next interation of the do-loop will balance pParent 
53545
 
          ** use either balance_nonroot() or balance_deeper(). Until this
53546
 
          ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
53547
 
          ** buffer. 
53548
 
          **
53549
 
          ** The purpose of the following assert() is to check that only a
53550
 
          ** single call to balance_quick() is made for each call to this
53551
 
          ** function. If this were not verified, a subtle bug involving reuse
53552
 
          ** of the aBalanceQuickSpace[] might sneak in.
53553
 
          */
53554
 
          assert( (balance_quick_called++)==0 );
53555
 
          rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
53556
 
        }else
53557
 
#endif
53558
 
        {
53559
 
          /* In this case, call balance_nonroot() to redistribute cells
53560
 
          ** between pPage and up to 2 of its sibling pages. This involves
53561
 
          ** modifying the contents of pParent, which may cause pParent to
53562
 
          ** become overfull or underfull. The next iteration of the do-loop
53563
 
          ** will balance the parent page to correct this.
53564
 
          ** 
53565
 
          ** If the parent page becomes overfull, the overflow cell or cells
53566
 
          ** are stored in the pSpace buffer allocated immediately below. 
53567
 
          ** A subsequent iteration of the do-loop will deal with this by
53568
 
          ** calling balance_nonroot() (balance_deeper() may be called first,
53569
 
          ** but it doesn't deal with overflow cells - just moves them to a
53570
 
          ** different page). Once this subsequent call to balance_nonroot() 
53571
 
          ** has completed, it is safe to release the pSpace buffer used by
53572
 
          ** the previous call, as the overflow cell data will have been 
53573
 
          ** copied either into the body of a database page or into the new
53574
 
          ** pSpace buffer passed to the latter call to balance_nonroot().
53575
 
          */
53576
 
          u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
53577
 
          rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1);
53578
 
          if( pFree ){
53579
 
            /* If pFree is not NULL, it points to the pSpace buffer used 
53580
 
            ** by a previous call to balance_nonroot(). Its contents are
53581
 
            ** now stored either on real database pages or within the 
53582
 
            ** new pSpace buffer, so it may be safely freed here. */
53583
 
            sqlite3PageFree(pFree);
53584
 
          }
53585
 
 
53586
 
          /* The pSpace buffer will be freed after the next call to
53587
 
          ** balance_nonroot(), or just before this function returns, whichever
53588
 
          ** comes first. */
53589
 
          pFree = pSpace;
53590
 
        }
53591
 
      }
53592
 
 
53593
 
      pPage->nOverflow = 0;
53594
 
 
53595
 
      /* The next iteration of the do-loop balances the parent page. */
53596
 
      releasePage(pPage);
53597
 
      pCur->iPage--;
53598
 
    }
53599
 
  }while( rc==SQLITE_OK );
53600
 
 
53601
 
  if( pFree ){
53602
 
    sqlite3PageFree(pFree);
53603
 
  }
53604
 
  return rc;
53605
 
}
53606
 
 
53607
 
 
53608
 
/*
53609
 
** Insert a new record into the BTree.  The key is given by (pKey,nKey)
53610
 
** and the data is given by (pData,nData).  The cursor is used only to
53611
 
** define what table the record should be inserted into.  The cursor
53612
 
** is left pointing at a random location.
53613
 
**
53614
 
** For an INTKEY table, only the nKey value of the key is used.  pKey is
53615
 
** ignored.  For a ZERODATA table, the pData and nData are both ignored.
53616
 
**
53617
 
** If the seekResult parameter is non-zero, then a successful call to
53618
 
** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
53619
 
** been performed. seekResult is the search result returned (a negative
53620
 
** number if pCur points at an entry that is smaller than (pKey, nKey), or
53621
 
** a positive value if pCur points at an etry that is larger than 
53622
 
** (pKey, nKey)). 
53623
 
**
53624
 
** If the seekResult parameter is non-zero, then the caller guarantees that
53625
 
** cursor pCur is pointing at the existing copy of a row that is to be
53626
 
** overwritten.  If the seekResult parameter is 0, then cursor pCur may
53627
 
** point to any entry or to no entry at all and so this function has to seek
53628
 
** the cursor before the new key can be inserted.
53629
 
*/
53630
 
SQLITE_PRIVATE int sqlite3BtreeInsert(
53631
 
  BtCursor *pCur,                /* Insert data into the table of this cursor */
53632
 
  const void *pKey, i64 nKey,    /* The key of the new record */
53633
 
  const void *pData, int nData,  /* The data of the new record */
53634
 
  int nZero,                     /* Number of extra 0 bytes to append to data */
53635
 
  int appendBias,                /* True if this is likely an append */
53636
 
  int seekResult                 /* Result of prior MovetoUnpacked() call */
53637
 
){
53638
 
  int rc;
53639
 
  int loc = seekResult;          /* -1: before desired location  +1: after */
53640
 
  int szNew = 0;
53641
 
  int idx;
53642
 
  MemPage *pPage;
53643
 
  Btree *p = pCur->pBtree;
53644
 
  BtShared *pBt = p->pBt;
53645
 
  unsigned char *oldCell;
53646
 
  unsigned char *newCell = 0;
53647
 
 
53648
 
  if( pCur->eState==CURSOR_FAULT ){
53649
 
    assert( pCur->skipNext!=SQLITE_OK );
53650
 
    return pCur->skipNext;
53651
 
  }
53652
 
 
53653
 
  assert( cursorHoldsMutex(pCur) );
53654
 
  assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE && !pBt->readOnly );
53655
 
  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
53656
 
 
53657
 
  /* Assert that the caller has been consistent. If this cursor was opened
53658
 
  ** expecting an index b-tree, then the caller should be inserting blob
53659
 
  ** keys with no associated data. If the cursor was opened expecting an
53660
 
  ** intkey table, the caller should be inserting integer keys with a
53661
 
  ** blob of associated data.  */
53662
 
  assert( (pKey==0)==(pCur->pKeyInfo==0) );
53663
 
 
53664
 
  /* If this is an insert into a table b-tree, invalidate any incrblob 
53665
 
  ** cursors open on the row being replaced (assuming this is a replace
53666
 
  ** operation - if it is not, the following is a no-op).  */
53667
 
  if( pCur->pKeyInfo==0 ){
53668
 
    invalidateIncrblobCursors(p, nKey, 0);
53669
 
  }
53670
 
 
53671
 
  /* Save the positions of any other cursors open on this table.
53672
 
  **
53673
 
  ** In some cases, the call to btreeMoveto() below is a no-op. For
53674
 
  ** example, when inserting data into a table with auto-generated integer
53675
 
  ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the 
53676
 
  ** integer key to use. It then calls this function to actually insert the 
53677
 
  ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
53678
 
  ** that the cursor is already where it needs to be and returns without
53679
 
  ** doing any work. To avoid thwarting these optimizations, it is important
53680
 
  ** not to clear the cursor here.
53681
 
  */
53682
 
  rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
53683
 
  if( rc ) return rc;
53684
 
  if( !loc ){
53685
 
    rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
53686
 
    if( rc ) return rc;
53687
 
  }
53688
 
  assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
53689
 
 
53690
 
  pPage = pCur->apPage[pCur->iPage];
53691
 
  assert( pPage->intKey || nKey>=0 );
53692
 
  assert( pPage->leaf || !pPage->intKey );
53693
 
 
53694
 
  TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
53695
 
          pCur->pgnoRoot, nKey, nData, pPage->pgno,
53696
 
          loc==0 ? "overwrite" : "new entry"));
53697
 
  assert( pPage->isInit );
53698
 
  allocateTempSpace(pBt);
53699
 
  newCell = pBt->pTmpSpace;
53700
 
  if( newCell==0 ) return SQLITE_NOMEM;
53701
 
  rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
53702
 
  if( rc ) goto end_insert;
53703
 
  assert( szNew==cellSizePtr(pPage, newCell) );
53704
 
  assert( szNew <= MX_CELL_SIZE(pBt) );
53705
 
  idx = pCur->aiIdx[pCur->iPage];
53706
 
  if( loc==0 ){
53707
 
    u16 szOld;
53708
 
    assert( idx<pPage->nCell );
53709
 
    rc = sqlite3PagerWrite(pPage->pDbPage);
53710
 
    if( rc ){
53711
 
      goto end_insert;
53712
 
    }
53713
 
    oldCell = findCell(pPage, idx);
53714
 
    if( !pPage->leaf ){
53715
 
      memcpy(newCell, oldCell, 4);
53716
 
    }
53717
 
    szOld = cellSizePtr(pPage, oldCell);
53718
 
    rc = clearCell(pPage, oldCell);
53719
 
    dropCell(pPage, idx, szOld, &rc);
53720
 
    if( rc ) goto end_insert;
53721
 
  }else if( loc<0 && pPage->nCell>0 ){
53722
 
    assert( pPage->leaf );
53723
 
    idx = ++pCur->aiIdx[pCur->iPage];
53724
 
  }else{
53725
 
    assert( pPage->leaf );
53726
 
  }
53727
 
  insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
53728
 
  assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
53729
 
 
53730
 
  /* If no error has occured and pPage has an overflow cell, call balance() 
53731
 
  ** to redistribute the cells within the tree. Since balance() may move
53732
 
  ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
53733
 
  ** variables.
53734
 
  **
53735
 
  ** Previous versions of SQLite called moveToRoot() to move the cursor
53736
 
  ** back to the root page as balance() used to invalidate the contents
53737
 
  ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
53738
 
  ** set the cursor state to "invalid". This makes common insert operations
53739
 
  ** slightly faster.
53740
 
  **
53741
 
  ** There is a subtle but important optimization here too. When inserting
53742
 
  ** multiple records into an intkey b-tree using a single cursor (as can
53743
 
  ** happen while processing an "INSERT INTO ... SELECT" statement), it
53744
 
  ** is advantageous to leave the cursor pointing to the last entry in
53745
 
  ** the b-tree if possible. If the cursor is left pointing to the last
53746
 
  ** entry in the table, and the next row inserted has an integer key
53747
 
  ** larger than the largest existing key, it is possible to insert the
53748
 
  ** row without seeking the cursor. This can be a big performance boost.
53749
 
  */
53750
 
  pCur->info.nSize = 0;
53751
 
  pCur->validNKey = 0;
53752
 
  if( rc==SQLITE_OK && pPage->nOverflow ){
53753
 
    rc = balance(pCur);
53754
 
 
53755
 
    /* Must make sure nOverflow is reset to zero even if the balance()
53756
 
    ** fails. Internal data structure corruption will result otherwise. 
53757
 
    ** Also, set the cursor state to invalid. This stops saveCursorPosition()
53758
 
    ** from trying to save the current position of the cursor.  */
53759
 
    pCur->apPage[pCur->iPage]->nOverflow = 0;
53760
 
    pCur->eState = CURSOR_INVALID;
53761
 
  }
53762
 
  assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
53763
 
 
53764
 
end_insert:
53765
 
  return rc;
53766
 
}
53767
 
 
53768
 
/*
53769
 
** Delete the entry that the cursor is pointing to.  The cursor
53770
 
** is left pointing at a arbitrary location.
53771
 
*/
53772
 
SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
53773
 
  Btree *p = pCur->pBtree;
53774
 
  BtShared *pBt = p->pBt;              
53775
 
  int rc;                              /* Return code */
53776
 
  MemPage *pPage;                      /* Page to delete cell from */
53777
 
  unsigned char *pCell;                /* Pointer to cell to delete */
53778
 
  int iCellIdx;                        /* Index of cell to delete */
53779
 
  int iCellDepth;                      /* Depth of node containing pCell */ 
53780
 
 
53781
 
  assert( cursorHoldsMutex(pCur) );
53782
 
  assert( pBt->inTransaction==TRANS_WRITE );
53783
 
  assert( !pBt->readOnly );
53784
 
  assert( pCur->wrFlag );
53785
 
  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
53786
 
  assert( !hasReadConflicts(p, pCur->pgnoRoot) );
53787
 
 
53788
 
  if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) 
53789
 
   || NEVER(pCur->eState!=CURSOR_VALID)
53790
 
  ){
53791
 
    return SQLITE_ERROR;  /* Something has gone awry. */
53792
 
  }
53793
 
 
53794
 
  /* If this is a delete operation to remove a row from a table b-tree,
53795
 
  ** invalidate any incrblob cursors open on the row being deleted.  */
53796
 
  if( pCur->pKeyInfo==0 ){
53797
 
    invalidateIncrblobCursors(p, pCur->info.nKey, 0);
53798
 
  }
53799
 
 
53800
 
  iCellDepth = pCur->iPage;
53801
 
  iCellIdx = pCur->aiIdx[iCellDepth];
53802
 
  pPage = pCur->apPage[iCellDepth];
53803
 
  pCell = findCell(pPage, iCellIdx);
53804
 
 
53805
 
  /* If the page containing the entry to delete is not a leaf page, move
53806
 
  ** the cursor to the largest entry in the tree that is smaller than
53807
 
  ** the entry being deleted. This cell will replace the cell being deleted
53808
 
  ** from the internal node. The 'previous' entry is used for this instead
53809
 
  ** of the 'next' entry, as the previous entry is always a part of the
53810
 
  ** sub-tree headed by the child page of the cell being deleted. This makes
53811
 
  ** balancing the tree following the delete operation easier.  */
53812
 
  if( !pPage->leaf ){
53813
 
    int notUsed;
53814
 
    rc = sqlite3BtreePrevious(pCur, &notUsed);
53815
 
    if( rc ) return rc;
53816
 
  }
53817
 
 
53818
 
  /* Save the positions of any other cursors open on this table before
53819
 
  ** making any modifications. Make the page containing the entry to be 
53820
 
  ** deleted writable. Then free any overflow pages associated with the 
53821
 
  ** entry and finally remove the cell itself from within the page.  
53822
 
  */
53823
 
  rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
53824
 
  if( rc ) return rc;
53825
 
  rc = sqlite3PagerWrite(pPage->pDbPage);
53826
 
  if( rc ) return rc;
53827
 
  rc = clearCell(pPage, pCell);
53828
 
  dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
53829
 
  if( rc ) return rc;
53830
 
 
53831
 
  /* If the cell deleted was not located on a leaf page, then the cursor
53832
 
  ** is currently pointing to the largest entry in the sub-tree headed
53833
 
  ** by the child-page of the cell that was just deleted from an internal
53834
 
  ** node. The cell from the leaf node needs to be moved to the internal
53835
 
  ** node to replace the deleted cell.  */
53836
 
  if( !pPage->leaf ){
53837
 
    MemPage *pLeaf = pCur->apPage[pCur->iPage];
53838
 
    int nCell;
53839
 
    Pgno n = pCur->apPage[iCellDepth+1]->pgno;
53840
 
    unsigned char *pTmp;
53841
 
 
53842
 
    pCell = findCell(pLeaf, pLeaf->nCell-1);
53843
 
    nCell = cellSizePtr(pLeaf, pCell);
53844
 
    assert( MX_CELL_SIZE(pBt) >= nCell );
53845
 
 
53846
 
    allocateTempSpace(pBt);
53847
 
    pTmp = pBt->pTmpSpace;
53848
 
 
53849
 
    rc = sqlite3PagerWrite(pLeaf->pDbPage);
53850
 
    insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
53851
 
    dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
53852
 
    if( rc ) return rc;
53853
 
  }
53854
 
 
53855
 
  /* Balance the tree. If the entry deleted was located on a leaf page,
53856
 
  ** then the cursor still points to that page. In this case the first
53857
 
  ** call to balance() repairs the tree, and the if(...) condition is
53858
 
  ** never true.
53859
 
  **
53860
 
  ** Otherwise, if the entry deleted was on an internal node page, then
53861
 
  ** pCur is pointing to the leaf page from which a cell was removed to
53862
 
  ** replace the cell deleted from the internal node. This is slightly
53863
 
  ** tricky as the leaf node may be underfull, and the internal node may
53864
 
  ** be either under or overfull. In this case run the balancing algorithm
53865
 
  ** on the leaf node first. If the balance proceeds far enough up the
53866
 
  ** tree that we can be sure that any problem in the internal node has
53867
 
  ** been corrected, so be it. Otherwise, after balancing the leaf node,
53868
 
  ** walk the cursor up the tree to the internal node and balance it as 
53869
 
  ** well.  */
53870
 
  rc = balance(pCur);
53871
 
  if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
53872
 
    while( pCur->iPage>iCellDepth ){
53873
 
      releasePage(pCur->apPage[pCur->iPage--]);
53874
 
    }
53875
 
    rc = balance(pCur);
53876
 
  }
53877
 
 
53878
 
  if( rc==SQLITE_OK ){
53879
 
    moveToRoot(pCur);
53880
 
  }
53881
 
  return rc;
53882
 
}
53883
 
 
53884
 
/*
53885
 
** Create a new BTree table.  Write into *piTable the page
53886
 
** number for the root page of the new table.
53887
 
**
53888
 
** The type of type is determined by the flags parameter.  Only the
53889
 
** following values of flags are currently in use.  Other values for
53890
 
** flags might not work:
53891
 
**
53892
 
**     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
53893
 
**     BTREE_ZERODATA                  Used for SQL indices
53894
 
*/
53895
 
static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
53896
 
  BtShared *pBt = p->pBt;
53897
 
  MemPage *pRoot;
53898
 
  Pgno pgnoRoot;
53899
 
  int rc;
53900
 
  int ptfFlags;          /* Page-type flage for the root page of new table */
53901
 
 
53902
 
  assert( sqlite3BtreeHoldsMutex(p) );
53903
 
  assert( pBt->inTransaction==TRANS_WRITE );
53904
 
  assert( !pBt->readOnly );
53905
 
 
53906
 
#ifdef SQLITE_OMIT_AUTOVACUUM
53907
 
  rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
53908
 
  if( rc ){
53909
 
    return rc;
53910
 
  }
53911
 
#else
53912
 
  if( pBt->autoVacuum ){
53913
 
    Pgno pgnoMove;      /* Move a page here to make room for the root-page */
53914
 
    MemPage *pPageMove; /* The page to move to. */
53915
 
 
53916
 
    /* Creating a new table may probably require moving an existing database
53917
 
    ** to make room for the new tables root page. In case this page turns
53918
 
    ** out to be an overflow page, delete all overflow page-map caches
53919
 
    ** held by open cursors.
53920
 
    */
53921
 
    invalidateAllOverflowCache(pBt);
53922
 
 
53923
 
    /* Read the value of meta[3] from the database to determine where the
53924
 
    ** root page of the new table should go. meta[3] is the largest root-page
53925
 
    ** created so far, so the new root-page is (meta[3]+1).
53926
 
    */
53927
 
    sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
53928
 
    pgnoRoot++;
53929
 
 
53930
 
    /* The new root-page may not be allocated on a pointer-map page, or the
53931
 
    ** PENDING_BYTE page.
53932
 
    */
53933
 
    while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
53934
 
        pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
53935
 
      pgnoRoot++;
53936
 
    }
53937
 
    assert( pgnoRoot>=3 );
53938
 
 
53939
 
    /* Allocate a page. The page that currently resides at pgnoRoot will
53940
 
    ** be moved to the allocated page (unless the allocated page happens
53941
 
    ** to reside at pgnoRoot).
53942
 
    */
53943
 
    rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
53944
 
    if( rc!=SQLITE_OK ){
53945
 
      return rc;
53946
 
    }
53947
 
 
53948
 
    if( pgnoMove!=pgnoRoot ){
53949
 
      /* pgnoRoot is the page that will be used for the root-page of
53950
 
      ** the new table (assuming an error did not occur). But we were
53951
 
      ** allocated pgnoMove. If required (i.e. if it was not allocated
53952
 
      ** by extending the file), the current page at position pgnoMove
53953
 
      ** is already journaled.
53954
 
      */
53955
 
      u8 eType = 0;
53956
 
      Pgno iPtrPage = 0;
53957
 
 
53958
 
      releasePage(pPageMove);
53959
 
 
53960
 
      /* Move the page currently at pgnoRoot to pgnoMove. */
53961
 
      rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
53962
 
      if( rc!=SQLITE_OK ){
53963
 
        return rc;
53964
 
      }
53965
 
      rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
53966
 
      if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
53967
 
        rc = SQLITE_CORRUPT_BKPT;
53968
 
      }
53969
 
      if( rc!=SQLITE_OK ){
53970
 
        releasePage(pRoot);
53971
 
        return rc;
53972
 
      }
53973
 
      assert( eType!=PTRMAP_ROOTPAGE );
53974
 
      assert( eType!=PTRMAP_FREEPAGE );
53975
 
      rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
53976
 
      releasePage(pRoot);
53977
 
 
53978
 
      /* Obtain the page at pgnoRoot */
53979
 
      if( rc!=SQLITE_OK ){
53980
 
        return rc;
53981
 
      }
53982
 
      rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
53983
 
      if( rc!=SQLITE_OK ){
53984
 
        return rc;
53985
 
      }
53986
 
      rc = sqlite3PagerWrite(pRoot->pDbPage);
53987
 
      if( rc!=SQLITE_OK ){
53988
 
        releasePage(pRoot);
53989
 
        return rc;
53990
 
      }
53991
 
    }else{
53992
 
      pRoot = pPageMove;
53993
 
    } 
53994
 
 
53995
 
    /* Update the pointer-map and meta-data with the new root-page number. */
53996
 
    ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
53997
 
    if( rc ){
53998
 
      releasePage(pRoot);
53999
 
      return rc;
54000
 
    }
54001
 
 
54002
 
    /* When the new root page was allocated, page 1 was made writable in
54003
 
    ** order either to increase the database filesize, or to decrement the
54004
 
    ** freelist count.  Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
54005
 
    */
54006
 
    assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
54007
 
    rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
54008
 
    if( NEVER(rc) ){
54009
 
      releasePage(pRoot);
54010
 
      return rc;
54011
 
    }
54012
 
 
54013
 
  }else{
54014
 
    rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
54015
 
    if( rc ) return rc;
54016
 
  }
54017
 
#endif
54018
 
  assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
54019
 
  if( createTabFlags & BTREE_INTKEY ){
54020
 
    ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
54021
 
  }else{
54022
 
    ptfFlags = PTF_ZERODATA | PTF_LEAF;
54023
 
  }
54024
 
  zeroPage(pRoot, ptfFlags);
54025
 
  sqlite3PagerUnref(pRoot->pDbPage);
54026
 
  assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
54027
 
  *piTable = (int)pgnoRoot;
54028
 
  return SQLITE_OK;
54029
 
}
54030
 
SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
54031
 
  int rc;
54032
 
  sqlite3BtreeEnter(p);
54033
 
  rc = btreeCreateTable(p, piTable, flags);
54034
 
  sqlite3BtreeLeave(p);
54035
 
  return rc;
54036
 
}
54037
 
 
54038
 
/*
54039
 
** Erase the given database page and all its children.  Return
54040
 
** the page to the freelist.
54041
 
*/
54042
 
static int clearDatabasePage(
54043
 
  BtShared *pBt,           /* The BTree that contains the table */
54044
 
  Pgno pgno,               /* Page number to clear */
54045
 
  int freePageFlag,        /* Deallocate page if true */
54046
 
  int *pnChange            /* Add number of Cells freed to this counter */
54047
 
){
54048
 
  MemPage *pPage;
54049
 
  int rc;
54050
 
  unsigned char *pCell;
54051
 
  int i;
54052
 
 
54053
 
  assert( sqlite3_mutex_held(pBt->mutex) );
54054
 
  if( pgno>btreePagecount(pBt) ){
54055
 
    return SQLITE_CORRUPT_BKPT;
54056
 
  }
54057
 
 
54058
 
  rc = getAndInitPage(pBt, pgno, &pPage);
54059
 
  if( rc ) return rc;
54060
 
  for(i=0; i<pPage->nCell; i++){
54061
 
    pCell = findCell(pPage, i);
54062
 
    if( !pPage->leaf ){
54063
 
      rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
54064
 
      if( rc ) goto cleardatabasepage_out;
54065
 
    }
54066
 
    rc = clearCell(pPage, pCell);
54067
 
    if( rc ) goto cleardatabasepage_out;
54068
 
  }
54069
 
  if( !pPage->leaf ){
54070
 
    rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
54071
 
    if( rc ) goto cleardatabasepage_out;
54072
 
  }else if( pnChange ){
54073
 
    assert( pPage->intKey );
54074
 
    *pnChange += pPage->nCell;
54075
 
  }
54076
 
  if( freePageFlag ){
54077
 
    freePage(pPage, &rc);
54078
 
  }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
54079
 
    zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
54080
 
  }
54081
 
 
54082
 
cleardatabasepage_out:
54083
 
  releasePage(pPage);
54084
 
  return rc;
54085
 
}
54086
 
 
54087
 
/*
54088
 
** Delete all information from a single table in the database.  iTable is
54089
 
** the page number of the root of the table.  After this routine returns,
54090
 
** the root page is empty, but still exists.
54091
 
**
54092
 
** This routine will fail with SQLITE_LOCKED if there are any open
54093
 
** read cursors on the table.  Open write cursors are moved to the
54094
 
** root of the table.
54095
 
**
54096
 
** If pnChange is not NULL, then table iTable must be an intkey table. The
54097
 
** integer value pointed to by pnChange is incremented by the number of
54098
 
** entries in the table.
54099
 
*/
54100
 
SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
54101
 
  int rc;
54102
 
  BtShared *pBt = p->pBt;
54103
 
  sqlite3BtreeEnter(p);
54104
 
  assert( p->inTrans==TRANS_WRITE );
54105
 
 
54106
 
  /* Invalidate all incrblob cursors open on table iTable (assuming iTable
54107
 
  ** is the root of a table b-tree - if it is not, the following call is
54108
 
  ** a no-op).  */
54109
 
  invalidateIncrblobCursors(p, 0, 1);
54110
 
 
54111
 
  rc = saveAllCursors(pBt, (Pgno)iTable, 0);
54112
 
  if( SQLITE_OK==rc ){
54113
 
    rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
54114
 
  }
54115
 
  sqlite3BtreeLeave(p);
54116
 
  return rc;
54117
 
}
54118
 
 
54119
 
/*
54120
 
** Erase all information in a table and add the root of the table to
54121
 
** the freelist.  Except, the root of the principle table (the one on
54122
 
** page 1) is never added to the freelist.
54123
 
**
54124
 
** This routine will fail with SQLITE_LOCKED if there are any open
54125
 
** cursors on the table.
54126
 
**
54127
 
** If AUTOVACUUM is enabled and the page at iTable is not the last
54128
 
** root page in the database file, then the last root page 
54129
 
** in the database file is moved into the slot formerly occupied by
54130
 
** iTable and that last slot formerly occupied by the last root page
54131
 
** is added to the freelist instead of iTable.  In this say, all
54132
 
** root pages are kept at the beginning of the database file, which
54133
 
** is necessary for AUTOVACUUM to work right.  *piMoved is set to the 
54134
 
** page number that used to be the last root page in the file before
54135
 
** the move.  If no page gets moved, *piMoved is set to 0.
54136
 
** The last root page is recorded in meta[3] and the value of
54137
 
** meta[3] is updated by this procedure.
54138
 
*/
54139
 
static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
54140
 
  int rc;
54141
 
  MemPage *pPage = 0;
54142
 
  BtShared *pBt = p->pBt;
54143
 
 
54144
 
  assert( sqlite3BtreeHoldsMutex(p) );
54145
 
  assert( p->inTrans==TRANS_WRITE );
54146
 
 
54147
 
  /* It is illegal to drop a table if any cursors are open on the
54148
 
  ** database. This is because in auto-vacuum mode the backend may
54149
 
  ** need to move another root-page to fill a gap left by the deleted
54150
 
  ** root page. If an open cursor was using this page a problem would 
54151
 
  ** occur.
54152
 
  **
54153
 
  ** This error is caught long before control reaches this point.
54154
 
  */
54155
 
  if( NEVER(pBt->pCursor) ){
54156
 
    sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
54157
 
    return SQLITE_LOCKED_SHAREDCACHE;
54158
 
  }
54159
 
 
54160
 
  rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
54161
 
  if( rc ) return rc;
54162
 
  rc = sqlite3BtreeClearTable(p, iTable, 0);
54163
 
  if( rc ){
54164
 
    releasePage(pPage);
54165
 
    return rc;
54166
 
  }
54167
 
 
54168
 
  *piMoved = 0;
54169
 
 
54170
 
  if( iTable>1 ){
54171
 
#ifdef SQLITE_OMIT_AUTOVACUUM
54172
 
    freePage(pPage, &rc);
54173
 
    releasePage(pPage);
54174
 
#else
54175
 
    if( pBt->autoVacuum ){
54176
 
      Pgno maxRootPgno;
54177
 
      sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
54178
 
 
54179
 
      if( iTable==maxRootPgno ){
54180
 
        /* If the table being dropped is the table with the largest root-page
54181
 
        ** number in the database, put the root page on the free list. 
54182
 
        */
54183
 
        freePage(pPage, &rc);
54184
 
        releasePage(pPage);
54185
 
        if( rc!=SQLITE_OK ){
54186
 
          return rc;
54187
 
        }
54188
 
      }else{
54189
 
        /* The table being dropped does not have the largest root-page
54190
 
        ** number in the database. So move the page that does into the 
54191
 
        ** gap left by the deleted root-page.
54192
 
        */
54193
 
        MemPage *pMove;
54194
 
        releasePage(pPage);
54195
 
        rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
54196
 
        if( rc!=SQLITE_OK ){
54197
 
          return rc;
54198
 
        }
54199
 
        rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
54200
 
        releasePage(pMove);
54201
 
        if( rc!=SQLITE_OK ){
54202
 
          return rc;
54203
 
        }
54204
 
        pMove = 0;
54205
 
        rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
54206
 
        freePage(pMove, &rc);
54207
 
        releasePage(pMove);
54208
 
        if( rc!=SQLITE_OK ){
54209
 
          return rc;
54210
 
        }
54211
 
        *piMoved = maxRootPgno;
54212
 
      }
54213
 
 
54214
 
      /* Set the new 'max-root-page' value in the database header. This
54215
 
      ** is the old value less one, less one more if that happens to
54216
 
      ** be a root-page number, less one again if that is the
54217
 
      ** PENDING_BYTE_PAGE.
54218
 
      */
54219
 
      maxRootPgno--;
54220
 
      while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
54221
 
             || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
54222
 
        maxRootPgno--;
54223
 
      }
54224
 
      assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
54225
 
 
54226
 
      rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
54227
 
    }else{
54228
 
      freePage(pPage, &rc);
54229
 
      releasePage(pPage);
54230
 
    }
54231
 
#endif
54232
 
  }else{
54233
 
    /* If sqlite3BtreeDropTable was called on page 1.
54234
 
    ** This really never should happen except in a corrupt
54235
 
    ** database. 
54236
 
    */
54237
 
    zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
54238
 
    releasePage(pPage);
54239
 
  }
54240
 
  return rc;  
54241
 
}
54242
 
SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
54243
 
  int rc;
54244
 
  sqlite3BtreeEnter(p);
54245
 
  rc = btreeDropTable(p, iTable, piMoved);
54246
 
  sqlite3BtreeLeave(p);
54247
 
  return rc;
54248
 
}
54249
 
 
54250
 
 
54251
 
/*
54252
 
** This function may only be called if the b-tree connection already
54253
 
** has a read or write transaction open on the database.
54254
 
**
54255
 
** Read the meta-information out of a database file.  Meta[0]
54256
 
** is the number of free pages currently in the database.  Meta[1]
54257
 
** through meta[15] are available for use by higher layers.  Meta[0]
54258
 
** is read-only, the others are read/write.
54259
 
** 
54260
 
** The schema layer numbers meta values differently.  At the schema
54261
 
** layer (and the SetCookie and ReadCookie opcodes) the number of
54262
 
** free pages is not visible.  So Cookie[0] is the same as Meta[1].
54263
 
*/
54264
 
SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
54265
 
  BtShared *pBt = p->pBt;
54266
 
 
54267
 
  sqlite3BtreeEnter(p);
54268
 
  assert( p->inTrans>TRANS_NONE );
54269
 
  assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
54270
 
  assert( pBt->pPage1 );
54271
 
  assert( idx>=0 && idx<=15 );
54272
 
 
54273
 
  *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
54274
 
 
54275
 
  /* If auto-vacuum is disabled in this build and this is an auto-vacuum
54276
 
  ** database, mark the database as read-only.  */
54277
 
#ifdef SQLITE_OMIT_AUTOVACUUM
54278
 
  if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ) pBt->readOnly = 1;
54279
 
#endif
54280
 
 
54281
 
  sqlite3BtreeLeave(p);
54282
 
}
54283
 
 
54284
 
/*
54285
 
** Write meta-information back into the database.  Meta[0] is
54286
 
** read-only and may not be written.
54287
 
*/
54288
 
SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
54289
 
  BtShared *pBt = p->pBt;
54290
 
  unsigned char *pP1;
54291
 
  int rc;
54292
 
  assert( idx>=1 && idx<=15 );
54293
 
  sqlite3BtreeEnter(p);
54294
 
  assert( p->inTrans==TRANS_WRITE );
54295
 
  assert( pBt->pPage1!=0 );
54296
 
  pP1 = pBt->pPage1->aData;
54297
 
  rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
54298
 
  if( rc==SQLITE_OK ){
54299
 
    put4byte(&pP1[36 + idx*4], iMeta);
54300
 
#ifndef SQLITE_OMIT_AUTOVACUUM
54301
 
    if( idx==BTREE_INCR_VACUUM ){
54302
 
      assert( pBt->autoVacuum || iMeta==0 );
54303
 
      assert( iMeta==0 || iMeta==1 );
54304
 
      pBt->incrVacuum = (u8)iMeta;
54305
 
    }
54306
 
#endif
54307
 
  }
54308
 
  sqlite3BtreeLeave(p);
54309
 
  return rc;
54310
 
}
54311
 
 
54312
 
#ifndef SQLITE_OMIT_BTREECOUNT
54313
 
/*
54314
 
** The first argument, pCur, is a cursor opened on some b-tree. Count the
54315
 
** number of entries in the b-tree and write the result to *pnEntry.
54316
 
**
54317
 
** SQLITE_OK is returned if the operation is successfully executed. 
54318
 
** Otherwise, if an error is encountered (i.e. an IO error or database
54319
 
** corruption) an SQLite error code is returned.
54320
 
*/
54321
 
SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
54322
 
  i64 nEntry = 0;                      /* Value to return in *pnEntry */
54323
 
  int rc;                              /* Return code */
54324
 
  rc = moveToRoot(pCur);
54325
 
 
54326
 
  /* Unless an error occurs, the following loop runs one iteration for each
54327
 
  ** page in the B-Tree structure (not including overflow pages). 
54328
 
  */
54329
 
  while( rc==SQLITE_OK ){
54330
 
    int iIdx;                          /* Index of child node in parent */
54331
 
    MemPage *pPage;                    /* Current page of the b-tree */
54332
 
 
54333
 
    /* If this is a leaf page or the tree is not an int-key tree, then 
54334
 
    ** this page contains countable entries. Increment the entry counter
54335
 
    ** accordingly.
54336
 
    */
54337
 
    pPage = pCur->apPage[pCur->iPage];
54338
 
    if( pPage->leaf || !pPage->intKey ){
54339
 
      nEntry += pPage->nCell;
54340
 
    }
54341
 
 
54342
 
    /* pPage is a leaf node. This loop navigates the cursor so that it 
54343
 
    ** points to the first interior cell that it points to the parent of
54344
 
    ** the next page in the tree that has not yet been visited. The
54345
 
    ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
54346
 
    ** of the page, or to the number of cells in the page if the next page
54347
 
    ** to visit is the right-child of its parent.
54348
 
    **
54349
 
    ** If all pages in the tree have been visited, return SQLITE_OK to the
54350
 
    ** caller.
54351
 
    */
54352
 
    if( pPage->leaf ){
54353
 
      do {
54354
 
        if( pCur->iPage==0 ){
54355
 
          /* All pages of the b-tree have been visited. Return successfully. */
54356
 
          *pnEntry = nEntry;
54357
 
          return SQLITE_OK;
54358
 
        }
54359
 
        moveToParent(pCur);
54360
 
      }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
54361
 
 
54362
 
      pCur->aiIdx[pCur->iPage]++;
54363
 
      pPage = pCur->apPage[pCur->iPage];
54364
 
    }
54365
 
 
54366
 
    /* Descend to the child node of the cell that the cursor currently 
54367
 
    ** points at. This is the right-child if (iIdx==pPage->nCell).
54368
 
    */
54369
 
    iIdx = pCur->aiIdx[pCur->iPage];
54370
 
    if( iIdx==pPage->nCell ){
54371
 
      rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
54372
 
    }else{
54373
 
      rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
54374
 
    }
54375
 
  }
54376
 
 
54377
 
  /* An error has occurred. Return an error code. */
54378
 
  return rc;
54379
 
}
54380
 
#endif
54381
 
 
54382
 
/*
54383
 
** Return the pager associated with a BTree.  This routine is used for
54384
 
** testing and debugging only.
54385
 
*/
54386
 
SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
54387
 
  return p->pBt->pPager;
54388
 
}
54389
 
 
54390
 
#ifndef SQLITE_OMIT_INTEGRITY_CHECK
54391
 
/*
54392
 
** Append a message to the error message string.
54393
 
*/
54394
 
static void checkAppendMsg(
54395
 
  IntegrityCk *pCheck,
54396
 
  char *zMsg1,
54397
 
  const char *zFormat,
54398
 
  ...
54399
 
){
54400
 
  va_list ap;
54401
 
  if( !pCheck->mxErr ) return;
54402
 
  pCheck->mxErr--;
54403
 
  pCheck->nErr++;
54404
 
  va_start(ap, zFormat);
54405
 
  if( pCheck->errMsg.nChar ){
54406
 
    sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
54407
 
  }
54408
 
  if( zMsg1 ){
54409
 
    sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
54410
 
  }
54411
 
  sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
54412
 
  va_end(ap);
54413
 
  if( pCheck->errMsg.mallocFailed ){
54414
 
    pCheck->mallocFailed = 1;
54415
 
  }
54416
 
}
54417
 
#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
54418
 
 
54419
 
#ifndef SQLITE_OMIT_INTEGRITY_CHECK
54420
 
/*
54421
 
** Add 1 to the reference count for page iPage.  If this is the second
54422
 
** reference to the page, add an error message to pCheck->zErrMsg.
54423
 
** Return 1 if there are 2 ore more references to the page and 0 if
54424
 
** if this is the first reference to the page.
54425
 
**
54426
 
** Also check that the page number is in bounds.
54427
 
*/
54428
 
static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
54429
 
  if( iPage==0 ) return 1;
54430
 
  if( iPage>pCheck->nPage ){
54431
 
    checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
54432
 
    return 1;
54433
 
  }
54434
 
  if( pCheck->anRef[iPage]==1 ){
54435
 
    checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
54436
 
    return 1;
54437
 
  }
54438
 
  return  (pCheck->anRef[iPage]++)>1;
54439
 
}
54440
 
 
54441
 
#ifndef SQLITE_OMIT_AUTOVACUUM
54442
 
/*
54443
 
** Check that the entry in the pointer-map for page iChild maps to 
54444
 
** page iParent, pointer type ptrType. If not, append an error message
54445
 
** to pCheck.
54446
 
*/
54447
 
static void checkPtrmap(
54448
 
  IntegrityCk *pCheck,   /* Integrity check context */
54449
 
  Pgno iChild,           /* Child page number */
54450
 
  u8 eType,              /* Expected pointer map type */
54451
 
  Pgno iParent,          /* Expected pointer map parent page number */
54452
 
  char *zContext         /* Context description (used for error msg) */
54453
 
){
54454
 
  int rc;
54455
 
  u8 ePtrmapType;
54456
 
  Pgno iPtrmapParent;
54457
 
 
54458
 
  rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
54459
 
  if( rc!=SQLITE_OK ){
54460
 
    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
54461
 
    checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
54462
 
    return;
54463
 
  }
54464
 
 
54465
 
  if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
54466
 
    checkAppendMsg(pCheck, zContext, 
54467
 
      "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", 
54468
 
      iChild, eType, iParent, ePtrmapType, iPtrmapParent);
54469
 
  }
54470
 
}
54471
 
#endif
54472
 
 
54473
 
/*
54474
 
** Check the integrity of the freelist or of an overflow page list.
54475
 
** Verify that the number of pages on the list is N.
54476
 
*/
54477
 
static void checkList(
54478
 
  IntegrityCk *pCheck,  /* Integrity checking context */
54479
 
  int isFreeList,       /* True for a freelist.  False for overflow page list */
54480
 
  int iPage,            /* Page number for first page in the list */
54481
 
  int N,                /* Expected number of pages in the list */
54482
 
  char *zContext        /* Context for error messages */
54483
 
){
54484
 
  int i;
54485
 
  int expected = N;
54486
 
  int iFirst = iPage;
54487
 
  while( N-- > 0 && pCheck->mxErr ){
54488
 
    DbPage *pOvflPage;
54489
 
    unsigned char *pOvflData;
54490
 
    if( iPage<1 ){
54491
 
      checkAppendMsg(pCheck, zContext,
54492
 
         "%d of %d pages missing from overflow list starting at %d",
54493
 
          N+1, expected, iFirst);
54494
 
      break;
54495
 
    }
54496
 
    if( checkRef(pCheck, iPage, zContext) ) break;
54497
 
    if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
54498
 
      checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
54499
 
      break;
54500
 
    }
54501
 
    pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
54502
 
    if( isFreeList ){
54503
 
      int n = get4byte(&pOvflData[4]);
54504
 
#ifndef SQLITE_OMIT_AUTOVACUUM
54505
 
      if( pCheck->pBt->autoVacuum ){
54506
 
        checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
54507
 
      }
54508
 
#endif
54509
 
      if( n>(int)pCheck->pBt->usableSize/4-2 ){
54510
 
        checkAppendMsg(pCheck, zContext,
54511
 
           "freelist leaf count too big on page %d", iPage);
54512
 
        N--;
54513
 
      }else{
54514
 
        for(i=0; i<n; i++){
54515
 
          Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
54516
 
#ifndef SQLITE_OMIT_AUTOVACUUM
54517
 
          if( pCheck->pBt->autoVacuum ){
54518
 
            checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
54519
 
          }
54520
 
#endif
54521
 
          checkRef(pCheck, iFreePage, zContext);
54522
 
        }
54523
 
        N -= n;
54524
 
      }
54525
 
    }
54526
 
#ifndef SQLITE_OMIT_AUTOVACUUM
54527
 
    else{
54528
 
      /* If this database supports auto-vacuum and iPage is not the last
54529
 
      ** page in this overflow list, check that the pointer-map entry for
54530
 
      ** the following page matches iPage.
54531
 
      */
54532
 
      if( pCheck->pBt->autoVacuum && N>0 ){
54533
 
        i = get4byte(pOvflData);
54534
 
        checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
54535
 
      }
54536
 
    }
54537
 
#endif
54538
 
    iPage = get4byte(pOvflData);
54539
 
    sqlite3PagerUnref(pOvflPage);
54540
 
  }
54541
 
}
54542
 
#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
54543
 
 
54544
 
#ifndef SQLITE_OMIT_INTEGRITY_CHECK
54545
 
/*
54546
 
** Do various sanity checks on a single page of a tree.  Return
54547
 
** the tree depth.  Root pages return 0.  Parents of root pages
54548
 
** return 1, and so forth.
54549
 
** 
54550
 
** These checks are done:
54551
 
**
54552
 
**      1.  Make sure that cells and freeblocks do not overlap
54553
 
**          but combine to completely cover the page.
54554
 
**  NO  2.  Make sure cell keys are in order.
54555
 
**  NO  3.  Make sure no key is less than or equal to zLowerBound.
54556
 
**  NO  4.  Make sure no key is greater than or equal to zUpperBound.
54557
 
**      5.  Check the integrity of overflow pages.
54558
 
**      6.  Recursively call checkTreePage on all children.
54559
 
**      7.  Verify that the depth of all children is the same.
54560
 
**      8.  Make sure this page is at least 33% full or else it is
54561
 
**          the root of the tree.
54562
 
*/
54563
 
static int checkTreePage(
54564
 
  IntegrityCk *pCheck,  /* Context for the sanity check */
54565
 
  int iPage,            /* Page number of the page to check */
54566
 
  char *zParentContext, /* Parent context */
54567
 
  i64 *pnParentMinKey, 
54568
 
  i64 *pnParentMaxKey
54569
 
){
54570
 
  MemPage *pPage;
54571
 
  int i, rc, depth, d2, pgno, cnt;
54572
 
  int hdr, cellStart;
54573
 
  int nCell;
54574
 
  u8 *data;
54575
 
  BtShared *pBt;
54576
 
  int usableSize;
54577
 
  char zContext[100];
54578
 
  char *hit = 0;
54579
 
  i64 nMinKey = 0;
54580
 
  i64 nMaxKey = 0;
54581
 
 
54582
 
  sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
54583
 
 
54584
 
  /* Check that the page exists
54585
 
  */
54586
 
  pBt = pCheck->pBt;
54587
 
  usableSize = pBt->usableSize;
54588
 
  if( iPage==0 ) return 0;
54589
 
  if( checkRef(pCheck, iPage, zParentContext) ) return 0;
54590
 
  if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
54591
 
    checkAppendMsg(pCheck, zContext,
54592
 
       "unable to get the page. error code=%d", rc);
54593
 
    return 0;
54594
 
  }
54595
 
 
54596
 
  /* Clear MemPage.isInit to make sure the corruption detection code in
54597
 
  ** btreeInitPage() is executed.  */
54598
 
  pPage->isInit = 0;
54599
 
  if( (rc = btreeInitPage(pPage))!=0 ){
54600
 
    assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
54601
 
    checkAppendMsg(pCheck, zContext, 
54602
 
                   "btreeInitPage() returns error code %d", rc);
54603
 
    releasePage(pPage);
54604
 
    return 0;
54605
 
  }
54606
 
 
54607
 
  /* Check out all the cells.
54608
 
  */
54609
 
  depth = 0;
54610
 
  for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
54611
 
    u8 *pCell;
54612
 
    u32 sz;
54613
 
    CellInfo info;
54614
 
 
54615
 
    /* Check payload overflow pages
54616
 
    */
54617
 
    sqlite3_snprintf(sizeof(zContext), zContext,
54618
 
             "On tree page %d cell %d: ", iPage, i);
54619
 
    pCell = findCell(pPage,i);
54620
 
    btreeParseCellPtr(pPage, pCell, &info);
54621
 
    sz = info.nData;
54622
 
    if( !pPage->intKey ) sz += (int)info.nKey;
54623
 
    /* For intKey pages, check that the keys are in order.
54624
 
    */
54625
 
    else if( i==0 ) nMinKey = nMaxKey = info.nKey;
54626
 
    else{
54627
 
      if( info.nKey <= nMaxKey ){
54628
 
        checkAppendMsg(pCheck, zContext, 
54629
 
            "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey);
54630
 
      }
54631
 
      nMaxKey = info.nKey;
54632
 
    }
54633
 
    assert( sz==info.nPayload );
54634
 
    if( (sz>info.nLocal) 
54635
 
     && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
54636
 
    ){
54637
 
      int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
54638
 
      Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
54639
 
#ifndef SQLITE_OMIT_AUTOVACUUM
54640
 
      if( pBt->autoVacuum ){
54641
 
        checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
54642
 
      }
54643
 
#endif
54644
 
      checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
54645
 
    }
54646
 
 
54647
 
    /* Check sanity of left child page.
54648
 
    */
54649
 
    if( !pPage->leaf ){
54650
 
      pgno = get4byte(pCell);
54651
 
#ifndef SQLITE_OMIT_AUTOVACUUM
54652
 
      if( pBt->autoVacuum ){
54653
 
        checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
54654
 
      }
54655
 
#endif
54656
 
      d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey);
54657
 
      if( i>0 && d2!=depth ){
54658
 
        checkAppendMsg(pCheck, zContext, "Child page depth differs");
54659
 
      }
54660
 
      depth = d2;
54661
 
    }
54662
 
  }
54663
 
 
54664
 
  if( !pPage->leaf ){
54665
 
    pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
54666
 
    sqlite3_snprintf(sizeof(zContext), zContext, 
54667
 
                     "On page %d at right child: ", iPage);
54668
 
#ifndef SQLITE_OMIT_AUTOVACUUM
54669
 
    if( pBt->autoVacuum ){
54670
 
      checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
54671
 
    }
54672
 
#endif
54673
 
    checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
54674
 
  }
54675
 
 
54676
 
  /* For intKey leaf pages, check that the min/max keys are in order
54677
 
  ** with any left/parent/right pages.
54678
 
  */
54679
 
  if( pPage->leaf && pPage->intKey ){
54680
 
    /* if we are a left child page */
54681
 
    if( pnParentMinKey ){
54682
 
      /* if we are the left most child page */
54683
 
      if( !pnParentMaxKey ){
54684
 
        if( nMaxKey > *pnParentMinKey ){
54685
 
          checkAppendMsg(pCheck, zContext, 
54686
 
              "Rowid %lld out of order (max larger than parent min of %lld)",
54687
 
              nMaxKey, *pnParentMinKey);
54688
 
        }
54689
 
      }else{
54690
 
        if( nMinKey <= *pnParentMinKey ){
54691
 
          checkAppendMsg(pCheck, zContext, 
54692
 
              "Rowid %lld out of order (min less than parent min of %lld)",
54693
 
              nMinKey, *pnParentMinKey);
54694
 
        }
54695
 
        if( nMaxKey > *pnParentMaxKey ){
54696
 
          checkAppendMsg(pCheck, zContext, 
54697
 
              "Rowid %lld out of order (max larger than parent max of %lld)",
54698
 
              nMaxKey, *pnParentMaxKey);
54699
 
        }
54700
 
        *pnParentMinKey = nMaxKey;
54701
 
      }
54702
 
    /* else if we're a right child page */
54703
 
    } else if( pnParentMaxKey ){
54704
 
      if( nMinKey <= *pnParentMaxKey ){
54705
 
        checkAppendMsg(pCheck, zContext, 
54706
 
            "Rowid %lld out of order (min less than parent max of %lld)",
54707
 
            nMinKey, *pnParentMaxKey);
54708
 
      }
54709
 
    }
54710
 
  }
54711
 
 
54712
 
  /* Check for complete coverage of the page
54713
 
  */
54714
 
  data = pPage->aData;
54715
 
  hdr = pPage->hdrOffset;
54716
 
  hit = sqlite3PageMalloc( pBt->pageSize );
54717
 
  if( hit==0 ){
54718
 
    pCheck->mallocFailed = 1;
54719
 
  }else{
54720
 
    int contentOffset = get2byteNotZero(&data[hdr+5]);
54721
 
    assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
54722
 
    memset(hit+contentOffset, 0, usableSize-contentOffset);
54723
 
    memset(hit, 1, contentOffset);
54724
 
    nCell = get2byte(&data[hdr+3]);
54725
 
    cellStart = hdr + 12 - 4*pPage->leaf;
54726
 
    for(i=0; i<nCell; i++){
54727
 
      int pc = get2byte(&data[cellStart+i*2]);
54728
 
      u32 size = 65536;
54729
 
      int j;
54730
 
      if( pc<=usableSize-4 ){
54731
 
        size = cellSizePtr(pPage, &data[pc]);
54732
 
      }
54733
 
      if( (int)(pc+size-1)>=usableSize ){
54734
 
        checkAppendMsg(pCheck, 0, 
54735
 
            "Corruption detected in cell %d on page %d",i,iPage);
54736
 
      }else{
54737
 
        for(j=pc+size-1; j>=pc; j--) hit[j]++;
54738
 
      }
54739
 
    }
54740
 
    i = get2byte(&data[hdr+1]);
54741
 
    while( i>0 ){
54742
 
      int size, j;
54743
 
      assert( i<=usableSize-4 );     /* Enforced by btreeInitPage() */
54744
 
      size = get2byte(&data[i+2]);
54745
 
      assert( i+size<=usableSize );  /* Enforced by btreeInitPage() */
54746
 
      for(j=i+size-1; j>=i; j--) hit[j]++;
54747
 
      j = get2byte(&data[i]);
54748
 
      assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
54749
 
      assert( j<=usableSize-4 );   /* Enforced by btreeInitPage() */
54750
 
      i = j;
54751
 
    }
54752
 
    for(i=cnt=0; i<usableSize; i++){
54753
 
      if( hit[i]==0 ){
54754
 
        cnt++;
54755
 
      }else if( hit[i]>1 ){
54756
 
        checkAppendMsg(pCheck, 0,
54757
 
          "Multiple uses for byte %d of page %d", i, iPage);
54758
 
        break;
54759
 
      }
54760
 
    }
54761
 
    if( cnt!=data[hdr+7] ){
54762
 
      checkAppendMsg(pCheck, 0, 
54763
 
          "Fragmentation of %d bytes reported as %d on page %d",
54764
 
          cnt, data[hdr+7], iPage);
54765
 
    }
54766
 
  }
54767
 
  sqlite3PageFree(hit);
54768
 
  releasePage(pPage);
54769
 
  return depth+1;
54770
 
}
54771
 
#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
54772
 
 
54773
 
#ifndef SQLITE_OMIT_INTEGRITY_CHECK
54774
 
/*
54775
 
** This routine does a complete check of the given BTree file.  aRoot[] is
54776
 
** an array of pages numbers were each page number is the root page of
54777
 
** a table.  nRoot is the number of entries in aRoot.
54778
 
**
54779
 
** A read-only or read-write transaction must be opened before calling
54780
 
** this function.
54781
 
**
54782
 
** Write the number of error seen in *pnErr.  Except for some memory
54783
 
** allocation errors,  an error message held in memory obtained from
54784
 
** malloc is returned if *pnErr is non-zero.  If *pnErr==0 then NULL is
54785
 
** returned.  If a memory allocation error occurs, NULL is returned.
54786
 
*/
54787
 
SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
54788
 
  Btree *p,     /* The btree to be checked */
54789
 
  int *aRoot,   /* An array of root pages numbers for individual trees */
54790
 
  int nRoot,    /* Number of entries in aRoot[] */
54791
 
  int mxErr,    /* Stop reporting errors after this many */
54792
 
  int *pnErr    /* Write number of errors seen to this variable */
54793
 
){
54794
 
  Pgno i;
54795
 
  int nRef;
54796
 
  IntegrityCk sCheck;
54797
 
  BtShared *pBt = p->pBt;
54798
 
  char zErr[100];
54799
 
 
54800
 
  sqlite3BtreeEnter(p);
54801
 
  assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
54802
 
  nRef = sqlite3PagerRefcount(pBt->pPager);
54803
 
  sCheck.pBt = pBt;
54804
 
  sCheck.pPager = pBt->pPager;
54805
 
  sCheck.nPage = btreePagecount(sCheck.pBt);
54806
 
  sCheck.mxErr = mxErr;
54807
 
  sCheck.nErr = 0;
54808
 
  sCheck.mallocFailed = 0;
54809
 
  *pnErr = 0;
54810
 
  if( sCheck.nPage==0 ){
54811
 
    sqlite3BtreeLeave(p);
54812
 
    return 0;
54813
 
  }
54814
 
  sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
54815
 
  if( !sCheck.anRef ){
54816
 
    *pnErr = 1;
54817
 
    sqlite3BtreeLeave(p);
54818
 
    return 0;
54819
 
  }
54820
 
  for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
54821
 
  i = PENDING_BYTE_PAGE(pBt);
54822
 
  if( i<=sCheck.nPage ){
54823
 
    sCheck.anRef[i] = 1;
54824
 
  }
54825
 
  sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
54826
 
  sCheck.errMsg.useMalloc = 2;
54827
 
 
54828
 
  /* Check the integrity of the freelist
54829
 
  */
54830
 
  checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
54831
 
            get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
54832
 
 
54833
 
  /* Check all the tables.
54834
 
  */
54835
 
  for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
54836
 
    if( aRoot[i]==0 ) continue;
54837
 
#ifndef SQLITE_OMIT_AUTOVACUUM
54838
 
    if( pBt->autoVacuum && aRoot[i]>1 ){
54839
 
      checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
54840
 
    }
54841
 
#endif
54842
 
    checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL);
54843
 
  }
54844
 
 
54845
 
  /* Make sure every page in the file is referenced
54846
 
  */
54847
 
  for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
54848
 
#ifdef SQLITE_OMIT_AUTOVACUUM
54849
 
    if( sCheck.anRef[i]==0 ){
54850
 
      checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
54851
 
    }
54852
 
#else
54853
 
    /* If the database supports auto-vacuum, make sure no tables contain
54854
 
    ** references to pointer-map pages.
54855
 
    */
54856
 
    if( sCheck.anRef[i]==0 && 
54857
 
       (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
54858
 
      checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
54859
 
    }
54860
 
    if( sCheck.anRef[i]!=0 && 
54861
 
       (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
54862
 
      checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
54863
 
    }
54864
 
#endif
54865
 
  }
54866
 
 
54867
 
  /* Make sure this analysis did not leave any unref() pages.
54868
 
  ** This is an internal consistency check; an integrity check
54869
 
  ** of the integrity check.
54870
 
  */
54871
 
  if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
54872
 
    checkAppendMsg(&sCheck, 0, 
54873
 
      "Outstanding page count goes from %d to %d during this analysis",
54874
 
      nRef, sqlite3PagerRefcount(pBt->pPager)
54875
 
    );
54876
 
  }
54877
 
 
54878
 
  /* Clean  up and report errors.
54879
 
  */
54880
 
  sqlite3BtreeLeave(p);
54881
 
  sqlite3_free(sCheck.anRef);
54882
 
  if( sCheck.mallocFailed ){
54883
 
    sqlite3StrAccumReset(&sCheck.errMsg);
54884
 
    *pnErr = sCheck.nErr+1;
54885
 
    return 0;
54886
 
  }
54887
 
  *pnErr = sCheck.nErr;
54888
 
  if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
54889
 
  return sqlite3StrAccumFinish(&sCheck.errMsg);
54890
 
}
54891
 
#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
54892
 
 
54893
 
/*
54894
 
** Return the full pathname of the underlying database file.
54895
 
**
54896
 
** The pager filename is invariant as long as the pager is
54897
 
** open so it is safe to access without the BtShared mutex.
54898
 
*/
54899
 
SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
54900
 
  assert( p->pBt->pPager!=0 );
54901
 
  return sqlite3PagerFilename(p->pBt->pPager);
54902
 
}
54903
 
 
54904
 
/*
54905
 
** Return the pathname of the journal file for this database. The return
54906
 
** value of this routine is the same regardless of whether the journal file
54907
 
** has been created or not.
54908
 
**
54909
 
** The pager journal filename is invariant as long as the pager is
54910
 
** open so it is safe to access without the BtShared mutex.
54911
 
*/
54912
 
SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
54913
 
  assert( p->pBt->pPager!=0 );
54914
 
  return sqlite3PagerJournalname(p->pBt->pPager);
54915
 
}
54916
 
 
54917
 
/*
54918
 
** Return non-zero if a transaction is active.
54919
 
*/
54920
 
SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
54921
 
  assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
54922
 
  return (p && (p->inTrans==TRANS_WRITE));
54923
 
}
54924
 
 
54925
 
#ifndef SQLITE_OMIT_WAL
54926
 
/*
54927
 
** Run a checkpoint on the Btree passed as the first argument.
54928
 
**
54929
 
** Return SQLITE_LOCKED if this or any other connection has an open 
54930
 
** transaction on the shared-cache the argument Btree is connected to.
54931
 
**
54932
 
** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
54933
 
*/
54934
 
SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
54935
 
  int rc = SQLITE_OK;
54936
 
  if( p ){
54937
 
    BtShared *pBt = p->pBt;
54938
 
    sqlite3BtreeEnter(p);
54939
 
    if( pBt->inTransaction!=TRANS_NONE ){
54940
 
      rc = SQLITE_LOCKED;
54941
 
    }else{
54942
 
      rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
54943
 
    }
54944
 
    sqlite3BtreeLeave(p);
54945
 
  }
54946
 
  return rc;
54947
 
}
54948
 
#endif
54949
 
 
54950
 
/*
54951
 
** Return non-zero if a read (or write) transaction is active.
54952
 
*/
54953
 
SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
54954
 
  assert( p );
54955
 
  assert( sqlite3_mutex_held(p->db->mutex) );
54956
 
  return p->inTrans!=TRANS_NONE;
54957
 
}
54958
 
 
54959
 
SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
54960
 
  assert( p );
54961
 
  assert( sqlite3_mutex_held(p->db->mutex) );
54962
 
  return p->nBackup!=0;
54963
 
}
54964
 
 
54965
 
/*
54966
 
** This function returns a pointer to a blob of memory associated with
54967
 
** a single shared-btree. The memory is used by client code for its own
54968
 
** purposes (for example, to store a high-level schema associated with 
54969
 
** the shared-btree). The btree layer manages reference counting issues.
54970
 
**
54971
 
** The first time this is called on a shared-btree, nBytes bytes of memory
54972
 
** are allocated, zeroed, and returned to the caller. For each subsequent 
54973
 
** call the nBytes parameter is ignored and a pointer to the same blob
54974
 
** of memory returned. 
54975
 
**
54976
 
** If the nBytes parameter is 0 and the blob of memory has not yet been
54977
 
** allocated, a null pointer is returned. If the blob has already been
54978
 
** allocated, it is returned as normal.
54979
 
**
54980
 
** Just before the shared-btree is closed, the function passed as the 
54981
 
** xFree argument when the memory allocation was made is invoked on the 
54982
 
** blob of allocated memory. The xFree function should not call sqlite3_free()
54983
 
** on the memory, the btree layer does that.
54984
 
*/
54985
 
SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
54986
 
  BtShared *pBt = p->pBt;
54987
 
  sqlite3BtreeEnter(p);
54988
 
  if( !pBt->pSchema && nBytes ){
54989
 
    pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
54990
 
    pBt->xFreeSchema = xFree;
54991
 
  }
54992
 
  sqlite3BtreeLeave(p);
54993
 
  return pBt->pSchema;
54994
 
}
54995
 
 
54996
 
/*
54997
 
** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared 
54998
 
** btree as the argument handle holds an exclusive lock on the 
54999
 
** sqlite_master table. Otherwise SQLITE_OK.
55000
 
*/
55001
 
SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
55002
 
  int rc;
55003
 
  assert( sqlite3_mutex_held(p->db->mutex) );
55004
 
  sqlite3BtreeEnter(p);
55005
 
  rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
55006
 
  assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
55007
 
  sqlite3BtreeLeave(p);
55008
 
  return rc;
55009
 
}
55010
 
 
55011
 
 
55012
 
#ifndef SQLITE_OMIT_SHARED_CACHE
55013
 
/*
55014
 
** Obtain a lock on the table whose root page is iTab.  The
55015
 
** lock is a write lock if isWritelock is true or a read lock
55016
 
** if it is false.
55017
 
*/
55018
 
SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
55019
 
  int rc = SQLITE_OK;
55020
 
  assert( p->inTrans!=TRANS_NONE );
55021
 
  if( p->sharable ){
55022
 
    u8 lockType = READ_LOCK + isWriteLock;
55023
 
    assert( READ_LOCK+1==WRITE_LOCK );
55024
 
    assert( isWriteLock==0 || isWriteLock==1 );
55025
 
 
55026
 
    sqlite3BtreeEnter(p);
55027
 
    rc = querySharedCacheTableLock(p, iTab, lockType);
55028
 
    if( rc==SQLITE_OK ){
55029
 
      rc = setSharedCacheTableLock(p, iTab, lockType);
55030
 
    }
55031
 
    sqlite3BtreeLeave(p);
55032
 
  }
55033
 
  return rc;
55034
 
}
55035
 
#endif
55036
 
 
55037
 
#ifndef SQLITE_OMIT_INCRBLOB
55038
 
/*
55039
 
** Argument pCsr must be a cursor opened for writing on an 
55040
 
** INTKEY table currently pointing at a valid table entry. 
55041
 
** This function modifies the data stored as part of that entry.
55042
 
**
55043
 
** Only the data content may only be modified, it is not possible to 
55044
 
** change the length of the data stored. If this function is called with
55045
 
** parameters that attempt to write past the end of the existing data,
55046
 
** no modifications are made and SQLITE_CORRUPT is returned.
55047
 
*/
55048
 
SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
55049
 
  int rc;
55050
 
  assert( cursorHoldsMutex(pCsr) );
55051
 
  assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
55052
 
  assert( pCsr->isIncrblobHandle );
55053
 
 
55054
 
  rc = restoreCursorPosition(pCsr);
55055
 
  if( rc!=SQLITE_OK ){
55056
 
    return rc;
55057
 
  }
55058
 
  assert( pCsr->eState!=CURSOR_REQUIRESEEK );
55059
 
  if( pCsr->eState!=CURSOR_VALID ){
55060
 
    return SQLITE_ABORT;
55061
 
  }
55062
 
 
55063
 
  /* Check some assumptions: 
55064
 
  **   (a) the cursor is open for writing,
55065
 
  **   (b) there is a read/write transaction open,
55066
 
  **   (c) the connection holds a write-lock on the table (if required),
55067
 
  **   (d) there are no conflicting read-locks, and
55068
 
  **   (e) the cursor points at a valid row of an intKey table.
55069
 
  */
55070
 
  if( !pCsr->wrFlag ){
55071
 
    return SQLITE_READONLY;
55072
 
  }
55073
 
  assert( !pCsr->pBt->readOnly && pCsr->pBt->inTransaction==TRANS_WRITE );
55074
 
  assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
55075
 
  assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
55076
 
  assert( pCsr->apPage[pCsr->iPage]->intKey );
55077
 
 
55078
 
  return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
55079
 
}
55080
 
 
55081
 
/* 
55082
 
** Set a flag on this cursor to cache the locations of pages from the 
55083
 
** overflow list for the current row. This is used by cursors opened
55084
 
** for incremental blob IO only.
55085
 
**
55086
 
** This function sets a flag only. The actual page location cache
55087
 
** (stored in BtCursor.aOverflow[]) is allocated and used by function
55088
 
** accessPayload() (the worker function for sqlite3BtreeData() and
55089
 
** sqlite3BtreePutData()).
55090
 
*/
55091
 
SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){
55092
 
  assert( cursorHoldsMutex(pCur) );
55093
 
  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
55094
 
  invalidateOverflowCache(pCur);
55095
 
  pCur->isIncrblobHandle = 1;
55096
 
}
55097
 
#endif
55098
 
 
55099
 
/*
55100
 
** Set both the "read version" (single byte at byte offset 18) and 
55101
 
** "write version" (single byte at byte offset 19) fields in the database
55102
 
** header to iVersion.
55103
 
*/
55104
 
SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
55105
 
  BtShared *pBt = pBtree->pBt;
55106
 
  int rc;                         /* Return code */
55107
 
 
55108
 
  assert( pBtree->inTrans==TRANS_NONE );
55109
 
  assert( iVersion==1 || iVersion==2 );
55110
 
 
55111
 
  /* If setting the version fields to 1, do not automatically open the
55112
 
  ** WAL connection, even if the version fields are currently set to 2.
55113
 
  */
55114
 
  pBt->doNotUseWAL = (u8)(iVersion==1);
55115
 
 
55116
 
  rc = sqlite3BtreeBeginTrans(pBtree, 0);
55117
 
  if( rc==SQLITE_OK ){
55118
 
    u8 *aData = pBt->pPage1->aData;
55119
 
    if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
55120
 
      rc = sqlite3BtreeBeginTrans(pBtree, 2);
55121
 
      if( rc==SQLITE_OK ){
55122
 
        rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
55123
 
        if( rc==SQLITE_OK ){
55124
 
          aData[18] = (u8)iVersion;
55125
 
          aData[19] = (u8)iVersion;
55126
 
        }
55127
 
      }
55128
 
    }
55129
 
  }
55130
 
 
55131
 
  pBt->doNotUseWAL = 0;
55132
 
  return rc;
55133
 
}
55134
 
 
55135
 
/************** End of btree.c ***********************************************/
55136
 
/************** Begin file backup.c ******************************************/
55137
 
/*
55138
 
** 2009 January 28
55139
 
**
55140
 
** The author disclaims copyright to this source code.  In place of
55141
 
** a legal notice, here is a blessing:
55142
 
**
55143
 
**    May you do good and not evil.
55144
 
**    May you find forgiveness for yourself and forgive others.
55145
 
**    May you share freely, never taking more than you give.
55146
 
**
55147
 
*************************************************************************
55148
 
** This file contains the implementation of the sqlite3_backup_XXX() 
55149
 
** API functions and the related features.
55150
 
*/
55151
 
 
55152
 
/* Macro to find the minimum of two numeric values.
55153
 
*/
55154
 
#ifndef MIN
55155
 
# define MIN(x,y) ((x)<(y)?(x):(y))
55156
 
#endif
55157
 
 
55158
 
/*
55159
 
** Structure allocated for each backup operation.
55160
 
*/
55161
 
struct sqlite3_backup {
55162
 
  sqlite3* pDestDb;        /* Destination database handle */
55163
 
  Btree *pDest;            /* Destination b-tree file */
55164
 
  u32 iDestSchema;         /* Original schema cookie in destination */
55165
 
  int bDestLocked;         /* True once a write-transaction is open on pDest */
55166
 
 
55167
 
  Pgno iNext;              /* Page number of the next source page to copy */
55168
 
  sqlite3* pSrcDb;         /* Source database handle */
55169
 
  Btree *pSrc;             /* Source b-tree file */
55170
 
 
55171
 
  int rc;                  /* Backup process error code */
55172
 
 
55173
 
  /* These two variables are set by every call to backup_step(). They are
55174
 
  ** read by calls to backup_remaining() and backup_pagecount().
55175
 
  */
55176
 
  Pgno nRemaining;         /* Number of pages left to copy */
55177
 
  Pgno nPagecount;         /* Total number of pages to copy */
55178
 
 
55179
 
  int isAttached;          /* True once backup has been registered with pager */
55180
 
  sqlite3_backup *pNext;   /* Next backup associated with source pager */
55181
 
};
55182
 
 
55183
 
/*
55184
 
** THREAD SAFETY NOTES:
55185
 
**
55186
 
**   Once it has been created using backup_init(), a single sqlite3_backup
55187
 
**   structure may be accessed via two groups of thread-safe entry points:
55188
 
**
55189
 
**     * Via the sqlite3_backup_XXX() API function backup_step() and 
55190
 
**       backup_finish(). Both these functions obtain the source database
55191
 
**       handle mutex and the mutex associated with the source BtShared 
55192
 
**       structure, in that order.
55193
 
**
55194
 
**     * Via the BackupUpdate() and BackupRestart() functions, which are
55195
 
**       invoked by the pager layer to report various state changes in
55196
 
**       the page cache associated with the source database. The mutex
55197
 
**       associated with the source database BtShared structure will always 
55198
 
**       be held when either of these functions are invoked.
55199
 
**
55200
 
**   The other sqlite3_backup_XXX() API functions, backup_remaining() and
55201
 
**   backup_pagecount() are not thread-safe functions. If they are called
55202
 
**   while some other thread is calling backup_step() or backup_finish(),
55203
 
**   the values returned may be invalid. There is no way for a call to
55204
 
**   BackupUpdate() or BackupRestart() to interfere with backup_remaining()
55205
 
**   or backup_pagecount().
55206
 
**
55207
 
**   Depending on the SQLite configuration, the database handles and/or
55208
 
**   the Btree objects may have their own mutexes that require locking.
55209
 
**   Non-sharable Btrees (in-memory databases for example), do not have
55210
 
**   associated mutexes.
55211
 
*/
55212
 
 
55213
 
/*
55214
 
** Return a pointer corresponding to database zDb (i.e. "main", "temp")
55215
 
** in connection handle pDb. If such a database cannot be found, return
55216
 
** a NULL pointer and write an error message to pErrorDb.
55217
 
**
55218
 
** If the "temp" database is requested, it may need to be opened by this 
55219
 
** function. If an error occurs while doing so, return 0 and write an 
55220
 
** error message to pErrorDb.
55221
 
*/
55222
 
static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
55223
 
  int i = sqlite3FindDbName(pDb, zDb);
55224
 
 
55225
 
  if( i==1 ){
55226
 
    Parse *pParse;
55227
 
    int rc = 0;
55228
 
    pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
55229
 
    if( pParse==0 ){
55230
 
      sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
55231
 
      rc = SQLITE_NOMEM;
55232
 
    }else{
55233
 
      pParse->db = pDb;
55234
 
      if( sqlite3OpenTempDatabase(pParse) ){
55235
 
        sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
55236
 
        rc = SQLITE_ERROR;
55237
 
      }
55238
 
      sqlite3DbFree(pErrorDb, pParse->zErrMsg);
55239
 
      sqlite3StackFree(pErrorDb, pParse);
55240
 
    }
55241
 
    if( rc ){
55242
 
      return 0;
55243
 
    }
55244
 
  }
55245
 
 
55246
 
  if( i<0 ){
55247
 
    sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
55248
 
    return 0;
55249
 
  }
55250
 
 
55251
 
  return pDb->aDb[i].pBt;
55252
 
}
55253
 
 
55254
 
/*
55255
 
** Attempt to set the page size of the destination to match the page size
55256
 
** of the source.
55257
 
*/
55258
 
static int setDestPgsz(sqlite3_backup *p){
55259
 
  int rc;
55260
 
  rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
55261
 
  return rc;
55262
 
}
55263
 
 
55264
 
/*
55265
 
** Create an sqlite3_backup process to copy the contents of zSrcDb from
55266
 
** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
55267
 
** a pointer to the new sqlite3_backup object.
55268
 
**
55269
 
** If an error occurs, NULL is returned and an error code and error message
55270
 
** stored in database handle pDestDb.
55271
 
*/
55272
 
SQLITE_API sqlite3_backup *sqlite3_backup_init(
55273
 
  sqlite3* pDestDb,                     /* Database to write to */
55274
 
  const char *zDestDb,                  /* Name of database within pDestDb */
55275
 
  sqlite3* pSrcDb,                      /* Database connection to read from */
55276
 
  const char *zSrcDb                    /* Name of database within pSrcDb */
55277
 
){
55278
 
  sqlite3_backup *p;                    /* Value to return */
55279
 
 
55280
 
  /* Lock the source database handle. The destination database
55281
 
  ** handle is not locked in this routine, but it is locked in
55282
 
  ** sqlite3_backup_step(). The user is required to ensure that no
55283
 
  ** other thread accesses the destination handle for the duration
55284
 
  ** of the backup operation.  Any attempt to use the destination
55285
 
  ** database connection while a backup is in progress may cause
55286
 
  ** a malfunction or a deadlock.
55287
 
  */
55288
 
  sqlite3_mutex_enter(pSrcDb->mutex);
55289
 
  sqlite3_mutex_enter(pDestDb->mutex);
55290
 
 
55291
 
  if( pSrcDb==pDestDb ){
55292
 
    sqlite3Error(
55293
 
        pDestDb, SQLITE_ERROR, "source and destination must be distinct"
55294
 
    );
55295
 
    p = 0;
55296
 
  }else {
55297
 
    /* Allocate space for a new sqlite3_backup object...
55298
 
    ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
55299
 
    ** call to sqlite3_backup_init() and is destroyed by a call to
55300
 
    ** sqlite3_backup_finish(). */
55301
 
    p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup));
55302
 
    if( !p ){
55303
 
      sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
55304
 
    }
55305
 
  }
55306
 
 
55307
 
  /* If the allocation succeeded, populate the new object. */
55308
 
  if( p ){
55309
 
    memset(p, 0, sizeof(sqlite3_backup));
55310
 
    p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
55311
 
    p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
55312
 
    p->pDestDb = pDestDb;
55313
 
    p->pSrcDb = pSrcDb;
55314
 
    p->iNext = 1;
55315
 
    p->isAttached = 0;
55316
 
 
55317
 
    if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){
55318
 
      /* One (or both) of the named databases did not exist or an OOM
55319
 
      ** error was hit.  The error has already been written into the
55320
 
      ** pDestDb handle.  All that is left to do here is free the
55321
 
      ** sqlite3_backup structure.
55322
 
      */
55323
 
      sqlite3_free(p);
55324
 
      p = 0;
55325
 
    }
55326
 
  }
55327
 
  if( p ){
55328
 
    p->pSrc->nBackup++;
55329
 
  }
55330
 
 
55331
 
  sqlite3_mutex_leave(pDestDb->mutex);
55332
 
  sqlite3_mutex_leave(pSrcDb->mutex);
55333
 
  return p;
55334
 
}
55335
 
 
55336
 
/*
55337
 
** Argument rc is an SQLite error code. Return true if this error is 
55338
 
** considered fatal if encountered during a backup operation. All errors
55339
 
** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
55340
 
*/
55341
 
static int isFatalError(int rc){
55342
 
  return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
55343
 
}
55344
 
 
55345
 
/*
55346
 
** Parameter zSrcData points to a buffer containing the data for 
55347
 
** page iSrcPg from the source database. Copy this data into the 
55348
 
** destination database.
55349
 
*/
55350
 
static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){
55351
 
  Pager * const pDestPager = sqlite3BtreePager(p->pDest);
55352
 
  const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
55353
 
  int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
55354
 
  const int nCopy = MIN(nSrcPgsz, nDestPgsz);
55355
 
  const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
55356
 
#ifdef SQLITE_HAS_CODEC
55357
 
  int nSrcReserve = sqlite3BtreeGetReserve(p->pSrc);
55358
 
  int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
55359
 
#endif
55360
 
 
55361
 
  int rc = SQLITE_OK;
55362
 
  i64 iOff;
55363
 
 
55364
 
  assert( p->bDestLocked );
55365
 
  assert( !isFatalError(p->rc) );
55366
 
  assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
55367
 
  assert( zSrcData );
55368
 
 
55369
 
  /* Catch the case where the destination is an in-memory database and the
55370
 
  ** page sizes of the source and destination differ. 
55371
 
  */
55372
 
  if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
55373
 
    rc = SQLITE_READONLY;
55374
 
  }
55375
 
 
55376
 
#ifdef SQLITE_HAS_CODEC
55377
 
  /* Backup is not possible if the page size of the destination is changing
55378
 
  ** and a codec is in use.
55379
 
  */
55380
 
  if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
55381
 
    rc = SQLITE_READONLY;
55382
 
  }
55383
 
 
55384
 
  /* Backup is not possible if the number of bytes of reserve space differ
55385
 
  ** between source and destination.  If there is a difference, try to
55386
 
  ** fix the destination to agree with the source.  If that is not possible,
55387
 
  ** then the backup cannot proceed.
55388
 
  */
55389
 
  if( nSrcReserve!=nDestReserve ){
55390
 
    u32 newPgsz = nSrcPgsz;
55391
 
    rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
55392
 
    if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
55393
 
  }
55394
 
#endif
55395
 
 
55396
 
  /* This loop runs once for each destination page spanned by the source 
55397
 
  ** page. For each iteration, variable iOff is set to the byte offset
55398
 
  ** of the destination page.
55399
 
  */
55400
 
  for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
55401
 
    DbPage *pDestPg = 0;
55402
 
    Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
55403
 
    if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
55404
 
    if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
55405
 
     && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
55406
 
    ){
55407
 
      const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
55408
 
      u8 *zDestData = sqlite3PagerGetData(pDestPg);
55409
 
      u8 *zOut = &zDestData[iOff%nDestPgsz];
55410
 
 
55411
 
      /* Copy the data from the source page into the destination page.
55412
 
      ** Then clear the Btree layer MemPage.isInit flag. Both this module
55413
 
      ** and the pager code use this trick (clearing the first byte
55414
 
      ** of the page 'extra' space to invalidate the Btree layers
55415
 
      ** cached parse of the page). MemPage.isInit is marked 
55416
 
      ** "MUST BE FIRST" for this purpose.
55417
 
      */
55418
 
      memcpy(zOut, zIn, nCopy);
55419
 
      ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
55420
 
    }
55421
 
    sqlite3PagerUnref(pDestPg);
55422
 
  }
55423
 
 
55424
 
  return rc;
55425
 
}
55426
 
 
55427
 
/*
55428
 
** If pFile is currently larger than iSize bytes, then truncate it to
55429
 
** exactly iSize bytes. If pFile is not larger than iSize bytes, then
55430
 
** this function is a no-op.
55431
 
**
55432
 
** Return SQLITE_OK if everything is successful, or an SQLite error 
55433
 
** code if an error occurs.
55434
 
*/
55435
 
static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
55436
 
  i64 iCurrent;
55437
 
  int rc = sqlite3OsFileSize(pFile, &iCurrent);
55438
 
  if( rc==SQLITE_OK && iCurrent>iSize ){
55439
 
    rc = sqlite3OsTruncate(pFile, iSize);
55440
 
  }
55441
 
  return rc;
55442
 
}
55443
 
 
55444
 
/*
55445
 
** Register this backup object with the associated source pager for
55446
 
** callbacks when pages are changed or the cache invalidated.
55447
 
*/
55448
 
static void attachBackupObject(sqlite3_backup *p){
55449
 
  sqlite3_backup **pp;
55450
 
  assert( sqlite3BtreeHoldsMutex(p->pSrc) );
55451
 
  pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
55452
 
  p->pNext = *pp;
55453
 
  *pp = p;
55454
 
  p->isAttached = 1;
55455
 
}
55456
 
 
55457
 
/*
55458
 
** Copy nPage pages from the source b-tree to the destination.
55459
 
*/
55460
 
SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
55461
 
  int rc;
55462
 
  int destMode;       /* Destination journal mode */
55463
 
  int pgszSrc = 0;    /* Source page size */
55464
 
  int pgszDest = 0;   /* Destination page size */
55465
 
 
55466
 
  sqlite3_mutex_enter(p->pSrcDb->mutex);
55467
 
  sqlite3BtreeEnter(p->pSrc);
55468
 
  if( p->pDestDb ){
55469
 
    sqlite3_mutex_enter(p->pDestDb->mutex);
55470
 
  }
55471
 
 
55472
 
  rc = p->rc;
55473
 
  if( !isFatalError(rc) ){
55474
 
    Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
55475
 
    Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
55476
 
    int ii;                            /* Iterator variable */
55477
 
    int nSrcPage = -1;                 /* Size of source db in pages */
55478
 
    int bCloseTrans = 0;               /* True if src db requires unlocking */
55479
 
 
55480
 
    /* If the source pager is currently in a write-transaction, return
55481
 
    ** SQLITE_BUSY immediately.
55482
 
    */
55483
 
    if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
55484
 
      rc = SQLITE_BUSY;
55485
 
    }else{
55486
 
      rc = SQLITE_OK;
55487
 
    }
55488
 
 
55489
 
    /* Lock the destination database, if it is not locked already. */
55490
 
    if( SQLITE_OK==rc && p->bDestLocked==0
55491
 
     && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) 
55492
 
    ){
55493
 
      p->bDestLocked = 1;
55494
 
      sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
55495
 
    }
55496
 
 
55497
 
    /* If there is no open read-transaction on the source database, open
55498
 
    ** one now. If a transaction is opened here, then it will be closed
55499
 
    ** before this function exits.
55500
 
    */
55501
 
    if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
55502
 
      rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
55503
 
      bCloseTrans = 1;
55504
 
    }
55505
 
 
55506
 
    /* Do not allow backup if the destination database is in WAL mode
55507
 
    ** and the page sizes are different between source and destination */
55508
 
    pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
55509
 
    pgszDest = sqlite3BtreeGetPageSize(p->pDest);
55510
 
    destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
55511
 
    if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
55512
 
      rc = SQLITE_READONLY;
55513
 
    }
55514
 
  
55515
 
    /* Now that there is a read-lock on the source database, query the
55516
 
    ** source pager for the number of pages in the database.
55517
 
    */
55518
 
    nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
55519
 
    assert( nSrcPage>=0 );
55520
 
    for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
55521
 
      const Pgno iSrcPg = p->iNext;                 /* Source page number */
55522
 
      if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
55523
 
        DbPage *pSrcPg;                             /* Source page object */
55524
 
        rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
55525
 
        if( rc==SQLITE_OK ){
55526
 
          rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg));
55527
 
          sqlite3PagerUnref(pSrcPg);
55528
 
        }
55529
 
      }
55530
 
      p->iNext++;
55531
 
    }
55532
 
    if( rc==SQLITE_OK ){
55533
 
      p->nPagecount = nSrcPage;
55534
 
      p->nRemaining = nSrcPage+1-p->iNext;
55535
 
      if( p->iNext>(Pgno)nSrcPage ){
55536
 
        rc = SQLITE_DONE;
55537
 
      }else if( !p->isAttached ){
55538
 
        attachBackupObject(p);
55539
 
      }
55540
 
    }
55541
 
  
55542
 
    /* Update the schema version field in the destination database. This
55543
 
    ** is to make sure that the schema-version really does change in
55544
 
    ** the case where the source and destination databases have the
55545
 
    ** same schema version.
55546
 
    */
55547
 
    if( rc==SQLITE_DONE 
55548
 
     && (rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1))==SQLITE_OK
55549
 
    ){
55550
 
      int nDestTruncate;
55551
 
  
55552
 
      if( p->pDestDb ){
55553
 
        sqlite3ResetInternalSchema(p->pDestDb, -1);
55554
 
      }
55555
 
 
55556
 
      /* Set nDestTruncate to the final number of pages in the destination
55557
 
      ** database. The complication here is that the destination page
55558
 
      ** size may be different to the source page size. 
55559
 
      **
55560
 
      ** If the source page size is smaller than the destination page size, 
55561
 
      ** round up. In this case the call to sqlite3OsTruncate() below will
55562
 
      ** fix the size of the file. However it is important to call
55563
 
      ** sqlite3PagerTruncateImage() here so that any pages in the 
55564
 
      ** destination file that lie beyond the nDestTruncate page mark are
55565
 
      ** journalled by PagerCommitPhaseOne() before they are destroyed
55566
 
      ** by the file truncation.
55567
 
      */
55568
 
      assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
55569
 
      assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
55570
 
      if( pgszSrc<pgszDest ){
55571
 
        int ratio = pgszDest/pgszSrc;
55572
 
        nDestTruncate = (nSrcPage+ratio-1)/ratio;
55573
 
        if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
55574
 
          nDestTruncate--;
55575
 
        }
55576
 
      }else{
55577
 
        nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
55578
 
      }
55579
 
      sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
55580
 
 
55581
 
      if( pgszSrc<pgszDest ){
55582
 
        /* If the source page-size is smaller than the destination page-size,
55583
 
        ** two extra things may need to happen:
55584
 
        **
55585
 
        **   * The destination may need to be truncated, and
55586
 
        **
55587
 
        **   * Data stored on the pages immediately following the 
55588
 
        **     pending-byte page in the source database may need to be
55589
 
        **     copied into the destination database.
55590
 
        */
55591
 
        const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
55592
 
        sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
55593
 
        i64 iOff;
55594
 
        i64 iEnd;
55595
 
 
55596
 
        assert( pFile );
55597
 
        assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
55598
 
              nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
55599
 
           && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
55600
 
        ));
55601
 
 
55602
 
        /* This call ensures that all data required to recreate the original
55603
 
        ** database has been stored in the journal for pDestPager and the
55604
 
        ** journal synced to disk. So at this point we may safely modify
55605
 
        ** the database file in any way, knowing that if a power failure
55606
 
        ** occurs, the original database will be reconstructed from the 
55607
 
        ** journal file.  */
55608
 
        rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
55609
 
 
55610
 
        /* Write the extra pages and truncate the database file as required. */
55611
 
        iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
55612
 
        for(
55613
 
          iOff=PENDING_BYTE+pgszSrc; 
55614
 
          rc==SQLITE_OK && iOff<iEnd; 
55615
 
          iOff+=pgszSrc
55616
 
        ){
55617
 
          PgHdr *pSrcPg = 0;
55618
 
          const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
55619
 
          rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
55620
 
          if( rc==SQLITE_OK ){
55621
 
            u8 *zData = sqlite3PagerGetData(pSrcPg);
55622
 
            rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
55623
 
          }
55624
 
          sqlite3PagerUnref(pSrcPg);
55625
 
        }
55626
 
        if( rc==SQLITE_OK ){
55627
 
          rc = backupTruncateFile(pFile, iSize);
55628
 
        }
55629
 
 
55630
 
        /* Sync the database file to disk. */
55631
 
        if( rc==SQLITE_OK ){
55632
 
          rc = sqlite3PagerSync(pDestPager);
55633
 
        }
55634
 
      }else{
55635
 
        rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
55636
 
      }
55637
 
  
55638
 
      /* Finish committing the transaction to the destination database. */
55639
 
      if( SQLITE_OK==rc
55640
 
       && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
55641
 
      ){
55642
 
        rc = SQLITE_DONE;
55643
 
      }
55644
 
    }
55645
 
  
55646
 
    /* If bCloseTrans is true, then this function opened a read transaction
55647
 
    ** on the source database. Close the read transaction here. There is
55648
 
    ** no need to check the return values of the btree methods here, as
55649
 
    ** "committing" a read-only transaction cannot fail.
55650
 
    */
55651
 
    if( bCloseTrans ){
55652
 
      TESTONLY( int rc2 );
55653
 
      TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
55654
 
      TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
55655
 
      assert( rc2==SQLITE_OK );
55656
 
    }
55657
 
  
55658
 
    if( rc==SQLITE_IOERR_NOMEM ){
55659
 
      rc = SQLITE_NOMEM;
55660
 
    }
55661
 
    p->rc = rc;
55662
 
  }
55663
 
  if( p->pDestDb ){
55664
 
    sqlite3_mutex_leave(p->pDestDb->mutex);
55665
 
  }
55666
 
  sqlite3BtreeLeave(p->pSrc);
55667
 
  sqlite3_mutex_leave(p->pSrcDb->mutex);
55668
 
  return rc;
55669
 
}
55670
 
 
55671
 
/*
55672
 
** Release all resources associated with an sqlite3_backup* handle.
55673
 
*/
55674
 
SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
55675
 
  sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
55676
 
  sqlite3_mutex *mutex;                /* Mutex to protect source database */
55677
 
  int rc;                              /* Value to return */
55678
 
 
55679
 
  /* Enter the mutexes */
55680
 
  if( p==0 ) return SQLITE_OK;
55681
 
  sqlite3_mutex_enter(p->pSrcDb->mutex);
55682
 
  sqlite3BtreeEnter(p->pSrc);
55683
 
  mutex = p->pSrcDb->mutex;
55684
 
  if( p->pDestDb ){
55685
 
    sqlite3_mutex_enter(p->pDestDb->mutex);
55686
 
  }
55687
 
 
55688
 
  /* Detach this backup from the source pager. */
55689
 
  if( p->pDestDb ){
55690
 
    p->pSrc->nBackup--;
55691
 
  }
55692
 
  if( p->isAttached ){
55693
 
    pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
55694
 
    while( *pp!=p ){
55695
 
      pp = &(*pp)->pNext;
55696
 
    }
55697
 
    *pp = p->pNext;
55698
 
  }
55699
 
 
55700
 
  /* If a transaction is still open on the Btree, roll it back. */
55701
 
  sqlite3BtreeRollback(p->pDest);
55702
 
 
55703
 
  /* Set the error code of the destination database handle. */
55704
 
  rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
55705
 
  sqlite3Error(p->pDestDb, rc, 0);
55706
 
 
55707
 
  /* Exit the mutexes and free the backup context structure. */
55708
 
  if( p->pDestDb ){
55709
 
    sqlite3_mutex_leave(p->pDestDb->mutex);
55710
 
  }
55711
 
  sqlite3BtreeLeave(p->pSrc);
55712
 
  if( p->pDestDb ){
55713
 
    /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
55714
 
    ** call to sqlite3_backup_init() and is destroyed by a call to
55715
 
    ** sqlite3_backup_finish(). */
55716
 
    sqlite3_free(p);
55717
 
  }
55718
 
  sqlite3_mutex_leave(mutex);
55719
 
  return rc;
55720
 
}
55721
 
 
55722
 
/*
55723
 
** Return the number of pages still to be backed up as of the most recent
55724
 
** call to sqlite3_backup_step().
55725
 
*/
55726
 
SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
55727
 
  return p->nRemaining;
55728
 
}
55729
 
 
55730
 
/*
55731
 
** Return the total number of pages in the source database as of the most 
55732
 
** recent call to sqlite3_backup_step().
55733
 
*/
55734
 
SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
55735
 
  return p->nPagecount;
55736
 
}
55737
 
 
55738
 
/*
55739
 
** This function is called after the contents of page iPage of the
55740
 
** source database have been modified. If page iPage has already been 
55741
 
** copied into the destination database, then the data written to the
55742
 
** destination is now invalidated. The destination copy of iPage needs
55743
 
** to be updated with the new data before the backup operation is
55744
 
** complete.
55745
 
**
55746
 
** It is assumed that the mutex associated with the BtShared object
55747
 
** corresponding to the source database is held when this function is
55748
 
** called.
55749
 
*/
55750
 
SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
55751
 
  sqlite3_backup *p;                   /* Iterator variable */
55752
 
  for(p=pBackup; p; p=p->pNext){
55753
 
    assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
55754
 
    if( !isFatalError(p->rc) && iPage<p->iNext ){
55755
 
      /* The backup process p has already copied page iPage. But now it
55756
 
      ** has been modified by a transaction on the source pager. Copy
55757
 
      ** the new data into the backup.
55758
 
      */
55759
 
      int rc;
55760
 
      assert( p->pDestDb );
55761
 
      sqlite3_mutex_enter(p->pDestDb->mutex);
55762
 
      rc = backupOnePage(p, iPage, aData);
55763
 
      sqlite3_mutex_leave(p->pDestDb->mutex);
55764
 
      assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
55765
 
      if( rc!=SQLITE_OK ){
55766
 
        p->rc = rc;
55767
 
      }
55768
 
    }
55769
 
  }
55770
 
}
55771
 
 
55772
 
/*
55773
 
** Restart the backup process. This is called when the pager layer
55774
 
** detects that the database has been modified by an external database
55775
 
** connection. In this case there is no way of knowing which of the
55776
 
** pages that have been copied into the destination database are still 
55777
 
** valid and which are not, so the entire process needs to be restarted.
55778
 
**
55779
 
** It is assumed that the mutex associated with the BtShared object
55780
 
** corresponding to the source database is held when this function is
55781
 
** called.
55782
 
*/
55783
 
SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
55784
 
  sqlite3_backup *p;                   /* Iterator variable */
55785
 
  for(p=pBackup; p; p=p->pNext){
55786
 
    assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
55787
 
    p->iNext = 1;
55788
 
  }
55789
 
}
55790
 
 
55791
 
#ifndef SQLITE_OMIT_VACUUM
55792
 
/*
55793
 
** Copy the complete content of pBtFrom into pBtTo.  A transaction
55794
 
** must be active for both files.
55795
 
**
55796
 
** The size of file pTo may be reduced by this operation. If anything 
55797
 
** goes wrong, the transaction on pTo is rolled back. If successful, the 
55798
 
** transaction is committed before returning.
55799
 
*/
55800
 
SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
55801
 
  int rc;
55802
 
  sqlite3_backup b;
55803
 
  sqlite3BtreeEnter(pTo);
55804
 
  sqlite3BtreeEnter(pFrom);
55805
 
 
55806
 
  /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
55807
 
  ** to 0. This is used by the implementations of sqlite3_backup_step()
55808
 
  ** and sqlite3_backup_finish() to detect that they are being called
55809
 
  ** from this function, not directly by the user.
55810
 
  */
55811
 
  memset(&b, 0, sizeof(b));
55812
 
  b.pSrcDb = pFrom->db;
55813
 
  b.pSrc = pFrom;
55814
 
  b.pDest = pTo;
55815
 
  b.iNext = 1;
55816
 
 
55817
 
  /* 0x7FFFFFFF is the hard limit for the number of pages in a database
55818
 
  ** file. By passing this as the number of pages to copy to
55819
 
  ** sqlite3_backup_step(), we can guarantee that the copy finishes 
55820
 
  ** within a single call (unless an error occurs). The assert() statement
55821
 
  ** checks this assumption - (p->rc) should be set to either SQLITE_DONE 
55822
 
  ** or an error code.
55823
 
  */
55824
 
  sqlite3_backup_step(&b, 0x7FFFFFFF);
55825
 
  assert( b.rc!=SQLITE_OK );
55826
 
  rc = sqlite3_backup_finish(&b);
55827
 
  if( rc==SQLITE_OK ){
55828
 
    pTo->pBt->pageSizeFixed = 0;
55829
 
  }
55830
 
 
55831
 
  sqlite3BtreeLeave(pFrom);
55832
 
  sqlite3BtreeLeave(pTo);
55833
 
  return rc;
55834
 
}
55835
 
#endif /* SQLITE_OMIT_VACUUM */
55836
 
 
55837
 
/************** End of backup.c **********************************************/
55838
 
/************** Begin file vdbemem.c *****************************************/
55839
 
/*
55840
 
** 2004 May 26
55841
 
**
55842
 
** The author disclaims copyright to this source code.  In place of
55843
 
** a legal notice, here is a blessing:
55844
 
**
55845
 
**    May you do good and not evil.
55846
 
**    May you find forgiveness for yourself and forgive others.
55847
 
**    May you share freely, never taking more than you give.
55848
 
**
55849
 
*************************************************************************
55850
 
**
55851
 
** This file contains code use to manipulate "Mem" structure.  A "Mem"
55852
 
** stores a single value in the VDBE.  Mem is an opaque structure visible
55853
 
** only within the VDBE.  Interface routines refer to a Mem using the
55854
 
** name sqlite_value
55855
 
*/
55856
 
 
55857
 
/*
55858
 
** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
55859
 
** P if required.
55860
 
*/
55861
 
#define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
55862
 
 
55863
 
/*
55864
 
** If pMem is an object with a valid string representation, this routine
55865
 
** ensures the internal encoding for the string representation is
55866
 
** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
55867
 
**
55868
 
** If pMem is not a string object, or the encoding of the string
55869
 
** representation is already stored using the requested encoding, then this
55870
 
** routine is a no-op.
55871
 
**
55872
 
** SQLITE_OK is returned if the conversion is successful (or not required).
55873
 
** SQLITE_NOMEM may be returned if a malloc() fails during conversion
55874
 
** between formats.
55875
 
*/
55876
 
SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
55877
 
  int rc;
55878
 
  assert( (pMem->flags&MEM_RowSet)==0 );
55879
 
  assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
55880
 
           || desiredEnc==SQLITE_UTF16BE );
55881
 
  if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
55882
 
    return SQLITE_OK;
55883
 
  }
55884
 
  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
55885
 
#ifdef SQLITE_OMIT_UTF16
55886
 
  return SQLITE_ERROR;
55887
 
#else
55888
 
 
55889
 
  /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
55890
 
  ** then the encoding of the value may not have changed.
55891
 
  */
55892
 
  rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
55893
 
  assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
55894
 
  assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
55895
 
  assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
55896
 
  return rc;
55897
 
#endif
55898
 
}
55899
 
 
55900
 
/*
55901
 
** Make sure pMem->z points to a writable allocation of at least 
55902
 
** n bytes.
55903
 
**
55904
 
** If the memory cell currently contains string or blob data
55905
 
** and the third argument passed to this function is true, the 
55906
 
** current content of the cell is preserved. Otherwise, it may
55907
 
** be discarded.  
55908
 
**
55909
 
** This function sets the MEM_Dyn flag and clears any xDel callback.
55910
 
** It also clears MEM_Ephem and MEM_Static. If the preserve flag is 
55911
 
** not set, Mem.n is zeroed.
55912
 
*/
55913
 
SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
55914
 
  assert( 1 >=
55915
 
    ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
55916
 
    (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + 
55917
 
    ((pMem->flags&MEM_Ephem) ? 1 : 0) + 
55918
 
    ((pMem->flags&MEM_Static) ? 1 : 0)
55919
 
  );
55920
 
  assert( (pMem->flags&MEM_RowSet)==0 );
55921
 
 
55922
 
  if( n<32 ) n = 32;
55923
 
  if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
55924
 
    if( preserve && pMem->z==pMem->zMalloc ){
55925
 
      pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
55926
 
      preserve = 0;
55927
 
    }else{
55928
 
      sqlite3DbFree(pMem->db, pMem->zMalloc);
55929
 
      pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
55930
 
    }
55931
 
  }
55932
 
 
55933
 
  if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
55934
 
    memcpy(pMem->zMalloc, pMem->z, pMem->n);
55935
 
  }
55936
 
  if( pMem->flags&MEM_Dyn && pMem->xDel ){
55937
 
    pMem->xDel((void *)(pMem->z));
55938
 
  }
55939
 
 
55940
 
  pMem->z = pMem->zMalloc;
55941
 
  if( pMem->z==0 ){
55942
 
    pMem->flags = MEM_Null;
55943
 
  }else{
55944
 
    pMem->flags &= ~(MEM_Ephem|MEM_Static);
55945
 
  }
55946
 
  pMem->xDel = 0;
55947
 
  return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
55948
 
}
55949
 
 
55950
 
/*
55951
 
** Make the given Mem object MEM_Dyn.  In other words, make it so
55952
 
** that any TEXT or BLOB content is stored in memory obtained from
55953
 
** malloc().  In this way, we know that the memory is safe to be
55954
 
** overwritten or altered.
55955
 
**
55956
 
** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
55957
 
*/
55958
 
SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
55959
 
  int f;
55960
 
  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
55961
 
  assert( (pMem->flags&MEM_RowSet)==0 );
55962
 
  expandBlob(pMem);
55963
 
  f = pMem->flags;
55964
 
  if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
55965
 
    if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
55966
 
      return SQLITE_NOMEM;
55967
 
    }
55968
 
    pMem->z[pMem->n] = 0;
55969
 
    pMem->z[pMem->n+1] = 0;
55970
 
    pMem->flags |= MEM_Term;
55971
 
#ifdef SQLITE_DEBUG
55972
 
    pMem->pScopyFrom = 0;
55973
 
#endif
55974
 
  }
55975
 
 
55976
 
  return SQLITE_OK;
55977
 
}
55978
 
 
55979
 
/*
55980
 
** If the given Mem* has a zero-filled tail, turn it into an ordinary
55981
 
** blob stored in dynamically allocated space.
55982
 
*/
55983
 
#ifndef SQLITE_OMIT_INCRBLOB
55984
 
SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
55985
 
  if( pMem->flags & MEM_Zero ){
55986
 
    int nByte;
55987
 
    assert( pMem->flags&MEM_Blob );
55988
 
    assert( (pMem->flags&MEM_RowSet)==0 );
55989
 
    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
55990
 
 
55991
 
    /* Set nByte to the number of bytes required to store the expanded blob. */
55992
 
    nByte = pMem->n + pMem->u.nZero;
55993
 
    if( nByte<=0 ){
55994
 
      nByte = 1;
55995
 
    }
55996
 
    if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
55997
 
      return SQLITE_NOMEM;
55998
 
    }
55999
 
 
56000
 
    memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
56001
 
    pMem->n += pMem->u.nZero;
56002
 
    pMem->flags &= ~(MEM_Zero|MEM_Term);
56003
 
  }
56004
 
  return SQLITE_OK;
56005
 
}
56006
 
#endif
56007
 
 
56008
 
 
56009
 
/*
56010
 
** Make sure the given Mem is \u0000 terminated.
56011
 
*/
56012
 
SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
56013
 
  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56014
 
  if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
56015
 
    return SQLITE_OK;   /* Nothing to do */
56016
 
  }
56017
 
  if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
56018
 
    return SQLITE_NOMEM;
56019
 
  }
56020
 
  pMem->z[pMem->n] = 0;
56021
 
  pMem->z[pMem->n+1] = 0;
56022
 
  pMem->flags |= MEM_Term;
56023
 
  return SQLITE_OK;
56024
 
}
56025
 
 
56026
 
/*
56027
 
** Add MEM_Str to the set of representations for the given Mem.  Numbers
56028
 
** are converted using sqlite3_snprintf().  Converting a BLOB to a string
56029
 
** is a no-op.
56030
 
**
56031
 
** Existing representations MEM_Int and MEM_Real are *not* invalidated.
56032
 
**
56033
 
** A MEM_Null value will never be passed to this function. This function is
56034
 
** used for converting values to text for returning to the user (i.e. via
56035
 
** sqlite3_value_text()), or for ensuring that values to be used as btree
56036
 
** keys are strings. In the former case a NULL pointer is returned the
56037
 
** user and the later is an internal programming error.
56038
 
*/
56039
 
SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, int enc){
56040
 
  int rc = SQLITE_OK;
56041
 
  int fg = pMem->flags;
56042
 
  const int nByte = 32;
56043
 
 
56044
 
  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56045
 
  assert( !(fg&MEM_Zero) );
56046
 
  assert( !(fg&(MEM_Str|MEM_Blob)) );
56047
 
  assert( fg&(MEM_Int|MEM_Real) );
56048
 
  assert( (pMem->flags&MEM_RowSet)==0 );
56049
 
  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
56050
 
 
56051
 
 
56052
 
  if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
56053
 
    return SQLITE_NOMEM;
56054
 
  }
56055
 
 
56056
 
  /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
56057
 
  ** string representation of the value. Then, if the required encoding
56058
 
  ** is UTF-16le or UTF-16be do a translation.
56059
 
  ** 
56060
 
  ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
56061
 
  */
56062
 
  if( fg & MEM_Int ){
56063
 
    sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
56064
 
  }else{
56065
 
    assert( fg & MEM_Real );
56066
 
    sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
56067
 
  }
56068
 
  pMem->n = sqlite3Strlen30(pMem->z);
56069
 
  pMem->enc = SQLITE_UTF8;
56070
 
  pMem->flags |= MEM_Str|MEM_Term;
56071
 
  sqlite3VdbeChangeEncoding(pMem, enc);
56072
 
  return rc;
56073
 
}
56074
 
 
56075
 
/*
56076
 
** Memory cell pMem contains the context of an aggregate function.
56077
 
** This routine calls the finalize method for that function.  The
56078
 
** result of the aggregate is stored back into pMem.
56079
 
**
56080
 
** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
56081
 
** otherwise.
56082
 
*/
56083
 
SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
56084
 
  int rc = SQLITE_OK;
56085
 
  if( ALWAYS(pFunc && pFunc->xFinalize) ){
56086
 
    sqlite3_context ctx;
56087
 
    assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
56088
 
    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56089
 
    memset(&ctx, 0, sizeof(ctx));
56090
 
    ctx.s.flags = MEM_Null;
56091
 
    ctx.s.db = pMem->db;
56092
 
    ctx.pMem = pMem;
56093
 
    ctx.pFunc = pFunc;
56094
 
    pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
56095
 
    assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
56096
 
    sqlite3DbFree(pMem->db, pMem->zMalloc);
56097
 
    memcpy(pMem, &ctx.s, sizeof(ctx.s));
56098
 
    rc = ctx.isError;
56099
 
  }
56100
 
  return rc;
56101
 
}
56102
 
 
56103
 
/*
56104
 
** If the memory cell contains a string value that must be freed by
56105
 
** invoking an external callback, free it now. Calling this function
56106
 
** does not free any Mem.zMalloc buffer.
56107
 
*/
56108
 
SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
56109
 
  assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
56110
 
  testcase( p->flags & MEM_Agg );
56111
 
  testcase( p->flags & MEM_Dyn );
56112
 
  testcase( p->flags & MEM_RowSet );
56113
 
  testcase( p->flags & MEM_Frame );
56114
 
  if( p->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame) ){
56115
 
    if( p->flags&MEM_Agg ){
56116
 
      sqlite3VdbeMemFinalize(p, p->u.pDef);
56117
 
      assert( (p->flags & MEM_Agg)==0 );
56118
 
      sqlite3VdbeMemRelease(p);
56119
 
    }else if( p->flags&MEM_Dyn && p->xDel ){
56120
 
      assert( (p->flags&MEM_RowSet)==0 );
56121
 
      p->xDel((void *)p->z);
56122
 
      p->xDel = 0;
56123
 
    }else if( p->flags&MEM_RowSet ){
56124
 
      sqlite3RowSetClear(p->u.pRowSet);
56125
 
    }else if( p->flags&MEM_Frame ){
56126
 
      sqlite3VdbeMemSetNull(p);
56127
 
    }
56128
 
  }
56129
 
}
56130
 
 
56131
 
/*
56132
 
** Release any memory held by the Mem. This may leave the Mem in an
56133
 
** inconsistent state, for example with (Mem.z==0) and
56134
 
** (Mem.type==SQLITE_TEXT).
56135
 
*/
56136
 
SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
56137
 
  sqlite3VdbeMemReleaseExternal(p);
56138
 
  sqlite3DbFree(p->db, p->zMalloc);
56139
 
  p->z = 0;
56140
 
  p->zMalloc = 0;
56141
 
  p->xDel = 0;
56142
 
}
56143
 
 
56144
 
/*
56145
 
** Convert a 64-bit IEEE double into a 64-bit signed integer.
56146
 
** If the double is too large, return 0x8000000000000000.
56147
 
**
56148
 
** Most systems appear to do this simply by assigning
56149
 
** variables and without the extra range tests.  But
56150
 
** there are reports that windows throws an expection
56151
 
** if the floating point value is out of range. (See ticket #2880.)
56152
 
** Because we do not completely understand the problem, we will
56153
 
** take the conservative approach and always do range tests
56154
 
** before attempting the conversion.
56155
 
*/
56156
 
static i64 doubleToInt64(double r){
56157
 
#ifdef SQLITE_OMIT_FLOATING_POINT
56158
 
  /* When floating-point is omitted, double and int64 are the same thing */
56159
 
  return r;
56160
 
#else
56161
 
  /*
56162
 
  ** Many compilers we encounter do not define constants for the
56163
 
  ** minimum and maximum 64-bit integers, or they define them
56164
 
  ** inconsistently.  And many do not understand the "LL" notation.
56165
 
  ** So we define our own static constants here using nothing
56166
 
  ** larger than a 32-bit integer constant.
56167
 
  */
56168
 
  static const i64 maxInt = LARGEST_INT64;
56169
 
  static const i64 minInt = SMALLEST_INT64;
56170
 
 
56171
 
  if( r<(double)minInt ){
56172
 
    return minInt;
56173
 
  }else if( r>(double)maxInt ){
56174
 
    /* minInt is correct here - not maxInt.  It turns out that assigning
56175
 
    ** a very large positive number to an integer results in a very large
56176
 
    ** negative integer.  This makes no sense, but it is what x86 hardware
56177
 
    ** does so for compatibility we will do the same in software. */
56178
 
    return minInt;
56179
 
  }else{
56180
 
    return (i64)r;
56181
 
  }
56182
 
#endif
56183
 
}
56184
 
 
56185
 
/*
56186
 
** Return some kind of integer value which is the best we can do
56187
 
** at representing the value that *pMem describes as an integer.
56188
 
** If pMem is an integer, then the value is exact.  If pMem is
56189
 
** a floating-point then the value returned is the integer part.
56190
 
** If pMem is a string or blob, then we make an attempt to convert
56191
 
** it into a integer and return that.  If pMem represents an
56192
 
** an SQL-NULL value, return 0.
56193
 
**
56194
 
** If pMem represents a string value, its encoding might be changed.
56195
 
*/
56196
 
SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
56197
 
  int flags;
56198
 
  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56199
 
  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
56200
 
  flags = pMem->flags;
56201
 
  if( flags & MEM_Int ){
56202
 
    return pMem->u.i;
56203
 
  }else if( flags & MEM_Real ){
56204
 
    return doubleToInt64(pMem->r);
56205
 
  }else if( flags & (MEM_Str|MEM_Blob) ){
56206
 
    i64 value = 0;
56207
 
    assert( pMem->z || pMem->n==0 );
56208
 
    testcase( pMem->z==0 );
56209
 
    sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
56210
 
    return value;
56211
 
  }else{
56212
 
    return 0;
56213
 
  }
56214
 
}
56215
 
 
56216
 
/*
56217
 
** Return the best representation of pMem that we can get into a
56218
 
** double.  If pMem is already a double or an integer, return its
56219
 
** value.  If it is a string or blob, try to convert it to a double.
56220
 
** If it is a NULL, return 0.0.
56221
 
*/
56222
 
SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
56223
 
  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56224
 
  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
56225
 
  if( pMem->flags & MEM_Real ){
56226
 
    return pMem->r;
56227
 
  }else if( pMem->flags & MEM_Int ){
56228
 
    return (double)pMem->u.i;
56229
 
  }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
56230
 
    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
56231
 
    double val = (double)0;
56232
 
    sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
56233
 
    return val;
56234
 
  }else{
56235
 
    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
56236
 
    return (double)0;
56237
 
  }
56238
 
}
56239
 
 
56240
 
/*
56241
 
** The MEM structure is already a MEM_Real.  Try to also make it a
56242
 
** MEM_Int if we can.
56243
 
*/
56244
 
SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
56245
 
  assert( pMem->flags & MEM_Real );
56246
 
  assert( (pMem->flags & MEM_RowSet)==0 );
56247
 
  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56248
 
  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
56249
 
 
56250
 
  pMem->u.i = doubleToInt64(pMem->r);
56251
 
 
56252
 
  /* Only mark the value as an integer if
56253
 
  **
56254
 
  **    (1) the round-trip conversion real->int->real is a no-op, and
56255
 
  **    (2) The integer is neither the largest nor the smallest
56256
 
  **        possible integer (ticket #3922)
56257
 
  **
56258
 
  ** The second and third terms in the following conditional enforces
56259
 
  ** the second condition under the assumption that addition overflow causes
56260
 
  ** values to wrap around.  On x86 hardware, the third term is always
56261
 
  ** true and could be omitted.  But we leave it in because other
56262
 
  ** architectures might behave differently.
56263
 
  */
56264
 
  if( pMem->r==(double)pMem->u.i && pMem->u.i>SMALLEST_INT64
56265
 
      && ALWAYS(pMem->u.i<LARGEST_INT64) ){
56266
 
    pMem->flags |= MEM_Int;
56267
 
  }
56268
 
}
56269
 
 
56270
 
/*
56271
 
** Convert pMem to type integer.  Invalidate any prior representations.
56272
 
*/
56273
 
SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
56274
 
  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56275
 
  assert( (pMem->flags & MEM_RowSet)==0 );
56276
 
  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
56277
 
 
56278
 
  pMem->u.i = sqlite3VdbeIntValue(pMem);
56279
 
  MemSetTypeFlag(pMem, MEM_Int);
56280
 
  return SQLITE_OK;
56281
 
}
56282
 
 
56283
 
/*
56284
 
** Convert pMem so that it is of type MEM_Real.
56285
 
** Invalidate any prior representations.
56286
 
*/
56287
 
SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
56288
 
  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56289
 
  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
56290
 
 
56291
 
  pMem->r = sqlite3VdbeRealValue(pMem);
56292
 
  MemSetTypeFlag(pMem, MEM_Real);
56293
 
  return SQLITE_OK;
56294
 
}
56295
 
 
56296
 
/*
56297
 
** Convert pMem so that it has types MEM_Real or MEM_Int or both.
56298
 
** Invalidate any prior representations.
56299
 
**
56300
 
** Every effort is made to force the conversion, even if the input
56301
 
** is a string that does not look completely like a number.  Convert
56302
 
** as much of the string as we can and ignore the rest.
56303
 
*/
56304
 
SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
56305
 
  if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
56306
 
    assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
56307
 
    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56308
 
    if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
56309
 
      MemSetTypeFlag(pMem, MEM_Int);
56310
 
    }else{
56311
 
      pMem->r = sqlite3VdbeRealValue(pMem);
56312
 
      MemSetTypeFlag(pMem, MEM_Real);
56313
 
      sqlite3VdbeIntegerAffinity(pMem);
56314
 
    }
56315
 
  }
56316
 
  assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
56317
 
  pMem->flags &= ~(MEM_Str|MEM_Blob);
56318
 
  return SQLITE_OK;
56319
 
}
56320
 
 
56321
 
/*
56322
 
** Delete any previous value and set the value stored in *pMem to NULL.
56323
 
*/
56324
 
SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
56325
 
  if( pMem->flags & MEM_Frame ){
56326
 
    VdbeFrame *pFrame = pMem->u.pFrame;
56327
 
    pFrame->pParent = pFrame->v->pDelFrame;
56328
 
    pFrame->v->pDelFrame = pFrame;
56329
 
  }
56330
 
  if( pMem->flags & MEM_RowSet ){
56331
 
    sqlite3RowSetClear(pMem->u.pRowSet);
56332
 
  }
56333
 
  MemSetTypeFlag(pMem, MEM_Null);
56334
 
  pMem->type = SQLITE_NULL;
56335
 
}
56336
 
 
56337
 
/*
56338
 
** Delete any previous value and set the value to be a BLOB of length
56339
 
** n containing all zeros.
56340
 
*/
56341
 
SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
56342
 
  sqlite3VdbeMemRelease(pMem);
56343
 
  pMem->flags = MEM_Blob|MEM_Zero;
56344
 
  pMem->type = SQLITE_BLOB;
56345
 
  pMem->n = 0;
56346
 
  if( n<0 ) n = 0;
56347
 
  pMem->u.nZero = n;
56348
 
  pMem->enc = SQLITE_UTF8;
56349
 
 
56350
 
#ifdef SQLITE_OMIT_INCRBLOB
56351
 
  sqlite3VdbeMemGrow(pMem, n, 0);
56352
 
  if( pMem->z ){
56353
 
    pMem->n = n;
56354
 
    memset(pMem->z, 0, n);
56355
 
  }
56356
 
#endif
56357
 
}
56358
 
 
56359
 
/*
56360
 
** Delete any previous value and set the value stored in *pMem to val,
56361
 
** manifest type INTEGER.
56362
 
*/
56363
 
SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
56364
 
  sqlite3VdbeMemRelease(pMem);
56365
 
  pMem->u.i = val;
56366
 
  pMem->flags = MEM_Int;
56367
 
  pMem->type = SQLITE_INTEGER;
56368
 
}
56369
 
 
56370
 
#ifndef SQLITE_OMIT_FLOATING_POINT
56371
 
/*
56372
 
** Delete any previous value and set the value stored in *pMem to val,
56373
 
** manifest type REAL.
56374
 
*/
56375
 
SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
56376
 
  if( sqlite3IsNaN(val) ){
56377
 
    sqlite3VdbeMemSetNull(pMem);
56378
 
  }else{
56379
 
    sqlite3VdbeMemRelease(pMem);
56380
 
    pMem->r = val;
56381
 
    pMem->flags = MEM_Real;
56382
 
    pMem->type = SQLITE_FLOAT;
56383
 
  }
56384
 
}
56385
 
#endif
56386
 
 
56387
 
/*
56388
 
** Delete any previous value and set the value of pMem to be an
56389
 
** empty boolean index.
56390
 
*/
56391
 
SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
56392
 
  sqlite3 *db = pMem->db;
56393
 
  assert( db!=0 );
56394
 
  assert( (pMem->flags & MEM_RowSet)==0 );
56395
 
  sqlite3VdbeMemRelease(pMem);
56396
 
  pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
56397
 
  if( db->mallocFailed ){
56398
 
    pMem->flags = MEM_Null;
56399
 
  }else{
56400
 
    assert( pMem->zMalloc );
56401
 
    pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, 
56402
 
                                       sqlite3DbMallocSize(db, pMem->zMalloc));
56403
 
    assert( pMem->u.pRowSet!=0 );
56404
 
    pMem->flags = MEM_RowSet;
56405
 
  }
56406
 
}
56407
 
 
56408
 
/*
56409
 
** Return true if the Mem object contains a TEXT or BLOB that is
56410
 
** too large - whose size exceeds SQLITE_MAX_LENGTH.
56411
 
*/
56412
 
SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
56413
 
  assert( p->db!=0 );
56414
 
  if( p->flags & (MEM_Str|MEM_Blob) ){
56415
 
    int n = p->n;
56416
 
    if( p->flags & MEM_Zero ){
56417
 
      n += p->u.nZero;
56418
 
    }
56419
 
    return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
56420
 
  }
56421
 
  return 0; 
56422
 
}
56423
 
 
56424
 
#ifdef SQLITE_DEBUG
56425
 
/*
56426
 
** This routine prepares a memory cell for modication by breaking
56427
 
** its link to a shallow copy and by marking any current shallow
56428
 
** copies of this cell as invalid.
56429
 
**
56430
 
** This is used for testing and debugging only - to make sure shallow
56431
 
** copies are not misused.
56432
 
*/
56433
 
SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe *pVdbe, Mem *pMem){
56434
 
  int i;
56435
 
  Mem *pX;
56436
 
  for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
56437
 
    if( pX->pScopyFrom==pMem ){
56438
 
      pX->flags |= MEM_Invalid;
56439
 
      pX->pScopyFrom = 0;
56440
 
    }
56441
 
  }
56442
 
  pMem->pScopyFrom = 0;
56443
 
}
56444
 
#endif /* SQLITE_DEBUG */
56445
 
 
56446
 
/*
56447
 
** Size of struct Mem not including the Mem.zMalloc member.
56448
 
*/
56449
 
#define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
56450
 
 
56451
 
/*
56452
 
** Make an shallow copy of pFrom into pTo.  Prior contents of
56453
 
** pTo are freed.  The pFrom->z field is not duplicated.  If
56454
 
** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
56455
 
** and flags gets srcType (either MEM_Ephem or MEM_Static).
56456
 
*/
56457
 
SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
56458
 
  assert( (pFrom->flags & MEM_RowSet)==0 );
56459
 
  sqlite3VdbeMemReleaseExternal(pTo);
56460
 
  memcpy(pTo, pFrom, MEMCELLSIZE);
56461
 
  pTo->xDel = 0;
56462
 
  if( (pFrom->flags&MEM_Static)==0 ){
56463
 
    pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
56464
 
    assert( srcType==MEM_Ephem || srcType==MEM_Static );
56465
 
    pTo->flags |= srcType;
56466
 
  }
56467
 
}
56468
 
 
56469
 
/*
56470
 
** Make a full copy of pFrom into pTo.  Prior contents of pTo are
56471
 
** freed before the copy is made.
56472
 
*/
56473
 
SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
56474
 
  int rc = SQLITE_OK;
56475
 
 
56476
 
  assert( (pFrom->flags & MEM_RowSet)==0 );
56477
 
  sqlite3VdbeMemReleaseExternal(pTo);
56478
 
  memcpy(pTo, pFrom, MEMCELLSIZE);
56479
 
  pTo->flags &= ~MEM_Dyn;
56480
 
 
56481
 
  if( pTo->flags&(MEM_Str|MEM_Blob) ){
56482
 
    if( 0==(pFrom->flags&MEM_Static) ){
56483
 
      pTo->flags |= MEM_Ephem;
56484
 
      rc = sqlite3VdbeMemMakeWriteable(pTo);
56485
 
    }
56486
 
  }
56487
 
 
56488
 
  return rc;
56489
 
}
56490
 
 
56491
 
/*
56492
 
** Transfer the contents of pFrom to pTo. Any existing value in pTo is
56493
 
** freed. If pFrom contains ephemeral data, a copy is made.
56494
 
**
56495
 
** pFrom contains an SQL NULL when this routine returns.
56496
 
*/
56497
 
SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
56498
 
  assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
56499
 
  assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
56500
 
  assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
56501
 
 
56502
 
  sqlite3VdbeMemRelease(pTo);
56503
 
  memcpy(pTo, pFrom, sizeof(Mem));
56504
 
  pFrom->flags = MEM_Null;
56505
 
  pFrom->xDel = 0;
56506
 
  pFrom->zMalloc = 0;
56507
 
}
56508
 
 
56509
 
/*
56510
 
** Change the value of a Mem to be a string or a BLOB.
56511
 
**
56512
 
** The memory management strategy depends on the value of the xDel
56513
 
** parameter. If the value passed is SQLITE_TRANSIENT, then the 
56514
 
** string is copied into a (possibly existing) buffer managed by the 
56515
 
** Mem structure. Otherwise, any existing buffer is freed and the
56516
 
** pointer copied.
56517
 
**
56518
 
** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
56519
 
** size limit) then no memory allocation occurs.  If the string can be
56520
 
** stored without allocating memory, then it is.  If a memory allocation
56521
 
** is required to store the string, then value of pMem is unchanged.  In
56522
 
** either case, SQLITE_TOOBIG is returned.
56523
 
*/
56524
 
SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
56525
 
  Mem *pMem,          /* Memory cell to set to string value */
56526
 
  const char *z,      /* String pointer */
56527
 
  int n,              /* Bytes in string, or negative */
56528
 
  u8 enc,             /* Encoding of z.  0 for BLOBs */
56529
 
  void (*xDel)(void*) /* Destructor function */
56530
 
){
56531
 
  int nByte = n;      /* New value for pMem->n */
56532
 
  int iLimit;         /* Maximum allowed string or blob size */
56533
 
  u16 flags = 0;      /* New value for pMem->flags */
56534
 
 
56535
 
  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
56536
 
  assert( (pMem->flags & MEM_RowSet)==0 );
56537
 
 
56538
 
  /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
56539
 
  if( !z ){
56540
 
    sqlite3VdbeMemSetNull(pMem);
56541
 
    return SQLITE_OK;
56542
 
  }
56543
 
 
56544
 
  if( pMem->db ){
56545
 
    iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
56546
 
  }else{
56547
 
    iLimit = SQLITE_MAX_LENGTH;
56548
 
  }
56549
 
  flags = (enc==0?MEM_Blob:MEM_Str);
56550
 
  if( nByte<0 ){
56551
 
    assert( enc!=0 );
56552
 
    if( enc==SQLITE_UTF8 ){
56553
 
      for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
56554
 
    }else{
56555
 
      for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
56556
 
    }
56557
 
    flags |= MEM_Term;
56558
 
  }
56559
 
 
56560
 
  /* The following block sets the new values of Mem.z and Mem.xDel. It
56561
 
  ** also sets a flag in local variable "flags" to indicate the memory
56562
 
  ** management (one of MEM_Dyn or MEM_Static).
56563
 
  */
56564
 
  if( xDel==SQLITE_TRANSIENT ){
56565
 
    int nAlloc = nByte;
56566
 
    if( flags&MEM_Term ){
56567
 
      nAlloc += (enc==SQLITE_UTF8?1:2);
56568
 
    }
56569
 
    if( nByte>iLimit ){
56570
 
      return SQLITE_TOOBIG;
56571
 
    }
56572
 
    if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
56573
 
      return SQLITE_NOMEM;
56574
 
    }
56575
 
    memcpy(pMem->z, z, nAlloc);
56576
 
  }else if( xDel==SQLITE_DYNAMIC ){
56577
 
    sqlite3VdbeMemRelease(pMem);
56578
 
    pMem->zMalloc = pMem->z = (char *)z;
56579
 
    pMem->xDel = 0;
56580
 
  }else{
56581
 
    sqlite3VdbeMemRelease(pMem);
56582
 
    pMem->z = (char *)z;
56583
 
    pMem->xDel = xDel;
56584
 
    flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
56585
 
  }
56586
 
 
56587
 
  pMem->n = nByte;
56588
 
  pMem->flags = flags;
56589
 
  pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
56590
 
  pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
56591
 
 
56592
 
#ifndef SQLITE_OMIT_UTF16
56593
 
  if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
56594
 
    return SQLITE_NOMEM;
56595
 
  }
56596
 
#endif
56597
 
 
56598
 
  if( nByte>iLimit ){
56599
 
    return SQLITE_TOOBIG;
56600
 
  }
56601
 
 
56602
 
  return SQLITE_OK;
56603
 
}
56604
 
 
56605
 
/*
56606
 
** Compare the values contained by the two memory cells, returning
56607
 
** negative, zero or positive if pMem1 is less than, equal to, or greater
56608
 
** than pMem2. Sorting order is NULL's first, followed by numbers (integers
56609
 
** and reals) sorted numerically, followed by text ordered by the collating
56610
 
** sequence pColl and finally blob's ordered by memcmp().
56611
 
**
56612
 
** Two NULL values are considered equal by this function.
56613
 
*/
56614
 
SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
56615
 
  int rc;
56616
 
  int f1, f2;
56617
 
  int combined_flags;
56618
 
 
56619
 
  f1 = pMem1->flags;
56620
 
  f2 = pMem2->flags;
56621
 
  combined_flags = f1|f2;
56622
 
  assert( (combined_flags & MEM_RowSet)==0 );
56623
 
 
56624
 
  /* If one value is NULL, it is less than the other. If both values
56625
 
  ** are NULL, return 0.
56626
 
  */
56627
 
  if( combined_flags&MEM_Null ){
56628
 
    return (f2&MEM_Null) - (f1&MEM_Null);
56629
 
  }
56630
 
 
56631
 
  /* If one value is a number and the other is not, the number is less.
56632
 
  ** If both are numbers, compare as reals if one is a real, or as integers
56633
 
  ** if both values are integers.
56634
 
  */
56635
 
  if( combined_flags&(MEM_Int|MEM_Real) ){
56636
 
    if( !(f1&(MEM_Int|MEM_Real)) ){
56637
 
      return 1;
56638
 
    }
56639
 
    if( !(f2&(MEM_Int|MEM_Real)) ){
56640
 
      return -1;
56641
 
    }
56642
 
    if( (f1 & f2 & MEM_Int)==0 ){
56643
 
      double r1, r2;
56644
 
      if( (f1&MEM_Real)==0 ){
56645
 
        r1 = (double)pMem1->u.i;
56646
 
      }else{
56647
 
        r1 = pMem1->r;
56648
 
      }
56649
 
      if( (f2&MEM_Real)==0 ){
56650
 
        r2 = (double)pMem2->u.i;
56651
 
      }else{
56652
 
        r2 = pMem2->r;
56653
 
      }
56654
 
      if( r1<r2 ) return -1;
56655
 
      if( r1>r2 ) return 1;
56656
 
      return 0;
56657
 
    }else{
56658
 
      assert( f1&MEM_Int );
56659
 
      assert( f2&MEM_Int );
56660
 
      if( pMem1->u.i < pMem2->u.i ) return -1;
56661
 
      if( pMem1->u.i > pMem2->u.i ) return 1;
56662
 
      return 0;
56663
 
    }
56664
 
  }
56665
 
 
56666
 
  /* If one value is a string and the other is a blob, the string is less.
56667
 
  ** If both are strings, compare using the collating functions.
56668
 
  */
56669
 
  if( combined_flags&MEM_Str ){
56670
 
    if( (f1 & MEM_Str)==0 ){
56671
 
      return 1;
56672
 
    }
56673
 
    if( (f2 & MEM_Str)==0 ){
56674
 
      return -1;
56675
 
    }
56676
 
 
56677
 
    assert( pMem1->enc==pMem2->enc );
56678
 
    assert( pMem1->enc==SQLITE_UTF8 || 
56679
 
            pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
56680
 
 
56681
 
    /* The collation sequence must be defined at this point, even if
56682
 
    ** the user deletes the collation sequence after the vdbe program is
56683
 
    ** compiled (this was not always the case).
56684
 
    */
56685
 
    assert( !pColl || pColl->xCmp );
56686
 
 
56687
 
    if( pColl ){
56688
 
      if( pMem1->enc==pColl->enc ){
56689
 
        /* The strings are already in the correct encoding.  Call the
56690
 
        ** comparison function directly */
56691
 
        return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
56692
 
      }else{
56693
 
        const void *v1, *v2;
56694
 
        int n1, n2;
56695
 
        Mem c1;
56696
 
        Mem c2;
56697
 
        memset(&c1, 0, sizeof(c1));
56698
 
        memset(&c2, 0, sizeof(c2));
56699
 
        sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
56700
 
        sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
56701
 
        v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
56702
 
        n1 = v1==0 ? 0 : c1.n;
56703
 
        v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
56704
 
        n2 = v2==0 ? 0 : c2.n;
56705
 
        rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
56706
 
        sqlite3VdbeMemRelease(&c1);
56707
 
        sqlite3VdbeMemRelease(&c2);
56708
 
        return rc;
56709
 
      }
56710
 
    }
56711
 
    /* If a NULL pointer was passed as the collate function, fall through
56712
 
    ** to the blob case and use memcmp().  */
56713
 
  }
56714
 
 
56715
 
  /* Both values must be blobs.  Compare using memcmp().  */
56716
 
  rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
56717
 
  if( rc==0 ){
56718
 
    rc = pMem1->n - pMem2->n;
56719
 
  }
56720
 
  return rc;
56721
 
}
56722
 
 
56723
 
/*
56724
 
** Move data out of a btree key or data field and into a Mem structure.
56725
 
** The data or key is taken from the entry that pCur is currently pointing
56726
 
** to.  offset and amt determine what portion of the data or key to retrieve.
56727
 
** key is true to get the key or false to get data.  The result is written
56728
 
** into the pMem element.
56729
 
**
56730
 
** The pMem structure is assumed to be uninitialized.  Any prior content
56731
 
** is overwritten without being freed.
56732
 
**
56733
 
** If this routine fails for any reason (malloc returns NULL or unable
56734
 
** to read from the disk) then the pMem is left in an inconsistent state.
56735
 
*/
56736
 
SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
56737
 
  BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
56738
 
  int offset,       /* Offset from the start of data to return bytes from. */
56739
 
  int amt,          /* Number of bytes to return. */
56740
 
  int key,          /* If true, retrieve from the btree key, not data. */
56741
 
  Mem *pMem         /* OUT: Return data in this Mem structure. */
56742
 
){
56743
 
  char *zData;        /* Data from the btree layer */
56744
 
  int available = 0;  /* Number of bytes available on the local btree page */
56745
 
  int rc = SQLITE_OK; /* Return code */
56746
 
 
56747
 
  assert( sqlite3BtreeCursorIsValid(pCur) );
56748
 
 
56749
 
  /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() 
56750
 
  ** that both the BtShared and database handle mutexes are held. */
56751
 
  assert( (pMem->flags & MEM_RowSet)==0 );
56752
 
  if( key ){
56753
 
    zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
56754
 
  }else{
56755
 
    zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
56756
 
  }
56757
 
  assert( zData!=0 );
56758
 
 
56759
 
  if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
56760
 
    sqlite3VdbeMemRelease(pMem);
56761
 
    pMem->z = &zData[offset];
56762
 
    pMem->flags = MEM_Blob|MEM_Ephem;
56763
 
  }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
56764
 
    pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
56765
 
    pMem->enc = 0;
56766
 
    pMem->type = SQLITE_BLOB;
56767
 
    if( key ){
56768
 
      rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
56769
 
    }else{
56770
 
      rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
56771
 
    }
56772
 
    pMem->z[amt] = 0;
56773
 
    pMem->z[amt+1] = 0;
56774
 
    if( rc!=SQLITE_OK ){
56775
 
      sqlite3VdbeMemRelease(pMem);
56776
 
    }
56777
 
  }
56778
 
  pMem->n = amt;
56779
 
 
56780
 
  return rc;
56781
 
}
56782
 
 
56783
 
/* This function is only available internally, it is not part of the
56784
 
** external API. It works in a similar way to sqlite3_value_text(),
56785
 
** except the data returned is in the encoding specified by the second
56786
 
** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
56787
 
** SQLITE_UTF8.
56788
 
**
56789
 
** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
56790
 
** If that is the case, then the result must be aligned on an even byte
56791
 
** boundary.
56792
 
*/
56793
 
SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
56794
 
  if( !pVal ) return 0;
56795
 
 
56796
 
  assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
56797
 
  assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
56798
 
  assert( (pVal->flags & MEM_RowSet)==0 );
56799
 
 
56800
 
  if( pVal->flags&MEM_Null ){
56801
 
    return 0;
56802
 
  }
56803
 
  assert( (MEM_Blob>>3) == MEM_Str );
56804
 
  pVal->flags |= (pVal->flags & MEM_Blob)>>3;
56805
 
  expandBlob(pVal);
56806
 
  if( pVal->flags&MEM_Str ){
56807
 
    sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
56808
 
    if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
56809
 
      assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
56810
 
      if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
56811
 
        return 0;
56812
 
      }
56813
 
    }
56814
 
    sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-59893-45467 */
56815
 
  }else{
56816
 
    assert( (pVal->flags&MEM_Blob)==0 );
56817
 
    sqlite3VdbeMemStringify(pVal, enc);
56818
 
    assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
56819
 
  }
56820
 
  assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
56821
 
              || pVal->db->mallocFailed );
56822
 
  if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
56823
 
    return pVal->z;
56824
 
  }else{
56825
 
    return 0;
56826
 
  }
56827
 
}
56828
 
 
56829
 
/*
56830
 
** Create a new sqlite3_value object.
56831
 
*/
56832
 
SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
56833
 
  Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
56834
 
  if( p ){
56835
 
    p->flags = MEM_Null;
56836
 
    p->type = SQLITE_NULL;
56837
 
    p->db = db;
56838
 
  }
56839
 
  return p;
56840
 
}
56841
 
 
56842
 
/*
56843
 
** Create a new sqlite3_value object, containing the value of pExpr.
56844
 
**
56845
 
** This only works for very simple expressions that consist of one constant
56846
 
** token (i.e. "5", "5.1", "'a string'"). If the expression can
56847
 
** be converted directly into a value, then the value is allocated and
56848
 
** a pointer written to *ppVal. The caller is responsible for deallocating
56849
 
** the value by passing it to sqlite3ValueFree() later on. If the expression
56850
 
** cannot be converted to a value, then *ppVal is set to NULL.
56851
 
*/
56852
 
SQLITE_PRIVATE int sqlite3ValueFromExpr(
56853
 
  sqlite3 *db,              /* The database connection */
56854
 
  Expr *pExpr,              /* The expression to evaluate */
56855
 
  u8 enc,                   /* Encoding to use */
56856
 
  u8 affinity,              /* Affinity to use */
56857
 
  sqlite3_value **ppVal     /* Write the new value here */
56858
 
){
56859
 
  int op;
56860
 
  char *zVal = 0;
56861
 
  sqlite3_value *pVal = 0;
56862
 
  int negInt = 1;
56863
 
  const char *zNeg = "";
56864
 
 
56865
 
  if( !pExpr ){
56866
 
    *ppVal = 0;
56867
 
    return SQLITE_OK;
56868
 
  }
56869
 
  op = pExpr->op;
56870
 
 
56871
 
  /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2.
56872
 
  ** The ifdef here is to enable us to achieve 100% branch test coverage even
56873
 
  ** when SQLITE_ENABLE_STAT2 is omitted.
56874
 
  */
56875
 
#ifdef SQLITE_ENABLE_STAT2
56876
 
  if( op==TK_REGISTER ) op = pExpr->op2;
56877
 
#else
56878
 
  if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
56879
 
#endif
56880
 
 
56881
 
  /* Handle negative integers in a single step.  This is needed in the
56882
 
  ** case when the value is -9223372036854775808.
56883
 
  */
56884
 
  if( op==TK_UMINUS
56885
 
   && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
56886
 
    pExpr = pExpr->pLeft;
56887
 
    op = pExpr->op;
56888
 
    negInt = -1;
56889
 
    zNeg = "-";
56890
 
  }
56891
 
 
56892
 
  if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
56893
 
    pVal = sqlite3ValueNew(db);
56894
 
    if( pVal==0 ) goto no_mem;
56895
 
    if( ExprHasProperty(pExpr, EP_IntValue) ){
56896
 
      sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
56897
 
    }else{
56898
 
      zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
56899
 
      if( zVal==0 ) goto no_mem;
56900
 
      sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
56901
 
      if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
56902
 
    }
56903
 
    if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
56904
 
      sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
56905
 
    }else{
56906
 
      sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
56907
 
    }
56908
 
    if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
56909
 
    if( enc!=SQLITE_UTF8 ){
56910
 
      sqlite3VdbeChangeEncoding(pVal, enc);
56911
 
    }
56912
 
  }else if( op==TK_UMINUS ) {
56913
 
    /* This branch happens for multiple negative signs.  Ex: -(-5) */
56914
 
    if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
56915
 
      sqlite3VdbeMemNumerify(pVal);
56916
 
      if( pVal->u.i==SMALLEST_INT64 ){
56917
 
        pVal->flags &= MEM_Int;
56918
 
        pVal->flags |= MEM_Real;
56919
 
        pVal->r = (double)LARGEST_INT64;
56920
 
      }else{
56921
 
        pVal->u.i = -pVal->u.i;
56922
 
      }
56923
 
      pVal->r = -pVal->r;
56924
 
      sqlite3ValueApplyAffinity(pVal, affinity, enc);
56925
 
    }
56926
 
  }else if( op==TK_NULL ){
56927
 
    pVal = sqlite3ValueNew(db);
56928
 
    if( pVal==0 ) goto no_mem;
56929
 
  }
56930
 
#ifndef SQLITE_OMIT_BLOB_LITERAL
56931
 
  else if( op==TK_BLOB ){
56932
 
    int nVal;
56933
 
    assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
56934
 
    assert( pExpr->u.zToken[1]=='\'' );
56935
 
    pVal = sqlite3ValueNew(db);
56936
 
    if( !pVal ) goto no_mem;
56937
 
    zVal = &pExpr->u.zToken[2];
56938
 
    nVal = sqlite3Strlen30(zVal)-1;
56939
 
    assert( zVal[nVal]=='\'' );
56940
 
    sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
56941
 
                         0, SQLITE_DYNAMIC);
56942
 
  }
56943
 
#endif
56944
 
 
56945
 
  if( pVal ){
56946
 
    sqlite3VdbeMemStoreType(pVal);
56947
 
  }
56948
 
  *ppVal = pVal;
56949
 
  return SQLITE_OK;
56950
 
 
56951
 
no_mem:
56952
 
  db->mallocFailed = 1;
56953
 
  sqlite3DbFree(db, zVal);
56954
 
  sqlite3ValueFree(pVal);
56955
 
  *ppVal = 0;
56956
 
  return SQLITE_NOMEM;
56957
 
}
56958
 
 
56959
 
/*
56960
 
** Change the string value of an sqlite3_value object
56961
 
*/
56962
 
SQLITE_PRIVATE void sqlite3ValueSetStr(
56963
 
  sqlite3_value *v,     /* Value to be set */
56964
 
  int n,                /* Length of string z */
56965
 
  const void *z,        /* Text of the new string */
56966
 
  u8 enc,               /* Encoding to use */
56967
 
  void (*xDel)(void*)   /* Destructor for the string */
56968
 
){
56969
 
  if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
56970
 
}
56971
 
 
56972
 
/*
56973
 
** Free an sqlite3_value object
56974
 
*/
56975
 
SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
56976
 
  if( !v ) return;
56977
 
  sqlite3VdbeMemRelease((Mem *)v);
56978
 
  sqlite3DbFree(((Mem*)v)->db, v);
56979
 
}
56980
 
 
56981
 
/*
56982
 
** Return the number of bytes in the sqlite3_value object assuming
56983
 
** that it uses the encoding "enc"
56984
 
*/
56985
 
SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
56986
 
  Mem *p = (Mem*)pVal;
56987
 
  if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
56988
 
    if( p->flags & MEM_Zero ){
56989
 
      return p->n + p->u.nZero;
56990
 
    }else{
56991
 
      return p->n;
56992
 
    }
56993
 
  }
56994
 
  return 0;
56995
 
}
56996
 
 
56997
 
/************** End of vdbemem.c *********************************************/
56998
 
/************** Begin file vdbeaux.c *****************************************/
56999
 
/*
57000
 
** 2003 September 6
57001
 
**
57002
 
** The author disclaims copyright to this source code.  In place of
57003
 
** a legal notice, here is a blessing:
57004
 
**
57005
 
**    May you do good and not evil.
57006
 
**    May you find forgiveness for yourself and forgive others.
57007
 
**    May you share freely, never taking more than you give.
57008
 
**
57009
 
*************************************************************************
57010
 
** This file contains code used for creating, destroying, and populating
57011
 
** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)  Prior
57012
 
** to version 2.8.7, all this code was combined into the vdbe.c source file.
57013
 
** But that file was getting too big so this subroutines were split out.
57014
 
*/
57015
 
 
57016
 
 
57017
 
 
57018
 
/*
57019
 
** When debugging the code generator in a symbolic debugger, one can
57020
 
** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed
57021
 
** as they are added to the instruction stream.
57022
 
*/
57023
 
#ifdef SQLITE_DEBUG
57024
 
SQLITE_PRIVATE int sqlite3VdbeAddopTrace = 0;
57025
 
#endif
57026
 
 
57027
 
 
57028
 
/*
57029
 
** Create a new virtual database engine.
57030
 
*/
57031
 
SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3 *db){
57032
 
  Vdbe *p;
57033
 
  p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
57034
 
  if( p==0 ) return 0;
57035
 
  p->db = db;
57036
 
  if( db->pVdbe ){
57037
 
    db->pVdbe->pPrev = p;
57038
 
  }
57039
 
  p->pNext = db->pVdbe;
57040
 
  p->pPrev = 0;
57041
 
  db->pVdbe = p;
57042
 
  p->magic = VDBE_MAGIC_INIT;
57043
 
  return p;
57044
 
}
57045
 
 
57046
 
/*
57047
 
** Remember the SQL string for a prepared statement.
57048
 
*/
57049
 
SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
57050
 
  assert( isPrepareV2==1 || isPrepareV2==0 );
57051
 
  if( p==0 ) return;
57052
 
#ifdef SQLITE_OMIT_TRACE
57053
 
  if( !isPrepareV2 ) return;
57054
 
#endif
57055
 
  assert( p->zSql==0 );
57056
 
  p->zSql = sqlite3DbStrNDup(p->db, z, n);
57057
 
  p->isPrepareV2 = (u8)isPrepareV2;
57058
 
}
57059
 
 
57060
 
/*
57061
 
** Return the SQL associated with a prepared statement
57062
 
*/
57063
 
SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
57064
 
  Vdbe *p = (Vdbe *)pStmt;
57065
 
  return (p && p->isPrepareV2) ? p->zSql : 0;
57066
 
}
57067
 
 
57068
 
/*
57069
 
** Swap all content between two VDBE structures.
57070
 
*/
57071
 
SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
57072
 
  Vdbe tmp, *pTmp;
57073
 
  char *zTmp;
57074
 
  tmp = *pA;
57075
 
  *pA = *pB;
57076
 
  *pB = tmp;
57077
 
  pTmp = pA->pNext;
57078
 
  pA->pNext = pB->pNext;
57079
 
  pB->pNext = pTmp;
57080
 
  pTmp = pA->pPrev;
57081
 
  pA->pPrev = pB->pPrev;
57082
 
  pB->pPrev = pTmp;
57083
 
  zTmp = pA->zSql;
57084
 
  pA->zSql = pB->zSql;
57085
 
  pB->zSql = zTmp;
57086
 
  pB->isPrepareV2 = pA->isPrepareV2;
57087
 
}
57088
 
 
57089
 
#ifdef SQLITE_DEBUG
57090
 
/*
57091
 
** Turn tracing on or off
57092
 
*/
57093
 
SQLITE_PRIVATE void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
57094
 
  p->trace = trace;
57095
 
}
57096
 
#endif
57097
 
 
57098
 
/*
57099
 
** Resize the Vdbe.aOp array so that it is at least one op larger than 
57100
 
** it was.
57101
 
**
57102
 
** If an out-of-memory error occurs while resizing the array, return
57103
 
** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain 
57104
 
** unchanged (this is so that any opcodes already allocated can be 
57105
 
** correctly deallocated along with the rest of the Vdbe).
57106
 
*/
57107
 
static int growOpArray(Vdbe *p){
57108
 
  VdbeOp *pNew;
57109
 
  int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
57110
 
  pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
57111
 
  if( pNew ){
57112
 
    p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
57113
 
    p->aOp = pNew;
57114
 
  }
57115
 
  return (pNew ? SQLITE_OK : SQLITE_NOMEM);
57116
 
}
57117
 
 
57118
 
/*
57119
 
** Add a new instruction to the list of instructions current in the
57120
 
** VDBE.  Return the address of the new instruction.
57121
 
**
57122
 
** Parameters:
57123
 
**
57124
 
**    p               Pointer to the VDBE
57125
 
**
57126
 
**    op              The opcode for this instruction
57127
 
**
57128
 
**    p1, p2, p3      Operands
57129
 
**
57130
 
** Use the sqlite3VdbeResolveLabel() function to fix an address and
57131
 
** the sqlite3VdbeChangeP4() function to change the value of the P4
57132
 
** operand.
57133
 
*/
57134
 
SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
57135
 
  int i;
57136
 
  VdbeOp *pOp;
57137
 
 
57138
 
  i = p->nOp;
57139
 
  assert( p->magic==VDBE_MAGIC_INIT );
57140
 
  assert( op>0 && op<0xff );
57141
 
  if( p->nOpAlloc<=i ){
57142
 
    if( growOpArray(p) ){
57143
 
      return 1;
57144
 
    }
57145
 
  }
57146
 
  p->nOp++;
57147
 
  pOp = &p->aOp[i];
57148
 
  pOp->opcode = (u8)op;
57149
 
  pOp->p5 = 0;
57150
 
  pOp->p1 = p1;
57151
 
  pOp->p2 = p2;
57152
 
  pOp->p3 = p3;
57153
 
  pOp->p4.p = 0;
57154
 
  pOp->p4type = P4_NOTUSED;
57155
 
  p->expired = 0;
57156
 
  if( op==OP_ParseSchema ){
57157
 
    /* Any program that uses the OP_ParseSchema opcode needs to lock
57158
 
    ** all btrees. */
57159
 
    int j;
57160
 
    for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
57161
 
  }
57162
 
#ifdef SQLITE_DEBUG
57163
 
  pOp->zComment = 0;
57164
 
  if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
57165
 
#endif
57166
 
#ifdef VDBE_PROFILE
57167
 
  pOp->cycles = 0;
57168
 
  pOp->cnt = 0;
57169
 
#endif
57170
 
  return i;
57171
 
}
57172
 
SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
57173
 
  return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
57174
 
}
57175
 
SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
57176
 
  return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
57177
 
}
57178
 
SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
57179
 
  return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
57180
 
}
57181
 
 
57182
 
 
57183
 
/*
57184
 
** Add an opcode that includes the p4 value as a pointer.
57185
 
*/
57186
 
SQLITE_PRIVATE int sqlite3VdbeAddOp4(
57187
 
  Vdbe *p,            /* Add the opcode to this VM */
57188
 
  int op,             /* The new opcode */
57189
 
  int p1,             /* The P1 operand */
57190
 
  int p2,             /* The P2 operand */
57191
 
  int p3,             /* The P3 operand */
57192
 
  const char *zP4,    /* The P4 operand */
57193
 
  int p4type          /* P4 operand type */
57194
 
){
57195
 
  int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
57196
 
  sqlite3VdbeChangeP4(p, addr, zP4, p4type);
57197
 
  return addr;
57198
 
}
57199
 
 
57200
 
/*
57201
 
** Add an opcode that includes the p4 value as an integer.
57202
 
*/
57203
 
SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
57204
 
  Vdbe *p,            /* Add the opcode to this VM */
57205
 
  int op,             /* The new opcode */
57206
 
  int p1,             /* The P1 operand */
57207
 
  int p2,             /* The P2 operand */
57208
 
  int p3,             /* The P3 operand */
57209
 
  int p4              /* The P4 operand as an integer */
57210
 
){
57211
 
  int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
57212
 
  sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
57213
 
  return addr;
57214
 
}
57215
 
 
57216
 
/*
57217
 
** Create a new symbolic label for an instruction that has yet to be
57218
 
** coded.  The symbolic label is really just a negative number.  The
57219
 
** label can be used as the P2 value of an operation.  Later, when
57220
 
** the label is resolved to a specific address, the VDBE will scan
57221
 
** through its operation list and change all values of P2 which match
57222
 
** the label into the resolved address.
57223
 
**
57224
 
** The VDBE knows that a P2 value is a label because labels are
57225
 
** always negative and P2 values are suppose to be non-negative.
57226
 
** Hence, a negative P2 value is a label that has yet to be resolved.
57227
 
**
57228
 
** Zero is returned if a malloc() fails.
57229
 
*/
57230
 
SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *p){
57231
 
  int i;
57232
 
  i = p->nLabel++;
57233
 
  assert( p->magic==VDBE_MAGIC_INIT );
57234
 
  if( i>=p->nLabelAlloc ){
57235
 
    int n = p->nLabelAlloc*2 + 5;
57236
 
    p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
57237
 
                                       n*sizeof(p->aLabel[0]));
57238
 
    p->nLabelAlloc = sqlite3DbMallocSize(p->db, p->aLabel)/sizeof(p->aLabel[0]);
57239
 
  }
57240
 
  if( p->aLabel ){
57241
 
    p->aLabel[i] = -1;
57242
 
  }
57243
 
  return -1-i;
57244
 
}
57245
 
 
57246
 
/*
57247
 
** Resolve label "x" to be the address of the next instruction to
57248
 
** be inserted.  The parameter "x" must have been obtained from
57249
 
** a prior call to sqlite3VdbeMakeLabel().
57250
 
*/
57251
 
SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *p, int x){
57252
 
  int j = -1-x;
57253
 
  assert( p->magic==VDBE_MAGIC_INIT );
57254
 
  assert( j>=0 && j<p->nLabel );
57255
 
  if( p->aLabel ){
57256
 
    p->aLabel[j] = p->nOp;
57257
 
  }
57258
 
}
57259
 
 
57260
 
/*
57261
 
** Mark the VDBE as one that can only be run one time.
57262
 
*/
57263
 
SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
57264
 
  p->runOnlyOnce = 1;
57265
 
}
57266
 
 
57267
 
#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
57268
 
 
57269
 
/*
57270
 
** The following type and function are used to iterate through all opcodes
57271
 
** in a Vdbe main program and each of the sub-programs (triggers) it may 
57272
 
** invoke directly or indirectly. It should be used as follows:
57273
 
**
57274
 
**   Op *pOp;
57275
 
**   VdbeOpIter sIter;
57276
 
**
57277
 
**   memset(&sIter, 0, sizeof(sIter));
57278
 
**   sIter.v = v;                            // v is of type Vdbe* 
57279
 
**   while( (pOp = opIterNext(&sIter)) ){
57280
 
**     // Do something with pOp
57281
 
**   }
57282
 
**   sqlite3DbFree(v->db, sIter.apSub);
57283
 
** 
57284
 
*/
57285
 
typedef struct VdbeOpIter VdbeOpIter;
57286
 
struct VdbeOpIter {
57287
 
  Vdbe *v;                   /* Vdbe to iterate through the opcodes of */
57288
 
  SubProgram **apSub;        /* Array of subprograms */
57289
 
  int nSub;                  /* Number of entries in apSub */
57290
 
  int iAddr;                 /* Address of next instruction to return */
57291
 
  int iSub;                  /* 0 = main program, 1 = first sub-program etc. */
57292
 
};
57293
 
static Op *opIterNext(VdbeOpIter *p){
57294
 
  Vdbe *v = p->v;
57295
 
  Op *pRet = 0;
57296
 
  Op *aOp;
57297
 
  int nOp;
57298
 
 
57299
 
  if( p->iSub<=p->nSub ){
57300
 
 
57301
 
    if( p->iSub==0 ){
57302
 
      aOp = v->aOp;
57303
 
      nOp = v->nOp;
57304
 
    }else{
57305
 
      aOp = p->apSub[p->iSub-1]->aOp;
57306
 
      nOp = p->apSub[p->iSub-1]->nOp;
57307
 
    }
57308
 
    assert( p->iAddr<nOp );
57309
 
 
57310
 
    pRet = &aOp[p->iAddr];
57311
 
    p->iAddr++;
57312
 
    if( p->iAddr==nOp ){
57313
 
      p->iSub++;
57314
 
      p->iAddr = 0;
57315
 
    }
57316
 
  
57317
 
    if( pRet->p4type==P4_SUBPROGRAM ){
57318
 
      int nByte = (p->nSub+1)*sizeof(SubProgram*);
57319
 
      int j;
57320
 
      for(j=0; j<p->nSub; j++){
57321
 
        if( p->apSub[j]==pRet->p4.pProgram ) break;
57322
 
      }
57323
 
      if( j==p->nSub ){
57324
 
        p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
57325
 
        if( !p->apSub ){
57326
 
          pRet = 0;
57327
 
        }else{
57328
 
          p->apSub[p->nSub++] = pRet->p4.pProgram;
57329
 
        }
57330
 
      }
57331
 
    }
57332
 
  }
57333
 
 
57334
 
  return pRet;
57335
 
}
57336
 
 
57337
 
/*
57338
 
** Check if the program stored in the VM associated with pParse may
57339
 
** throw an ABORT exception (causing the statement, but not entire transaction
57340
 
** to be rolled back). This condition is true if the main program or any
57341
 
** sub-programs contains any of the following:
57342
 
**
57343
 
**   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
57344
 
**   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
57345
 
**   *  OP_Destroy
57346
 
**   *  OP_VUpdate
57347
 
**   *  OP_VRename
57348
 
**   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
57349
 
**
57350
 
** Then check that the value of Parse.mayAbort is true if an
57351
 
** ABORT may be thrown, or false otherwise. Return true if it does
57352
 
** match, or false otherwise. This function is intended to be used as
57353
 
** part of an assert statement in the compiler. Similar to:
57354
 
**
57355
 
**   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
57356
 
*/
57357
 
SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
57358
 
  int hasAbort = 0;
57359
 
  Op *pOp;
57360
 
  VdbeOpIter sIter;
57361
 
  memset(&sIter, 0, sizeof(sIter));
57362
 
  sIter.v = v;
57363
 
 
57364
 
  while( (pOp = opIterNext(&sIter))!=0 ){
57365
 
    int opcode = pOp->opcode;
57366
 
    if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename 
57367
 
#ifndef SQLITE_OMIT_FOREIGN_KEY
57368
 
     || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1) 
57369
 
#endif
57370
 
     || ((opcode==OP_Halt || opcode==OP_HaltIfNull) 
57371
 
      && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
57372
 
    ){
57373
 
      hasAbort = 1;
57374
 
      break;
57375
 
    }
57376
 
  }
57377
 
  sqlite3DbFree(v->db, sIter.apSub);
57378
 
 
57379
 
  /* Return true if hasAbort==mayAbort. Or if a malloc failure occured.
57380
 
  ** If malloc failed, then the while() loop above may not have iterated
57381
 
  ** through all opcodes and hasAbort may be set incorrectly. Return
57382
 
  ** true for this case to prevent the assert() in the callers frame
57383
 
  ** from failing.  */
57384
 
  return ( v->db->mallocFailed || hasAbort==mayAbort );
57385
 
}
57386
 
#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
57387
 
 
57388
 
/*
57389
 
** Loop through the program looking for P2 values that are negative
57390
 
** on jump instructions.  Each such value is a label.  Resolve the
57391
 
** label by setting the P2 value to its correct non-zero value.
57392
 
**
57393
 
** This routine is called once after all opcodes have been inserted.
57394
 
**
57395
 
** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument 
57396
 
** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by 
57397
 
** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
57398
 
**
57399
 
** The Op.opflags field is set on all opcodes.
57400
 
*/
57401
 
static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
57402
 
  int i;
57403
 
  int nMaxArgs = *pMaxFuncArgs;
57404
 
  Op *pOp;
57405
 
  int *aLabel = p->aLabel;
57406
 
  p->readOnly = 1;
57407
 
  for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
57408
 
    u8 opcode = pOp->opcode;
57409
 
 
57410
 
    pOp->opflags = sqlite3OpcodeProperty[opcode];
57411
 
    if( opcode==OP_Function || opcode==OP_AggStep ){
57412
 
      if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
57413
 
    }else if( (opcode==OP_Transaction && pOp->p2!=0) || opcode==OP_Vacuum ){
57414
 
      p->readOnly = 0;
57415
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
57416
 
    }else if( opcode==OP_VUpdate ){
57417
 
      if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
57418
 
    }else if( opcode==OP_VFilter ){
57419
 
      int n;
57420
 
      assert( p->nOp - i >= 3 );
57421
 
      assert( pOp[-1].opcode==OP_Integer );
57422
 
      n = pOp[-1].p1;
57423
 
      if( n>nMaxArgs ) nMaxArgs = n;
57424
 
#endif
57425
 
    }
57426
 
 
57427
 
    if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
57428
 
      assert( -1-pOp->p2<p->nLabel );
57429
 
      pOp->p2 = aLabel[-1-pOp->p2];
57430
 
    }
57431
 
  }
57432
 
  sqlite3DbFree(p->db, p->aLabel);
57433
 
  p->aLabel = 0;
57434
 
 
57435
 
  *pMaxFuncArgs = nMaxArgs;
57436
 
}
57437
 
 
57438
 
/*
57439
 
** Return the address of the next instruction to be inserted.
57440
 
*/
57441
 
SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
57442
 
  assert( p->magic==VDBE_MAGIC_INIT );
57443
 
  return p->nOp;
57444
 
}
57445
 
 
57446
 
/*
57447
 
** This function returns a pointer to the array of opcodes associated with
57448
 
** the Vdbe passed as the first argument. It is the callers responsibility
57449
 
** to arrange for the returned array to be eventually freed using the 
57450
 
** vdbeFreeOpArray() function.
57451
 
**
57452
 
** Before returning, *pnOp is set to the number of entries in the returned
57453
 
** array. Also, *pnMaxArg is set to the larger of its current value and 
57454
 
** the number of entries in the Vdbe.apArg[] array required to execute the 
57455
 
** returned program.
57456
 
*/
57457
 
SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
57458
 
  VdbeOp *aOp = p->aOp;
57459
 
  assert( aOp && !p->db->mallocFailed );
57460
 
 
57461
 
  /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
57462
 
  assert( p->btreeMask==0 );
57463
 
 
57464
 
  resolveP2Values(p, pnMaxArg);
57465
 
  *pnOp = p->nOp;
57466
 
  p->aOp = 0;
57467
 
  return aOp;
57468
 
}
57469
 
 
57470
 
/*
57471
 
** Add a whole list of operations to the operation stack.  Return the
57472
 
** address of the first operation added.
57473
 
*/
57474
 
SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
57475
 
  int addr;
57476
 
  assert( p->magic==VDBE_MAGIC_INIT );
57477
 
  if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
57478
 
    return 0;
57479
 
  }
57480
 
  addr = p->nOp;
57481
 
  if( ALWAYS(nOp>0) ){
57482
 
    int i;
57483
 
    VdbeOpList const *pIn = aOp;
57484
 
    for(i=0; i<nOp; i++, pIn++){
57485
 
      int p2 = pIn->p2;
57486
 
      VdbeOp *pOut = &p->aOp[i+addr];
57487
 
      pOut->opcode = pIn->opcode;
57488
 
      pOut->p1 = pIn->p1;
57489
 
      if( p2<0 && (sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP)!=0 ){
57490
 
        pOut->p2 = addr + ADDR(p2);
57491
 
      }else{
57492
 
        pOut->p2 = p2;
57493
 
      }
57494
 
      pOut->p3 = pIn->p3;
57495
 
      pOut->p4type = P4_NOTUSED;
57496
 
      pOut->p4.p = 0;
57497
 
      pOut->p5 = 0;
57498
 
#ifdef SQLITE_DEBUG
57499
 
      pOut->zComment = 0;
57500
 
      if( sqlite3VdbeAddopTrace ){
57501
 
        sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
57502
 
      }
57503
 
#endif
57504
 
    }
57505
 
    p->nOp += nOp;
57506
 
  }
57507
 
  return addr;
57508
 
}
57509
 
 
57510
 
/*
57511
 
** Change the value of the P1 operand for a specific instruction.
57512
 
** This routine is useful when a large program is loaded from a
57513
 
** static array using sqlite3VdbeAddOpList but we want to make a
57514
 
** few minor changes to the program.
57515
 
*/
57516
 
SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
57517
 
  assert( p!=0 );
57518
 
  assert( addr>=0 );
57519
 
  if( p->nOp>addr ){
57520
 
    p->aOp[addr].p1 = val;
57521
 
  }
57522
 
}
57523
 
 
57524
 
/*
57525
 
** Change the value of the P2 operand for a specific instruction.
57526
 
** This routine is useful for setting a jump destination.
57527
 
*/
57528
 
SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
57529
 
  assert( p!=0 );
57530
 
  assert( addr>=0 );
57531
 
  if( p->nOp>addr ){
57532
 
    p->aOp[addr].p2 = val;
57533
 
  }
57534
 
}
57535
 
 
57536
 
/*
57537
 
** Change the value of the P3 operand for a specific instruction.
57538
 
*/
57539
 
SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
57540
 
  assert( p!=0 );
57541
 
  assert( addr>=0 );
57542
 
  if( p->nOp>addr ){
57543
 
    p->aOp[addr].p3 = val;
57544
 
  }
57545
 
}
57546
 
 
57547
 
/*
57548
 
** Change the value of the P5 operand for the most recently
57549
 
** added operation.
57550
 
*/
57551
 
SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
57552
 
  assert( p!=0 );
57553
 
  if( p->aOp ){
57554
 
    assert( p->nOp>0 );
57555
 
    p->aOp[p->nOp-1].p5 = val;
57556
 
  }
57557
 
}
57558
 
 
57559
 
/*
57560
 
** Change the P2 operand of instruction addr so that it points to
57561
 
** the address of the next instruction to be coded.
57562
 
*/
57563
 
SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
57564
 
  assert( addr>=0 );
57565
 
  sqlite3VdbeChangeP2(p, addr, p->nOp);
57566
 
}
57567
 
 
57568
 
 
57569
 
/*
57570
 
** If the input FuncDef structure is ephemeral, then free it.  If
57571
 
** the FuncDef is not ephermal, then do nothing.
57572
 
*/
57573
 
static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
57574
 
  if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
57575
 
    sqlite3DbFree(db, pDef);
57576
 
  }
57577
 
}
57578
 
 
57579
 
static void vdbeFreeOpArray(sqlite3 *, Op *, int);
57580
 
 
57581
 
/*
57582
 
** Delete a P4 value if necessary.
57583
 
*/
57584
 
static void freeP4(sqlite3 *db, int p4type, void *p4){
57585
 
  if( p4 ){
57586
 
    assert( db );
57587
 
    switch( p4type ){
57588
 
      case P4_REAL:
57589
 
      case P4_INT64:
57590
 
      case P4_DYNAMIC:
57591
 
      case P4_KEYINFO:
57592
 
      case P4_INTARRAY:
57593
 
      case P4_KEYINFO_HANDOFF: {
57594
 
        sqlite3DbFree(db, p4);
57595
 
        break;
57596
 
      }
57597
 
      case P4_MPRINTF: {
57598
 
        if( db->pnBytesFreed==0 ) sqlite3_free(p4);
57599
 
        break;
57600
 
      }
57601
 
      case P4_VDBEFUNC: {
57602
 
        VdbeFunc *pVdbeFunc = (VdbeFunc *)p4;
57603
 
        freeEphemeralFunction(db, pVdbeFunc->pFunc);
57604
 
        if( db->pnBytesFreed==0 ) sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
57605
 
        sqlite3DbFree(db, pVdbeFunc);
57606
 
        break;
57607
 
      }
57608
 
      case P4_FUNCDEF: {
57609
 
        freeEphemeralFunction(db, (FuncDef*)p4);
57610
 
        break;
57611
 
      }
57612
 
      case P4_MEM: {
57613
 
        if( db->pnBytesFreed==0 ){
57614
 
          sqlite3ValueFree((sqlite3_value*)p4);
57615
 
        }else{
57616
 
          Mem *p = (Mem*)p4;
57617
 
          sqlite3DbFree(db, p->zMalloc);
57618
 
          sqlite3DbFree(db, p);
57619
 
        }
57620
 
        break;
57621
 
      }
57622
 
      case P4_VTAB : {
57623
 
        if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
57624
 
        break;
57625
 
      }
57626
 
    }
57627
 
  }
57628
 
}
57629
 
 
57630
 
/*
57631
 
** Free the space allocated for aOp and any p4 values allocated for the
57632
 
** opcodes contained within. If aOp is not NULL it is assumed to contain 
57633
 
** nOp entries. 
57634
 
*/
57635
 
static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
57636
 
  if( aOp ){
57637
 
    Op *pOp;
57638
 
    for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
57639
 
      freeP4(db, pOp->p4type, pOp->p4.p);
57640
 
#ifdef SQLITE_DEBUG
57641
 
      sqlite3DbFree(db, pOp->zComment);
57642
 
#endif     
57643
 
    }
57644
 
  }
57645
 
  sqlite3DbFree(db, aOp);
57646
 
}
57647
 
 
57648
 
/*
57649
 
** Link the SubProgram object passed as the second argument into the linked
57650
 
** list at Vdbe.pSubProgram. This list is used to delete all sub-program
57651
 
** objects when the VM is no longer required.
57652
 
*/
57653
 
SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
57654
 
  p->pNext = pVdbe->pProgram;
57655
 
  pVdbe->pProgram = p;
57656
 
}
57657
 
 
57658
 
/*
57659
 
** Change N opcodes starting at addr to No-ops.
57660
 
*/
57661
 
SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
57662
 
  if( p->aOp ){
57663
 
    VdbeOp *pOp = &p->aOp[addr];
57664
 
    sqlite3 *db = p->db;
57665
 
    while( N-- ){
57666
 
      freeP4(db, pOp->p4type, pOp->p4.p);
57667
 
      memset(pOp, 0, sizeof(pOp[0]));
57668
 
      pOp->opcode = OP_Noop;
57669
 
      pOp++;
57670
 
    }
57671
 
  }
57672
 
}
57673
 
 
57674
 
/*
57675
 
** Change the value of the P4 operand for a specific instruction.
57676
 
** This routine is useful when a large program is loaded from a
57677
 
** static array using sqlite3VdbeAddOpList but we want to make a
57678
 
** few minor changes to the program.
57679
 
**
57680
 
** If n>=0 then the P4 operand is dynamic, meaning that a copy of
57681
 
** the string is made into memory obtained from sqlite3_malloc().
57682
 
** A value of n==0 means copy bytes of zP4 up to and including the
57683
 
** first null byte.  If n>0 then copy n+1 bytes of zP4.
57684
 
**
57685
 
** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
57686
 
** A copy is made of the KeyInfo structure into memory obtained from
57687
 
** sqlite3_malloc, to be freed when the Vdbe is finalized.
57688
 
** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
57689
 
** stored in memory that the caller has obtained from sqlite3_malloc. The 
57690
 
** caller should not free the allocation, it will be freed when the Vdbe is
57691
 
** finalized.
57692
 
** 
57693
 
** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
57694
 
** to a string or structure that is guaranteed to exist for the lifetime of
57695
 
** the Vdbe. In these cases we can just copy the pointer.
57696
 
**
57697
 
** If addr<0 then change P4 on the most recently inserted instruction.
57698
 
*/
57699
 
SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
57700
 
  Op *pOp;
57701
 
  sqlite3 *db;
57702
 
  assert( p!=0 );
57703
 
  db = p->db;
57704
 
  assert( p->magic==VDBE_MAGIC_INIT );
57705
 
  if( p->aOp==0 || db->mallocFailed ){
57706
 
    if ( n!=P4_KEYINFO && n!=P4_VTAB ) {
57707
 
      freeP4(db, n, (void*)*(char**)&zP4);
57708
 
    }
57709
 
    return;
57710
 
  }
57711
 
  assert( p->nOp>0 );
57712
 
  assert( addr<p->nOp );
57713
 
  if( addr<0 ){
57714
 
    addr = p->nOp - 1;
57715
 
  }
57716
 
  pOp = &p->aOp[addr];
57717
 
  freeP4(db, pOp->p4type, pOp->p4.p);
57718
 
  pOp->p4.p = 0;
57719
 
  if( n==P4_INT32 ){
57720
 
    /* Note: this cast is safe, because the origin data point was an int
57721
 
    ** that was cast to a (const char *). */
57722
 
    pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
57723
 
    pOp->p4type = P4_INT32;
57724
 
  }else if( zP4==0 ){
57725
 
    pOp->p4.p = 0;
57726
 
    pOp->p4type = P4_NOTUSED;
57727
 
  }else if( n==P4_KEYINFO ){
57728
 
    KeyInfo *pKeyInfo;
57729
 
    int nField, nByte;
57730
 
 
57731
 
    nField = ((KeyInfo*)zP4)->nField;
57732
 
    nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
57733
 
    pKeyInfo = sqlite3DbMallocRaw(0, nByte);
57734
 
    pOp->p4.pKeyInfo = pKeyInfo;
57735
 
    if( pKeyInfo ){
57736
 
      u8 *aSortOrder;
57737
 
      memcpy((char*)pKeyInfo, zP4, nByte - nField);
57738
 
      aSortOrder = pKeyInfo->aSortOrder;
57739
 
      if( aSortOrder ){
57740
 
        pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
57741
 
        memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
57742
 
      }
57743
 
      pOp->p4type = P4_KEYINFO;
57744
 
    }else{
57745
 
      p->db->mallocFailed = 1;
57746
 
      pOp->p4type = P4_NOTUSED;
57747
 
    }
57748
 
  }else if( n==P4_KEYINFO_HANDOFF ){
57749
 
    pOp->p4.p = (void*)zP4;
57750
 
    pOp->p4type = P4_KEYINFO;
57751
 
  }else if( n==P4_VTAB ){
57752
 
    pOp->p4.p = (void*)zP4;
57753
 
    pOp->p4type = P4_VTAB;
57754
 
    sqlite3VtabLock((VTable *)zP4);
57755
 
    assert( ((VTable *)zP4)->db==p->db );
57756
 
  }else if( n<0 ){
57757
 
    pOp->p4.p = (void*)zP4;
57758
 
    pOp->p4type = (signed char)n;
57759
 
  }else{
57760
 
    if( n==0 ) n = sqlite3Strlen30(zP4);
57761
 
    pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
57762
 
    pOp->p4type = P4_DYNAMIC;
57763
 
  }
57764
 
}
57765
 
 
57766
 
#ifndef NDEBUG
57767
 
/*
57768
 
** Change the comment on the the most recently coded instruction.  Or
57769
 
** insert a No-op and add the comment to that new instruction.  This
57770
 
** makes the code easier to read during debugging.  None of this happens
57771
 
** in a production build.
57772
 
*/
57773
 
SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
57774
 
  va_list ap;
57775
 
  if( !p ) return;
57776
 
  assert( p->nOp>0 || p->aOp==0 );
57777
 
  assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
57778
 
  if( p->nOp ){
57779
 
    char **pz = &p->aOp[p->nOp-1].zComment;
57780
 
    va_start(ap, zFormat);
57781
 
    sqlite3DbFree(p->db, *pz);
57782
 
    *pz = sqlite3VMPrintf(p->db, zFormat, ap);
57783
 
    va_end(ap);
57784
 
  }
57785
 
}
57786
 
SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
57787
 
  va_list ap;
57788
 
  if( !p ) return;
57789
 
  sqlite3VdbeAddOp0(p, OP_Noop);
57790
 
  assert( p->nOp>0 || p->aOp==0 );
57791
 
  assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
57792
 
  if( p->nOp ){
57793
 
    char **pz = &p->aOp[p->nOp-1].zComment;
57794
 
    va_start(ap, zFormat);
57795
 
    sqlite3DbFree(p->db, *pz);
57796
 
    *pz = sqlite3VMPrintf(p->db, zFormat, ap);
57797
 
    va_end(ap);
57798
 
  }
57799
 
}
57800
 
#endif  /* NDEBUG */
57801
 
 
57802
 
/*
57803
 
** Return the opcode for a given address.  If the address is -1, then
57804
 
** return the most recently inserted opcode.
57805
 
**
57806
 
** If a memory allocation error has occurred prior to the calling of this
57807
 
** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
57808
 
** is readable but not writable, though it is cast to a writable value.
57809
 
** The return of a dummy opcode allows the call to continue functioning
57810
 
** after a OOM fault without having to check to see if the return from 
57811
 
** this routine is a valid pointer.  But because the dummy.opcode is 0,
57812
 
** dummy will never be written to.  This is verified by code inspection and
57813
 
** by running with Valgrind.
57814
 
**
57815
 
** About the #ifdef SQLITE_OMIT_TRACE:  Normally, this routine is never called
57816
 
** unless p->nOp>0.  This is because in the absense of SQLITE_OMIT_TRACE,
57817
 
** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
57818
 
** a new VDBE is created.  So we are free to set addr to p->nOp-1 without
57819
 
** having to double-check to make sure that the result is non-negative. But
57820
 
** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
57821
 
** check the value of p->nOp-1 before continuing.
57822
 
*/
57823
 
SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
57824
 
  /* C89 specifies that the constant "dummy" will be initialized to all
57825
 
  ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
57826
 
  static const VdbeOp dummy = {0, 0, 0, 0};
57827
 
  assert( p->magic==VDBE_MAGIC_INIT );
57828
 
  if( addr<0 ){
57829
 
#ifdef SQLITE_OMIT_TRACE
57830
 
    if( p->nOp==0 ) return (VdbeOp*)&dummy;
57831
 
#endif
57832
 
    addr = p->nOp - 1;
57833
 
  }
57834
 
  assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
57835
 
  if( p->db->mallocFailed ){
57836
 
    return (VdbeOp*)&dummy;
57837
 
  }else{
57838
 
    return &p->aOp[addr];
57839
 
  }
57840
 
}
57841
 
 
57842
 
#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
57843
 
     || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
57844
 
/*
57845
 
** Compute a string that describes the P4 parameter for an opcode.
57846
 
** Use zTemp for any required temporary buffer space.
57847
 
*/
57848
 
static char *displayP4(Op *pOp, char *zTemp, int nTemp){
57849
 
  char *zP4 = zTemp;
57850
 
  assert( nTemp>=20 );
57851
 
  switch( pOp->p4type ){
57852
 
    case P4_KEYINFO_STATIC:
57853
 
    case P4_KEYINFO: {
57854
 
      int i, j;
57855
 
      KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
57856
 
      sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
57857
 
      i = sqlite3Strlen30(zTemp);
57858
 
      for(j=0; j<pKeyInfo->nField; j++){
57859
 
        CollSeq *pColl = pKeyInfo->aColl[j];
57860
 
        if( pColl ){
57861
 
          int n = sqlite3Strlen30(pColl->zName);
57862
 
          if( i+n>nTemp-6 ){
57863
 
            memcpy(&zTemp[i],",...",4);
57864
 
            break;
57865
 
          }
57866
 
          zTemp[i++] = ',';
57867
 
          if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
57868
 
            zTemp[i++] = '-';
57869
 
          }
57870
 
          memcpy(&zTemp[i], pColl->zName,n+1);
57871
 
          i += n;
57872
 
        }else if( i+4<nTemp-6 ){
57873
 
          memcpy(&zTemp[i],",nil",4);
57874
 
          i += 4;
57875
 
        }
57876
 
      }
57877
 
      zTemp[i++] = ')';
57878
 
      zTemp[i] = 0;
57879
 
      assert( i<nTemp );
57880
 
      break;
57881
 
    }
57882
 
    case P4_COLLSEQ: {
57883
 
      CollSeq *pColl = pOp->p4.pColl;
57884
 
      sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
57885
 
      break;
57886
 
    }
57887
 
    case P4_FUNCDEF: {
57888
 
      FuncDef *pDef = pOp->p4.pFunc;
57889
 
      sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
57890
 
      break;
57891
 
    }
57892
 
    case P4_INT64: {
57893
 
      sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
57894
 
      break;
57895
 
    }
57896
 
    case P4_INT32: {
57897
 
      sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
57898
 
      break;
57899
 
    }
57900
 
    case P4_REAL: {
57901
 
      sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
57902
 
      break;
57903
 
    }
57904
 
    case P4_MEM: {
57905
 
      Mem *pMem = pOp->p4.pMem;
57906
 
      assert( (pMem->flags & MEM_Null)==0 );
57907
 
      if( pMem->flags & MEM_Str ){
57908
 
        zP4 = pMem->z;
57909
 
      }else if( pMem->flags & MEM_Int ){
57910
 
        sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
57911
 
      }else if( pMem->flags & MEM_Real ){
57912
 
        sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
57913
 
      }else{
57914
 
        assert( pMem->flags & MEM_Blob );
57915
 
        zP4 = "(blob)";
57916
 
      }
57917
 
      break;
57918
 
    }
57919
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
57920
 
    case P4_VTAB: {
57921
 
      sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
57922
 
      sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
57923
 
      break;
57924
 
    }
57925
 
#endif
57926
 
    case P4_INTARRAY: {
57927
 
      sqlite3_snprintf(nTemp, zTemp, "intarray");
57928
 
      break;
57929
 
    }
57930
 
    case P4_SUBPROGRAM: {
57931
 
      sqlite3_snprintf(nTemp, zTemp, "program");
57932
 
      break;
57933
 
    }
57934
 
    default: {
57935
 
      zP4 = pOp->p4.z;
57936
 
      if( zP4==0 ){
57937
 
        zP4 = zTemp;
57938
 
        zTemp[0] = 0;
57939
 
      }
57940
 
    }
57941
 
  }
57942
 
  assert( zP4!=0 );
57943
 
  return zP4;
57944
 
}
57945
 
#endif
57946
 
 
57947
 
/*
57948
 
** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
57949
 
**
57950
 
** The prepared statements need to know in advance the complete set of
57951
 
** attached databases that they will be using.  A mask of these databases
57952
 
** is maintained in p->btreeMask and is used for locking and other purposes.
57953
 
*/
57954
 
SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
57955
 
  assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
57956
 
  assert( i<(int)sizeof(p->btreeMask)*8 );
57957
 
  p->btreeMask |= ((yDbMask)1)<<i;
57958
 
  if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
57959
 
    p->lockMask |= ((yDbMask)1)<<i;
57960
 
  }
57961
 
}
57962
 
 
57963
 
#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
57964
 
/*
57965
 
** If SQLite is compiled to support shared-cache mode and to be threadsafe,
57966
 
** this routine obtains the mutex associated with each BtShared structure
57967
 
** that may be accessed by the VM passed as an argument. In doing so it also
57968
 
** sets the BtShared.db member of each of the BtShared structures, ensuring
57969
 
** that the correct busy-handler callback is invoked if required.
57970
 
**
57971
 
** If SQLite is not threadsafe but does support shared-cache mode, then
57972
 
** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
57973
 
** of all of BtShared structures accessible via the database handle 
57974
 
** associated with the VM.
57975
 
**
57976
 
** If SQLite is not threadsafe and does not support shared-cache mode, this
57977
 
** function is a no-op.
57978
 
**
57979
 
** The p->btreeMask field is a bitmask of all btrees that the prepared 
57980
 
** statement p will ever use.  Let N be the number of bits in p->btreeMask
57981
 
** corresponding to btrees that use shared cache.  Then the runtime of
57982
 
** this routine is N*N.  But as N is rarely more than 1, this should not
57983
 
** be a problem.
57984
 
*/
57985
 
SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
57986
 
  int i;
57987
 
  yDbMask mask;
57988
 
  sqlite3 *db;
57989
 
  Db *aDb;
57990
 
  int nDb;
57991
 
  if( p->lockMask==0 ) return;  /* The common case */
57992
 
  db = p->db;
57993
 
  aDb = db->aDb;
57994
 
  nDb = db->nDb;
57995
 
  for(i=0, mask=1; i<nDb; i++, mask += mask){
57996
 
    if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
57997
 
      sqlite3BtreeEnter(aDb[i].pBt);
57998
 
    }
57999
 
  }
58000
 
}
58001
 
#endif
58002
 
 
58003
 
#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
58004
 
/*
58005
 
** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
58006
 
*/
58007
 
SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
58008
 
  int i;
58009
 
  yDbMask mask;
58010
 
  sqlite3 *db;
58011
 
  Db *aDb;
58012
 
  int nDb;
58013
 
  if( p->lockMask==0 ) return;  /* The common case */
58014
 
  db = p->db;
58015
 
  aDb = db->aDb;
58016
 
  nDb = db->nDb;
58017
 
  for(i=0, mask=1; i<nDb; i++, mask += mask){
58018
 
    if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
58019
 
      sqlite3BtreeLeave(aDb[i].pBt);
58020
 
    }
58021
 
  }
58022
 
}
58023
 
#endif
58024
 
 
58025
 
#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
58026
 
/*
58027
 
** Print a single opcode.  This routine is used for debugging only.
58028
 
*/
58029
 
SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
58030
 
  char *zP4;
58031
 
  char zPtr[50];
58032
 
  static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
58033
 
  if( pOut==0 ) pOut = stdout;
58034
 
  zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
58035
 
  fprintf(pOut, zFormat1, pc, 
58036
 
      sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
58037
 
#ifdef SQLITE_DEBUG
58038
 
      pOp->zComment ? pOp->zComment : ""
58039
 
#else
58040
 
      ""
58041
 
#endif
58042
 
  );
58043
 
  fflush(pOut);
58044
 
}
58045
 
#endif
58046
 
 
58047
 
/*
58048
 
** Release an array of N Mem elements
58049
 
*/
58050
 
static void releaseMemArray(Mem *p, int N){
58051
 
  if( p && N ){
58052
 
    Mem *pEnd;
58053
 
    sqlite3 *db = p->db;
58054
 
    u8 malloc_failed = db->mallocFailed;
58055
 
    if( db->pnBytesFreed ){
58056
 
      for(pEnd=&p[N]; p<pEnd; p++){
58057
 
        sqlite3DbFree(db, p->zMalloc);
58058
 
      }
58059
 
      return;
58060
 
    }
58061
 
    for(pEnd=&p[N]; p<pEnd; p++){
58062
 
      assert( (&p[1])==pEnd || p[0].db==p[1].db );
58063
 
 
58064
 
      /* This block is really an inlined version of sqlite3VdbeMemRelease()
58065
 
      ** that takes advantage of the fact that the memory cell value is 
58066
 
      ** being set to NULL after releasing any dynamic resources.
58067
 
      **
58068
 
      ** The justification for duplicating code is that according to 
58069
 
      ** callgrind, this causes a certain test case to hit the CPU 4.7 
58070
 
      ** percent less (x86 linux, gcc version 4.1.2, -O6) than if 
58071
 
      ** sqlite3MemRelease() were called from here. With -O2, this jumps
58072
 
      ** to 6.6 percent. The test case is inserting 1000 rows into a table 
58073
 
      ** with no indexes using a single prepared INSERT statement, bind() 
58074
 
      ** and reset(). Inserts are grouped into a transaction.
58075
 
      */
58076
 
      if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
58077
 
        sqlite3VdbeMemRelease(p);
58078
 
      }else if( p->zMalloc ){
58079
 
        sqlite3DbFree(db, p->zMalloc);
58080
 
        p->zMalloc = 0;
58081
 
      }
58082
 
 
58083
 
      p->flags = MEM_Null;
58084
 
    }
58085
 
    db->mallocFailed = malloc_failed;
58086
 
  }
58087
 
}
58088
 
 
58089
 
/*
58090
 
** Delete a VdbeFrame object and its contents. VdbeFrame objects are
58091
 
** allocated by the OP_Program opcode in sqlite3VdbeExec().
58092
 
*/
58093
 
SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
58094
 
  int i;
58095
 
  Mem *aMem = VdbeFrameMem(p);
58096
 
  VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
58097
 
  for(i=0; i<p->nChildCsr; i++){
58098
 
    sqlite3VdbeFreeCursor(p->v, apCsr[i]);
58099
 
  }
58100
 
  releaseMemArray(aMem, p->nChildMem);
58101
 
  sqlite3DbFree(p->v->db, p);
58102
 
}
58103
 
 
58104
 
#ifndef SQLITE_OMIT_EXPLAIN
58105
 
/*
58106
 
** Give a listing of the program in the virtual machine.
58107
 
**
58108
 
** The interface is the same as sqlite3VdbeExec().  But instead of
58109
 
** running the code, it invokes the callback once for each instruction.
58110
 
** This feature is used to implement "EXPLAIN".
58111
 
**
58112
 
** When p->explain==1, each instruction is listed.  When
58113
 
** p->explain==2, only OP_Explain instructions are listed and these
58114
 
** are shown in a different format.  p->explain==2 is used to implement
58115
 
** EXPLAIN QUERY PLAN.
58116
 
**
58117
 
** When p->explain==1, first the main program is listed, then each of
58118
 
** the trigger subprograms are listed one by one.
58119
 
*/
58120
 
SQLITE_PRIVATE int sqlite3VdbeList(
58121
 
  Vdbe *p                   /* The VDBE */
58122
 
){
58123
 
  int nRow;                            /* Stop when row count reaches this */
58124
 
  int nSub = 0;                        /* Number of sub-vdbes seen so far */
58125
 
  SubProgram **apSub = 0;              /* Array of sub-vdbes */
58126
 
  Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
58127
 
  sqlite3 *db = p->db;                 /* The database connection */
58128
 
  int i;                               /* Loop counter */
58129
 
  int rc = SQLITE_OK;                  /* Return code */
58130
 
  Mem *pMem = p->pResultSet = &p->aMem[1];  /* First Mem of result set */
58131
 
 
58132
 
  assert( p->explain );
58133
 
  assert( p->magic==VDBE_MAGIC_RUN );
58134
 
  assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
58135
 
 
58136
 
  /* Even though this opcode does not use dynamic strings for
58137
 
  ** the result, result columns may become dynamic if the user calls
58138
 
  ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
58139
 
  */
58140
 
  releaseMemArray(pMem, 8);
58141
 
 
58142
 
  if( p->rc==SQLITE_NOMEM ){
58143
 
    /* This happens if a malloc() inside a call to sqlite3_column_text() or
58144
 
    ** sqlite3_column_text16() failed.  */
58145
 
    db->mallocFailed = 1;
58146
 
    return SQLITE_ERROR;
58147
 
  }
58148
 
 
58149
 
  /* When the number of output rows reaches nRow, that means the
58150
 
  ** listing has finished and sqlite3_step() should return SQLITE_DONE.
58151
 
  ** nRow is the sum of the number of rows in the main program, plus
58152
 
  ** the sum of the number of rows in all trigger subprograms encountered
58153
 
  ** so far.  The nRow value will increase as new trigger subprograms are
58154
 
  ** encountered, but p->pc will eventually catch up to nRow.
58155
 
  */
58156
 
  nRow = p->nOp;
58157
 
  if( p->explain==1 ){
58158
 
    /* The first 8 memory cells are used for the result set.  So we will
58159
 
    ** commandeer the 9th cell to use as storage for an array of pointers
58160
 
    ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
58161
 
    ** cells.  */
58162
 
    assert( p->nMem>9 );
58163
 
    pSub = &p->aMem[9];
58164
 
    if( pSub->flags&MEM_Blob ){
58165
 
      /* On the first call to sqlite3_step(), pSub will hold a NULL.  It is
58166
 
      ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
58167
 
      nSub = pSub->n/sizeof(Vdbe*);
58168
 
      apSub = (SubProgram **)pSub->z;
58169
 
    }
58170
 
    for(i=0; i<nSub; i++){
58171
 
      nRow += apSub[i]->nOp;
58172
 
    }
58173
 
  }
58174
 
 
58175
 
  do{
58176
 
    i = p->pc++;
58177
 
  }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
58178
 
  if( i>=nRow ){
58179
 
    p->rc = SQLITE_OK;
58180
 
    rc = SQLITE_DONE;
58181
 
  }else if( db->u1.isInterrupted ){
58182
 
    p->rc = SQLITE_INTERRUPT;
58183
 
    rc = SQLITE_ERROR;
58184
 
    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
58185
 
  }else{
58186
 
    char *z;
58187
 
    Op *pOp;
58188
 
    if( i<p->nOp ){
58189
 
      /* The output line number is small enough that we are still in the
58190
 
      ** main program. */
58191
 
      pOp = &p->aOp[i];
58192
 
    }else{
58193
 
      /* We are currently listing subprograms.  Figure out which one and
58194
 
      ** pick up the appropriate opcode. */
58195
 
      int j;
58196
 
      i -= p->nOp;
58197
 
      for(j=0; i>=apSub[j]->nOp; j++){
58198
 
        i -= apSub[j]->nOp;
58199
 
      }
58200
 
      pOp = &apSub[j]->aOp[i];
58201
 
    }
58202
 
    if( p->explain==1 ){
58203
 
      pMem->flags = MEM_Int;
58204
 
      pMem->type = SQLITE_INTEGER;
58205
 
      pMem->u.i = i;                                /* Program counter */
58206
 
      pMem++;
58207
 
  
58208
 
      pMem->flags = MEM_Static|MEM_Str|MEM_Term;
58209
 
      pMem->z = (char*)sqlite3OpcodeName(pOp->opcode);  /* Opcode */
58210
 
      assert( pMem->z!=0 );
58211
 
      pMem->n = sqlite3Strlen30(pMem->z);
58212
 
      pMem->type = SQLITE_TEXT;
58213
 
      pMem->enc = SQLITE_UTF8;
58214
 
      pMem++;
58215
 
 
58216
 
      /* When an OP_Program opcode is encounter (the only opcode that has
58217
 
      ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
58218
 
      ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
58219
 
      ** has not already been seen.
58220
 
      */
58221
 
      if( pOp->p4type==P4_SUBPROGRAM ){
58222
 
        int nByte = (nSub+1)*sizeof(SubProgram*);
58223
 
        int j;
58224
 
        for(j=0; j<nSub; j++){
58225
 
          if( apSub[j]==pOp->p4.pProgram ) break;
58226
 
        }
58227
 
        if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, 1) ){
58228
 
          apSub = (SubProgram **)pSub->z;
58229
 
          apSub[nSub++] = pOp->p4.pProgram;
58230
 
          pSub->flags |= MEM_Blob;
58231
 
          pSub->n = nSub*sizeof(SubProgram*);
58232
 
        }
58233
 
      }
58234
 
    }
58235
 
 
58236
 
    pMem->flags = MEM_Int;
58237
 
    pMem->u.i = pOp->p1;                          /* P1 */
58238
 
    pMem->type = SQLITE_INTEGER;
58239
 
    pMem++;
58240
 
 
58241
 
    pMem->flags = MEM_Int;
58242
 
    pMem->u.i = pOp->p2;                          /* P2 */
58243
 
    pMem->type = SQLITE_INTEGER;
58244
 
    pMem++;
58245
 
 
58246
 
    pMem->flags = MEM_Int;
58247
 
    pMem->u.i = pOp->p3;                          /* P3 */
58248
 
    pMem->type = SQLITE_INTEGER;
58249
 
    pMem++;
58250
 
 
58251
 
    if( sqlite3VdbeMemGrow(pMem, 32, 0) ){            /* P4 */
58252
 
      assert( p->db->mallocFailed );
58253
 
      return SQLITE_ERROR;
58254
 
    }
58255
 
    pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
58256
 
    z = displayP4(pOp, pMem->z, 32);
58257
 
    if( z!=pMem->z ){
58258
 
      sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0);
58259
 
    }else{
58260
 
      assert( pMem->z!=0 );
58261
 
      pMem->n = sqlite3Strlen30(pMem->z);
58262
 
      pMem->enc = SQLITE_UTF8;
58263
 
    }
58264
 
    pMem->type = SQLITE_TEXT;
58265
 
    pMem++;
58266
 
 
58267
 
    if( p->explain==1 ){
58268
 
      if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
58269
 
        assert( p->db->mallocFailed );
58270
 
        return SQLITE_ERROR;
58271
 
      }
58272
 
      pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
58273
 
      pMem->n = 2;
58274
 
      sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
58275
 
      pMem->type = SQLITE_TEXT;
58276
 
      pMem->enc = SQLITE_UTF8;
58277
 
      pMem++;
58278
 
  
58279
 
#ifdef SQLITE_DEBUG
58280
 
      if( pOp->zComment ){
58281
 
        pMem->flags = MEM_Str|MEM_Term;
58282
 
        pMem->z = pOp->zComment;
58283
 
        pMem->n = sqlite3Strlen30(pMem->z);
58284
 
        pMem->enc = SQLITE_UTF8;
58285
 
        pMem->type = SQLITE_TEXT;
58286
 
      }else
58287
 
#endif
58288
 
      {
58289
 
        pMem->flags = MEM_Null;                       /* Comment */
58290
 
        pMem->type = SQLITE_NULL;
58291
 
      }
58292
 
    }
58293
 
 
58294
 
    p->nResColumn = 8 - 4*(p->explain-1);
58295
 
    p->rc = SQLITE_OK;
58296
 
    rc = SQLITE_ROW;
58297
 
  }
58298
 
  return rc;
58299
 
}
58300
 
#endif /* SQLITE_OMIT_EXPLAIN */
58301
 
 
58302
 
#ifdef SQLITE_DEBUG
58303
 
/*
58304
 
** Print the SQL that was used to generate a VDBE program.
58305
 
*/
58306
 
SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
58307
 
  int nOp = p->nOp;
58308
 
  VdbeOp *pOp;
58309
 
  if( nOp<1 ) return;
58310
 
  pOp = &p->aOp[0];
58311
 
  if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
58312
 
    const char *z = pOp->p4.z;
58313
 
    while( sqlite3Isspace(*z) ) z++;
58314
 
    printf("SQL: [%s]\n", z);
58315
 
  }
58316
 
}
58317
 
#endif
58318
 
 
58319
 
#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
58320
 
/*
58321
 
** Print an IOTRACE message showing SQL content.
58322
 
*/
58323
 
SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
58324
 
  int nOp = p->nOp;
58325
 
  VdbeOp *pOp;
58326
 
  if( sqlite3IoTrace==0 ) return;
58327
 
  if( nOp<1 ) return;
58328
 
  pOp = &p->aOp[0];
58329
 
  if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
58330
 
    int i, j;
58331
 
    char z[1000];
58332
 
    sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
58333
 
    for(i=0; sqlite3Isspace(z[i]); i++){}
58334
 
    for(j=0; z[i]; i++){
58335
 
      if( sqlite3Isspace(z[i]) ){
58336
 
        if( z[i-1]!=' ' ){
58337
 
          z[j++] = ' ';
58338
 
        }
58339
 
      }else{
58340
 
        z[j++] = z[i];
58341
 
      }
58342
 
    }
58343
 
    z[j] = 0;
58344
 
    sqlite3IoTrace("SQL %s\n", z);
58345
 
  }
58346
 
}
58347
 
#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
58348
 
 
58349
 
/*
58350
 
** Allocate space from a fixed size buffer and return a pointer to
58351
 
** that space.  If insufficient space is available, return NULL.
58352
 
**
58353
 
** The pBuf parameter is the initial value of a pointer which will
58354
 
** receive the new memory.  pBuf is normally NULL.  If pBuf is not
58355
 
** NULL, it means that memory space has already been allocated and that
58356
 
** this routine should not allocate any new memory.  When pBuf is not
58357
 
** NULL simply return pBuf.  Only allocate new memory space when pBuf
58358
 
** is NULL.
58359
 
**
58360
 
** nByte is the number of bytes of space needed.
58361
 
**
58362
 
** *ppFrom points to available space and pEnd points to the end of the
58363
 
** available space.  When space is allocated, *ppFrom is advanced past
58364
 
** the end of the allocated space.
58365
 
**
58366
 
** *pnByte is a counter of the number of bytes of space that have failed
58367
 
** to allocate.  If there is insufficient space in *ppFrom to satisfy the
58368
 
** request, then increment *pnByte by the amount of the request.
58369
 
*/
58370
 
static void *allocSpace(
58371
 
  void *pBuf,          /* Where return pointer will be stored */
58372
 
  int nByte,           /* Number of bytes to allocate */
58373
 
  u8 **ppFrom,         /* IN/OUT: Allocate from *ppFrom */
58374
 
  u8 *pEnd,            /* Pointer to 1 byte past the end of *ppFrom buffer */
58375
 
  int *pnByte          /* If allocation cannot be made, increment *pnByte */
58376
 
){
58377
 
  assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
58378
 
  if( pBuf ) return pBuf;
58379
 
  nByte = ROUND8(nByte);
58380
 
  if( &(*ppFrom)[nByte] <= pEnd ){
58381
 
    pBuf = (void*)*ppFrom;
58382
 
    *ppFrom += nByte;
58383
 
  }else{
58384
 
    *pnByte += nByte;
58385
 
  }
58386
 
  return pBuf;
58387
 
}
58388
 
 
58389
 
/*
58390
 
** Prepare a virtual machine for execution.  This involves things such
58391
 
** as allocating stack space and initializing the program counter.
58392
 
** After the VDBE has be prepped, it can be executed by one or more
58393
 
** calls to sqlite3VdbeExec().  
58394
 
**
58395
 
** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
58396
 
** VDBE_MAGIC_RUN.
58397
 
**
58398
 
** This function may be called more than once on a single virtual machine.
58399
 
** The first call is made while compiling the SQL statement. Subsequent
58400
 
** calls are made as part of the process of resetting a statement to be
58401
 
** re-executed (from a call to sqlite3_reset()). The nVar, nMem, nCursor 
58402
 
** and isExplain parameters are only passed correct values the first time
58403
 
** the function is called. On subsequent calls, from sqlite3_reset(), nVar
58404
 
** is passed -1 and nMem, nCursor and isExplain are all passed zero.
58405
 
*/
58406
 
SQLITE_PRIVATE void sqlite3VdbeMakeReady(
58407
 
  Vdbe *p,                       /* The VDBE */
58408
 
  int nVar,                      /* Number of '?' see in the SQL statement */
58409
 
  int nMem,                      /* Number of memory cells to allocate */
58410
 
  int nCursor,                   /* Number of cursors to allocate */
58411
 
  int nArg,                      /* Maximum number of args in SubPrograms */
58412
 
  int isExplain,                 /* True if the EXPLAIN keywords is present */
58413
 
  int usesStmtJournal            /* True to set Vdbe.usesStmtJournal */
58414
 
){
58415
 
  int n;
58416
 
  sqlite3 *db = p->db;
58417
 
 
58418
 
  assert( p!=0 );
58419
 
  assert( p->magic==VDBE_MAGIC_INIT );
58420
 
 
58421
 
  /* There should be at least one opcode.
58422
 
  */
58423
 
  assert( p->nOp>0 );
58424
 
 
58425
 
  /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
58426
 
  p->magic = VDBE_MAGIC_RUN;
58427
 
 
58428
 
  /* For each cursor required, also allocate a memory cell. Memory
58429
 
  ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
58430
 
  ** the vdbe program. Instead they are used to allocate space for
58431
 
  ** VdbeCursor/BtCursor structures. The blob of memory associated with 
58432
 
  ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
58433
 
  ** stores the blob of memory associated with cursor 1, etc.
58434
 
  **
58435
 
  ** See also: allocateCursor().
58436
 
  */
58437
 
  nMem += nCursor;
58438
 
 
58439
 
  /* Allocate space for memory registers, SQL variables, VDBE cursors and 
58440
 
  ** an array to marshal SQL function arguments in. This is only done the
58441
 
  ** first time this function is called for a given VDBE, not when it is
58442
 
  ** being called from sqlite3_reset() to reset the virtual machine.
58443
 
  */
58444
 
  if( nVar>=0 && ALWAYS(db->mallocFailed==0) ){
58445
 
    u8 *zCsr = (u8 *)&p->aOp[p->nOp];       /* Memory avaliable for alloation */
58446
 
    u8 *zEnd = (u8 *)&p->aOp[p->nOpAlloc];  /* First byte past available mem */
58447
 
    int nByte;                              /* How much extra memory needed */
58448
 
 
58449
 
    resolveP2Values(p, &nArg);
58450
 
    p->usesStmtJournal = (u8)usesStmtJournal;
58451
 
    if( isExplain && nMem<10 ){
58452
 
      nMem = 10;
58453
 
    }
58454
 
    memset(zCsr, 0, zEnd-zCsr);
58455
 
    zCsr += (zCsr - (u8*)0)&7;
58456
 
    assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
58457
 
 
58458
 
    /* Memory for registers, parameters, cursor, etc, is allocated in two
58459
 
    ** passes.  On the first pass, we try to reuse unused space at the 
58460
 
    ** end of the opcode array.  If we are unable to satisfy all memory
58461
 
    ** requirements by reusing the opcode array tail, then the second
58462
 
    ** pass will fill in the rest using a fresh allocation.  
58463
 
    **
58464
 
    ** This two-pass approach that reuses as much memory as possible from
58465
 
    ** the leftover space at the end of the opcode array can significantly
58466
 
    ** reduce the amount of memory held by a prepared statement.
58467
 
    */
58468
 
    do {
58469
 
      nByte = 0;
58470
 
      p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
58471
 
      p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
58472
 
      p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
58473
 
      p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
58474
 
      p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
58475
 
                            &zCsr, zEnd, &nByte);
58476
 
      if( nByte ){
58477
 
        p->pFree = sqlite3DbMallocZero(db, nByte);
58478
 
      }
58479
 
      zCsr = p->pFree;
58480
 
      zEnd = &zCsr[nByte];
58481
 
    }while( nByte && !db->mallocFailed );
58482
 
 
58483
 
    p->nCursor = (u16)nCursor;
58484
 
    if( p->aVar ){
58485
 
      p->nVar = (ynVar)nVar;
58486
 
      for(n=0; n<nVar; n++){
58487
 
        p->aVar[n].flags = MEM_Null;
58488
 
        p->aVar[n].db = db;
58489
 
      }
58490
 
    }
58491
 
    if( p->aMem ){
58492
 
      p->aMem--;                      /* aMem[] goes from 1..nMem */
58493
 
      p->nMem = nMem;                 /*       not from 0..nMem-1 */
58494
 
      for(n=1; n<=nMem; n++){
58495
 
        p->aMem[n].flags = MEM_Null;
58496
 
        p->aMem[n].db = db;
58497
 
      }
58498
 
    }
58499
 
  }
58500
 
#ifdef SQLITE_DEBUG
58501
 
  for(n=1; n<p->nMem; n++){
58502
 
    assert( p->aMem[n].db==db );
58503
 
  }
58504
 
#endif
58505
 
 
58506
 
  p->pc = -1;
58507
 
  p->rc = SQLITE_OK;
58508
 
  p->errorAction = OE_Abort;
58509
 
  p->explain |= isExplain;
58510
 
  p->magic = VDBE_MAGIC_RUN;
58511
 
  p->nChange = 0;
58512
 
  p->cacheCtr = 1;
58513
 
  p->minWriteFileFormat = 255;
58514
 
  p->iStatement = 0;
58515
 
  p->nFkConstraint = 0;
58516
 
#ifdef VDBE_PROFILE
58517
 
  {
58518
 
    int i;
58519
 
    for(i=0; i<p->nOp; i++){
58520
 
      p->aOp[i].cnt = 0;
58521
 
      p->aOp[i].cycles = 0;
58522
 
    }
58523
 
  }
58524
 
#endif
58525
 
}
58526
 
 
58527
 
/*
58528
 
** Close a VDBE cursor and release all the resources that cursor 
58529
 
** happens to hold.
58530
 
*/
58531
 
SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
58532
 
  if( pCx==0 ){
58533
 
    return;
58534
 
  }
58535
 
  if( pCx->pBt ){
58536
 
    sqlite3BtreeClose(pCx->pBt);
58537
 
    /* The pCx->pCursor will be close automatically, if it exists, by
58538
 
    ** the call above. */
58539
 
  }else if( pCx->pCursor ){
58540
 
    sqlite3BtreeCloseCursor(pCx->pCursor);
58541
 
  }
58542
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
58543
 
  if( pCx->pVtabCursor ){
58544
 
    sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
58545
 
    const sqlite3_module *pModule = pCx->pModule;
58546
 
    p->inVtabMethod = 1;
58547
 
    pModule->xClose(pVtabCursor);
58548
 
    p->inVtabMethod = 0;
58549
 
  }
58550
 
#endif
58551
 
}
58552
 
 
58553
 
/*
58554
 
** Copy the values stored in the VdbeFrame structure to its Vdbe. This
58555
 
** is used, for example, when a trigger sub-program is halted to restore
58556
 
** control to the main program.
58557
 
*/
58558
 
SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
58559
 
  Vdbe *v = pFrame->v;
58560
 
  v->aOp = pFrame->aOp;
58561
 
  v->nOp = pFrame->nOp;
58562
 
  v->aMem = pFrame->aMem;
58563
 
  v->nMem = pFrame->nMem;
58564
 
  v->apCsr = pFrame->apCsr;
58565
 
  v->nCursor = pFrame->nCursor;
58566
 
  v->db->lastRowid = pFrame->lastRowid;
58567
 
  v->nChange = pFrame->nChange;
58568
 
  return pFrame->pc;
58569
 
}
58570
 
 
58571
 
/*
58572
 
** Close all cursors.
58573
 
**
58574
 
** Also release any dynamic memory held by the VM in the Vdbe.aMem memory 
58575
 
** cell array. This is necessary as the memory cell array may contain
58576
 
** pointers to VdbeFrame objects, which may in turn contain pointers to
58577
 
** open cursors.
58578
 
*/
58579
 
static void closeAllCursors(Vdbe *p){
58580
 
  if( p->pFrame ){
58581
 
    VdbeFrame *pFrame;
58582
 
    for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
58583
 
    sqlite3VdbeFrameRestore(pFrame);
58584
 
  }
58585
 
  p->pFrame = 0;
58586
 
  p->nFrame = 0;
58587
 
 
58588
 
  if( p->apCsr ){
58589
 
    int i;
58590
 
    for(i=0; i<p->nCursor; i++){
58591
 
      VdbeCursor *pC = p->apCsr[i];
58592
 
      if( pC ){
58593
 
        sqlite3VdbeFreeCursor(p, pC);
58594
 
        p->apCsr[i] = 0;
58595
 
      }
58596
 
    }
58597
 
  }
58598
 
  if( p->aMem ){
58599
 
    releaseMemArray(&p->aMem[1], p->nMem);
58600
 
  }
58601
 
  while( p->pDelFrame ){
58602
 
    VdbeFrame *pDel = p->pDelFrame;
58603
 
    p->pDelFrame = pDel->pParent;
58604
 
    sqlite3VdbeFrameDelete(pDel);
58605
 
  }
58606
 
}
58607
 
 
58608
 
/*
58609
 
** Clean up the VM after execution.
58610
 
**
58611
 
** This routine will automatically close any cursors, lists, and/or
58612
 
** sorters that were left open.  It also deletes the values of
58613
 
** variables in the aVar[] array.
58614
 
*/
58615
 
static void Cleanup(Vdbe *p){
58616
 
  sqlite3 *db = p->db;
58617
 
 
58618
 
#ifdef SQLITE_DEBUG
58619
 
  /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
58620
 
  ** Vdbe.aMem[] arrays have already been cleaned up.  */
58621
 
  int i;
58622
 
  for(i=0; i<p->nCursor; i++) assert( p->apCsr==0 || p->apCsr[i]==0 );
58623
 
  for(i=1; i<=p->nMem; i++) assert( p->aMem==0 || p->aMem[i].flags==MEM_Null );
58624
 
#endif
58625
 
 
58626
 
  sqlite3DbFree(db, p->zErrMsg);
58627
 
  p->zErrMsg = 0;
58628
 
  p->pResultSet = 0;
58629
 
}
58630
 
 
58631
 
/*
58632
 
** Set the number of result columns that will be returned by this SQL
58633
 
** statement. This is now set at compile time, rather than during
58634
 
** execution of the vdbe program so that sqlite3_column_count() can
58635
 
** be called on an SQL statement before sqlite3_step().
58636
 
*/
58637
 
SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
58638
 
  Mem *pColName;
58639
 
  int n;
58640
 
  sqlite3 *db = p->db;
58641
 
 
58642
 
  releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
58643
 
  sqlite3DbFree(db, p->aColName);
58644
 
  n = nResColumn*COLNAME_N;
58645
 
  p->nResColumn = (u16)nResColumn;
58646
 
  p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
58647
 
  if( p->aColName==0 ) return;
58648
 
  while( n-- > 0 ){
58649
 
    pColName->flags = MEM_Null;
58650
 
    pColName->db = p->db;
58651
 
    pColName++;
58652
 
  }
58653
 
}
58654
 
 
58655
 
/*
58656
 
** Set the name of the idx'th column to be returned by the SQL statement.
58657
 
** zName must be a pointer to a nul terminated string.
58658
 
**
58659
 
** This call must be made after a call to sqlite3VdbeSetNumCols().
58660
 
**
58661
 
** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
58662
 
** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
58663
 
** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
58664
 
*/
58665
 
SQLITE_PRIVATE int sqlite3VdbeSetColName(
58666
 
  Vdbe *p,                         /* Vdbe being configured */
58667
 
  int idx,                         /* Index of column zName applies to */
58668
 
  int var,                         /* One of the COLNAME_* constants */
58669
 
  const char *zName,               /* Pointer to buffer containing name */
58670
 
  void (*xDel)(void*)              /* Memory management strategy for zName */
58671
 
){
58672
 
  int rc;
58673
 
  Mem *pColName;
58674
 
  assert( idx<p->nResColumn );
58675
 
  assert( var<COLNAME_N );
58676
 
  if( p->db->mallocFailed ){
58677
 
    assert( !zName || xDel!=SQLITE_DYNAMIC );
58678
 
    return SQLITE_NOMEM;
58679
 
  }
58680
 
  assert( p->aColName!=0 );
58681
 
  pColName = &(p->aColName[idx+var*p->nResColumn]);
58682
 
  rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
58683
 
  assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
58684
 
  return rc;
58685
 
}
58686
 
 
58687
 
/*
58688
 
** A read or write transaction may or may not be active on database handle
58689
 
** db. If a transaction is active, commit it. If there is a
58690
 
** write-transaction spanning more than one database file, this routine
58691
 
** takes care of the master journal trickery.
58692
 
*/
58693
 
static int vdbeCommit(sqlite3 *db, Vdbe *p){
58694
 
  int i;
58695
 
  int nTrans = 0;  /* Number of databases with an active write-transaction */
58696
 
  int rc = SQLITE_OK;
58697
 
  int needXcommit = 0;
58698
 
 
58699
 
#ifdef SQLITE_OMIT_VIRTUALTABLE
58700
 
  /* With this option, sqlite3VtabSync() is defined to be simply 
58701
 
  ** SQLITE_OK so p is not used. 
58702
 
  */
58703
 
  UNUSED_PARAMETER(p);
58704
 
#endif
58705
 
 
58706
 
  /* Before doing anything else, call the xSync() callback for any
58707
 
  ** virtual module tables written in this transaction. This has to
58708
 
  ** be done before determining whether a master journal file is 
58709
 
  ** required, as an xSync() callback may add an attached database
58710
 
  ** to the transaction.
58711
 
  */
58712
 
  rc = sqlite3VtabSync(db, &p->zErrMsg);
58713
 
 
58714
 
  /* This loop determines (a) if the commit hook should be invoked and
58715
 
  ** (b) how many database files have open write transactions, not 
58716
 
  ** including the temp database. (b) is important because if more than 
58717
 
  ** one database file has an open write transaction, a master journal
58718
 
  ** file is required for an atomic commit.
58719
 
  */ 
58720
 
  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
58721
 
    Btree *pBt = db->aDb[i].pBt;
58722
 
    if( sqlite3BtreeIsInTrans(pBt) ){
58723
 
      needXcommit = 1;
58724
 
      if( i!=1 ) nTrans++;
58725
 
      rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
58726
 
    }
58727
 
  }
58728
 
  if( rc!=SQLITE_OK ){
58729
 
    return rc;
58730
 
  }
58731
 
 
58732
 
  /* If there are any write-transactions at all, invoke the commit hook */
58733
 
  if( needXcommit && db->xCommitCallback ){
58734
 
    rc = db->xCommitCallback(db->pCommitArg);
58735
 
    if( rc ){
58736
 
      return SQLITE_CONSTRAINT;
58737
 
    }
58738
 
  }
58739
 
 
58740
 
  /* The simple case - no more than one database file (not counting the
58741
 
  ** TEMP database) has a transaction active.   There is no need for the
58742
 
  ** master-journal.
58743
 
  **
58744
 
  ** If the return value of sqlite3BtreeGetFilename() is a zero length
58745
 
  ** string, it means the main database is :memory: or a temp file.  In 
58746
 
  ** that case we do not support atomic multi-file commits, so use the 
58747
 
  ** simple case then too.
58748
 
  */
58749
 
  if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
58750
 
   || nTrans<=1
58751
 
  ){
58752
 
    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
58753
 
      Btree *pBt = db->aDb[i].pBt;
58754
 
      if( pBt ){
58755
 
        rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
58756
 
      }
58757
 
    }
58758
 
 
58759
 
    /* Do the commit only if all databases successfully complete phase 1. 
58760
 
    ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
58761
 
    ** IO error while deleting or truncating a journal file. It is unlikely,
58762
 
    ** but could happen. In this case abandon processing and return the error.
58763
 
    */
58764
 
    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
58765
 
      Btree *pBt = db->aDb[i].pBt;
58766
 
      if( pBt ){
58767
 
        rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
58768
 
      }
58769
 
    }
58770
 
    if( rc==SQLITE_OK ){
58771
 
      sqlite3VtabCommit(db);
58772
 
    }
58773
 
  }
58774
 
 
58775
 
  /* The complex case - There is a multi-file write-transaction active.
58776
 
  ** This requires a master journal file to ensure the transaction is
58777
 
  ** committed atomicly.
58778
 
  */
58779
 
#ifndef SQLITE_OMIT_DISKIO
58780
 
  else{
58781
 
    sqlite3_vfs *pVfs = db->pVfs;
58782
 
    int needSync = 0;
58783
 
    char *zMaster = 0;   /* File-name for the master journal */
58784
 
    char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
58785
 
    sqlite3_file *pMaster = 0;
58786
 
    i64 offset = 0;
58787
 
    int res;
58788
 
 
58789
 
    /* Select a master journal file name */
58790
 
    do {
58791
 
      u32 iRandom;
58792
 
      sqlite3DbFree(db, zMaster);
58793
 
      sqlite3_randomness(sizeof(iRandom), &iRandom);
58794
 
      zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, iRandom&0x7fffffff);
58795
 
      if( !zMaster ){
58796
 
        return SQLITE_NOMEM;
58797
 
      }
58798
 
      rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
58799
 
    }while( rc==SQLITE_OK && res );
58800
 
    if( rc==SQLITE_OK ){
58801
 
      /* Open the master journal. */
58802
 
      rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, 
58803
 
          SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
58804
 
          SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
58805
 
      );
58806
 
    }
58807
 
    if( rc!=SQLITE_OK ){
58808
 
      sqlite3DbFree(db, zMaster);
58809
 
      return rc;
58810
 
    }
58811
 
 
58812
 
    /* Write the name of each database file in the transaction into the new
58813
 
    ** master journal file. If an error occurs at this point close
58814
 
    ** and delete the master journal file. All the individual journal files
58815
 
    ** still have 'null' as the master journal pointer, so they will roll
58816
 
    ** back independently if a failure occurs.
58817
 
    */
58818
 
    for(i=0; i<db->nDb; i++){
58819
 
      Btree *pBt = db->aDb[i].pBt;
58820
 
      if( sqlite3BtreeIsInTrans(pBt) ){
58821
 
        char const *zFile = sqlite3BtreeGetJournalname(pBt);
58822
 
        if( zFile==0 ){
58823
 
          continue;  /* Ignore TEMP and :memory: databases */
58824
 
        }
58825
 
        assert( zFile[0]!=0 );
58826
 
        if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
58827
 
          needSync = 1;
58828
 
        }
58829
 
        rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
58830
 
        offset += sqlite3Strlen30(zFile)+1;
58831
 
        if( rc!=SQLITE_OK ){
58832
 
          sqlite3OsCloseFree(pMaster);
58833
 
          sqlite3OsDelete(pVfs, zMaster, 0);
58834
 
          sqlite3DbFree(db, zMaster);
58835
 
          return rc;
58836
 
        }
58837
 
      }
58838
 
    }
58839
 
 
58840
 
    /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
58841
 
    ** flag is set this is not required.
58842
 
    */
58843
 
    if( needSync 
58844
 
     && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
58845
 
     && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
58846
 
    ){
58847
 
      sqlite3OsCloseFree(pMaster);
58848
 
      sqlite3OsDelete(pVfs, zMaster, 0);
58849
 
      sqlite3DbFree(db, zMaster);
58850
 
      return rc;
58851
 
    }
58852
 
 
58853
 
    /* Sync all the db files involved in the transaction. The same call
58854
 
    ** sets the master journal pointer in each individual journal. If
58855
 
    ** an error occurs here, do not delete the master journal file.
58856
 
    **
58857
 
    ** If the error occurs during the first call to
58858
 
    ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
58859
 
    ** master journal file will be orphaned. But we cannot delete it,
58860
 
    ** in case the master journal file name was written into the journal
58861
 
    ** file before the failure occurred.
58862
 
    */
58863
 
    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
58864
 
      Btree *pBt = db->aDb[i].pBt;
58865
 
      if( pBt ){
58866
 
        rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
58867
 
      }
58868
 
    }
58869
 
    sqlite3OsCloseFree(pMaster);
58870
 
    assert( rc!=SQLITE_BUSY );
58871
 
    if( rc!=SQLITE_OK ){
58872
 
      sqlite3DbFree(db, zMaster);
58873
 
      return rc;
58874
 
    }
58875
 
 
58876
 
    /* Delete the master journal file. This commits the transaction. After
58877
 
    ** doing this the directory is synced again before any individual
58878
 
    ** transaction files are deleted.
58879
 
    */
58880
 
    rc = sqlite3OsDelete(pVfs, zMaster, 1);
58881
 
    sqlite3DbFree(db, zMaster);
58882
 
    zMaster = 0;
58883
 
    if( rc ){
58884
 
      return rc;
58885
 
    }
58886
 
 
58887
 
    /* All files and directories have already been synced, so the following
58888
 
    ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
58889
 
    ** deleting or truncating journals. If something goes wrong while
58890
 
    ** this is happening we don't really care. The integrity of the
58891
 
    ** transaction is already guaranteed, but some stray 'cold' journals
58892
 
    ** may be lying around. Returning an error code won't help matters.
58893
 
    */
58894
 
    disable_simulated_io_errors();
58895
 
    sqlite3BeginBenignMalloc();
58896
 
    for(i=0; i<db->nDb; i++){ 
58897
 
      Btree *pBt = db->aDb[i].pBt;
58898
 
      if( pBt ){
58899
 
        sqlite3BtreeCommitPhaseTwo(pBt, 1);
58900
 
      }
58901
 
    }
58902
 
    sqlite3EndBenignMalloc();
58903
 
    enable_simulated_io_errors();
58904
 
 
58905
 
    sqlite3VtabCommit(db);
58906
 
  }
58907
 
#endif
58908
 
 
58909
 
  return rc;
58910
 
}
58911
 
 
58912
 
/* 
58913
 
** This routine checks that the sqlite3.activeVdbeCnt count variable
58914
 
** matches the number of vdbe's in the list sqlite3.pVdbe that are
58915
 
** currently active. An assertion fails if the two counts do not match.
58916
 
** This is an internal self-check only - it is not an essential processing
58917
 
** step.
58918
 
**
58919
 
** This is a no-op if NDEBUG is defined.
58920
 
*/
58921
 
#ifndef NDEBUG
58922
 
static void checkActiveVdbeCnt(sqlite3 *db){
58923
 
  Vdbe *p;
58924
 
  int cnt = 0;
58925
 
  int nWrite = 0;
58926
 
  p = db->pVdbe;
58927
 
  while( p ){
58928
 
    if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
58929
 
      cnt++;
58930
 
      if( p->readOnly==0 ) nWrite++;
58931
 
    }
58932
 
    p = p->pNext;
58933
 
  }
58934
 
  assert( cnt==db->activeVdbeCnt );
58935
 
  assert( nWrite==db->writeVdbeCnt );
58936
 
}
58937
 
#else
58938
 
#define checkActiveVdbeCnt(x)
58939
 
#endif
58940
 
 
58941
 
/*
58942
 
** For every Btree that in database connection db which 
58943
 
** has been modified, "trip" or invalidate each cursor in
58944
 
** that Btree might have been modified so that the cursor
58945
 
** can never be used again.  This happens when a rollback
58946
 
*** occurs.  We have to trip all the other cursors, even
58947
 
** cursor from other VMs in different database connections,
58948
 
** so that none of them try to use the data at which they
58949
 
** were pointing and which now may have been changed due
58950
 
** to the rollback.
58951
 
**
58952
 
** Remember that a rollback can delete tables complete and
58953
 
** reorder rootpages.  So it is not sufficient just to save
58954
 
** the state of the cursor.  We have to invalidate the cursor
58955
 
** so that it is never used again.
58956
 
*/
58957
 
static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
58958
 
  int i;
58959
 
  for(i=0; i<db->nDb; i++){
58960
 
    Btree *p = db->aDb[i].pBt;
58961
 
    if( p && sqlite3BtreeIsInTrans(p) ){
58962
 
      sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
58963
 
    }
58964
 
  }
58965
 
}
58966
 
 
58967
 
/*
58968
 
** If the Vdbe passed as the first argument opened a statement-transaction,
58969
 
** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
58970
 
** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
58971
 
** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the 
58972
 
** statement transaction is commtted.
58973
 
**
58974
 
** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. 
58975
 
** Otherwise SQLITE_OK.
58976
 
*/
58977
 
SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
58978
 
  sqlite3 *const db = p->db;
58979
 
  int rc = SQLITE_OK;
58980
 
 
58981
 
  /* If p->iStatement is greater than zero, then this Vdbe opened a 
58982
 
  ** statement transaction that should be closed here. The only exception
58983
 
  ** is that an IO error may have occured, causing an emergency rollback.
58984
 
  ** In this case (db->nStatement==0), and there is nothing to do.
58985
 
  */
58986
 
  if( db->nStatement && p->iStatement ){
58987
 
    int i;
58988
 
    const int iSavepoint = p->iStatement-1;
58989
 
 
58990
 
    assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
58991
 
    assert( db->nStatement>0 );
58992
 
    assert( p->iStatement==(db->nStatement+db->nSavepoint) );
58993
 
 
58994
 
    for(i=0; i<db->nDb; i++){ 
58995
 
      int rc2 = SQLITE_OK;
58996
 
      Btree *pBt = db->aDb[i].pBt;
58997
 
      if( pBt ){
58998
 
        if( eOp==SAVEPOINT_ROLLBACK ){
58999
 
          rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
59000
 
        }
59001
 
        if( rc2==SQLITE_OK ){
59002
 
          rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
59003
 
        }
59004
 
        if( rc==SQLITE_OK ){
59005
 
          rc = rc2;
59006
 
        }
59007
 
      }
59008
 
    }
59009
 
    db->nStatement--;
59010
 
    p->iStatement = 0;
59011
 
 
59012
 
    /* If the statement transaction is being rolled back, also restore the 
59013
 
    ** database handles deferred constraint counter to the value it had when 
59014
 
    ** the statement transaction was opened.  */
59015
 
    if( eOp==SAVEPOINT_ROLLBACK ){
59016
 
      db->nDeferredCons = p->nStmtDefCons;
59017
 
    }
59018
 
  }
59019
 
  return rc;
59020
 
}
59021
 
 
59022
 
/*
59023
 
** This function is called when a transaction opened by the database 
59024
 
** handle associated with the VM passed as an argument is about to be 
59025
 
** committed. If there are outstanding deferred foreign key constraint
59026
 
** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
59027
 
**
59028
 
** If there are outstanding FK violations and this function returns 
59029
 
** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT and write
59030
 
** an error message to it. Then return SQLITE_ERROR.
59031
 
*/
59032
 
#ifndef SQLITE_OMIT_FOREIGN_KEY
59033
 
SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
59034
 
  sqlite3 *db = p->db;
59035
 
  if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){
59036
 
    p->rc = SQLITE_CONSTRAINT;
59037
 
    p->errorAction = OE_Abort;
59038
 
    sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed");
59039
 
    return SQLITE_ERROR;
59040
 
  }
59041
 
  return SQLITE_OK;
59042
 
}
59043
 
#endif
59044
 
 
59045
 
/*
59046
 
** This routine is called the when a VDBE tries to halt.  If the VDBE
59047
 
** has made changes and is in autocommit mode, then commit those
59048
 
** changes.  If a rollback is needed, then do the rollback.
59049
 
**
59050
 
** This routine is the only way to move the state of a VM from
59051
 
** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
59052
 
** call this on a VM that is in the SQLITE_MAGIC_HALT state.
59053
 
**
59054
 
** Return an error code.  If the commit could not complete because of
59055
 
** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
59056
 
** means the close did not happen and needs to be repeated.
59057
 
*/
59058
 
SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
59059
 
  int rc;                         /* Used to store transient return codes */
59060
 
  sqlite3 *db = p->db;
59061
 
 
59062
 
  /* This function contains the logic that determines if a statement or
59063
 
  ** transaction will be committed or rolled back as a result of the
59064
 
  ** execution of this virtual machine. 
59065
 
  **
59066
 
  ** If any of the following errors occur:
59067
 
  **
59068
 
  **     SQLITE_NOMEM
59069
 
  **     SQLITE_IOERR
59070
 
  **     SQLITE_FULL
59071
 
  **     SQLITE_INTERRUPT
59072
 
  **
59073
 
  ** Then the internal cache might have been left in an inconsistent
59074
 
  ** state.  We need to rollback the statement transaction, if there is
59075
 
  ** one, or the complete transaction if there is no statement transaction.
59076
 
  */
59077
 
 
59078
 
  if( p->db->mallocFailed ){
59079
 
    p->rc = SQLITE_NOMEM;
59080
 
  }
59081
 
  closeAllCursors(p);
59082
 
  if( p->magic!=VDBE_MAGIC_RUN ){
59083
 
    return SQLITE_OK;
59084
 
  }
59085
 
  checkActiveVdbeCnt(db);
59086
 
 
59087
 
  /* No commit or rollback needed if the program never started */
59088
 
  if( p->pc>=0 ){
59089
 
    int mrc;   /* Primary error code from p->rc */
59090
 
    int eStatementOp = 0;
59091
 
    int isSpecialError;            /* Set to true if a 'special' error */
59092
 
 
59093
 
    /* Lock all btrees used by the statement */
59094
 
    sqlite3VdbeEnter(p);
59095
 
 
59096
 
    /* Check for one of the special errors */
59097
 
    mrc = p->rc & 0xff;
59098
 
    assert( p->rc!=SQLITE_IOERR_BLOCKED );  /* This error no longer exists */
59099
 
    isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
59100
 
                     || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
59101
 
    if( isSpecialError ){
59102
 
      /* If the query was read-only and the error code is SQLITE_INTERRUPT, 
59103
 
      ** no rollback is necessary. Otherwise, at least a savepoint 
59104
 
      ** transaction must be rolled back to restore the database to a 
59105
 
      ** consistent state.
59106
 
      **
59107
 
      ** Even if the statement is read-only, it is important to perform
59108
 
      ** a statement or transaction rollback operation. If the error 
59109
 
      ** occured while writing to the journal, sub-journal or database
59110
 
      ** file as part of an effort to free up cache space (see function
59111
 
      ** pagerStress() in pager.c), the rollback is required to restore 
59112
 
      ** the pager to a consistent state.
59113
 
      */
59114
 
      if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
59115
 
        if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
59116
 
          eStatementOp = SAVEPOINT_ROLLBACK;
59117
 
        }else{
59118
 
          /* We are forced to roll back the active transaction. Before doing
59119
 
          ** so, abort any other statements this handle currently has active.
59120
 
          */
59121
 
          invalidateCursorsOnModifiedBtrees(db);
59122
 
          sqlite3RollbackAll(db);
59123
 
          sqlite3CloseSavepoints(db);
59124
 
          db->autoCommit = 1;
59125
 
        }
59126
 
      }
59127
 
    }
59128
 
 
59129
 
    /* Check for immediate foreign key violations. */
59130
 
    if( p->rc==SQLITE_OK ){
59131
 
      sqlite3VdbeCheckFk(p, 0);
59132
 
    }
59133
 
  
59134
 
    /* If the auto-commit flag is set and this is the only active writer 
59135
 
    ** VM, then we do either a commit or rollback of the current transaction. 
59136
 
    **
59137
 
    ** Note: This block also runs if one of the special errors handled 
59138
 
    ** above has occurred. 
59139
 
    */
59140
 
    if( !sqlite3VtabInSync(db) 
59141
 
     && db->autoCommit 
59142
 
     && db->writeVdbeCnt==(p->readOnly==0) 
59143
 
    ){
59144
 
      if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
59145
 
        rc = sqlite3VdbeCheckFk(p, 1);
59146
 
        if( rc!=SQLITE_OK ){
59147
 
          if( NEVER(p->readOnly) ){
59148
 
            sqlite3VdbeLeave(p);
59149
 
            return SQLITE_ERROR;
59150
 
          }
59151
 
          rc = SQLITE_CONSTRAINT;
59152
 
        }else{ 
59153
 
          /* The auto-commit flag is true, the vdbe program was successful 
59154
 
          ** or hit an 'OR FAIL' constraint and there are no deferred foreign
59155
 
          ** key constraints to hold up the transaction. This means a commit 
59156
 
          ** is required. */
59157
 
          rc = vdbeCommit(db, p);
59158
 
        }
59159
 
        if( rc==SQLITE_BUSY && p->readOnly ){
59160
 
          sqlite3VdbeLeave(p);
59161
 
          return SQLITE_BUSY;
59162
 
        }else if( rc!=SQLITE_OK ){
59163
 
          p->rc = rc;
59164
 
          sqlite3RollbackAll(db);
59165
 
        }else{
59166
 
          db->nDeferredCons = 0;
59167
 
          sqlite3CommitInternalChanges(db);
59168
 
        }
59169
 
      }else{
59170
 
        sqlite3RollbackAll(db);
59171
 
      }
59172
 
      db->nStatement = 0;
59173
 
    }else if( eStatementOp==0 ){
59174
 
      if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
59175
 
        eStatementOp = SAVEPOINT_RELEASE;
59176
 
      }else if( p->errorAction==OE_Abort ){
59177
 
        eStatementOp = SAVEPOINT_ROLLBACK;
59178
 
      }else{
59179
 
        invalidateCursorsOnModifiedBtrees(db);
59180
 
        sqlite3RollbackAll(db);
59181
 
        sqlite3CloseSavepoints(db);
59182
 
        db->autoCommit = 1;
59183
 
      }
59184
 
    }
59185
 
  
59186
 
    /* If eStatementOp is non-zero, then a statement transaction needs to
59187
 
    ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
59188
 
    ** do so. If this operation returns an error, and the current statement
59189
 
    ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
59190
 
    ** current statement error code.
59191
 
    **
59192
 
    ** Note that sqlite3VdbeCloseStatement() can only fail if eStatementOp
59193
 
    ** is SAVEPOINT_ROLLBACK.  But if p->rc==SQLITE_OK then eStatementOp
59194
 
    ** must be SAVEPOINT_RELEASE.  Hence the NEVER(p->rc==SQLITE_OK) in 
59195
 
    ** the following code.
59196
 
    */
59197
 
    if( eStatementOp ){
59198
 
      rc = sqlite3VdbeCloseStatement(p, eStatementOp);
59199
 
      if( rc ){
59200
 
        assert( eStatementOp==SAVEPOINT_ROLLBACK );
59201
 
        if( NEVER(p->rc==SQLITE_OK) || p->rc==SQLITE_CONSTRAINT ){
59202
 
          p->rc = rc;
59203
 
          sqlite3DbFree(db, p->zErrMsg);
59204
 
          p->zErrMsg = 0;
59205
 
        }
59206
 
        invalidateCursorsOnModifiedBtrees(db);
59207
 
        sqlite3RollbackAll(db);
59208
 
        sqlite3CloseSavepoints(db);
59209
 
        db->autoCommit = 1;
59210
 
      }
59211
 
    }
59212
 
  
59213
 
    /* If this was an INSERT, UPDATE or DELETE and no statement transaction
59214
 
    ** has been rolled back, update the database connection change-counter. 
59215
 
    */
59216
 
    if( p->changeCntOn ){
59217
 
      if( eStatementOp!=SAVEPOINT_ROLLBACK ){
59218
 
        sqlite3VdbeSetChanges(db, p->nChange);
59219
 
      }else{
59220
 
        sqlite3VdbeSetChanges(db, 0);
59221
 
      }
59222
 
      p->nChange = 0;
59223
 
    }
59224
 
  
59225
 
    /* Rollback or commit any schema changes that occurred. */
59226
 
    if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
59227
 
      sqlite3ResetInternalSchema(db, -1);
59228
 
      db->flags = (db->flags | SQLITE_InternChanges);
59229
 
    }
59230
 
 
59231
 
    /* Release the locks */
59232
 
    sqlite3VdbeLeave(p);
59233
 
  }
59234
 
 
59235
 
  /* We have successfully halted and closed the VM.  Record this fact. */
59236
 
  if( p->pc>=0 ){
59237
 
    db->activeVdbeCnt--;
59238
 
    if( !p->readOnly ){
59239
 
      db->writeVdbeCnt--;
59240
 
    }
59241
 
    assert( db->activeVdbeCnt>=db->writeVdbeCnt );
59242
 
  }
59243
 
  p->magic = VDBE_MAGIC_HALT;
59244
 
  checkActiveVdbeCnt(db);
59245
 
  if( p->db->mallocFailed ){
59246
 
    p->rc = SQLITE_NOMEM;
59247
 
  }
59248
 
 
59249
 
  /* If the auto-commit flag is set to true, then any locks that were held
59250
 
  ** by connection db have now been released. Call sqlite3ConnectionUnlocked() 
59251
 
  ** to invoke any required unlock-notify callbacks.
59252
 
  */
59253
 
  if( db->autoCommit ){
59254
 
    sqlite3ConnectionUnlocked(db);
59255
 
  }
59256
 
 
59257
 
  assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 );
59258
 
  return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
59259
 
}
59260
 
 
59261
 
 
59262
 
/*
59263
 
** Each VDBE holds the result of the most recent sqlite3_step() call
59264
 
** in p->rc.  This routine sets that result back to SQLITE_OK.
59265
 
*/
59266
 
SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
59267
 
  p->rc = SQLITE_OK;
59268
 
}
59269
 
 
59270
 
/*
59271
 
** Clean up a VDBE after execution but do not delete the VDBE just yet.
59272
 
** Write any error messages into *pzErrMsg.  Return the result code.
59273
 
**
59274
 
** After this routine is run, the VDBE should be ready to be executed
59275
 
** again.
59276
 
**
59277
 
** To look at it another way, this routine resets the state of the
59278
 
** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
59279
 
** VDBE_MAGIC_INIT.
59280
 
*/
59281
 
SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
59282
 
  sqlite3 *db;
59283
 
  db = p->db;
59284
 
 
59285
 
  /* If the VM did not run to completion or if it encountered an
59286
 
  ** error, then it might not have been halted properly.  So halt
59287
 
  ** it now.
59288
 
  */
59289
 
  sqlite3VdbeHalt(p);
59290
 
 
59291
 
  /* If the VDBE has be run even partially, then transfer the error code
59292
 
  ** and error message from the VDBE into the main database structure.  But
59293
 
  ** if the VDBE has just been set to run but has not actually executed any
59294
 
  ** instructions yet, leave the main database error information unchanged.
59295
 
  */
59296
 
  if( p->pc>=0 ){
59297
 
    if( p->zErrMsg ){
59298
 
      sqlite3BeginBenignMalloc();
59299
 
      sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT);
59300
 
      sqlite3EndBenignMalloc();
59301
 
      db->errCode = p->rc;
59302
 
      sqlite3DbFree(db, p->zErrMsg);
59303
 
      p->zErrMsg = 0;
59304
 
    }else if( p->rc ){
59305
 
      sqlite3Error(db, p->rc, 0);
59306
 
    }else{
59307
 
      sqlite3Error(db, SQLITE_OK, 0);
59308
 
    }
59309
 
    if( p->runOnlyOnce ) p->expired = 1;
59310
 
  }else if( p->rc && p->expired ){
59311
 
    /* The expired flag was set on the VDBE before the first call
59312
 
    ** to sqlite3_step(). For consistency (since sqlite3_step() was
59313
 
    ** called), set the database error in this case as well.
59314
 
    */
59315
 
    sqlite3Error(db, p->rc, 0);
59316
 
    sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
59317
 
    sqlite3DbFree(db, p->zErrMsg);
59318
 
    p->zErrMsg = 0;
59319
 
  }
59320
 
 
59321
 
  /* Reclaim all memory used by the VDBE
59322
 
  */
59323
 
  Cleanup(p);
59324
 
 
59325
 
  /* Save profiling information from this VDBE run.
59326
 
  */
59327
 
#ifdef VDBE_PROFILE
59328
 
  {
59329
 
    FILE *out = fopen("vdbe_profile.out", "a");
59330
 
    if( out ){
59331
 
      int i;
59332
 
      fprintf(out, "---- ");
59333
 
      for(i=0; i<p->nOp; i++){
59334
 
        fprintf(out, "%02x", p->aOp[i].opcode);
59335
 
      }
59336
 
      fprintf(out, "\n");
59337
 
      for(i=0; i<p->nOp; i++){
59338
 
        fprintf(out, "%6d %10lld %8lld ",
59339
 
           p->aOp[i].cnt,
59340
 
           p->aOp[i].cycles,
59341
 
           p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
59342
 
        );
59343
 
        sqlite3VdbePrintOp(out, i, &p->aOp[i]);
59344
 
      }
59345
 
      fclose(out);
59346
 
    }
59347
 
  }
59348
 
#endif
59349
 
  p->magic = VDBE_MAGIC_INIT;
59350
 
  return p->rc & db->errMask;
59351
 
}
59352
 
 
59353
 
/*
59354
 
** Clean up and delete a VDBE after execution.  Return an integer which is
59355
 
** the result code.  Write any error message text into *pzErrMsg.
59356
 
*/
59357
 
SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
59358
 
  int rc = SQLITE_OK;
59359
 
  if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
59360
 
    rc = sqlite3VdbeReset(p);
59361
 
    assert( (rc & p->db->errMask)==rc );
59362
 
  }
59363
 
  sqlite3VdbeDelete(p);
59364
 
  return rc;
59365
 
}
59366
 
 
59367
 
/*
59368
 
** Call the destructor for each auxdata entry in pVdbeFunc for which
59369
 
** the corresponding bit in mask is clear.  Auxdata entries beyond 31
59370
 
** are always destroyed.  To destroy all auxdata entries, call this
59371
 
** routine with mask==0.
59372
 
*/
59373
 
SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
59374
 
  int i;
59375
 
  for(i=0; i<pVdbeFunc->nAux; i++){
59376
 
    struct AuxData *pAux = &pVdbeFunc->apAux[i];
59377
 
    if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){
59378
 
      if( pAux->xDelete ){
59379
 
        pAux->xDelete(pAux->pAux);
59380
 
      }
59381
 
      pAux->pAux = 0;
59382
 
    }
59383
 
  }
59384
 
}
59385
 
 
59386
 
/*
59387
 
** Free all memory associated with the Vdbe passed as the second argument.
59388
 
** The difference between this function and sqlite3VdbeDelete() is that
59389
 
** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
59390
 
** the database connection.
59391
 
*/
59392
 
SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){
59393
 
  SubProgram *pSub, *pNext;
59394
 
  assert( p->db==0 || p->db==db );
59395
 
  releaseMemArray(p->aVar, p->nVar);
59396
 
  releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
59397
 
  for(pSub=p->pProgram; pSub; pSub=pNext){
59398
 
    pNext = pSub->pNext;
59399
 
    vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
59400
 
    sqlite3DbFree(db, pSub);
59401
 
  }
59402
 
  vdbeFreeOpArray(db, p->aOp, p->nOp);
59403
 
  sqlite3DbFree(db, p->aLabel);
59404
 
  sqlite3DbFree(db, p->aColName);
59405
 
  sqlite3DbFree(db, p->zSql);
59406
 
  sqlite3DbFree(db, p->pFree);
59407
 
  sqlite3DbFree(db, p);
59408
 
}
59409
 
 
59410
 
/*
59411
 
** Delete an entire VDBE.
59412
 
*/
59413
 
SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
59414
 
  sqlite3 *db;
59415
 
 
59416
 
  if( NEVER(p==0) ) return;
59417
 
  db = p->db;
59418
 
  if( p->pPrev ){
59419
 
    p->pPrev->pNext = p->pNext;
59420
 
  }else{
59421
 
    assert( db->pVdbe==p );
59422
 
    db->pVdbe = p->pNext;
59423
 
  }
59424
 
  if( p->pNext ){
59425
 
    p->pNext->pPrev = p->pPrev;
59426
 
  }
59427
 
  p->magic = VDBE_MAGIC_DEAD;
59428
 
  p->db = 0;
59429
 
  sqlite3VdbeDeleteObject(db, p);
59430
 
}
59431
 
 
59432
 
/*
59433
 
** Make sure the cursor p is ready to read or write the row to which it
59434
 
** was last positioned.  Return an error code if an OOM fault or I/O error
59435
 
** prevents us from positioning the cursor to its correct position.
59436
 
**
59437
 
** If a MoveTo operation is pending on the given cursor, then do that
59438
 
** MoveTo now.  If no move is pending, check to see if the row has been
59439
 
** deleted out from under the cursor and if it has, mark the row as
59440
 
** a NULL row.
59441
 
**
59442
 
** If the cursor is already pointing to the correct row and that row has
59443
 
** not been deleted out from under the cursor, then this routine is a no-op.
59444
 
*/
59445
 
SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
59446
 
  if( p->deferredMoveto ){
59447
 
    int res, rc;
59448
 
#ifdef SQLITE_TEST
59449
 
    extern int sqlite3_search_count;
59450
 
#endif
59451
 
    assert( p->isTable );
59452
 
    rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
59453
 
    if( rc ) return rc;
59454
 
    p->lastRowid = p->movetoTarget;
59455
 
    if( res!=0 ) return SQLITE_CORRUPT_BKPT;
59456
 
    p->rowidIsValid = 1;
59457
 
#ifdef SQLITE_TEST
59458
 
    sqlite3_search_count++;
59459
 
#endif
59460
 
    p->deferredMoveto = 0;
59461
 
    p->cacheStatus = CACHE_STALE;
59462
 
  }else if( ALWAYS(p->pCursor) ){
59463
 
    int hasMoved;
59464
 
    int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
59465
 
    if( rc ) return rc;
59466
 
    if( hasMoved ){
59467
 
      p->cacheStatus = CACHE_STALE;
59468
 
      p->nullRow = 1;
59469
 
    }
59470
 
  }
59471
 
  return SQLITE_OK;
59472
 
}
59473
 
 
59474
 
/*
59475
 
** The following functions:
59476
 
**
59477
 
** sqlite3VdbeSerialType()
59478
 
** sqlite3VdbeSerialTypeLen()
59479
 
** sqlite3VdbeSerialLen()
59480
 
** sqlite3VdbeSerialPut()
59481
 
** sqlite3VdbeSerialGet()
59482
 
**
59483
 
** encapsulate the code that serializes values for storage in SQLite
59484
 
** data and index records. Each serialized value consists of a
59485
 
** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
59486
 
** integer, stored as a varint.
59487
 
**
59488
 
** In an SQLite index record, the serial type is stored directly before
59489
 
** the blob of data that it corresponds to. In a table record, all serial
59490
 
** types are stored at the start of the record, and the blobs of data at
59491
 
** the end. Hence these functions allow the caller to handle the
59492
 
** serial-type and data blob seperately.
59493
 
**
59494
 
** The following table describes the various storage classes for data:
59495
 
**
59496
 
**   serial type        bytes of data      type
59497
 
**   --------------     ---------------    ---------------
59498
 
**      0                     0            NULL
59499
 
**      1                     1            signed integer
59500
 
**      2                     2            signed integer
59501
 
**      3                     3            signed integer
59502
 
**      4                     4            signed integer
59503
 
**      5                     6            signed integer
59504
 
**      6                     8            signed integer
59505
 
**      7                     8            IEEE float
59506
 
**      8                     0            Integer constant 0
59507
 
**      9                     0            Integer constant 1
59508
 
**     10,11                               reserved for expansion
59509
 
**    N>=12 and even       (N-12)/2        BLOB
59510
 
**    N>=13 and odd        (N-13)/2        text
59511
 
**
59512
 
** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
59513
 
** of SQLite will not understand those serial types.
59514
 
*/
59515
 
 
59516
 
/*
59517
 
** Return the serial-type for the value stored in pMem.
59518
 
*/
59519
 
SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
59520
 
  int flags = pMem->flags;
59521
 
  int n;
59522
 
 
59523
 
  if( flags&MEM_Null ){
59524
 
    return 0;
59525
 
  }
59526
 
  if( flags&MEM_Int ){
59527
 
    /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
59528
 
#   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
59529
 
    i64 i = pMem->u.i;
59530
 
    u64 u;
59531
 
    if( file_format>=4 && (i&1)==i ){
59532
 
      return 8+(u32)i;
59533
 
    }
59534
 
    if( i<0 ){
59535
 
      if( i<(-MAX_6BYTE) ) return 6;
59536
 
      /* Previous test prevents:  u = -(-9223372036854775808) */
59537
 
      u = -i;
59538
 
    }else{
59539
 
      u = i;
59540
 
    }
59541
 
    if( u<=127 ) return 1;
59542
 
    if( u<=32767 ) return 2;
59543
 
    if( u<=8388607 ) return 3;
59544
 
    if( u<=2147483647 ) return 4;
59545
 
    if( u<=MAX_6BYTE ) return 5;
59546
 
    return 6;
59547
 
  }
59548
 
  if( flags&MEM_Real ){
59549
 
    return 7;
59550
 
  }
59551
 
  assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
59552
 
  n = pMem->n;
59553
 
  if( flags & MEM_Zero ){
59554
 
    n += pMem->u.nZero;
59555
 
  }
59556
 
  assert( n>=0 );
59557
 
  return ((n*2) + 12 + ((flags&MEM_Str)!=0));
59558
 
}
59559
 
 
59560
 
/*
59561
 
** Return the length of the data corresponding to the supplied serial-type.
59562
 
*/
59563
 
SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
59564
 
  if( serial_type>=12 ){
59565
 
    return (serial_type-12)/2;
59566
 
  }else{
59567
 
    static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
59568
 
    return aSize[serial_type];
59569
 
  }
59570
 
}
59571
 
 
59572
 
/*
59573
 
** If we are on an architecture with mixed-endian floating 
59574
 
** points (ex: ARM7) then swap the lower 4 bytes with the 
59575
 
** upper 4 bytes.  Return the result.
59576
 
**
59577
 
** For most architectures, this is a no-op.
59578
 
**
59579
 
** (later):  It is reported to me that the mixed-endian problem
59580
 
** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
59581
 
** that early versions of GCC stored the two words of a 64-bit
59582
 
** float in the wrong order.  And that error has been propagated
59583
 
** ever since.  The blame is not necessarily with GCC, though.
59584
 
** GCC might have just copying the problem from a prior compiler.
59585
 
** I am also told that newer versions of GCC that follow a different
59586
 
** ABI get the byte order right.
59587
 
**
59588
 
** Developers using SQLite on an ARM7 should compile and run their
59589
 
** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
59590
 
** enabled, some asserts below will ensure that the byte order of
59591
 
** floating point values is correct.
59592
 
**
59593
 
** (2007-08-30)  Frank van Vugt has studied this problem closely
59594
 
** and has send his findings to the SQLite developers.  Frank
59595
 
** writes that some Linux kernels offer floating point hardware
59596
 
** emulation that uses only 32-bit mantissas instead of a full 
59597
 
** 48-bits as required by the IEEE standard.  (This is the
59598
 
** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
59599
 
** byte swapping becomes very complicated.  To avoid problems,
59600
 
** the necessary byte swapping is carried out using a 64-bit integer
59601
 
** rather than a 64-bit float.  Frank assures us that the code here
59602
 
** works for him.  We, the developers, have no way to independently
59603
 
** verify this, but Frank seems to know what he is talking about
59604
 
** so we trust him.
59605
 
*/
59606
 
#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
59607
 
static u64 floatSwap(u64 in){
59608
 
  union {
59609
 
    u64 r;
59610
 
    u32 i[2];
59611
 
  } u;
59612
 
  u32 t;
59613
 
 
59614
 
  u.r = in;
59615
 
  t = u.i[0];
59616
 
  u.i[0] = u.i[1];
59617
 
  u.i[1] = t;
59618
 
  return u.r;
59619
 
}
59620
 
# define swapMixedEndianFloat(X)  X = floatSwap(X)
59621
 
#else
59622
 
# define swapMixedEndianFloat(X)
59623
 
#endif
59624
 
 
59625
 
/*
59626
 
** Write the serialized data blob for the value stored in pMem into 
59627
 
** buf. It is assumed that the caller has allocated sufficient space.
59628
 
** Return the number of bytes written.
59629
 
**
59630
 
** nBuf is the amount of space left in buf[].  nBuf must always be
59631
 
** large enough to hold the entire field.  Except, if the field is
59632
 
** a blob with a zero-filled tail, then buf[] might be just the right
59633
 
** size to hold everything except for the zero-filled tail.  If buf[]
59634
 
** is only big enough to hold the non-zero prefix, then only write that
59635
 
** prefix into buf[].  But if buf[] is large enough to hold both the
59636
 
** prefix and the tail then write the prefix and set the tail to all
59637
 
** zeros.
59638
 
**
59639
 
** Return the number of bytes actually written into buf[].  The number
59640
 
** of bytes in the zero-filled tail is included in the return value only
59641
 
** if those bytes were zeroed in buf[].
59642
 
*/ 
59643
 
SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
59644
 
  u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
59645
 
  u32 len;
59646
 
 
59647
 
  /* Integer and Real */
59648
 
  if( serial_type<=7 && serial_type>0 ){
59649
 
    u64 v;
59650
 
    u32 i;
59651
 
    if( serial_type==7 ){
59652
 
      assert( sizeof(v)==sizeof(pMem->r) );
59653
 
      memcpy(&v, &pMem->r, sizeof(v));
59654
 
      swapMixedEndianFloat(v);
59655
 
    }else{
59656
 
      v = pMem->u.i;
59657
 
    }
59658
 
    len = i = sqlite3VdbeSerialTypeLen(serial_type);
59659
 
    assert( len<=(u32)nBuf );
59660
 
    while( i-- ){
59661
 
      buf[i] = (u8)(v&0xFF);
59662
 
      v >>= 8;
59663
 
    }
59664
 
    return len;
59665
 
  }
59666
 
 
59667
 
  /* String or blob */
59668
 
  if( serial_type>=12 ){
59669
 
    assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
59670
 
             == (int)sqlite3VdbeSerialTypeLen(serial_type) );
59671
 
    assert( pMem->n<=nBuf );
59672
 
    len = pMem->n;
59673
 
    memcpy(buf, pMem->z, len);
59674
 
    if( pMem->flags & MEM_Zero ){
59675
 
      len += pMem->u.nZero;
59676
 
      assert( nBuf>=0 );
59677
 
      if( len > (u32)nBuf ){
59678
 
        len = (u32)nBuf;
59679
 
      }
59680
 
      memset(&buf[pMem->n], 0, len-pMem->n);
59681
 
    }
59682
 
    return len;
59683
 
  }
59684
 
 
59685
 
  /* NULL or constants 0 or 1 */
59686
 
  return 0;
59687
 
}
59688
 
 
59689
 
/*
59690
 
** Deserialize the data blob pointed to by buf as serial type serial_type
59691
 
** and store the result in pMem.  Return the number of bytes read.
59692
 
*/ 
59693
 
SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
59694
 
  const unsigned char *buf,     /* Buffer to deserialize from */
59695
 
  u32 serial_type,              /* Serial type to deserialize */
59696
 
  Mem *pMem                     /* Memory cell to write value into */
59697
 
){
59698
 
  switch( serial_type ){
59699
 
    case 10:   /* Reserved for future use */
59700
 
    case 11:   /* Reserved for future use */
59701
 
    case 0: {  /* NULL */
59702
 
      pMem->flags = MEM_Null;
59703
 
      break;
59704
 
    }
59705
 
    case 1: { /* 1-byte signed integer */
59706
 
      pMem->u.i = (signed char)buf[0];
59707
 
      pMem->flags = MEM_Int;
59708
 
      return 1;
59709
 
    }
59710
 
    case 2: { /* 2-byte signed integer */
59711
 
      pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
59712
 
      pMem->flags = MEM_Int;
59713
 
      return 2;
59714
 
    }
59715
 
    case 3: { /* 3-byte signed integer */
59716
 
      pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
59717
 
      pMem->flags = MEM_Int;
59718
 
      return 3;
59719
 
    }
59720
 
    case 4: { /* 4-byte signed integer */
59721
 
      pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
59722
 
      pMem->flags = MEM_Int;
59723
 
      return 4;
59724
 
    }
59725
 
    case 5: { /* 6-byte signed integer */
59726
 
      u64 x = (((signed char)buf[0])<<8) | buf[1];
59727
 
      u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
59728
 
      x = (x<<32) | y;
59729
 
      pMem->u.i = *(i64*)&x;
59730
 
      pMem->flags = MEM_Int;
59731
 
      return 6;
59732
 
    }
59733
 
    case 6:   /* 8-byte signed integer */
59734
 
    case 7: { /* IEEE floating point */
59735
 
      u64 x;
59736
 
      u32 y;
59737
 
#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
59738
 
      /* Verify that integers and floating point values use the same
59739
 
      ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
59740
 
      ** defined that 64-bit floating point values really are mixed
59741
 
      ** endian.
59742
 
      */
59743
 
      static const u64 t1 = ((u64)0x3ff00000)<<32;
59744
 
      static const double r1 = 1.0;
59745
 
      u64 t2 = t1;
59746
 
      swapMixedEndianFloat(t2);
59747
 
      assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
59748
 
#endif
59749
 
 
59750
 
      x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
59751
 
      y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
59752
 
      x = (x<<32) | y;
59753
 
      if( serial_type==6 ){
59754
 
        pMem->u.i = *(i64*)&x;
59755
 
        pMem->flags = MEM_Int;
59756
 
      }else{
59757
 
        assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
59758
 
        swapMixedEndianFloat(x);
59759
 
        memcpy(&pMem->r, &x, sizeof(x));
59760
 
        pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
59761
 
      }
59762
 
      return 8;
59763
 
    }
59764
 
    case 8:    /* Integer 0 */
59765
 
    case 9: {  /* Integer 1 */
59766
 
      pMem->u.i = serial_type-8;
59767
 
      pMem->flags = MEM_Int;
59768
 
      return 0;
59769
 
    }
59770
 
    default: {
59771
 
      u32 len = (serial_type-12)/2;
59772
 
      pMem->z = (char *)buf;
59773
 
      pMem->n = len;
59774
 
      pMem->xDel = 0;
59775
 
      if( serial_type&0x01 ){
59776
 
        pMem->flags = MEM_Str | MEM_Ephem;
59777
 
      }else{
59778
 
        pMem->flags = MEM_Blob | MEM_Ephem;
59779
 
      }
59780
 
      return len;
59781
 
    }
59782
 
  }
59783
 
  return 0;
59784
 
}
59785
 
 
59786
 
 
59787
 
/*
59788
 
** Given the nKey-byte encoding of a record in pKey[], parse the
59789
 
** record into a UnpackedRecord structure.  Return a pointer to
59790
 
** that structure.
59791
 
**
59792
 
** The calling function might provide szSpace bytes of memory
59793
 
** space at pSpace.  This space can be used to hold the returned
59794
 
** VDbeParsedRecord structure if it is large enough.  If it is
59795
 
** not big enough, space is obtained from sqlite3_malloc().
59796
 
**
59797
 
** The returned structure should be closed by a call to
59798
 
** sqlite3VdbeDeleteUnpackedRecord().
59799
 
*/ 
59800
 
SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeRecordUnpack(
59801
 
  KeyInfo *pKeyInfo,     /* Information about the record format */
59802
 
  int nKey,              /* Size of the binary record */
59803
 
  const void *pKey,      /* The binary record */
59804
 
  char *pSpace,          /* Unaligned space available to hold the object */
59805
 
  int szSpace            /* Size of pSpace[] in bytes */
59806
 
){
59807
 
  const unsigned char *aKey = (const unsigned char *)pKey;
59808
 
  UnpackedRecord *p;  /* The unpacked record that we will return */
59809
 
  int nByte;          /* Memory space needed to hold p, in bytes */
59810
 
  int d;
59811
 
  u32 idx;
59812
 
  u16 u;              /* Unsigned loop counter */
59813
 
  u32 szHdr;
59814
 
  Mem *pMem;
59815
 
  int nOff;           /* Increase pSpace by this much to 8-byte align it */
59816
 
  
59817
 
  /*
59818
 
  ** We want to shift the pointer pSpace up such that it is 8-byte aligned.
59819
 
  ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift 
59820
 
  ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
59821
 
  */
59822
 
  nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
59823
 
  pSpace += nOff;
59824
 
  szSpace -= nOff;
59825
 
  nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
59826
 
  if( nByte>szSpace ){
59827
 
    p = sqlite3DbMallocRaw(pKeyInfo->db, nByte);
59828
 
    if( p==0 ) return 0;
59829
 
    p->flags = UNPACKED_NEED_FREE | UNPACKED_NEED_DESTROY;
59830
 
  }else{
59831
 
    p = (UnpackedRecord*)pSpace;
59832
 
    p->flags = UNPACKED_NEED_DESTROY;
59833
 
  }
59834
 
  p->pKeyInfo = pKeyInfo;
59835
 
  p->nField = pKeyInfo->nField + 1;
59836
 
  p->aMem = pMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
59837
 
  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
59838
 
  idx = getVarint32(aKey, szHdr);
59839
 
  d = szHdr;
59840
 
  u = 0;
59841
 
  while( idx<szHdr && u<p->nField && d<=nKey ){
59842
 
    u32 serial_type;
59843
 
 
59844
 
    idx += getVarint32(&aKey[idx], serial_type);
59845
 
    pMem->enc = pKeyInfo->enc;
59846
 
    pMem->db = pKeyInfo->db;
59847
 
    pMem->flags = 0;
59848
 
    pMem->zMalloc = 0;
59849
 
    d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
59850
 
    pMem++;
59851
 
    u++;
59852
 
  }
59853
 
  assert( u<=pKeyInfo->nField + 1 );
59854
 
  p->nField = u;
59855
 
  return (void*)p;
59856
 
}
59857
 
 
59858
 
/*
59859
 
** This routine destroys a UnpackedRecord object.
59860
 
*/
59861
 
SQLITE_PRIVATE void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
59862
 
  int i;
59863
 
  Mem *pMem;
59864
 
 
59865
 
  assert( p!=0 );
59866
 
  assert( p->flags & UNPACKED_NEED_DESTROY );
59867
 
  for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
59868
 
    /* The unpacked record is always constructed by the
59869
 
    ** sqlite3VdbeUnpackRecord() function above, which makes all
59870
 
    ** strings and blobs static.  And none of the elements are
59871
 
    ** ever transformed, so there is never anything to delete.
59872
 
    */
59873
 
    if( NEVER(pMem->zMalloc) ) sqlite3VdbeMemRelease(pMem);
59874
 
  }
59875
 
  if( p->flags & UNPACKED_NEED_FREE ){
59876
 
    sqlite3DbFree(p->pKeyInfo->db, p);
59877
 
  }
59878
 
}
59879
 
 
59880
 
/*
59881
 
** This function compares the two table rows or index records
59882
 
** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
59883
 
** or positive integer if key1 is less than, equal to or 
59884
 
** greater than key2.  The {nKey1, pKey1} key must be a blob
59885
 
** created by th OP_MakeRecord opcode of the VDBE.  The pPKey2
59886
 
** key must be a parsed key such as obtained from
59887
 
** sqlite3VdbeParseRecord.
59888
 
**
59889
 
** Key1 and Key2 do not have to contain the same number of fields.
59890
 
** The key with fewer fields is usually compares less than the 
59891
 
** longer key.  However if the UNPACKED_INCRKEY flags in pPKey2 is set
59892
 
** and the common prefixes are equal, then key1 is less than key2.
59893
 
** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
59894
 
** equal, then the keys are considered to be equal and
59895
 
** the parts beyond the common prefix are ignored.
59896
 
**
59897
 
** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of
59898
 
** the header of pKey1 is ignored.  It is assumed that pKey1 is
59899
 
** an index key, and thus ends with a rowid value.  The last byte
59900
 
** of the header will therefore be the serial type of the rowid:
59901
 
** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types.
59902
 
** The serial type of the final rowid will always be a single byte.
59903
 
** By ignoring this last byte of the header, we force the comparison
59904
 
** to ignore the rowid at the end of key1.
59905
 
*/
59906
 
SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
59907
 
  int nKey1, const void *pKey1, /* Left key */
59908
 
  UnpackedRecord *pPKey2        /* Right key */
59909
 
){
59910
 
  int d1;            /* Offset into aKey[] of next data element */
59911
 
  u32 idx1;          /* Offset into aKey[] of next header element */
59912
 
  u32 szHdr1;        /* Number of bytes in header */
59913
 
  int i = 0;
59914
 
  int nField;
59915
 
  int rc = 0;
59916
 
  const unsigned char *aKey1 = (const unsigned char *)pKey1;
59917
 
  KeyInfo *pKeyInfo;
59918
 
  Mem mem1;
59919
 
 
59920
 
  pKeyInfo = pPKey2->pKeyInfo;
59921
 
  mem1.enc = pKeyInfo->enc;
59922
 
  mem1.db = pKeyInfo->db;
59923
 
  /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
59924
 
  VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
59925
 
 
59926
 
  /* Compilers may complain that mem1.u.i is potentially uninitialized.
59927
 
  ** We could initialize it, as shown here, to silence those complaints.
59928
 
  ** But in fact, mem1.u.i will never actually be used initialized, and doing 
59929
 
  ** the unnecessary initialization has a measurable negative performance
59930
 
  ** impact, since this routine is a very high runner.  And so, we choose
59931
 
  ** to ignore the compiler warnings and leave this variable uninitialized.
59932
 
  */
59933
 
  /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
59934
 
  
59935
 
  idx1 = getVarint32(aKey1, szHdr1);
59936
 
  d1 = szHdr1;
59937
 
  if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){
59938
 
    szHdr1--;
59939
 
  }
59940
 
  nField = pKeyInfo->nField;
59941
 
  while( idx1<szHdr1 && i<pPKey2->nField ){
59942
 
    u32 serial_type1;
59943
 
 
59944
 
    /* Read the serial types for the next element in each key. */
59945
 
    idx1 += getVarint32( aKey1+idx1, serial_type1 );
59946
 
    if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
59947
 
 
59948
 
    /* Extract the values to be compared.
59949
 
    */
59950
 
    d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
59951
 
 
59952
 
    /* Do the comparison
59953
 
    */
59954
 
    rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
59955
 
                           i<nField ? pKeyInfo->aColl[i] : 0);
59956
 
    if( rc!=0 ){
59957
 
      assert( mem1.zMalloc==0 );  /* See comment below */
59958
 
 
59959
 
      /* Invert the result if we are using DESC sort order. */
59960
 
      if( pKeyInfo->aSortOrder && i<nField && pKeyInfo->aSortOrder[i] ){
59961
 
        rc = -rc;
59962
 
      }
59963
 
    
59964
 
      /* If the PREFIX_SEARCH flag is set and all fields except the final
59965
 
      ** rowid field were equal, then clear the PREFIX_SEARCH flag and set 
59966
 
      ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
59967
 
      ** This is used by the OP_IsUnique opcode.
59968
 
      */
59969
 
      if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){
59970
 
        assert( idx1==szHdr1 && rc );
59971
 
        assert( mem1.flags & MEM_Int );
59972
 
        pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH;
59973
 
        pPKey2->rowid = mem1.u.i;
59974
 
      }
59975
 
    
59976
 
      return rc;
59977
 
    }
59978
 
    i++;
59979
 
  }
59980
 
 
59981
 
  /* No memory allocation is ever used on mem1.  Prove this using
59982
 
  ** the following assert().  If the assert() fails, it indicates a
59983
 
  ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
59984
 
  */
59985
 
  assert( mem1.zMalloc==0 );
59986
 
 
59987
 
  /* rc==0 here means that one of the keys ran out of fields and
59988
 
  ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
59989
 
  ** flag is set, then break the tie by treating key2 as larger.
59990
 
  ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
59991
 
  ** are considered to be equal.  Otherwise, the longer key is the 
59992
 
  ** larger.  As it happens, the pPKey2 will always be the longer
59993
 
  ** if there is a difference.
59994
 
  */
59995
 
  assert( rc==0 );
59996
 
  if( pPKey2->flags & UNPACKED_INCRKEY ){
59997
 
    rc = -1;
59998
 
  }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
59999
 
    /* Leave rc==0 */
60000
 
  }else if( idx1<szHdr1 ){
60001
 
    rc = 1;
60002
 
  }
60003
 
  return rc;
60004
 
}
60005
 
 
60006
 
 
60007
 
/*
60008
 
** pCur points at an index entry created using the OP_MakeRecord opcode.
60009
 
** Read the rowid (the last field in the record) and store it in *rowid.
60010
 
** Return SQLITE_OK if everything works, or an error code otherwise.
60011
 
**
60012
 
** pCur might be pointing to text obtained from a corrupt database file.
60013
 
** So the content cannot be trusted.  Do appropriate checks on the content.
60014
 
*/
60015
 
SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
60016
 
  i64 nCellKey = 0;
60017
 
  int rc;
60018
 
  u32 szHdr;        /* Size of the header */
60019
 
  u32 typeRowid;    /* Serial type of the rowid */
60020
 
  u32 lenRowid;     /* Size of the rowid */
60021
 
  Mem m, v;
60022
 
 
60023
 
  UNUSED_PARAMETER(db);
60024
 
 
60025
 
  /* Get the size of the index entry.  Only indices entries of less
60026
 
  ** than 2GiB are support - anything large must be database corruption.
60027
 
  ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
60028
 
  ** this code can safely assume that nCellKey is 32-bits  
60029
 
  */
60030
 
  assert( sqlite3BtreeCursorIsValid(pCur) );
60031
 
  rc = sqlite3BtreeKeySize(pCur, &nCellKey);
60032
 
  assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
60033
 
  assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
60034
 
 
60035
 
  /* Read in the complete content of the index entry */
60036
 
  memset(&m, 0, sizeof(m));
60037
 
  rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
60038
 
  if( rc ){
60039
 
    return rc;
60040
 
  }
60041
 
 
60042
 
  /* The index entry must begin with a header size */
60043
 
  (void)getVarint32((u8*)m.z, szHdr);
60044
 
  testcase( szHdr==3 );
60045
 
  testcase( szHdr==m.n );
60046
 
  if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
60047
 
    goto idx_rowid_corruption;
60048
 
  }
60049
 
 
60050
 
  /* The last field of the index should be an integer - the ROWID.
60051
 
  ** Verify that the last entry really is an integer. */
60052
 
  (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
60053
 
  testcase( typeRowid==1 );
60054
 
  testcase( typeRowid==2 );
60055
 
  testcase( typeRowid==3 );
60056
 
  testcase( typeRowid==4 );
60057
 
  testcase( typeRowid==5 );
60058
 
  testcase( typeRowid==6 );
60059
 
  testcase( typeRowid==8 );
60060
 
  testcase( typeRowid==9 );
60061
 
  if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
60062
 
    goto idx_rowid_corruption;
60063
 
  }
60064
 
  lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
60065
 
  testcase( (u32)m.n==szHdr+lenRowid );
60066
 
  if( unlikely((u32)m.n<szHdr+lenRowid) ){
60067
 
    goto idx_rowid_corruption;
60068
 
  }
60069
 
 
60070
 
  /* Fetch the integer off the end of the index record */
60071
 
  sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
60072
 
  *rowid = v.u.i;
60073
 
  sqlite3VdbeMemRelease(&m);
60074
 
  return SQLITE_OK;
60075
 
 
60076
 
  /* Jump here if database corruption is detected after m has been
60077
 
  ** allocated.  Free the m object and return SQLITE_CORRUPT. */
60078
 
idx_rowid_corruption:
60079
 
  testcase( m.zMalloc!=0 );
60080
 
  sqlite3VdbeMemRelease(&m);
60081
 
  return SQLITE_CORRUPT_BKPT;
60082
 
}
60083
 
 
60084
 
/*
60085
 
** Compare the key of the index entry that cursor pC is pointing to against
60086
 
** the key string in pUnpacked.  Write into *pRes a number
60087
 
** that is negative, zero, or positive if pC is less than, equal to,
60088
 
** or greater than pUnpacked.  Return SQLITE_OK on success.
60089
 
**
60090
 
** pUnpacked is either created without a rowid or is truncated so that it
60091
 
** omits the rowid at the end.  The rowid at the end of the index entry
60092
 
** is ignored as well.  Hence, this routine only compares the prefixes 
60093
 
** of the keys prior to the final rowid, not the entire key.
60094
 
*/
60095
 
SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
60096
 
  VdbeCursor *pC,             /* The cursor to compare against */
60097
 
  UnpackedRecord *pUnpacked,  /* Unpacked version of key to compare against */
60098
 
  int *res                    /* Write the comparison result here */
60099
 
){
60100
 
  i64 nCellKey = 0;
60101
 
  int rc;
60102
 
  BtCursor *pCur = pC->pCursor;
60103
 
  Mem m;
60104
 
 
60105
 
  assert( sqlite3BtreeCursorIsValid(pCur) );
60106
 
  rc = sqlite3BtreeKeySize(pCur, &nCellKey);
60107
 
  assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
60108
 
  /* nCellKey will always be between 0 and 0xffffffff because of the say
60109
 
  ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
60110
 
  if( nCellKey<=0 || nCellKey>0x7fffffff ){
60111
 
    *res = 0;
60112
 
    return SQLITE_CORRUPT_BKPT;
60113
 
  }
60114
 
  memset(&m, 0, sizeof(m));
60115
 
  rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
60116
 
  if( rc ){
60117
 
    return rc;
60118
 
  }
60119
 
  assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID );
60120
 
  *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
60121
 
  sqlite3VdbeMemRelease(&m);
60122
 
  return SQLITE_OK;
60123
 
}
60124
 
 
60125
 
/*
60126
 
** This routine sets the value to be returned by subsequent calls to
60127
 
** sqlite3_changes() on the database handle 'db'. 
60128
 
*/
60129
 
SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
60130
 
  assert( sqlite3_mutex_held(db->mutex) );
60131
 
  db->nChange = nChange;
60132
 
  db->nTotalChange += nChange;
60133
 
}
60134
 
 
60135
 
/*
60136
 
** Set a flag in the vdbe to update the change counter when it is finalised
60137
 
** or reset.
60138
 
*/
60139
 
SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
60140
 
  v->changeCntOn = 1;
60141
 
}
60142
 
 
60143
 
/*
60144
 
** Mark every prepared statement associated with a database connection
60145
 
** as expired.
60146
 
**
60147
 
** An expired statement means that recompilation of the statement is
60148
 
** recommend.  Statements expire when things happen that make their
60149
 
** programs obsolete.  Removing user-defined functions or collating
60150
 
** sequences, or changing an authorization function are the types of
60151
 
** things that make prepared statements obsolete.
60152
 
*/
60153
 
SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){
60154
 
  Vdbe *p;
60155
 
  for(p = db->pVdbe; p; p=p->pNext){
60156
 
    p->expired = 1;
60157
 
  }
60158
 
}
60159
 
 
60160
 
/*
60161
 
** Return the database associated with the Vdbe.
60162
 
*/
60163
 
SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
60164
 
  return v->db;
60165
 
}
60166
 
 
60167
 
/*
60168
 
** Return a pointer to an sqlite3_value structure containing the value bound
60169
 
** parameter iVar of VM v. Except, if the value is an SQL NULL, return 
60170
 
** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
60171
 
** constants) to the value before returning it.
60172
 
**
60173
 
** The returned value must be freed by the caller using sqlite3ValueFree().
60174
 
*/
60175
 
SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe *v, int iVar, u8 aff){
60176
 
  assert( iVar>0 );
60177
 
  if( v ){
60178
 
    Mem *pMem = &v->aVar[iVar-1];
60179
 
    if( 0==(pMem->flags & MEM_Null) ){
60180
 
      sqlite3_value *pRet = sqlite3ValueNew(v->db);
60181
 
      if( pRet ){
60182
 
        sqlite3VdbeMemCopy((Mem *)pRet, pMem);
60183
 
        sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
60184
 
        sqlite3VdbeMemStoreType((Mem *)pRet);
60185
 
      }
60186
 
      return pRet;
60187
 
    }
60188
 
  }
60189
 
  return 0;
60190
 
}
60191
 
 
60192
 
/*
60193
 
** Configure SQL variable iVar so that binding a new value to it signals
60194
 
** to sqlite3_reoptimize() that re-preparing the statement may result
60195
 
** in a better query plan.
60196
 
*/
60197
 
SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
60198
 
  assert( iVar>0 );
60199
 
  if( iVar>32 ){
60200
 
    v->expmask = 0xffffffff;
60201
 
  }else{
60202
 
    v->expmask |= ((u32)1 << (iVar-1));
60203
 
  }
60204
 
}
60205
 
 
60206
 
/************** End of vdbeaux.c *********************************************/
60207
 
/************** Begin file vdbeapi.c *****************************************/
60208
 
/*
60209
 
** 2004 May 26
60210
 
**
60211
 
** The author disclaims copyright to this source code.  In place of
60212
 
** a legal notice, here is a blessing:
60213
 
**
60214
 
**    May you do good and not evil.
60215
 
**    May you find forgiveness for yourself and forgive others.
60216
 
**    May you share freely, never taking more than you give.
60217
 
**
60218
 
*************************************************************************
60219
 
**
60220
 
** This file contains code use to implement APIs that are part of the
60221
 
** VDBE.
60222
 
*/
60223
 
 
60224
 
#ifndef SQLITE_OMIT_DEPRECATED
60225
 
/*
60226
 
** Return TRUE (non-zero) of the statement supplied as an argument needs
60227
 
** to be recompiled.  A statement needs to be recompiled whenever the
60228
 
** execution environment changes in a way that would alter the program
60229
 
** that sqlite3_prepare() generates.  For example, if new functions or
60230
 
** collating sequences are registered or if an authorizer function is
60231
 
** added or changed.
60232
 
*/
60233
 
SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
60234
 
  Vdbe *p = (Vdbe*)pStmt;
60235
 
  return p==0 || p->expired;
60236
 
}
60237
 
#endif
60238
 
 
60239
 
/*
60240
 
** Check on a Vdbe to make sure it has not been finalized.  Log
60241
 
** an error and return true if it has been finalized (or is otherwise
60242
 
** invalid).  Return false if it is ok.
60243
 
*/
60244
 
static int vdbeSafety(Vdbe *p){
60245
 
  if( p->db==0 ){
60246
 
    sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
60247
 
    return 1;
60248
 
  }else{
60249
 
    return 0;
60250
 
  }
60251
 
}
60252
 
static int vdbeSafetyNotNull(Vdbe *p){
60253
 
  if( p==0 ){
60254
 
    sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
60255
 
    return 1;
60256
 
  }else{
60257
 
    return vdbeSafety(p);
60258
 
  }
60259
 
}
60260
 
 
60261
 
/*
60262
 
** The following routine destroys a virtual machine that is created by
60263
 
** the sqlite3_compile() routine. The integer returned is an SQLITE_
60264
 
** success/failure code that describes the result of executing the virtual
60265
 
** machine.
60266
 
**
60267
 
** This routine sets the error code and string returned by
60268
 
** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
60269
 
*/
60270
 
SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
60271
 
  int rc;
60272
 
  if( pStmt==0 ){
60273
 
    /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
60274
 
    ** pointer is a harmless no-op. */
60275
 
    rc = SQLITE_OK;
60276
 
  }else{
60277
 
    Vdbe *v = (Vdbe*)pStmt;
60278
 
    sqlite3 *db = v->db;
60279
 
#if SQLITE_THREADSAFE
60280
 
    sqlite3_mutex *mutex;
60281
 
#endif
60282
 
    if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
60283
 
#if SQLITE_THREADSAFE
60284
 
    mutex = v->db->mutex;
60285
 
#endif
60286
 
    sqlite3_mutex_enter(mutex);
60287
 
    rc = sqlite3VdbeFinalize(v);
60288
 
    rc = sqlite3ApiExit(db, rc);
60289
 
    sqlite3_mutex_leave(mutex);
60290
 
  }
60291
 
  return rc;
60292
 
}
60293
 
 
60294
 
/*
60295
 
** Terminate the current execution of an SQL statement and reset it
60296
 
** back to its starting state so that it can be reused. A success code from
60297
 
** the prior execution is returned.
60298
 
**
60299
 
** This routine sets the error code and string returned by
60300
 
** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
60301
 
*/
60302
 
SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
60303
 
  int rc;
60304
 
  if( pStmt==0 ){
60305
 
    rc = SQLITE_OK;
60306
 
  }else{
60307
 
    Vdbe *v = (Vdbe*)pStmt;
60308
 
    sqlite3_mutex_enter(v->db->mutex);
60309
 
    rc = sqlite3VdbeReset(v);
60310
 
    sqlite3VdbeMakeReady(v, -1, 0, 0, 0, 0, 0);
60311
 
    assert( (rc & (v->db->errMask))==rc );
60312
 
    rc = sqlite3ApiExit(v->db, rc);
60313
 
    sqlite3_mutex_leave(v->db->mutex);
60314
 
  }
60315
 
  return rc;
60316
 
}
60317
 
 
60318
 
/*
60319
 
** Set all the parameters in the compiled SQL statement to NULL.
60320
 
*/
60321
 
SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
60322
 
  int i;
60323
 
  int rc = SQLITE_OK;
60324
 
  Vdbe *p = (Vdbe*)pStmt;
60325
 
#if SQLITE_THREADSAFE
60326
 
  sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
60327
 
#endif
60328
 
  sqlite3_mutex_enter(mutex);
60329
 
  for(i=0; i<p->nVar; i++){
60330
 
    sqlite3VdbeMemRelease(&p->aVar[i]);
60331
 
    p->aVar[i].flags = MEM_Null;
60332
 
  }
60333
 
  if( p->isPrepareV2 && p->expmask ){
60334
 
    p->expired = 1;
60335
 
  }
60336
 
  sqlite3_mutex_leave(mutex);
60337
 
  return rc;
60338
 
}
60339
 
 
60340
 
 
60341
 
/**************************** sqlite3_value_  *******************************
60342
 
** The following routines extract information from a Mem or sqlite3_value
60343
 
** structure.
60344
 
*/
60345
 
SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
60346
 
  Mem *p = (Mem*)pVal;
60347
 
  if( p->flags & (MEM_Blob|MEM_Str) ){
60348
 
    sqlite3VdbeMemExpandBlob(p);
60349
 
    p->flags &= ~MEM_Str;
60350
 
    p->flags |= MEM_Blob;
60351
 
    return p->n ? p->z : 0;
60352
 
  }else{
60353
 
    return sqlite3_value_text(pVal);
60354
 
  }
60355
 
}
60356
 
SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
60357
 
  return sqlite3ValueBytes(pVal, SQLITE_UTF8);
60358
 
}
60359
 
SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
60360
 
  return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
60361
 
}
60362
 
SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
60363
 
  return sqlite3VdbeRealValue((Mem*)pVal);
60364
 
}
60365
 
SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
60366
 
  return (int)sqlite3VdbeIntValue((Mem*)pVal);
60367
 
}
60368
 
SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
60369
 
  return sqlite3VdbeIntValue((Mem*)pVal);
60370
 
}
60371
 
SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
60372
 
  return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
60373
 
}
60374
 
#ifndef SQLITE_OMIT_UTF16
60375
 
SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
60376
 
  return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
60377
 
}
60378
 
SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
60379
 
  return sqlite3ValueText(pVal, SQLITE_UTF16BE);
60380
 
}
60381
 
SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
60382
 
  return sqlite3ValueText(pVal, SQLITE_UTF16LE);
60383
 
}
60384
 
#endif /* SQLITE_OMIT_UTF16 */
60385
 
SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
60386
 
  return pVal->type;
60387
 
}
60388
 
 
60389
 
/**************************** sqlite3_result_  *******************************
60390
 
** The following routines are used by user-defined functions to specify
60391
 
** the function result.
60392
 
**
60393
 
** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
60394
 
** result as a string or blob but if the string or blob is too large, it
60395
 
** then sets the error code to SQLITE_TOOBIG
60396
 
*/
60397
 
static void setResultStrOrError(
60398
 
  sqlite3_context *pCtx,  /* Function context */
60399
 
  const char *z,          /* String pointer */
60400
 
  int n,                  /* Bytes in string, or negative */
60401
 
  u8 enc,                 /* Encoding of z.  0 for BLOBs */
60402
 
  void (*xDel)(void*)     /* Destructor function */
60403
 
){
60404
 
  if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
60405
 
    sqlite3_result_error_toobig(pCtx);
60406
 
  }
60407
 
}
60408
 
SQLITE_API void sqlite3_result_blob(
60409
 
  sqlite3_context *pCtx, 
60410
 
  const void *z, 
60411
 
  int n, 
60412
 
  void (*xDel)(void *)
60413
 
){
60414
 
  assert( n>=0 );
60415
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60416
 
  setResultStrOrError(pCtx, z, n, 0, xDel);
60417
 
}
60418
 
SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
60419
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60420
 
  sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
60421
 
}
60422
 
SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
60423
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60424
 
  pCtx->isError = SQLITE_ERROR;
60425
 
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
60426
 
}
60427
 
#ifndef SQLITE_OMIT_UTF16
60428
 
SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
60429
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60430
 
  pCtx->isError = SQLITE_ERROR;
60431
 
  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
60432
 
}
60433
 
#endif
60434
 
SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
60435
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60436
 
  sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
60437
 
}
60438
 
SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
60439
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60440
 
  sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
60441
 
}
60442
 
SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
60443
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60444
 
  sqlite3VdbeMemSetNull(&pCtx->s);
60445
 
}
60446
 
SQLITE_API void sqlite3_result_text(
60447
 
  sqlite3_context *pCtx, 
60448
 
  const char *z, 
60449
 
  int n,
60450
 
  void (*xDel)(void *)
60451
 
){
60452
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60453
 
  setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
60454
 
}
60455
 
#ifndef SQLITE_OMIT_UTF16
60456
 
SQLITE_API void sqlite3_result_text16(
60457
 
  sqlite3_context *pCtx, 
60458
 
  const void *z, 
60459
 
  int n, 
60460
 
  void (*xDel)(void *)
60461
 
){
60462
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60463
 
  setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
60464
 
}
60465
 
SQLITE_API void sqlite3_result_text16be(
60466
 
  sqlite3_context *pCtx, 
60467
 
  const void *z, 
60468
 
  int n, 
60469
 
  void (*xDel)(void *)
60470
 
){
60471
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60472
 
  setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
60473
 
}
60474
 
SQLITE_API void sqlite3_result_text16le(
60475
 
  sqlite3_context *pCtx, 
60476
 
  const void *z, 
60477
 
  int n, 
60478
 
  void (*xDel)(void *)
60479
 
){
60480
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60481
 
  setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
60482
 
}
60483
 
#endif /* SQLITE_OMIT_UTF16 */
60484
 
SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
60485
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60486
 
  sqlite3VdbeMemCopy(&pCtx->s, pValue);
60487
 
}
60488
 
SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
60489
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60490
 
  sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
60491
 
}
60492
 
SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
60493
 
  pCtx->isError = errCode;
60494
 
  if( pCtx->s.flags & MEM_Null ){
60495
 
    sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1, 
60496
 
                         SQLITE_UTF8, SQLITE_STATIC);
60497
 
  }
60498
 
}
60499
 
 
60500
 
/* Force an SQLITE_TOOBIG error. */
60501
 
SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
60502
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60503
 
  pCtx->isError = SQLITE_TOOBIG;
60504
 
  sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, 
60505
 
                       SQLITE_UTF8, SQLITE_STATIC);
60506
 
}
60507
 
 
60508
 
/* An SQLITE_NOMEM error. */
60509
 
SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
60510
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60511
 
  sqlite3VdbeMemSetNull(&pCtx->s);
60512
 
  pCtx->isError = SQLITE_NOMEM;
60513
 
  pCtx->s.db->mallocFailed = 1;
60514
 
}
60515
 
 
60516
 
/*
60517
 
** This function is called after a transaction has been committed. It 
60518
 
** invokes callbacks registered with sqlite3_wal_hook() as required.
60519
 
*/
60520
 
static int doWalCallbacks(sqlite3 *db){
60521
 
  int rc = SQLITE_OK;
60522
 
#ifndef SQLITE_OMIT_WAL
60523
 
  int i;
60524
 
  for(i=0; i<db->nDb; i++){
60525
 
    Btree *pBt = db->aDb[i].pBt;
60526
 
    if( pBt ){
60527
 
      int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
60528
 
      if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
60529
 
        rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
60530
 
      }
60531
 
    }
60532
 
  }
60533
 
#endif
60534
 
  return rc;
60535
 
}
60536
 
 
60537
 
/*
60538
 
** Execute the statement pStmt, either until a row of data is ready, the
60539
 
** statement is completely executed or an error occurs.
60540
 
**
60541
 
** This routine implements the bulk of the logic behind the sqlite_step()
60542
 
** API.  The only thing omitted is the automatic recompile if a 
60543
 
** schema change has occurred.  That detail is handled by the
60544
 
** outer sqlite3_step() wrapper procedure.
60545
 
*/
60546
 
static int sqlite3Step(Vdbe *p){
60547
 
  sqlite3 *db;
60548
 
  int rc;
60549
 
 
60550
 
  assert(p);
60551
 
  if( p->magic!=VDBE_MAGIC_RUN ){
60552
 
    /* We used to require that sqlite3_reset() be called before retrying
60553
 
    ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
60554
 
    ** with version 3.7.0, we changed this so that sqlite3_reset() would
60555
 
    ** be called automatically instead of throwing the SQLITE_MISUSE error.
60556
 
    ** This "automatic-reset" change is not technically an incompatibility, 
60557
 
    ** since any application that receives an SQLITE_MISUSE is broken by
60558
 
    ** definition.
60559
 
    **
60560
 
    ** Nevertheless, some published applications that were originally written
60561
 
    ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 
60562
 
    ** returns, and the so were broken by the automatic-reset change.  As a
60563
 
    ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
60564
 
    ** legacy behavior of returning SQLITE_MISUSE for cases where the 
60565
 
    ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
60566
 
    ** or SQLITE_BUSY error.
60567
 
    */
60568
 
#ifdef SQLITE_OMIT_AUTORESET
60569
 
    if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
60570
 
      sqlite3_reset((sqlite3_stmt*)p);
60571
 
    }else{
60572
 
      return SQLITE_MISUSE_BKPT;
60573
 
    }
60574
 
#else
60575
 
    sqlite3_reset((sqlite3_stmt*)p);
60576
 
#endif
60577
 
  }
60578
 
 
60579
 
  /* Check that malloc() has not failed. If it has, return early. */
60580
 
  db = p->db;
60581
 
  if( db->mallocFailed ){
60582
 
    p->rc = SQLITE_NOMEM;
60583
 
    return SQLITE_NOMEM;
60584
 
  }
60585
 
 
60586
 
  if( p->pc<=0 && p->expired ){
60587
 
    p->rc = SQLITE_SCHEMA;
60588
 
    rc = SQLITE_ERROR;
60589
 
    goto end_of_step;
60590
 
  }
60591
 
  if( p->pc<0 ){
60592
 
    /* If there are no other statements currently running, then
60593
 
    ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
60594
 
    ** from interrupting a statement that has not yet started.
60595
 
    */
60596
 
    if( db->activeVdbeCnt==0 ){
60597
 
      db->u1.isInterrupted = 0;
60598
 
    }
60599
 
 
60600
 
    assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 );
60601
 
 
60602
 
#ifndef SQLITE_OMIT_TRACE
60603
 
    if( db->xProfile && !db->init.busy ){
60604
 
      sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
60605
 
    }
60606
 
#endif
60607
 
 
60608
 
    db->activeVdbeCnt++;
60609
 
    if( p->readOnly==0 ) db->writeVdbeCnt++;
60610
 
    p->pc = 0;
60611
 
  }
60612
 
#ifndef SQLITE_OMIT_EXPLAIN
60613
 
  if( p->explain ){
60614
 
    rc = sqlite3VdbeList(p);
60615
 
  }else
60616
 
#endif /* SQLITE_OMIT_EXPLAIN */
60617
 
  {
60618
 
    db->vdbeExecCnt++;
60619
 
    rc = sqlite3VdbeExec(p);
60620
 
    db->vdbeExecCnt--;
60621
 
  }
60622
 
 
60623
 
#ifndef SQLITE_OMIT_TRACE
60624
 
  /* Invoke the profile callback if there is one
60625
 
  */
60626
 
  if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
60627
 
    sqlite3_int64 iNow;
60628
 
    sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
60629
 
    db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
60630
 
  }
60631
 
#endif
60632
 
 
60633
 
  if( rc==SQLITE_DONE ){
60634
 
    assert( p->rc==SQLITE_OK );
60635
 
    p->rc = doWalCallbacks(db);
60636
 
    if( p->rc!=SQLITE_OK ){
60637
 
      rc = SQLITE_ERROR;
60638
 
    }
60639
 
  }
60640
 
 
60641
 
  db->errCode = rc;
60642
 
  if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
60643
 
    p->rc = SQLITE_NOMEM;
60644
 
  }
60645
 
end_of_step:
60646
 
  /* At this point local variable rc holds the value that should be 
60647
 
  ** returned if this statement was compiled using the legacy 
60648
 
  ** sqlite3_prepare() interface. According to the docs, this can only
60649
 
  ** be one of the values in the first assert() below. Variable p->rc 
60650
 
  ** contains the value that would be returned if sqlite3_finalize() 
60651
 
  ** were called on statement p.
60652
 
  */
60653
 
  assert( rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR 
60654
 
       || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
60655
 
  );
60656
 
  assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
60657
 
  if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
60658
 
    /* If this statement was prepared using sqlite3_prepare_v2(), and an
60659
 
    ** error has occured, then return the error code in p->rc to the
60660
 
    ** caller. Set the error code in the database handle to the same value.
60661
 
    */ 
60662
 
    rc = db->errCode = p->rc;
60663
 
  }
60664
 
  return (rc&db->errMask);
60665
 
}
60666
 
 
60667
 
/*
60668
 
** This is the top-level implementation of sqlite3_step().  Call
60669
 
** sqlite3Step() to do most of the work.  If a schema error occurs,
60670
 
** call sqlite3Reprepare() and try again.
60671
 
*/
60672
 
SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
60673
 
  int rc = SQLITE_OK;      /* Result from sqlite3Step() */
60674
 
  int rc2 = SQLITE_OK;     /* Result from sqlite3Reprepare() */
60675
 
  Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
60676
 
  int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
60677
 
  sqlite3 *db;             /* The database connection */
60678
 
 
60679
 
  if( vdbeSafetyNotNull(v) ){
60680
 
    return SQLITE_MISUSE_BKPT;
60681
 
  }
60682
 
  db = v->db;
60683
 
  sqlite3_mutex_enter(db->mutex);
60684
 
  while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
60685
 
         && cnt++ < 5
60686
 
         && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
60687
 
    sqlite3_reset(pStmt);
60688
 
    v->expired = 0;
60689
 
  }
60690
 
  if( rc2!=SQLITE_OK && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
60691
 
    /* This case occurs after failing to recompile an sql statement. 
60692
 
    ** The error message from the SQL compiler has already been loaded 
60693
 
    ** into the database handle. This block copies the error message 
60694
 
    ** from the database handle into the statement and sets the statement
60695
 
    ** program counter to 0 to ensure that when the statement is 
60696
 
    ** finalized or reset the parser error message is available via
60697
 
    ** sqlite3_errmsg() and sqlite3_errcode().
60698
 
    */
60699
 
    const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
60700
 
    sqlite3DbFree(db, v->zErrMsg);
60701
 
    if( !db->mallocFailed ){
60702
 
      v->zErrMsg = sqlite3DbStrDup(db, zErr);
60703
 
      v->rc = rc2;
60704
 
    } else {
60705
 
      v->zErrMsg = 0;
60706
 
      v->rc = rc = SQLITE_NOMEM;
60707
 
    }
60708
 
  }
60709
 
  rc = sqlite3ApiExit(db, rc);
60710
 
  sqlite3_mutex_leave(db->mutex);
60711
 
  return rc;
60712
 
}
60713
 
 
60714
 
/*
60715
 
** Extract the user data from a sqlite3_context structure and return a
60716
 
** pointer to it.
60717
 
*/
60718
 
SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
60719
 
  assert( p && p->pFunc );
60720
 
  return p->pFunc->pUserData;
60721
 
}
60722
 
 
60723
 
/*
60724
 
** Extract the user data from a sqlite3_context structure and return a
60725
 
** pointer to it.
60726
 
**
60727
 
** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
60728
 
** returns a copy of the pointer to the database connection (the 1st
60729
 
** parameter) of the sqlite3_create_function() and
60730
 
** sqlite3_create_function16() routines that originally registered the
60731
 
** application defined function.
60732
 
*/
60733
 
SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
60734
 
  assert( p && p->pFunc );
60735
 
  return p->s.db;
60736
 
}
60737
 
 
60738
 
/*
60739
 
** The following is the implementation of an SQL function that always
60740
 
** fails with an error message stating that the function is used in the
60741
 
** wrong context.  The sqlite3_overload_function() API might construct
60742
 
** SQL function that use this routine so that the functions will exist
60743
 
** for name resolution but are actually overloaded by the xFindFunction
60744
 
** method of virtual tables.
60745
 
*/
60746
 
SQLITE_PRIVATE void sqlite3InvalidFunction(
60747
 
  sqlite3_context *context,  /* The function calling context */
60748
 
  int NotUsed,               /* Number of arguments to the function */
60749
 
  sqlite3_value **NotUsed2   /* Value of each argument */
60750
 
){
60751
 
  const char *zName = context->pFunc->zName;
60752
 
  char *zErr;
60753
 
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
60754
 
  zErr = sqlite3_mprintf(
60755
 
      "unable to use function %s in the requested context", zName);
60756
 
  sqlite3_result_error(context, zErr, -1);
60757
 
  sqlite3_free(zErr);
60758
 
}
60759
 
 
60760
 
/*
60761
 
** Allocate or return the aggregate context for a user function.  A new
60762
 
** context is allocated on the first call.  Subsequent calls return the
60763
 
** same context that was returned on prior calls.
60764
 
*/
60765
 
SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
60766
 
  Mem *pMem;
60767
 
  assert( p && p->pFunc && p->pFunc->xStep );
60768
 
  assert( sqlite3_mutex_held(p->s.db->mutex) );
60769
 
  pMem = p->pMem;
60770
 
  testcase( nByte<0 );
60771
 
  if( (pMem->flags & MEM_Agg)==0 ){
60772
 
    if( nByte<=0 ){
60773
 
      sqlite3VdbeMemReleaseExternal(pMem);
60774
 
      pMem->flags = MEM_Null;
60775
 
      pMem->z = 0;
60776
 
    }else{
60777
 
      sqlite3VdbeMemGrow(pMem, nByte, 0);
60778
 
      pMem->flags = MEM_Agg;
60779
 
      pMem->u.pDef = p->pFunc;
60780
 
      if( pMem->z ){
60781
 
        memset(pMem->z, 0, nByte);
60782
 
      }
60783
 
    }
60784
 
  }
60785
 
  return (void*)pMem->z;
60786
 
}
60787
 
 
60788
 
/*
60789
 
** Return the auxilary data pointer, if any, for the iArg'th argument to
60790
 
** the user-function defined by pCtx.
60791
 
*/
60792
 
SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
60793
 
  VdbeFunc *pVdbeFunc;
60794
 
 
60795
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60796
 
  pVdbeFunc = pCtx->pVdbeFunc;
60797
 
  if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
60798
 
    return 0;
60799
 
  }
60800
 
  return pVdbeFunc->apAux[iArg].pAux;
60801
 
}
60802
 
 
60803
 
/*
60804
 
** Set the auxilary data pointer and delete function, for the iArg'th
60805
 
** argument to the user-function defined by pCtx. Any previous value is
60806
 
** deleted by calling the delete function specified when it was set.
60807
 
*/
60808
 
SQLITE_API void sqlite3_set_auxdata(
60809
 
  sqlite3_context *pCtx, 
60810
 
  int iArg, 
60811
 
  void *pAux, 
60812
 
  void (*xDelete)(void*)
60813
 
){
60814
 
  struct AuxData *pAuxData;
60815
 
  VdbeFunc *pVdbeFunc;
60816
 
  if( iArg<0 ) goto failed;
60817
 
 
60818
 
  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
60819
 
  pVdbeFunc = pCtx->pVdbeFunc;
60820
 
  if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
60821
 
    int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
60822
 
    int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
60823
 
    pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
60824
 
    if( !pVdbeFunc ){
60825
 
      goto failed;
60826
 
    }
60827
 
    pCtx->pVdbeFunc = pVdbeFunc;
60828
 
    memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
60829
 
    pVdbeFunc->nAux = iArg+1;
60830
 
    pVdbeFunc->pFunc = pCtx->pFunc;
60831
 
  }
60832
 
 
60833
 
  pAuxData = &pVdbeFunc->apAux[iArg];
60834
 
  if( pAuxData->pAux && pAuxData->xDelete ){
60835
 
    pAuxData->xDelete(pAuxData->pAux);
60836
 
  }
60837
 
  pAuxData->pAux = pAux;
60838
 
  pAuxData->xDelete = xDelete;
60839
 
  return;
60840
 
 
60841
 
failed:
60842
 
  if( xDelete ){
60843
 
    xDelete(pAux);
60844
 
  }
60845
 
}
60846
 
 
60847
 
#ifndef SQLITE_OMIT_DEPRECATED
60848
 
/*
60849
 
** Return the number of times the Step function of a aggregate has been 
60850
 
** called.
60851
 
**
60852
 
** This function is deprecated.  Do not use it for new code.  It is
60853
 
** provide only to avoid breaking legacy code.  New aggregate function
60854
 
** implementations should keep their own counts within their aggregate
60855
 
** context.
60856
 
*/
60857
 
SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
60858
 
  assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
60859
 
  return p->pMem->n;
60860
 
}
60861
 
#endif
60862
 
 
60863
 
/*
60864
 
** Return the number of columns in the result set for the statement pStmt.
60865
 
*/
60866
 
SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
60867
 
  Vdbe *pVm = (Vdbe *)pStmt;
60868
 
  return pVm ? pVm->nResColumn : 0;
60869
 
}
60870
 
 
60871
 
/*
60872
 
** Return the number of values available from the current row of the
60873
 
** currently executing statement pStmt.
60874
 
*/
60875
 
SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
60876
 
  Vdbe *pVm = (Vdbe *)pStmt;
60877
 
  if( pVm==0 || pVm->pResultSet==0 ) return 0;
60878
 
  return pVm->nResColumn;
60879
 
}
60880
 
 
60881
 
 
60882
 
/*
60883
 
** Check to see if column iCol of the given statement is valid.  If
60884
 
** it is, return a pointer to the Mem for the value of that column.
60885
 
** If iCol is not valid, return a pointer to a Mem which has a value
60886
 
** of NULL.
60887
 
*/
60888
 
static Mem *columnMem(sqlite3_stmt *pStmt, int i){
60889
 
  Vdbe *pVm;
60890
 
  Mem *pOut;
60891
 
 
60892
 
  pVm = (Vdbe *)pStmt;
60893
 
  if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
60894
 
    sqlite3_mutex_enter(pVm->db->mutex);
60895
 
    pOut = &pVm->pResultSet[i];
60896
 
  }else{
60897
 
    /* If the value passed as the second argument is out of range, return
60898
 
    ** a pointer to the following static Mem object which contains the
60899
 
    ** value SQL NULL. Even though the Mem structure contains an element
60900
 
    ** of type i64, on certain architecture (x86) with certain compiler
60901
 
    ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
60902
 
    ** instead of an 8-byte one. This all works fine, except that when
60903
 
    ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
60904
 
    ** that a Mem structure is located on an 8-byte boundary. To prevent
60905
 
    ** this assert() from failing, when building with SQLITE_DEBUG defined
60906
 
    ** using gcc, force nullMem to be 8-byte aligned using the magical
60907
 
    ** __attribute__((aligned(8))) macro.  */
60908
 
    static const Mem nullMem 
60909
 
#if defined(SQLITE_DEBUG) && defined(__GNUC__)
60910
 
      __attribute__((aligned(8))) 
60911
 
#endif
60912
 
      = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0,
60913
 
#ifdef SQLITE_DEBUG
60914
 
         0, 0,  /* pScopyFrom, pFiller */
60915
 
#endif
60916
 
         0, 0 };
60917
 
 
60918
 
    if( pVm && ALWAYS(pVm->db) ){
60919
 
      sqlite3_mutex_enter(pVm->db->mutex);
60920
 
      sqlite3Error(pVm->db, SQLITE_RANGE, 0);
60921
 
    }
60922
 
    pOut = (Mem*)&nullMem;
60923
 
  }
60924
 
  return pOut;
60925
 
}
60926
 
 
60927
 
/*
60928
 
** This function is called after invoking an sqlite3_value_XXX function on a 
60929
 
** column value (i.e. a value returned by evaluating an SQL expression in the
60930
 
** select list of a SELECT statement) that may cause a malloc() failure. If 
60931
 
** malloc() has failed, the threads mallocFailed flag is cleared and the result
60932
 
** code of statement pStmt set to SQLITE_NOMEM.
60933
 
**
60934
 
** Specifically, this is called from within:
60935
 
**
60936
 
**     sqlite3_column_int()
60937
 
**     sqlite3_column_int64()
60938
 
**     sqlite3_column_text()
60939
 
**     sqlite3_column_text16()
60940
 
**     sqlite3_column_real()
60941
 
**     sqlite3_column_bytes()
60942
 
**     sqlite3_column_bytes16()
60943
 
**     sqiite3_column_blob()
60944
 
*/
60945
 
static void columnMallocFailure(sqlite3_stmt *pStmt)
60946
 
{
60947
 
  /* If malloc() failed during an encoding conversion within an
60948
 
  ** sqlite3_column_XXX API, then set the return code of the statement to
60949
 
  ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
60950
 
  ** and _finalize() will return NOMEM.
60951
 
  */
60952
 
  Vdbe *p = (Vdbe *)pStmt;
60953
 
  if( p ){
60954
 
    p->rc = sqlite3ApiExit(p->db, p->rc);
60955
 
    sqlite3_mutex_leave(p->db->mutex);
60956
 
  }
60957
 
}
60958
 
 
60959
 
/**************************** sqlite3_column_  *******************************
60960
 
** The following routines are used to access elements of the current row
60961
 
** in the result set.
60962
 
*/
60963
 
SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
60964
 
  const void *val;
60965
 
  val = sqlite3_value_blob( columnMem(pStmt,i) );
60966
 
  /* Even though there is no encoding conversion, value_blob() might
60967
 
  ** need to call malloc() to expand the result of a zeroblob() 
60968
 
  ** expression. 
60969
 
  */
60970
 
  columnMallocFailure(pStmt);
60971
 
  return val;
60972
 
}
60973
 
SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
60974
 
  int val = sqlite3_value_bytes( columnMem(pStmt,i) );
60975
 
  columnMallocFailure(pStmt);
60976
 
  return val;
60977
 
}
60978
 
SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
60979
 
  int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
60980
 
  columnMallocFailure(pStmt);
60981
 
  return val;
60982
 
}
60983
 
SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
60984
 
  double val = sqlite3_value_double( columnMem(pStmt,i) );
60985
 
  columnMallocFailure(pStmt);
60986
 
  return val;
60987
 
}
60988
 
SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
60989
 
  int val = sqlite3_value_int( columnMem(pStmt,i) );
60990
 
  columnMallocFailure(pStmt);
60991
 
  return val;
60992
 
}
60993
 
SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
60994
 
  sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
60995
 
  columnMallocFailure(pStmt);
60996
 
  return val;
60997
 
}
60998
 
SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
60999
 
  const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
61000
 
  columnMallocFailure(pStmt);
61001
 
  return val;
61002
 
}
61003
 
SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
61004
 
  Mem *pOut = columnMem(pStmt, i);
61005
 
  if( pOut->flags&MEM_Static ){
61006
 
    pOut->flags &= ~MEM_Static;
61007
 
    pOut->flags |= MEM_Ephem;
61008
 
  }
61009
 
  columnMallocFailure(pStmt);
61010
 
  return (sqlite3_value *)pOut;
61011
 
}
61012
 
#ifndef SQLITE_OMIT_UTF16
61013
 
SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
61014
 
  const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
61015
 
  columnMallocFailure(pStmt);
61016
 
  return val;
61017
 
}
61018
 
#endif /* SQLITE_OMIT_UTF16 */
61019
 
SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
61020
 
  int iType = sqlite3_value_type( columnMem(pStmt,i) );
61021
 
  columnMallocFailure(pStmt);
61022
 
  return iType;
61023
 
}
61024
 
 
61025
 
/* The following function is experimental and subject to change or
61026
 
** removal */
61027
 
/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
61028
 
**  return sqlite3_value_numeric_type( columnMem(pStmt,i) );
61029
 
**}
61030
 
*/
61031
 
 
61032
 
/*
61033
 
** Convert the N-th element of pStmt->pColName[] into a string using
61034
 
** xFunc() then return that string.  If N is out of range, return 0.
61035
 
**
61036
 
** There are up to 5 names for each column.  useType determines which
61037
 
** name is returned.  Here are the names:
61038
 
**
61039
 
**    0      The column name as it should be displayed for output
61040
 
**    1      The datatype name for the column
61041
 
**    2      The name of the database that the column derives from
61042
 
**    3      The name of the table that the column derives from
61043
 
**    4      The name of the table column that the result column derives from
61044
 
**
61045
 
** If the result is not a simple column reference (if it is an expression
61046
 
** or a constant) then useTypes 2, 3, and 4 return NULL.
61047
 
*/
61048
 
static const void *columnName(
61049
 
  sqlite3_stmt *pStmt,
61050
 
  int N,
61051
 
  const void *(*xFunc)(Mem*),
61052
 
  int useType
61053
 
){
61054
 
  const void *ret = 0;
61055
 
  Vdbe *p = (Vdbe *)pStmt;
61056
 
  int n;
61057
 
  sqlite3 *db = p->db;
61058
 
  
61059
 
  assert( db!=0 );
61060
 
  n = sqlite3_column_count(pStmt);
61061
 
  if( N<n && N>=0 ){
61062
 
    N += useType*n;
61063
 
    sqlite3_mutex_enter(db->mutex);
61064
 
    assert( db->mallocFailed==0 );
61065
 
    ret = xFunc(&p->aColName[N]);
61066
 
     /* A malloc may have failed inside of the xFunc() call. If this
61067
 
    ** is the case, clear the mallocFailed flag and return NULL.
61068
 
    */
61069
 
    if( db->mallocFailed ){
61070
 
      db->mallocFailed = 0;
61071
 
      ret = 0;
61072
 
    }
61073
 
    sqlite3_mutex_leave(db->mutex);
61074
 
  }
61075
 
  return ret;
61076
 
}
61077
 
 
61078
 
/*
61079
 
** Return the name of the Nth column of the result set returned by SQL
61080
 
** statement pStmt.
61081
 
*/
61082
 
SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
61083
 
  return columnName(
61084
 
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
61085
 
}
61086
 
#ifndef SQLITE_OMIT_UTF16
61087
 
SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
61088
 
  return columnName(
61089
 
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
61090
 
}
61091
 
#endif
61092
 
 
61093
 
/*
61094
 
** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
61095
 
** not define OMIT_DECLTYPE.
61096
 
*/
61097
 
#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
61098
 
# error "Must not define both SQLITE_OMIT_DECLTYPE \
61099
 
         and SQLITE_ENABLE_COLUMN_METADATA"
61100
 
#endif
61101
 
 
61102
 
#ifndef SQLITE_OMIT_DECLTYPE
61103
 
/*
61104
 
** Return the column declaration type (if applicable) of the 'i'th column
61105
 
** of the result set of SQL statement pStmt.
61106
 
*/
61107
 
SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
61108
 
  return columnName(
61109
 
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
61110
 
}
61111
 
#ifndef SQLITE_OMIT_UTF16
61112
 
SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
61113
 
  return columnName(
61114
 
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
61115
 
}
61116
 
#endif /* SQLITE_OMIT_UTF16 */
61117
 
#endif /* SQLITE_OMIT_DECLTYPE */
61118
 
 
61119
 
#ifdef SQLITE_ENABLE_COLUMN_METADATA
61120
 
/*
61121
 
** Return the name of the database from which a result column derives.
61122
 
** NULL is returned if the result column is an expression or constant or
61123
 
** anything else which is not an unabiguous reference to a database column.
61124
 
*/
61125
 
SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
61126
 
  return columnName(
61127
 
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
61128
 
}
61129
 
#ifndef SQLITE_OMIT_UTF16
61130
 
SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
61131
 
  return columnName(
61132
 
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
61133
 
}
61134
 
#endif /* SQLITE_OMIT_UTF16 */
61135
 
 
61136
 
/*
61137
 
** Return the name of the table from which a result column derives.
61138
 
** NULL is returned if the result column is an expression or constant or
61139
 
** anything else which is not an unabiguous reference to a database column.
61140
 
*/
61141
 
SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
61142
 
  return columnName(
61143
 
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
61144
 
}
61145
 
#ifndef SQLITE_OMIT_UTF16
61146
 
SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
61147
 
  return columnName(
61148
 
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
61149
 
}
61150
 
#endif /* SQLITE_OMIT_UTF16 */
61151
 
 
61152
 
/*
61153
 
** Return the name of the table column from which a result column derives.
61154
 
** NULL is returned if the result column is an expression or constant or
61155
 
** anything else which is not an unabiguous reference to a database column.
61156
 
*/
61157
 
SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
61158
 
  return columnName(
61159
 
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
61160
 
}
61161
 
#ifndef SQLITE_OMIT_UTF16
61162
 
SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
61163
 
  return columnName(
61164
 
      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
61165
 
}
61166
 
#endif /* SQLITE_OMIT_UTF16 */
61167
 
#endif /* SQLITE_ENABLE_COLUMN_METADATA */
61168
 
 
61169
 
 
61170
 
/******************************* sqlite3_bind_  ***************************
61171
 
** 
61172
 
** Routines used to attach values to wildcards in a compiled SQL statement.
61173
 
*/
61174
 
/*
61175
 
** Unbind the value bound to variable i in virtual machine p. This is the 
61176
 
** the same as binding a NULL value to the column. If the "i" parameter is
61177
 
** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
61178
 
**
61179
 
** A successful evaluation of this routine acquires the mutex on p.
61180
 
** the mutex is released if any kind of error occurs.
61181
 
**
61182
 
** The error code stored in database p->db is overwritten with the return
61183
 
** value in any case.
61184
 
*/
61185
 
static int vdbeUnbind(Vdbe *p, int i){
61186
 
  Mem *pVar;
61187
 
  if( vdbeSafetyNotNull(p) ){
61188
 
    return SQLITE_MISUSE_BKPT;
61189
 
  }
61190
 
  sqlite3_mutex_enter(p->db->mutex);
61191
 
  if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
61192
 
    sqlite3Error(p->db, SQLITE_MISUSE, 0);
61193
 
    sqlite3_mutex_leave(p->db->mutex);
61194
 
    sqlite3_log(SQLITE_MISUSE, 
61195
 
        "bind on a busy prepared statement: [%s]", p->zSql);
61196
 
    return SQLITE_MISUSE_BKPT;
61197
 
  }
61198
 
  if( i<1 || i>p->nVar ){
61199
 
    sqlite3Error(p->db, SQLITE_RANGE, 0);
61200
 
    sqlite3_mutex_leave(p->db->mutex);
61201
 
    return SQLITE_RANGE;
61202
 
  }
61203
 
  i--;
61204
 
  pVar = &p->aVar[i];
61205
 
  sqlite3VdbeMemRelease(pVar);
61206
 
  pVar->flags = MEM_Null;
61207
 
  sqlite3Error(p->db, SQLITE_OK, 0);
61208
 
 
61209
 
  /* If the bit corresponding to this variable in Vdbe.expmask is set, then 
61210
 
  ** binding a new value to this variable invalidates the current query plan.
61211
 
  **
61212
 
  ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
61213
 
  ** parameter in the WHERE clause might influence the choice of query plan
61214
 
  ** for a statement, then the statement will be automatically recompiled,
61215
 
  ** as if there had been a schema change, on the first sqlite3_step() call
61216
 
  ** following any change to the bindings of that parameter.
61217
 
  */
61218
 
  if( p->isPrepareV2 &&
61219
 
     ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
61220
 
  ){
61221
 
    p->expired = 1;
61222
 
  }
61223
 
  return SQLITE_OK;
61224
 
}
61225
 
 
61226
 
/*
61227
 
** Bind a text or BLOB value.
61228
 
*/
61229
 
static int bindText(
61230
 
  sqlite3_stmt *pStmt,   /* The statement to bind against */
61231
 
  int i,                 /* Index of the parameter to bind */
61232
 
  const void *zData,     /* Pointer to the data to be bound */
61233
 
  int nData,             /* Number of bytes of data to be bound */
61234
 
  void (*xDel)(void*),   /* Destructor for the data */
61235
 
  u8 encoding            /* Encoding for the data */
61236
 
){
61237
 
  Vdbe *p = (Vdbe *)pStmt;
61238
 
  Mem *pVar;
61239
 
  int rc;
61240
 
 
61241
 
  rc = vdbeUnbind(p, i);
61242
 
  if( rc==SQLITE_OK ){
61243
 
    if( zData!=0 ){
61244
 
      pVar = &p->aVar[i-1];
61245
 
      rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
61246
 
      if( rc==SQLITE_OK && encoding!=0 ){
61247
 
        rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
61248
 
      }
61249
 
      sqlite3Error(p->db, rc, 0);
61250
 
      rc = sqlite3ApiExit(p->db, rc);
61251
 
    }
61252
 
    sqlite3_mutex_leave(p->db->mutex);
61253
 
  }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
61254
 
    xDel((void*)zData);
61255
 
  }
61256
 
  return rc;
61257
 
}
61258
 
 
61259
 
 
61260
 
/*
61261
 
** Bind a blob value to an SQL statement variable.
61262
 
*/
61263
 
SQLITE_API int sqlite3_bind_blob(
61264
 
  sqlite3_stmt *pStmt, 
61265
 
  int i, 
61266
 
  const void *zData, 
61267
 
  int nData, 
61268
 
  void (*xDel)(void*)
61269
 
){
61270
 
  return bindText(pStmt, i, zData, nData, xDel, 0);
61271
 
}
61272
 
SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
61273
 
  int rc;
61274
 
  Vdbe *p = (Vdbe *)pStmt;
61275
 
  rc = vdbeUnbind(p, i);
61276
 
  if( rc==SQLITE_OK ){
61277
 
    sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
61278
 
    sqlite3_mutex_leave(p->db->mutex);
61279
 
  }
61280
 
  return rc;
61281
 
}
61282
 
SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
61283
 
  return sqlite3_bind_int64(p, i, (i64)iValue);
61284
 
}
61285
 
SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
61286
 
  int rc;
61287
 
  Vdbe *p = (Vdbe *)pStmt;
61288
 
  rc = vdbeUnbind(p, i);
61289
 
  if( rc==SQLITE_OK ){
61290
 
    sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
61291
 
    sqlite3_mutex_leave(p->db->mutex);
61292
 
  }
61293
 
  return rc;
61294
 
}
61295
 
SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
61296
 
  int rc;
61297
 
  Vdbe *p = (Vdbe*)pStmt;
61298
 
  rc = vdbeUnbind(p, i);
61299
 
  if( rc==SQLITE_OK ){
61300
 
    sqlite3_mutex_leave(p->db->mutex);
61301
 
  }
61302
 
  return rc;
61303
 
}
61304
 
SQLITE_API int sqlite3_bind_text( 
61305
 
  sqlite3_stmt *pStmt, 
61306
 
  int i, 
61307
 
  const char *zData, 
61308
 
  int nData, 
61309
 
  void (*xDel)(void*)
61310
 
){
61311
 
  return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
61312
 
}
61313
 
#ifndef SQLITE_OMIT_UTF16
61314
 
SQLITE_API int sqlite3_bind_text16(
61315
 
  sqlite3_stmt *pStmt, 
61316
 
  int i, 
61317
 
  const void *zData, 
61318
 
  int nData, 
61319
 
  void (*xDel)(void*)
61320
 
){
61321
 
  return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
61322
 
}
61323
 
#endif /* SQLITE_OMIT_UTF16 */
61324
 
SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
61325
 
  int rc;
61326
 
  switch( pValue->type ){
61327
 
    case SQLITE_INTEGER: {
61328
 
      rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
61329
 
      break;
61330
 
    }
61331
 
    case SQLITE_FLOAT: {
61332
 
      rc = sqlite3_bind_double(pStmt, i, pValue->r);
61333
 
      break;
61334
 
    }
61335
 
    case SQLITE_BLOB: {
61336
 
      if( pValue->flags & MEM_Zero ){
61337
 
        rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
61338
 
      }else{
61339
 
        rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
61340
 
      }
61341
 
      break;
61342
 
    }
61343
 
    case SQLITE_TEXT: {
61344
 
      rc = bindText(pStmt,i,  pValue->z, pValue->n, SQLITE_TRANSIENT,
61345
 
                              pValue->enc);
61346
 
      break;
61347
 
    }
61348
 
    default: {
61349
 
      rc = sqlite3_bind_null(pStmt, i);
61350
 
      break;
61351
 
    }
61352
 
  }
61353
 
  return rc;
61354
 
}
61355
 
SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
61356
 
  int rc;
61357
 
  Vdbe *p = (Vdbe *)pStmt;
61358
 
  rc = vdbeUnbind(p, i);
61359
 
  if( rc==SQLITE_OK ){
61360
 
    sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
61361
 
    sqlite3_mutex_leave(p->db->mutex);
61362
 
  }
61363
 
  return rc;
61364
 
}
61365
 
 
61366
 
/*
61367
 
** Return the number of wildcards that can be potentially bound to.
61368
 
** This routine is added to support DBD::SQLite.  
61369
 
*/
61370
 
SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
61371
 
  Vdbe *p = (Vdbe*)pStmt;
61372
 
  return p ? p->nVar : 0;
61373
 
}
61374
 
 
61375
 
/*
61376
 
** Create a mapping from variable numbers to variable names
61377
 
** in the Vdbe.azVar[] array, if such a mapping does not already
61378
 
** exist.
61379
 
*/
61380
 
static void createVarMap(Vdbe *p){
61381
 
  if( !p->okVar ){
61382
 
    int j;
61383
 
    Op *pOp;
61384
 
    sqlite3_mutex_enter(p->db->mutex);
61385
 
    /* The race condition here is harmless.  If two threads call this
61386
 
    ** routine on the same Vdbe at the same time, they both might end
61387
 
    ** up initializing the Vdbe.azVar[] array.  That is a little extra
61388
 
    ** work but it results in the same answer.
61389
 
    */
61390
 
    for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
61391
 
      if( pOp->opcode==OP_Variable ){
61392
 
        assert( pOp->p1>0 && pOp->p1<=p->nVar );
61393
 
        p->azVar[pOp->p1-1] = pOp->p4.z;
61394
 
      }
61395
 
    }
61396
 
    p->okVar = 1;
61397
 
    sqlite3_mutex_leave(p->db->mutex);
61398
 
  }
61399
 
}
61400
 
 
61401
 
/*
61402
 
** Return the name of a wildcard parameter.  Return NULL if the index
61403
 
** is out of range or if the wildcard is unnamed.
61404
 
**
61405
 
** The result is always UTF-8.
61406
 
*/
61407
 
SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
61408
 
  Vdbe *p = (Vdbe*)pStmt;
61409
 
  if( p==0 || i<1 || i>p->nVar ){
61410
 
    return 0;
61411
 
  }
61412
 
  createVarMap(p);
61413
 
  return p->azVar[i-1];
61414
 
}
61415
 
 
61416
 
/*
61417
 
** Given a wildcard parameter name, return the index of the variable
61418
 
** with that name.  If there is no variable with the given name,
61419
 
** return 0.
61420
 
*/
61421
 
SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
61422
 
  int i;
61423
 
  if( p==0 ){
61424
 
    return 0;
61425
 
  }
61426
 
  createVarMap(p); 
61427
 
  if( zName ){
61428
 
    for(i=0; i<p->nVar; i++){
61429
 
      const char *z = p->azVar[i];
61430
 
      if( z && memcmp(z,zName,nName)==0 && z[nName]==0 ){
61431
 
        return i+1;
61432
 
      }
61433
 
    }
61434
 
  }
61435
 
  return 0;
61436
 
}
61437
 
SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
61438
 
  return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
61439
 
}
61440
 
 
61441
 
/*
61442
 
** Transfer all bindings from the first statement over to the second.
61443
 
*/
61444
 
SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
61445
 
  Vdbe *pFrom = (Vdbe*)pFromStmt;
61446
 
  Vdbe *pTo = (Vdbe*)pToStmt;
61447
 
  int i;
61448
 
  assert( pTo->db==pFrom->db );
61449
 
  assert( pTo->nVar==pFrom->nVar );
61450
 
  sqlite3_mutex_enter(pTo->db->mutex);
61451
 
  for(i=0; i<pFrom->nVar; i++){
61452
 
    sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
61453
 
  }
61454
 
  sqlite3_mutex_leave(pTo->db->mutex);
61455
 
  return SQLITE_OK;
61456
 
}
61457
 
 
61458
 
#ifndef SQLITE_OMIT_DEPRECATED
61459
 
/*
61460
 
** Deprecated external interface.  Internal/core SQLite code
61461
 
** should call sqlite3TransferBindings.
61462
 
**
61463
 
** Is is misuse to call this routine with statements from different
61464
 
** database connections.  But as this is a deprecated interface, we
61465
 
** will not bother to check for that condition.
61466
 
**
61467
 
** If the two statements contain a different number of bindings, then
61468
 
** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
61469
 
** SQLITE_OK is returned.
61470
 
*/
61471
 
SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
61472
 
  Vdbe *pFrom = (Vdbe*)pFromStmt;
61473
 
  Vdbe *pTo = (Vdbe*)pToStmt;
61474
 
  if( pFrom->nVar!=pTo->nVar ){
61475
 
    return SQLITE_ERROR;
61476
 
  }
61477
 
  if( pTo->isPrepareV2 && pTo->expmask ){
61478
 
    pTo->expired = 1;
61479
 
  }
61480
 
  if( pFrom->isPrepareV2 && pFrom->expmask ){
61481
 
    pFrom->expired = 1;
61482
 
  }
61483
 
  return sqlite3TransferBindings(pFromStmt, pToStmt);
61484
 
}
61485
 
#endif
61486
 
 
61487
 
/*
61488
 
** Return the sqlite3* database handle to which the prepared statement given
61489
 
** in the argument belongs.  This is the same database handle that was
61490
 
** the first argument to the sqlite3_prepare() that was used to create
61491
 
** the statement in the first place.
61492
 
*/
61493
 
SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
61494
 
  return pStmt ? ((Vdbe*)pStmt)->db : 0;
61495
 
}
61496
 
 
61497
 
/*
61498
 
** Return true if the prepared statement is guaranteed to not modify the
61499
 
** database.
61500
 
*/
61501
 
SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
61502
 
  return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
61503
 
}
61504
 
 
61505
 
/*
61506
 
** Return a pointer to the next prepared statement after pStmt associated
61507
 
** with database connection pDb.  If pStmt is NULL, return the first
61508
 
** prepared statement for the database connection.  Return NULL if there
61509
 
** are no more.
61510
 
*/
61511
 
SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
61512
 
  sqlite3_stmt *pNext;
61513
 
  sqlite3_mutex_enter(pDb->mutex);
61514
 
  if( pStmt==0 ){
61515
 
    pNext = (sqlite3_stmt*)pDb->pVdbe;
61516
 
  }else{
61517
 
    pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
61518
 
  }
61519
 
  sqlite3_mutex_leave(pDb->mutex);
61520
 
  return pNext;
61521
 
}
61522
 
 
61523
 
/*
61524
 
** Return the value of a status counter for a prepared statement
61525
 
*/
61526
 
SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
61527
 
  Vdbe *pVdbe = (Vdbe*)pStmt;
61528
 
  int v = pVdbe->aCounter[op-1];
61529
 
  if( resetFlag ) pVdbe->aCounter[op-1] = 0;
61530
 
  return v;
61531
 
}
61532
 
 
61533
 
/************** End of vdbeapi.c *********************************************/
61534
 
/************** Begin file vdbetrace.c ***************************************/
61535
 
/*
61536
 
** 2009 November 25
61537
 
**
61538
 
** The author disclaims copyright to this source code.  In place of
61539
 
** a legal notice, here is a blessing:
61540
 
**
61541
 
**    May you do good and not evil.
61542
 
**    May you find forgiveness for yourself and forgive others.
61543
 
**    May you share freely, never taking more than you give.
61544
 
**
61545
 
*************************************************************************
61546
 
**
61547
 
** This file contains code used to insert the values of host parameters
61548
 
** (aka "wildcards") into the SQL text output by sqlite3_trace().
61549
 
*/
61550
 
 
61551
 
#ifndef SQLITE_OMIT_TRACE
61552
 
 
61553
 
/*
61554
 
** zSql is a zero-terminated string of UTF-8 SQL text.  Return the number of
61555
 
** bytes in this text up to but excluding the first character in
61556
 
** a host parameter.  If the text contains no host parameters, return
61557
 
** the total number of bytes in the text.
61558
 
*/
61559
 
static int findNextHostParameter(const char *zSql, int *pnToken){
61560
 
  int tokenType;
61561
 
  int nTotal = 0;
61562
 
  int n;
61563
 
 
61564
 
  *pnToken = 0;
61565
 
  while( zSql[0] ){
61566
 
    n = sqlite3GetToken((u8*)zSql, &tokenType);
61567
 
    assert( n>0 && tokenType!=TK_ILLEGAL );
61568
 
    if( tokenType==TK_VARIABLE ){
61569
 
      *pnToken = n;
61570
 
      break;
61571
 
    }
61572
 
    nTotal += n;
61573
 
    zSql += n;
61574
 
  }
61575
 
  return nTotal;
61576
 
}
61577
 
 
61578
 
/*
61579
 
** This function returns a pointer to a nul-terminated string in memory
61580
 
** obtained from sqlite3DbMalloc(). If sqlite3.vdbeExecCnt is 1, then the
61581
 
** string contains a copy of zRawSql but with host parameters expanded to 
61582
 
** their current bindings. Or, if sqlite3.vdbeExecCnt is greater than 1, 
61583
 
** then the returned string holds a copy of zRawSql with "-- " prepended
61584
 
** to each line of text.
61585
 
**
61586
 
** The calling function is responsible for making sure the memory returned
61587
 
** is eventually freed.
61588
 
**
61589
 
** ALGORITHM:  Scan the input string looking for host parameters in any of
61590
 
** these forms:  ?, ?N, $A, @A, :A.  Take care to avoid text within
61591
 
** string literals, quoted identifier names, and comments.  For text forms,
61592
 
** the host parameter index is found by scanning the perpared
61593
 
** statement for the corresponding OP_Variable opcode.  Once the host
61594
 
** parameter index is known, locate the value in p->aVar[].  Then render
61595
 
** the value as a literal in place of the host parameter name.
61596
 
*/
61597
 
SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
61598
 
  Vdbe *p,                 /* The prepared statement being evaluated */
61599
 
  const char *zRawSql      /* Raw text of the SQL statement */
61600
 
){
61601
 
  sqlite3 *db;             /* The database connection */
61602
 
  int idx = 0;             /* Index of a host parameter */
61603
 
  int nextIndex = 1;       /* Index of next ? host parameter */
61604
 
  int n;                   /* Length of a token prefix */
61605
 
  int nToken;              /* Length of the parameter token */
61606
 
  int i;                   /* Loop counter */
61607
 
  Mem *pVar;               /* Value of a host parameter */
61608
 
  StrAccum out;            /* Accumulate the output here */
61609
 
  char zBase[100];         /* Initial working space */
61610
 
 
61611
 
  db = p->db;
61612
 
  sqlite3StrAccumInit(&out, zBase, sizeof(zBase), 
61613
 
                      db->aLimit[SQLITE_LIMIT_LENGTH]);
61614
 
  out.db = db;
61615
 
  if( db->vdbeExecCnt>1 ){
61616
 
    while( *zRawSql ){
61617
 
      const char *zStart = zRawSql;
61618
 
      while( *(zRawSql++)!='\n' && *zRawSql );
61619
 
      sqlite3StrAccumAppend(&out, "-- ", 3);
61620
 
      sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
61621
 
    }
61622
 
  }else{
61623
 
    while( zRawSql[0] ){
61624
 
      n = findNextHostParameter(zRawSql, &nToken);
61625
 
      assert( n>0 );
61626
 
      sqlite3StrAccumAppend(&out, zRawSql, n);
61627
 
      zRawSql += n;
61628
 
      assert( zRawSql[0] || nToken==0 );
61629
 
      if( nToken==0 ) break;
61630
 
      if( zRawSql[0]=='?' ){
61631
 
        if( nToken>1 ){
61632
 
          assert( sqlite3Isdigit(zRawSql[1]) );
61633
 
          sqlite3GetInt32(&zRawSql[1], &idx);
61634
 
        }else{
61635
 
          idx = nextIndex;
61636
 
        }
61637
 
      }else{
61638
 
        assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
61639
 
        testcase( zRawSql[0]==':' );
61640
 
        testcase( zRawSql[0]=='$' );
61641
 
        testcase( zRawSql[0]=='@' );
61642
 
        idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
61643
 
        assert( idx>0 );
61644
 
      }
61645
 
      zRawSql += nToken;
61646
 
      nextIndex = idx + 1;
61647
 
      assert( idx>0 && idx<=p->nVar );
61648
 
      pVar = &p->aVar[idx-1];
61649
 
      if( pVar->flags & MEM_Null ){
61650
 
        sqlite3StrAccumAppend(&out, "NULL", 4);
61651
 
      }else if( pVar->flags & MEM_Int ){
61652
 
        sqlite3XPrintf(&out, "%lld", pVar->u.i);
61653
 
      }else if( pVar->flags & MEM_Real ){
61654
 
        sqlite3XPrintf(&out, "%!.15g", pVar->r);
61655
 
      }else if( pVar->flags & MEM_Str ){
61656
 
#ifndef SQLITE_OMIT_UTF16
61657
 
        u8 enc = ENC(db);
61658
 
        if( enc!=SQLITE_UTF8 ){
61659
 
          Mem utf8;
61660
 
          memset(&utf8, 0, sizeof(utf8));
61661
 
          utf8.db = db;
61662
 
          sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
61663
 
          sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
61664
 
          sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
61665
 
          sqlite3VdbeMemRelease(&utf8);
61666
 
        }else
61667
 
#endif
61668
 
        {
61669
 
          sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
61670
 
        }
61671
 
      }else if( pVar->flags & MEM_Zero ){
61672
 
        sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
61673
 
      }else{
61674
 
        assert( pVar->flags & MEM_Blob );
61675
 
        sqlite3StrAccumAppend(&out, "x'", 2);
61676
 
        for(i=0; i<pVar->n; i++){
61677
 
          sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
61678
 
        }
61679
 
        sqlite3StrAccumAppend(&out, "'", 1);
61680
 
      }
61681
 
    }
61682
 
  }
61683
 
  return sqlite3StrAccumFinish(&out);
61684
 
}
61685
 
 
61686
 
#endif /* #ifndef SQLITE_OMIT_TRACE */
61687
 
 
61688
 
/************** End of vdbetrace.c *******************************************/
61689
 
/************** Begin file vdbe.c ********************************************/
61690
 
/*
61691
 
** 2001 September 15
61692
 
**
61693
 
** The author disclaims copyright to this source code.  In place of
61694
 
** a legal notice, here is a blessing:
61695
 
**
61696
 
**    May you do good and not evil.
61697
 
**    May you find forgiveness for yourself and forgive others.
61698
 
**    May you share freely, never taking more than you give.
61699
 
**
61700
 
*************************************************************************
61701
 
** The code in this file implements execution method of the 
61702
 
** Virtual Database Engine (VDBE).  A separate file ("vdbeaux.c")
61703
 
** handles housekeeping details such as creating and deleting
61704
 
** VDBE instances.  This file is solely interested in executing
61705
 
** the VDBE program.
61706
 
**
61707
 
** In the external interface, an "sqlite3_stmt*" is an opaque pointer
61708
 
** to a VDBE.
61709
 
**
61710
 
** The SQL parser generates a program which is then executed by
61711
 
** the VDBE to do the work of the SQL statement.  VDBE programs are 
61712
 
** similar in form to assembly language.  The program consists of
61713
 
** a linear sequence of operations.  Each operation has an opcode 
61714
 
** and 5 operands.  Operands P1, P2, and P3 are integers.  Operand P4 
61715
 
** is a null-terminated string.  Operand P5 is an unsigned character.
61716
 
** Few opcodes use all 5 operands.
61717
 
**
61718
 
** Computation results are stored on a set of registers numbered beginning
61719
 
** with 1 and going up to Vdbe.nMem.  Each register can store
61720
 
** either an integer, a null-terminated string, a floating point
61721
 
** number, or the SQL "NULL" value.  An implicit conversion from one
61722
 
** type to the other occurs as necessary.
61723
 
** 
61724
 
** Most of the code in this file is taken up by the sqlite3VdbeExec()
61725
 
** function which does the work of interpreting a VDBE program.
61726
 
** But other routines are also provided to help in building up
61727
 
** a program instruction by instruction.
61728
 
**
61729
 
** Various scripts scan this source file in order to generate HTML
61730
 
** documentation, headers files, or other derived files.  The formatting
61731
 
** of the code in this file is, therefore, important.  See other comments
61732
 
** in this file for details.  If in doubt, do not deviate from existing
61733
 
** commenting and indentation practices when changing or adding code.
61734
 
*/
61735
 
 
61736
 
/*
61737
 
** Invoke this macro on memory cells just prior to changing the
61738
 
** value of the cell.  This macro verifies that shallow copies are
61739
 
** not misused.
61740
 
*/
61741
 
#ifdef SQLITE_DEBUG
61742
 
# define memAboutToChange(P,M) sqlite3VdbeMemPrepareToChange(P,M)
61743
 
#else
61744
 
# define memAboutToChange(P,M)
61745
 
#endif
61746
 
 
61747
 
/*
61748
 
** The following global variable is incremented every time a cursor
61749
 
** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes.  The test
61750
 
** procedures use this information to make sure that indices are
61751
 
** working correctly.  This variable has no function other than to
61752
 
** help verify the correct operation of the library.
61753
 
*/
61754
 
#ifdef SQLITE_TEST
61755
 
SQLITE_API int sqlite3_search_count = 0;
61756
 
#endif
61757
 
 
61758
 
/*
61759
 
** When this global variable is positive, it gets decremented once before
61760
 
** each instruction in the VDBE.  When reaches zero, the u1.isInterrupted
61761
 
** field of the sqlite3 structure is set in order to simulate and interrupt.
61762
 
**
61763
 
** This facility is used for testing purposes only.  It does not function
61764
 
** in an ordinary build.
61765
 
*/
61766
 
#ifdef SQLITE_TEST
61767
 
SQLITE_API int sqlite3_interrupt_count = 0;
61768
 
#endif
61769
 
 
61770
 
/*
61771
 
** The next global variable is incremented each type the OP_Sort opcode
61772
 
** is executed.  The test procedures use this information to make sure that
61773
 
** sorting is occurring or not occurring at appropriate times.   This variable
61774
 
** has no function other than to help verify the correct operation of the
61775
 
** library.
61776
 
*/
61777
 
#ifdef SQLITE_TEST
61778
 
SQLITE_API int sqlite3_sort_count = 0;
61779
 
#endif
61780
 
 
61781
 
/*
61782
 
** The next global variable records the size of the largest MEM_Blob
61783
 
** or MEM_Str that has been used by a VDBE opcode.  The test procedures
61784
 
** use this information to make sure that the zero-blob functionality
61785
 
** is working correctly.   This variable has no function other than to
61786
 
** help verify the correct operation of the library.
61787
 
*/
61788
 
#ifdef SQLITE_TEST
61789
 
SQLITE_API int sqlite3_max_blobsize = 0;
61790
 
static void updateMaxBlobsize(Mem *p){
61791
 
  if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
61792
 
    sqlite3_max_blobsize = p->n;
61793
 
  }
61794
 
}
61795
 
#endif
61796
 
 
61797
 
/*
61798
 
** The next global variable is incremented each type the OP_Found opcode
61799
 
** is executed. This is used to test whether or not the foreign key
61800
 
** operation implemented using OP_FkIsZero is working. This variable
61801
 
** has no function other than to help verify the correct operation of the
61802
 
** library.
61803
 
*/
61804
 
#ifdef SQLITE_TEST
61805
 
SQLITE_API int sqlite3_found_count = 0;
61806
 
#endif
61807
 
 
61808
 
/*
61809
 
** Test a register to see if it exceeds the current maximum blob size.
61810
 
** If it does, record the new maximum blob size.
61811
 
*/
61812
 
#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
61813
 
# define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
61814
 
#else
61815
 
# define UPDATE_MAX_BLOBSIZE(P)
61816
 
#endif
61817
 
 
61818
 
/*
61819
 
** Convert the given register into a string if it isn't one
61820
 
** already. Return non-zero if a malloc() fails.
61821
 
*/
61822
 
#define Stringify(P, enc) \
61823
 
   if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
61824
 
     { goto no_mem; }
61825
 
 
61826
 
/*
61827
 
** An ephemeral string value (signified by the MEM_Ephem flag) contains
61828
 
** a pointer to a dynamically allocated string where some other entity
61829
 
** is responsible for deallocating that string.  Because the register
61830
 
** does not control the string, it might be deleted without the register
61831
 
** knowing it.
61832
 
**
61833
 
** This routine converts an ephemeral string into a dynamically allocated
61834
 
** string that the register itself controls.  In other words, it
61835
 
** converts an MEM_Ephem string into an MEM_Dyn string.
61836
 
*/
61837
 
#define Deephemeralize(P) \
61838
 
   if( ((P)->flags&MEM_Ephem)!=0 \
61839
 
       && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
61840
 
 
61841
 
/*
61842
 
** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
61843
 
** P if required.
61844
 
*/
61845
 
#define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
61846
 
 
61847
 
/*
61848
 
** Argument pMem points at a register that will be passed to a
61849
 
** user-defined function or returned to the user as the result of a query.
61850
 
** This routine sets the pMem->type variable used by the sqlite3_value_*() 
61851
 
** routines.
61852
 
*/
61853
 
SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem){
61854
 
  int flags = pMem->flags;
61855
 
  if( flags & MEM_Null ){
61856
 
    pMem->type = SQLITE_NULL;
61857
 
  }
61858
 
  else if( flags & MEM_Int ){
61859
 
    pMem->type = SQLITE_INTEGER;
61860
 
  }
61861
 
  else if( flags & MEM_Real ){
61862
 
    pMem->type = SQLITE_FLOAT;
61863
 
  }
61864
 
  else if( flags & MEM_Str ){
61865
 
    pMem->type = SQLITE_TEXT;
61866
 
  }else{
61867
 
    pMem->type = SQLITE_BLOB;
61868
 
  }
61869
 
}
61870
 
 
61871
 
/*
61872
 
** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
61873
 
** if we run out of memory.
61874
 
*/
61875
 
static VdbeCursor *allocateCursor(
61876
 
  Vdbe *p,              /* The virtual machine */
61877
 
  int iCur,             /* Index of the new VdbeCursor */
61878
 
  int nField,           /* Number of fields in the table or index */
61879
 
  int iDb,              /* When database the cursor belongs to, or -1 */
61880
 
  int isBtreeCursor     /* True for B-Tree.  False for pseudo-table or vtab */
61881
 
){
61882
 
  /* Find the memory cell that will be used to store the blob of memory
61883
 
  ** required for this VdbeCursor structure. It is convenient to use a 
61884
 
  ** vdbe memory cell to manage the memory allocation required for a
61885
 
  ** VdbeCursor structure for the following reasons:
61886
 
  **
61887
 
  **   * Sometimes cursor numbers are used for a couple of different
61888
 
  **     purposes in a vdbe program. The different uses might require
61889
 
  **     different sized allocations. Memory cells provide growable
61890
 
  **     allocations.
61891
 
  **
61892
 
  **   * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
61893
 
  **     be freed lazily via the sqlite3_release_memory() API. This
61894
 
  **     minimizes the number of malloc calls made by the system.
61895
 
  **
61896
 
  ** Memory cells for cursors are allocated at the top of the address
61897
 
  ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
61898
 
  ** cursor 1 is managed by memory cell (p->nMem-1), etc.
61899
 
  */
61900
 
  Mem *pMem = &p->aMem[p->nMem-iCur];
61901
 
 
61902
 
  int nByte;
61903
 
  VdbeCursor *pCx = 0;
61904
 
  nByte = 
61905
 
      ROUND8(sizeof(VdbeCursor)) + 
61906
 
      (isBtreeCursor?sqlite3BtreeCursorSize():0) + 
61907
 
      2*nField*sizeof(u32);
61908
 
 
61909
 
  assert( iCur<p->nCursor );
61910
 
  if( p->apCsr[iCur] ){
61911
 
    sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
61912
 
    p->apCsr[iCur] = 0;
61913
 
  }
61914
 
  if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
61915
 
    p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
61916
 
    memset(pCx, 0, sizeof(VdbeCursor));
61917
 
    pCx->iDb = iDb;
61918
 
    pCx->nField = nField;
61919
 
    if( nField ){
61920
 
      pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))];
61921
 
    }
61922
 
    if( isBtreeCursor ){
61923
 
      pCx->pCursor = (BtCursor*)
61924
 
          &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)];
61925
 
      sqlite3BtreeCursorZero(pCx->pCursor);
61926
 
    }
61927
 
  }
61928
 
  return pCx;
61929
 
}
61930
 
 
61931
 
/*
61932
 
** Try to convert a value into a numeric representation if we can
61933
 
** do so without loss of information.  In other words, if the string
61934
 
** looks like a number, convert it into a number.  If it does not
61935
 
** look like a number, leave it alone.
61936
 
*/
61937
 
static void applyNumericAffinity(Mem *pRec){
61938
 
  if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
61939
 
    double rValue;
61940
 
    i64 iValue;
61941
 
    u8 enc = pRec->enc;
61942
 
    if( (pRec->flags&MEM_Str)==0 ) return;
61943
 
    if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
61944
 
    if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
61945
 
      pRec->u.i = iValue;
61946
 
      pRec->flags |= MEM_Int;
61947
 
    }else{
61948
 
      pRec->r = rValue;
61949
 
      pRec->flags |= MEM_Real;
61950
 
    }
61951
 
  }
61952
 
}
61953
 
 
61954
 
/*
61955
 
** Processing is determine by the affinity parameter:
61956
 
**
61957
 
** SQLITE_AFF_INTEGER:
61958
 
** SQLITE_AFF_REAL:
61959
 
** SQLITE_AFF_NUMERIC:
61960
 
**    Try to convert pRec to an integer representation or a 
61961
 
**    floating-point representation if an integer representation
61962
 
**    is not possible.  Note that the integer representation is
61963
 
**    always preferred, even if the affinity is REAL, because
61964
 
**    an integer representation is more space efficient on disk.
61965
 
**
61966
 
** SQLITE_AFF_TEXT:
61967
 
**    Convert pRec to a text representation.
61968
 
**
61969
 
** SQLITE_AFF_NONE:
61970
 
**    No-op.  pRec is unchanged.
61971
 
*/
61972
 
static void applyAffinity(
61973
 
  Mem *pRec,          /* The value to apply affinity to */
61974
 
  char affinity,      /* The affinity to be applied */
61975
 
  u8 enc              /* Use this text encoding */
61976
 
){
61977
 
  if( affinity==SQLITE_AFF_TEXT ){
61978
 
    /* Only attempt the conversion to TEXT if there is an integer or real
61979
 
    ** representation (blob and NULL do not get converted) but no string
61980
 
    ** representation.
61981
 
    */
61982
 
    if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
61983
 
      sqlite3VdbeMemStringify(pRec, enc);
61984
 
    }
61985
 
    pRec->flags &= ~(MEM_Real|MEM_Int);
61986
 
  }else if( affinity!=SQLITE_AFF_NONE ){
61987
 
    assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
61988
 
             || affinity==SQLITE_AFF_NUMERIC );
61989
 
    applyNumericAffinity(pRec);
61990
 
    if( pRec->flags & MEM_Real ){
61991
 
      sqlite3VdbeIntegerAffinity(pRec);
61992
 
    }
61993
 
  }
61994
 
}
61995
 
 
61996
 
/*
61997
 
** Try to convert the type of a function argument or a result column
61998
 
** into a numeric representation.  Use either INTEGER or REAL whichever
61999
 
** is appropriate.  But only do the conversion if it is possible without
62000
 
** loss of information and return the revised type of the argument.
62001
 
*/
62002
 
SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
62003
 
  Mem *pMem = (Mem*)pVal;
62004
 
  if( pMem->type==SQLITE_TEXT ){
62005
 
    applyNumericAffinity(pMem);
62006
 
    sqlite3VdbeMemStoreType(pMem);
62007
 
  }
62008
 
  return pMem->type;
62009
 
}
62010
 
 
62011
 
/*
62012
 
** Exported version of applyAffinity(). This one works on sqlite3_value*, 
62013
 
** not the internal Mem* type.
62014
 
*/
62015
 
SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
62016
 
  sqlite3_value *pVal, 
62017
 
  u8 affinity, 
62018
 
  u8 enc
62019
 
){
62020
 
  applyAffinity((Mem *)pVal, affinity, enc);
62021
 
}
62022
 
 
62023
 
#ifdef SQLITE_DEBUG
62024
 
/*
62025
 
** Write a nice string representation of the contents of cell pMem
62026
 
** into buffer zBuf, length nBuf.
62027
 
*/
62028
 
SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
62029
 
  char *zCsr = zBuf;
62030
 
  int f = pMem->flags;
62031
 
 
62032
 
  static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
62033
 
 
62034
 
  if( f&MEM_Blob ){
62035
 
    int i;
62036
 
    char c;
62037
 
    if( f & MEM_Dyn ){
62038
 
      c = 'z';
62039
 
      assert( (f & (MEM_Static|MEM_Ephem))==0 );
62040
 
    }else if( f & MEM_Static ){
62041
 
      c = 't';
62042
 
      assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
62043
 
    }else if( f & MEM_Ephem ){
62044
 
      c = 'e';
62045
 
      assert( (f & (MEM_Static|MEM_Dyn))==0 );
62046
 
    }else{
62047
 
      c = 's';
62048
 
    }
62049
 
 
62050
 
    sqlite3_snprintf(100, zCsr, "%c", c);
62051
 
    zCsr += sqlite3Strlen30(zCsr);
62052
 
    sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
62053
 
    zCsr += sqlite3Strlen30(zCsr);
62054
 
    for(i=0; i<16 && i<pMem->n; i++){
62055
 
      sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
62056
 
      zCsr += sqlite3Strlen30(zCsr);
62057
 
    }
62058
 
    for(i=0; i<16 && i<pMem->n; i++){
62059
 
      char z = pMem->z[i];
62060
 
      if( z<32 || z>126 ) *zCsr++ = '.';
62061
 
      else *zCsr++ = z;
62062
 
    }
62063
 
 
62064
 
    sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
62065
 
    zCsr += sqlite3Strlen30(zCsr);
62066
 
    if( f & MEM_Zero ){
62067
 
      sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
62068
 
      zCsr += sqlite3Strlen30(zCsr);
62069
 
    }
62070
 
    *zCsr = '\0';
62071
 
  }else if( f & MEM_Str ){
62072
 
    int j, k;
62073
 
    zBuf[0] = ' ';
62074
 
    if( f & MEM_Dyn ){
62075
 
      zBuf[1] = 'z';
62076
 
      assert( (f & (MEM_Static|MEM_Ephem))==0 );
62077
 
    }else if( f & MEM_Static ){
62078
 
      zBuf[1] = 't';
62079
 
      assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
62080
 
    }else if( f & MEM_Ephem ){
62081
 
      zBuf[1] = 'e';
62082
 
      assert( (f & (MEM_Static|MEM_Dyn))==0 );
62083
 
    }else{
62084
 
      zBuf[1] = 's';
62085
 
    }
62086
 
    k = 2;
62087
 
    sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
62088
 
    k += sqlite3Strlen30(&zBuf[k]);
62089
 
    zBuf[k++] = '[';
62090
 
    for(j=0; j<15 && j<pMem->n; j++){
62091
 
      u8 c = pMem->z[j];
62092
 
      if( c>=0x20 && c<0x7f ){
62093
 
        zBuf[k++] = c;
62094
 
      }else{
62095
 
        zBuf[k++] = '.';
62096
 
      }
62097
 
    }
62098
 
    zBuf[k++] = ']';
62099
 
    sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
62100
 
    k += sqlite3Strlen30(&zBuf[k]);
62101
 
    zBuf[k++] = 0;
62102
 
  }
62103
 
}
62104
 
#endif
62105
 
 
62106
 
#ifdef SQLITE_DEBUG
62107
 
/*
62108
 
** Print the value of a register for tracing purposes:
62109
 
*/
62110
 
static void memTracePrint(FILE *out, Mem *p){
62111
 
  if( p->flags & MEM_Null ){
62112
 
    fprintf(out, " NULL");
62113
 
  }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
62114
 
    fprintf(out, " si:%lld", p->u.i);
62115
 
  }else if( p->flags & MEM_Int ){
62116
 
    fprintf(out, " i:%lld", p->u.i);
62117
 
#ifndef SQLITE_OMIT_FLOATING_POINT
62118
 
  }else if( p->flags & MEM_Real ){
62119
 
    fprintf(out, " r:%g", p->r);
62120
 
#endif
62121
 
  }else if( p->flags & MEM_RowSet ){
62122
 
    fprintf(out, " (rowset)");
62123
 
  }else{
62124
 
    char zBuf[200];
62125
 
    sqlite3VdbeMemPrettyPrint(p, zBuf);
62126
 
    fprintf(out, " ");
62127
 
    fprintf(out, "%s", zBuf);
62128
 
  }
62129
 
}
62130
 
static void registerTrace(FILE *out, int iReg, Mem *p){
62131
 
  fprintf(out, "REG[%d] = ", iReg);
62132
 
  memTracePrint(out, p);
62133
 
  fprintf(out, "\n");
62134
 
}
62135
 
#endif
62136
 
 
62137
 
#ifdef SQLITE_DEBUG
62138
 
#  define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
62139
 
#else
62140
 
#  define REGISTER_TRACE(R,M)
62141
 
#endif
62142
 
 
62143
 
 
62144
 
#ifdef VDBE_PROFILE
62145
 
 
62146
 
/* 
62147
 
** hwtime.h contains inline assembler code for implementing 
62148
 
** high-performance timing routines.
62149
 
*/
62150
 
/************** Include hwtime.h in the middle of vdbe.c *********************/
62151
 
/************** Begin file hwtime.h ******************************************/
62152
 
/*
62153
 
** 2008 May 27
62154
 
**
62155
 
** The author disclaims copyright to this source code.  In place of
62156
 
** a legal notice, here is a blessing:
62157
 
**
62158
 
**    May you do good and not evil.
62159
 
**    May you find forgiveness for yourself and forgive others.
62160
 
**    May you share freely, never taking more than you give.
62161
 
**
62162
 
******************************************************************************
62163
 
**
62164
 
** This file contains inline asm code for retrieving "high-performance"
62165
 
** counters for x86 class CPUs.
62166
 
*/
62167
 
#ifndef _HWTIME_H_
62168
 
#define _HWTIME_H_
62169
 
 
62170
 
/*
62171
 
** The following routine only works on pentium-class (or newer) processors.
62172
 
** It uses the RDTSC opcode to read the cycle count value out of the
62173
 
** processor and returns that value.  This can be used for high-res
62174
 
** profiling.
62175
 
*/
62176
 
#if (defined(__GNUC__) || defined(_MSC_VER)) && \
62177
 
      (defined(i386) || defined(__i386__) || defined(_M_IX86))
62178
 
 
62179
 
  #if defined(__GNUC__)
62180
 
 
62181
 
  __inline__ sqlite_uint64 sqlite3Hwtime(void){
62182
 
     unsigned int lo, hi;
62183
 
     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
62184
 
     return (sqlite_uint64)hi << 32 | lo;
62185
 
  }
62186
 
 
62187
 
  #elif defined(_MSC_VER)
62188
 
 
62189
 
  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
62190
 
     __asm {
62191
 
        rdtsc
62192
 
        ret       ; return value at EDX:EAX
62193
 
     }
62194
 
  }
62195
 
 
62196
 
  #endif
62197
 
 
62198
 
#elif (defined(__GNUC__) && defined(__x86_64__))
62199
 
 
62200
 
  __inline__ sqlite_uint64 sqlite3Hwtime(void){
62201
 
      unsigned long val;
62202
 
      __asm__ __volatile__ ("rdtsc" : "=A" (val));
62203
 
      return val;
62204
 
  }
62205
 
 
62206
 
#elif (defined(__GNUC__) && defined(__ppc__))
62207
 
 
62208
 
  __inline__ sqlite_uint64 sqlite3Hwtime(void){
62209
 
      unsigned long long retval;
62210
 
      unsigned long junk;
62211
 
      __asm__ __volatile__ ("\n\
62212
 
          1:      mftbu   %1\n\
62213
 
                  mftb    %L0\n\
62214
 
                  mftbu   %0\n\
62215
 
                  cmpw    %0,%1\n\
62216
 
                  bne     1b"
62217
 
                  : "=r" (retval), "=r" (junk));
62218
 
      return retval;
62219
 
  }
62220
 
 
62221
 
#else
62222
 
 
62223
 
  #error Need implementation of sqlite3Hwtime() for your platform.
62224
 
 
62225
 
  /*
62226
 
  ** To compile without implementing sqlite3Hwtime() for your platform,
62227
 
  ** you can remove the above #error and use the following
62228
 
  ** stub function.  You will lose timing support for many
62229
 
  ** of the debugging and testing utilities, but it should at
62230
 
  ** least compile and run.
62231
 
  */
62232
 
SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
62233
 
 
62234
 
#endif
62235
 
 
62236
 
#endif /* !defined(_HWTIME_H_) */
62237
 
 
62238
 
/************** End of hwtime.h **********************************************/
62239
 
/************** Continuing where we left off in vdbe.c ***********************/
62240
 
 
62241
 
#endif
62242
 
 
62243
 
/*
62244
 
** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
62245
 
** sqlite3_interrupt() routine has been called.  If it has been, then
62246
 
** processing of the VDBE program is interrupted.
62247
 
**
62248
 
** This macro added to every instruction that does a jump in order to
62249
 
** implement a loop.  This test used to be on every single instruction,
62250
 
** but that meant we more testing that we needed.  By only testing the
62251
 
** flag on jump instructions, we get a (small) speed improvement.
62252
 
*/
62253
 
#define CHECK_FOR_INTERRUPT \
62254
 
   if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
62255
 
 
62256
 
 
62257
 
#ifndef NDEBUG
62258
 
/*
62259
 
** This function is only called from within an assert() expression. It
62260
 
** checks that the sqlite3.nTransaction variable is correctly set to
62261
 
** the number of non-transaction savepoints currently in the 
62262
 
** linked list starting at sqlite3.pSavepoint.
62263
 
** 
62264
 
** Usage:
62265
 
**
62266
 
**     assert( checkSavepointCount(db) );
62267
 
*/
62268
 
static int checkSavepointCount(sqlite3 *db){
62269
 
  int n = 0;
62270
 
  Savepoint *p;
62271
 
  for(p=db->pSavepoint; p; p=p->pNext) n++;
62272
 
  assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
62273
 
  return 1;
62274
 
}
62275
 
#endif
62276
 
 
62277
 
/*
62278
 
** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
62279
 
** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
62280
 
** in memory obtained from sqlite3DbMalloc).
62281
 
*/
62282
 
static void importVtabErrMsg(Vdbe *p, sqlite3_vtab *pVtab){
62283
 
  sqlite3 *db = p->db;
62284
 
  sqlite3DbFree(db, p->zErrMsg);
62285
 
  p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
62286
 
  sqlite3_free(pVtab->zErrMsg);
62287
 
  pVtab->zErrMsg = 0;
62288
 
}
62289
 
 
62290
 
 
62291
 
/*
62292
 
** Execute as much of a VDBE program as we can then return.
62293
 
**
62294
 
** sqlite3VdbeMakeReady() must be called before this routine in order to
62295
 
** close the program with a final OP_Halt and to set up the callbacks
62296
 
** and the error message pointer.
62297
 
**
62298
 
** Whenever a row or result data is available, this routine will either
62299
 
** invoke the result callback (if there is one) or return with
62300
 
** SQLITE_ROW.
62301
 
**
62302
 
** If an attempt is made to open a locked database, then this routine
62303
 
** will either invoke the busy callback (if there is one) or it will
62304
 
** return SQLITE_BUSY.
62305
 
**
62306
 
** If an error occurs, an error message is written to memory obtained
62307
 
** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
62308
 
** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
62309
 
**
62310
 
** If the callback ever returns non-zero, then the program exits
62311
 
** immediately.  There will be no error message but the p->rc field is
62312
 
** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
62313
 
**
62314
 
** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
62315
 
** routine to return SQLITE_ERROR.
62316
 
**
62317
 
** Other fatal errors return SQLITE_ERROR.
62318
 
**
62319
 
** After this routine has finished, sqlite3VdbeFinalize() should be
62320
 
** used to clean up the mess that was left behind.
62321
 
*/
62322
 
SQLITE_PRIVATE int sqlite3VdbeExec(
62323
 
  Vdbe *p                    /* The VDBE */
62324
 
){
62325
 
  int pc=0;                  /* The program counter */
62326
 
  Op *aOp = p->aOp;          /* Copy of p->aOp */
62327
 
  Op *pOp;                   /* Current operation */
62328
 
  int rc = SQLITE_OK;        /* Value to return */
62329
 
  sqlite3 *db = p->db;       /* The database */
62330
 
  u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
62331
 
  u8 encoding = ENC(db);     /* The database encoding */
62332
 
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
62333
 
  int checkProgress;         /* True if progress callbacks are enabled */
62334
 
  int nProgressOps = 0;      /* Opcodes executed since progress callback. */
62335
 
#endif
62336
 
  Mem *aMem = p->aMem;       /* Copy of p->aMem */
62337
 
  Mem *pIn1 = 0;             /* 1st input operand */
62338
 
  Mem *pIn2 = 0;             /* 2nd input operand */
62339
 
  Mem *pIn3 = 0;             /* 3rd input operand */
62340
 
  Mem *pOut = 0;             /* Output operand */
62341
 
  int iCompare = 0;          /* Result of last OP_Compare operation */
62342
 
  int *aPermute = 0;         /* Permutation of columns for OP_Compare */
62343
 
#ifdef VDBE_PROFILE
62344
 
  u64 start;                 /* CPU clock count at start of opcode */
62345
 
  int origPc;                /* Program counter at start of opcode */
62346
 
#endif
62347
 
  /********************************************************************
62348
 
  ** Automatically generated code
62349
 
  **
62350
 
  ** The following union is automatically generated by the
62351
 
  ** vdbe-compress.tcl script.  The purpose of this union is to
62352
 
  ** reduce the amount of stack space required by this function.
62353
 
  ** See comments in the vdbe-compress.tcl script for details.
62354
 
  */
62355
 
  union vdbeExecUnion {
62356
 
    struct OP_Yield_stack_vars {
62357
 
      int pcDest;
62358
 
    } aa;
62359
 
    struct OP_Variable_stack_vars {
62360
 
      Mem *pVar;       /* Value being transferred */
62361
 
    } ab;
62362
 
    struct OP_Move_stack_vars {
62363
 
      char *zMalloc;   /* Holding variable for allocated memory */
62364
 
      int n;           /* Number of registers left to copy */
62365
 
      int p1;          /* Register to copy from */
62366
 
      int p2;          /* Register to copy to */
62367
 
    } ac;
62368
 
    struct OP_ResultRow_stack_vars {
62369
 
      Mem *pMem;
62370
 
      int i;
62371
 
    } ad;
62372
 
    struct OP_Concat_stack_vars {
62373
 
      i64 nByte;
62374
 
    } ae;
62375
 
    struct OP_Remainder_stack_vars {
62376
 
      int flags;      /* Combined MEM_* flags from both inputs */
62377
 
      i64 iA;         /* Integer value of left operand */
62378
 
      i64 iB;         /* Integer value of right operand */
62379
 
      double rA;      /* Real value of left operand */
62380
 
      double rB;      /* Real value of right operand */
62381
 
    } af;
62382
 
    struct OP_Function_stack_vars {
62383
 
      int i;
62384
 
      Mem *pArg;
62385
 
      sqlite3_context ctx;
62386
 
      sqlite3_value **apVal;
62387
 
      int n;
62388
 
    } ag;
62389
 
    struct OP_ShiftRight_stack_vars {
62390
 
      i64 iA;
62391
 
      u64 uA;
62392
 
      i64 iB;
62393
 
      u8 op;
62394
 
    } ah;
62395
 
    struct OP_Ge_stack_vars {
62396
 
      int res;            /* Result of the comparison of pIn1 against pIn3 */
62397
 
      char affinity;      /* Affinity to use for comparison */
62398
 
      u16 flags1;         /* Copy of initial value of pIn1->flags */
62399
 
      u16 flags3;         /* Copy of initial value of pIn3->flags */
62400
 
    } ai;
62401
 
    struct OP_Compare_stack_vars {
62402
 
      int n;
62403
 
      int i;
62404
 
      int p1;
62405
 
      int p2;
62406
 
      const KeyInfo *pKeyInfo;
62407
 
      int idx;
62408
 
      CollSeq *pColl;    /* Collating sequence to use on this term */
62409
 
      int bRev;          /* True for DESCENDING sort order */
62410
 
    } aj;
62411
 
    struct OP_Or_stack_vars {
62412
 
      int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
62413
 
      int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
62414
 
    } ak;
62415
 
    struct OP_IfNot_stack_vars {
62416
 
      int c;
62417
 
    } al;
62418
 
    struct OP_Column_stack_vars {
62419
 
      u32 payloadSize;   /* Number of bytes in the record */
62420
 
      i64 payloadSize64; /* Number of bytes in the record */
62421
 
      int p1;            /* P1 value of the opcode */
62422
 
      int p2;            /* column number to retrieve */
62423
 
      VdbeCursor *pC;    /* The VDBE cursor */
62424
 
      char *zRec;        /* Pointer to complete record-data */
62425
 
      BtCursor *pCrsr;   /* The BTree cursor */
62426
 
      u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
62427
 
      u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
62428
 
      int nField;        /* number of fields in the record */
62429
 
      int len;           /* The length of the serialized data for the column */
62430
 
      int i;             /* Loop counter */
62431
 
      char *zData;       /* Part of the record being decoded */
62432
 
      Mem *pDest;        /* Where to write the extracted value */
62433
 
      Mem sMem;          /* For storing the record being decoded */
62434
 
      u8 *zIdx;          /* Index into header */
62435
 
      u8 *zEndHdr;       /* Pointer to first byte after the header */
62436
 
      u32 offset;        /* Offset into the data */
62437
 
      u32 szField;       /* Number of bytes in the content of a field */
62438
 
      int szHdr;         /* Size of the header size field at start of record */
62439
 
      int avail;         /* Number of bytes of available data */
62440
 
      Mem *pReg;         /* PseudoTable input register */
62441
 
    } am;
62442
 
    struct OP_Affinity_stack_vars {
62443
 
      const char *zAffinity;   /* The affinity to be applied */
62444
 
      char cAff;               /* A single character of affinity */
62445
 
    } an;
62446
 
    struct OP_MakeRecord_stack_vars {
62447
 
      u8 *zNewRecord;        /* A buffer to hold the data for the new record */
62448
 
      Mem *pRec;             /* The new record */
62449
 
      u64 nData;             /* Number of bytes of data space */
62450
 
      int nHdr;              /* Number of bytes of header space */
62451
 
      i64 nByte;             /* Data space required for this record */
62452
 
      int nZero;             /* Number of zero bytes at the end of the record */
62453
 
      int nVarint;           /* Number of bytes in a varint */
62454
 
      u32 serial_type;       /* Type field */
62455
 
      Mem *pData0;           /* First field to be combined into the record */
62456
 
      Mem *pLast;            /* Last field of the record */
62457
 
      int nField;            /* Number of fields in the record */
62458
 
      char *zAffinity;       /* The affinity string for the record */
62459
 
      int file_format;       /* File format to use for encoding */
62460
 
      int i;                 /* Space used in zNewRecord[] */
62461
 
      int len;               /* Length of a field */
62462
 
    } ao;
62463
 
    struct OP_Count_stack_vars {
62464
 
      i64 nEntry;
62465
 
      BtCursor *pCrsr;
62466
 
    } ap;
62467
 
    struct OP_Savepoint_stack_vars {
62468
 
      int p1;                         /* Value of P1 operand */
62469
 
      char *zName;                    /* Name of savepoint */
62470
 
      int nName;
62471
 
      Savepoint *pNew;
62472
 
      Savepoint *pSavepoint;
62473
 
      Savepoint *pTmp;
62474
 
      int iSavepoint;
62475
 
      int ii;
62476
 
    } aq;
62477
 
    struct OP_AutoCommit_stack_vars {
62478
 
      int desiredAutoCommit;
62479
 
      int iRollback;
62480
 
      int turnOnAC;
62481
 
    } ar;
62482
 
    struct OP_Transaction_stack_vars {
62483
 
      Btree *pBt;
62484
 
    } as;
62485
 
    struct OP_ReadCookie_stack_vars {
62486
 
      int iMeta;
62487
 
      int iDb;
62488
 
      int iCookie;
62489
 
    } at;
62490
 
    struct OP_SetCookie_stack_vars {
62491
 
      Db *pDb;
62492
 
    } au;
62493
 
    struct OP_VerifyCookie_stack_vars {
62494
 
      int iMeta;
62495
 
      int iGen;
62496
 
      Btree *pBt;
62497
 
    } av;
62498
 
    struct OP_OpenWrite_stack_vars {
62499
 
      int nField;
62500
 
      KeyInfo *pKeyInfo;
62501
 
      int p2;
62502
 
      int iDb;
62503
 
      int wrFlag;
62504
 
      Btree *pX;
62505
 
      VdbeCursor *pCur;
62506
 
      Db *pDb;
62507
 
    } aw;
62508
 
    struct OP_OpenEphemeral_stack_vars {
62509
 
      VdbeCursor *pCx;
62510
 
    } ax;
62511
 
    struct OP_OpenPseudo_stack_vars {
62512
 
      VdbeCursor *pCx;
62513
 
    } ay;
62514
 
    struct OP_SeekGt_stack_vars {
62515
 
      int res;
62516
 
      int oc;
62517
 
      VdbeCursor *pC;
62518
 
      UnpackedRecord r;
62519
 
      int nField;
62520
 
      i64 iKey;      /* The rowid we are to seek to */
62521
 
    } az;
62522
 
    struct OP_Seek_stack_vars {
62523
 
      VdbeCursor *pC;
62524
 
    } ba;
62525
 
    struct OP_Found_stack_vars {
62526
 
      int alreadyExists;
62527
 
      VdbeCursor *pC;
62528
 
      int res;
62529
 
      UnpackedRecord *pIdxKey;
62530
 
      UnpackedRecord r;
62531
 
      char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
62532
 
    } bb;
62533
 
    struct OP_IsUnique_stack_vars {
62534
 
      u16 ii;
62535
 
      VdbeCursor *pCx;
62536
 
      BtCursor *pCrsr;
62537
 
      u16 nField;
62538
 
      Mem *aMx;
62539
 
      UnpackedRecord r;                  /* B-Tree index search key */
62540
 
      i64 R;                             /* Rowid stored in register P3 */
62541
 
    } bc;
62542
 
    struct OP_NotExists_stack_vars {
62543
 
      VdbeCursor *pC;
62544
 
      BtCursor *pCrsr;
62545
 
      int res;
62546
 
      u64 iKey;
62547
 
    } bd;
62548
 
    struct OP_NewRowid_stack_vars {
62549
 
      i64 v;                 /* The new rowid */
62550
 
      VdbeCursor *pC;        /* Cursor of table to get the new rowid */
62551
 
      int res;               /* Result of an sqlite3BtreeLast() */
62552
 
      int cnt;               /* Counter to limit the number of searches */
62553
 
      Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
62554
 
      VdbeFrame *pFrame;     /* Root frame of VDBE */
62555
 
    } be;
62556
 
    struct OP_InsertInt_stack_vars {
62557
 
      Mem *pData;       /* MEM cell holding data for the record to be inserted */
62558
 
      Mem *pKey;        /* MEM cell holding key  for the record */
62559
 
      i64 iKey;         /* The integer ROWID or key for the record to be inserted */
62560
 
      VdbeCursor *pC;   /* Cursor to table into which insert is written */
62561
 
      int nZero;        /* Number of zero-bytes to append */
62562
 
      int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
62563
 
      const char *zDb;  /* database name - used by the update hook */
62564
 
      const char *zTbl; /* Table name - used by the opdate hook */
62565
 
      int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
62566
 
    } bf;
62567
 
    struct OP_Delete_stack_vars {
62568
 
      i64 iKey;
62569
 
      VdbeCursor *pC;
62570
 
    } bg;
62571
 
    struct OP_RowData_stack_vars {
62572
 
      VdbeCursor *pC;
62573
 
      BtCursor *pCrsr;
62574
 
      u32 n;
62575
 
      i64 n64;
62576
 
    } bh;
62577
 
    struct OP_Rowid_stack_vars {
62578
 
      VdbeCursor *pC;
62579
 
      i64 v;
62580
 
      sqlite3_vtab *pVtab;
62581
 
      const sqlite3_module *pModule;
62582
 
    } bi;
62583
 
    struct OP_NullRow_stack_vars {
62584
 
      VdbeCursor *pC;
62585
 
    } bj;
62586
 
    struct OP_Last_stack_vars {
62587
 
      VdbeCursor *pC;
62588
 
      BtCursor *pCrsr;
62589
 
      int res;
62590
 
    } bk;
62591
 
    struct OP_Rewind_stack_vars {
62592
 
      VdbeCursor *pC;
62593
 
      BtCursor *pCrsr;
62594
 
      int res;
62595
 
    } bl;
62596
 
    struct OP_Next_stack_vars {
62597
 
      VdbeCursor *pC;
62598
 
      BtCursor *pCrsr;
62599
 
      int res;
62600
 
    } bm;
62601
 
    struct OP_IdxInsert_stack_vars {
62602
 
      VdbeCursor *pC;
62603
 
      BtCursor *pCrsr;
62604
 
      int nKey;
62605
 
      const char *zKey;
62606
 
    } bn;
62607
 
    struct OP_IdxDelete_stack_vars {
62608
 
      VdbeCursor *pC;
62609
 
      BtCursor *pCrsr;
62610
 
      int res;
62611
 
      UnpackedRecord r;
62612
 
    } bo;
62613
 
    struct OP_IdxRowid_stack_vars {
62614
 
      BtCursor *pCrsr;
62615
 
      VdbeCursor *pC;
62616
 
      i64 rowid;
62617
 
    } bp;
62618
 
    struct OP_IdxGE_stack_vars {
62619
 
      VdbeCursor *pC;
62620
 
      int res;
62621
 
      UnpackedRecord r;
62622
 
    } bq;
62623
 
    struct OP_Destroy_stack_vars {
62624
 
      int iMoved;
62625
 
      int iCnt;
62626
 
      Vdbe *pVdbe;
62627
 
      int iDb;
62628
 
    } br;
62629
 
    struct OP_Clear_stack_vars {
62630
 
      int nChange;
62631
 
    } bs;
62632
 
    struct OP_CreateTable_stack_vars {
62633
 
      int pgno;
62634
 
      int flags;
62635
 
      Db *pDb;
62636
 
    } bt;
62637
 
    struct OP_ParseSchema_stack_vars {
62638
 
      int iDb;
62639
 
      const char *zMaster;
62640
 
      char *zSql;
62641
 
      InitData initData;
62642
 
    } bu;
62643
 
    struct OP_IntegrityCk_stack_vars {
62644
 
      int nRoot;      /* Number of tables to check.  (Number of root pages.) */
62645
 
      int *aRoot;     /* Array of rootpage numbers for tables to be checked */
62646
 
      int j;          /* Loop counter */
62647
 
      int nErr;       /* Number of errors reported */
62648
 
      char *z;        /* Text of the error report */
62649
 
      Mem *pnErr;     /* Register keeping track of errors remaining */
62650
 
    } bv;
62651
 
    struct OP_RowSetRead_stack_vars {
62652
 
      i64 val;
62653
 
    } bw;
62654
 
    struct OP_RowSetTest_stack_vars {
62655
 
      int iSet;
62656
 
      int exists;
62657
 
    } bx;
62658
 
    struct OP_Program_stack_vars {
62659
 
      int nMem;               /* Number of memory registers for sub-program */
62660
 
      int nByte;              /* Bytes of runtime space required for sub-program */
62661
 
      Mem *pRt;               /* Register to allocate runtime space */
62662
 
      Mem *pMem;              /* Used to iterate through memory cells */
62663
 
      Mem *pEnd;              /* Last memory cell in new array */
62664
 
      VdbeFrame *pFrame;      /* New vdbe frame to execute in */
62665
 
      SubProgram *pProgram;   /* Sub-program to execute */
62666
 
      void *t;                /* Token identifying trigger */
62667
 
    } by;
62668
 
    struct OP_Param_stack_vars {
62669
 
      VdbeFrame *pFrame;
62670
 
      Mem *pIn;
62671
 
    } bz;
62672
 
    struct OP_MemMax_stack_vars {
62673
 
      Mem *pIn1;
62674
 
      VdbeFrame *pFrame;
62675
 
    } ca;
62676
 
    struct OP_AggStep_stack_vars {
62677
 
      int n;
62678
 
      int i;
62679
 
      Mem *pMem;
62680
 
      Mem *pRec;
62681
 
      sqlite3_context ctx;
62682
 
      sqlite3_value **apVal;
62683
 
    } cb;
62684
 
    struct OP_AggFinal_stack_vars {
62685
 
      Mem *pMem;
62686
 
    } cc;
62687
 
    struct OP_Checkpoint_stack_vars {
62688
 
      int i;                          /* Loop counter */
62689
 
      int aRes[3];                    /* Results */
62690
 
      Mem *pMem;                      /* Write results here */
62691
 
    } cd;
62692
 
    struct OP_JournalMode_stack_vars {
62693
 
      Btree *pBt;                     /* Btree to change journal mode of */
62694
 
      Pager *pPager;                  /* Pager associated with pBt */
62695
 
      int eNew;                       /* New journal mode */
62696
 
      int eOld;                       /* The old journal mode */
62697
 
      const char *zFilename;          /* Name of database file for pPager */
62698
 
    } ce;
62699
 
    struct OP_IncrVacuum_stack_vars {
62700
 
      Btree *pBt;
62701
 
    } cf;
62702
 
    struct OP_VBegin_stack_vars {
62703
 
      VTable *pVTab;
62704
 
    } cg;
62705
 
    struct OP_VOpen_stack_vars {
62706
 
      VdbeCursor *pCur;
62707
 
      sqlite3_vtab_cursor *pVtabCursor;
62708
 
      sqlite3_vtab *pVtab;
62709
 
      sqlite3_module *pModule;
62710
 
    } ch;
62711
 
    struct OP_VFilter_stack_vars {
62712
 
      int nArg;
62713
 
      int iQuery;
62714
 
      const sqlite3_module *pModule;
62715
 
      Mem *pQuery;
62716
 
      Mem *pArgc;
62717
 
      sqlite3_vtab_cursor *pVtabCursor;
62718
 
      sqlite3_vtab *pVtab;
62719
 
      VdbeCursor *pCur;
62720
 
      int res;
62721
 
      int i;
62722
 
      Mem **apArg;
62723
 
    } ci;
62724
 
    struct OP_VColumn_stack_vars {
62725
 
      sqlite3_vtab *pVtab;
62726
 
      const sqlite3_module *pModule;
62727
 
      Mem *pDest;
62728
 
      sqlite3_context sContext;
62729
 
    } cj;
62730
 
    struct OP_VNext_stack_vars {
62731
 
      sqlite3_vtab *pVtab;
62732
 
      const sqlite3_module *pModule;
62733
 
      int res;
62734
 
      VdbeCursor *pCur;
62735
 
    } ck;
62736
 
    struct OP_VRename_stack_vars {
62737
 
      sqlite3_vtab *pVtab;
62738
 
      Mem *pName;
62739
 
    } cl;
62740
 
    struct OP_VUpdate_stack_vars {
62741
 
      sqlite3_vtab *pVtab;
62742
 
      sqlite3_module *pModule;
62743
 
      int nArg;
62744
 
      int i;
62745
 
      sqlite_int64 rowid;
62746
 
      Mem **apArg;
62747
 
      Mem *pX;
62748
 
    } cm;
62749
 
    struct OP_Trace_stack_vars {
62750
 
      char *zTrace;
62751
 
    } cn;
62752
 
  } u;
62753
 
  /* End automatically generated code
62754
 
  ********************************************************************/
62755
 
 
62756
 
  assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
62757
 
  sqlite3VdbeEnter(p);
62758
 
  if( p->rc==SQLITE_NOMEM ){
62759
 
    /* This happens if a malloc() inside a call to sqlite3_column_text() or
62760
 
    ** sqlite3_column_text16() failed.  */
62761
 
    goto no_mem;
62762
 
  }
62763
 
  assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
62764
 
  p->rc = SQLITE_OK;
62765
 
  assert( p->explain==0 );
62766
 
  p->pResultSet = 0;
62767
 
  db->busyHandler.nBusy = 0;
62768
 
  CHECK_FOR_INTERRUPT;
62769
 
  sqlite3VdbeIOTraceSql(p);
62770
 
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
62771
 
  checkProgress = db->xProgress!=0;
62772
 
#endif
62773
 
#ifdef SQLITE_DEBUG
62774
 
  sqlite3BeginBenignMalloc();
62775
 
  if( p->pc==0  && (p->db->flags & SQLITE_VdbeListing)!=0 ){
62776
 
    int i;
62777
 
    printf("VDBE Program Listing:\n");
62778
 
    sqlite3VdbePrintSql(p);
62779
 
    for(i=0; i<p->nOp; i++){
62780
 
      sqlite3VdbePrintOp(stdout, i, &aOp[i]);
62781
 
    }
62782
 
  }
62783
 
  sqlite3EndBenignMalloc();
62784
 
#endif
62785
 
  for(pc=p->pc; rc==SQLITE_OK; pc++){
62786
 
    assert( pc>=0 && pc<p->nOp );
62787
 
    if( db->mallocFailed ) goto no_mem;
62788
 
#ifdef VDBE_PROFILE
62789
 
    origPc = pc;
62790
 
    start = sqlite3Hwtime();
62791
 
#endif
62792
 
    pOp = &aOp[pc];
62793
 
 
62794
 
    /* Only allow tracing if SQLITE_DEBUG is defined.
62795
 
    */
62796
 
#ifdef SQLITE_DEBUG
62797
 
    if( p->trace ){
62798
 
      if( pc==0 ){
62799
 
        printf("VDBE Execution Trace:\n");
62800
 
        sqlite3VdbePrintSql(p);
62801
 
      }
62802
 
      sqlite3VdbePrintOp(p->trace, pc, pOp);
62803
 
    }
62804
 
#endif
62805
 
      
62806
 
 
62807
 
    /* Check to see if we need to simulate an interrupt.  This only happens
62808
 
    ** if we have a special test build.
62809
 
    */
62810
 
#ifdef SQLITE_TEST
62811
 
    if( sqlite3_interrupt_count>0 ){
62812
 
      sqlite3_interrupt_count--;
62813
 
      if( sqlite3_interrupt_count==0 ){
62814
 
        sqlite3_interrupt(db);
62815
 
      }
62816
 
    }
62817
 
#endif
62818
 
 
62819
 
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
62820
 
    /* Call the progress callback if it is configured and the required number
62821
 
    ** of VDBE ops have been executed (either since this invocation of
62822
 
    ** sqlite3VdbeExec() or since last time the progress callback was called).
62823
 
    ** If the progress callback returns non-zero, exit the virtual machine with
62824
 
    ** a return code SQLITE_ABORT.
62825
 
    */
62826
 
    if( checkProgress ){
62827
 
      if( db->nProgressOps==nProgressOps ){
62828
 
        int prc;
62829
 
        prc = db->xProgress(db->pProgressArg);
62830
 
        if( prc!=0 ){
62831
 
          rc = SQLITE_INTERRUPT;
62832
 
          goto vdbe_error_halt;
62833
 
        }
62834
 
        nProgressOps = 0;
62835
 
      }
62836
 
      nProgressOps++;
62837
 
    }
62838
 
#endif
62839
 
 
62840
 
    /* On any opcode with the "out2-prerelase" tag, free any
62841
 
    ** external allocations out of mem[p2] and set mem[p2] to be
62842
 
    ** an undefined integer.  Opcodes will either fill in the integer
62843
 
    ** value or convert mem[p2] to a different type.
62844
 
    */
62845
 
    assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
62846
 
    if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
62847
 
      assert( pOp->p2>0 );
62848
 
      assert( pOp->p2<=p->nMem );
62849
 
      pOut = &aMem[pOp->p2];
62850
 
      memAboutToChange(p, pOut);
62851
 
      sqlite3VdbeMemReleaseExternal(pOut);
62852
 
      pOut->flags = MEM_Int;
62853
 
    }
62854
 
 
62855
 
    /* Sanity checking on other operands */
62856
 
#ifdef SQLITE_DEBUG
62857
 
    if( (pOp->opflags & OPFLG_IN1)!=0 ){
62858
 
      assert( pOp->p1>0 );
62859
 
      assert( pOp->p1<=p->nMem );
62860
 
      assert( memIsValid(&aMem[pOp->p1]) );
62861
 
      REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
62862
 
    }
62863
 
    if( (pOp->opflags & OPFLG_IN2)!=0 ){
62864
 
      assert( pOp->p2>0 );
62865
 
      assert( pOp->p2<=p->nMem );
62866
 
      assert( memIsValid(&aMem[pOp->p2]) );
62867
 
      REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
62868
 
    }
62869
 
    if( (pOp->opflags & OPFLG_IN3)!=0 ){
62870
 
      assert( pOp->p3>0 );
62871
 
      assert( pOp->p3<=p->nMem );
62872
 
      assert( memIsValid(&aMem[pOp->p3]) );
62873
 
      REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
62874
 
    }
62875
 
    if( (pOp->opflags & OPFLG_OUT2)!=0 ){
62876
 
      assert( pOp->p2>0 );
62877
 
      assert( pOp->p2<=p->nMem );
62878
 
      memAboutToChange(p, &aMem[pOp->p2]);
62879
 
    }
62880
 
    if( (pOp->opflags & OPFLG_OUT3)!=0 ){
62881
 
      assert( pOp->p3>0 );
62882
 
      assert( pOp->p3<=p->nMem );
62883
 
      memAboutToChange(p, &aMem[pOp->p3]);
62884
 
    }
62885
 
#endif
62886
 
  
62887
 
    switch( pOp->opcode ){
62888
 
 
62889
 
/*****************************************************************************
62890
 
** What follows is a massive switch statement where each case implements a
62891
 
** separate instruction in the virtual machine.  If we follow the usual
62892
 
** indentation conventions, each case should be indented by 6 spaces.  But
62893
 
** that is a lot of wasted space on the left margin.  So the code within
62894
 
** the switch statement will break with convention and be flush-left. Another
62895
 
** big comment (similar to this one) will mark the point in the code where
62896
 
** we transition back to normal indentation.
62897
 
**
62898
 
** The formatting of each case is important.  The makefile for SQLite
62899
 
** generates two C files "opcodes.h" and "opcodes.c" by scanning this
62900
 
** file looking for lines that begin with "case OP_".  The opcodes.h files
62901
 
** will be filled with #defines that give unique integer values to each
62902
 
** opcode and the opcodes.c file is filled with an array of strings where
62903
 
** each string is the symbolic name for the corresponding opcode.  If the
62904
 
** case statement is followed by a comment of the form "/# same as ... #/"
62905
 
** that comment is used to determine the particular value of the opcode.
62906
 
**
62907
 
** Other keywords in the comment that follows each case are used to
62908
 
** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
62909
 
** Keywords include: in1, in2, in3, out2_prerelease, out2, out3.  See
62910
 
** the mkopcodeh.awk script for additional information.
62911
 
**
62912
 
** Documentation about VDBE opcodes is generated by scanning this file
62913
 
** for lines of that contain "Opcode:".  That line and all subsequent
62914
 
** comment lines are used in the generation of the opcode.html documentation
62915
 
** file.
62916
 
**
62917
 
** SUMMARY:
62918
 
**
62919
 
**     Formatting is important to scripts that scan this file.
62920
 
**     Do not deviate from the formatting style currently in use.
62921
 
**
62922
 
*****************************************************************************/
62923
 
 
62924
 
/* Opcode:  Goto * P2 * * *
62925
 
**
62926
 
** An unconditional jump to address P2.
62927
 
** The next instruction executed will be 
62928
 
** the one at index P2 from the beginning of
62929
 
** the program.
62930
 
*/
62931
 
case OP_Goto: {             /* jump */
62932
 
  CHECK_FOR_INTERRUPT;
62933
 
  pc = pOp->p2 - 1;
62934
 
  break;
62935
 
}
62936
 
 
62937
 
/* Opcode:  Gosub P1 P2 * * *
62938
 
**
62939
 
** Write the current address onto register P1
62940
 
** and then jump to address P2.
62941
 
*/
62942
 
case OP_Gosub: {            /* jump, in1 */
62943
 
  pIn1 = &aMem[pOp->p1];
62944
 
  assert( (pIn1->flags & MEM_Dyn)==0 );
62945
 
  memAboutToChange(p, pIn1);
62946
 
  pIn1->flags = MEM_Int;
62947
 
  pIn1->u.i = pc;
62948
 
  REGISTER_TRACE(pOp->p1, pIn1);
62949
 
  pc = pOp->p2 - 1;
62950
 
  break;
62951
 
}
62952
 
 
62953
 
/* Opcode:  Return P1 * * * *
62954
 
**
62955
 
** Jump to the next instruction after the address in register P1.
62956
 
*/
62957
 
case OP_Return: {           /* in1 */
62958
 
  pIn1 = &aMem[pOp->p1];
62959
 
  assert( pIn1->flags & MEM_Int );
62960
 
  pc = (int)pIn1->u.i;
62961
 
  break;
62962
 
}
62963
 
 
62964
 
/* Opcode:  Yield P1 * * * *
62965
 
**
62966
 
** Swap the program counter with the value in register P1.
62967
 
*/
62968
 
case OP_Yield: {            /* in1 */
62969
 
#if 0  /* local variables moved into u.aa */
62970
 
  int pcDest;
62971
 
#endif /* local variables moved into u.aa */
62972
 
  pIn1 = &aMem[pOp->p1];
62973
 
  assert( (pIn1->flags & MEM_Dyn)==0 );
62974
 
  pIn1->flags = MEM_Int;
62975
 
  u.aa.pcDest = (int)pIn1->u.i;
62976
 
  pIn1->u.i = pc;
62977
 
  REGISTER_TRACE(pOp->p1, pIn1);
62978
 
  pc = u.aa.pcDest;
62979
 
  break;
62980
 
}
62981
 
 
62982
 
/* Opcode:  HaltIfNull  P1 P2 P3 P4 *
62983
 
**
62984
 
** Check the value in register P3.  If is is NULL then Halt using
62985
 
** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
62986
 
** value in register P3 is not NULL, then this routine is a no-op.
62987
 
*/
62988
 
case OP_HaltIfNull: {      /* in3 */
62989
 
  pIn3 = &aMem[pOp->p3];
62990
 
  if( (pIn3->flags & MEM_Null)==0 ) break;
62991
 
  /* Fall through into OP_Halt */
62992
 
}
62993
 
 
62994
 
/* Opcode:  Halt P1 P2 * P4 *
62995
 
**
62996
 
** Exit immediately.  All open cursors, etc are closed
62997
 
** automatically.
62998
 
**
62999
 
** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
63000
 
** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
63001
 
** For errors, it can be some other value.  If P1!=0 then P2 will determine
63002
 
** whether or not to rollback the current transaction.  Do not rollback
63003
 
** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
63004
 
** then back out all changes that have occurred during this execution of the
63005
 
** VDBE, but do not rollback the transaction. 
63006
 
**
63007
 
** If P4 is not null then it is an error message string.
63008
 
**
63009
 
** There is an implied "Halt 0 0 0" instruction inserted at the very end of
63010
 
** every program.  So a jump past the last instruction of the program
63011
 
** is the same as executing Halt.
63012
 
*/
63013
 
case OP_Halt: {
63014
 
  if( pOp->p1==SQLITE_OK && p->pFrame ){
63015
 
    /* Halt the sub-program. Return control to the parent frame. */
63016
 
    VdbeFrame *pFrame = p->pFrame;
63017
 
    p->pFrame = pFrame->pParent;
63018
 
    p->nFrame--;
63019
 
    sqlite3VdbeSetChanges(db, p->nChange);
63020
 
    pc = sqlite3VdbeFrameRestore(pFrame);
63021
 
    if( pOp->p2==OE_Ignore ){
63022
 
      /* Instruction pc is the OP_Program that invoked the sub-program 
63023
 
      ** currently being halted. If the p2 instruction of this OP_Halt
63024
 
      ** instruction is set to OE_Ignore, then the sub-program is throwing
63025
 
      ** an IGNORE exception. In this case jump to the address specified
63026
 
      ** as the p2 of the calling OP_Program.  */
63027
 
      pc = p->aOp[pc].p2-1;
63028
 
    }
63029
 
    aOp = p->aOp;
63030
 
    aMem = p->aMem;
63031
 
    break;
63032
 
  }
63033
 
 
63034
 
  p->rc = pOp->p1;
63035
 
  p->errorAction = (u8)pOp->p2;
63036
 
  p->pc = pc;
63037
 
  if( pOp->p4.z ){
63038
 
    assert( p->rc!=SQLITE_OK );
63039
 
    sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
63040
 
    testcase( sqlite3GlobalConfig.xLog!=0 );
63041
 
    sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
63042
 
  }else if( p->rc ){
63043
 
    testcase( sqlite3GlobalConfig.xLog!=0 );
63044
 
    sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql);
63045
 
  }
63046
 
  rc = sqlite3VdbeHalt(p);
63047
 
  assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
63048
 
  if( rc==SQLITE_BUSY ){
63049
 
    p->rc = rc = SQLITE_BUSY;
63050
 
  }else{
63051
 
    assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
63052
 
    assert( rc==SQLITE_OK || db->nDeferredCons>0 );
63053
 
    rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
63054
 
  }
63055
 
  goto vdbe_return;
63056
 
}
63057
 
 
63058
 
/* Opcode: Integer P1 P2 * * *
63059
 
**
63060
 
** The 32-bit integer value P1 is written into register P2.
63061
 
*/
63062
 
case OP_Integer: {         /* out2-prerelease */
63063
 
  pOut->u.i = pOp->p1;
63064
 
  break;
63065
 
}
63066
 
 
63067
 
/* Opcode: Int64 * P2 * P4 *
63068
 
**
63069
 
** P4 is a pointer to a 64-bit integer value.
63070
 
** Write that value into register P2.
63071
 
*/
63072
 
case OP_Int64: {           /* out2-prerelease */
63073
 
  assert( pOp->p4.pI64!=0 );
63074
 
  pOut->u.i = *pOp->p4.pI64;
63075
 
  break;
63076
 
}
63077
 
 
63078
 
#ifndef SQLITE_OMIT_FLOATING_POINT
63079
 
/* Opcode: Real * P2 * P4 *
63080
 
**
63081
 
** P4 is a pointer to a 64-bit floating point value.
63082
 
** Write that value into register P2.
63083
 
*/
63084
 
case OP_Real: {            /* same as TK_FLOAT, out2-prerelease */
63085
 
  pOut->flags = MEM_Real;
63086
 
  assert( !sqlite3IsNaN(*pOp->p4.pReal) );
63087
 
  pOut->r = *pOp->p4.pReal;
63088
 
  break;
63089
 
}
63090
 
#endif
63091
 
 
63092
 
/* Opcode: String8 * P2 * P4 *
63093
 
**
63094
 
** P4 points to a nul terminated UTF-8 string. This opcode is transformed 
63095
 
** into an OP_String before it is executed for the first time.
63096
 
*/
63097
 
case OP_String8: {         /* same as TK_STRING, out2-prerelease */
63098
 
  assert( pOp->p4.z!=0 );
63099
 
  pOp->opcode = OP_String;
63100
 
  pOp->p1 = sqlite3Strlen30(pOp->p4.z);
63101
 
 
63102
 
#ifndef SQLITE_OMIT_UTF16
63103
 
  if( encoding!=SQLITE_UTF8 ){
63104
 
    rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
63105
 
    if( rc==SQLITE_TOOBIG ) goto too_big;
63106
 
    if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
63107
 
    assert( pOut->zMalloc==pOut->z );
63108
 
    assert( pOut->flags & MEM_Dyn );
63109
 
    pOut->zMalloc = 0;
63110
 
    pOut->flags |= MEM_Static;
63111
 
    pOut->flags &= ~MEM_Dyn;
63112
 
    if( pOp->p4type==P4_DYNAMIC ){
63113
 
      sqlite3DbFree(db, pOp->p4.z);
63114
 
    }
63115
 
    pOp->p4type = P4_DYNAMIC;
63116
 
    pOp->p4.z = pOut->z;
63117
 
    pOp->p1 = pOut->n;
63118
 
  }
63119
 
#endif
63120
 
  if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
63121
 
    goto too_big;
63122
 
  }
63123
 
  /* Fall through to the next case, OP_String */
63124
 
}
63125
 
  
63126
 
/* Opcode: String P1 P2 * P4 *
63127
 
**
63128
 
** The string value P4 of length P1 (bytes) is stored in register P2.
63129
 
*/
63130
 
case OP_String: {          /* out2-prerelease */
63131
 
  assert( pOp->p4.z!=0 );
63132
 
  pOut->flags = MEM_Str|MEM_Static|MEM_Term;
63133
 
  pOut->z = pOp->p4.z;
63134
 
  pOut->n = pOp->p1;
63135
 
  pOut->enc = encoding;
63136
 
  UPDATE_MAX_BLOBSIZE(pOut);
63137
 
  break;
63138
 
}
63139
 
 
63140
 
/* Opcode: Null * P2 * * *
63141
 
**
63142
 
** Write a NULL into register P2.
63143
 
*/
63144
 
case OP_Null: {           /* out2-prerelease */
63145
 
  pOut->flags = MEM_Null;
63146
 
  break;
63147
 
}
63148
 
 
63149
 
 
63150
 
/* Opcode: Blob P1 P2 * P4
63151
 
**
63152
 
** P4 points to a blob of data P1 bytes long.  Store this
63153
 
** blob in register P2.
63154
 
*/
63155
 
case OP_Blob: {                /* out2-prerelease */
63156
 
  assert( pOp->p1 <= SQLITE_MAX_LENGTH );
63157
 
  sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
63158
 
  pOut->enc = encoding;
63159
 
  UPDATE_MAX_BLOBSIZE(pOut);
63160
 
  break;
63161
 
}
63162
 
 
63163
 
/* Opcode: Variable P1 P2 * P4 *
63164
 
**
63165
 
** Transfer the values of bound parameter P1 into register P2
63166
 
**
63167
 
** If the parameter is named, then its name appears in P4 and P3==1.
63168
 
** The P4 value is used by sqlite3_bind_parameter_name().
63169
 
*/
63170
 
case OP_Variable: {            /* out2-prerelease */
63171
 
#if 0  /* local variables moved into u.ab */
63172
 
  Mem *pVar;       /* Value being transferred */
63173
 
#endif /* local variables moved into u.ab */
63174
 
 
63175
 
  assert( pOp->p1>0 && pOp->p1<=p->nVar );
63176
 
  u.ab.pVar = &p->aVar[pOp->p1 - 1];
63177
 
  if( sqlite3VdbeMemTooBig(u.ab.pVar) ){
63178
 
    goto too_big;
63179
 
  }
63180
 
  sqlite3VdbeMemShallowCopy(pOut, u.ab.pVar, MEM_Static);
63181
 
  UPDATE_MAX_BLOBSIZE(pOut);
63182
 
  break;
63183
 
}
63184
 
 
63185
 
/* Opcode: Move P1 P2 P3 * *
63186
 
**
63187
 
** Move the values in register P1..P1+P3-1 over into
63188
 
** registers P2..P2+P3-1.  Registers P1..P1+P1-1 are
63189
 
** left holding a NULL.  It is an error for register ranges
63190
 
** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
63191
 
*/
63192
 
case OP_Move: {
63193
 
#if 0  /* local variables moved into u.ac */
63194
 
  char *zMalloc;   /* Holding variable for allocated memory */
63195
 
  int n;           /* Number of registers left to copy */
63196
 
  int p1;          /* Register to copy from */
63197
 
  int p2;          /* Register to copy to */
63198
 
#endif /* local variables moved into u.ac */
63199
 
 
63200
 
  u.ac.n = pOp->p3;
63201
 
  u.ac.p1 = pOp->p1;
63202
 
  u.ac.p2 = pOp->p2;
63203
 
  assert( u.ac.n>0 && u.ac.p1>0 && u.ac.p2>0 );
63204
 
  assert( u.ac.p1+u.ac.n<=u.ac.p2 || u.ac.p2+u.ac.n<=u.ac.p1 );
63205
 
 
63206
 
  pIn1 = &aMem[u.ac.p1];
63207
 
  pOut = &aMem[u.ac.p2];
63208
 
  while( u.ac.n-- ){
63209
 
    assert( pOut<=&aMem[p->nMem] );
63210
 
    assert( pIn1<=&aMem[p->nMem] );
63211
 
    assert( memIsValid(pIn1) );
63212
 
    memAboutToChange(p, pOut);
63213
 
    u.ac.zMalloc = pOut->zMalloc;
63214
 
    pOut->zMalloc = 0;
63215
 
    sqlite3VdbeMemMove(pOut, pIn1);
63216
 
    pIn1->zMalloc = u.ac.zMalloc;
63217
 
    REGISTER_TRACE(u.ac.p2++, pOut);
63218
 
    pIn1++;
63219
 
    pOut++;
63220
 
  }
63221
 
  break;
63222
 
}
63223
 
 
63224
 
/* Opcode: Copy P1 P2 * * *
63225
 
**
63226
 
** Make a copy of register P1 into register P2.
63227
 
**
63228
 
** This instruction makes a deep copy of the value.  A duplicate
63229
 
** is made of any string or blob constant.  See also OP_SCopy.
63230
 
*/
63231
 
case OP_Copy: {             /* in1, out2 */
63232
 
  pIn1 = &aMem[pOp->p1];
63233
 
  pOut = &aMem[pOp->p2];
63234
 
  assert( pOut!=pIn1 );
63235
 
  sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
63236
 
  Deephemeralize(pOut);
63237
 
  REGISTER_TRACE(pOp->p2, pOut);
63238
 
  break;
63239
 
}
63240
 
 
63241
 
/* Opcode: SCopy P1 P2 * * *
63242
 
**
63243
 
** Make a shallow copy of register P1 into register P2.
63244
 
**
63245
 
** This instruction makes a shallow copy of the value.  If the value
63246
 
** is a string or blob, then the copy is only a pointer to the
63247
 
** original and hence if the original changes so will the copy.
63248
 
** Worse, if the original is deallocated, the copy becomes invalid.
63249
 
** Thus the program must guarantee that the original will not change
63250
 
** during the lifetime of the copy.  Use OP_Copy to make a complete
63251
 
** copy.
63252
 
*/
63253
 
case OP_SCopy: {            /* in1, out2 */
63254
 
  pIn1 = &aMem[pOp->p1];
63255
 
  pOut = &aMem[pOp->p2];
63256
 
  assert( pOut!=pIn1 );
63257
 
  sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
63258
 
#ifdef SQLITE_DEBUG
63259
 
  if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
63260
 
#endif
63261
 
  REGISTER_TRACE(pOp->p2, pOut);
63262
 
  break;
63263
 
}
63264
 
 
63265
 
/* Opcode: ResultRow P1 P2 * * *
63266
 
**
63267
 
** The registers P1 through P1+P2-1 contain a single row of
63268
 
** results. This opcode causes the sqlite3_step() call to terminate
63269
 
** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
63270
 
** structure to provide access to the top P1 values as the result
63271
 
** row.
63272
 
*/
63273
 
case OP_ResultRow: {
63274
 
#if 0  /* local variables moved into u.ad */
63275
 
  Mem *pMem;
63276
 
  int i;
63277
 
#endif /* local variables moved into u.ad */
63278
 
  assert( p->nResColumn==pOp->p2 );
63279
 
  assert( pOp->p1>0 );
63280
 
  assert( pOp->p1+pOp->p2<=p->nMem+1 );
63281
 
 
63282
 
  /* If this statement has violated immediate foreign key constraints, do
63283
 
  ** not return the number of rows modified. And do not RELEASE the statement
63284
 
  ** transaction. It needs to be rolled back.  */
63285
 
  if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
63286
 
    assert( db->flags&SQLITE_CountRows );
63287
 
    assert( p->usesStmtJournal );
63288
 
    break;
63289
 
  }
63290
 
 
63291
 
  /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
63292
 
  ** DML statements invoke this opcode to return the number of rows
63293
 
  ** modified to the user. This is the only way that a VM that
63294
 
  ** opens a statement transaction may invoke this opcode.
63295
 
  **
63296
 
  ** In case this is such a statement, close any statement transaction
63297
 
  ** opened by this VM before returning control to the user. This is to
63298
 
  ** ensure that statement-transactions are always nested, not overlapping.
63299
 
  ** If the open statement-transaction is not closed here, then the user
63300
 
  ** may step another VM that opens its own statement transaction. This
63301
 
  ** may lead to overlapping statement transactions.
63302
 
  **
63303
 
  ** The statement transaction is never a top-level transaction.  Hence
63304
 
  ** the RELEASE call below can never fail.
63305
 
  */
63306
 
  assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
63307
 
  rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
63308
 
  if( NEVER(rc!=SQLITE_OK) ){
63309
 
    break;
63310
 
  }
63311
 
 
63312
 
  /* Invalidate all ephemeral cursor row caches */
63313
 
  p->cacheCtr = (p->cacheCtr + 2)|1;
63314
 
 
63315
 
  /* Make sure the results of the current row are \000 terminated
63316
 
  ** and have an assigned type.  The results are de-ephemeralized as
63317
 
  ** as side effect.
63318
 
  */
63319
 
  u.ad.pMem = p->pResultSet = &aMem[pOp->p1];
63320
 
  for(u.ad.i=0; u.ad.i<pOp->p2; u.ad.i++){
63321
 
    assert( memIsValid(&u.ad.pMem[u.ad.i]) );
63322
 
    Deephemeralize(&u.ad.pMem[u.ad.i]);
63323
 
    assert( (u.ad.pMem[u.ad.i].flags & MEM_Ephem)==0
63324
 
            || (u.ad.pMem[u.ad.i].flags & (MEM_Str|MEM_Blob))==0 );
63325
 
    sqlite3VdbeMemNulTerminate(&u.ad.pMem[u.ad.i]);
63326
 
    sqlite3VdbeMemStoreType(&u.ad.pMem[u.ad.i]);
63327
 
    REGISTER_TRACE(pOp->p1+u.ad.i, &u.ad.pMem[u.ad.i]);
63328
 
  }
63329
 
  if( db->mallocFailed ) goto no_mem;
63330
 
 
63331
 
  /* Return SQLITE_ROW
63332
 
  */
63333
 
  p->pc = pc + 1;
63334
 
  rc = SQLITE_ROW;
63335
 
  goto vdbe_return;
63336
 
}
63337
 
 
63338
 
/* Opcode: Concat P1 P2 P3 * *
63339
 
**
63340
 
** Add the text in register P1 onto the end of the text in
63341
 
** register P2 and store the result in register P3.
63342
 
** If either the P1 or P2 text are NULL then store NULL in P3.
63343
 
**
63344
 
**   P3 = P2 || P1
63345
 
**
63346
 
** It is illegal for P1 and P3 to be the same register. Sometimes,
63347
 
** if P3 is the same register as P2, the implementation is able
63348
 
** to avoid a memcpy().
63349
 
*/
63350
 
case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
63351
 
#if 0  /* local variables moved into u.ae */
63352
 
  i64 nByte;
63353
 
#endif /* local variables moved into u.ae */
63354
 
 
63355
 
  pIn1 = &aMem[pOp->p1];
63356
 
  pIn2 = &aMem[pOp->p2];
63357
 
  pOut = &aMem[pOp->p3];
63358
 
  assert( pIn1!=pOut );
63359
 
  if( (pIn1->flags | pIn2->flags) & MEM_Null ){
63360
 
    sqlite3VdbeMemSetNull(pOut);
63361
 
    break;
63362
 
  }
63363
 
  if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
63364
 
  Stringify(pIn1, encoding);
63365
 
  Stringify(pIn2, encoding);
63366
 
  u.ae.nByte = pIn1->n + pIn2->n;
63367
 
  if( u.ae.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
63368
 
    goto too_big;
63369
 
  }
63370
 
  MemSetTypeFlag(pOut, MEM_Str);
63371
 
  if( sqlite3VdbeMemGrow(pOut, (int)u.ae.nByte+2, pOut==pIn2) ){
63372
 
    goto no_mem;
63373
 
  }
63374
 
  if( pOut!=pIn2 ){
63375
 
    memcpy(pOut->z, pIn2->z, pIn2->n);
63376
 
  }
63377
 
  memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
63378
 
  pOut->z[u.ae.nByte] = 0;
63379
 
  pOut->z[u.ae.nByte+1] = 0;
63380
 
  pOut->flags |= MEM_Term;
63381
 
  pOut->n = (int)u.ae.nByte;
63382
 
  pOut->enc = encoding;
63383
 
  UPDATE_MAX_BLOBSIZE(pOut);
63384
 
  break;
63385
 
}
63386
 
 
63387
 
/* Opcode: Add P1 P2 P3 * *
63388
 
**
63389
 
** Add the value in register P1 to the value in register P2
63390
 
** and store the result in register P3.
63391
 
** If either input is NULL, the result is NULL.
63392
 
*/
63393
 
/* Opcode: Multiply P1 P2 P3 * *
63394
 
**
63395
 
**
63396
 
** Multiply the value in register P1 by the value in register P2
63397
 
** and store the result in register P3.
63398
 
** If either input is NULL, the result is NULL.
63399
 
*/
63400
 
/* Opcode: Subtract P1 P2 P3 * *
63401
 
**
63402
 
** Subtract the value in register P1 from the value in register P2
63403
 
** and store the result in register P3.
63404
 
** If either input is NULL, the result is NULL.
63405
 
*/
63406
 
/* Opcode: Divide P1 P2 P3 * *
63407
 
**
63408
 
** Divide the value in register P1 by the value in register P2
63409
 
** and store the result in register P3 (P3=P2/P1). If the value in 
63410
 
** register P1 is zero, then the result is NULL. If either input is 
63411
 
** NULL, the result is NULL.
63412
 
*/
63413
 
/* Opcode: Remainder P1 P2 P3 * *
63414
 
**
63415
 
** Compute the remainder after integer division of the value in
63416
 
** register P1 by the value in register P2 and store the result in P3. 
63417
 
** If the value in register P2 is zero the result is NULL.
63418
 
** If either operand is NULL, the result is NULL.
63419
 
*/
63420
 
case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
63421
 
case OP_Subtract:              /* same as TK_MINUS, in1, in2, out3 */
63422
 
case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
63423
 
case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
63424
 
case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
63425
 
#if 0  /* local variables moved into u.af */
63426
 
  int flags;      /* Combined MEM_* flags from both inputs */
63427
 
  i64 iA;         /* Integer value of left operand */
63428
 
  i64 iB;         /* Integer value of right operand */
63429
 
  double rA;      /* Real value of left operand */
63430
 
  double rB;      /* Real value of right operand */
63431
 
#endif /* local variables moved into u.af */
63432
 
 
63433
 
  pIn1 = &aMem[pOp->p1];
63434
 
  applyNumericAffinity(pIn1);
63435
 
  pIn2 = &aMem[pOp->p2];
63436
 
  applyNumericAffinity(pIn2);
63437
 
  pOut = &aMem[pOp->p3];
63438
 
  u.af.flags = pIn1->flags | pIn2->flags;
63439
 
  if( (u.af.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
63440
 
  if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
63441
 
    u.af.iA = pIn1->u.i;
63442
 
    u.af.iB = pIn2->u.i;
63443
 
    switch( pOp->opcode ){
63444
 
      case OP_Add:       if( sqlite3AddInt64(&u.af.iB,u.af.iA) ) goto fp_math;  break;
63445
 
      case OP_Subtract:  if( sqlite3SubInt64(&u.af.iB,u.af.iA) ) goto fp_math;  break;
63446
 
      case OP_Multiply:  if( sqlite3MulInt64(&u.af.iB,u.af.iA) ) goto fp_math;  break;
63447
 
      case OP_Divide: {
63448
 
        if( u.af.iA==0 ) goto arithmetic_result_is_null;
63449
 
        if( u.af.iA==-1 && u.af.iB==SMALLEST_INT64 ) goto fp_math;
63450
 
        u.af.iB /= u.af.iA;
63451
 
        break;
63452
 
      }
63453
 
      default: {
63454
 
        if( u.af.iA==0 ) goto arithmetic_result_is_null;
63455
 
        if( u.af.iA==-1 ) u.af.iA = 1;
63456
 
        u.af.iB %= u.af.iA;
63457
 
        break;
63458
 
      }
63459
 
    }
63460
 
    pOut->u.i = u.af.iB;
63461
 
    MemSetTypeFlag(pOut, MEM_Int);
63462
 
  }else{
63463
 
fp_math:
63464
 
    u.af.rA = sqlite3VdbeRealValue(pIn1);
63465
 
    u.af.rB = sqlite3VdbeRealValue(pIn2);
63466
 
    switch( pOp->opcode ){
63467
 
      case OP_Add:         u.af.rB += u.af.rA;       break;
63468
 
      case OP_Subtract:    u.af.rB -= u.af.rA;       break;
63469
 
      case OP_Multiply:    u.af.rB *= u.af.rA;       break;
63470
 
      case OP_Divide: {
63471
 
        /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
63472
 
        if( u.af.rA==(double)0 ) goto arithmetic_result_is_null;
63473
 
        u.af.rB /= u.af.rA;
63474
 
        break;
63475
 
      }
63476
 
      default: {
63477
 
        u.af.iA = (i64)u.af.rA;
63478
 
        u.af.iB = (i64)u.af.rB;
63479
 
        if( u.af.iA==0 ) goto arithmetic_result_is_null;
63480
 
        if( u.af.iA==-1 ) u.af.iA = 1;
63481
 
        u.af.rB = (double)(u.af.iB % u.af.iA);
63482
 
        break;
63483
 
      }
63484
 
    }
63485
 
#ifdef SQLITE_OMIT_FLOATING_POINT
63486
 
    pOut->u.i = u.af.rB;
63487
 
    MemSetTypeFlag(pOut, MEM_Int);
63488
 
#else
63489
 
    if( sqlite3IsNaN(u.af.rB) ){
63490
 
      goto arithmetic_result_is_null;
63491
 
    }
63492
 
    pOut->r = u.af.rB;
63493
 
    MemSetTypeFlag(pOut, MEM_Real);
63494
 
    if( (u.af.flags & MEM_Real)==0 ){
63495
 
      sqlite3VdbeIntegerAffinity(pOut);
63496
 
    }
63497
 
#endif
63498
 
  }
63499
 
  break;
63500
 
 
63501
 
arithmetic_result_is_null:
63502
 
  sqlite3VdbeMemSetNull(pOut);
63503
 
  break;
63504
 
}
63505
 
 
63506
 
/* Opcode: CollSeq * * P4
63507
 
**
63508
 
** P4 is a pointer to a CollSeq struct. If the next call to a user function
63509
 
** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
63510
 
** be returned. This is used by the built-in min(), max() and nullif()
63511
 
** functions.
63512
 
**
63513
 
** The interface used by the implementation of the aforementioned functions
63514
 
** to retrieve the collation sequence set by this opcode is not available
63515
 
** publicly, only to user functions defined in func.c.
63516
 
*/
63517
 
case OP_CollSeq: {
63518
 
  assert( pOp->p4type==P4_COLLSEQ );
63519
 
  break;
63520
 
}
63521
 
 
63522
 
/* Opcode: Function P1 P2 P3 P4 P5
63523
 
**
63524
 
** Invoke a user function (P4 is a pointer to a Function structure that
63525
 
** defines the function) with P5 arguments taken from register P2 and
63526
 
** successors.  The result of the function is stored in register P3.
63527
 
** Register P3 must not be one of the function inputs.
63528
 
**
63529
 
** P1 is a 32-bit bitmask indicating whether or not each argument to the 
63530
 
** function was determined to be constant at compile time. If the first
63531
 
** argument was constant then bit 0 of P1 is set. This is used to determine
63532
 
** whether meta data associated with a user function argument using the
63533
 
** sqlite3_set_auxdata() API may be safely retained until the next
63534
 
** invocation of this opcode.
63535
 
**
63536
 
** See also: AggStep and AggFinal
63537
 
*/
63538
 
case OP_Function: {
63539
 
#if 0  /* local variables moved into u.ag */
63540
 
  int i;
63541
 
  Mem *pArg;
63542
 
  sqlite3_context ctx;
63543
 
  sqlite3_value **apVal;
63544
 
  int n;
63545
 
#endif /* local variables moved into u.ag */
63546
 
 
63547
 
  u.ag.n = pOp->p5;
63548
 
  u.ag.apVal = p->apArg;
63549
 
  assert( u.ag.apVal || u.ag.n==0 );
63550
 
  assert( pOp->p3>0 && pOp->p3<=p->nMem );
63551
 
  pOut = &aMem[pOp->p3];
63552
 
  memAboutToChange(p, pOut);
63553
 
 
63554
 
  assert( u.ag.n==0 || (pOp->p2>0 && pOp->p2+u.ag.n<=p->nMem+1) );
63555
 
  assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ag.n );
63556
 
  u.ag.pArg = &aMem[pOp->p2];
63557
 
  for(u.ag.i=0; u.ag.i<u.ag.n; u.ag.i++, u.ag.pArg++){
63558
 
    assert( memIsValid(u.ag.pArg) );
63559
 
    u.ag.apVal[u.ag.i] = u.ag.pArg;
63560
 
    Deephemeralize(u.ag.pArg);
63561
 
    sqlite3VdbeMemStoreType(u.ag.pArg);
63562
 
    REGISTER_TRACE(pOp->p2+u.ag.i, u.ag.pArg);
63563
 
  }
63564
 
 
63565
 
  assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
63566
 
  if( pOp->p4type==P4_FUNCDEF ){
63567
 
    u.ag.ctx.pFunc = pOp->p4.pFunc;
63568
 
    u.ag.ctx.pVdbeFunc = 0;
63569
 
  }else{
63570
 
    u.ag.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
63571
 
    u.ag.ctx.pFunc = u.ag.ctx.pVdbeFunc->pFunc;
63572
 
  }
63573
 
 
63574
 
  u.ag.ctx.s.flags = MEM_Null;
63575
 
  u.ag.ctx.s.db = db;
63576
 
  u.ag.ctx.s.xDel = 0;
63577
 
  u.ag.ctx.s.zMalloc = 0;
63578
 
 
63579
 
  /* The output cell may already have a buffer allocated. Move
63580
 
  ** the pointer to u.ag.ctx.s so in case the user-function can use
63581
 
  ** the already allocated buffer instead of allocating a new one.
63582
 
  */
63583
 
  sqlite3VdbeMemMove(&u.ag.ctx.s, pOut);
63584
 
  MemSetTypeFlag(&u.ag.ctx.s, MEM_Null);
63585
 
 
63586
 
  u.ag.ctx.isError = 0;
63587
 
  if( u.ag.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
63588
 
    assert( pOp>aOp );
63589
 
    assert( pOp[-1].p4type==P4_COLLSEQ );
63590
 
    assert( pOp[-1].opcode==OP_CollSeq );
63591
 
    u.ag.ctx.pColl = pOp[-1].p4.pColl;
63592
 
  }
63593
 
  (*u.ag.ctx.pFunc->xFunc)(&u.ag.ctx, u.ag.n, u.ag.apVal); /* IMP: R-24505-23230 */
63594
 
  if( db->mallocFailed ){
63595
 
    /* Even though a malloc() has failed, the implementation of the
63596
 
    ** user function may have called an sqlite3_result_XXX() function
63597
 
    ** to return a value. The following call releases any resources
63598
 
    ** associated with such a value.
63599
 
    */
63600
 
    sqlite3VdbeMemRelease(&u.ag.ctx.s);
63601
 
    goto no_mem;
63602
 
  }
63603
 
 
63604
 
  /* If any auxiliary data functions have been called by this user function,
63605
 
  ** immediately call the destructor for any non-static values.
63606
 
  */
63607
 
  if( u.ag.ctx.pVdbeFunc ){
63608
 
    sqlite3VdbeDeleteAuxData(u.ag.ctx.pVdbeFunc, pOp->p1);
63609
 
    pOp->p4.pVdbeFunc = u.ag.ctx.pVdbeFunc;
63610
 
    pOp->p4type = P4_VDBEFUNC;
63611
 
  }
63612
 
 
63613
 
  /* If the function returned an error, throw an exception */
63614
 
  if( u.ag.ctx.isError ){
63615
 
    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ag.ctx.s));
63616
 
    rc = u.ag.ctx.isError;
63617
 
  }
63618
 
 
63619
 
  /* Copy the result of the function into register P3 */
63620
 
  sqlite3VdbeChangeEncoding(&u.ag.ctx.s, encoding);
63621
 
  sqlite3VdbeMemMove(pOut, &u.ag.ctx.s);
63622
 
  if( sqlite3VdbeMemTooBig(pOut) ){
63623
 
    goto too_big;
63624
 
  }
63625
 
 
63626
 
#if 0
63627
 
  /* The app-defined function has done something that as caused this
63628
 
  ** statement to expire.  (Perhaps the function called sqlite3_exec()
63629
 
  ** with a CREATE TABLE statement.)
63630
 
  */
63631
 
  if( p->expired ) rc = SQLITE_ABORT;
63632
 
#endif
63633
 
 
63634
 
  REGISTER_TRACE(pOp->p3, pOut);
63635
 
  UPDATE_MAX_BLOBSIZE(pOut);
63636
 
  break;
63637
 
}
63638
 
 
63639
 
/* Opcode: BitAnd P1 P2 P3 * *
63640
 
**
63641
 
** Take the bit-wise AND of the values in register P1 and P2 and
63642
 
** store the result in register P3.
63643
 
** If either input is NULL, the result is NULL.
63644
 
*/
63645
 
/* Opcode: BitOr P1 P2 P3 * *
63646
 
**
63647
 
** Take the bit-wise OR of the values in register P1 and P2 and
63648
 
** store the result in register P3.
63649
 
** If either input is NULL, the result is NULL.
63650
 
*/
63651
 
/* Opcode: ShiftLeft P1 P2 P3 * *
63652
 
**
63653
 
** Shift the integer value in register P2 to the left by the
63654
 
** number of bits specified by the integer in register P1.
63655
 
** Store the result in register P3.
63656
 
** If either input is NULL, the result is NULL.
63657
 
*/
63658
 
/* Opcode: ShiftRight P1 P2 P3 * *
63659
 
**
63660
 
** Shift the integer value in register P2 to the right by the
63661
 
** number of bits specified by the integer in register P1.
63662
 
** Store the result in register P3.
63663
 
** If either input is NULL, the result is NULL.
63664
 
*/
63665
 
case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
63666
 
case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
63667
 
case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
63668
 
case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
63669
 
#if 0  /* local variables moved into u.ah */
63670
 
  i64 iA;
63671
 
  u64 uA;
63672
 
  i64 iB;
63673
 
  u8 op;
63674
 
#endif /* local variables moved into u.ah */
63675
 
 
63676
 
  pIn1 = &aMem[pOp->p1];
63677
 
  pIn2 = &aMem[pOp->p2];
63678
 
  pOut = &aMem[pOp->p3];
63679
 
  if( (pIn1->flags | pIn2->flags) & MEM_Null ){
63680
 
    sqlite3VdbeMemSetNull(pOut);
63681
 
    break;
63682
 
  }
63683
 
  u.ah.iA = sqlite3VdbeIntValue(pIn2);
63684
 
  u.ah.iB = sqlite3VdbeIntValue(pIn1);
63685
 
  u.ah.op = pOp->opcode;
63686
 
  if( u.ah.op==OP_BitAnd ){
63687
 
    u.ah.iA &= u.ah.iB;
63688
 
  }else if( u.ah.op==OP_BitOr ){
63689
 
    u.ah.iA |= u.ah.iB;
63690
 
  }else if( u.ah.iB!=0 ){
63691
 
    assert( u.ah.op==OP_ShiftRight || u.ah.op==OP_ShiftLeft );
63692
 
 
63693
 
    /* If shifting by a negative amount, shift in the other direction */
63694
 
    if( u.ah.iB<0 ){
63695
 
      assert( OP_ShiftRight==OP_ShiftLeft+1 );
63696
 
      u.ah.op = 2*OP_ShiftLeft + 1 - u.ah.op;
63697
 
      u.ah.iB = u.ah.iB>(-64) ? -u.ah.iB : 64;
63698
 
    }
63699
 
 
63700
 
    if( u.ah.iB>=64 ){
63701
 
      u.ah.iA = (u.ah.iA>=0 || u.ah.op==OP_ShiftLeft) ? 0 : -1;
63702
 
    }else{
63703
 
      memcpy(&u.ah.uA, &u.ah.iA, sizeof(u.ah.uA));
63704
 
      if( u.ah.op==OP_ShiftLeft ){
63705
 
        u.ah.uA <<= u.ah.iB;
63706
 
      }else{
63707
 
        u.ah.uA >>= u.ah.iB;
63708
 
        /* Sign-extend on a right shift of a negative number */
63709
 
        if( u.ah.iA<0 ) u.ah.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.ah.iB);
63710
 
      }
63711
 
      memcpy(&u.ah.iA, &u.ah.uA, sizeof(u.ah.iA));
63712
 
    }
63713
 
  }
63714
 
  pOut->u.i = u.ah.iA;
63715
 
  MemSetTypeFlag(pOut, MEM_Int);
63716
 
  break;
63717
 
}
63718
 
 
63719
 
/* Opcode: AddImm  P1 P2 * * *
63720
 
** 
63721
 
** Add the constant P2 to the value in register P1.
63722
 
** The result is always an integer.
63723
 
**
63724
 
** To force any register to be an integer, just add 0.
63725
 
*/
63726
 
case OP_AddImm: {            /* in1 */
63727
 
  pIn1 = &aMem[pOp->p1];
63728
 
  memAboutToChange(p, pIn1);
63729
 
  sqlite3VdbeMemIntegerify(pIn1);
63730
 
  pIn1->u.i += pOp->p2;
63731
 
  break;
63732
 
}
63733
 
 
63734
 
/* Opcode: MustBeInt P1 P2 * * *
63735
 
** 
63736
 
** Force the value in register P1 to be an integer.  If the value
63737
 
** in P1 is not an integer and cannot be converted into an integer
63738
 
** without data loss, then jump immediately to P2, or if P2==0
63739
 
** raise an SQLITE_MISMATCH exception.
63740
 
*/
63741
 
case OP_MustBeInt: {            /* jump, in1 */
63742
 
  pIn1 = &aMem[pOp->p1];
63743
 
  applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
63744
 
  if( (pIn1->flags & MEM_Int)==0 ){
63745
 
    if( pOp->p2==0 ){
63746
 
      rc = SQLITE_MISMATCH;
63747
 
      goto abort_due_to_error;
63748
 
    }else{
63749
 
      pc = pOp->p2 - 1;
63750
 
    }
63751
 
  }else{
63752
 
    MemSetTypeFlag(pIn1, MEM_Int);
63753
 
  }
63754
 
  break;
63755
 
}
63756
 
 
63757
 
#ifndef SQLITE_OMIT_FLOATING_POINT
63758
 
/* Opcode: RealAffinity P1 * * * *
63759
 
**
63760
 
** If register P1 holds an integer convert it to a real value.
63761
 
**
63762
 
** This opcode is used when extracting information from a column that
63763
 
** has REAL affinity.  Such column values may still be stored as
63764
 
** integers, for space efficiency, but after extraction we want them
63765
 
** to have only a real value.
63766
 
*/
63767
 
case OP_RealAffinity: {                  /* in1 */
63768
 
  pIn1 = &aMem[pOp->p1];
63769
 
  if( pIn1->flags & MEM_Int ){
63770
 
    sqlite3VdbeMemRealify(pIn1);
63771
 
  }
63772
 
  break;
63773
 
}
63774
 
#endif
63775
 
 
63776
 
#ifndef SQLITE_OMIT_CAST
63777
 
/* Opcode: ToText P1 * * * *
63778
 
**
63779
 
** Force the value in register P1 to be text.
63780
 
** If the value is numeric, convert it to a string using the
63781
 
** equivalent of printf().  Blob values are unchanged and
63782
 
** are afterwards simply interpreted as text.
63783
 
**
63784
 
** A NULL value is not changed by this routine.  It remains NULL.
63785
 
*/
63786
 
case OP_ToText: {                  /* same as TK_TO_TEXT, in1 */
63787
 
  pIn1 = &aMem[pOp->p1];
63788
 
  memAboutToChange(p, pIn1);
63789
 
  if( pIn1->flags & MEM_Null ) break;
63790
 
  assert( MEM_Str==(MEM_Blob>>3) );
63791
 
  pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
63792
 
  applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
63793
 
  rc = ExpandBlob(pIn1);
63794
 
  assert( pIn1->flags & MEM_Str || db->mallocFailed );
63795
 
  pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
63796
 
  UPDATE_MAX_BLOBSIZE(pIn1);
63797
 
  break;
63798
 
}
63799
 
 
63800
 
/* Opcode: ToBlob P1 * * * *
63801
 
**
63802
 
** Force the value in register P1 to be a BLOB.
63803
 
** If the value is numeric, convert it to a string first.
63804
 
** Strings are simply reinterpreted as blobs with no change
63805
 
** to the underlying data.
63806
 
**
63807
 
** A NULL value is not changed by this routine.  It remains NULL.
63808
 
*/
63809
 
case OP_ToBlob: {                  /* same as TK_TO_BLOB, in1 */
63810
 
  pIn1 = &aMem[pOp->p1];
63811
 
  if( pIn1->flags & MEM_Null ) break;
63812
 
  if( (pIn1->flags & MEM_Blob)==0 ){
63813
 
    applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
63814
 
    assert( pIn1->flags & MEM_Str || db->mallocFailed );
63815
 
    MemSetTypeFlag(pIn1, MEM_Blob);
63816
 
  }else{
63817
 
    pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
63818
 
  }
63819
 
  UPDATE_MAX_BLOBSIZE(pIn1);
63820
 
  break;
63821
 
}
63822
 
 
63823
 
/* Opcode: ToNumeric P1 * * * *
63824
 
**
63825
 
** Force the value in register P1 to be numeric (either an
63826
 
** integer or a floating-point number.)
63827
 
** If the value is text or blob, try to convert it to an using the
63828
 
** equivalent of atoi() or atof() and store 0 if no such conversion 
63829
 
** is possible.
63830
 
**
63831
 
** A NULL value is not changed by this routine.  It remains NULL.
63832
 
*/
63833
 
case OP_ToNumeric: {                  /* same as TK_TO_NUMERIC, in1 */
63834
 
  pIn1 = &aMem[pOp->p1];
63835
 
  sqlite3VdbeMemNumerify(pIn1);
63836
 
  break;
63837
 
}
63838
 
#endif /* SQLITE_OMIT_CAST */
63839
 
 
63840
 
/* Opcode: ToInt P1 * * * *
63841
 
**
63842
 
** Force the value in register P1 to be an integer.  If
63843
 
** The value is currently a real number, drop its fractional part.
63844
 
** If the value is text or blob, try to convert it to an integer using the
63845
 
** equivalent of atoi() and store 0 if no such conversion is possible.
63846
 
**
63847
 
** A NULL value is not changed by this routine.  It remains NULL.
63848
 
*/
63849
 
case OP_ToInt: {                  /* same as TK_TO_INT, in1 */
63850
 
  pIn1 = &aMem[pOp->p1];
63851
 
  if( (pIn1->flags & MEM_Null)==0 ){
63852
 
    sqlite3VdbeMemIntegerify(pIn1);
63853
 
  }
63854
 
  break;
63855
 
}
63856
 
 
63857
 
#if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
63858
 
/* Opcode: ToReal P1 * * * *
63859
 
**
63860
 
** Force the value in register P1 to be a floating point number.
63861
 
** If The value is currently an integer, convert it.
63862
 
** If the value is text or blob, try to convert it to an integer using the
63863
 
** equivalent of atoi() and store 0.0 if no such conversion is possible.
63864
 
**
63865
 
** A NULL value is not changed by this routine.  It remains NULL.
63866
 
*/
63867
 
case OP_ToReal: {                  /* same as TK_TO_REAL, in1 */
63868
 
  pIn1 = &aMem[pOp->p1];
63869
 
  memAboutToChange(p, pIn1);
63870
 
  if( (pIn1->flags & MEM_Null)==0 ){
63871
 
    sqlite3VdbeMemRealify(pIn1);
63872
 
  }
63873
 
  break;
63874
 
}
63875
 
#endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
63876
 
 
63877
 
/* Opcode: Lt P1 P2 P3 P4 P5
63878
 
**
63879
 
** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
63880
 
** jump to address P2.  
63881
 
**
63882
 
** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
63883
 
** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL 
63884
 
** bit is clear then fall through if either operand is NULL.
63885
 
**
63886
 
** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
63887
 
** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 
63888
 
** to coerce both inputs according to this affinity before the
63889
 
** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
63890
 
** affinity is used. Note that the affinity conversions are stored
63891
 
** back into the input registers P1 and P3.  So this opcode can cause
63892
 
** persistent changes to registers P1 and P3.
63893
 
**
63894
 
** Once any conversions have taken place, and neither value is NULL, 
63895
 
** the values are compared. If both values are blobs then memcmp() is
63896
 
** used to determine the results of the comparison.  If both values
63897
 
** are text, then the appropriate collating function specified in
63898
 
** P4 is  used to do the comparison.  If P4 is not specified then
63899
 
** memcmp() is used to compare text string.  If both values are
63900
 
** numeric, then a numeric comparison is used. If the two values
63901
 
** are of different types, then numbers are considered less than
63902
 
** strings and strings are considered less than blobs.
63903
 
**
63904
 
** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
63905
 
** store a boolean result (either 0, or 1, or NULL) in register P2.
63906
 
*/
63907
 
/* Opcode: Ne P1 P2 P3 P4 P5
63908
 
**
63909
 
** This works just like the Lt opcode except that the jump is taken if
63910
 
** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
63911
 
** additional information.
63912
 
**
63913
 
** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
63914
 
** true or false and is never NULL.  If both operands are NULL then the result
63915
 
** of comparison is false.  If either operand is NULL then the result is true.
63916
 
** If neither operand is NULL the the result is the same as it would be if
63917
 
** the SQLITE_NULLEQ flag were omitted from P5.
63918
 
*/
63919
 
/* Opcode: Eq P1 P2 P3 P4 P5
63920
 
**
63921
 
** This works just like the Lt opcode except that the jump is taken if
63922
 
** the operands in registers P1 and P3 are equal.
63923
 
** See the Lt opcode for additional information.
63924
 
**
63925
 
** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
63926
 
** true or false and is never NULL.  If both operands are NULL then the result
63927
 
** of comparison is true.  If either operand is NULL then the result is false.
63928
 
** If neither operand is NULL the the result is the same as it would be if
63929
 
** the SQLITE_NULLEQ flag were omitted from P5.
63930
 
*/
63931
 
/* Opcode: Le P1 P2 P3 P4 P5
63932
 
**
63933
 
** This works just like the Lt opcode except that the jump is taken if
63934
 
** the content of register P3 is less than or equal to the content of
63935
 
** register P1.  See the Lt opcode for additional information.
63936
 
*/
63937
 
/* Opcode: Gt P1 P2 P3 P4 P5
63938
 
**
63939
 
** This works just like the Lt opcode except that the jump is taken if
63940
 
** the content of register P3 is greater than the content of
63941
 
** register P1.  See the Lt opcode for additional information.
63942
 
*/
63943
 
/* Opcode: Ge P1 P2 P3 P4 P5
63944
 
**
63945
 
** This works just like the Lt opcode except that the jump is taken if
63946
 
** the content of register P3 is greater than or equal to the content of
63947
 
** register P1.  See the Lt opcode for additional information.
63948
 
*/
63949
 
case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
63950
 
case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
63951
 
case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
63952
 
case OP_Le:               /* same as TK_LE, jump, in1, in3 */
63953
 
case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
63954
 
case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
63955
 
#if 0  /* local variables moved into u.ai */
63956
 
  int res;            /* Result of the comparison of pIn1 against pIn3 */
63957
 
  char affinity;      /* Affinity to use for comparison */
63958
 
  u16 flags1;         /* Copy of initial value of pIn1->flags */
63959
 
  u16 flags3;         /* Copy of initial value of pIn3->flags */
63960
 
#endif /* local variables moved into u.ai */
63961
 
 
63962
 
  pIn1 = &aMem[pOp->p1];
63963
 
  pIn3 = &aMem[pOp->p3];
63964
 
  u.ai.flags1 = pIn1->flags;
63965
 
  u.ai.flags3 = pIn3->flags;
63966
 
  if( (pIn1->flags | pIn3->flags)&MEM_Null ){
63967
 
    /* One or both operands are NULL */
63968
 
    if( pOp->p5 & SQLITE_NULLEQ ){
63969
 
      /* If SQLITE_NULLEQ is set (which will only happen if the operator is
63970
 
      ** OP_Eq or OP_Ne) then take the jump or not depending on whether
63971
 
      ** or not both operands are null.
63972
 
      */
63973
 
      assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
63974
 
      u.ai.res = (pIn1->flags & pIn3->flags & MEM_Null)==0;
63975
 
    }else{
63976
 
      /* SQLITE_NULLEQ is clear and at least one operand is NULL,
63977
 
      ** then the result is always NULL.
63978
 
      ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
63979
 
      */
63980
 
      if( pOp->p5 & SQLITE_STOREP2 ){
63981
 
        pOut = &aMem[pOp->p2];
63982
 
        MemSetTypeFlag(pOut, MEM_Null);
63983
 
        REGISTER_TRACE(pOp->p2, pOut);
63984
 
      }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
63985
 
        pc = pOp->p2-1;
63986
 
      }
63987
 
      break;
63988
 
    }
63989
 
  }else{
63990
 
    /* Neither operand is NULL.  Do a comparison. */
63991
 
    u.ai.affinity = pOp->p5 & SQLITE_AFF_MASK;
63992
 
    if( u.ai.affinity ){
63993
 
      applyAffinity(pIn1, u.ai.affinity, encoding);
63994
 
      applyAffinity(pIn3, u.ai.affinity, encoding);
63995
 
      if( db->mallocFailed ) goto no_mem;
63996
 
    }
63997
 
 
63998
 
    assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
63999
 
    ExpandBlob(pIn1);
64000
 
    ExpandBlob(pIn3);
64001
 
    u.ai.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
64002
 
  }
64003
 
  switch( pOp->opcode ){
64004
 
    case OP_Eq:    u.ai.res = u.ai.res==0;     break;
64005
 
    case OP_Ne:    u.ai.res = u.ai.res!=0;     break;
64006
 
    case OP_Lt:    u.ai.res = u.ai.res<0;      break;
64007
 
    case OP_Le:    u.ai.res = u.ai.res<=0;     break;
64008
 
    case OP_Gt:    u.ai.res = u.ai.res>0;      break;
64009
 
    default:       u.ai.res = u.ai.res>=0;     break;
64010
 
  }
64011
 
 
64012
 
  if( pOp->p5 & SQLITE_STOREP2 ){
64013
 
    pOut = &aMem[pOp->p2];
64014
 
    memAboutToChange(p, pOut);
64015
 
    MemSetTypeFlag(pOut, MEM_Int);
64016
 
    pOut->u.i = u.ai.res;
64017
 
    REGISTER_TRACE(pOp->p2, pOut);
64018
 
  }else if( u.ai.res ){
64019
 
    pc = pOp->p2-1;
64020
 
  }
64021
 
 
64022
 
  /* Undo any changes made by applyAffinity() to the input registers. */
64023
 
  pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.ai.flags1&MEM_TypeMask);
64024
 
  pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.ai.flags3&MEM_TypeMask);
64025
 
  break;
64026
 
}
64027
 
 
64028
 
/* Opcode: Permutation * * * P4 *
64029
 
**
64030
 
** Set the permutation used by the OP_Compare operator to be the array
64031
 
** of integers in P4.
64032
 
**
64033
 
** The permutation is only valid until the next OP_Permutation, OP_Compare,
64034
 
** OP_Halt, or OP_ResultRow.  Typically the OP_Permutation should occur
64035
 
** immediately prior to the OP_Compare.
64036
 
*/
64037
 
case OP_Permutation: {
64038
 
  assert( pOp->p4type==P4_INTARRAY );
64039
 
  assert( pOp->p4.ai );
64040
 
  aPermute = pOp->p4.ai;
64041
 
  break;
64042
 
}
64043
 
 
64044
 
/* Opcode: Compare P1 P2 P3 P4 *
64045
 
**
64046
 
** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
64047
 
** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
64048
 
** the comparison for use by the next OP_Jump instruct.
64049
 
**
64050
 
** P4 is a KeyInfo structure that defines collating sequences and sort
64051
 
** orders for the comparison.  The permutation applies to registers
64052
 
** only.  The KeyInfo elements are used sequentially.
64053
 
**
64054
 
** The comparison is a sort comparison, so NULLs compare equal,
64055
 
** NULLs are less than numbers, numbers are less than strings,
64056
 
** and strings are less than blobs.
64057
 
*/
64058
 
case OP_Compare: {
64059
 
#if 0  /* local variables moved into u.aj */
64060
 
  int n;
64061
 
  int i;
64062
 
  int p1;
64063
 
  int p2;
64064
 
  const KeyInfo *pKeyInfo;
64065
 
  int idx;
64066
 
  CollSeq *pColl;    /* Collating sequence to use on this term */
64067
 
  int bRev;          /* True for DESCENDING sort order */
64068
 
#endif /* local variables moved into u.aj */
64069
 
 
64070
 
  u.aj.n = pOp->p3;
64071
 
  u.aj.pKeyInfo = pOp->p4.pKeyInfo;
64072
 
  assert( u.aj.n>0 );
64073
 
  assert( u.aj.pKeyInfo!=0 );
64074
 
  u.aj.p1 = pOp->p1;
64075
 
  u.aj.p2 = pOp->p2;
64076
 
#if SQLITE_DEBUG
64077
 
  if( aPermute ){
64078
 
    int k, mx = 0;
64079
 
    for(k=0; k<u.aj.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
64080
 
    assert( u.aj.p1>0 && u.aj.p1+mx<=p->nMem+1 );
64081
 
    assert( u.aj.p2>0 && u.aj.p2+mx<=p->nMem+1 );
64082
 
  }else{
64083
 
    assert( u.aj.p1>0 && u.aj.p1+u.aj.n<=p->nMem+1 );
64084
 
    assert( u.aj.p2>0 && u.aj.p2+u.aj.n<=p->nMem+1 );
64085
 
  }
64086
 
#endif /* SQLITE_DEBUG */
64087
 
  for(u.aj.i=0; u.aj.i<u.aj.n; u.aj.i++){
64088
 
    u.aj.idx = aPermute ? aPermute[u.aj.i] : u.aj.i;
64089
 
    assert( memIsValid(&aMem[u.aj.p1+u.aj.idx]) );
64090
 
    assert( memIsValid(&aMem[u.aj.p2+u.aj.idx]) );
64091
 
    REGISTER_TRACE(u.aj.p1+u.aj.idx, &aMem[u.aj.p1+u.aj.idx]);
64092
 
    REGISTER_TRACE(u.aj.p2+u.aj.idx, &aMem[u.aj.p2+u.aj.idx]);
64093
 
    assert( u.aj.i<u.aj.pKeyInfo->nField );
64094
 
    u.aj.pColl = u.aj.pKeyInfo->aColl[u.aj.i];
64095
 
    u.aj.bRev = u.aj.pKeyInfo->aSortOrder[u.aj.i];
64096
 
    iCompare = sqlite3MemCompare(&aMem[u.aj.p1+u.aj.idx], &aMem[u.aj.p2+u.aj.idx], u.aj.pColl);
64097
 
    if( iCompare ){
64098
 
      if( u.aj.bRev ) iCompare = -iCompare;
64099
 
      break;
64100
 
    }
64101
 
  }
64102
 
  aPermute = 0;
64103
 
  break;
64104
 
}
64105
 
 
64106
 
/* Opcode: Jump P1 P2 P3 * *
64107
 
**
64108
 
** Jump to the instruction at address P1, P2, or P3 depending on whether
64109
 
** in the most recent OP_Compare instruction the P1 vector was less than
64110
 
** equal to, or greater than the P2 vector, respectively.
64111
 
*/
64112
 
case OP_Jump: {             /* jump */
64113
 
  if( iCompare<0 ){
64114
 
    pc = pOp->p1 - 1;
64115
 
  }else if( iCompare==0 ){
64116
 
    pc = pOp->p2 - 1;
64117
 
  }else{
64118
 
    pc = pOp->p3 - 1;
64119
 
  }
64120
 
  break;
64121
 
}
64122
 
 
64123
 
/* Opcode: And P1 P2 P3 * *
64124
 
**
64125
 
** Take the logical AND of the values in registers P1 and P2 and
64126
 
** write the result into register P3.
64127
 
**
64128
 
** If either P1 or P2 is 0 (false) then the result is 0 even if
64129
 
** the other input is NULL.  A NULL and true or two NULLs give
64130
 
** a NULL output.
64131
 
*/
64132
 
/* Opcode: Or P1 P2 P3 * *
64133
 
**
64134
 
** Take the logical OR of the values in register P1 and P2 and
64135
 
** store the answer in register P3.
64136
 
**
64137
 
** If either P1 or P2 is nonzero (true) then the result is 1 (true)
64138
 
** even if the other input is NULL.  A NULL and false or two NULLs
64139
 
** give a NULL output.
64140
 
*/
64141
 
case OP_And:              /* same as TK_AND, in1, in2, out3 */
64142
 
case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
64143
 
#if 0  /* local variables moved into u.ak */
64144
 
  int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
64145
 
  int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
64146
 
#endif /* local variables moved into u.ak */
64147
 
 
64148
 
  pIn1 = &aMem[pOp->p1];
64149
 
  if( pIn1->flags & MEM_Null ){
64150
 
    u.ak.v1 = 2;
64151
 
  }else{
64152
 
    u.ak.v1 = sqlite3VdbeIntValue(pIn1)!=0;
64153
 
  }
64154
 
  pIn2 = &aMem[pOp->p2];
64155
 
  if( pIn2->flags & MEM_Null ){
64156
 
    u.ak.v2 = 2;
64157
 
  }else{
64158
 
    u.ak.v2 = sqlite3VdbeIntValue(pIn2)!=0;
64159
 
  }
64160
 
  if( pOp->opcode==OP_And ){
64161
 
    static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
64162
 
    u.ak.v1 = and_logic[u.ak.v1*3+u.ak.v2];
64163
 
  }else{
64164
 
    static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
64165
 
    u.ak.v1 = or_logic[u.ak.v1*3+u.ak.v2];
64166
 
  }
64167
 
  pOut = &aMem[pOp->p3];
64168
 
  if( u.ak.v1==2 ){
64169
 
    MemSetTypeFlag(pOut, MEM_Null);
64170
 
  }else{
64171
 
    pOut->u.i = u.ak.v1;
64172
 
    MemSetTypeFlag(pOut, MEM_Int);
64173
 
  }
64174
 
  break;
64175
 
}
64176
 
 
64177
 
/* Opcode: Not P1 P2 * * *
64178
 
**
64179
 
** Interpret the value in register P1 as a boolean value.  Store the
64180
 
** boolean complement in register P2.  If the value in register P1 is 
64181
 
** NULL, then a NULL is stored in P2.
64182
 
*/
64183
 
case OP_Not: {                /* same as TK_NOT, in1, out2 */
64184
 
  pIn1 = &aMem[pOp->p1];
64185
 
  pOut = &aMem[pOp->p2];
64186
 
  if( pIn1->flags & MEM_Null ){
64187
 
    sqlite3VdbeMemSetNull(pOut);
64188
 
  }else{
64189
 
    sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
64190
 
  }
64191
 
  break;
64192
 
}
64193
 
 
64194
 
/* Opcode: BitNot P1 P2 * * *
64195
 
**
64196
 
** Interpret the content of register P1 as an integer.  Store the
64197
 
** ones-complement of the P1 value into register P2.  If P1 holds
64198
 
** a NULL then store a NULL in P2.
64199
 
*/
64200
 
case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
64201
 
  pIn1 = &aMem[pOp->p1];
64202
 
  pOut = &aMem[pOp->p2];
64203
 
  if( pIn1->flags & MEM_Null ){
64204
 
    sqlite3VdbeMemSetNull(pOut);
64205
 
  }else{
64206
 
    sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
64207
 
  }
64208
 
  break;
64209
 
}
64210
 
 
64211
 
/* Opcode: If P1 P2 P3 * *
64212
 
**
64213
 
** Jump to P2 if the value in register P1 is true.  The value is
64214
 
** is considered true if it is numeric and non-zero.  If the value
64215
 
** in P1 is NULL then take the jump if P3 is true.
64216
 
*/
64217
 
/* Opcode: IfNot P1 P2 P3 * *
64218
 
**
64219
 
** Jump to P2 if the value in register P1 is False.  The value is
64220
 
** is considered true if it has a numeric value of zero.  If the value
64221
 
** in P1 is NULL then take the jump if P3 is true.
64222
 
*/
64223
 
case OP_If:                 /* jump, in1 */
64224
 
case OP_IfNot: {            /* jump, in1 */
64225
 
#if 0  /* local variables moved into u.al */
64226
 
  int c;
64227
 
#endif /* local variables moved into u.al */
64228
 
  pIn1 = &aMem[pOp->p1];
64229
 
  if( pIn1->flags & MEM_Null ){
64230
 
    u.al.c = pOp->p3;
64231
 
  }else{
64232
 
#ifdef SQLITE_OMIT_FLOATING_POINT
64233
 
    u.al.c = sqlite3VdbeIntValue(pIn1)!=0;
64234
 
#else
64235
 
    u.al.c = sqlite3VdbeRealValue(pIn1)!=0.0;
64236
 
#endif
64237
 
    if( pOp->opcode==OP_IfNot ) u.al.c = !u.al.c;
64238
 
  }
64239
 
  if( u.al.c ){
64240
 
    pc = pOp->p2-1;
64241
 
  }
64242
 
  break;
64243
 
}
64244
 
 
64245
 
/* Opcode: IsNull P1 P2 * * *
64246
 
**
64247
 
** Jump to P2 if the value in register P1 is NULL.
64248
 
*/
64249
 
case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
64250
 
  pIn1 = &aMem[pOp->p1];
64251
 
  if( (pIn1->flags & MEM_Null)!=0 ){
64252
 
    pc = pOp->p2 - 1;
64253
 
  }
64254
 
  break;
64255
 
}
64256
 
 
64257
 
/* Opcode: NotNull P1 P2 * * *
64258
 
**
64259
 
** Jump to P2 if the value in register P1 is not NULL.  
64260
 
*/
64261
 
case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
64262
 
  pIn1 = &aMem[pOp->p1];
64263
 
  if( (pIn1->flags & MEM_Null)==0 ){
64264
 
    pc = pOp->p2 - 1;
64265
 
  }
64266
 
  break;
64267
 
}
64268
 
 
64269
 
/* Opcode: Column P1 P2 P3 P4 P5
64270
 
**
64271
 
** Interpret the data that cursor P1 points to as a structure built using
64272
 
** the MakeRecord instruction.  (See the MakeRecord opcode for additional
64273
 
** information about the format of the data.)  Extract the P2-th column
64274
 
** from this record.  If there are less that (P2+1) 
64275
 
** values in the record, extract a NULL.
64276
 
**
64277
 
** The value extracted is stored in register P3.
64278
 
**
64279
 
** If the column contains fewer than P2 fields, then extract a NULL.  Or,
64280
 
** if the P4 argument is a P4_MEM use the value of the P4 argument as
64281
 
** the result.
64282
 
**
64283
 
** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
64284
 
** then the cache of the cursor is reset prior to extracting the column.
64285
 
** The first OP_Column against a pseudo-table after the value of the content
64286
 
** register has changed should have this bit set.
64287
 
*/
64288
 
case OP_Column: {
64289
 
#if 0  /* local variables moved into u.am */
64290
 
  u32 payloadSize;   /* Number of bytes in the record */
64291
 
  i64 payloadSize64; /* Number of bytes in the record */
64292
 
  int p1;            /* P1 value of the opcode */
64293
 
  int p2;            /* column number to retrieve */
64294
 
  VdbeCursor *pC;    /* The VDBE cursor */
64295
 
  char *zRec;        /* Pointer to complete record-data */
64296
 
  BtCursor *pCrsr;   /* The BTree cursor */
64297
 
  u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
64298
 
  u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
64299
 
  int nField;        /* number of fields in the record */
64300
 
  int len;           /* The length of the serialized data for the column */
64301
 
  int i;             /* Loop counter */
64302
 
  char *zData;       /* Part of the record being decoded */
64303
 
  Mem *pDest;        /* Where to write the extracted value */
64304
 
  Mem sMem;          /* For storing the record being decoded */
64305
 
  u8 *zIdx;          /* Index into header */
64306
 
  u8 *zEndHdr;       /* Pointer to first byte after the header */
64307
 
  u32 offset;        /* Offset into the data */
64308
 
  u32 szField;       /* Number of bytes in the content of a field */
64309
 
  int szHdr;         /* Size of the header size field at start of record */
64310
 
  int avail;         /* Number of bytes of available data */
64311
 
  Mem *pReg;         /* PseudoTable input register */
64312
 
#endif /* local variables moved into u.am */
64313
 
 
64314
 
 
64315
 
  u.am.p1 = pOp->p1;
64316
 
  u.am.p2 = pOp->p2;
64317
 
  u.am.pC = 0;
64318
 
  memset(&u.am.sMem, 0, sizeof(u.am.sMem));
64319
 
  assert( u.am.p1<p->nCursor );
64320
 
  assert( pOp->p3>0 && pOp->p3<=p->nMem );
64321
 
  u.am.pDest = &aMem[pOp->p3];
64322
 
  memAboutToChange(p, u.am.pDest);
64323
 
  MemSetTypeFlag(u.am.pDest, MEM_Null);
64324
 
  u.am.zRec = 0;
64325
 
 
64326
 
  /* This block sets the variable u.am.payloadSize to be the total number of
64327
 
  ** bytes in the record.
64328
 
  **
64329
 
  ** u.am.zRec is set to be the complete text of the record if it is available.
64330
 
  ** The complete record text is always available for pseudo-tables
64331
 
  ** If the record is stored in a cursor, the complete record text
64332
 
  ** might be available in the  u.am.pC->aRow cache.  Or it might not be.
64333
 
  ** If the data is unavailable,  u.am.zRec is set to NULL.
64334
 
  **
64335
 
  ** We also compute the number of columns in the record.  For cursors,
64336
 
  ** the number of columns is stored in the VdbeCursor.nField element.
64337
 
  */
64338
 
  u.am.pC = p->apCsr[u.am.p1];
64339
 
  assert( u.am.pC!=0 );
64340
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
64341
 
  assert( u.am.pC->pVtabCursor==0 );
64342
 
#endif
64343
 
  u.am.pCrsr = u.am.pC->pCursor;
64344
 
  if( u.am.pCrsr!=0 ){
64345
 
    /* The record is stored in a B-Tree */
64346
 
    rc = sqlite3VdbeCursorMoveto(u.am.pC);
64347
 
    if( rc ) goto abort_due_to_error;
64348
 
    if( u.am.pC->nullRow ){
64349
 
      u.am.payloadSize = 0;
64350
 
    }else if( u.am.pC->cacheStatus==p->cacheCtr ){
64351
 
      u.am.payloadSize = u.am.pC->payloadSize;
64352
 
      u.am.zRec = (char*)u.am.pC->aRow;
64353
 
    }else if( u.am.pC->isIndex ){
64354
 
      assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
64355
 
      rc = sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64);
64356
 
      assert( rc==SQLITE_OK );   /* True because of CursorMoveto() call above */
64357
 
      /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
64358
 
      ** payload size, so it is impossible for u.am.payloadSize64 to be
64359
 
      ** larger than 32 bits. */
64360
 
      assert( (u.am.payloadSize64 & SQLITE_MAX_U32)==(u64)u.am.payloadSize64 );
64361
 
      u.am.payloadSize = (u32)u.am.payloadSize64;
64362
 
    }else{
64363
 
      assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
64364
 
      rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
64365
 
      assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
64366
 
    }
64367
 
  }else if( u.am.pC->pseudoTableReg>0 ){
64368
 
    u.am.pReg = &aMem[u.am.pC->pseudoTableReg];
64369
 
    assert( u.am.pReg->flags & MEM_Blob );
64370
 
    assert( memIsValid(u.am.pReg) );
64371
 
    u.am.payloadSize = u.am.pReg->n;
64372
 
    u.am.zRec = u.am.pReg->z;
64373
 
    u.am.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
64374
 
    assert( u.am.payloadSize==0 || u.am.zRec!=0 );
64375
 
  }else{
64376
 
    /* Consider the row to be NULL */
64377
 
    u.am.payloadSize = 0;
64378
 
  }
64379
 
 
64380
 
  /* If u.am.payloadSize is 0, then just store a NULL */
64381
 
  if( u.am.payloadSize==0 ){
64382
 
    assert( u.am.pDest->flags&MEM_Null );
64383
 
    goto op_column_out;
64384
 
  }
64385
 
  assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
64386
 
  if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
64387
 
    goto too_big;
64388
 
  }
64389
 
 
64390
 
  u.am.nField = u.am.pC->nField;
64391
 
  assert( u.am.p2<u.am.nField );
64392
 
 
64393
 
  /* Read and parse the table header.  Store the results of the parse
64394
 
  ** into the record header cache fields of the cursor.
64395
 
  */
64396
 
  u.am.aType = u.am.pC->aType;
64397
 
  if( u.am.pC->cacheStatus==p->cacheCtr ){
64398
 
    u.am.aOffset = u.am.pC->aOffset;
64399
 
  }else{
64400
 
    assert(u.am.aType);
64401
 
    u.am.avail = 0;
64402
 
    u.am.pC->aOffset = u.am.aOffset = &u.am.aType[u.am.nField];
64403
 
    u.am.pC->payloadSize = u.am.payloadSize;
64404
 
    u.am.pC->cacheStatus = p->cacheCtr;
64405
 
 
64406
 
    /* Figure out how many bytes are in the header */
64407
 
    if( u.am.zRec ){
64408
 
      u.am.zData = u.am.zRec;
64409
 
    }else{
64410
 
      if( u.am.pC->isIndex ){
64411
 
        u.am.zData = (char*)sqlite3BtreeKeyFetch(u.am.pCrsr, &u.am.avail);
64412
 
      }else{
64413
 
        u.am.zData = (char*)sqlite3BtreeDataFetch(u.am.pCrsr, &u.am.avail);
64414
 
      }
64415
 
      /* If KeyFetch()/DataFetch() managed to get the entire payload,
64416
 
      ** save the payload in the u.am.pC->aRow cache.  That will save us from
64417
 
      ** having to make additional calls to fetch the content portion of
64418
 
      ** the record.
64419
 
      */
64420
 
      assert( u.am.avail>=0 );
64421
 
      if( u.am.payloadSize <= (u32)u.am.avail ){
64422
 
        u.am.zRec = u.am.zData;
64423
 
        u.am.pC->aRow = (u8*)u.am.zData;
64424
 
      }else{
64425
 
        u.am.pC->aRow = 0;
64426
 
      }
64427
 
    }
64428
 
    /* The following assert is true in all cases accept when
64429
 
    ** the database file has been corrupted externally.
64430
 
    **    assert( u.am.zRec!=0 || u.am.avail>=u.am.payloadSize || u.am.avail>=9 ); */
64431
 
    u.am.szHdr = getVarint32((u8*)u.am.zData, u.am.offset);
64432
 
 
64433
 
    /* Make sure a corrupt database has not given us an oversize header.
64434
 
    ** Do this now to avoid an oversize memory allocation.
64435
 
    **
64436
 
    ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
64437
 
    ** types use so much data space that there can only be 4096 and 32 of
64438
 
    ** them, respectively.  So the maximum header length results from a
64439
 
    ** 3-byte type for each of the maximum of 32768 columns plus three
64440
 
    ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
64441
 
    */
64442
 
    if( u.am.offset > 98307 ){
64443
 
      rc = SQLITE_CORRUPT_BKPT;
64444
 
      goto op_column_out;
64445
 
    }
64446
 
 
64447
 
    /* Compute in u.am.len the number of bytes of data we need to read in order
64448
 
    ** to get u.am.nField type values.  u.am.offset is an upper bound on this.  But
64449
 
    ** u.am.nField might be significantly less than the true number of columns
64450
 
    ** in the table, and in that case, 5*u.am.nField+3 might be smaller than u.am.offset.
64451
 
    ** We want to minimize u.am.len in order to limit the size of the memory
64452
 
    ** allocation, especially if a corrupt database file has caused u.am.offset
64453
 
    ** to be oversized. Offset is limited to 98307 above.  But 98307 might
64454
 
    ** still exceed Robson memory allocation limits on some configurations.
64455
 
    ** On systems that cannot tolerate large memory allocations, u.am.nField*5+3
64456
 
    ** will likely be much smaller since u.am.nField will likely be less than
64457
 
    ** 20 or so.  This insures that Robson memory allocation limits are
64458
 
    ** not exceeded even for corrupt database files.
64459
 
    */
64460
 
    u.am.len = u.am.nField*5 + 3;
64461
 
    if( u.am.len > (int)u.am.offset ) u.am.len = (int)u.am.offset;
64462
 
 
64463
 
    /* The KeyFetch() or DataFetch() above are fast and will get the entire
64464
 
    ** record header in most cases.  But they will fail to get the complete
64465
 
    ** record header if the record header does not fit on a single page
64466
 
    ** in the B-Tree.  When that happens, use sqlite3VdbeMemFromBtree() to
64467
 
    ** acquire the complete header text.
64468
 
    */
64469
 
    if( !u.am.zRec && u.am.avail<u.am.len ){
64470
 
      u.am.sMem.flags = 0;
64471
 
      u.am.sMem.db = 0;
64472
 
      rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, 0, u.am.len, u.am.pC->isIndex, &u.am.sMem);
64473
 
      if( rc!=SQLITE_OK ){
64474
 
        goto op_column_out;
64475
 
      }
64476
 
      u.am.zData = u.am.sMem.z;
64477
 
    }
64478
 
    u.am.zEndHdr = (u8 *)&u.am.zData[u.am.len];
64479
 
    u.am.zIdx = (u8 *)&u.am.zData[u.am.szHdr];
64480
 
 
64481
 
    /* Scan the header and use it to fill in the u.am.aType[] and u.am.aOffset[]
64482
 
    ** arrays.  u.am.aType[u.am.i] will contain the type integer for the u.am.i-th
64483
 
    ** column and u.am.aOffset[u.am.i] will contain the u.am.offset from the beginning
64484
 
    ** of the record to the start of the data for the u.am.i-th column
64485
 
    */
64486
 
    for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){
64487
 
      if( u.am.zIdx<u.am.zEndHdr ){
64488
 
        u.am.aOffset[u.am.i] = u.am.offset;
64489
 
        u.am.zIdx += getVarint32(u.am.zIdx, u.am.aType[u.am.i]);
64490
 
        u.am.szField = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.i]);
64491
 
        u.am.offset += u.am.szField;
64492
 
        if( u.am.offset<u.am.szField ){  /* True if u.am.offset overflows */
64493
 
          u.am.zIdx = &u.am.zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
64494
 
          break;
64495
 
        }
64496
 
      }else{
64497
 
        /* If u.am.i is less that u.am.nField, then there are less fields in this
64498
 
        ** record than SetNumColumns indicated there are columns in the
64499
 
        ** table. Set the u.am.offset for any extra columns not present in
64500
 
        ** the record to 0. This tells code below to store a NULL
64501
 
        ** instead of deserializing a value from the record.
64502
 
        */
64503
 
        u.am.aOffset[u.am.i] = 0;
64504
 
      }
64505
 
    }
64506
 
    sqlite3VdbeMemRelease(&u.am.sMem);
64507
 
    u.am.sMem.flags = MEM_Null;
64508
 
 
64509
 
    /* If we have read more header data than was contained in the header,
64510
 
    ** or if the end of the last field appears to be past the end of the
64511
 
    ** record, or if the end of the last field appears to be before the end
64512
 
    ** of the record (when all fields present), then we must be dealing
64513
 
    ** with a corrupt database.
64514
 
    */
64515
 
    if( (u.am.zIdx > u.am.zEndHdr) || (u.am.offset > u.am.payloadSize)
64516
 
         || (u.am.zIdx==u.am.zEndHdr && u.am.offset!=u.am.payloadSize) ){
64517
 
      rc = SQLITE_CORRUPT_BKPT;
64518
 
      goto op_column_out;
64519
 
    }
64520
 
  }
64521
 
 
64522
 
  /* Get the column information. If u.am.aOffset[u.am.p2] is non-zero, then
64523
 
  ** deserialize the value from the record. If u.am.aOffset[u.am.p2] is zero,
64524
 
  ** then there are not enough fields in the record to satisfy the
64525
 
  ** request.  In this case, set the value NULL or to P4 if P4 is
64526
 
  ** a pointer to a Mem object.
64527
 
  */
64528
 
  if( u.am.aOffset[u.am.p2] ){
64529
 
    assert( rc==SQLITE_OK );
64530
 
    if( u.am.zRec ){
64531
 
      sqlite3VdbeMemReleaseExternal(u.am.pDest);
64532
 
      sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest);
64533
 
    }else{
64534
 
      u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]);
64535
 
      sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest);
64536
 
      rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem);
64537
 
      if( rc!=SQLITE_OK ){
64538
 
        goto op_column_out;
64539
 
      }
64540
 
      u.am.zData = u.am.sMem.z;
64541
 
      sqlite3VdbeSerialGet((u8*)u.am.zData, u.am.aType[u.am.p2], u.am.pDest);
64542
 
    }
64543
 
    u.am.pDest->enc = encoding;
64544
 
  }else{
64545
 
    if( pOp->p4type==P4_MEM ){
64546
 
      sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static);
64547
 
    }else{
64548
 
      assert( u.am.pDest->flags&MEM_Null );
64549
 
    }
64550
 
  }
64551
 
 
64552
 
  /* If we dynamically allocated space to hold the data (in the
64553
 
  ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
64554
 
  ** dynamically allocated space over to the u.am.pDest structure.
64555
 
  ** This prevents a memory copy.
64556
 
  */
64557
 
  if( u.am.sMem.zMalloc ){
64558
 
    assert( u.am.sMem.z==u.am.sMem.zMalloc );
64559
 
    assert( !(u.am.pDest->flags & MEM_Dyn) );
64560
 
    assert( !(u.am.pDest->flags & (MEM_Blob|MEM_Str)) || u.am.pDest->z==u.am.sMem.z );
64561
 
    u.am.pDest->flags &= ~(MEM_Ephem|MEM_Static);
64562
 
    u.am.pDest->flags |= MEM_Term;
64563
 
    u.am.pDest->z = u.am.sMem.z;
64564
 
    u.am.pDest->zMalloc = u.am.sMem.zMalloc;
64565
 
  }
64566
 
 
64567
 
  rc = sqlite3VdbeMemMakeWriteable(u.am.pDest);
64568
 
 
64569
 
op_column_out:
64570
 
  UPDATE_MAX_BLOBSIZE(u.am.pDest);
64571
 
  REGISTER_TRACE(pOp->p3, u.am.pDest);
64572
 
  break;
64573
 
}
64574
 
 
64575
 
/* Opcode: Affinity P1 P2 * P4 *
64576
 
**
64577
 
** Apply affinities to a range of P2 registers starting with P1.
64578
 
**
64579
 
** P4 is a string that is P2 characters long. The nth character of the
64580
 
** string indicates the column affinity that should be used for the nth
64581
 
** memory cell in the range.
64582
 
*/
64583
 
case OP_Affinity: {
64584
 
#if 0  /* local variables moved into u.an */
64585
 
  const char *zAffinity;   /* The affinity to be applied */
64586
 
  char cAff;               /* A single character of affinity */
64587
 
#endif /* local variables moved into u.an */
64588
 
 
64589
 
  u.an.zAffinity = pOp->p4.z;
64590
 
  assert( u.an.zAffinity!=0 );
64591
 
  assert( u.an.zAffinity[pOp->p2]==0 );
64592
 
  pIn1 = &aMem[pOp->p1];
64593
 
  while( (u.an.cAff = *(u.an.zAffinity++))!=0 ){
64594
 
    assert( pIn1 <= &p->aMem[p->nMem] );
64595
 
    assert( memIsValid(pIn1) );
64596
 
    ExpandBlob(pIn1);
64597
 
    applyAffinity(pIn1, u.an.cAff, encoding);
64598
 
    pIn1++;
64599
 
  }
64600
 
  break;
64601
 
}
64602
 
 
64603
 
/* Opcode: MakeRecord P1 P2 P3 P4 *
64604
 
**
64605
 
** Convert P2 registers beginning with P1 into the [record format]
64606
 
** use as a data record in a database table or as a key
64607
 
** in an index.  The OP_Column opcode can decode the record later.
64608
 
**
64609
 
** P4 may be a string that is P2 characters long.  The nth character of the
64610
 
** string indicates the column affinity that should be used for the nth
64611
 
** field of the index key.
64612
 
**
64613
 
** The mapping from character to affinity is given by the SQLITE_AFF_
64614
 
** macros defined in sqliteInt.h.
64615
 
**
64616
 
** If P4 is NULL then all index fields have the affinity NONE.
64617
 
*/
64618
 
case OP_MakeRecord: {
64619
 
#if 0  /* local variables moved into u.ao */
64620
 
  u8 *zNewRecord;        /* A buffer to hold the data for the new record */
64621
 
  Mem *pRec;             /* The new record */
64622
 
  u64 nData;             /* Number of bytes of data space */
64623
 
  int nHdr;              /* Number of bytes of header space */
64624
 
  i64 nByte;             /* Data space required for this record */
64625
 
  int nZero;             /* Number of zero bytes at the end of the record */
64626
 
  int nVarint;           /* Number of bytes in a varint */
64627
 
  u32 serial_type;       /* Type field */
64628
 
  Mem *pData0;           /* First field to be combined into the record */
64629
 
  Mem *pLast;            /* Last field of the record */
64630
 
  int nField;            /* Number of fields in the record */
64631
 
  char *zAffinity;       /* The affinity string for the record */
64632
 
  int file_format;       /* File format to use for encoding */
64633
 
  int i;                 /* Space used in zNewRecord[] */
64634
 
  int len;               /* Length of a field */
64635
 
#endif /* local variables moved into u.ao */
64636
 
 
64637
 
  /* Assuming the record contains N fields, the record format looks
64638
 
  ** like this:
64639
 
  **
64640
 
  ** ------------------------------------------------------------------------
64641
 
  ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
64642
 
  ** ------------------------------------------------------------------------
64643
 
  **
64644
 
  ** Data(0) is taken from register P1.  Data(1) comes from register P1+1
64645
 
  ** and so froth.
64646
 
  **
64647
 
  ** Each type field is a varint representing the serial type of the
64648
 
  ** corresponding data element (see sqlite3VdbeSerialType()). The
64649
 
  ** hdr-size field is also a varint which is the offset from the beginning
64650
 
  ** of the record to data0.
64651
 
  */
64652
 
  u.ao.nData = 0;         /* Number of bytes of data space */
64653
 
  u.ao.nHdr = 0;          /* Number of bytes of header space */
64654
 
  u.ao.nZero = 0;         /* Number of zero bytes at the end of the record */
64655
 
  u.ao.nField = pOp->p1;
64656
 
  u.ao.zAffinity = pOp->p4.z;
64657
 
  assert( u.ao.nField>0 && pOp->p2>0 && pOp->p2+u.ao.nField<=p->nMem+1 );
64658
 
  u.ao.pData0 = &aMem[u.ao.nField];
64659
 
  u.ao.nField = pOp->p2;
64660
 
  u.ao.pLast = &u.ao.pData0[u.ao.nField-1];
64661
 
  u.ao.file_format = p->minWriteFileFormat;
64662
 
 
64663
 
  /* Identify the output register */
64664
 
  assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
64665
 
  pOut = &aMem[pOp->p3];
64666
 
  memAboutToChange(p, pOut);
64667
 
 
64668
 
  /* Loop through the elements that will make up the record to figure
64669
 
  ** out how much space is required for the new record.
64670
 
  */
64671
 
  for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
64672
 
    assert( memIsValid(u.ao.pRec) );
64673
 
    if( u.ao.zAffinity ){
64674
 
      applyAffinity(u.ao.pRec, u.ao.zAffinity[u.ao.pRec-u.ao.pData0], encoding);
64675
 
    }
64676
 
    if( u.ao.pRec->flags&MEM_Zero && u.ao.pRec->n>0 ){
64677
 
      sqlite3VdbeMemExpandBlob(u.ao.pRec);
64678
 
    }
64679
 
    u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
64680
 
    u.ao.len = sqlite3VdbeSerialTypeLen(u.ao.serial_type);
64681
 
    u.ao.nData += u.ao.len;
64682
 
    u.ao.nHdr += sqlite3VarintLen(u.ao.serial_type);
64683
 
    if( u.ao.pRec->flags & MEM_Zero ){
64684
 
      /* Only pure zero-filled BLOBs can be input to this Opcode.
64685
 
      ** We do not allow blobs with a prefix and a zero-filled tail. */
64686
 
      u.ao.nZero += u.ao.pRec->u.nZero;
64687
 
    }else if( u.ao.len ){
64688
 
      u.ao.nZero = 0;
64689
 
    }
64690
 
  }
64691
 
 
64692
 
  /* Add the initial header varint and total the size */
64693
 
  u.ao.nHdr += u.ao.nVarint = sqlite3VarintLen(u.ao.nHdr);
64694
 
  if( u.ao.nVarint<sqlite3VarintLen(u.ao.nHdr) ){
64695
 
    u.ao.nHdr++;
64696
 
  }
64697
 
  u.ao.nByte = u.ao.nHdr+u.ao.nData-u.ao.nZero;
64698
 
  if( u.ao.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
64699
 
    goto too_big;
64700
 
  }
64701
 
 
64702
 
  /* Make sure the output register has a buffer large enough to store
64703
 
  ** the new record. The output register (pOp->p3) is not allowed to
64704
 
  ** be one of the input registers (because the following call to
64705
 
  ** sqlite3VdbeMemGrow() could clobber the value before it is used).
64706
 
  */
64707
 
  if( sqlite3VdbeMemGrow(pOut, (int)u.ao.nByte, 0) ){
64708
 
    goto no_mem;
64709
 
  }
64710
 
  u.ao.zNewRecord = (u8 *)pOut->z;
64711
 
 
64712
 
  /* Write the record */
64713
 
  u.ao.i = putVarint32(u.ao.zNewRecord, u.ao.nHdr);
64714
 
  for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
64715
 
    u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
64716
 
    u.ao.i += putVarint32(&u.ao.zNewRecord[u.ao.i], u.ao.serial_type);      /* serial type */
64717
 
  }
64718
 
  for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){  /* serial data */
64719
 
    u.ao.i += sqlite3VdbeSerialPut(&u.ao.zNewRecord[u.ao.i], (int)(u.ao.nByte-u.ao.i), u.ao.pRec,u.ao.file_format);
64720
 
  }
64721
 
  assert( u.ao.i==u.ao.nByte );
64722
 
 
64723
 
  assert( pOp->p3>0 && pOp->p3<=p->nMem );
64724
 
  pOut->n = (int)u.ao.nByte;
64725
 
  pOut->flags = MEM_Blob | MEM_Dyn;
64726
 
  pOut->xDel = 0;
64727
 
  if( u.ao.nZero ){
64728
 
    pOut->u.nZero = u.ao.nZero;
64729
 
    pOut->flags |= MEM_Zero;
64730
 
  }
64731
 
  pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
64732
 
  REGISTER_TRACE(pOp->p3, pOut);
64733
 
  UPDATE_MAX_BLOBSIZE(pOut);
64734
 
  break;
64735
 
}
64736
 
 
64737
 
/* Opcode: Count P1 P2 * * *
64738
 
**
64739
 
** Store the number of entries (an integer value) in the table or index 
64740
 
** opened by cursor P1 in register P2
64741
 
*/
64742
 
#ifndef SQLITE_OMIT_BTREECOUNT
64743
 
case OP_Count: {         /* out2-prerelease */
64744
 
#if 0  /* local variables moved into u.ap */
64745
 
  i64 nEntry;
64746
 
  BtCursor *pCrsr;
64747
 
#endif /* local variables moved into u.ap */
64748
 
 
64749
 
  u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor;
64750
 
  if( u.ap.pCrsr ){
64751
 
    rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry);
64752
 
  }else{
64753
 
    u.ap.nEntry = 0;
64754
 
  }
64755
 
  pOut->u.i = u.ap.nEntry;
64756
 
  break;
64757
 
}
64758
 
#endif
64759
 
 
64760
 
/* Opcode: Savepoint P1 * * P4 *
64761
 
**
64762
 
** Open, release or rollback the savepoint named by parameter P4, depending
64763
 
** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
64764
 
** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
64765
 
*/
64766
 
case OP_Savepoint: {
64767
 
#if 0  /* local variables moved into u.aq */
64768
 
  int p1;                         /* Value of P1 operand */
64769
 
  char *zName;                    /* Name of savepoint */
64770
 
  int nName;
64771
 
  Savepoint *pNew;
64772
 
  Savepoint *pSavepoint;
64773
 
  Savepoint *pTmp;
64774
 
  int iSavepoint;
64775
 
  int ii;
64776
 
#endif /* local variables moved into u.aq */
64777
 
 
64778
 
  u.aq.p1 = pOp->p1;
64779
 
  u.aq.zName = pOp->p4.z;
64780
 
 
64781
 
  /* Assert that the u.aq.p1 parameter is valid. Also that if there is no open
64782
 
  ** transaction, then there cannot be any savepoints.
64783
 
  */
64784
 
  assert( db->pSavepoint==0 || db->autoCommit==0 );
64785
 
  assert( u.aq.p1==SAVEPOINT_BEGIN||u.aq.p1==SAVEPOINT_RELEASE||u.aq.p1==SAVEPOINT_ROLLBACK );
64786
 
  assert( db->pSavepoint || db->isTransactionSavepoint==0 );
64787
 
  assert( checkSavepointCount(db) );
64788
 
 
64789
 
  if( u.aq.p1==SAVEPOINT_BEGIN ){
64790
 
    if( db->writeVdbeCnt>0 ){
64791
 
      /* A new savepoint cannot be created if there are active write
64792
 
      ** statements (i.e. open read/write incremental blob handles).
64793
 
      */
64794
 
      sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
64795
 
        "SQL statements in progress");
64796
 
      rc = SQLITE_BUSY;
64797
 
    }else{
64798
 
      u.aq.nName = sqlite3Strlen30(u.aq.zName);
64799
 
 
64800
 
      /* Create a new savepoint structure. */
64801
 
      u.aq.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.aq.nName+1);
64802
 
      if( u.aq.pNew ){
64803
 
        u.aq.pNew->zName = (char *)&u.aq.pNew[1];
64804
 
        memcpy(u.aq.pNew->zName, u.aq.zName, u.aq.nName+1);
64805
 
 
64806
 
        /* If there is no open transaction, then mark this as a special
64807
 
        ** "transaction savepoint". */
64808
 
        if( db->autoCommit ){
64809
 
          db->autoCommit = 0;
64810
 
          db->isTransactionSavepoint = 1;
64811
 
        }else{
64812
 
          db->nSavepoint++;
64813
 
        }
64814
 
 
64815
 
        /* Link the new savepoint into the database handle's list. */
64816
 
        u.aq.pNew->pNext = db->pSavepoint;
64817
 
        db->pSavepoint = u.aq.pNew;
64818
 
        u.aq.pNew->nDeferredCons = db->nDeferredCons;
64819
 
      }
64820
 
    }
64821
 
  }else{
64822
 
    u.aq.iSavepoint = 0;
64823
 
 
64824
 
    /* Find the named savepoint. If there is no such savepoint, then an
64825
 
    ** an error is returned to the user.  */
64826
 
    for(
64827
 
      u.aq.pSavepoint = db->pSavepoint;
64828
 
      u.aq.pSavepoint && sqlite3StrICmp(u.aq.pSavepoint->zName, u.aq.zName);
64829
 
      u.aq.pSavepoint = u.aq.pSavepoint->pNext
64830
 
    ){
64831
 
      u.aq.iSavepoint++;
64832
 
    }
64833
 
    if( !u.aq.pSavepoint ){
64834
 
      sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.aq.zName);
64835
 
      rc = SQLITE_ERROR;
64836
 
    }else if(
64837
 
        db->writeVdbeCnt>0 || (u.aq.p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
64838
 
    ){
64839
 
      /* It is not possible to release (commit) a savepoint if there are
64840
 
      ** active write statements. It is not possible to rollback a savepoint
64841
 
      ** if there are any active statements at all.
64842
 
      */
64843
 
      sqlite3SetString(&p->zErrMsg, db,
64844
 
        "cannot %s savepoint - SQL statements in progress",
64845
 
        (u.aq.p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
64846
 
      );
64847
 
      rc = SQLITE_BUSY;
64848
 
    }else{
64849
 
 
64850
 
      /* Determine whether or not this is a transaction savepoint. If so,
64851
 
      ** and this is a RELEASE command, then the current transaction
64852
 
      ** is committed.
64853
 
      */
64854
 
      int isTransaction = u.aq.pSavepoint->pNext==0 && db->isTransactionSavepoint;
64855
 
      if( isTransaction && u.aq.p1==SAVEPOINT_RELEASE ){
64856
 
        if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
64857
 
          goto vdbe_return;
64858
 
        }
64859
 
        db->autoCommit = 1;
64860
 
        if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
64861
 
          p->pc = pc;
64862
 
          db->autoCommit = 0;
64863
 
          p->rc = rc = SQLITE_BUSY;
64864
 
          goto vdbe_return;
64865
 
        }
64866
 
        db->isTransactionSavepoint = 0;
64867
 
        rc = p->rc;
64868
 
      }else{
64869
 
        u.aq.iSavepoint = db->nSavepoint - u.aq.iSavepoint - 1;
64870
 
        for(u.aq.ii=0; u.aq.ii<db->nDb; u.aq.ii++){
64871
 
          rc = sqlite3BtreeSavepoint(db->aDb[u.aq.ii].pBt, u.aq.p1, u.aq.iSavepoint);
64872
 
          if( rc!=SQLITE_OK ){
64873
 
            goto abort_due_to_error;
64874
 
          }
64875
 
        }
64876
 
        if( u.aq.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
64877
 
          sqlite3ExpirePreparedStatements(db);
64878
 
          sqlite3ResetInternalSchema(db, -1);
64879
 
          db->flags = (db->flags | SQLITE_InternChanges);
64880
 
        }
64881
 
      }
64882
 
 
64883
 
      /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
64884
 
      ** savepoints nested inside of the savepoint being operated on. */
64885
 
      while( db->pSavepoint!=u.aq.pSavepoint ){
64886
 
        u.aq.pTmp = db->pSavepoint;
64887
 
        db->pSavepoint = u.aq.pTmp->pNext;
64888
 
        sqlite3DbFree(db, u.aq.pTmp);
64889
 
        db->nSavepoint--;
64890
 
      }
64891
 
 
64892
 
      /* If it is a RELEASE, then destroy the savepoint being operated on
64893
 
      ** too. If it is a ROLLBACK TO, then set the number of deferred
64894
 
      ** constraint violations present in the database to the value stored
64895
 
      ** when the savepoint was created.  */
64896
 
      if( u.aq.p1==SAVEPOINT_RELEASE ){
64897
 
        assert( u.aq.pSavepoint==db->pSavepoint );
64898
 
        db->pSavepoint = u.aq.pSavepoint->pNext;
64899
 
        sqlite3DbFree(db, u.aq.pSavepoint);
64900
 
        if( !isTransaction ){
64901
 
          db->nSavepoint--;
64902
 
        }
64903
 
      }else{
64904
 
        db->nDeferredCons = u.aq.pSavepoint->nDeferredCons;
64905
 
      }
64906
 
    }
64907
 
  }
64908
 
 
64909
 
  break;
64910
 
}
64911
 
 
64912
 
/* Opcode: AutoCommit P1 P2 * * *
64913
 
**
64914
 
** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
64915
 
** back any currently active btree transactions. If there are any active
64916
 
** VMs (apart from this one), then a ROLLBACK fails.  A COMMIT fails if
64917
 
** there are active writing VMs or active VMs that use shared cache.
64918
 
**
64919
 
** This instruction causes the VM to halt.
64920
 
*/
64921
 
case OP_AutoCommit: {
64922
 
#if 0  /* local variables moved into u.ar */
64923
 
  int desiredAutoCommit;
64924
 
  int iRollback;
64925
 
  int turnOnAC;
64926
 
#endif /* local variables moved into u.ar */
64927
 
 
64928
 
  u.ar.desiredAutoCommit = pOp->p1;
64929
 
  u.ar.iRollback = pOp->p2;
64930
 
  u.ar.turnOnAC = u.ar.desiredAutoCommit && !db->autoCommit;
64931
 
  assert( u.ar.desiredAutoCommit==1 || u.ar.desiredAutoCommit==0 );
64932
 
  assert( u.ar.desiredAutoCommit==1 || u.ar.iRollback==0 );
64933
 
  assert( db->activeVdbeCnt>0 );  /* At least this one VM is active */
64934
 
 
64935
 
  if( u.ar.turnOnAC && u.ar.iRollback && db->activeVdbeCnt>1 ){
64936
 
    /* If this instruction implements a ROLLBACK and other VMs are
64937
 
    ** still running, and a transaction is active, return an error indicating
64938
 
    ** that the other VMs must complete first.
64939
 
    */
64940
 
    sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
64941
 
        "SQL statements in progress");
64942
 
    rc = SQLITE_BUSY;
64943
 
  }else if( u.ar.turnOnAC && !u.ar.iRollback && db->writeVdbeCnt>0 ){
64944
 
    /* If this instruction implements a COMMIT and other VMs are writing
64945
 
    ** return an error indicating that the other VMs must complete first.
64946
 
    */
64947
 
    sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
64948
 
        "SQL statements in progress");
64949
 
    rc = SQLITE_BUSY;
64950
 
  }else if( u.ar.desiredAutoCommit!=db->autoCommit ){
64951
 
    if( u.ar.iRollback ){
64952
 
      assert( u.ar.desiredAutoCommit==1 );
64953
 
      sqlite3RollbackAll(db);
64954
 
      db->autoCommit = 1;
64955
 
    }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
64956
 
      goto vdbe_return;
64957
 
    }else{
64958
 
      db->autoCommit = (u8)u.ar.desiredAutoCommit;
64959
 
      if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
64960
 
        p->pc = pc;
64961
 
        db->autoCommit = (u8)(1-u.ar.desiredAutoCommit);
64962
 
        p->rc = rc = SQLITE_BUSY;
64963
 
        goto vdbe_return;
64964
 
      }
64965
 
    }
64966
 
    assert( db->nStatement==0 );
64967
 
    sqlite3CloseSavepoints(db);
64968
 
    if( p->rc==SQLITE_OK ){
64969
 
      rc = SQLITE_DONE;
64970
 
    }else{
64971
 
      rc = SQLITE_ERROR;
64972
 
    }
64973
 
    goto vdbe_return;
64974
 
  }else{
64975
 
    sqlite3SetString(&p->zErrMsg, db,
64976
 
        (!u.ar.desiredAutoCommit)?"cannot start a transaction within a transaction":(
64977
 
        (u.ar.iRollback)?"cannot rollback - no transaction is active":
64978
 
                   "cannot commit - no transaction is active"));
64979
 
 
64980
 
    rc = SQLITE_ERROR;
64981
 
  }
64982
 
  break;
64983
 
}
64984
 
 
64985
 
/* Opcode: Transaction P1 P2 * * *
64986
 
**
64987
 
** Begin a transaction.  The transaction ends when a Commit or Rollback
64988
 
** opcode is encountered.  Depending on the ON CONFLICT setting, the
64989
 
** transaction might also be rolled back if an error is encountered.
64990
 
**
64991
 
** P1 is the index of the database file on which the transaction is
64992
 
** started.  Index 0 is the main database file and index 1 is the
64993
 
** file used for temporary tables.  Indices of 2 or more are used for
64994
 
** attached databases.
64995
 
**
64996
 
** If P2 is non-zero, then a write-transaction is started.  A RESERVED lock is
64997
 
** obtained on the database file when a write-transaction is started.  No
64998
 
** other process can start another write transaction while this transaction is
64999
 
** underway.  Starting a write transaction also creates a rollback journal. A
65000
 
** write transaction must be started before any changes can be made to the
65001
 
** database.  If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
65002
 
** on the file.
65003
 
**
65004
 
** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
65005
 
** true (this flag is set if the Vdbe may modify more than one row and may
65006
 
** throw an ABORT exception), a statement transaction may also be opened.
65007
 
** More specifically, a statement transaction is opened iff the database
65008
 
** connection is currently not in autocommit mode, or if there are other
65009
 
** active statements. A statement transaction allows the affects of this
65010
 
** VDBE to be rolled back after an error without having to roll back the
65011
 
** entire transaction. If no error is encountered, the statement transaction
65012
 
** will automatically commit when the VDBE halts.
65013
 
**
65014
 
** If P2 is zero, then a read-lock is obtained on the database file.
65015
 
*/
65016
 
case OP_Transaction: {
65017
 
#if 0  /* local variables moved into u.as */
65018
 
  Btree *pBt;
65019
 
#endif /* local variables moved into u.as */
65020
 
 
65021
 
  assert( pOp->p1>=0 && pOp->p1<db->nDb );
65022
 
  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
65023
 
  u.as.pBt = db->aDb[pOp->p1].pBt;
65024
 
 
65025
 
  if( u.as.pBt ){
65026
 
    rc = sqlite3BtreeBeginTrans(u.as.pBt, pOp->p2);
65027
 
    if( rc==SQLITE_BUSY ){
65028
 
      p->pc = pc;
65029
 
      p->rc = rc = SQLITE_BUSY;
65030
 
      goto vdbe_return;
65031
 
    }
65032
 
    if( rc!=SQLITE_OK ){
65033
 
      goto abort_due_to_error;
65034
 
    }
65035
 
 
65036
 
    if( pOp->p2 && p->usesStmtJournal
65037
 
     && (db->autoCommit==0 || db->activeVdbeCnt>1)
65038
 
    ){
65039
 
      assert( sqlite3BtreeIsInTrans(u.as.pBt) );
65040
 
      if( p->iStatement==0 ){
65041
 
        assert( db->nStatement>=0 && db->nSavepoint>=0 );
65042
 
        db->nStatement++;
65043
 
        p->iStatement = db->nSavepoint + db->nStatement;
65044
 
      }
65045
 
      rc = sqlite3BtreeBeginStmt(u.as.pBt, p->iStatement);
65046
 
 
65047
 
      /* Store the current value of the database handles deferred constraint
65048
 
      ** counter. If the statement transaction needs to be rolled back,
65049
 
      ** the value of this counter needs to be restored too.  */
65050
 
      p->nStmtDefCons = db->nDeferredCons;
65051
 
    }
65052
 
  }
65053
 
  break;
65054
 
}
65055
 
 
65056
 
/* Opcode: ReadCookie P1 P2 P3 * *
65057
 
**
65058
 
** Read cookie number P3 from database P1 and write it into register P2.
65059
 
** P3==1 is the schema version.  P3==2 is the database format.
65060
 
** P3==3 is the recommended pager cache size, and so forth.  P1==0 is
65061
 
** the main database file and P1==1 is the database file used to store
65062
 
** temporary tables.
65063
 
**
65064
 
** There must be a read-lock on the database (either a transaction
65065
 
** must be started or there must be an open cursor) before
65066
 
** executing this instruction.
65067
 
*/
65068
 
case OP_ReadCookie: {               /* out2-prerelease */
65069
 
#if 0  /* local variables moved into u.at */
65070
 
  int iMeta;
65071
 
  int iDb;
65072
 
  int iCookie;
65073
 
#endif /* local variables moved into u.at */
65074
 
 
65075
 
  u.at.iDb = pOp->p1;
65076
 
  u.at.iCookie = pOp->p3;
65077
 
  assert( pOp->p3<SQLITE_N_BTREE_META );
65078
 
  assert( u.at.iDb>=0 && u.at.iDb<db->nDb );
65079
 
  assert( db->aDb[u.at.iDb].pBt!=0 );
65080
 
  assert( (p->btreeMask & (((yDbMask)1)<<u.at.iDb))!=0 );
65081
 
 
65082
 
  sqlite3BtreeGetMeta(db->aDb[u.at.iDb].pBt, u.at.iCookie, (u32 *)&u.at.iMeta);
65083
 
  pOut->u.i = u.at.iMeta;
65084
 
  break;
65085
 
}
65086
 
 
65087
 
/* Opcode: SetCookie P1 P2 P3 * *
65088
 
**
65089
 
** Write the content of register P3 (interpreted as an integer)
65090
 
** into cookie number P2 of database P1.  P2==1 is the schema version.  
65091
 
** P2==2 is the database format. P2==3 is the recommended pager cache 
65092
 
** size, and so forth.  P1==0 is the main database file and P1==1 is the 
65093
 
** database file used to store temporary tables.
65094
 
**
65095
 
** A transaction must be started before executing this opcode.
65096
 
*/
65097
 
case OP_SetCookie: {       /* in3 */
65098
 
#if 0  /* local variables moved into u.au */
65099
 
  Db *pDb;
65100
 
#endif /* local variables moved into u.au */
65101
 
  assert( pOp->p2<SQLITE_N_BTREE_META );
65102
 
  assert( pOp->p1>=0 && pOp->p1<db->nDb );
65103
 
  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
65104
 
  u.au.pDb = &db->aDb[pOp->p1];
65105
 
  assert( u.au.pDb->pBt!=0 );
65106
 
  assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
65107
 
  pIn3 = &aMem[pOp->p3];
65108
 
  sqlite3VdbeMemIntegerify(pIn3);
65109
 
  /* See note about index shifting on OP_ReadCookie */
65110
 
  rc = sqlite3BtreeUpdateMeta(u.au.pDb->pBt, pOp->p2, (int)pIn3->u.i);
65111
 
  if( pOp->p2==BTREE_SCHEMA_VERSION ){
65112
 
    /* When the schema cookie changes, record the new cookie internally */
65113
 
    u.au.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
65114
 
    db->flags |= SQLITE_InternChanges;
65115
 
  }else if( pOp->p2==BTREE_FILE_FORMAT ){
65116
 
    /* Record changes in the file format */
65117
 
    u.au.pDb->pSchema->file_format = (u8)pIn3->u.i;
65118
 
  }
65119
 
  if( pOp->p1==1 ){
65120
 
    /* Invalidate all prepared statements whenever the TEMP database
65121
 
    ** schema is changed.  Ticket #1644 */
65122
 
    sqlite3ExpirePreparedStatements(db);
65123
 
    p->expired = 0;
65124
 
  }
65125
 
  break;
65126
 
}
65127
 
 
65128
 
/* Opcode: VerifyCookie P1 P2 P3 * *
65129
 
**
65130
 
** Check the value of global database parameter number 0 (the
65131
 
** schema version) and make sure it is equal to P2 and that the
65132
 
** generation counter on the local schema parse equals P3.
65133
 
**
65134
 
** P1 is the database number which is 0 for the main database file
65135
 
** and 1 for the file holding temporary tables and some higher number
65136
 
** for auxiliary databases.
65137
 
**
65138
 
** The cookie changes its value whenever the database schema changes.
65139
 
** This operation is used to detect when that the cookie has changed
65140
 
** and that the current process needs to reread the schema.
65141
 
**
65142
 
** Either a transaction needs to have been started or an OP_Open needs
65143
 
** to be executed (to establish a read lock) before this opcode is
65144
 
** invoked.
65145
 
*/
65146
 
case OP_VerifyCookie: {
65147
 
#if 0  /* local variables moved into u.av */
65148
 
  int iMeta;
65149
 
  int iGen;
65150
 
  Btree *pBt;
65151
 
#endif /* local variables moved into u.av */
65152
 
 
65153
 
  assert( pOp->p1>=0 && pOp->p1<db->nDb );
65154
 
  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
65155
 
  assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
65156
 
  u.av.pBt = db->aDb[pOp->p1].pBt;
65157
 
  if( u.av.pBt ){
65158
 
    sqlite3BtreeGetMeta(u.av.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.av.iMeta);
65159
 
    u.av.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
65160
 
  }else{
65161
 
    u.av.iGen = u.av.iMeta = 0;
65162
 
  }
65163
 
  if( u.av.iMeta!=pOp->p2 || u.av.iGen!=pOp->p3 ){
65164
 
    sqlite3DbFree(db, p->zErrMsg);
65165
 
    p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
65166
 
    /* If the schema-cookie from the database file matches the cookie
65167
 
    ** stored with the in-memory representation of the schema, do
65168
 
    ** not reload the schema from the database file.
65169
 
    **
65170
 
    ** If virtual-tables are in use, this is not just an optimization.
65171
 
    ** Often, v-tables store their data in other SQLite tables, which
65172
 
    ** are queried from within xNext() and other v-table methods using
65173
 
    ** prepared queries. If such a query is out-of-date, we do not want to
65174
 
    ** discard the database schema, as the user code implementing the
65175
 
    ** v-table would have to be ready for the sqlite3_vtab structure itself
65176
 
    ** to be invalidated whenever sqlite3_step() is called from within
65177
 
    ** a v-table method.
65178
 
    */
65179
 
    if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.av.iMeta ){
65180
 
      sqlite3ResetInternalSchema(db, pOp->p1);
65181
 
    }
65182
 
 
65183
 
    p->expired = 1;
65184
 
    rc = SQLITE_SCHEMA;
65185
 
  }
65186
 
  break;
65187
 
}
65188
 
 
65189
 
/* Opcode: OpenRead P1 P2 P3 P4 P5
65190
 
**
65191
 
** Open a read-only cursor for the database table whose root page is
65192
 
** P2 in a database file.  The database file is determined by P3. 
65193
 
** P3==0 means the main database, P3==1 means the database used for 
65194
 
** temporary tables, and P3>1 means used the corresponding attached
65195
 
** database.  Give the new cursor an identifier of P1.  The P1
65196
 
** values need not be contiguous but all P1 values should be small integers.
65197
 
** It is an error for P1 to be negative.
65198
 
**
65199
 
** If P5!=0 then use the content of register P2 as the root page, not
65200
 
** the value of P2 itself.
65201
 
**
65202
 
** There will be a read lock on the database whenever there is an
65203
 
** open cursor.  If the database was unlocked prior to this instruction
65204
 
** then a read lock is acquired as part of this instruction.  A read
65205
 
** lock allows other processes to read the database but prohibits
65206
 
** any other process from modifying the database.  The read lock is
65207
 
** released when all cursors are closed.  If this instruction attempts
65208
 
** to get a read lock but fails, the script terminates with an
65209
 
** SQLITE_BUSY error code.
65210
 
**
65211
 
** The P4 value may be either an integer (P4_INT32) or a pointer to
65212
 
** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
65213
 
** structure, then said structure defines the content and collating 
65214
 
** sequence of the index being opened. Otherwise, if P4 is an integer 
65215
 
** value, it is set to the number of columns in the table.
65216
 
**
65217
 
** See also OpenWrite.
65218
 
*/
65219
 
/* Opcode: OpenWrite P1 P2 P3 P4 P5
65220
 
**
65221
 
** Open a read/write cursor named P1 on the table or index whose root
65222
 
** page is P2.  Or if P5!=0 use the content of register P2 to find the
65223
 
** root page.
65224
 
**
65225
 
** The P4 value may be either an integer (P4_INT32) or a pointer to
65226
 
** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
65227
 
** structure, then said structure defines the content and collating 
65228
 
** sequence of the index being opened. Otherwise, if P4 is an integer 
65229
 
** value, it is set to the number of columns in the table, or to the
65230
 
** largest index of any column of the table that is actually used.
65231
 
**
65232
 
** This instruction works just like OpenRead except that it opens the cursor
65233
 
** in read/write mode.  For a given table, there can be one or more read-only
65234
 
** cursors or a single read/write cursor but not both.
65235
 
**
65236
 
** See also OpenRead.
65237
 
*/
65238
 
case OP_OpenRead:
65239
 
case OP_OpenWrite: {
65240
 
#if 0  /* local variables moved into u.aw */
65241
 
  int nField;
65242
 
  KeyInfo *pKeyInfo;
65243
 
  int p2;
65244
 
  int iDb;
65245
 
  int wrFlag;
65246
 
  Btree *pX;
65247
 
  VdbeCursor *pCur;
65248
 
  Db *pDb;
65249
 
#endif /* local variables moved into u.aw */
65250
 
 
65251
 
  if( p->expired ){
65252
 
    rc = SQLITE_ABORT;
65253
 
    break;
65254
 
  }
65255
 
 
65256
 
  u.aw.nField = 0;
65257
 
  u.aw.pKeyInfo = 0;
65258
 
  u.aw.p2 = pOp->p2;
65259
 
  u.aw.iDb = pOp->p3;
65260
 
  assert( u.aw.iDb>=0 && u.aw.iDb<db->nDb );
65261
 
  assert( (p->btreeMask & (((yDbMask)1)<<u.aw.iDb))!=0 );
65262
 
  u.aw.pDb = &db->aDb[u.aw.iDb];
65263
 
  u.aw.pX = u.aw.pDb->pBt;
65264
 
  assert( u.aw.pX!=0 );
65265
 
  if( pOp->opcode==OP_OpenWrite ){
65266
 
    u.aw.wrFlag = 1;
65267
 
    assert( sqlite3SchemaMutexHeld(db, u.aw.iDb, 0) );
65268
 
    if( u.aw.pDb->pSchema->file_format < p->minWriteFileFormat ){
65269
 
      p->minWriteFileFormat = u.aw.pDb->pSchema->file_format;
65270
 
    }
65271
 
  }else{
65272
 
    u.aw.wrFlag = 0;
65273
 
  }
65274
 
  if( pOp->p5 ){
65275
 
    assert( u.aw.p2>0 );
65276
 
    assert( u.aw.p2<=p->nMem );
65277
 
    pIn2 = &aMem[u.aw.p2];
65278
 
    assert( memIsValid(pIn2) );
65279
 
    assert( (pIn2->flags & MEM_Int)!=0 );
65280
 
    sqlite3VdbeMemIntegerify(pIn2);
65281
 
    u.aw.p2 = (int)pIn2->u.i;
65282
 
    /* The u.aw.p2 value always comes from a prior OP_CreateTable opcode and
65283
 
    ** that opcode will always set the u.aw.p2 value to 2 or more or else fail.
65284
 
    ** If there were a failure, the prepared statement would have halted
65285
 
    ** before reaching this instruction. */
65286
 
    if( NEVER(u.aw.p2<2) ) {
65287
 
      rc = SQLITE_CORRUPT_BKPT;
65288
 
      goto abort_due_to_error;
65289
 
    }
65290
 
  }
65291
 
  if( pOp->p4type==P4_KEYINFO ){
65292
 
    u.aw.pKeyInfo = pOp->p4.pKeyInfo;
65293
 
    u.aw.pKeyInfo->enc = ENC(p->db);
65294
 
    u.aw.nField = u.aw.pKeyInfo->nField+1;
65295
 
  }else if( pOp->p4type==P4_INT32 ){
65296
 
    u.aw.nField = pOp->p4.i;
65297
 
  }
65298
 
  assert( pOp->p1>=0 );
65299
 
  u.aw.pCur = allocateCursor(p, pOp->p1, u.aw.nField, u.aw.iDb, 1);
65300
 
  if( u.aw.pCur==0 ) goto no_mem;
65301
 
  u.aw.pCur->nullRow = 1;
65302
 
  u.aw.pCur->isOrdered = 1;
65303
 
  rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor);
65304
 
  u.aw.pCur->pKeyInfo = u.aw.pKeyInfo;
65305
 
 
65306
 
  /* Since it performs no memory allocation or IO, the only values that
65307
 
  ** sqlite3BtreeCursor() may return are SQLITE_EMPTY and SQLITE_OK.
65308
 
  ** SQLITE_EMPTY is only returned when attempting to open the table
65309
 
  ** rooted at page 1 of a zero-byte database.  */
65310
 
  assert( rc==SQLITE_EMPTY || rc==SQLITE_OK );
65311
 
  if( rc==SQLITE_EMPTY ){
65312
 
    u.aw.pCur->pCursor = 0;
65313
 
    rc = SQLITE_OK;
65314
 
  }
65315
 
 
65316
 
  /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
65317
 
  ** SQLite used to check if the root-page flags were sane at this point
65318
 
  ** and report database corruption if they were not, but this check has
65319
 
  ** since moved into the btree layer.  */
65320
 
  u.aw.pCur->isTable = pOp->p4type!=P4_KEYINFO;
65321
 
  u.aw.pCur->isIndex = !u.aw.pCur->isTable;
65322
 
  break;
65323
 
}
65324
 
 
65325
 
/* Opcode: OpenEphemeral P1 P2 * P4 *
65326
 
**
65327
 
** Open a new cursor P1 to a transient table.
65328
 
** The cursor is always opened read/write even if 
65329
 
** the main database is read-only.  The ephemeral
65330
 
** table is deleted automatically when the cursor is closed.
65331
 
**
65332
 
** P2 is the number of columns in the ephemeral table.
65333
 
** The cursor points to a BTree table if P4==0 and to a BTree index
65334
 
** if P4 is not 0.  If P4 is not NULL, it points to a KeyInfo structure
65335
 
** that defines the format of keys in the index.
65336
 
**
65337
 
** This opcode was once called OpenTemp.  But that created
65338
 
** confusion because the term "temp table", might refer either
65339
 
** to a TEMP table at the SQL level, or to a table opened by
65340
 
** this opcode.  Then this opcode was call OpenVirtual.  But
65341
 
** that created confusion with the whole virtual-table idea.
65342
 
*/
65343
 
/* Opcode: OpenAutoindex P1 P2 * P4 *
65344
 
**
65345
 
** This opcode works the same as OP_OpenEphemeral.  It has a
65346
 
** different name to distinguish its use.  Tables created using
65347
 
** by this opcode will be used for automatically created transient
65348
 
** indices in joins.
65349
 
*/
65350
 
case OP_OpenAutoindex: 
65351
 
case OP_OpenEphemeral: {
65352
 
#if 0  /* local variables moved into u.ax */
65353
 
  VdbeCursor *pCx;
65354
 
#endif /* local variables moved into u.ax */
65355
 
  static const int vfsFlags =
65356
 
      SQLITE_OPEN_READWRITE |
65357
 
      SQLITE_OPEN_CREATE |
65358
 
      SQLITE_OPEN_EXCLUSIVE |
65359
 
      SQLITE_OPEN_DELETEONCLOSE |
65360
 
      SQLITE_OPEN_TRANSIENT_DB;
65361
 
 
65362
 
  assert( pOp->p1>=0 );
65363
 
  u.ax.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
65364
 
  if( u.ax.pCx==0 ) goto no_mem;
65365
 
  u.ax.pCx->nullRow = 1;
65366
 
  rc = sqlite3BtreeOpen(0, db, &u.ax.pCx->pBt,
65367
 
                        BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
65368
 
  if( rc==SQLITE_OK ){
65369
 
    rc = sqlite3BtreeBeginTrans(u.ax.pCx->pBt, 1);
65370
 
  }
65371
 
  if( rc==SQLITE_OK ){
65372
 
    /* If a transient index is required, create it by calling
65373
 
    ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
65374
 
    ** opening it. If a transient table is required, just use the
65375
 
    ** automatically created table with root-page 1 (an BLOB_INTKEY table).
65376
 
    */
65377
 
    if( pOp->p4.pKeyInfo ){
65378
 
      int pgno;
65379
 
      assert( pOp->p4type==P4_KEYINFO );
65380
 
      rc = sqlite3BtreeCreateTable(u.ax.pCx->pBt, &pgno, BTREE_BLOBKEY);
65381
 
      if( rc==SQLITE_OK ){
65382
 
        assert( pgno==MASTER_ROOT+1 );
65383
 
        rc = sqlite3BtreeCursor(u.ax.pCx->pBt, pgno, 1,
65384
 
                                (KeyInfo*)pOp->p4.z, u.ax.pCx->pCursor);
65385
 
        u.ax.pCx->pKeyInfo = pOp->p4.pKeyInfo;
65386
 
        u.ax.pCx->pKeyInfo->enc = ENC(p->db);
65387
 
      }
65388
 
      u.ax.pCx->isTable = 0;
65389
 
    }else{
65390
 
      rc = sqlite3BtreeCursor(u.ax.pCx->pBt, MASTER_ROOT, 1, 0, u.ax.pCx->pCursor);
65391
 
      u.ax.pCx->isTable = 1;
65392
 
    }
65393
 
  }
65394
 
  u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
65395
 
  u.ax.pCx->isIndex = !u.ax.pCx->isTable;
65396
 
  break;
65397
 
}
65398
 
 
65399
 
/* Opcode: OpenPseudo P1 P2 P3 * *
65400
 
**
65401
 
** Open a new cursor that points to a fake table that contains a single
65402
 
** row of data.  The content of that one row in the content of memory
65403
 
** register P2.  In other words, cursor P1 becomes an alias for the 
65404
 
** MEM_Blob content contained in register P2.
65405
 
**
65406
 
** A pseudo-table created by this opcode is used to hold a single
65407
 
** row output from the sorter so that the row can be decomposed into
65408
 
** individual columns using the OP_Column opcode.  The OP_Column opcode
65409
 
** is the only cursor opcode that works with a pseudo-table.
65410
 
**
65411
 
** P3 is the number of fields in the records that will be stored by
65412
 
** the pseudo-table.
65413
 
*/
65414
 
case OP_OpenPseudo: {
65415
 
#if 0  /* local variables moved into u.ay */
65416
 
  VdbeCursor *pCx;
65417
 
#endif /* local variables moved into u.ay */
65418
 
 
65419
 
  assert( pOp->p1>=0 );
65420
 
  u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
65421
 
  if( u.ay.pCx==0 ) goto no_mem;
65422
 
  u.ay.pCx->nullRow = 1;
65423
 
  u.ay.pCx->pseudoTableReg = pOp->p2;
65424
 
  u.ay.pCx->isTable = 1;
65425
 
  u.ay.pCx->isIndex = 0;
65426
 
  break;
65427
 
}
65428
 
 
65429
 
/* Opcode: Close P1 * * * *
65430
 
**
65431
 
** Close a cursor previously opened as P1.  If P1 is not
65432
 
** currently open, this instruction is a no-op.
65433
 
*/
65434
 
case OP_Close: {
65435
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
65436
 
  sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
65437
 
  p->apCsr[pOp->p1] = 0;
65438
 
  break;
65439
 
}
65440
 
 
65441
 
/* Opcode: SeekGe P1 P2 P3 P4 *
65442
 
**
65443
 
** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
65444
 
** use the value in register P3 as the key.  If cursor P1 refers 
65445
 
** to an SQL index, then P3 is the first in an array of P4 registers 
65446
 
** that are used as an unpacked index key. 
65447
 
**
65448
 
** Reposition cursor P1 so that  it points to the smallest entry that 
65449
 
** is greater than or equal to the key value. If there are no records 
65450
 
** greater than or equal to the key and P2 is not zero, then jump to P2.
65451
 
**
65452
 
** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
65453
 
*/
65454
 
/* Opcode: SeekGt P1 P2 P3 P4 *
65455
 
**
65456
 
** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
65457
 
** use the value in register P3 as a key. If cursor P1 refers 
65458
 
** to an SQL index, then P3 is the first in an array of P4 registers 
65459
 
** that are used as an unpacked index key. 
65460
 
**
65461
 
** Reposition cursor P1 so that  it points to the smallest entry that 
65462
 
** is greater than the key value. If there are no records greater than 
65463
 
** the key and P2 is not zero, then jump to P2.
65464
 
**
65465
 
** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
65466
 
*/
65467
 
/* Opcode: SeekLt P1 P2 P3 P4 * 
65468
 
**
65469
 
** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
65470
 
** use the value in register P3 as a key. If cursor P1 refers 
65471
 
** to an SQL index, then P3 is the first in an array of P4 registers 
65472
 
** that are used as an unpacked index key. 
65473
 
**
65474
 
** Reposition cursor P1 so that  it points to the largest entry that 
65475
 
** is less than the key value. If there are no records less than 
65476
 
** the key and P2 is not zero, then jump to P2.
65477
 
**
65478
 
** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
65479
 
*/
65480
 
/* Opcode: SeekLe P1 P2 P3 P4 *
65481
 
**
65482
 
** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
65483
 
** use the value in register P3 as a key. If cursor P1 refers 
65484
 
** to an SQL index, then P3 is the first in an array of P4 registers 
65485
 
** that are used as an unpacked index key. 
65486
 
**
65487
 
** Reposition cursor P1 so that it points to the largest entry that 
65488
 
** is less than or equal to the key value. If there are no records 
65489
 
** less than or equal to the key and P2 is not zero, then jump to P2.
65490
 
**
65491
 
** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
65492
 
*/
65493
 
case OP_SeekLt:         /* jump, in3 */
65494
 
case OP_SeekLe:         /* jump, in3 */
65495
 
case OP_SeekGe:         /* jump, in3 */
65496
 
case OP_SeekGt: {       /* jump, in3 */
65497
 
#if 0  /* local variables moved into u.az */
65498
 
  int res;
65499
 
  int oc;
65500
 
  VdbeCursor *pC;
65501
 
  UnpackedRecord r;
65502
 
  int nField;
65503
 
  i64 iKey;      /* The rowid we are to seek to */
65504
 
#endif /* local variables moved into u.az */
65505
 
 
65506
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
65507
 
  assert( pOp->p2!=0 );
65508
 
  u.az.pC = p->apCsr[pOp->p1];
65509
 
  assert( u.az.pC!=0 );
65510
 
  assert( u.az.pC->pseudoTableReg==0 );
65511
 
  assert( OP_SeekLe == OP_SeekLt+1 );
65512
 
  assert( OP_SeekGe == OP_SeekLt+2 );
65513
 
  assert( OP_SeekGt == OP_SeekLt+3 );
65514
 
  assert( u.az.pC->isOrdered );
65515
 
  if( u.az.pC->pCursor!=0 ){
65516
 
    u.az.oc = pOp->opcode;
65517
 
    u.az.pC->nullRow = 0;
65518
 
    if( u.az.pC->isTable ){
65519
 
      /* The input value in P3 might be of any type: integer, real, string,
65520
 
      ** blob, or NULL.  But it needs to be an integer before we can do
65521
 
      ** the seek, so covert it. */
65522
 
      pIn3 = &aMem[pOp->p3];
65523
 
      applyNumericAffinity(pIn3);
65524
 
      u.az.iKey = sqlite3VdbeIntValue(pIn3);
65525
 
      u.az.pC->rowidIsValid = 0;
65526
 
 
65527
 
      /* If the P3 value could not be converted into an integer without
65528
 
      ** loss of information, then special processing is required... */
65529
 
      if( (pIn3->flags & MEM_Int)==0 ){
65530
 
        if( (pIn3->flags & MEM_Real)==0 ){
65531
 
          /* If the P3 value cannot be converted into any kind of a number,
65532
 
          ** then the seek is not possible, so jump to P2 */
65533
 
          pc = pOp->p2 - 1;
65534
 
          break;
65535
 
        }
65536
 
        /* If we reach this point, then the P3 value must be a floating
65537
 
        ** point number. */
65538
 
        assert( (pIn3->flags & MEM_Real)!=0 );
65539
 
 
65540
 
        if( u.az.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.az.iKey || pIn3->r>0) ){
65541
 
          /* The P3 value is too large in magnitude to be expressed as an
65542
 
          ** integer. */
65543
 
          u.az.res = 1;
65544
 
          if( pIn3->r<0 ){
65545
 
            if( u.az.oc>=OP_SeekGe ){  assert( u.az.oc==OP_SeekGe || u.az.oc==OP_SeekGt );
65546
 
              rc = sqlite3BtreeFirst(u.az.pC->pCursor, &u.az.res);
65547
 
              if( rc!=SQLITE_OK ) goto abort_due_to_error;
65548
 
            }
65549
 
          }else{
65550
 
            if( u.az.oc<=OP_SeekLe ){  assert( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekLe );
65551
 
              rc = sqlite3BtreeLast(u.az.pC->pCursor, &u.az.res);
65552
 
              if( rc!=SQLITE_OK ) goto abort_due_to_error;
65553
 
            }
65554
 
          }
65555
 
          if( u.az.res ){
65556
 
            pc = pOp->p2 - 1;
65557
 
          }
65558
 
          break;
65559
 
        }else if( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekGe ){
65560
 
          /* Use the ceiling() function to convert real->int */
65561
 
          if( pIn3->r > (double)u.az.iKey ) u.az.iKey++;
65562
 
        }else{
65563
 
          /* Use the floor() function to convert real->int */
65564
 
          assert( u.az.oc==OP_SeekLe || u.az.oc==OP_SeekGt );
65565
 
          if( pIn3->r < (double)u.az.iKey ) u.az.iKey--;
65566
 
        }
65567
 
      }
65568
 
      rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, 0, (u64)u.az.iKey, 0, &u.az.res);
65569
 
      if( rc!=SQLITE_OK ){
65570
 
        goto abort_due_to_error;
65571
 
      }
65572
 
      if( u.az.res==0 ){
65573
 
        u.az.pC->rowidIsValid = 1;
65574
 
        u.az.pC->lastRowid = u.az.iKey;
65575
 
      }
65576
 
    }else{
65577
 
      u.az.nField = pOp->p4.i;
65578
 
      assert( pOp->p4type==P4_INT32 );
65579
 
      assert( u.az.nField>0 );
65580
 
      u.az.r.pKeyInfo = u.az.pC->pKeyInfo;
65581
 
      u.az.r.nField = (u16)u.az.nField;
65582
 
 
65583
 
      /* The next line of code computes as follows, only faster:
65584
 
      **   if( u.az.oc==OP_SeekGt || u.az.oc==OP_SeekLe ){
65585
 
      **     u.az.r.flags = UNPACKED_INCRKEY;
65586
 
      **   }else{
65587
 
      **     u.az.r.flags = 0;
65588
 
      **   }
65589
 
      */
65590
 
      u.az.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.az.oc - OP_SeekLt)));
65591
 
      assert( u.az.oc!=OP_SeekGt || u.az.r.flags==UNPACKED_INCRKEY );
65592
 
      assert( u.az.oc!=OP_SeekLe || u.az.r.flags==UNPACKED_INCRKEY );
65593
 
      assert( u.az.oc!=OP_SeekGe || u.az.r.flags==0 );
65594
 
      assert( u.az.oc!=OP_SeekLt || u.az.r.flags==0 );
65595
 
 
65596
 
      u.az.r.aMem = &aMem[pOp->p3];
65597
 
#ifdef SQLITE_DEBUG
65598
 
      { int i; for(i=0; i<u.az.r.nField; i++) assert( memIsValid(&u.az.r.aMem[i]) ); }
65599
 
#endif
65600
 
      ExpandBlob(u.az.r.aMem);
65601
 
      rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, &u.az.r, 0, 0, &u.az.res);
65602
 
      if( rc!=SQLITE_OK ){
65603
 
        goto abort_due_to_error;
65604
 
      }
65605
 
      u.az.pC->rowidIsValid = 0;
65606
 
    }
65607
 
    u.az.pC->deferredMoveto = 0;
65608
 
    u.az.pC->cacheStatus = CACHE_STALE;
65609
 
#ifdef SQLITE_TEST
65610
 
    sqlite3_search_count++;
65611
 
#endif
65612
 
    if( u.az.oc>=OP_SeekGe ){  assert( u.az.oc==OP_SeekGe || u.az.oc==OP_SeekGt );
65613
 
      if( u.az.res<0 || (u.az.res==0 && u.az.oc==OP_SeekGt) ){
65614
 
        rc = sqlite3BtreeNext(u.az.pC->pCursor, &u.az.res);
65615
 
        if( rc!=SQLITE_OK ) goto abort_due_to_error;
65616
 
        u.az.pC->rowidIsValid = 0;
65617
 
      }else{
65618
 
        u.az.res = 0;
65619
 
      }
65620
 
    }else{
65621
 
      assert( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekLe );
65622
 
      if( u.az.res>0 || (u.az.res==0 && u.az.oc==OP_SeekLt) ){
65623
 
        rc = sqlite3BtreePrevious(u.az.pC->pCursor, &u.az.res);
65624
 
        if( rc!=SQLITE_OK ) goto abort_due_to_error;
65625
 
        u.az.pC->rowidIsValid = 0;
65626
 
      }else{
65627
 
        /* u.az.res might be negative because the table is empty.  Check to
65628
 
        ** see if this is the case.
65629
 
        */
65630
 
        u.az.res = sqlite3BtreeEof(u.az.pC->pCursor);
65631
 
      }
65632
 
    }
65633
 
    assert( pOp->p2>0 );
65634
 
    if( u.az.res ){
65635
 
      pc = pOp->p2 - 1;
65636
 
    }
65637
 
  }else{
65638
 
    /* This happens when attempting to open the sqlite3_master table
65639
 
    ** for read access returns SQLITE_EMPTY. In this case always
65640
 
    ** take the jump (since there are no records in the table).
65641
 
    */
65642
 
    pc = pOp->p2 - 1;
65643
 
  }
65644
 
  break;
65645
 
}
65646
 
 
65647
 
/* Opcode: Seek P1 P2 * * *
65648
 
**
65649
 
** P1 is an open table cursor and P2 is a rowid integer.  Arrange
65650
 
** for P1 to move so that it points to the rowid given by P2.
65651
 
**
65652
 
** This is actually a deferred seek.  Nothing actually happens until
65653
 
** the cursor is used to read a record.  That way, if no reads
65654
 
** occur, no unnecessary I/O happens.
65655
 
*/
65656
 
case OP_Seek: {    /* in2 */
65657
 
#if 0  /* local variables moved into u.ba */
65658
 
  VdbeCursor *pC;
65659
 
#endif /* local variables moved into u.ba */
65660
 
 
65661
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
65662
 
  u.ba.pC = p->apCsr[pOp->p1];
65663
 
  assert( u.ba.pC!=0 );
65664
 
  if( ALWAYS(u.ba.pC->pCursor!=0) ){
65665
 
    assert( u.ba.pC->isTable );
65666
 
    u.ba.pC->nullRow = 0;
65667
 
    pIn2 = &aMem[pOp->p2];
65668
 
    u.ba.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
65669
 
    u.ba.pC->rowidIsValid = 0;
65670
 
    u.ba.pC->deferredMoveto = 1;
65671
 
  }
65672
 
  break;
65673
 
}
65674
 
  
65675
 
 
65676
 
/* Opcode: Found P1 P2 P3 P4 *
65677
 
**
65678
 
** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
65679
 
** P4>0 then register P3 is the first of P4 registers that form an unpacked
65680
 
** record.
65681
 
**
65682
 
** Cursor P1 is on an index btree.  If the record identified by P3 and P4
65683
 
** is a prefix of any entry in P1 then a jump is made to P2 and
65684
 
** P1 is left pointing at the matching entry.
65685
 
*/
65686
 
/* Opcode: NotFound P1 P2 P3 P4 *
65687
 
**
65688
 
** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
65689
 
** P4>0 then register P3 is the first of P4 registers that form an unpacked
65690
 
** record.
65691
 
** 
65692
 
** Cursor P1 is on an index btree.  If the record identified by P3 and P4
65693
 
** is not the prefix of any entry in P1 then a jump is made to P2.  If P1 
65694
 
** does contain an entry whose prefix matches the P3/P4 record then control
65695
 
** falls through to the next instruction and P1 is left pointing at the
65696
 
** matching entry.
65697
 
**
65698
 
** See also: Found, NotExists, IsUnique
65699
 
*/
65700
 
case OP_NotFound:       /* jump, in3 */
65701
 
case OP_Found: {        /* jump, in3 */
65702
 
#if 0  /* local variables moved into u.bb */
65703
 
  int alreadyExists;
65704
 
  VdbeCursor *pC;
65705
 
  int res;
65706
 
  UnpackedRecord *pIdxKey;
65707
 
  UnpackedRecord r;
65708
 
  char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
65709
 
#endif /* local variables moved into u.bb */
65710
 
 
65711
 
#ifdef SQLITE_TEST
65712
 
  sqlite3_found_count++;
65713
 
#endif
65714
 
 
65715
 
  u.bb.alreadyExists = 0;
65716
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
65717
 
  assert( pOp->p4type==P4_INT32 );
65718
 
  u.bb.pC = p->apCsr[pOp->p1];
65719
 
  assert( u.bb.pC!=0 );
65720
 
  pIn3 = &aMem[pOp->p3];
65721
 
  if( ALWAYS(u.bb.pC->pCursor!=0) ){
65722
 
 
65723
 
    assert( u.bb.pC->isTable==0 );
65724
 
    if( pOp->p4.i>0 ){
65725
 
      u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo;
65726
 
      u.bb.r.nField = (u16)pOp->p4.i;
65727
 
      u.bb.r.aMem = pIn3;
65728
 
#ifdef SQLITE_DEBUG
65729
 
      { int i; for(i=0; i<u.bb.r.nField; i++) assert( memIsValid(&u.bb.r.aMem[i]) ); }
65730
 
#endif
65731
 
      u.bb.r.flags = UNPACKED_PREFIX_MATCH;
65732
 
      u.bb.pIdxKey = &u.bb.r;
65733
 
    }else{
65734
 
      assert( pIn3->flags & MEM_Blob );
65735
 
      assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
65736
 
      u.bb.pIdxKey = sqlite3VdbeRecordUnpack(u.bb.pC->pKeyInfo, pIn3->n, pIn3->z,
65737
 
                                        u.bb.aTempRec, sizeof(u.bb.aTempRec));
65738
 
      if( u.bb.pIdxKey==0 ){
65739
 
        goto no_mem;
65740
 
      }
65741
 
      u.bb.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
65742
 
    }
65743
 
    rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, u.bb.pIdxKey, 0, 0, &u.bb.res);
65744
 
    if( pOp->p4.i==0 ){
65745
 
      sqlite3VdbeDeleteUnpackedRecord(u.bb.pIdxKey);
65746
 
    }
65747
 
    if( rc!=SQLITE_OK ){
65748
 
      break;
65749
 
    }
65750
 
    u.bb.alreadyExists = (u.bb.res==0);
65751
 
    u.bb.pC->deferredMoveto = 0;
65752
 
    u.bb.pC->cacheStatus = CACHE_STALE;
65753
 
  }
65754
 
  if( pOp->opcode==OP_Found ){
65755
 
    if( u.bb.alreadyExists ) pc = pOp->p2 - 1;
65756
 
  }else{
65757
 
    if( !u.bb.alreadyExists ) pc = pOp->p2 - 1;
65758
 
  }
65759
 
  break;
65760
 
}
65761
 
 
65762
 
/* Opcode: IsUnique P1 P2 P3 P4 *
65763
 
**
65764
 
** Cursor P1 is open on an index b-tree - that is to say, a btree which
65765
 
** no data and where the key are records generated by OP_MakeRecord with
65766
 
** the list field being the integer ROWID of the entry that the index
65767
 
** entry refers to.
65768
 
**
65769
 
** The P3 register contains an integer record number. Call this record 
65770
 
** number R. Register P4 is the first in a set of N contiguous registers
65771
 
** that make up an unpacked index key that can be used with cursor P1.
65772
 
** The value of N can be inferred from the cursor. N includes the rowid
65773
 
** value appended to the end of the index record. This rowid value may
65774
 
** or may not be the same as R.
65775
 
**
65776
 
** If any of the N registers beginning with register P4 contains a NULL
65777
 
** value, jump immediately to P2.
65778
 
**
65779
 
** Otherwise, this instruction checks if cursor P1 contains an entry
65780
 
** where the first (N-1) fields match but the rowid value at the end
65781
 
** of the index entry is not R. If there is no such entry, control jumps
65782
 
** to instruction P2. Otherwise, the rowid of the conflicting index
65783
 
** entry is copied to register P3 and control falls through to the next
65784
 
** instruction.
65785
 
**
65786
 
** See also: NotFound, NotExists, Found
65787
 
*/
65788
 
case OP_IsUnique: {        /* jump, in3 */
65789
 
#if 0  /* local variables moved into u.bc */
65790
 
  u16 ii;
65791
 
  VdbeCursor *pCx;
65792
 
  BtCursor *pCrsr;
65793
 
  u16 nField;
65794
 
  Mem *aMx;
65795
 
  UnpackedRecord r;                  /* B-Tree index search key */
65796
 
  i64 R;                             /* Rowid stored in register P3 */
65797
 
#endif /* local variables moved into u.bc */
65798
 
 
65799
 
  pIn3 = &aMem[pOp->p3];
65800
 
  u.bc.aMx = &aMem[pOp->p4.i];
65801
 
  /* Assert that the values of parameters P1 and P4 are in range. */
65802
 
  assert( pOp->p4type==P4_INT32 );
65803
 
  assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
65804
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
65805
 
 
65806
 
  /* Find the index cursor. */
65807
 
  u.bc.pCx = p->apCsr[pOp->p1];
65808
 
  assert( u.bc.pCx->deferredMoveto==0 );
65809
 
  u.bc.pCx->seekResult = 0;
65810
 
  u.bc.pCx->cacheStatus = CACHE_STALE;
65811
 
  u.bc.pCrsr = u.bc.pCx->pCursor;
65812
 
 
65813
 
  /* If any of the values are NULL, take the jump. */
65814
 
  u.bc.nField = u.bc.pCx->pKeyInfo->nField;
65815
 
  for(u.bc.ii=0; u.bc.ii<u.bc.nField; u.bc.ii++){
65816
 
    if( u.bc.aMx[u.bc.ii].flags & MEM_Null ){
65817
 
      pc = pOp->p2 - 1;
65818
 
      u.bc.pCrsr = 0;
65819
 
      break;
65820
 
    }
65821
 
  }
65822
 
  assert( (u.bc.aMx[u.bc.nField].flags & MEM_Null)==0 );
65823
 
 
65824
 
  if( u.bc.pCrsr!=0 ){
65825
 
    /* Populate the index search key. */
65826
 
    u.bc.r.pKeyInfo = u.bc.pCx->pKeyInfo;
65827
 
    u.bc.r.nField = u.bc.nField + 1;
65828
 
    u.bc.r.flags = UNPACKED_PREFIX_SEARCH;
65829
 
    u.bc.r.aMem = u.bc.aMx;
65830
 
#ifdef SQLITE_DEBUG
65831
 
    { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
65832
 
#endif
65833
 
 
65834
 
    /* Extract the value of u.bc.R from register P3. */
65835
 
    sqlite3VdbeMemIntegerify(pIn3);
65836
 
    u.bc.R = pIn3->u.i;
65837
 
 
65838
 
    /* Search the B-Tree index. If no conflicting record is found, jump
65839
 
    ** to P2. Otherwise, copy the rowid of the conflicting record to
65840
 
    ** register P3 and fall through to the next instruction.  */
65841
 
    rc = sqlite3BtreeMovetoUnpacked(u.bc.pCrsr, &u.bc.r, 0, 0, &u.bc.pCx->seekResult);
65842
 
    if( (u.bc.r.flags & UNPACKED_PREFIX_SEARCH) || u.bc.r.rowid==u.bc.R ){
65843
 
      pc = pOp->p2 - 1;
65844
 
    }else{
65845
 
      pIn3->u.i = u.bc.r.rowid;
65846
 
    }
65847
 
  }
65848
 
  break;
65849
 
}
65850
 
 
65851
 
/* Opcode: NotExists P1 P2 P3 * *
65852
 
**
65853
 
** Use the content of register P3 as a integer key.  If a record 
65854
 
** with that key does not exist in table of P1, then jump to P2. 
65855
 
** If the record does exist, then fall through.  The cursor is left 
65856
 
** pointing to the record if it exists.
65857
 
**
65858
 
** The difference between this operation and NotFound is that this
65859
 
** operation assumes the key is an integer and that P1 is a table whereas
65860
 
** NotFound assumes key is a blob constructed from MakeRecord and
65861
 
** P1 is an index.
65862
 
**
65863
 
** See also: Found, NotFound, IsUnique
65864
 
*/
65865
 
case OP_NotExists: {        /* jump, in3 */
65866
 
#if 0  /* local variables moved into u.bd */
65867
 
  VdbeCursor *pC;
65868
 
  BtCursor *pCrsr;
65869
 
  int res;
65870
 
  u64 iKey;
65871
 
#endif /* local variables moved into u.bd */
65872
 
 
65873
 
  pIn3 = &aMem[pOp->p3];
65874
 
  assert( pIn3->flags & MEM_Int );
65875
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
65876
 
  u.bd.pC = p->apCsr[pOp->p1];
65877
 
  assert( u.bd.pC!=0 );
65878
 
  assert( u.bd.pC->isTable );
65879
 
  assert( u.bd.pC->pseudoTableReg==0 );
65880
 
  u.bd.pCrsr = u.bd.pC->pCursor;
65881
 
  if( u.bd.pCrsr!=0 ){
65882
 
    u.bd.res = 0;
65883
 
    u.bd.iKey = pIn3->u.i;
65884
 
    rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, 0, u.bd.iKey, 0, &u.bd.res);
65885
 
    u.bd.pC->lastRowid = pIn3->u.i;
65886
 
    u.bd.pC->rowidIsValid = u.bd.res==0 ?1:0;
65887
 
    u.bd.pC->nullRow = 0;
65888
 
    u.bd.pC->cacheStatus = CACHE_STALE;
65889
 
    u.bd.pC->deferredMoveto = 0;
65890
 
    if( u.bd.res!=0 ){
65891
 
      pc = pOp->p2 - 1;
65892
 
      assert( u.bd.pC->rowidIsValid==0 );
65893
 
    }
65894
 
    u.bd.pC->seekResult = u.bd.res;
65895
 
  }else{
65896
 
    /* This happens when an attempt to open a read cursor on the
65897
 
    ** sqlite_master table returns SQLITE_EMPTY.
65898
 
    */
65899
 
    pc = pOp->p2 - 1;
65900
 
    assert( u.bd.pC->rowidIsValid==0 );
65901
 
    u.bd.pC->seekResult = 0;
65902
 
  }
65903
 
  break;
65904
 
}
65905
 
 
65906
 
/* Opcode: Sequence P1 P2 * * *
65907
 
**
65908
 
** Find the next available sequence number for cursor P1.
65909
 
** Write the sequence number into register P2.
65910
 
** The sequence number on the cursor is incremented after this
65911
 
** instruction.  
65912
 
*/
65913
 
case OP_Sequence: {           /* out2-prerelease */
65914
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
65915
 
  assert( p->apCsr[pOp->p1]!=0 );
65916
 
  pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
65917
 
  break;
65918
 
}
65919
 
 
65920
 
 
65921
 
/* Opcode: NewRowid P1 P2 P3 * *
65922
 
**
65923
 
** Get a new integer record number (a.k.a "rowid") used as the key to a table.
65924
 
** The record number is not previously used as a key in the database
65925
 
** table that cursor P1 points to.  The new record number is written
65926
 
** written to register P2.
65927
 
**
65928
 
** If P3>0 then P3 is a register in the root frame of this VDBE that holds 
65929
 
** the largest previously generated record number. No new record numbers are
65930
 
** allowed to be less than this value. When this value reaches its maximum, 
65931
 
** a SQLITE_FULL error is generated. The P3 register is updated with the '
65932
 
** generated record number. This P3 mechanism is used to help implement the
65933
 
** AUTOINCREMENT feature.
65934
 
*/
65935
 
case OP_NewRowid: {           /* out2-prerelease */
65936
 
#if 0  /* local variables moved into u.be */
65937
 
  i64 v;                 /* The new rowid */
65938
 
  VdbeCursor *pC;        /* Cursor of table to get the new rowid */
65939
 
  int res;               /* Result of an sqlite3BtreeLast() */
65940
 
  int cnt;               /* Counter to limit the number of searches */
65941
 
  Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
65942
 
  VdbeFrame *pFrame;     /* Root frame of VDBE */
65943
 
#endif /* local variables moved into u.be */
65944
 
 
65945
 
  u.be.v = 0;
65946
 
  u.be.res = 0;
65947
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
65948
 
  u.be.pC = p->apCsr[pOp->p1];
65949
 
  assert( u.be.pC!=0 );
65950
 
  if( NEVER(u.be.pC->pCursor==0) ){
65951
 
    /* The zero initialization above is all that is needed */
65952
 
  }else{
65953
 
    /* The next rowid or record number (different terms for the same
65954
 
    ** thing) is obtained in a two-step algorithm.
65955
 
    **
65956
 
    ** First we attempt to find the largest existing rowid and add one
65957
 
    ** to that.  But if the largest existing rowid is already the maximum
65958
 
    ** positive integer, we have to fall through to the second
65959
 
    ** probabilistic algorithm
65960
 
    **
65961
 
    ** The second algorithm is to select a rowid at random and see if
65962
 
    ** it already exists in the table.  If it does not exist, we have
65963
 
    ** succeeded.  If the random rowid does exist, we select a new one
65964
 
    ** and try again, up to 100 times.
65965
 
    */
65966
 
    assert( u.be.pC->isTable );
65967
 
 
65968
 
#ifdef SQLITE_32BIT_ROWID
65969
 
#   define MAX_ROWID 0x7fffffff
65970
 
#else
65971
 
    /* Some compilers complain about constants of the form 0x7fffffffffffffff.
65972
 
    ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
65973
 
    ** to provide the constant while making all compilers happy.
65974
 
    */
65975
 
#   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
65976
 
#endif
65977
 
 
65978
 
    if( !u.be.pC->useRandomRowid ){
65979
 
      u.be.v = sqlite3BtreeGetCachedRowid(u.be.pC->pCursor);
65980
 
      if( u.be.v==0 ){
65981
 
        rc = sqlite3BtreeLast(u.be.pC->pCursor, &u.be.res);
65982
 
        if( rc!=SQLITE_OK ){
65983
 
          goto abort_due_to_error;
65984
 
        }
65985
 
        if( u.be.res ){
65986
 
          u.be.v = 1;   /* IMP: R-61914-48074 */
65987
 
        }else{
65988
 
          assert( sqlite3BtreeCursorIsValid(u.be.pC->pCursor) );
65989
 
          rc = sqlite3BtreeKeySize(u.be.pC->pCursor, &u.be.v);
65990
 
          assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
65991
 
          if( u.be.v==MAX_ROWID ){
65992
 
            u.be.pC->useRandomRowid = 1;
65993
 
          }else{
65994
 
            u.be.v++;   /* IMP: R-29538-34987 */
65995
 
          }
65996
 
        }
65997
 
      }
65998
 
 
65999
 
#ifndef SQLITE_OMIT_AUTOINCREMENT
66000
 
      if( pOp->p3 ){
66001
 
        /* Assert that P3 is a valid memory cell. */
66002
 
        assert( pOp->p3>0 );
66003
 
        if( p->pFrame ){
66004
 
          for(u.be.pFrame=p->pFrame; u.be.pFrame->pParent; u.be.pFrame=u.be.pFrame->pParent);
66005
 
          /* Assert that P3 is a valid memory cell. */
66006
 
          assert( pOp->p3<=u.be.pFrame->nMem );
66007
 
          u.be.pMem = &u.be.pFrame->aMem[pOp->p3];
66008
 
        }else{
66009
 
          /* Assert that P3 is a valid memory cell. */
66010
 
          assert( pOp->p3<=p->nMem );
66011
 
          u.be.pMem = &aMem[pOp->p3];
66012
 
          memAboutToChange(p, u.be.pMem);
66013
 
        }
66014
 
        assert( memIsValid(u.be.pMem) );
66015
 
 
66016
 
        REGISTER_TRACE(pOp->p3, u.be.pMem);
66017
 
        sqlite3VdbeMemIntegerify(u.be.pMem);
66018
 
        assert( (u.be.pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
66019
 
        if( u.be.pMem->u.i==MAX_ROWID || u.be.pC->useRandomRowid ){
66020
 
          rc = SQLITE_FULL;   /* IMP: R-12275-61338 */
66021
 
          goto abort_due_to_error;
66022
 
        }
66023
 
        if( u.be.v<u.be.pMem->u.i+1 ){
66024
 
          u.be.v = u.be.pMem->u.i + 1;
66025
 
        }
66026
 
        u.be.pMem->u.i = u.be.v;
66027
 
      }
66028
 
#endif
66029
 
 
66030
 
      sqlite3BtreeSetCachedRowid(u.be.pC->pCursor, u.be.v<MAX_ROWID ? u.be.v+1 : 0);
66031
 
    }
66032
 
    if( u.be.pC->useRandomRowid ){
66033
 
      /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
66034
 
      ** largest possible integer (9223372036854775807) then the database
66035
 
      ** engine starts picking positive candidate ROWIDs at random until
66036
 
      ** it finds one that is not previously used. */
66037
 
      assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
66038
 
                             ** an AUTOINCREMENT table. */
66039
 
      /* on the first attempt, simply do one more than previous */
66040
 
      u.be.v = db->lastRowid;
66041
 
      u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
66042
 
      u.be.v++; /* ensure non-zero */
66043
 
      u.be.cnt = 0;
66044
 
      while(   ((rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, 0, (u64)u.be.v,
66045
 
                                                 0, &u.be.res))==SQLITE_OK)
66046
 
            && (u.be.res==0)
66047
 
            && (++u.be.cnt<100)){
66048
 
        /* collision - try another random rowid */
66049
 
        sqlite3_randomness(sizeof(u.be.v), &u.be.v);
66050
 
        if( u.be.cnt<5 ){
66051
 
          /* try "small" random rowids for the initial attempts */
66052
 
          u.be.v &= 0xffffff;
66053
 
        }else{
66054
 
          u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
66055
 
        }
66056
 
        u.be.v++; /* ensure non-zero */
66057
 
      }
66058
 
      if( rc==SQLITE_OK && u.be.res==0 ){
66059
 
        rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
66060
 
        goto abort_due_to_error;
66061
 
      }
66062
 
      assert( u.be.v>0 );  /* EV: R-40812-03570 */
66063
 
    }
66064
 
    u.be.pC->rowidIsValid = 0;
66065
 
    u.be.pC->deferredMoveto = 0;
66066
 
    u.be.pC->cacheStatus = CACHE_STALE;
66067
 
  }
66068
 
  pOut->u.i = u.be.v;
66069
 
  break;
66070
 
}
66071
 
 
66072
 
/* Opcode: Insert P1 P2 P3 P4 P5
66073
 
**
66074
 
** Write an entry into the table of cursor P1.  A new entry is
66075
 
** created if it doesn't already exist or the data for an existing
66076
 
** entry is overwritten.  The data is the value MEM_Blob stored in register
66077
 
** number P2. The key is stored in register P3. The key must
66078
 
** be a MEM_Int.
66079
 
**
66080
 
** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
66081
 
** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P5 is set,
66082
 
** then rowid is stored for subsequent return by the
66083
 
** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
66084
 
**
66085
 
** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
66086
 
** the last seek operation (OP_NotExists) was a success, then this
66087
 
** operation will not attempt to find the appropriate row before doing
66088
 
** the insert but will instead overwrite the row that the cursor is
66089
 
** currently pointing to.  Presumably, the prior OP_NotExists opcode
66090
 
** has already positioned the cursor correctly.  This is an optimization
66091
 
** that boosts performance by avoiding redundant seeks.
66092
 
**
66093
 
** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
66094
 
** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
66095
 
** is part of an INSERT operation.  The difference is only important to
66096
 
** the update hook.
66097
 
**
66098
 
** Parameter P4 may point to a string containing the table-name, or
66099
 
** may be NULL. If it is not NULL, then the update-hook 
66100
 
** (sqlite3.xUpdateCallback) is invoked following a successful insert.
66101
 
**
66102
 
** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
66103
 
** allocated, then ownership of P2 is transferred to the pseudo-cursor
66104
 
** and register P2 becomes ephemeral.  If the cursor is changed, the
66105
 
** value of register P2 will then change.  Make sure this does not
66106
 
** cause any problems.)
66107
 
**
66108
 
** This instruction only works on tables.  The equivalent instruction
66109
 
** for indices is OP_IdxInsert.
66110
 
*/
66111
 
/* Opcode: InsertInt P1 P2 P3 P4 P5
66112
 
**
66113
 
** This works exactly like OP_Insert except that the key is the
66114
 
** integer value P3, not the value of the integer stored in register P3.
66115
 
*/
66116
 
case OP_Insert: 
66117
 
case OP_InsertInt: {
66118
 
#if 0  /* local variables moved into u.bf */
66119
 
  Mem *pData;       /* MEM cell holding data for the record to be inserted */
66120
 
  Mem *pKey;        /* MEM cell holding key  for the record */
66121
 
  i64 iKey;         /* The integer ROWID or key for the record to be inserted */
66122
 
  VdbeCursor *pC;   /* Cursor to table into which insert is written */
66123
 
  int nZero;        /* Number of zero-bytes to append */
66124
 
  int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
66125
 
  const char *zDb;  /* database name - used by the update hook */
66126
 
  const char *zTbl; /* Table name - used by the opdate hook */
66127
 
  int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
66128
 
#endif /* local variables moved into u.bf */
66129
 
 
66130
 
  u.bf.pData = &aMem[pOp->p2];
66131
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66132
 
  assert( memIsValid(u.bf.pData) );
66133
 
  u.bf.pC = p->apCsr[pOp->p1];
66134
 
  assert( u.bf.pC!=0 );
66135
 
  assert( u.bf.pC->pCursor!=0 );
66136
 
  assert( u.bf.pC->pseudoTableReg==0 );
66137
 
  assert( u.bf.pC->isTable );
66138
 
  REGISTER_TRACE(pOp->p2, u.bf.pData);
66139
 
 
66140
 
  if( pOp->opcode==OP_Insert ){
66141
 
    u.bf.pKey = &aMem[pOp->p3];
66142
 
    assert( u.bf.pKey->flags & MEM_Int );
66143
 
    assert( memIsValid(u.bf.pKey) );
66144
 
    REGISTER_TRACE(pOp->p3, u.bf.pKey);
66145
 
    u.bf.iKey = u.bf.pKey->u.i;
66146
 
  }else{
66147
 
    assert( pOp->opcode==OP_InsertInt );
66148
 
    u.bf.iKey = pOp->p3;
66149
 
  }
66150
 
 
66151
 
  if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
66152
 
  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = u.bf.iKey;
66153
 
  if( u.bf.pData->flags & MEM_Null ){
66154
 
    u.bf.pData->z = 0;
66155
 
    u.bf.pData->n = 0;
66156
 
  }else{
66157
 
    assert( u.bf.pData->flags & (MEM_Blob|MEM_Str) );
66158
 
  }
66159
 
  u.bf.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bf.pC->seekResult : 0);
66160
 
  if( u.bf.pData->flags & MEM_Zero ){
66161
 
    u.bf.nZero = u.bf.pData->u.nZero;
66162
 
  }else{
66163
 
    u.bf.nZero = 0;
66164
 
  }
66165
 
  sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, 0);
66166
 
  rc = sqlite3BtreeInsert(u.bf.pC->pCursor, 0, u.bf.iKey,
66167
 
                          u.bf.pData->z, u.bf.pData->n, u.bf.nZero,
66168
 
                          pOp->p5 & OPFLAG_APPEND, u.bf.seekResult
66169
 
  );
66170
 
  u.bf.pC->rowidIsValid = 0;
66171
 
  u.bf.pC->deferredMoveto = 0;
66172
 
  u.bf.pC->cacheStatus = CACHE_STALE;
66173
 
 
66174
 
  /* Invoke the update-hook if required. */
66175
 
  if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
66176
 
    u.bf.zDb = db->aDb[u.bf.pC->iDb].zName;
66177
 
    u.bf.zTbl = pOp->p4.z;
66178
 
    u.bf.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
66179
 
    assert( u.bf.pC->isTable );
66180
 
    db->xUpdateCallback(db->pUpdateArg, u.bf.op, u.bf.zDb, u.bf.zTbl, u.bf.iKey);
66181
 
    assert( u.bf.pC->iDb>=0 );
66182
 
  }
66183
 
  break;
66184
 
}
66185
 
 
66186
 
/* Opcode: Delete P1 P2 * P4 *
66187
 
**
66188
 
** Delete the record at which the P1 cursor is currently pointing.
66189
 
**
66190
 
** The cursor will be left pointing at either the next or the previous
66191
 
** record in the table. If it is left pointing at the next record, then
66192
 
** the next Next instruction will be a no-op.  Hence it is OK to delete
66193
 
** a record from within an Next loop.
66194
 
**
66195
 
** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
66196
 
** incremented (otherwise not).
66197
 
**
66198
 
** P1 must not be pseudo-table.  It has to be a real table with
66199
 
** multiple rows.
66200
 
**
66201
 
** If P4 is not NULL, then it is the name of the table that P1 is
66202
 
** pointing to.  The update hook will be invoked, if it exists.
66203
 
** If P4 is not NULL then the P1 cursor must have been positioned
66204
 
** using OP_NotFound prior to invoking this opcode.
66205
 
*/
66206
 
case OP_Delete: {
66207
 
#if 0  /* local variables moved into u.bg */
66208
 
  i64 iKey;
66209
 
  VdbeCursor *pC;
66210
 
#endif /* local variables moved into u.bg */
66211
 
 
66212
 
  u.bg.iKey = 0;
66213
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66214
 
  u.bg.pC = p->apCsr[pOp->p1];
66215
 
  assert( u.bg.pC!=0 );
66216
 
  assert( u.bg.pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
66217
 
 
66218
 
  /* If the update-hook will be invoked, set u.bg.iKey to the rowid of the
66219
 
  ** row being deleted.
66220
 
  */
66221
 
  if( db->xUpdateCallback && pOp->p4.z ){
66222
 
    assert( u.bg.pC->isTable );
66223
 
    assert( u.bg.pC->rowidIsValid );  /* lastRowid set by previous OP_NotFound */
66224
 
    u.bg.iKey = u.bg.pC->lastRowid;
66225
 
  }
66226
 
 
66227
 
  /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
66228
 
  ** OP_Column on the same table without any intervening operations that
66229
 
  ** might move or invalidate the cursor.  Hence cursor u.bg.pC is always pointing
66230
 
  ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
66231
 
  ** below is always a no-op and cannot fail.  We will run it anyhow, though,
66232
 
  ** to guard against future changes to the code generator.
66233
 
  **/
66234
 
  assert( u.bg.pC->deferredMoveto==0 );
66235
 
  rc = sqlite3VdbeCursorMoveto(u.bg.pC);
66236
 
  if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
66237
 
 
66238
 
  sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0);
66239
 
  rc = sqlite3BtreeDelete(u.bg.pC->pCursor);
66240
 
  u.bg.pC->cacheStatus = CACHE_STALE;
66241
 
 
66242
 
  /* Invoke the update-hook if required. */
66243
 
  if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
66244
 
    const char *zDb = db->aDb[u.bg.pC->iDb].zName;
66245
 
    const char *zTbl = pOp->p4.z;
66246
 
    db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bg.iKey);
66247
 
    assert( u.bg.pC->iDb>=0 );
66248
 
  }
66249
 
  if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
66250
 
  break;
66251
 
}
66252
 
/* Opcode: ResetCount * * * * *
66253
 
**
66254
 
** The value of the change counter is copied to the database handle
66255
 
** change counter (returned by subsequent calls to sqlite3_changes()).
66256
 
** Then the VMs internal change counter resets to 0.
66257
 
** This is used by trigger programs.
66258
 
*/
66259
 
case OP_ResetCount: {
66260
 
  sqlite3VdbeSetChanges(db, p->nChange);
66261
 
  p->nChange = 0;
66262
 
  break;
66263
 
}
66264
 
 
66265
 
/* Opcode: RowData P1 P2 * * *
66266
 
**
66267
 
** Write into register P2 the complete row data for cursor P1.
66268
 
** There is no interpretation of the data.  
66269
 
** It is just copied onto the P2 register exactly as 
66270
 
** it is found in the database file.
66271
 
**
66272
 
** If the P1 cursor must be pointing to a valid row (not a NULL row)
66273
 
** of a real table, not a pseudo-table.
66274
 
*/
66275
 
/* Opcode: RowKey P1 P2 * * *
66276
 
**
66277
 
** Write into register P2 the complete row key for cursor P1.
66278
 
** There is no interpretation of the data.  
66279
 
** The key is copied onto the P3 register exactly as 
66280
 
** it is found in the database file.
66281
 
**
66282
 
** If the P1 cursor must be pointing to a valid row (not a NULL row)
66283
 
** of a real table, not a pseudo-table.
66284
 
*/
66285
 
case OP_RowKey:
66286
 
case OP_RowData: {
66287
 
#if 0  /* local variables moved into u.bh */
66288
 
  VdbeCursor *pC;
66289
 
  BtCursor *pCrsr;
66290
 
  u32 n;
66291
 
  i64 n64;
66292
 
#endif /* local variables moved into u.bh */
66293
 
 
66294
 
  pOut = &aMem[pOp->p2];
66295
 
  memAboutToChange(p, pOut);
66296
 
 
66297
 
  /* Note that RowKey and RowData are really exactly the same instruction */
66298
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66299
 
  u.bh.pC = p->apCsr[pOp->p1];
66300
 
  assert( u.bh.pC->isTable || pOp->opcode==OP_RowKey );
66301
 
  assert( u.bh.pC->isIndex || pOp->opcode==OP_RowData );
66302
 
  assert( u.bh.pC!=0 );
66303
 
  assert( u.bh.pC->nullRow==0 );
66304
 
  assert( u.bh.pC->pseudoTableReg==0 );
66305
 
  assert( u.bh.pC->pCursor!=0 );
66306
 
  u.bh.pCrsr = u.bh.pC->pCursor;
66307
 
  assert( sqlite3BtreeCursorIsValid(u.bh.pCrsr) );
66308
 
 
66309
 
  /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
66310
 
  ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
66311
 
  ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
66312
 
  ** a no-op and can never fail.  But we leave it in place as a safety.
66313
 
  */
66314
 
  assert( u.bh.pC->deferredMoveto==0 );
66315
 
  rc = sqlite3VdbeCursorMoveto(u.bh.pC);
66316
 
  if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
66317
 
 
66318
 
  if( u.bh.pC->isIndex ){
66319
 
    assert( !u.bh.pC->isTable );
66320
 
    rc = sqlite3BtreeKeySize(u.bh.pCrsr, &u.bh.n64);
66321
 
    assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
66322
 
    if( u.bh.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
66323
 
      goto too_big;
66324
 
    }
66325
 
    u.bh.n = (u32)u.bh.n64;
66326
 
  }else{
66327
 
    rc = sqlite3BtreeDataSize(u.bh.pCrsr, &u.bh.n);
66328
 
    assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
66329
 
    if( u.bh.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
66330
 
      goto too_big;
66331
 
    }
66332
 
  }
66333
 
  if( sqlite3VdbeMemGrow(pOut, u.bh.n, 0) ){
66334
 
    goto no_mem;
66335
 
  }
66336
 
  pOut->n = u.bh.n;
66337
 
  MemSetTypeFlag(pOut, MEM_Blob);
66338
 
  if( u.bh.pC->isIndex ){
66339
 
    rc = sqlite3BtreeKey(u.bh.pCrsr, 0, u.bh.n, pOut->z);
66340
 
  }else{
66341
 
    rc = sqlite3BtreeData(u.bh.pCrsr, 0, u.bh.n, pOut->z);
66342
 
  }
66343
 
  pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
66344
 
  UPDATE_MAX_BLOBSIZE(pOut);
66345
 
  break;
66346
 
}
66347
 
 
66348
 
/* Opcode: Rowid P1 P2 * * *
66349
 
**
66350
 
** Store in register P2 an integer which is the key of the table entry that
66351
 
** P1 is currently point to.
66352
 
**
66353
 
** P1 can be either an ordinary table or a virtual table.  There used to
66354
 
** be a separate OP_VRowid opcode for use with virtual tables, but this
66355
 
** one opcode now works for both table types.
66356
 
*/
66357
 
case OP_Rowid: {                 /* out2-prerelease */
66358
 
#if 0  /* local variables moved into u.bi */
66359
 
  VdbeCursor *pC;
66360
 
  i64 v;
66361
 
  sqlite3_vtab *pVtab;
66362
 
  const sqlite3_module *pModule;
66363
 
#endif /* local variables moved into u.bi */
66364
 
 
66365
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66366
 
  u.bi.pC = p->apCsr[pOp->p1];
66367
 
  assert( u.bi.pC!=0 );
66368
 
  assert( u.bi.pC->pseudoTableReg==0 );
66369
 
  if( u.bi.pC->nullRow ){
66370
 
    pOut->flags = MEM_Null;
66371
 
    break;
66372
 
  }else if( u.bi.pC->deferredMoveto ){
66373
 
    u.bi.v = u.bi.pC->movetoTarget;
66374
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
66375
 
  }else if( u.bi.pC->pVtabCursor ){
66376
 
    u.bi.pVtab = u.bi.pC->pVtabCursor->pVtab;
66377
 
    u.bi.pModule = u.bi.pVtab->pModule;
66378
 
    assert( u.bi.pModule->xRowid );
66379
 
    rc = u.bi.pModule->xRowid(u.bi.pC->pVtabCursor, &u.bi.v);
66380
 
    importVtabErrMsg(p, u.bi.pVtab);
66381
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
66382
 
  }else{
66383
 
    assert( u.bi.pC->pCursor!=0 );
66384
 
    rc = sqlite3VdbeCursorMoveto(u.bi.pC);
66385
 
    if( rc ) goto abort_due_to_error;
66386
 
    if( u.bi.pC->rowidIsValid ){
66387
 
      u.bi.v = u.bi.pC->lastRowid;
66388
 
    }else{
66389
 
      rc = sqlite3BtreeKeySize(u.bi.pC->pCursor, &u.bi.v);
66390
 
      assert( rc==SQLITE_OK );  /* Always so because of CursorMoveto() above */
66391
 
    }
66392
 
  }
66393
 
  pOut->u.i = u.bi.v;
66394
 
  break;
66395
 
}
66396
 
 
66397
 
/* Opcode: NullRow P1 * * * *
66398
 
**
66399
 
** Move the cursor P1 to a null row.  Any OP_Column operations
66400
 
** that occur while the cursor is on the null row will always
66401
 
** write a NULL.
66402
 
*/
66403
 
case OP_NullRow: {
66404
 
#if 0  /* local variables moved into u.bj */
66405
 
  VdbeCursor *pC;
66406
 
#endif /* local variables moved into u.bj */
66407
 
 
66408
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66409
 
  u.bj.pC = p->apCsr[pOp->p1];
66410
 
  assert( u.bj.pC!=0 );
66411
 
  u.bj.pC->nullRow = 1;
66412
 
  u.bj.pC->rowidIsValid = 0;
66413
 
  if( u.bj.pC->pCursor ){
66414
 
    sqlite3BtreeClearCursor(u.bj.pC->pCursor);
66415
 
  }
66416
 
  break;
66417
 
}
66418
 
 
66419
 
/* Opcode: Last P1 P2 * * *
66420
 
**
66421
 
** The next use of the Rowid or Column or Next instruction for P1 
66422
 
** will refer to the last entry in the database table or index.
66423
 
** If the table or index is empty and P2>0, then jump immediately to P2.
66424
 
** If P2 is 0 or if the table or index is not empty, fall through
66425
 
** to the following instruction.
66426
 
*/
66427
 
case OP_Last: {        /* jump */
66428
 
#if 0  /* local variables moved into u.bk */
66429
 
  VdbeCursor *pC;
66430
 
  BtCursor *pCrsr;
66431
 
  int res;
66432
 
#endif /* local variables moved into u.bk */
66433
 
 
66434
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66435
 
  u.bk.pC = p->apCsr[pOp->p1];
66436
 
  assert( u.bk.pC!=0 );
66437
 
  u.bk.pCrsr = u.bk.pC->pCursor;
66438
 
  if( u.bk.pCrsr==0 ){
66439
 
    u.bk.res = 1;
66440
 
  }else{
66441
 
    rc = sqlite3BtreeLast(u.bk.pCrsr, &u.bk.res);
66442
 
  }
66443
 
  u.bk.pC->nullRow = (u8)u.bk.res;
66444
 
  u.bk.pC->deferredMoveto = 0;
66445
 
  u.bk.pC->rowidIsValid = 0;
66446
 
  u.bk.pC->cacheStatus = CACHE_STALE;
66447
 
  if( pOp->p2>0 && u.bk.res ){
66448
 
    pc = pOp->p2 - 1;
66449
 
  }
66450
 
  break;
66451
 
}
66452
 
 
66453
 
 
66454
 
/* Opcode: Sort P1 P2 * * *
66455
 
**
66456
 
** This opcode does exactly the same thing as OP_Rewind except that
66457
 
** it increments an undocumented global variable used for testing.
66458
 
**
66459
 
** Sorting is accomplished by writing records into a sorting index,
66460
 
** then rewinding that index and playing it back from beginning to
66461
 
** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
66462
 
** rewinding so that the global variable will be incremented and
66463
 
** regression tests can determine whether or not the optimizer is
66464
 
** correctly optimizing out sorts.
66465
 
*/
66466
 
case OP_Sort: {        /* jump */
66467
 
#ifdef SQLITE_TEST
66468
 
  sqlite3_sort_count++;
66469
 
  sqlite3_search_count--;
66470
 
#endif
66471
 
  p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
66472
 
  /* Fall through into OP_Rewind */
66473
 
}
66474
 
/* Opcode: Rewind P1 P2 * * *
66475
 
**
66476
 
** The next use of the Rowid or Column or Next instruction for P1 
66477
 
** will refer to the first entry in the database table or index.
66478
 
** If the table or index is empty and P2>0, then jump immediately to P2.
66479
 
** If P2 is 0 or if the table or index is not empty, fall through
66480
 
** to the following instruction.
66481
 
*/
66482
 
case OP_Rewind: {        /* jump */
66483
 
#if 0  /* local variables moved into u.bl */
66484
 
  VdbeCursor *pC;
66485
 
  BtCursor *pCrsr;
66486
 
  int res;
66487
 
#endif /* local variables moved into u.bl */
66488
 
 
66489
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66490
 
  u.bl.pC = p->apCsr[pOp->p1];
66491
 
  assert( u.bl.pC!=0 );
66492
 
  u.bl.res = 1;
66493
 
  if( (u.bl.pCrsr = u.bl.pC->pCursor)!=0 ){
66494
 
    rc = sqlite3BtreeFirst(u.bl.pCrsr, &u.bl.res);
66495
 
    u.bl.pC->atFirst = u.bl.res==0 ?1:0;
66496
 
    u.bl.pC->deferredMoveto = 0;
66497
 
    u.bl.pC->cacheStatus = CACHE_STALE;
66498
 
    u.bl.pC->rowidIsValid = 0;
66499
 
  }
66500
 
  u.bl.pC->nullRow = (u8)u.bl.res;
66501
 
  assert( pOp->p2>0 && pOp->p2<p->nOp );
66502
 
  if( u.bl.res ){
66503
 
    pc = pOp->p2 - 1;
66504
 
  }
66505
 
  break;
66506
 
}
66507
 
 
66508
 
/* Opcode: Next P1 P2 * * P5
66509
 
**
66510
 
** Advance cursor P1 so that it points to the next key/data pair in its
66511
 
** table or index.  If there are no more key/value pairs then fall through
66512
 
** to the following instruction.  But if the cursor advance was successful,
66513
 
** jump immediately to P2.
66514
 
**
66515
 
** The P1 cursor must be for a real table, not a pseudo-table.
66516
 
**
66517
 
** If P5 is positive and the jump is taken, then event counter
66518
 
** number P5-1 in the prepared statement is incremented.
66519
 
**
66520
 
** See also: Prev
66521
 
*/
66522
 
/* Opcode: Prev P1 P2 * * P5
66523
 
**
66524
 
** Back up cursor P1 so that it points to the previous key/data pair in its
66525
 
** table or index.  If there is no previous key/value pairs then fall through
66526
 
** to the following instruction.  But if the cursor backup was successful,
66527
 
** jump immediately to P2.
66528
 
**
66529
 
** The P1 cursor must be for a real table, not a pseudo-table.
66530
 
**
66531
 
** If P5 is positive and the jump is taken, then event counter
66532
 
** number P5-1 in the prepared statement is incremented.
66533
 
*/
66534
 
case OP_Prev:          /* jump */
66535
 
case OP_Next: {        /* jump */
66536
 
#if 0  /* local variables moved into u.bm */
66537
 
  VdbeCursor *pC;
66538
 
  BtCursor *pCrsr;
66539
 
  int res;
66540
 
#endif /* local variables moved into u.bm */
66541
 
 
66542
 
  CHECK_FOR_INTERRUPT;
66543
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66544
 
  assert( pOp->p5<=ArraySize(p->aCounter) );
66545
 
  u.bm.pC = p->apCsr[pOp->p1];
66546
 
  if( u.bm.pC==0 ){
66547
 
    break;  /* See ticket #2273 */
66548
 
  }
66549
 
  u.bm.pCrsr = u.bm.pC->pCursor;
66550
 
  if( u.bm.pCrsr==0 ){
66551
 
    u.bm.pC->nullRow = 1;
66552
 
    break;
66553
 
  }
66554
 
  u.bm.res = 1;
66555
 
  assert( u.bm.pC->deferredMoveto==0 );
66556
 
  rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(u.bm.pCrsr, &u.bm.res) :
66557
 
                              sqlite3BtreePrevious(u.bm.pCrsr, &u.bm.res);
66558
 
  u.bm.pC->nullRow = (u8)u.bm.res;
66559
 
  u.bm.pC->cacheStatus = CACHE_STALE;
66560
 
  if( u.bm.res==0 ){
66561
 
    pc = pOp->p2 - 1;
66562
 
    if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
66563
 
#ifdef SQLITE_TEST
66564
 
    sqlite3_search_count++;
66565
 
#endif
66566
 
  }
66567
 
  u.bm.pC->rowidIsValid = 0;
66568
 
  break;
66569
 
}
66570
 
 
66571
 
/* Opcode: IdxInsert P1 P2 P3 * P5
66572
 
**
66573
 
** Register P2 holds a SQL index key made using the
66574
 
** MakeRecord instructions.  This opcode writes that key
66575
 
** into the index P1.  Data for the entry is nil.
66576
 
**
66577
 
** P3 is a flag that provides a hint to the b-tree layer that this
66578
 
** insert is likely to be an append.
66579
 
**
66580
 
** This instruction only works for indices.  The equivalent instruction
66581
 
** for tables is OP_Insert.
66582
 
*/
66583
 
case OP_IdxInsert: {        /* in2 */
66584
 
#if 0  /* local variables moved into u.bn */
66585
 
  VdbeCursor *pC;
66586
 
  BtCursor *pCrsr;
66587
 
  int nKey;
66588
 
  const char *zKey;
66589
 
#endif /* local variables moved into u.bn */
66590
 
 
66591
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66592
 
  u.bn.pC = p->apCsr[pOp->p1];
66593
 
  assert( u.bn.pC!=0 );
66594
 
  pIn2 = &aMem[pOp->p2];
66595
 
  assert( pIn2->flags & MEM_Blob );
66596
 
  u.bn.pCrsr = u.bn.pC->pCursor;
66597
 
  if( ALWAYS(u.bn.pCrsr!=0) ){
66598
 
    assert( u.bn.pC->isTable==0 );
66599
 
    rc = ExpandBlob(pIn2);
66600
 
    if( rc==SQLITE_OK ){
66601
 
      u.bn.nKey = pIn2->n;
66602
 
      u.bn.zKey = pIn2->z;
66603
 
      rc = sqlite3BtreeInsert(u.bn.pCrsr, u.bn.zKey, u.bn.nKey, "", 0, 0, pOp->p3,
66604
 
          ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bn.pC->seekResult : 0)
66605
 
      );
66606
 
      assert( u.bn.pC->deferredMoveto==0 );
66607
 
      u.bn.pC->cacheStatus = CACHE_STALE;
66608
 
    }
66609
 
  }
66610
 
  break;
66611
 
}
66612
 
 
66613
 
/* Opcode: IdxDelete P1 P2 P3 * *
66614
 
**
66615
 
** The content of P3 registers starting at register P2 form
66616
 
** an unpacked index key. This opcode removes that entry from the 
66617
 
** index opened by cursor P1.
66618
 
*/
66619
 
case OP_IdxDelete: {
66620
 
#if 0  /* local variables moved into u.bo */
66621
 
  VdbeCursor *pC;
66622
 
  BtCursor *pCrsr;
66623
 
  int res;
66624
 
  UnpackedRecord r;
66625
 
#endif /* local variables moved into u.bo */
66626
 
 
66627
 
  assert( pOp->p3>0 );
66628
 
  assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
66629
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66630
 
  u.bo.pC = p->apCsr[pOp->p1];
66631
 
  assert( u.bo.pC!=0 );
66632
 
  u.bo.pCrsr = u.bo.pC->pCursor;
66633
 
  if( ALWAYS(u.bo.pCrsr!=0) ){
66634
 
    u.bo.r.pKeyInfo = u.bo.pC->pKeyInfo;
66635
 
    u.bo.r.nField = (u16)pOp->p3;
66636
 
    u.bo.r.flags = 0;
66637
 
    u.bo.r.aMem = &aMem[pOp->p2];
66638
 
#ifdef SQLITE_DEBUG
66639
 
    { int i; for(i=0; i<u.bo.r.nField; i++) assert( memIsValid(&u.bo.r.aMem[i]) ); }
66640
 
#endif
66641
 
    rc = sqlite3BtreeMovetoUnpacked(u.bo.pCrsr, &u.bo.r, 0, 0, &u.bo.res);
66642
 
    if( rc==SQLITE_OK && u.bo.res==0 ){
66643
 
      rc = sqlite3BtreeDelete(u.bo.pCrsr);
66644
 
    }
66645
 
    assert( u.bo.pC->deferredMoveto==0 );
66646
 
    u.bo.pC->cacheStatus = CACHE_STALE;
66647
 
  }
66648
 
  break;
66649
 
}
66650
 
 
66651
 
/* Opcode: IdxRowid P1 P2 * * *
66652
 
**
66653
 
** Write into register P2 an integer which is the last entry in the record at
66654
 
** the end of the index key pointed to by cursor P1.  This integer should be
66655
 
** the rowid of the table entry to which this index entry points.
66656
 
**
66657
 
** See also: Rowid, MakeRecord.
66658
 
*/
66659
 
case OP_IdxRowid: {              /* out2-prerelease */
66660
 
#if 0  /* local variables moved into u.bp */
66661
 
  BtCursor *pCrsr;
66662
 
  VdbeCursor *pC;
66663
 
  i64 rowid;
66664
 
#endif /* local variables moved into u.bp */
66665
 
 
66666
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66667
 
  u.bp.pC = p->apCsr[pOp->p1];
66668
 
  assert( u.bp.pC!=0 );
66669
 
  u.bp.pCrsr = u.bp.pC->pCursor;
66670
 
  pOut->flags = MEM_Null;
66671
 
  if( ALWAYS(u.bp.pCrsr!=0) ){
66672
 
    rc = sqlite3VdbeCursorMoveto(u.bp.pC);
66673
 
    if( NEVER(rc) ) goto abort_due_to_error;
66674
 
    assert( u.bp.pC->deferredMoveto==0 );
66675
 
    assert( u.bp.pC->isTable==0 );
66676
 
    if( !u.bp.pC->nullRow ){
66677
 
      rc = sqlite3VdbeIdxRowid(db, u.bp.pCrsr, &u.bp.rowid);
66678
 
      if( rc!=SQLITE_OK ){
66679
 
        goto abort_due_to_error;
66680
 
      }
66681
 
      pOut->u.i = u.bp.rowid;
66682
 
      pOut->flags = MEM_Int;
66683
 
    }
66684
 
  }
66685
 
  break;
66686
 
}
66687
 
 
66688
 
/* Opcode: IdxGE P1 P2 P3 P4 P5
66689
 
**
66690
 
** The P4 register values beginning with P3 form an unpacked index 
66691
 
** key that omits the ROWID.  Compare this key value against the index 
66692
 
** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
66693
 
**
66694
 
** If the P1 index entry is greater than or equal to the key value
66695
 
** then jump to P2.  Otherwise fall through to the next instruction.
66696
 
**
66697
 
** If P5 is non-zero then the key value is increased by an epsilon 
66698
 
** prior to the comparison.  This make the opcode work like IdxGT except
66699
 
** that if the key from register P3 is a prefix of the key in the cursor,
66700
 
** the result is false whereas it would be true with IdxGT.
66701
 
*/
66702
 
/* Opcode: IdxLT P1 P2 P3 P4 P5
66703
 
**
66704
 
** The P4 register values beginning with P3 form an unpacked index 
66705
 
** key that omits the ROWID.  Compare this key value against the index 
66706
 
** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
66707
 
**
66708
 
** If the P1 index entry is less than the key value then jump to P2.
66709
 
** Otherwise fall through to the next instruction.
66710
 
**
66711
 
** If P5 is non-zero then the key value is increased by an epsilon prior 
66712
 
** to the comparison.  This makes the opcode work like IdxLE.
66713
 
*/
66714
 
case OP_IdxLT:          /* jump */
66715
 
case OP_IdxGE: {        /* jump */
66716
 
#if 0  /* local variables moved into u.bq */
66717
 
  VdbeCursor *pC;
66718
 
  int res;
66719
 
  UnpackedRecord r;
66720
 
#endif /* local variables moved into u.bq */
66721
 
 
66722
 
  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66723
 
  u.bq.pC = p->apCsr[pOp->p1];
66724
 
  assert( u.bq.pC!=0 );
66725
 
  assert( u.bq.pC->isOrdered );
66726
 
  if( ALWAYS(u.bq.pC->pCursor!=0) ){
66727
 
    assert( u.bq.pC->deferredMoveto==0 );
66728
 
    assert( pOp->p5==0 || pOp->p5==1 );
66729
 
    assert( pOp->p4type==P4_INT32 );
66730
 
    u.bq.r.pKeyInfo = u.bq.pC->pKeyInfo;
66731
 
    u.bq.r.nField = (u16)pOp->p4.i;
66732
 
    if( pOp->p5 ){
66733
 
      u.bq.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
66734
 
    }else{
66735
 
      u.bq.r.flags = UNPACKED_IGNORE_ROWID;
66736
 
    }
66737
 
    u.bq.r.aMem = &aMem[pOp->p3];
66738
 
#ifdef SQLITE_DEBUG
66739
 
    { int i; for(i=0; i<u.bq.r.nField; i++) assert( memIsValid(&u.bq.r.aMem[i]) ); }
66740
 
#endif
66741
 
    rc = sqlite3VdbeIdxKeyCompare(u.bq.pC, &u.bq.r, &u.bq.res);
66742
 
    if( pOp->opcode==OP_IdxLT ){
66743
 
      u.bq.res = -u.bq.res;
66744
 
    }else{
66745
 
      assert( pOp->opcode==OP_IdxGE );
66746
 
      u.bq.res++;
66747
 
    }
66748
 
    if( u.bq.res>0 ){
66749
 
      pc = pOp->p2 - 1 ;
66750
 
    }
66751
 
  }
66752
 
  break;
66753
 
}
66754
 
 
66755
 
/* Opcode: Destroy P1 P2 P3 * *
66756
 
**
66757
 
** Delete an entire database table or index whose root page in the database
66758
 
** file is given by P1.
66759
 
**
66760
 
** The table being destroyed is in the main database file if P3==0.  If
66761
 
** P3==1 then the table to be clear is in the auxiliary database file
66762
 
** that is used to store tables create using CREATE TEMPORARY TABLE.
66763
 
**
66764
 
** If AUTOVACUUM is enabled then it is possible that another root page
66765
 
** might be moved into the newly deleted root page in order to keep all
66766
 
** root pages contiguous at the beginning of the database.  The former
66767
 
** value of the root page that moved - its value before the move occurred -
66768
 
** is stored in register P2.  If no page 
66769
 
** movement was required (because the table being dropped was already 
66770
 
** the last one in the database) then a zero is stored in register P2.
66771
 
** If AUTOVACUUM is disabled then a zero is stored in register P2.
66772
 
**
66773
 
** See also: Clear
66774
 
*/
66775
 
case OP_Destroy: {     /* out2-prerelease */
66776
 
#if 0  /* local variables moved into u.br */
66777
 
  int iMoved;
66778
 
  int iCnt;
66779
 
  Vdbe *pVdbe;
66780
 
  int iDb;
66781
 
#endif /* local variables moved into u.br */
66782
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
66783
 
  u.br.iCnt = 0;
66784
 
  for(u.br.pVdbe=db->pVdbe; u.br.pVdbe; u.br.pVdbe = u.br.pVdbe->pNext){
66785
 
    if( u.br.pVdbe->magic==VDBE_MAGIC_RUN && u.br.pVdbe->inVtabMethod<2 && u.br.pVdbe->pc>=0 ){
66786
 
      u.br.iCnt++;
66787
 
    }
66788
 
  }
66789
 
#else
66790
 
  u.br.iCnt = db->activeVdbeCnt;
66791
 
#endif
66792
 
  pOut->flags = MEM_Null;
66793
 
  if( u.br.iCnt>1 ){
66794
 
    rc = SQLITE_LOCKED;
66795
 
    p->errorAction = OE_Abort;
66796
 
  }else{
66797
 
    u.br.iDb = pOp->p3;
66798
 
    assert( u.br.iCnt==1 );
66799
 
    assert( (p->btreeMask & (((yDbMask)1)<<u.br.iDb))!=0 );
66800
 
    rc = sqlite3BtreeDropTable(db->aDb[u.br.iDb].pBt, pOp->p1, &u.br.iMoved);
66801
 
    pOut->flags = MEM_Int;
66802
 
    pOut->u.i = u.br.iMoved;
66803
 
#ifndef SQLITE_OMIT_AUTOVACUUM
66804
 
    if( rc==SQLITE_OK && u.br.iMoved!=0 ){
66805
 
      sqlite3RootPageMoved(db, u.br.iDb, u.br.iMoved, pOp->p1);
66806
 
      /* All OP_Destroy operations occur on the same btree */
66807
 
      assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.br.iDb+1 );
66808
 
      resetSchemaOnFault = (u8)u.br.iDb+1;
66809
 
    }
66810
 
#endif
66811
 
  }
66812
 
  break;
66813
 
}
66814
 
 
66815
 
/* Opcode: Clear P1 P2 P3
66816
 
**
66817
 
** Delete all contents of the database table or index whose root page
66818
 
** in the database file is given by P1.  But, unlike Destroy, do not
66819
 
** remove the table or index from the database file.
66820
 
**
66821
 
** The table being clear is in the main database file if P2==0.  If
66822
 
** P2==1 then the table to be clear is in the auxiliary database file
66823
 
** that is used to store tables create using CREATE TEMPORARY TABLE.
66824
 
**
66825
 
** If the P3 value is non-zero, then the table referred to must be an
66826
 
** intkey table (an SQL table, not an index). In this case the row change 
66827
 
** count is incremented by the number of rows in the table being cleared. 
66828
 
** If P3 is greater than zero, then the value stored in register P3 is
66829
 
** also incremented by the number of rows in the table being cleared.
66830
 
**
66831
 
** See also: Destroy
66832
 
*/
66833
 
case OP_Clear: {
66834
 
#if 0  /* local variables moved into u.bs */
66835
 
  int nChange;
66836
 
#endif /* local variables moved into u.bs */
66837
 
 
66838
 
  u.bs.nChange = 0;
66839
 
  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
66840
 
  rc = sqlite3BtreeClearTable(
66841
 
      db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bs.nChange : 0)
66842
 
  );
66843
 
  if( pOp->p3 ){
66844
 
    p->nChange += u.bs.nChange;
66845
 
    if( pOp->p3>0 ){
66846
 
      assert( memIsValid(&aMem[pOp->p3]) );
66847
 
      memAboutToChange(p, &aMem[pOp->p3]);
66848
 
      aMem[pOp->p3].u.i += u.bs.nChange;
66849
 
    }
66850
 
  }
66851
 
  break;
66852
 
}
66853
 
 
66854
 
/* Opcode: CreateTable P1 P2 * * *
66855
 
**
66856
 
** Allocate a new table in the main database file if P1==0 or in the
66857
 
** auxiliary database file if P1==1 or in an attached database if
66858
 
** P1>1.  Write the root page number of the new table into
66859
 
** register P2
66860
 
**
66861
 
** The difference between a table and an index is this:  A table must
66862
 
** have a 4-byte integer key and can have arbitrary data.  An index
66863
 
** has an arbitrary key but no data.
66864
 
**
66865
 
** See also: CreateIndex
66866
 
*/
66867
 
/* Opcode: CreateIndex P1 P2 * * *
66868
 
**
66869
 
** Allocate a new index in the main database file if P1==0 or in the
66870
 
** auxiliary database file if P1==1 or in an attached database if
66871
 
** P1>1.  Write the root page number of the new table into
66872
 
** register P2.
66873
 
**
66874
 
** See documentation on OP_CreateTable for additional information.
66875
 
*/
66876
 
case OP_CreateIndex:            /* out2-prerelease */
66877
 
case OP_CreateTable: {          /* out2-prerelease */
66878
 
#if 0  /* local variables moved into u.bt */
66879
 
  int pgno;
66880
 
  int flags;
66881
 
  Db *pDb;
66882
 
#endif /* local variables moved into u.bt */
66883
 
 
66884
 
  u.bt.pgno = 0;
66885
 
  assert( pOp->p1>=0 && pOp->p1<db->nDb );
66886
 
  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66887
 
  u.bt.pDb = &db->aDb[pOp->p1];
66888
 
  assert( u.bt.pDb->pBt!=0 );
66889
 
  if( pOp->opcode==OP_CreateTable ){
66890
 
    /* u.bt.flags = BTREE_INTKEY; */
66891
 
    u.bt.flags = BTREE_INTKEY;
66892
 
  }else{
66893
 
    u.bt.flags = BTREE_BLOBKEY;
66894
 
  }
66895
 
  rc = sqlite3BtreeCreateTable(u.bt.pDb->pBt, &u.bt.pgno, u.bt.flags);
66896
 
  pOut->u.i = u.bt.pgno;
66897
 
  break;
66898
 
}
66899
 
 
66900
 
/* Opcode: ParseSchema P1 * * P4 *
66901
 
**
66902
 
** Read and parse all entries from the SQLITE_MASTER table of database P1
66903
 
** that match the WHERE clause P4. 
66904
 
**
66905
 
** This opcode invokes the parser to create a new virtual machine,
66906
 
** then runs the new virtual machine.  It is thus a re-entrant opcode.
66907
 
*/
66908
 
case OP_ParseSchema: {
66909
 
#if 0  /* local variables moved into u.bu */
66910
 
  int iDb;
66911
 
  const char *zMaster;
66912
 
  char *zSql;
66913
 
  InitData initData;
66914
 
#endif /* local variables moved into u.bu */
66915
 
 
66916
 
  /* Any prepared statement that invokes this opcode will hold mutexes
66917
 
  ** on every btree.  This is a prerequisite for invoking
66918
 
  ** sqlite3InitCallback().
66919
 
  */
66920
 
#ifdef SQLITE_DEBUG
66921
 
  for(u.bu.iDb=0; u.bu.iDb<db->nDb; u.bu.iDb++){
66922
 
    assert( u.bu.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bu.iDb].pBt) );
66923
 
  }
66924
 
#endif
66925
 
 
66926
 
  u.bu.iDb = pOp->p1;
66927
 
  assert( u.bu.iDb>=0 && u.bu.iDb<db->nDb );
66928
 
  assert( DbHasProperty(db, u.bu.iDb, DB_SchemaLoaded) );
66929
 
  /* Used to be a conditional */ {
66930
 
    u.bu.zMaster = SCHEMA_TABLE(u.bu.iDb);
66931
 
    u.bu.initData.db = db;
66932
 
    u.bu.initData.iDb = pOp->p1;
66933
 
    u.bu.initData.pzErrMsg = &p->zErrMsg;
66934
 
    u.bu.zSql = sqlite3MPrintf(db,
66935
 
       "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
66936
 
       db->aDb[u.bu.iDb].zName, u.bu.zMaster, pOp->p4.z);
66937
 
    if( u.bu.zSql==0 ){
66938
 
      rc = SQLITE_NOMEM;
66939
 
    }else{
66940
 
      assert( db->init.busy==0 );
66941
 
      db->init.busy = 1;
66942
 
      u.bu.initData.rc = SQLITE_OK;
66943
 
      assert( !db->mallocFailed );
66944
 
      rc = sqlite3_exec(db, u.bu.zSql, sqlite3InitCallback, &u.bu.initData, 0);
66945
 
      if( rc==SQLITE_OK ) rc = u.bu.initData.rc;
66946
 
      sqlite3DbFree(db, u.bu.zSql);
66947
 
      db->init.busy = 0;
66948
 
    }
66949
 
  }
66950
 
  if( rc==SQLITE_NOMEM ){
66951
 
    goto no_mem;
66952
 
  }
66953
 
  break;
66954
 
}
66955
 
 
66956
 
#if !defined(SQLITE_OMIT_ANALYZE)
66957
 
/* Opcode: LoadAnalysis P1 * * * *
66958
 
**
66959
 
** Read the sqlite_stat1 table for database P1 and load the content
66960
 
** of that table into the internal index hash table.  This will cause
66961
 
** the analysis to be used when preparing all subsequent queries.
66962
 
*/
66963
 
case OP_LoadAnalysis: {
66964
 
  assert( pOp->p1>=0 && pOp->p1<db->nDb );
66965
 
  rc = sqlite3AnalysisLoad(db, pOp->p1);
66966
 
  break;  
66967
 
}
66968
 
#endif /* !defined(SQLITE_OMIT_ANALYZE) */
66969
 
 
66970
 
/* Opcode: DropTable P1 * * P4 *
66971
 
**
66972
 
** Remove the internal (in-memory) data structures that describe
66973
 
** the table named P4 in database P1.  This is called after a table
66974
 
** is dropped in order to keep the internal representation of the
66975
 
** schema consistent with what is on disk.
66976
 
*/
66977
 
case OP_DropTable: {
66978
 
  sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
66979
 
  break;
66980
 
}
66981
 
 
66982
 
/* Opcode: DropIndex P1 * * P4 *
66983
 
**
66984
 
** Remove the internal (in-memory) data structures that describe
66985
 
** the index named P4 in database P1.  This is called after an index
66986
 
** is dropped in order to keep the internal representation of the
66987
 
** schema consistent with what is on disk.
66988
 
*/
66989
 
case OP_DropIndex: {
66990
 
  sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
66991
 
  break;
66992
 
}
66993
 
 
66994
 
/* Opcode: DropTrigger P1 * * P4 *
66995
 
**
66996
 
** Remove the internal (in-memory) data structures that describe
66997
 
** the trigger named P4 in database P1.  This is called after a trigger
66998
 
** is dropped in order to keep the internal representation of the
66999
 
** schema consistent with what is on disk.
67000
 
*/
67001
 
case OP_DropTrigger: {
67002
 
  sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
67003
 
  break;
67004
 
}
67005
 
 
67006
 
 
67007
 
#ifndef SQLITE_OMIT_INTEGRITY_CHECK
67008
 
/* Opcode: IntegrityCk P1 P2 P3 * P5
67009
 
**
67010
 
** Do an analysis of the currently open database.  Store in
67011
 
** register P1 the text of an error message describing any problems.
67012
 
** If no problems are found, store a NULL in register P1.
67013
 
**
67014
 
** The register P3 contains the maximum number of allowed errors.
67015
 
** At most reg(P3) errors will be reported.
67016
 
** In other words, the analysis stops as soon as reg(P1) errors are 
67017
 
** seen.  Reg(P1) is updated with the number of errors remaining.
67018
 
**
67019
 
** The root page numbers of all tables in the database are integer
67020
 
** stored in reg(P1), reg(P1+1), reg(P1+2), ....  There are P2 tables
67021
 
** total.
67022
 
**
67023
 
** If P5 is not zero, the check is done on the auxiliary database
67024
 
** file, not the main database file.
67025
 
**
67026
 
** This opcode is used to implement the integrity_check pragma.
67027
 
*/
67028
 
case OP_IntegrityCk: {
67029
 
#if 0  /* local variables moved into u.bv */
67030
 
  int nRoot;      /* Number of tables to check.  (Number of root pages.) */
67031
 
  int *aRoot;     /* Array of rootpage numbers for tables to be checked */
67032
 
  int j;          /* Loop counter */
67033
 
  int nErr;       /* Number of errors reported */
67034
 
  char *z;        /* Text of the error report */
67035
 
  Mem *pnErr;     /* Register keeping track of errors remaining */
67036
 
#endif /* local variables moved into u.bv */
67037
 
 
67038
 
  u.bv.nRoot = pOp->p2;
67039
 
  assert( u.bv.nRoot>0 );
67040
 
  u.bv.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bv.nRoot+1) );
67041
 
  if( u.bv.aRoot==0 ) goto no_mem;
67042
 
  assert( pOp->p3>0 && pOp->p3<=p->nMem );
67043
 
  u.bv.pnErr = &aMem[pOp->p3];
67044
 
  assert( (u.bv.pnErr->flags & MEM_Int)!=0 );
67045
 
  assert( (u.bv.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
67046
 
  pIn1 = &aMem[pOp->p1];
67047
 
  for(u.bv.j=0; u.bv.j<u.bv.nRoot; u.bv.j++){
67048
 
    u.bv.aRoot[u.bv.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bv.j]);
67049
 
  }
67050
 
  u.bv.aRoot[u.bv.j] = 0;
67051
 
  assert( pOp->p5<db->nDb );
67052
 
  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
67053
 
  u.bv.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bv.aRoot, u.bv.nRoot,
67054
 
                                 (int)u.bv.pnErr->u.i, &u.bv.nErr);
67055
 
  sqlite3DbFree(db, u.bv.aRoot);
67056
 
  u.bv.pnErr->u.i -= u.bv.nErr;
67057
 
  sqlite3VdbeMemSetNull(pIn1);
67058
 
  if( u.bv.nErr==0 ){
67059
 
    assert( u.bv.z==0 );
67060
 
  }else if( u.bv.z==0 ){
67061
 
    goto no_mem;
67062
 
  }else{
67063
 
    sqlite3VdbeMemSetStr(pIn1, u.bv.z, -1, SQLITE_UTF8, sqlite3_free);
67064
 
  }
67065
 
  UPDATE_MAX_BLOBSIZE(pIn1);
67066
 
  sqlite3VdbeChangeEncoding(pIn1, encoding);
67067
 
  break;
67068
 
}
67069
 
#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
67070
 
 
67071
 
/* Opcode: RowSetAdd P1 P2 * * *
67072
 
**
67073
 
** Insert the integer value held by register P2 into a boolean index
67074
 
** held in register P1.
67075
 
**
67076
 
** An assertion fails if P2 is not an integer.
67077
 
*/
67078
 
case OP_RowSetAdd: {       /* in1, in2 */
67079
 
  pIn1 = &aMem[pOp->p1];
67080
 
  pIn2 = &aMem[pOp->p2];
67081
 
  assert( (pIn2->flags & MEM_Int)!=0 );
67082
 
  if( (pIn1->flags & MEM_RowSet)==0 ){
67083
 
    sqlite3VdbeMemSetRowSet(pIn1);
67084
 
    if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
67085
 
  }
67086
 
  sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
67087
 
  break;
67088
 
}
67089
 
 
67090
 
/* Opcode: RowSetRead P1 P2 P3 * *
67091
 
**
67092
 
** Extract the smallest value from boolean index P1 and put that value into
67093
 
** register P3.  Or, if boolean index P1 is initially empty, leave P3
67094
 
** unchanged and jump to instruction P2.
67095
 
*/
67096
 
case OP_RowSetRead: {       /* jump, in1, out3 */
67097
 
#if 0  /* local variables moved into u.bw */
67098
 
  i64 val;
67099
 
#endif /* local variables moved into u.bw */
67100
 
  CHECK_FOR_INTERRUPT;
67101
 
  pIn1 = &aMem[pOp->p1];
67102
 
  if( (pIn1->flags & MEM_RowSet)==0
67103
 
   || sqlite3RowSetNext(pIn1->u.pRowSet, &u.bw.val)==0
67104
 
  ){
67105
 
    /* The boolean index is empty */
67106
 
    sqlite3VdbeMemSetNull(pIn1);
67107
 
    pc = pOp->p2 - 1;
67108
 
  }else{
67109
 
    /* A value was pulled from the index */
67110
 
    sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.bw.val);
67111
 
  }
67112
 
  break;
67113
 
}
67114
 
 
67115
 
/* Opcode: RowSetTest P1 P2 P3 P4
67116
 
**
67117
 
** Register P3 is assumed to hold a 64-bit integer value. If register P1
67118
 
** contains a RowSet object and that RowSet object contains
67119
 
** the value held in P3, jump to register P2. Otherwise, insert the
67120
 
** integer in P3 into the RowSet and continue on to the
67121
 
** next opcode.
67122
 
**
67123
 
** The RowSet object is optimized for the case where successive sets
67124
 
** of integers, where each set contains no duplicates. Each set
67125
 
** of values is identified by a unique P4 value. The first set
67126
 
** must have P4==0, the final set P4=-1.  P4 must be either -1 or
67127
 
** non-negative.  For non-negative values of P4 only the lower 4
67128
 
** bits are significant.
67129
 
**
67130
 
** This allows optimizations: (a) when P4==0 there is no need to test
67131
 
** the rowset object for P3, as it is guaranteed not to contain it,
67132
 
** (b) when P4==-1 there is no need to insert the value, as it will
67133
 
** never be tested for, and (c) when a value that is part of set X is
67134
 
** inserted, there is no need to search to see if the same value was
67135
 
** previously inserted as part of set X (only if it was previously
67136
 
** inserted as part of some other set).
67137
 
*/
67138
 
case OP_RowSetTest: {                     /* jump, in1, in3 */
67139
 
#if 0  /* local variables moved into u.bx */
67140
 
  int iSet;
67141
 
  int exists;
67142
 
#endif /* local variables moved into u.bx */
67143
 
 
67144
 
  pIn1 = &aMem[pOp->p1];
67145
 
  pIn3 = &aMem[pOp->p3];
67146
 
  u.bx.iSet = pOp->p4.i;
67147
 
  assert( pIn3->flags&MEM_Int );
67148
 
 
67149
 
  /* If there is anything other than a rowset object in memory cell P1,
67150
 
  ** delete it now and initialize P1 with an empty rowset
67151
 
  */
67152
 
  if( (pIn1->flags & MEM_RowSet)==0 ){
67153
 
    sqlite3VdbeMemSetRowSet(pIn1);
67154
 
    if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
67155
 
  }
67156
 
 
67157
 
  assert( pOp->p4type==P4_INT32 );
67158
 
  assert( u.bx.iSet==-1 || u.bx.iSet>=0 );
67159
 
  if( u.bx.iSet ){
67160
 
    u.bx.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
67161
 
                               (u8)(u.bx.iSet>=0 ? u.bx.iSet & 0xf : 0xff),
67162
 
                               pIn3->u.i);
67163
 
    if( u.bx.exists ){
67164
 
      pc = pOp->p2 - 1;
67165
 
      break;
67166
 
    }
67167
 
  }
67168
 
  if( u.bx.iSet>=0 ){
67169
 
    sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
67170
 
  }
67171
 
  break;
67172
 
}
67173
 
 
67174
 
 
67175
 
#ifndef SQLITE_OMIT_TRIGGER
67176
 
 
67177
 
/* Opcode: Program P1 P2 P3 P4 *
67178
 
**
67179
 
** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). 
67180
 
**
67181
 
** P1 contains the address of the memory cell that contains the first memory 
67182
 
** cell in an array of values used as arguments to the sub-program. P2 
67183
 
** contains the address to jump to if the sub-program throws an IGNORE 
67184
 
** exception using the RAISE() function. Register P3 contains the address 
67185
 
** of a memory cell in this (the parent) VM that is used to allocate the 
67186
 
** memory required by the sub-vdbe at runtime.
67187
 
**
67188
 
** P4 is a pointer to the VM containing the trigger program.
67189
 
*/
67190
 
case OP_Program: {        /* jump */
67191
 
#if 0  /* local variables moved into u.by */
67192
 
  int nMem;               /* Number of memory registers for sub-program */
67193
 
  int nByte;              /* Bytes of runtime space required for sub-program */
67194
 
  Mem *pRt;               /* Register to allocate runtime space */
67195
 
  Mem *pMem;              /* Used to iterate through memory cells */
67196
 
  Mem *pEnd;              /* Last memory cell in new array */
67197
 
  VdbeFrame *pFrame;      /* New vdbe frame to execute in */
67198
 
  SubProgram *pProgram;   /* Sub-program to execute */
67199
 
  void *t;                /* Token identifying trigger */
67200
 
#endif /* local variables moved into u.by */
67201
 
 
67202
 
  u.by.pProgram = pOp->p4.pProgram;
67203
 
  u.by.pRt = &aMem[pOp->p3];
67204
 
  assert( memIsValid(u.by.pRt) );
67205
 
  assert( u.by.pProgram->nOp>0 );
67206
 
 
67207
 
  /* If the p5 flag is clear, then recursive invocation of triggers is
67208
 
  ** disabled for backwards compatibility (p5 is set if this sub-program
67209
 
  ** is really a trigger, not a foreign key action, and the flag set
67210
 
  ** and cleared by the "PRAGMA recursive_triggers" command is clear).
67211
 
  **
67212
 
  ** It is recursive invocation of triggers, at the SQL level, that is
67213
 
  ** disabled. In some cases a single trigger may generate more than one
67214
 
  ** SubProgram (if the trigger may be executed with more than one different
67215
 
  ** ON CONFLICT algorithm). SubProgram structures associated with a
67216
 
  ** single trigger all have the same value for the SubProgram.token
67217
 
  ** variable.  */
67218
 
  if( pOp->p5 ){
67219
 
    u.by.t = u.by.pProgram->token;
67220
 
    for(u.by.pFrame=p->pFrame; u.by.pFrame && u.by.pFrame->token!=u.by.t; u.by.pFrame=u.by.pFrame->pParent);
67221
 
    if( u.by.pFrame ) break;
67222
 
  }
67223
 
 
67224
 
  if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
67225
 
    rc = SQLITE_ERROR;
67226
 
    sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
67227
 
    break;
67228
 
  }
67229
 
 
67230
 
  /* Register u.by.pRt is used to store the memory required to save the state
67231
 
  ** of the current program, and the memory required at runtime to execute
67232
 
  ** the trigger program. If this trigger has been fired before, then u.by.pRt
67233
 
  ** is already allocated. Otherwise, it must be initialized.  */
67234
 
  if( (u.by.pRt->flags&MEM_Frame)==0 ){
67235
 
    /* SubProgram.nMem is set to the number of memory cells used by the
67236
 
    ** program stored in SubProgram.aOp. As well as these, one memory
67237
 
    ** cell is required for each cursor used by the program. Set local
67238
 
    ** variable u.by.nMem (and later, VdbeFrame.nChildMem) to this value.
67239
 
    */
67240
 
    u.by.nMem = u.by.pProgram->nMem + u.by.pProgram->nCsr;
67241
 
    u.by.nByte = ROUND8(sizeof(VdbeFrame))
67242
 
              + u.by.nMem * sizeof(Mem)
67243
 
              + u.by.pProgram->nCsr * sizeof(VdbeCursor *);
67244
 
    u.by.pFrame = sqlite3DbMallocZero(db, u.by.nByte);
67245
 
    if( !u.by.pFrame ){
67246
 
      goto no_mem;
67247
 
    }
67248
 
    sqlite3VdbeMemRelease(u.by.pRt);
67249
 
    u.by.pRt->flags = MEM_Frame;
67250
 
    u.by.pRt->u.pFrame = u.by.pFrame;
67251
 
 
67252
 
    u.by.pFrame->v = p;
67253
 
    u.by.pFrame->nChildMem = u.by.nMem;
67254
 
    u.by.pFrame->nChildCsr = u.by.pProgram->nCsr;
67255
 
    u.by.pFrame->pc = pc;
67256
 
    u.by.pFrame->aMem = p->aMem;
67257
 
    u.by.pFrame->nMem = p->nMem;
67258
 
    u.by.pFrame->apCsr = p->apCsr;
67259
 
    u.by.pFrame->nCursor = p->nCursor;
67260
 
    u.by.pFrame->aOp = p->aOp;
67261
 
    u.by.pFrame->nOp = p->nOp;
67262
 
    u.by.pFrame->token = u.by.pProgram->token;
67263
 
 
67264
 
    u.by.pEnd = &VdbeFrameMem(u.by.pFrame)[u.by.pFrame->nChildMem];
67265
 
    for(u.by.pMem=VdbeFrameMem(u.by.pFrame); u.by.pMem!=u.by.pEnd; u.by.pMem++){
67266
 
      u.by.pMem->flags = MEM_Null;
67267
 
      u.by.pMem->db = db;
67268
 
    }
67269
 
  }else{
67270
 
    u.by.pFrame = u.by.pRt->u.pFrame;
67271
 
    assert( u.by.pProgram->nMem+u.by.pProgram->nCsr==u.by.pFrame->nChildMem );
67272
 
    assert( u.by.pProgram->nCsr==u.by.pFrame->nChildCsr );
67273
 
    assert( pc==u.by.pFrame->pc );
67274
 
  }
67275
 
 
67276
 
  p->nFrame++;
67277
 
  u.by.pFrame->pParent = p->pFrame;
67278
 
  u.by.pFrame->lastRowid = db->lastRowid;
67279
 
  u.by.pFrame->nChange = p->nChange;
67280
 
  p->nChange = 0;
67281
 
  p->pFrame = u.by.pFrame;
67282
 
  p->aMem = aMem = &VdbeFrameMem(u.by.pFrame)[-1];
67283
 
  p->nMem = u.by.pFrame->nChildMem;
67284
 
  p->nCursor = (u16)u.by.pFrame->nChildCsr;
67285
 
  p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
67286
 
  p->aOp = aOp = u.by.pProgram->aOp;
67287
 
  p->nOp = u.by.pProgram->nOp;
67288
 
  pc = -1;
67289
 
 
67290
 
  break;
67291
 
}
67292
 
 
67293
 
/* Opcode: Param P1 P2 * * *
67294
 
**
67295
 
** This opcode is only ever present in sub-programs called via the 
67296
 
** OP_Program instruction. Copy a value currently stored in a memory 
67297
 
** cell of the calling (parent) frame to cell P2 in the current frames 
67298
 
** address space. This is used by trigger programs to access the new.* 
67299
 
** and old.* values.
67300
 
**
67301
 
** The address of the cell in the parent frame is determined by adding
67302
 
** the value of the P1 argument to the value of the P1 argument to the
67303
 
** calling OP_Program instruction.
67304
 
*/
67305
 
case OP_Param: {           /* out2-prerelease */
67306
 
#if 0  /* local variables moved into u.bz */
67307
 
  VdbeFrame *pFrame;
67308
 
  Mem *pIn;
67309
 
#endif /* local variables moved into u.bz */
67310
 
  u.bz.pFrame = p->pFrame;
67311
 
  u.bz.pIn = &u.bz.pFrame->aMem[pOp->p1 + u.bz.pFrame->aOp[u.bz.pFrame->pc].p1];
67312
 
  sqlite3VdbeMemShallowCopy(pOut, u.bz.pIn, MEM_Ephem);
67313
 
  break;
67314
 
}
67315
 
 
67316
 
#endif /* #ifndef SQLITE_OMIT_TRIGGER */
67317
 
 
67318
 
#ifndef SQLITE_OMIT_FOREIGN_KEY
67319
 
/* Opcode: FkCounter P1 P2 * * *
67320
 
**
67321
 
** Increment a "constraint counter" by P2 (P2 may be negative or positive).
67322
 
** If P1 is non-zero, the database constraint counter is incremented 
67323
 
** (deferred foreign key constraints). Otherwise, if P1 is zero, the 
67324
 
** statement counter is incremented (immediate foreign key constraints).
67325
 
*/
67326
 
case OP_FkCounter: {
67327
 
  if( pOp->p1 ){
67328
 
    db->nDeferredCons += pOp->p2;
67329
 
  }else{
67330
 
    p->nFkConstraint += pOp->p2;
67331
 
  }
67332
 
  break;
67333
 
}
67334
 
 
67335
 
/* Opcode: FkIfZero P1 P2 * * *
67336
 
**
67337
 
** This opcode tests if a foreign key constraint-counter is currently zero.
67338
 
** If so, jump to instruction P2. Otherwise, fall through to the next 
67339
 
** instruction.
67340
 
**
67341
 
** If P1 is non-zero, then the jump is taken if the database constraint-counter
67342
 
** is zero (the one that counts deferred constraint violations). If P1 is
67343
 
** zero, the jump is taken if the statement constraint-counter is zero
67344
 
** (immediate foreign key constraint violations).
67345
 
*/
67346
 
case OP_FkIfZero: {         /* jump */
67347
 
  if( pOp->p1 ){
67348
 
    if( db->nDeferredCons==0 ) pc = pOp->p2-1;
67349
 
  }else{
67350
 
    if( p->nFkConstraint==0 ) pc = pOp->p2-1;
67351
 
  }
67352
 
  break;
67353
 
}
67354
 
#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
67355
 
 
67356
 
#ifndef SQLITE_OMIT_AUTOINCREMENT
67357
 
/* Opcode: MemMax P1 P2 * * *
67358
 
**
67359
 
** P1 is a register in the root frame of this VM (the root frame is
67360
 
** different from the current frame if this instruction is being executed
67361
 
** within a sub-program). Set the value of register P1 to the maximum of 
67362
 
** its current value and the value in register P2.
67363
 
**
67364
 
** This instruction throws an error if the memory cell is not initially
67365
 
** an integer.
67366
 
*/
67367
 
case OP_MemMax: {        /* in2 */
67368
 
#if 0  /* local variables moved into u.ca */
67369
 
  Mem *pIn1;
67370
 
  VdbeFrame *pFrame;
67371
 
#endif /* local variables moved into u.ca */
67372
 
  if( p->pFrame ){
67373
 
    for(u.ca.pFrame=p->pFrame; u.ca.pFrame->pParent; u.ca.pFrame=u.ca.pFrame->pParent);
67374
 
    u.ca.pIn1 = &u.ca.pFrame->aMem[pOp->p1];
67375
 
  }else{
67376
 
    u.ca.pIn1 = &aMem[pOp->p1];
67377
 
  }
67378
 
  assert( memIsValid(u.ca.pIn1) );
67379
 
  sqlite3VdbeMemIntegerify(u.ca.pIn1);
67380
 
  pIn2 = &aMem[pOp->p2];
67381
 
  sqlite3VdbeMemIntegerify(pIn2);
67382
 
  if( u.ca.pIn1->u.i<pIn2->u.i){
67383
 
    u.ca.pIn1->u.i = pIn2->u.i;
67384
 
  }
67385
 
  break;
67386
 
}
67387
 
#endif /* SQLITE_OMIT_AUTOINCREMENT */
67388
 
 
67389
 
/* Opcode: IfPos P1 P2 * * *
67390
 
**
67391
 
** If the value of register P1 is 1 or greater, jump to P2.
67392
 
**
67393
 
** It is illegal to use this instruction on a register that does
67394
 
** not contain an integer.  An assertion fault will result if you try.
67395
 
*/
67396
 
case OP_IfPos: {        /* jump, in1 */
67397
 
  pIn1 = &aMem[pOp->p1];
67398
 
  assert( pIn1->flags&MEM_Int );
67399
 
  if( pIn1->u.i>0 ){
67400
 
     pc = pOp->p2 - 1;
67401
 
  }
67402
 
  break;
67403
 
}
67404
 
 
67405
 
/* Opcode: IfNeg P1 P2 * * *
67406
 
**
67407
 
** If the value of register P1 is less than zero, jump to P2. 
67408
 
**
67409
 
** It is illegal to use this instruction on a register that does
67410
 
** not contain an integer.  An assertion fault will result if you try.
67411
 
*/
67412
 
case OP_IfNeg: {        /* jump, in1 */
67413
 
  pIn1 = &aMem[pOp->p1];
67414
 
  assert( pIn1->flags&MEM_Int );
67415
 
  if( pIn1->u.i<0 ){
67416
 
     pc = pOp->p2 - 1;
67417
 
  }
67418
 
  break;
67419
 
}
67420
 
 
67421
 
/* Opcode: IfZero P1 P2 P3 * *
67422
 
**
67423
 
** The register P1 must contain an integer.  Add literal P3 to the
67424
 
** value in register P1.  If the result is exactly 0, jump to P2. 
67425
 
**
67426
 
** It is illegal to use this instruction on a register that does
67427
 
** not contain an integer.  An assertion fault will result if you try.
67428
 
*/
67429
 
case OP_IfZero: {        /* jump, in1 */
67430
 
  pIn1 = &aMem[pOp->p1];
67431
 
  assert( pIn1->flags&MEM_Int );
67432
 
  pIn1->u.i += pOp->p3;
67433
 
  if( pIn1->u.i==0 ){
67434
 
     pc = pOp->p2 - 1;
67435
 
  }
67436
 
  break;
67437
 
}
67438
 
 
67439
 
/* Opcode: AggStep * P2 P3 P4 P5
67440
 
**
67441
 
** Execute the step function for an aggregate.  The
67442
 
** function has P5 arguments.   P4 is a pointer to the FuncDef
67443
 
** structure that specifies the function.  Use register
67444
 
** P3 as the accumulator.
67445
 
**
67446
 
** The P5 arguments are taken from register P2 and its
67447
 
** successors.
67448
 
*/
67449
 
case OP_AggStep: {
67450
 
#if 0  /* local variables moved into u.cb */
67451
 
  int n;
67452
 
  int i;
67453
 
  Mem *pMem;
67454
 
  Mem *pRec;
67455
 
  sqlite3_context ctx;
67456
 
  sqlite3_value **apVal;
67457
 
#endif /* local variables moved into u.cb */
67458
 
 
67459
 
  u.cb.n = pOp->p5;
67460
 
  assert( u.cb.n>=0 );
67461
 
  u.cb.pRec = &aMem[pOp->p2];
67462
 
  u.cb.apVal = p->apArg;
67463
 
  assert( u.cb.apVal || u.cb.n==0 );
67464
 
  for(u.cb.i=0; u.cb.i<u.cb.n; u.cb.i++, u.cb.pRec++){
67465
 
    assert( memIsValid(u.cb.pRec) );
67466
 
    u.cb.apVal[u.cb.i] = u.cb.pRec;
67467
 
    memAboutToChange(p, u.cb.pRec);
67468
 
    sqlite3VdbeMemStoreType(u.cb.pRec);
67469
 
  }
67470
 
  u.cb.ctx.pFunc = pOp->p4.pFunc;
67471
 
  assert( pOp->p3>0 && pOp->p3<=p->nMem );
67472
 
  u.cb.ctx.pMem = u.cb.pMem = &aMem[pOp->p3];
67473
 
  u.cb.pMem->n++;
67474
 
  u.cb.ctx.s.flags = MEM_Null;
67475
 
  u.cb.ctx.s.z = 0;
67476
 
  u.cb.ctx.s.zMalloc = 0;
67477
 
  u.cb.ctx.s.xDel = 0;
67478
 
  u.cb.ctx.s.db = db;
67479
 
  u.cb.ctx.isError = 0;
67480
 
  u.cb.ctx.pColl = 0;
67481
 
  if( u.cb.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
67482
 
    assert( pOp>p->aOp );
67483
 
    assert( pOp[-1].p4type==P4_COLLSEQ );
67484
 
    assert( pOp[-1].opcode==OP_CollSeq );
67485
 
    u.cb.ctx.pColl = pOp[-1].p4.pColl;
67486
 
  }
67487
 
  (u.cb.ctx.pFunc->xStep)(&u.cb.ctx, u.cb.n, u.cb.apVal); /* IMP: R-24505-23230 */
67488
 
  if( u.cb.ctx.isError ){
67489
 
    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cb.ctx.s));
67490
 
    rc = u.cb.ctx.isError;
67491
 
  }
67492
 
 
67493
 
  sqlite3VdbeMemRelease(&u.cb.ctx.s);
67494
 
 
67495
 
  break;
67496
 
}
67497
 
 
67498
 
/* Opcode: AggFinal P1 P2 * P4 *
67499
 
**
67500
 
** Execute the finalizer function for an aggregate.  P1 is
67501
 
** the memory location that is the accumulator for the aggregate.
67502
 
**
67503
 
** P2 is the number of arguments that the step function takes and
67504
 
** P4 is a pointer to the FuncDef for this function.  The P2
67505
 
** argument is not used by this opcode.  It is only there to disambiguate
67506
 
** functions that can take varying numbers of arguments.  The
67507
 
** P4 argument is only needed for the degenerate case where
67508
 
** the step function was not previously called.
67509
 
*/
67510
 
case OP_AggFinal: {
67511
 
#if 0  /* local variables moved into u.cc */
67512
 
  Mem *pMem;
67513
 
#endif /* local variables moved into u.cc */
67514
 
  assert( pOp->p1>0 && pOp->p1<=p->nMem );
67515
 
  u.cc.pMem = &aMem[pOp->p1];
67516
 
  assert( (u.cc.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
67517
 
  rc = sqlite3VdbeMemFinalize(u.cc.pMem, pOp->p4.pFunc);
67518
 
  if( rc ){
67519
 
    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cc.pMem));
67520
 
  }
67521
 
  sqlite3VdbeChangeEncoding(u.cc.pMem, encoding);
67522
 
  UPDATE_MAX_BLOBSIZE(u.cc.pMem);
67523
 
  if( sqlite3VdbeMemTooBig(u.cc.pMem) ){
67524
 
    goto too_big;
67525
 
  }
67526
 
  break;
67527
 
}
67528
 
 
67529
 
#ifndef SQLITE_OMIT_WAL
67530
 
/* Opcode: Checkpoint P1 P2 P3 * *
67531
 
**
67532
 
** Checkpoint database P1. This is a no-op if P1 is not currently in
67533
 
** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
67534
 
** or RESTART.  Write 1 or 0 into mem[P3] if the checkpoint returns
67535
 
** SQLITE_BUSY or not, respectively.  Write the number of pages in the
67536
 
** WAL after the checkpoint into mem[P3+1] and the number of pages
67537
 
** in the WAL that have been checkpointed after the checkpoint
67538
 
** completes into mem[P3+2].  However on an error, mem[P3+1] and
67539
 
** mem[P3+2] are initialized to -1.
67540
 
*/
67541
 
case OP_Checkpoint: {
67542
 
#if 0  /* local variables moved into u.cd */
67543
 
  int i;                          /* Loop counter */
67544
 
  int aRes[3];                    /* Results */
67545
 
  Mem *pMem;                      /* Write results here */
67546
 
#endif /* local variables moved into u.cd */
67547
 
 
67548
 
  u.cd.aRes[0] = 0;
67549
 
  u.cd.aRes[1] = u.cd.aRes[2] = -1;
67550
 
  assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
67551
 
       || pOp->p2==SQLITE_CHECKPOINT_FULL
67552
 
       || pOp->p2==SQLITE_CHECKPOINT_RESTART
67553
 
  );
67554
 
  rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cd.aRes[1], &u.cd.aRes[2]);
67555
 
  if( rc==SQLITE_BUSY ){
67556
 
    rc = SQLITE_OK;
67557
 
    u.cd.aRes[0] = 1;
67558
 
  }
67559
 
  for(u.cd.i=0, u.cd.pMem = &aMem[pOp->p3]; u.cd.i<3; u.cd.i++, u.cd.pMem++){
67560
 
    sqlite3VdbeMemSetInt64(u.cd.pMem, (i64)u.cd.aRes[u.cd.i]);
67561
 
  }
67562
 
  break;
67563
 
};  
67564
 
#endif
67565
 
 
67566
 
#ifndef SQLITE_OMIT_PRAGMA
67567
 
/* Opcode: JournalMode P1 P2 P3 * P5
67568
 
**
67569
 
** Change the journal mode of database P1 to P3. P3 must be one of the
67570
 
** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
67571
 
** modes (delete, truncate, persist, off and memory), this is a simple
67572
 
** operation. No IO is required.
67573
 
**
67574
 
** If changing into or out of WAL mode the procedure is more complicated.
67575
 
**
67576
 
** Write a string containing the final journal-mode to register P2.
67577
 
*/
67578
 
case OP_JournalMode: {    /* out2-prerelease */
67579
 
#if 0  /* local variables moved into u.ce */
67580
 
  Btree *pBt;                     /* Btree to change journal mode of */
67581
 
  Pager *pPager;                  /* Pager associated with pBt */
67582
 
  int eNew;                       /* New journal mode */
67583
 
  int eOld;                       /* The old journal mode */
67584
 
  const char *zFilename;          /* Name of database file for pPager */
67585
 
#endif /* local variables moved into u.ce */
67586
 
 
67587
 
  u.ce.eNew = pOp->p3;
67588
 
  assert( u.ce.eNew==PAGER_JOURNALMODE_DELETE
67589
 
       || u.ce.eNew==PAGER_JOURNALMODE_TRUNCATE
67590
 
       || u.ce.eNew==PAGER_JOURNALMODE_PERSIST
67591
 
       || u.ce.eNew==PAGER_JOURNALMODE_OFF
67592
 
       || u.ce.eNew==PAGER_JOURNALMODE_MEMORY
67593
 
       || u.ce.eNew==PAGER_JOURNALMODE_WAL
67594
 
       || u.ce.eNew==PAGER_JOURNALMODE_QUERY
67595
 
  );
67596
 
  assert( pOp->p1>=0 && pOp->p1<db->nDb );
67597
 
 
67598
 
  u.ce.pBt = db->aDb[pOp->p1].pBt;
67599
 
  u.ce.pPager = sqlite3BtreePager(u.ce.pBt);
67600
 
  u.ce.eOld = sqlite3PagerGetJournalMode(u.ce.pPager);
67601
 
  if( u.ce.eNew==PAGER_JOURNALMODE_QUERY ) u.ce.eNew = u.ce.eOld;
67602
 
  if( !sqlite3PagerOkToChangeJournalMode(u.ce.pPager) ) u.ce.eNew = u.ce.eOld;
67603
 
 
67604
 
#ifndef SQLITE_OMIT_WAL
67605
 
  u.ce.zFilename = sqlite3PagerFilename(u.ce.pPager);
67606
 
 
67607
 
  /* Do not allow a transition to journal_mode=WAL for a database
67608
 
  ** in temporary storage or if the VFS does not support shared memory
67609
 
  */
67610
 
  if( u.ce.eNew==PAGER_JOURNALMODE_WAL
67611
 
   && (u.ce.zFilename[0]==0                         /* Temp file */
67612
 
       || !sqlite3PagerWalSupported(u.ce.pPager))   /* No shared-memory support */
67613
 
  ){
67614
 
    u.ce.eNew = u.ce.eOld;
67615
 
  }
67616
 
 
67617
 
  if( (u.ce.eNew!=u.ce.eOld)
67618
 
   && (u.ce.eOld==PAGER_JOURNALMODE_WAL || u.ce.eNew==PAGER_JOURNALMODE_WAL)
67619
 
  ){
67620
 
    if( !db->autoCommit || db->activeVdbeCnt>1 ){
67621
 
      rc = SQLITE_ERROR;
67622
 
      sqlite3SetString(&p->zErrMsg, db,
67623
 
          "cannot change %s wal mode from within a transaction",
67624
 
          (u.ce.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
67625
 
      );
67626
 
      break;
67627
 
    }else{
67628
 
 
67629
 
      if( u.ce.eOld==PAGER_JOURNALMODE_WAL ){
67630
 
        /* If leaving WAL mode, close the log file. If successful, the call
67631
 
        ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
67632
 
        ** file. An EXCLUSIVE lock may still be held on the database file
67633
 
        ** after a successful return.
67634
 
        */
67635
 
        rc = sqlite3PagerCloseWal(u.ce.pPager);
67636
 
        if( rc==SQLITE_OK ){
67637
 
          sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
67638
 
        }
67639
 
      }else if( u.ce.eOld==PAGER_JOURNALMODE_MEMORY ){
67640
 
        /* Cannot transition directly from MEMORY to WAL.  Use mode OFF
67641
 
        ** as an intermediate */
67642
 
        sqlite3PagerSetJournalMode(u.ce.pPager, PAGER_JOURNALMODE_OFF);
67643
 
      }
67644
 
 
67645
 
      /* Open a transaction on the database file. Regardless of the journal
67646
 
      ** mode, this transaction always uses a rollback journal.
67647
 
      */
67648
 
      assert( sqlite3BtreeIsInTrans(u.ce.pBt)==0 );
67649
 
      if( rc==SQLITE_OK ){
67650
 
        rc = sqlite3BtreeSetVersion(u.ce.pBt, (u.ce.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
67651
 
      }
67652
 
    }
67653
 
  }
67654
 
#endif /* ifndef SQLITE_OMIT_WAL */
67655
 
 
67656
 
  if( rc ){
67657
 
    u.ce.eNew = u.ce.eOld;
67658
 
  }
67659
 
  u.ce.eNew = sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
67660
 
 
67661
 
  pOut = &aMem[pOp->p2];
67662
 
  pOut->flags = MEM_Str|MEM_Static|MEM_Term;
67663
 
  pOut->z = (char *)sqlite3JournalModename(u.ce.eNew);
67664
 
  pOut->n = sqlite3Strlen30(pOut->z);
67665
 
  pOut->enc = SQLITE_UTF8;
67666
 
  sqlite3VdbeChangeEncoding(pOut, encoding);
67667
 
  break;
67668
 
};
67669
 
#endif /* SQLITE_OMIT_PRAGMA */
67670
 
 
67671
 
#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
67672
 
/* Opcode: Vacuum * * * * *
67673
 
**
67674
 
** Vacuum the entire database.  This opcode will cause other virtual
67675
 
** machines to be created and run.  It may not be called from within
67676
 
** a transaction.
67677
 
*/
67678
 
case OP_Vacuum: {
67679
 
  rc = sqlite3RunVacuum(&p->zErrMsg, db);
67680
 
  break;
67681
 
}
67682
 
#endif
67683
 
 
67684
 
#if !defined(SQLITE_OMIT_AUTOVACUUM)
67685
 
/* Opcode: IncrVacuum P1 P2 * * *
67686
 
**
67687
 
** Perform a single step of the incremental vacuum procedure on
67688
 
** the P1 database. If the vacuum has finished, jump to instruction
67689
 
** P2. Otherwise, fall through to the next instruction.
67690
 
*/
67691
 
case OP_IncrVacuum: {        /* jump */
67692
 
#if 0  /* local variables moved into u.cf */
67693
 
  Btree *pBt;
67694
 
#endif /* local variables moved into u.cf */
67695
 
 
67696
 
  assert( pOp->p1>=0 && pOp->p1<db->nDb );
67697
 
  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
67698
 
  u.cf.pBt = db->aDb[pOp->p1].pBt;
67699
 
  rc = sqlite3BtreeIncrVacuum(u.cf.pBt);
67700
 
  if( rc==SQLITE_DONE ){
67701
 
    pc = pOp->p2 - 1;
67702
 
    rc = SQLITE_OK;
67703
 
  }
67704
 
  break;
67705
 
}
67706
 
#endif
67707
 
 
67708
 
/* Opcode: Expire P1 * * * *
67709
 
**
67710
 
** Cause precompiled statements to become expired. An expired statement
67711
 
** fails with an error code of SQLITE_SCHEMA if it is ever executed 
67712
 
** (via sqlite3_step()).
67713
 
** 
67714
 
** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
67715
 
** then only the currently executing statement is affected. 
67716
 
*/
67717
 
case OP_Expire: {
67718
 
  if( !pOp->p1 ){
67719
 
    sqlite3ExpirePreparedStatements(db);
67720
 
  }else{
67721
 
    p->expired = 1;
67722
 
  }
67723
 
  break;
67724
 
}
67725
 
 
67726
 
#ifndef SQLITE_OMIT_SHARED_CACHE
67727
 
/* Opcode: TableLock P1 P2 P3 P4 *
67728
 
**
67729
 
** Obtain a lock on a particular table. This instruction is only used when
67730
 
** the shared-cache feature is enabled. 
67731
 
**
67732
 
** P1 is the index of the database in sqlite3.aDb[] of the database
67733
 
** on which the lock is acquired.  A readlock is obtained if P3==0 or
67734
 
** a write lock if P3==1.
67735
 
**
67736
 
** P2 contains the root-page of the table to lock.
67737
 
**
67738
 
** P4 contains a pointer to the name of the table being locked. This is only
67739
 
** used to generate an error message if the lock cannot be obtained.
67740
 
*/
67741
 
case OP_TableLock: {
67742
 
  u8 isWriteLock = (u8)pOp->p3;
67743
 
  if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
67744
 
    int p1 = pOp->p1; 
67745
 
    assert( p1>=0 && p1<db->nDb );
67746
 
    assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
67747
 
    assert( isWriteLock==0 || isWriteLock==1 );
67748
 
    rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
67749
 
    if( (rc&0xFF)==SQLITE_LOCKED ){
67750
 
      const char *z = pOp->p4.z;
67751
 
      sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
67752
 
    }
67753
 
  }
67754
 
  break;
67755
 
}
67756
 
#endif /* SQLITE_OMIT_SHARED_CACHE */
67757
 
 
67758
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
67759
 
/* Opcode: VBegin * * * P4 *
67760
 
**
67761
 
** P4 may be a pointer to an sqlite3_vtab structure. If so, call the 
67762
 
** xBegin method for that table.
67763
 
**
67764
 
** Also, whether or not P4 is set, check that this is not being called from
67765
 
** within a callback to a virtual table xSync() method. If it is, the error
67766
 
** code will be set to SQLITE_LOCKED.
67767
 
*/
67768
 
case OP_VBegin: {
67769
 
#if 0  /* local variables moved into u.cg */
67770
 
  VTable *pVTab;
67771
 
#endif /* local variables moved into u.cg */
67772
 
  u.cg.pVTab = pOp->p4.pVtab;
67773
 
  rc = sqlite3VtabBegin(db, u.cg.pVTab);
67774
 
  if( u.cg.pVTab ) importVtabErrMsg(p, u.cg.pVTab->pVtab);
67775
 
  break;
67776
 
}
67777
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
67778
 
 
67779
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
67780
 
/* Opcode: VCreate P1 * * P4 *
67781
 
**
67782
 
** P4 is the name of a virtual table in database P1. Call the xCreate method
67783
 
** for that table.
67784
 
*/
67785
 
case OP_VCreate: {
67786
 
  rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
67787
 
  break;
67788
 
}
67789
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
67790
 
 
67791
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
67792
 
/* Opcode: VDestroy P1 * * P4 *
67793
 
**
67794
 
** P4 is the name of a virtual table in database P1.  Call the xDestroy method
67795
 
** of that table.
67796
 
*/
67797
 
case OP_VDestroy: {
67798
 
  p->inVtabMethod = 2;
67799
 
  rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
67800
 
  p->inVtabMethod = 0;
67801
 
  break;
67802
 
}
67803
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
67804
 
 
67805
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
67806
 
/* Opcode: VOpen P1 * * P4 *
67807
 
**
67808
 
** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
67809
 
** P1 is a cursor number.  This opcode opens a cursor to the virtual
67810
 
** table and stores that cursor in P1.
67811
 
*/
67812
 
case OP_VOpen: {
67813
 
#if 0  /* local variables moved into u.ch */
67814
 
  VdbeCursor *pCur;
67815
 
  sqlite3_vtab_cursor *pVtabCursor;
67816
 
  sqlite3_vtab *pVtab;
67817
 
  sqlite3_module *pModule;
67818
 
#endif /* local variables moved into u.ch */
67819
 
 
67820
 
  u.ch.pCur = 0;
67821
 
  u.ch.pVtabCursor = 0;
67822
 
  u.ch.pVtab = pOp->p4.pVtab->pVtab;
67823
 
  u.ch.pModule = (sqlite3_module *)u.ch.pVtab->pModule;
67824
 
  assert(u.ch.pVtab && u.ch.pModule);
67825
 
  rc = u.ch.pModule->xOpen(u.ch.pVtab, &u.ch.pVtabCursor);
67826
 
  importVtabErrMsg(p, u.ch.pVtab);
67827
 
  if( SQLITE_OK==rc ){
67828
 
    /* Initialize sqlite3_vtab_cursor base class */
67829
 
    u.ch.pVtabCursor->pVtab = u.ch.pVtab;
67830
 
 
67831
 
    /* Initialise vdbe cursor object */
67832
 
    u.ch.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
67833
 
    if( u.ch.pCur ){
67834
 
      u.ch.pCur->pVtabCursor = u.ch.pVtabCursor;
67835
 
      u.ch.pCur->pModule = u.ch.pVtabCursor->pVtab->pModule;
67836
 
    }else{
67837
 
      db->mallocFailed = 1;
67838
 
      u.ch.pModule->xClose(u.ch.pVtabCursor);
67839
 
    }
67840
 
  }
67841
 
  break;
67842
 
}
67843
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
67844
 
 
67845
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
67846
 
/* Opcode: VFilter P1 P2 P3 P4 *
67847
 
**
67848
 
** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
67849
 
** the filtered result set is empty.
67850
 
**
67851
 
** P4 is either NULL or a string that was generated by the xBestIndex
67852
 
** method of the module.  The interpretation of the P4 string is left
67853
 
** to the module implementation.
67854
 
**
67855
 
** This opcode invokes the xFilter method on the virtual table specified
67856
 
** by P1.  The integer query plan parameter to xFilter is stored in register
67857
 
** P3. Register P3+1 stores the argc parameter to be passed to the
67858
 
** xFilter method. Registers P3+2..P3+1+argc are the argc
67859
 
** additional parameters which are passed to
67860
 
** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
67861
 
**
67862
 
** A jump is made to P2 if the result set after filtering would be empty.
67863
 
*/
67864
 
case OP_VFilter: {   /* jump */
67865
 
#if 0  /* local variables moved into u.ci */
67866
 
  int nArg;
67867
 
  int iQuery;
67868
 
  const sqlite3_module *pModule;
67869
 
  Mem *pQuery;
67870
 
  Mem *pArgc;
67871
 
  sqlite3_vtab_cursor *pVtabCursor;
67872
 
  sqlite3_vtab *pVtab;
67873
 
  VdbeCursor *pCur;
67874
 
  int res;
67875
 
  int i;
67876
 
  Mem **apArg;
67877
 
#endif /* local variables moved into u.ci */
67878
 
 
67879
 
  u.ci.pQuery = &aMem[pOp->p3];
67880
 
  u.ci.pArgc = &u.ci.pQuery[1];
67881
 
  u.ci.pCur = p->apCsr[pOp->p1];
67882
 
  assert( memIsValid(u.ci.pQuery) );
67883
 
  REGISTER_TRACE(pOp->p3, u.ci.pQuery);
67884
 
  assert( u.ci.pCur->pVtabCursor );
67885
 
  u.ci.pVtabCursor = u.ci.pCur->pVtabCursor;
67886
 
  u.ci.pVtab = u.ci.pVtabCursor->pVtab;
67887
 
  u.ci.pModule = u.ci.pVtab->pModule;
67888
 
 
67889
 
  /* Grab the index number and argc parameters */
67890
 
  assert( (u.ci.pQuery->flags&MEM_Int)!=0 && u.ci.pArgc->flags==MEM_Int );
67891
 
  u.ci.nArg = (int)u.ci.pArgc->u.i;
67892
 
  u.ci.iQuery = (int)u.ci.pQuery->u.i;
67893
 
 
67894
 
  /* Invoke the xFilter method */
67895
 
  {
67896
 
    u.ci.res = 0;
67897
 
    u.ci.apArg = p->apArg;
67898
 
    for(u.ci.i = 0; u.ci.i<u.ci.nArg; u.ci.i++){
67899
 
      u.ci.apArg[u.ci.i] = &u.ci.pArgc[u.ci.i+1];
67900
 
      sqlite3VdbeMemStoreType(u.ci.apArg[u.ci.i]);
67901
 
    }
67902
 
 
67903
 
    p->inVtabMethod = 1;
67904
 
    rc = u.ci.pModule->xFilter(u.ci.pVtabCursor, u.ci.iQuery, pOp->p4.z, u.ci.nArg, u.ci.apArg);
67905
 
    p->inVtabMethod = 0;
67906
 
    importVtabErrMsg(p, u.ci.pVtab);
67907
 
    if( rc==SQLITE_OK ){
67908
 
      u.ci.res = u.ci.pModule->xEof(u.ci.pVtabCursor);
67909
 
    }
67910
 
 
67911
 
    if( u.ci.res ){
67912
 
      pc = pOp->p2 - 1;
67913
 
    }
67914
 
  }
67915
 
  u.ci.pCur->nullRow = 0;
67916
 
 
67917
 
  break;
67918
 
}
67919
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
67920
 
 
67921
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
67922
 
/* Opcode: VColumn P1 P2 P3 * *
67923
 
**
67924
 
** Store the value of the P2-th column of
67925
 
** the row of the virtual-table that the 
67926
 
** P1 cursor is pointing to into register P3.
67927
 
*/
67928
 
case OP_VColumn: {
67929
 
#if 0  /* local variables moved into u.cj */
67930
 
  sqlite3_vtab *pVtab;
67931
 
  const sqlite3_module *pModule;
67932
 
  Mem *pDest;
67933
 
  sqlite3_context sContext;
67934
 
#endif /* local variables moved into u.cj */
67935
 
 
67936
 
  VdbeCursor *pCur = p->apCsr[pOp->p1];
67937
 
  assert( pCur->pVtabCursor );
67938
 
  assert( pOp->p3>0 && pOp->p3<=p->nMem );
67939
 
  u.cj.pDest = &aMem[pOp->p3];
67940
 
  memAboutToChange(p, u.cj.pDest);
67941
 
  if( pCur->nullRow ){
67942
 
    sqlite3VdbeMemSetNull(u.cj.pDest);
67943
 
    break;
67944
 
  }
67945
 
  u.cj.pVtab = pCur->pVtabCursor->pVtab;
67946
 
  u.cj.pModule = u.cj.pVtab->pModule;
67947
 
  assert( u.cj.pModule->xColumn );
67948
 
  memset(&u.cj.sContext, 0, sizeof(u.cj.sContext));
67949
 
 
67950
 
  /* The output cell may already have a buffer allocated. Move
67951
 
  ** the current contents to u.cj.sContext.s so in case the user-function
67952
 
  ** can use the already allocated buffer instead of allocating a
67953
 
  ** new one.
67954
 
  */
67955
 
  sqlite3VdbeMemMove(&u.cj.sContext.s, u.cj.pDest);
67956
 
  MemSetTypeFlag(&u.cj.sContext.s, MEM_Null);
67957
 
 
67958
 
  rc = u.cj.pModule->xColumn(pCur->pVtabCursor, &u.cj.sContext, pOp->p2);
67959
 
  importVtabErrMsg(p, u.cj.pVtab);
67960
 
  if( u.cj.sContext.isError ){
67961
 
    rc = u.cj.sContext.isError;
67962
 
  }
67963
 
 
67964
 
  /* Copy the result of the function to the P3 register. We
67965
 
  ** do this regardless of whether or not an error occurred to ensure any
67966
 
  ** dynamic allocation in u.cj.sContext.s (a Mem struct) is  released.
67967
 
  */
67968
 
  sqlite3VdbeChangeEncoding(&u.cj.sContext.s, encoding);
67969
 
  sqlite3VdbeMemMove(u.cj.pDest, &u.cj.sContext.s);
67970
 
  REGISTER_TRACE(pOp->p3, u.cj.pDest);
67971
 
  UPDATE_MAX_BLOBSIZE(u.cj.pDest);
67972
 
 
67973
 
  if( sqlite3VdbeMemTooBig(u.cj.pDest) ){
67974
 
    goto too_big;
67975
 
  }
67976
 
  break;
67977
 
}
67978
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
67979
 
 
67980
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
67981
 
/* Opcode: VNext P1 P2 * * *
67982
 
**
67983
 
** Advance virtual table P1 to the next row in its result set and
67984
 
** jump to instruction P2.  Or, if the virtual table has reached
67985
 
** the end of its result set, then fall through to the next instruction.
67986
 
*/
67987
 
case OP_VNext: {   /* jump */
67988
 
#if 0  /* local variables moved into u.ck */
67989
 
  sqlite3_vtab *pVtab;
67990
 
  const sqlite3_module *pModule;
67991
 
  int res;
67992
 
  VdbeCursor *pCur;
67993
 
#endif /* local variables moved into u.ck */
67994
 
 
67995
 
  u.ck.res = 0;
67996
 
  u.ck.pCur = p->apCsr[pOp->p1];
67997
 
  assert( u.ck.pCur->pVtabCursor );
67998
 
  if( u.ck.pCur->nullRow ){
67999
 
    break;
68000
 
  }
68001
 
  u.ck.pVtab = u.ck.pCur->pVtabCursor->pVtab;
68002
 
  u.ck.pModule = u.ck.pVtab->pModule;
68003
 
  assert( u.ck.pModule->xNext );
68004
 
 
68005
 
  /* Invoke the xNext() method of the module. There is no way for the
68006
 
  ** underlying implementation to return an error if one occurs during
68007
 
  ** xNext(). Instead, if an error occurs, true is returned (indicating that
68008
 
  ** data is available) and the error code returned when xColumn or
68009
 
  ** some other method is next invoked on the save virtual table cursor.
68010
 
  */
68011
 
  p->inVtabMethod = 1;
68012
 
  rc = u.ck.pModule->xNext(u.ck.pCur->pVtabCursor);
68013
 
  p->inVtabMethod = 0;
68014
 
  importVtabErrMsg(p, u.ck.pVtab);
68015
 
  if( rc==SQLITE_OK ){
68016
 
    u.ck.res = u.ck.pModule->xEof(u.ck.pCur->pVtabCursor);
68017
 
  }
68018
 
 
68019
 
  if( !u.ck.res ){
68020
 
    /* If there is data, jump to P2 */
68021
 
    pc = pOp->p2 - 1;
68022
 
  }
68023
 
  break;
68024
 
}
68025
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
68026
 
 
68027
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
68028
 
/* Opcode: VRename P1 * * P4 *
68029
 
**
68030
 
** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
68031
 
** This opcode invokes the corresponding xRename method. The value
68032
 
** in register P1 is passed as the zName argument to the xRename method.
68033
 
*/
68034
 
case OP_VRename: {
68035
 
#if 0  /* local variables moved into u.cl */
68036
 
  sqlite3_vtab *pVtab;
68037
 
  Mem *pName;
68038
 
#endif /* local variables moved into u.cl */
68039
 
 
68040
 
  u.cl.pVtab = pOp->p4.pVtab->pVtab;
68041
 
  u.cl.pName = &aMem[pOp->p1];
68042
 
  assert( u.cl.pVtab->pModule->xRename );
68043
 
  assert( memIsValid(u.cl.pName) );
68044
 
  REGISTER_TRACE(pOp->p1, u.cl.pName);
68045
 
  assert( u.cl.pName->flags & MEM_Str );
68046
 
  rc = u.cl.pVtab->pModule->xRename(u.cl.pVtab, u.cl.pName->z);
68047
 
  importVtabErrMsg(p, u.cl.pVtab);
68048
 
  p->expired = 0;
68049
 
 
68050
 
  break;
68051
 
}
68052
 
#endif
68053
 
 
68054
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
68055
 
/* Opcode: VUpdate P1 P2 P3 P4 *
68056
 
**
68057
 
** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
68058
 
** This opcode invokes the corresponding xUpdate method. P2 values
68059
 
** are contiguous memory cells starting at P3 to pass to the xUpdate 
68060
 
** invocation. The value in register (P3+P2-1) corresponds to the 
68061
 
** p2th element of the argv array passed to xUpdate.
68062
 
**
68063
 
** The xUpdate method will do a DELETE or an INSERT or both.
68064
 
** The argv[0] element (which corresponds to memory cell P3)
68065
 
** is the rowid of a row to delete.  If argv[0] is NULL then no 
68066
 
** deletion occurs.  The argv[1] element is the rowid of the new 
68067
 
** row.  This can be NULL to have the virtual table select the new 
68068
 
** rowid for itself.  The subsequent elements in the array are 
68069
 
** the values of columns in the new row.
68070
 
**
68071
 
** If P2==1 then no insert is performed.  argv[0] is the rowid of
68072
 
** a row to delete.
68073
 
**
68074
 
** P1 is a boolean flag. If it is set to true and the xUpdate call
68075
 
** is successful, then the value returned by sqlite3_last_insert_rowid() 
68076
 
** is set to the value of the rowid for the row just inserted.
68077
 
*/
68078
 
case OP_VUpdate: {
68079
 
#if 0  /* local variables moved into u.cm */
68080
 
  sqlite3_vtab *pVtab;
68081
 
  sqlite3_module *pModule;
68082
 
  int nArg;
68083
 
  int i;
68084
 
  sqlite_int64 rowid;
68085
 
  Mem **apArg;
68086
 
  Mem *pX;
68087
 
#endif /* local variables moved into u.cm */
68088
 
 
68089
 
  u.cm.pVtab = pOp->p4.pVtab->pVtab;
68090
 
  u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
68091
 
  u.cm.nArg = pOp->p2;
68092
 
  assert( pOp->p4type==P4_VTAB );
68093
 
  if( ALWAYS(u.cm.pModule->xUpdate) ){
68094
 
    u.cm.apArg = p->apArg;
68095
 
    u.cm.pX = &aMem[pOp->p3];
68096
 
    for(u.cm.i=0; u.cm.i<u.cm.nArg; u.cm.i++){
68097
 
      assert( memIsValid(u.cm.pX) );
68098
 
      memAboutToChange(p, u.cm.pX);
68099
 
      sqlite3VdbeMemStoreType(u.cm.pX);
68100
 
      u.cm.apArg[u.cm.i] = u.cm.pX;
68101
 
      u.cm.pX++;
68102
 
    }
68103
 
    rc = u.cm.pModule->xUpdate(u.cm.pVtab, u.cm.nArg, u.cm.apArg, &u.cm.rowid);
68104
 
    importVtabErrMsg(p, u.cm.pVtab);
68105
 
    if( rc==SQLITE_OK && pOp->p1 ){
68106
 
      assert( u.cm.nArg>1 && u.cm.apArg[0] && (u.cm.apArg[0]->flags&MEM_Null) );
68107
 
      db->lastRowid = u.cm.rowid;
68108
 
    }
68109
 
    p->nChange++;
68110
 
  }
68111
 
  break;
68112
 
}
68113
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
68114
 
 
68115
 
#ifndef  SQLITE_OMIT_PAGER_PRAGMAS
68116
 
/* Opcode: Pagecount P1 P2 * * *
68117
 
**
68118
 
** Write the current number of pages in database P1 to memory cell P2.
68119
 
*/
68120
 
case OP_Pagecount: {            /* out2-prerelease */
68121
 
  pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
68122
 
  break;
68123
 
}
68124
 
#endif
68125
 
 
68126
 
 
68127
 
#ifndef  SQLITE_OMIT_PAGER_PRAGMAS
68128
 
/* Opcode: MaxPgcnt P1 P2 P3 * *
68129
 
**
68130
 
** Try to set the maximum page count for database P1 to the value in P3.
68131
 
** Do not let the maximum page count fall below the current page count and
68132
 
** do not change the maximum page count value if P3==0.
68133
 
**
68134
 
** Store the maximum page count after the change in register P2.
68135
 
*/
68136
 
case OP_MaxPgcnt: {            /* out2-prerelease */
68137
 
  unsigned int newMax;
68138
 
  Btree *pBt;
68139
 
 
68140
 
  pBt = db->aDb[pOp->p1].pBt;
68141
 
  newMax = 0;
68142
 
  if( pOp->p3 ){
68143
 
    newMax = sqlite3BtreeLastPage(pBt);
68144
 
    if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
68145
 
  }
68146
 
  pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
68147
 
  break;
68148
 
}
68149
 
#endif
68150
 
 
68151
 
 
68152
 
#ifndef SQLITE_OMIT_TRACE
68153
 
/* Opcode: Trace * * * P4 *
68154
 
**
68155
 
** If tracing is enabled (by the sqlite3_trace()) interface, then
68156
 
** the UTF-8 string contained in P4 is emitted on the trace callback.
68157
 
*/
68158
 
case OP_Trace: {
68159
 
#if 0  /* local variables moved into u.cn */
68160
 
  char *zTrace;
68161
 
#endif /* local variables moved into u.cn */
68162
 
 
68163
 
  u.cn.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
68164
 
  if( u.cn.zTrace ){
68165
 
    if( db->xTrace ){
68166
 
      char *z = sqlite3VdbeExpandSql(p, u.cn.zTrace);
68167
 
      db->xTrace(db->pTraceArg, z);
68168
 
      sqlite3DbFree(db, z);
68169
 
    }
68170
 
#ifdef SQLITE_DEBUG
68171
 
    if( (db->flags & SQLITE_SqlTrace)!=0 ){
68172
 
      sqlite3DebugPrintf("SQL-trace: %s\n", u.cn.zTrace);
68173
 
    }
68174
 
#endif /* SQLITE_DEBUG */
68175
 
  }
68176
 
  break;
68177
 
}
68178
 
#endif
68179
 
 
68180
 
 
68181
 
/* Opcode: Noop * * * * *
68182
 
**
68183
 
** Do nothing.  This instruction is often useful as a jump
68184
 
** destination.
68185
 
*/
68186
 
/*
68187
 
** The magic Explain opcode are only inserted when explain==2 (which
68188
 
** is to say when the EXPLAIN QUERY PLAN syntax is used.)
68189
 
** This opcode records information from the optimizer.  It is the
68190
 
** the same as a no-op.  This opcodesnever appears in a real VM program.
68191
 
*/
68192
 
default: {          /* This is really OP_Noop and OP_Explain */
68193
 
  assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
68194
 
  break;
68195
 
}
68196
 
 
68197
 
/*****************************************************************************
68198
 
** The cases of the switch statement above this line should all be indented
68199
 
** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
68200
 
** readability.  From this point on down, the normal indentation rules are
68201
 
** restored.
68202
 
*****************************************************************************/
68203
 
    }
68204
 
 
68205
 
#ifdef VDBE_PROFILE
68206
 
    {
68207
 
      u64 elapsed = sqlite3Hwtime() - start;
68208
 
      pOp->cycles += elapsed;
68209
 
      pOp->cnt++;
68210
 
#if 0
68211
 
        fprintf(stdout, "%10llu ", elapsed);
68212
 
        sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
68213
 
#endif
68214
 
    }
68215
 
#endif
68216
 
 
68217
 
    /* The following code adds nothing to the actual functionality
68218
 
    ** of the program.  It is only here for testing and debugging.
68219
 
    ** On the other hand, it does burn CPU cycles every time through
68220
 
    ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
68221
 
    */
68222
 
#ifndef NDEBUG
68223
 
    assert( pc>=-1 && pc<p->nOp );
68224
 
 
68225
 
#ifdef SQLITE_DEBUG
68226
 
    if( p->trace ){
68227
 
      if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
68228
 
      if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
68229
 
        registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]);
68230
 
      }
68231
 
      if( pOp->opflags & OPFLG_OUT3 ){
68232
 
        registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]);
68233
 
      }
68234
 
    }
68235
 
#endif  /* SQLITE_DEBUG */
68236
 
#endif  /* NDEBUG */
68237
 
  }  /* The end of the for(;;) loop the loops through opcodes */
68238
 
 
68239
 
  /* If we reach this point, it means that execution is finished with
68240
 
  ** an error of some kind.
68241
 
  */
68242
 
vdbe_error_halt:
68243
 
  assert( rc );
68244
 
  p->rc = rc;
68245
 
  testcase( sqlite3GlobalConfig.xLog!=0 );
68246
 
  sqlite3_log(rc, "statement aborts at %d: [%s] %s", 
68247
 
                   pc, p->zSql, p->zErrMsg);
68248
 
  sqlite3VdbeHalt(p);
68249
 
  if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
68250
 
  rc = SQLITE_ERROR;
68251
 
  if( resetSchemaOnFault>0 ){
68252
 
    sqlite3ResetInternalSchema(db, resetSchemaOnFault-1);
68253
 
  }
68254
 
 
68255
 
  /* This is the only way out of this procedure.  We have to
68256
 
  ** release the mutexes on btrees that were acquired at the
68257
 
  ** top. */
68258
 
vdbe_return:
68259
 
  sqlite3VdbeLeave(p);
68260
 
  return rc;
68261
 
 
68262
 
  /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
68263
 
  ** is encountered.
68264
 
  */
68265
 
too_big:
68266
 
  sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
68267
 
  rc = SQLITE_TOOBIG;
68268
 
  goto vdbe_error_halt;
68269
 
 
68270
 
  /* Jump to here if a malloc() fails.
68271
 
  */
68272
 
no_mem:
68273
 
  db->mallocFailed = 1;
68274
 
  sqlite3SetString(&p->zErrMsg, db, "out of memory");
68275
 
  rc = SQLITE_NOMEM;
68276
 
  goto vdbe_error_halt;
68277
 
 
68278
 
  /* Jump to here for any other kind of fatal error.  The "rc" variable
68279
 
  ** should hold the error number.
68280
 
  */
68281
 
abort_due_to_error:
68282
 
  assert( p->zErrMsg==0 );
68283
 
  if( db->mallocFailed ) rc = SQLITE_NOMEM;
68284
 
  if( rc!=SQLITE_IOERR_NOMEM ){
68285
 
    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
68286
 
  }
68287
 
  goto vdbe_error_halt;
68288
 
 
68289
 
  /* Jump to here if the sqlite3_interrupt() API sets the interrupt
68290
 
  ** flag.
68291
 
  */
68292
 
abort_due_to_interrupt:
68293
 
  assert( db->u1.isInterrupted );
68294
 
  rc = SQLITE_INTERRUPT;
68295
 
  p->rc = rc;
68296
 
  sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
68297
 
  goto vdbe_error_halt;
68298
 
}
68299
 
 
68300
 
/************** End of vdbe.c ************************************************/
68301
 
/************** Begin file vdbeblob.c ****************************************/
68302
 
/*
68303
 
** 2007 May 1
68304
 
**
68305
 
** The author disclaims copyright to this source code.  In place of
68306
 
** a legal notice, here is a blessing:
68307
 
**
68308
 
**    May you do good and not evil.
68309
 
**    May you find forgiveness for yourself and forgive others.
68310
 
**    May you share freely, never taking more than you give.
68311
 
**
68312
 
*************************************************************************
68313
 
**
68314
 
** This file contains code used to implement incremental BLOB I/O.
68315
 
*/
68316
 
 
68317
 
 
68318
 
#ifndef SQLITE_OMIT_INCRBLOB
68319
 
 
68320
 
/*
68321
 
** Valid sqlite3_blob* handles point to Incrblob structures.
68322
 
*/
68323
 
typedef struct Incrblob Incrblob;
68324
 
struct Incrblob {
68325
 
  int flags;              /* Copy of "flags" passed to sqlite3_blob_open() */
68326
 
  int nByte;              /* Size of open blob, in bytes */
68327
 
  int iOffset;            /* Byte offset of blob in cursor data */
68328
 
  int iCol;               /* Table column this handle is open on */
68329
 
  BtCursor *pCsr;         /* Cursor pointing at blob row */
68330
 
  sqlite3_stmt *pStmt;    /* Statement holding cursor open */
68331
 
  sqlite3 *db;            /* The associated database */
68332
 
};
68333
 
 
68334
 
 
68335
 
/*
68336
 
** This function is used by both blob_open() and blob_reopen(). It seeks
68337
 
** the b-tree cursor associated with blob handle p to point to row iRow.
68338
 
** If successful, SQLITE_OK is returned and subsequent calls to
68339
 
** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
68340
 
**
68341
 
** If an error occurs, or if the specified row does not exist or does not
68342
 
** contain a value of type TEXT or BLOB in the column nominated when the
68343
 
** blob handle was opened, then an error code is returned and *pzErr may
68344
 
** be set to point to a buffer containing an error message. It is the
68345
 
** responsibility of the caller to free the error message buffer using
68346
 
** sqlite3DbFree().
68347
 
**
68348
 
** If an error does occur, then the b-tree cursor is closed. All subsequent
68349
 
** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will 
68350
 
** immediately return SQLITE_ABORT.
68351
 
*/
68352
 
static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
68353
 
  int rc;                         /* Error code */
68354
 
  char *zErr = 0;                 /* Error message */
68355
 
  Vdbe *v = (Vdbe *)p->pStmt;
68356
 
 
68357
 
  /* Set the value of the SQL statements only variable to integer iRow. 
68358
 
  ** This is done directly instead of using sqlite3_bind_int64() to avoid 
68359
 
  ** triggering asserts related to mutexes.
68360
 
  */
68361
 
  assert( v->aVar[0].flags&MEM_Int );
68362
 
  v->aVar[0].u.i = iRow;
68363
 
 
68364
 
  rc = sqlite3_step(p->pStmt);
68365
 
  if( rc==SQLITE_ROW ){
68366
 
    u32 type = v->apCsr[0]->aType[p->iCol];
68367
 
    if( type<12 ){
68368
 
      zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
68369
 
          type==0?"null": type==7?"real": "integer"
68370
 
      );
68371
 
      rc = SQLITE_ERROR;
68372
 
      sqlite3_finalize(p->pStmt);
68373
 
      p->pStmt = 0;
68374
 
    }else{
68375
 
      p->iOffset = v->apCsr[0]->aOffset[p->iCol];
68376
 
      p->nByte = sqlite3VdbeSerialTypeLen(type);
68377
 
      p->pCsr =  v->apCsr[0]->pCursor;
68378
 
      sqlite3BtreeEnterCursor(p->pCsr);
68379
 
      sqlite3BtreeCacheOverflow(p->pCsr);
68380
 
      sqlite3BtreeLeaveCursor(p->pCsr);
68381
 
    }
68382
 
  }
68383
 
 
68384
 
  if( rc==SQLITE_ROW ){
68385
 
    rc = SQLITE_OK;
68386
 
  }else if( p->pStmt ){
68387
 
    rc = sqlite3_finalize(p->pStmt);
68388
 
    p->pStmt = 0;
68389
 
    if( rc==SQLITE_OK ){
68390
 
      zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
68391
 
      rc = SQLITE_ERROR;
68392
 
    }else{
68393
 
      zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
68394
 
    }
68395
 
  }
68396
 
 
68397
 
  assert( rc!=SQLITE_OK || zErr==0 );
68398
 
  assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
68399
 
 
68400
 
  *pzErr = zErr;
68401
 
  return rc;
68402
 
}
68403
 
 
68404
 
/*
68405
 
** Open a blob handle.
68406
 
*/
68407
 
SQLITE_API int sqlite3_blob_open(
68408
 
  sqlite3* db,            /* The database connection */
68409
 
  const char *zDb,        /* The attached database containing the blob */
68410
 
  const char *zTable,     /* The table containing the blob */
68411
 
  const char *zColumn,    /* The column containing the blob */
68412
 
  sqlite_int64 iRow,      /* The row containing the glob */
68413
 
  int flags,              /* True -> read/write access, false -> read-only */
68414
 
  sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
68415
 
){
68416
 
  int nAttempt = 0;
68417
 
  int iCol;               /* Index of zColumn in row-record */
68418
 
 
68419
 
  /* This VDBE program seeks a btree cursor to the identified 
68420
 
  ** db/table/row entry. The reason for using a vdbe program instead
68421
 
  ** of writing code to use the b-tree layer directly is that the
68422
 
  ** vdbe program will take advantage of the various transaction,
68423
 
  ** locking and error handling infrastructure built into the vdbe.
68424
 
  **
68425
 
  ** After seeking the cursor, the vdbe executes an OP_ResultRow.
68426
 
  ** Code external to the Vdbe then "borrows" the b-tree cursor and
68427
 
  ** uses it to implement the blob_read(), blob_write() and 
68428
 
  ** blob_bytes() functions.
68429
 
  **
68430
 
  ** The sqlite3_blob_close() function finalizes the vdbe program,
68431
 
  ** which closes the b-tree cursor and (possibly) commits the 
68432
 
  ** transaction.
68433
 
  */
68434
 
  static const VdbeOpList openBlob[] = {
68435
 
    {OP_Transaction, 0, 0, 0},     /* 0: Start a transaction */
68436
 
    {OP_VerifyCookie, 0, 0, 0},    /* 1: Check the schema cookie */
68437
 
    {OP_TableLock, 0, 0, 0},       /* 2: Acquire a read or write lock */
68438
 
 
68439
 
    /* One of the following two instructions is replaced by an OP_Noop. */
68440
 
    {OP_OpenRead, 0, 0, 0},        /* 3: Open cursor 0 for reading */
68441
 
    {OP_OpenWrite, 0, 0, 0},       /* 4: Open cursor 0 for read/write */
68442
 
 
68443
 
    {OP_Variable, 1, 1, 1},        /* 5: Push the rowid to the stack */
68444
 
    {OP_NotExists, 0, 10, 1},      /* 6: Seek the cursor */
68445
 
    {OP_Column, 0, 0, 1},          /* 7  */
68446
 
    {OP_ResultRow, 1, 0, 0},       /* 8  */
68447
 
    {OP_Goto, 0, 5, 0},            /* 9  */
68448
 
    {OP_Close, 0, 0, 0},           /* 10 */
68449
 
    {OP_Halt, 0, 0, 0},            /* 11 */
68450
 
  };
68451
 
 
68452
 
  int rc = SQLITE_OK;
68453
 
  char *zErr = 0;
68454
 
  Table *pTab;
68455
 
  Parse *pParse = 0;
68456
 
  Incrblob *pBlob = 0;
68457
 
 
68458
 
  flags = !!flags;                /* flags = (flags ? 1 : 0); */
68459
 
  *ppBlob = 0;
68460
 
 
68461
 
  sqlite3_mutex_enter(db->mutex);
68462
 
 
68463
 
  pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
68464
 
  if( !pBlob ) goto blob_open_out;
68465
 
  pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
68466
 
  if( !pParse ) goto blob_open_out;
68467
 
 
68468
 
  do {
68469
 
    memset(pParse, 0, sizeof(Parse));
68470
 
    pParse->db = db;
68471
 
    sqlite3DbFree(db, zErr);
68472
 
    zErr = 0;
68473
 
 
68474
 
    sqlite3BtreeEnterAll(db);
68475
 
    pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
68476
 
    if( pTab && IsVirtual(pTab) ){
68477
 
      pTab = 0;
68478
 
      sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
68479
 
    }
68480
 
#ifndef SQLITE_OMIT_VIEW
68481
 
    if( pTab && pTab->pSelect ){
68482
 
      pTab = 0;
68483
 
      sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
68484
 
    }
68485
 
#endif
68486
 
    if( !pTab ){
68487
 
      if( pParse->zErrMsg ){
68488
 
        sqlite3DbFree(db, zErr);
68489
 
        zErr = pParse->zErrMsg;
68490
 
        pParse->zErrMsg = 0;
68491
 
      }
68492
 
      rc = SQLITE_ERROR;
68493
 
      sqlite3BtreeLeaveAll(db);
68494
 
      goto blob_open_out;
68495
 
    }
68496
 
 
68497
 
    /* Now search pTab for the exact column. */
68498
 
    for(iCol=0; iCol<pTab->nCol; iCol++) {
68499
 
      if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
68500
 
        break;
68501
 
      }
68502
 
    }
68503
 
    if( iCol==pTab->nCol ){
68504
 
      sqlite3DbFree(db, zErr);
68505
 
      zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
68506
 
      rc = SQLITE_ERROR;
68507
 
      sqlite3BtreeLeaveAll(db);
68508
 
      goto blob_open_out;
68509
 
    }
68510
 
 
68511
 
    /* If the value is being opened for writing, check that the
68512
 
    ** column is not indexed, and that it is not part of a foreign key. 
68513
 
    ** It is against the rules to open a column to which either of these
68514
 
    ** descriptions applies for writing.  */
68515
 
    if( flags ){
68516
 
      const char *zFault = 0;
68517
 
      Index *pIdx;
68518
 
#ifndef SQLITE_OMIT_FOREIGN_KEY
68519
 
      if( db->flags&SQLITE_ForeignKeys ){
68520
 
        /* Check that the column is not part of an FK child key definition. It
68521
 
        ** is not necessary to check if it is part of a parent key, as parent
68522
 
        ** key columns must be indexed. The check below will pick up this 
68523
 
        ** case.  */
68524
 
        FKey *pFKey;
68525
 
        for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
68526
 
          int j;
68527
 
          for(j=0; j<pFKey->nCol; j++){
68528
 
            if( pFKey->aCol[j].iFrom==iCol ){
68529
 
              zFault = "foreign key";
68530
 
            }
68531
 
          }
68532
 
        }
68533
 
      }
68534
 
#endif
68535
 
      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
68536
 
        int j;
68537
 
        for(j=0; j<pIdx->nColumn; j++){
68538
 
          if( pIdx->aiColumn[j]==iCol ){
68539
 
            zFault = "indexed";
68540
 
          }
68541
 
        }
68542
 
      }
68543
 
      if( zFault ){
68544
 
        sqlite3DbFree(db, zErr);
68545
 
        zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
68546
 
        rc = SQLITE_ERROR;
68547
 
        sqlite3BtreeLeaveAll(db);
68548
 
        goto blob_open_out;
68549
 
      }
68550
 
    }
68551
 
 
68552
 
    pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(db);
68553
 
    assert( pBlob->pStmt || db->mallocFailed );
68554
 
    if( pBlob->pStmt ){
68555
 
      Vdbe *v = (Vdbe *)pBlob->pStmt;
68556
 
      int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
68557
 
 
68558
 
      sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
68559
 
 
68560
 
 
68561
 
      /* Configure the OP_Transaction */
68562
 
      sqlite3VdbeChangeP1(v, 0, iDb);
68563
 
      sqlite3VdbeChangeP2(v, 0, flags);
68564
 
 
68565
 
      /* Configure the OP_VerifyCookie */
68566
 
      sqlite3VdbeChangeP1(v, 1, iDb);
68567
 
      sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
68568
 
      sqlite3VdbeChangeP3(v, 1, pTab->pSchema->iGeneration);
68569
 
 
68570
 
      /* Make sure a mutex is held on the table to be accessed */
68571
 
      sqlite3VdbeUsesBtree(v, iDb); 
68572
 
 
68573
 
      /* Configure the OP_TableLock instruction */
68574
 
#ifdef SQLITE_OMIT_SHARED_CACHE
68575
 
      sqlite3VdbeChangeToNoop(v, 2, 1);
68576
 
#else
68577
 
      sqlite3VdbeChangeP1(v, 2, iDb);
68578
 
      sqlite3VdbeChangeP2(v, 2, pTab->tnum);
68579
 
      sqlite3VdbeChangeP3(v, 2, flags);
68580
 
      sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
68581
 
#endif
68582
 
 
68583
 
      /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
68584
 
      ** parameter of the other to pTab->tnum.  */
68585
 
      sqlite3VdbeChangeToNoop(v, 4 - flags, 1);
68586
 
      sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
68587
 
      sqlite3VdbeChangeP3(v, 3 + flags, iDb);
68588
 
 
68589
 
      /* Configure the number of columns. Configure the cursor to
68590
 
      ** think that the table has one more column than it really
68591
 
      ** does. An OP_Column to retrieve this imaginary column will
68592
 
      ** always return an SQL NULL. This is useful because it means
68593
 
      ** we can invoke OP_Column to fill in the vdbe cursors type 
68594
 
      ** and offset cache without causing any IO.
68595
 
      */
68596
 
      sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
68597
 
      sqlite3VdbeChangeP2(v, 7, pTab->nCol);
68598
 
      if( !db->mallocFailed ){
68599
 
        sqlite3VdbeMakeReady(v, 1, 1, 1, 0, 0, 0);
68600
 
      }
68601
 
    }
68602
 
   
68603
 
    pBlob->flags = flags;
68604
 
    pBlob->iCol = iCol;
68605
 
    pBlob->db = db;
68606
 
    sqlite3BtreeLeaveAll(db);
68607
 
    if( db->mallocFailed ){
68608
 
      goto blob_open_out;
68609
 
    }
68610
 
    sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
68611
 
    rc = blobSeekToRow(pBlob, iRow, &zErr);
68612
 
  } while( (++nAttempt)<5 && rc==SQLITE_SCHEMA );
68613
 
 
68614
 
blob_open_out:
68615
 
  if( rc==SQLITE_OK && db->mallocFailed==0 ){
68616
 
    *ppBlob = (sqlite3_blob *)pBlob;
68617
 
  }else{
68618
 
    if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
68619
 
    sqlite3DbFree(db, pBlob);
68620
 
  }
68621
 
  sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
68622
 
  sqlite3DbFree(db, zErr);
68623
 
  sqlite3StackFree(db, pParse);
68624
 
  rc = sqlite3ApiExit(db, rc);
68625
 
  sqlite3_mutex_leave(db->mutex);
68626
 
  return rc;
68627
 
}
68628
 
 
68629
 
/*
68630
 
** Close a blob handle that was previously created using
68631
 
** sqlite3_blob_open().
68632
 
*/
68633
 
SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
68634
 
  Incrblob *p = (Incrblob *)pBlob;
68635
 
  int rc;
68636
 
  sqlite3 *db;
68637
 
 
68638
 
  if( p ){
68639
 
    db = p->db;
68640
 
    sqlite3_mutex_enter(db->mutex);
68641
 
    rc = sqlite3_finalize(p->pStmt);
68642
 
    sqlite3DbFree(db, p);
68643
 
    sqlite3_mutex_leave(db->mutex);
68644
 
  }else{
68645
 
    rc = SQLITE_OK;
68646
 
  }
68647
 
  return rc;
68648
 
}
68649
 
 
68650
 
/*
68651
 
** Perform a read or write operation on a blob
68652
 
*/
68653
 
static int blobReadWrite(
68654
 
  sqlite3_blob *pBlob, 
68655
 
  void *z, 
68656
 
  int n, 
68657
 
  int iOffset, 
68658
 
  int (*xCall)(BtCursor*, u32, u32, void*)
68659
 
){
68660
 
  int rc;
68661
 
  Incrblob *p = (Incrblob *)pBlob;
68662
 
  Vdbe *v;
68663
 
  sqlite3 *db;
68664
 
 
68665
 
  if( p==0 ) return SQLITE_MISUSE_BKPT;
68666
 
  db = p->db;
68667
 
  sqlite3_mutex_enter(db->mutex);
68668
 
  v = (Vdbe*)p->pStmt;
68669
 
 
68670
 
  if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
68671
 
    /* Request is out of range. Return a transient error. */
68672
 
    rc = SQLITE_ERROR;
68673
 
    sqlite3Error(db, SQLITE_ERROR, 0);
68674
 
  }else if( v==0 ){
68675
 
    /* If there is no statement handle, then the blob-handle has
68676
 
    ** already been invalidated. Return SQLITE_ABORT in this case.
68677
 
    */
68678
 
    rc = SQLITE_ABORT;
68679
 
  }else{
68680
 
    /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
68681
 
    ** returned, clean-up the statement handle.
68682
 
    */
68683
 
    assert( db == v->db );
68684
 
    sqlite3BtreeEnterCursor(p->pCsr);
68685
 
    rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
68686
 
    sqlite3BtreeLeaveCursor(p->pCsr);
68687
 
    if( rc==SQLITE_ABORT ){
68688
 
      sqlite3VdbeFinalize(v);
68689
 
      p->pStmt = 0;
68690
 
    }else{
68691
 
      db->errCode = rc;
68692
 
      v->rc = rc;
68693
 
    }
68694
 
  }
68695
 
  rc = sqlite3ApiExit(db, rc);
68696
 
  sqlite3_mutex_leave(db->mutex);
68697
 
  return rc;
68698
 
}
68699
 
 
68700
 
/*
68701
 
** Read data from a blob handle.
68702
 
*/
68703
 
SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
68704
 
  return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
68705
 
}
68706
 
 
68707
 
/*
68708
 
** Write data to a blob handle.
68709
 
*/
68710
 
SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
68711
 
  return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
68712
 
}
68713
 
 
68714
 
/*
68715
 
** Query a blob handle for the size of the data.
68716
 
**
68717
 
** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
68718
 
** so no mutex is required for access.
68719
 
*/
68720
 
SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
68721
 
  Incrblob *p = (Incrblob *)pBlob;
68722
 
  return (p && p->pStmt) ? p->nByte : 0;
68723
 
}
68724
 
 
68725
 
/*
68726
 
** Move an existing blob handle to point to a different row of the same
68727
 
** database table.
68728
 
**
68729
 
** If an error occurs, or if the specified row does not exist or does not
68730
 
** contain a blob or text value, then an error code is returned and the
68731
 
** database handle error code and message set. If this happens, then all 
68732
 
** subsequent calls to sqlite3_blob_xxx() functions (except blob_close()) 
68733
 
** immediately return SQLITE_ABORT.
68734
 
*/
68735
 
SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
68736
 
  int rc;
68737
 
  Incrblob *p = (Incrblob *)pBlob;
68738
 
  sqlite3 *db;
68739
 
 
68740
 
  if( p==0 ) return SQLITE_MISUSE_BKPT;
68741
 
  db = p->db;
68742
 
  sqlite3_mutex_enter(db->mutex);
68743
 
 
68744
 
  if( p->pStmt==0 ){
68745
 
    /* If there is no statement handle, then the blob-handle has
68746
 
    ** already been invalidated. Return SQLITE_ABORT in this case.
68747
 
    */
68748
 
    rc = SQLITE_ABORT;
68749
 
  }else{
68750
 
    char *zErr;
68751
 
    rc = blobSeekToRow(p, iRow, &zErr);
68752
 
    if( rc!=SQLITE_OK ){
68753
 
      sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
68754
 
      sqlite3DbFree(db, zErr);
68755
 
    }
68756
 
    assert( rc!=SQLITE_SCHEMA );
68757
 
  }
68758
 
 
68759
 
  rc = sqlite3ApiExit(db, rc);
68760
 
  assert( rc==SQLITE_OK || p->pStmt==0 );
68761
 
  sqlite3_mutex_leave(db->mutex);
68762
 
  return rc;
68763
 
}
68764
 
 
68765
 
#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
68766
 
 
68767
 
/************** End of vdbeblob.c ********************************************/
68768
 
/************** Begin file journal.c *****************************************/
68769
 
/*
68770
 
** 2007 August 22
68771
 
**
68772
 
** The author disclaims copyright to this source code.  In place of
68773
 
** a legal notice, here is a blessing:
68774
 
**
68775
 
**    May you do good and not evil.
68776
 
**    May you find forgiveness for yourself and forgive others.
68777
 
**    May you share freely, never taking more than you give.
68778
 
**
68779
 
*************************************************************************
68780
 
**
68781
 
** This file implements a special kind of sqlite3_file object used
68782
 
** by SQLite to create journal files if the atomic-write optimization
68783
 
** is enabled.
68784
 
**
68785
 
** The distinctive characteristic of this sqlite3_file is that the
68786
 
** actual on disk file is created lazily. When the file is created,
68787
 
** the caller specifies a buffer size for an in-memory buffer to
68788
 
** be used to service read() and write() requests. The actual file
68789
 
** on disk is not created or populated until either:
68790
 
**
68791
 
**   1) The in-memory representation grows too large for the allocated 
68792
 
**      buffer, or
68793
 
**   2) The sqlite3JournalCreate() function is called.
68794
 
*/
68795
 
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
68796
 
 
68797
 
 
68798
 
/*
68799
 
** A JournalFile object is a subclass of sqlite3_file used by
68800
 
** as an open file handle for journal files.
68801
 
*/
68802
 
struct JournalFile {
68803
 
  sqlite3_io_methods *pMethod;    /* I/O methods on journal files */
68804
 
  int nBuf;                       /* Size of zBuf[] in bytes */
68805
 
  char *zBuf;                     /* Space to buffer journal writes */
68806
 
  int iSize;                      /* Amount of zBuf[] currently used */
68807
 
  int flags;                      /* xOpen flags */
68808
 
  sqlite3_vfs *pVfs;              /* The "real" underlying VFS */
68809
 
  sqlite3_file *pReal;            /* The "real" underlying file descriptor */
68810
 
  const char *zJournal;           /* Name of the journal file */
68811
 
};
68812
 
typedef struct JournalFile JournalFile;
68813
 
 
68814
 
/*
68815
 
** If it does not already exists, create and populate the on-disk file 
68816
 
** for JournalFile p.
68817
 
*/
68818
 
static int createFile(JournalFile *p){
68819
 
  int rc = SQLITE_OK;
68820
 
  if( !p->pReal ){
68821
 
    sqlite3_file *pReal = (sqlite3_file *)&p[1];
68822
 
    rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
68823
 
    if( rc==SQLITE_OK ){
68824
 
      p->pReal = pReal;
68825
 
      if( p->iSize>0 ){
68826
 
        assert(p->iSize<=p->nBuf);
68827
 
        rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
68828
 
      }
68829
 
    }
68830
 
  }
68831
 
  return rc;
68832
 
}
68833
 
 
68834
 
/*
68835
 
** Close the file.
68836
 
*/
68837
 
static int jrnlClose(sqlite3_file *pJfd){
68838
 
  JournalFile *p = (JournalFile *)pJfd;
68839
 
  if( p->pReal ){
68840
 
    sqlite3OsClose(p->pReal);
68841
 
  }
68842
 
  sqlite3_free(p->zBuf);
68843
 
  return SQLITE_OK;
68844
 
}
68845
 
 
68846
 
/*
68847
 
** Read data from the file.
68848
 
*/
68849
 
static int jrnlRead(
68850
 
  sqlite3_file *pJfd,    /* The journal file from which to read */
68851
 
  void *zBuf,            /* Put the results here */
68852
 
  int iAmt,              /* Number of bytes to read */
68853
 
  sqlite_int64 iOfst     /* Begin reading at this offset */
68854
 
){
68855
 
  int rc = SQLITE_OK;
68856
 
  JournalFile *p = (JournalFile *)pJfd;
68857
 
  if( p->pReal ){
68858
 
    rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
68859
 
  }else if( (iAmt+iOfst)>p->iSize ){
68860
 
    rc = SQLITE_IOERR_SHORT_READ;
68861
 
  }else{
68862
 
    memcpy(zBuf, &p->zBuf[iOfst], iAmt);
68863
 
  }
68864
 
  return rc;
68865
 
}
68866
 
 
68867
 
/*
68868
 
** Write data to the file.
68869
 
*/
68870
 
static int jrnlWrite(
68871
 
  sqlite3_file *pJfd,    /* The journal file into which to write */
68872
 
  const void *zBuf,      /* Take data to be written from here */
68873
 
  int iAmt,              /* Number of bytes to write */
68874
 
  sqlite_int64 iOfst     /* Begin writing at this offset into the file */
68875
 
){
68876
 
  int rc = SQLITE_OK;
68877
 
  JournalFile *p = (JournalFile *)pJfd;
68878
 
  if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
68879
 
    rc = createFile(p);
68880
 
  }
68881
 
  if( rc==SQLITE_OK ){
68882
 
    if( p->pReal ){
68883
 
      rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
68884
 
    }else{
68885
 
      memcpy(&p->zBuf[iOfst], zBuf, iAmt);
68886
 
      if( p->iSize<(iOfst+iAmt) ){
68887
 
        p->iSize = (iOfst+iAmt);
68888
 
      }
68889
 
    }
68890
 
  }
68891
 
  return rc;
68892
 
}
68893
 
 
68894
 
/*
68895
 
** Truncate the file.
68896
 
*/
68897
 
static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
68898
 
  int rc = SQLITE_OK;
68899
 
  JournalFile *p = (JournalFile *)pJfd;
68900
 
  if( p->pReal ){
68901
 
    rc = sqlite3OsTruncate(p->pReal, size);
68902
 
  }else if( size<p->iSize ){
68903
 
    p->iSize = size;
68904
 
  }
68905
 
  return rc;
68906
 
}
68907
 
 
68908
 
/*
68909
 
** Sync the file.
68910
 
*/
68911
 
static int jrnlSync(sqlite3_file *pJfd, int flags){
68912
 
  int rc;
68913
 
  JournalFile *p = (JournalFile *)pJfd;
68914
 
  if( p->pReal ){
68915
 
    rc = sqlite3OsSync(p->pReal, flags);
68916
 
  }else{
68917
 
    rc = SQLITE_OK;
68918
 
  }
68919
 
  return rc;
68920
 
}
68921
 
 
68922
 
/*
68923
 
** Query the size of the file in bytes.
68924
 
*/
68925
 
static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
68926
 
  int rc = SQLITE_OK;
68927
 
  JournalFile *p = (JournalFile *)pJfd;
68928
 
  if( p->pReal ){
68929
 
    rc = sqlite3OsFileSize(p->pReal, pSize);
68930
 
  }else{
68931
 
    *pSize = (sqlite_int64) p->iSize;
68932
 
  }
68933
 
  return rc;
68934
 
}
68935
 
 
68936
 
/*
68937
 
** Table of methods for JournalFile sqlite3_file object.
68938
 
*/
68939
 
static struct sqlite3_io_methods JournalFileMethods = {
68940
 
  1,             /* iVersion */
68941
 
  jrnlClose,     /* xClose */
68942
 
  jrnlRead,      /* xRead */
68943
 
  jrnlWrite,     /* xWrite */
68944
 
  jrnlTruncate,  /* xTruncate */
68945
 
  jrnlSync,      /* xSync */
68946
 
  jrnlFileSize,  /* xFileSize */
68947
 
  0,             /* xLock */
68948
 
  0,             /* xUnlock */
68949
 
  0,             /* xCheckReservedLock */
68950
 
  0,             /* xFileControl */
68951
 
  0,             /* xSectorSize */
68952
 
  0,             /* xDeviceCharacteristics */
68953
 
  0,             /* xShmMap */
68954
 
  0,             /* xShmLock */
68955
 
  0,             /* xShmBarrier */
68956
 
  0              /* xShmUnmap */
68957
 
};
68958
 
 
68959
 
/* 
68960
 
** Open a journal file.
68961
 
*/
68962
 
SQLITE_PRIVATE int sqlite3JournalOpen(
68963
 
  sqlite3_vfs *pVfs,         /* The VFS to use for actual file I/O */
68964
 
  const char *zName,         /* Name of the journal file */
68965
 
  sqlite3_file *pJfd,        /* Preallocated, blank file handle */
68966
 
  int flags,                 /* Opening flags */
68967
 
  int nBuf                   /* Bytes buffered before opening the file */
68968
 
){
68969
 
  JournalFile *p = (JournalFile *)pJfd;
68970
 
  memset(p, 0, sqlite3JournalSize(pVfs));
68971
 
  if( nBuf>0 ){
68972
 
    p->zBuf = sqlite3MallocZero(nBuf);
68973
 
    if( !p->zBuf ){
68974
 
      return SQLITE_NOMEM;
68975
 
    }
68976
 
  }else{
68977
 
    return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
68978
 
  }
68979
 
  p->pMethod = &JournalFileMethods;
68980
 
  p->nBuf = nBuf;
68981
 
  p->flags = flags;
68982
 
  p->zJournal = zName;
68983
 
  p->pVfs = pVfs;
68984
 
  return SQLITE_OK;
68985
 
}
68986
 
 
68987
 
/*
68988
 
** If the argument p points to a JournalFile structure, and the underlying
68989
 
** file has not yet been created, create it now.
68990
 
*/
68991
 
SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
68992
 
  if( p->pMethods!=&JournalFileMethods ){
68993
 
    return SQLITE_OK;
68994
 
  }
68995
 
  return createFile((JournalFile *)p);
68996
 
}
68997
 
 
68998
 
/* 
68999
 
** Return the number of bytes required to store a JournalFile that uses vfs
69000
 
** pVfs to create the underlying on-disk files.
69001
 
*/
69002
 
SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
69003
 
  return (pVfs->szOsFile+sizeof(JournalFile));
69004
 
}
69005
 
#endif
69006
 
 
69007
 
/************** End of journal.c *********************************************/
69008
 
/************** Begin file memjournal.c **************************************/
69009
 
/*
69010
 
** 2008 October 7
69011
 
**
69012
 
** The author disclaims copyright to this source code.  In place of
69013
 
** a legal notice, here is a blessing:
69014
 
**
69015
 
**    May you do good and not evil.
69016
 
**    May you find forgiveness for yourself and forgive others.
69017
 
**    May you share freely, never taking more than you give.
69018
 
**
69019
 
*************************************************************************
69020
 
**
69021
 
** This file contains code use to implement an in-memory rollback journal.
69022
 
** The in-memory rollback journal is used to journal transactions for
69023
 
** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
69024
 
*/
69025
 
 
69026
 
/* Forward references to internal structures */
69027
 
typedef struct MemJournal MemJournal;
69028
 
typedef struct FilePoint FilePoint;
69029
 
typedef struct FileChunk FileChunk;
69030
 
 
69031
 
/* Space to hold the rollback journal is allocated in increments of
69032
 
** this many bytes.
69033
 
**
69034
 
** The size chosen is a little less than a power of two.  That way,
69035
 
** the FileChunk object will have a size that almost exactly fills
69036
 
** a power-of-two allocation.  This mimimizes wasted space in power-of-two
69037
 
** memory allocators.
69038
 
*/
69039
 
#define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
69040
 
 
69041
 
/* Macro to find the minimum of two numeric values.
69042
 
*/
69043
 
#ifndef MIN
69044
 
# define MIN(x,y) ((x)<(y)?(x):(y))
69045
 
#endif
69046
 
 
69047
 
/*
69048
 
** The rollback journal is composed of a linked list of these structures.
69049
 
*/
69050
 
struct FileChunk {
69051
 
  FileChunk *pNext;               /* Next chunk in the journal */
69052
 
  u8 zChunk[JOURNAL_CHUNKSIZE];   /* Content of this chunk */
69053
 
};
69054
 
 
69055
 
/*
69056
 
** An instance of this object serves as a cursor into the rollback journal.
69057
 
** The cursor can be either for reading or writing.
69058
 
*/
69059
 
struct FilePoint {
69060
 
  sqlite3_int64 iOffset;          /* Offset from the beginning of the file */
69061
 
  FileChunk *pChunk;              /* Specific chunk into which cursor points */
69062
 
};
69063
 
 
69064
 
/*
69065
 
** This subclass is a subclass of sqlite3_file.  Each open memory-journal
69066
 
** is an instance of this class.
69067
 
*/
69068
 
struct MemJournal {
69069
 
  sqlite3_io_methods *pMethod;    /* Parent class. MUST BE FIRST */
69070
 
  FileChunk *pFirst;              /* Head of in-memory chunk-list */
69071
 
  FilePoint endpoint;             /* Pointer to the end of the file */
69072
 
  FilePoint readpoint;            /* Pointer to the end of the last xRead() */
69073
 
};
69074
 
 
69075
 
/*
69076
 
** Read data from the in-memory journal file.  This is the implementation
69077
 
** of the sqlite3_vfs.xRead method.
69078
 
*/
69079
 
static int memjrnlRead(
69080
 
  sqlite3_file *pJfd,    /* The journal file from which to read */
69081
 
  void *zBuf,            /* Put the results here */
69082
 
  int iAmt,              /* Number of bytes to read */
69083
 
  sqlite_int64 iOfst     /* Begin reading at this offset */
69084
 
){
69085
 
  MemJournal *p = (MemJournal *)pJfd;
69086
 
  u8 *zOut = zBuf;
69087
 
  int nRead = iAmt;
69088
 
  int iChunkOffset;
69089
 
  FileChunk *pChunk;
69090
 
 
69091
 
  /* SQLite never tries to read past the end of a rollback journal file */
69092
 
  assert( iOfst+iAmt<=p->endpoint.iOffset );
69093
 
 
69094
 
  if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
69095
 
    sqlite3_int64 iOff = 0;
69096
 
    for(pChunk=p->pFirst; 
69097
 
        ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
69098
 
        pChunk=pChunk->pNext
69099
 
    ){
69100
 
      iOff += JOURNAL_CHUNKSIZE;
69101
 
    }
69102
 
  }else{
69103
 
    pChunk = p->readpoint.pChunk;
69104
 
  }
69105
 
 
69106
 
  iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
69107
 
  do {
69108
 
    int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
69109
 
    int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
69110
 
    memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
69111
 
    zOut += nCopy;
69112
 
    nRead -= iSpace;
69113
 
    iChunkOffset = 0;
69114
 
  } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
69115
 
  p->readpoint.iOffset = iOfst+iAmt;
69116
 
  p->readpoint.pChunk = pChunk;
69117
 
 
69118
 
  return SQLITE_OK;
69119
 
}
69120
 
 
69121
 
/*
69122
 
** Write data to the file.
69123
 
*/
69124
 
static int memjrnlWrite(
69125
 
  sqlite3_file *pJfd,    /* The journal file into which to write */
69126
 
  const void *zBuf,      /* Take data to be written from here */
69127
 
  int iAmt,              /* Number of bytes to write */
69128
 
  sqlite_int64 iOfst     /* Begin writing at this offset into the file */
69129
 
){
69130
 
  MemJournal *p = (MemJournal *)pJfd;
69131
 
  int nWrite = iAmt;
69132
 
  u8 *zWrite = (u8 *)zBuf;
69133
 
 
69134
 
  /* An in-memory journal file should only ever be appended to. Random
69135
 
  ** access writes are not required by sqlite.
69136
 
  */
69137
 
  assert( iOfst==p->endpoint.iOffset );
69138
 
  UNUSED_PARAMETER(iOfst);
69139
 
 
69140
 
  while( nWrite>0 ){
69141
 
    FileChunk *pChunk = p->endpoint.pChunk;
69142
 
    int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
69143
 
    int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
69144
 
 
69145
 
    if( iChunkOffset==0 ){
69146
 
      /* New chunk is required to extend the file. */
69147
 
      FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
69148
 
      if( !pNew ){
69149
 
        return SQLITE_IOERR_NOMEM;
69150
 
      }
69151
 
      pNew->pNext = 0;
69152
 
      if( pChunk ){
69153
 
        assert( p->pFirst );
69154
 
        pChunk->pNext = pNew;
69155
 
      }else{
69156
 
        assert( !p->pFirst );
69157
 
        p->pFirst = pNew;
69158
 
      }
69159
 
      p->endpoint.pChunk = pNew;
69160
 
    }
69161
 
 
69162
 
    memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
69163
 
    zWrite += iSpace;
69164
 
    nWrite -= iSpace;
69165
 
    p->endpoint.iOffset += iSpace;
69166
 
  }
69167
 
 
69168
 
  return SQLITE_OK;
69169
 
}
69170
 
 
69171
 
/*
69172
 
** Truncate the file.
69173
 
*/
69174
 
static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
69175
 
  MemJournal *p = (MemJournal *)pJfd;
69176
 
  FileChunk *pChunk;
69177
 
  assert(size==0);
69178
 
  UNUSED_PARAMETER(size);
69179
 
  pChunk = p->pFirst;
69180
 
  while( pChunk ){
69181
 
    FileChunk *pTmp = pChunk;
69182
 
    pChunk = pChunk->pNext;
69183
 
    sqlite3_free(pTmp);
69184
 
  }
69185
 
  sqlite3MemJournalOpen(pJfd);
69186
 
  return SQLITE_OK;
69187
 
}
69188
 
 
69189
 
/*
69190
 
** Close the file.
69191
 
*/
69192
 
static int memjrnlClose(sqlite3_file *pJfd){
69193
 
  memjrnlTruncate(pJfd, 0);
69194
 
  return SQLITE_OK;
69195
 
}
69196
 
 
69197
 
 
69198
 
/*
69199
 
** Sync the file.
69200
 
**
69201
 
** Syncing an in-memory journal is a no-op.  And, in fact, this routine
69202
 
** is never called in a working implementation.  This implementation
69203
 
** exists purely as a contingency, in case some malfunction in some other
69204
 
** part of SQLite causes Sync to be called by mistake.
69205
 
*/
69206
 
static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
69207
 
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
69208
 
  return SQLITE_OK;
69209
 
}
69210
 
 
69211
 
/*
69212
 
** Query the size of the file in bytes.
69213
 
*/
69214
 
static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
69215
 
  MemJournal *p = (MemJournal *)pJfd;
69216
 
  *pSize = (sqlite_int64) p->endpoint.iOffset;
69217
 
  return SQLITE_OK;
69218
 
}
69219
 
 
69220
 
/*
69221
 
** Table of methods for MemJournal sqlite3_file object.
69222
 
*/
69223
 
static const struct sqlite3_io_methods MemJournalMethods = {
69224
 
  1,                /* iVersion */
69225
 
  memjrnlClose,     /* xClose */
69226
 
  memjrnlRead,      /* xRead */
69227
 
  memjrnlWrite,     /* xWrite */
69228
 
  memjrnlTruncate,  /* xTruncate */
69229
 
  memjrnlSync,      /* xSync */
69230
 
  memjrnlFileSize,  /* xFileSize */
69231
 
  0,                /* xLock */
69232
 
  0,                /* xUnlock */
69233
 
  0,                /* xCheckReservedLock */
69234
 
  0,                /* xFileControl */
69235
 
  0,                /* xSectorSize */
69236
 
  0,                /* xDeviceCharacteristics */
69237
 
  0,                /* xShmMap */
69238
 
  0,                /* xShmLock */
69239
 
  0,                /* xShmBarrier */
69240
 
  0                 /* xShmUnlock */
69241
 
};
69242
 
 
69243
 
/* 
69244
 
** Open a journal file.
69245
 
*/
69246
 
SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
69247
 
  MemJournal *p = (MemJournal *)pJfd;
69248
 
  assert( EIGHT_BYTE_ALIGNMENT(p) );
69249
 
  memset(p, 0, sqlite3MemJournalSize());
69250
 
  p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
69251
 
}
69252
 
 
69253
 
/*
69254
 
** Return true if the file-handle passed as an argument is 
69255
 
** an in-memory journal 
69256
 
*/
69257
 
SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){
69258
 
  return pJfd->pMethods==&MemJournalMethods;
69259
 
}
69260
 
 
69261
 
/* 
69262
 
** Return the number of bytes required to store a MemJournal file descriptor.
69263
 
*/
69264
 
SQLITE_PRIVATE int sqlite3MemJournalSize(void){
69265
 
  return sizeof(MemJournal);
69266
 
}
69267
 
 
69268
 
/************** End of memjournal.c ******************************************/
69269
 
/************** Begin file walker.c ******************************************/
69270
 
/*
69271
 
** 2008 August 16
69272
 
**
69273
 
** The author disclaims copyright to this source code.  In place of
69274
 
** a legal notice, here is a blessing:
69275
 
**
69276
 
**    May you do good and not evil.
69277
 
**    May you find forgiveness for yourself and forgive others.
69278
 
**    May you share freely, never taking more than you give.
69279
 
**
69280
 
*************************************************************************
69281
 
** This file contains routines used for walking the parser tree for
69282
 
** an SQL statement.
69283
 
*/
69284
 
 
69285
 
 
69286
 
/*
69287
 
** Walk an expression tree.  Invoke the callback once for each node
69288
 
** of the expression, while decending.  (In other words, the callback
69289
 
** is invoked before visiting children.)
69290
 
**
69291
 
** The return value from the callback should be one of the WRC_*
69292
 
** constants to specify how to proceed with the walk.
69293
 
**
69294
 
**    WRC_Continue      Continue descending down the tree.
69295
 
**
69296
 
**    WRC_Prune         Do not descend into child nodes.  But allow
69297
 
**                      the walk to continue with sibling nodes.
69298
 
**
69299
 
**    WRC_Abort         Do no more callbacks.  Unwind the stack and
69300
 
**                      return the top-level walk call.
69301
 
**
69302
 
** The return value from this routine is WRC_Abort to abandon the tree walk
69303
 
** and WRC_Continue to continue.
69304
 
*/
69305
 
SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
69306
 
  int rc;
69307
 
  if( pExpr==0 ) return WRC_Continue;
69308
 
  testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
69309
 
  testcase( ExprHasProperty(pExpr, EP_Reduced) );
69310
 
  rc = pWalker->xExprCallback(pWalker, pExpr);
69311
 
  if( rc==WRC_Continue
69312
 
              && !ExprHasAnyProperty(pExpr,EP_TokenOnly) ){
69313
 
    if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
69314
 
    if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
69315
 
    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
69316
 
      if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
69317
 
    }else{
69318
 
      if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
69319
 
    }
69320
 
  }
69321
 
  return rc & WRC_Abort;
69322
 
}
69323
 
 
69324
 
/*
69325
 
** Call sqlite3WalkExpr() for every expression in list p or until
69326
 
** an abort request is seen.
69327
 
*/
69328
 
SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
69329
 
  int i;
69330
 
  struct ExprList_item *pItem;
69331
 
  if( p ){
69332
 
    for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
69333
 
      if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
69334
 
    }
69335
 
  }
69336
 
  return WRC_Continue;
69337
 
}
69338
 
 
69339
 
/*
69340
 
** Walk all expressions associated with SELECT statement p.  Do
69341
 
** not invoke the SELECT callback on p, but do (of course) invoke
69342
 
** any expr callbacks and SELECT callbacks that come from subqueries.
69343
 
** Return WRC_Abort or WRC_Continue.
69344
 
*/
69345
 
SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
69346
 
  if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
69347
 
  if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
69348
 
  if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
69349
 
  if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
69350
 
  if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
69351
 
  if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
69352
 
  if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
69353
 
  return WRC_Continue;
69354
 
}
69355
 
 
69356
 
/*
69357
 
** Walk the parse trees associated with all subqueries in the
69358
 
** FROM clause of SELECT statement p.  Do not invoke the select
69359
 
** callback on p, but do invoke it on each FROM clause subquery
69360
 
** and on any subqueries further down in the tree.  Return 
69361
 
** WRC_Abort or WRC_Continue;
69362
 
*/
69363
 
SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
69364
 
  SrcList *pSrc;
69365
 
  int i;
69366
 
  struct SrcList_item *pItem;
69367
 
 
69368
 
  pSrc = p->pSrc;
69369
 
  if( ALWAYS(pSrc) ){
69370
 
    for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
69371
 
      if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
69372
 
        return WRC_Abort;
69373
 
      }
69374
 
    }
69375
 
  }
69376
 
  return WRC_Continue;
69377
 
69378
 
 
69379
 
/*
69380
 
** Call sqlite3WalkExpr() for every expression in Select statement p.
69381
 
** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
69382
 
** on the compound select chain, p->pPrior.
69383
 
**
69384
 
** Return WRC_Continue under normal conditions.  Return WRC_Abort if
69385
 
** there is an abort request.
69386
 
**
69387
 
** If the Walker does not have an xSelectCallback() then this routine
69388
 
** is a no-op returning WRC_Continue.
69389
 
*/
69390
 
SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
69391
 
  int rc;
69392
 
  if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
69393
 
  rc = WRC_Continue;
69394
 
  while( p  ){
69395
 
    rc = pWalker->xSelectCallback(pWalker, p);
69396
 
    if( rc ) break;
69397
 
    if( sqlite3WalkSelectExpr(pWalker, p) ) return WRC_Abort;
69398
 
    if( sqlite3WalkSelectFrom(pWalker, p) ) return WRC_Abort;
69399
 
    p = p->pPrior;
69400
 
  }
69401
 
  return rc & WRC_Abort;
69402
 
}
69403
 
 
69404
 
/************** End of walker.c **********************************************/
69405
 
/************** Begin file resolve.c *****************************************/
69406
 
/*
69407
 
** 2008 August 18
69408
 
**
69409
 
** The author disclaims copyright to this source code.  In place of
69410
 
** a legal notice, here is a blessing:
69411
 
**
69412
 
**    May you do good and not evil.
69413
 
**    May you find forgiveness for yourself and forgive others.
69414
 
**    May you share freely, never taking more than you give.
69415
 
**
69416
 
*************************************************************************
69417
 
**
69418
 
** This file contains routines used for walking the parser tree and
69419
 
** resolve all identifiers by associating them with a particular
69420
 
** table and column.
69421
 
*/
69422
 
 
69423
 
/*
69424
 
** Turn the pExpr expression into an alias for the iCol-th column of the
69425
 
** result set in pEList.
69426
 
**
69427
 
** If the result set column is a simple column reference, then this routine
69428
 
** makes an exact copy.  But for any other kind of expression, this
69429
 
** routine make a copy of the result set column as the argument to the
69430
 
** TK_AS operator.  The TK_AS operator causes the expression to be
69431
 
** evaluated just once and then reused for each alias.
69432
 
**
69433
 
** The reason for suppressing the TK_AS term when the expression is a simple
69434
 
** column reference is so that the column reference will be recognized as
69435
 
** usable by indices within the WHERE clause processing logic. 
69436
 
**
69437
 
** Hack:  The TK_AS operator is inhibited if zType[0]=='G'.  This means
69438
 
** that in a GROUP BY clause, the expression is evaluated twice.  Hence:
69439
 
**
69440
 
**     SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
69441
 
**
69442
 
** Is equivalent to:
69443
 
**
69444
 
**     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
69445
 
**
69446
 
** The result of random()%5 in the GROUP BY clause is probably different
69447
 
** from the result in the result-set.  We might fix this someday.  Or
69448
 
** then again, we might not...
69449
 
*/
69450
 
static void resolveAlias(
69451
 
  Parse *pParse,         /* Parsing context */
69452
 
  ExprList *pEList,      /* A result set */
69453
 
  int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
69454
 
  Expr *pExpr,           /* Transform this into an alias to the result set */
69455
 
  const char *zType      /* "GROUP" or "ORDER" or "" */
69456
 
){
69457
 
  Expr *pOrig;           /* The iCol-th column of the result set */
69458
 
  Expr *pDup;            /* Copy of pOrig */
69459
 
  sqlite3 *db;           /* The database connection */
69460
 
 
69461
 
  assert( iCol>=0 && iCol<pEList->nExpr );
69462
 
  pOrig = pEList->a[iCol].pExpr;
69463
 
  assert( pOrig!=0 );
69464
 
  assert( pOrig->flags & EP_Resolved );
69465
 
  db = pParse->db;
69466
 
  if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
69467
 
    pDup = sqlite3ExprDup(db, pOrig, 0);
69468
 
    pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
69469
 
    if( pDup==0 ) return;
69470
 
    if( pEList->a[iCol].iAlias==0 ){
69471
 
      pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
69472
 
    }
69473
 
    pDup->iTable = pEList->a[iCol].iAlias;
69474
 
  }else if( ExprHasProperty(pOrig, EP_IntValue) || pOrig->u.zToken==0 ){
69475
 
    pDup = sqlite3ExprDup(db, pOrig, 0);
69476
 
    if( pDup==0 ) return;
69477
 
  }else{
69478
 
    char *zToken = pOrig->u.zToken;
69479
 
    assert( zToken!=0 );
69480
 
    pOrig->u.zToken = 0;
69481
 
    pDup = sqlite3ExprDup(db, pOrig, 0);
69482
 
    pOrig->u.zToken = zToken;
69483
 
    if( pDup==0 ) return;
69484
 
    assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
69485
 
    pDup->flags2 |= EP2_MallocedToken;
69486
 
    pDup->u.zToken = sqlite3DbStrDup(db, zToken);
69487
 
  }
69488
 
  if( pExpr->flags & EP_ExpCollate ){
69489
 
    pDup->pColl = pExpr->pColl;
69490
 
    pDup->flags |= EP_ExpCollate;
69491
 
  }
69492
 
 
69493
 
  /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
69494
 
  ** prevents ExprDelete() from deleting the Expr structure itself,
69495
 
  ** allowing it to be repopulated by the memcpy() on the following line.
69496
 
  */
69497
 
  ExprSetProperty(pExpr, EP_Static);
69498
 
  sqlite3ExprDelete(db, pExpr);
69499
 
  memcpy(pExpr, pDup, sizeof(*pExpr));
69500
 
  sqlite3DbFree(db, pDup);
69501
 
}
69502
 
 
69503
 
/*
69504
 
** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
69505
 
** that name in the set of source tables in pSrcList and make the pExpr 
69506
 
** expression node refer back to that source column.  The following changes
69507
 
** are made to pExpr:
69508
 
**
69509
 
**    pExpr->iDb           Set the index in db->aDb[] of the database X
69510
 
**                         (even if X is implied).
69511
 
**    pExpr->iTable        Set to the cursor number for the table obtained
69512
 
**                         from pSrcList.
69513
 
**    pExpr->pTab          Points to the Table structure of X.Y (even if
69514
 
**                         X and/or Y are implied.)
69515
 
**    pExpr->iColumn       Set to the column number within the table.
69516
 
**    pExpr->op            Set to TK_COLUMN.
69517
 
**    pExpr->pLeft         Any expression this points to is deleted
69518
 
**    pExpr->pRight        Any expression this points to is deleted.
69519
 
**
69520
 
** The zDb variable is the name of the database (the "X").  This value may be
69521
 
** NULL meaning that name is of the form Y.Z or Z.  Any available database
69522
 
** can be used.  The zTable variable is the name of the table (the "Y").  This
69523
 
** value can be NULL if zDb is also NULL.  If zTable is NULL it
69524
 
** means that the form of the name is Z and that columns from any table
69525
 
** can be used.
69526
 
**
69527
 
** If the name cannot be resolved unambiguously, leave an error message
69528
 
** in pParse and return WRC_Abort.  Return WRC_Prune on success.
69529
 
*/
69530
 
static int lookupName(
69531
 
  Parse *pParse,       /* The parsing context */
69532
 
  const char *zDb,     /* Name of the database containing table, or NULL */
69533
 
  const char *zTab,    /* Name of table containing column, or NULL */
69534
 
  const char *zCol,    /* Name of the column. */
69535
 
  NameContext *pNC,    /* The name context used to resolve the name */
69536
 
  Expr *pExpr          /* Make this EXPR node point to the selected column */
69537
 
){
69538
 
  int i, j;            /* Loop counters */
69539
 
  int cnt = 0;                      /* Number of matching column names */
69540
 
  int cntTab = 0;                   /* Number of matching table names */
69541
 
  sqlite3 *db = pParse->db;         /* The database connection */
69542
 
  struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
69543
 
  struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
69544
 
  NameContext *pTopNC = pNC;        /* First namecontext in the list */
69545
 
  Schema *pSchema = 0;              /* Schema of the expression */
69546
 
  int isTrigger = 0;
69547
 
 
69548
 
  assert( pNC );     /* the name context cannot be NULL. */
69549
 
  assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
69550
 
  assert( ~ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
69551
 
 
69552
 
  /* Initialize the node to no-match */
69553
 
  pExpr->iTable = -1;
69554
 
  pExpr->pTab = 0;
69555
 
  ExprSetIrreducible(pExpr);
69556
 
 
69557
 
  /* Start at the inner-most context and move outward until a match is found */
69558
 
  while( pNC && cnt==0 ){
69559
 
    ExprList *pEList;
69560
 
    SrcList *pSrcList = pNC->pSrcList;
69561
 
 
69562
 
    if( pSrcList ){
69563
 
      for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
69564
 
        Table *pTab;
69565
 
        int iDb;
69566
 
        Column *pCol;
69567
 
  
69568
 
        pTab = pItem->pTab;
69569
 
        assert( pTab!=0 && pTab->zName!=0 );
69570
 
        iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
69571
 
        assert( pTab->nCol>0 );
69572
 
        if( zTab ){
69573
 
          if( pItem->zAlias ){
69574
 
            char *zTabName = pItem->zAlias;
69575
 
            if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
69576
 
          }else{
69577
 
            char *zTabName = pTab->zName;
69578
 
            if( NEVER(zTabName==0) || sqlite3StrICmp(zTabName, zTab)!=0 ){
69579
 
              continue;
69580
 
            }
69581
 
            if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
69582
 
              continue;
69583
 
            }
69584
 
          }
69585
 
        }
69586
 
        if( 0==(cntTab++) ){
69587
 
          pExpr->iTable = pItem->iCursor;
69588
 
          pExpr->pTab = pTab;
69589
 
          pSchema = pTab->pSchema;
69590
 
          pMatch = pItem;
69591
 
        }
69592
 
        for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
69593
 
          if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
69594
 
            IdList *pUsing;
69595
 
            cnt++;
69596
 
            pExpr->iTable = pItem->iCursor;
69597
 
            pExpr->pTab = pTab;
69598
 
            pMatch = pItem;
69599
 
            pSchema = pTab->pSchema;
69600
 
            /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
69601
 
            pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
69602
 
            if( i<pSrcList->nSrc-1 ){
69603
 
              if( pItem[1].jointype & JT_NATURAL ){
69604
 
                /* If this match occurred in the left table of a natural join,
69605
 
                ** then skip the right table to avoid a duplicate match */
69606
 
                pItem++;
69607
 
                i++;
69608
 
              }else if( (pUsing = pItem[1].pUsing)!=0 ){
69609
 
                /* If this match occurs on a column that is in the USING clause
69610
 
                ** of a join, skip the search of the right table of the join
69611
 
                ** to avoid a duplicate match there. */
69612
 
                int k;
69613
 
                for(k=0; k<pUsing->nId; k++){
69614
 
                  if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
69615
 
                    pItem++;
69616
 
                    i++;
69617
 
                    break;
69618
 
                  }
69619
 
                }
69620
 
              }
69621
 
            }
69622
 
            break;
69623
 
          }
69624
 
        }
69625
 
      }
69626
 
    }
69627
 
 
69628
 
#ifndef SQLITE_OMIT_TRIGGER
69629
 
    /* If we have not already resolved the name, then maybe 
69630
 
    ** it is a new.* or old.* trigger argument reference
69631
 
    */
69632
 
    if( zDb==0 && zTab!=0 && cnt==0 && pParse->pTriggerTab!=0 ){
69633
 
      int op = pParse->eTriggerOp;
69634
 
      Table *pTab = 0;
69635
 
      assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
69636
 
      if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
69637
 
        pExpr->iTable = 1;
69638
 
        pTab = pParse->pTriggerTab;
69639
 
      }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
69640
 
        pExpr->iTable = 0;
69641
 
        pTab = pParse->pTriggerTab;
69642
 
      }
69643
 
 
69644
 
      if( pTab ){ 
69645
 
        int iCol;
69646
 
        pSchema = pTab->pSchema;
69647
 
        cntTab++;
69648
 
        for(iCol=0; iCol<pTab->nCol; iCol++){
69649
 
          Column *pCol = &pTab->aCol[iCol];
69650
 
          if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
69651
 
            if( iCol==pTab->iPKey ){
69652
 
              iCol = -1;
69653
 
            }
69654
 
            break;
69655
 
          }
69656
 
        }
69657
 
        if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) ){
69658
 
          iCol = -1;        /* IMP: R-44911-55124 */
69659
 
        }
69660
 
        if( iCol<pTab->nCol ){
69661
 
          cnt++;
69662
 
          if( iCol<0 ){
69663
 
            pExpr->affinity = SQLITE_AFF_INTEGER;
69664
 
          }else if( pExpr->iTable==0 ){
69665
 
            testcase( iCol==31 );
69666
 
            testcase( iCol==32 );
69667
 
            pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
69668
 
          }else{
69669
 
            testcase( iCol==31 );
69670
 
            testcase( iCol==32 );
69671
 
            pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
69672
 
          }
69673
 
          pExpr->iColumn = (i16)iCol;
69674
 
          pExpr->pTab = pTab;
69675
 
          isTrigger = 1;
69676
 
        }
69677
 
      }
69678
 
    }
69679
 
#endif /* !defined(SQLITE_OMIT_TRIGGER) */
69680
 
 
69681
 
    /*
69682
 
    ** Perhaps the name is a reference to the ROWID
69683
 
    */
69684
 
    if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
69685
 
      cnt = 1;
69686
 
      pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
69687
 
      pExpr->affinity = SQLITE_AFF_INTEGER;
69688
 
    }
69689
 
 
69690
 
    /*
69691
 
    ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
69692
 
    ** might refer to an result-set alias.  This happens, for example, when
69693
 
    ** we are resolving names in the WHERE clause of the following command:
69694
 
    **
69695
 
    **     SELECT a+b AS x FROM table WHERE x<10;
69696
 
    **
69697
 
    ** In cases like this, replace pExpr with a copy of the expression that
69698
 
    ** forms the result set entry ("a+b" in the example) and return immediately.
69699
 
    ** Note that the expression in the result set should have already been
69700
 
    ** resolved by the time the WHERE clause is resolved.
69701
 
    */
69702
 
    if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
69703
 
      for(j=0; j<pEList->nExpr; j++){
69704
 
        char *zAs = pEList->a[j].zName;
69705
 
        if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
69706
 
          Expr *pOrig;
69707
 
          assert( pExpr->pLeft==0 && pExpr->pRight==0 );
69708
 
          assert( pExpr->x.pList==0 );
69709
 
          assert( pExpr->x.pSelect==0 );
69710
 
          pOrig = pEList->a[j].pExpr;
69711
 
          if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
69712
 
            sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
69713
 
            return WRC_Abort;
69714
 
          }
69715
 
          resolveAlias(pParse, pEList, j, pExpr, "");
69716
 
          cnt = 1;
69717
 
          pMatch = 0;
69718
 
          assert( zTab==0 && zDb==0 );
69719
 
          goto lookupname_end;
69720
 
        }
69721
 
      } 
69722
 
    }
69723
 
 
69724
 
    /* Advance to the next name context.  The loop will exit when either
69725
 
    ** we have a match (cnt>0) or when we run out of name contexts.
69726
 
    */
69727
 
    if( cnt==0 ){
69728
 
      pNC = pNC->pNext;
69729
 
    }
69730
 
  }
69731
 
 
69732
 
  /*
69733
 
  ** If X and Y are NULL (in other words if only the column name Z is
69734
 
  ** supplied) and the value of Z is enclosed in double-quotes, then
69735
 
  ** Z is a string literal if it doesn't match any column names.  In that
69736
 
  ** case, we need to return right away and not make any changes to
69737
 
  ** pExpr.
69738
 
  **
69739
 
  ** Because no reference was made to outer contexts, the pNC->nRef
69740
 
  ** fields are not changed in any context.
69741
 
  */
69742
 
  if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
69743
 
    pExpr->op = TK_STRING;
69744
 
    pExpr->pTab = 0;
69745
 
    return WRC_Prune;
69746
 
  }
69747
 
 
69748
 
  /*
69749
 
  ** cnt==0 means there was not match.  cnt>1 means there were two or
69750
 
  ** more matches.  Either way, we have an error.
69751
 
  */
69752
 
  if( cnt!=1 ){
69753
 
    const char *zErr;
69754
 
    zErr = cnt==0 ? "no such column" : "ambiguous column name";
69755
 
    if( zDb ){
69756
 
      sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
69757
 
    }else if( zTab ){
69758
 
      sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
69759
 
    }else{
69760
 
      sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
69761
 
    }
69762
 
    pParse->checkSchema = 1;
69763
 
    pTopNC->nErr++;
69764
 
  }
69765
 
 
69766
 
  /* If a column from a table in pSrcList is referenced, then record
69767
 
  ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
69768
 
  ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
69769
 
  ** column number is greater than the number of bits in the bitmask
69770
 
  ** then set the high-order bit of the bitmask.
69771
 
  */
69772
 
  if( pExpr->iColumn>=0 && pMatch!=0 ){
69773
 
    int n = pExpr->iColumn;
69774
 
    testcase( n==BMS-1 );
69775
 
    if( n>=BMS ){
69776
 
      n = BMS-1;
69777
 
    }
69778
 
    assert( pMatch->iCursor==pExpr->iTable );
69779
 
    pMatch->colUsed |= ((Bitmask)1)<<n;
69780
 
  }
69781
 
 
69782
 
  /* Clean up and return
69783
 
  */
69784
 
  sqlite3ExprDelete(db, pExpr->pLeft);
69785
 
  pExpr->pLeft = 0;
69786
 
  sqlite3ExprDelete(db, pExpr->pRight);
69787
 
  pExpr->pRight = 0;
69788
 
  pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
69789
 
lookupname_end:
69790
 
  if( cnt==1 ){
69791
 
    assert( pNC!=0 );
69792
 
    sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
69793
 
    /* Increment the nRef value on all name contexts from TopNC up to
69794
 
    ** the point where the name matched. */
69795
 
    for(;;){
69796
 
      assert( pTopNC!=0 );
69797
 
      pTopNC->nRef++;
69798
 
      if( pTopNC==pNC ) break;
69799
 
      pTopNC = pTopNC->pNext;
69800
 
    }
69801
 
    return WRC_Prune;
69802
 
  } else {
69803
 
    return WRC_Abort;
69804
 
  }
69805
 
}
69806
 
 
69807
 
/*
69808
 
** Allocate and return a pointer to an expression to load the column iCol
69809
 
** from datasource iSrc in SrcList pSrc.
69810
 
*/
69811
 
SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
69812
 
  Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
69813
 
  if( p ){
69814
 
    struct SrcList_item *pItem = &pSrc->a[iSrc];
69815
 
    p->pTab = pItem->pTab;
69816
 
    p->iTable = pItem->iCursor;
69817
 
    if( p->pTab->iPKey==iCol ){
69818
 
      p->iColumn = -1;
69819
 
    }else{
69820
 
      p->iColumn = (ynVar)iCol;
69821
 
      testcase( iCol==BMS );
69822
 
      testcase( iCol==BMS-1 );
69823
 
      pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
69824
 
    }
69825
 
    ExprSetProperty(p, EP_Resolved);
69826
 
  }
69827
 
  return p;
69828
 
}
69829
 
 
69830
 
/*
69831
 
** This routine is callback for sqlite3WalkExpr().
69832
 
**
69833
 
** Resolve symbolic names into TK_COLUMN operators for the current
69834
 
** node in the expression tree.  Return 0 to continue the search down
69835
 
** the tree or 2 to abort the tree walk.
69836
 
**
69837
 
** This routine also does error checking and name resolution for
69838
 
** function names.  The operator for aggregate functions is changed
69839
 
** to TK_AGG_FUNCTION.
69840
 
*/
69841
 
static int resolveExprStep(Walker *pWalker, Expr *pExpr){
69842
 
  NameContext *pNC;
69843
 
  Parse *pParse;
69844
 
 
69845
 
  pNC = pWalker->u.pNC;
69846
 
  assert( pNC!=0 );
69847
 
  pParse = pNC->pParse;
69848
 
  assert( pParse==pWalker->pParse );
69849
 
 
69850
 
  if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune;
69851
 
  ExprSetProperty(pExpr, EP_Resolved);
69852
 
#ifndef NDEBUG
69853
 
  if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
69854
 
    SrcList *pSrcList = pNC->pSrcList;
69855
 
    int i;
69856
 
    for(i=0; i<pNC->pSrcList->nSrc; i++){
69857
 
      assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
69858
 
    }
69859
 
  }
69860
 
#endif
69861
 
  switch( pExpr->op ){
69862
 
 
69863
 
#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
69864
 
    /* The special operator TK_ROW means use the rowid for the first
69865
 
    ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
69866
 
    ** clause processing on UPDATE and DELETE statements.
69867
 
    */
69868
 
    case TK_ROW: {
69869
 
      SrcList *pSrcList = pNC->pSrcList;
69870
 
      struct SrcList_item *pItem;
69871
 
      assert( pSrcList && pSrcList->nSrc==1 );
69872
 
      pItem = pSrcList->a; 
69873
 
      pExpr->op = TK_COLUMN;
69874
 
      pExpr->pTab = pItem->pTab;
69875
 
      pExpr->iTable = pItem->iCursor;
69876
 
      pExpr->iColumn = -1;
69877
 
      pExpr->affinity = SQLITE_AFF_INTEGER;
69878
 
      break;
69879
 
    }
69880
 
#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
69881
 
 
69882
 
    /* A lone identifier is the name of a column.
69883
 
    */
69884
 
    case TK_ID: {
69885
 
      return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
69886
 
    }
69887
 
  
69888
 
    /* A table name and column name:     ID.ID
69889
 
    ** Or a database, table and column:  ID.ID.ID
69890
 
    */
69891
 
    case TK_DOT: {
69892
 
      const char *zColumn;
69893
 
      const char *zTable;
69894
 
      const char *zDb;
69895
 
      Expr *pRight;
69896
 
 
69897
 
      /* if( pSrcList==0 ) break; */
69898
 
      pRight = pExpr->pRight;
69899
 
      if( pRight->op==TK_ID ){
69900
 
        zDb = 0;
69901
 
        zTable = pExpr->pLeft->u.zToken;
69902
 
        zColumn = pRight->u.zToken;
69903
 
      }else{
69904
 
        assert( pRight->op==TK_DOT );
69905
 
        zDb = pExpr->pLeft->u.zToken;
69906
 
        zTable = pRight->pLeft->u.zToken;
69907
 
        zColumn = pRight->pRight->u.zToken;
69908
 
      }
69909
 
      return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
69910
 
    }
69911
 
 
69912
 
    /* Resolve function names
69913
 
    */
69914
 
    case TK_CONST_FUNC:
69915
 
    case TK_FUNCTION: {
69916
 
      ExprList *pList = pExpr->x.pList;    /* The argument list */
69917
 
      int n = pList ? pList->nExpr : 0;    /* Number of arguments */
69918
 
      int no_such_func = 0;       /* True if no such function exists */
69919
 
      int wrong_num_args = 0;     /* True if wrong number of arguments */
69920
 
      int is_agg = 0;             /* True if is an aggregate function */
69921
 
      int auth;                   /* Authorization to use the function */
69922
 
      int nId;                    /* Number of characters in function name */
69923
 
      const char *zId;            /* The function name. */
69924
 
      FuncDef *pDef;              /* Information about the function */
69925
 
      u8 enc = ENC(pParse->db);   /* The database encoding */
69926
 
 
69927
 
      testcase( pExpr->op==TK_CONST_FUNC );
69928
 
      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
69929
 
      zId = pExpr->u.zToken;
69930
 
      nId = sqlite3Strlen30(zId);
69931
 
      pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
69932
 
      if( pDef==0 ){
69933
 
        pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
69934
 
        if( pDef==0 ){
69935
 
          no_such_func = 1;
69936
 
        }else{
69937
 
          wrong_num_args = 1;
69938
 
        }
69939
 
      }else{
69940
 
        is_agg = pDef->xFunc==0;
69941
 
      }
69942
 
#ifndef SQLITE_OMIT_AUTHORIZATION
69943
 
      if( pDef ){
69944
 
        auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
69945
 
        if( auth!=SQLITE_OK ){
69946
 
          if( auth==SQLITE_DENY ){
69947
 
            sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
69948
 
                                    pDef->zName);
69949
 
            pNC->nErr++;
69950
 
          }
69951
 
          pExpr->op = TK_NULL;
69952
 
          return WRC_Prune;
69953
 
        }
69954
 
      }
69955
 
#endif
69956
 
      if( is_agg && !pNC->allowAgg ){
69957
 
        sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
69958
 
        pNC->nErr++;
69959
 
        is_agg = 0;
69960
 
      }else if( no_such_func ){
69961
 
        sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
69962
 
        pNC->nErr++;
69963
 
      }else if( wrong_num_args ){
69964
 
        sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
69965
 
             nId, zId);
69966
 
        pNC->nErr++;
69967
 
      }
69968
 
      if( is_agg ){
69969
 
        pExpr->op = TK_AGG_FUNCTION;
69970
 
        pNC->hasAgg = 1;
69971
 
      }
69972
 
      if( is_agg ) pNC->allowAgg = 0;
69973
 
      sqlite3WalkExprList(pWalker, pList);
69974
 
      if( is_agg ) pNC->allowAgg = 1;
69975
 
      /* FIX ME:  Compute pExpr->affinity based on the expected return
69976
 
      ** type of the function 
69977
 
      */
69978
 
      return WRC_Prune;
69979
 
    }
69980
 
#ifndef SQLITE_OMIT_SUBQUERY
69981
 
    case TK_SELECT:
69982
 
    case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
69983
 
#endif
69984
 
    case TK_IN: {
69985
 
      testcase( pExpr->op==TK_IN );
69986
 
      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
69987
 
        int nRef = pNC->nRef;
69988
 
#ifndef SQLITE_OMIT_CHECK
69989
 
        if( pNC->isCheck ){
69990
 
          sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
69991
 
        }
69992
 
#endif
69993
 
        sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
69994
 
        assert( pNC->nRef>=nRef );
69995
 
        if( nRef!=pNC->nRef ){
69996
 
          ExprSetProperty(pExpr, EP_VarSelect);
69997
 
        }
69998
 
      }
69999
 
      break;
70000
 
    }
70001
 
#ifndef SQLITE_OMIT_CHECK
70002
 
    case TK_VARIABLE: {
70003
 
      if( pNC->isCheck ){
70004
 
        sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
70005
 
      }
70006
 
      break;
70007
 
    }
70008
 
#endif
70009
 
  }
70010
 
  return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
70011
 
}
70012
 
 
70013
 
/*
70014
 
** pEList is a list of expressions which are really the result set of the
70015
 
** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
70016
 
** This routine checks to see if pE is a simple identifier which corresponds
70017
 
** to the AS-name of one of the terms of the expression list.  If it is,
70018
 
** this routine return an integer between 1 and N where N is the number of
70019
 
** elements in pEList, corresponding to the matching entry.  If there is
70020
 
** no match, or if pE is not a simple identifier, then this routine
70021
 
** return 0.
70022
 
**
70023
 
** pEList has been resolved.  pE has not.
70024
 
*/
70025
 
static int resolveAsName(
70026
 
  Parse *pParse,     /* Parsing context for error messages */
70027
 
  ExprList *pEList,  /* List of expressions to scan */
70028
 
  Expr *pE           /* Expression we are trying to match */
70029
 
){
70030
 
  int i;             /* Loop counter */
70031
 
 
70032
 
  UNUSED_PARAMETER(pParse);
70033
 
 
70034
 
  if( pE->op==TK_ID ){
70035
 
    char *zCol = pE->u.zToken;
70036
 
    for(i=0; i<pEList->nExpr; i++){
70037
 
      char *zAs = pEList->a[i].zName;
70038
 
      if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
70039
 
        return i+1;
70040
 
      }
70041
 
    }
70042
 
  }
70043
 
  return 0;
70044
 
}
70045
 
 
70046
 
/*
70047
 
** pE is a pointer to an expression which is a single term in the
70048
 
** ORDER BY of a compound SELECT.  The expression has not been
70049
 
** name resolved.
70050
 
**
70051
 
** At the point this routine is called, we already know that the
70052
 
** ORDER BY term is not an integer index into the result set.  That
70053
 
** case is handled by the calling routine.
70054
 
**
70055
 
** Attempt to match pE against result set columns in the left-most
70056
 
** SELECT statement.  Return the index i of the matching column,
70057
 
** as an indication to the caller that it should sort by the i-th column.
70058
 
** The left-most column is 1.  In other words, the value returned is the
70059
 
** same integer value that would be used in the SQL statement to indicate
70060
 
** the column.
70061
 
**
70062
 
** If there is no match, return 0.  Return -1 if an error occurs.
70063
 
*/
70064
 
static int resolveOrderByTermToExprList(
70065
 
  Parse *pParse,     /* Parsing context for error messages */
70066
 
  Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
70067
 
  Expr *pE           /* The specific ORDER BY term */
70068
 
){
70069
 
  int i;             /* Loop counter */
70070
 
  ExprList *pEList;  /* The columns of the result set */
70071
 
  NameContext nc;    /* Name context for resolving pE */
70072
 
  sqlite3 *db;       /* Database connection */
70073
 
  int rc;            /* Return code from subprocedures */
70074
 
  u8 savedSuppErr;   /* Saved value of db->suppressErr */
70075
 
 
70076
 
  assert( sqlite3ExprIsInteger(pE, &i)==0 );
70077
 
  pEList = pSelect->pEList;
70078
 
 
70079
 
  /* Resolve all names in the ORDER BY term expression
70080
 
  */
70081
 
  memset(&nc, 0, sizeof(nc));
70082
 
  nc.pParse = pParse;
70083
 
  nc.pSrcList = pSelect->pSrc;
70084
 
  nc.pEList = pEList;
70085
 
  nc.allowAgg = 1;
70086
 
  nc.nErr = 0;
70087
 
  db = pParse->db;
70088
 
  savedSuppErr = db->suppressErr;
70089
 
  db->suppressErr = 1;
70090
 
  rc = sqlite3ResolveExprNames(&nc, pE);
70091
 
  db->suppressErr = savedSuppErr;
70092
 
  if( rc ) return 0;
70093
 
 
70094
 
  /* Try to match the ORDER BY expression against an expression
70095
 
  ** in the result set.  Return an 1-based index of the matching
70096
 
  ** result-set entry.
70097
 
  */
70098
 
  for(i=0; i<pEList->nExpr; i++){
70099
 
    if( sqlite3ExprCompare(pEList->a[i].pExpr, pE)<2 ){
70100
 
      return i+1;
70101
 
    }
70102
 
  }
70103
 
 
70104
 
  /* If no match, return 0. */
70105
 
  return 0;
70106
 
}
70107
 
 
70108
 
/*
70109
 
** Generate an ORDER BY or GROUP BY term out-of-range error.
70110
 
*/
70111
 
static void resolveOutOfRangeError(
70112
 
  Parse *pParse,         /* The error context into which to write the error */
70113
 
  const char *zType,     /* "ORDER" or "GROUP" */
70114
 
  int i,                 /* The index (1-based) of the term out of range */
70115
 
  int mx                 /* Largest permissible value of i */
70116
 
){
70117
 
  sqlite3ErrorMsg(pParse, 
70118
 
    "%r %s BY term out of range - should be "
70119
 
    "between 1 and %d", i, zType, mx);
70120
 
}
70121
 
 
70122
 
/*
70123
 
** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
70124
 
** each term of the ORDER BY clause is a constant integer between 1
70125
 
** and N where N is the number of columns in the compound SELECT.
70126
 
**
70127
 
** ORDER BY terms that are already an integer between 1 and N are
70128
 
** unmodified.  ORDER BY terms that are integers outside the range of
70129
 
** 1 through N generate an error.  ORDER BY terms that are expressions
70130
 
** are matched against result set expressions of compound SELECT
70131
 
** beginning with the left-most SELECT and working toward the right.
70132
 
** At the first match, the ORDER BY expression is transformed into
70133
 
** the integer column number.
70134
 
**
70135
 
** Return the number of errors seen.
70136
 
*/
70137
 
static int resolveCompoundOrderBy(
70138
 
  Parse *pParse,        /* Parsing context.  Leave error messages here */
70139
 
  Select *pSelect       /* The SELECT statement containing the ORDER BY */
70140
 
){
70141
 
  int i;
70142
 
  ExprList *pOrderBy;
70143
 
  ExprList *pEList;
70144
 
  sqlite3 *db;
70145
 
  int moreToDo = 1;
70146
 
 
70147
 
  pOrderBy = pSelect->pOrderBy;
70148
 
  if( pOrderBy==0 ) return 0;
70149
 
  db = pParse->db;
70150
 
#if SQLITE_MAX_COLUMN
70151
 
  if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
70152
 
    sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
70153
 
    return 1;
70154
 
  }
70155
 
#endif
70156
 
  for(i=0; i<pOrderBy->nExpr; i++){
70157
 
    pOrderBy->a[i].done = 0;
70158
 
  }
70159
 
  pSelect->pNext = 0;
70160
 
  while( pSelect->pPrior ){
70161
 
    pSelect->pPrior->pNext = pSelect;
70162
 
    pSelect = pSelect->pPrior;
70163
 
  }
70164
 
  while( pSelect && moreToDo ){
70165
 
    struct ExprList_item *pItem;
70166
 
    moreToDo = 0;
70167
 
    pEList = pSelect->pEList;
70168
 
    assert( pEList!=0 );
70169
 
    for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
70170
 
      int iCol = -1;
70171
 
      Expr *pE, *pDup;
70172
 
      if( pItem->done ) continue;
70173
 
      pE = pItem->pExpr;
70174
 
      if( sqlite3ExprIsInteger(pE, &iCol) ){
70175
 
        if( iCol<=0 || iCol>pEList->nExpr ){
70176
 
          resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
70177
 
          return 1;
70178
 
        }
70179
 
      }else{
70180
 
        iCol = resolveAsName(pParse, pEList, pE);
70181
 
        if( iCol==0 ){
70182
 
          pDup = sqlite3ExprDup(db, pE, 0);
70183
 
          if( !db->mallocFailed ){
70184
 
            assert(pDup);
70185
 
            iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
70186
 
          }
70187
 
          sqlite3ExprDelete(db, pDup);
70188
 
        }
70189
 
      }
70190
 
      if( iCol>0 ){
70191
 
        CollSeq *pColl = pE->pColl;
70192
 
        int flags = pE->flags & EP_ExpCollate;
70193
 
        sqlite3ExprDelete(db, pE);
70194
 
        pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0);
70195
 
        if( pE==0 ) return 1;
70196
 
        pE->pColl = pColl;
70197
 
        pE->flags |= EP_IntValue | flags;
70198
 
        pE->u.iValue = iCol;
70199
 
        pItem->iCol = (u16)iCol;
70200
 
        pItem->done = 1;
70201
 
      }else{
70202
 
        moreToDo = 1;
70203
 
      }
70204
 
    }
70205
 
    pSelect = pSelect->pNext;
70206
 
  }
70207
 
  for(i=0; i<pOrderBy->nExpr; i++){
70208
 
    if( pOrderBy->a[i].done==0 ){
70209
 
      sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
70210
 
            "column in the result set", i+1);
70211
 
      return 1;
70212
 
    }
70213
 
  }
70214
 
  return 0;
70215
 
}
70216
 
 
70217
 
/*
70218
 
** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
70219
 
** the SELECT statement pSelect.  If any term is reference to a
70220
 
** result set expression (as determined by the ExprList.a.iCol field)
70221
 
** then convert that term into a copy of the corresponding result set
70222
 
** column.
70223
 
**
70224
 
** If any errors are detected, add an error message to pParse and
70225
 
** return non-zero.  Return zero if no errors are seen.
70226
 
*/
70227
 
SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
70228
 
  Parse *pParse,        /* Parsing context.  Leave error messages here */
70229
 
  Select *pSelect,      /* The SELECT statement containing the clause */
70230
 
  ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
70231
 
  const char *zType     /* "ORDER" or "GROUP" */
70232
 
){
70233
 
  int i;
70234
 
  sqlite3 *db = pParse->db;
70235
 
  ExprList *pEList;
70236
 
  struct ExprList_item *pItem;
70237
 
 
70238
 
  if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
70239
 
#if SQLITE_MAX_COLUMN
70240
 
  if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
70241
 
    sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
70242
 
    return 1;
70243
 
  }
70244
 
#endif
70245
 
  pEList = pSelect->pEList;
70246
 
  assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
70247
 
  for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
70248
 
    if( pItem->iCol ){
70249
 
      if( pItem->iCol>pEList->nExpr ){
70250
 
        resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
70251
 
        return 1;
70252
 
      }
70253
 
      resolveAlias(pParse, pEList, pItem->iCol-1, pItem->pExpr, zType);
70254
 
    }
70255
 
  }
70256
 
  return 0;
70257
 
}
70258
 
 
70259
 
/*
70260
 
** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
70261
 
** The Name context of the SELECT statement is pNC.  zType is either
70262
 
** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
70263
 
**
70264
 
** This routine resolves each term of the clause into an expression.
70265
 
** If the order-by term is an integer I between 1 and N (where N is the
70266
 
** number of columns in the result set of the SELECT) then the expression
70267
 
** in the resolution is a copy of the I-th result-set expression.  If
70268
 
** the order-by term is an identify that corresponds to the AS-name of
70269
 
** a result-set expression, then the term resolves to a copy of the
70270
 
** result-set expression.  Otherwise, the expression is resolved in
70271
 
** the usual way - using sqlite3ResolveExprNames().
70272
 
**
70273
 
** This routine returns the number of errors.  If errors occur, then
70274
 
** an appropriate error message might be left in pParse.  (OOM errors
70275
 
** excepted.)
70276
 
*/
70277
 
static int resolveOrderGroupBy(
70278
 
  NameContext *pNC,     /* The name context of the SELECT statement */
70279
 
  Select *pSelect,      /* The SELECT statement holding pOrderBy */
70280
 
  ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
70281
 
  const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
70282
 
){
70283
 
  int i;                         /* Loop counter */
70284
 
  int iCol;                      /* Column number */
70285
 
  struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
70286
 
  Parse *pParse;                 /* Parsing context */
70287
 
  int nResult;                   /* Number of terms in the result set */
70288
 
 
70289
 
  if( pOrderBy==0 ) return 0;
70290
 
  nResult = pSelect->pEList->nExpr;
70291
 
  pParse = pNC->pParse;
70292
 
  for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
70293
 
    Expr *pE = pItem->pExpr;
70294
 
    iCol = resolveAsName(pParse, pSelect->pEList, pE);
70295
 
    if( iCol>0 ){
70296
 
      /* If an AS-name match is found, mark this ORDER BY column as being
70297
 
      ** a copy of the iCol-th result-set column.  The subsequent call to
70298
 
      ** sqlite3ResolveOrderGroupBy() will convert the expression to a
70299
 
      ** copy of the iCol-th result-set expression. */
70300
 
      pItem->iCol = (u16)iCol;
70301
 
      continue;
70302
 
    }
70303
 
    if( sqlite3ExprIsInteger(pE, &iCol) ){
70304
 
      /* The ORDER BY term is an integer constant.  Again, set the column
70305
 
      ** number so that sqlite3ResolveOrderGroupBy() will convert the
70306
 
      ** order-by term to a copy of the result-set expression */
70307
 
      if( iCol<1 ){
70308
 
        resolveOutOfRangeError(pParse, zType, i+1, nResult);
70309
 
        return 1;
70310
 
      }
70311
 
      pItem->iCol = (u16)iCol;
70312
 
      continue;
70313
 
    }
70314
 
 
70315
 
    /* Otherwise, treat the ORDER BY term as an ordinary expression */
70316
 
    pItem->iCol = 0;
70317
 
    if( sqlite3ResolveExprNames(pNC, pE) ){
70318
 
      return 1;
70319
 
    }
70320
 
  }
70321
 
  return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
70322
 
}
70323
 
 
70324
 
/*
70325
 
** Resolve names in the SELECT statement p and all of its descendents.
70326
 
*/
70327
 
static int resolveSelectStep(Walker *pWalker, Select *p){
70328
 
  NameContext *pOuterNC;  /* Context that contains this SELECT */
70329
 
  NameContext sNC;        /* Name context of this SELECT */
70330
 
  int isCompound;         /* True if p is a compound select */
70331
 
  int nCompound;          /* Number of compound terms processed so far */
70332
 
  Parse *pParse;          /* Parsing context */
70333
 
  ExprList *pEList;       /* Result set expression list */
70334
 
  int i;                  /* Loop counter */
70335
 
  ExprList *pGroupBy;     /* The GROUP BY clause */
70336
 
  Select *pLeftmost;      /* Left-most of SELECT of a compound */
70337
 
  sqlite3 *db;            /* Database connection */
70338
 
  
70339
 
 
70340
 
  assert( p!=0 );
70341
 
  if( p->selFlags & SF_Resolved ){
70342
 
    return WRC_Prune;
70343
 
  }
70344
 
  pOuterNC = pWalker->u.pNC;
70345
 
  pParse = pWalker->pParse;
70346
 
  db = pParse->db;
70347
 
 
70348
 
  /* Normally sqlite3SelectExpand() will be called first and will have
70349
 
  ** already expanded this SELECT.  However, if this is a subquery within
70350
 
  ** an expression, sqlite3ResolveExprNames() will be called without a
70351
 
  ** prior call to sqlite3SelectExpand().  When that happens, let
70352
 
  ** sqlite3SelectPrep() do all of the processing for this SELECT.
70353
 
  ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
70354
 
  ** this routine in the correct order.
70355
 
  */
70356
 
  if( (p->selFlags & SF_Expanded)==0 ){
70357
 
    sqlite3SelectPrep(pParse, p, pOuterNC);
70358
 
    return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
70359
 
  }
70360
 
 
70361
 
  isCompound = p->pPrior!=0;
70362
 
  nCompound = 0;
70363
 
  pLeftmost = p;
70364
 
  while( p ){
70365
 
    assert( (p->selFlags & SF_Expanded)!=0 );
70366
 
    assert( (p->selFlags & SF_Resolved)==0 );
70367
 
    p->selFlags |= SF_Resolved;
70368
 
 
70369
 
    /* Resolve the expressions in the LIMIT and OFFSET clauses. These
70370
 
    ** are not allowed to refer to any names, so pass an empty NameContext.
70371
 
    */
70372
 
    memset(&sNC, 0, sizeof(sNC));
70373
 
    sNC.pParse = pParse;
70374
 
    if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
70375
 
        sqlite3ResolveExprNames(&sNC, p->pOffset) ){
70376
 
      return WRC_Abort;
70377
 
    }
70378
 
  
70379
 
    /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
70380
 
    ** resolve the result-set expression list.
70381
 
    */
70382
 
    sNC.allowAgg = 1;
70383
 
    sNC.pSrcList = p->pSrc;
70384
 
    sNC.pNext = pOuterNC;
70385
 
  
70386
 
    /* Resolve names in the result set. */
70387
 
    pEList = p->pEList;
70388
 
    assert( pEList!=0 );
70389
 
    for(i=0; i<pEList->nExpr; i++){
70390
 
      Expr *pX = pEList->a[i].pExpr;
70391
 
      if( sqlite3ResolveExprNames(&sNC, pX) ){
70392
 
        return WRC_Abort;
70393
 
      }
70394
 
    }
70395
 
  
70396
 
    /* Recursively resolve names in all subqueries
70397
 
    */
70398
 
    for(i=0; i<p->pSrc->nSrc; i++){
70399
 
      struct SrcList_item *pItem = &p->pSrc->a[i];
70400
 
      if( pItem->pSelect ){
70401
 
        const char *zSavedContext = pParse->zAuthContext;
70402
 
        if( pItem->zName ) pParse->zAuthContext = pItem->zName;
70403
 
        sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
70404
 
        pParse->zAuthContext = zSavedContext;
70405
 
        if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
70406
 
      }
70407
 
    }
70408
 
  
70409
 
    /* If there are no aggregate functions in the result-set, and no GROUP BY 
70410
 
    ** expression, do not allow aggregates in any of the other expressions.
70411
 
    */
70412
 
    assert( (p->selFlags & SF_Aggregate)==0 );
70413
 
    pGroupBy = p->pGroupBy;
70414
 
    if( pGroupBy || sNC.hasAgg ){
70415
 
      p->selFlags |= SF_Aggregate;
70416
 
    }else{
70417
 
      sNC.allowAgg = 0;
70418
 
    }
70419
 
  
70420
 
    /* If a HAVING clause is present, then there must be a GROUP BY clause.
70421
 
    */
70422
 
    if( p->pHaving && !pGroupBy ){
70423
 
      sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
70424
 
      return WRC_Abort;
70425
 
    }
70426
 
  
70427
 
    /* Add the expression list to the name-context before parsing the
70428
 
    ** other expressions in the SELECT statement. This is so that
70429
 
    ** expressions in the WHERE clause (etc.) can refer to expressions by
70430
 
    ** aliases in the result set.
70431
 
    **
70432
 
    ** Minor point: If this is the case, then the expression will be
70433
 
    ** re-evaluated for each reference to it.
70434
 
    */
70435
 
    sNC.pEList = p->pEList;
70436
 
    if( sqlite3ResolveExprNames(&sNC, p->pWhere) ||
70437
 
       sqlite3ResolveExprNames(&sNC, p->pHaving)
70438
 
    ){
70439
 
      return WRC_Abort;
70440
 
    }
70441
 
 
70442
 
    /* The ORDER BY and GROUP BY clauses may not refer to terms in
70443
 
    ** outer queries 
70444
 
    */
70445
 
    sNC.pNext = 0;
70446
 
    sNC.allowAgg = 1;
70447
 
 
70448
 
    /* Process the ORDER BY clause for singleton SELECT statements.
70449
 
    ** The ORDER BY clause for compounds SELECT statements is handled
70450
 
    ** below, after all of the result-sets for all of the elements of
70451
 
    ** the compound have been resolved.
70452
 
    */
70453
 
    if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
70454
 
      return WRC_Abort;
70455
 
    }
70456
 
    if( db->mallocFailed ){
70457
 
      return WRC_Abort;
70458
 
    }
70459
 
  
70460
 
    /* Resolve the GROUP BY clause.  At the same time, make sure 
70461
 
    ** the GROUP BY clause does not contain aggregate functions.
70462
 
    */
70463
 
    if( pGroupBy ){
70464
 
      struct ExprList_item *pItem;
70465
 
    
70466
 
      if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
70467
 
        return WRC_Abort;
70468
 
      }
70469
 
      for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
70470
 
        if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
70471
 
          sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
70472
 
              "the GROUP BY clause");
70473
 
          return WRC_Abort;
70474
 
        }
70475
 
      }
70476
 
    }
70477
 
 
70478
 
    /* Advance to the next term of the compound
70479
 
    */
70480
 
    p = p->pPrior;
70481
 
    nCompound++;
70482
 
  }
70483
 
 
70484
 
  /* Resolve the ORDER BY on a compound SELECT after all terms of
70485
 
  ** the compound have been resolved.
70486
 
  */
70487
 
  if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
70488
 
    return WRC_Abort;
70489
 
  }
70490
 
 
70491
 
  return WRC_Prune;
70492
 
}
70493
 
 
70494
 
/*
70495
 
** This routine walks an expression tree and resolves references to
70496
 
** table columns and result-set columns.  At the same time, do error
70497
 
** checking on function usage and set a flag if any aggregate functions
70498
 
** are seen.
70499
 
**
70500
 
** To resolve table columns references we look for nodes (or subtrees) of the 
70501
 
** form X.Y.Z or Y.Z or just Z where
70502
 
**
70503
 
**      X:   The name of a database.  Ex:  "main" or "temp" or
70504
 
**           the symbolic name assigned to an ATTACH-ed database.
70505
 
**
70506
 
**      Y:   The name of a table in a FROM clause.  Or in a trigger
70507
 
**           one of the special names "old" or "new".
70508
 
**
70509
 
**      Z:   The name of a column in table Y.
70510
 
**
70511
 
** The node at the root of the subtree is modified as follows:
70512
 
**
70513
 
**    Expr.op        Changed to TK_COLUMN
70514
 
**    Expr.pTab      Points to the Table object for X.Y
70515
 
**    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
70516
 
**    Expr.iTable    The VDBE cursor number for X.Y
70517
 
**
70518
 
**
70519
 
** To resolve result-set references, look for expression nodes of the
70520
 
** form Z (with no X and Y prefix) where the Z matches the right-hand
70521
 
** size of an AS clause in the result-set of a SELECT.  The Z expression
70522
 
** is replaced by a copy of the left-hand side of the result-set expression.
70523
 
** Table-name and function resolution occurs on the substituted expression
70524
 
** tree.  For example, in:
70525
 
**
70526
 
**      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
70527
 
**
70528
 
** The "x" term of the order by is replaced by "a+b" to render:
70529
 
**
70530
 
**      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
70531
 
**
70532
 
** Function calls are checked to make sure that the function is 
70533
 
** defined and that the correct number of arguments are specified.
70534
 
** If the function is an aggregate function, then the pNC->hasAgg is
70535
 
** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
70536
 
** If an expression contains aggregate functions then the EP_Agg
70537
 
** property on the expression is set.
70538
 
**
70539
 
** An error message is left in pParse if anything is amiss.  The number
70540
 
** if errors is returned.
70541
 
*/
70542
 
SQLITE_PRIVATE int sqlite3ResolveExprNames( 
70543
 
  NameContext *pNC,       /* Namespace to resolve expressions in. */
70544
 
  Expr *pExpr             /* The expression to be analyzed. */
70545
 
){
70546
 
  int savedHasAgg;
70547
 
  Walker w;
70548
 
 
70549
 
  if( pExpr==0 ) return 0;
70550
 
#if SQLITE_MAX_EXPR_DEPTH>0
70551
 
  {
70552
 
    Parse *pParse = pNC->pParse;
70553
 
    if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
70554
 
      return 1;
70555
 
    }
70556
 
    pParse->nHeight += pExpr->nHeight;
70557
 
  }
70558
 
#endif
70559
 
  savedHasAgg = pNC->hasAgg;
70560
 
  pNC->hasAgg = 0;
70561
 
  w.xExprCallback = resolveExprStep;
70562
 
  w.xSelectCallback = resolveSelectStep;
70563
 
  w.pParse = pNC->pParse;
70564
 
  w.u.pNC = pNC;
70565
 
  sqlite3WalkExpr(&w, pExpr);
70566
 
#if SQLITE_MAX_EXPR_DEPTH>0
70567
 
  pNC->pParse->nHeight -= pExpr->nHeight;
70568
 
#endif
70569
 
  if( pNC->nErr>0 || w.pParse->nErr>0 ){
70570
 
    ExprSetProperty(pExpr, EP_Error);
70571
 
  }
70572
 
  if( pNC->hasAgg ){
70573
 
    ExprSetProperty(pExpr, EP_Agg);
70574
 
  }else if( savedHasAgg ){
70575
 
    pNC->hasAgg = 1;
70576
 
  }
70577
 
  return ExprHasProperty(pExpr, EP_Error);
70578
 
}
70579
 
 
70580
 
 
70581
 
/*
70582
 
** Resolve all names in all expressions of a SELECT and in all
70583
 
** decendents of the SELECT, including compounds off of p->pPrior,
70584
 
** subqueries in expressions, and subqueries used as FROM clause
70585
 
** terms.
70586
 
**
70587
 
** See sqlite3ResolveExprNames() for a description of the kinds of
70588
 
** transformations that occur.
70589
 
**
70590
 
** All SELECT statements should have been expanded using
70591
 
** sqlite3SelectExpand() prior to invoking this routine.
70592
 
*/
70593
 
SQLITE_PRIVATE void sqlite3ResolveSelectNames(
70594
 
  Parse *pParse,         /* The parser context */
70595
 
  Select *p,             /* The SELECT statement being coded. */
70596
 
  NameContext *pOuterNC  /* Name context for parent SELECT statement */
70597
 
){
70598
 
  Walker w;
70599
 
 
70600
 
  assert( p!=0 );
70601
 
  w.xExprCallback = resolveExprStep;
70602
 
  w.xSelectCallback = resolveSelectStep;
70603
 
  w.pParse = pParse;
70604
 
  w.u.pNC = pOuterNC;
70605
 
  sqlite3WalkSelect(&w, p);
70606
 
}
70607
 
 
70608
 
/************** End of resolve.c *********************************************/
70609
 
/************** Begin file expr.c ********************************************/
70610
 
/*
70611
 
** 2001 September 15
70612
 
**
70613
 
** The author disclaims copyright to this source code.  In place of
70614
 
** a legal notice, here is a blessing:
70615
 
**
70616
 
**    May you do good and not evil.
70617
 
**    May you find forgiveness for yourself and forgive others.
70618
 
**    May you share freely, never taking more than you give.
70619
 
**
70620
 
*************************************************************************
70621
 
** This file contains routines used for analyzing expressions and
70622
 
** for generating VDBE code that evaluates expressions in SQLite.
70623
 
*/
70624
 
 
70625
 
/*
70626
 
** Return the 'affinity' of the expression pExpr if any.
70627
 
**
70628
 
** If pExpr is a column, a reference to a column via an 'AS' alias,
70629
 
** or a sub-select with a column as the return value, then the 
70630
 
** affinity of that column is returned. Otherwise, 0x00 is returned,
70631
 
** indicating no affinity for the expression.
70632
 
**
70633
 
** i.e. the WHERE clause expresssions in the following statements all
70634
 
** have an affinity:
70635
 
**
70636
 
** CREATE TABLE t1(a);
70637
 
** SELECT * FROM t1 WHERE a;
70638
 
** SELECT a AS b FROM t1 WHERE b;
70639
 
** SELECT * FROM t1 WHERE (select a from t1);
70640
 
*/
70641
 
SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
70642
 
  int op = pExpr->op;
70643
 
  if( op==TK_SELECT ){
70644
 
    assert( pExpr->flags&EP_xIsSelect );
70645
 
    return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
70646
 
  }
70647
 
#ifndef SQLITE_OMIT_CAST
70648
 
  if( op==TK_CAST ){
70649
 
    assert( !ExprHasProperty(pExpr, EP_IntValue) );
70650
 
    return sqlite3AffinityType(pExpr->u.zToken);
70651
 
  }
70652
 
#endif
70653
 
  if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) 
70654
 
   && pExpr->pTab!=0
70655
 
  ){
70656
 
    /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
70657
 
    ** a TK_COLUMN but was previously evaluated and cached in a register */
70658
 
    int j = pExpr->iColumn;
70659
 
    if( j<0 ) return SQLITE_AFF_INTEGER;
70660
 
    assert( pExpr->pTab && j<pExpr->pTab->nCol );
70661
 
    return pExpr->pTab->aCol[j].affinity;
70662
 
  }
70663
 
  return pExpr->affinity;
70664
 
}
70665
 
 
70666
 
/*
70667
 
** Set the explicit collating sequence for an expression to the
70668
 
** collating sequence supplied in the second argument.
70669
 
*/
70670
 
SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr *pExpr, CollSeq *pColl){
70671
 
  if( pExpr && pColl ){
70672
 
    pExpr->pColl = pColl;
70673
 
    pExpr->flags |= EP_ExpCollate;
70674
 
  }
70675
 
  return pExpr;
70676
 
}
70677
 
 
70678
 
/*
70679
 
** Set the collating sequence for expression pExpr to be the collating
70680
 
** sequence named by pToken.   Return a pointer to the revised expression.
70681
 
** The collating sequence is marked as "explicit" using the EP_ExpCollate
70682
 
** flag.  An explicit collating sequence will override implicit
70683
 
** collating sequences.
70684
 
*/
70685
 
SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr *pExpr, Token *pCollName){
70686
 
  char *zColl = 0;            /* Dequoted name of collation sequence */
70687
 
  CollSeq *pColl;
70688
 
  sqlite3 *db = pParse->db;
70689
 
  zColl = sqlite3NameFromToken(db, pCollName);
70690
 
  pColl = sqlite3LocateCollSeq(pParse, zColl);
70691
 
  sqlite3ExprSetColl(pExpr, pColl);
70692
 
  sqlite3DbFree(db, zColl);
70693
 
  return pExpr;
70694
 
}
70695
 
 
70696
 
/*
70697
 
** Return the default collation sequence for the expression pExpr. If
70698
 
** there is no default collation type, return 0.
70699
 
*/
70700
 
SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
70701
 
  CollSeq *pColl = 0;
70702
 
  Expr *p = pExpr;
70703
 
  while( p ){
70704
 
    int op;
70705
 
    pColl = p->pColl;
70706
 
    if( pColl ) break;
70707
 
    op = p->op;
70708
 
    if( p->pTab!=0 && (
70709
 
        op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER || op==TK_TRIGGER
70710
 
    )){
70711
 
      /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
70712
 
      ** a TK_COLUMN but was previously evaluated and cached in a register */
70713
 
      const char *zColl;
70714
 
      int j = p->iColumn;
70715
 
      if( j>=0 ){
70716
 
        sqlite3 *db = pParse->db;
70717
 
        zColl = p->pTab->aCol[j].zColl;
70718
 
        pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
70719
 
        pExpr->pColl = pColl;
70720
 
      }
70721
 
      break;
70722
 
    }
70723
 
    if( op!=TK_CAST && op!=TK_UPLUS ){
70724
 
      break;
70725
 
    }
70726
 
    p = p->pLeft;
70727
 
  }
70728
 
  if( sqlite3CheckCollSeq(pParse, pColl) ){ 
70729
 
    pColl = 0;
70730
 
  }
70731
 
  return pColl;
70732
 
}
70733
 
 
70734
 
/*
70735
 
** pExpr is an operand of a comparison operator.  aff2 is the
70736
 
** type affinity of the other operand.  This routine returns the
70737
 
** type affinity that should be used for the comparison operator.
70738
 
*/
70739
 
SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
70740
 
  char aff1 = sqlite3ExprAffinity(pExpr);
70741
 
  if( aff1 && aff2 ){
70742
 
    /* Both sides of the comparison are columns. If one has numeric
70743
 
    ** affinity, use that. Otherwise use no affinity.
70744
 
    */
70745
 
    if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
70746
 
      return SQLITE_AFF_NUMERIC;
70747
 
    }else{
70748
 
      return SQLITE_AFF_NONE;
70749
 
    }
70750
 
  }else if( !aff1 && !aff2 ){
70751
 
    /* Neither side of the comparison is a column.  Compare the
70752
 
    ** results directly.
70753
 
    */
70754
 
    return SQLITE_AFF_NONE;
70755
 
  }else{
70756
 
    /* One side is a column, the other is not. Use the columns affinity. */
70757
 
    assert( aff1==0 || aff2==0 );
70758
 
    return (aff1 + aff2);
70759
 
  }
70760
 
}
70761
 
 
70762
 
/*
70763
 
** pExpr is a comparison operator.  Return the type affinity that should
70764
 
** be applied to both operands prior to doing the comparison.
70765
 
*/
70766
 
static char comparisonAffinity(Expr *pExpr){
70767
 
  char aff;
70768
 
  assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
70769
 
          pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
70770
 
          pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
70771
 
  assert( pExpr->pLeft );
70772
 
  aff = sqlite3ExprAffinity(pExpr->pLeft);
70773
 
  if( pExpr->pRight ){
70774
 
    aff = sqlite3CompareAffinity(pExpr->pRight, aff);
70775
 
  }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
70776
 
    aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
70777
 
  }else if( !aff ){
70778
 
    aff = SQLITE_AFF_NONE;
70779
 
  }
70780
 
  return aff;
70781
 
}
70782
 
 
70783
 
/*
70784
 
** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
70785
 
** idx_affinity is the affinity of an indexed column. Return true
70786
 
** if the index with affinity idx_affinity may be used to implement
70787
 
** the comparison in pExpr.
70788
 
*/
70789
 
SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
70790
 
  char aff = comparisonAffinity(pExpr);
70791
 
  switch( aff ){
70792
 
    case SQLITE_AFF_NONE:
70793
 
      return 1;
70794
 
    case SQLITE_AFF_TEXT:
70795
 
      return idx_affinity==SQLITE_AFF_TEXT;
70796
 
    default:
70797
 
      return sqlite3IsNumericAffinity(idx_affinity);
70798
 
  }
70799
 
}
70800
 
 
70801
 
/*
70802
 
** Return the P5 value that should be used for a binary comparison
70803
 
** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
70804
 
*/
70805
 
static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
70806
 
  u8 aff = (char)sqlite3ExprAffinity(pExpr2);
70807
 
  aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
70808
 
  return aff;
70809
 
}
70810
 
 
70811
 
/*
70812
 
** Return a pointer to the collation sequence that should be used by
70813
 
** a binary comparison operator comparing pLeft and pRight.
70814
 
**
70815
 
** If the left hand expression has a collating sequence type, then it is
70816
 
** used. Otherwise the collation sequence for the right hand expression
70817
 
** is used, or the default (BINARY) if neither expression has a collating
70818
 
** type.
70819
 
**
70820
 
** Argument pRight (but not pLeft) may be a null pointer. In this case,
70821
 
** it is not considered.
70822
 
*/
70823
 
SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
70824
 
  Parse *pParse, 
70825
 
  Expr *pLeft, 
70826
 
  Expr *pRight
70827
 
){
70828
 
  CollSeq *pColl;
70829
 
  assert( pLeft );
70830
 
  if( pLeft->flags & EP_ExpCollate ){
70831
 
    assert( pLeft->pColl );
70832
 
    pColl = pLeft->pColl;
70833
 
  }else if( pRight && pRight->flags & EP_ExpCollate ){
70834
 
    assert( pRight->pColl );
70835
 
    pColl = pRight->pColl;
70836
 
  }else{
70837
 
    pColl = sqlite3ExprCollSeq(pParse, pLeft);
70838
 
    if( !pColl ){
70839
 
      pColl = sqlite3ExprCollSeq(pParse, pRight);
70840
 
    }
70841
 
  }
70842
 
  return pColl;
70843
 
}
70844
 
 
70845
 
/*
70846
 
** Generate code for a comparison operator.
70847
 
*/
70848
 
static int codeCompare(
70849
 
  Parse *pParse,    /* The parsing (and code generating) context */
70850
 
  Expr *pLeft,      /* The left operand */
70851
 
  Expr *pRight,     /* The right operand */
70852
 
  int opcode,       /* The comparison opcode */
70853
 
  int in1, int in2, /* Register holding operands */
70854
 
  int dest,         /* Jump here if true.  */
70855
 
  int jumpIfNull    /* If true, jump if either operand is NULL */
70856
 
){
70857
 
  int p5;
70858
 
  int addr;
70859
 
  CollSeq *p4;
70860
 
 
70861
 
  p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
70862
 
  p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
70863
 
  addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
70864
 
                           (void*)p4, P4_COLLSEQ);
70865
 
  sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
70866
 
  return addr;
70867
 
}
70868
 
 
70869
 
#if SQLITE_MAX_EXPR_DEPTH>0
70870
 
/*
70871
 
** Check that argument nHeight is less than or equal to the maximum
70872
 
** expression depth allowed. If it is not, leave an error message in
70873
 
** pParse.
70874
 
*/
70875
 
SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
70876
 
  int rc = SQLITE_OK;
70877
 
  int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
70878
 
  if( nHeight>mxHeight ){
70879
 
    sqlite3ErrorMsg(pParse, 
70880
 
       "Expression tree is too large (maximum depth %d)", mxHeight
70881
 
    );
70882
 
    rc = SQLITE_ERROR;
70883
 
  }
70884
 
  return rc;
70885
 
}
70886
 
 
70887
 
/* The following three functions, heightOfExpr(), heightOfExprList()
70888
 
** and heightOfSelect(), are used to determine the maximum height
70889
 
** of any expression tree referenced by the structure passed as the
70890
 
** first argument.
70891
 
**
70892
 
** If this maximum height is greater than the current value pointed
70893
 
** to by pnHeight, the second parameter, then set *pnHeight to that
70894
 
** value.
70895
 
*/
70896
 
static void heightOfExpr(Expr *p, int *pnHeight){
70897
 
  if( p ){
70898
 
    if( p->nHeight>*pnHeight ){
70899
 
      *pnHeight = p->nHeight;
70900
 
    }
70901
 
  }
70902
 
}
70903
 
static void heightOfExprList(ExprList *p, int *pnHeight){
70904
 
  if( p ){
70905
 
    int i;
70906
 
    for(i=0; i<p->nExpr; i++){
70907
 
      heightOfExpr(p->a[i].pExpr, pnHeight);
70908
 
    }
70909
 
  }
70910
 
}
70911
 
static void heightOfSelect(Select *p, int *pnHeight){
70912
 
  if( p ){
70913
 
    heightOfExpr(p->pWhere, pnHeight);
70914
 
    heightOfExpr(p->pHaving, pnHeight);
70915
 
    heightOfExpr(p->pLimit, pnHeight);
70916
 
    heightOfExpr(p->pOffset, pnHeight);
70917
 
    heightOfExprList(p->pEList, pnHeight);
70918
 
    heightOfExprList(p->pGroupBy, pnHeight);
70919
 
    heightOfExprList(p->pOrderBy, pnHeight);
70920
 
    heightOfSelect(p->pPrior, pnHeight);
70921
 
  }
70922
 
}
70923
 
 
70924
 
/*
70925
 
** Set the Expr.nHeight variable in the structure passed as an 
70926
 
** argument. An expression with no children, Expr.pList or 
70927
 
** Expr.pSelect member has a height of 1. Any other expression
70928
 
** has a height equal to the maximum height of any other 
70929
 
** referenced Expr plus one.
70930
 
*/
70931
 
static void exprSetHeight(Expr *p){
70932
 
  int nHeight = 0;
70933
 
  heightOfExpr(p->pLeft, &nHeight);
70934
 
  heightOfExpr(p->pRight, &nHeight);
70935
 
  if( ExprHasProperty(p, EP_xIsSelect) ){
70936
 
    heightOfSelect(p->x.pSelect, &nHeight);
70937
 
  }else{
70938
 
    heightOfExprList(p->x.pList, &nHeight);
70939
 
  }
70940
 
  p->nHeight = nHeight + 1;
70941
 
}
70942
 
 
70943
 
/*
70944
 
** Set the Expr.nHeight variable using the exprSetHeight() function. If
70945
 
** the height is greater than the maximum allowed expression depth,
70946
 
** leave an error in pParse.
70947
 
*/
70948
 
SQLITE_PRIVATE void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
70949
 
  exprSetHeight(p);
70950
 
  sqlite3ExprCheckHeight(pParse, p->nHeight);
70951
 
}
70952
 
 
70953
 
/*
70954
 
** Return the maximum height of any expression tree referenced
70955
 
** by the select statement passed as an argument.
70956
 
*/
70957
 
SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
70958
 
  int nHeight = 0;
70959
 
  heightOfSelect(p, &nHeight);
70960
 
  return nHeight;
70961
 
}
70962
 
#else
70963
 
  #define exprSetHeight(y)
70964
 
#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
70965
 
 
70966
 
/*
70967
 
** This routine is the core allocator for Expr nodes.
70968
 
**
70969
 
** Construct a new expression node and return a pointer to it.  Memory
70970
 
** for this node and for the pToken argument is a single allocation
70971
 
** obtained from sqlite3DbMalloc().  The calling function
70972
 
** is responsible for making sure the node eventually gets freed.
70973
 
**
70974
 
** If dequote is true, then the token (if it exists) is dequoted.
70975
 
** If dequote is false, no dequoting is performance.  The deQuote
70976
 
** parameter is ignored if pToken is NULL or if the token does not
70977
 
** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
70978
 
** then the EP_DblQuoted flag is set on the expression node.
70979
 
**
70980
 
** Special case:  If op==TK_INTEGER and pToken points to a string that
70981
 
** can be translated into a 32-bit integer, then the token is not
70982
 
** stored in u.zToken.  Instead, the integer values is written
70983
 
** into u.iValue and the EP_IntValue flag is set.  No extra storage
70984
 
** is allocated to hold the integer text and the dequote flag is ignored.
70985
 
*/
70986
 
SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
70987
 
  sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
70988
 
  int op,                 /* Expression opcode */
70989
 
  const Token *pToken,    /* Token argument.  Might be NULL */
70990
 
  int dequote             /* True to dequote */
70991
 
){
70992
 
  Expr *pNew;
70993
 
  int nExtra = 0;
70994
 
  int iValue = 0;
70995
 
 
70996
 
  if( pToken ){
70997
 
    if( op!=TK_INTEGER || pToken->z==0
70998
 
          || sqlite3GetInt32(pToken->z, &iValue)==0 ){
70999
 
      nExtra = pToken->n+1;
71000
 
      assert( iValue>=0 );
71001
 
    }
71002
 
  }
71003
 
  pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
71004
 
  if( pNew ){
71005
 
    pNew->op = (u8)op;
71006
 
    pNew->iAgg = -1;
71007
 
    if( pToken ){
71008
 
      if( nExtra==0 ){
71009
 
        pNew->flags |= EP_IntValue;
71010
 
        pNew->u.iValue = iValue;
71011
 
      }else{
71012
 
        int c;
71013
 
        pNew->u.zToken = (char*)&pNew[1];
71014
 
        memcpy(pNew->u.zToken, pToken->z, pToken->n);
71015
 
        pNew->u.zToken[pToken->n] = 0;
71016
 
        if( dequote && nExtra>=3 
71017
 
             && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
71018
 
          sqlite3Dequote(pNew->u.zToken);
71019
 
          if( c=='"' ) pNew->flags |= EP_DblQuoted;
71020
 
        }
71021
 
      }
71022
 
    }
71023
 
#if SQLITE_MAX_EXPR_DEPTH>0
71024
 
    pNew->nHeight = 1;
71025
 
#endif  
71026
 
  }
71027
 
  return pNew;
71028
 
}
71029
 
 
71030
 
/*
71031
 
** Allocate a new expression node from a zero-terminated token that has
71032
 
** already been dequoted.
71033
 
*/
71034
 
SQLITE_PRIVATE Expr *sqlite3Expr(
71035
 
  sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
71036
 
  int op,                 /* Expression opcode */
71037
 
  const char *zToken      /* Token argument.  Might be NULL */
71038
 
){
71039
 
  Token x;
71040
 
  x.z = zToken;
71041
 
  x.n = zToken ? sqlite3Strlen30(zToken) : 0;
71042
 
  return sqlite3ExprAlloc(db, op, &x, 0);
71043
 
}
71044
 
 
71045
 
/*
71046
 
** Attach subtrees pLeft and pRight to the Expr node pRoot.
71047
 
**
71048
 
** If pRoot==NULL that means that a memory allocation error has occurred.
71049
 
** In that case, delete the subtrees pLeft and pRight.
71050
 
*/
71051
 
SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
71052
 
  sqlite3 *db,
71053
 
  Expr *pRoot,
71054
 
  Expr *pLeft,
71055
 
  Expr *pRight
71056
 
){
71057
 
  if( pRoot==0 ){
71058
 
    assert( db->mallocFailed );
71059
 
    sqlite3ExprDelete(db, pLeft);
71060
 
    sqlite3ExprDelete(db, pRight);
71061
 
  }else{
71062
 
    if( pRight ){
71063
 
      pRoot->pRight = pRight;
71064
 
      if( pRight->flags & EP_ExpCollate ){
71065
 
        pRoot->flags |= EP_ExpCollate;
71066
 
        pRoot->pColl = pRight->pColl;
71067
 
      }
71068
 
    }
71069
 
    if( pLeft ){
71070
 
      pRoot->pLeft = pLeft;
71071
 
      if( pLeft->flags & EP_ExpCollate ){
71072
 
        pRoot->flags |= EP_ExpCollate;
71073
 
        pRoot->pColl = pLeft->pColl;
71074
 
      }
71075
 
    }
71076
 
    exprSetHeight(pRoot);
71077
 
  }
71078
 
}
71079
 
 
71080
 
/*
71081
 
** Allocate a Expr node which joins as many as two subtrees.
71082
 
**
71083
 
** One or both of the subtrees can be NULL.  Return a pointer to the new
71084
 
** Expr node.  Or, if an OOM error occurs, set pParse->db->mallocFailed,
71085
 
** free the subtrees and return NULL.
71086
 
*/
71087
 
SQLITE_PRIVATE Expr *sqlite3PExpr(
71088
 
  Parse *pParse,          /* Parsing context */
71089
 
  int op,                 /* Expression opcode */
71090
 
  Expr *pLeft,            /* Left operand */
71091
 
  Expr *pRight,           /* Right operand */
71092
 
  const Token *pToken     /* Argument token */
71093
 
){
71094
 
  Expr *p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
71095
 
  sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
71096
 
  if( p ) {
71097
 
    sqlite3ExprCheckHeight(pParse, p->nHeight);
71098
 
  }
71099
 
  return p;
71100
 
}
71101
 
 
71102
 
/*
71103
 
** Join two expressions using an AND operator.  If either expression is
71104
 
** NULL, then just return the other expression.
71105
 
*/
71106
 
SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
71107
 
  if( pLeft==0 ){
71108
 
    return pRight;
71109
 
  }else if( pRight==0 ){
71110
 
    return pLeft;
71111
 
  }else{
71112
 
    Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
71113
 
    sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
71114
 
    return pNew;
71115
 
  }
71116
 
}
71117
 
 
71118
 
/*
71119
 
** Construct a new expression node for a function with multiple
71120
 
** arguments.
71121
 
*/
71122
 
SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
71123
 
  Expr *pNew;
71124
 
  sqlite3 *db = pParse->db;
71125
 
  assert( pToken );
71126
 
  pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
71127
 
  if( pNew==0 ){
71128
 
    sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
71129
 
    return 0;
71130
 
  }
71131
 
  pNew->x.pList = pList;
71132
 
  assert( !ExprHasProperty(pNew, EP_xIsSelect) );
71133
 
  sqlite3ExprSetHeight(pParse, pNew);
71134
 
  return pNew;
71135
 
}
71136
 
 
71137
 
/*
71138
 
** Assign a variable number to an expression that encodes a wildcard
71139
 
** in the original SQL statement.  
71140
 
**
71141
 
** Wildcards consisting of a single "?" are assigned the next sequential
71142
 
** variable number.
71143
 
**
71144
 
** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
71145
 
** sure "nnn" is not too be to avoid a denial of service attack when
71146
 
** the SQL statement comes from an external source.
71147
 
**
71148
 
** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
71149
 
** as the previous instance of the same wildcard.  Or if this is the first
71150
 
** instance of the wildcard, the next sequenial variable number is
71151
 
** assigned.
71152
 
*/
71153
 
SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
71154
 
  sqlite3 *db = pParse->db;
71155
 
  const char *z;
71156
 
 
71157
 
  if( pExpr==0 ) return;
71158
 
  assert( !ExprHasAnyProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
71159
 
  z = pExpr->u.zToken;
71160
 
  assert( z!=0 );
71161
 
  assert( z[0]!=0 );
71162
 
  if( z[1]==0 ){
71163
 
    /* Wildcard of the form "?".  Assign the next variable number */
71164
 
    assert( z[0]=='?' );
71165
 
    pExpr->iColumn = (ynVar)(++pParse->nVar);
71166
 
  }else if( z[0]=='?' ){
71167
 
    /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
71168
 
    ** use it as the variable number */
71169
 
    i64 i;
71170
 
    int bOk = 0==sqlite3Atoi64(&z[1], &i, sqlite3Strlen30(&z[1]), SQLITE_UTF8);
71171
 
    pExpr->iColumn = (ynVar)i;
71172
 
    testcase( i==0 );
71173
 
    testcase( i==1 );
71174
 
    testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
71175
 
    testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
71176
 
    if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
71177
 
      sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
71178
 
          db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
71179
 
    }
71180
 
    if( i>pParse->nVar ){
71181
 
      pParse->nVar = (int)i;
71182
 
    }
71183
 
  }else{
71184
 
    /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
71185
 
    ** number as the prior appearance of the same name, or if the name
71186
 
    ** has never appeared before, reuse the same variable number
71187
 
    */
71188
 
    int i;
71189
 
    u32 n;
71190
 
    n = sqlite3Strlen30(z);
71191
 
    for(i=0; i<pParse->nVarExpr; i++){
71192
 
      Expr *pE = pParse->apVarExpr[i];
71193
 
      assert( pE!=0 );
71194
 
      if( memcmp(pE->u.zToken, z, n)==0 && pE->u.zToken[n]==0 ){
71195
 
        pExpr->iColumn = pE->iColumn;
71196
 
        break;
71197
 
      }
71198
 
    }
71199
 
    if( i>=pParse->nVarExpr ){
71200
 
      pExpr->iColumn = (ynVar)(++pParse->nVar);
71201
 
      if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
71202
 
        pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
71203
 
        pParse->apVarExpr =
71204
 
            sqlite3DbReallocOrFree(
71205
 
              db,
71206
 
              pParse->apVarExpr,
71207
 
              pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
71208
 
            );
71209
 
      }
71210
 
      if( !db->mallocFailed ){
71211
 
        assert( pParse->apVarExpr!=0 );
71212
 
        pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
71213
 
      }
71214
 
    }
71215
 
  } 
71216
 
  if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
71217
 
    sqlite3ErrorMsg(pParse, "too many SQL variables");
71218
 
  }
71219
 
}
71220
 
 
71221
 
/*
71222
 
** Recursively delete an expression tree.
71223
 
*/
71224
 
SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
71225
 
  if( p==0 ) return;
71226
 
  /* Sanity check: Assert that the IntValue is non-negative if it exists */
71227
 
  assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
71228
 
  if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
71229
 
    sqlite3ExprDelete(db, p->pLeft);
71230
 
    sqlite3ExprDelete(db, p->pRight);
71231
 
    if( !ExprHasProperty(p, EP_Reduced) && (p->flags2 & EP2_MallocedToken)!=0 ){
71232
 
      sqlite3DbFree(db, p->u.zToken);
71233
 
    }
71234
 
    if( ExprHasProperty(p, EP_xIsSelect) ){
71235
 
      sqlite3SelectDelete(db, p->x.pSelect);
71236
 
    }else{
71237
 
      sqlite3ExprListDelete(db, p->x.pList);
71238
 
    }
71239
 
  }
71240
 
  if( !ExprHasProperty(p, EP_Static) ){
71241
 
    sqlite3DbFree(db, p);
71242
 
  }
71243
 
}
71244
 
 
71245
 
/*
71246
 
** Return the number of bytes allocated for the expression structure 
71247
 
** passed as the first argument. This is always one of EXPR_FULLSIZE,
71248
 
** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
71249
 
*/
71250
 
static int exprStructSize(Expr *p){
71251
 
  if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
71252
 
  if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
71253
 
  return EXPR_FULLSIZE;
71254
 
}
71255
 
 
71256
 
/*
71257
 
** The dupedExpr*Size() routines each return the number of bytes required
71258
 
** to store a copy of an expression or expression tree.  They differ in
71259
 
** how much of the tree is measured.
71260
 
**
71261
 
**     dupedExprStructSize()     Size of only the Expr structure 
71262
 
**     dupedExprNodeSize()       Size of Expr + space for token
71263
 
**     dupedExprSize()           Expr + token + subtree components
71264
 
**
71265
 
***************************************************************************
71266
 
**
71267
 
** The dupedExprStructSize() function returns two values OR-ed together:  
71268
 
** (1) the space required for a copy of the Expr structure only and 
71269
 
** (2) the EP_xxx flags that indicate what the structure size should be.
71270
 
** The return values is always one of:
71271
 
**
71272
 
**      EXPR_FULLSIZE
71273
 
**      EXPR_REDUCEDSIZE   | EP_Reduced
71274
 
**      EXPR_TOKENONLYSIZE | EP_TokenOnly
71275
 
**
71276
 
** The size of the structure can be found by masking the return value
71277
 
** of this routine with 0xfff.  The flags can be found by masking the
71278
 
** return value with EP_Reduced|EP_TokenOnly.
71279
 
**
71280
 
** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
71281
 
** (unreduced) Expr objects as they or originally constructed by the parser.
71282
 
** During expression analysis, extra information is computed and moved into
71283
 
** later parts of teh Expr object and that extra information might get chopped
71284
 
** off if the expression is reduced.  Note also that it does not work to
71285
 
** make a EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
71286
 
** to reduce a pristine expression tree from the parser.  The implementation
71287
 
** of dupedExprStructSize() contain multiple assert() statements that attempt
71288
 
** to enforce this constraint.
71289
 
*/
71290
 
static int dupedExprStructSize(Expr *p, int flags){
71291
 
  int nSize;
71292
 
  assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
71293
 
  if( 0==(flags&EXPRDUP_REDUCE) ){
71294
 
    nSize = EXPR_FULLSIZE;
71295
 
  }else{
71296
 
    assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
71297
 
    assert( !ExprHasProperty(p, EP_FromJoin) ); 
71298
 
    assert( (p->flags2 & EP2_MallocedToken)==0 );
71299
 
    assert( (p->flags2 & EP2_Irreducible)==0 );
71300
 
    if( p->pLeft || p->pRight || p->pColl || p->x.pList ){
71301
 
      nSize = EXPR_REDUCEDSIZE | EP_Reduced;
71302
 
    }else{
71303
 
      nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
71304
 
    }
71305
 
  }
71306
 
  return nSize;
71307
 
}
71308
 
 
71309
 
/*
71310
 
** This function returns the space in bytes required to store the copy 
71311
 
** of the Expr structure and a copy of the Expr.u.zToken string (if that
71312
 
** string is defined.)
71313
 
*/
71314
 
static int dupedExprNodeSize(Expr *p, int flags){
71315
 
  int nByte = dupedExprStructSize(p, flags) & 0xfff;
71316
 
  if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
71317
 
    nByte += sqlite3Strlen30(p->u.zToken)+1;
71318
 
  }
71319
 
  return ROUND8(nByte);
71320
 
}
71321
 
 
71322
 
/*
71323
 
** Return the number of bytes required to create a duplicate of the 
71324
 
** expression passed as the first argument. The second argument is a
71325
 
** mask containing EXPRDUP_XXX flags.
71326
 
**
71327
 
** The value returned includes space to create a copy of the Expr struct
71328
 
** itself and the buffer referred to by Expr.u.zToken, if any.
71329
 
**
71330
 
** If the EXPRDUP_REDUCE flag is set, then the return value includes 
71331
 
** space to duplicate all Expr nodes in the tree formed by Expr.pLeft 
71332
 
** and Expr.pRight variables (but not for any structures pointed to or 
71333
 
** descended from the Expr.x.pList or Expr.x.pSelect variables).
71334
 
*/
71335
 
static int dupedExprSize(Expr *p, int flags){
71336
 
  int nByte = 0;
71337
 
  if( p ){
71338
 
    nByte = dupedExprNodeSize(p, flags);
71339
 
    if( flags&EXPRDUP_REDUCE ){
71340
 
      nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
71341
 
    }
71342
 
  }
71343
 
  return nByte;
71344
 
}
71345
 
 
71346
 
/*
71347
 
** This function is similar to sqlite3ExprDup(), except that if pzBuffer 
71348
 
** is not NULL then *pzBuffer is assumed to point to a buffer large enough 
71349
 
** to store the copy of expression p, the copies of p->u.zToken
71350
 
** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
71351
 
** if any. Before returning, *pzBuffer is set to the first byte passed the
71352
 
** portion of the buffer copied into by this function.
71353
 
*/
71354
 
static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
71355
 
  Expr *pNew = 0;                      /* Value to return */
71356
 
  if( p ){
71357
 
    const int isReduced = (flags&EXPRDUP_REDUCE);
71358
 
    u8 *zAlloc;
71359
 
    u32 staticFlag = 0;
71360
 
 
71361
 
    assert( pzBuffer==0 || isReduced );
71362
 
 
71363
 
    /* Figure out where to write the new Expr structure. */
71364
 
    if( pzBuffer ){
71365
 
      zAlloc = *pzBuffer;
71366
 
      staticFlag = EP_Static;
71367
 
    }else{
71368
 
      zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
71369
 
    }
71370
 
    pNew = (Expr *)zAlloc;
71371
 
 
71372
 
    if( pNew ){
71373
 
      /* Set nNewSize to the size allocated for the structure pointed to
71374
 
      ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
71375
 
      ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
71376
 
      ** by the copy of the p->u.zToken string (if any).
71377
 
      */
71378
 
      const unsigned nStructSize = dupedExprStructSize(p, flags);
71379
 
      const int nNewSize = nStructSize & 0xfff;
71380
 
      int nToken;
71381
 
      if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
71382
 
        nToken = sqlite3Strlen30(p->u.zToken) + 1;
71383
 
      }else{
71384
 
        nToken = 0;
71385
 
      }
71386
 
      if( isReduced ){
71387
 
        assert( ExprHasProperty(p, EP_Reduced)==0 );
71388
 
        memcpy(zAlloc, p, nNewSize);
71389
 
      }else{
71390
 
        int nSize = exprStructSize(p);
71391
 
        memcpy(zAlloc, p, nSize);
71392
 
        memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
71393
 
      }
71394
 
 
71395
 
      /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
71396
 
      pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
71397
 
      pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
71398
 
      pNew->flags |= staticFlag;
71399
 
 
71400
 
      /* Copy the p->u.zToken string, if any. */
71401
 
      if( nToken ){
71402
 
        char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
71403
 
        memcpy(zToken, p->u.zToken, nToken);
71404
 
      }
71405
 
 
71406
 
      if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
71407
 
        /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
71408
 
        if( ExprHasProperty(p, EP_xIsSelect) ){
71409
 
          pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
71410
 
        }else{
71411
 
          pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
71412
 
        }
71413
 
      }
71414
 
 
71415
 
      /* Fill in pNew->pLeft and pNew->pRight. */
71416
 
      if( ExprHasAnyProperty(pNew, EP_Reduced|EP_TokenOnly) ){
71417
 
        zAlloc += dupedExprNodeSize(p, flags);
71418
 
        if( ExprHasProperty(pNew, EP_Reduced) ){
71419
 
          pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
71420
 
          pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
71421
 
        }
71422
 
        if( pzBuffer ){
71423
 
          *pzBuffer = zAlloc;
71424
 
        }
71425
 
      }else{
71426
 
        pNew->flags2 = 0;
71427
 
        if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
71428
 
          pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
71429
 
          pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
71430
 
        }
71431
 
      }
71432
 
 
71433
 
    }
71434
 
  }
71435
 
  return pNew;
71436
 
}
71437
 
 
71438
 
/*
71439
 
** The following group of routines make deep copies of expressions,
71440
 
** expression lists, ID lists, and select statements.  The copies can
71441
 
** be deleted (by being passed to their respective ...Delete() routines)
71442
 
** without effecting the originals.
71443
 
**
71444
 
** The expression list, ID, and source lists return by sqlite3ExprListDup(),
71445
 
** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 
71446
 
** by subsequent calls to sqlite*ListAppend() routines.
71447
 
**
71448
 
** Any tables that the SrcList might point to are not duplicated.
71449
 
**
71450
 
** The flags parameter contains a combination of the EXPRDUP_XXX flags.
71451
 
** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
71452
 
** truncated version of the usual Expr structure that will be stored as
71453
 
** part of the in-memory representation of the database schema.
71454
 
*/
71455
 
SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
71456
 
  return exprDup(db, p, flags, 0);
71457
 
}
71458
 
SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
71459
 
  ExprList *pNew;
71460
 
  struct ExprList_item *pItem, *pOldItem;
71461
 
  int i;
71462
 
  if( p==0 ) return 0;
71463
 
  pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
71464
 
  if( pNew==0 ) return 0;
71465
 
  pNew->iECursor = 0;
71466
 
  pNew->nExpr = pNew->nAlloc = p->nExpr;
71467
 
  pNew->a = pItem = sqlite3DbMallocRaw(db,  p->nExpr*sizeof(p->a[0]) );
71468
 
  if( pItem==0 ){
71469
 
    sqlite3DbFree(db, pNew);
71470
 
    return 0;
71471
 
  } 
71472
 
  pOldItem = p->a;
71473
 
  for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
71474
 
    Expr *pOldExpr = pOldItem->pExpr;
71475
 
    pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
71476
 
    pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
71477
 
    pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
71478
 
    pItem->sortOrder = pOldItem->sortOrder;
71479
 
    pItem->done = 0;
71480
 
    pItem->iCol = pOldItem->iCol;
71481
 
    pItem->iAlias = pOldItem->iAlias;
71482
 
  }
71483
 
  return pNew;
71484
 
}
71485
 
 
71486
 
/*
71487
 
** If cursors, triggers, views and subqueries are all omitted from
71488
 
** the build, then none of the following routines, except for 
71489
 
** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
71490
 
** called with a NULL argument.
71491
 
*/
71492
 
#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
71493
 
 || !defined(SQLITE_OMIT_SUBQUERY)
71494
 
SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
71495
 
  SrcList *pNew;
71496
 
  int i;
71497
 
  int nByte;
71498
 
  if( p==0 ) return 0;
71499
 
  nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
71500
 
  pNew = sqlite3DbMallocRaw(db, nByte );
71501
 
  if( pNew==0 ) return 0;
71502
 
  pNew->nSrc = pNew->nAlloc = p->nSrc;
71503
 
  for(i=0; i<p->nSrc; i++){
71504
 
    struct SrcList_item *pNewItem = &pNew->a[i];
71505
 
    struct SrcList_item *pOldItem = &p->a[i];
71506
 
    Table *pTab;
71507
 
    pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
71508
 
    pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
71509
 
    pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
71510
 
    pNewItem->jointype = pOldItem->jointype;
71511
 
    pNewItem->iCursor = pOldItem->iCursor;
71512
 
    pNewItem->isPopulated = pOldItem->isPopulated;
71513
 
    pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
71514
 
    pNewItem->notIndexed = pOldItem->notIndexed;
71515
 
    pNewItem->pIndex = pOldItem->pIndex;
71516
 
    pTab = pNewItem->pTab = pOldItem->pTab;
71517
 
    if( pTab ){
71518
 
      pTab->nRef++;
71519
 
    }
71520
 
    pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
71521
 
    pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
71522
 
    pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
71523
 
    pNewItem->colUsed = pOldItem->colUsed;
71524
 
  }
71525
 
  return pNew;
71526
 
}
71527
 
SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
71528
 
  IdList *pNew;
71529
 
  int i;
71530
 
  if( p==0 ) return 0;
71531
 
  pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
71532
 
  if( pNew==0 ) return 0;
71533
 
  pNew->nId = pNew->nAlloc = p->nId;
71534
 
  pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
71535
 
  if( pNew->a==0 ){
71536
 
    sqlite3DbFree(db, pNew);
71537
 
    return 0;
71538
 
  }
71539
 
  for(i=0; i<p->nId; i++){
71540
 
    struct IdList_item *pNewItem = &pNew->a[i];
71541
 
    struct IdList_item *pOldItem = &p->a[i];
71542
 
    pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
71543
 
    pNewItem->idx = pOldItem->idx;
71544
 
  }
71545
 
  return pNew;
71546
 
}
71547
 
SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
71548
 
  Select *pNew;
71549
 
  if( p==0 ) return 0;
71550
 
  pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
71551
 
  if( pNew==0 ) return 0;
71552
 
  pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
71553
 
  pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
71554
 
  pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
71555
 
  pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
71556
 
  pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
71557
 
  pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
71558
 
  pNew->op = p->op;
71559
 
  pNew->pPrior = sqlite3SelectDup(db, p->pPrior, flags);
71560
 
  pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
71561
 
  pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
71562
 
  pNew->iLimit = 0;
71563
 
  pNew->iOffset = 0;
71564
 
  pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
71565
 
  pNew->pRightmost = 0;
71566
 
  pNew->addrOpenEphm[0] = -1;
71567
 
  pNew->addrOpenEphm[1] = -1;
71568
 
  pNew->addrOpenEphm[2] = -1;
71569
 
  return pNew;
71570
 
}
71571
 
#else
71572
 
SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
71573
 
  assert( p==0 );
71574
 
  return 0;
71575
 
}
71576
 
#endif
71577
 
 
71578
 
 
71579
 
/*
71580
 
** Add a new element to the end of an expression list.  If pList is
71581
 
** initially NULL, then create a new expression list.
71582
 
**
71583
 
** If a memory allocation error occurs, the entire list is freed and
71584
 
** NULL is returned.  If non-NULL is returned, then it is guaranteed
71585
 
** that the new entry was successfully appended.
71586
 
*/
71587
 
SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
71588
 
  Parse *pParse,          /* Parsing context */
71589
 
  ExprList *pList,        /* List to which to append. Might be NULL */
71590
 
  Expr *pExpr             /* Expression to be appended. Might be NULL */
71591
 
){
71592
 
  sqlite3 *db = pParse->db;
71593
 
  if( pList==0 ){
71594
 
    pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
71595
 
    if( pList==0 ){
71596
 
      goto no_mem;
71597
 
    }
71598
 
    assert( pList->nAlloc==0 );
71599
 
  }
71600
 
  if( pList->nAlloc<=pList->nExpr ){
71601
 
    struct ExprList_item *a;
71602
 
    int n = pList->nAlloc*2 + 4;
71603
 
    a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
71604
 
    if( a==0 ){
71605
 
      goto no_mem;
71606
 
    }
71607
 
    pList->a = a;
71608
 
    pList->nAlloc = sqlite3DbMallocSize(db, a)/sizeof(a[0]);
71609
 
  }
71610
 
  assert( pList->a!=0 );
71611
 
  #if 1
71612
 
  {
71613
 
    struct ExprList_item *pItem = &pList->a[pList->nExpr++];
71614
 
    memset(pItem, 0, sizeof(*pItem));
71615
 
    pItem->pExpr = pExpr;
71616
 
  }
71617
 
  #endif
71618
 
  return pList;
71619
 
 
71620
 
no_mem:     
71621
 
  /* Avoid leaking memory if malloc has failed. */
71622
 
  sqlite3ExprDelete(db, pExpr);
71623
 
  sqlite3ExprListDelete(db, pList);
71624
 
  return 0;
71625
 
}
71626
 
 
71627
 
/*
71628
 
** Set the ExprList.a[].zName element of the most recently added item
71629
 
** on the expression list.
71630
 
**
71631
 
** pList might be NULL following an OOM error.  But pName should never be
71632
 
** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
71633
 
** is set.
71634
 
*/
71635
 
SQLITE_PRIVATE void sqlite3ExprListSetName(
71636
 
  Parse *pParse,          /* Parsing context */
71637
 
  ExprList *pList,        /* List to which to add the span. */
71638
 
  Token *pName,           /* Name to be added */
71639
 
  int dequote             /* True to cause the name to be dequoted */
71640
 
){
71641
 
  assert( pList!=0 || pParse->db->mallocFailed!=0 );
71642
 
  if( pList ){
71643
 
    struct ExprList_item *pItem;
71644
 
    assert( pList->nExpr>0 );
71645
 
    pItem = &pList->a[pList->nExpr-1];
71646
 
    assert( pItem->zName==0 );
71647
 
    pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
71648
 
    if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
71649
 
  }
71650
 
}
71651
 
 
71652
 
/*
71653
 
** Set the ExprList.a[].zSpan element of the most recently added item
71654
 
** on the expression list.
71655
 
**
71656
 
** pList might be NULL following an OOM error.  But pSpan should never be
71657
 
** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
71658
 
** is set.
71659
 
*/
71660
 
SQLITE_PRIVATE void sqlite3ExprListSetSpan(
71661
 
  Parse *pParse,          /* Parsing context */
71662
 
  ExprList *pList,        /* List to which to add the span. */
71663
 
  ExprSpan *pSpan         /* The span to be added */
71664
 
){
71665
 
  sqlite3 *db = pParse->db;
71666
 
  assert( pList!=0 || db->mallocFailed!=0 );
71667
 
  if( pList ){
71668
 
    struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
71669
 
    assert( pList->nExpr>0 );
71670
 
    assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
71671
 
    sqlite3DbFree(db, pItem->zSpan);
71672
 
    pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
71673
 
                                    (int)(pSpan->zEnd - pSpan->zStart));
71674
 
  }
71675
 
}
71676
 
 
71677
 
/*
71678
 
** If the expression list pEList contains more than iLimit elements,
71679
 
** leave an error message in pParse.
71680
 
*/
71681
 
SQLITE_PRIVATE void sqlite3ExprListCheckLength(
71682
 
  Parse *pParse,
71683
 
  ExprList *pEList,
71684
 
  const char *zObject
71685
 
){
71686
 
  int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
71687
 
  testcase( pEList && pEList->nExpr==mx );
71688
 
  testcase( pEList && pEList->nExpr==mx+1 );
71689
 
  if( pEList && pEList->nExpr>mx ){
71690
 
    sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
71691
 
  }
71692
 
}
71693
 
 
71694
 
/*
71695
 
** Delete an entire expression list.
71696
 
*/
71697
 
SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
71698
 
  int i;
71699
 
  struct ExprList_item *pItem;
71700
 
  if( pList==0 ) return;
71701
 
  assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
71702
 
  assert( pList->nExpr<=pList->nAlloc );
71703
 
  for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
71704
 
    sqlite3ExprDelete(db, pItem->pExpr);
71705
 
    sqlite3DbFree(db, pItem->zName);
71706
 
    sqlite3DbFree(db, pItem->zSpan);
71707
 
  }
71708
 
  sqlite3DbFree(db, pList->a);
71709
 
  sqlite3DbFree(db, pList);
71710
 
}
71711
 
 
71712
 
/*
71713
 
** These routines are Walker callbacks.  Walker.u.pi is a pointer
71714
 
** to an integer.  These routines are checking an expression to see
71715
 
** if it is a constant.  Set *Walker.u.pi to 0 if the expression is
71716
 
** not constant.
71717
 
**
71718
 
** These callback routines are used to implement the following:
71719
 
**
71720
 
**     sqlite3ExprIsConstant()
71721
 
**     sqlite3ExprIsConstantNotJoin()
71722
 
**     sqlite3ExprIsConstantOrFunction()
71723
 
**
71724
 
*/
71725
 
static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
71726
 
 
71727
 
  /* If pWalker->u.i is 3 then any term of the expression that comes from
71728
 
  ** the ON or USING clauses of a join disqualifies the expression
71729
 
  ** from being considered constant. */
71730
 
  if( pWalker->u.i==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
71731
 
    pWalker->u.i = 0;
71732
 
    return WRC_Abort;
71733
 
  }
71734
 
 
71735
 
  switch( pExpr->op ){
71736
 
    /* Consider functions to be constant if all their arguments are constant
71737
 
    ** and pWalker->u.i==2 */
71738
 
    case TK_FUNCTION:
71739
 
      if( pWalker->u.i==2 ) return 0;
71740
 
      /* Fall through */
71741
 
    case TK_ID:
71742
 
    case TK_COLUMN:
71743
 
    case TK_AGG_FUNCTION:
71744
 
    case TK_AGG_COLUMN:
71745
 
      testcase( pExpr->op==TK_ID );
71746
 
      testcase( pExpr->op==TK_COLUMN );
71747
 
      testcase( pExpr->op==TK_AGG_FUNCTION );
71748
 
      testcase( pExpr->op==TK_AGG_COLUMN );
71749
 
      pWalker->u.i = 0;
71750
 
      return WRC_Abort;
71751
 
    default:
71752
 
      testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
71753
 
      testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
71754
 
      return WRC_Continue;
71755
 
  }
71756
 
}
71757
 
static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
71758
 
  UNUSED_PARAMETER(NotUsed);
71759
 
  pWalker->u.i = 0;
71760
 
  return WRC_Abort;
71761
 
}
71762
 
static int exprIsConst(Expr *p, int initFlag){
71763
 
  Walker w;
71764
 
  w.u.i = initFlag;
71765
 
  w.xExprCallback = exprNodeIsConstant;
71766
 
  w.xSelectCallback = selectNodeIsConstant;
71767
 
  sqlite3WalkExpr(&w, p);
71768
 
  return w.u.i;
71769
 
}
71770
 
 
71771
 
/*
71772
 
** Walk an expression tree.  Return 1 if the expression is constant
71773
 
** and 0 if it involves variables or function calls.
71774
 
**
71775
 
** For the purposes of this function, a double-quoted string (ex: "abc")
71776
 
** is considered a variable but a single-quoted string (ex: 'abc') is
71777
 
** a constant.
71778
 
*/
71779
 
SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
71780
 
  return exprIsConst(p, 1);
71781
 
}
71782
 
 
71783
 
/*
71784
 
** Walk an expression tree.  Return 1 if the expression is constant
71785
 
** that does no originate from the ON or USING clauses of a join.
71786
 
** Return 0 if it involves variables or function calls or terms from
71787
 
** an ON or USING clause.
71788
 
*/
71789
 
SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
71790
 
  return exprIsConst(p, 3);
71791
 
}
71792
 
 
71793
 
/*
71794
 
** Walk an expression tree.  Return 1 if the expression is constant
71795
 
** or a function call with constant arguments.  Return and 0 if there
71796
 
** are any variables.
71797
 
**
71798
 
** For the purposes of this function, a double-quoted string (ex: "abc")
71799
 
** is considered a variable but a single-quoted string (ex: 'abc') is
71800
 
** a constant.
71801
 
*/
71802
 
SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p){
71803
 
  return exprIsConst(p, 2);
71804
 
}
71805
 
 
71806
 
/*
71807
 
** If the expression p codes a constant integer that is small enough
71808
 
** to fit in a 32-bit integer, return 1 and put the value of the integer
71809
 
** in *pValue.  If the expression is not an integer or if it is too big
71810
 
** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
71811
 
*/
71812
 
SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
71813
 
  int rc = 0;
71814
 
 
71815
 
  /* If an expression is an integer literal that fits in a signed 32-bit
71816
 
  ** integer, then the EP_IntValue flag will have already been set */
71817
 
  assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
71818
 
           || sqlite3GetInt32(p->u.zToken, &rc)==0 );
71819
 
 
71820
 
  if( p->flags & EP_IntValue ){
71821
 
    *pValue = p->u.iValue;
71822
 
    return 1;
71823
 
  }
71824
 
  switch( p->op ){
71825
 
    case TK_UPLUS: {
71826
 
      rc = sqlite3ExprIsInteger(p->pLeft, pValue);
71827
 
      break;
71828
 
    }
71829
 
    case TK_UMINUS: {
71830
 
      int v;
71831
 
      if( sqlite3ExprIsInteger(p->pLeft, &v) ){
71832
 
        *pValue = -v;
71833
 
        rc = 1;
71834
 
      }
71835
 
      break;
71836
 
    }
71837
 
    default: break;
71838
 
  }
71839
 
  return rc;
71840
 
}
71841
 
 
71842
 
/*
71843
 
** Return FALSE if there is no chance that the expression can be NULL.
71844
 
**
71845
 
** If the expression might be NULL or if the expression is too complex
71846
 
** to tell return TRUE.  
71847
 
**
71848
 
** This routine is used as an optimization, to skip OP_IsNull opcodes
71849
 
** when we know that a value cannot be NULL.  Hence, a false positive
71850
 
** (returning TRUE when in fact the expression can never be NULL) might
71851
 
** be a small performance hit but is otherwise harmless.  On the other
71852
 
** hand, a false negative (returning FALSE when the result could be NULL)
71853
 
** will likely result in an incorrect answer.  So when in doubt, return
71854
 
** TRUE.
71855
 
*/
71856
 
SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
71857
 
  u8 op;
71858
 
  while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
71859
 
  op = p->op;
71860
 
  if( op==TK_REGISTER ) op = p->op2;
71861
 
  switch( op ){
71862
 
    case TK_INTEGER:
71863
 
    case TK_STRING:
71864
 
    case TK_FLOAT:
71865
 
    case TK_BLOB:
71866
 
      return 0;
71867
 
    default:
71868
 
      return 1;
71869
 
  }
71870
 
}
71871
 
 
71872
 
/*
71873
 
** Generate an OP_IsNull instruction that tests register iReg and jumps
71874
 
** to location iDest if the value in iReg is NULL.  The value in iReg 
71875
 
** was computed by pExpr.  If we can look at pExpr at compile-time and
71876
 
** determine that it can never generate a NULL, then the OP_IsNull operation
71877
 
** can be omitted.
71878
 
*/
71879
 
SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(
71880
 
  Vdbe *v,            /* The VDBE under construction */
71881
 
  const Expr *pExpr,  /* Only generate OP_IsNull if this expr can be NULL */
71882
 
  int iReg,           /* Test the value in this register for NULL */
71883
 
  int iDest           /* Jump here if the value is null */
71884
 
){
71885
 
  if( sqlite3ExprCanBeNull(pExpr) ){
71886
 
    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iDest);
71887
 
  }
71888
 
}
71889
 
 
71890
 
/*
71891
 
** Return TRUE if the given expression is a constant which would be
71892
 
** unchanged by OP_Affinity with the affinity given in the second
71893
 
** argument.
71894
 
**
71895
 
** This routine is used to determine if the OP_Affinity operation
71896
 
** can be omitted.  When in doubt return FALSE.  A false negative
71897
 
** is harmless.  A false positive, however, can result in the wrong
71898
 
** answer.
71899
 
*/
71900
 
SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
71901
 
  u8 op;
71902
 
  if( aff==SQLITE_AFF_NONE ) return 1;
71903
 
  while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
71904
 
  op = p->op;
71905
 
  if( op==TK_REGISTER ) op = p->op2;
71906
 
  switch( op ){
71907
 
    case TK_INTEGER: {
71908
 
      return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
71909
 
    }
71910
 
    case TK_FLOAT: {
71911
 
      return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
71912
 
    }
71913
 
    case TK_STRING: {
71914
 
      return aff==SQLITE_AFF_TEXT;
71915
 
    }
71916
 
    case TK_BLOB: {
71917
 
      return 1;
71918
 
    }
71919
 
    case TK_COLUMN: {
71920
 
      assert( p->iTable>=0 );  /* p cannot be part of a CHECK constraint */
71921
 
      return p->iColumn<0
71922
 
          && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
71923
 
    }
71924
 
    default: {
71925
 
      return 0;
71926
 
    }
71927
 
  }
71928
 
}
71929
 
 
71930
 
/*
71931
 
** Return TRUE if the given string is a row-id column name.
71932
 
*/
71933
 
SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
71934
 
  if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
71935
 
  if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
71936
 
  if( sqlite3StrICmp(z, "OID")==0 ) return 1;
71937
 
  return 0;
71938
 
}
71939
 
 
71940
 
/*
71941
 
** Return true if we are able to the IN operator optimization on a
71942
 
** query of the form
71943
 
**
71944
 
**       x IN (SELECT ...)
71945
 
**
71946
 
** Where the SELECT... clause is as specified by the parameter to this
71947
 
** routine.
71948
 
**
71949
 
** The Select object passed in has already been preprocessed and no
71950
 
** errors have been found.
71951
 
*/
71952
 
#ifndef SQLITE_OMIT_SUBQUERY
71953
 
static int isCandidateForInOpt(Select *p){
71954
 
  SrcList *pSrc;
71955
 
  ExprList *pEList;
71956
 
  Table *pTab;
71957
 
  if( p==0 ) return 0;                   /* right-hand side of IN is SELECT */
71958
 
  if( p->pPrior ) return 0;              /* Not a compound SELECT */
71959
 
  if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
71960
 
    testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
71961
 
    testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
71962
 
    return 0; /* No DISTINCT keyword and no aggregate functions */
71963
 
  }
71964
 
  assert( p->pGroupBy==0 );              /* Has no GROUP BY clause */
71965
 
  if( p->pLimit ) return 0;              /* Has no LIMIT clause */
71966
 
  assert( p->pOffset==0 );               /* No LIMIT means no OFFSET */
71967
 
  if( p->pWhere ) return 0;              /* Has no WHERE clause */
71968
 
  pSrc = p->pSrc;
71969
 
  assert( pSrc!=0 );
71970
 
  if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
71971
 
  if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
71972
 
  pTab = pSrc->a[0].pTab;
71973
 
  if( NEVER(pTab==0) ) return 0;
71974
 
  assert( pTab->pSelect==0 );            /* FROM clause is not a view */
71975
 
  if( IsVirtual(pTab) ) return 0;        /* FROM clause not a virtual table */
71976
 
  pEList = p->pEList;
71977
 
  if( pEList->nExpr!=1 ) return 0;       /* One column in the result set */
71978
 
  if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
71979
 
  return 1;
71980
 
}
71981
 
#endif /* SQLITE_OMIT_SUBQUERY */
71982
 
 
71983
 
/*
71984
 
** This function is used by the implementation of the IN (...) operator.
71985
 
** It's job is to find or create a b-tree structure that may be used
71986
 
** either to test for membership of the (...) set or to iterate through
71987
 
** its members, skipping duplicates.
71988
 
**
71989
 
** The index of the cursor opened on the b-tree (database table, database index 
71990
 
** or ephermal table) is stored in pX->iTable before this function returns.
71991
 
** The returned value of this function indicates the b-tree type, as follows:
71992
 
**
71993
 
**   IN_INDEX_ROWID - The cursor was opened on a database table.
71994
 
**   IN_INDEX_INDEX - The cursor was opened on a database index.
71995
 
**   IN_INDEX_EPH -   The cursor was opened on a specially created and
71996
 
**                    populated epheremal table.
71997
 
**
71998
 
** An existing b-tree may only be used if the SELECT is of the simple
71999
 
** form:
72000
 
**
72001
 
**     SELECT <column> FROM <table>
72002
 
**
72003
 
** If the prNotFound parameter is 0, then the b-tree will be used to iterate
72004
 
** through the set members, skipping any duplicates. In this case an
72005
 
** epheremal table must be used unless the selected <column> is guaranteed
72006
 
** to be unique - either because it is an INTEGER PRIMARY KEY or it
72007
 
** has a UNIQUE constraint or UNIQUE index.
72008
 
**
72009
 
** If the prNotFound parameter is not 0, then the b-tree will be used 
72010
 
** for fast set membership tests. In this case an epheremal table must 
72011
 
** be used unless <column> is an INTEGER PRIMARY KEY or an index can 
72012
 
** be found with <column> as its left-most column.
72013
 
**
72014
 
** When the b-tree is being used for membership tests, the calling function
72015
 
** needs to know whether or not the structure contains an SQL NULL 
72016
 
** value in order to correctly evaluate expressions like "X IN (Y, Z)".
72017
 
** If there is any chance that the (...) might contain a NULL value at
72018
 
** runtime, then a register is allocated and the register number written
72019
 
** to *prNotFound. If there is no chance that the (...) contains a
72020
 
** NULL value, then *prNotFound is left unchanged.
72021
 
**
72022
 
** If a register is allocated and its location stored in *prNotFound, then
72023
 
** its initial value is NULL.  If the (...) does not remain constant
72024
 
** for the duration of the query (i.e. the SELECT within the (...)
72025
 
** is a correlated subquery) then the value of the allocated register is
72026
 
** reset to NULL each time the subquery is rerun. This allows the
72027
 
** caller to use vdbe code equivalent to the following:
72028
 
**
72029
 
**   if( register==NULL ){
72030
 
**     has_null = <test if data structure contains null>
72031
 
**     register = 1
72032
 
**   }
72033
 
**
72034
 
** in order to avoid running the <test if data structure contains null>
72035
 
** test more often than is necessary.
72036
 
*/
72037
 
#ifndef SQLITE_OMIT_SUBQUERY
72038
 
SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
72039
 
  Select *p;                            /* SELECT to the right of IN operator */
72040
 
  int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
72041
 
  int iTab = pParse->nTab++;            /* Cursor of the RHS table */
72042
 
  int mustBeUnique = (prNotFound==0);   /* True if RHS must be unique */
72043
 
 
72044
 
  assert( pX->op==TK_IN );
72045
 
 
72046
 
  /* Check to see if an existing table or index can be used to
72047
 
  ** satisfy the query.  This is preferable to generating a new 
72048
 
  ** ephemeral table.
72049
 
  */
72050
 
  p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
72051
 
  if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
72052
 
    sqlite3 *db = pParse->db;              /* Database connection */
72053
 
    Expr *pExpr = p->pEList->a[0].pExpr;   /* Expression <column> */
72054
 
    int iCol = pExpr->iColumn;             /* Index of column <column> */
72055
 
    Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
72056
 
    Table *pTab = p->pSrc->a[0].pTab;      /* Table <table>. */
72057
 
    int iDb;                               /* Database idx for pTab */
72058
 
   
72059
 
    /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
72060
 
    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
72061
 
    sqlite3CodeVerifySchema(pParse, iDb);
72062
 
    sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
72063
 
 
72064
 
    /* This function is only called from two places. In both cases the vdbe
72065
 
    ** has already been allocated. So assume sqlite3GetVdbe() is always
72066
 
    ** successful here.
72067
 
    */
72068
 
    assert(v);
72069
 
    if( iCol<0 ){
72070
 
      int iMem = ++pParse->nMem;
72071
 
      int iAddr;
72072
 
 
72073
 
      iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
72074
 
      sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
72075
 
 
72076
 
      sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
72077
 
      eType = IN_INDEX_ROWID;
72078
 
 
72079
 
      sqlite3VdbeJumpHere(v, iAddr);
72080
 
    }else{
72081
 
      Index *pIdx;                         /* Iterator variable */
72082
 
 
72083
 
      /* The collation sequence used by the comparison. If an index is to
72084
 
      ** be used in place of a temp-table, it must be ordered according
72085
 
      ** to this collation sequence.  */
72086
 
      CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
72087
 
 
72088
 
      /* Check that the affinity that will be used to perform the 
72089
 
      ** comparison is the same as the affinity of the column. If
72090
 
      ** it is not, it is not possible to use any index.
72091
 
      */
72092
 
      char aff = comparisonAffinity(pX);
72093
 
      int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
72094
 
 
72095
 
      for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
72096
 
        if( (pIdx->aiColumn[0]==iCol)
72097
 
         && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
72098
 
         && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
72099
 
        ){
72100
 
          int iMem = ++pParse->nMem;
72101
 
          int iAddr;
72102
 
          char *pKey;
72103
 
  
72104
 
          pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
72105
 
          iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
72106
 
          sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
72107
 
  
72108
 
          sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
72109
 
                               pKey,P4_KEYINFO_HANDOFF);
72110
 
          VdbeComment((v, "%s", pIdx->zName));
72111
 
          eType = IN_INDEX_INDEX;
72112
 
 
72113
 
          sqlite3VdbeJumpHere(v, iAddr);
72114
 
          if( prNotFound && !pTab->aCol[iCol].notNull ){
72115
 
            *prNotFound = ++pParse->nMem;
72116
 
          }
72117
 
        }
72118
 
      }
72119
 
    }
72120
 
  }
72121
 
 
72122
 
  if( eType==0 ){
72123
 
    /* Could not found an existing table or index to use as the RHS b-tree.
72124
 
    ** We will have to generate an ephemeral table to do the job.
72125
 
    */
72126
 
    double savedNQueryLoop = pParse->nQueryLoop;
72127
 
    int rMayHaveNull = 0;
72128
 
    eType = IN_INDEX_EPH;
72129
 
    if( prNotFound ){
72130
 
      *prNotFound = rMayHaveNull = ++pParse->nMem;
72131
 
    }else{
72132
 
      testcase( pParse->nQueryLoop>(double)1 );
72133
 
      pParse->nQueryLoop = (double)1;
72134
 
      if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){
72135
 
        eType = IN_INDEX_ROWID;
72136
 
      }
72137
 
    }
72138
 
    sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
72139
 
    pParse->nQueryLoop = savedNQueryLoop;
72140
 
  }else{
72141
 
    pX->iTable = iTab;
72142
 
  }
72143
 
  return eType;
72144
 
}
72145
 
#endif
72146
 
 
72147
 
/*
72148
 
** Generate code for scalar subqueries used as a subquery expression, EXISTS,
72149
 
** or IN operators.  Examples:
72150
 
**
72151
 
**     (SELECT a FROM b)          -- subquery
72152
 
**     EXISTS (SELECT a FROM b)   -- EXISTS subquery
72153
 
**     x IN (4,5,11)              -- IN operator with list on right-hand side
72154
 
**     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
72155
 
**
72156
 
** The pExpr parameter describes the expression that contains the IN
72157
 
** operator or subquery.
72158
 
**
72159
 
** If parameter isRowid is non-zero, then expression pExpr is guaranteed
72160
 
** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
72161
 
** to some integer key column of a table B-Tree. In this case, use an
72162
 
** intkey B-Tree to store the set of IN(...) values instead of the usual
72163
 
** (slower) variable length keys B-Tree.
72164
 
**
72165
 
** If rMayHaveNull is non-zero, that means that the operation is an IN
72166
 
** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
72167
 
** Furthermore, the IN is in a WHERE clause and that we really want
72168
 
** to iterate over the RHS of the IN operator in order to quickly locate
72169
 
** all corresponding LHS elements.  All this routine does is initialize
72170
 
** the register given by rMayHaveNull to NULL.  Calling routines will take
72171
 
** care of changing this register value to non-NULL if the RHS is NULL-free.
72172
 
**
72173
 
** If rMayHaveNull is zero, that means that the subquery is being used
72174
 
** for membership testing only.  There is no need to initialize any
72175
 
** registers to indicate the presense or absence of NULLs on the RHS.
72176
 
**
72177
 
** For a SELECT or EXISTS operator, return the register that holds the
72178
 
** result.  For IN operators or if an error occurs, the return value is 0.
72179
 
*/
72180
 
#ifndef SQLITE_OMIT_SUBQUERY
72181
 
SQLITE_PRIVATE int sqlite3CodeSubselect(
72182
 
  Parse *pParse,          /* Parsing context */
72183
 
  Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
72184
 
  int rMayHaveNull,       /* Register that records whether NULLs exist in RHS */
72185
 
  int isRowid             /* If true, LHS of IN operator is a rowid */
72186
 
){
72187
 
  int testAddr = 0;                       /* One-time test address */
72188
 
  int rReg = 0;                           /* Register storing resulting */
72189
 
  Vdbe *v = sqlite3GetVdbe(pParse);
72190
 
  if( NEVER(v==0) ) return 0;
72191
 
  sqlite3ExprCachePush(pParse);
72192
 
 
72193
 
  /* This code must be run in its entirety every time it is encountered
72194
 
  ** if any of the following is true:
72195
 
  **
72196
 
  **    *  The right-hand side is a correlated subquery
72197
 
  **    *  The right-hand side is an expression list containing variables
72198
 
  **    *  We are inside a trigger
72199
 
  **
72200
 
  ** If all of the above are false, then we can run this code just once
72201
 
  ** save the results, and reuse the same result on subsequent invocations.
72202
 
  */
72203
 
  if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->pTriggerTab ){
72204
 
    int mem = ++pParse->nMem;
72205
 
    sqlite3VdbeAddOp1(v, OP_If, mem);
72206
 
    testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
72207
 
    assert( testAddr>0 || pParse->db->mallocFailed );
72208
 
  }
72209
 
 
72210
 
#ifndef SQLITE_OMIT_EXPLAIN
72211
 
  if( pParse->explain==2 ){
72212
 
    char *zMsg = sqlite3MPrintf(
72213
 
        pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr?"":"CORRELATED ",
72214
 
        pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
72215
 
    );
72216
 
    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
72217
 
  }
72218
 
#endif
72219
 
 
72220
 
  switch( pExpr->op ){
72221
 
    case TK_IN: {
72222
 
      char affinity;              /* Affinity of the LHS of the IN */
72223
 
      KeyInfo keyInfo;            /* Keyinfo for the generated table */
72224
 
      int addr;                   /* Address of OP_OpenEphemeral instruction */
72225
 
      Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
72226
 
 
72227
 
      if( rMayHaveNull ){
72228
 
        sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
72229
 
      }
72230
 
 
72231
 
      affinity = sqlite3ExprAffinity(pLeft);
72232
 
 
72233
 
      /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
72234
 
      ** expression it is handled the same way.  An ephemeral table is 
72235
 
      ** filled with single-field index keys representing the results
72236
 
      ** from the SELECT or the <exprlist>.
72237
 
      **
72238
 
      ** If the 'x' expression is a column value, or the SELECT...
72239
 
      ** statement returns a column value, then the affinity of that
72240
 
      ** column is used to build the index keys. If both 'x' and the
72241
 
      ** SELECT... statement are columns, then numeric affinity is used
72242
 
      ** if either column has NUMERIC or INTEGER affinity. If neither
72243
 
      ** 'x' nor the SELECT... statement are columns, then numeric affinity
72244
 
      ** is used.
72245
 
      */
72246
 
      pExpr->iTable = pParse->nTab++;
72247
 
      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
72248
 
      if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
72249
 
      memset(&keyInfo, 0, sizeof(keyInfo));
72250
 
      keyInfo.nField = 1;
72251
 
 
72252
 
      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
72253
 
        /* Case 1:     expr IN (SELECT ...)
72254
 
        **
72255
 
        ** Generate code to write the results of the select into the temporary
72256
 
        ** table allocated and opened above.
72257
 
        */
72258
 
        SelectDest dest;
72259
 
        ExprList *pEList;
72260
 
 
72261
 
        assert( !isRowid );
72262
 
        sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
72263
 
        dest.affinity = (u8)affinity;
72264
 
        assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
72265
 
        pExpr->x.pSelect->iLimit = 0;
72266
 
        if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
72267
 
          return 0;
72268
 
        }
72269
 
        pEList = pExpr->x.pSelect->pEList;
72270
 
        if( ALWAYS(pEList!=0 && pEList->nExpr>0) ){ 
72271
 
          keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
72272
 
              pEList->a[0].pExpr);
72273
 
        }
72274
 
      }else if( ALWAYS(pExpr->x.pList!=0) ){
72275
 
        /* Case 2:     expr IN (exprlist)
72276
 
        **
72277
 
        ** For each expression, build an index key from the evaluation and
72278
 
        ** store it in the temporary table. If <expr> is a column, then use
72279
 
        ** that columns affinity when building index keys. If <expr> is not
72280
 
        ** a column, use numeric affinity.
72281
 
        */
72282
 
        int i;
72283
 
        ExprList *pList = pExpr->x.pList;
72284
 
        struct ExprList_item *pItem;
72285
 
        int r1, r2, r3;
72286
 
 
72287
 
        if( !affinity ){
72288
 
          affinity = SQLITE_AFF_NONE;
72289
 
        }
72290
 
        keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
72291
 
 
72292
 
        /* Loop through each expression in <exprlist>. */
72293
 
        r1 = sqlite3GetTempReg(pParse);
72294
 
        r2 = sqlite3GetTempReg(pParse);
72295
 
        sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
72296
 
        for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
72297
 
          Expr *pE2 = pItem->pExpr;
72298
 
          int iValToIns;
72299
 
 
72300
 
          /* If the expression is not constant then we will need to
72301
 
          ** disable the test that was generated above that makes sure
72302
 
          ** this code only executes once.  Because for a non-constant
72303
 
          ** expression we need to rerun this code each time.
72304
 
          */
72305
 
          if( testAddr && !sqlite3ExprIsConstant(pE2) ){
72306
 
            sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
72307
 
            testAddr = 0;
72308
 
          }
72309
 
 
72310
 
          /* Evaluate the expression and insert it into the temp table */
72311
 
          if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
72312
 
            sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
72313
 
          }else{
72314
 
            r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
72315
 
            if( isRowid ){
72316
 
              sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
72317
 
                                sqlite3VdbeCurrentAddr(v)+2);
72318
 
              sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
72319
 
            }else{
72320
 
              sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
72321
 
              sqlite3ExprCacheAffinityChange(pParse, r3, 1);
72322
 
              sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
72323
 
            }
72324
 
          }
72325
 
        }
72326
 
        sqlite3ReleaseTempReg(pParse, r1);
72327
 
        sqlite3ReleaseTempReg(pParse, r2);
72328
 
      }
72329
 
      if( !isRowid ){
72330
 
        sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
72331
 
      }
72332
 
      break;
72333
 
    }
72334
 
 
72335
 
    case TK_EXISTS:
72336
 
    case TK_SELECT:
72337
 
    default: {
72338
 
      /* If this has to be a scalar SELECT.  Generate code to put the
72339
 
      ** value of this select in a memory cell and record the number
72340
 
      ** of the memory cell in iColumn.  If this is an EXISTS, write
72341
 
      ** an integer 0 (not exists) or 1 (exists) into a memory cell
72342
 
      ** and record that memory cell in iColumn.
72343
 
      */
72344
 
      Select *pSel;                         /* SELECT statement to encode */
72345
 
      SelectDest dest;                      /* How to deal with SELECt result */
72346
 
 
72347
 
      testcase( pExpr->op==TK_EXISTS );
72348
 
      testcase( pExpr->op==TK_SELECT );
72349
 
      assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
72350
 
 
72351
 
      assert( ExprHasProperty(pExpr, EP_xIsSelect) );
72352
 
      pSel = pExpr->x.pSelect;
72353
 
      sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
72354
 
      if( pExpr->op==TK_SELECT ){
72355
 
        dest.eDest = SRT_Mem;
72356
 
        sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
72357
 
        VdbeComment((v, "Init subquery result"));
72358
 
      }else{
72359
 
        dest.eDest = SRT_Exists;
72360
 
        sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
72361
 
        VdbeComment((v, "Init EXISTS result"));
72362
 
      }
72363
 
      sqlite3ExprDelete(pParse->db, pSel->pLimit);
72364
 
      pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
72365
 
                                  &sqlite3IntTokens[1]);
72366
 
      pSel->iLimit = 0;
72367
 
      if( sqlite3Select(pParse, pSel, &dest) ){
72368
 
        return 0;
72369
 
      }
72370
 
      rReg = dest.iParm;
72371
 
      ExprSetIrreducible(pExpr);
72372
 
      break;
72373
 
    }
72374
 
  }
72375
 
 
72376
 
  if( testAddr ){
72377
 
    sqlite3VdbeJumpHere(v, testAddr-1);
72378
 
  }
72379
 
  sqlite3ExprCachePop(pParse, 1);
72380
 
 
72381
 
  return rReg;
72382
 
}
72383
 
#endif /* SQLITE_OMIT_SUBQUERY */
72384
 
 
72385
 
#ifndef SQLITE_OMIT_SUBQUERY
72386
 
/*
72387
 
** Generate code for an IN expression.
72388
 
**
72389
 
**      x IN (SELECT ...)
72390
 
**      x IN (value, value, ...)
72391
 
**
72392
 
** The left-hand side (LHS) is a scalar expression.  The right-hand side (RHS)
72393
 
** is an array of zero or more values.  The expression is true if the LHS is
72394
 
** contained within the RHS.  The value of the expression is unknown (NULL)
72395
 
** if the LHS is NULL or if the LHS is not contained within the RHS and the
72396
 
** RHS contains one or more NULL values.
72397
 
**
72398
 
** This routine generates code will jump to destIfFalse if the LHS is not 
72399
 
** contained within the RHS.  If due to NULLs we cannot determine if the LHS
72400
 
** is contained in the RHS then jump to destIfNull.  If the LHS is contained
72401
 
** within the RHS then fall through.
72402
 
*/
72403
 
static void sqlite3ExprCodeIN(
72404
 
  Parse *pParse,        /* Parsing and code generating context */
72405
 
  Expr *pExpr,          /* The IN expression */
72406
 
  int destIfFalse,      /* Jump here if LHS is not contained in the RHS */
72407
 
  int destIfNull        /* Jump here if the results are unknown due to NULLs */
72408
 
){
72409
 
  int rRhsHasNull = 0;  /* Register that is true if RHS contains NULL values */
72410
 
  char affinity;        /* Comparison affinity to use */
72411
 
  int eType;            /* Type of the RHS */
72412
 
  int r1;               /* Temporary use register */
72413
 
  Vdbe *v;              /* Statement under construction */
72414
 
 
72415
 
  /* Compute the RHS.   After this step, the table with cursor
72416
 
  ** pExpr->iTable will contains the values that make up the RHS.
72417
 
  */
72418
 
  v = pParse->pVdbe;
72419
 
  assert( v!=0 );       /* OOM detected prior to this routine */
72420
 
  VdbeNoopComment((v, "begin IN expr"));
72421
 
  eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull);
72422
 
 
72423
 
  /* Figure out the affinity to use to create a key from the results
72424
 
  ** of the expression. affinityStr stores a static string suitable for
72425
 
  ** P4 of OP_MakeRecord.
72426
 
  */
72427
 
  affinity = comparisonAffinity(pExpr);
72428
 
 
72429
 
  /* Code the LHS, the <expr> from "<expr> IN (...)".
72430
 
  */
72431
 
  sqlite3ExprCachePush(pParse);
72432
 
  r1 = sqlite3GetTempReg(pParse);
72433
 
  sqlite3ExprCode(pParse, pExpr->pLeft, r1);
72434
 
 
72435
 
  /* If the LHS is NULL, then the result is either false or NULL depending
72436
 
  ** on whether the RHS is empty or not, respectively.
72437
 
  */
72438
 
  if( destIfNull==destIfFalse ){
72439
 
    /* Shortcut for the common case where the false and NULL outcomes are
72440
 
    ** the same. */
72441
 
    sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull);
72442
 
  }else{
72443
 
    int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
72444
 
    sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
72445
 
    sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
72446
 
    sqlite3VdbeJumpHere(v, addr1);
72447
 
  }
72448
 
 
72449
 
  if( eType==IN_INDEX_ROWID ){
72450
 
    /* In this case, the RHS is the ROWID of table b-tree
72451
 
    */
72452
 
    sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse);
72453
 
    sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
72454
 
  }else{
72455
 
    /* In this case, the RHS is an index b-tree.
72456
 
    */
72457
 
    sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
72458
 
 
72459
 
    /* If the set membership test fails, then the result of the 
72460
 
    ** "x IN (...)" expression must be either 0 or NULL. If the set
72461
 
    ** contains no NULL values, then the result is 0. If the set 
72462
 
    ** contains one or more NULL values, then the result of the
72463
 
    ** expression is also NULL.
72464
 
    */
72465
 
    if( rRhsHasNull==0 || destIfFalse==destIfNull ){
72466
 
      /* This branch runs if it is known at compile time that the RHS
72467
 
      ** cannot contain NULL values. This happens as the result
72468
 
      ** of a "NOT NULL" constraint in the database schema.
72469
 
      **
72470
 
      ** Also run this branch if NULL is equivalent to FALSE
72471
 
      ** for this particular IN operator.
72472
 
      */
72473
 
      sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
72474
 
 
72475
 
    }else{
72476
 
      /* In this branch, the RHS of the IN might contain a NULL and
72477
 
      ** the presence of a NULL on the RHS makes a difference in the
72478
 
      ** outcome.
72479
 
      */
72480
 
      int j1, j2, j3;
72481
 
 
72482
 
      /* First check to see if the LHS is contained in the RHS.  If so,
72483
 
      ** then the presence of NULLs in the RHS does not matter, so jump
72484
 
      ** over all of the code that follows.
72485
 
      */
72486
 
      j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
72487
 
 
72488
 
      /* Here we begin generating code that runs if the LHS is not
72489
 
      ** contained within the RHS.  Generate additional code that
72490
 
      ** tests the RHS for NULLs.  If the RHS contains a NULL then
72491
 
      ** jump to destIfNull.  If there are no NULLs in the RHS then
72492
 
      ** jump to destIfFalse.
72493
 
      */
72494
 
      j2 = sqlite3VdbeAddOp1(v, OP_NotNull, rRhsHasNull);
72495
 
      j3 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
72496
 
      sqlite3VdbeAddOp2(v, OP_Integer, -1, rRhsHasNull);
72497
 
      sqlite3VdbeJumpHere(v, j3);
72498
 
      sqlite3VdbeAddOp2(v, OP_AddImm, rRhsHasNull, 1);
72499
 
      sqlite3VdbeJumpHere(v, j2);
72500
 
 
72501
 
      /* Jump to the appropriate target depending on whether or not
72502
 
      ** the RHS contains a NULL
72503
 
      */
72504
 
      sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull);
72505
 
      sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
72506
 
 
72507
 
      /* The OP_Found at the top of this branch jumps here when true, 
72508
 
      ** causing the overall IN expression evaluation to fall through.
72509
 
      */
72510
 
      sqlite3VdbeJumpHere(v, j1);
72511
 
    }
72512
 
  }
72513
 
  sqlite3ReleaseTempReg(pParse, r1);
72514
 
  sqlite3ExprCachePop(pParse, 1);
72515
 
  VdbeComment((v, "end IN expr"));
72516
 
}
72517
 
#endif /* SQLITE_OMIT_SUBQUERY */
72518
 
 
72519
 
/*
72520
 
** Duplicate an 8-byte value
72521
 
*/
72522
 
static char *dup8bytes(Vdbe *v, const char *in){
72523
 
  char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
72524
 
  if( out ){
72525
 
    memcpy(out, in, 8);
72526
 
  }
72527
 
  return out;
72528
 
}
72529
 
 
72530
 
#ifndef SQLITE_OMIT_FLOATING_POINT
72531
 
/*
72532
 
** Generate an instruction that will put the floating point
72533
 
** value described by z[0..n-1] into register iMem.
72534
 
**
72535
 
** The z[] string will probably not be zero-terminated.  But the 
72536
 
** z[n] character is guaranteed to be something that does not look
72537
 
** like the continuation of the number.
72538
 
*/
72539
 
static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
72540
 
  if( ALWAYS(z!=0) ){
72541
 
    double value;
72542
 
    char *zV;
72543
 
    sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
72544
 
    assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
72545
 
    if( negateFlag ) value = -value;
72546
 
    zV = dup8bytes(v, (char*)&value);
72547
 
    sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
72548
 
  }
72549
 
}
72550
 
#endif
72551
 
 
72552
 
 
72553
 
/*
72554
 
** Generate an instruction that will put the integer describe by
72555
 
** text z[0..n-1] into register iMem.
72556
 
**
72557
 
** Expr.u.zToken is always UTF8 and zero-terminated.
72558
 
*/
72559
 
static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
72560
 
  Vdbe *v = pParse->pVdbe;
72561
 
  if( pExpr->flags & EP_IntValue ){
72562
 
    int i = pExpr->u.iValue;
72563
 
    assert( i>=0 );
72564
 
    if( negFlag ) i = -i;
72565
 
    sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
72566
 
  }else{
72567
 
    int c;
72568
 
    i64 value;
72569
 
    const char *z = pExpr->u.zToken;
72570
 
    assert( z!=0 );
72571
 
    c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
72572
 
    if( c==0 || (c==2 && negFlag) ){
72573
 
      char *zV;
72574
 
      if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
72575
 
      zV = dup8bytes(v, (char*)&value);
72576
 
      sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
72577
 
    }else{
72578
 
#ifdef SQLITE_OMIT_FLOATING_POINT
72579
 
      sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
72580
 
#else
72581
 
      codeReal(v, z, negFlag, iMem);
72582
 
#endif
72583
 
    }
72584
 
  }
72585
 
}
72586
 
 
72587
 
/*
72588
 
** Clear a cache entry.
72589
 
*/
72590
 
static void cacheEntryClear(Parse *pParse, struct yColCache *p){
72591
 
  if( p->tempReg ){
72592
 
    if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
72593
 
      pParse->aTempReg[pParse->nTempReg++] = p->iReg;
72594
 
    }
72595
 
    p->tempReg = 0;
72596
 
  }
72597
 
}
72598
 
 
72599
 
 
72600
 
/*
72601
 
** Record in the column cache that a particular column from a
72602
 
** particular table is stored in a particular register.
72603
 
*/
72604
 
SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
72605
 
  int i;
72606
 
  int minLru;
72607
 
  int idxLru;
72608
 
  struct yColCache *p;
72609
 
 
72610
 
  assert( iReg>0 );  /* Register numbers are always positive */
72611
 
  assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
72612
 
 
72613
 
  /* The SQLITE_ColumnCache flag disables the column cache.  This is used
72614
 
  ** for testing only - to verify that SQLite always gets the same answer
72615
 
  ** with and without the column cache.
72616
 
  */
72617
 
  if( pParse->db->flags & SQLITE_ColumnCache ) return;
72618
 
 
72619
 
  /* First replace any existing entry.
72620
 
  **
72621
 
  ** Actually, the way the column cache is currently used, we are guaranteed
72622
 
  ** that the object will never already be in cache.  Verify this guarantee.
72623
 
  */
72624
 
#ifndef NDEBUG
72625
 
  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
72626
 
#if 0 /* This code wold remove the entry from the cache if it existed */
72627
 
    if( p->iReg && p->iTable==iTab && p->iColumn==iCol ){
72628
 
      cacheEntryClear(pParse, p);
72629
 
      p->iLevel = pParse->iCacheLevel;
72630
 
      p->iReg = iReg;
72631
 
      p->lru = pParse->iCacheCnt++;
72632
 
      return;
72633
 
    }
72634
 
#endif
72635
 
    assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
72636
 
  }
72637
 
#endif
72638
 
 
72639
 
  /* Find an empty slot and replace it */
72640
 
  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
72641
 
    if( p->iReg==0 ){
72642
 
      p->iLevel = pParse->iCacheLevel;
72643
 
      p->iTable = iTab;
72644
 
      p->iColumn = iCol;
72645
 
      p->iReg = iReg;
72646
 
      p->tempReg = 0;
72647
 
      p->lru = pParse->iCacheCnt++;
72648
 
      return;
72649
 
    }
72650
 
  }
72651
 
 
72652
 
  /* Replace the last recently used */
72653
 
  minLru = 0x7fffffff;
72654
 
  idxLru = -1;
72655
 
  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
72656
 
    if( p->lru<minLru ){
72657
 
      idxLru = i;
72658
 
      minLru = p->lru;
72659
 
    }
72660
 
  }
72661
 
  if( ALWAYS(idxLru>=0) ){
72662
 
    p = &pParse->aColCache[idxLru];
72663
 
    p->iLevel = pParse->iCacheLevel;
72664
 
    p->iTable = iTab;
72665
 
    p->iColumn = iCol;
72666
 
    p->iReg = iReg;
72667
 
    p->tempReg = 0;
72668
 
    p->lru = pParse->iCacheCnt++;
72669
 
    return;
72670
 
  }
72671
 
}
72672
 
 
72673
 
/*
72674
 
** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
72675
 
** Purge the range of registers from the column cache.
72676
 
*/
72677
 
SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
72678
 
  int i;
72679
 
  int iLast = iReg + nReg - 1;
72680
 
  struct yColCache *p;
72681
 
  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
72682
 
    int r = p->iReg;
72683
 
    if( r>=iReg && r<=iLast ){
72684
 
      cacheEntryClear(pParse, p);
72685
 
      p->iReg = 0;
72686
 
    }
72687
 
  }
72688
 
}
72689
 
 
72690
 
/*
72691
 
** Remember the current column cache context.  Any new entries added
72692
 
** added to the column cache after this call are removed when the
72693
 
** corresponding pop occurs.
72694
 
*/
72695
 
SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){
72696
 
  pParse->iCacheLevel++;
72697
 
}
72698
 
 
72699
 
/*
72700
 
** Remove from the column cache any entries that were added since the
72701
 
** the previous N Push operations.  In other words, restore the cache
72702
 
** to the state it was in N Pushes ago.
72703
 
*/
72704
 
SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse, int N){
72705
 
  int i;
72706
 
  struct yColCache *p;
72707
 
  assert( N>0 );
72708
 
  assert( pParse->iCacheLevel>=N );
72709
 
  pParse->iCacheLevel -= N;
72710
 
  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
72711
 
    if( p->iReg && p->iLevel>pParse->iCacheLevel ){
72712
 
      cacheEntryClear(pParse, p);
72713
 
      p->iReg = 0;
72714
 
    }
72715
 
  }
72716
 
}
72717
 
 
72718
 
/*
72719
 
** When a cached column is reused, make sure that its register is
72720
 
** no longer available as a temp register.  ticket #3879:  that same
72721
 
** register might be in the cache in multiple places, so be sure to
72722
 
** get them all.
72723
 
*/
72724
 
static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
72725
 
  int i;
72726
 
  struct yColCache *p;
72727
 
  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
72728
 
    if( p->iReg==iReg ){
72729
 
      p->tempReg = 0;
72730
 
    }
72731
 
  }
72732
 
}
72733
 
 
72734
 
/*
72735
 
** Generate code to extract the value of the iCol-th column of a table.
72736
 
*/
72737
 
SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
72738
 
  Vdbe *v,        /* The VDBE under construction */
72739
 
  Table *pTab,    /* The table containing the value */
72740
 
  int iTabCur,    /* The cursor for this table */
72741
 
  int iCol,       /* Index of the column to extract */
72742
 
  int regOut      /* Extract the valud into this register */
72743
 
){
72744
 
  if( iCol<0 || iCol==pTab->iPKey ){
72745
 
    sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
72746
 
  }else{
72747
 
    int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
72748
 
    sqlite3VdbeAddOp3(v, op, iTabCur, iCol, regOut);
72749
 
  }
72750
 
  if( iCol>=0 ){
72751
 
    sqlite3ColumnDefault(v, pTab, iCol, regOut);
72752
 
  }
72753
 
}
72754
 
 
72755
 
/*
72756
 
** Generate code that will extract the iColumn-th column from
72757
 
** table pTab and store the column value in a register.  An effort
72758
 
** is made to store the column value in register iReg, but this is
72759
 
** not guaranteed.  The location of the column value is returned.
72760
 
**
72761
 
** There must be an open cursor to pTab in iTable when this routine
72762
 
** is called.  If iColumn<0 then code is generated that extracts the rowid.
72763
 
*/
72764
 
SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
72765
 
  Parse *pParse,   /* Parsing and code generating context */
72766
 
  Table *pTab,     /* Description of the table we are reading from */
72767
 
  int iColumn,     /* Index of the table column */
72768
 
  int iTable,      /* The cursor pointing to the table */
72769
 
  int iReg         /* Store results here */
72770
 
){
72771
 
  Vdbe *v = pParse->pVdbe;
72772
 
  int i;
72773
 
  struct yColCache *p;
72774
 
 
72775
 
  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
72776
 
    if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
72777
 
      p->lru = pParse->iCacheCnt++;
72778
 
      sqlite3ExprCachePinRegister(pParse, p->iReg);
72779
 
      return p->iReg;
72780
 
    }
72781
 
  }  
72782
 
  assert( v!=0 );
72783
 
  sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
72784
 
  sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
72785
 
  return iReg;
72786
 
}
72787
 
 
72788
 
/*
72789
 
** Clear all column cache entries.
72790
 
*/
72791
 
SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
72792
 
  int i;
72793
 
  struct yColCache *p;
72794
 
 
72795
 
  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
72796
 
    if( p->iReg ){
72797
 
      cacheEntryClear(pParse, p);
72798
 
      p->iReg = 0;
72799
 
    }
72800
 
  }
72801
 
}
72802
 
 
72803
 
/*
72804
 
** Record the fact that an affinity change has occurred on iCount
72805
 
** registers starting with iStart.
72806
 
*/
72807
 
SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
72808
 
  sqlite3ExprCacheRemove(pParse, iStart, iCount);
72809
 
}
72810
 
 
72811
 
/*
72812
 
** Generate code to move content from registers iFrom...iFrom+nReg-1
72813
 
** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
72814
 
*/
72815
 
SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
72816
 
  int i;
72817
 
  struct yColCache *p;
72818
 
  if( NEVER(iFrom==iTo) ) return;
72819
 
  sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
72820
 
  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
72821
 
    int x = p->iReg;
72822
 
    if( x>=iFrom && x<iFrom+nReg ){
72823
 
      p->iReg += iTo-iFrom;
72824
 
    }
72825
 
  }
72826
 
}
72827
 
 
72828
 
/*
72829
 
** Generate code to copy content from registers iFrom...iFrom+nReg-1
72830
 
** over to iTo..iTo+nReg-1.
72831
 
*/
72832
 
SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
72833
 
  int i;
72834
 
  if( NEVER(iFrom==iTo) ) return;
72835
 
  for(i=0; i<nReg; i++){
72836
 
    sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
72837
 
  }
72838
 
}
72839
 
 
72840
 
#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
72841
 
/*
72842
 
** Return true if any register in the range iFrom..iTo (inclusive)
72843
 
** is used as part of the column cache.
72844
 
**
72845
 
** This routine is used within assert() and testcase() macros only
72846
 
** and does not appear in a normal build.
72847
 
*/
72848
 
static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
72849
 
  int i;
72850
 
  struct yColCache *p;
72851
 
  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
72852
 
    int r = p->iReg;
72853
 
    if( r>=iFrom && r<=iTo ) return 1;    /*NO_TEST*/
72854
 
  }
72855
 
  return 0;
72856
 
}
72857
 
#endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
72858
 
 
72859
 
/*
72860
 
** Generate code into the current Vdbe to evaluate the given
72861
 
** expression.  Attempt to store the results in register "target".
72862
 
** Return the register where results are stored.
72863
 
**
72864
 
** With this routine, there is no guarantee that results will
72865
 
** be stored in target.  The result might be stored in some other
72866
 
** register if it is convenient to do so.  The calling function
72867
 
** must check the return code and move the results to the desired
72868
 
** register.
72869
 
*/
72870
 
SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
72871
 
  Vdbe *v = pParse->pVdbe;  /* The VM under construction */
72872
 
  int op;                   /* The opcode being coded */
72873
 
  int inReg = target;       /* Results stored in register inReg */
72874
 
  int regFree1 = 0;         /* If non-zero free this temporary register */
72875
 
  int regFree2 = 0;         /* If non-zero free this temporary register */
72876
 
  int r1, r2, r3, r4;       /* Various register numbers */
72877
 
  sqlite3 *db = pParse->db; /* The database connection */
72878
 
 
72879
 
  assert( target>0 && target<=pParse->nMem );
72880
 
  if( v==0 ){
72881
 
    assert( pParse->db->mallocFailed );
72882
 
    return 0;
72883
 
  }
72884
 
 
72885
 
  if( pExpr==0 ){
72886
 
    op = TK_NULL;
72887
 
  }else{
72888
 
    op = pExpr->op;
72889
 
  }
72890
 
  switch( op ){
72891
 
    case TK_AGG_COLUMN: {
72892
 
      AggInfo *pAggInfo = pExpr->pAggInfo;
72893
 
      struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
72894
 
      if( !pAggInfo->directMode ){
72895
 
        assert( pCol->iMem>0 );
72896
 
        inReg = pCol->iMem;
72897
 
        break;
72898
 
      }else if( pAggInfo->useSortingIdx ){
72899
 
        sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
72900
 
                              pCol->iSorterColumn, target);
72901
 
        break;
72902
 
      }
72903
 
      /* Otherwise, fall thru into the TK_COLUMN case */
72904
 
    }
72905
 
    case TK_COLUMN: {
72906
 
      if( pExpr->iTable<0 ){
72907
 
        /* This only happens when coding check constraints */
72908
 
        assert( pParse->ckBase>0 );
72909
 
        inReg = pExpr->iColumn + pParse->ckBase;
72910
 
      }else{
72911
 
        inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
72912
 
                                 pExpr->iColumn, pExpr->iTable, target);
72913
 
      }
72914
 
      break;
72915
 
    }
72916
 
    case TK_INTEGER: {
72917
 
      codeInteger(pParse, pExpr, 0, target);
72918
 
      break;
72919
 
    }
72920
 
#ifndef SQLITE_OMIT_FLOATING_POINT
72921
 
    case TK_FLOAT: {
72922
 
      assert( !ExprHasProperty(pExpr, EP_IntValue) );
72923
 
      codeReal(v, pExpr->u.zToken, 0, target);
72924
 
      break;
72925
 
    }
72926
 
#endif
72927
 
    case TK_STRING: {
72928
 
      assert( !ExprHasProperty(pExpr, EP_IntValue) );
72929
 
      sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0);
72930
 
      break;
72931
 
    }
72932
 
    case TK_NULL: {
72933
 
      sqlite3VdbeAddOp2(v, OP_Null, 0, target);
72934
 
      break;
72935
 
    }
72936
 
#ifndef SQLITE_OMIT_BLOB_LITERAL
72937
 
    case TK_BLOB: {
72938
 
      int n;
72939
 
      const char *z;
72940
 
      char *zBlob;
72941
 
      assert( !ExprHasProperty(pExpr, EP_IntValue) );
72942
 
      assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
72943
 
      assert( pExpr->u.zToken[1]=='\'' );
72944
 
      z = &pExpr->u.zToken[2];
72945
 
      n = sqlite3Strlen30(z) - 1;
72946
 
      assert( z[n]=='\'' );
72947
 
      zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
72948
 
      sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
72949
 
      break;
72950
 
    }
72951
 
#endif
72952
 
    case TK_VARIABLE: {
72953
 
      assert( !ExprHasProperty(pExpr, EP_IntValue) );
72954
 
      assert( pExpr->u.zToken!=0 );
72955
 
      assert( pExpr->u.zToken[0]!=0 );
72956
 
      sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
72957
 
      if( pExpr->u.zToken[1]!=0 ){
72958
 
        sqlite3VdbeChangeP4(v, -1, pExpr->u.zToken, P4_TRANSIENT);
72959
 
      }
72960
 
      break;
72961
 
    }
72962
 
    case TK_REGISTER: {
72963
 
      inReg = pExpr->iTable;
72964
 
      break;
72965
 
    }
72966
 
    case TK_AS: {
72967
 
      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
72968
 
      break;
72969
 
    }
72970
 
#ifndef SQLITE_OMIT_CAST
72971
 
    case TK_CAST: {
72972
 
      /* Expressions of the form:   CAST(pLeft AS token) */
72973
 
      int aff, to_op;
72974
 
      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
72975
 
      assert( !ExprHasProperty(pExpr, EP_IntValue) );
72976
 
      aff = sqlite3AffinityType(pExpr->u.zToken);
72977
 
      to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
72978
 
      assert( to_op==OP_ToText    || aff!=SQLITE_AFF_TEXT    );
72979
 
      assert( to_op==OP_ToBlob    || aff!=SQLITE_AFF_NONE    );
72980
 
      assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
72981
 
      assert( to_op==OP_ToInt     || aff!=SQLITE_AFF_INTEGER );
72982
 
      assert( to_op==OP_ToReal    || aff!=SQLITE_AFF_REAL    );
72983
 
      testcase( to_op==OP_ToText );
72984
 
      testcase( to_op==OP_ToBlob );
72985
 
      testcase( to_op==OP_ToNumeric );
72986
 
      testcase( to_op==OP_ToInt );
72987
 
      testcase( to_op==OP_ToReal );
72988
 
      if( inReg!=target ){
72989
 
        sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
72990
 
        inReg = target;
72991
 
      }
72992
 
      sqlite3VdbeAddOp1(v, to_op, inReg);
72993
 
      testcase( usedAsColumnCache(pParse, inReg, inReg) );
72994
 
      sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
72995
 
      break;
72996
 
    }
72997
 
#endif /* SQLITE_OMIT_CAST */
72998
 
    case TK_LT:
72999
 
    case TK_LE:
73000
 
    case TK_GT:
73001
 
    case TK_GE:
73002
 
    case TK_NE:
73003
 
    case TK_EQ: {
73004
 
      assert( TK_LT==OP_Lt );
73005
 
      assert( TK_LE==OP_Le );
73006
 
      assert( TK_GT==OP_Gt );
73007
 
      assert( TK_GE==OP_Ge );
73008
 
      assert( TK_EQ==OP_Eq );
73009
 
      assert( TK_NE==OP_Ne );
73010
 
      testcase( op==TK_LT );
73011
 
      testcase( op==TK_LE );
73012
 
      testcase( op==TK_GT );
73013
 
      testcase( op==TK_GE );
73014
 
      testcase( op==TK_EQ );
73015
 
      testcase( op==TK_NE );
73016
 
      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73017
 
      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
73018
 
      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
73019
 
                  r1, r2, inReg, SQLITE_STOREP2);
73020
 
      testcase( regFree1==0 );
73021
 
      testcase( regFree2==0 );
73022
 
      break;
73023
 
    }
73024
 
    case TK_IS:
73025
 
    case TK_ISNOT: {
73026
 
      testcase( op==TK_IS );
73027
 
      testcase( op==TK_ISNOT );
73028
 
      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73029
 
      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
73030
 
      op = (op==TK_IS) ? TK_EQ : TK_NE;
73031
 
      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
73032
 
                  r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
73033
 
      testcase( regFree1==0 );
73034
 
      testcase( regFree2==0 );
73035
 
      break;
73036
 
    }
73037
 
    case TK_AND:
73038
 
    case TK_OR:
73039
 
    case TK_PLUS:
73040
 
    case TK_STAR:
73041
 
    case TK_MINUS:
73042
 
    case TK_REM:
73043
 
    case TK_BITAND:
73044
 
    case TK_BITOR:
73045
 
    case TK_SLASH:
73046
 
    case TK_LSHIFT:
73047
 
    case TK_RSHIFT: 
73048
 
    case TK_CONCAT: {
73049
 
      assert( TK_AND==OP_And );
73050
 
      assert( TK_OR==OP_Or );
73051
 
      assert( TK_PLUS==OP_Add );
73052
 
      assert( TK_MINUS==OP_Subtract );
73053
 
      assert( TK_REM==OP_Remainder );
73054
 
      assert( TK_BITAND==OP_BitAnd );
73055
 
      assert( TK_BITOR==OP_BitOr );
73056
 
      assert( TK_SLASH==OP_Divide );
73057
 
      assert( TK_LSHIFT==OP_ShiftLeft );
73058
 
      assert( TK_RSHIFT==OP_ShiftRight );
73059
 
      assert( TK_CONCAT==OP_Concat );
73060
 
      testcase( op==TK_AND );
73061
 
      testcase( op==TK_OR );
73062
 
      testcase( op==TK_PLUS );
73063
 
      testcase( op==TK_MINUS );
73064
 
      testcase( op==TK_REM );
73065
 
      testcase( op==TK_BITAND );
73066
 
      testcase( op==TK_BITOR );
73067
 
      testcase( op==TK_SLASH );
73068
 
      testcase( op==TK_LSHIFT );
73069
 
      testcase( op==TK_RSHIFT );
73070
 
      testcase( op==TK_CONCAT );
73071
 
      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73072
 
      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
73073
 
      sqlite3VdbeAddOp3(v, op, r2, r1, target);
73074
 
      testcase( regFree1==0 );
73075
 
      testcase( regFree2==0 );
73076
 
      break;
73077
 
    }
73078
 
    case TK_UMINUS: {
73079
 
      Expr *pLeft = pExpr->pLeft;
73080
 
      assert( pLeft );
73081
 
      if( pLeft->op==TK_INTEGER ){
73082
 
        codeInteger(pParse, pLeft, 1, target);
73083
 
#ifndef SQLITE_OMIT_FLOATING_POINT
73084
 
      }else if( pLeft->op==TK_FLOAT ){
73085
 
        assert( !ExprHasProperty(pExpr, EP_IntValue) );
73086
 
        codeReal(v, pLeft->u.zToken, 1, target);
73087
 
#endif
73088
 
      }else{
73089
 
        regFree1 = r1 = sqlite3GetTempReg(pParse);
73090
 
        sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
73091
 
        r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
73092
 
        sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
73093
 
        testcase( regFree2==0 );
73094
 
      }
73095
 
      inReg = target;
73096
 
      break;
73097
 
    }
73098
 
    case TK_BITNOT:
73099
 
    case TK_NOT: {
73100
 
      assert( TK_BITNOT==OP_BitNot );
73101
 
      assert( TK_NOT==OP_Not );
73102
 
      testcase( op==TK_BITNOT );
73103
 
      testcase( op==TK_NOT );
73104
 
      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73105
 
      testcase( regFree1==0 );
73106
 
      inReg = target;
73107
 
      sqlite3VdbeAddOp2(v, op, r1, inReg);
73108
 
      break;
73109
 
    }
73110
 
    case TK_ISNULL:
73111
 
    case TK_NOTNULL: {
73112
 
      int addr;
73113
 
      assert( TK_ISNULL==OP_IsNull );
73114
 
      assert( TK_NOTNULL==OP_NotNull );
73115
 
      testcase( op==TK_ISNULL );
73116
 
      testcase( op==TK_NOTNULL );
73117
 
      sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
73118
 
      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73119
 
      testcase( regFree1==0 );
73120
 
      addr = sqlite3VdbeAddOp1(v, op, r1);
73121
 
      sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
73122
 
      sqlite3VdbeJumpHere(v, addr);
73123
 
      break;
73124
 
    }
73125
 
    case TK_AGG_FUNCTION: {
73126
 
      AggInfo *pInfo = pExpr->pAggInfo;
73127
 
      if( pInfo==0 ){
73128
 
        assert( !ExprHasProperty(pExpr, EP_IntValue) );
73129
 
        sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
73130
 
      }else{
73131
 
        inReg = pInfo->aFunc[pExpr->iAgg].iMem;
73132
 
      }
73133
 
      break;
73134
 
    }
73135
 
    case TK_CONST_FUNC:
73136
 
    case TK_FUNCTION: {
73137
 
      ExprList *pFarg;       /* List of function arguments */
73138
 
      int nFarg;             /* Number of function arguments */
73139
 
      FuncDef *pDef;         /* The function definition object */
73140
 
      int nId;               /* Length of the function name in bytes */
73141
 
      const char *zId;       /* The function name */
73142
 
      int constMask = 0;     /* Mask of function arguments that are constant */
73143
 
      int i;                 /* Loop counter */
73144
 
      u8 enc = ENC(db);      /* The text encoding used by this database */
73145
 
      CollSeq *pColl = 0;    /* A collating sequence */
73146
 
 
73147
 
      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
73148
 
      testcase( op==TK_CONST_FUNC );
73149
 
      testcase( op==TK_FUNCTION );
73150
 
      if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
73151
 
        pFarg = 0;
73152
 
      }else{
73153
 
        pFarg = pExpr->x.pList;
73154
 
      }
73155
 
      nFarg = pFarg ? pFarg->nExpr : 0;
73156
 
      assert( !ExprHasProperty(pExpr, EP_IntValue) );
73157
 
      zId = pExpr->u.zToken;
73158
 
      nId = sqlite3Strlen30(zId);
73159
 
      pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
73160
 
      if( pDef==0 ){
73161
 
        sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
73162
 
        break;
73163
 
      }
73164
 
 
73165
 
      /* Attempt a direct implementation of the built-in COALESCE() and
73166
 
      ** IFNULL() functions.  This avoids unnecessary evalation of
73167
 
      ** arguments past the first non-NULL argument.
73168
 
      */
73169
 
      if( pDef->flags & SQLITE_FUNC_COALESCE ){
73170
 
        int endCoalesce = sqlite3VdbeMakeLabel(v);
73171
 
        assert( nFarg>=2 );
73172
 
        sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
73173
 
        for(i=1; i<nFarg; i++){
73174
 
          sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
73175
 
          sqlite3ExprCacheRemove(pParse, target, 1);
73176
 
          sqlite3ExprCachePush(pParse);
73177
 
          sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
73178
 
          sqlite3ExprCachePop(pParse, 1);
73179
 
        }
73180
 
        sqlite3VdbeResolveLabel(v, endCoalesce);
73181
 
        break;
73182
 
      }
73183
 
 
73184
 
 
73185
 
      if( pFarg ){
73186
 
        r1 = sqlite3GetTempRange(pParse, nFarg);
73187
 
        sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
73188
 
        sqlite3ExprCodeExprList(pParse, pFarg, r1, 1);
73189
 
        sqlite3ExprCachePop(pParse, 1);   /* Ticket 2ea2425d34be */
73190
 
      }else{
73191
 
        r1 = 0;
73192
 
      }
73193
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
73194
 
      /* Possibly overload the function if the first argument is
73195
 
      ** a virtual table column.
73196
 
      **
73197
 
      ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
73198
 
      ** second argument, not the first, as the argument to test to
73199
 
      ** see if it is a column in a virtual table.  This is done because
73200
 
      ** the left operand of infix functions (the operand we want to
73201
 
      ** control overloading) ends up as the second argument to the
73202
 
      ** function.  The expression "A glob B" is equivalent to 
73203
 
      ** "glob(B,A).  We want to use the A in "A glob B" to test
73204
 
      ** for function overloading.  But we use the B term in "glob(B,A)".
73205
 
      */
73206
 
      if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
73207
 
        pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
73208
 
      }else if( nFarg>0 ){
73209
 
        pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
73210
 
      }
73211
 
#endif
73212
 
      for(i=0; i<nFarg; i++){
73213
 
        if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
73214
 
          constMask |= (1<<i);
73215
 
        }
73216
 
        if( (pDef->flags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
73217
 
          pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
73218
 
        }
73219
 
      }
73220
 
      if( pDef->flags & SQLITE_FUNC_NEEDCOLL ){
73221
 
        if( !pColl ) pColl = db->pDfltColl; 
73222
 
        sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
73223
 
      }
73224
 
      sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
73225
 
                        (char*)pDef, P4_FUNCDEF);
73226
 
      sqlite3VdbeChangeP5(v, (u8)nFarg);
73227
 
      if( nFarg ){
73228
 
        sqlite3ReleaseTempRange(pParse, r1, nFarg);
73229
 
      }
73230
 
      break;
73231
 
    }
73232
 
#ifndef SQLITE_OMIT_SUBQUERY
73233
 
    case TK_EXISTS:
73234
 
    case TK_SELECT: {
73235
 
      testcase( op==TK_EXISTS );
73236
 
      testcase( op==TK_SELECT );
73237
 
      inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
73238
 
      break;
73239
 
    }
73240
 
    case TK_IN: {
73241
 
      int destIfFalse = sqlite3VdbeMakeLabel(v);
73242
 
      int destIfNull = sqlite3VdbeMakeLabel(v);
73243
 
      sqlite3VdbeAddOp2(v, OP_Null, 0, target);
73244
 
      sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
73245
 
      sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
73246
 
      sqlite3VdbeResolveLabel(v, destIfFalse);
73247
 
      sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
73248
 
      sqlite3VdbeResolveLabel(v, destIfNull);
73249
 
      break;
73250
 
    }
73251
 
#endif /* SQLITE_OMIT_SUBQUERY */
73252
 
 
73253
 
 
73254
 
    /*
73255
 
    **    x BETWEEN y AND z
73256
 
    **
73257
 
    ** This is equivalent to
73258
 
    **
73259
 
    **    x>=y AND x<=z
73260
 
    **
73261
 
    ** X is stored in pExpr->pLeft.
73262
 
    ** Y is stored in pExpr->pList->a[0].pExpr.
73263
 
    ** Z is stored in pExpr->pList->a[1].pExpr.
73264
 
    */
73265
 
    case TK_BETWEEN: {
73266
 
      Expr *pLeft = pExpr->pLeft;
73267
 
      struct ExprList_item *pLItem = pExpr->x.pList->a;
73268
 
      Expr *pRight = pLItem->pExpr;
73269
 
 
73270
 
      r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
73271
 
      r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
73272
 
      testcase( regFree1==0 );
73273
 
      testcase( regFree2==0 );
73274
 
      r3 = sqlite3GetTempReg(pParse);
73275
 
      r4 = sqlite3GetTempReg(pParse);
73276
 
      codeCompare(pParse, pLeft, pRight, OP_Ge,
73277
 
                  r1, r2, r3, SQLITE_STOREP2);
73278
 
      pLItem++;
73279
 
      pRight = pLItem->pExpr;
73280
 
      sqlite3ReleaseTempReg(pParse, regFree2);
73281
 
      r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
73282
 
      testcase( regFree2==0 );
73283
 
      codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
73284
 
      sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
73285
 
      sqlite3ReleaseTempReg(pParse, r3);
73286
 
      sqlite3ReleaseTempReg(pParse, r4);
73287
 
      break;
73288
 
    }
73289
 
    case TK_UPLUS: {
73290
 
      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
73291
 
      break;
73292
 
    }
73293
 
 
73294
 
    case TK_TRIGGER: {
73295
 
      /* If the opcode is TK_TRIGGER, then the expression is a reference
73296
 
      ** to a column in the new.* or old.* pseudo-tables available to
73297
 
      ** trigger programs. In this case Expr.iTable is set to 1 for the
73298
 
      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
73299
 
      ** is set to the column of the pseudo-table to read, or to -1 to
73300
 
      ** read the rowid field.
73301
 
      **
73302
 
      ** The expression is implemented using an OP_Param opcode. The p1
73303
 
      ** parameter is set to 0 for an old.rowid reference, or to (i+1)
73304
 
      ** to reference another column of the old.* pseudo-table, where 
73305
 
      ** i is the index of the column. For a new.rowid reference, p1 is
73306
 
      ** set to (n+1), where n is the number of columns in each pseudo-table.
73307
 
      ** For a reference to any other column in the new.* pseudo-table, p1
73308
 
      ** is set to (n+2+i), where n and i are as defined previously. For
73309
 
      ** example, if the table on which triggers are being fired is
73310
 
      ** declared as:
73311
 
      **
73312
 
      **   CREATE TABLE t1(a, b);
73313
 
      **
73314
 
      ** Then p1 is interpreted as follows:
73315
 
      **
73316
 
      **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
73317
 
      **   p1==1   ->    old.a         p1==4   ->    new.a
73318
 
      **   p1==2   ->    old.b         p1==5   ->    new.b       
73319
 
      */
73320
 
      Table *pTab = pExpr->pTab;
73321
 
      int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
73322
 
 
73323
 
      assert( pExpr->iTable==0 || pExpr->iTable==1 );
73324
 
      assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
73325
 
      assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
73326
 
      assert( p1>=0 && p1<(pTab->nCol*2+2) );
73327
 
 
73328
 
      sqlite3VdbeAddOp2(v, OP_Param, p1, target);
73329
 
      VdbeComment((v, "%s.%s -> $%d",
73330
 
        (pExpr->iTable ? "new" : "old"),
73331
 
        (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
73332
 
        target
73333
 
      ));
73334
 
 
73335
 
#ifndef SQLITE_OMIT_FLOATING_POINT
73336
 
      /* If the column has REAL affinity, it may currently be stored as an
73337
 
      ** integer. Use OP_RealAffinity to make sure it is really real.  */
73338
 
      if( pExpr->iColumn>=0 
73339
 
       && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
73340
 
      ){
73341
 
        sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
73342
 
      }
73343
 
#endif
73344
 
      break;
73345
 
    }
73346
 
 
73347
 
 
73348
 
    /*
73349
 
    ** Form A:
73350
 
    **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
73351
 
    **
73352
 
    ** Form B:
73353
 
    **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
73354
 
    **
73355
 
    ** Form A is can be transformed into the equivalent form B as follows:
73356
 
    **   CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
73357
 
    **        WHEN x=eN THEN rN ELSE y END
73358
 
    **
73359
 
    ** X (if it exists) is in pExpr->pLeft.
73360
 
    ** Y is in pExpr->pRight.  The Y is also optional.  If there is no
73361
 
    ** ELSE clause and no other term matches, then the result of the
73362
 
    ** exprssion is NULL.
73363
 
    ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
73364
 
    **
73365
 
    ** The result of the expression is the Ri for the first matching Ei,
73366
 
    ** or if there is no matching Ei, the ELSE term Y, or if there is
73367
 
    ** no ELSE term, NULL.
73368
 
    */
73369
 
    default: assert( op==TK_CASE ); {
73370
 
      int endLabel;                     /* GOTO label for end of CASE stmt */
73371
 
      int nextCase;                     /* GOTO label for next WHEN clause */
73372
 
      int nExpr;                        /* 2x number of WHEN terms */
73373
 
      int i;                            /* Loop counter */
73374
 
      ExprList *pEList;                 /* List of WHEN terms */
73375
 
      struct ExprList_item *aListelem;  /* Array of WHEN terms */
73376
 
      Expr opCompare;                   /* The X==Ei expression */
73377
 
      Expr cacheX;                      /* Cached expression X */
73378
 
      Expr *pX;                         /* The X expression */
73379
 
      Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
73380
 
      VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
73381
 
 
73382
 
      assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
73383
 
      assert((pExpr->x.pList->nExpr % 2) == 0);
73384
 
      assert(pExpr->x.pList->nExpr > 0);
73385
 
      pEList = pExpr->x.pList;
73386
 
      aListelem = pEList->a;
73387
 
      nExpr = pEList->nExpr;
73388
 
      endLabel = sqlite3VdbeMakeLabel(v);
73389
 
      if( (pX = pExpr->pLeft)!=0 ){
73390
 
        cacheX = *pX;
73391
 
        testcase( pX->op==TK_COLUMN );
73392
 
        testcase( pX->op==TK_REGISTER );
73393
 
        cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
73394
 
        testcase( regFree1==0 );
73395
 
        cacheX.op = TK_REGISTER;
73396
 
        opCompare.op = TK_EQ;
73397
 
        opCompare.pLeft = &cacheX;
73398
 
        pTest = &opCompare;
73399
 
        /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
73400
 
        ** The value in regFree1 might get SCopy-ed into the file result.
73401
 
        ** So make sure that the regFree1 register is not reused for other
73402
 
        ** purposes and possibly overwritten.  */
73403
 
        regFree1 = 0;
73404
 
      }
73405
 
      for(i=0; i<nExpr; i=i+2){
73406
 
        sqlite3ExprCachePush(pParse);
73407
 
        if( pX ){
73408
 
          assert( pTest!=0 );
73409
 
          opCompare.pRight = aListelem[i].pExpr;
73410
 
        }else{
73411
 
          pTest = aListelem[i].pExpr;
73412
 
        }
73413
 
        nextCase = sqlite3VdbeMakeLabel(v);
73414
 
        testcase( pTest->op==TK_COLUMN );
73415
 
        sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
73416
 
        testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
73417
 
        testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
73418
 
        sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
73419
 
        sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
73420
 
        sqlite3ExprCachePop(pParse, 1);
73421
 
        sqlite3VdbeResolveLabel(v, nextCase);
73422
 
      }
73423
 
      if( pExpr->pRight ){
73424
 
        sqlite3ExprCachePush(pParse);
73425
 
        sqlite3ExprCode(pParse, pExpr->pRight, target);
73426
 
        sqlite3ExprCachePop(pParse, 1);
73427
 
      }else{
73428
 
        sqlite3VdbeAddOp2(v, OP_Null, 0, target);
73429
 
      }
73430
 
      assert( db->mallocFailed || pParse->nErr>0 
73431
 
           || pParse->iCacheLevel==iCacheLevel );
73432
 
      sqlite3VdbeResolveLabel(v, endLabel);
73433
 
      break;
73434
 
    }
73435
 
#ifndef SQLITE_OMIT_TRIGGER
73436
 
    case TK_RAISE: {
73437
 
      assert( pExpr->affinity==OE_Rollback 
73438
 
           || pExpr->affinity==OE_Abort
73439
 
           || pExpr->affinity==OE_Fail
73440
 
           || pExpr->affinity==OE_Ignore
73441
 
      );
73442
 
      if( !pParse->pTriggerTab ){
73443
 
        sqlite3ErrorMsg(pParse,
73444
 
                       "RAISE() may only be used within a trigger-program");
73445
 
        return 0;
73446
 
      }
73447
 
      if( pExpr->affinity==OE_Abort ){
73448
 
        sqlite3MayAbort(pParse);
73449
 
      }
73450
 
      assert( !ExprHasProperty(pExpr, EP_IntValue) );
73451
 
      if( pExpr->affinity==OE_Ignore ){
73452
 
        sqlite3VdbeAddOp4(
73453
 
            v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
73454
 
      }else{
73455
 
        sqlite3HaltConstraint(pParse, pExpr->affinity, pExpr->u.zToken, 0);
73456
 
      }
73457
 
 
73458
 
      break;
73459
 
    }
73460
 
#endif
73461
 
  }
73462
 
  sqlite3ReleaseTempReg(pParse, regFree1);
73463
 
  sqlite3ReleaseTempReg(pParse, regFree2);
73464
 
  return inReg;
73465
 
}
73466
 
 
73467
 
/*
73468
 
** Generate code to evaluate an expression and store the results
73469
 
** into a register.  Return the register number where the results
73470
 
** are stored.
73471
 
**
73472
 
** If the register is a temporary register that can be deallocated,
73473
 
** then write its number into *pReg.  If the result register is not
73474
 
** a temporary, then set *pReg to zero.
73475
 
*/
73476
 
SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
73477
 
  int r1 = sqlite3GetTempReg(pParse);
73478
 
  int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
73479
 
  if( r2==r1 ){
73480
 
    *pReg = r1;
73481
 
  }else{
73482
 
    sqlite3ReleaseTempReg(pParse, r1);
73483
 
    *pReg = 0;
73484
 
  }
73485
 
  return r2;
73486
 
}
73487
 
 
73488
 
/*
73489
 
** Generate code that will evaluate expression pExpr and store the
73490
 
** results in register target.  The results are guaranteed to appear
73491
 
** in register target.
73492
 
*/
73493
 
SQLITE_PRIVATE int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
73494
 
  int inReg;
73495
 
 
73496
 
  assert( target>0 && target<=pParse->nMem );
73497
 
  if( pExpr && pExpr->op==TK_REGISTER ){
73498
 
    sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
73499
 
  }else{
73500
 
    inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
73501
 
    assert( pParse->pVdbe || pParse->db->mallocFailed );
73502
 
    if( inReg!=target && pParse->pVdbe ){
73503
 
      sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
73504
 
    }
73505
 
  }
73506
 
  return target;
73507
 
}
73508
 
 
73509
 
/*
73510
 
** Generate code that evalutes the given expression and puts the result
73511
 
** in register target.
73512
 
**
73513
 
** Also make a copy of the expression results into another "cache" register
73514
 
** and modify the expression so that the next time it is evaluated,
73515
 
** the result is a copy of the cache register.
73516
 
**
73517
 
** This routine is used for expressions that are used multiple 
73518
 
** times.  They are evaluated once and the results of the expression
73519
 
** are reused.
73520
 
*/
73521
 
SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
73522
 
  Vdbe *v = pParse->pVdbe;
73523
 
  int inReg;
73524
 
  inReg = sqlite3ExprCode(pParse, pExpr, target);
73525
 
  assert( target>0 );
73526
 
  /* This routine is called for terms to INSERT or UPDATE.  And the only
73527
 
  ** other place where expressions can be converted into TK_REGISTER is
73528
 
  ** in WHERE clause processing.  So as currently implemented, there is
73529
 
  ** no way for a TK_REGISTER to exist here.  But it seems prudent to
73530
 
  ** keep the ALWAYS() in case the conditions above change with future
73531
 
  ** modifications or enhancements. */
73532
 
  if( ALWAYS(pExpr->op!=TK_REGISTER) ){  
73533
 
    int iMem;
73534
 
    iMem = ++pParse->nMem;
73535
 
    sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
73536
 
    pExpr->iTable = iMem;
73537
 
    pExpr->op2 = pExpr->op;
73538
 
    pExpr->op = TK_REGISTER;
73539
 
  }
73540
 
  return inReg;
73541
 
}
73542
 
 
73543
 
/*
73544
 
** Return TRUE if pExpr is an constant expression that is appropriate
73545
 
** for factoring out of a loop.  Appropriate expressions are:
73546
 
**
73547
 
**    *  Any expression that evaluates to two or more opcodes.
73548
 
**
73549
 
**    *  Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, 
73550
 
**       or OP_Variable that does not need to be placed in a 
73551
 
**       specific register.
73552
 
**
73553
 
** There is no point in factoring out single-instruction constant
73554
 
** expressions that need to be placed in a particular register.  
73555
 
** We could factor them out, but then we would end up adding an
73556
 
** OP_SCopy instruction to move the value into the correct register
73557
 
** later.  We might as well just use the original instruction and
73558
 
** avoid the OP_SCopy.
73559
 
*/
73560
 
static int isAppropriateForFactoring(Expr *p){
73561
 
  if( !sqlite3ExprIsConstantNotJoin(p) ){
73562
 
    return 0;  /* Only constant expressions are appropriate for factoring */
73563
 
  }
73564
 
  if( (p->flags & EP_FixedDest)==0 ){
73565
 
    return 1;  /* Any constant without a fixed destination is appropriate */
73566
 
  }
73567
 
  while( p->op==TK_UPLUS ) p = p->pLeft;
73568
 
  switch( p->op ){
73569
 
#ifndef SQLITE_OMIT_BLOB_LITERAL
73570
 
    case TK_BLOB:
73571
 
#endif
73572
 
    case TK_VARIABLE:
73573
 
    case TK_INTEGER:
73574
 
    case TK_FLOAT:
73575
 
    case TK_NULL:
73576
 
    case TK_STRING: {
73577
 
      testcase( p->op==TK_BLOB );
73578
 
      testcase( p->op==TK_VARIABLE );
73579
 
      testcase( p->op==TK_INTEGER );
73580
 
      testcase( p->op==TK_FLOAT );
73581
 
      testcase( p->op==TK_NULL );
73582
 
      testcase( p->op==TK_STRING );
73583
 
      /* Single-instruction constants with a fixed destination are
73584
 
      ** better done in-line.  If we factor them, they will just end
73585
 
      ** up generating an OP_SCopy to move the value to the destination
73586
 
      ** register. */
73587
 
      return 0;
73588
 
    }
73589
 
    case TK_UMINUS: {
73590
 
      if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
73591
 
        return 0;
73592
 
      }
73593
 
      break;
73594
 
    }
73595
 
    default: {
73596
 
      break;
73597
 
    }
73598
 
  }
73599
 
  return 1;
73600
 
}
73601
 
 
73602
 
/*
73603
 
** If pExpr is a constant expression that is appropriate for
73604
 
** factoring out of a loop, then evaluate the expression
73605
 
** into a register and convert the expression into a TK_REGISTER
73606
 
** expression.
73607
 
*/
73608
 
static int evalConstExpr(Walker *pWalker, Expr *pExpr){
73609
 
  Parse *pParse = pWalker->pParse;
73610
 
  switch( pExpr->op ){
73611
 
    case TK_IN:
73612
 
    case TK_REGISTER: {
73613
 
      return WRC_Prune;
73614
 
    }
73615
 
    case TK_FUNCTION:
73616
 
    case TK_AGG_FUNCTION:
73617
 
    case TK_CONST_FUNC: {
73618
 
      /* The arguments to a function have a fixed destination.
73619
 
      ** Mark them this way to avoid generated unneeded OP_SCopy
73620
 
      ** instructions. 
73621
 
      */
73622
 
      ExprList *pList = pExpr->x.pList;
73623
 
      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
73624
 
      if( pList ){
73625
 
        int i = pList->nExpr;
73626
 
        struct ExprList_item *pItem = pList->a;
73627
 
        for(; i>0; i--, pItem++){
73628
 
          if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest;
73629
 
        }
73630
 
      }
73631
 
      break;
73632
 
    }
73633
 
  }
73634
 
  if( isAppropriateForFactoring(pExpr) ){
73635
 
    int r1 = ++pParse->nMem;
73636
 
    int r2;
73637
 
    r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
73638
 
    if( NEVER(r1!=r2) ) sqlite3ReleaseTempReg(pParse, r1);
73639
 
    pExpr->op2 = pExpr->op;
73640
 
    pExpr->op = TK_REGISTER;
73641
 
    pExpr->iTable = r2;
73642
 
    return WRC_Prune;
73643
 
  }
73644
 
  return WRC_Continue;
73645
 
}
73646
 
 
73647
 
/*
73648
 
** Preevaluate constant subexpressions within pExpr and store the
73649
 
** results in registers.  Modify pExpr so that the constant subexpresions
73650
 
** are TK_REGISTER opcodes that refer to the precomputed values.
73651
 
**
73652
 
** This routine is a no-op if the jump to the cookie-check code has
73653
 
** already occur.  Since the cookie-check jump is generated prior to
73654
 
** any other serious processing, this check ensures that there is no
73655
 
** way to accidently bypass the constant initializations.
73656
 
**
73657
 
** This routine is also a no-op if the SQLITE_FactorOutConst optimization
73658
 
** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS)
73659
 
** interface.  This allows test logic to verify that the same answer is
73660
 
** obtained for queries regardless of whether or not constants are
73661
 
** precomputed into registers or if they are inserted in-line.
73662
 
*/
73663
 
SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
73664
 
  Walker w;
73665
 
  if( pParse->cookieGoto ) return;
73666
 
  if( (pParse->db->flags & SQLITE_FactorOutConst)!=0 ) return;
73667
 
  w.xExprCallback = evalConstExpr;
73668
 
  w.xSelectCallback = 0;
73669
 
  w.pParse = pParse;
73670
 
  sqlite3WalkExpr(&w, pExpr);
73671
 
}
73672
 
 
73673
 
 
73674
 
/*
73675
 
** Generate code that pushes the value of every element of the given
73676
 
** expression list into a sequence of registers beginning at target.
73677
 
**
73678
 
** Return the number of elements evaluated.
73679
 
*/
73680
 
SQLITE_PRIVATE int sqlite3ExprCodeExprList(
73681
 
  Parse *pParse,     /* Parsing context */
73682
 
  ExprList *pList,   /* The expression list to be coded */
73683
 
  int target,        /* Where to write results */
73684
 
  int doHardCopy     /* Make a hard copy of every element */
73685
 
){
73686
 
  struct ExprList_item *pItem;
73687
 
  int i, n;
73688
 
  assert( pList!=0 );
73689
 
  assert( target>0 );
73690
 
  assert( pParse->pVdbe!=0 );  /* Never gets this far otherwise */
73691
 
  n = pList->nExpr;
73692
 
  for(pItem=pList->a, i=0; i<n; i++, pItem++){
73693
 
    Expr *pExpr = pItem->pExpr;
73694
 
    int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
73695
 
    if( inReg!=target+i ){
73696
 
      sqlite3VdbeAddOp2(pParse->pVdbe, doHardCopy ? OP_Copy : OP_SCopy,
73697
 
                        inReg, target+i);
73698
 
    }
73699
 
  }
73700
 
  return n;
73701
 
}
73702
 
 
73703
 
/*
73704
 
** Generate code for a BETWEEN operator.
73705
 
**
73706
 
**    x BETWEEN y AND z
73707
 
**
73708
 
** The above is equivalent to 
73709
 
**
73710
 
**    x>=y AND x<=z
73711
 
**
73712
 
** Code it as such, taking care to do the common subexpression
73713
 
** elementation of x.
73714
 
*/
73715
 
static void exprCodeBetween(
73716
 
  Parse *pParse,    /* Parsing and code generating context */
73717
 
  Expr *pExpr,      /* The BETWEEN expression */
73718
 
  int dest,         /* Jump here if the jump is taken */
73719
 
  int jumpIfTrue,   /* Take the jump if the BETWEEN is true */
73720
 
  int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
73721
 
){
73722
 
  Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
73723
 
  Expr compLeft;    /* The  x>=y  term */
73724
 
  Expr compRight;   /* The  x<=z  term */
73725
 
  Expr exprX;       /* The  x  subexpression */
73726
 
  int regFree1 = 0; /* Temporary use register */
73727
 
 
73728
 
  assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
73729
 
  exprX = *pExpr->pLeft;
73730
 
  exprAnd.op = TK_AND;
73731
 
  exprAnd.pLeft = &compLeft;
73732
 
  exprAnd.pRight = &compRight;
73733
 
  compLeft.op = TK_GE;
73734
 
  compLeft.pLeft = &exprX;
73735
 
  compLeft.pRight = pExpr->x.pList->a[0].pExpr;
73736
 
  compRight.op = TK_LE;
73737
 
  compRight.pLeft = &exprX;
73738
 
  compRight.pRight = pExpr->x.pList->a[1].pExpr;
73739
 
  exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
73740
 
  exprX.op = TK_REGISTER;
73741
 
  if( jumpIfTrue ){
73742
 
    sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
73743
 
  }else{
73744
 
    sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
73745
 
  }
73746
 
  sqlite3ReleaseTempReg(pParse, regFree1);
73747
 
 
73748
 
  /* Ensure adequate test coverage */
73749
 
  testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
73750
 
  testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
73751
 
  testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
73752
 
  testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
73753
 
  testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
73754
 
  testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
73755
 
  testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
73756
 
  testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
73757
 
}
73758
 
 
73759
 
/*
73760
 
** Generate code for a boolean expression such that a jump is made
73761
 
** to the label "dest" if the expression is true but execution
73762
 
** continues straight thru if the expression is false.
73763
 
**
73764
 
** If the expression evaluates to NULL (neither true nor false), then
73765
 
** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
73766
 
**
73767
 
** This code depends on the fact that certain token values (ex: TK_EQ)
73768
 
** are the same as opcode values (ex: OP_Eq) that implement the corresponding
73769
 
** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in
73770
 
** the make process cause these values to align.  Assert()s in the code
73771
 
** below verify that the numbers are aligned correctly.
73772
 
*/
73773
 
SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
73774
 
  Vdbe *v = pParse->pVdbe;
73775
 
  int op = 0;
73776
 
  int regFree1 = 0;
73777
 
  int regFree2 = 0;
73778
 
  int r1, r2;
73779
 
 
73780
 
  assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
73781
 
  if( NEVER(v==0) )     return;  /* Existance of VDBE checked by caller */
73782
 
  if( NEVER(pExpr==0) ) return;  /* No way this can happen */
73783
 
  op = pExpr->op;
73784
 
  switch( op ){
73785
 
    case TK_AND: {
73786
 
      int d2 = sqlite3VdbeMakeLabel(v);
73787
 
      testcase( jumpIfNull==0 );
73788
 
      sqlite3ExprCachePush(pParse);
73789
 
      sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
73790
 
      sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
73791
 
      sqlite3VdbeResolveLabel(v, d2);
73792
 
      sqlite3ExprCachePop(pParse, 1);
73793
 
      break;
73794
 
    }
73795
 
    case TK_OR: {
73796
 
      testcase( jumpIfNull==0 );
73797
 
      sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
73798
 
      sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
73799
 
      break;
73800
 
    }
73801
 
    case TK_NOT: {
73802
 
      testcase( jumpIfNull==0 );
73803
 
      sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
73804
 
      break;
73805
 
    }
73806
 
    case TK_LT:
73807
 
    case TK_LE:
73808
 
    case TK_GT:
73809
 
    case TK_GE:
73810
 
    case TK_NE:
73811
 
    case TK_EQ: {
73812
 
      assert( TK_LT==OP_Lt );
73813
 
      assert( TK_LE==OP_Le );
73814
 
      assert( TK_GT==OP_Gt );
73815
 
      assert( TK_GE==OP_Ge );
73816
 
      assert( TK_EQ==OP_Eq );
73817
 
      assert( TK_NE==OP_Ne );
73818
 
      testcase( op==TK_LT );
73819
 
      testcase( op==TK_LE );
73820
 
      testcase( op==TK_GT );
73821
 
      testcase( op==TK_GE );
73822
 
      testcase( op==TK_EQ );
73823
 
      testcase( op==TK_NE );
73824
 
      testcase( jumpIfNull==0 );
73825
 
      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73826
 
      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
73827
 
      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
73828
 
                  r1, r2, dest, jumpIfNull);
73829
 
      testcase( regFree1==0 );
73830
 
      testcase( regFree2==0 );
73831
 
      break;
73832
 
    }
73833
 
    case TK_IS:
73834
 
    case TK_ISNOT: {
73835
 
      testcase( op==TK_IS );
73836
 
      testcase( op==TK_ISNOT );
73837
 
      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73838
 
      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
73839
 
      op = (op==TK_IS) ? TK_EQ : TK_NE;
73840
 
      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
73841
 
                  r1, r2, dest, SQLITE_NULLEQ);
73842
 
      testcase( regFree1==0 );
73843
 
      testcase( regFree2==0 );
73844
 
      break;
73845
 
    }
73846
 
    case TK_ISNULL:
73847
 
    case TK_NOTNULL: {
73848
 
      assert( TK_ISNULL==OP_IsNull );
73849
 
      assert( TK_NOTNULL==OP_NotNull );
73850
 
      testcase( op==TK_ISNULL );
73851
 
      testcase( op==TK_NOTNULL );
73852
 
      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73853
 
      sqlite3VdbeAddOp2(v, op, r1, dest);
73854
 
      testcase( regFree1==0 );
73855
 
      break;
73856
 
    }
73857
 
    case TK_BETWEEN: {
73858
 
      testcase( jumpIfNull==0 );
73859
 
      exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
73860
 
      break;
73861
 
    }
73862
 
#ifndef SQLITE_OMIT_SUBQUERY
73863
 
    case TK_IN: {
73864
 
      int destIfFalse = sqlite3VdbeMakeLabel(v);
73865
 
      int destIfNull = jumpIfNull ? dest : destIfFalse;
73866
 
      sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
73867
 
      sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
73868
 
      sqlite3VdbeResolveLabel(v, destIfFalse);
73869
 
      break;
73870
 
    }
73871
 
#endif
73872
 
    default: {
73873
 
      r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
73874
 
      sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
73875
 
      testcase( regFree1==0 );
73876
 
      testcase( jumpIfNull==0 );
73877
 
      break;
73878
 
    }
73879
 
  }
73880
 
  sqlite3ReleaseTempReg(pParse, regFree1);
73881
 
  sqlite3ReleaseTempReg(pParse, regFree2);  
73882
 
}
73883
 
 
73884
 
/*
73885
 
** Generate code for a boolean expression such that a jump is made
73886
 
** to the label "dest" if the expression is false but execution
73887
 
** continues straight thru if the expression is true.
73888
 
**
73889
 
** If the expression evaluates to NULL (neither true nor false) then
73890
 
** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
73891
 
** is 0.
73892
 
*/
73893
 
SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
73894
 
  Vdbe *v = pParse->pVdbe;
73895
 
  int op = 0;
73896
 
  int regFree1 = 0;
73897
 
  int regFree2 = 0;
73898
 
  int r1, r2;
73899
 
 
73900
 
  assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
73901
 
  if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */
73902
 
  if( pExpr==0 )    return;
73903
 
 
73904
 
  /* The value of pExpr->op and op are related as follows:
73905
 
  **
73906
 
  **       pExpr->op            op
73907
 
  **       ---------          ----------
73908
 
  **       TK_ISNULL          OP_NotNull
73909
 
  **       TK_NOTNULL         OP_IsNull
73910
 
  **       TK_NE              OP_Eq
73911
 
  **       TK_EQ              OP_Ne
73912
 
  **       TK_GT              OP_Le
73913
 
  **       TK_LE              OP_Gt
73914
 
  **       TK_GE              OP_Lt
73915
 
  **       TK_LT              OP_Ge
73916
 
  **
73917
 
  ** For other values of pExpr->op, op is undefined and unused.
73918
 
  ** The value of TK_ and OP_ constants are arranged such that we
73919
 
  ** can compute the mapping above using the following expression.
73920
 
  ** Assert()s verify that the computation is correct.
73921
 
  */
73922
 
  op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
73923
 
 
73924
 
  /* Verify correct alignment of TK_ and OP_ constants
73925
 
  */
73926
 
  assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
73927
 
  assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
73928
 
  assert( pExpr->op!=TK_NE || op==OP_Eq );
73929
 
  assert( pExpr->op!=TK_EQ || op==OP_Ne );
73930
 
  assert( pExpr->op!=TK_LT || op==OP_Ge );
73931
 
  assert( pExpr->op!=TK_LE || op==OP_Gt );
73932
 
  assert( pExpr->op!=TK_GT || op==OP_Le );
73933
 
  assert( pExpr->op!=TK_GE || op==OP_Lt );
73934
 
 
73935
 
  switch( pExpr->op ){
73936
 
    case TK_AND: {
73937
 
      testcase( jumpIfNull==0 );
73938
 
      sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
73939
 
      sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
73940
 
      break;
73941
 
    }
73942
 
    case TK_OR: {
73943
 
      int d2 = sqlite3VdbeMakeLabel(v);
73944
 
      testcase( jumpIfNull==0 );
73945
 
      sqlite3ExprCachePush(pParse);
73946
 
      sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
73947
 
      sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
73948
 
      sqlite3VdbeResolveLabel(v, d2);
73949
 
      sqlite3ExprCachePop(pParse, 1);
73950
 
      break;
73951
 
    }
73952
 
    case TK_NOT: {
73953
 
      testcase( jumpIfNull==0 );
73954
 
      sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
73955
 
      break;
73956
 
    }
73957
 
    case TK_LT:
73958
 
    case TK_LE:
73959
 
    case TK_GT:
73960
 
    case TK_GE:
73961
 
    case TK_NE:
73962
 
    case TK_EQ: {
73963
 
      testcase( op==TK_LT );
73964
 
      testcase( op==TK_LE );
73965
 
      testcase( op==TK_GT );
73966
 
      testcase( op==TK_GE );
73967
 
      testcase( op==TK_EQ );
73968
 
      testcase( op==TK_NE );
73969
 
      testcase( jumpIfNull==0 );
73970
 
      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73971
 
      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
73972
 
      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
73973
 
                  r1, r2, dest, jumpIfNull);
73974
 
      testcase( regFree1==0 );
73975
 
      testcase( regFree2==0 );
73976
 
      break;
73977
 
    }
73978
 
    case TK_IS:
73979
 
    case TK_ISNOT: {
73980
 
      testcase( pExpr->op==TK_IS );
73981
 
      testcase( pExpr->op==TK_ISNOT );
73982
 
      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73983
 
      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
73984
 
      op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
73985
 
      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
73986
 
                  r1, r2, dest, SQLITE_NULLEQ);
73987
 
      testcase( regFree1==0 );
73988
 
      testcase( regFree2==0 );
73989
 
      break;
73990
 
    }
73991
 
    case TK_ISNULL:
73992
 
    case TK_NOTNULL: {
73993
 
      testcase( op==TK_ISNULL );
73994
 
      testcase( op==TK_NOTNULL );
73995
 
      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
73996
 
      sqlite3VdbeAddOp2(v, op, r1, dest);
73997
 
      testcase( regFree1==0 );
73998
 
      break;
73999
 
    }
74000
 
    case TK_BETWEEN: {
74001
 
      testcase( jumpIfNull==0 );
74002
 
      exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
74003
 
      break;
74004
 
    }
74005
 
#ifndef SQLITE_OMIT_SUBQUERY
74006
 
    case TK_IN: {
74007
 
      if( jumpIfNull ){
74008
 
        sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
74009
 
      }else{
74010
 
        int destIfNull = sqlite3VdbeMakeLabel(v);
74011
 
        sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
74012
 
        sqlite3VdbeResolveLabel(v, destIfNull);
74013
 
      }
74014
 
      break;
74015
 
    }
74016
 
#endif
74017
 
    default: {
74018
 
      r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
74019
 
      sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
74020
 
      testcase( regFree1==0 );
74021
 
      testcase( jumpIfNull==0 );
74022
 
      break;
74023
 
    }
74024
 
  }
74025
 
  sqlite3ReleaseTempReg(pParse, regFree1);
74026
 
  sqlite3ReleaseTempReg(pParse, regFree2);
74027
 
}
74028
 
 
74029
 
/*
74030
 
** Do a deep comparison of two expression trees.  Return 0 if the two
74031
 
** expressions are completely identical.  Return 1 if they differ only
74032
 
** by a COLLATE operator at the top level.  Return 2 if there are differences
74033
 
** other than the top-level COLLATE operator.
74034
 
**
74035
 
** Sometimes this routine will return 2 even if the two expressions
74036
 
** really are equivalent.  If we cannot prove that the expressions are
74037
 
** identical, we return 2 just to be safe.  So if this routine
74038
 
** returns 2, then you do not really know for certain if the two
74039
 
** expressions are the same.  But if you get a 0 or 1 return, then you
74040
 
** can be sure the expressions are the same.  In the places where
74041
 
** this routine is used, it does not hurt to get an extra 2 - that
74042
 
** just might result in some slightly slower code.  But returning
74043
 
** an incorrect 0 or 1 could lead to a malfunction.
74044
 
*/
74045
 
SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB){
74046
 
  if( pA==0||pB==0 ){
74047
 
    return pB==pA ? 0 : 2;
74048
 
  }
74049
 
  assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) );
74050
 
  assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) );
74051
 
  if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){
74052
 
    return 2;
74053
 
  }
74054
 
  if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
74055
 
  if( pA->op!=pB->op ) return 2;
74056
 
  if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2;
74057
 
  if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
74058
 
  if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2;
74059
 
  if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
74060
 
  if( ExprHasProperty(pA, EP_IntValue) ){
74061
 
    if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
74062
 
      return 2;
74063
 
    }
74064
 
  }else if( pA->op!=TK_COLUMN && pA->u.zToken ){
74065
 
    if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
74066
 
    if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ){
74067
 
      return 2;
74068
 
    }
74069
 
  }
74070
 
  if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
74071
 
  if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
74072
 
  return 0;
74073
 
}
74074
 
 
74075
 
/*
74076
 
** Compare two ExprList objects.  Return 0 if they are identical and 
74077
 
** non-zero if they differ in any way.
74078
 
**
74079
 
** This routine might return non-zero for equivalent ExprLists.  The
74080
 
** only consequence will be disabled optimizations.  But this routine
74081
 
** must never return 0 if the two ExprList objects are different, or
74082
 
** a malfunction will result.
74083
 
**
74084
 
** Two NULL pointers are considered to be the same.  But a NULL pointer
74085
 
** always differs from a non-NULL pointer.
74086
 
*/
74087
 
SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB){
74088
 
  int i;
74089
 
  if( pA==0 && pB==0 ) return 0;
74090
 
  if( pA==0 || pB==0 ) return 1;
74091
 
  if( pA->nExpr!=pB->nExpr ) return 1;
74092
 
  for(i=0; i<pA->nExpr; i++){
74093
 
    Expr *pExprA = pA->a[i].pExpr;
74094
 
    Expr *pExprB = pB->a[i].pExpr;
74095
 
    if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
74096
 
    if( sqlite3ExprCompare(pExprA, pExprB) ) return 1;
74097
 
  }
74098
 
  return 0;
74099
 
}
74100
 
 
74101
 
/*
74102
 
** Add a new element to the pAggInfo->aCol[] array.  Return the index of
74103
 
** the new element.  Return a negative number if malloc fails.
74104
 
*/
74105
 
static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
74106
 
  int i;
74107
 
  pInfo->aCol = sqlite3ArrayAllocate(
74108
 
       db,
74109
 
       pInfo->aCol,
74110
 
       sizeof(pInfo->aCol[0]),
74111
 
       3,
74112
 
       &pInfo->nColumn,
74113
 
       &pInfo->nColumnAlloc,
74114
 
       &i
74115
 
  );
74116
 
  return i;
74117
 
}    
74118
 
 
74119
 
/*
74120
 
** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
74121
 
** the new element.  Return a negative number if malloc fails.
74122
 
*/
74123
 
static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
74124
 
  int i;
74125
 
  pInfo->aFunc = sqlite3ArrayAllocate(
74126
 
       db, 
74127
 
       pInfo->aFunc,
74128
 
       sizeof(pInfo->aFunc[0]),
74129
 
       3,
74130
 
       &pInfo->nFunc,
74131
 
       &pInfo->nFuncAlloc,
74132
 
       &i
74133
 
  );
74134
 
  return i;
74135
 
}    
74136
 
 
74137
 
/*
74138
 
** This is the xExprCallback for a tree walker.  It is used to
74139
 
** implement sqlite3ExprAnalyzeAggregates().  See sqlite3ExprAnalyzeAggregates
74140
 
** for additional information.
74141
 
*/
74142
 
static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
74143
 
  int i;
74144
 
  NameContext *pNC = pWalker->u.pNC;
74145
 
  Parse *pParse = pNC->pParse;
74146
 
  SrcList *pSrcList = pNC->pSrcList;
74147
 
  AggInfo *pAggInfo = pNC->pAggInfo;
74148
 
 
74149
 
  switch( pExpr->op ){
74150
 
    case TK_AGG_COLUMN:
74151
 
    case TK_COLUMN: {
74152
 
      testcase( pExpr->op==TK_AGG_COLUMN );
74153
 
      testcase( pExpr->op==TK_COLUMN );
74154
 
      /* Check to see if the column is in one of the tables in the FROM
74155
 
      ** clause of the aggregate query */
74156
 
      if( ALWAYS(pSrcList!=0) ){
74157
 
        struct SrcList_item *pItem = pSrcList->a;
74158
 
        for(i=0; i<pSrcList->nSrc; i++, pItem++){
74159
 
          struct AggInfo_col *pCol;
74160
 
          assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
74161
 
          if( pExpr->iTable==pItem->iCursor ){
74162
 
            /* If we reach this point, it means that pExpr refers to a table
74163
 
            ** that is in the FROM clause of the aggregate query.  
74164
 
            **
74165
 
            ** Make an entry for the column in pAggInfo->aCol[] if there
74166
 
            ** is not an entry there already.
74167
 
            */
74168
 
            int k;
74169
 
            pCol = pAggInfo->aCol;
74170
 
            for(k=0; k<pAggInfo->nColumn; k++, pCol++){
74171
 
              if( pCol->iTable==pExpr->iTable &&
74172
 
                  pCol->iColumn==pExpr->iColumn ){
74173
 
                break;
74174
 
              }
74175
 
            }
74176
 
            if( (k>=pAggInfo->nColumn)
74177
 
             && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 
74178
 
            ){
74179
 
              pCol = &pAggInfo->aCol[k];
74180
 
              pCol->pTab = pExpr->pTab;
74181
 
              pCol->iTable = pExpr->iTable;
74182
 
              pCol->iColumn = pExpr->iColumn;
74183
 
              pCol->iMem = ++pParse->nMem;
74184
 
              pCol->iSorterColumn = -1;
74185
 
              pCol->pExpr = pExpr;
74186
 
              if( pAggInfo->pGroupBy ){
74187
 
                int j, n;
74188
 
                ExprList *pGB = pAggInfo->pGroupBy;
74189
 
                struct ExprList_item *pTerm = pGB->a;
74190
 
                n = pGB->nExpr;
74191
 
                for(j=0; j<n; j++, pTerm++){
74192
 
                  Expr *pE = pTerm->pExpr;
74193
 
                  if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
74194
 
                      pE->iColumn==pExpr->iColumn ){
74195
 
                    pCol->iSorterColumn = j;
74196
 
                    break;
74197
 
                  }
74198
 
                }
74199
 
              }
74200
 
              if( pCol->iSorterColumn<0 ){
74201
 
                pCol->iSorterColumn = pAggInfo->nSortingColumn++;
74202
 
              }
74203
 
            }
74204
 
            /* There is now an entry for pExpr in pAggInfo->aCol[] (either
74205
 
            ** because it was there before or because we just created it).
74206
 
            ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
74207
 
            ** pAggInfo->aCol[] entry.
74208
 
            */
74209
 
            ExprSetIrreducible(pExpr);
74210
 
            pExpr->pAggInfo = pAggInfo;
74211
 
            pExpr->op = TK_AGG_COLUMN;
74212
 
            pExpr->iAgg = (i16)k;
74213
 
            break;
74214
 
          } /* endif pExpr->iTable==pItem->iCursor */
74215
 
        } /* end loop over pSrcList */
74216
 
      }
74217
 
      return WRC_Prune;
74218
 
    }
74219
 
    case TK_AGG_FUNCTION: {
74220
 
      /* The pNC->nDepth==0 test causes aggregate functions in subqueries
74221
 
      ** to be ignored */
74222
 
      if( pNC->nDepth==0 ){
74223
 
        /* Check to see if pExpr is a duplicate of another aggregate 
74224
 
        ** function that is already in the pAggInfo structure
74225
 
        */
74226
 
        struct AggInfo_func *pItem = pAggInfo->aFunc;
74227
 
        for(i=0; i<pAggInfo->nFunc; i++, pItem++){
74228
 
          if( sqlite3ExprCompare(pItem->pExpr, pExpr)==0 ){
74229
 
            break;
74230
 
          }
74231
 
        }
74232
 
        if( i>=pAggInfo->nFunc ){
74233
 
          /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
74234
 
          */
74235
 
          u8 enc = ENC(pParse->db);
74236
 
          i = addAggInfoFunc(pParse->db, pAggInfo);
74237
 
          if( i>=0 ){
74238
 
            assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
74239
 
            pItem = &pAggInfo->aFunc[i];
74240
 
            pItem->pExpr = pExpr;
74241
 
            pItem->iMem = ++pParse->nMem;
74242
 
            assert( !ExprHasProperty(pExpr, EP_IntValue) );
74243
 
            pItem->pFunc = sqlite3FindFunction(pParse->db,
74244
 
                   pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
74245
 
                   pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
74246
 
            if( pExpr->flags & EP_Distinct ){
74247
 
              pItem->iDistinct = pParse->nTab++;
74248
 
            }else{
74249
 
              pItem->iDistinct = -1;
74250
 
            }
74251
 
          }
74252
 
        }
74253
 
        /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
74254
 
        */
74255
 
        assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
74256
 
        ExprSetIrreducible(pExpr);
74257
 
        pExpr->iAgg = (i16)i;
74258
 
        pExpr->pAggInfo = pAggInfo;
74259
 
        return WRC_Prune;
74260
 
      }
74261
 
    }
74262
 
  }
74263
 
  return WRC_Continue;
74264
 
}
74265
 
static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
74266
 
  NameContext *pNC = pWalker->u.pNC;
74267
 
  if( pNC->nDepth==0 ){
74268
 
    pNC->nDepth++;
74269
 
    sqlite3WalkSelect(pWalker, pSelect);
74270
 
    pNC->nDepth--;
74271
 
    return WRC_Prune;
74272
 
  }else{
74273
 
    return WRC_Continue;
74274
 
  }
74275
 
}
74276
 
 
74277
 
/*
74278
 
** Analyze the given expression looking for aggregate functions and
74279
 
** for variables that need to be added to the pParse->aAgg[] array.
74280
 
** Make additional entries to the pParse->aAgg[] array as necessary.
74281
 
**
74282
 
** This routine should only be called after the expression has been
74283
 
** analyzed by sqlite3ResolveExprNames().
74284
 
*/
74285
 
SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
74286
 
  Walker w;
74287
 
  w.xExprCallback = analyzeAggregate;
74288
 
  w.xSelectCallback = analyzeAggregatesInSelect;
74289
 
  w.u.pNC = pNC;
74290
 
  assert( pNC->pSrcList!=0 );
74291
 
  sqlite3WalkExpr(&w, pExpr);
74292
 
}
74293
 
 
74294
 
/*
74295
 
** Call sqlite3ExprAnalyzeAggregates() for every expression in an
74296
 
** expression list.  Return the number of errors.
74297
 
**
74298
 
** If an error is found, the analysis is cut short.
74299
 
*/
74300
 
SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
74301
 
  struct ExprList_item *pItem;
74302
 
  int i;
74303
 
  if( pList ){
74304
 
    for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
74305
 
      sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
74306
 
    }
74307
 
  }
74308
 
}
74309
 
 
74310
 
/*
74311
 
** Allocate a single new register for use to hold some intermediate result.
74312
 
*/
74313
 
SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
74314
 
  if( pParse->nTempReg==0 ){
74315
 
    return ++pParse->nMem;
74316
 
  }
74317
 
  return pParse->aTempReg[--pParse->nTempReg];
74318
 
}
74319
 
 
74320
 
/*
74321
 
** Deallocate a register, making available for reuse for some other
74322
 
** purpose.
74323
 
**
74324
 
** If a register is currently being used by the column cache, then
74325
 
** the dallocation is deferred until the column cache line that uses
74326
 
** the register becomes stale.
74327
 
*/
74328
 
SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
74329
 
  if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
74330
 
    int i;
74331
 
    struct yColCache *p;
74332
 
    for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
74333
 
      if( p->iReg==iReg ){
74334
 
        p->tempReg = 1;
74335
 
        return;
74336
 
      }
74337
 
    }
74338
 
    pParse->aTempReg[pParse->nTempReg++] = iReg;
74339
 
  }
74340
 
}
74341
 
 
74342
 
/*
74343
 
** Allocate or deallocate a block of nReg consecutive registers
74344
 
*/
74345
 
SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
74346
 
  int i, n;
74347
 
  i = pParse->iRangeReg;
74348
 
  n = pParse->nRangeReg;
74349
 
  if( nReg<=n ){
74350
 
    assert( !usedAsColumnCache(pParse, i, i+n-1) );
74351
 
    pParse->iRangeReg += nReg;
74352
 
    pParse->nRangeReg -= nReg;
74353
 
  }else{
74354
 
    i = pParse->nMem+1;
74355
 
    pParse->nMem += nReg;
74356
 
  }
74357
 
  return i;
74358
 
}
74359
 
SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
74360
 
  sqlite3ExprCacheRemove(pParse, iReg, nReg);
74361
 
  if( nReg>pParse->nRangeReg ){
74362
 
    pParse->nRangeReg = nReg;
74363
 
    pParse->iRangeReg = iReg;
74364
 
  }
74365
 
}
74366
 
 
74367
 
/************** End of expr.c ************************************************/
74368
 
/************** Begin file alter.c *******************************************/
74369
 
/*
74370
 
** 2005 February 15
74371
 
**
74372
 
** The author disclaims copyright to this source code.  In place of
74373
 
** a legal notice, here is a blessing:
74374
 
**
74375
 
**    May you do good and not evil.
74376
 
**    May you find forgiveness for yourself and forgive others.
74377
 
**    May you share freely, never taking more than you give.
74378
 
**
74379
 
*************************************************************************
74380
 
** This file contains C code routines that used to generate VDBE code
74381
 
** that implements the ALTER TABLE command.
74382
 
*/
74383
 
 
74384
 
/*
74385
 
** The code in this file only exists if we are not omitting the
74386
 
** ALTER TABLE logic from the build.
74387
 
*/
74388
 
#ifndef SQLITE_OMIT_ALTERTABLE
74389
 
 
74390
 
 
74391
 
/*
74392
 
** This function is used by SQL generated to implement the 
74393
 
** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
74394
 
** CREATE INDEX command. The second is a table name. The table name in 
74395
 
** the CREATE TABLE or CREATE INDEX statement is replaced with the third
74396
 
** argument and the result returned. Examples:
74397
 
**
74398
 
** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
74399
 
**     -> 'CREATE TABLE def(a, b, c)'
74400
 
**
74401
 
** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
74402
 
**     -> 'CREATE INDEX i ON def(a, b, c)'
74403
 
*/
74404
 
static void renameTableFunc(
74405
 
  sqlite3_context *context,
74406
 
  int NotUsed,
74407
 
  sqlite3_value **argv
74408
 
){
74409
 
  unsigned char const *zSql = sqlite3_value_text(argv[0]);
74410
 
  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
74411
 
 
74412
 
  int token;
74413
 
  Token tname;
74414
 
  unsigned char const *zCsr = zSql;
74415
 
  int len = 0;
74416
 
  char *zRet;
74417
 
 
74418
 
  sqlite3 *db = sqlite3_context_db_handle(context);
74419
 
 
74420
 
  UNUSED_PARAMETER(NotUsed);
74421
 
 
74422
 
  /* The principle used to locate the table name in the CREATE TABLE 
74423
 
  ** statement is that the table name is the first non-space token that
74424
 
  ** is immediately followed by a TK_LP or TK_USING token.
74425
 
  */
74426
 
  if( zSql ){
74427
 
    do {
74428
 
      if( !*zCsr ){
74429
 
        /* Ran out of input before finding an opening bracket. Return NULL. */
74430
 
        return;
74431
 
      }
74432
 
 
74433
 
      /* Store the token that zCsr points to in tname. */
74434
 
      tname.z = (char*)zCsr;
74435
 
      tname.n = len;
74436
 
 
74437
 
      /* Advance zCsr to the next token. Store that token type in 'token',
74438
 
      ** and its length in 'len' (to be used next iteration of this loop).
74439
 
      */
74440
 
      do {
74441
 
        zCsr += len;
74442
 
        len = sqlite3GetToken(zCsr, &token);
74443
 
      } while( token==TK_SPACE );
74444
 
      assert( len>0 );
74445
 
    } while( token!=TK_LP && token!=TK_USING );
74446
 
 
74447
 
    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
74448
 
       zTableName, tname.z+tname.n);
74449
 
    sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
74450
 
  }
74451
 
}
74452
 
 
74453
 
/*
74454
 
** This C function implements an SQL user function that is used by SQL code
74455
 
** generated by the ALTER TABLE ... RENAME command to modify the definition
74456
 
** of any foreign key constraints that use the table being renamed as the 
74457
 
** parent table. It is passed three arguments:
74458
 
**
74459
 
**   1) The complete text of the CREATE TABLE statement being modified,
74460
 
**   2) The old name of the table being renamed, and
74461
 
**   3) The new name of the table being renamed.
74462
 
**
74463
 
** It returns the new CREATE TABLE statement. For example:
74464
 
**
74465
 
**   sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
74466
 
**       -> 'CREATE TABLE t1(a REFERENCES t3)'
74467
 
*/
74468
 
#ifndef SQLITE_OMIT_FOREIGN_KEY
74469
 
static void renameParentFunc(
74470
 
  sqlite3_context *context,
74471
 
  int NotUsed,
74472
 
  sqlite3_value **argv
74473
 
){
74474
 
  sqlite3 *db = sqlite3_context_db_handle(context);
74475
 
  char *zOutput = 0;
74476
 
  char *zResult;
74477
 
  unsigned char const *zInput = sqlite3_value_text(argv[0]);
74478
 
  unsigned char const *zOld = sqlite3_value_text(argv[1]);
74479
 
  unsigned char const *zNew = sqlite3_value_text(argv[2]);
74480
 
 
74481
 
  unsigned const char *z;         /* Pointer to token */
74482
 
  int n;                          /* Length of token z */
74483
 
  int token;                      /* Type of token */
74484
 
 
74485
 
  UNUSED_PARAMETER(NotUsed);
74486
 
  for(z=zInput; *z; z=z+n){
74487
 
    n = sqlite3GetToken(z, &token);
74488
 
    if( token==TK_REFERENCES ){
74489
 
      char *zParent;
74490
 
      do {
74491
 
        z += n;
74492
 
        n = sqlite3GetToken(z, &token);
74493
 
      }while( token==TK_SPACE );
74494
 
 
74495
 
      zParent = sqlite3DbStrNDup(db, (const char *)z, n);
74496
 
      if( zParent==0 ) break;
74497
 
      sqlite3Dequote(zParent);
74498
 
      if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
74499
 
        char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", 
74500
 
            (zOutput?zOutput:""), z-zInput, zInput, (const char *)zNew
74501
 
        );
74502
 
        sqlite3DbFree(db, zOutput);
74503
 
        zOutput = zOut;
74504
 
        zInput = &z[n];
74505
 
      }
74506
 
      sqlite3DbFree(db, zParent);
74507
 
    }
74508
 
  }
74509
 
 
74510
 
  zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), 
74511
 
  sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
74512
 
  sqlite3DbFree(db, zOutput);
74513
 
}
74514
 
#endif
74515
 
 
74516
 
#ifndef SQLITE_OMIT_TRIGGER
74517
 
/* This function is used by SQL generated to implement the
74518
 
** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER 
74519
 
** statement. The second is a table name. The table name in the CREATE 
74520
 
** TRIGGER statement is replaced with the third argument and the result 
74521
 
** returned. This is analagous to renameTableFunc() above, except for CREATE
74522
 
** TRIGGER, not CREATE INDEX and CREATE TABLE.
74523
 
*/
74524
 
static void renameTriggerFunc(
74525
 
  sqlite3_context *context,
74526
 
  int NotUsed,
74527
 
  sqlite3_value **argv
74528
 
){
74529
 
  unsigned char const *zSql = sqlite3_value_text(argv[0]);
74530
 
  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
74531
 
 
74532
 
  int token;
74533
 
  Token tname;
74534
 
  int dist = 3;
74535
 
  unsigned char const *zCsr = zSql;
74536
 
  int len = 0;
74537
 
  char *zRet;
74538
 
  sqlite3 *db = sqlite3_context_db_handle(context);
74539
 
 
74540
 
  UNUSED_PARAMETER(NotUsed);
74541
 
 
74542
 
  /* The principle used to locate the table name in the CREATE TRIGGER 
74543
 
  ** statement is that the table name is the first token that is immediatedly
74544
 
  ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
74545
 
  ** of TK_WHEN, TK_BEGIN or TK_FOR.
74546
 
  */
74547
 
  if( zSql ){
74548
 
    do {
74549
 
 
74550
 
      if( !*zCsr ){
74551
 
        /* Ran out of input before finding the table name. Return NULL. */
74552
 
        return;
74553
 
      }
74554
 
 
74555
 
      /* Store the token that zCsr points to in tname. */
74556
 
      tname.z = (char*)zCsr;
74557
 
      tname.n = len;
74558
 
 
74559
 
      /* Advance zCsr to the next token. Store that token type in 'token',
74560
 
      ** and its length in 'len' (to be used next iteration of this loop).
74561
 
      */
74562
 
      do {
74563
 
        zCsr += len;
74564
 
        len = sqlite3GetToken(zCsr, &token);
74565
 
      }while( token==TK_SPACE );
74566
 
      assert( len>0 );
74567
 
 
74568
 
      /* Variable 'dist' stores the number of tokens read since the most
74569
 
      ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN 
74570
 
      ** token is read and 'dist' equals 2, the condition stated above
74571
 
      ** to be met.
74572
 
      **
74573
 
      ** Note that ON cannot be a database, table or column name, so
74574
 
      ** there is no need to worry about syntax like 
74575
 
      ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
74576
 
      */
74577
 
      dist++;
74578
 
      if( token==TK_DOT || token==TK_ON ){
74579
 
        dist = 0;
74580
 
      }
74581
 
    } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
74582
 
 
74583
 
    /* Variable tname now contains the token that is the old table-name
74584
 
    ** in the CREATE TRIGGER statement.
74585
 
    */
74586
 
    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
74587
 
       zTableName, tname.z+tname.n);
74588
 
    sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
74589
 
  }
74590
 
}
74591
 
#endif   /* !SQLITE_OMIT_TRIGGER */
74592
 
 
74593
 
/*
74594
 
** Register built-in functions used to help implement ALTER TABLE
74595
 
*/
74596
 
SQLITE_PRIVATE void sqlite3AlterFunctions(void){
74597
 
  static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
74598
 
    FUNCTION(sqlite_rename_table,   2, 0, 0, renameTableFunc),
74599
 
#ifndef SQLITE_OMIT_TRIGGER
74600
 
    FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
74601
 
#endif
74602
 
#ifndef SQLITE_OMIT_FOREIGN_KEY
74603
 
    FUNCTION(sqlite_rename_parent,  3, 0, 0, renameParentFunc),
74604
 
#endif
74605
 
  };
74606
 
  int i;
74607
 
  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
74608
 
  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
74609
 
 
74610
 
  for(i=0; i<ArraySize(aAlterTableFuncs); i++){
74611
 
    sqlite3FuncDefInsert(pHash, &aFunc[i]);
74612
 
  }
74613
 
}
74614
 
 
74615
 
/*
74616
 
** This function is used to create the text of expressions of the form:
74617
 
**
74618
 
**   name=<constant1> OR name=<constant2> OR ...
74619
 
**
74620
 
** If argument zWhere is NULL, then a pointer string containing the text 
74621
 
** "name=<constant>" is returned, where <constant> is the quoted version
74622
 
** of the string passed as argument zConstant. The returned buffer is
74623
 
** allocated using sqlite3DbMalloc(). It is the responsibility of the
74624
 
** caller to ensure that it is eventually freed.
74625
 
**
74626
 
** If argument zWhere is not NULL, then the string returned is 
74627
 
** "<where> OR name=<constant>", where <where> is the contents of zWhere.
74628
 
** In this case zWhere is passed to sqlite3DbFree() before returning.
74629
 
** 
74630
 
*/
74631
 
static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
74632
 
  char *zNew;
74633
 
  if( !zWhere ){
74634
 
    zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
74635
 
  }else{
74636
 
    zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
74637
 
    sqlite3DbFree(db, zWhere);
74638
 
  }
74639
 
  return zNew;
74640
 
}
74641
 
 
74642
 
#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
74643
 
/*
74644
 
** Generate the text of a WHERE expression which can be used to select all
74645
 
** tables that have foreign key constraints that refer to table pTab (i.e.
74646
 
** constraints for which pTab is the parent table) from the sqlite_master
74647
 
** table.
74648
 
*/
74649
 
static char *whereForeignKeys(Parse *pParse, Table *pTab){
74650
 
  FKey *p;
74651
 
  char *zWhere = 0;
74652
 
  for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
74653
 
    zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
74654
 
  }
74655
 
  return zWhere;
74656
 
}
74657
 
#endif
74658
 
 
74659
 
/*
74660
 
** Generate the text of a WHERE expression which can be used to select all
74661
 
** temporary triggers on table pTab from the sqlite_temp_master table. If
74662
 
** table pTab has no temporary triggers, or is itself stored in the 
74663
 
** temporary database, NULL is returned.
74664
 
*/
74665
 
static char *whereTempTriggers(Parse *pParse, Table *pTab){
74666
 
  Trigger *pTrig;
74667
 
  char *zWhere = 0;
74668
 
  const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
74669
 
 
74670
 
  /* If the table is not located in the temp-db (in which case NULL is 
74671
 
  ** returned, loop through the tables list of triggers. For each trigger
74672
 
  ** that is not part of the temp-db schema, add a clause to the WHERE 
74673
 
  ** expression being built up in zWhere.
74674
 
  */
74675
 
  if( pTab->pSchema!=pTempSchema ){
74676
 
    sqlite3 *db = pParse->db;
74677
 
    for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
74678
 
      if( pTrig->pSchema==pTempSchema ){
74679
 
        zWhere = whereOrName(db, zWhere, pTrig->zName);
74680
 
      }
74681
 
    }
74682
 
  }
74683
 
  if( zWhere ){
74684
 
    char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
74685
 
    sqlite3DbFree(pParse->db, zWhere);
74686
 
    zWhere = zNew;
74687
 
  }
74688
 
  return zWhere;
74689
 
}
74690
 
 
74691
 
/*
74692
 
** Generate code to drop and reload the internal representation of table
74693
 
** pTab from the database, including triggers and temporary triggers.
74694
 
** Argument zName is the name of the table in the database schema at
74695
 
** the time the generated code is executed. This can be different from
74696
 
** pTab->zName if this function is being called to code part of an 
74697
 
** "ALTER TABLE RENAME TO" statement.
74698
 
*/
74699
 
static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
74700
 
  Vdbe *v;
74701
 
  char *zWhere;
74702
 
  int iDb;                   /* Index of database containing pTab */
74703
 
#ifndef SQLITE_OMIT_TRIGGER
74704
 
  Trigger *pTrig;
74705
 
#endif
74706
 
 
74707
 
  v = sqlite3GetVdbe(pParse);
74708
 
  if( NEVER(v==0) ) return;
74709
 
  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
74710
 
  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
74711
 
  assert( iDb>=0 );
74712
 
 
74713
 
#ifndef SQLITE_OMIT_TRIGGER
74714
 
  /* Drop any table triggers from the internal schema. */
74715
 
  for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
74716
 
    int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
74717
 
    assert( iTrigDb==iDb || iTrigDb==1 );
74718
 
    sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
74719
 
  }
74720
 
#endif
74721
 
 
74722
 
  /* Drop the table and index from the internal schema.  */
74723
 
  sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
74724
 
 
74725
 
  /* Reload the table, index and permanent trigger schemas. */
74726
 
  zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
74727
 
  if( !zWhere ) return;
74728
 
  sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
74729
 
 
74730
 
#ifndef SQLITE_OMIT_TRIGGER
74731
 
  /* Now, if the table is not stored in the temp database, reload any temp 
74732
 
  ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. 
74733
 
  */
74734
 
  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
74735
 
    sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC);
74736
 
  }
74737
 
#endif
74738
 
}
74739
 
 
74740
 
/*
74741
 
** Parameter zName is the name of a table that is about to be altered
74742
 
** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
74743
 
** If the table is a system table, this function leaves an error message
74744
 
** in pParse->zErr (system tables may not be altered) and returns non-zero.
74745
 
**
74746
 
** Or, if zName is not a system table, zero is returned.
74747
 
*/
74748
 
static int isSystemTable(Parse *pParse, const char *zName){
74749
 
  if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
74750
 
    sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
74751
 
    return 1;
74752
 
  }
74753
 
  return 0;
74754
 
}
74755
 
 
74756
 
/*
74757
 
** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" 
74758
 
** command. 
74759
 
*/
74760
 
SQLITE_PRIVATE void sqlite3AlterRenameTable(
74761
 
  Parse *pParse,            /* Parser context. */
74762
 
  SrcList *pSrc,            /* The table to rename. */
74763
 
  Token *pName              /* The new table name. */
74764
 
){
74765
 
  int iDb;                  /* Database that contains the table */
74766
 
  char *zDb;                /* Name of database iDb */
74767
 
  Table *pTab;              /* Table being renamed */
74768
 
  char *zName = 0;          /* NULL-terminated version of pName */ 
74769
 
  sqlite3 *db = pParse->db; /* Database connection */
74770
 
  int nTabName;             /* Number of UTF-8 characters in zTabName */
74771
 
  const char *zTabName;     /* Original name of the table */
74772
 
  Vdbe *v;
74773
 
#ifndef SQLITE_OMIT_TRIGGER
74774
 
  char *zWhere = 0;         /* Where clause to locate temp triggers */
74775
 
#endif
74776
 
  VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
74777
 
  int savedDbFlags;         /* Saved value of db->flags */
74778
 
 
74779
 
  savedDbFlags = db->flags;  
74780
 
  if( NEVER(db->mallocFailed) ) goto exit_rename_table;
74781
 
  assert( pSrc->nSrc==1 );
74782
 
  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
74783
 
 
74784
 
  pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
74785
 
  if( !pTab ) goto exit_rename_table;
74786
 
  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
74787
 
  zDb = db->aDb[iDb].zName;
74788
 
  db->flags |= SQLITE_PreferBuiltin;
74789
 
 
74790
 
  /* Get a NULL terminated version of the new table name. */
74791
 
  zName = sqlite3NameFromToken(db, pName);
74792
 
  if( !zName ) goto exit_rename_table;
74793
 
 
74794
 
  /* Check that a table or index named 'zName' does not already exist
74795
 
  ** in database iDb. If so, this is an error.
74796
 
  */
74797
 
  if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
74798
 
    sqlite3ErrorMsg(pParse, 
74799
 
        "there is already another table or index with this name: %s", zName);
74800
 
    goto exit_rename_table;
74801
 
  }
74802
 
 
74803
 
  /* Make sure it is not a system table being altered, or a reserved name
74804
 
  ** that the table is being renamed to.
74805
 
  */
74806
 
  if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
74807
 
    goto exit_rename_table;
74808
 
  }
74809
 
  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
74810
 
    exit_rename_table;
74811
 
  }
74812
 
 
74813
 
#ifndef SQLITE_OMIT_VIEW
74814
 
  if( pTab->pSelect ){
74815
 
    sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
74816
 
    goto exit_rename_table;
74817
 
  }
74818
 
#endif
74819
 
 
74820
 
#ifndef SQLITE_OMIT_AUTHORIZATION
74821
 
  /* Invoke the authorization callback. */
74822
 
  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
74823
 
    goto exit_rename_table;
74824
 
  }
74825
 
#endif
74826
 
 
74827
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
74828
 
  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
74829
 
    goto exit_rename_table;
74830
 
  }
74831
 
  if( IsVirtual(pTab) ){
74832
 
    pVTab = sqlite3GetVTable(db, pTab);
74833
 
    if( pVTab->pVtab->pModule->xRename==0 ){
74834
 
      pVTab = 0;
74835
 
    }
74836
 
  }
74837
 
#endif
74838
 
 
74839
 
  /* Begin a transaction and code the VerifyCookie for database iDb. 
74840
 
  ** Then modify the schema cookie (since the ALTER TABLE modifies the
74841
 
  ** schema). Open a statement transaction if the table is a virtual
74842
 
  ** table.
74843
 
  */
74844
 
  v = sqlite3GetVdbe(pParse);
74845
 
  if( v==0 ){
74846
 
    goto exit_rename_table;
74847
 
  }
74848
 
  sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
74849
 
  sqlite3ChangeCookie(pParse, iDb);
74850
 
 
74851
 
  /* If this is a virtual table, invoke the xRename() function if
74852
 
  ** one is defined. The xRename() callback will modify the names
74853
 
  ** of any resources used by the v-table implementation (including other
74854
 
  ** SQLite tables) that are identified by the name of the virtual table.
74855
 
  */
74856
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
74857
 
  if( pVTab ){
74858
 
    int i = ++pParse->nMem;
74859
 
    sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
74860
 
    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
74861
 
    sqlite3MayAbort(pParse);
74862
 
  }
74863
 
#endif
74864
 
 
74865
 
  /* figure out how many UTF-8 characters are in zName */
74866
 
  zTabName = pTab->zName;
74867
 
  nTabName = sqlite3Utf8CharLen(zTabName, -1);
74868
 
 
74869
 
#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
74870
 
  if( db->flags&SQLITE_ForeignKeys ){
74871
 
    /* If foreign-key support is enabled, rewrite the CREATE TABLE 
74872
 
    ** statements corresponding to all child tables of foreign key constraints
74873
 
    ** for which the renamed table is the parent table.  */
74874
 
    if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
74875
 
      sqlite3NestedParse(pParse, 
74876
 
          "UPDATE \"%w\".%s SET "
74877
 
              "sql = sqlite_rename_parent(sql, %Q, %Q) "
74878
 
              "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere);
74879
 
      sqlite3DbFree(db, zWhere);
74880
 
    }
74881
 
  }
74882
 
#endif
74883
 
 
74884
 
  /* Modify the sqlite_master table to use the new table name. */
74885
 
  sqlite3NestedParse(pParse,
74886
 
      "UPDATE %Q.%s SET "
74887
 
#ifdef SQLITE_OMIT_TRIGGER
74888
 
          "sql = sqlite_rename_table(sql, %Q), "
74889
 
#else
74890
 
          "sql = CASE "
74891
 
            "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
74892
 
            "ELSE sqlite_rename_table(sql, %Q) END, "
74893
 
#endif
74894
 
          "tbl_name = %Q, "
74895
 
          "name = CASE "
74896
 
            "WHEN type='table' THEN %Q "
74897
 
            "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
74898
 
             "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
74899
 
            "ELSE name END "
74900
 
      "WHERE tbl_name=%Q AND "
74901
 
          "(type='table' OR type='index' OR type='trigger');", 
74902
 
      zDb, SCHEMA_TABLE(iDb), zName, zName, zName, 
74903
 
#ifndef SQLITE_OMIT_TRIGGER
74904
 
      zName,
74905
 
#endif
74906
 
      zName, nTabName, zTabName
74907
 
  );
74908
 
 
74909
 
#ifndef SQLITE_OMIT_AUTOINCREMENT
74910
 
  /* If the sqlite_sequence table exists in this database, then update 
74911
 
  ** it with the new table name.
74912
 
  */
74913
 
  if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
74914
 
    sqlite3NestedParse(pParse,
74915
 
        "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
74916
 
        zDb, zName, pTab->zName);
74917
 
  }
74918
 
#endif
74919
 
 
74920
 
#ifndef SQLITE_OMIT_TRIGGER
74921
 
  /* If there are TEMP triggers on this table, modify the sqlite_temp_master
74922
 
  ** table. Don't do this if the table being ALTERed is itself located in
74923
 
  ** the temp database.
74924
 
  */
74925
 
  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
74926
 
    sqlite3NestedParse(pParse, 
74927
 
        "UPDATE sqlite_temp_master SET "
74928
 
            "sql = sqlite_rename_trigger(sql, %Q), "
74929
 
            "tbl_name = %Q "
74930
 
            "WHERE %s;", zName, zName, zWhere);
74931
 
    sqlite3DbFree(db, zWhere);
74932
 
  }
74933
 
#endif
74934
 
 
74935
 
#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
74936
 
  if( db->flags&SQLITE_ForeignKeys ){
74937
 
    FKey *p;
74938
 
    for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
74939
 
      Table *pFrom = p->pFrom;
74940
 
      if( pFrom!=pTab ){
74941
 
        reloadTableSchema(pParse, p->pFrom, pFrom->zName);
74942
 
      }
74943
 
    }
74944
 
  }
74945
 
#endif
74946
 
 
74947
 
  /* Drop and reload the internal table schema. */
74948
 
  reloadTableSchema(pParse, pTab, zName);
74949
 
 
74950
 
exit_rename_table:
74951
 
  sqlite3SrcListDelete(db, pSrc);
74952
 
  sqlite3DbFree(db, zName);
74953
 
  db->flags = savedDbFlags;
74954
 
}
74955
 
 
74956
 
 
74957
 
/*
74958
 
** Generate code to make sure the file format number is at least minFormat.
74959
 
** The generated code will increase the file format number if necessary.
74960
 
*/
74961
 
SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
74962
 
  Vdbe *v;
74963
 
  v = sqlite3GetVdbe(pParse);
74964
 
  /* The VDBE should have been allocated before this routine is called.
74965
 
  ** If that allocation failed, we would have quit before reaching this
74966
 
  ** point */
74967
 
  if( ALWAYS(v) ){
74968
 
    int r1 = sqlite3GetTempReg(pParse);
74969
 
    int r2 = sqlite3GetTempReg(pParse);
74970
 
    int j1;
74971
 
    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
74972
 
    sqlite3VdbeUsesBtree(v, iDb);
74973
 
    sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
74974
 
    j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
74975
 
    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
74976
 
    sqlite3VdbeJumpHere(v, j1);
74977
 
    sqlite3ReleaseTempReg(pParse, r1);
74978
 
    sqlite3ReleaseTempReg(pParse, r2);
74979
 
  }
74980
 
}
74981
 
 
74982
 
/*
74983
 
** This function is called after an "ALTER TABLE ... ADD" statement
74984
 
** has been parsed. Argument pColDef contains the text of the new
74985
 
** column definition.
74986
 
**
74987
 
** The Table structure pParse->pNewTable was extended to include
74988
 
** the new column during parsing.
74989
 
*/
74990
 
SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
74991
 
  Table *pNew;              /* Copy of pParse->pNewTable */
74992
 
  Table *pTab;              /* Table being altered */
74993
 
  int iDb;                  /* Database number */
74994
 
  const char *zDb;          /* Database name */
74995
 
  const char *zTab;         /* Table name */
74996
 
  char *zCol;               /* Null-terminated column definition */
74997
 
  Column *pCol;             /* The new column */
74998
 
  Expr *pDflt;              /* Default value for the new column */
74999
 
  sqlite3 *db;              /* The database connection; */
75000
 
 
75001
 
  db = pParse->db;
75002
 
  if( pParse->nErr || db->mallocFailed ) return;
75003
 
  pNew = pParse->pNewTable;
75004
 
  assert( pNew );
75005
 
 
75006
 
  assert( sqlite3BtreeHoldsAllMutexes(db) );
75007
 
  iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
75008
 
  zDb = db->aDb[iDb].zName;
75009
 
  zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
75010
 
  pCol = &pNew->aCol[pNew->nCol-1];
75011
 
  pDflt = pCol->pDflt;
75012
 
  pTab = sqlite3FindTable(db, zTab, zDb);
75013
 
  assert( pTab );
75014
 
 
75015
 
#ifndef SQLITE_OMIT_AUTHORIZATION
75016
 
  /* Invoke the authorization callback. */
75017
 
  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
75018
 
    return;
75019
 
  }
75020
 
#endif
75021
 
 
75022
 
  /* If the default value for the new column was specified with a 
75023
 
  ** literal NULL, then set pDflt to 0. This simplifies checking
75024
 
  ** for an SQL NULL default below.
75025
 
  */
75026
 
  if( pDflt && pDflt->op==TK_NULL ){
75027
 
    pDflt = 0;
75028
 
  }
75029
 
 
75030
 
  /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
75031
 
  ** If there is a NOT NULL constraint, then the default value for the
75032
 
  ** column must not be NULL.
75033
 
  */
75034
 
  if( pCol->isPrimKey ){
75035
 
    sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
75036
 
    return;
75037
 
  }
75038
 
  if( pNew->pIndex ){
75039
 
    sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
75040
 
    return;
75041
 
  }
75042
 
  if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
75043
 
    sqlite3ErrorMsg(pParse, 
75044
 
        "Cannot add a REFERENCES column with non-NULL default value");
75045
 
    return;
75046
 
  }
75047
 
  if( pCol->notNull && !pDflt ){
75048
 
    sqlite3ErrorMsg(pParse, 
75049
 
        "Cannot add a NOT NULL column with default value NULL");
75050
 
    return;
75051
 
  }
75052
 
 
75053
 
  /* Ensure the default expression is something that sqlite3ValueFromExpr()
75054
 
  ** can handle (i.e. not CURRENT_TIME etc.)
75055
 
  */
75056
 
  if( pDflt ){
75057
 
    sqlite3_value *pVal;
75058
 
    if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
75059
 
      db->mallocFailed = 1;
75060
 
      return;
75061
 
    }
75062
 
    if( !pVal ){
75063
 
      sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
75064
 
      return;
75065
 
    }
75066
 
    sqlite3ValueFree(pVal);
75067
 
  }
75068
 
 
75069
 
  /* Modify the CREATE TABLE statement. */
75070
 
  zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
75071
 
  if( zCol ){
75072
 
    char *zEnd = &zCol[pColDef->n-1];
75073
 
    int savedDbFlags = db->flags;
75074
 
    while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
75075
 
      *zEnd-- = '\0';
75076
 
    }
75077
 
    db->flags |= SQLITE_PreferBuiltin;
75078
 
    sqlite3NestedParse(pParse, 
75079
 
        "UPDATE \"%w\".%s SET "
75080
 
          "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
75081
 
        "WHERE type = 'table' AND name = %Q", 
75082
 
      zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
75083
 
      zTab
75084
 
    );
75085
 
    sqlite3DbFree(db, zCol);
75086
 
    db->flags = savedDbFlags;
75087
 
  }
75088
 
 
75089
 
  /* If the default value of the new column is NULL, then set the file
75090
 
  ** format to 2. If the default value of the new column is not NULL,
75091
 
  ** the file format becomes 3.
75092
 
  */
75093
 
  sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
75094
 
 
75095
 
  /* Reload the schema of the modified table. */
75096
 
  reloadTableSchema(pParse, pTab, pTab->zName);
75097
 
}
75098
 
 
75099
 
/*
75100
 
** This function is called by the parser after the table-name in
75101
 
** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument 
75102
 
** pSrc is the full-name of the table being altered.
75103
 
**
75104
 
** This routine makes a (partial) copy of the Table structure
75105
 
** for the table being altered and sets Parse.pNewTable to point
75106
 
** to it. Routines called by the parser as the column definition
75107
 
** is parsed (i.e. sqlite3AddColumn()) add the new Column data to 
75108
 
** the copy. The copy of the Table structure is deleted by tokenize.c 
75109
 
** after parsing is finished.
75110
 
**
75111
 
** Routine sqlite3AlterFinishAddColumn() will be called to complete
75112
 
** coding the "ALTER TABLE ... ADD" statement.
75113
 
*/
75114
 
SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
75115
 
  Table *pNew;
75116
 
  Table *pTab;
75117
 
  Vdbe *v;
75118
 
  int iDb;
75119
 
  int i;
75120
 
  int nAlloc;
75121
 
  sqlite3 *db = pParse->db;
75122
 
 
75123
 
  /* Look up the table being altered. */
75124
 
  assert( pParse->pNewTable==0 );
75125
 
  assert( sqlite3BtreeHoldsAllMutexes(db) );
75126
 
  if( db->mallocFailed ) goto exit_begin_add_column;
75127
 
  pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
75128
 
  if( !pTab ) goto exit_begin_add_column;
75129
 
 
75130
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
75131
 
  if( IsVirtual(pTab) ){
75132
 
    sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
75133
 
    goto exit_begin_add_column;
75134
 
  }
75135
 
#endif
75136
 
 
75137
 
  /* Make sure this is not an attempt to ALTER a view. */
75138
 
  if( pTab->pSelect ){
75139
 
    sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
75140
 
    goto exit_begin_add_column;
75141
 
  }
75142
 
  if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
75143
 
    goto exit_begin_add_column;
75144
 
  }
75145
 
 
75146
 
  assert( pTab->addColOffset>0 );
75147
 
  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
75148
 
 
75149
 
  /* Put a copy of the Table struct in Parse.pNewTable for the
75150
 
  ** sqlite3AddColumn() function and friends to modify.  But modify
75151
 
  ** the name by adding an "sqlite_altertab_" prefix.  By adding this
75152
 
  ** prefix, we insure that the name will not collide with an existing
75153
 
  ** table because user table are not allowed to have the "sqlite_"
75154
 
  ** prefix on their name.
75155
 
  */
75156
 
  pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
75157
 
  if( !pNew ) goto exit_begin_add_column;
75158
 
  pParse->pNewTable = pNew;
75159
 
  pNew->nRef = 1;
75160
 
  pNew->nCol = pTab->nCol;
75161
 
  assert( pNew->nCol>0 );
75162
 
  nAlloc = (((pNew->nCol-1)/8)*8)+8;
75163
 
  assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
75164
 
  pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
75165
 
  pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
75166
 
  if( !pNew->aCol || !pNew->zName ){
75167
 
    db->mallocFailed = 1;
75168
 
    goto exit_begin_add_column;
75169
 
  }
75170
 
  memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
75171
 
  for(i=0; i<pNew->nCol; i++){
75172
 
    Column *pCol = &pNew->aCol[i];
75173
 
    pCol->zName = sqlite3DbStrDup(db, pCol->zName);
75174
 
    pCol->zColl = 0;
75175
 
    pCol->zType = 0;
75176
 
    pCol->pDflt = 0;
75177
 
    pCol->zDflt = 0;
75178
 
  }
75179
 
  pNew->pSchema = db->aDb[iDb].pSchema;
75180
 
  pNew->addColOffset = pTab->addColOffset;
75181
 
  pNew->nRef = 1;
75182
 
 
75183
 
  /* Begin a transaction and increment the schema cookie.  */
75184
 
  sqlite3BeginWriteOperation(pParse, 0, iDb);
75185
 
  v = sqlite3GetVdbe(pParse);
75186
 
  if( !v ) goto exit_begin_add_column;
75187
 
  sqlite3ChangeCookie(pParse, iDb);
75188
 
 
75189
 
exit_begin_add_column:
75190
 
  sqlite3SrcListDelete(db, pSrc);
75191
 
  return;
75192
 
}
75193
 
#endif  /* SQLITE_ALTER_TABLE */
75194
 
 
75195
 
/************** End of alter.c ***********************************************/
75196
 
/************** Begin file analyze.c *****************************************/
75197
 
/*
75198
 
** 2005 July 8
75199
 
**
75200
 
** The author disclaims copyright to this source code.  In place of
75201
 
** a legal notice, here is a blessing:
75202
 
**
75203
 
**    May you do good and not evil.
75204
 
**    May you find forgiveness for yourself and forgive others.
75205
 
**    May you share freely, never taking more than you give.
75206
 
**
75207
 
*************************************************************************
75208
 
** This file contains code associated with the ANALYZE command.
75209
 
*/
75210
 
#ifndef SQLITE_OMIT_ANALYZE
75211
 
 
75212
 
/*
75213
 
** This routine generates code that opens the sqlite_stat1 table for
75214
 
** writing with cursor iStatCur. If the library was built with the
75215
 
** SQLITE_ENABLE_STAT2 macro defined, then the sqlite_stat2 table is
75216
 
** opened for writing using cursor (iStatCur+1)
75217
 
**
75218
 
** If the sqlite_stat1 tables does not previously exist, it is created.
75219
 
** Similarly, if the sqlite_stat2 table does not exist and the library
75220
 
** is compiled with SQLITE_ENABLE_STAT2 defined, it is created. 
75221
 
**
75222
 
** Argument zWhere may be a pointer to a buffer containing a table name,
75223
 
** or it may be a NULL pointer. If it is not NULL, then all entries in
75224
 
** the sqlite_stat1 and (if applicable) sqlite_stat2 tables associated
75225
 
** with the named table are deleted. If zWhere==0, then code is generated
75226
 
** to delete all stat table entries.
75227
 
*/
75228
 
static void openStatTable(
75229
 
  Parse *pParse,          /* Parsing context */
75230
 
  int iDb,                /* The database we are looking in */
75231
 
  int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
75232
 
  const char *zWhere,     /* Delete entries for this table or index */
75233
 
  const char *zWhereType  /* Either "tbl" or "idx" */
75234
 
){
75235
 
  static const struct {
75236
 
    const char *zName;
75237
 
    const char *zCols;
75238
 
  } aTable[] = {
75239
 
    { "sqlite_stat1", "tbl,idx,stat" },
75240
 
#ifdef SQLITE_ENABLE_STAT2
75241
 
    { "sqlite_stat2", "tbl,idx,sampleno,sample" },
75242
 
#endif
75243
 
  };
75244
 
 
75245
 
  int aRoot[] = {0, 0};
75246
 
  u8 aCreateTbl[] = {0, 0};
75247
 
 
75248
 
  int i;
75249
 
  sqlite3 *db = pParse->db;
75250
 
  Db *pDb;
75251
 
  Vdbe *v = sqlite3GetVdbe(pParse);
75252
 
  if( v==0 ) return;
75253
 
  assert( sqlite3BtreeHoldsAllMutexes(db) );
75254
 
  assert( sqlite3VdbeDb(v)==db );
75255
 
  pDb = &db->aDb[iDb];
75256
 
 
75257
 
  for(i=0; i<ArraySize(aTable); i++){
75258
 
    const char *zTab = aTable[i].zName;
75259
 
    Table *pStat;
75260
 
    if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
75261
 
      /* The sqlite_stat[12] table does not exist. Create it. Note that a 
75262
 
      ** side-effect of the CREATE TABLE statement is to leave the rootpage 
75263
 
      ** of the new table in register pParse->regRoot. This is important 
75264
 
      ** because the OpenWrite opcode below will be needing it. */
75265
 
      sqlite3NestedParse(pParse,
75266
 
          "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
75267
 
      );
75268
 
      aRoot[i] = pParse->regRoot;
75269
 
      aCreateTbl[i] = 1;
75270
 
    }else{
75271
 
      /* The table already exists. If zWhere is not NULL, delete all entries 
75272
 
      ** associated with the table zWhere. If zWhere is NULL, delete the
75273
 
      ** entire contents of the table. */
75274
 
      aRoot[i] = pStat->tnum;
75275
 
      sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
75276
 
      if( zWhere ){
75277
 
        sqlite3NestedParse(pParse,
75278
 
           "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere
75279
 
        );
75280
 
      }else{
75281
 
        /* The sqlite_stat[12] table already exists.  Delete all rows. */
75282
 
        sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
75283
 
      }
75284
 
    }
75285
 
  }
75286
 
 
75287
 
  /* Open the sqlite_stat[12] tables for writing. */
75288
 
  for(i=0; i<ArraySize(aTable); i++){
75289
 
    sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
75290
 
    sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
75291
 
    sqlite3VdbeChangeP5(v, aCreateTbl[i]);
75292
 
  }
75293
 
}
75294
 
 
75295
 
/*
75296
 
** Generate code to do an analysis of all indices associated with
75297
 
** a single table.
75298
 
*/
75299
 
static void analyzeOneTable(
75300
 
  Parse *pParse,   /* Parser context */
75301
 
  Table *pTab,     /* Table whose indices are to be analyzed */
75302
 
  Index *pOnlyIdx, /* If not NULL, only analyze this one index */
75303
 
  int iStatCur,    /* Index of VdbeCursor that writes the sqlite_stat1 table */
75304
 
  int iMem         /* Available memory locations begin here */
75305
 
){
75306
 
  sqlite3 *db = pParse->db;    /* Database handle */
75307
 
  Index *pIdx;                 /* An index to being analyzed */
75308
 
  int iIdxCur;                 /* Cursor open on index being analyzed */
75309
 
  Vdbe *v;                     /* The virtual machine being built up */
75310
 
  int i;                       /* Loop counter */
75311
 
  int topOfLoop;               /* The top of the loop */
75312
 
  int endOfLoop;               /* The end of the loop */
75313
 
  int jZeroRows = -1;          /* Jump from here if number of rows is zero */
75314
 
  int iDb;                     /* Index of database containing pTab */
75315
 
  int regTabname = iMem++;     /* Register containing table name */
75316
 
  int regIdxname = iMem++;     /* Register containing index name */
75317
 
  int regSampleno = iMem++;    /* Register containing next sample number */
75318
 
  int regCol = iMem++;         /* Content of a column analyzed table */
75319
 
  int regRec = iMem++;         /* Register holding completed record */
75320
 
  int regTemp = iMem++;        /* Temporary use register */
75321
 
  int regRowid = iMem++;       /* Rowid for the inserted record */
75322
 
 
75323
 
#ifdef SQLITE_ENABLE_STAT2
75324
 
  int addr = 0;                /* Instruction address */
75325
 
  int regTemp2 = iMem++;       /* Temporary use register */
75326
 
  int regSamplerecno = iMem++; /* Index of next sample to record */
75327
 
  int regRecno = iMem++;       /* Current sample index */
75328
 
  int regLast = iMem++;        /* Index of last sample to record */
75329
 
  int regFirst = iMem++;       /* Index of first sample to record */
75330
 
#endif
75331
 
 
75332
 
  v = sqlite3GetVdbe(pParse);
75333
 
  if( v==0 || NEVER(pTab==0) ){
75334
 
    return;
75335
 
  }
75336
 
  if( pTab->tnum==0 ){
75337
 
    /* Do not gather statistics on views or virtual tables */
75338
 
    return;
75339
 
  }
75340
 
  if( memcmp(pTab->zName, "sqlite_", 7)==0 ){
75341
 
    /* Do not gather statistics on system tables */
75342
 
    return;
75343
 
  }
75344
 
  assert( sqlite3BtreeHoldsAllMutexes(db) );
75345
 
  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
75346
 
  assert( iDb>=0 );
75347
 
  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
75348
 
#ifndef SQLITE_OMIT_AUTHORIZATION
75349
 
  if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
75350
 
      db->aDb[iDb].zName ) ){
75351
 
    return;
75352
 
  }
75353
 
#endif
75354
 
 
75355
 
  /* Establish a read-lock on the table at the shared-cache level. */
75356
 
  sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
75357
 
 
75358
 
  iIdxCur = pParse->nTab++;
75359
 
  sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
75360
 
  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
75361
 
    int nCol;
75362
 
    KeyInfo *pKey;
75363
 
 
75364
 
    if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
75365
 
    nCol = pIdx->nColumn;
75366
 
    pKey = sqlite3IndexKeyinfo(pParse, pIdx);
75367
 
    if( iMem+1+(nCol*2)>pParse->nMem ){
75368
 
      pParse->nMem = iMem+1+(nCol*2);
75369
 
    }
75370
 
 
75371
 
    /* Open a cursor to the index to be analyzed. */
75372
 
    assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
75373
 
    sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
75374
 
        (char *)pKey, P4_KEYINFO_HANDOFF);
75375
 
    VdbeComment((v, "%s", pIdx->zName));
75376
 
 
75377
 
    /* Populate the register containing the index name. */
75378
 
    sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
75379
 
 
75380
 
#ifdef SQLITE_ENABLE_STAT2
75381
 
 
75382
 
    /* If this iteration of the loop is generating code to analyze the
75383
 
    ** first index in the pTab->pIndex list, then register regLast has
75384
 
    ** not been populated. In this case populate it now.  */
75385
 
    if( pTab->pIndex==pIdx ){
75386
 
      sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
75387
 
      sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
75388
 
      sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
75389
 
 
75390
 
      sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
75391
 
      sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
75392
 
      addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
75393
 
      sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
75394
 
      sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
75395
 
      sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
75396
 
      sqlite3VdbeAddOp3(v, OP_Divide,  regTemp2, regLast, regLast);
75397
 
      sqlite3VdbeJumpHere(v, addr);
75398
 
    }
75399
 
 
75400
 
    /* Zero the regSampleno and regRecno registers. */
75401
 
    sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
75402
 
    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
75403
 
    sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
75404
 
#endif
75405
 
 
75406
 
    /* The block of memory cells initialized here is used as follows.
75407
 
    **
75408
 
    **    iMem:                
75409
 
    **        The total number of rows in the table.
75410
 
    **
75411
 
    **    iMem+1 .. iMem+nCol: 
75412
 
    **        Number of distinct entries in index considering the 
75413
 
    **        left-most N columns only, where N is between 1 and nCol, 
75414
 
    **        inclusive.
75415
 
    **
75416
 
    **    iMem+nCol+1 .. Mem+2*nCol:  
75417
 
    **        Previous value of indexed columns, from left to right.
75418
 
    **
75419
 
    ** Cells iMem through iMem+nCol are initialized to 0. The others are 
75420
 
    ** initialized to contain an SQL NULL.
75421
 
    */
75422
 
    for(i=0; i<=nCol; i++){
75423
 
      sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
75424
 
    }
75425
 
    for(i=0; i<nCol; i++){
75426
 
      sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
75427
 
    }
75428
 
 
75429
 
    /* Start the analysis loop. This loop runs through all the entries in
75430
 
    ** the index b-tree.  */
75431
 
    endOfLoop = sqlite3VdbeMakeLabel(v);
75432
 
    sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
75433
 
    topOfLoop = sqlite3VdbeCurrentAddr(v);
75434
 
    sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
75435
 
 
75436
 
    for(i=0; i<nCol; i++){
75437
 
      CollSeq *pColl;
75438
 
      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
75439
 
      if( i==0 ){
75440
 
#ifdef SQLITE_ENABLE_STAT2
75441
 
        /* Check if the record that cursor iIdxCur points to contains a
75442
 
        ** value that should be stored in the sqlite_stat2 table. If so,
75443
 
        ** store it.  */
75444
 
        int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
75445
 
        assert( regTabname+1==regIdxname 
75446
 
             && regTabname+2==regSampleno
75447
 
             && regTabname+3==regCol
75448
 
        );
75449
 
        sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
75450
 
        sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
75451
 
        sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
75452
 
        sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
75453
 
 
75454
 
        /* Calculate new values for regSamplerecno and regSampleno.
75455
 
        **
75456
 
        **   sampleno = sampleno + 1
75457
 
        **   samplerecno = samplerecno+(remaining records)/(remaining samples)
75458
 
        */
75459
 
        sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
75460
 
        sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
75461
 
        sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
75462
 
        sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
75463
 
        sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
75464
 
        sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
75465
 
        sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
75466
 
 
75467
 
        sqlite3VdbeJumpHere(v, ne);
75468
 
        sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
75469
 
#endif
75470
 
 
75471
 
        /* Always record the very first row */
75472
 
        sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
75473
 
      }
75474
 
      assert( pIdx->azColl!=0 );
75475
 
      assert( pIdx->azColl[i]!=0 );
75476
 
      pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
75477
 
      sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
75478
 
                       (char*)pColl, P4_COLLSEQ);
75479
 
      sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
75480
 
    }
75481
 
    if( db->mallocFailed ){
75482
 
      /* If a malloc failure has occurred, then the result of the expression 
75483
 
      ** passed as the second argument to the call to sqlite3VdbeJumpHere() 
75484
 
      ** below may be negative. Which causes an assert() to fail (or an
75485
 
      ** out-of-bounds write if SQLITE_DEBUG is not defined).  */
75486
 
      return;
75487
 
    }
75488
 
    sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
75489
 
    for(i=0; i<nCol; i++){
75490
 
      int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
75491
 
      if( i==0 ){
75492
 
        sqlite3VdbeJumpHere(v, addr2-1);  /* Set jump dest for the OP_IfNot */
75493
 
      }
75494
 
      sqlite3VdbeJumpHere(v, addr2);      /* Set jump dest for the OP_Ne */
75495
 
      sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
75496
 
      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
75497
 
    }
75498
 
 
75499
 
    /* End of the analysis loop. */
75500
 
    sqlite3VdbeResolveLabel(v, endOfLoop);
75501
 
    sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
75502
 
    sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
75503
 
 
75504
 
    /* Store the results in sqlite_stat1.
75505
 
    **
75506
 
    ** The result is a single row of the sqlite_stat1 table.  The first
75507
 
    ** two columns are the names of the table and index.  The third column
75508
 
    ** is a string composed of a list of integer statistics about the
75509
 
    ** index.  The first integer in the list is the total number of entries
75510
 
    ** in the index.  There is one additional integer in the list for each
75511
 
    ** column of the table.  This additional integer is a guess of how many
75512
 
    ** rows of the table the index will select.  If D is the count of distinct
75513
 
    ** values and K is the total number of rows, then the integer is computed
75514
 
    ** as:
75515
 
    **
75516
 
    **        I = (K+D-1)/D
75517
 
    **
75518
 
    ** If K==0 then no entry is made into the sqlite_stat1 table.  
75519
 
    ** If K>0 then it is always the case the D>0 so division by zero
75520
 
    ** is never possible.
75521
 
    */
75522
 
    sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
75523
 
    if( jZeroRows<0 ){
75524
 
      jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
75525
 
    }
75526
 
    for(i=0; i<nCol; i++){
75527
 
      sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
75528
 
      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
75529
 
      sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
75530
 
      sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
75531
 
      sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
75532
 
      sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
75533
 
      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
75534
 
    }
75535
 
    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
75536
 
    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
75537
 
    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
75538
 
    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
75539
 
  }
75540
 
 
75541
 
  /* If the table has no indices, create a single sqlite_stat1 entry
75542
 
  ** containing NULL as the index name and the row count as the content.
75543
 
  */
75544
 
  if( pTab->pIndex==0 ){
75545
 
    sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
75546
 
    VdbeComment((v, "%s", pTab->zName));
75547
 
    sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
75548
 
    sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
75549
 
    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
75550
 
  }else{
75551
 
    sqlite3VdbeJumpHere(v, jZeroRows);
75552
 
    jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
75553
 
  }
75554
 
  sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
75555
 
  sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
75556
 
  sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
75557
 
  sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
75558
 
  sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
75559
 
  if( pParse->nMem<regRec ) pParse->nMem = regRec;
75560
 
  sqlite3VdbeJumpHere(v, jZeroRows);
75561
 
}
75562
 
 
75563
 
/*
75564
 
** Generate code that will cause the most recent index analysis to
75565
 
** be loaded into internal hash tables where is can be used.
75566
 
*/
75567
 
static void loadAnalysis(Parse *pParse, int iDb){
75568
 
  Vdbe *v = sqlite3GetVdbe(pParse);
75569
 
  if( v ){
75570
 
    sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
75571
 
  }
75572
 
}
75573
 
 
75574
 
/*
75575
 
** Generate code that will do an analysis of an entire database
75576
 
*/
75577
 
static void analyzeDatabase(Parse *pParse, int iDb){
75578
 
  sqlite3 *db = pParse->db;
75579
 
  Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
75580
 
  HashElem *k;
75581
 
  int iStatCur;
75582
 
  int iMem;
75583
 
 
75584
 
  sqlite3BeginWriteOperation(pParse, 0, iDb);
75585
 
  iStatCur = pParse->nTab;
75586
 
  pParse->nTab += 2;
75587
 
  openStatTable(pParse, iDb, iStatCur, 0, 0);
75588
 
  iMem = pParse->nMem+1;
75589
 
  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
75590
 
  for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
75591
 
    Table *pTab = (Table*)sqliteHashData(k);
75592
 
    analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
75593
 
  }
75594
 
  loadAnalysis(pParse, iDb);
75595
 
}
75596
 
 
75597
 
/*
75598
 
** Generate code that will do an analysis of a single table in
75599
 
** a database.  If pOnlyIdx is not NULL then it is a single index
75600
 
** in pTab that should be analyzed.
75601
 
*/
75602
 
static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
75603
 
  int iDb;
75604
 
  int iStatCur;
75605
 
 
75606
 
  assert( pTab!=0 );
75607
 
  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
75608
 
  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
75609
 
  sqlite3BeginWriteOperation(pParse, 0, iDb);
75610
 
  iStatCur = pParse->nTab;
75611
 
  pParse->nTab += 2;
75612
 
  if( pOnlyIdx ){
75613
 
    openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
75614
 
  }else{
75615
 
    openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
75616
 
  }
75617
 
  analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
75618
 
  loadAnalysis(pParse, iDb);
75619
 
}
75620
 
 
75621
 
/*
75622
 
** Generate code for the ANALYZE command.  The parser calls this routine
75623
 
** when it recognizes an ANALYZE command.
75624
 
**
75625
 
**        ANALYZE                            -- 1
75626
 
**        ANALYZE  <database>                -- 2
75627
 
**        ANALYZE  ?<database>.?<tablename>  -- 3
75628
 
**
75629
 
** Form 1 causes all indices in all attached databases to be analyzed.
75630
 
** Form 2 analyzes all indices the single database named.
75631
 
** Form 3 analyzes all indices associated with the named table.
75632
 
*/
75633
 
SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
75634
 
  sqlite3 *db = pParse->db;
75635
 
  int iDb;
75636
 
  int i;
75637
 
  char *z, *zDb;
75638
 
  Table *pTab;
75639
 
  Index *pIdx;
75640
 
  Token *pTableName;
75641
 
 
75642
 
  /* Read the database schema. If an error occurs, leave an error message
75643
 
  ** and code in pParse and return NULL. */
75644
 
  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
75645
 
  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
75646
 
    return;
75647
 
  }
75648
 
 
75649
 
  assert( pName2!=0 || pName1==0 );
75650
 
  if( pName1==0 ){
75651
 
    /* Form 1:  Analyze everything */
75652
 
    for(i=0; i<db->nDb; i++){
75653
 
      if( i==1 ) continue;  /* Do not analyze the TEMP database */
75654
 
      analyzeDatabase(pParse, i);
75655
 
    }
75656
 
  }else if( pName2->n==0 ){
75657
 
    /* Form 2:  Analyze the database or table named */
75658
 
    iDb = sqlite3FindDb(db, pName1);
75659
 
    if( iDb>=0 ){
75660
 
      analyzeDatabase(pParse, iDb);
75661
 
    }else{
75662
 
      z = sqlite3NameFromToken(db, pName1);
75663
 
      if( z ){
75664
 
        if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
75665
 
          analyzeTable(pParse, pIdx->pTable, pIdx);
75666
 
        }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
75667
 
          analyzeTable(pParse, pTab, 0);
75668
 
        }
75669
 
        sqlite3DbFree(db, z);
75670
 
      }
75671
 
    }
75672
 
  }else{
75673
 
    /* Form 3: Analyze the fully qualified table name */
75674
 
    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
75675
 
    if( iDb>=0 ){
75676
 
      zDb = db->aDb[iDb].zName;
75677
 
      z = sqlite3NameFromToken(db, pTableName);
75678
 
      if( z ){
75679
 
        if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
75680
 
          analyzeTable(pParse, pIdx->pTable, pIdx);
75681
 
        }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
75682
 
          analyzeTable(pParse, pTab, 0);
75683
 
        }
75684
 
        sqlite3DbFree(db, z);
75685
 
      }
75686
 
    }   
75687
 
  }
75688
 
}
75689
 
 
75690
 
/*
75691
 
** Used to pass information from the analyzer reader through to the
75692
 
** callback routine.
75693
 
*/
75694
 
typedef struct analysisInfo analysisInfo;
75695
 
struct analysisInfo {
75696
 
  sqlite3 *db;
75697
 
  const char *zDatabase;
75698
 
};
75699
 
 
75700
 
/*
75701
 
** This callback is invoked once for each index when reading the
75702
 
** sqlite_stat1 table.  
75703
 
**
75704
 
**     argv[0] = name of the table
75705
 
**     argv[1] = name of the index (might be NULL)
75706
 
**     argv[2] = results of analysis - on integer for each column
75707
 
**
75708
 
** Entries for which argv[1]==NULL simply record the number of rows in
75709
 
** the table.
75710
 
*/
75711
 
static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
75712
 
  analysisInfo *pInfo = (analysisInfo*)pData;
75713
 
  Index *pIndex;
75714
 
  Table *pTable;
75715
 
  int i, c, n;
75716
 
  unsigned int v;
75717
 
  const char *z;
75718
 
 
75719
 
  assert( argc==3 );
75720
 
  UNUSED_PARAMETER2(NotUsed, argc);
75721
 
 
75722
 
  if( argv==0 || argv[0]==0 || argv[2]==0 ){
75723
 
    return 0;
75724
 
  }
75725
 
  pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
75726
 
  if( pTable==0 ){
75727
 
    return 0;
75728
 
  }
75729
 
  if( argv[1] ){
75730
 
    pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
75731
 
  }else{
75732
 
    pIndex = 0;
75733
 
  }
75734
 
  n = pIndex ? pIndex->nColumn : 0;
75735
 
  z = argv[2];
75736
 
  for(i=0; *z && i<=n; i++){
75737
 
    v = 0;
75738
 
    while( (c=z[0])>='0' && c<='9' ){
75739
 
      v = v*10 + c - '0';
75740
 
      z++;
75741
 
    }
75742
 
    if( i==0 ) pTable->nRowEst = v;
75743
 
    if( pIndex==0 ) break;
75744
 
    pIndex->aiRowEst[i] = v;
75745
 
    if( *z==' ' ) z++;
75746
 
    if( memcmp(z, "unordered", 10)==0 ){
75747
 
      pIndex->bUnordered = 1;
75748
 
      break;
75749
 
    }
75750
 
  }
75751
 
  return 0;
75752
 
}
75753
 
 
75754
 
/*
75755
 
** If the Index.aSample variable is not NULL, delete the aSample[] array
75756
 
** and its contents.
75757
 
*/
75758
 
SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
75759
 
#ifdef SQLITE_ENABLE_STAT2
75760
 
  if( pIdx->aSample ){
75761
 
    int j;
75762
 
    for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
75763
 
      IndexSample *p = &pIdx->aSample[j];
75764
 
      if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
75765
 
        sqlite3DbFree(db, p->u.z);
75766
 
      }
75767
 
    }
75768
 
    sqlite3DbFree(db, pIdx->aSample);
75769
 
  }
75770
 
#else
75771
 
  UNUSED_PARAMETER(db);
75772
 
  UNUSED_PARAMETER(pIdx);
75773
 
#endif
75774
 
}
75775
 
 
75776
 
/*
75777
 
** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
75778
 
** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
75779
 
** arrays. The contents of sqlite_stat2 are used to populate the
75780
 
** Index.aSample[] arrays.
75781
 
**
75782
 
** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
75783
 
** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined 
75784
 
** during compilation and the sqlite_stat2 table is present, no data is 
75785
 
** read from it.
75786
 
**
75787
 
** If SQLITE_ENABLE_STAT2 was defined during compilation and the 
75788
 
** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
75789
 
** returned. However, in this case, data is read from the sqlite_stat1
75790
 
** table (if it is present) before returning.
75791
 
**
75792
 
** If an OOM error occurs, this function always sets db->mallocFailed.
75793
 
** This means if the caller does not care about other errors, the return
75794
 
** code may be ignored.
75795
 
*/
75796
 
SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
75797
 
  analysisInfo sInfo;
75798
 
  HashElem *i;
75799
 
  char *zSql;
75800
 
  int rc;
75801
 
 
75802
 
  assert( iDb>=0 && iDb<db->nDb );
75803
 
  assert( db->aDb[iDb].pBt!=0 );
75804
 
 
75805
 
  /* Clear any prior statistics */
75806
 
  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
75807
 
  for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
75808
 
    Index *pIdx = sqliteHashData(i);
75809
 
    sqlite3DefaultRowEst(pIdx);
75810
 
    sqlite3DeleteIndexSamples(db, pIdx);
75811
 
    pIdx->aSample = 0;
75812
 
  }
75813
 
 
75814
 
  /* Check to make sure the sqlite_stat1 table exists */
75815
 
  sInfo.db = db;
75816
 
  sInfo.zDatabase = db->aDb[iDb].zName;
75817
 
  if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
75818
 
    return SQLITE_ERROR;
75819
 
  }
75820
 
 
75821
 
  /* Load new statistics out of the sqlite_stat1 table */
75822
 
  zSql = sqlite3MPrintf(db, 
75823
 
      "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
75824
 
  if( zSql==0 ){
75825
 
    rc = SQLITE_NOMEM;
75826
 
  }else{
75827
 
    rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
75828
 
    sqlite3DbFree(db, zSql);
75829
 
  }
75830
 
 
75831
 
 
75832
 
  /* Load the statistics from the sqlite_stat2 table. */
75833
 
#ifdef SQLITE_ENABLE_STAT2
75834
 
  if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
75835
 
    rc = SQLITE_ERROR;
75836
 
  }
75837
 
  if( rc==SQLITE_OK ){
75838
 
    sqlite3_stmt *pStmt = 0;
75839
 
 
75840
 
    zSql = sqlite3MPrintf(db, 
75841
 
        "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
75842
 
    if( !zSql ){
75843
 
      rc = SQLITE_NOMEM;
75844
 
    }else{
75845
 
      rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
75846
 
      sqlite3DbFree(db, zSql);
75847
 
    }
75848
 
 
75849
 
    if( rc==SQLITE_OK ){
75850
 
      while( sqlite3_step(pStmt)==SQLITE_ROW ){
75851
 
        char *zIndex;   /* Index name */
75852
 
        Index *pIdx;    /* Pointer to the index object */
75853
 
 
75854
 
        zIndex = (char *)sqlite3_column_text(pStmt, 0);
75855
 
        pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0;
75856
 
        if( pIdx ){
75857
 
          int iSample = sqlite3_column_int(pStmt, 1);
75858
 
          if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
75859
 
            int eType = sqlite3_column_type(pStmt, 2);
75860
 
 
75861
 
            if( pIdx->aSample==0 ){
75862
 
              static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
75863
 
              pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz);
75864
 
              if( pIdx->aSample==0 ){
75865
 
                db->mallocFailed = 1;
75866
 
                break;
75867
 
              }
75868
 
              memset(pIdx->aSample, 0, sz);
75869
 
            }
75870
 
 
75871
 
            assert( pIdx->aSample );
75872
 
            {
75873
 
              IndexSample *pSample = &pIdx->aSample[iSample];
75874
 
              pSample->eType = (u8)eType;
75875
 
              if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
75876
 
                pSample->u.r = sqlite3_column_double(pStmt, 2);
75877
 
              }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
75878
 
                const char *z = (const char *)(
75879
 
                    (eType==SQLITE_BLOB) ?
75880
 
                    sqlite3_column_blob(pStmt, 2):
75881
 
                    sqlite3_column_text(pStmt, 2)
75882
 
                );
75883
 
                int n = sqlite3_column_bytes(pStmt, 2);
75884
 
                if( n>24 ){
75885
 
                  n = 24;
75886
 
                }
75887
 
                pSample->nByte = (u8)n;
75888
 
                if( n < 1){
75889
 
                  pSample->u.z = 0;
75890
 
                }else{
75891
 
                  pSample->u.z = sqlite3DbStrNDup(0, z, n);
75892
 
                  if( pSample->u.z==0 ){
75893
 
                    db->mallocFailed = 1;
75894
 
                    break;
75895
 
                  }
75896
 
                }
75897
 
              }
75898
 
            }
75899
 
          }
75900
 
        }
75901
 
      }
75902
 
      rc = sqlite3_finalize(pStmt);
75903
 
    }
75904
 
  }
75905
 
#endif
75906
 
 
75907
 
  if( rc==SQLITE_NOMEM ){
75908
 
    db->mallocFailed = 1;
75909
 
  }
75910
 
  return rc;
75911
 
}
75912
 
 
75913
 
 
75914
 
#endif /* SQLITE_OMIT_ANALYZE */
75915
 
 
75916
 
/************** End of analyze.c *********************************************/
75917
 
/************** Begin file attach.c ******************************************/
75918
 
/*
75919
 
** 2003 April 6
75920
 
**
75921
 
** The author disclaims copyright to this source code.  In place of
75922
 
** a legal notice, here is a blessing:
75923
 
**
75924
 
**    May you do good and not evil.
75925
 
**    May you find forgiveness for yourself and forgive others.
75926
 
**    May you share freely, never taking more than you give.
75927
 
**
75928
 
*************************************************************************
75929
 
** This file contains code used to implement the ATTACH and DETACH commands.
75930
 
*/
75931
 
 
75932
 
#ifndef SQLITE_OMIT_ATTACH
75933
 
/*
75934
 
** Resolve an expression that was part of an ATTACH or DETACH statement. This
75935
 
** is slightly different from resolving a normal SQL expression, because simple
75936
 
** identifiers are treated as strings, not possible column names or aliases.
75937
 
**
75938
 
** i.e. if the parser sees:
75939
 
**
75940
 
**     ATTACH DATABASE abc AS def
75941
 
**
75942
 
** it treats the two expressions as literal strings 'abc' and 'def' instead of
75943
 
** looking for columns of the same name.
75944
 
**
75945
 
** This only applies to the root node of pExpr, so the statement:
75946
 
**
75947
 
**     ATTACH DATABASE abc||def AS 'db2'
75948
 
**
75949
 
** will fail because neither abc or def can be resolved.
75950
 
*/
75951
 
static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
75952
 
{
75953
 
  int rc = SQLITE_OK;
75954
 
  if( pExpr ){
75955
 
    if( pExpr->op!=TK_ID ){
75956
 
      rc = sqlite3ResolveExprNames(pName, pExpr);
75957
 
      if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){
75958
 
        sqlite3ErrorMsg(pName->pParse, "invalid name: \"%s\"", pExpr->u.zToken);
75959
 
        return SQLITE_ERROR;
75960
 
      }
75961
 
    }else{
75962
 
      pExpr->op = TK_STRING;
75963
 
    }
75964
 
  }
75965
 
  return rc;
75966
 
}
75967
 
 
75968
 
/*
75969
 
** An SQL user-function registered to do the work of an ATTACH statement. The
75970
 
** three arguments to the function come directly from an attach statement:
75971
 
**
75972
 
**     ATTACH DATABASE x AS y KEY z
75973
 
**
75974
 
**     SELECT sqlite_attach(x, y, z)
75975
 
**
75976
 
** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
75977
 
** third argument.
75978
 
*/
75979
 
static void attachFunc(
75980
 
  sqlite3_context *context,
75981
 
  int NotUsed,
75982
 
  sqlite3_value **argv
75983
 
){
75984
 
  int i;
75985
 
  int rc = 0;
75986
 
  sqlite3 *db = sqlite3_context_db_handle(context);
75987
 
  const char *zName;
75988
 
  const char *zFile;
75989
 
  Db *aNew;
75990
 
  char *zErrDyn = 0;
75991
 
 
75992
 
  UNUSED_PARAMETER(NotUsed);
75993
 
 
75994
 
  zFile = (const char *)sqlite3_value_text(argv[0]);
75995
 
  zName = (const char *)sqlite3_value_text(argv[1]);
75996
 
  if( zFile==0 ) zFile = "";
75997
 
  if( zName==0 ) zName = "";
75998
 
 
75999
 
  /* Check for the following errors:
76000
 
  **
76001
 
  **     * Too many attached databases,
76002
 
  **     * Transaction currently open
76003
 
  **     * Specified database name already being used.
76004
 
  */
76005
 
  if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
76006
 
    zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
76007
 
      db->aLimit[SQLITE_LIMIT_ATTACHED]
76008
 
    );
76009
 
    goto attach_error;
76010
 
  }
76011
 
  if( !db->autoCommit ){
76012
 
    zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
76013
 
    goto attach_error;
76014
 
  }
76015
 
  for(i=0; i<db->nDb; i++){
76016
 
    char *z = db->aDb[i].zName;
76017
 
    assert( z && zName );
76018
 
    if( sqlite3StrICmp(z, zName)==0 ){
76019
 
      zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
76020
 
      goto attach_error;
76021
 
    }
76022
 
  }
76023
 
 
76024
 
  /* Allocate the new entry in the db->aDb[] array and initialise the schema
76025
 
  ** hash tables.
76026
 
  */
76027
 
  if( db->aDb==db->aDbStatic ){
76028
 
    aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
76029
 
    if( aNew==0 ) return;
76030
 
    memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
76031
 
  }else{
76032
 
    aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
76033
 
    if( aNew==0 ) return;
76034
 
  }
76035
 
  db->aDb = aNew;
76036
 
  aNew = &db->aDb[db->nDb];
76037
 
  memset(aNew, 0, sizeof(*aNew));
76038
 
 
76039
 
  /* Open the database file. If the btree is successfully opened, use
76040
 
  ** it to obtain the database schema. At this point the schema may
76041
 
  ** or may not be initialised.
76042
 
  */
76043
 
  rc = sqlite3BtreeOpen(zFile, db, &aNew->pBt, 0,
76044
 
                        db->openFlags | SQLITE_OPEN_MAIN_DB);
76045
 
  db->nDb++;
76046
 
  if( rc==SQLITE_CONSTRAINT ){
76047
 
    rc = SQLITE_ERROR;
76048
 
    zErrDyn = sqlite3MPrintf(db, "database is already attached");
76049
 
  }else if( rc==SQLITE_OK ){
76050
 
    Pager *pPager;
76051
 
    aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
76052
 
    if( !aNew->pSchema ){
76053
 
      rc = SQLITE_NOMEM;
76054
 
    }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
76055
 
      zErrDyn = sqlite3MPrintf(db, 
76056
 
        "attached databases must use the same text encoding as main database");
76057
 
      rc = SQLITE_ERROR;
76058
 
    }
76059
 
    pPager = sqlite3BtreePager(aNew->pBt);
76060
 
    sqlite3PagerLockingMode(pPager, db->dfltLockMode);
76061
 
    sqlite3BtreeSecureDelete(aNew->pBt,
76062
 
                             sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
76063
 
  }
76064
 
  aNew->safety_level = 3;
76065
 
  aNew->zName = sqlite3DbStrDup(db, zName);
76066
 
  if( rc==SQLITE_OK && aNew->zName==0 ){
76067
 
    rc = SQLITE_NOMEM;
76068
 
  }
76069
 
 
76070
 
 
76071
 
#ifdef SQLITE_HAS_CODEC
76072
 
  if( rc==SQLITE_OK ){
76073
 
    extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
76074
 
    extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
76075
 
    int nKey;
76076
 
    char *zKey;
76077
 
    int t = sqlite3_value_type(argv[2]);
76078
 
    switch( t ){
76079
 
      case SQLITE_INTEGER:
76080
 
      case SQLITE_FLOAT:
76081
 
        zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
76082
 
        rc = SQLITE_ERROR;
76083
 
        break;
76084
 
        
76085
 
      case SQLITE_TEXT:
76086
 
      case SQLITE_BLOB:
76087
 
        nKey = sqlite3_value_bytes(argv[2]);
76088
 
        zKey = (char *)sqlite3_value_blob(argv[2]);
76089
 
        rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
76090
 
        break;
76091
 
 
76092
 
      case SQLITE_NULL:
76093
 
        /* No key specified.  Use the key from the main database */
76094
 
        sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
76095
 
        if( nKey>0 || sqlite3BtreeGetReserve(db->aDb[0].pBt)>0 ){
76096
 
          rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
76097
 
        }
76098
 
        break;
76099
 
    }
76100
 
  }
76101
 
#endif
76102
 
 
76103
 
  /* If the file was opened successfully, read the schema for the new database.
76104
 
  ** If this fails, or if opening the file failed, then close the file and 
76105
 
  ** remove the entry from the db->aDb[] array. i.e. put everything back the way
76106
 
  ** we found it.
76107
 
  */
76108
 
  if( rc==SQLITE_OK ){
76109
 
    sqlite3BtreeEnterAll(db);
76110
 
    rc = sqlite3Init(db, &zErrDyn);
76111
 
    sqlite3BtreeLeaveAll(db);
76112
 
  }
76113
 
  if( rc ){
76114
 
    int iDb = db->nDb - 1;
76115
 
    assert( iDb>=2 );
76116
 
    if( db->aDb[iDb].pBt ){
76117
 
      sqlite3BtreeClose(db->aDb[iDb].pBt);
76118
 
      db->aDb[iDb].pBt = 0;
76119
 
      db->aDb[iDb].pSchema = 0;
76120
 
    }
76121
 
    sqlite3ResetInternalSchema(db, -1);
76122
 
    db->nDb = iDb;
76123
 
    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
76124
 
      db->mallocFailed = 1;
76125
 
      sqlite3DbFree(db, zErrDyn);
76126
 
      zErrDyn = sqlite3MPrintf(db, "out of memory");
76127
 
    }else if( zErrDyn==0 ){
76128
 
      zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
76129
 
    }
76130
 
    goto attach_error;
76131
 
  }
76132
 
  
76133
 
  return;
76134
 
 
76135
 
attach_error:
76136
 
  /* Return an error if we get here */
76137
 
  if( zErrDyn ){
76138
 
    sqlite3_result_error(context, zErrDyn, -1);
76139
 
    sqlite3DbFree(db, zErrDyn);
76140
 
  }
76141
 
  if( rc ) sqlite3_result_error_code(context, rc);
76142
 
}
76143
 
 
76144
 
/*
76145
 
** An SQL user-function registered to do the work of an DETACH statement. The
76146
 
** three arguments to the function come directly from a detach statement:
76147
 
**
76148
 
**     DETACH DATABASE x
76149
 
**
76150
 
**     SELECT sqlite_detach(x)
76151
 
*/
76152
 
static void detachFunc(
76153
 
  sqlite3_context *context,
76154
 
  int NotUsed,
76155
 
  sqlite3_value **argv
76156
 
){
76157
 
  const char *zName = (const char *)sqlite3_value_text(argv[0]);
76158
 
  sqlite3 *db = sqlite3_context_db_handle(context);
76159
 
  int i;
76160
 
  Db *pDb = 0;
76161
 
  char zErr[128];
76162
 
 
76163
 
  UNUSED_PARAMETER(NotUsed);
76164
 
 
76165
 
  if( zName==0 ) zName = "";
76166
 
  for(i=0; i<db->nDb; i++){
76167
 
    pDb = &db->aDb[i];
76168
 
    if( pDb->pBt==0 ) continue;
76169
 
    if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
76170
 
  }
76171
 
 
76172
 
  if( i>=db->nDb ){
76173
 
    sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
76174
 
    goto detach_error;
76175
 
  }
76176
 
  if( i<2 ){
76177
 
    sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
76178
 
    goto detach_error;
76179
 
  }
76180
 
  if( !db->autoCommit ){
76181
 
    sqlite3_snprintf(sizeof(zErr), zErr,
76182
 
                     "cannot DETACH database within transaction");
76183
 
    goto detach_error;
76184
 
  }
76185
 
  if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
76186
 
    sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
76187
 
    goto detach_error;
76188
 
  }
76189
 
 
76190
 
  sqlite3BtreeClose(pDb->pBt);
76191
 
  pDb->pBt = 0;
76192
 
  pDb->pSchema = 0;
76193
 
  sqlite3ResetInternalSchema(db, -1);
76194
 
  return;
76195
 
 
76196
 
detach_error:
76197
 
  sqlite3_result_error(context, zErr, -1);
76198
 
}
76199
 
 
76200
 
/*
76201
 
** This procedure generates VDBE code for a single invocation of either the
76202
 
** sqlite_detach() or sqlite_attach() SQL user functions.
76203
 
*/
76204
 
static void codeAttach(
76205
 
  Parse *pParse,       /* The parser context */
76206
 
  int type,            /* Either SQLITE_ATTACH or SQLITE_DETACH */
76207
 
  FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
76208
 
  Expr *pAuthArg,      /* Expression to pass to authorization callback */
76209
 
  Expr *pFilename,     /* Name of database file */
76210
 
  Expr *pDbname,       /* Name of the database to use internally */
76211
 
  Expr *pKey           /* Database key for encryption extension */
76212
 
){
76213
 
  int rc;
76214
 
  NameContext sName;
76215
 
  Vdbe *v;
76216
 
  sqlite3* db = pParse->db;
76217
 
  int regArgs;
76218
 
 
76219
 
  memset(&sName, 0, sizeof(NameContext));
76220
 
  sName.pParse = pParse;
76221
 
 
76222
 
  if( 
76223
 
      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
76224
 
      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
76225
 
      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
76226
 
  ){
76227
 
    pParse->nErr++;
76228
 
    goto attach_end;
76229
 
  }
76230
 
 
76231
 
#ifndef SQLITE_OMIT_AUTHORIZATION
76232
 
  if( pAuthArg ){
76233
 
    char *zAuthArg;
76234
 
    if( pAuthArg->op==TK_STRING ){
76235
 
      zAuthArg = pAuthArg->u.zToken;
76236
 
    }else{
76237
 
      zAuthArg = 0;
76238
 
    }
76239
 
    rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
76240
 
    if(rc!=SQLITE_OK ){
76241
 
      goto attach_end;
76242
 
    }
76243
 
  }
76244
 
#endif /* SQLITE_OMIT_AUTHORIZATION */
76245
 
 
76246
 
 
76247
 
  v = sqlite3GetVdbe(pParse);
76248
 
  regArgs = sqlite3GetTempRange(pParse, 4);
76249
 
  sqlite3ExprCode(pParse, pFilename, regArgs);
76250
 
  sqlite3ExprCode(pParse, pDbname, regArgs+1);
76251
 
  sqlite3ExprCode(pParse, pKey, regArgs+2);
76252
 
 
76253
 
  assert( v || db->mallocFailed );
76254
 
  if( v ){
76255
 
    sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
76256
 
    assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
76257
 
    sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
76258
 
    sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
76259
 
 
76260
 
    /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
76261
 
    ** statement only). For DETACH, set it to false (expire all existing
76262
 
    ** statements).
76263
 
    */
76264
 
    sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
76265
 
  }
76266
 
  
76267
 
attach_end:
76268
 
  sqlite3ExprDelete(db, pFilename);
76269
 
  sqlite3ExprDelete(db, pDbname);
76270
 
  sqlite3ExprDelete(db, pKey);
76271
 
}
76272
 
 
76273
 
/*
76274
 
** Called by the parser to compile a DETACH statement.
76275
 
**
76276
 
**     DETACH pDbname
76277
 
*/
76278
 
SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
76279
 
  static const FuncDef detach_func = {
76280
 
    1,                /* nArg */
76281
 
    SQLITE_UTF8,      /* iPrefEnc */
76282
 
    0,                /* flags */
76283
 
    0,                /* pUserData */
76284
 
    0,                /* pNext */
76285
 
    detachFunc,       /* xFunc */
76286
 
    0,                /* xStep */
76287
 
    0,                /* xFinalize */
76288
 
    "sqlite_detach",  /* zName */
76289
 
    0,                /* pHash */
76290
 
    0                 /* pDestructor */
76291
 
  };
76292
 
  codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
76293
 
}
76294
 
 
76295
 
/*
76296
 
** Called by the parser to compile an ATTACH statement.
76297
 
**
76298
 
**     ATTACH p AS pDbname KEY pKey
76299
 
*/
76300
 
SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
76301
 
  static const FuncDef attach_func = {
76302
 
    3,                /* nArg */
76303
 
    SQLITE_UTF8,      /* iPrefEnc */
76304
 
    0,                /* flags */
76305
 
    0,                /* pUserData */
76306
 
    0,                /* pNext */
76307
 
    attachFunc,       /* xFunc */
76308
 
    0,                /* xStep */
76309
 
    0,                /* xFinalize */
76310
 
    "sqlite_attach",  /* zName */
76311
 
    0,                /* pHash */
76312
 
    0                 /* pDestructor */
76313
 
  };
76314
 
  codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
76315
 
}
76316
 
#endif /* SQLITE_OMIT_ATTACH */
76317
 
 
76318
 
/*
76319
 
** Initialize a DbFixer structure.  This routine must be called prior
76320
 
** to passing the structure to one of the sqliteFixAAAA() routines below.
76321
 
**
76322
 
** The return value indicates whether or not fixation is required.  TRUE
76323
 
** means we do need to fix the database references, FALSE means we do not.
76324
 
*/
76325
 
SQLITE_PRIVATE int sqlite3FixInit(
76326
 
  DbFixer *pFix,      /* The fixer to be initialized */
76327
 
  Parse *pParse,      /* Error messages will be written here */
76328
 
  int iDb,            /* This is the database that must be used */
76329
 
  const char *zType,  /* "view", "trigger", or "index" */
76330
 
  const Token *pName  /* Name of the view, trigger, or index */
76331
 
){
76332
 
  sqlite3 *db;
76333
 
 
76334
 
  if( NEVER(iDb<0) || iDb==1 ) return 0;
76335
 
  db = pParse->db;
76336
 
  assert( db->nDb>iDb );
76337
 
  pFix->pParse = pParse;
76338
 
  pFix->zDb = db->aDb[iDb].zName;
76339
 
  pFix->zType = zType;
76340
 
  pFix->pName = pName;
76341
 
  return 1;
76342
 
}
76343
 
 
76344
 
/*
76345
 
** The following set of routines walk through the parse tree and assign
76346
 
** a specific database to all table references where the database name
76347
 
** was left unspecified in the original SQL statement.  The pFix structure
76348
 
** must have been initialized by a prior call to sqlite3FixInit().
76349
 
**
76350
 
** These routines are used to make sure that an index, trigger, or
76351
 
** view in one database does not refer to objects in a different database.
76352
 
** (Exception: indices, triggers, and views in the TEMP database are
76353
 
** allowed to refer to anything.)  If a reference is explicitly made
76354
 
** to an object in a different database, an error message is added to
76355
 
** pParse->zErrMsg and these routines return non-zero.  If everything
76356
 
** checks out, these routines return 0.
76357
 
*/
76358
 
SQLITE_PRIVATE int sqlite3FixSrcList(
76359
 
  DbFixer *pFix,       /* Context of the fixation */
76360
 
  SrcList *pList       /* The Source list to check and modify */
76361
 
){
76362
 
  int i;
76363
 
  const char *zDb;
76364
 
  struct SrcList_item *pItem;
76365
 
 
76366
 
  if( NEVER(pList==0) ) return 0;
76367
 
  zDb = pFix->zDb;
76368
 
  for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
76369
 
    if( pItem->zDatabase==0 ){
76370
 
      pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb);
76371
 
    }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){
76372
 
      sqlite3ErrorMsg(pFix->pParse,
76373
 
         "%s %T cannot reference objects in database %s",
76374
 
         pFix->zType, pFix->pName, pItem->zDatabase);
76375
 
      return 1;
76376
 
    }
76377
 
#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
76378
 
    if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
76379
 
    if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
76380
 
#endif
76381
 
  }
76382
 
  return 0;
76383
 
}
76384
 
#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
76385
 
SQLITE_PRIVATE int sqlite3FixSelect(
76386
 
  DbFixer *pFix,       /* Context of the fixation */
76387
 
  Select *pSelect      /* The SELECT statement to be fixed to one database */
76388
 
){
76389
 
  while( pSelect ){
76390
 
    if( sqlite3FixExprList(pFix, pSelect->pEList) ){
76391
 
      return 1;
76392
 
    }
76393
 
    if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
76394
 
      return 1;
76395
 
    }
76396
 
    if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
76397
 
      return 1;
76398
 
    }
76399
 
    if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
76400
 
      return 1;
76401
 
    }
76402
 
    pSelect = pSelect->pPrior;
76403
 
  }
76404
 
  return 0;
76405
 
}
76406
 
SQLITE_PRIVATE int sqlite3FixExpr(
76407
 
  DbFixer *pFix,     /* Context of the fixation */
76408
 
  Expr *pExpr        /* The expression to be fixed to one database */
76409
 
){
76410
 
  while( pExpr ){
76411
 
    if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ) break;
76412
 
    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
76413
 
      if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
76414
 
    }else{
76415
 
      if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
76416
 
    }
76417
 
    if( sqlite3FixExpr(pFix, pExpr->pRight) ){
76418
 
      return 1;
76419
 
    }
76420
 
    pExpr = pExpr->pLeft;
76421
 
  }
76422
 
  return 0;
76423
 
}
76424
 
SQLITE_PRIVATE int sqlite3FixExprList(
76425
 
  DbFixer *pFix,     /* Context of the fixation */
76426
 
  ExprList *pList    /* The expression to be fixed to one database */
76427
 
){
76428
 
  int i;
76429
 
  struct ExprList_item *pItem;
76430
 
  if( pList==0 ) return 0;
76431
 
  for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
76432
 
    if( sqlite3FixExpr(pFix, pItem->pExpr) ){
76433
 
      return 1;
76434
 
    }
76435
 
  }
76436
 
  return 0;
76437
 
}
76438
 
#endif
76439
 
 
76440
 
#ifndef SQLITE_OMIT_TRIGGER
76441
 
SQLITE_PRIVATE int sqlite3FixTriggerStep(
76442
 
  DbFixer *pFix,     /* Context of the fixation */
76443
 
  TriggerStep *pStep /* The trigger step be fixed to one database */
76444
 
){
76445
 
  while( pStep ){
76446
 
    if( sqlite3FixSelect(pFix, pStep->pSelect) ){
76447
 
      return 1;
76448
 
    }
76449
 
    if( sqlite3FixExpr(pFix, pStep->pWhere) ){
76450
 
      return 1;
76451
 
    }
76452
 
    if( sqlite3FixExprList(pFix, pStep->pExprList) ){
76453
 
      return 1;
76454
 
    }
76455
 
    pStep = pStep->pNext;
76456
 
  }
76457
 
  return 0;
76458
 
}
76459
 
#endif
76460
 
 
76461
 
/************** End of attach.c **********************************************/
76462
 
/************** Begin file auth.c ********************************************/
76463
 
/*
76464
 
** 2003 January 11
76465
 
**
76466
 
** The author disclaims copyright to this source code.  In place of
76467
 
** a legal notice, here is a blessing:
76468
 
**
76469
 
**    May you do good and not evil.
76470
 
**    May you find forgiveness for yourself and forgive others.
76471
 
**    May you share freely, never taking more than you give.
76472
 
**
76473
 
*************************************************************************
76474
 
** This file contains code used to implement the sqlite3_set_authorizer()
76475
 
** API.  This facility is an optional feature of the library.  Embedded
76476
 
** systems that do not need this facility may omit it by recompiling
76477
 
** the library with -DSQLITE_OMIT_AUTHORIZATION=1
76478
 
*/
76479
 
 
76480
 
/*
76481
 
** All of the code in this file may be omitted by defining a single
76482
 
** macro.
76483
 
*/
76484
 
#ifndef SQLITE_OMIT_AUTHORIZATION
76485
 
 
76486
 
/*
76487
 
** Set or clear the access authorization function.
76488
 
**
76489
 
** The access authorization function is be called during the compilation
76490
 
** phase to verify that the user has read and/or write access permission on
76491
 
** various fields of the database.  The first argument to the auth function
76492
 
** is a copy of the 3rd argument to this routine.  The second argument
76493
 
** to the auth function is one of these constants:
76494
 
**
76495
 
**       SQLITE_CREATE_INDEX
76496
 
**       SQLITE_CREATE_TABLE
76497
 
**       SQLITE_CREATE_TEMP_INDEX
76498
 
**       SQLITE_CREATE_TEMP_TABLE
76499
 
**       SQLITE_CREATE_TEMP_TRIGGER
76500
 
**       SQLITE_CREATE_TEMP_VIEW
76501
 
**       SQLITE_CREATE_TRIGGER
76502
 
**       SQLITE_CREATE_VIEW
76503
 
**       SQLITE_DELETE
76504
 
**       SQLITE_DROP_INDEX
76505
 
**       SQLITE_DROP_TABLE
76506
 
**       SQLITE_DROP_TEMP_INDEX
76507
 
**       SQLITE_DROP_TEMP_TABLE
76508
 
**       SQLITE_DROP_TEMP_TRIGGER
76509
 
**       SQLITE_DROP_TEMP_VIEW
76510
 
**       SQLITE_DROP_TRIGGER
76511
 
**       SQLITE_DROP_VIEW
76512
 
**       SQLITE_INSERT
76513
 
**       SQLITE_PRAGMA
76514
 
**       SQLITE_READ
76515
 
**       SQLITE_SELECT
76516
 
**       SQLITE_TRANSACTION
76517
 
**       SQLITE_UPDATE
76518
 
**
76519
 
** The third and fourth arguments to the auth function are the name of
76520
 
** the table and the column that are being accessed.  The auth function
76521
 
** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE.  If
76522
 
** SQLITE_OK is returned, it means that access is allowed.  SQLITE_DENY
76523
 
** means that the SQL statement will never-run - the sqlite3_exec() call
76524
 
** will return with an error.  SQLITE_IGNORE means that the SQL statement
76525
 
** should run but attempts to read the specified column will return NULL
76526
 
** and attempts to write the column will be ignored.
76527
 
**
76528
 
** Setting the auth function to NULL disables this hook.  The default
76529
 
** setting of the auth function is NULL.
76530
 
*/
76531
 
SQLITE_API int sqlite3_set_authorizer(
76532
 
  sqlite3 *db,
76533
 
  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
76534
 
  void *pArg
76535
 
){
76536
 
  sqlite3_mutex_enter(db->mutex);
76537
 
  db->xAuth = xAuth;
76538
 
  db->pAuthArg = pArg;
76539
 
  sqlite3ExpirePreparedStatements(db);
76540
 
  sqlite3_mutex_leave(db->mutex);
76541
 
  return SQLITE_OK;
76542
 
}
76543
 
 
76544
 
/*
76545
 
** Write an error message into pParse->zErrMsg that explains that the
76546
 
** user-supplied authorization function returned an illegal value.
76547
 
*/
76548
 
static void sqliteAuthBadReturnCode(Parse *pParse){
76549
 
  sqlite3ErrorMsg(pParse, "authorizer malfunction");
76550
 
  pParse->rc = SQLITE_ERROR;
76551
 
}
76552
 
 
76553
 
/*
76554
 
** Invoke the authorization callback for permission to read column zCol from
76555
 
** table zTab in database zDb. This function assumes that an authorization
76556
 
** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
76557
 
**
76558
 
** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
76559
 
** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
76560
 
** is treated as SQLITE_DENY. In this case an error is left in pParse.
76561
 
*/
76562
 
SQLITE_PRIVATE int sqlite3AuthReadCol(
76563
 
  Parse *pParse,                  /* The parser context */
76564
 
  const char *zTab,               /* Table name */
76565
 
  const char *zCol,               /* Column name */
76566
 
  int iDb                         /* Index of containing database. */
76567
 
){
76568
 
  sqlite3 *db = pParse->db;       /* Database handle */
76569
 
  char *zDb = db->aDb[iDb].zName; /* Name of attached database */
76570
 
  int rc;                         /* Auth callback return code */
76571
 
 
76572
 
  rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
76573
 
  if( rc==SQLITE_DENY ){
76574
 
    if( db->nDb>2 || iDb!=0 ){
76575
 
      sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
76576
 
    }else{
76577
 
      sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
76578
 
    }
76579
 
    pParse->rc = SQLITE_AUTH;
76580
 
  }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
76581
 
    sqliteAuthBadReturnCode(pParse);
76582
 
  }
76583
 
  return rc;
76584
 
}
76585
 
 
76586
 
/*
76587
 
** The pExpr should be a TK_COLUMN expression.  The table referred to
76588
 
** is in pTabList or else it is the NEW or OLD table of a trigger.  
76589
 
** Check to see if it is OK to read this particular column.
76590
 
**
76591
 
** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN 
76592
 
** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
76593
 
** then generate an error.
76594
 
*/
76595
 
SQLITE_PRIVATE void sqlite3AuthRead(
76596
 
  Parse *pParse,        /* The parser context */
76597
 
  Expr *pExpr,          /* The expression to check authorization on */
76598
 
  Schema *pSchema,      /* The schema of the expression */
76599
 
  SrcList *pTabList     /* All table that pExpr might refer to */
76600
 
){
76601
 
  sqlite3 *db = pParse->db;
76602
 
  Table *pTab = 0;      /* The table being read */
76603
 
  const char *zCol;     /* Name of the column of the table */
76604
 
  int iSrc;             /* Index in pTabList->a[] of table being read */
76605
 
  int iDb;              /* The index of the database the expression refers to */
76606
 
  int iCol;             /* Index of column in table */
76607
 
 
76608
 
  if( db->xAuth==0 ) return;
76609
 
  iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
76610
 
  if( iDb<0 ){
76611
 
    /* An attempt to read a column out of a subquery or other
76612
 
    ** temporary table. */
76613
 
    return;
76614
 
  }
76615
 
 
76616
 
  assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
76617
 
  if( pExpr->op==TK_TRIGGER ){
76618
 
    pTab = pParse->pTriggerTab;
76619
 
  }else{
76620
 
    assert( pTabList );
76621
 
    for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
76622
 
      if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
76623
 
        pTab = pTabList->a[iSrc].pTab;
76624
 
        break;
76625
 
      }
76626
 
    }
76627
 
  }
76628
 
  iCol = pExpr->iColumn;
76629
 
  if( NEVER(pTab==0) ) return;
76630
 
 
76631
 
  if( iCol>=0 ){
76632
 
    assert( iCol<pTab->nCol );
76633
 
    zCol = pTab->aCol[iCol].zName;
76634
 
  }else if( pTab->iPKey>=0 ){
76635
 
    assert( pTab->iPKey<pTab->nCol );
76636
 
    zCol = pTab->aCol[pTab->iPKey].zName;
76637
 
  }else{
76638
 
    zCol = "ROWID";
76639
 
  }
76640
 
  assert( iDb>=0 && iDb<db->nDb );
76641
 
  if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
76642
 
    pExpr->op = TK_NULL;
76643
 
  }
76644
 
}
76645
 
 
76646
 
/*
76647
 
** Do an authorization check using the code and arguments given.  Return
76648
 
** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
76649
 
** is returned, then the error count and error message in pParse are
76650
 
** modified appropriately.
76651
 
*/
76652
 
SQLITE_PRIVATE int sqlite3AuthCheck(
76653
 
  Parse *pParse,
76654
 
  int code,
76655
 
  const char *zArg1,
76656
 
  const char *zArg2,
76657
 
  const char *zArg3
76658
 
){
76659
 
  sqlite3 *db = pParse->db;
76660
 
  int rc;
76661
 
 
76662
 
  /* Don't do any authorization checks if the database is initialising
76663
 
  ** or if the parser is being invoked from within sqlite3_declare_vtab.
76664
 
  */
76665
 
  if( db->init.busy || IN_DECLARE_VTAB ){
76666
 
    return SQLITE_OK;
76667
 
  }
76668
 
 
76669
 
  if( db->xAuth==0 ){
76670
 
    return SQLITE_OK;
76671
 
  }
76672
 
  rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
76673
 
  if( rc==SQLITE_DENY ){
76674
 
    sqlite3ErrorMsg(pParse, "not authorized");
76675
 
    pParse->rc = SQLITE_AUTH;
76676
 
  }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
76677
 
    rc = SQLITE_DENY;
76678
 
    sqliteAuthBadReturnCode(pParse);
76679
 
  }
76680
 
  return rc;
76681
 
}
76682
 
 
76683
 
/*
76684
 
** Push an authorization context.  After this routine is called, the
76685
 
** zArg3 argument to authorization callbacks will be zContext until
76686
 
** popped.  Or if pParse==0, this routine is a no-op.
76687
 
*/
76688
 
SQLITE_PRIVATE void sqlite3AuthContextPush(
76689
 
  Parse *pParse,
76690
 
  AuthContext *pContext, 
76691
 
  const char *zContext
76692
 
){
76693
 
  assert( pParse );
76694
 
  pContext->pParse = pParse;
76695
 
  pContext->zAuthContext = pParse->zAuthContext;
76696
 
  pParse->zAuthContext = zContext;
76697
 
}
76698
 
 
76699
 
/*
76700
 
** Pop an authorization context that was previously pushed
76701
 
** by sqlite3AuthContextPush
76702
 
*/
76703
 
SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
76704
 
  if( pContext->pParse ){
76705
 
    pContext->pParse->zAuthContext = pContext->zAuthContext;
76706
 
    pContext->pParse = 0;
76707
 
  }
76708
 
}
76709
 
 
76710
 
#endif /* SQLITE_OMIT_AUTHORIZATION */
76711
 
 
76712
 
/************** End of auth.c ************************************************/
76713
 
/************** Begin file build.c *******************************************/
76714
 
/*
76715
 
** 2001 September 15
76716
 
**
76717
 
** The author disclaims copyright to this source code.  In place of
76718
 
** a legal notice, here is a blessing:
76719
 
**
76720
 
**    May you do good and not evil.
76721
 
**    May you find forgiveness for yourself and forgive others.
76722
 
**    May you share freely, never taking more than you give.
76723
 
**
76724
 
*************************************************************************
76725
 
** This file contains C code routines that are called by the SQLite parser
76726
 
** when syntax rules are reduced.  The routines in this file handle the
76727
 
** following kinds of SQL syntax:
76728
 
**
76729
 
**     CREATE TABLE
76730
 
**     DROP TABLE
76731
 
**     CREATE INDEX
76732
 
**     DROP INDEX
76733
 
**     creating ID lists
76734
 
**     BEGIN TRANSACTION
76735
 
**     COMMIT
76736
 
**     ROLLBACK
76737
 
*/
76738
 
 
76739
 
/*
76740
 
** This routine is called when a new SQL statement is beginning to
76741
 
** be parsed.  Initialize the pParse structure as needed.
76742
 
*/
76743
 
SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){
76744
 
  pParse->explain = (u8)explainFlag;
76745
 
  pParse->nVar = 0;
76746
 
}
76747
 
 
76748
 
#ifndef SQLITE_OMIT_SHARED_CACHE
76749
 
/*
76750
 
** The TableLock structure is only used by the sqlite3TableLock() and
76751
 
** codeTableLocks() functions.
76752
 
*/
76753
 
struct TableLock {
76754
 
  int iDb;             /* The database containing the table to be locked */
76755
 
  int iTab;            /* The root page of the table to be locked */
76756
 
  u8 isWriteLock;      /* True for write lock.  False for a read lock */
76757
 
  const char *zName;   /* Name of the table */
76758
 
};
76759
 
 
76760
 
/*
76761
 
** Record the fact that we want to lock a table at run-time.  
76762
 
**
76763
 
** The table to be locked has root page iTab and is found in database iDb.
76764
 
** A read or a write lock can be taken depending on isWritelock.
76765
 
**
76766
 
** This routine just records the fact that the lock is desired.  The
76767
 
** code to make the lock occur is generated by a later call to
76768
 
** codeTableLocks() which occurs during sqlite3FinishCoding().
76769
 
*/
76770
 
SQLITE_PRIVATE void sqlite3TableLock(
76771
 
  Parse *pParse,     /* Parsing context */
76772
 
  int iDb,           /* Index of the database containing the table to lock */
76773
 
  int iTab,          /* Root page number of the table to be locked */
76774
 
  u8 isWriteLock,    /* True for a write lock */
76775
 
  const char *zName  /* Name of the table to be locked */
76776
 
){
76777
 
  Parse *pToplevel = sqlite3ParseToplevel(pParse);
76778
 
  int i;
76779
 
  int nBytes;
76780
 
  TableLock *p;
76781
 
  assert( iDb>=0 );
76782
 
 
76783
 
  for(i=0; i<pToplevel->nTableLock; i++){
76784
 
    p = &pToplevel->aTableLock[i];
76785
 
    if( p->iDb==iDb && p->iTab==iTab ){
76786
 
      p->isWriteLock = (p->isWriteLock || isWriteLock);
76787
 
      return;
76788
 
    }
76789
 
  }
76790
 
 
76791
 
  nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
76792
 
  pToplevel->aTableLock =
76793
 
      sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
76794
 
  if( pToplevel->aTableLock ){
76795
 
    p = &pToplevel->aTableLock[pToplevel->nTableLock++];
76796
 
    p->iDb = iDb;
76797
 
    p->iTab = iTab;
76798
 
    p->isWriteLock = isWriteLock;
76799
 
    p->zName = zName;
76800
 
  }else{
76801
 
    pToplevel->nTableLock = 0;
76802
 
    pToplevel->db->mallocFailed = 1;
76803
 
  }
76804
 
}
76805
 
 
76806
 
/*
76807
 
** Code an OP_TableLock instruction for each table locked by the
76808
 
** statement (configured by calls to sqlite3TableLock()).
76809
 
*/
76810
 
static void codeTableLocks(Parse *pParse){
76811
 
  int i;
76812
 
  Vdbe *pVdbe; 
76813
 
 
76814
 
  pVdbe = sqlite3GetVdbe(pParse);
76815
 
  assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
76816
 
 
76817
 
  for(i=0; i<pParse->nTableLock; i++){
76818
 
    TableLock *p = &pParse->aTableLock[i];
76819
 
    int p1 = p->iDb;
76820
 
    sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
76821
 
                      p->zName, P4_STATIC);
76822
 
  }
76823
 
}
76824
 
#else
76825
 
  #define codeTableLocks(x)
76826
 
#endif
76827
 
 
76828
 
/*
76829
 
** This routine is called after a single SQL statement has been
76830
 
** parsed and a VDBE program to execute that statement has been
76831
 
** prepared.  This routine puts the finishing touches on the
76832
 
** VDBE program and resets the pParse structure for the next
76833
 
** parse.
76834
 
**
76835
 
** Note that if an error occurred, it might be the case that
76836
 
** no VDBE code was generated.
76837
 
*/
76838
 
SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
76839
 
  sqlite3 *db;
76840
 
  Vdbe *v;
76841
 
 
76842
 
  db = pParse->db;
76843
 
  if( db->mallocFailed ) return;
76844
 
  if( pParse->nested ) return;
76845
 
  if( pParse->nErr ) return;
76846
 
 
76847
 
  /* Begin by generating some termination code at the end of the
76848
 
  ** vdbe program
76849
 
  */
76850
 
  v = sqlite3GetVdbe(pParse);
76851
 
  assert( !pParse->isMultiWrite 
76852
 
       || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
76853
 
  if( v ){
76854
 
    sqlite3VdbeAddOp0(v, OP_Halt);
76855
 
 
76856
 
    /* The cookie mask contains one bit for each database file open.
76857
 
    ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
76858
 
    ** set for each database that is used.  Generate code to start a
76859
 
    ** transaction on each used database and to verify the schema cookie
76860
 
    ** on each used database.
76861
 
    */
76862
 
    if( pParse->cookieGoto>0 ){
76863
 
      yDbMask mask;
76864
 
      int iDb;
76865
 
      sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
76866
 
      for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
76867
 
        if( (mask & pParse->cookieMask)==0 ) continue;
76868
 
        sqlite3VdbeUsesBtree(v, iDb);
76869
 
        sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
76870
 
        if( db->init.busy==0 ){
76871
 
          assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
76872
 
          sqlite3VdbeAddOp3(v, OP_VerifyCookie,
76873
 
                            iDb, pParse->cookieValue[iDb],
76874
 
                            db->aDb[iDb].pSchema->iGeneration);
76875
 
        }
76876
 
      }
76877
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
76878
 
      {
76879
 
        int i;
76880
 
        for(i=0; i<pParse->nVtabLock; i++){
76881
 
          char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
76882
 
          sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
76883
 
        }
76884
 
        pParse->nVtabLock = 0;
76885
 
      }
76886
 
#endif
76887
 
 
76888
 
      /* Once all the cookies have been verified and transactions opened, 
76889
 
      ** obtain the required table-locks. This is a no-op unless the 
76890
 
      ** shared-cache feature is enabled.
76891
 
      */
76892
 
      codeTableLocks(pParse);
76893
 
 
76894
 
      /* Initialize any AUTOINCREMENT data structures required.
76895
 
      */
76896
 
      sqlite3AutoincrementBegin(pParse);
76897
 
 
76898
 
      /* Finally, jump back to the beginning of the executable code. */
76899
 
      sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
76900
 
    }
76901
 
  }
76902
 
 
76903
 
 
76904
 
  /* Get the VDBE program ready for execution
76905
 
  */
76906
 
  if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
76907
 
#ifdef SQLITE_DEBUG
76908
 
    FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
76909
 
    sqlite3VdbeTrace(v, trace);
76910
 
#endif
76911
 
    assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
76912
 
    /* A minimum of one cursor is required if autoincrement is used
76913
 
    *  See ticket [a696379c1f08866] */
76914
 
    if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
76915
 
    sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem,
76916
 
                         pParse->nTab, pParse->nMaxArg, pParse->explain,
76917
 
                         pParse->isMultiWrite && pParse->mayAbort);
76918
 
    pParse->rc = SQLITE_DONE;
76919
 
    pParse->colNamesSet = 0;
76920
 
  }else{
76921
 
    pParse->rc = SQLITE_ERROR;
76922
 
  }
76923
 
  pParse->nTab = 0;
76924
 
  pParse->nMem = 0;
76925
 
  pParse->nSet = 0;
76926
 
  pParse->nVar = 0;
76927
 
  pParse->cookieMask = 0;
76928
 
  pParse->cookieGoto = 0;
76929
 
}
76930
 
 
76931
 
/*
76932
 
** Run the parser and code generator recursively in order to generate
76933
 
** code for the SQL statement given onto the end of the pParse context
76934
 
** currently under construction.  When the parser is run recursively
76935
 
** this way, the final OP_Halt is not appended and other initialization
76936
 
** and finalization steps are omitted because those are handling by the
76937
 
** outermost parser.
76938
 
**
76939
 
** Not everything is nestable.  This facility is designed to permit
76940
 
** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER.  Use
76941
 
** care if you decide to try to use this routine for some other purposes.
76942
 
*/
76943
 
SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
76944
 
  va_list ap;
76945
 
  char *zSql;
76946
 
  char *zErrMsg = 0;
76947
 
  sqlite3 *db = pParse->db;
76948
 
# define SAVE_SZ  (sizeof(Parse) - offsetof(Parse,nVar))
76949
 
  char saveBuf[SAVE_SZ];
76950
 
 
76951
 
  if( pParse->nErr ) return;
76952
 
  assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
76953
 
  va_start(ap, zFormat);
76954
 
  zSql = sqlite3VMPrintf(db, zFormat, ap);
76955
 
  va_end(ap);
76956
 
  if( zSql==0 ){
76957
 
    return;   /* A malloc must have failed */
76958
 
  }
76959
 
  pParse->nested++;
76960
 
  memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
76961
 
  memset(&pParse->nVar, 0, SAVE_SZ);
76962
 
  sqlite3RunParser(pParse, zSql, &zErrMsg);
76963
 
  sqlite3DbFree(db, zErrMsg);
76964
 
  sqlite3DbFree(db, zSql);
76965
 
  memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
76966
 
  pParse->nested--;
76967
 
}
76968
 
 
76969
 
/*
76970
 
** Locate the in-memory structure that describes a particular database
76971
 
** table given the name of that table and (optionally) the name of the
76972
 
** database containing the table.  Return NULL if not found.
76973
 
**
76974
 
** If zDatabase is 0, all databases are searched for the table and the
76975
 
** first matching table is returned.  (No checking for duplicate table
76976
 
** names is done.)  The search order is TEMP first, then MAIN, then any
76977
 
** auxiliary databases added using the ATTACH command.
76978
 
**
76979
 
** See also sqlite3LocateTable().
76980
 
*/
76981
 
SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
76982
 
  Table *p = 0;
76983
 
  int i;
76984
 
  int nName;
76985
 
  assert( zName!=0 );
76986
 
  nName = sqlite3Strlen30(zName);
76987
 
  /* All mutexes are required for schema access.  Make sure we hold them. */
76988
 
  assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
76989
 
  for(i=OMIT_TEMPDB; i<db->nDb; i++){
76990
 
    int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
76991
 
    if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
76992
 
    assert( sqlite3SchemaMutexHeld(db, j, 0) );
76993
 
    p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
76994
 
    if( p ) break;
76995
 
  }
76996
 
  return p;
76997
 
}
76998
 
 
76999
 
/*
77000
 
** Locate the in-memory structure that describes a particular database
77001
 
** table given the name of that table and (optionally) the name of the
77002
 
** database containing the table.  Return NULL if not found.  Also leave an
77003
 
** error message in pParse->zErrMsg.
77004
 
**
77005
 
** The difference between this routine and sqlite3FindTable() is that this
77006
 
** routine leaves an error message in pParse->zErrMsg where
77007
 
** sqlite3FindTable() does not.
77008
 
*/
77009
 
SQLITE_PRIVATE Table *sqlite3LocateTable(
77010
 
  Parse *pParse,         /* context in which to report errors */
77011
 
  int isView,            /* True if looking for a VIEW rather than a TABLE */
77012
 
  const char *zName,     /* Name of the table we are looking for */
77013
 
  const char *zDbase     /* Name of the database.  Might be NULL */
77014
 
){
77015
 
  Table *p;
77016
 
 
77017
 
  /* Read the database schema. If an error occurs, leave an error message
77018
 
  ** and code in pParse and return NULL. */
77019
 
  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
77020
 
    return 0;
77021
 
  }
77022
 
 
77023
 
  p = sqlite3FindTable(pParse->db, zName, zDbase);
77024
 
  if( p==0 ){
77025
 
    const char *zMsg = isView ? "no such view" : "no such table";
77026
 
    if( zDbase ){
77027
 
      sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
77028
 
    }else{
77029
 
      sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
77030
 
    }
77031
 
    pParse->checkSchema = 1;
77032
 
  }
77033
 
  return p;
77034
 
}
77035
 
 
77036
 
/*
77037
 
** Locate the in-memory structure that describes 
77038
 
** a particular index given the name of that index
77039
 
** and the name of the database that contains the index.
77040
 
** Return NULL if not found.
77041
 
**
77042
 
** If zDatabase is 0, all databases are searched for the
77043
 
** table and the first matching index is returned.  (No checking
77044
 
** for duplicate index names is done.)  The search order is
77045
 
** TEMP first, then MAIN, then any auxiliary databases added
77046
 
** using the ATTACH command.
77047
 
*/
77048
 
SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
77049
 
  Index *p = 0;
77050
 
  int i;
77051
 
  int nName = sqlite3Strlen30(zName);
77052
 
  /* All mutexes are required for schema access.  Make sure we hold them. */
77053
 
  assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
77054
 
  for(i=OMIT_TEMPDB; i<db->nDb; i++){
77055
 
    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
77056
 
    Schema *pSchema = db->aDb[j].pSchema;
77057
 
    assert( pSchema );
77058
 
    if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
77059
 
    assert( sqlite3SchemaMutexHeld(db, j, 0) );
77060
 
    p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
77061
 
    if( p ) break;
77062
 
  }
77063
 
  return p;
77064
 
}
77065
 
 
77066
 
/*
77067
 
** Reclaim the memory used by an index
77068
 
*/
77069
 
static void freeIndex(sqlite3 *db, Index *p){
77070
 
#ifndef SQLITE_OMIT_ANALYZE
77071
 
  sqlite3DeleteIndexSamples(db, p);
77072
 
#endif
77073
 
  sqlite3DbFree(db, p->zColAff);
77074
 
  sqlite3DbFree(db, p);
77075
 
}
77076
 
 
77077
 
/*
77078
 
** For the index called zIdxName which is found in the database iDb,
77079
 
** unlike that index from its Table then remove the index from
77080
 
** the index hash table and free all memory structures associated
77081
 
** with the index.
77082
 
*/
77083
 
SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
77084
 
  Index *pIndex;
77085
 
  int len;
77086
 
  Hash *pHash;
77087
 
 
77088
 
  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
77089
 
  pHash = &db->aDb[iDb].pSchema->idxHash;
77090
 
  len = sqlite3Strlen30(zIdxName);
77091
 
  pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
77092
 
  if( ALWAYS(pIndex) ){
77093
 
    if( pIndex->pTable->pIndex==pIndex ){
77094
 
      pIndex->pTable->pIndex = pIndex->pNext;
77095
 
    }else{
77096
 
      Index *p;
77097
 
      /* Justification of ALWAYS();  The index must be on the list of
77098
 
      ** indices. */
77099
 
      p = pIndex->pTable->pIndex;
77100
 
      while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
77101
 
      if( ALWAYS(p && p->pNext==pIndex) ){
77102
 
        p->pNext = pIndex->pNext;
77103
 
      }
77104
 
    }
77105
 
    freeIndex(db, pIndex);
77106
 
  }
77107
 
  db->flags |= SQLITE_InternChanges;
77108
 
}
77109
 
 
77110
 
/*
77111
 
** Erase all schema information from the in-memory hash tables of
77112
 
** a single database.  This routine is called to reclaim memory
77113
 
** before the database closes.  It is also called during a rollback
77114
 
** if there were schema changes during the transaction or if a
77115
 
** schema-cookie mismatch occurs.
77116
 
**
77117
 
** If iDb<0 then reset the internal schema tables for all database
77118
 
** files.  If iDb>=0 then reset the internal schema for only the
77119
 
** single file indicated.
77120
 
*/
77121
 
SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){
77122
 
  int i, j;
77123
 
  assert( iDb<db->nDb );
77124
 
 
77125
 
  if( iDb>=0 ){
77126
 
    /* Case 1:  Reset the single schema identified by iDb */
77127
 
    Db *pDb = &db->aDb[iDb];
77128
 
    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
77129
 
    assert( pDb->pSchema!=0 );
77130
 
    sqlite3SchemaClear(pDb->pSchema);
77131
 
 
77132
 
    /* If any database other than TEMP is reset, then also reset TEMP
77133
 
    ** since TEMP might be holding triggers that reference tables in the
77134
 
    ** other database.
77135
 
    */
77136
 
    if( iDb!=1 ){
77137
 
      pDb = &db->aDb[1];
77138
 
      assert( pDb->pSchema!=0 );
77139
 
      sqlite3SchemaClear(pDb->pSchema);
77140
 
    }
77141
 
    return;
77142
 
  }
77143
 
  /* Case 2 (from here to the end): Reset all schemas for all attached
77144
 
  ** databases. */
77145
 
  assert( iDb<0 );
77146
 
  sqlite3BtreeEnterAll(db);
77147
 
  for(i=0; i<db->nDb; i++){
77148
 
    Db *pDb = &db->aDb[i];
77149
 
    if( pDb->pSchema ){
77150
 
      sqlite3SchemaClear(pDb->pSchema);
77151
 
    }
77152
 
  }
77153
 
  db->flags &= ~SQLITE_InternChanges;
77154
 
  sqlite3VtabUnlockList(db);
77155
 
  sqlite3BtreeLeaveAll(db);
77156
 
 
77157
 
  /* If one or more of the auxiliary database files has been closed,
77158
 
  ** then remove them from the auxiliary database list.  We take the
77159
 
  ** opportunity to do this here since we have just deleted all of the
77160
 
  ** schema hash tables and therefore do not have to make any changes
77161
 
  ** to any of those tables.
77162
 
  */
77163
 
  for(i=j=2; i<db->nDb; i++){
77164
 
    struct Db *pDb = &db->aDb[i];
77165
 
    if( pDb->pBt==0 ){
77166
 
      sqlite3DbFree(db, pDb->zName);
77167
 
      pDb->zName = 0;
77168
 
      continue;
77169
 
    }
77170
 
    if( j<i ){
77171
 
      db->aDb[j] = db->aDb[i];
77172
 
    }
77173
 
    j++;
77174
 
  }
77175
 
  memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
77176
 
  db->nDb = j;
77177
 
  if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
77178
 
    memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
77179
 
    sqlite3DbFree(db, db->aDb);
77180
 
    db->aDb = db->aDbStatic;
77181
 
  }
77182
 
}
77183
 
 
77184
 
/*
77185
 
** This routine is called when a commit occurs.
77186
 
*/
77187
 
SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
77188
 
  db->flags &= ~SQLITE_InternChanges;
77189
 
}
77190
 
 
77191
 
/*
77192
 
** Delete memory allocated for the column names of a table or view (the
77193
 
** Table.aCol[] array).
77194
 
*/
77195
 
static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){
77196
 
  int i;
77197
 
  Column *pCol;
77198
 
  assert( pTable!=0 );
77199
 
  if( (pCol = pTable->aCol)!=0 ){
77200
 
    for(i=0; i<pTable->nCol; i++, pCol++){
77201
 
      sqlite3DbFree(db, pCol->zName);
77202
 
      sqlite3ExprDelete(db, pCol->pDflt);
77203
 
      sqlite3DbFree(db, pCol->zDflt);
77204
 
      sqlite3DbFree(db, pCol->zType);
77205
 
      sqlite3DbFree(db, pCol->zColl);
77206
 
    }
77207
 
    sqlite3DbFree(db, pTable->aCol);
77208
 
  }
77209
 
}
77210
 
 
77211
 
/*
77212
 
** Remove the memory data structures associated with the given
77213
 
** Table.  No changes are made to disk by this routine.
77214
 
**
77215
 
** This routine just deletes the data structure.  It does not unlink
77216
 
** the table data structure from the hash table.  But it does destroy
77217
 
** memory structures of the indices and foreign keys associated with 
77218
 
** the table.
77219
 
*/
77220
 
SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
77221
 
  Index *pIndex, *pNext;
77222
 
 
77223
 
  assert( !pTable || pTable->nRef>0 );
77224
 
 
77225
 
  /* Do not delete the table until the reference count reaches zero. */
77226
 
  if( !pTable ) return;
77227
 
  if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
77228
 
 
77229
 
  /* Delete all indices associated with this table. */
77230
 
  for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
77231
 
    pNext = pIndex->pNext;
77232
 
    assert( pIndex->pSchema==pTable->pSchema );
77233
 
    if( !db || db->pnBytesFreed==0 ){
77234
 
      char *zName = pIndex->zName; 
77235
 
      TESTONLY ( Index *pOld = ) sqlite3HashInsert(
77236
 
          &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
77237
 
      );
77238
 
      assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
77239
 
      assert( pOld==pIndex || pOld==0 );
77240
 
    }
77241
 
    freeIndex(db, pIndex);
77242
 
  }
77243
 
 
77244
 
  /* Delete any foreign keys attached to this table. */
77245
 
  sqlite3FkDelete(db, pTable);
77246
 
 
77247
 
  /* Delete the Table structure itself.
77248
 
  */
77249
 
  sqliteDeleteColumnNames(db, pTable);
77250
 
  sqlite3DbFree(db, pTable->zName);
77251
 
  sqlite3DbFree(db, pTable->zColAff);
77252
 
  sqlite3SelectDelete(db, pTable->pSelect);
77253
 
#ifndef SQLITE_OMIT_CHECK
77254
 
  sqlite3ExprDelete(db, pTable->pCheck);
77255
 
#endif
77256
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
77257
 
  sqlite3VtabClear(db, pTable);
77258
 
#endif
77259
 
  sqlite3DbFree(db, pTable);
77260
 
}
77261
 
 
77262
 
/*
77263
 
** Unlink the given table from the hash tables and the delete the
77264
 
** table structure with all its indices and foreign keys.
77265
 
*/
77266
 
SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
77267
 
  Table *p;
77268
 
  Db *pDb;
77269
 
 
77270
 
  assert( db!=0 );
77271
 
  assert( iDb>=0 && iDb<db->nDb );
77272
 
  assert( zTabName );
77273
 
  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
77274
 
  testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
77275
 
  pDb = &db->aDb[iDb];
77276
 
  p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName,
77277
 
                        sqlite3Strlen30(zTabName),0);
77278
 
  sqlite3DeleteTable(db, p);
77279
 
  db->flags |= SQLITE_InternChanges;
77280
 
}
77281
 
 
77282
 
/*
77283
 
** Given a token, return a string that consists of the text of that
77284
 
** token.  Space to hold the returned string
77285
 
** is obtained from sqliteMalloc() and must be freed by the calling
77286
 
** function.
77287
 
**
77288
 
** Any quotation marks (ex:  "name", 'name', [name], or `name`) that
77289
 
** surround the body of the token are removed.
77290
 
**
77291
 
** Tokens are often just pointers into the original SQL text and so
77292
 
** are not \000 terminated and are not persistent.  The returned string
77293
 
** is \000 terminated and is persistent.
77294
 
*/
77295
 
SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
77296
 
  char *zName;
77297
 
  if( pName ){
77298
 
    zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
77299
 
    sqlite3Dequote(zName);
77300
 
  }else{
77301
 
    zName = 0;
77302
 
  }
77303
 
  return zName;
77304
 
}
77305
 
 
77306
 
/*
77307
 
** Open the sqlite_master table stored in database number iDb for
77308
 
** writing. The table is opened using cursor 0.
77309
 
*/
77310
 
SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
77311
 
  Vdbe *v = sqlite3GetVdbe(p);
77312
 
  sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
77313
 
  sqlite3VdbeAddOp3(v, OP_OpenWrite, 0, MASTER_ROOT, iDb);
77314
 
  sqlite3VdbeChangeP4(v, -1, (char *)5, P4_INT32);  /* 5 column table */
77315
 
  if( p->nTab==0 ){
77316
 
    p->nTab = 1;
77317
 
  }
77318
 
}
77319
 
 
77320
 
/*
77321
 
** Parameter zName points to a nul-terminated buffer containing the name
77322
 
** of a database ("main", "temp" or the name of an attached db). This
77323
 
** function returns the index of the named database in db->aDb[], or
77324
 
** -1 if the named db cannot be found.
77325
 
*/
77326
 
SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
77327
 
  int i = -1;         /* Database number */
77328
 
  if( zName ){
77329
 
    Db *pDb;
77330
 
    int n = sqlite3Strlen30(zName);
77331
 
    for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
77332
 
      #if (!OMIT_TEMPDB)
77333
 
        if( n==sqlite3Strlen30(pDb->zName) && 0==sqlite3StrICmp(pDb->zName, zName) ){
77334
 
          break;
77335
 
                   }
77336
 
      #else
77337
 
                if( i!=1 && n==sqlite3Strlen30(pDb->zName) && 0==sqlite3StrICmp(pDb->zName, zName) ){
77338
 
                        break;
77339
 
                }
77340
 
      #endif
77341
 
    }
77342
 
  }
77343
 
  return i;
77344
 
}
77345
 
 
77346
 
/*
77347
 
** The token *pName contains the name of a database (either "main" or
77348
 
** "temp" or the name of an attached db). This routine returns the
77349
 
** index of the named database in db->aDb[], or -1 if the named db 
77350
 
** does not exist.
77351
 
*/
77352
 
SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
77353
 
  int i;                               /* Database number */
77354
 
  char *zName;                         /* Name we are searching for */
77355
 
  zName = sqlite3NameFromToken(db, pName);
77356
 
  i = sqlite3FindDbName(db, zName);
77357
 
  sqlite3DbFree(db, zName);
77358
 
  return i;
77359
 
}
77360
 
 
77361
 
/* The table or view or trigger name is passed to this routine via tokens
77362
 
** pName1 and pName2. If the table name was fully qualified, for example:
77363
 
**
77364
 
** CREATE TABLE xxx.yyy (...);
77365
 
** 
77366
 
** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
77367
 
** the table name is not fully qualified, i.e.:
77368
 
**
77369
 
** CREATE TABLE yyy(...);
77370
 
**
77371
 
** Then pName1 is set to "yyy" and pName2 is "".
77372
 
**
77373
 
** This routine sets the *ppUnqual pointer to point at the token (pName1 or
77374
 
** pName2) that stores the unqualified table name.  The index of the
77375
 
** database "xxx" is returned.
77376
 
*/
77377
 
SQLITE_PRIVATE int sqlite3TwoPartName(
77378
 
  Parse *pParse,      /* Parsing and code generating context */
77379
 
  Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
77380
 
  Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
77381
 
  Token **pUnqual     /* Write the unqualified object name here */
77382
 
){
77383
 
  int iDb;                    /* Database holding the object */
77384
 
  sqlite3 *db = pParse->db;
77385
 
 
77386
 
  if( ALWAYS(pName2!=0) && pName2->n>0 ){
77387
 
    if( db->init.busy ) {
77388
 
      sqlite3ErrorMsg(pParse, "corrupt database");
77389
 
      pParse->nErr++;
77390
 
      return -1;
77391
 
    }
77392
 
    *pUnqual = pName2;
77393
 
    iDb = sqlite3FindDb(db, pName1);
77394
 
    if( iDb<0 ){
77395
 
      sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
77396
 
      pParse->nErr++;
77397
 
      return -1;
77398
 
    }
77399
 
  }else{
77400
 
    assert( db->init.iDb==0 || db->init.busy );
77401
 
    iDb = db->init.iDb;
77402
 
    *pUnqual = pName1;
77403
 
  }
77404
 
  return iDb;
77405
 
}
77406
 
 
77407
 
/*
77408
 
** This routine is used to check if the UTF-8 string zName is a legal
77409
 
** unqualified name for a new schema object (table, index, view or
77410
 
** trigger). All names are legal except those that begin with the string
77411
 
** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
77412
 
** is reserved for internal use.
77413
 
*/
77414
 
SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
77415
 
  if( !pParse->db->init.busy && pParse->nested==0 
77416
 
          && (pParse->db->flags & SQLITE_WriteSchema)==0
77417
 
          && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
77418
 
    sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
77419
 
    return SQLITE_ERROR;
77420
 
  }
77421
 
  return SQLITE_OK;
77422
 
}
77423
 
 
77424
 
/*
77425
 
** Begin constructing a new table representation in memory.  This is
77426
 
** the first of several action routines that get called in response
77427
 
** to a CREATE TABLE statement.  In particular, this routine is called
77428
 
** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
77429
 
** flag is true if the table should be stored in the auxiliary database
77430
 
** file instead of in the main database file.  This is normally the case
77431
 
** when the "TEMP" or "TEMPORARY" keyword occurs in between
77432
 
** CREATE and TABLE.
77433
 
**
77434
 
** The new table record is initialized and put in pParse->pNewTable.
77435
 
** As more of the CREATE TABLE statement is parsed, additional action
77436
 
** routines will be called to add more information to this record.
77437
 
** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
77438
 
** is called to complete the construction of the new table record.
77439
 
*/
77440
 
SQLITE_PRIVATE void sqlite3StartTable(
77441
 
  Parse *pParse,   /* Parser context */
77442
 
  Token *pName1,   /* First part of the name of the table or view */
77443
 
  Token *pName2,   /* Second part of the name of the table or view */
77444
 
  int isTemp,      /* True if this is a TEMP table */
77445
 
  int isView,      /* True if this is a VIEW */
77446
 
  int isVirtual,   /* True if this is a VIRTUAL table */
77447
 
  int noErr        /* Do nothing if table already exists */
77448
 
){
77449
 
  Table *pTable;
77450
 
  char *zName = 0; /* The name of the new table */
77451
 
  sqlite3 *db = pParse->db;
77452
 
  Vdbe *v;
77453
 
  int iDb;         /* Database number to create the table in */
77454
 
  Token *pName;    /* Unqualified name of the table to create */
77455
 
 
77456
 
  /* The table or view name to create is passed to this routine via tokens
77457
 
  ** pName1 and pName2. If the table name was fully qualified, for example:
77458
 
  **
77459
 
  ** CREATE TABLE xxx.yyy (...);
77460
 
  ** 
77461
 
  ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
77462
 
  ** the table name is not fully qualified, i.e.:
77463
 
  **
77464
 
  ** CREATE TABLE yyy(...);
77465
 
  **
77466
 
  ** Then pName1 is set to "yyy" and pName2 is "".
77467
 
  **
77468
 
  ** The call below sets the pName pointer to point at the token (pName1 or
77469
 
  ** pName2) that stores the unqualified table name. The variable iDb is
77470
 
  ** set to the index of the database that the table or view is to be
77471
 
  ** created in.
77472
 
  */
77473
 
  iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
77474
 
  if( iDb<0 ) return;
77475
 
  #if (!OMIT_TEMPDB)
77476
 
  if(isTemp && pName2->n>0 && iDb!=1 ){
77477
 
    /* If creating a temp table, the name may not be qualified. Unless 
77478
 
    ** the database name is "temp" anyway.  */
77479
 
    sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
77480
 
    return;
77481
 
  }
77482
 
  #endif
77483
 
  #if (!OMIT_TEMPDB)
77484
 
  if( isTemp ) iDb = 1;
77485
 
  #endif
77486
 
 
77487
 
  pParse->sNameToken = *pName;
77488
 
  zName = sqlite3NameFromToken(db, pName);
77489
 
  if( zName==0 ) return;
77490
 
  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
77491
 
    goto begin_table_error;
77492
 
  }
77493
 
  if( db->init.iDb==1 ) isTemp = 1;
77494
 
#ifndef SQLITE_OMIT_AUTHORIZATION
77495
 
  assert( (isTemp & 1)==isTemp );
77496
 
  {
77497
 
    int code;
77498
 
    char *zDb = db->aDb[iDb].zName;
77499
 
    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
77500
 
      goto begin_table_error;
77501
 
    }
77502
 
    if( isView ){
77503
 
      #if(!OMIT_TEMPDB)
77504
 
      if( isTemp ){
77505
 
        code = SQLITE_CREATE_TEMP_VIEW;
77506
 
      }else{
77507
 
        code = SQLITE_CREATE_VIEW;
77508
 
      }
77509
 
                #else
77510
 
                 code = SQLITE_CREATE_VIEW;
77511
 
                #endif
77512
 
    }else{
77513
 
                #if(!OMIT_TEMPDB)
77514
 
      if( isTemp ){
77515
 
        code = SQLITE_CREATE_TEMP_TABLE;
77516
 
      }else{
77517
 
        code = SQLITE_CREATE_TABLE;
77518
 
      }
77519
 
                #else
77520
 
                 code = SQLITE_CREATE_TABLE;
77521
 
                #endif
77522
 
    }
77523
 
    if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
77524
 
      goto begin_table_error;
77525
 
    }
77526
 
  }
77527
 
#endif
77528
 
 
77529
 
  /* Make sure the new table name does not collide with an existing
77530
 
  ** index or table name in the same database.  Issue an error message if
77531
 
  ** it does. The exception is if the statement being parsed was passed
77532
 
  ** to an sqlite3_declare_vtab() call. In that case only the column names
77533
 
  ** and types will be used, so there is no need to test for namespace
77534
 
  ** collisions.
77535
 
  */
77536
 
  if( !IN_DECLARE_VTAB ){
77537
 
    char *zDb = db->aDb[iDb].zName;
77538
 
    if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
77539
 
      goto begin_table_error;
77540
 
    }
77541
 
    pTable = sqlite3FindTable(db, zName, zDb);
77542
 
    if( pTable ){
77543
 
      if( !noErr ){
77544
 
        sqlite3ErrorMsg(pParse, "table %T already exists", pName);
77545
 
      }else{
77546
 
        assert( !db->init.busy );
77547
 
        sqlite3CodeVerifySchema(pParse, iDb);
77548
 
      }
77549
 
      goto begin_table_error;
77550
 
    }
77551
 
    if( sqlite3FindIndex(db, zName, zDb)!=0 ){
77552
 
      sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
77553
 
      goto begin_table_error;
77554
 
    }
77555
 
  }
77556
 
 
77557
 
  pTable = sqlite3DbMallocZero(db, sizeof(Table));
77558
 
  if( pTable==0 ){
77559
 
    db->mallocFailed = 1;
77560
 
    pParse->rc = SQLITE_NOMEM;
77561
 
    pParse->nErr++;
77562
 
    goto begin_table_error;
77563
 
  }
77564
 
  pTable->zName = zName;
77565
 
  pTable->iPKey = -1;
77566
 
  pTable->pSchema = db->aDb[iDb].pSchema;
77567
 
  pTable->nRef = 1;
77568
 
  pTable->nRowEst = 1000000;
77569
 
  assert( pParse->pNewTable==0 );
77570
 
  pParse->pNewTable = pTable;
77571
 
 
77572
 
  /* If this is the magic sqlite_sequence table used by autoincrement,
77573
 
  ** then record a pointer to this table in the main database structure
77574
 
  ** so that INSERT can find the table easily.
77575
 
  */
77576
 
#ifndef SQLITE_OMIT_AUTOINCREMENT
77577
 
  if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
77578
 
    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
77579
 
    pTable->pSchema->pSeqTab = pTable;
77580
 
  }
77581
 
#endif
77582
 
 
77583
 
  /* Begin generating the code that will insert the table record into
77584
 
  ** the SQLITE_MASTER table.  Note in particular that we must go ahead
77585
 
  ** and allocate the record number for the table entry now.  Before any
77586
 
  ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
77587
 
  ** indices to be created and the table record must come before the 
77588
 
  ** indices.  Hence, the record number for the table must be allocated
77589
 
  ** now.
77590
 
  */
77591
 
  if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
77592
 
    int j1;
77593
 
    int fileFormat;
77594
 
    int reg1, reg2, reg3;
77595
 
    sqlite3BeginWriteOperation(pParse, 0, iDb);
77596
 
 
77597
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
77598
 
    if( isVirtual ){
77599
 
      sqlite3VdbeAddOp0(v, OP_VBegin);
77600
 
    }
77601
 
#endif
77602
 
 
77603
 
    /* If the file format and encoding in the database have not been set, 
77604
 
    ** set them now.
77605
 
    */
77606
 
    reg1 = pParse->regRowid = ++pParse->nMem;
77607
 
    reg2 = pParse->regRoot = ++pParse->nMem;
77608
 
    reg3 = ++pParse->nMem;
77609
 
    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
77610
 
    sqlite3VdbeUsesBtree(v, iDb);
77611
 
    j1 = sqlite3VdbeAddOp1(v, OP_If, reg3);
77612
 
    fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
77613
 
                  1 : SQLITE_MAX_FILE_FORMAT;
77614
 
    sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
77615
 
    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
77616
 
    sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
77617
 
    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
77618
 
    sqlite3VdbeJumpHere(v, j1);
77619
 
 
77620
 
    /* This just creates a place-holder record in the sqlite_master table.
77621
 
    ** The record created does not contain anything yet.  It will be replaced
77622
 
    ** by the real entry in code generated at sqlite3EndTable().
77623
 
    **
77624
 
    ** The rowid for the new entry is left in register pParse->regRowid.
77625
 
    ** The root page number of the new table is left in reg pParse->regRoot.
77626
 
    ** The rowid and root page number values are needed by the code that
77627
 
    ** sqlite3EndTable will generate.
77628
 
    */
77629
 
#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
77630
 
    if( isView || isVirtual ){
77631
 
      sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
77632
 
    }else
77633
 
#endif
77634
 
    {
77635
 
      sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
77636
 
    }
77637
 
    sqlite3OpenMasterTable(pParse, iDb);
77638
 
    sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
77639
 
    sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
77640
 
    sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
77641
 
    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
77642
 
    sqlite3VdbeAddOp0(v, OP_Close);
77643
 
  }
77644
 
 
77645
 
  /* Normal (non-error) return. */
77646
 
  return;
77647
 
 
77648
 
  /* If an error occurs, we jump here */
77649
 
begin_table_error:
77650
 
  sqlite3DbFree(db, zName);
77651
 
  return;
77652
 
}
77653
 
 
77654
 
/*
77655
 
** This macro is used to compare two strings in a case-insensitive manner.
77656
 
** It is slightly faster than calling sqlite3StrICmp() directly, but
77657
 
** produces larger code.
77658
 
**
77659
 
** WARNING: This macro is not compatible with the strcmp() family. It
77660
 
** returns true if the two strings are equal, otherwise false.
77661
 
*/
77662
 
#define STRICMP(x, y) (\
77663
 
sqlite3UpperToLower[*(unsigned char *)(x)]==   \
77664
 
sqlite3UpperToLower[*(unsigned char *)(y)]     \
77665
 
&& sqlite3StrICmp((x)+1,(y)+1)==0 )
77666
 
 
77667
 
/*
77668
 
** Add a new column to the table currently being constructed.
77669
 
**
77670
 
** The parser calls this routine once for each column declaration
77671
 
** in a CREATE TABLE statement.  sqlite3StartTable() gets called
77672
 
** first to get things going.  Then this routine is called for each
77673
 
** column.
77674
 
*/
77675
 
SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
77676
 
  Table *p;
77677
 
  int i;
77678
 
  char *z;
77679
 
  Column *pCol;
77680
 
  sqlite3 *db = pParse->db;
77681
 
  if( (p = pParse->pNewTable)==0 ) return;
77682
 
#if SQLITE_MAX_COLUMN
77683
 
  if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
77684
 
    sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
77685
 
    return;
77686
 
  }
77687
 
#endif
77688
 
  z = sqlite3NameFromToken(db, pName);
77689
 
  if( z==0 ) return;
77690
 
  for(i=0; i<p->nCol; i++){
77691
 
    if( STRICMP(z, p->aCol[i].zName) ){
77692
 
      sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
77693
 
      sqlite3DbFree(db, z);
77694
 
      return;
77695
 
    }
77696
 
  }
77697
 
  if( (p->nCol & 0x7)==0 ){
77698
 
    Column *aNew;
77699
 
    aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
77700
 
    if( aNew==0 ){
77701
 
      sqlite3DbFree(db, z);
77702
 
      return;
77703
 
    }
77704
 
    p->aCol = aNew;
77705
 
  }
77706
 
  pCol = &p->aCol[p->nCol];
77707
 
  memset(pCol, 0, sizeof(p->aCol[0]));
77708
 
  pCol->zName = z;
77709
 
 
77710
 
  /* If there is no type specified, columns have the default affinity
77711
 
  ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
77712
 
  ** be called next to set pCol->affinity correctly.
77713
 
  */
77714
 
  pCol->affinity = SQLITE_AFF_NONE;
77715
 
  p->nCol++;
77716
 
}
77717
 
 
77718
 
/*
77719
 
** This routine is called by the parser while in the middle of
77720
 
** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
77721
 
** been seen on a column.  This routine sets the notNull flag on
77722
 
** the column currently under construction.
77723
 
*/
77724
 
SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
77725
 
  Table *p;
77726
 
  p = pParse->pNewTable;
77727
 
  if( p==0 || NEVER(p->nCol<1) ) return;
77728
 
  p->aCol[p->nCol-1].notNull = (u8)onError;
77729
 
}
77730
 
 
77731
 
/*
77732
 
** Scan the column type name zType (length nType) and return the
77733
 
** associated affinity type.
77734
 
**
77735
 
** This routine does a case-independent search of zType for the 
77736
 
** substrings in the following table. If one of the substrings is
77737
 
** found, the corresponding affinity is returned. If zType contains
77738
 
** more than one of the substrings, entries toward the top of 
77739
 
** the table take priority. For example, if zType is 'BLOBINT', 
77740
 
** SQLITE_AFF_INTEGER is returned.
77741
 
**
77742
 
** Substring     | Affinity
77743
 
** --------------------------------
77744
 
** 'INT'         | SQLITE_AFF_INTEGER
77745
 
** 'CHAR'        | SQLITE_AFF_TEXT
77746
 
** 'CLOB'        | SQLITE_AFF_TEXT
77747
 
** 'TEXT'        | SQLITE_AFF_TEXT
77748
 
** 'BLOB'        | SQLITE_AFF_NONE
77749
 
** 'REAL'        | SQLITE_AFF_REAL
77750
 
** 'FLOA'        | SQLITE_AFF_REAL
77751
 
** 'DOUB'        | SQLITE_AFF_REAL
77752
 
**
77753
 
** If none of the substrings in the above table are found,
77754
 
** SQLITE_AFF_NUMERIC is returned.
77755
 
*/
77756
 
SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn){
77757
 
  u32 h = 0;
77758
 
  char aff = SQLITE_AFF_NUMERIC;
77759
 
 
77760
 
  if( zIn ) while( zIn[0] ){
77761
 
    h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
77762
 
    zIn++;
77763
 
    if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
77764
 
      aff = SQLITE_AFF_TEXT; 
77765
 
    }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
77766
 
      aff = SQLITE_AFF_TEXT;
77767
 
    }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
77768
 
      aff = SQLITE_AFF_TEXT;
77769
 
    }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
77770
 
        && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
77771
 
      aff = SQLITE_AFF_NONE;
77772
 
#ifndef SQLITE_OMIT_FLOATING_POINT
77773
 
    }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
77774
 
        && aff==SQLITE_AFF_NUMERIC ){
77775
 
      aff = SQLITE_AFF_REAL;
77776
 
    }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
77777
 
        && aff==SQLITE_AFF_NUMERIC ){
77778
 
      aff = SQLITE_AFF_REAL;
77779
 
    }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */
77780
 
        && aff==SQLITE_AFF_NUMERIC ){
77781
 
      aff = SQLITE_AFF_REAL;
77782
 
#endif
77783
 
    }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
77784
 
      aff = SQLITE_AFF_INTEGER;
77785
 
      break;
77786
 
    }
77787
 
  }
77788
 
 
77789
 
  return aff;
77790
 
}
77791
 
 
77792
 
/*
77793
 
** This routine is called by the parser while in the middle of
77794
 
** parsing a CREATE TABLE statement.  The pFirst token is the first
77795
 
** token in the sequence of tokens that describe the type of the
77796
 
** column currently under construction.   pLast is the last token
77797
 
** in the sequence.  Use this information to construct a string
77798
 
** that contains the typename of the column and store that string
77799
 
** in zType.
77800
 
*/ 
77801
 
SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
77802
 
  Table *p;
77803
 
  Column *pCol;
77804
 
 
77805
 
  p = pParse->pNewTable;
77806
 
  if( p==0 || NEVER(p->nCol<1) ) return;
77807
 
  pCol = &p->aCol[p->nCol-1];
77808
 
  assert( pCol->zType==0 );
77809
 
  pCol->zType = sqlite3NameFromToken(pParse->db, pType);
77810
 
  pCol->affinity = sqlite3AffinityType(pCol->zType);
77811
 
}
77812
 
 
77813
 
/*
77814
 
** The expression is the default value for the most recently added column
77815
 
** of the table currently under construction.
77816
 
**
77817
 
** Default value expressions must be constant.  Raise an exception if this
77818
 
** is not the case.
77819
 
**
77820
 
** This routine is called by the parser while in the middle of
77821
 
** parsing a CREATE TABLE statement.
77822
 
*/
77823
 
SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
77824
 
  Table *p;
77825
 
  Column *pCol;
77826
 
  sqlite3 *db = pParse->db;
77827
 
  p = pParse->pNewTable;
77828
 
  if( p!=0 ){
77829
 
    pCol = &(p->aCol[p->nCol-1]);
77830
 
    if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
77831
 
      sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
77832
 
          pCol->zName);
77833
 
    }else{
77834
 
      /* A copy of pExpr is used instead of the original, as pExpr contains
77835
 
      ** tokens that point to volatile memory. The 'span' of the expression
77836
 
      ** is required by pragma table_info.
77837
 
      */
77838
 
      sqlite3ExprDelete(db, pCol->pDflt);
77839
 
      pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
77840
 
      sqlite3DbFree(db, pCol->zDflt);
77841
 
      pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
77842
 
                                     (int)(pSpan->zEnd - pSpan->zStart));
77843
 
    }
77844
 
  }
77845
 
  sqlite3ExprDelete(db, pSpan->pExpr);
77846
 
}
77847
 
 
77848
 
/*
77849
 
** Designate the PRIMARY KEY for the table.  pList is a list of names 
77850
 
** of columns that form the primary key.  If pList is NULL, then the
77851
 
** most recently added column of the table is the primary key.
77852
 
**
77853
 
** A table can have at most one primary key.  If the table already has
77854
 
** a primary key (and this is the second primary key) then create an
77855
 
** error.
77856
 
**
77857
 
** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
77858
 
** then we will try to use that column as the rowid.  Set the Table.iPKey
77859
 
** field of the table under construction to be the index of the
77860
 
** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
77861
 
** no INTEGER PRIMARY KEY.
77862
 
**
77863
 
** If the key is not an INTEGER PRIMARY KEY, then create a unique
77864
 
** index for the key.  No index is created for INTEGER PRIMARY KEYs.
77865
 
*/
77866
 
SQLITE_PRIVATE void sqlite3AddPrimaryKey(
77867
 
  Parse *pParse,    /* Parsing context */
77868
 
  ExprList *pList,  /* List of field names to be indexed */
77869
 
  int onError,      /* What to do with a uniqueness conflict */
77870
 
  int autoInc,      /* True if the AUTOINCREMENT keyword is present */
77871
 
  int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
77872
 
){
77873
 
  Table *pTab = pParse->pNewTable;
77874
 
  char *zType = 0;
77875
 
  int iCol = -1, i;
77876
 
  if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
77877
 
  if( pTab->tabFlags & TF_HasPrimaryKey ){
77878
 
    sqlite3ErrorMsg(pParse, 
77879
 
      "table \"%s\" has more than one primary key", pTab->zName);
77880
 
    goto primary_key_exit;
77881
 
  }
77882
 
  pTab->tabFlags |= TF_HasPrimaryKey;
77883
 
  if( pList==0 ){
77884
 
    iCol = pTab->nCol - 1;
77885
 
    pTab->aCol[iCol].isPrimKey = 1;
77886
 
  }else{
77887
 
    for(i=0; i<pList->nExpr; i++){
77888
 
      for(iCol=0; iCol<pTab->nCol; iCol++){
77889
 
        if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
77890
 
          break;
77891
 
        }
77892
 
      }
77893
 
      if( iCol<pTab->nCol ){
77894
 
        pTab->aCol[iCol].isPrimKey = 1;
77895
 
      }
77896
 
    }
77897
 
    if( pList->nExpr>1 ) iCol = -1;
77898
 
  }
77899
 
  if( iCol>=0 && iCol<pTab->nCol ){
77900
 
    zType = pTab->aCol[iCol].zType;
77901
 
  }
77902
 
  if( zType && sqlite3StrICmp(zType, "INTEGER")==0
77903
 
        && sortOrder==SQLITE_SO_ASC ){
77904
 
    pTab->iPKey = iCol;
77905
 
    pTab->keyConf = (u8)onError;
77906
 
    assert( autoInc==0 || autoInc==1 );
77907
 
    pTab->tabFlags |= autoInc*TF_Autoincrement;
77908
 
  }else if( autoInc ){
77909
 
#ifndef SQLITE_OMIT_AUTOINCREMENT
77910
 
    sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
77911
 
       "INTEGER PRIMARY KEY");
77912
 
#endif
77913
 
  }else{
77914
 
    Index *p;
77915
 
    p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
77916
 
    if( p ){
77917
 
      p->autoIndex = 2;
77918
 
    }
77919
 
    pList = 0;
77920
 
  }
77921
 
 
77922
 
primary_key_exit:
77923
 
  sqlite3ExprListDelete(pParse->db, pList);
77924
 
  return;
77925
 
}
77926
 
 
77927
 
/*
77928
 
** Add a new CHECK constraint to the table currently under construction.
77929
 
*/
77930
 
SQLITE_PRIVATE void sqlite3AddCheckConstraint(
77931
 
  Parse *pParse,    /* Parsing context */
77932
 
  Expr *pCheckExpr  /* The check expression */
77933
 
){
77934
 
  sqlite3 *db = pParse->db;
77935
 
#ifndef SQLITE_OMIT_CHECK
77936
 
  Table *pTab = pParse->pNewTable;
77937
 
  if( pTab && !IN_DECLARE_VTAB ){
77938
 
    pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck, pCheckExpr);
77939
 
  }else
77940
 
#endif
77941
 
  {
77942
 
    sqlite3ExprDelete(db, pCheckExpr);
77943
 
  }
77944
 
}
77945
 
 
77946
 
/*
77947
 
** Set the collation function of the most recently parsed table column
77948
 
** to the CollSeq given.
77949
 
*/
77950
 
SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
77951
 
  Table *p;
77952
 
  int i;
77953
 
  char *zColl;              /* Dequoted name of collation sequence */
77954
 
  sqlite3 *db;
77955
 
 
77956
 
  if( (p = pParse->pNewTable)==0 ) return;
77957
 
  i = p->nCol-1;
77958
 
  db = pParse->db;
77959
 
  zColl = sqlite3NameFromToken(db, pToken);
77960
 
  if( !zColl ) return;
77961
 
 
77962
 
  if( sqlite3LocateCollSeq(pParse, zColl) ){
77963
 
    Index *pIdx;
77964
 
    p->aCol[i].zColl = zColl;
77965
 
  
77966
 
    /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
77967
 
    ** then an index may have been created on this column before the
77968
 
    ** collation type was added. Correct this if it is the case.
77969
 
    */
77970
 
    for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
77971
 
      assert( pIdx->nColumn==1 );
77972
 
      if( pIdx->aiColumn[0]==i ){
77973
 
        pIdx->azColl[0] = p->aCol[i].zColl;
77974
 
      }
77975
 
    }
77976
 
  }else{
77977
 
    sqlite3DbFree(db, zColl);
77978
 
  }
77979
 
}
77980
 
 
77981
 
/*
77982
 
** This function returns the collation sequence for database native text
77983
 
** encoding identified by the string zName, length nName.
77984
 
**
77985
 
** If the requested collation sequence is not available, or not available
77986
 
** in the database native encoding, the collation factory is invoked to
77987
 
** request it. If the collation factory does not supply such a sequence,
77988
 
** and the sequence is available in another text encoding, then that is
77989
 
** returned instead.
77990
 
**
77991
 
** If no versions of the requested collations sequence are available, or
77992
 
** another error occurs, NULL is returned and an error message written into
77993
 
** pParse.
77994
 
**
77995
 
** This routine is a wrapper around sqlite3FindCollSeq().  This routine
77996
 
** invokes the collation factory if the named collation cannot be found
77997
 
** and generates an error message.
77998
 
**
77999
 
** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
78000
 
*/
78001
 
SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
78002
 
  sqlite3 *db = pParse->db;
78003
 
  u8 enc = ENC(db);
78004
 
  u8 initbusy = db->init.busy;
78005
 
  CollSeq *pColl;
78006
 
 
78007
 
  pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
78008
 
  if( !initbusy && (!pColl || !pColl->xCmp) ){
78009
 
    pColl = sqlite3GetCollSeq(db, enc, pColl, zName);
78010
 
    if( !pColl ){
78011
 
      sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
78012
 
    }
78013
 
  }
78014
 
 
78015
 
  return pColl;
78016
 
}
78017
 
 
78018
 
 
78019
 
/*
78020
 
** Generate code that will increment the schema cookie.
78021
 
**
78022
 
** The schema cookie is used to determine when the schema for the
78023
 
** database changes.  After each schema change, the cookie value
78024
 
** changes.  When a process first reads the schema it records the
78025
 
** cookie.  Thereafter, whenever it goes to access the database,
78026
 
** it checks the cookie to make sure the schema has not changed
78027
 
** since it was last read.
78028
 
**
78029
 
** This plan is not completely bullet-proof.  It is possible for
78030
 
** the schema to change multiple times and for the cookie to be
78031
 
** set back to prior value.  But schema changes are infrequent
78032
 
** and the probability of hitting the same cookie value is only
78033
 
** 1 chance in 2^32.  So we're safe enough.
78034
 
*/
78035
 
SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
78036
 
  int r1 = sqlite3GetTempReg(pParse);
78037
 
  sqlite3 *db = pParse->db;
78038
 
  Vdbe *v = pParse->pVdbe;
78039
 
  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
78040
 
  sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
78041
 
  sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
78042
 
  sqlite3ReleaseTempReg(pParse, r1);
78043
 
}
78044
 
 
78045
 
/*
78046
 
** Measure the number of characters needed to output the given
78047
 
** identifier.  The number returned includes any quotes used
78048
 
** but does not include the null terminator.
78049
 
**
78050
 
** The estimate is conservative.  It might be larger that what is
78051
 
** really needed.
78052
 
*/
78053
 
static int identLength(const char *z){
78054
 
  int n;
78055
 
  for(n=0; *z; n++, z++){
78056
 
    if( *z=='"' ){ n++; }
78057
 
  }
78058
 
  return n + 2;
78059
 
}
78060
 
 
78061
 
/*
78062
 
** The first parameter is a pointer to an output buffer. The second 
78063
 
** parameter is a pointer to an integer that contains the offset at
78064
 
** which to write into the output buffer. This function copies the
78065
 
** nul-terminated string pointed to by the third parameter, zSignedIdent,
78066
 
** to the specified offset in the buffer and updates *pIdx to refer
78067
 
** to the first byte after the last byte written before returning.
78068
 
** 
78069
 
** If the string zSignedIdent consists entirely of alpha-numeric
78070
 
** characters, does not begin with a digit and is not an SQL keyword,
78071
 
** then it is copied to the output buffer exactly as it is. Otherwise,
78072
 
** it is quoted using double-quotes.
78073
 
*/
78074
 
static void identPut(char *z, int *pIdx, char *zSignedIdent){
78075
 
  unsigned char *zIdent = (unsigned char*)zSignedIdent;
78076
 
  int i, j, needQuote;
78077
 
  i = *pIdx;
78078
 
 
78079
 
  for(j=0; zIdent[j]; j++){
78080
 
    if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
78081
 
  }
78082
 
  needQuote = sqlite3Isdigit(zIdent[0]) || sqlite3KeywordCode(zIdent, j)!=TK_ID;
78083
 
  if( !needQuote ){
78084
 
    needQuote = zIdent[j];
78085
 
  }
78086
 
 
78087
 
  if( needQuote ) z[i++] = '"';
78088
 
  for(j=0; zIdent[j]; j++){
78089
 
    z[i++] = zIdent[j];
78090
 
    if( zIdent[j]=='"' ) z[i++] = '"';
78091
 
  }
78092
 
  if( needQuote ) z[i++] = '"';
78093
 
  z[i] = 0;
78094
 
  *pIdx = i;
78095
 
}
78096
 
 
78097
 
/*
78098
 
** Generate a CREATE TABLE statement appropriate for the given
78099
 
** table.  Memory to hold the text of the statement is obtained
78100
 
** from sqliteMalloc() and must be freed by the calling function.
78101
 
*/
78102
 
static char *createTableStmt(sqlite3 *db, Table *p){
78103
 
  int i, k, n;
78104
 
  char *zStmt;
78105
 
  char *zSep, *zSep2, *zEnd;
78106
 
  Column *pCol;
78107
 
  n = 0;
78108
 
  for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
78109
 
    n += identLength(pCol->zName) + 5;
78110
 
  }
78111
 
  n += identLength(p->zName);
78112
 
  if( n<50 ){ 
78113
 
    zSep = "";
78114
 
    zSep2 = ",";
78115
 
    zEnd = ")";
78116
 
  }else{
78117
 
    zSep = "\n  ";
78118
 
    zSep2 = ",\n  ";
78119
 
    zEnd = "\n)";
78120
 
  }
78121
 
  n += 35 + 6*p->nCol;
78122
 
  zStmt = sqlite3DbMallocRaw(0, n);
78123
 
  if( zStmt==0 ){
78124
 
    db->mallocFailed = 1;
78125
 
    return 0;
78126
 
  }
78127
 
  sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
78128
 
  k = sqlite3Strlen30(zStmt);
78129
 
  identPut(zStmt, &k, p->zName);
78130
 
  zStmt[k++] = '(';
78131
 
  for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
78132
 
    static const char * const azType[] = {
78133
 
        /* SQLITE_AFF_TEXT    */ " TEXT",
78134
 
        /* SQLITE_AFF_NONE    */ "",
78135
 
        /* SQLITE_AFF_NUMERIC */ " NUM",
78136
 
        /* SQLITE_AFF_INTEGER */ " INT",
78137
 
        /* SQLITE_AFF_REAL    */ " REAL"
78138
 
    };
78139
 
    int len;
78140
 
    const char *zType;
78141
 
 
78142
 
    sqlite3_snprintf(n-k, &zStmt[k], zSep);
78143
 
    k += sqlite3Strlen30(&zStmt[k]);
78144
 
    zSep = zSep2;
78145
 
    identPut(zStmt, &k, pCol->zName);
78146
 
    assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 );
78147
 
    assert( pCol->affinity-SQLITE_AFF_TEXT < ArraySize(azType) );
78148
 
    testcase( pCol->affinity==SQLITE_AFF_TEXT );
78149
 
    testcase( pCol->affinity==SQLITE_AFF_NONE );
78150
 
    testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
78151
 
    testcase( pCol->affinity==SQLITE_AFF_INTEGER );
78152
 
    testcase( pCol->affinity==SQLITE_AFF_REAL );
78153
 
    
78154
 
    zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
78155
 
    len = sqlite3Strlen30(zType);
78156
 
    assert( pCol->affinity==SQLITE_AFF_NONE 
78157
 
            || pCol->affinity==sqlite3AffinityType(zType) );
78158
 
    memcpy(&zStmt[k], zType, len);
78159
 
    k += len;
78160
 
    assert( k<=n );
78161
 
  }
78162
 
  sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
78163
 
  return zStmt;
78164
 
}
78165
 
 
78166
 
/*
78167
 
** This routine is called to report the final ")" that terminates
78168
 
** a CREATE TABLE statement.
78169
 
**
78170
 
** The table structure that other action routines have been building
78171
 
** is added to the internal hash tables, assuming no errors have
78172
 
** occurred.
78173
 
**
78174
 
** An entry for the table is made in the master table on disk, unless
78175
 
** this is a temporary table or db->init.busy==1.  When db->init.busy==1
78176
 
** it means we are reading the sqlite_master table because we just
78177
 
** connected to the database or because the sqlite_master table has
78178
 
** recently changed, so the entry for this table already exists in
78179
 
** the sqlite_master table.  We do not want to create it again.
78180
 
**
78181
 
** If the pSelect argument is not NULL, it means that this routine
78182
 
** was called to create a table generated from a 
78183
 
** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
78184
 
** the new table will match the result set of the SELECT.
78185
 
*/
78186
 
SQLITE_PRIVATE void sqlite3EndTable(
78187
 
  Parse *pParse,          /* Parse context */
78188
 
  Token *pCons,           /* The ',' token after the last column defn. */
78189
 
  Token *pEnd,            /* The final ')' token in the CREATE TABLE */
78190
 
  Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
78191
 
){
78192
 
  Table *p;
78193
 
  sqlite3 *db = pParse->db;
78194
 
  int iDb;
78195
 
 
78196
 
  if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
78197
 
    return;
78198
 
  }
78199
 
  p = pParse->pNewTable;
78200
 
  if( p==0 ) return;
78201
 
 
78202
 
  assert( !db->init.busy || !pSelect );
78203
 
 
78204
 
  iDb = sqlite3SchemaToIndex(db, p->pSchema);
78205
 
 
78206
 
#ifndef SQLITE_OMIT_CHECK
78207
 
  /* Resolve names in all CHECK constraint expressions.
78208
 
  */
78209
 
  if( p->pCheck ){
78210
 
    SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
78211
 
    NameContext sNC;                /* Name context for pParse->pNewTable */
78212
 
 
78213
 
    memset(&sNC, 0, sizeof(sNC));
78214
 
    memset(&sSrc, 0, sizeof(sSrc));
78215
 
    sSrc.nSrc = 1;
78216
 
    sSrc.a[0].zName = p->zName;
78217
 
    sSrc.a[0].pTab = p;
78218
 
    sSrc.a[0].iCursor = -1;
78219
 
    sNC.pParse = pParse;
78220
 
    sNC.pSrcList = &sSrc;
78221
 
    sNC.isCheck = 1;
78222
 
    if( sqlite3ResolveExprNames(&sNC, p->pCheck) ){
78223
 
      return;
78224
 
    }
78225
 
  }
78226
 
#endif /* !defined(SQLITE_OMIT_CHECK) */
78227
 
 
78228
 
  /* If the db->init.busy is 1 it means we are reading the SQL off the
78229
 
  ** "sqlite_master" or "sqlite_temp_master" table on the disk.
78230
 
  ** So do not write to the disk again.  Extract the root page number
78231
 
  ** for the table from the db->init.newTnum field.  (The page number
78232
 
  ** should have been put there by the sqliteOpenCb routine.)
78233
 
  */
78234
 
  if( db->init.busy ){
78235
 
    p->tnum = db->init.newTnum;
78236
 
  }
78237
 
 
78238
 
  /* If not initializing, then create a record for the new table
78239
 
  ** in the SQLITE_MASTER table of the database.
78240
 
  **
78241
 
  ** If this is a TEMPORARY table, write the entry into the auxiliary
78242
 
  ** file instead of into the main database file.
78243
 
  */
78244
 
  if( !db->init.busy ){
78245
 
    int n;
78246
 
    Vdbe *v;
78247
 
    char *zType;    /* "view" or "table" */
78248
 
    char *zType2;   /* "VIEW" or "TABLE" */
78249
 
    char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */
78250
 
 
78251
 
    v = sqlite3GetVdbe(pParse);
78252
 
    if( NEVER(v==0) ) return;
78253
 
 
78254
 
    sqlite3VdbeAddOp1(v, OP_Close, 0);
78255
 
 
78256
 
    /* 
78257
 
    ** Initialize zType for the new view or table.
78258
 
    */
78259
 
    if( p->pSelect==0 ){
78260
 
      /* A regular table */
78261
 
      zType = "table";
78262
 
      zType2 = "TABLE";
78263
 
#ifndef SQLITE_OMIT_VIEW
78264
 
    }else{
78265
 
      /* A view */
78266
 
      zType = "view";
78267
 
      zType2 = "VIEW";
78268
 
#endif
78269
 
    }
78270
 
 
78271
 
    /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
78272
 
    ** statement to populate the new table. The root-page number for the
78273
 
    ** new table is in register pParse->regRoot.
78274
 
    **
78275
 
    ** Once the SELECT has been coded by sqlite3Select(), it is in a
78276
 
    ** suitable state to query for the column names and types to be used
78277
 
    ** by the new table.
78278
 
    **
78279
 
    ** A shared-cache write-lock is not required to write to the new table,
78280
 
    ** as a schema-lock must have already been obtained to create it. Since
78281
 
    ** a schema-lock excludes all other database users, the write-lock would
78282
 
    ** be redundant.
78283
 
    */
78284
 
    if( pSelect ){
78285
 
      SelectDest dest;
78286
 
      Table *pSelTab;
78287
 
 
78288
 
      assert(pParse->nTab==1);
78289
 
      sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
78290
 
      sqlite3VdbeChangeP5(v, 1);
78291
 
      pParse->nTab = 2;
78292
 
      sqlite3SelectDestInit(&dest, SRT_Table, 1);
78293
 
      sqlite3Select(pParse, pSelect, &dest);
78294
 
      sqlite3VdbeAddOp1(v, OP_Close, 1);
78295
 
      if( pParse->nErr==0 ){
78296
 
        pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
78297
 
        if( pSelTab==0 ) return;
78298
 
        assert( p->aCol==0 );
78299
 
        p->nCol = pSelTab->nCol;
78300
 
        p->aCol = pSelTab->aCol;
78301
 
        pSelTab->nCol = 0;
78302
 
        pSelTab->aCol = 0;
78303
 
        sqlite3DeleteTable(db, pSelTab);
78304
 
      }
78305
 
    }
78306
 
 
78307
 
    /* Compute the complete text of the CREATE statement */
78308
 
    if( pSelect ){
78309
 
      zStmt = createTableStmt(db, p);
78310
 
    }else{
78311
 
      n = (int)(pEnd->z - pParse->sNameToken.z) + 1;
78312
 
      zStmt = sqlite3MPrintf(db, 
78313
 
          "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
78314
 
      );
78315
 
    }
78316
 
 
78317
 
    /* A slot for the record has already been allocated in the 
78318
 
    ** SQLITE_MASTER table.  We just need to update that slot with all
78319
 
    ** the information we've collected.
78320
 
    */
78321
 
    sqlite3NestedParse(pParse,
78322
 
      "UPDATE %Q.%s "
78323
 
         "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
78324
 
       "WHERE rowid=#%d",
78325
 
      db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
78326
 
      zType,
78327
 
      p->zName,
78328
 
      p->zName,
78329
 
      pParse->regRoot,
78330
 
      zStmt,
78331
 
      pParse->regRowid
78332
 
    );
78333
 
    sqlite3DbFree(db, zStmt);
78334
 
    sqlite3ChangeCookie(pParse, iDb);
78335
 
 
78336
 
#ifndef SQLITE_OMIT_AUTOINCREMENT
78337
 
    /* Check to see if we need to create an sqlite_sequence table for
78338
 
    ** keeping track of autoincrement keys.
78339
 
    */
78340
 
    if( p->tabFlags & TF_Autoincrement ){
78341
 
      Db *pDb = &db->aDb[iDb];
78342
 
      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
78343
 
      if( pDb->pSchema->pSeqTab==0 ){
78344
 
        sqlite3NestedParse(pParse,
78345
 
          "CREATE TABLE %Q.sqlite_sequence(name,seq)",
78346
 
          pDb->zName
78347
 
        );
78348
 
      }
78349
 
    }
78350
 
#endif
78351
 
 
78352
 
    /* Reparse everything to update our internal data structures */
78353
 
    sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0,
78354
 
        sqlite3MPrintf(db, "tbl_name='%q'",p->zName), P4_DYNAMIC);
78355
 
  }
78356
 
 
78357
 
 
78358
 
  /* Add the table to the in-memory representation of the database.
78359
 
  */
78360
 
  if( db->init.busy ){
78361
 
    Table *pOld;
78362
 
    Schema *pSchema = p->pSchema;
78363
 
    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
78364
 
    pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
78365
 
                             sqlite3Strlen30(p->zName),p);
78366
 
    if( pOld ){
78367
 
      assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
78368
 
      db->mallocFailed = 1;
78369
 
      return;
78370
 
    }
78371
 
    pParse->pNewTable = 0;
78372
 
    db->nTable++;
78373
 
    db->flags |= SQLITE_InternChanges;
78374
 
 
78375
 
#ifndef SQLITE_OMIT_ALTERTABLE
78376
 
    if( !p->pSelect ){
78377
 
      const char *zName = (const char *)pParse->sNameToken.z;
78378
 
      int nName;
78379
 
      assert( !pSelect && pCons && pEnd );
78380
 
      if( pCons->z==0 ){
78381
 
        pCons = pEnd;
78382
 
      }
78383
 
      nName = (int)((const char *)pCons->z - zName);
78384
 
      p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
78385
 
    }
78386
 
#endif
78387
 
  }
78388
 
}
78389
 
 
78390
 
#ifndef SQLITE_OMIT_VIEW
78391
 
/*
78392
 
** The parser calls this routine in order to create a new VIEW
78393
 
*/
78394
 
SQLITE_PRIVATE void sqlite3CreateView(
78395
 
  Parse *pParse,     /* The parsing context */
78396
 
  Token *pBegin,     /* The CREATE token that begins the statement */
78397
 
  Token *pName1,     /* The token that holds the name of the view */
78398
 
  Token *pName2,     /* The token that holds the name of the view */
78399
 
  Select *pSelect,   /* A SELECT statement that will become the new view */
78400
 
  int isTemp,        /* TRUE for a TEMPORARY view */
78401
 
  int noErr          /* Suppress error messages if VIEW already exists */
78402
 
){
78403
 
  Table *p;
78404
 
  int n;
78405
 
  const char *z;
78406
 
  Token sEnd;
78407
 
  DbFixer sFix;
78408
 
  Token *pName;
78409
 
  int iDb;
78410
 
  sqlite3 *db = pParse->db;
78411
 
 
78412
 
  if( pParse->nVar>0 ){
78413
 
    sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
78414
 
    sqlite3SelectDelete(db, pSelect);
78415
 
    return;
78416
 
  }
78417
 
  sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
78418
 
  p = pParse->pNewTable;
78419
 
  if( p==0 || pParse->nErr ){
78420
 
    sqlite3SelectDelete(db, pSelect);
78421
 
    return;
78422
 
  }
78423
 
  sqlite3TwoPartName(pParse, pName1, pName2, &pName);
78424
 
  iDb = sqlite3SchemaToIndex(db, p->pSchema);
78425
 
  if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName)
78426
 
    && sqlite3FixSelect(&sFix, pSelect)
78427
 
  ){
78428
 
    sqlite3SelectDelete(db, pSelect);
78429
 
    return;
78430
 
  }
78431
 
 
78432
 
  /* Make a copy of the entire SELECT statement that defines the view.
78433
 
  ** This will force all the Expr.token.z values to be dynamically
78434
 
  ** allocated rather than point to the input string - which means that
78435
 
  ** they will persist after the current sqlite3_exec() call returns.
78436
 
  */
78437
 
  p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
78438
 
  sqlite3SelectDelete(db, pSelect);
78439
 
  if( db->mallocFailed ){
78440
 
    return;
78441
 
  }
78442
 
  if( !db->init.busy ){
78443
 
    sqlite3ViewGetColumnNames(pParse, p);
78444
 
  }
78445
 
 
78446
 
  /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
78447
 
  ** the end.
78448
 
  */
78449
 
  sEnd = pParse->sLastToken;
78450
 
  if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
78451
 
    sEnd.z += sEnd.n;
78452
 
  }
78453
 
  sEnd.n = 0;
78454
 
  n = (int)(sEnd.z - pBegin->z);
78455
 
  z = pBegin->z;
78456
 
  while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
78457
 
  sEnd.z = &z[n-1];
78458
 
  sEnd.n = 1;
78459
 
 
78460
 
  /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
78461
 
  sqlite3EndTable(pParse, 0, &sEnd, 0);
78462
 
  return;
78463
 
}
78464
 
#endif /* SQLITE_OMIT_VIEW */
78465
 
 
78466
 
#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
78467
 
/*
78468
 
** The Table structure pTable is really a VIEW.  Fill in the names of
78469
 
** the columns of the view in the pTable structure.  Return the number
78470
 
** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
78471
 
*/
78472
 
SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
78473
 
  Table *pSelTab;   /* A fake table from which we get the result set */
78474
 
  Select *pSel;     /* Copy of the SELECT that implements the view */
78475
 
  int nErr = 0;     /* Number of errors encountered */
78476
 
  int n;            /* Temporarily holds the number of cursors assigned */
78477
 
  sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
78478
 
  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
78479
 
 
78480
 
  assert( pTable );
78481
 
 
78482
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
78483
 
  if( sqlite3VtabCallConnect(pParse, pTable) ){
78484
 
    return SQLITE_ERROR;
78485
 
  }
78486
 
  if( IsVirtual(pTable) ) return 0;
78487
 
#endif
78488
 
 
78489
 
#ifndef SQLITE_OMIT_VIEW
78490
 
  /* A positive nCol means the columns names for this view are
78491
 
  ** already known.
78492
 
  */
78493
 
  if( pTable->nCol>0 ) return 0;
78494
 
 
78495
 
  /* A negative nCol is a special marker meaning that we are currently
78496
 
  ** trying to compute the column names.  If we enter this routine with
78497
 
  ** a negative nCol, it means two or more views form a loop, like this:
78498
 
  **
78499
 
  **     CREATE VIEW one AS SELECT * FROM two;
78500
 
  **     CREATE VIEW two AS SELECT * FROM one;
78501
 
  **
78502
 
  ** Actually, the error above is now caught prior to reaching this point.
78503
 
  ** But the following test is still important as it does come up
78504
 
  ** in the following:
78505
 
  ** 
78506
 
  **     CREATE TABLE main.ex1(a);
78507
 
  **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
78508
 
  **     SELECT * FROM temp.ex1;
78509
 
  */
78510
 
  if( pTable->nCol<0 ){
78511
 
    sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
78512
 
    return 1;
78513
 
  }
78514
 
  assert( pTable->nCol>=0 );
78515
 
 
78516
 
  /* If we get this far, it means we need to compute the table names.
78517
 
  ** Note that the call to sqlite3ResultSetOfSelect() will expand any
78518
 
  ** "*" elements in the results set of the view and will assign cursors
78519
 
  ** to the elements of the FROM clause.  But we do not want these changes
78520
 
  ** to be permanent.  So the computation is done on a copy of the SELECT
78521
 
  ** statement that defines the view.
78522
 
  */
78523
 
  assert( pTable->pSelect );
78524
 
  pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
78525
 
  if( pSel ){
78526
 
    u8 enableLookaside = db->lookaside.bEnabled;
78527
 
    n = pParse->nTab;
78528
 
    sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
78529
 
    pTable->nCol = -1;
78530
 
    db->lookaside.bEnabled = 0;
78531
 
#ifndef SQLITE_OMIT_AUTHORIZATION
78532
 
    xAuth = db->xAuth;
78533
 
    db->xAuth = 0;
78534
 
    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
78535
 
    db->xAuth = xAuth;
78536
 
#else
78537
 
    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
78538
 
#endif
78539
 
    db->lookaside.bEnabled = enableLookaside;
78540
 
    pParse->nTab = n;
78541
 
    if( pSelTab ){
78542
 
      assert( pTable->aCol==0 );
78543
 
      pTable->nCol = pSelTab->nCol;
78544
 
      pTable->aCol = pSelTab->aCol;
78545
 
      pSelTab->nCol = 0;
78546
 
      pSelTab->aCol = 0;
78547
 
      sqlite3DeleteTable(db, pSelTab);
78548
 
      assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
78549
 
      pTable->pSchema->flags |= DB_UnresetViews;
78550
 
    }else{
78551
 
      pTable->nCol = 0;
78552
 
      nErr++;
78553
 
    }
78554
 
    sqlite3SelectDelete(db, pSel);
78555
 
  } else {
78556
 
    nErr++;
78557
 
  }
78558
 
#endif /* SQLITE_OMIT_VIEW */
78559
 
  return nErr;  
78560
 
}
78561
 
#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
78562
 
 
78563
 
#ifndef SQLITE_OMIT_VIEW
78564
 
/*
78565
 
** Clear the column names from every VIEW in database idx.
78566
 
*/
78567
 
static void sqliteViewResetAll(sqlite3 *db, int idx){
78568
 
  HashElem *i;
78569
 
  assert( sqlite3SchemaMutexHeld(db, idx, 0) );
78570
 
  if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
78571
 
  for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
78572
 
    Table *pTab = sqliteHashData(i);
78573
 
    if( pTab->pSelect ){
78574
 
      sqliteDeleteColumnNames(db, pTab);
78575
 
      pTab->aCol = 0;
78576
 
      pTab->nCol = 0;
78577
 
    }
78578
 
  }
78579
 
  DbClearProperty(db, idx, DB_UnresetViews);
78580
 
}
78581
 
#else
78582
 
# define sqliteViewResetAll(A,B)
78583
 
#endif /* SQLITE_OMIT_VIEW */
78584
 
 
78585
 
/*
78586
 
** This function is called by the VDBE to adjust the internal schema
78587
 
** used by SQLite when the btree layer moves a table root page. The
78588
 
** root-page of a table or index in database iDb has changed from iFrom
78589
 
** to iTo.
78590
 
**
78591
 
** Ticket #1728:  The symbol table might still contain information
78592
 
** on tables and/or indices that are the process of being deleted.
78593
 
** If you are unlucky, one of those deleted indices or tables might
78594
 
** have the same rootpage number as the real table or index that is
78595
 
** being moved.  So we cannot stop searching after the first match 
78596
 
** because the first match might be for one of the deleted indices
78597
 
** or tables and not the table/index that is actually being moved.
78598
 
** We must continue looping until all tables and indices with
78599
 
** rootpage==iFrom have been converted to have a rootpage of iTo
78600
 
** in order to be certain that we got the right one.
78601
 
*/
78602
 
#ifndef SQLITE_OMIT_AUTOVACUUM
78603
 
SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
78604
 
  HashElem *pElem;
78605
 
  Hash *pHash;
78606
 
  Db *pDb;
78607
 
 
78608
 
  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
78609
 
  pDb = &db->aDb[iDb];
78610
 
  pHash = &pDb->pSchema->tblHash;
78611
 
  for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
78612
 
    Table *pTab = sqliteHashData(pElem);
78613
 
    if( pTab->tnum==iFrom ){
78614
 
      pTab->tnum = iTo;
78615
 
    }
78616
 
  }
78617
 
  pHash = &pDb->pSchema->idxHash;
78618
 
  for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
78619
 
    Index *pIdx = sqliteHashData(pElem);
78620
 
    if( pIdx->tnum==iFrom ){
78621
 
      pIdx->tnum = iTo;
78622
 
    }
78623
 
  }
78624
 
}
78625
 
#endif
78626
 
 
78627
 
/*
78628
 
** Write code to erase the table with root-page iTable from database iDb.
78629
 
** Also write code to modify the sqlite_master table and internal schema
78630
 
** if a root-page of another table is moved by the btree-layer whilst
78631
 
** erasing iTable (this can happen with an auto-vacuum database).
78632
 
*/ 
78633
 
static void destroyRootPage(Parse *pParse, int iTable, int iDb){
78634
 
  Vdbe *v = sqlite3GetVdbe(pParse);
78635
 
  int r1 = sqlite3GetTempReg(pParse);
78636
 
  sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
78637
 
  sqlite3MayAbort(pParse);
78638
 
#ifndef SQLITE_OMIT_AUTOVACUUM
78639
 
  /* OP_Destroy stores an in integer r1. If this integer
78640
 
  ** is non-zero, then it is the root page number of a table moved to
78641
 
  ** location iTable. The following code modifies the sqlite_master table to
78642
 
  ** reflect this.
78643
 
  **
78644
 
  ** The "#NNN" in the SQL is a special constant that means whatever value
78645
 
  ** is in register NNN.  See grammar rules associated with the TK_REGISTER
78646
 
  ** token for additional information.
78647
 
  */
78648
 
  sqlite3NestedParse(pParse, 
78649
 
     "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
78650
 
     pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
78651
 
#endif
78652
 
  sqlite3ReleaseTempReg(pParse, r1);
78653
 
}
78654
 
 
78655
 
/*
78656
 
** Write VDBE code to erase table pTab and all associated indices on disk.
78657
 
** Code to update the sqlite_master tables and internal schema definitions
78658
 
** in case a root-page belonging to another table is moved by the btree layer
78659
 
** is also added (this can happen with an auto-vacuum database).
78660
 
*/
78661
 
static void destroyTable(Parse *pParse, Table *pTab){
78662
 
#ifdef SQLITE_OMIT_AUTOVACUUM
78663
 
  Index *pIdx;
78664
 
  int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
78665
 
  destroyRootPage(pParse, pTab->tnum, iDb);
78666
 
  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
78667
 
    destroyRootPage(pParse, pIdx->tnum, iDb);
78668
 
  }
78669
 
#else
78670
 
  /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
78671
 
  ** is not defined), then it is important to call OP_Destroy on the
78672
 
  ** table and index root-pages in order, starting with the numerically 
78673
 
  ** largest root-page number. This guarantees that none of the root-pages
78674
 
  ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
78675
 
  ** following were coded:
78676
 
  **
78677
 
  ** OP_Destroy 4 0
78678
 
  ** ...
78679
 
  ** OP_Destroy 5 0
78680
 
  **
78681
 
  ** and root page 5 happened to be the largest root-page number in the
78682
 
  ** database, then root page 5 would be moved to page 4 by the 
78683
 
  ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
78684
 
  ** a free-list page.
78685
 
  */
78686
 
  int iTab = pTab->tnum;
78687
 
  int iDestroyed = 0;
78688
 
 
78689
 
  for(;;) {
78690
 
    Index *pIdx;
78691
 
    int iLargest = 0;
78692
 
 
78693
 
    if( iDestroyed==0 || iTab<iDestroyed ){
78694
 
      iLargest = iTab;
78695
 
    }
78696
 
    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
78697
 
      int iIdx = pIdx->tnum;
78698
 
      assert( pIdx->pSchema==pTab->pSchema );
78699
 
      if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
78700
 
        iLargest = iIdx;
78701
 
      }
78702
 
    }
78703
 
    if( iLargest==0 ){
78704
 
      return;
78705
 
    }else{
78706
 
      int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
78707
 
      destroyRootPage(pParse, iLargest, iDb);
78708
 
      iDestroyed = iLargest;
78709
 
    }
78710
 
  }
78711
 
#endif
78712
 
}
78713
 
 
78714
 
/*
78715
 
** This routine is called to do the work of a DROP TABLE statement.
78716
 
** pName is the name of the table to be dropped.
78717
 
*/
78718
 
SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
78719
 
  Table *pTab;
78720
 
  Vdbe *v;
78721
 
  sqlite3 *db = pParse->db;
78722
 
  int iDb;
78723
 
 
78724
 
  if( db->mallocFailed ){
78725
 
    goto exit_drop_table;
78726
 
  }
78727
 
  assert( pParse->nErr==0 );
78728
 
  assert( pName->nSrc==1 );
78729
 
  if( noErr ) db->suppressErr++;
78730
 
  pTab = sqlite3LocateTable(pParse, isView, 
78731
 
                            pName->a[0].zName, pName->a[0].zDatabase);
78732
 
  if( noErr ) db->suppressErr--;
78733
 
 
78734
 
  if( pTab==0 ){
78735
 
    if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
78736
 
    goto exit_drop_table;
78737
 
  }
78738
 
  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
78739
 
  assert( iDb>=0 && iDb<db->nDb );
78740
 
 
78741
 
  /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
78742
 
  ** it is initialized.
78743
 
  */
78744
 
  if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
78745
 
    goto exit_drop_table;
78746
 
  }
78747
 
#ifndef SQLITE_OMIT_AUTHORIZATION
78748
 
  {
78749
 
    int code;
78750
 
    const char *zTab = SCHEMA_TABLE(iDb);
78751
 
    const char *zDb = db->aDb[iDb].zName;
78752
 
    const char *zArg2 = 0;
78753
 
    if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
78754
 
      goto exit_drop_table;
78755
 
    }
78756
 
    if( isView ){
78757
 
                #if (!OMIT_TEMPDB)
78758
 
      if( iDb==1 ){
78759
 
        code = SQLITE_DROP_TEMP_VIEW;
78760
 
      }else{
78761
 
        code = SQLITE_DROP_VIEW;
78762
 
      }
78763
 
                #else
78764
 
                 code = SQLITE_DROP_VIEW;
78765
 
                #endif
78766
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
78767
 
    }else if( IsVirtual(pTab) ){
78768
 
      code = SQLITE_DROP_VTABLE;
78769
 
      zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
78770
 
#endif
78771
 
    }else{
78772
 
                #if (!OMIT_TEMPDB)
78773
 
      if( iDb==1 ){
78774
 
        code = SQLITE_DROP_TEMP_TABLE;
78775
 
      }else{
78776
 
        code = SQLITE_DROP_TABLE;
78777
 
      }
78778
 
                #else
78779
 
                 code = SQLITE_DROP_TABLE;
78780
 
                #endif
78781
 
    }
78782
 
    if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
78783
 
      goto exit_drop_table;
78784
 
    }
78785
 
    if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
78786
 
      goto exit_drop_table;
78787
 
    }
78788
 
  }
78789
 
#endif
78790
 
  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
78791
 
    sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
78792
 
    goto exit_drop_table;
78793
 
  }
78794
 
 
78795
 
#ifndef SQLITE_OMIT_VIEW
78796
 
  /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
78797
 
  ** on a table.
78798
 
  */
78799
 
  if( isView && pTab->pSelect==0 ){
78800
 
    sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
78801
 
    goto exit_drop_table;
78802
 
  }
78803
 
  if( !isView && pTab->pSelect ){
78804
 
    sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
78805
 
    goto exit_drop_table;
78806
 
  }
78807
 
#endif
78808
 
 
78809
 
  /* Generate code to remove the table from the master table
78810
 
  ** on disk.
78811
 
  */
78812
 
  v = sqlite3GetVdbe(pParse);
78813
 
  if( v ){
78814
 
    Trigger *pTrigger;
78815
 
    Db *pDb = &db->aDb[iDb];
78816
 
    sqlite3BeginWriteOperation(pParse, 1, iDb);
78817
 
 
78818
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
78819
 
    if( IsVirtual(pTab) ){
78820
 
      sqlite3VdbeAddOp0(v, OP_VBegin);
78821
 
    }
78822
 
#endif
78823
 
    sqlite3FkDropTable(pParse, pName, pTab);
78824
 
 
78825
 
    /* Drop all triggers associated with the table being dropped. Code
78826
 
    ** is generated to remove entries from sqlite_master and/or
78827
 
    ** sqlite_temp_master if required.
78828
 
    */
78829
 
    pTrigger = sqlite3TriggerList(pParse, pTab);
78830
 
    while( pTrigger ){
78831
 
      assert( pTrigger->pSchema==pTab->pSchema || 
78832
 
          pTrigger->pSchema==db->aDb[1].pSchema );
78833
 
      sqlite3DropTriggerPtr(pParse, pTrigger);
78834
 
      pTrigger = pTrigger->pNext;
78835
 
    }
78836
 
 
78837
 
#ifndef SQLITE_OMIT_AUTOINCREMENT
78838
 
    /* Remove any entries of the sqlite_sequence table associated with
78839
 
    ** the table being dropped. This is done before the table is dropped
78840
 
    ** at the btree level, in case the sqlite_sequence table needs to
78841
 
    ** move as a result of the drop (can happen in auto-vacuum mode).
78842
 
    */
78843
 
    if( pTab->tabFlags & TF_Autoincrement ){
78844
 
      sqlite3NestedParse(pParse,
78845
 
        "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
78846
 
        pDb->zName, pTab->zName
78847
 
      );
78848
 
    }
78849
 
#endif
78850
 
 
78851
 
    /* Drop all SQLITE_MASTER table and index entries that refer to the
78852
 
    ** table. The program name loops through the master table and deletes
78853
 
    ** every row that refers to a table of the same name as the one being
78854
 
    ** dropped. Triggers are handled seperately because a trigger can be
78855
 
    ** created in the temp database that refers to a table in another
78856
 
    ** database.
78857
 
    */
78858
 
    sqlite3NestedParse(pParse, 
78859
 
        "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
78860
 
        pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
78861
 
 
78862
 
    /* Drop any statistics from the sqlite_stat1 table, if it exists */
78863
 
    if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){
78864
 
      sqlite3NestedParse(pParse,
78865
 
        "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q", pDb->zName, pTab->zName
78866
 
      );
78867
 
    }
78868
 
 
78869
 
    if( !isView && !IsVirtual(pTab) ){
78870
 
      destroyTable(pParse, pTab);
78871
 
    }
78872
 
 
78873
 
    /* Remove the table entry from SQLite's internal schema and modify
78874
 
    ** the schema cookie.
78875
 
    */
78876
 
    if( IsVirtual(pTab) ){
78877
 
      sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
78878
 
    }
78879
 
    sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
78880
 
    sqlite3ChangeCookie(pParse, iDb);
78881
 
  }
78882
 
  sqliteViewResetAll(db, iDb);
78883
 
 
78884
 
exit_drop_table:
78885
 
  sqlite3SrcListDelete(db, pName);
78886
 
}
78887
 
 
78888
 
/*
78889
 
** This routine is called to create a new foreign key on the table
78890
 
** currently under construction.  pFromCol determines which columns
78891
 
** in the current table point to the foreign key.  If pFromCol==0 then
78892
 
** connect the key to the last column inserted.  pTo is the name of
78893
 
** the table referred to.  pToCol is a list of tables in the other
78894
 
** pTo table that the foreign key points to.  flags contains all
78895
 
** information about the conflict resolution algorithms specified
78896
 
** in the ON DELETE, ON UPDATE and ON INSERT clauses.
78897
 
**
78898
 
** An FKey structure is created and added to the table currently
78899
 
** under construction in the pParse->pNewTable field.
78900
 
**
78901
 
** The foreign key is set for IMMEDIATE processing.  A subsequent call
78902
 
** to sqlite3DeferForeignKey() might change this to DEFERRED.
78903
 
*/
78904
 
SQLITE_PRIVATE void sqlite3CreateForeignKey(
78905
 
  Parse *pParse,       /* Parsing context */
78906
 
  ExprList *pFromCol,  /* Columns in this table that point to other table */
78907
 
  Token *pTo,          /* Name of the other table */
78908
 
  ExprList *pToCol,    /* Columns in the other table */
78909
 
  int flags            /* Conflict resolution algorithms. */
78910
 
){
78911
 
  sqlite3 *db = pParse->db;
78912
 
#ifndef SQLITE_OMIT_FOREIGN_KEY
78913
 
  FKey *pFKey = 0;
78914
 
  FKey *pNextTo;
78915
 
  Table *p = pParse->pNewTable;
78916
 
  int nByte;
78917
 
  int i;
78918
 
  int nCol;
78919
 
  char *z;
78920
 
 
78921
 
  assert( pTo!=0 );
78922
 
  if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
78923
 
  if( pFromCol==0 ){
78924
 
    int iCol = p->nCol-1;
78925
 
    if( NEVER(iCol<0) ) goto fk_end;
78926
 
    if( pToCol && pToCol->nExpr!=1 ){
78927
 
      sqlite3ErrorMsg(pParse, "foreign key on %s"
78928
 
         " should reference only one column of table %T",
78929
 
         p->aCol[iCol].zName, pTo);
78930
 
      goto fk_end;
78931
 
    }
78932
 
    nCol = 1;
78933
 
  }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
78934
 
    sqlite3ErrorMsg(pParse,
78935
 
        "number of columns in foreign key does not match the number of "
78936
 
        "columns in the referenced table");
78937
 
    goto fk_end;
78938
 
  }else{
78939
 
    nCol = pFromCol->nExpr;
78940
 
  }
78941
 
  nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
78942
 
  if( pToCol ){
78943
 
    for(i=0; i<pToCol->nExpr; i++){
78944
 
      nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
78945
 
    }
78946
 
  }
78947
 
  pFKey = sqlite3DbMallocZero(db, nByte );
78948
 
  if( pFKey==0 ){
78949
 
    goto fk_end;
78950
 
  }
78951
 
  pFKey->pFrom = p;
78952
 
  pFKey->pNextFrom = p->pFKey;
78953
 
  z = (char*)&pFKey->aCol[nCol];
78954
 
  pFKey->zTo = z;
78955
 
  memcpy(z, pTo->z, pTo->n);
78956
 
  z[pTo->n] = 0;
78957
 
  sqlite3Dequote(z);
78958
 
  z += pTo->n+1;
78959
 
  pFKey->nCol = nCol;
78960
 
  if( pFromCol==0 ){
78961
 
    pFKey->aCol[0].iFrom = p->nCol-1;
78962
 
  }else{
78963
 
    for(i=0; i<nCol; i++){
78964
 
      int j;
78965
 
      for(j=0; j<p->nCol; j++){
78966
 
        if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
78967
 
          pFKey->aCol[i].iFrom = j;
78968
 
          break;
78969
 
        }
78970
 
      }
78971
 
      if( j>=p->nCol ){
78972
 
        sqlite3ErrorMsg(pParse, 
78973
 
          "unknown column \"%s\" in foreign key definition", 
78974
 
          pFromCol->a[i].zName);
78975
 
        goto fk_end;
78976
 
      }
78977
 
    }
78978
 
  }
78979
 
  if( pToCol ){
78980
 
    for(i=0; i<nCol; i++){
78981
 
      int n = sqlite3Strlen30(pToCol->a[i].zName);
78982
 
      pFKey->aCol[i].zCol = z;
78983
 
      memcpy(z, pToCol->a[i].zName, n);
78984
 
      z[n] = 0;
78985
 
      z += n+1;
78986
 
    }
78987
 
  }
78988
 
  pFKey->isDeferred = 0;
78989
 
  pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
78990
 
  pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
78991
 
 
78992
 
  assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
78993
 
  pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 
78994
 
      pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
78995
 
  );
78996
 
  if( pNextTo==pFKey ){
78997
 
    db->mallocFailed = 1;
78998
 
    goto fk_end;
78999
 
  }
79000
 
  if( pNextTo ){
79001
 
    assert( pNextTo->pPrevTo==0 );
79002
 
    pFKey->pNextTo = pNextTo;
79003
 
    pNextTo->pPrevTo = pFKey;
79004
 
  }
79005
 
 
79006
 
  /* Link the foreign key to the table as the last step.
79007
 
  */
79008
 
  p->pFKey = pFKey;
79009
 
  pFKey = 0;
79010
 
 
79011
 
fk_end:
79012
 
  sqlite3DbFree(db, pFKey);
79013
 
#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
79014
 
  sqlite3ExprListDelete(db, pFromCol);
79015
 
  sqlite3ExprListDelete(db, pToCol);
79016
 
}
79017
 
 
79018
 
/*
79019
 
** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
79020
 
** clause is seen as part of a foreign key definition.  The isDeferred
79021
 
** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
79022
 
** The behavior of the most recently created foreign key is adjusted
79023
 
** accordingly.
79024
 
*/
79025
 
SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
79026
 
#ifndef SQLITE_OMIT_FOREIGN_KEY
79027
 
  Table *pTab;
79028
 
  FKey *pFKey;
79029
 
  if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
79030
 
  assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
79031
 
  pFKey->isDeferred = (u8)isDeferred;
79032
 
#endif
79033
 
}
79034
 
 
79035
 
/*
79036
 
** Generate code that will erase and refill index *pIdx.  This is
79037
 
** used to initialize a newly created index or to recompute the
79038
 
** content of an index in response to a REINDEX command.
79039
 
**
79040
 
** if memRootPage is not negative, it means that the index is newly
79041
 
** created.  The register specified by memRootPage contains the
79042
 
** root page number of the index.  If memRootPage is negative, then
79043
 
** the index already exists and must be cleared before being refilled and
79044
 
** the root page number of the index is taken from pIndex->tnum.
79045
 
*/
79046
 
static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
79047
 
  Table *pTab = pIndex->pTable;  /* The table that is indexed */
79048
 
  int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
79049
 
  int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
79050
 
  int addr1;                     /* Address of top of loop */
79051
 
  int tnum;                      /* Root page of index */
79052
 
  Vdbe *v;                       /* Generate code into this virtual machine */
79053
 
  KeyInfo *pKey;                 /* KeyInfo for index */
79054
 
  int regIdxKey;                 /* Registers containing the index key */
79055
 
  int regRecord;                 /* Register holding assemblied index record */
79056
 
  sqlite3 *db = pParse->db;      /* The database connection */
79057
 
  int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
79058
 
 
79059
 
#ifndef SQLITE_OMIT_AUTHORIZATION
79060
 
  if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
79061
 
      db->aDb[iDb].zName ) ){
79062
 
    return;
79063
 
  }
79064
 
#endif
79065
 
 
79066
 
  /* Require a write-lock on the table to perform this operation */
79067
 
  sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
79068
 
 
79069
 
  v = sqlite3GetVdbe(pParse);
79070
 
  if( v==0 ) return;
79071
 
  if( memRootPage>=0 ){
79072
 
    tnum = memRootPage;
79073
 
  }else{
79074
 
    tnum = pIndex->tnum;
79075
 
    sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
79076
 
  }
79077
 
  pKey = sqlite3IndexKeyinfo(pParse, pIndex);
79078
 
  sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
79079
 
                    (char *)pKey, P4_KEYINFO_HANDOFF);
79080
 
  if( memRootPage>=0 ){
79081
 
    sqlite3VdbeChangeP5(v, 1);
79082
 
  }
79083
 
  sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
79084
 
  addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
79085
 
  regRecord = sqlite3GetTempReg(pParse);
79086
 
  regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
79087
 
  if( pIndex->onError!=OE_None ){
79088
 
    const int regRowid = regIdxKey + pIndex->nColumn;
79089
 
    const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
79090
 
    void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
79091
 
 
79092
 
    /* The registers accessed by the OP_IsUnique opcode were allocated
79093
 
    ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
79094
 
    ** call above. Just before that function was freed they were released
79095
 
    ** (made available to the compiler for reuse) using 
79096
 
    ** sqlite3ReleaseTempRange(). So in some ways having the OP_IsUnique
79097
 
    ** opcode use the values stored within seems dangerous. However, since
79098
 
    ** we can be sure that no other temp registers have been allocated
79099
 
    ** since sqlite3ReleaseTempRange() was called, it is safe to do so.
79100
 
    */
79101
 
    sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
79102
 
    sqlite3HaltConstraint(
79103
 
        pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
79104
 
  }
79105
 
  sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
79106
 
  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
79107
 
  sqlite3ReleaseTempReg(pParse, regRecord);
79108
 
  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
79109
 
  sqlite3VdbeJumpHere(v, addr1);
79110
 
  sqlite3VdbeAddOp1(v, OP_Close, iTab);
79111
 
  sqlite3VdbeAddOp1(v, OP_Close, iIdx);
79112
 
}
79113
 
 
79114
 
/*
79115
 
** Create a new index for an SQL table.  pName1.pName2 is the name of the index 
79116
 
** and pTblList is the name of the table that is to be indexed.  Both will 
79117
 
** be NULL for a primary key or an index that is created to satisfy a
79118
 
** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
79119
 
** as the table to be indexed.  pParse->pNewTable is a table that is
79120
 
** currently being constructed by a CREATE TABLE statement.
79121
 
**
79122
 
** pList is a list of columns to be indexed.  pList will be NULL if this
79123
 
** is a primary key or unique-constraint on the most recent column added
79124
 
** to the table currently under construction.  
79125
 
**
79126
 
** If the index is created successfully, return a pointer to the new Index
79127
 
** structure. This is used by sqlite3AddPrimaryKey() to mark the index
79128
 
** as the tables primary key (Index.autoIndex==2).
79129
 
*/
79130
 
SQLITE_PRIVATE Index *sqlite3CreateIndex(
79131
 
  Parse *pParse,     /* All information about this parse */
79132
 
  Token *pName1,     /* First part of index name. May be NULL */
79133
 
  Token *pName2,     /* Second part of index name. May be NULL */
79134
 
  SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
79135
 
  ExprList *pList,   /* A list of columns to be indexed */
79136
 
  int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
79137
 
  Token *pStart,     /* The CREATE token that begins this statement */
79138
 
  Token *pEnd,       /* The ")" that closes the CREATE INDEX statement */
79139
 
  int sortOrder,     /* Sort order of primary key when pList==NULL */
79140
 
  int ifNotExist     /* Omit error if index already exists */
79141
 
){
79142
 
  Index *pRet = 0;     /* Pointer to return */
79143
 
  Table *pTab = 0;     /* Table to be indexed */
79144
 
  Index *pIndex = 0;   /* The index to be created */
79145
 
  char *zName = 0;     /* Name of the index */
79146
 
  int nName;           /* Number of characters in zName */
79147
 
  int i, j;
79148
 
  Token nullId;        /* Fake token for an empty ID list */
79149
 
  DbFixer sFix;        /* For assigning database names to pTable */
79150
 
  int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
79151
 
  sqlite3 *db = pParse->db;
79152
 
  Db *pDb;             /* The specific table containing the indexed database */
79153
 
  int iDb;             /* Index of the database that is being written */
79154
 
  Token *pName = 0;    /* Unqualified name of the index to create */
79155
 
  struct ExprList_item *pListItem; /* For looping over pList */
79156
 
  int nCol;
79157
 
  int nExtra = 0;
79158
 
  char *zExtra;
79159
 
 
79160
 
  assert( pStart==0 || pEnd!=0 ); /* pEnd must be non-NULL if pStart is */
79161
 
  assert( pParse->nErr==0 );      /* Never called with prior errors */
79162
 
  if( db->mallocFailed || IN_DECLARE_VTAB ){
79163
 
    goto exit_create_index;
79164
 
  }
79165
 
  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
79166
 
    goto exit_create_index;
79167
 
  }
79168
 
 
79169
 
  /*
79170
 
  ** Find the table that is to be indexed.  Return early if not found.
79171
 
  */
79172
 
  if( pTblName!=0 ){
79173
 
 
79174
 
    /* Use the two-part index name to determine the database 
79175
 
    ** to search for the table. 'Fix' the table name to this db
79176
 
    ** before looking up the table.
79177
 
    */
79178
 
    assert( pName1 && pName2 );
79179
 
    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
79180
 
    if( iDb<0 ) goto exit_create_index;
79181
 
 
79182
 
#ifndef SQLITE_OMIT_TEMPDB
79183
 
    /* If the index name was unqualified, check if the the table
79184
 
    ** is a temp table. If so, set the database to 1. Do not do this
79185
 
    ** if initialising a database schema.
79186
 
    */
79187
 
    if( !db->init.busy ){
79188
 
      pTab = sqlite3SrcListLookup(pParse, pTblName);
79189
 
      if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
79190
 
        iDb = 1;
79191
 
      }
79192
 
    }
79193
 
#endif
79194
 
 
79195
 
    if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
79196
 
        sqlite3FixSrcList(&sFix, pTblName)
79197
 
    ){
79198
 
      /* Because the parser constructs pTblName from a single identifier,
79199
 
      ** sqlite3FixSrcList can never fail. */
79200
 
      assert(0);
79201
 
    }
79202
 
    pTab = sqlite3LocateTable(pParse, 0, pTblName->a[0].zName, 
79203
 
        pTblName->a[0].zDatabase);
79204
 
    if( !pTab || db->mallocFailed ) goto exit_create_index;
79205
 
    assert( db->aDb[iDb].pSchema==pTab->pSchema );
79206
 
  }else{
79207
 
    assert( pName==0 );
79208
 
    pTab = pParse->pNewTable;
79209
 
    if( !pTab ) goto exit_create_index;
79210
 
    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
79211
 
  }
79212
 
  pDb = &db->aDb[iDb];
79213
 
 
79214
 
  assert( pTab!=0 );
79215
 
  assert( pParse->nErr==0 );
79216
 
  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
79217
 
       && memcmp(&pTab->zName[7],"altertab_",9)!=0 ){
79218
 
    sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
79219
 
    goto exit_create_index;
79220
 
  }
79221
 
#ifndef SQLITE_OMIT_VIEW
79222
 
  if( pTab->pSelect ){
79223
 
    sqlite3ErrorMsg(pParse, "views may not be indexed");
79224
 
    goto exit_create_index;
79225
 
  }
79226
 
#endif
79227
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
79228
 
  if( IsVirtual(pTab) ){
79229
 
    sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
79230
 
    goto exit_create_index;
79231
 
  }
79232
 
#endif
79233
 
 
79234
 
  /*
79235
 
  ** Find the name of the index.  Make sure there is not already another
79236
 
  ** index or table with the same name.  
79237
 
  **
79238
 
  ** Exception:  If we are reading the names of permanent indices from the
79239
 
  ** sqlite_master table (because some other process changed the schema) and
79240
 
  ** one of the index names collides with the name of a temporary table or
79241
 
  ** index, then we will continue to process this index.
79242
 
  **
79243
 
  ** If pName==0 it means that we are
79244
 
  ** dealing with a primary key or UNIQUE constraint.  We have to invent our
79245
 
  ** own name.
79246
 
  */
79247
 
  if( pName ){
79248
 
    zName = sqlite3NameFromToken(db, pName);
79249
 
    if( zName==0 ) goto exit_create_index;
79250
 
    if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
79251
 
      goto exit_create_index;
79252
 
    }
79253
 
    if( !db->init.busy ){
79254
 
      if( sqlite3FindTable(db, zName, 0)!=0 ){
79255
 
        sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
79256
 
        goto exit_create_index;
79257
 
      }
79258
 
    }
79259
 
    if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
79260
 
      if( !ifNotExist ){
79261
 
        sqlite3ErrorMsg(pParse, "index %s already exists", zName);
79262
 
      }else{
79263
 
        assert( !db->init.busy );
79264
 
        sqlite3CodeVerifySchema(pParse, iDb);
79265
 
      }
79266
 
      goto exit_create_index;
79267
 
    }
79268
 
  }else{
79269
 
    int n;
79270
 
    Index *pLoop;
79271
 
    for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
79272
 
    zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
79273
 
    if( zName==0 ){
79274
 
      goto exit_create_index;
79275
 
    }
79276
 
  }
79277
 
 
79278
 
  /* Check for authorization to create an index.
79279
 
  */
79280
 
#ifndef SQLITE_OMIT_AUTHORIZATION
79281
 
  {
79282
 
    const char *zDb = pDb->zName;
79283
 
    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
79284
 
      goto exit_create_index;
79285
 
    }
79286
 
    i = SQLITE_CREATE_INDEX;
79287
 
         #if (!OMIT_TEMPDB )
79288
 
    if(  iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
79289
 
         #endif
79290
 
    if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
79291
 
      goto exit_create_index;
79292
 
    }
79293
 
  }
79294
 
#endif
79295
 
 
79296
 
  /* If pList==0, it means this routine was called to make a primary
79297
 
  ** key out of the last column added to the table under construction.
79298
 
  ** So create a fake list to simulate this.
79299
 
  */
79300
 
  if( pList==0 ){
79301
 
    nullId.z = pTab->aCol[pTab->nCol-1].zName;
79302
 
    nullId.n = sqlite3Strlen30((char*)nullId.z);
79303
 
    pList = sqlite3ExprListAppend(pParse, 0, 0);
79304
 
    if( pList==0 ) goto exit_create_index;
79305
 
    sqlite3ExprListSetName(pParse, pList, &nullId, 0);
79306
 
    pList->a[0].sortOrder = (u8)sortOrder;
79307
 
  }
79308
 
 
79309
 
  /* Figure out how many bytes of space are required to store explicitly
79310
 
  ** specified collation sequence names.
79311
 
  */
79312
 
  for(i=0; i<pList->nExpr; i++){
79313
 
    Expr *pExpr = pList->a[i].pExpr;
79314
 
    if( pExpr ){
79315
 
      CollSeq *pColl = pExpr->pColl;
79316
 
      /* Either pColl!=0 or there was an OOM failure.  But if an OOM
79317
 
      ** failure we have quit before reaching this point. */
79318
 
      if( ALWAYS(pColl) ){
79319
 
        nExtra += (1 + sqlite3Strlen30(pColl->zName));
79320
 
      }
79321
 
    }
79322
 
  }
79323
 
 
79324
 
  /* 
79325
 
  ** Allocate the index structure. 
79326
 
  */
79327
 
  nName = sqlite3Strlen30(zName);
79328
 
  nCol = pList->nExpr;
79329
 
  pIndex = sqlite3DbMallocZero(db, 
79330
 
      sizeof(Index) +              /* Index structure  */
79331
 
      sizeof(int)*nCol +           /* Index.aiColumn   */
79332
 
      sizeof(int)*(nCol+1) +       /* Index.aiRowEst   */
79333
 
      sizeof(char *)*nCol +        /* Index.azColl     */
79334
 
      sizeof(u8)*nCol +            /* Index.aSortOrder */
79335
 
      nName + 1 +                  /* Index.zName      */
79336
 
      nExtra                       /* Collation sequence names */
79337
 
  );
79338
 
  if( db->mallocFailed ){
79339
 
    goto exit_create_index;
79340
 
  }
79341
 
  pIndex->azColl = (char**)(&pIndex[1]);
79342
 
  pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
79343
 
  pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
79344
 
  pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
79345
 
  pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
79346
 
  zExtra = (char *)(&pIndex->zName[nName+1]);
79347
 
  memcpy(pIndex->zName, zName, nName+1);
79348
 
  pIndex->pTable = pTab;
79349
 
  pIndex->nColumn = pList->nExpr;
79350
 
  pIndex->onError = (u8)onError;
79351
 
  pIndex->autoIndex = (u8)(pName==0);
79352
 
  pIndex->pSchema = db->aDb[iDb].pSchema;
79353
 
  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
79354
 
 
79355
 
  /* Check to see if we should honor DESC requests on index columns
79356
 
  */
79357
 
  if( pDb->pSchema->file_format>=4 ){
79358
 
    sortOrderMask = -1;   /* Honor DESC */
79359
 
  }else{
79360
 
    sortOrderMask = 0;    /* Ignore DESC */
79361
 
  }
79362
 
 
79363
 
  /* Scan the names of the columns of the table to be indexed and
79364
 
  ** load the column indices into the Index structure.  Report an error
79365
 
  ** if any column is not found.
79366
 
  **
79367
 
  ** TODO:  Add a test to make sure that the same column is not named
79368
 
  ** more than once within the same index.  Only the first instance of
79369
 
  ** the column will ever be used by the optimizer.  Note that using the
79370
 
  ** same column more than once cannot be an error because that would 
79371
 
  ** break backwards compatibility - it needs to be a warning.
79372
 
  */
79373
 
  for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
79374
 
    const char *zColName = pListItem->zName;
79375
 
    Column *pTabCol;
79376
 
    int requestedSortOrder;
79377
 
    char *zColl;                   /* Collation sequence name */
79378
 
 
79379
 
    for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
79380
 
      if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
79381
 
    }
79382
 
    if( j>=pTab->nCol ){
79383
 
      sqlite3ErrorMsg(pParse, "table %s has no column named %s",
79384
 
        pTab->zName, zColName);
79385
 
      pParse->checkSchema = 1;
79386
 
      goto exit_create_index;
79387
 
    }
79388
 
    pIndex->aiColumn[i] = j;
79389
 
    /* Justification of the ALWAYS(pListItem->pExpr->pColl):  Because of
79390
 
    ** the way the "idxlist" non-terminal is constructed by the parser,
79391
 
    ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl
79392
 
    ** must exist or else there must have been an OOM error.  But if there
79393
 
    ** was an OOM error, we would never reach this point. */
79394
 
    if( pListItem->pExpr && ALWAYS(pListItem->pExpr->pColl) ){
79395
 
      int nColl;
79396
 
      zColl = pListItem->pExpr->pColl->zName;
79397
 
      nColl = sqlite3Strlen30(zColl) + 1;
79398
 
      assert( nExtra>=nColl );
79399
 
      memcpy(zExtra, zColl, nColl);
79400
 
      zColl = zExtra;
79401
 
      zExtra += nColl;
79402
 
      nExtra -= nColl;
79403
 
    }else{
79404
 
      zColl = pTab->aCol[j].zColl;
79405
 
      if( !zColl ){
79406
 
        zColl = db->pDfltColl->zName;
79407
 
      }
79408
 
    }
79409
 
    if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
79410
 
      goto exit_create_index;
79411
 
    }
79412
 
    pIndex->azColl[i] = zColl;
79413
 
    requestedSortOrder = pListItem->sortOrder & sortOrderMask;
79414
 
    pIndex->aSortOrder[i] = (u8)requestedSortOrder;
79415
 
  }
79416
 
  sqlite3DefaultRowEst(pIndex);
79417
 
 
79418
 
  if( pTab==pParse->pNewTable ){
79419
 
    /* This routine has been called to create an automatic index as a
79420
 
    ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
79421
 
    ** a PRIMARY KEY or UNIQUE clause following the column definitions.
79422
 
    ** i.e. one of:
79423
 
    **
79424
 
    ** CREATE TABLE t(x PRIMARY KEY, y);
79425
 
    ** CREATE TABLE t(x, y, UNIQUE(x, y));
79426
 
    **
79427
 
    ** Either way, check to see if the table already has such an index. If
79428
 
    ** so, don't bother creating this one. This only applies to
79429
 
    ** automatically created indices. Users can do as they wish with
79430
 
    ** explicit indices.
79431
 
    **
79432
 
    ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
79433
 
    ** (and thus suppressing the second one) even if they have different
79434
 
    ** sort orders.
79435
 
    **
79436
 
    ** If there are different collating sequences or if the columns of
79437
 
    ** the constraint occur in different orders, then the constraints are
79438
 
    ** considered distinct and both result in separate indices.
79439
 
    */
79440
 
    Index *pIdx;
79441
 
    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
79442
 
      int k;
79443
 
      assert( pIdx->onError!=OE_None );
79444
 
      assert( pIdx->autoIndex );
79445
 
      assert( pIndex->onError!=OE_None );
79446
 
 
79447
 
      if( pIdx->nColumn!=pIndex->nColumn ) continue;
79448
 
      for(k=0; k<pIdx->nColumn; k++){
79449
 
        const char *z1;
79450
 
        const char *z2;
79451
 
        if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
79452
 
        z1 = pIdx->azColl[k];
79453
 
        z2 = pIndex->azColl[k];
79454
 
        if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
79455
 
      }
79456
 
      if( k==pIdx->nColumn ){
79457
 
        if( pIdx->onError!=pIndex->onError ){
79458
 
          /* This constraint creates the same index as a previous
79459
 
          ** constraint specified somewhere in the CREATE TABLE statement.
79460
 
          ** However the ON CONFLICT clauses are different. If both this 
79461
 
          ** constraint and the previous equivalent constraint have explicit
79462
 
          ** ON CONFLICT clauses this is an error. Otherwise, use the
79463
 
          ** explicitly specified behaviour for the index.
79464
 
          */
79465
 
          if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
79466
 
            sqlite3ErrorMsg(pParse, 
79467
 
                "conflicting ON CONFLICT clauses specified", 0);
79468
 
          }
79469
 
          if( pIdx->onError==OE_Default ){
79470
 
            pIdx->onError = pIndex->onError;
79471
 
          }
79472
 
        }
79473
 
        goto exit_create_index;
79474
 
      }
79475
 
    }
79476
 
  }
79477
 
 
79478
 
  /* Link the new Index structure to its table and to the other
79479
 
  ** in-memory database structures. 
79480
 
  */
79481
 
  if( db->init.busy ){
79482
 
    Index *p;
79483
 
    assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
79484
 
    p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
79485
 
                          pIndex->zName, sqlite3Strlen30(pIndex->zName),
79486
 
                          pIndex);
79487
 
    if( p ){
79488
 
      assert( p==pIndex );  /* Malloc must have failed */
79489
 
      db->mallocFailed = 1;
79490
 
      goto exit_create_index;
79491
 
    }
79492
 
    db->flags |= SQLITE_InternChanges;
79493
 
    if( pTblName!=0 ){
79494
 
      pIndex->tnum = db->init.newTnum;
79495
 
    }
79496
 
  }
79497
 
 
79498
 
  /* If the db->init.busy is 0 then create the index on disk.  This
79499
 
  ** involves writing the index into the master table and filling in the
79500
 
  ** index with the current table contents.
79501
 
  **
79502
 
  ** The db->init.busy is 0 when the user first enters a CREATE INDEX 
79503
 
  ** command.  db->init.busy is 1 when a database is opened and 
79504
 
  ** CREATE INDEX statements are read out of the master table.  In
79505
 
  ** the latter case the index already exists on disk, which is why
79506
 
  ** we don't want to recreate it.
79507
 
  **
79508
 
  ** If pTblName==0 it means this index is generated as a primary key
79509
 
  ** or UNIQUE constraint of a CREATE TABLE statement.  Since the table
79510
 
  ** has just been created, it contains no data and the index initialization
79511
 
  ** step can be skipped.
79512
 
  */
79513
 
  else{ /* if( db->init.busy==0 ) */
79514
 
    Vdbe *v;
79515
 
    char *zStmt;
79516
 
    int iMem = ++pParse->nMem;
79517
 
 
79518
 
    v = sqlite3GetVdbe(pParse);
79519
 
    if( v==0 ) goto exit_create_index;
79520
 
 
79521
 
 
79522
 
    /* Create the rootpage for the index
79523
 
    */
79524
 
    sqlite3BeginWriteOperation(pParse, 1, iDb);
79525
 
    sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
79526
 
 
79527
 
    /* Gather the complete text of the CREATE INDEX statement into
79528
 
    ** the zStmt variable
79529
 
    */
79530
 
    if( pStart ){
79531
 
      assert( pEnd!=0 );
79532
 
      /* A named index with an explicit CREATE INDEX statement */
79533
 
      zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
79534
 
        onError==OE_None ? "" : " UNIQUE",
79535
 
        pEnd->z - pName->z + 1,
79536
 
        pName->z);
79537
 
    }else{
79538
 
      /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
79539
 
      /* zStmt = sqlite3MPrintf(""); */
79540
 
      zStmt = 0;
79541
 
    }
79542
 
 
79543
 
    /* Add an entry in sqlite_master for this index
79544
 
    */
79545
 
    sqlite3NestedParse(pParse, 
79546
 
        "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
79547
 
        db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
79548
 
        pIndex->zName,
79549
 
        pTab->zName,
79550
 
        iMem,
79551
 
        zStmt
79552
 
    );
79553
 
    sqlite3DbFree(db, zStmt);
79554
 
 
79555
 
    /* Fill the index with data and reparse the schema. Code an OP_Expire
79556
 
    ** to invalidate all pre-compiled statements.
79557
 
    */
79558
 
    if( pTblName ){
79559
 
      sqlite3RefillIndex(pParse, pIndex, iMem);
79560
 
      sqlite3ChangeCookie(pParse, iDb);
79561
 
      sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0,
79562
 
         sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 
79563
 
         P4_DYNAMIC);
79564
 
      sqlite3VdbeAddOp1(v, OP_Expire, 0);
79565
 
    }
79566
 
  }
79567
 
 
79568
 
  /* When adding an index to the list of indices for a table, make
79569
 
  ** sure all indices labeled OE_Replace come after all those labeled
79570
 
  ** OE_Ignore.  This is necessary for the correct constraint check
79571
 
  ** processing (in sqlite3GenerateConstraintChecks()) as part of
79572
 
  ** UPDATE and INSERT statements.  
79573
 
  */
79574
 
  if( db->init.busy || pTblName==0 ){
79575
 
    if( onError!=OE_Replace || pTab->pIndex==0
79576
 
         || pTab->pIndex->onError==OE_Replace){
79577
 
      pIndex->pNext = pTab->pIndex;
79578
 
      pTab->pIndex = pIndex;
79579
 
    }else{
79580
 
      Index *pOther = pTab->pIndex;
79581
 
      while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
79582
 
        pOther = pOther->pNext;
79583
 
      }
79584
 
      pIndex->pNext = pOther->pNext;
79585
 
      pOther->pNext = pIndex;
79586
 
    }
79587
 
    pRet = pIndex;
79588
 
    pIndex = 0;
79589
 
  }
79590
 
 
79591
 
  /* Clean up before exiting */
79592
 
exit_create_index:
79593
 
  if( pIndex ){
79594
 
    sqlite3DbFree(db, pIndex->zColAff);
79595
 
    sqlite3DbFree(db, pIndex);
79596
 
  }
79597
 
  sqlite3ExprListDelete(db, pList);
79598
 
  sqlite3SrcListDelete(db, pTblName);
79599
 
  sqlite3DbFree(db, zName);
79600
 
  return pRet;
79601
 
}
79602
 
 
79603
 
/*
79604
 
** Fill the Index.aiRowEst[] array with default information - information
79605
 
** to be used when we have not run the ANALYZE command.
79606
 
**
79607
 
** aiRowEst[0] is suppose to contain the number of elements in the index.
79608
 
** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
79609
 
** number of rows in the table that match any particular value of the
79610
 
** first column of the index.  aiRowEst[2] is an estimate of the number
79611
 
** of rows that match any particular combiniation of the first 2 columns
79612
 
** of the index.  And so forth.  It must always be the case that
79613
 
*
79614
 
**           aiRowEst[N]<=aiRowEst[N-1]
79615
 
**           aiRowEst[N]>=1
79616
 
**
79617
 
** Apart from that, we have little to go on besides intuition as to
79618
 
** how aiRowEst[] should be initialized.  The numbers generated here
79619
 
** are based on typical values found in actual indices.
79620
 
*/
79621
 
SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
79622
 
  unsigned *a = pIdx->aiRowEst;
79623
 
  int i;
79624
 
  unsigned n;
79625
 
  assert( a!=0 );
79626
 
  a[0] = pIdx->pTable->nRowEst;
79627
 
  if( a[0]<10 ) a[0] = 10;
79628
 
  n = 10;
79629
 
  for(i=1; i<=pIdx->nColumn; i++){
79630
 
    a[i] = n;
79631
 
    if( n>5 ) n--;
79632
 
  }
79633
 
  if( pIdx->onError!=OE_None ){
79634
 
    a[pIdx->nColumn] = 1;
79635
 
  }
79636
 
}
79637
 
 
79638
 
/*
79639
 
** This routine will drop an existing named index.  This routine
79640
 
** implements the DROP INDEX statement.
79641
 
*/
79642
 
SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
79643
 
  Index *pIndex;
79644
 
  Vdbe *v;
79645
 
  sqlite3 *db = pParse->db;
79646
 
  int iDb;
79647
 
 
79648
 
  assert( pParse->nErr==0 );   /* Never called with prior errors */
79649
 
  if( db->mallocFailed ){
79650
 
    goto exit_drop_index;
79651
 
  }
79652
 
  assert( pName->nSrc==1 );
79653
 
  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
79654
 
    goto exit_drop_index;
79655
 
  }
79656
 
  pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
79657
 
  if( pIndex==0 ){
79658
 
    if( !ifExists ){
79659
 
      sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
79660
 
    }else{
79661
 
      sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
79662
 
    }
79663
 
    pParse->checkSchema = 1;
79664
 
    goto exit_drop_index;
79665
 
  }
79666
 
  if( pIndex->autoIndex ){
79667
 
    sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
79668
 
      "or PRIMARY KEY constraint cannot be dropped", 0);
79669
 
    goto exit_drop_index;
79670
 
  }
79671
 
  iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
79672
 
#ifndef SQLITE_OMIT_AUTHORIZATION
79673
 
  {
79674
 
    int code = SQLITE_DROP_INDEX;
79675
 
    Table *pTab = pIndex->pTable;
79676
 
    const char *zDb = db->aDb[iDb].zName;
79677
 
    const char *zTab = SCHEMA_TABLE(iDb);
79678
 
    if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
79679
 
      goto exit_drop_index;
79680
 
    }
79681
 
         #if (!OMIT_TEMPDB)
79682
 
    if( iDb ) code = SQLITE_DROP_TEMP_INDEX;
79683
 
         #endif
79684
 
    if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
79685
 
      goto exit_drop_index;
79686
 
    }
79687
 
  }
79688
 
#endif
79689
 
 
79690
 
  /* Generate code to remove the index and from the master table */
79691
 
  v = sqlite3GetVdbe(pParse);
79692
 
  if( v ){
79693
 
    sqlite3BeginWriteOperation(pParse, 1, iDb);
79694
 
    sqlite3NestedParse(pParse,
79695
 
       "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
79696
 
       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
79697
 
       pIndex->zName
79698
 
    );
79699
 
    if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){
79700
 
      sqlite3NestedParse(pParse,
79701
 
        "DELETE FROM %Q.sqlite_stat1 WHERE idx=%Q",
79702
 
        db->aDb[iDb].zName, pIndex->zName
79703
 
      );
79704
 
    }
79705
 
    sqlite3ChangeCookie(pParse, iDb);
79706
 
    destroyRootPage(pParse, pIndex->tnum, iDb);
79707
 
    sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
79708
 
  }
79709
 
 
79710
 
exit_drop_index:
79711
 
  sqlite3SrcListDelete(db, pName);
79712
 
}
79713
 
 
79714
 
/*
79715
 
** pArray is a pointer to an array of objects.  Each object in the
79716
 
** array is szEntry bytes in size.  This routine allocates a new
79717
 
** object on the end of the array.
79718
 
**
79719
 
** *pnEntry is the number of entries already in use.  *pnAlloc is
79720
 
** the previously allocated size of the array.  initSize is the
79721
 
** suggested initial array size allocation.
79722
 
**
79723
 
** The index of the new entry is returned in *pIdx.
79724
 
**
79725
 
** This routine returns a pointer to the array of objects.  This
79726
 
** might be the same as the pArray parameter or it might be a different
79727
 
** pointer if the array was resized.
79728
 
*/
79729
 
SQLITE_PRIVATE void *sqlite3ArrayAllocate(
79730
 
  sqlite3 *db,      /* Connection to notify of malloc failures */
79731
 
  void *pArray,     /* Array of objects.  Might be reallocated */
79732
 
  int szEntry,      /* Size of each object in the array */
79733
 
  int initSize,     /* Suggested initial allocation, in elements */
79734
 
  int *pnEntry,     /* Number of objects currently in use */
79735
 
  int *pnAlloc,     /* Current size of the allocation, in elements */
79736
 
  int *pIdx         /* Write the index of a new slot here */
79737
 
){
79738
 
  char *z;
79739
 
  if( *pnEntry >= *pnAlloc ){
79740
 
    void *pNew;
79741
 
    int newSize;
79742
 
    newSize = (*pnAlloc)*2 + initSize;
79743
 
    pNew = sqlite3DbRealloc(db, pArray, newSize*szEntry);
79744
 
    if( pNew==0 ){
79745
 
      *pIdx = -1;
79746
 
      return pArray;
79747
 
    }
79748
 
    *pnAlloc = sqlite3DbMallocSize(db, pNew)/szEntry;
79749
 
    pArray = pNew;
79750
 
  }
79751
 
  z = (char*)pArray;
79752
 
  memset(&z[*pnEntry * szEntry], 0, szEntry);
79753
 
  *pIdx = *pnEntry;
79754
 
  ++*pnEntry;
79755
 
  return pArray;
79756
 
}
79757
 
 
79758
 
/*
79759
 
** Append a new element to the given IdList.  Create a new IdList if
79760
 
** need be.
79761
 
**
79762
 
** A new IdList is returned, or NULL if malloc() fails.
79763
 
*/
79764
 
SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
79765
 
  int i;
79766
 
  if( pList==0 ){
79767
 
    pList = sqlite3DbMallocZero(db, sizeof(IdList) );
79768
 
    if( pList==0 ) return 0;
79769
 
    pList->nAlloc = 0;
79770
 
  }
79771
 
  pList->a = sqlite3ArrayAllocate(
79772
 
      db,
79773
 
      pList->a,
79774
 
      sizeof(pList->a[0]),
79775
 
      5,
79776
 
      &pList->nId,
79777
 
      &pList->nAlloc,
79778
 
      &i
79779
 
  );
79780
 
  if( i<0 ){
79781
 
    sqlite3IdListDelete(db, pList);
79782
 
    return 0;
79783
 
  }
79784
 
  pList->a[i].zName = sqlite3NameFromToken(db, pToken);
79785
 
  return pList;
79786
 
}
79787
 
 
79788
 
/*
79789
 
** Delete an IdList.
79790
 
*/
79791
 
SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
79792
 
  int i;
79793
 
  if( pList==0 ) return;
79794
 
  for(i=0; i<pList->nId; i++){
79795
 
    sqlite3DbFree(db, pList->a[i].zName);
79796
 
  }
79797
 
  sqlite3DbFree(db, pList->a);
79798
 
  sqlite3DbFree(db, pList);
79799
 
}
79800
 
 
79801
 
/*
79802
 
** Return the index in pList of the identifier named zId.  Return -1
79803
 
** if not found.
79804
 
*/
79805
 
SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
79806
 
  int i;
79807
 
  if( pList==0 ) return -1;
79808
 
  for(i=0; i<pList->nId; i++){
79809
 
    if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
79810
 
  }
79811
 
  return -1;
79812
 
}
79813
 
 
79814
 
/*
79815
 
** Expand the space allocated for the given SrcList object by
79816
 
** creating nExtra new slots beginning at iStart.  iStart is zero based.
79817
 
** New slots are zeroed.
79818
 
**
79819
 
** For example, suppose a SrcList initially contains two entries: A,B.
79820
 
** To append 3 new entries onto the end, do this:
79821
 
**
79822
 
**    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
79823
 
**
79824
 
** After the call above it would contain:  A, B, nil, nil, nil.
79825
 
** If the iStart argument had been 1 instead of 2, then the result
79826
 
** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
79827
 
** the iStart value would be 0.  The result then would
79828
 
** be: nil, nil, nil, A, B.
79829
 
**
79830
 
** If a memory allocation fails the SrcList is unchanged.  The
79831
 
** db->mallocFailed flag will be set to true.
79832
 
*/
79833
 
SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
79834
 
  sqlite3 *db,       /* Database connection to notify of OOM errors */
79835
 
  SrcList *pSrc,     /* The SrcList to be enlarged */
79836
 
  int nExtra,        /* Number of new slots to add to pSrc->a[] */
79837
 
  int iStart         /* Index in pSrc->a[] of first new slot */
79838
 
){
79839
 
  int i;
79840
 
 
79841
 
  /* Sanity checking on calling parameters */
79842
 
  assert( iStart>=0 );
79843
 
  assert( nExtra>=1 );
79844
 
  assert( pSrc!=0 );
79845
 
  assert( iStart<=pSrc->nSrc );
79846
 
 
79847
 
  /* Allocate additional space if needed */
79848
 
  if( pSrc->nSrc+nExtra>pSrc->nAlloc ){
79849
 
    SrcList *pNew;
79850
 
    int nAlloc = pSrc->nSrc+nExtra;
79851
 
    int nGot;
79852
 
    pNew = sqlite3DbRealloc(db, pSrc,
79853
 
               sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
79854
 
    if( pNew==0 ){
79855
 
      assert( db->mallocFailed );
79856
 
      return pSrc;
79857
 
    }
79858
 
    pSrc = pNew;
79859
 
    nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
79860
 
    pSrc->nAlloc = (u16)nGot;
79861
 
  }
79862
 
 
79863
 
  /* Move existing slots that come after the newly inserted slots
79864
 
  ** out of the way */
79865
 
  for(i=pSrc->nSrc-1; i>=iStart; i--){
79866
 
    pSrc->a[i+nExtra] = pSrc->a[i];
79867
 
  }
79868
 
  pSrc->nSrc += (i16)nExtra;
79869
 
 
79870
 
  /* Zero the newly allocated slots */
79871
 
  memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
79872
 
  for(i=iStart; i<iStart+nExtra; i++){
79873
 
    pSrc->a[i].iCursor = -1;
79874
 
  }
79875
 
 
79876
 
  /* Return a pointer to the enlarged SrcList */
79877
 
  return pSrc;
79878
 
}
79879
 
 
79880
 
 
79881
 
/*
79882
 
** Append a new table name to the given SrcList.  Create a new SrcList if
79883
 
** need be.  A new entry is created in the SrcList even if pTable is NULL.
79884
 
**
79885
 
** A SrcList is returned, or NULL if there is an OOM error.  The returned
79886
 
** SrcList might be the same as the SrcList that was input or it might be
79887
 
** a new one.  If an OOM error does occurs, then the prior value of pList
79888
 
** that is input to this routine is automatically freed.
79889
 
**
79890
 
** If pDatabase is not null, it means that the table has an optional
79891
 
** database name prefix.  Like this:  "database.table".  The pDatabase
79892
 
** points to the table name and the pTable points to the database name.
79893
 
** The SrcList.a[].zName field is filled with the table name which might
79894
 
** come from pTable (if pDatabase is NULL) or from pDatabase.  
79895
 
** SrcList.a[].zDatabase is filled with the database name from pTable,
79896
 
** or with NULL if no database is specified.
79897
 
**
79898
 
** In other words, if call like this:
79899
 
**
79900
 
**         sqlite3SrcListAppend(D,A,B,0);
79901
 
**
79902
 
** Then B is a table name and the database name is unspecified.  If called
79903
 
** like this:
79904
 
**
79905
 
**         sqlite3SrcListAppend(D,A,B,C);
79906
 
**
79907
 
** Then C is the table name and B is the database name.  If C is defined
79908
 
** then so is B.  In other words, we never have a case where:
79909
 
**
79910
 
**         sqlite3SrcListAppend(D,A,0,C);
79911
 
**
79912
 
** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
79913
 
** before being added to the SrcList.
79914
 
*/
79915
 
SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
79916
 
  sqlite3 *db,        /* Connection to notify of malloc failures */
79917
 
  SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
79918
 
  Token *pTable,      /* Table to append */
79919
 
  Token *pDatabase    /* Database of the table */
79920
 
){
79921
 
  struct SrcList_item *pItem;
79922
 
  assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
79923
 
  if( pList==0 ){
79924
 
    pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
79925
 
    if( pList==0 ) return 0;
79926
 
    pList->nAlloc = 1;
79927
 
  }
79928
 
  pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
79929
 
  if( db->mallocFailed ){
79930
 
    sqlite3SrcListDelete(db, pList);
79931
 
    return 0;
79932
 
  }
79933
 
  pItem = &pList->a[pList->nSrc-1];
79934
 
  if( pDatabase && pDatabase->z==0 ){
79935
 
    pDatabase = 0;
79936
 
  }
79937
 
  if( pDatabase ){
79938
 
    Token *pTemp = pDatabase;
79939
 
    pDatabase = pTable;
79940
 
    pTable = pTemp;
79941
 
  }
79942
 
  pItem->zName = sqlite3NameFromToken(db, pTable);
79943
 
  pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
79944
 
  return pList;
79945
 
}
79946
 
 
79947
 
/*
79948
 
** Assign VdbeCursor index numbers to all tables in a SrcList
79949
 
*/
79950
 
SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
79951
 
  int i;
79952
 
  struct SrcList_item *pItem;
79953
 
  assert(pList || pParse->db->mallocFailed );
79954
 
  if( pList ){
79955
 
    for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
79956
 
      if( pItem->iCursor>=0 ) break;
79957
 
      pItem->iCursor = pParse->nTab++;
79958
 
      if( pItem->pSelect ){
79959
 
        sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
79960
 
      }
79961
 
    }
79962
 
  }
79963
 
}
79964
 
 
79965
 
/*
79966
 
** Delete an entire SrcList including all its substructure.
79967
 
*/
79968
 
SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
79969
 
  int i;
79970
 
  struct SrcList_item *pItem;
79971
 
  if( pList==0 ) return;
79972
 
  for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
79973
 
    sqlite3DbFree(db, pItem->zDatabase);
79974
 
    sqlite3DbFree(db, pItem->zName);
79975
 
    sqlite3DbFree(db, pItem->zAlias);
79976
 
    sqlite3DbFree(db, pItem->zIndex);
79977
 
    sqlite3DeleteTable(db, pItem->pTab);
79978
 
    sqlite3SelectDelete(db, pItem->pSelect);
79979
 
    sqlite3ExprDelete(db, pItem->pOn);
79980
 
    sqlite3IdListDelete(db, pItem->pUsing);
79981
 
  }
79982
 
  sqlite3DbFree(db, pList);
79983
 
}
79984
 
 
79985
 
/*
79986
 
** This routine is called by the parser to add a new term to the
79987
 
** end of a growing FROM clause.  The "p" parameter is the part of
79988
 
** the FROM clause that has already been constructed.  "p" is NULL
79989
 
** if this is the first term of the FROM clause.  pTable and pDatabase
79990
 
** are the name of the table and database named in the FROM clause term.
79991
 
** pDatabase is NULL if the database name qualifier is missing - the
79992
 
** usual case.  If the term has a alias, then pAlias points to the
79993
 
** alias token.  If the term is a subquery, then pSubquery is the
79994
 
** SELECT statement that the subquery encodes.  The pTable and
79995
 
** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
79996
 
** parameters are the content of the ON and USING clauses.
79997
 
**
79998
 
** Return a new SrcList which encodes is the FROM with the new
79999
 
** term added.
80000
 
*/
80001
 
SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
80002
 
  Parse *pParse,          /* Parsing context */
80003
 
  SrcList *p,             /* The left part of the FROM clause already seen */
80004
 
  Token *pTable,          /* Name of the table to add to the FROM clause */
80005
 
  Token *pDatabase,       /* Name of the database containing pTable */
80006
 
  Token *pAlias,          /* The right-hand side of the AS subexpression */
80007
 
  Select *pSubquery,      /* A subquery used in place of a table name */
80008
 
  Expr *pOn,              /* The ON clause of a join */
80009
 
  IdList *pUsing          /* The USING clause of a join */
80010
 
){
80011
 
  struct SrcList_item *pItem;
80012
 
  sqlite3 *db = pParse->db;
80013
 
  if( !p && (pOn || pUsing) ){
80014
 
    sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", 
80015
 
      (pOn ? "ON" : "USING")
80016
 
    );
80017
 
    goto append_from_error;
80018
 
  }
80019
 
  p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
80020
 
  if( p==0 || NEVER(p->nSrc==0) ){
80021
 
    goto append_from_error;
80022
 
  }
80023
 
  pItem = &p->a[p->nSrc-1];
80024
 
  assert( pAlias!=0 );
80025
 
  if( pAlias->n ){
80026
 
    pItem->zAlias = sqlite3NameFromToken(db, pAlias);
80027
 
  }
80028
 
  pItem->pSelect = pSubquery;
80029
 
  pItem->pOn = pOn;
80030
 
  pItem->pUsing = pUsing;
80031
 
  return p;
80032
 
 
80033
 
 append_from_error:
80034
 
  assert( p==0 );
80035
 
  sqlite3ExprDelete(db, pOn);
80036
 
  sqlite3IdListDelete(db, pUsing);
80037
 
  sqlite3SelectDelete(db, pSubquery);
80038
 
  return 0;
80039
 
}
80040
 
 
80041
 
/*
80042
 
** Add an INDEXED BY or NOT INDEXED clause to the most recently added 
80043
 
** element of the source-list passed as the second argument.
80044
 
*/
80045
 
SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
80046
 
  assert( pIndexedBy!=0 );
80047
 
  if( p && ALWAYS(p->nSrc>0) ){
80048
 
    struct SrcList_item *pItem = &p->a[p->nSrc-1];
80049
 
    assert( pItem->notIndexed==0 && pItem->zIndex==0 );
80050
 
    if( pIndexedBy->n==1 && !pIndexedBy->z ){
80051
 
      /* A "NOT INDEXED" clause was supplied. See parse.y 
80052
 
      ** construct "indexed_opt" for details. */
80053
 
      pItem->notIndexed = 1;
80054
 
    }else{
80055
 
      pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
80056
 
    }
80057
 
  }
80058
 
}
80059
 
 
80060
 
/*
80061
 
** When building up a FROM clause in the parser, the join operator
80062
 
** is initially attached to the left operand.  But the code generator
80063
 
** expects the join operator to be on the right operand.  This routine
80064
 
** Shifts all join operators from left to right for an entire FROM
80065
 
** clause.
80066
 
**
80067
 
** Example: Suppose the join is like this:
80068
 
**
80069
 
**           A natural cross join B
80070
 
**
80071
 
** The operator is "natural cross join".  The A and B operands are stored
80072
 
** in p->a[0] and p->a[1], respectively.  The parser initially stores the
80073
 
** operator with A.  This routine shifts that operator over to B.
80074
 
*/
80075
 
SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
80076
 
  if( p && p->a ){
80077
 
    int i;
80078
 
    for(i=p->nSrc-1; i>0; i--){
80079
 
      p->a[i].jointype = p->a[i-1].jointype;
80080
 
    }
80081
 
    p->a[0].jointype = 0;
80082
 
  }
80083
 
}
80084
 
 
80085
 
/*
80086
 
** Begin a transaction
80087
 
*/
80088
 
SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
80089
 
  sqlite3 *db;
80090
 
  Vdbe *v;
80091
 
  int i;
80092
 
 
80093
 
  assert( pParse!=0 );
80094
 
  db = pParse->db;
80095
 
  assert( db!=0 );
80096
 
/*  if( db->aDb[0].pBt==0 ) return; */
80097
 
  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
80098
 
    return;
80099
 
  }
80100
 
  v = sqlite3GetVdbe(pParse);
80101
 
  if( !v ) return;
80102
 
  if( type!=TK_DEFERRED ){
80103
 
    for(i=0; i<db->nDb; i++){
80104
 
      sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
80105
 
      sqlite3VdbeUsesBtree(v, i);
80106
 
    }
80107
 
  }
80108
 
  sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
80109
 
}
80110
 
 
80111
 
/*
80112
 
** Commit a transaction
80113
 
*/
80114
 
SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
80115
 
  sqlite3 *db;
80116
 
  Vdbe *v;
80117
 
 
80118
 
  assert( pParse!=0 );
80119
 
  db = pParse->db;
80120
 
  assert( db!=0 );
80121
 
/*  if( db->aDb[0].pBt==0 ) return; */
80122
 
  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
80123
 
    return;
80124
 
  }
80125
 
  v = sqlite3GetVdbe(pParse);
80126
 
  if( v ){
80127
 
    sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
80128
 
  }
80129
 
}
80130
 
 
80131
 
/*
80132
 
** Rollback a transaction
80133
 
*/
80134
 
SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
80135
 
  sqlite3 *db;
80136
 
  Vdbe *v;
80137
 
 
80138
 
  assert( pParse!=0 );
80139
 
  db = pParse->db;
80140
 
  assert( db!=0 );
80141
 
/*  if( db->aDb[0].pBt==0 ) return; */
80142
 
  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
80143
 
    return;
80144
 
  }
80145
 
  v = sqlite3GetVdbe(pParse);
80146
 
  if( v ){
80147
 
    sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
80148
 
  }
80149
 
}
80150
 
 
80151
 
/*
80152
 
** This function is called by the parser when it parses a command to create,
80153
 
** release or rollback an SQL savepoint. 
80154
 
*/
80155
 
SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
80156
 
  char *zName = sqlite3NameFromToken(pParse->db, pName);
80157
 
  if( zName ){
80158
 
    Vdbe *v = sqlite3GetVdbe(pParse);
80159
 
#ifndef SQLITE_OMIT_AUTHORIZATION
80160
 
    static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
80161
 
    assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
80162
 
#endif
80163
 
    if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
80164
 
      sqlite3DbFree(pParse->db, zName);
80165
 
      return;
80166
 
    }
80167
 
    sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
80168
 
  }
80169
 
}
80170
 
 
80171
 
/*
80172
 
** Make sure the TEMP database is open and available for use.  Return
80173
 
** the number of errors.  Leave any error messages in the pParse structure.
80174
 
*/
80175
 
SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
80176
 
  sqlite3 *db = pParse->db;
80177
 
  if( db->aDb[1].pBt==0 && !pParse->explain ){
80178
 
    int rc;
80179
 
    Btree *pBt;
80180
 
    static const int flags = 
80181
 
          SQLITE_OPEN_READWRITE |
80182
 
          SQLITE_OPEN_CREATE |
80183
 
          SQLITE_OPEN_EXCLUSIVE |
80184
 
          SQLITE_OPEN_DELETEONCLOSE |
80185
 
          SQLITE_OPEN_TEMP_DB;
80186
 
 
80187
 
    rc = sqlite3BtreeOpen(0, db, &pBt, 0, flags);
80188
 
    if( rc!=SQLITE_OK ){
80189
 
      sqlite3ErrorMsg(pParse, "unable to open a temporary database "
80190
 
        "file for storing temporary tables");
80191
 
      pParse->rc = rc;
80192
 
      return 1;
80193
 
    }
80194
 
    db->aDb[1].pBt = pBt;
80195
 
    assert( db->aDb[1].pSchema );
80196
 
    if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
80197
 
      db->mallocFailed = 1;
80198
 
      return 1;
80199
 
    }
80200
 
  }
80201
 
  return 0;
80202
 
}
80203
 
 
80204
 
/*
80205
 
** Generate VDBE code that will verify the schema cookie and start
80206
 
** a read-transaction for all named database files.
80207
 
**
80208
 
** It is important that all schema cookies be verified and all
80209
 
** read transactions be started before anything else happens in
80210
 
** the VDBE program.  But this routine can be called after much other
80211
 
** code has been generated.  So here is what we do:
80212
 
**
80213
 
** The first time this routine is called, we code an OP_Goto that
80214
 
** will jump to a subroutine at the end of the program.  Then we
80215
 
** record every database that needs its schema verified in the
80216
 
** pParse->cookieMask field.  Later, after all other code has been
80217
 
** generated, the subroutine that does the cookie verifications and
80218
 
** starts the transactions will be coded and the OP_Goto P2 value
80219
 
** will be made to point to that subroutine.  The generation of the
80220
 
** cookie verification subroutine code happens in sqlite3FinishCoding().
80221
 
**
80222
 
** If iDb<0 then code the OP_Goto only - don't set flag to verify the
80223
 
** schema on any databases.  This can be used to position the OP_Goto
80224
 
** early in the code, before we know if any database tables will be used.
80225
 
*/
80226
 
SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
80227
 
  Parse *pToplevel = sqlite3ParseToplevel(pParse);
80228
 
 
80229
 
  if( pToplevel->cookieGoto==0 ){
80230
 
    Vdbe *v = sqlite3GetVdbe(pToplevel);
80231
 
    if( v==0 ) return;  /* This only happens if there was a prior error */
80232
 
    pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
80233
 
  }
80234
 
  if( iDb>=0 ){
80235
 
    sqlite3 *db = pToplevel->db;
80236
 
    yDbMask mask;
80237
 
 
80238
 
    assert( iDb<db->nDb );
80239
 
    assert( db->aDb[iDb].pBt!=0 || iDb==1 );
80240
 
    assert( iDb<SQLITE_MAX_ATTACHED+2 );
80241
 
    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
80242
 
    mask = ((yDbMask)1)<<iDb;
80243
 
    if( (pToplevel->cookieMask & mask)==0 ){
80244
 
      pToplevel->cookieMask |= mask;
80245
 
      pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
80246
 
      #if (!OMIT_TEMPDB)
80247
 
      if( iDb==1 ){
80248
 
        sqlite3OpenTempDatabase(pToplevel);
80249
 
      }
80250
 
                #endif
80251
 
    }
80252
 
  }
80253
 
}
80254
 
 
80255
 
/*
80256
 
** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each 
80257
 
** attached database. Otherwise, invoke it for the database named zDb only.
80258
 
*/
80259
 
SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
80260
 
  sqlite3 *db = pParse->db;
80261
 
  int i;
80262
 
  for(i=0; i<db->nDb; i++){
80263
 
    Db *pDb = &db->aDb[i];
80264
 
    if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
80265
 
      sqlite3CodeVerifySchema(pParse, i);
80266
 
    }
80267
 
  }
80268
 
}
80269
 
 
80270
 
/*
80271
 
** Generate VDBE code that prepares for doing an operation that
80272
 
** might change the database.
80273
 
**
80274
 
** This routine starts a new transaction if we are not already within
80275
 
** a transaction.  If we are already within a transaction, then a checkpoint
80276
 
** is set if the setStatement parameter is true.  A checkpoint should
80277
 
** be set for operations that might fail (due to a constraint) part of
80278
 
** the way through and which will need to undo some writes without having to
80279
 
** rollback the whole transaction.  For operations where all constraints
80280
 
** can be checked before any changes are made to the database, it is never
80281
 
** necessary to undo a write and the checkpoint should not be set.
80282
 
*/
80283
 
SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
80284
 
  Parse *pToplevel = sqlite3ParseToplevel(pParse);
80285
 
  sqlite3CodeVerifySchema(pParse, iDb);
80286
 
  pToplevel->writeMask |= ((yDbMask)1)<<iDb;
80287
 
  pToplevel->isMultiWrite |= setStatement;
80288
 
}
80289
 
 
80290
 
/*
80291
 
** Indicate that the statement currently under construction might write
80292
 
** more than one entry (example: deleting one row then inserting another,
80293
 
** inserting multiple rows in a table, or inserting a row and index entries.)
80294
 
** If an abort occurs after some of these writes have completed, then it will
80295
 
** be necessary to undo the completed writes.
80296
 
*/
80297
 
SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
80298
 
  Parse *pToplevel = sqlite3ParseToplevel(pParse);
80299
 
  pToplevel->isMultiWrite = 1;
80300
 
}
80301
 
 
80302
 
/* 
80303
 
** The code generator calls this routine if is discovers that it is
80304
 
** possible to abort a statement prior to completion.  In order to 
80305
 
** perform this abort without corrupting the database, we need to make
80306
 
** sure that the statement is protected by a statement transaction.
80307
 
**
80308
 
** Technically, we only need to set the mayAbort flag if the
80309
 
** isMultiWrite flag was previously set.  There is a time dependency
80310
 
** such that the abort must occur after the multiwrite.  This makes
80311
 
** some statements involving the REPLACE conflict resolution algorithm
80312
 
** go a little faster.  But taking advantage of this time dependency
80313
 
** makes it more difficult to prove that the code is correct (in 
80314
 
** particular, it prevents us from writing an effective
80315
 
** implementation of sqlite3AssertMayAbort()) and so we have chosen
80316
 
** to take the safe route and skip the optimization.
80317
 
*/
80318
 
SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
80319
 
  Parse *pToplevel = sqlite3ParseToplevel(pParse);
80320
 
  pToplevel->mayAbort = 1;
80321
 
}
80322
 
 
80323
 
/*
80324
 
** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
80325
 
** error. The onError parameter determines which (if any) of the statement
80326
 
** and/or current transaction is rolled back.
80327
 
*/
80328
 
SQLITE_PRIVATE void sqlite3HaltConstraint(Parse *pParse, int onError, char *p4, int p4type){
80329
 
  Vdbe *v = sqlite3GetVdbe(pParse);
80330
 
  if( onError==OE_Abort ){
80331
 
    sqlite3MayAbort(pParse);
80332
 
  }
80333
 
  sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, p4, p4type);
80334
 
}
80335
 
 
80336
 
/*
80337
 
** Check to see if pIndex uses the collating sequence pColl.  Return
80338
 
** true if it does and false if it does not.
80339
 
*/
80340
 
#ifndef SQLITE_OMIT_REINDEX
80341
 
static int collationMatch(const char *zColl, Index *pIndex){
80342
 
  int i;
80343
 
  assert( zColl!=0 );
80344
 
  for(i=0; i<pIndex->nColumn; i++){
80345
 
    const char *z = pIndex->azColl[i];
80346
 
    assert( z!=0 );
80347
 
    if( 0==sqlite3StrICmp(z, zColl) ){
80348
 
      return 1;
80349
 
    }
80350
 
  }
80351
 
  return 0;
80352
 
}
80353
 
#endif
80354
 
 
80355
 
/*
80356
 
** Recompute all indices of pTab that use the collating sequence pColl.
80357
 
** If pColl==0 then recompute all indices of pTab.
80358
 
*/
80359
 
#ifndef SQLITE_OMIT_REINDEX
80360
 
static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
80361
 
  Index *pIndex;              /* An index associated with pTab */
80362
 
 
80363
 
  for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
80364
 
    if( zColl==0 || collationMatch(zColl, pIndex) ){
80365
 
      int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
80366
 
      sqlite3BeginWriteOperation(pParse, 0, iDb);
80367
 
      sqlite3RefillIndex(pParse, pIndex, -1);
80368
 
    }
80369
 
  }
80370
 
}
80371
 
#endif
80372
 
 
80373
 
/*
80374
 
** Recompute all indices of all tables in all databases where the
80375
 
** indices use the collating sequence pColl.  If pColl==0 then recompute
80376
 
** all indices everywhere.
80377
 
*/
80378
 
#ifndef SQLITE_OMIT_REINDEX
80379
 
static void reindexDatabases(Parse *pParse, char const *zColl){
80380
 
  Db *pDb;                    /* A single database */
80381
 
  int iDb;                    /* The database index number */
80382
 
  sqlite3 *db = pParse->db;   /* The database connection */
80383
 
  HashElem *k;                /* For looping over tables in pDb */
80384
 
  Table *pTab;                /* A table in the database */
80385
 
 
80386
 
  assert( sqlite3BtreeHoldsAllMutexes(db) );  /* Needed for schema access */
80387
 
  for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
80388
 
    assert( pDb!=0 );
80389
 
    for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
80390
 
      pTab = (Table*)sqliteHashData(k);
80391
 
      reindexTable(pParse, pTab, zColl);
80392
 
    }
80393
 
  }
80394
 
}
80395
 
#endif
80396
 
 
80397
 
/*
80398
 
** Generate code for the REINDEX command.
80399
 
**
80400
 
**        REINDEX                            -- 1
80401
 
**        REINDEX  <collation>               -- 2
80402
 
**        REINDEX  ?<database>.?<tablename>  -- 3
80403
 
**        REINDEX  ?<database>.?<indexname>  -- 4
80404
 
**
80405
 
** Form 1 causes all indices in all attached databases to be rebuilt.
80406
 
** Form 2 rebuilds all indices in all databases that use the named
80407
 
** collating function.  Forms 3 and 4 rebuild the named index or all
80408
 
** indices associated with the named table.
80409
 
*/
80410
 
#ifndef SQLITE_OMIT_REINDEX
80411
 
SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
80412
 
  CollSeq *pColl;             /* Collating sequence to be reindexed, or NULL */
80413
 
  char *z;                    /* Name of a table or index */
80414
 
  const char *zDb;            /* Name of the database */
80415
 
  Table *pTab;                /* A table in the database */
80416
 
  Index *pIndex;              /* An index associated with pTab */
80417
 
  int iDb;                    /* The database index number */
80418
 
  sqlite3 *db = pParse->db;   /* The database connection */
80419
 
  Token *pObjName;            /* Name of the table or index to be reindexed */
80420
 
 
80421
 
  /* Read the database schema. If an error occurs, leave an error message
80422
 
  ** and code in pParse and return NULL. */
80423
 
  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
80424
 
    return;
80425
 
  }
80426
 
 
80427
 
  if( pName1==0 ){
80428
 
    reindexDatabases(pParse, 0);
80429
 
    return;
80430
 
  }else if( NEVER(pName2==0) || pName2->z==0 ){
80431
 
    char *zColl;
80432
 
    assert( pName1->z );
80433
 
    zColl = sqlite3NameFromToken(pParse->db, pName1);
80434
 
    if( !zColl ) return;
80435
 
    pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
80436
 
    if( pColl ){
80437
 
      reindexDatabases(pParse, zColl);
80438
 
      sqlite3DbFree(db, zColl);
80439
 
      return;
80440
 
    }
80441
 
    sqlite3DbFree(db, zColl);
80442
 
  }
80443
 
  iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
80444
 
  if( iDb<0 ) return;
80445
 
  z = sqlite3NameFromToken(db, pObjName);
80446
 
  if( z==0 ) return;
80447
 
  zDb = db->aDb[iDb].zName;
80448
 
  pTab = sqlite3FindTable(db, z, zDb);
80449
 
  if( pTab ){
80450
 
    reindexTable(pParse, pTab, 0);
80451
 
    sqlite3DbFree(db, z);
80452
 
    return;
80453
 
  }
80454
 
  pIndex = sqlite3FindIndex(db, z, zDb);
80455
 
  sqlite3DbFree(db, z);
80456
 
  if( pIndex ){
80457
 
    sqlite3BeginWriteOperation(pParse, 0, iDb);
80458
 
    sqlite3RefillIndex(pParse, pIndex, -1);
80459
 
    return;
80460
 
  }
80461
 
  sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
80462
 
}
80463
 
#endif
80464
 
 
80465
 
/*
80466
 
** Return a dynamicly allocated KeyInfo structure that can be used
80467
 
** with OP_OpenRead or OP_OpenWrite to access database index pIdx.
80468
 
**
80469
 
** If successful, a pointer to the new structure is returned. In this case
80470
 
** the caller is responsible for calling sqlite3DbFree(db, ) on the returned 
80471
 
** pointer. If an error occurs (out of memory or missing collation 
80472
 
** sequence), NULL is returned and the state of pParse updated to reflect
80473
 
** the error.
80474
 
*/
80475
 
SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){
80476
 
  int i;
80477
 
  int nCol = pIdx->nColumn;
80478
 
  int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol;
80479
 
  sqlite3 *db = pParse->db;
80480
 
  KeyInfo *pKey = (KeyInfo *)sqlite3DbMallocZero(db, nBytes);
80481
 
 
80482
 
  if( pKey ){
80483
 
    pKey->db = pParse->db;
80484
 
    pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]);
80485
 
    assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) );
80486
 
    for(i=0; i<nCol; i++){
80487
 
      char *zColl = pIdx->azColl[i];
80488
 
      assert( zColl );
80489
 
      pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl);
80490
 
      pKey->aSortOrder[i] = pIdx->aSortOrder[i];
80491
 
    }
80492
 
    pKey->nField = (u16)nCol;
80493
 
  }
80494
 
 
80495
 
  if( pParse->nErr ){
80496
 
    sqlite3DbFree(db, pKey);
80497
 
    pKey = 0;
80498
 
  }
80499
 
  return pKey;
80500
 
}
80501
 
 
80502
 
/************** End of build.c ***********************************************/
80503
 
/************** Begin file callback.c ****************************************/
80504
 
/*
80505
 
** 2005 May 23 
80506
 
**
80507
 
** The author disclaims copyright to this source code.  In place of
80508
 
** a legal notice, here is a blessing:
80509
 
**
80510
 
**    May you do good and not evil.
80511
 
**    May you find forgiveness for yourself and forgive others.
80512
 
**    May you share freely, never taking more than you give.
80513
 
**
80514
 
*************************************************************************
80515
 
**
80516
 
** This file contains functions used to access the internal hash tables
80517
 
** of user defined functions and collation sequences.
80518
 
*/
80519
 
 
80520
 
 
80521
 
/*
80522
 
** Invoke the 'collation needed' callback to request a collation sequence
80523
 
** in the encoding enc of name zName, length nName.
80524
 
*/
80525
 
static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
80526
 
  assert( !db->xCollNeeded || !db->xCollNeeded16 );
80527
 
  if( db->xCollNeeded ){
80528
 
    char *zExternal = sqlite3DbStrDup(db, zName);
80529
 
    if( !zExternal ) return;
80530
 
    db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
80531
 
    sqlite3DbFree(db, zExternal);
80532
 
  }
80533
 
#ifndef SQLITE_OMIT_UTF16
80534
 
  if( db->xCollNeeded16 ){
80535
 
    char const *zExternal;
80536
 
    sqlite3_value *pTmp = sqlite3ValueNew(db);
80537
 
    sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
80538
 
    zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
80539
 
    if( zExternal ){
80540
 
      db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
80541
 
    }
80542
 
    sqlite3ValueFree(pTmp);
80543
 
  }
80544
 
#endif
80545
 
}
80546
 
 
80547
 
/*
80548
 
** This routine is called if the collation factory fails to deliver a
80549
 
** collation function in the best encoding but there may be other versions
80550
 
** of this collation function (for other text encodings) available. Use one
80551
 
** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
80552
 
** possible.
80553
 
*/
80554
 
static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
80555
 
  CollSeq *pColl2;
80556
 
  char *z = pColl->zName;
80557
 
  int i;
80558
 
  static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
80559
 
  for(i=0; i<3; i++){
80560
 
    pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
80561
 
    if( pColl2->xCmp!=0 ){
80562
 
      memcpy(pColl, pColl2, sizeof(CollSeq));
80563
 
      pColl->xDel = 0;         /* Do not copy the destructor */
80564
 
      return SQLITE_OK;
80565
 
    }
80566
 
  }
80567
 
  return SQLITE_ERROR;
80568
 
}
80569
 
 
80570
 
/*
80571
 
** This function is responsible for invoking the collation factory callback
80572
 
** or substituting a collation sequence of a different encoding when the
80573
 
** requested collation sequence is not available in the desired encoding.
80574
 
** 
80575
 
** If it is not NULL, then pColl must point to the database native encoding 
80576
 
** collation sequence with name zName, length nName.
80577
 
**
80578
 
** The return value is either the collation sequence to be used in database
80579
 
** db for collation type name zName, length nName, or NULL, if no collation
80580
 
** sequence can be found.
80581
 
**
80582
 
** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
80583
 
*/
80584
 
SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
80585
 
  sqlite3* db,          /* The database connection */
80586
 
  u8 enc,               /* The desired encoding for the collating sequence */
80587
 
  CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
80588
 
  const char *zName     /* Collating sequence name */
80589
 
){
80590
 
  CollSeq *p;
80591
 
 
80592
 
  p = pColl;
80593
 
  if( !p ){
80594
 
    p = sqlite3FindCollSeq(db, enc, zName, 0);
80595
 
  }
80596
 
  if( !p || !p->xCmp ){
80597
 
    /* No collation sequence of this type for this encoding is registered.
80598
 
    ** Call the collation factory to see if it can supply us with one.
80599
 
    */
80600
 
    callCollNeeded(db, enc, zName);
80601
 
    p = sqlite3FindCollSeq(db, enc, zName, 0);
80602
 
  }
80603
 
  if( p && !p->xCmp && synthCollSeq(db, p) ){
80604
 
    p = 0;
80605
 
  }
80606
 
  assert( !p || p->xCmp );
80607
 
  return p;
80608
 
}
80609
 
 
80610
 
/*
80611
 
** This routine is called on a collation sequence before it is used to
80612
 
** check that it is defined. An undefined collation sequence exists when
80613
 
** a database is loaded that contains references to collation sequences
80614
 
** that have not been defined by sqlite3_create_collation() etc.
80615
 
**
80616
 
** If required, this routine calls the 'collation needed' callback to
80617
 
** request a definition of the collating sequence. If this doesn't work, 
80618
 
** an equivalent collating sequence that uses a text encoding different
80619
 
** from the main database is substituted, if one is available.
80620
 
*/
80621
 
SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
80622
 
  if( pColl ){
80623
 
    const char *zName = pColl->zName;
80624
 
    sqlite3 *db = pParse->db;
80625
 
    CollSeq *p = sqlite3GetCollSeq(db, ENC(db), pColl, zName);
80626
 
    if( !p ){
80627
 
      sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
80628
 
      pParse->nErr++;
80629
 
      return SQLITE_ERROR;
80630
 
    }
80631
 
    assert( p==pColl );
80632
 
  }
80633
 
  return SQLITE_OK;
80634
 
}
80635
 
 
80636
 
 
80637
 
 
80638
 
/*
80639
 
** Locate and return an entry from the db.aCollSeq hash table. If the entry
80640
 
** specified by zName and nName is not found and parameter 'create' is
80641
 
** true, then create a new entry. Otherwise return NULL.
80642
 
**
80643
 
** Each pointer stored in the sqlite3.aCollSeq hash table contains an
80644
 
** array of three CollSeq structures. The first is the collation sequence
80645
 
** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
80646
 
**
80647
 
** Stored immediately after the three collation sequences is a copy of
80648
 
** the collation sequence name. A pointer to this string is stored in
80649
 
** each collation sequence structure.
80650
 
*/
80651
 
static CollSeq *findCollSeqEntry(
80652
 
  sqlite3 *db,          /* Database connection */
80653
 
  const char *zName,    /* Name of the collating sequence */
80654
 
  int create            /* Create a new entry if true */
80655
 
){
80656
 
  CollSeq *pColl;
80657
 
  int nName = sqlite3Strlen30(zName);
80658
 
  pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
80659
 
 
80660
 
  if( 0==pColl && create ){
80661
 
    pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
80662
 
    if( pColl ){
80663
 
      CollSeq *pDel = 0;
80664
 
      pColl[0].zName = (char*)&pColl[3];
80665
 
      pColl[0].enc = SQLITE_UTF8;
80666
 
      pColl[1].zName = (char*)&pColl[3];
80667
 
      pColl[1].enc = SQLITE_UTF16LE;
80668
 
      pColl[2].zName = (char*)&pColl[3];
80669
 
      pColl[2].enc = SQLITE_UTF16BE;
80670
 
      memcpy(pColl[0].zName, zName, nName);
80671
 
      pColl[0].zName[nName] = 0;
80672
 
      pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
80673
 
 
80674
 
      /* If a malloc() failure occurred in sqlite3HashInsert(), it will 
80675
 
      ** return the pColl pointer to be deleted (because it wasn't added
80676
 
      ** to the hash table).
80677
 
      */
80678
 
      assert( pDel==0 || pDel==pColl );
80679
 
      if( pDel!=0 ){
80680
 
        db->mallocFailed = 1;
80681
 
        sqlite3DbFree(db, pDel);
80682
 
        pColl = 0;
80683
 
      }
80684
 
    }
80685
 
  }
80686
 
  return pColl;
80687
 
}
80688
 
 
80689
 
/*
80690
 
** Parameter zName points to a UTF-8 encoded string nName bytes long.
80691
 
** Return the CollSeq* pointer for the collation sequence named zName
80692
 
** for the encoding 'enc' from the database 'db'.
80693
 
**
80694
 
** If the entry specified is not found and 'create' is true, then create a
80695
 
** new entry.  Otherwise return NULL.
80696
 
**
80697
 
** A separate function sqlite3LocateCollSeq() is a wrapper around
80698
 
** this routine.  sqlite3LocateCollSeq() invokes the collation factory
80699
 
** if necessary and generates an error message if the collating sequence
80700
 
** cannot be found.
80701
 
**
80702
 
** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
80703
 
*/
80704
 
SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
80705
 
  sqlite3 *db,
80706
 
  u8 enc,
80707
 
  const char *zName,
80708
 
  int create
80709
 
){
80710
 
  CollSeq *pColl;
80711
 
  if( zName ){
80712
 
    pColl = findCollSeqEntry(db, zName, create);
80713
 
  }else{
80714
 
    pColl = db->pDfltColl;
80715
 
  }
80716
 
  assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
80717
 
  assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
80718
 
  if( pColl ) pColl += enc-1;
80719
 
  return pColl;
80720
 
}
80721
 
 
80722
 
/* During the search for the best function definition, this procedure
80723
 
** is called to test how well the function passed as the first argument
80724
 
** matches the request for a function with nArg arguments in a system
80725
 
** that uses encoding enc. The value returned indicates how well the
80726
 
** request is matched. A higher value indicates a better match.
80727
 
**
80728
 
** The returned value is always between 0 and 6, as follows:
80729
 
**
80730
 
** 0: Not a match, or if nArg<0 and the function is has no implementation.
80731
 
** 1: A variable arguments function that prefers UTF-8 when a UTF-16
80732
 
**    encoding is requested, or vice versa.
80733
 
** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
80734
 
**    requested, or vice versa.
80735
 
** 3: A variable arguments function using the same text encoding.
80736
 
** 4: A function with the exact number of arguments requested that
80737
 
**    prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
80738
 
** 5: A function with the exact number of arguments requested that
80739
 
**    prefers UTF-16LE when UTF-16BE is requested, or vice versa.
80740
 
** 6: An exact match.
80741
 
**
80742
 
*/
80743
 
static int matchQuality(FuncDef *p, int nArg, u8 enc){
80744
 
  int match = 0;
80745
 
  if( p->nArg==-1 || p->nArg==nArg 
80746
 
   || (nArg==-1 && (p->xFunc!=0 || p->xStep!=0))
80747
 
  ){
80748
 
    match = 1;
80749
 
    if( p->nArg==nArg || nArg==-1 ){
80750
 
      match = 4;
80751
 
    }
80752
 
    if( enc==p->iPrefEnc ){
80753
 
      match += 2;
80754
 
    }
80755
 
    else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
80756
 
             (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
80757
 
      match += 1;
80758
 
    }
80759
 
  }
80760
 
  return match;
80761
 
}
80762
 
 
80763
 
/*
80764
 
** Search a FuncDefHash for a function with the given name.  Return
80765
 
** a pointer to the matching FuncDef if found, or 0 if there is no match.
80766
 
*/
80767
 
static FuncDef *functionSearch(
80768
 
  FuncDefHash *pHash,  /* Hash table to search */
80769
 
  int h,               /* Hash of the name */
80770
 
  const char *zFunc,   /* Name of function */
80771
 
  int nFunc            /* Number of bytes in zFunc */
80772
 
){
80773
 
  FuncDef *p;
80774
 
  for(p=pHash->a[h]; p; p=p->pHash){
80775
 
    if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
80776
 
      return p;
80777
 
    }
80778
 
  }
80779
 
  return 0;
80780
 
}
80781
 
 
80782
 
/*
80783
 
** Insert a new FuncDef into a FuncDefHash hash table.
80784
 
*/
80785
 
SQLITE_PRIVATE void sqlite3FuncDefInsert(
80786
 
  FuncDefHash *pHash,  /* The hash table into which to insert */
80787
 
  FuncDef *pDef        /* The function definition to insert */
80788
 
){
80789
 
  FuncDef *pOther;
80790
 
  int nName = sqlite3Strlen30(pDef->zName);
80791
 
  u8 c1 = (u8)pDef->zName[0];
80792
 
  int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
80793
 
  pOther = functionSearch(pHash, h, pDef->zName, nName);
80794
 
  if( pOther ){
80795
 
    assert( pOther!=pDef && pOther->pNext!=pDef );
80796
 
    pDef->pNext = pOther->pNext;
80797
 
    pOther->pNext = pDef;
80798
 
  }else{
80799
 
    pDef->pNext = 0;
80800
 
    pDef->pHash = pHash->a[h];
80801
 
    pHash->a[h] = pDef;
80802
 
  }
80803
 
}
80804
 
  
80805
 
  
80806
 
 
80807
 
/*
80808
 
** Locate a user function given a name, a number of arguments and a flag
80809
 
** indicating whether the function prefers UTF-16 over UTF-8.  Return a
80810
 
** pointer to the FuncDef structure that defines that function, or return
80811
 
** NULL if the function does not exist.
80812
 
**
80813
 
** If the createFlag argument is true, then a new (blank) FuncDef
80814
 
** structure is created and liked into the "db" structure if a
80815
 
** no matching function previously existed.  When createFlag is true
80816
 
** and the nArg parameter is -1, then only a function that accepts
80817
 
** any number of arguments will be returned.
80818
 
**
80819
 
** If createFlag is false and nArg is -1, then the first valid
80820
 
** function found is returned.  A function is valid if either xFunc
80821
 
** or xStep is non-zero.
80822
 
**
80823
 
** If createFlag is false, then a function with the required name and
80824
 
** number of arguments may be returned even if the eTextRep flag does not
80825
 
** match that requested.
80826
 
*/
80827
 
SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
80828
 
  sqlite3 *db,       /* An open database */
80829
 
  const char *zName, /* Name of the function.  Not null-terminated */
80830
 
  int nName,         /* Number of characters in the name */
80831
 
  int nArg,          /* Number of arguments.  -1 means any number */
80832
 
  u8 enc,            /* Preferred text encoding */
80833
 
  int createFlag     /* Create new entry if true and does not otherwise exist */
80834
 
){
80835
 
  FuncDef *p;         /* Iterator variable */
80836
 
  FuncDef *pBest = 0; /* Best match found so far */
80837
 
  int bestScore = 0;  /* Score of best match */
80838
 
  int h;              /* Hash value */
80839
 
 
80840
 
 
80841
 
  assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
80842
 
  h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
80843
 
 
80844
 
  /* First search for a match amongst the application-defined functions.
80845
 
  */
80846
 
  p = functionSearch(&db->aFunc, h, zName, nName);
80847
 
  while( p ){
80848
 
    int score = matchQuality(p, nArg, enc);
80849
 
    if( score>bestScore ){
80850
 
      pBest = p;
80851
 
      bestScore = score;
80852
 
    }
80853
 
    p = p->pNext;
80854
 
  }
80855
 
 
80856
 
  /* If no match is found, search the built-in functions.
80857
 
  **
80858
 
  ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
80859
 
  ** functions even if a prior app-defined function was found.  And give
80860
 
  ** priority to built-in functions.
80861
 
  **
80862
 
  ** Except, if createFlag is true, that means that we are trying to
80863
 
  ** install a new function.  Whatever FuncDef structure is returned it will
80864
 
  ** have fields overwritten with new information appropriate for the
80865
 
  ** new function.  But the FuncDefs for built-in functions are read-only.
80866
 
  ** So we must not search for built-ins when creating a new function.
80867
 
  */ 
80868
 
  if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
80869
 
    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
80870
 
    bestScore = 0;
80871
 
    p = functionSearch(pHash, h, zName, nName);
80872
 
    while( p ){
80873
 
      int score = matchQuality(p, nArg, enc);
80874
 
      if( score>bestScore ){
80875
 
        pBest = p;
80876
 
        bestScore = score;
80877
 
      }
80878
 
      p = p->pNext;
80879
 
    }
80880
 
  }
80881
 
 
80882
 
  /* If the createFlag parameter is true and the search did not reveal an
80883
 
  ** exact match for the name, number of arguments and encoding, then add a
80884
 
  ** new entry to the hash table and return it.
80885
 
  */
80886
 
  if( createFlag && (bestScore<6 || pBest->nArg!=nArg) && 
80887
 
      (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
80888
 
    pBest->zName = (char *)&pBest[1];
80889
 
    pBest->nArg = (u16)nArg;
80890
 
    pBest->iPrefEnc = enc;
80891
 
    memcpy(pBest->zName, zName, nName);
80892
 
    pBest->zName[nName] = 0;
80893
 
    sqlite3FuncDefInsert(&db->aFunc, pBest);
80894
 
  }
80895
 
 
80896
 
  if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
80897
 
    return pBest;
80898
 
  }
80899
 
  return 0;
80900
 
}
80901
 
 
80902
 
/*
80903
 
** Free all resources held by the schema structure. The void* argument points
80904
 
** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the 
80905
 
** pointer itself, it just cleans up subsidiary resources (i.e. the contents
80906
 
** of the schema hash tables).
80907
 
**
80908
 
** The Schema.cache_size variable is not cleared.
80909
 
*/
80910
 
SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
80911
 
  Hash temp1;
80912
 
  Hash temp2;
80913
 
  HashElem *pElem;
80914
 
  Schema *pSchema = (Schema *)p;
80915
 
 
80916
 
  temp1 = pSchema->tblHash;
80917
 
  temp2 = pSchema->trigHash;
80918
 
  sqlite3HashInit(&pSchema->trigHash);
80919
 
  sqlite3HashClear(&pSchema->idxHash);
80920
 
  for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
80921
 
    sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
80922
 
  }
80923
 
  sqlite3HashClear(&temp2);
80924
 
  sqlite3HashInit(&pSchema->tblHash);
80925
 
  for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
80926
 
    Table *pTab = sqliteHashData(pElem);
80927
 
    sqlite3DeleteTable(0, pTab);
80928
 
  }
80929
 
  sqlite3HashClear(&temp1);
80930
 
  sqlite3HashClear(&pSchema->fkeyHash);
80931
 
  pSchema->pSeqTab = 0;
80932
 
  if( pSchema->flags & DB_SchemaLoaded ){
80933
 
    pSchema->iGeneration++;
80934
 
    pSchema->flags &= ~DB_SchemaLoaded;
80935
 
  }
80936
 
}
80937
 
 
80938
 
/*
80939
 
** Find and return the schema associated with a BTree.  Create
80940
 
** a new one if necessary.
80941
 
*/
80942
 
SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
80943
 
  Schema * p;
80944
 
  if( pBt ){
80945
 
    p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
80946
 
  }else{
80947
 
    p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
80948
 
  }
80949
 
  if( !p ){
80950
 
    db->mallocFailed = 1;
80951
 
  }else if ( 0==p->file_format ){
80952
 
    sqlite3HashInit(&p->tblHash);
80953
 
    sqlite3HashInit(&p->idxHash);
80954
 
    sqlite3HashInit(&p->trigHash);
80955
 
    sqlite3HashInit(&p->fkeyHash);
80956
 
    p->enc = SQLITE_UTF8;
80957
 
  }
80958
 
  return p;
80959
 
}
80960
 
 
80961
 
/************** End of callback.c ********************************************/
80962
 
/************** Begin file delete.c ******************************************/
80963
 
/*
80964
 
** 2001 September 15
80965
 
**
80966
 
** The author disclaims copyright to this source code.  In place of
80967
 
** a legal notice, here is a blessing:
80968
 
**
80969
 
**    May you do good and not evil.
80970
 
**    May you find forgiveness for yourself and forgive others.
80971
 
**    May you share freely, never taking more than you give.
80972
 
**
80973
 
*************************************************************************
80974
 
** This file contains C code routines that are called by the parser
80975
 
** in order to generate code for DELETE FROM statements.
80976
 
*/
80977
 
 
80978
 
/*
80979
 
** While a SrcList can in general represent multiple tables and subqueries
80980
 
** (as in the FROM clause of a SELECT statement) in this case it contains
80981
 
** the name of a single table, as one might find in an INSERT, DELETE,
80982
 
** or UPDATE statement.  Look up that table in the symbol table and
80983
 
** return a pointer.  Set an error message and return NULL if the table 
80984
 
** name is not found or if any other error occurs.
80985
 
**
80986
 
** The following fields are initialized appropriate in pSrc:
80987
 
**
80988
 
**    pSrc->a[0].pTab       Pointer to the Table object
80989
 
**    pSrc->a[0].pIndex     Pointer to the INDEXED BY index, if there is one
80990
 
**
80991
 
*/
80992
 
SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
80993
 
  struct SrcList_item *pItem = pSrc->a;
80994
 
  Table *pTab;
80995
 
  assert( pItem && pSrc->nSrc==1 );
80996
 
  pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
80997
 
  sqlite3DeleteTable(pParse->db, pItem->pTab);
80998
 
  pItem->pTab = pTab;
80999
 
  if( pTab ){
81000
 
    pTab->nRef++;
81001
 
  }
81002
 
  if( sqlite3IndexedByLookup(pParse, pItem) ){
81003
 
    pTab = 0;
81004
 
  }
81005
 
  return pTab;
81006
 
}
81007
 
 
81008
 
/*
81009
 
** Check to make sure the given table is writable.  If it is not
81010
 
** writable, generate an error message and return 1.  If it is
81011
 
** writable return 0;
81012
 
*/
81013
 
SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
81014
 
  /* A table is not writable under the following circumstances:
81015
 
  **
81016
 
  **   1) It is a virtual table and no implementation of the xUpdate method
81017
 
  **      has been provided, or
81018
 
  **   2) It is a system table (i.e. sqlite_master), this call is not
81019
 
  **      part of a nested parse and writable_schema pragma has not 
81020
 
  **      been specified.
81021
 
  **
81022
 
  ** In either case leave an error message in pParse and return non-zero.
81023
 
  */
81024
 
  if( ( IsVirtual(pTab) 
81025
 
     && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
81026
 
   || ( (pTab->tabFlags & TF_Readonly)!=0
81027
 
     && (pParse->db->flags & SQLITE_WriteSchema)==0
81028
 
     && pParse->nested==0 )
81029
 
  ){
81030
 
    sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
81031
 
    return 1;
81032
 
  }
81033
 
 
81034
 
#ifndef SQLITE_OMIT_VIEW
81035
 
  if( !viewOk && pTab->pSelect ){
81036
 
    sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
81037
 
    return 1;
81038
 
  }
81039
 
#endif
81040
 
  return 0;
81041
 
}
81042
 
 
81043
 
 
81044
 
#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
81045
 
/*
81046
 
** Evaluate a view and store its result in an ephemeral table.  The
81047
 
** pWhere argument is an optional WHERE clause that restricts the
81048
 
** set of rows in the view that are to be added to the ephemeral table.
81049
 
*/
81050
 
SQLITE_PRIVATE void sqlite3MaterializeView(
81051
 
  Parse *pParse,       /* Parsing context */
81052
 
  Table *pView,        /* View definition */
81053
 
  Expr *pWhere,        /* Optional WHERE clause to be added */
81054
 
  int iCur             /* Cursor number for ephemerial table */
81055
 
){
81056
 
  SelectDest dest;
81057
 
  Select *pDup;
81058
 
  sqlite3 *db = pParse->db;
81059
 
 
81060
 
  pDup = sqlite3SelectDup(db, pView->pSelect, 0);
81061
 
  if( pWhere ){
81062
 
    SrcList *pFrom;
81063
 
    
81064
 
    pWhere = sqlite3ExprDup(db, pWhere, 0);
81065
 
    pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
81066
 
    if( pFrom ){
81067
 
      assert( pFrom->nSrc==1 );
81068
 
      pFrom->a[0].zAlias = sqlite3DbStrDup(db, pView->zName);
81069
 
      pFrom->a[0].pSelect = pDup;
81070
 
      assert( pFrom->a[0].pOn==0 );
81071
 
      assert( pFrom->a[0].pUsing==0 );
81072
 
    }else{
81073
 
      sqlite3SelectDelete(db, pDup);
81074
 
    }
81075
 
    pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
81076
 
  }
81077
 
  sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
81078
 
  sqlite3Select(pParse, pDup, &dest);
81079
 
  sqlite3SelectDelete(db, pDup);
81080
 
}
81081
 
#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
81082
 
 
81083
 
#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
81084
 
/*
81085
 
** Generate an expression tree to implement the WHERE, ORDER BY,
81086
 
** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
81087
 
**
81088
 
**     DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
81089
 
**                            \__________________________/
81090
 
**                               pLimitWhere (pInClause)
81091
 
*/
81092
 
SQLITE_PRIVATE Expr *sqlite3LimitWhere(
81093
 
  Parse *pParse,               /* The parser context */
81094
 
  SrcList *pSrc,               /* the FROM clause -- which tables to scan */
81095
 
  Expr *pWhere,                /* The WHERE clause.  May be null */
81096
 
  ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
81097
 
  Expr *pLimit,                /* The LIMIT clause.  May be null */
81098
 
  Expr *pOffset,               /* The OFFSET clause.  May be null */
81099
 
  char *zStmtType              /* Either DELETE or UPDATE.  For error messages. */
81100
 
){
81101
 
  Expr *pWhereRowid = NULL;    /* WHERE rowid .. */
81102
 
  Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
81103
 
  Expr *pSelectRowid = NULL;   /* SELECT rowid ... */
81104
 
  ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
81105
 
  SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
81106
 
  Select *pSelect = NULL;      /* Complete SELECT tree */
81107
 
 
81108
 
  /* Check that there isn't an ORDER BY without a LIMIT clause.
81109
 
  */
81110
 
  if( pOrderBy && (pLimit == 0) ) {
81111
 
    sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
81112
 
    pParse->parseError = 1;
81113
 
    goto limit_where_cleanup_2;
81114
 
  }
81115
 
 
81116
 
  /* We only need to generate a select expression if there
81117
 
  ** is a limit/offset term to enforce.
81118
 
  */
81119
 
  if( pLimit == 0 ) {
81120
 
    /* if pLimit is null, pOffset will always be null as well. */
81121
 
    assert( pOffset == 0 );
81122
 
    return pWhere;
81123
 
  }
81124
 
 
81125
 
  /* Generate a select expression tree to enforce the limit/offset 
81126
 
  ** term for the DELETE or UPDATE statement.  For example:
81127
 
  **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
81128
 
  ** becomes:
81129
 
  **   DELETE FROM table_a WHERE rowid IN ( 
81130
 
  **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
81131
 
  **   );
81132
 
  */
81133
 
 
81134
 
  pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
81135
 
  if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
81136
 
  pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
81137
 
  if( pEList == 0 ) goto limit_where_cleanup_2;
81138
 
 
81139
 
  /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
81140
 
  ** and the SELECT subtree. */
81141
 
  pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
81142
 
  if( pSelectSrc == 0 ) {
81143
 
    sqlite3ExprListDelete(pParse->db, pEList);
81144
 
    goto limit_where_cleanup_2;
81145
 
  }
81146
 
 
81147
 
  /* generate the SELECT expression tree. */
81148
 
  pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
81149
 
                             pOrderBy,0,pLimit,pOffset);
81150
 
  if( pSelect == 0 ) return 0;
81151
 
 
81152
 
  /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
81153
 
  pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
81154
 
  if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
81155
 
  pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
81156
 
  if( pInClause == 0 ) goto limit_where_cleanup_1;
81157
 
 
81158
 
  pInClause->x.pSelect = pSelect;
81159
 
  pInClause->flags |= EP_xIsSelect;
81160
 
  sqlite3ExprSetHeight(pParse, pInClause);
81161
 
  return pInClause;
81162
 
 
81163
 
  /* something went wrong. clean up anything allocated. */
81164
 
limit_where_cleanup_1:
81165
 
  sqlite3SelectDelete(pParse->db, pSelect);
81166
 
  return 0;
81167
 
 
81168
 
limit_where_cleanup_2:
81169
 
  sqlite3ExprDelete(pParse->db, pWhere);
81170
 
  sqlite3ExprListDelete(pParse->db, pOrderBy);
81171
 
  sqlite3ExprDelete(pParse->db, pLimit);
81172
 
  sqlite3ExprDelete(pParse->db, pOffset);
81173
 
  return 0;
81174
 
}
81175
 
#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
81176
 
 
81177
 
/*
81178
 
** Generate code for a DELETE FROM statement.
81179
 
**
81180
 
**     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
81181
 
**                 \________/       \________________/
81182
 
**                  pTabList              pWhere
81183
 
*/
81184
 
SQLITE_PRIVATE void sqlite3DeleteFrom(
81185
 
  Parse *pParse,         /* The parser context */
81186
 
  SrcList *pTabList,     /* The table from which we should delete things */
81187
 
  Expr *pWhere           /* The WHERE clause.  May be null */
81188
 
){
81189
 
  Vdbe *v;               /* The virtual database engine */
81190
 
  Table *pTab;           /* The table from which records will be deleted */
81191
 
  const char *zDb;       /* Name of database holding pTab */
81192
 
  int end, addr = 0;     /* A couple addresses of generated code */
81193
 
  int i;                 /* Loop counter */
81194
 
  WhereInfo *pWInfo;     /* Information about the WHERE clause */
81195
 
  Index *pIdx;           /* For looping over indices of the table */
81196
 
  int iCur;              /* VDBE Cursor number for pTab */
81197
 
  sqlite3 *db;           /* Main database structure */
81198
 
  AuthContext sContext;  /* Authorization context */
81199
 
  NameContext sNC;       /* Name context to resolve expressions in */
81200
 
  int iDb;               /* Database number */
81201
 
  int memCnt = -1;       /* Memory cell used for change counting */
81202
 
  int rcauth;            /* Value returned by authorization callback */
81203
 
 
81204
 
#ifndef SQLITE_OMIT_TRIGGER
81205
 
  int isView;                  /* True if attempting to delete from a view */
81206
 
  Trigger *pTrigger;           /* List of table triggers, if required */
81207
 
#endif
81208
 
 
81209
 
  memset(&sContext, 0, sizeof(sContext));
81210
 
  db = pParse->db;
81211
 
  if( pParse->nErr || db->mallocFailed ){
81212
 
    goto delete_from_cleanup;
81213
 
  }
81214
 
  assert( pTabList->nSrc==1 );
81215
 
 
81216
 
  /* Locate the table which we want to delete.  This table has to be
81217
 
  ** put in an SrcList structure because some of the subroutines we
81218
 
  ** will be calling are designed to work with multiple tables and expect
81219
 
  ** an SrcList* parameter instead of just a Table* parameter.
81220
 
  */
81221
 
  pTab = sqlite3SrcListLookup(pParse, pTabList);
81222
 
  if( pTab==0 )  goto delete_from_cleanup;
81223
 
 
81224
 
  /* Figure out if we have any triggers and if the table being
81225
 
  ** deleted from is a view
81226
 
  */
81227
 
#ifndef SQLITE_OMIT_TRIGGER
81228
 
  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
81229
 
  isView = pTab->pSelect!=0;
81230
 
#else
81231
 
# define pTrigger 0
81232
 
# define isView 0
81233
 
#endif
81234
 
#ifdef SQLITE_OMIT_VIEW
81235
 
# undef isView
81236
 
# define isView 0
81237
 
#endif
81238
 
 
81239
 
  /* If pTab is really a view, make sure it has been initialized.
81240
 
  */
81241
 
  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
81242
 
    goto delete_from_cleanup;
81243
 
  }
81244
 
 
81245
 
  if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
81246
 
    goto delete_from_cleanup;
81247
 
  }
81248
 
  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
81249
 
  assert( iDb<db->nDb );
81250
 
  zDb = db->aDb[iDb].zName;
81251
 
  rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
81252
 
  assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
81253
 
  if( rcauth==SQLITE_DENY ){
81254
 
    goto delete_from_cleanup;
81255
 
  }
81256
 
  assert(!isView || pTrigger);
81257
 
 
81258
 
  /* Assign  cursor number to the table and all its indices.
81259
 
  */
81260
 
  assert( pTabList->nSrc==1 );
81261
 
  iCur = pTabList->a[0].iCursor = pParse->nTab++;
81262
 
  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
81263
 
    pParse->nTab++;
81264
 
  }
81265
 
 
81266
 
  /* Start the view context
81267
 
  */
81268
 
  if( isView ){
81269
 
    sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
81270
 
  }
81271
 
 
81272
 
  /* Begin generating code.
81273
 
  */
81274
 
  v = sqlite3GetVdbe(pParse);
81275
 
  if( v==0 ){
81276
 
    goto delete_from_cleanup;
81277
 
  }
81278
 
  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
81279
 
  sqlite3BeginWriteOperation(pParse, 1, iDb);
81280
 
 
81281
 
  /* If we are trying to delete from a view, realize that view into
81282
 
  ** a ephemeral table.
81283
 
  */
81284
 
#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
81285
 
  if( isView ){
81286
 
    sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
81287
 
  }
81288
 
#endif
81289
 
 
81290
 
  /* Resolve the column names in the WHERE clause.
81291
 
  */
81292
 
  memset(&sNC, 0, sizeof(sNC));
81293
 
  sNC.pParse = pParse;
81294
 
  sNC.pSrcList = pTabList;
81295
 
  if( sqlite3ResolveExprNames(&sNC, pWhere) ){
81296
 
    goto delete_from_cleanup;
81297
 
  }
81298
 
 
81299
 
  /* Initialize the counter of the number of rows deleted, if
81300
 
  ** we are counting rows.
81301
 
  */
81302
 
  if( db->flags & SQLITE_CountRows ){
81303
 
    memCnt = ++pParse->nMem;
81304
 
    sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
81305
 
  }
81306
 
 
81307
 
#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
81308
 
  /* Special case: A DELETE without a WHERE clause deletes everything.
81309
 
  ** It is easier just to erase the whole table. Prior to version 3.6.5,
81310
 
  ** this optimization caused the row change count (the value returned by 
81311
 
  ** API function sqlite3_count_changes) to be set incorrectly.  */
81312
 
  if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab) 
81313
 
   && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
81314
 
  ){
81315
 
    assert( !isView );
81316
 
    sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
81317
 
                      pTab->zName, P4_STATIC);
81318
 
    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
81319
 
      assert( pIdx->pSchema==pTab->pSchema );
81320
 
      sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
81321
 
    }
81322
 
  }else
81323
 
#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
81324
 
  /* The usual case: There is a WHERE clause so we have to scan through
81325
 
  ** the table and pick which records to delete.
81326
 
  */
81327
 
  {
81328
 
    int iRowSet = ++pParse->nMem;   /* Register for rowset of rows to delete */
81329
 
    int iRowid = ++pParse->nMem;    /* Used for storing rowid values. */
81330
 
    int regRowid;                   /* Actual register containing rowids */
81331
 
 
81332
 
    /* Collect rowids of every row to be deleted.
81333
 
    */
81334
 
    sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
81335
 
    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,WHERE_DUPLICATES_OK);
81336
 
    if( pWInfo==0 ) goto delete_from_cleanup;
81337
 
    regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid);
81338
 
    sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
81339
 
    if( db->flags & SQLITE_CountRows ){
81340
 
      sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
81341
 
    }
81342
 
    sqlite3WhereEnd(pWInfo);
81343
 
 
81344
 
    /* Delete every item whose key was written to the list during the
81345
 
    ** database scan.  We have to delete items after the scan is complete
81346
 
    ** because deleting an item can change the scan order.  */
81347
 
    end = sqlite3VdbeMakeLabel(v);
81348
 
 
81349
 
    /* Unless this is a view, open cursors for the table we are 
81350
 
    ** deleting from and all its indices. If this is a view, then the
81351
 
    ** only effect this statement has is to fire the INSTEAD OF 
81352
 
    ** triggers.  */
81353
 
    if( !isView ){
81354
 
      sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite);
81355
 
    }
81356
 
 
81357
 
    addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid);
81358
 
 
81359
 
    /* Delete the row */
81360
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
81361
 
    if( IsVirtual(pTab) ){
81362
 
      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
81363
 
      sqlite3VtabMakeWritable(pParse, pTab);
81364
 
      sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB);
81365
 
      sqlite3MayAbort(pParse);
81366
 
    }else
81367
 
#endif
81368
 
    {
81369
 
      int count = (pParse->nested==0);    /* True to count changes */
81370
 
      sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, count, pTrigger, OE_Default);
81371
 
    }
81372
 
 
81373
 
    /* End of the delete loop */
81374
 
    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
81375
 
    sqlite3VdbeResolveLabel(v, end);
81376
 
 
81377
 
    /* Close the cursors open on the table and its indexes. */
81378
 
    if( !isView && !IsVirtual(pTab) ){
81379
 
      for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
81380
 
        sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum);
81381
 
      }
81382
 
      sqlite3VdbeAddOp1(v, OP_Close, iCur);
81383
 
    }
81384
 
  }
81385
 
 
81386
 
  /* Update the sqlite_sequence table by storing the content of the
81387
 
  ** maximum rowid counter values recorded while inserting into
81388
 
  ** autoincrement tables.
81389
 
  */
81390
 
  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
81391
 
    sqlite3AutoincrementEnd(pParse);
81392
 
  }
81393
 
 
81394
 
  /* Return the number of rows that were deleted. If this routine is 
81395
 
  ** generating code because of a call to sqlite3NestedParse(), do not
81396
 
  ** invoke the callback function.
81397
 
  */
81398
 
  if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
81399
 
    sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
81400
 
    sqlite3VdbeSetNumCols(v, 1);
81401
 
    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
81402
 
  }
81403
 
 
81404
 
delete_from_cleanup:
81405
 
  sqlite3AuthContextPop(&sContext);
81406
 
  sqlite3SrcListDelete(db, pTabList);
81407
 
  sqlite3ExprDelete(db, pWhere);
81408
 
  return;
81409
 
}
81410
 
/* Make sure "isView" and other macros defined above are undefined. Otherwise
81411
 
** thely may interfere with compilation of other functions in this file
81412
 
** (or in another file, if this file becomes part of the amalgamation).  */
81413
 
#ifdef isView
81414
 
 #undef isView
81415
 
#endif
81416
 
#ifdef pTrigger
81417
 
 #undef pTrigger
81418
 
#endif
81419
 
 
81420
 
/*
81421
 
** This routine generates VDBE code that causes a single row of a
81422
 
** single table to be deleted.
81423
 
**
81424
 
** The VDBE must be in a particular state when this routine is called.
81425
 
** These are the requirements:
81426
 
**
81427
 
**   1.  A read/write cursor pointing to pTab, the table containing the row
81428
 
**       to be deleted, must be opened as cursor number $iCur.
81429
 
**
81430
 
**   2.  Read/write cursors for all indices of pTab must be open as
81431
 
**       cursor number base+i for the i-th index.
81432
 
**
81433
 
**   3.  The record number of the row to be deleted must be stored in
81434
 
**       memory cell iRowid.
81435
 
**
81436
 
** This routine generates code to remove both the table record and all 
81437
 
** index entries that point to that record.
81438
 
*/
81439
 
SQLITE_PRIVATE void sqlite3GenerateRowDelete(
81440
 
  Parse *pParse,     /* Parsing context */
81441
 
  Table *pTab,       /* Table containing the row to be deleted */
81442
 
  int iCur,          /* Cursor number for the table */
81443
 
  int iRowid,        /* Memory cell that contains the rowid to delete */
81444
 
  int count,         /* If non-zero, increment the row change counter */
81445
 
  Trigger *pTrigger, /* List of triggers to (potentially) fire */
81446
 
  int onconf         /* Default ON CONFLICT policy for triggers */
81447
 
){
81448
 
  Vdbe *v = pParse->pVdbe;        /* Vdbe */
81449
 
  int iOld = 0;                   /* First register in OLD.* array */
81450
 
  int iLabel;                     /* Label resolved to end of generated code */
81451
 
 
81452
 
  /* Vdbe is guaranteed to have been allocated by this stage. */
81453
 
  assert( v );
81454
 
 
81455
 
  /* Seek cursor iCur to the row to delete. If this row no longer exists 
81456
 
  ** (this can happen if a trigger program has already deleted it), do
81457
 
  ** not attempt to delete it or fire any DELETE triggers.  */
81458
 
  iLabel = sqlite3VdbeMakeLabel(v);
81459
 
  sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
81460
 
 
81461
 
  /* If there are any triggers to fire, allocate a range of registers to
81462
 
  ** use for the old.* references in the triggers.  */
81463
 
  if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
81464
 
    u32 mask;                     /* Mask of OLD.* columns in use */
81465
 
    int iCol;                     /* Iterator used while populating OLD.* */
81466
 
 
81467
 
    /* TODO: Could use temporary registers here. Also could attempt to
81468
 
    ** avoid copying the contents of the rowid register.  */
81469
 
    mask = sqlite3TriggerColmask(
81470
 
        pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
81471
 
    );
81472
 
    mask |= sqlite3FkOldmask(pParse, pTab);
81473
 
    iOld = pParse->nMem+1;
81474
 
    pParse->nMem += (1 + pTab->nCol);
81475
 
 
81476
 
    /* Populate the OLD.* pseudo-table register array. These values will be 
81477
 
    ** used by any BEFORE and AFTER triggers that exist.  */
81478
 
    sqlite3VdbeAddOp2(v, OP_Copy, iRowid, iOld);
81479
 
    for(iCol=0; iCol<pTab->nCol; iCol++){
81480
 
      if( mask==0xffffffff || mask&(1<<iCol) ){
81481
 
        sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol, iOld+iCol+1);
81482
 
      }
81483
 
    }
81484
 
 
81485
 
    /* Invoke BEFORE DELETE trigger programs. */
81486
 
    sqlite3CodeRowTrigger(pParse, pTrigger, 
81487
 
        TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
81488
 
    );
81489
 
 
81490
 
    /* Seek the cursor to the row to be deleted again. It may be that
81491
 
    ** the BEFORE triggers coded above have already removed the row
81492
 
    ** being deleted. Do not attempt to delete the row a second time, and 
81493
 
    ** do not fire AFTER triggers.  */
81494
 
    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
81495
 
 
81496
 
    /* Do FK processing. This call checks that any FK constraints that
81497
 
    ** refer to this table (i.e. constraints attached to other tables) 
81498
 
    ** are not violated by deleting this row.  */
81499
 
    sqlite3FkCheck(pParse, pTab, iOld, 0);
81500
 
  }
81501
 
 
81502
 
  /* Delete the index and table entries. Skip this step if pTab is really
81503
 
  ** a view (in which case the only effect of the DELETE statement is to
81504
 
  ** fire the INSTEAD OF triggers).  */ 
81505
 
  if( pTab->pSelect==0 ){
81506
 
    sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0);
81507
 
    sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
81508
 
    if( count ){
81509
 
      sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
81510
 
    }
81511
 
  }
81512
 
 
81513
 
  /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
81514
 
  ** handle rows (possibly in other tables) that refer via a foreign key
81515
 
  ** to the row just deleted. */ 
81516
 
  sqlite3FkActions(pParse, pTab, 0, iOld);
81517
 
 
81518
 
  /* Invoke AFTER DELETE trigger programs. */
81519
 
  sqlite3CodeRowTrigger(pParse, pTrigger, 
81520
 
      TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
81521
 
  );
81522
 
 
81523
 
  /* Jump here if the row had already been deleted before any BEFORE
81524
 
  ** trigger programs were invoked. Or if a trigger program throws a 
81525
 
  ** RAISE(IGNORE) exception.  */
81526
 
  sqlite3VdbeResolveLabel(v, iLabel);
81527
 
}
81528
 
 
81529
 
/*
81530
 
** This routine generates VDBE code that causes the deletion of all
81531
 
** index entries associated with a single row of a single table.
81532
 
**
81533
 
** The VDBE must be in a particular state when this routine is called.
81534
 
** These are the requirements:
81535
 
**
81536
 
**   1.  A read/write cursor pointing to pTab, the table containing the row
81537
 
**       to be deleted, must be opened as cursor number "iCur".
81538
 
**
81539
 
**   2.  Read/write cursors for all indices of pTab must be open as
81540
 
**       cursor number iCur+i for the i-th index.
81541
 
**
81542
 
**   3.  The "iCur" cursor must be pointing to the row that is to be
81543
 
**       deleted.
81544
 
*/
81545
 
SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
81546
 
  Parse *pParse,     /* Parsing and code generating context */
81547
 
  Table *pTab,       /* Table containing the row to be deleted */
81548
 
  int iCur,          /* Cursor number for the table */
81549
 
  int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
81550
 
){
81551
 
  int i;
81552
 
  Index *pIdx;
81553
 
  int r1;
81554
 
 
81555
 
  for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
81556
 
    if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue;
81557
 
    r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0, 0);
81558
 
    sqlite3VdbeAddOp3(pParse->pVdbe, OP_IdxDelete, iCur+i, r1,pIdx->nColumn+1);
81559
 
  }
81560
 
}
81561
 
 
81562
 
/*
81563
 
** Generate code that will assemble an index key and put it in register
81564
 
** regOut.  The key with be for index pIdx which is an index on pTab.
81565
 
** iCur is the index of a cursor open on the pTab table and pointing to
81566
 
** the entry that needs indexing.
81567
 
**
81568
 
** Return a register number which is the first in a block of
81569
 
** registers that holds the elements of the index key.  The
81570
 
** block of registers has already been deallocated by the time
81571
 
** this routine returns.
81572
 
*/
81573
 
SQLITE_PRIVATE int sqlite3GenerateIndexKey(
81574
 
  Parse *pParse,     /* Parsing context */
81575
 
  Index *pIdx,       /* The index for which to generate a key */
81576
 
  int iCur,          /* Cursor number for the pIdx->pTable table */
81577
 
  int regOut,        /* Write the new index key to this register */
81578
 
  int doMakeRec      /* Run the OP_MakeRecord instruction if true */
81579
 
){
81580
 
  Vdbe *v = pParse->pVdbe;
81581
 
  int j;
81582
 
  Table *pTab = pIdx->pTable;
81583
 
  int regBase;
81584
 
  int nCol;
81585
 
 
81586
 
  nCol = pIdx->nColumn;
81587
 
  regBase = sqlite3GetTempRange(pParse, nCol+1);
81588
 
  sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol);
81589
 
  for(j=0; j<nCol; j++){
81590
 
    int idx = pIdx->aiColumn[j];
81591
 
    if( idx==pTab->iPKey ){
81592
 
      sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j);
81593
 
    }else{
81594
 
      sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j);
81595
 
      sqlite3ColumnDefault(v, pTab, idx, -1);
81596
 
    }
81597
 
  }
81598
 
  if( doMakeRec ){
81599
 
    sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut);
81600
 
    sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
81601
 
  }
81602
 
  sqlite3ReleaseTempRange(pParse, regBase, nCol+1);
81603
 
  return regBase;
81604
 
}
81605
 
 
81606
 
/************** End of delete.c **********************************************/
81607
 
/************** Begin file func.c ********************************************/
81608
 
/*
81609
 
** 2002 February 23
81610
 
**
81611
 
** The author disclaims copyright to this source code.  In place of
81612
 
** a legal notice, here is a blessing:
81613
 
**
81614
 
**    May you do good and not evil.
81615
 
**    May you find forgiveness for yourself and forgive others.
81616
 
**    May you share freely, never taking more than you give.
81617
 
**
81618
 
*************************************************************************
81619
 
** This file contains the C functions that implement various SQL
81620
 
** functions of SQLite.  
81621
 
**
81622
 
** There is only one exported symbol in this file - the function
81623
 
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
81624
 
** All other code has file scope.
81625
 
*/
81626
 
 
81627
 
/*
81628
 
** Return the collating function associated with a function.
81629
 
*/
81630
 
static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
81631
 
  return context->pColl;
81632
 
}
81633
 
 
81634
 
/*
81635
 
** Implementation of the non-aggregate min() and max() functions
81636
 
*/
81637
 
static void minmaxFunc(
81638
 
  sqlite3_context *context,
81639
 
  int argc,
81640
 
  sqlite3_value **argv
81641
 
){
81642
 
  int i;
81643
 
  int mask;    /* 0 for min() or 0xffffffff for max() */
81644
 
  int iBest;
81645
 
  CollSeq *pColl;
81646
 
 
81647
 
  assert( argc>1 );
81648
 
  mask = sqlite3_user_data(context)==0 ? 0 : -1;
81649
 
  pColl = sqlite3GetFuncCollSeq(context);
81650
 
  assert( pColl );
81651
 
  assert( mask==-1 || mask==0 );
81652
 
  iBest = 0;
81653
 
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
81654
 
  for(i=1; i<argc; i++){
81655
 
    if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
81656
 
    if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
81657
 
      testcase( mask==0 );
81658
 
      iBest = i;
81659
 
    }
81660
 
  }
81661
 
  sqlite3_result_value(context, argv[iBest]);
81662
 
}
81663
 
 
81664
 
/*
81665
 
** Return the type of the argument.
81666
 
*/
81667
 
static void typeofFunc(
81668
 
  sqlite3_context *context,
81669
 
  int NotUsed,
81670
 
  sqlite3_value **argv
81671
 
){
81672
 
  const char *z = 0;
81673
 
  UNUSED_PARAMETER(NotUsed);
81674
 
  switch( sqlite3_value_type(argv[0]) ){
81675
 
    case SQLITE_INTEGER: z = "integer"; break;
81676
 
    case SQLITE_TEXT:    z = "text";    break;
81677
 
    case SQLITE_FLOAT:   z = "real";    break;
81678
 
    case SQLITE_BLOB:    z = "blob";    break;
81679
 
    default:             z = "null";    break;
81680
 
  }
81681
 
  sqlite3_result_text(context, z, -1, SQLITE_STATIC);
81682
 
}
81683
 
 
81684
 
 
81685
 
/*
81686
 
** Implementation of the length() function
81687
 
*/
81688
 
static void lengthFunc(
81689
 
  sqlite3_context *context,
81690
 
  int argc,
81691
 
  sqlite3_value **argv
81692
 
){
81693
 
  int len;
81694
 
 
81695
 
  assert( argc==1 );
81696
 
  UNUSED_PARAMETER(argc);
81697
 
  switch( sqlite3_value_type(argv[0]) ){
81698
 
    case SQLITE_BLOB:
81699
 
    case SQLITE_INTEGER:
81700
 
    case SQLITE_FLOAT: {
81701
 
      sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
81702
 
      break;
81703
 
    }
81704
 
    case SQLITE_TEXT: {
81705
 
      const unsigned char *z = sqlite3_value_text(argv[0]);
81706
 
      if( z==0 ) return;
81707
 
      len = 0;
81708
 
      while( *z ){
81709
 
        len++;
81710
 
        SQLITE_SKIP_UTF8(z);
81711
 
      }
81712
 
      sqlite3_result_int(context, len);
81713
 
      break;
81714
 
    }
81715
 
    default: {
81716
 
      sqlite3_result_null(context);
81717
 
      break;
81718
 
    }
81719
 
  }
81720
 
}
81721
 
 
81722
 
/*
81723
 
** Implementation of the abs() function.
81724
 
**
81725
 
** IMP: R-23979-26855 The abs(X) function returns the absolute value of
81726
 
** the numeric argument X. 
81727
 
*/
81728
 
static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
81729
 
  assert( argc==1 );
81730
 
  UNUSED_PARAMETER(argc);
81731
 
  switch( sqlite3_value_type(argv[0]) ){
81732
 
    case SQLITE_INTEGER: {
81733
 
      i64 iVal = sqlite3_value_int64(argv[0]);
81734
 
      if( iVal<0 ){
81735
 
        if( (iVal<<1)==0 ){
81736
 
          /* IMP: R-35460-15084 If X is the integer -9223372036854775807 then
81737
 
          ** abs(X) throws an integer overflow error since there is no
81738
 
          ** equivalent positive 64-bit two complement value. */
81739
 
          sqlite3_result_error(context, "integer overflow", -1);
81740
 
          return;
81741
 
        }
81742
 
        iVal = -iVal;
81743
 
      } 
81744
 
      sqlite3_result_int64(context, iVal);
81745
 
      break;
81746
 
    }
81747
 
    case SQLITE_NULL: {
81748
 
      /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
81749
 
      sqlite3_result_null(context);
81750
 
      break;
81751
 
    }
81752
 
    default: {
81753
 
      /* Because sqlite3_value_double() returns 0.0 if the argument is not
81754
 
      ** something that can be converted into a number, we have:
81755
 
      ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that
81756
 
      ** cannot be converted to a numeric value. 
81757
 
      */
81758
 
      double rVal = sqlite3_value_double(argv[0]);
81759
 
      if( rVal<0 ) rVal = -rVal;
81760
 
      sqlite3_result_double(context, rVal);
81761
 
      break;
81762
 
    }
81763
 
  }
81764
 
}
81765
 
 
81766
 
/*
81767
 
** Implementation of the substr() function.
81768
 
**
81769
 
** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
81770
 
** p1 is 1-indexed.  So substr(x,1,1) returns the first character
81771
 
** of x.  If x is text, then we actually count UTF-8 characters.
81772
 
** If x is a blob, then we count bytes.
81773
 
**
81774
 
** If p1 is negative, then we begin abs(p1) from the end of x[].
81775
 
**
81776
 
** If p2 is negative, return the p2 characters preceeding p1.
81777
 
*/
81778
 
static void substrFunc(
81779
 
  sqlite3_context *context,
81780
 
  int argc,
81781
 
  sqlite3_value **argv
81782
 
){
81783
 
  const unsigned char *z;
81784
 
  const unsigned char *z2;
81785
 
  int len;
81786
 
  int p0type;
81787
 
  i64 p1, p2;
81788
 
  int negP2 = 0;
81789
 
 
81790
 
  assert( argc==3 || argc==2 );
81791
 
  if( sqlite3_value_type(argv[1])==SQLITE_NULL
81792
 
   || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
81793
 
  ){
81794
 
    return;
81795
 
  }
81796
 
  p0type = sqlite3_value_type(argv[0]);
81797
 
  p1 = sqlite3_value_int(argv[1]);
81798
 
  if( p0type==SQLITE_BLOB ){
81799
 
    len = sqlite3_value_bytes(argv[0]);
81800
 
    z = sqlite3_value_blob(argv[0]);
81801
 
    if( z==0 ) return;
81802
 
    assert( len==sqlite3_value_bytes(argv[0]) );
81803
 
  }else{
81804
 
    z = sqlite3_value_text(argv[0]);
81805
 
    if( z==0 ) return;
81806
 
    len = 0;
81807
 
    if( p1<0 ){
81808
 
      for(z2=z; *z2; len++){
81809
 
        SQLITE_SKIP_UTF8(z2);
81810
 
      }
81811
 
    }
81812
 
  }
81813
 
  if( argc==3 ){
81814
 
    p2 = sqlite3_value_int(argv[2]);
81815
 
    if( p2<0 ){
81816
 
      p2 = -p2;
81817
 
      negP2 = 1;
81818
 
    }
81819
 
  }else{
81820
 
    p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
81821
 
  }
81822
 
  if( p1<0 ){
81823
 
    p1 += len;
81824
 
    if( p1<0 ){
81825
 
      p2 += p1;
81826
 
      if( p2<0 ) p2 = 0;
81827
 
      p1 = 0;
81828
 
    }
81829
 
  }else if( p1>0 ){
81830
 
    p1--;
81831
 
  }else if( p2>0 ){
81832
 
    p2--;
81833
 
  }
81834
 
  if( negP2 ){
81835
 
    p1 -= p2;
81836
 
    if( p1<0 ){
81837
 
      p2 += p1;
81838
 
      p1 = 0;
81839
 
    }
81840
 
  }
81841
 
  assert( p1>=0 && p2>=0 );
81842
 
  if( p0type!=SQLITE_BLOB ){
81843
 
    while( *z && p1 ){
81844
 
      SQLITE_SKIP_UTF8(z);
81845
 
      p1--;
81846
 
    }
81847
 
    for(z2=z; *z2 && p2; p2--){
81848
 
      SQLITE_SKIP_UTF8(z2);
81849
 
    }
81850
 
    sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT);
81851
 
  }else{
81852
 
    if( p1+p2>len ){
81853
 
      p2 = len-p1;
81854
 
      if( p2<0 ) p2 = 0;
81855
 
    }
81856
 
    sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT);
81857
 
  }
81858
 
}
81859
 
 
81860
 
/*
81861
 
** Implementation of the round() function
81862
 
*/
81863
 
#ifndef SQLITE_OMIT_FLOATING_POINT
81864
 
static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
81865
 
  int n = 0;
81866
 
  double r;
81867
 
  char *zBuf;
81868
 
  assert( argc==1 || argc==2 );
81869
 
  if( argc==2 ){
81870
 
    if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
81871
 
    n = sqlite3_value_int(argv[1]);
81872
 
    if( n>30 ) n = 30;
81873
 
    if( n<0 ) n = 0;
81874
 
  }
81875
 
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
81876
 
  r = sqlite3_value_double(argv[0]);
81877
 
  /* If Y==0 and X will fit in a 64-bit int,
81878
 
  ** handle the rounding directly,
81879
 
  ** otherwise use printf.
81880
 
  */
81881
 
  if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
81882
 
    r = (double)((sqlite_int64)(r+0.5));
81883
 
  }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
81884
 
    r = -(double)((sqlite_int64)((-r)+0.5));
81885
 
  }else{
81886
 
    zBuf = sqlite3_mprintf("%.*f",n,r);
81887
 
    if( zBuf==0 ){
81888
 
      sqlite3_result_error_nomem(context);
81889
 
      return;
81890
 
    }
81891
 
    sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
81892
 
    sqlite3_free(zBuf);
81893
 
  }
81894
 
  sqlite3_result_double(context, r);
81895
 
}
81896
 
#endif
81897
 
 
81898
 
/*
81899
 
** Allocate nByte bytes of space using sqlite3_malloc(). If the
81900
 
** allocation fails, call sqlite3_result_error_nomem() to notify
81901
 
** the database handle that malloc() has failed and return NULL.
81902
 
** If nByte is larger than the maximum string or blob length, then
81903
 
** raise an SQLITE_TOOBIG exception and return NULL.
81904
 
*/
81905
 
static void *contextMalloc(sqlite3_context *context, i64 nByte){
81906
 
  char *z;
81907
 
  sqlite3 *db = sqlite3_context_db_handle(context);
81908
 
  assert( nByte>0 );
81909
 
  testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
81910
 
  testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
81911
 
  if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
81912
 
    sqlite3_result_error_toobig(context);
81913
 
    z = 0;
81914
 
  }else{
81915
 
    z = sqlite3Malloc((int)nByte);
81916
 
    if( !z ){
81917
 
      sqlite3_result_error_nomem(context);
81918
 
    }
81919
 
  }
81920
 
  return z;
81921
 
}
81922
 
 
81923
 
/*
81924
 
** Implementation of the upper() and lower() SQL functions.
81925
 
*/
81926
 
static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
81927
 
  char *z1;
81928
 
  const char *z2;
81929
 
  int i, n;
81930
 
  UNUSED_PARAMETER(argc);
81931
 
  z2 = (char*)sqlite3_value_text(argv[0]);
81932
 
  n = sqlite3_value_bytes(argv[0]);
81933
 
  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
81934
 
  assert( z2==(char*)sqlite3_value_text(argv[0]) );
81935
 
  if( z2 ){
81936
 
    z1 = contextMalloc(context, ((i64)n)+1);
81937
 
    if( z1 ){
81938
 
      memcpy(z1, z2, n+1);
81939
 
      for(i=0; z1[i]; i++){
81940
 
        z1[i] = (char)sqlite3Toupper(z1[i]);
81941
 
      }
81942
 
      sqlite3_result_text(context, z1, -1, sqlite3_free);
81943
 
    }
81944
 
  }
81945
 
}
81946
 
static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
81947
 
  u8 *z1;
81948
 
  const char *z2;
81949
 
  int i, n;
81950
 
  UNUSED_PARAMETER(argc);
81951
 
  z2 = (char*)sqlite3_value_text(argv[0]);
81952
 
  n = sqlite3_value_bytes(argv[0]);
81953
 
  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
81954
 
  assert( z2==(char*)sqlite3_value_text(argv[0]) );
81955
 
  if( z2 ){
81956
 
    z1 = contextMalloc(context, ((i64)n)+1);
81957
 
    if( z1 ){
81958
 
      memcpy(z1, z2, n+1);
81959
 
      for(i=0; z1[i]; i++){
81960
 
        z1[i] = sqlite3Tolower(z1[i]);
81961
 
      }
81962
 
      sqlite3_result_text(context, (char *)z1, -1, sqlite3_free);
81963
 
    }
81964
 
  }
81965
 
}
81966
 
 
81967
 
 
81968
 
#if 0  /* This function is never used. */
81969
 
/*
81970
 
** The COALESCE() and IFNULL() functions used to be implemented as shown
81971
 
** here.  But now they are implemented as VDBE code so that unused arguments
81972
 
** do not have to be computed.  This legacy implementation is retained as
81973
 
** comment.
81974
 
*/
81975
 
/*
81976
 
** Implementation of the IFNULL(), NVL(), and COALESCE() functions.  
81977
 
** All three do the same thing.  They return the first non-NULL
81978
 
** argument.
81979
 
*/
81980
 
static void ifnullFunc(
81981
 
  sqlite3_context *context,
81982
 
  int argc,
81983
 
  sqlite3_value **argv
81984
 
){
81985
 
  int i;
81986
 
  for(i=0; i<argc; i++){
81987
 
    if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
81988
 
      sqlite3_result_value(context, argv[i]);
81989
 
      break;
81990
 
    }
81991
 
  }
81992
 
}
81993
 
#endif /* NOT USED */
81994
 
#define ifnullFunc versionFunc   /* Substitute function - never called */
81995
 
 
81996
 
/*
81997
 
** Implementation of random().  Return a random integer.  
81998
 
*/
81999
 
static void randomFunc(
82000
 
  sqlite3_context *context,
82001
 
  int NotUsed,
82002
 
  sqlite3_value **NotUsed2
82003
 
){
82004
 
  sqlite_int64 r;
82005
 
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
82006
 
  sqlite3_randomness(sizeof(r), &r);
82007
 
  if( r<0 ){
82008
 
    /* We need to prevent a random number of 0x8000000000000000 
82009
 
    ** (or -9223372036854775808) since when you do abs() of that
82010
 
    ** number of you get the same value back again.  To do this
82011
 
    ** in a way that is testable, mask the sign bit off of negative
82012
 
    ** values, resulting in a positive value.  Then take the 
82013
 
    ** 2s complement of that positive value.  The end result can
82014
 
    ** therefore be no less than -9223372036854775807.
82015
 
    */
82016
 
    r = -(r ^ (((sqlite3_int64)1)<<63));
82017
 
  }
82018
 
  sqlite3_result_int64(context, r);
82019
 
}
82020
 
 
82021
 
/*
82022
 
** Implementation of randomblob(N).  Return a random blob
82023
 
** that is N bytes long.
82024
 
*/
82025
 
static void randomBlob(
82026
 
  sqlite3_context *context,
82027
 
  int argc,
82028
 
  sqlite3_value **argv
82029
 
){
82030
 
  int n;
82031
 
  unsigned char *p;
82032
 
  assert( argc==1 );
82033
 
  UNUSED_PARAMETER(argc);
82034
 
  n = sqlite3_value_int(argv[0]);
82035
 
  if( n<1 ){
82036
 
    n = 1;
82037
 
  }
82038
 
  p = contextMalloc(context, n);
82039
 
  if( p ){
82040
 
    sqlite3_randomness(n, p);
82041
 
    sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
82042
 
  }
82043
 
}
82044
 
 
82045
 
/*
82046
 
** Implementation of the last_insert_rowid() SQL function.  The return
82047
 
** value is the same as the sqlite3_last_insert_rowid() API function.
82048
 
*/
82049
 
static void last_insert_rowid(
82050
 
  sqlite3_context *context, 
82051
 
  int NotUsed, 
82052
 
  sqlite3_value **NotUsed2
82053
 
){
82054
 
  sqlite3 *db = sqlite3_context_db_handle(context);
82055
 
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
82056
 
  /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
82057
 
  ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
82058
 
  ** function. */
82059
 
  sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
82060
 
}
82061
 
 
82062
 
/*
82063
 
** Implementation of the changes() SQL function.
82064
 
**
82065
 
** IMP: R-62073-11209 The changes() SQL function is a wrapper
82066
 
** around the sqlite3_changes() C/C++ function and hence follows the same
82067
 
** rules for counting changes.
82068
 
*/
82069
 
static void changes(
82070
 
  sqlite3_context *context,
82071
 
  int NotUsed,
82072
 
  sqlite3_value **NotUsed2
82073
 
){
82074
 
  sqlite3 *db = sqlite3_context_db_handle(context);
82075
 
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
82076
 
  sqlite3_result_int(context, sqlite3_changes(db));
82077
 
}
82078
 
 
82079
 
/*
82080
 
** Implementation of the total_changes() SQL function.  The return value is
82081
 
** the same as the sqlite3_total_changes() API function.
82082
 
*/
82083
 
static void total_changes(
82084
 
  sqlite3_context *context,
82085
 
  int NotUsed,
82086
 
  sqlite3_value **NotUsed2
82087
 
){
82088
 
  sqlite3 *db = sqlite3_context_db_handle(context);
82089
 
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
82090
 
  /* IMP: R-52756-41993 This function is a wrapper around the
82091
 
  ** sqlite3_total_changes() C/C++ interface. */
82092
 
  sqlite3_result_int(context, sqlite3_total_changes(db));
82093
 
}
82094
 
 
82095
 
/*
82096
 
** A structure defining how to do GLOB-style comparisons.
82097
 
*/
82098
 
struct compareInfo {
82099
 
  u8 matchAll;
82100
 
  u8 matchOne;
82101
 
  u8 matchSet;
82102
 
  u8 noCase;
82103
 
};
82104
 
 
82105
 
/*
82106
 
** For LIKE and GLOB matching on EBCDIC machines, assume that every
82107
 
** character is exactly one byte in size.  Also, all characters are
82108
 
** able to participate in upper-case-to-lower-case mappings in EBCDIC
82109
 
** whereas only characters less than 0x80 do in ASCII.
82110
 
*/
82111
 
#if defined(SQLITE_EBCDIC)
82112
 
# define sqlite3Utf8Read(A,C)    (*(A++))
82113
 
# define GlogUpperToLower(A)     A = sqlite3UpperToLower[A]
82114
 
#else
82115
 
# define GlogUpperToLower(A)     if( A<0x80 ){ A = sqlite3UpperToLower[A]; }
82116
 
#endif
82117
 
 
82118
 
static const struct compareInfo globInfo = { '*', '?', '[', 0 };
82119
 
/* The correct SQL-92 behavior is for the LIKE operator to ignore
82120
 
** case.  Thus  'a' LIKE 'A' would be true. */
82121
 
static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
82122
 
/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
82123
 
** is case sensitive causing 'a' LIKE 'A' to be false */
82124
 
static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
82125
 
 
82126
 
/*
82127
 
** Compare two UTF-8 strings for equality where the first string can
82128
 
** potentially be a "glob" expression.  Return true (1) if they
82129
 
** are the same and false (0) if they are different.
82130
 
**
82131
 
** Globbing rules:
82132
 
**
82133
 
**      '*'       Matches any sequence of zero or more characters.
82134
 
**
82135
 
**      '?'       Matches exactly one character.
82136
 
**
82137
 
**     [...]      Matches one character from the enclosed list of
82138
 
**                characters.
82139
 
**
82140
 
**     [^...]     Matches one character not in the enclosed list.
82141
 
**
82142
 
** With the [...] and [^...] matching, a ']' character can be included
82143
 
** in the list by making it the first character after '[' or '^'.  A
82144
 
** range of characters can be specified using '-'.  Example:
82145
 
** "[a-z]" matches any single lower-case letter.  To match a '-', make
82146
 
** it the last character in the list.
82147
 
**
82148
 
** This routine is usually quick, but can be N**2 in the worst case.
82149
 
**
82150
 
** Hints: to match '*' or '?', put them in "[]".  Like this:
82151
 
**
82152
 
**         abc[*]xyz        Matches "abc*xyz" only
82153
 
*/
82154
 
static int patternCompare(
82155
 
  const u8 *zPattern,              /* The glob pattern */
82156
 
  const u8 *zString,               /* The string to compare against the glob */
82157
 
  const struct compareInfo *pInfo, /* Information about how to do the compare */
82158
 
  const int esc                    /* The escape character */
82159
 
){
82160
 
  int c, c2;
82161
 
  int invert;
82162
 
  int seen;
82163
 
  u8 matchOne = pInfo->matchOne;
82164
 
  u8 matchAll = pInfo->matchAll;
82165
 
  u8 matchSet = pInfo->matchSet;
82166
 
  u8 noCase = pInfo->noCase; 
82167
 
  int prevEscape = 0;     /* True if the previous character was 'escape' */
82168
 
 
82169
 
  while( (c = sqlite3Utf8Read(zPattern,&zPattern))!=0 ){
82170
 
    if( !prevEscape && c==matchAll ){
82171
 
      while( (c=sqlite3Utf8Read(zPattern,&zPattern)) == matchAll
82172
 
               || c == matchOne ){
82173
 
        if( c==matchOne && sqlite3Utf8Read(zString, &zString)==0 ){
82174
 
          return 0;
82175
 
        }
82176
 
      }
82177
 
      if( c==0 ){
82178
 
        return 1;
82179
 
      }else if( c==esc ){
82180
 
        c = sqlite3Utf8Read(zPattern, &zPattern);
82181
 
        if( c==0 ){
82182
 
          return 0;
82183
 
        }
82184
 
      }else if( c==matchSet ){
82185
 
        assert( esc==0 );         /* This is GLOB, not LIKE */
82186
 
        assert( matchSet<0x80 );  /* '[' is a single-byte character */
82187
 
        while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
82188
 
          SQLITE_SKIP_UTF8(zString);
82189
 
        }
82190
 
        return *zString!=0;
82191
 
      }
82192
 
      while( (c2 = sqlite3Utf8Read(zString,&zString))!=0 ){
82193
 
        if( noCase ){
82194
 
          GlogUpperToLower(c2);
82195
 
          GlogUpperToLower(c);
82196
 
          while( c2 != 0 && c2 != c ){
82197
 
            c2 = sqlite3Utf8Read(zString, &zString);
82198
 
            GlogUpperToLower(c2);
82199
 
          }
82200
 
        }else{
82201
 
          while( c2 != 0 && c2 != c ){
82202
 
            c2 = sqlite3Utf8Read(zString, &zString);
82203
 
          }
82204
 
        }
82205
 
        if( c2==0 ) return 0;
82206
 
        if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
82207
 
      }
82208
 
      return 0;
82209
 
    }else if( !prevEscape && c==matchOne ){
82210
 
      if( sqlite3Utf8Read(zString, &zString)==0 ){
82211
 
        return 0;
82212
 
      }
82213
 
    }else if( c==matchSet ){
82214
 
      int prior_c = 0;
82215
 
      assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
82216
 
      seen = 0;
82217
 
      invert = 0;
82218
 
      c = sqlite3Utf8Read(zString, &zString);
82219
 
      if( c==0 ) return 0;
82220
 
      c2 = sqlite3Utf8Read(zPattern, &zPattern);
82221
 
      if( c2=='^' ){
82222
 
        invert = 1;
82223
 
        c2 = sqlite3Utf8Read(zPattern, &zPattern);
82224
 
      }
82225
 
      if( c2==']' ){
82226
 
        if( c==']' ) seen = 1;
82227
 
        c2 = sqlite3Utf8Read(zPattern, &zPattern);
82228
 
      }
82229
 
      while( c2 && c2!=']' ){
82230
 
        if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
82231
 
          c2 = sqlite3Utf8Read(zPattern, &zPattern);
82232
 
          if( c>=prior_c && c<=c2 ) seen = 1;
82233
 
          prior_c = 0;
82234
 
        }else{
82235
 
          if( c==c2 ){
82236
 
            seen = 1;
82237
 
          }
82238
 
          prior_c = c2;
82239
 
        }
82240
 
        c2 = sqlite3Utf8Read(zPattern, &zPattern);
82241
 
      }
82242
 
      if( c2==0 || (seen ^ invert)==0 ){
82243
 
        return 0;
82244
 
      }
82245
 
    }else if( esc==c && !prevEscape ){
82246
 
      prevEscape = 1;
82247
 
    }else{
82248
 
      c2 = sqlite3Utf8Read(zString, &zString);
82249
 
      if( noCase ){
82250
 
        GlogUpperToLower(c);
82251
 
        GlogUpperToLower(c2);
82252
 
      }
82253
 
      if( c!=c2 ){
82254
 
        return 0;
82255
 
      }
82256
 
      prevEscape = 0;
82257
 
    }
82258
 
  }
82259
 
  return *zString==0;
82260
 
}
82261
 
 
82262
 
/*
82263
 
** Count the number of times that the LIKE operator (or GLOB which is
82264
 
** just a variation of LIKE) gets called.  This is used for testing
82265
 
** only.
82266
 
*/
82267
 
#ifdef SQLITE_TEST
82268
 
SQLITE_API int sqlite3_like_count = 0;
82269
 
#endif
82270
 
 
82271
 
 
82272
 
/*
82273
 
** Implementation of the like() SQL function.  This function implements
82274
 
** the build-in LIKE operator.  The first argument to the function is the
82275
 
** pattern and the second argument is the string.  So, the SQL statements:
82276
 
**
82277
 
**       A LIKE B
82278
 
**
82279
 
** is implemented as like(B,A).
82280
 
**
82281
 
** This same function (with a different compareInfo structure) computes
82282
 
** the GLOB operator.
82283
 
*/
82284
 
static void likeFunc(
82285
 
  sqlite3_context *context, 
82286
 
  int argc, 
82287
 
  sqlite3_value **argv
82288
 
){
82289
 
  const unsigned char *zA, *zB;
82290
 
  int escape = 0;
82291
 
  int nPat;
82292
 
  sqlite3 *db = sqlite3_context_db_handle(context);
82293
 
 
82294
 
  zB = sqlite3_value_text(argv[0]);
82295
 
  zA = sqlite3_value_text(argv[1]);
82296
 
 
82297
 
  /* Limit the length of the LIKE or GLOB pattern to avoid problems
82298
 
  ** of deep recursion and N*N behavior in patternCompare().
82299
 
  */
82300
 
  nPat = sqlite3_value_bytes(argv[0]);
82301
 
  testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
82302
 
  testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
82303
 
  if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
82304
 
    sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
82305
 
    return;
82306
 
  }
82307
 
  assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */
82308
 
 
82309
 
  if( argc==3 ){
82310
 
    /* The escape character string must consist of a single UTF-8 character.
82311
 
    ** Otherwise, return an error.
82312
 
    */
82313
 
    const unsigned char *zEsc = sqlite3_value_text(argv[2]);
82314
 
    if( zEsc==0 ) return;
82315
 
    if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
82316
 
      sqlite3_result_error(context, 
82317
 
          "ESCAPE expression must be a single character", -1);
82318
 
      return;
82319
 
    }
82320
 
    escape = sqlite3Utf8Read(zEsc, &zEsc);
82321
 
  }
82322
 
  if( zA && zB ){
82323
 
    struct compareInfo *pInfo = sqlite3_user_data(context);
82324
 
#ifdef SQLITE_TEST
82325
 
    sqlite3_like_count++;
82326
 
#endif
82327
 
    
82328
 
    sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
82329
 
  }
82330
 
}
82331
 
 
82332
 
/*
82333
 
** Implementation of the NULLIF(x,y) function.  The result is the first
82334
 
** argument if the arguments are different.  The result is NULL if the
82335
 
** arguments are equal to each other.
82336
 
*/
82337
 
static void nullifFunc(
82338
 
  sqlite3_context *context,
82339
 
  int NotUsed,
82340
 
  sqlite3_value **argv
82341
 
){
82342
 
  CollSeq *pColl = sqlite3GetFuncCollSeq(context);
82343
 
  UNUSED_PARAMETER(NotUsed);
82344
 
  if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
82345
 
    sqlite3_result_value(context, argv[0]);
82346
 
  }
82347
 
}
82348
 
 
82349
 
/*
82350
 
** Implementation of the sqlite_version() function.  The result is the version
82351
 
** of the SQLite library that is running.
82352
 
*/
82353
 
static void versionFunc(
82354
 
  sqlite3_context *context,
82355
 
  int NotUsed,
82356
 
  sqlite3_value **NotUsed2
82357
 
){
82358
 
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
82359
 
  /* IMP: R-48699-48617 This function is an SQL wrapper around the
82360
 
  ** sqlite3_libversion() C-interface. */
82361
 
  sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
82362
 
}
82363
 
 
82364
 
/*
82365
 
** Implementation of the sqlite_source_id() function. The result is a string
82366
 
** that identifies the particular version of the source code used to build
82367
 
** SQLite.
82368
 
*/
82369
 
static void sourceidFunc(
82370
 
  sqlite3_context *context,
82371
 
  int NotUsed,
82372
 
  sqlite3_value **NotUsed2
82373
 
){
82374
 
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
82375
 
  /* IMP: R-24470-31136 This function is an SQL wrapper around the
82376
 
  ** sqlite3_sourceid() C interface. */
82377
 
  sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
82378
 
}
82379
 
 
82380
 
/*
82381
 
** Implementation of the sqlite_compileoption_used() function.
82382
 
** The result is an integer that identifies if the compiler option
82383
 
** was used to build SQLite.
82384
 
*/
82385
 
#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
82386
 
static void compileoptionusedFunc(
82387
 
  sqlite3_context *context,
82388
 
  int argc,
82389
 
  sqlite3_value **argv
82390
 
){
82391
 
  const char *zOptName;
82392
 
  assert( argc==1 );
82393
 
  UNUSED_PARAMETER(argc);
82394
 
  /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
82395
 
  ** function is a wrapper around the sqlite3_compileoption_used() C/C++
82396
 
  ** function.
82397
 
  */
82398
 
  if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
82399
 
    sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
82400
 
  }
82401
 
}
82402
 
#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
82403
 
 
82404
 
/*
82405
 
** Implementation of the sqlite_compileoption_get() function. 
82406
 
** The result is a string that identifies the compiler options 
82407
 
** used to build SQLite.
82408
 
*/
82409
 
#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
82410
 
static void compileoptiongetFunc(
82411
 
  sqlite3_context *context,
82412
 
  int argc,
82413
 
  sqlite3_value **argv
82414
 
){
82415
 
  int n;
82416
 
  assert( argc==1 );
82417
 
  UNUSED_PARAMETER(argc);
82418
 
  /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
82419
 
  ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
82420
 
  */
82421
 
  n = sqlite3_value_int(argv[0]);
82422
 
  sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
82423
 
}
82424
 
#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
82425
 
 
82426
 
/* Array for converting from half-bytes (nybbles) into ASCII hex
82427
 
** digits. */
82428
 
static const char hexdigits[] = {
82429
 
  '0', '1', '2', '3', '4', '5', '6', '7',
82430
 
  '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 
82431
 
};
82432
 
 
82433
 
/*
82434
 
** EXPERIMENTAL - This is not an official function.  The interface may
82435
 
** change.  This function may disappear.  Do not write code that depends
82436
 
** on this function.
82437
 
**
82438
 
** Implementation of the QUOTE() function.  This function takes a single
82439
 
** argument.  If the argument is numeric, the return value is the same as
82440
 
** the argument.  If the argument is NULL, the return value is the string
82441
 
** "NULL".  Otherwise, the argument is enclosed in single quotes with
82442
 
** single-quote escapes.
82443
 
*/
82444
 
static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
82445
 
  assert( argc==1 );
82446
 
  UNUSED_PARAMETER(argc);
82447
 
  switch( sqlite3_value_type(argv[0]) ){
82448
 
    case SQLITE_INTEGER:
82449
 
    case SQLITE_FLOAT: {
82450
 
      sqlite3_result_value(context, argv[0]);
82451
 
      break;
82452
 
    }
82453
 
    case SQLITE_BLOB: {
82454
 
      char *zText = 0;
82455
 
      char const *zBlob = sqlite3_value_blob(argv[0]);
82456
 
      int nBlob = sqlite3_value_bytes(argv[0]);
82457
 
      assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
82458
 
      zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); 
82459
 
      if( zText ){
82460
 
        int i;
82461
 
        for(i=0; i<nBlob; i++){
82462
 
          zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
82463
 
          zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
82464
 
        }
82465
 
        zText[(nBlob*2)+2] = '\'';
82466
 
        zText[(nBlob*2)+3] = '\0';
82467
 
        zText[0] = 'X';
82468
 
        zText[1] = '\'';
82469
 
        sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
82470
 
        sqlite3_free(zText);
82471
 
      }
82472
 
      break;
82473
 
    }
82474
 
    case SQLITE_TEXT: {
82475
 
      int i,j;
82476
 
      u64 n;
82477
 
      const unsigned char *zArg = sqlite3_value_text(argv[0]);
82478
 
      char *z;
82479
 
 
82480
 
      if( zArg==0 ) return;
82481
 
      for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
82482
 
      z = contextMalloc(context, ((i64)i)+((i64)n)+3);
82483
 
      if( z ){
82484
 
        z[0] = '\'';
82485
 
        for(i=0, j=1; zArg[i]; i++){
82486
 
          z[j++] = zArg[i];
82487
 
          if( zArg[i]=='\'' ){
82488
 
            z[j++] = '\'';
82489
 
          }
82490
 
        }
82491
 
        z[j++] = '\'';
82492
 
        z[j] = 0;
82493
 
        sqlite3_result_text(context, z, j, sqlite3_free);
82494
 
      }
82495
 
      break;
82496
 
    }
82497
 
    default: {
82498
 
      assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
82499
 
      sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
82500
 
      break;
82501
 
    }
82502
 
  }
82503
 
}
82504
 
 
82505
 
/*
82506
 
** The hex() function.  Interpret the argument as a blob.  Return
82507
 
** a hexadecimal rendering as text.
82508
 
*/
82509
 
static void hexFunc(
82510
 
  sqlite3_context *context,
82511
 
  int argc,
82512
 
  sqlite3_value **argv
82513
 
){
82514
 
  int i, n;
82515
 
  const unsigned char *pBlob;
82516
 
  char *zHex, *z;
82517
 
  assert( argc==1 );
82518
 
  UNUSED_PARAMETER(argc);
82519
 
  pBlob = sqlite3_value_blob(argv[0]);
82520
 
  n = sqlite3_value_bytes(argv[0]);
82521
 
  assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */
82522
 
  z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
82523
 
  if( zHex ){
82524
 
    for(i=0; i<n; i++, pBlob++){
82525
 
      unsigned char c = *pBlob;
82526
 
      *(z++) = hexdigits[(c>>4)&0xf];
82527
 
      *(z++) = hexdigits[c&0xf];
82528
 
    }
82529
 
    *z = 0;
82530
 
    sqlite3_result_text(context, zHex, n*2, sqlite3_free);
82531
 
  }
82532
 
}
82533
 
 
82534
 
/*
82535
 
** The zeroblob(N) function returns a zero-filled blob of size N bytes.
82536
 
*/
82537
 
static void zeroblobFunc(
82538
 
  sqlite3_context *context,
82539
 
  int argc,
82540
 
  sqlite3_value **argv
82541
 
){
82542
 
  i64 n;
82543
 
  sqlite3 *db = sqlite3_context_db_handle(context);
82544
 
  assert( argc==1 );
82545
 
  UNUSED_PARAMETER(argc);
82546
 
  n = sqlite3_value_int64(argv[0]);
82547
 
  testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] );
82548
 
  testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
82549
 
  if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
82550
 
    sqlite3_result_error_toobig(context);
82551
 
  }else{
82552
 
    sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */
82553
 
  }
82554
 
}
82555
 
 
82556
 
/*
82557
 
** The replace() function.  Three arguments are all strings: call
82558
 
** them A, B, and C. The result is also a string which is derived
82559
 
** from A by replacing every occurance of B with C.  The match
82560
 
** must be exact.  Collating sequences are not used.
82561
 
*/
82562
 
static void replaceFunc(
82563
 
  sqlite3_context *context,
82564
 
  int argc,
82565
 
  sqlite3_value **argv
82566
 
){
82567
 
  const unsigned char *zStr;        /* The input string A */
82568
 
  const unsigned char *zPattern;    /* The pattern string B */
82569
 
  const unsigned char *zRep;        /* The replacement string C */
82570
 
  unsigned char *zOut;              /* The output */
82571
 
  int nStr;                /* Size of zStr */
82572
 
  int nPattern;            /* Size of zPattern */
82573
 
  int nRep;                /* Size of zRep */
82574
 
  i64 nOut;                /* Maximum size of zOut */
82575
 
  int loopLimit;           /* Last zStr[] that might match zPattern[] */
82576
 
  int i, j;                /* Loop counters */
82577
 
 
82578
 
  assert( argc==3 );
82579
 
  UNUSED_PARAMETER(argc);
82580
 
  zStr = sqlite3_value_text(argv[0]);
82581
 
  if( zStr==0 ) return;
82582
 
  nStr = sqlite3_value_bytes(argv[0]);
82583
 
  assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
82584
 
  zPattern = sqlite3_value_text(argv[1]);
82585
 
  if( zPattern==0 ){
82586
 
    assert( sqlite3_value_type(argv[1])==SQLITE_NULL
82587
 
            || sqlite3_context_db_handle(context)->mallocFailed );
82588
 
    return;
82589
 
  }
82590
 
  if( zPattern[0]==0 ){
82591
 
    assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
82592
 
    sqlite3_result_value(context, argv[0]);
82593
 
    return;
82594
 
  }
82595
 
  nPattern = sqlite3_value_bytes(argv[1]);
82596
 
  assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */
82597
 
  zRep = sqlite3_value_text(argv[2]);
82598
 
  if( zRep==0 ) return;
82599
 
  nRep = sqlite3_value_bytes(argv[2]);
82600
 
  assert( zRep==sqlite3_value_text(argv[2]) );
82601
 
  nOut = nStr + 1;
82602
 
  assert( nOut<SQLITE_MAX_LENGTH );
82603
 
  zOut = contextMalloc(context, (i64)nOut);
82604
 
  if( zOut==0 ){
82605
 
    return;
82606
 
  }
82607
 
  loopLimit = nStr - nPattern;  
82608
 
  for(i=j=0; i<=loopLimit; i++){
82609
 
    if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
82610
 
      zOut[j++] = zStr[i];
82611
 
    }else{
82612
 
      u8 *zOld;
82613
 
      sqlite3 *db = sqlite3_context_db_handle(context);
82614
 
      nOut += nRep - nPattern;
82615
 
      testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
82616
 
      testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
82617
 
      if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
82618
 
        sqlite3_result_error_toobig(context);
82619
 
        sqlite3_free(zOut);
82620
 
        return;
82621
 
      }
82622
 
      zOld = zOut;
82623
 
      zOut = sqlite3_realloc(zOut, (int)nOut);
82624
 
      if( zOut==0 ){
82625
 
        sqlite3_result_error_nomem(context);
82626
 
        sqlite3_free(zOld);
82627
 
        return;
82628
 
      }
82629
 
      memcpy(&zOut[j], zRep, nRep);
82630
 
      j += nRep;
82631
 
      i += nPattern-1;
82632
 
    }
82633
 
  }
82634
 
  assert( j+nStr-i+1==nOut );
82635
 
  memcpy(&zOut[j], &zStr[i], nStr-i);
82636
 
  j += nStr - i;
82637
 
  assert( j<=nOut );
82638
 
  zOut[j] = 0;
82639
 
  sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
82640
 
}
82641
 
 
82642
 
/*
82643
 
** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
82644
 
** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
82645
 
*/
82646
 
static void trimFunc(
82647
 
  sqlite3_context *context,
82648
 
  int argc,
82649
 
  sqlite3_value **argv
82650
 
){
82651
 
  const unsigned char *zIn;         /* Input string */
82652
 
  const unsigned char *zCharSet;    /* Set of characters to trim */
82653
 
  int nIn;                          /* Number of bytes in input */
82654
 
  int flags;                        /* 1: trimleft  2: trimright  3: trim */
82655
 
  int i;                            /* Loop counter */
82656
 
  unsigned char *aLen = 0;          /* Length of each character in zCharSet */
82657
 
  unsigned char **azChar = 0;       /* Individual characters in zCharSet */
82658
 
  int nChar;                        /* Number of characters in zCharSet */
82659
 
 
82660
 
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
82661
 
    return;
82662
 
  }
82663
 
  zIn = sqlite3_value_text(argv[0]);
82664
 
  if( zIn==0 ) return;
82665
 
  nIn = sqlite3_value_bytes(argv[0]);
82666
 
  assert( zIn==sqlite3_value_text(argv[0]) );
82667
 
  if( argc==1 ){
82668
 
    static const unsigned char lenOne[] = { 1 };
82669
 
    static unsigned char * const azOne[] = { (u8*)" " };
82670
 
    nChar = 1;
82671
 
    aLen = (u8*)lenOne;
82672
 
    azChar = (unsigned char **)azOne;
82673
 
    zCharSet = 0;
82674
 
  }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
82675
 
    return;
82676
 
  }else{
82677
 
    const unsigned char *z;
82678
 
    for(z=zCharSet, nChar=0; *z; nChar++){
82679
 
      SQLITE_SKIP_UTF8(z);
82680
 
    }
82681
 
    if( nChar>0 ){
82682
 
      azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
82683
 
      if( azChar==0 ){
82684
 
        return;
82685
 
      }
82686
 
      aLen = (unsigned char*)&azChar[nChar];
82687
 
      for(z=zCharSet, nChar=0; *z; nChar++){
82688
 
        azChar[nChar] = (unsigned char *)z;
82689
 
        SQLITE_SKIP_UTF8(z);
82690
 
        aLen[nChar] = (u8)(z - azChar[nChar]);
82691
 
      }
82692
 
    }
82693
 
  }
82694
 
  if( nChar>0 ){
82695
 
    flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
82696
 
    if( flags & 1 ){
82697
 
      while( nIn>0 ){
82698
 
        int len = 0;
82699
 
        for(i=0; i<nChar; i++){
82700
 
          len = aLen[i];
82701
 
          if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
82702
 
        }
82703
 
        if( i>=nChar ) break;
82704
 
        zIn += len;
82705
 
        nIn -= len;
82706
 
      }
82707
 
    }
82708
 
    if( flags & 2 ){
82709
 
      while( nIn>0 ){
82710
 
        int len = 0;
82711
 
        for(i=0; i<nChar; i++){
82712
 
          len = aLen[i];
82713
 
          if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
82714
 
        }
82715
 
        if( i>=nChar ) break;
82716
 
        nIn -= len;
82717
 
      }
82718
 
    }
82719
 
    if( zCharSet ){
82720
 
      sqlite3_free(azChar);
82721
 
    }
82722
 
  }
82723
 
  sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
82724
 
}
82725
 
 
82726
 
 
82727
 
/* IMP: R-25361-16150 This function is omitted from SQLite by default. It
82728
 
** is only available if the SQLITE_SOUNDEX compile-time option is used
82729
 
** when SQLite is built.
82730
 
*/
82731
 
#ifdef SQLITE_SOUNDEX
82732
 
/*
82733
 
** Compute the soundex encoding of a word.
82734
 
**
82735
 
** IMP: R-59782-00072 The soundex(X) function returns a string that is the
82736
 
** soundex encoding of the string X. 
82737
 
*/
82738
 
static void soundexFunc(
82739
 
  sqlite3_context *context,
82740
 
  int argc,
82741
 
  sqlite3_value **argv
82742
 
){
82743
 
  char zResult[8];
82744
 
  const u8 *zIn;
82745
 
  int i, j;
82746
 
  static const unsigned char iCode[] = {
82747
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82748
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82749
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82750
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82751
 
    0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
82752
 
    1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
82753
 
    0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
82754
 
    1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
82755
 
  };
82756
 
  assert( argc==1 );
82757
 
  zIn = (u8*)sqlite3_value_text(argv[0]);
82758
 
  if( zIn==0 ) zIn = (u8*)"";
82759
 
  for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
82760
 
  if( zIn[i] ){
82761
 
    u8 prevcode = iCode[zIn[i]&0x7f];
82762
 
    zResult[0] = sqlite3Toupper(zIn[i]);
82763
 
    for(j=1; j<4 && zIn[i]; i++){
82764
 
      int code = iCode[zIn[i]&0x7f];
82765
 
      if( code>0 ){
82766
 
        if( code!=prevcode ){
82767
 
          prevcode = code;
82768
 
          zResult[j++] = code + '0';
82769
 
        }
82770
 
      }else{
82771
 
        prevcode = 0;
82772
 
      }
82773
 
    }
82774
 
    while( j<4 ){
82775
 
      zResult[j++] = '0';
82776
 
    }
82777
 
    zResult[j] = 0;
82778
 
    sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
82779
 
  }else{
82780
 
    /* IMP: R-64894-50321 The string "?000" is returned if the argument
82781
 
    ** is NULL or contains no ASCII alphabetic characters. */
82782
 
    sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
82783
 
  }
82784
 
}
82785
 
#endif /* SQLITE_SOUNDEX */
82786
 
 
82787
 
#ifndef SQLITE_OMIT_LOAD_EXTENSION
82788
 
/*
82789
 
** A function that loads a shared-library extension then returns NULL.
82790
 
*/
82791
 
static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
82792
 
  const char *zFile = (const char *)sqlite3_value_text(argv[0]);
82793
 
  const char *zProc;
82794
 
  sqlite3 *db = sqlite3_context_db_handle(context);
82795
 
  char *zErrMsg = 0;
82796
 
 
82797
 
  if( argc==2 ){
82798
 
    zProc = (const char *)sqlite3_value_text(argv[1]);
82799
 
  }else{
82800
 
    zProc = 0;
82801
 
  }
82802
 
  if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
82803
 
    sqlite3_result_error(context, zErrMsg, -1);
82804
 
    sqlite3_free(zErrMsg);
82805
 
  }
82806
 
}
82807
 
#endif
82808
 
 
82809
 
 
82810
 
/*
82811
 
** An instance of the following structure holds the context of a
82812
 
** sum() or avg() aggregate computation.
82813
 
*/
82814
 
typedef struct SumCtx SumCtx;
82815
 
struct SumCtx {
82816
 
  double rSum;      /* Floating point sum */
82817
 
  i64 iSum;         /* Integer sum */   
82818
 
  i64 cnt;          /* Number of elements summed */
82819
 
  u8 overflow;      /* True if integer overflow seen */
82820
 
  u8 approx;        /* True if non-integer value was input to the sum */
82821
 
};
82822
 
 
82823
 
/*
82824
 
** Routines used to compute the sum, average, and total.
82825
 
**
82826
 
** The SUM() function follows the (broken) SQL standard which means
82827
 
** that it returns NULL if it sums over no inputs.  TOTAL returns
82828
 
** 0.0 in that case.  In addition, TOTAL always returns a float where
82829
 
** SUM might return an integer if it never encounters a floating point
82830
 
** value.  TOTAL never fails, but SUM might through an exception if
82831
 
** it overflows an integer.
82832
 
*/
82833
 
static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
82834
 
  SumCtx *p;
82835
 
  int type;
82836
 
  assert( argc==1 );
82837
 
  UNUSED_PARAMETER(argc);
82838
 
  p = sqlite3_aggregate_context(context, sizeof(*p));
82839
 
  type = sqlite3_value_numeric_type(argv[0]);
82840
 
  if( p && type!=SQLITE_NULL ){
82841
 
    p->cnt++;
82842
 
    if( type==SQLITE_INTEGER ){
82843
 
      i64 v = sqlite3_value_int64(argv[0]);
82844
 
      p->rSum += v;
82845
 
      if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
82846
 
        p->overflow = 1;
82847
 
      }
82848
 
    }else{
82849
 
      p->rSum += sqlite3_value_double(argv[0]);
82850
 
      p->approx = 1;
82851
 
    }
82852
 
  }
82853
 
}
82854
 
static void sumFinalize(sqlite3_context *context){
82855
 
  SumCtx *p;
82856
 
  p = sqlite3_aggregate_context(context, 0);
82857
 
  if( p && p->cnt>0 ){
82858
 
    if( p->overflow ){
82859
 
      sqlite3_result_error(context,"integer overflow",-1);
82860
 
    }else if( p->approx ){
82861
 
      sqlite3_result_double(context, p->rSum);
82862
 
    }else{
82863
 
      sqlite3_result_int64(context, p->iSum);
82864
 
    }
82865
 
  }
82866
 
}
82867
 
static void avgFinalize(sqlite3_context *context){
82868
 
  SumCtx *p;
82869
 
  p = sqlite3_aggregate_context(context, 0);
82870
 
  if( p && p->cnt>0 ){
82871
 
    sqlite3_result_double(context, p->rSum/(double)p->cnt);
82872
 
  }
82873
 
}
82874
 
static void totalFinalize(sqlite3_context *context){
82875
 
  SumCtx *p;
82876
 
  p = sqlite3_aggregate_context(context, 0);
82877
 
  /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
82878
 
  sqlite3_result_double(context, p ? p->rSum : (double)0);
82879
 
}
82880
 
 
82881
 
/*
82882
 
** The following structure keeps track of state information for the
82883
 
** count() aggregate function.
82884
 
*/
82885
 
typedef struct CountCtx CountCtx;
82886
 
struct CountCtx {
82887
 
  i64 n;
82888
 
};
82889
 
 
82890
 
/*
82891
 
** Routines to implement the count() aggregate function.
82892
 
*/
82893
 
static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
82894
 
  CountCtx *p;
82895
 
  p = sqlite3_aggregate_context(context, sizeof(*p));
82896
 
  if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
82897
 
    p->n++;
82898
 
  }
82899
 
 
82900
 
#ifndef SQLITE_OMIT_DEPRECATED
82901
 
  /* The sqlite3_aggregate_count() function is deprecated.  But just to make
82902
 
  ** sure it still operates correctly, verify that its count agrees with our 
82903
 
  ** internal count when using count(*) and when the total count can be
82904
 
  ** expressed as a 32-bit integer. */
82905
 
  assert( argc==1 || p==0 || p->n>0x7fffffff
82906
 
          || p->n==sqlite3_aggregate_count(context) );
82907
 
#endif
82908
 
}   
82909
 
static void countFinalize(sqlite3_context *context){
82910
 
  CountCtx *p;
82911
 
  p = sqlite3_aggregate_context(context, 0);
82912
 
  sqlite3_result_int64(context, p ? p->n : 0);
82913
 
}
82914
 
 
82915
 
/*
82916
 
** Routines to implement min() and max() aggregate functions.
82917
 
*/
82918
 
static void minmaxStep(
82919
 
  sqlite3_context *context, 
82920
 
  int NotUsed, 
82921
 
  sqlite3_value **argv
82922
 
){
82923
 
  Mem *pArg  = (Mem *)argv[0];
82924
 
  Mem *pBest;
82925
 
  UNUSED_PARAMETER(NotUsed);
82926
 
 
82927
 
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
82928
 
  pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
82929
 
  if( !pBest ) return;
82930
 
 
82931
 
  if( pBest->flags ){
82932
 
    int max;
82933
 
    int cmp;
82934
 
    CollSeq *pColl = sqlite3GetFuncCollSeq(context);
82935
 
    /* This step function is used for both the min() and max() aggregates,
82936
 
    ** the only difference between the two being that the sense of the
82937
 
    ** comparison is inverted. For the max() aggregate, the
82938
 
    ** sqlite3_user_data() function returns (void *)-1. For min() it
82939
 
    ** returns (void *)db, where db is the sqlite3* database pointer.
82940
 
    ** Therefore the next statement sets variable 'max' to 1 for the max()
82941
 
    ** aggregate, or 0 for min().
82942
 
    */
82943
 
    max = sqlite3_user_data(context)!=0;
82944
 
    cmp = sqlite3MemCompare(pBest, pArg, pColl);
82945
 
    if( (max && cmp<0) || (!max && cmp>0) ){
82946
 
      sqlite3VdbeMemCopy(pBest, pArg);
82947
 
    }
82948
 
  }else{
82949
 
    sqlite3VdbeMemCopy(pBest, pArg);
82950
 
  }
82951
 
}
82952
 
static void minMaxFinalize(sqlite3_context *context){
82953
 
  sqlite3_value *pRes;
82954
 
  pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
82955
 
  if( pRes ){
82956
 
    if( ALWAYS(pRes->flags) ){
82957
 
      sqlite3_result_value(context, pRes);
82958
 
    }
82959
 
    sqlite3VdbeMemRelease(pRes);
82960
 
  }
82961
 
}
82962
 
 
82963
 
/*
82964
 
** group_concat(EXPR, ?SEPARATOR?)
82965
 
*/
82966
 
static void groupConcatStep(
82967
 
  sqlite3_context *context,
82968
 
  int argc,
82969
 
  sqlite3_value **argv
82970
 
){
82971
 
  const char *zVal;
82972
 
  StrAccum *pAccum;
82973
 
  const char *zSep;
82974
 
  int nVal, nSep;
82975
 
  assert( argc==1 || argc==2 );
82976
 
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
82977
 
  pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
82978
 
 
82979
 
  if( pAccum ){
82980
 
    sqlite3 *db = sqlite3_context_db_handle(context);
82981
 
    int firstTerm = pAccum->useMalloc==0;
82982
 
    pAccum->useMalloc = 2;
82983
 
    pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
82984
 
    if( !firstTerm ){
82985
 
      if( argc==2 ){
82986
 
        zSep = (char*)sqlite3_value_text(argv[1]);
82987
 
        nSep = sqlite3_value_bytes(argv[1]);
82988
 
      }else{
82989
 
        zSep = ",";
82990
 
        nSep = 1;
82991
 
      }
82992
 
      sqlite3StrAccumAppend(pAccum, zSep, nSep);
82993
 
    }
82994
 
    zVal = (char*)sqlite3_value_text(argv[0]);
82995
 
    nVal = sqlite3_value_bytes(argv[0]);
82996
 
    sqlite3StrAccumAppend(pAccum, zVal, nVal);
82997
 
  }
82998
 
}
82999
 
static void groupConcatFinalize(sqlite3_context *context){
83000
 
  StrAccum *pAccum;
83001
 
  pAccum = sqlite3_aggregate_context(context, 0);
83002
 
  if( pAccum ){
83003
 
    if( pAccum->tooBig ){
83004
 
      sqlite3_result_error_toobig(context);
83005
 
    }else if( pAccum->mallocFailed ){
83006
 
      sqlite3_result_error_nomem(context);
83007
 
    }else{    
83008
 
      sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 
83009
 
                          sqlite3_free);
83010
 
    }
83011
 
  }
83012
 
}
83013
 
 
83014
 
/*
83015
 
** This routine does per-connection function registration.  Most
83016
 
** of the built-in functions above are part of the global function set.
83017
 
** This routine only deals with those that are not global.
83018
 
*/
83019
 
SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
83020
 
  int rc = sqlite3_overload_function(db, "MATCH", 2);
83021
 
  assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
83022
 
  if( rc==SQLITE_NOMEM ){
83023
 
    db->mallocFailed = 1;
83024
 
  }
83025
 
}
83026
 
 
83027
 
/*
83028
 
** Set the LIKEOPT flag on the 2-argument function with the given name.
83029
 
*/
83030
 
static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
83031
 
  FuncDef *pDef;
83032
 
  pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName),
83033
 
                             2, SQLITE_UTF8, 0);
83034
 
  if( ALWAYS(pDef) ){
83035
 
    pDef->flags = flagVal;
83036
 
  }
83037
 
}
83038
 
 
83039
 
/*
83040
 
** Register the built-in LIKE and GLOB functions.  The caseSensitive
83041
 
** parameter determines whether or not the LIKE operator is case
83042
 
** sensitive.  GLOB is always case sensitive.
83043
 
*/
83044
 
SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
83045
 
  struct compareInfo *pInfo;
83046
 
  if( caseSensitive ){
83047
 
    pInfo = (struct compareInfo*)&likeInfoAlt;
83048
 
  }else{
83049
 
    pInfo = (struct compareInfo*)&likeInfoNorm;
83050
 
  }
83051
 
  sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
83052
 
  sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
83053
 
  sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 
83054
 
      (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
83055
 
  setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
83056
 
  setLikeOptFlag(db, "like", 
83057
 
      caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
83058
 
}
83059
 
 
83060
 
/*
83061
 
** pExpr points to an expression which implements a function.  If
83062
 
** it is appropriate to apply the LIKE optimization to that function
83063
 
** then set aWc[0] through aWc[2] to the wildcard characters and
83064
 
** return TRUE.  If the function is not a LIKE-style function then
83065
 
** return FALSE.
83066
 
*/
83067
 
SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
83068
 
  FuncDef *pDef;
83069
 
  if( pExpr->op!=TK_FUNCTION 
83070
 
   || !pExpr->x.pList 
83071
 
   || pExpr->x.pList->nExpr!=2
83072
 
  ){
83073
 
    return 0;
83074
 
  }
83075
 
  assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
83076
 
  pDef = sqlite3FindFunction(db, pExpr->u.zToken, 
83077
 
                             sqlite3Strlen30(pExpr->u.zToken),
83078
 
                             2, SQLITE_UTF8, 0);
83079
 
  if( NEVER(pDef==0) || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
83080
 
    return 0;
83081
 
  }
83082
 
 
83083
 
  /* The memcpy() statement assumes that the wildcard characters are
83084
 
  ** the first three statements in the compareInfo structure.  The
83085
 
  ** asserts() that follow verify that assumption
83086
 
  */
83087
 
  memcpy(aWc, pDef->pUserData, 3);
83088
 
  assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
83089
 
  assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
83090
 
  assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
83091
 
  *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
83092
 
  return 1;
83093
 
}
83094
 
 
83095
 
/*
83096
 
** All all of the FuncDef structures in the aBuiltinFunc[] array above
83097
 
** to the global function hash table.  This occurs at start-time (as
83098
 
** a consequence of calling sqlite3_initialize()).
83099
 
**
83100
 
** After this routine runs
83101
 
*/
83102
 
SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
83103
 
  /*
83104
 
  ** The following array holds FuncDef structures for all of the functions
83105
 
  ** defined in this file.
83106
 
  **
83107
 
  ** The array cannot be constant since changes are made to the
83108
 
  ** FuncDef.pHash elements at start-time.  The elements of this array
83109
 
  ** are read-only after initialization is complete.
83110
 
  */
83111
 
  static SQLITE_WSD FuncDef aBuiltinFunc[] = {
83112
 
    FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
83113
 
    FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
83114
 
    FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
83115
 
    FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
83116
 
    FUNCTION(trim,               1, 3, 0, trimFunc         ),
83117
 
    FUNCTION(trim,               2, 3, 0, trimFunc         ),
83118
 
    FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
83119
 
    FUNCTION(min,                0, 0, 1, 0                ),
83120
 
    AGGREGATE(min,               1, 0, 1, minmaxStep,      minMaxFinalize ),
83121
 
    FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
83122
 
    FUNCTION(max,                0, 1, 1, 0                ),
83123
 
    AGGREGATE(max,               1, 1, 1, minmaxStep,      minMaxFinalize ),
83124
 
    FUNCTION(typeof,             1, 0, 0, typeofFunc       ),
83125
 
    FUNCTION(length,             1, 0, 0, lengthFunc       ),
83126
 
    FUNCTION(substr,             2, 0, 0, substrFunc       ),
83127
 
    FUNCTION(substr,             3, 0, 0, substrFunc       ),
83128
 
    FUNCTION(abs,                1, 0, 0, absFunc          ),
83129
 
#ifndef SQLITE_OMIT_FLOATING_POINT
83130
 
    FUNCTION(round,              1, 0, 0, roundFunc        ),
83131
 
    FUNCTION(round,              2, 0, 0, roundFunc        ),
83132
 
#endif
83133
 
    FUNCTION(upper,              1, 0, 0, upperFunc        ),
83134
 
    FUNCTION(lower,              1, 0, 0, lowerFunc        ),
83135
 
    FUNCTION(coalesce,           1, 0, 0, 0                ),
83136
 
    FUNCTION(coalesce,           0, 0, 0, 0                ),
83137
 
/*  FUNCTION(coalesce,          -1, 0, 0, ifnullFunc       ), */
83138
 
    {-1,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"coalesce",0,0},
83139
 
    FUNCTION(hex,                1, 0, 0, hexFunc          ),
83140
 
/*  FUNCTION(ifnull,             2, 0, 0, ifnullFunc       ), */
83141
 
    {2,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"ifnull",0,0},
83142
 
    FUNCTION(random,             0, 0, 0, randomFunc       ),
83143
 
    FUNCTION(randomblob,         1, 0, 0, randomBlob       ),
83144
 
    FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
83145
 
    FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
83146
 
    FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
83147
 
#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
83148
 
    FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
83149
 
    FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
83150
 
#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
83151
 
    FUNCTION(quote,              1, 0, 0, quoteFunc        ),
83152
 
    FUNCTION(last_insert_rowid,  0, 0, 0, last_insert_rowid),
83153
 
    FUNCTION(changes,            0, 0, 0, changes          ),
83154
 
    FUNCTION(total_changes,      0, 0, 0, total_changes    ),
83155
 
    FUNCTION(replace,            3, 0, 0, replaceFunc      ),
83156
 
    FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
83157
 
  #ifdef SQLITE_SOUNDEX
83158
 
    FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
83159
 
  #endif
83160
 
  #ifndef SQLITE_OMIT_LOAD_EXTENSION
83161
 
    FUNCTION(load_extension,     1, 0, 0, loadExt          ),
83162
 
    FUNCTION(load_extension,     2, 0, 0, loadExt          ),
83163
 
  #endif
83164
 
    AGGREGATE(sum,               1, 0, 0, sumStep,         sumFinalize    ),
83165
 
    AGGREGATE(total,             1, 0, 0, sumStep,         totalFinalize    ),
83166
 
    AGGREGATE(avg,               1, 0, 0, sumStep,         avgFinalize    ),
83167
 
 /* AGGREGATE(count,             0, 0, 0, countStep,       countFinalize  ), */
83168
 
    {0,SQLITE_UTF8,SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0},
83169
 
    AGGREGATE(count,             1, 0, 0, countStep,       countFinalize  ),
83170
 
    AGGREGATE(group_concat,      1, 0, 0, groupConcatStep, groupConcatFinalize),
83171
 
    AGGREGATE(group_concat,      2, 0, 0, groupConcatStep, groupConcatFinalize),
83172
 
  
83173
 
    LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
83174
 
  #ifdef SQLITE_CASE_SENSITIVE_LIKE
83175
 
    LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
83176
 
    LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
83177
 
  #else
83178
 
    LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
83179
 
    LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
83180
 
  #endif
83181
 
  };
83182
 
 
83183
 
  int i;
83184
 
  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
83185
 
  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
83186
 
 
83187
 
  for(i=0; i<ArraySize(aBuiltinFunc); i++){
83188
 
    sqlite3FuncDefInsert(pHash, &aFunc[i]);
83189
 
  }
83190
 
  sqlite3RegisterDateTimeFunctions();
83191
 
#ifndef SQLITE_OMIT_ALTERTABLE
83192
 
  sqlite3AlterFunctions();
83193
 
#endif
83194
 
}
83195
 
 
83196
 
/************** End of func.c ************************************************/
83197
 
/************** Begin file fkey.c ********************************************/
83198
 
/*
83199
 
**
83200
 
** The author disclaims copyright to this source code.  In place of
83201
 
** a legal notice, here is a blessing:
83202
 
**
83203
 
**    May you do good and not evil.
83204
 
**    May you find forgiveness for yourself and forgive others.
83205
 
**    May you share freely, never taking more than you give.
83206
 
**
83207
 
*************************************************************************
83208
 
** This file contains code used by the compiler to add foreign key
83209
 
** support to compiled SQL statements.
83210
 
*/
83211
 
 
83212
 
#ifndef SQLITE_OMIT_FOREIGN_KEY
83213
 
#ifndef SQLITE_OMIT_TRIGGER
83214
 
 
83215
 
/*
83216
 
** Deferred and Immediate FKs
83217
 
** --------------------------
83218
 
**
83219
 
** Foreign keys in SQLite come in two flavours: deferred and immediate.
83220
 
** If an immediate foreign key constraint is violated, SQLITE_CONSTRAINT
83221
 
** is returned and the current statement transaction rolled back. If a 
83222
 
** deferred foreign key constraint is violated, no action is taken 
83223
 
** immediately. However if the application attempts to commit the 
83224
 
** transaction before fixing the constraint violation, the attempt fails.
83225
 
**
83226
 
** Deferred constraints are implemented using a simple counter associated
83227
 
** with the database handle. The counter is set to zero each time a 
83228
 
** database transaction is opened. Each time a statement is executed 
83229
 
** that causes a foreign key violation, the counter is incremented. Each
83230
 
** time a statement is executed that removes an existing violation from
83231
 
** the database, the counter is decremented. When the transaction is
83232
 
** committed, the commit fails if the current value of the counter is
83233
 
** greater than zero. This scheme has two big drawbacks:
83234
 
**
83235
 
**   * When a commit fails due to a deferred foreign key constraint, 
83236
 
**     there is no way to tell which foreign constraint is not satisfied,
83237
 
**     or which row it is not satisfied for.
83238
 
**
83239
 
**   * If the database contains foreign key violations when the 
83240
 
**     transaction is opened, this may cause the mechanism to malfunction.
83241
 
**
83242
 
** Despite these problems, this approach is adopted as it seems simpler
83243
 
** than the alternatives.
83244
 
**
83245
 
** INSERT operations:
83246
 
**
83247
 
**   I.1) For each FK for which the table is the child table, search
83248
 
**        the parent table for a match. If none is found increment the
83249
 
**        constraint counter.
83250
 
**
83251
 
**   I.2) For each FK for which the table is the parent table, 
83252
 
**        search the child table for rows that correspond to the new
83253
 
**        row in the parent table. Decrement the counter for each row
83254
 
**        found (as the constraint is now satisfied).
83255
 
**
83256
 
** DELETE operations:
83257
 
**
83258
 
**   D.1) For each FK for which the table is the child table, 
83259
 
**        search the parent table for a row that corresponds to the 
83260
 
**        deleted row in the child table. If such a row is not found, 
83261
 
**        decrement the counter.
83262
 
**
83263
 
**   D.2) For each FK for which the table is the parent table, search 
83264
 
**        the child table for rows that correspond to the deleted row 
83265
 
**        in the parent table. For each found increment the counter.
83266
 
**
83267
 
** UPDATE operations:
83268
 
**
83269
 
**   An UPDATE command requires that all 4 steps above are taken, but only
83270
 
**   for FK constraints for which the affected columns are actually 
83271
 
**   modified (values must be compared at runtime).
83272
 
**
83273
 
** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
83274
 
** This simplifies the implementation a bit.
83275
 
**
83276
 
** For the purposes of immediate FK constraints, the OR REPLACE conflict
83277
 
** resolution is considered to delete rows before the new row is inserted.
83278
 
** If a delete caused by OR REPLACE violates an FK constraint, an exception
83279
 
** is thrown, even if the FK constraint would be satisfied after the new 
83280
 
** row is inserted.
83281
 
**
83282
 
** Immediate constraints are usually handled similarly. The only difference 
83283
 
** is that the counter used is stored as part of each individual statement
83284
 
** object (struct Vdbe). If, after the statement has run, its immediate
83285
 
** constraint counter is greater than zero, it returns SQLITE_CONSTRAINT
83286
 
** and the statement transaction is rolled back. An exception is an INSERT
83287
 
** statement that inserts a single row only (no triggers). In this case,
83288
 
** instead of using a counter, an exception is thrown immediately if the
83289
 
** INSERT violates a foreign key constraint. This is necessary as such
83290
 
** an INSERT does not open a statement transaction.
83291
 
**
83292
 
** TODO: How should dropping a table be handled? How should renaming a 
83293
 
** table be handled?
83294
 
**
83295
 
**
83296
 
** Query API Notes
83297
 
** ---------------
83298
 
**
83299
 
** Before coding an UPDATE or DELETE row operation, the code-generator
83300
 
** for those two operations needs to know whether or not the operation
83301
 
** requires any FK processing and, if so, which columns of the original
83302
 
** row are required by the FK processing VDBE code (i.e. if FKs were
83303
 
** implemented using triggers, which of the old.* columns would be 
83304
 
** accessed). No information is required by the code-generator before
83305
 
** coding an INSERT operation. The functions used by the UPDATE/DELETE
83306
 
** generation code to query for this information are:
83307
 
**
83308
 
**   sqlite3FkRequired() - Test to see if FK processing is required.
83309
 
**   sqlite3FkOldmask()  - Query for the set of required old.* columns.
83310
 
**
83311
 
**
83312
 
** Externally accessible module functions
83313
 
** --------------------------------------
83314
 
**
83315
 
**   sqlite3FkCheck()    - Check for foreign key violations.
83316
 
**   sqlite3FkActions()  - Code triggers for ON UPDATE/ON DELETE actions.
83317
 
**   sqlite3FkDelete()   - Delete an FKey structure.
83318
 
*/
83319
 
 
83320
 
/*
83321
 
** VDBE Calling Convention
83322
 
** -----------------------
83323
 
**
83324
 
** Example:
83325
 
**
83326
 
**   For the following INSERT statement:
83327
 
**
83328
 
**     CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
83329
 
**     INSERT INTO t1 VALUES(1, 2, 3.1);
83330
 
**
83331
 
**   Register (x):        2    (type integer)
83332
 
**   Register (x+1):      1    (type integer)
83333
 
**   Register (x+2):      NULL (type NULL)
83334
 
**   Register (x+3):      3.1  (type real)
83335
 
*/
83336
 
 
83337
 
/*
83338
 
** A foreign key constraint requires that the key columns in the parent
83339
 
** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
83340
 
** Given that pParent is the parent table for foreign key constraint pFKey, 
83341
 
** search the schema a unique index on the parent key columns. 
83342
 
**
83343
 
** If successful, zero is returned. If the parent key is an INTEGER PRIMARY 
83344
 
** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx 
83345
 
** is set to point to the unique index. 
83346
 
** 
83347
 
** If the parent key consists of a single column (the foreign key constraint
83348
 
** is not a composite foreign key), output variable *paiCol is set to NULL.
83349
 
** Otherwise, it is set to point to an allocated array of size N, where
83350
 
** N is the number of columns in the parent key. The first element of the
83351
 
** array is the index of the child table column that is mapped by the FK
83352
 
** constraint to the parent table column stored in the left-most column
83353
 
** of index *ppIdx. The second element of the array is the index of the
83354
 
** child table column that corresponds to the second left-most column of
83355
 
** *ppIdx, and so on.
83356
 
**
83357
 
** If the required index cannot be found, either because:
83358
 
**
83359
 
**   1) The named parent key columns do not exist, or
83360
 
**
83361
 
**   2) The named parent key columns do exist, but are not subject to a
83362
 
**      UNIQUE or PRIMARY KEY constraint, or
83363
 
**
83364
 
**   3) No parent key columns were provided explicitly as part of the
83365
 
**      foreign key definition, and the parent table does not have a
83366
 
**      PRIMARY KEY, or
83367
 
**
83368
 
**   4) No parent key columns were provided explicitly as part of the
83369
 
**      foreign key definition, and the PRIMARY KEY of the parent table 
83370
 
**      consists of a a different number of columns to the child key in 
83371
 
**      the child table.
83372
 
**
83373
 
** then non-zero is returned, and a "foreign key mismatch" error loaded
83374
 
** into pParse. If an OOM error occurs, non-zero is returned and the
83375
 
** pParse->db->mallocFailed flag is set.
83376
 
*/
83377
 
static int locateFkeyIndex(
83378
 
  Parse *pParse,                  /* Parse context to store any error in */
83379
 
  Table *pParent,                 /* Parent table of FK constraint pFKey */
83380
 
  FKey *pFKey,                    /* Foreign key to find index for */
83381
 
  Index **ppIdx,                  /* OUT: Unique index on parent table */
83382
 
  int **paiCol                    /* OUT: Map of index columns in pFKey */
83383
 
){
83384
 
  Index *pIdx = 0;                    /* Value to return via *ppIdx */
83385
 
  int *aiCol = 0;                     /* Value to return via *paiCol */
83386
 
  int nCol = pFKey->nCol;             /* Number of columns in parent key */
83387
 
  char *zKey = pFKey->aCol[0].zCol;   /* Name of left-most parent key column */
83388
 
 
83389
 
  /* The caller is responsible for zeroing output parameters. */
83390
 
  assert( ppIdx && *ppIdx==0 );
83391
 
  assert( !paiCol || *paiCol==0 );
83392
 
  assert( pParse );
83393
 
 
83394
 
  /* If this is a non-composite (single column) foreign key, check if it 
83395
 
  ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx 
83396
 
  ** and *paiCol set to zero and return early. 
83397
 
  **
83398
 
  ** Otherwise, for a composite foreign key (more than one column), allocate
83399
 
  ** space for the aiCol array (returned via output parameter *paiCol).
83400
 
  ** Non-composite foreign keys do not require the aiCol array.
83401
 
  */
83402
 
  if( nCol==1 ){
83403
 
    /* The FK maps to the IPK if any of the following are true:
83404
 
    **
83405
 
    **   1) There is an INTEGER PRIMARY KEY column and the FK is implicitly 
83406
 
    **      mapped to the primary key of table pParent, or
83407
 
    **   2) The FK is explicitly mapped to a column declared as INTEGER
83408
 
    **      PRIMARY KEY.
83409
 
    */
83410
 
    if( pParent->iPKey>=0 ){
83411
 
      if( !zKey ) return 0;
83412
 
      if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
83413
 
    }
83414
 
  }else if( paiCol ){
83415
 
    assert( nCol>1 );
83416
 
    aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
83417
 
    if( !aiCol ) return 1;
83418
 
    *paiCol = aiCol;
83419
 
  }
83420
 
 
83421
 
  for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
83422
 
    if( pIdx->nColumn==nCol && pIdx->onError!=OE_None ){ 
83423
 
      /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
83424
 
      ** of columns. If each indexed column corresponds to a foreign key
83425
 
      ** column of pFKey, then this index is a winner.  */
83426
 
 
83427
 
      if( zKey==0 ){
83428
 
        /* If zKey is NULL, then this foreign key is implicitly mapped to 
83429
 
        ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be 
83430
 
        ** identified by the test (Index.autoIndex==2).  */
83431
 
        if( pIdx->autoIndex==2 ){
83432
 
          if( aiCol ){
83433
 
            int i;
83434
 
            for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
83435
 
          }
83436
 
          break;
83437
 
        }
83438
 
      }else{
83439
 
        /* If zKey is non-NULL, then this foreign key was declared to
83440
 
        ** map to an explicit list of columns in table pParent. Check if this
83441
 
        ** index matches those columns. Also, check that the index uses
83442
 
        ** the default collation sequences for each column. */
83443
 
        int i, j;
83444
 
        for(i=0; i<nCol; i++){
83445
 
          int iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
83446
 
          char *zDfltColl;                  /* Def. collation for column */
83447
 
          char *zIdxCol;                    /* Name of indexed column */
83448
 
 
83449
 
          /* If the index uses a collation sequence that is different from
83450
 
          ** the default collation sequence for the column, this index is
83451
 
          ** unusable. Bail out early in this case.  */
83452
 
          zDfltColl = pParent->aCol[iCol].zColl;
83453
 
          if( !zDfltColl ){
83454
 
            zDfltColl = "BINARY";
83455
 
          }
83456
 
          if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
83457
 
 
83458
 
          zIdxCol = pParent->aCol[iCol].zName;
83459
 
          for(j=0; j<nCol; j++){
83460
 
            if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
83461
 
              if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
83462
 
              break;
83463
 
            }
83464
 
          }
83465
 
          if( j==nCol ) break;
83466
 
        }
83467
 
        if( i==nCol ) break;      /* pIdx is usable */
83468
 
      }
83469
 
    }
83470
 
  }
83471
 
 
83472
 
  if( !pIdx ){
83473
 
    if( !pParse->disableTriggers ){
83474
 
      sqlite3ErrorMsg(pParse, "foreign key mismatch");
83475
 
    }
83476
 
    sqlite3DbFree(pParse->db, aiCol);
83477
 
    return 1;
83478
 
  }
83479
 
 
83480
 
  *ppIdx = pIdx;
83481
 
  return 0;
83482
 
}
83483
 
 
83484
 
/*
83485
 
** This function is called when a row is inserted into or deleted from the 
83486
 
** child table of foreign key constraint pFKey. If an SQL UPDATE is executed 
83487
 
** on the child table of pFKey, this function is invoked twice for each row
83488
 
** affected - once to "delete" the old row, and then again to "insert" the
83489
 
** new row.
83490
 
**
83491
 
** Each time it is called, this function generates VDBE code to locate the
83492
 
** row in the parent table that corresponds to the row being inserted into 
83493
 
** or deleted from the child table. If the parent row can be found, no 
83494
 
** special action is taken. Otherwise, if the parent row can *not* be
83495
 
** found in the parent table:
83496
 
**
83497
 
**   Operation | FK type   | Action taken
83498
 
**   --------------------------------------------------------------------------
83499
 
**   INSERT      immediate   Increment the "immediate constraint counter".
83500
 
**
83501
 
**   DELETE      immediate   Decrement the "immediate constraint counter".
83502
 
**
83503
 
**   INSERT      deferred    Increment the "deferred constraint counter".
83504
 
**
83505
 
**   DELETE      deferred    Decrement the "deferred constraint counter".
83506
 
**
83507
 
** These operations are identified in the comment at the top of this file 
83508
 
** (fkey.c) as "I.1" and "D.1".
83509
 
*/
83510
 
static void fkLookupParent(
83511
 
  Parse *pParse,        /* Parse context */
83512
 
  int iDb,              /* Index of database housing pTab */
83513
 
  Table *pTab,          /* Parent table of FK pFKey */
83514
 
  Index *pIdx,          /* Unique index on parent key columns in pTab */
83515
 
  FKey *pFKey,          /* Foreign key constraint */
83516
 
  int *aiCol,           /* Map from parent key columns to child table columns */
83517
 
  int regData,          /* Address of array containing child table row */
83518
 
  int nIncr,            /* Increment constraint counter by this */
83519
 
  int isIgnore          /* If true, pretend pTab contains all NULL values */
83520
 
){
83521
 
  int i;                                    /* Iterator variable */
83522
 
  Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
83523
 
  int iCur = pParse->nTab - 1;              /* Cursor number to use */
83524
 
  int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
83525
 
 
83526
 
  /* If nIncr is less than zero, then check at runtime if there are any
83527
 
  ** outstanding constraints to resolve. If there are not, there is no need
83528
 
  ** to check if deleting this row resolves any outstanding violations.
83529
 
  **
83530
 
  ** Check if any of the key columns in the child table row are NULL. If 
83531
 
  ** any are, then the constraint is considered satisfied. No need to 
83532
 
  ** search for a matching row in the parent table.  */
83533
 
  if( nIncr<0 ){
83534
 
    sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
83535
 
  }
83536
 
  for(i=0; i<pFKey->nCol; i++){
83537
 
    int iReg = aiCol[i] + regData + 1;
83538
 
    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk);
83539
 
  }
83540
 
 
83541
 
  if( isIgnore==0 ){
83542
 
    if( pIdx==0 ){
83543
 
      /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
83544
 
      ** column of the parent table (table pTab).  */
83545
 
      int iMustBeInt;               /* Address of MustBeInt instruction */
83546
 
      int regTemp = sqlite3GetTempReg(pParse);
83547
 
  
83548
 
      /* Invoke MustBeInt to coerce the child key value to an integer (i.e. 
83549
 
      ** apply the affinity of the parent key). If this fails, then there
83550
 
      ** is no matching parent key. Before using MustBeInt, make a copy of
83551
 
      ** the value. Otherwise, the value inserted into the child key column
83552
 
      ** will have INTEGER affinity applied to it, which may not be correct.  */
83553
 
      sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
83554
 
      iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
83555
 
  
83556
 
      /* If the parent table is the same as the child table, and we are about
83557
 
      ** to increment the constraint-counter (i.e. this is an INSERT operation),
83558
 
      ** then check if the row being inserted matches itself. If so, do not
83559
 
      ** increment the constraint-counter.  */
83560
 
      if( pTab==pFKey->pFrom && nIncr==1 ){
83561
 
        sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp);
83562
 
      }
83563
 
  
83564
 
      sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
83565
 
      sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp);
83566
 
      sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
83567
 
      sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
83568
 
      sqlite3VdbeJumpHere(v, iMustBeInt);
83569
 
      sqlite3ReleaseTempReg(pParse, regTemp);
83570
 
    }else{
83571
 
      int nCol = pFKey->nCol;
83572
 
      int regTemp = sqlite3GetTempRange(pParse, nCol);
83573
 
      int regRec = sqlite3GetTempReg(pParse);
83574
 
      KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
83575
 
  
83576
 
      sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
83577
 
      sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
83578
 
      for(i=0; i<nCol; i++){
83579
 
        sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
83580
 
      }
83581
 
  
83582
 
      /* If the parent table is the same as the child table, and we are about
83583
 
      ** to increment the constraint-counter (i.e. this is an INSERT operation),
83584
 
      ** then check if the row being inserted matches itself. If so, do not
83585
 
      ** increment the constraint-counter.  */
83586
 
      if( pTab==pFKey->pFrom && nIncr==1 ){
83587
 
        int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
83588
 
        for(i=0; i<nCol; i++){
83589
 
          int iChild = aiCol[i]+1+regData;
83590
 
          int iParent = pIdx->aiColumn[i]+1+regData;
83591
 
          sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent);
83592
 
        }
83593
 
        sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
83594
 
      }
83595
 
  
83596
 
      sqlite3VdbeAddOp3(v, OP_MakeRecord, regTemp, nCol, regRec);
83597
 
      sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
83598
 
      sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0);
83599
 
  
83600
 
      sqlite3ReleaseTempReg(pParse, regRec);
83601
 
      sqlite3ReleaseTempRange(pParse, regTemp, nCol);
83602
 
    }
83603
 
  }
83604
 
 
83605
 
  if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
83606
 
    /* Special case: If this is an INSERT statement that will insert exactly
83607
 
    ** one row into the table, raise a constraint immediately instead of
83608
 
    ** incrementing a counter. This is necessary as the VM code is being
83609
 
    ** generated for will not open a statement transaction.  */
83610
 
    assert( nIncr==1 );
83611
 
    sqlite3HaltConstraint(
83612
 
        pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
83613
 
    );
83614
 
  }else{
83615
 
    if( nIncr>0 && pFKey->isDeferred==0 ){
83616
 
      sqlite3ParseToplevel(pParse)->mayAbort = 1;
83617
 
    }
83618
 
    sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
83619
 
  }
83620
 
 
83621
 
  sqlite3VdbeResolveLabel(v, iOk);
83622
 
  sqlite3VdbeAddOp1(v, OP_Close, iCur);
83623
 
}
83624
 
 
83625
 
/*
83626
 
** This function is called to generate code executed when a row is deleted
83627
 
** from the parent table of foreign key constraint pFKey and, if pFKey is 
83628
 
** deferred, when a row is inserted into the same table. When generating
83629
 
** code for an SQL UPDATE operation, this function may be called twice -
83630
 
** once to "delete" the old row and once to "insert" the new row.
83631
 
**
83632
 
** The code generated by this function scans through the rows in the child
83633
 
** table that correspond to the parent table row being deleted or inserted.
83634
 
** For each child row found, one of the following actions is taken:
83635
 
**
83636
 
**   Operation | FK type   | Action taken
83637
 
**   --------------------------------------------------------------------------
83638
 
**   DELETE      immediate   Increment the "immediate constraint counter".
83639
 
**                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
83640
 
**                           throw a "foreign key constraint failed" exception.
83641
 
**
83642
 
**   INSERT      immediate   Decrement the "immediate constraint counter".
83643
 
**
83644
 
**   DELETE      deferred    Increment the "deferred constraint counter".
83645
 
**                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
83646
 
**                           throw a "foreign key constraint failed" exception.
83647
 
**
83648
 
**   INSERT      deferred    Decrement the "deferred constraint counter".
83649
 
**
83650
 
** These operations are identified in the comment at the top of this file 
83651
 
** (fkey.c) as "I.2" and "D.2".
83652
 
*/
83653
 
static void fkScanChildren(
83654
 
  Parse *pParse,                  /* Parse context */
83655
 
  SrcList *pSrc,                  /* SrcList containing the table to scan */
83656
 
  Table *pTab,
83657
 
  Index *pIdx,                    /* Foreign key index */
83658
 
  FKey *pFKey,                    /* Foreign key relationship */
83659
 
  int *aiCol,                     /* Map from pIdx cols to child table cols */
83660
 
  int regData,                    /* Referenced table data starts here */
83661
 
  int nIncr                       /* Amount to increment deferred counter by */
83662
 
){
83663
 
  sqlite3 *db = pParse->db;       /* Database handle */
83664
 
  int i;                          /* Iterator variable */
83665
 
  Expr *pWhere = 0;               /* WHERE clause to scan with */
83666
 
  NameContext sNameContext;       /* Context used to resolve WHERE clause */
83667
 
  WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
83668
 
  int iFkIfZero = 0;              /* Address of OP_FkIfZero */
83669
 
  Vdbe *v = sqlite3GetVdbe(pParse);
83670
 
 
83671
 
  assert( !pIdx || pIdx->pTable==pTab );
83672
 
 
83673
 
  if( nIncr<0 ){
83674
 
    iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
83675
 
  }
83676
 
 
83677
 
  /* Create an Expr object representing an SQL expression like:
83678
 
  **
83679
 
  **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
83680
 
  **
83681
 
  ** The collation sequence used for the comparison should be that of
83682
 
  ** the parent key columns. The affinity of the parent key column should
83683
 
  ** be applied to each child key value before the comparison takes place.
83684
 
  */
83685
 
  for(i=0; i<pFKey->nCol; i++){
83686
 
    Expr *pLeft;                  /* Value from parent table row */
83687
 
    Expr *pRight;                 /* Column ref to child table */
83688
 
    Expr *pEq;                    /* Expression (pLeft = pRight) */
83689
 
    int iCol;                     /* Index of column in child table */ 
83690
 
    const char *zCol;             /* Name of column in child table */
83691
 
 
83692
 
    pLeft = sqlite3Expr(db, TK_REGISTER, 0);
83693
 
    if( pLeft ){
83694
 
      /* Set the collation sequence and affinity of the LHS of each TK_EQ
83695
 
      ** expression to the parent key column defaults.  */
83696
 
      if( pIdx ){
83697
 
        Column *pCol;
83698
 
        iCol = pIdx->aiColumn[i];
83699
 
        pCol = &pTab->aCol[iCol];
83700
 
        if( pTab->iPKey==iCol ) iCol = -1;
83701
 
        pLeft->iTable = regData+iCol+1;
83702
 
        pLeft->affinity = pCol->affinity;
83703
 
        pLeft->pColl = sqlite3LocateCollSeq(pParse, pCol->zColl);
83704
 
      }else{
83705
 
        pLeft->iTable = regData;
83706
 
        pLeft->affinity = SQLITE_AFF_INTEGER;
83707
 
      }
83708
 
    }
83709
 
    iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
83710
 
    assert( iCol>=0 );
83711
 
    zCol = pFKey->pFrom->aCol[iCol].zName;
83712
 
    pRight = sqlite3Expr(db, TK_ID, zCol);
83713
 
    pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
83714
 
    pWhere = sqlite3ExprAnd(db, pWhere, pEq);
83715
 
  }
83716
 
 
83717
 
  /* If the child table is the same as the parent table, and this scan
83718
 
  ** is taking place as part of a DELETE operation (operation D.2), omit the
83719
 
  ** row being deleted from the scan by adding ($rowid != rowid) to the WHERE 
83720
 
  ** clause, where $rowid is the rowid of the row being deleted.  */
83721
 
  if( pTab==pFKey->pFrom && nIncr>0 ){
83722
 
    Expr *pEq;                    /* Expression (pLeft = pRight) */
83723
 
    Expr *pLeft;                  /* Value from parent table row */
83724
 
    Expr *pRight;                 /* Column ref to child table */
83725
 
    pLeft = sqlite3Expr(db, TK_REGISTER, 0);
83726
 
    pRight = sqlite3Expr(db, TK_COLUMN, 0);
83727
 
    if( pLeft && pRight ){
83728
 
      pLeft->iTable = regData;
83729
 
      pLeft->affinity = SQLITE_AFF_INTEGER;
83730
 
      pRight->iTable = pSrc->a[0].iCursor;
83731
 
      pRight->iColumn = -1;
83732
 
    }
83733
 
    pEq = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
83734
 
    pWhere = sqlite3ExprAnd(db, pWhere, pEq);
83735
 
  }
83736
 
 
83737
 
  /* Resolve the references in the WHERE clause. */
83738
 
  memset(&sNameContext, 0, sizeof(NameContext));
83739
 
  sNameContext.pSrcList = pSrc;
83740
 
  sNameContext.pParse = pParse;
83741
 
  sqlite3ResolveExprNames(&sNameContext, pWhere);
83742
 
 
83743
 
  /* Create VDBE to loop through the entries in pSrc that match the WHERE
83744
 
  ** clause. If the constraint is not deferred, throw an exception for
83745
 
  ** each row found. Otherwise, for deferred constraints, increment the
83746
 
  ** deferred constraint counter by nIncr for each row selected.  */
83747
 
  pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0);
83748
 
  if( nIncr>0 && pFKey->isDeferred==0 ){
83749
 
    sqlite3ParseToplevel(pParse)->mayAbort = 1;
83750
 
  }
83751
 
  sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
83752
 
  if( pWInfo ){
83753
 
    sqlite3WhereEnd(pWInfo);
83754
 
  }
83755
 
 
83756
 
  /* Clean up the WHERE clause constructed above. */
83757
 
  sqlite3ExprDelete(db, pWhere);
83758
 
  if( iFkIfZero ){
83759
 
    sqlite3VdbeJumpHere(v, iFkIfZero);
83760
 
  }
83761
 
}
83762
 
 
83763
 
/*
83764
 
** This function returns a pointer to the head of a linked list of FK
83765
 
** constraints for which table pTab is the parent table. For example,
83766
 
** given the following schema:
83767
 
**
83768
 
**   CREATE TABLE t1(a PRIMARY KEY);
83769
 
**   CREATE TABLE t2(b REFERENCES t1(a);
83770
 
**
83771
 
** Calling this function with table "t1" as an argument returns a pointer
83772
 
** to the FKey structure representing the foreign key constraint on table
83773
 
** "t2". Calling this function with "t2" as the argument would return a
83774
 
** NULL pointer (as there are no FK constraints for which t2 is the parent
83775
 
** table).
83776
 
*/
83777
 
SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
83778
 
  int nName = sqlite3Strlen30(pTab->zName);
83779
 
  return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName, nName);
83780
 
}
83781
 
 
83782
 
/*
83783
 
** The second argument is a Trigger structure allocated by the 
83784
 
** fkActionTrigger() routine. This function deletes the Trigger structure
83785
 
** and all of its sub-components.
83786
 
**
83787
 
** The Trigger structure or any of its sub-components may be allocated from
83788
 
** the lookaside buffer belonging to database handle dbMem.
83789
 
*/
83790
 
static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
83791
 
  if( p ){
83792
 
    TriggerStep *pStep = p->step_list;
83793
 
    sqlite3ExprDelete(dbMem, pStep->pWhere);
83794
 
    sqlite3ExprListDelete(dbMem, pStep->pExprList);
83795
 
    sqlite3SelectDelete(dbMem, pStep->pSelect);
83796
 
    sqlite3ExprDelete(dbMem, p->pWhen);
83797
 
    sqlite3DbFree(dbMem, p);
83798
 
  }
83799
 
}
83800
 
 
83801
 
/*
83802
 
** This function is called to generate code that runs when table pTab is
83803
 
** being dropped from the database. The SrcList passed as the second argument
83804
 
** to this function contains a single entry guaranteed to resolve to
83805
 
** table pTab.
83806
 
**
83807
 
** Normally, no code is required. However, if either
83808
 
**
83809
 
**   (a) The table is the parent table of a FK constraint, or
83810
 
**   (b) The table is the child table of a deferred FK constraint and it is
83811
 
**       determined at runtime that there are outstanding deferred FK 
83812
 
**       constraint violations in the database,
83813
 
**
83814
 
** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
83815
 
** the table from the database. Triggers are disabled while running this
83816
 
** DELETE, but foreign key actions are not.
83817
 
*/
83818
 
SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
83819
 
  sqlite3 *db = pParse->db;
83820
 
  if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
83821
 
    int iSkip = 0;
83822
 
    Vdbe *v = sqlite3GetVdbe(pParse);
83823
 
 
83824
 
    assert( v );                  /* VDBE has already been allocated */
83825
 
    if( sqlite3FkReferences(pTab)==0 ){
83826
 
      /* Search for a deferred foreign key constraint for which this table
83827
 
      ** is the child table. If one cannot be found, return without 
83828
 
      ** generating any VDBE code. If one can be found, then jump over
83829
 
      ** the entire DELETE if there are no outstanding deferred constraints
83830
 
      ** when this statement is run.  */
83831
 
      FKey *p;
83832
 
      for(p=pTab->pFKey; p; p=p->pNextFrom){
83833
 
        if( p->isDeferred ) break;
83834
 
      }
83835
 
      if( !p ) return;
83836
 
      iSkip = sqlite3VdbeMakeLabel(v);
83837
 
      sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip);
83838
 
    }
83839
 
 
83840
 
    pParse->disableTriggers = 1;
83841
 
    sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
83842
 
    pParse->disableTriggers = 0;
83843
 
 
83844
 
    /* If the DELETE has generated immediate foreign key constraint 
83845
 
    ** violations, halt the VDBE and return an error at this point, before
83846
 
    ** any modifications to the schema are made. This is because statement
83847
 
    ** transactions are not able to rollback schema changes.  */
83848
 
    sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
83849
 
    sqlite3HaltConstraint(
83850
 
        pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
83851
 
    );
83852
 
 
83853
 
    if( iSkip ){
83854
 
      sqlite3VdbeResolveLabel(v, iSkip);
83855
 
    }
83856
 
  }
83857
 
}
83858
 
 
83859
 
/*
83860
 
** This function is called when inserting, deleting or updating a row of
83861
 
** table pTab to generate VDBE code to perform foreign key constraint 
83862
 
** processing for the operation.
83863
 
**
83864
 
** For a DELETE operation, parameter regOld is passed the index of the
83865
 
** first register in an array of (pTab->nCol+1) registers containing the
83866
 
** rowid of the row being deleted, followed by each of the column values
83867
 
** of the row being deleted, from left to right. Parameter regNew is passed
83868
 
** zero in this case.
83869
 
**
83870
 
** For an INSERT operation, regOld is passed zero and regNew is passed the
83871
 
** first register of an array of (pTab->nCol+1) registers containing the new
83872
 
** row data.
83873
 
**
83874
 
** For an UPDATE operation, this function is called twice. Once before
83875
 
** the original record is deleted from the table using the calling convention
83876
 
** described for DELETE. Then again after the original record is deleted
83877
 
** but before the new record is inserted using the INSERT convention. 
83878
 
*/
83879
 
SQLITE_PRIVATE void sqlite3FkCheck(
83880
 
  Parse *pParse,                  /* Parse context */
83881
 
  Table *pTab,                    /* Row is being deleted from this table */ 
83882
 
  int regOld,                     /* Previous row data is stored here */
83883
 
  int regNew                      /* New row data is stored here */
83884
 
){
83885
 
  sqlite3 *db = pParse->db;       /* Database handle */
83886
 
  FKey *pFKey;                    /* Used to iterate through FKs */
83887
 
  int iDb;                        /* Index of database containing pTab */
83888
 
  const char *zDb;                /* Name of database containing pTab */
83889
 
  int isIgnoreErrors = pParse->disableTriggers;
83890
 
 
83891
 
  /* Exactly one of regOld and regNew should be non-zero. */
83892
 
  assert( (regOld==0)!=(regNew==0) );
83893
 
 
83894
 
  /* If foreign-keys are disabled, this function is a no-op. */
83895
 
  if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
83896
 
 
83897
 
  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
83898
 
  zDb = db->aDb[iDb].zName;
83899
 
 
83900
 
  /* Loop through all the foreign key constraints for which pTab is the
83901
 
  ** child table (the table that the foreign key definition is part of).  */
83902
 
  for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
83903
 
    Table *pTo;                   /* Parent table of foreign key pFKey */
83904
 
    Index *pIdx = 0;              /* Index on key columns in pTo */
83905
 
    int *aiFree = 0;
83906
 
    int *aiCol;
83907
 
    int iCol;
83908
 
    int i;
83909
 
    int isIgnore = 0;
83910
 
 
83911
 
    /* Find the parent table of this foreign key. Also find a unique index 
83912
 
    ** on the parent key columns in the parent table. If either of these 
83913
 
    ** schema items cannot be located, set an error in pParse and return 
83914
 
    ** early.  */
83915
 
    if( pParse->disableTriggers ){
83916
 
      pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
83917
 
    }else{
83918
 
      pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
83919
 
    }
83920
 
    if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
83921
 
      if( !isIgnoreErrors || db->mallocFailed ) return;
83922
 
      continue;
83923
 
    }
83924
 
    assert( pFKey->nCol==1 || (aiFree && pIdx) );
83925
 
 
83926
 
    if( aiFree ){
83927
 
      aiCol = aiFree;
83928
 
    }else{
83929
 
      iCol = pFKey->aCol[0].iFrom;
83930
 
      aiCol = &iCol;
83931
 
    }
83932
 
    for(i=0; i<pFKey->nCol; i++){
83933
 
      if( aiCol[i]==pTab->iPKey ){
83934
 
        aiCol[i] = -1;
83935
 
      }
83936
 
#ifndef SQLITE_OMIT_AUTHORIZATION
83937
 
      /* Request permission to read the parent key columns. If the 
83938
 
      ** authorization callback returns SQLITE_IGNORE, behave as if any
83939
 
      ** values read from the parent table are NULL. */
83940
 
      if( db->xAuth ){
83941
 
        int rcauth;
83942
 
        char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
83943
 
        rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
83944
 
        isIgnore = (rcauth==SQLITE_IGNORE);
83945
 
      }
83946
 
#endif
83947
 
    }
83948
 
 
83949
 
    /* Take a shared-cache advisory read-lock on the parent table. Allocate 
83950
 
    ** a cursor to use to search the unique index on the parent key columns 
83951
 
    ** in the parent table.  */
83952
 
    sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
83953
 
    pParse->nTab++;
83954
 
 
83955
 
    if( regOld!=0 ){
83956
 
      /* A row is being removed from the child table. Search for the parent.
83957
 
      ** If the parent does not exist, removing the child row resolves an 
83958
 
      ** outstanding foreign key constraint violation. */
83959
 
      fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1,isIgnore);
83960
 
    }
83961
 
    if( regNew!=0 ){
83962
 
      /* A row is being added to the child table. If a parent row cannot
83963
 
      ** be found, adding the child row has violated the FK constraint. */ 
83964
 
      fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1,isIgnore);
83965
 
    }
83966
 
 
83967
 
    sqlite3DbFree(db, aiFree);
83968
 
  }
83969
 
 
83970
 
  /* Loop through all the foreign key constraints that refer to this table */
83971
 
  for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
83972
 
    Index *pIdx = 0;              /* Foreign key index for pFKey */
83973
 
    SrcList *pSrc;
83974
 
    int *aiCol = 0;
83975
 
 
83976
 
    if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
83977
 
      assert( regOld==0 && regNew!=0 );
83978
 
      /* Inserting a single row into a parent table cannot cause an immediate
83979
 
      ** foreign key violation. So do nothing in this case.  */
83980
 
      continue;
83981
 
    }
83982
 
 
83983
 
    if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
83984
 
      if( !isIgnoreErrors || db->mallocFailed ) return;
83985
 
      continue;
83986
 
    }
83987
 
    assert( aiCol || pFKey->nCol==1 );
83988
 
 
83989
 
    /* Create a SrcList structure containing a single table (the table 
83990
 
    ** the foreign key that refers to this table is attached to). This
83991
 
    ** is required for the sqlite3WhereXXX() interface.  */
83992
 
    pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
83993
 
    if( pSrc ){
83994
 
      struct SrcList_item *pItem = pSrc->a;
83995
 
      pItem->pTab = pFKey->pFrom;
83996
 
      pItem->zName = pFKey->pFrom->zName;
83997
 
      pItem->pTab->nRef++;
83998
 
      pItem->iCursor = pParse->nTab++;
83999
 
  
84000
 
      if( regNew!=0 ){
84001
 
        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
84002
 
      }
84003
 
      if( regOld!=0 ){
84004
 
        /* If there is a RESTRICT action configured for the current operation
84005
 
        ** on the parent table of this FK, then throw an exception 
84006
 
        ** immediately if the FK constraint is violated, even if this is a
84007
 
        ** deferred trigger. That's what RESTRICT means. To defer checking
84008
 
        ** the constraint, the FK should specify NO ACTION (represented
84009
 
        ** using OE_None). NO ACTION is the default.  */
84010
 
        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
84011
 
      }
84012
 
      pItem->zName = 0;
84013
 
      sqlite3SrcListDelete(db, pSrc);
84014
 
    }
84015
 
    sqlite3DbFree(db, aiCol);
84016
 
  }
84017
 
}
84018
 
 
84019
 
#define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
84020
 
 
84021
 
/*
84022
 
** This function is called before generating code to update or delete a 
84023
 
** row contained in table pTab.
84024
 
*/
84025
 
SQLITE_PRIVATE u32 sqlite3FkOldmask(
84026
 
  Parse *pParse,                  /* Parse context */
84027
 
  Table *pTab                     /* Table being modified */
84028
 
){
84029
 
  u32 mask = 0;
84030
 
  if( pParse->db->flags&SQLITE_ForeignKeys ){
84031
 
    FKey *p;
84032
 
    int i;
84033
 
    for(p=pTab->pFKey; p; p=p->pNextFrom){
84034
 
      for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
84035
 
    }
84036
 
    for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
84037
 
      Index *pIdx = 0;
84038
 
      locateFkeyIndex(pParse, pTab, p, &pIdx, 0);
84039
 
      if( pIdx ){
84040
 
        for(i=0; i<pIdx->nColumn; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
84041
 
      }
84042
 
    }
84043
 
  }
84044
 
  return mask;
84045
 
}
84046
 
 
84047
 
/*
84048
 
** This function is called before generating code to update or delete a 
84049
 
** row contained in table pTab. If the operation is a DELETE, then
84050
 
** parameter aChange is passed a NULL value. For an UPDATE, aChange points
84051
 
** to an array of size N, where N is the number of columns in table pTab.
84052
 
** If the i'th column is not modified by the UPDATE, then the corresponding 
84053
 
** entry in the aChange[] array is set to -1. If the column is modified,
84054
 
** the value is 0 or greater. Parameter chngRowid is set to true if the
84055
 
** UPDATE statement modifies the rowid fields of the table.
84056
 
**
84057
 
** If any foreign key processing will be required, this function returns
84058
 
** true. If there is no foreign key related processing, this function 
84059
 
** returns false.
84060
 
*/
84061
 
SQLITE_PRIVATE int sqlite3FkRequired(
84062
 
  Parse *pParse,                  /* Parse context */
84063
 
  Table *pTab,                    /* Table being modified */
84064
 
  int *aChange,                   /* Non-NULL for UPDATE operations */
84065
 
  int chngRowid                   /* True for UPDATE that affects rowid */
84066
 
){
84067
 
  if( pParse->db->flags&SQLITE_ForeignKeys ){
84068
 
    if( !aChange ){
84069
 
      /* A DELETE operation. Foreign key processing is required if the 
84070
 
      ** table in question is either the child or parent table for any 
84071
 
      ** foreign key constraint.  */
84072
 
      return (sqlite3FkReferences(pTab) || pTab->pFKey);
84073
 
    }else{
84074
 
      /* This is an UPDATE. Foreign key processing is only required if the
84075
 
      ** operation modifies one or more child or parent key columns. */
84076
 
      int i;
84077
 
      FKey *p;
84078
 
 
84079
 
      /* Check if any child key columns are being modified. */
84080
 
      for(p=pTab->pFKey; p; p=p->pNextFrom){
84081
 
        for(i=0; i<p->nCol; i++){
84082
 
          int iChildKey = p->aCol[i].iFrom;
84083
 
          if( aChange[iChildKey]>=0 ) return 1;
84084
 
          if( iChildKey==pTab->iPKey && chngRowid ) return 1;
84085
 
        }
84086
 
      }
84087
 
 
84088
 
      /* Check if any parent key columns are being modified. */
84089
 
      for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
84090
 
        for(i=0; i<p->nCol; i++){
84091
 
          char *zKey = p->aCol[i].zCol;
84092
 
          int iKey;
84093
 
          for(iKey=0; iKey<pTab->nCol; iKey++){
84094
 
            Column *pCol = &pTab->aCol[iKey];
84095
 
            if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey) : pCol->isPrimKey) ){
84096
 
              if( aChange[iKey]>=0 ) return 1;
84097
 
              if( iKey==pTab->iPKey && chngRowid ) return 1;
84098
 
            }
84099
 
          }
84100
 
        }
84101
 
      }
84102
 
    }
84103
 
  }
84104
 
  return 0;
84105
 
}
84106
 
 
84107
 
/*
84108
 
** This function is called when an UPDATE or DELETE operation is being 
84109
 
** compiled on table pTab, which is the parent table of foreign-key pFKey.
84110
 
** If the current operation is an UPDATE, then the pChanges parameter is
84111
 
** passed a pointer to the list of columns being modified. If it is a
84112
 
** DELETE, pChanges is passed a NULL pointer.
84113
 
**
84114
 
** It returns a pointer to a Trigger structure containing a trigger
84115
 
** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
84116
 
** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
84117
 
** returned (these actions require no special handling by the triggers
84118
 
** sub-system, code for them is created by fkScanChildren()).
84119
 
**
84120
 
** For example, if pFKey is the foreign key and pTab is table "p" in 
84121
 
** the following schema:
84122
 
**
84123
 
**   CREATE TABLE p(pk PRIMARY KEY);
84124
 
**   CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
84125
 
**
84126
 
** then the returned trigger structure is equivalent to:
84127
 
**
84128
 
**   CREATE TRIGGER ... DELETE ON p BEGIN
84129
 
**     DELETE FROM c WHERE ck = old.pk;
84130
 
**   END;
84131
 
**
84132
 
** The returned pointer is cached as part of the foreign key object. It
84133
 
** is eventually freed along with the rest of the foreign key object by 
84134
 
** sqlite3FkDelete().
84135
 
*/
84136
 
static Trigger *fkActionTrigger(
84137
 
  Parse *pParse,                  /* Parse context */
84138
 
  Table *pTab,                    /* Table being updated or deleted from */
84139
 
  FKey *pFKey,                    /* Foreign key to get action for */
84140
 
  ExprList *pChanges              /* Change-list for UPDATE, NULL for DELETE */
84141
 
){
84142
 
  sqlite3 *db = pParse->db;       /* Database handle */
84143
 
  int action;                     /* One of OE_None, OE_Cascade etc. */
84144
 
  Trigger *pTrigger;              /* Trigger definition to return */
84145
 
  int iAction = (pChanges!=0);    /* 1 for UPDATE, 0 for DELETE */
84146
 
 
84147
 
  action = pFKey->aAction[iAction];
84148
 
  pTrigger = pFKey->apTrigger[iAction];
84149
 
 
84150
 
  if( action!=OE_None && !pTrigger ){
84151
 
    u8 enableLookaside;           /* Copy of db->lookaside.bEnabled */
84152
 
    char const *zFrom;            /* Name of child table */
84153
 
    int nFrom;                    /* Length in bytes of zFrom */
84154
 
    Index *pIdx = 0;              /* Parent key index for this FK */
84155
 
    int *aiCol = 0;               /* child table cols -> parent key cols */
84156
 
    TriggerStep *pStep = 0;        /* First (only) step of trigger program */
84157
 
    Expr *pWhere = 0;             /* WHERE clause of trigger step */
84158
 
    ExprList *pList = 0;          /* Changes list if ON UPDATE CASCADE */
84159
 
    Select *pSelect = 0;          /* If RESTRICT, "SELECT RAISE(...)" */
84160
 
    int i;                        /* Iterator variable */
84161
 
    Expr *pWhen = 0;              /* WHEN clause for the trigger */
84162
 
 
84163
 
    if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
84164
 
    assert( aiCol || pFKey->nCol==1 );
84165
 
 
84166
 
    for(i=0; i<pFKey->nCol; i++){
84167
 
      Token tOld = { "old", 3 };  /* Literal "old" token */
84168
 
      Token tNew = { "new", 3 };  /* Literal "new" token */
84169
 
      Token tFromCol;             /* Name of column in child table */
84170
 
      Token tToCol;               /* Name of column in parent table */
84171
 
      int iFromCol;               /* Idx of column in child table */
84172
 
      Expr *pEq;                  /* tFromCol = OLD.tToCol */
84173
 
 
84174
 
      iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
84175
 
      assert( iFromCol>=0 );
84176
 
      tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid";
84177
 
      tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
84178
 
 
84179
 
      tToCol.n = sqlite3Strlen30(tToCol.z);
84180
 
      tFromCol.n = sqlite3Strlen30(tFromCol.z);
84181
 
 
84182
 
      /* Create the expression "OLD.zToCol = zFromCol". It is important
84183
 
      ** that the "OLD.zToCol" term is on the LHS of the = operator, so
84184
 
      ** that the affinity and collation sequence associated with the
84185
 
      ** parent table are used for the comparison. */
84186
 
      pEq = sqlite3PExpr(pParse, TK_EQ,
84187
 
          sqlite3PExpr(pParse, TK_DOT, 
84188
 
            sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
84189
 
            sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
84190
 
          , 0),
84191
 
          sqlite3PExpr(pParse, TK_ID, 0, 0, &tFromCol)
84192
 
      , 0);
84193
 
      pWhere = sqlite3ExprAnd(db, pWhere, pEq);
84194
 
 
84195
 
      /* For ON UPDATE, construct the next term of the WHEN clause.
84196
 
      ** The final WHEN clause will be like this:
84197
 
      **
84198
 
      **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
84199
 
      */
84200
 
      if( pChanges ){
84201
 
        pEq = sqlite3PExpr(pParse, TK_IS,
84202
 
            sqlite3PExpr(pParse, TK_DOT, 
84203
 
              sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
84204
 
              sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
84205
 
              0),
84206
 
            sqlite3PExpr(pParse, TK_DOT, 
84207
 
              sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
84208
 
              sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
84209
 
              0),
84210
 
            0);
84211
 
        pWhen = sqlite3ExprAnd(db, pWhen, pEq);
84212
 
      }
84213
 
  
84214
 
      if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
84215
 
        Expr *pNew;
84216
 
        if( action==OE_Cascade ){
84217
 
          pNew = sqlite3PExpr(pParse, TK_DOT, 
84218
 
            sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
84219
 
            sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
84220
 
          , 0);
84221
 
        }else if( action==OE_SetDflt ){
84222
 
          Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
84223
 
          if( pDflt ){
84224
 
            pNew = sqlite3ExprDup(db, pDflt, 0);
84225
 
          }else{
84226
 
            pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
84227
 
          }
84228
 
        }else{
84229
 
          pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
84230
 
        }
84231
 
        pList = sqlite3ExprListAppend(pParse, pList, pNew);
84232
 
        sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
84233
 
      }
84234
 
    }
84235
 
    sqlite3DbFree(db, aiCol);
84236
 
 
84237
 
    zFrom = pFKey->pFrom->zName;
84238
 
    nFrom = sqlite3Strlen30(zFrom);
84239
 
 
84240
 
    if( action==OE_Restrict ){
84241
 
      Token tFrom;
84242
 
      Expr *pRaise; 
84243
 
 
84244
 
      tFrom.z = zFrom;
84245
 
      tFrom.n = nFrom;
84246
 
      pRaise = sqlite3Expr(db, TK_RAISE, "foreign key constraint failed");
84247
 
      if( pRaise ){
84248
 
        pRaise->affinity = OE_Abort;
84249
 
      }
84250
 
      pSelect = sqlite3SelectNew(pParse, 
84251
 
          sqlite3ExprListAppend(pParse, 0, pRaise),
84252
 
          sqlite3SrcListAppend(db, 0, &tFrom, 0),
84253
 
          pWhere,
84254
 
          0, 0, 0, 0, 0, 0
84255
 
      );
84256
 
      pWhere = 0;
84257
 
    }
84258
 
 
84259
 
    /* Disable lookaside memory allocation */
84260
 
    enableLookaside = db->lookaside.bEnabled;
84261
 
    db->lookaside.bEnabled = 0;
84262
 
 
84263
 
    pTrigger = (Trigger *)sqlite3DbMallocZero(db, 
84264
 
        sizeof(Trigger) +         /* struct Trigger */
84265
 
        sizeof(TriggerStep) +     /* Single step in trigger program */
84266
 
        nFrom + 1                 /* Space for pStep->target.z */
84267
 
    );
84268
 
    if( pTrigger ){
84269
 
      pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
84270
 
      pStep->target.z = (char *)&pStep[1];
84271
 
      pStep->target.n = nFrom;
84272
 
      memcpy((char *)pStep->target.z, zFrom, nFrom);
84273
 
  
84274
 
      pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
84275
 
      pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
84276
 
      pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
84277
 
      if( pWhen ){
84278
 
        pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
84279
 
        pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
84280
 
      }
84281
 
    }
84282
 
 
84283
 
    /* Re-enable the lookaside buffer, if it was disabled earlier. */
84284
 
    db->lookaside.bEnabled = enableLookaside;
84285
 
 
84286
 
    sqlite3ExprDelete(db, pWhere);
84287
 
    sqlite3ExprDelete(db, pWhen);
84288
 
    sqlite3ExprListDelete(db, pList);
84289
 
    sqlite3SelectDelete(db, pSelect);
84290
 
    if( db->mallocFailed==1 ){
84291
 
      fkTriggerDelete(db, pTrigger);
84292
 
      return 0;
84293
 
    }
84294
 
 
84295
 
    switch( action ){
84296
 
      case OE_Restrict:
84297
 
        pStep->op = TK_SELECT; 
84298
 
        break;
84299
 
      case OE_Cascade: 
84300
 
        if( !pChanges ){ 
84301
 
          pStep->op = TK_DELETE; 
84302
 
          break; 
84303
 
        }
84304
 
      default:
84305
 
        pStep->op = TK_UPDATE;
84306
 
    }
84307
 
    pStep->pTrig = pTrigger;
84308
 
    pTrigger->pSchema = pTab->pSchema;
84309
 
    pTrigger->pTabSchema = pTab->pSchema;
84310
 
    pFKey->apTrigger[iAction] = pTrigger;
84311
 
    pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
84312
 
  }
84313
 
 
84314
 
  return pTrigger;
84315
 
}
84316
 
 
84317
 
/*
84318
 
** This function is called when deleting or updating a row to implement
84319
 
** any required CASCADE, SET NULL or SET DEFAULT actions.
84320
 
*/
84321
 
SQLITE_PRIVATE void sqlite3FkActions(
84322
 
  Parse *pParse,                  /* Parse context */
84323
 
  Table *pTab,                    /* Table being updated or deleted from */
84324
 
  ExprList *pChanges,             /* Change-list for UPDATE, NULL for DELETE */
84325
 
  int regOld                      /* Address of array containing old row */
84326
 
){
84327
 
  /* If foreign-key support is enabled, iterate through all FKs that 
84328
 
  ** refer to table pTab. If there is an action associated with the FK 
84329
 
  ** for this operation (either update or delete), invoke the associated 
84330
 
  ** trigger sub-program.  */
84331
 
  if( pParse->db->flags&SQLITE_ForeignKeys ){
84332
 
    FKey *pFKey;                  /* Iterator variable */
84333
 
    for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
84334
 
      Trigger *pAction = fkActionTrigger(pParse, pTab, pFKey, pChanges);
84335
 
      if( pAction ){
84336
 
        sqlite3CodeRowTriggerDirect(pParse, pAction, pTab, regOld, OE_Abort, 0);
84337
 
      }
84338
 
    }
84339
 
  }
84340
 
}
84341
 
 
84342
 
#endif /* ifndef SQLITE_OMIT_TRIGGER */
84343
 
 
84344
 
/*
84345
 
** Free all memory associated with foreign key definitions attached to
84346
 
** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
84347
 
** hash table.
84348
 
*/
84349
 
SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
84350
 
  FKey *pFKey;                    /* Iterator variable */
84351
 
  FKey *pNext;                    /* Copy of pFKey->pNextFrom */
84352
 
 
84353
 
  assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
84354
 
  for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
84355
 
 
84356
 
    /* Remove the FK from the fkeyHash hash table. */
84357
 
    if( !db || db->pnBytesFreed==0 ){
84358
 
      if( pFKey->pPrevTo ){
84359
 
        pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
84360
 
      }else{
84361
 
        void *p = (void *)pFKey->pNextTo;
84362
 
        const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
84363
 
        sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), p);
84364
 
      }
84365
 
      if( pFKey->pNextTo ){
84366
 
        pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
84367
 
      }
84368
 
    }
84369
 
 
84370
 
    /* EV: R-30323-21917 Each foreign key constraint in SQLite is
84371
 
    ** classified as either immediate or deferred.
84372
 
    */
84373
 
    assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
84374
 
 
84375
 
    /* Delete any triggers created to implement actions for this FK. */
84376
 
#ifndef SQLITE_OMIT_TRIGGER
84377
 
    fkTriggerDelete(db, pFKey->apTrigger[0]);
84378
 
    fkTriggerDelete(db, pFKey->apTrigger[1]);
84379
 
#endif
84380
 
 
84381
 
    pNext = pFKey->pNextFrom;
84382
 
    sqlite3DbFree(db, pFKey);
84383
 
  }
84384
 
}
84385
 
#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
84386
 
 
84387
 
/************** End of fkey.c ************************************************/
84388
 
/************** Begin file insert.c ******************************************/
84389
 
/*
84390
 
** 2001 September 15
84391
 
**
84392
 
** The author disclaims copyright to this source code.  In place of
84393
 
** a legal notice, here is a blessing:
84394
 
**
84395
 
**    May you do good and not evil.
84396
 
**    May you find forgiveness for yourself and forgive others.
84397
 
**    May you share freely, never taking more than you give.
84398
 
**
84399
 
*************************************************************************
84400
 
** This file contains C code routines that are called by the parser
84401
 
** to handle INSERT statements in SQLite.
84402
 
*/
84403
 
 
84404
 
/*
84405
 
** Generate code that will open a table for reading.
84406
 
*/
84407
 
SQLITE_PRIVATE void sqlite3OpenTable(
84408
 
  Parse *p,       /* Generate code into this VDBE */
84409
 
  int iCur,       /* The cursor number of the table */
84410
 
  int iDb,        /* The database index in sqlite3.aDb[] */
84411
 
  Table *pTab,    /* The table to be opened */
84412
 
  int opcode      /* OP_OpenRead or OP_OpenWrite */
84413
 
){
84414
 
  Vdbe *v;
84415
 
  if( IsVirtual(pTab) ) return;
84416
 
  v = sqlite3GetVdbe(p);
84417
 
  assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
84418
 
  sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName);
84419
 
  sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb);
84420
 
  sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32);
84421
 
  VdbeComment((v, "%s", pTab->zName));
84422
 
}
84423
 
 
84424
 
/*
84425
 
** Return a pointer to the column affinity string associated with index
84426
 
** pIdx. A column affinity string has one character for each column in 
84427
 
** the table, according to the affinity of the column:
84428
 
**
84429
 
**  Character      Column affinity
84430
 
**  ------------------------------
84431
 
**  'a'            TEXT
84432
 
**  'b'            NONE
84433
 
**  'c'            NUMERIC
84434
 
**  'd'            INTEGER
84435
 
**  'e'            REAL
84436
 
**
84437
 
** An extra 'b' is appended to the end of the string to cover the
84438
 
** rowid that appears as the last column in every index.
84439
 
**
84440
 
** Memory for the buffer containing the column index affinity string
84441
 
** is managed along with the rest of the Index structure. It will be
84442
 
** released when sqlite3DeleteIndex() is called.
84443
 
*/
84444
 
SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
84445
 
  if( !pIdx->zColAff ){
84446
 
    /* The first time a column affinity string for a particular index is
84447
 
    ** required, it is allocated and populated here. It is then stored as
84448
 
    ** a member of the Index structure for subsequent use.
84449
 
    **
84450
 
    ** The column affinity string will eventually be deleted by
84451
 
    ** sqliteDeleteIndex() when the Index structure itself is cleaned
84452
 
    ** up.
84453
 
    */
84454
 
    int n;
84455
 
    Table *pTab = pIdx->pTable;
84456
 
    sqlite3 *db = sqlite3VdbeDb(v);
84457
 
    pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+2);
84458
 
    if( !pIdx->zColAff ){
84459
 
      db->mallocFailed = 1;
84460
 
      return 0;
84461
 
    }
84462
 
    for(n=0; n<pIdx->nColumn; n++){
84463
 
      pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
84464
 
    }
84465
 
    pIdx->zColAff[n++] = SQLITE_AFF_NONE;
84466
 
    pIdx->zColAff[n] = 0;
84467
 
  }
84468
 
 
84469
 
  return pIdx->zColAff;
84470
 
}
84471
 
 
84472
 
/*
84473
 
** Set P4 of the most recently inserted opcode to a column affinity
84474
 
** string for table pTab. A column affinity string has one character
84475
 
** for each column indexed by the index, according to the affinity of the
84476
 
** column:
84477
 
**
84478
 
**  Character      Column affinity
84479
 
**  ------------------------------
84480
 
**  'a'            TEXT
84481
 
**  'b'            NONE
84482
 
**  'c'            NUMERIC
84483
 
**  'd'            INTEGER
84484
 
**  'e'            REAL
84485
 
*/
84486
 
SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
84487
 
  /* The first time a column affinity string for a particular table
84488
 
  ** is required, it is allocated and populated here. It is then 
84489
 
  ** stored as a member of the Table structure for subsequent use.
84490
 
  **
84491
 
  ** The column affinity string will eventually be deleted by
84492
 
  ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
84493
 
  */
84494
 
  if( !pTab->zColAff ){
84495
 
    char *zColAff;
84496
 
    int i;
84497
 
    sqlite3 *db = sqlite3VdbeDb(v);
84498
 
 
84499
 
    zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
84500
 
    if( !zColAff ){
84501
 
      db->mallocFailed = 1;
84502
 
      return;
84503
 
    }
84504
 
 
84505
 
    for(i=0; i<pTab->nCol; i++){
84506
 
      zColAff[i] = pTab->aCol[i].affinity;
84507
 
    }
84508
 
    zColAff[pTab->nCol] = '\0';
84509
 
 
84510
 
    pTab->zColAff = zColAff;
84511
 
  }
84512
 
 
84513
 
  sqlite3VdbeChangeP4(v, -1, pTab->zColAff, P4_TRANSIENT);
84514
 
}
84515
 
 
84516
 
/*
84517
 
** Return non-zero if the table pTab in database iDb or any of its indices
84518
 
** have been opened at any point in the VDBE program beginning at location
84519
 
** iStartAddr throught the end of the program.  This is used to see if 
84520
 
** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can 
84521
 
** run without using temporary table for the results of the SELECT. 
84522
 
*/
84523
 
static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){
84524
 
  Vdbe *v = sqlite3GetVdbe(p);
84525
 
  int i;
84526
 
  int iEnd = sqlite3VdbeCurrentAddr(v);
84527
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
84528
 
  VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
84529
 
#endif
84530
 
 
84531
 
  for(i=iStartAddr; i<iEnd; i++){
84532
 
    VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
84533
 
    assert( pOp!=0 );
84534
 
    if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
84535
 
      Index *pIndex;
84536
 
      int tnum = pOp->p2;
84537
 
      if( tnum==pTab->tnum ){
84538
 
        return 1;
84539
 
      }
84540
 
      for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
84541
 
        if( tnum==pIndex->tnum ){
84542
 
          return 1;
84543
 
        }
84544
 
      }
84545
 
    }
84546
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
84547
 
    if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
84548
 
      assert( pOp->p4.pVtab!=0 );
84549
 
      assert( pOp->p4type==P4_VTAB );
84550
 
      return 1;
84551
 
    }
84552
 
#endif
84553
 
  }
84554
 
  return 0;
84555
 
}
84556
 
 
84557
 
#ifndef SQLITE_OMIT_AUTOINCREMENT
84558
 
/*
84559
 
** Locate or create an AutoincInfo structure associated with table pTab
84560
 
** which is in database iDb.  Return the register number for the register
84561
 
** that holds the maximum rowid.
84562
 
**
84563
 
** There is at most one AutoincInfo structure per table even if the
84564
 
** same table is autoincremented multiple times due to inserts within
84565
 
** triggers.  A new AutoincInfo structure is created if this is the
84566
 
** first use of table pTab.  On 2nd and subsequent uses, the original
84567
 
** AutoincInfo structure is used.
84568
 
**
84569
 
** Three memory locations are allocated:
84570
 
**
84571
 
**   (1)  Register to hold the name of the pTab table.
84572
 
**   (2)  Register to hold the maximum ROWID of pTab.
84573
 
**   (3)  Register to hold the rowid in sqlite_sequence of pTab
84574
 
**
84575
 
** The 2nd register is the one that is returned.  That is all the
84576
 
** insert routine needs to know about.
84577
 
*/
84578
 
static int autoIncBegin(
84579
 
  Parse *pParse,      /* Parsing context */
84580
 
  int iDb,            /* Index of the database holding pTab */
84581
 
  Table *pTab         /* The table we are writing to */
84582
 
){
84583
 
  int memId = 0;      /* Register holding maximum rowid */
84584
 
  if( pTab->tabFlags & TF_Autoincrement ){
84585
 
    Parse *pToplevel = sqlite3ParseToplevel(pParse);
84586
 
    AutoincInfo *pInfo;
84587
 
 
84588
 
    pInfo = pToplevel->pAinc;
84589
 
    while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
84590
 
    if( pInfo==0 ){
84591
 
      pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
84592
 
      if( pInfo==0 ) return 0;
84593
 
      pInfo->pNext = pToplevel->pAinc;
84594
 
      pToplevel->pAinc = pInfo;
84595
 
      pInfo->pTab = pTab;
84596
 
      pInfo->iDb = iDb;
84597
 
      pToplevel->nMem++;                  /* Register to hold name of table */
84598
 
      pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
84599
 
      pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
84600
 
    }
84601
 
    memId = pInfo->regCtr;
84602
 
  }
84603
 
  return memId;
84604
 
}
84605
 
 
84606
 
/*
84607
 
** This routine generates code that will initialize all of the
84608
 
** register used by the autoincrement tracker.  
84609
 
*/
84610
 
SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
84611
 
  AutoincInfo *p;            /* Information about an AUTOINCREMENT */
84612
 
  sqlite3 *db = pParse->db;  /* The database connection */
84613
 
  Db *pDb;                   /* Database only autoinc table */
84614
 
  int memId;                 /* Register holding max rowid */
84615
 
  int addr;                  /* A VDBE address */
84616
 
  Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
84617
 
 
84618
 
  /* This routine is never called during trigger-generation.  It is
84619
 
  ** only called from the top-level */
84620
 
  assert( pParse->pTriggerTab==0 );
84621
 
  assert( pParse==sqlite3ParseToplevel(pParse) );
84622
 
 
84623
 
  assert( v );   /* We failed long ago if this is not so */
84624
 
  for(p = pParse->pAinc; p; p = p->pNext){
84625
 
    pDb = &db->aDb[p->iDb];
84626
 
    memId = p->regCtr;
84627
 
    assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
84628
 
    sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
84629
 
    addr = sqlite3VdbeCurrentAddr(v);
84630
 
    sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
84631
 
    sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
84632
 
    sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
84633
 
    sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
84634
 
    sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
84635
 
    sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
84636
 
    sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
84637
 
    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
84638
 
    sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
84639
 
    sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
84640
 
    sqlite3VdbeAddOp0(v, OP_Close);
84641
 
  }
84642
 
}
84643
 
 
84644
 
/*
84645
 
** Update the maximum rowid for an autoincrement calculation.
84646
 
**
84647
 
** This routine should be called when the top of the stack holds a
84648
 
** new rowid that is about to be inserted.  If that new rowid is
84649
 
** larger than the maximum rowid in the memId memory cell, then the
84650
 
** memory cell is updated.  The stack is unchanged.
84651
 
*/
84652
 
static void autoIncStep(Parse *pParse, int memId, int regRowid){
84653
 
  if( memId>0 ){
84654
 
    sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
84655
 
  }
84656
 
}
84657
 
 
84658
 
/*
84659
 
** This routine generates the code needed to write autoincrement
84660
 
** maximum rowid values back into the sqlite_sequence register.
84661
 
** Every statement that might do an INSERT into an autoincrement
84662
 
** table (either directly or through triggers) needs to call this
84663
 
** routine just before the "exit" code.
84664
 
*/
84665
 
SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
84666
 
  AutoincInfo *p;
84667
 
  Vdbe *v = pParse->pVdbe;
84668
 
  sqlite3 *db = pParse->db;
84669
 
 
84670
 
  assert( v );
84671
 
  for(p = pParse->pAinc; p; p = p->pNext){
84672
 
    Db *pDb = &db->aDb[p->iDb];
84673
 
    int j1, j2, j3, j4, j5;
84674
 
    int iRec;
84675
 
    int memId = p->regCtr;
84676
 
 
84677
 
    iRec = sqlite3GetTempReg(pParse);
84678
 
    assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
84679
 
    sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
84680
 
    j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
84681
 
    j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
84682
 
    j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
84683
 
    j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
84684
 
    sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
84685
 
    sqlite3VdbeJumpHere(v, j2);
84686
 
    sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
84687
 
    j5 = sqlite3VdbeAddOp0(v, OP_Goto);
84688
 
    sqlite3VdbeJumpHere(v, j4);
84689
 
    sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
84690
 
    sqlite3VdbeJumpHere(v, j1);
84691
 
    sqlite3VdbeJumpHere(v, j5);
84692
 
    sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
84693
 
    sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
84694
 
    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
84695
 
    sqlite3VdbeAddOp0(v, OP_Close);
84696
 
    sqlite3ReleaseTempReg(pParse, iRec);
84697
 
  }
84698
 
}
84699
 
#else
84700
 
/*
84701
 
** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
84702
 
** above are all no-ops
84703
 
*/
84704
 
# define autoIncBegin(A,B,C) (0)
84705
 
# define autoIncStep(A,B,C)
84706
 
#endif /* SQLITE_OMIT_AUTOINCREMENT */
84707
 
 
84708
 
 
84709
 
/* Forward declaration */
84710
 
static int xferOptimization(
84711
 
  Parse *pParse,        /* Parser context */
84712
 
  Table *pDest,         /* The table we are inserting into */
84713
 
  Select *pSelect,      /* A SELECT statement to use as the data source */
84714
 
  int onError,          /* How to handle constraint errors */
84715
 
  int iDbDest           /* The database of pDest */
84716
 
);
84717
 
 
84718
 
/*
84719
 
** This routine is call to handle SQL of the following forms:
84720
 
**
84721
 
**    insert into TABLE (IDLIST) values(EXPRLIST)
84722
 
**    insert into TABLE (IDLIST) select
84723
 
**
84724
 
** The IDLIST following the table name is always optional.  If omitted,
84725
 
** then a list of all columns for the table is substituted.  The IDLIST
84726
 
** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
84727
 
**
84728
 
** The pList parameter holds EXPRLIST in the first form of the INSERT
84729
 
** statement above, and pSelect is NULL.  For the second form, pList is
84730
 
** NULL and pSelect is a pointer to the select statement used to generate
84731
 
** data for the insert.
84732
 
**
84733
 
** The code generated follows one of four templates.  For a simple
84734
 
** select with data coming from a VALUES clause, the code executes
84735
 
** once straight down through.  Pseudo-code follows (we call this
84736
 
** the "1st template"):
84737
 
**
84738
 
**         open write cursor to <table> and its indices
84739
 
**         puts VALUES clause expressions onto the stack
84740
 
**         write the resulting record into <table>
84741
 
**         cleanup
84742
 
**
84743
 
** The three remaining templates assume the statement is of the form
84744
 
**
84745
 
**   INSERT INTO <table> SELECT ...
84746
 
**
84747
 
** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
84748
 
** in other words if the SELECT pulls all columns from a single table
84749
 
** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
84750
 
** if <table2> and <table1> are distinct tables but have identical
84751
 
** schemas, including all the same indices, then a special optimization
84752
 
** is invoked that copies raw records from <table2> over to <table1>.
84753
 
** See the xferOptimization() function for the implementation of this
84754
 
** template.  This is the 2nd template.
84755
 
**
84756
 
**         open a write cursor to <table>
84757
 
**         open read cursor on <table2>
84758
 
**         transfer all records in <table2> over to <table>
84759
 
**         close cursors
84760
 
**         foreach index on <table>
84761
 
**           open a write cursor on the <table> index
84762
 
**           open a read cursor on the corresponding <table2> index
84763
 
**           transfer all records from the read to the write cursors
84764
 
**           close cursors
84765
 
**         end foreach
84766
 
**
84767
 
** The 3rd template is for when the second template does not apply
84768
 
** and the SELECT clause does not read from <table> at any time.
84769
 
** The generated code follows this template:
84770
 
**
84771
 
**         EOF <- 0
84772
 
**         X <- A
84773
 
**         goto B
84774
 
**      A: setup for the SELECT
84775
 
**         loop over the rows in the SELECT
84776
 
**           load values into registers R..R+n
84777
 
**           yield X
84778
 
**         end loop
84779
 
**         cleanup after the SELECT
84780
 
**         EOF <- 1
84781
 
**         yield X
84782
 
**         goto A
84783
 
**      B: open write cursor to <table> and its indices
84784
 
**      C: yield X
84785
 
**         if EOF goto D
84786
 
**         insert the select result into <table> from R..R+n
84787
 
**         goto C
84788
 
**      D: cleanup
84789
 
**
84790
 
** The 4th template is used if the insert statement takes its
84791
 
** values from a SELECT but the data is being inserted into a table
84792
 
** that is also read as part of the SELECT.  In the third form,
84793
 
** we have to use a intermediate table to store the results of
84794
 
** the select.  The template is like this:
84795
 
**
84796
 
**         EOF <- 0
84797
 
**         X <- A
84798
 
**         goto B
84799
 
**      A: setup for the SELECT
84800
 
**         loop over the tables in the SELECT
84801
 
**           load value into register R..R+n
84802
 
**           yield X
84803
 
**         end loop
84804
 
**         cleanup after the SELECT
84805
 
**         EOF <- 1
84806
 
**         yield X
84807
 
**         halt-error
84808
 
**      B: open temp table
84809
 
**      L: yield X
84810
 
**         if EOF goto M
84811
 
**         insert row from R..R+n into temp table
84812
 
**         goto L
84813
 
**      M: open write cursor to <table> and its indices
84814
 
**         rewind temp table
84815
 
**      C: loop over rows of intermediate table
84816
 
**           transfer values form intermediate table into <table>
84817
 
**         end loop
84818
 
**      D: cleanup
84819
 
*/
84820
 
SQLITE_PRIVATE void sqlite3Insert(
84821
 
  Parse *pParse,        /* Parser context */
84822
 
  SrcList *pTabList,    /* Name of table into which we are inserting */
84823
 
  ExprList *pList,      /* List of values to be inserted */
84824
 
  Select *pSelect,      /* A SELECT statement to use as the data source */
84825
 
  IdList *pColumn,      /* Column names corresponding to IDLIST. */
84826
 
  int onError           /* How to handle constraint errors */
84827
 
){
84828
 
  sqlite3 *db;          /* The main database structure */
84829
 
  Table *pTab;          /* The table to insert into.  aka TABLE */
84830
 
  char *zTab;           /* Name of the table into which we are inserting */
84831
 
  const char *zDb;      /* Name of the database holding this table */
84832
 
  int i, j, idx;        /* Loop counters */
84833
 
  Vdbe *v;              /* Generate code into this virtual machine */
84834
 
  Index *pIdx;          /* For looping over indices of the table */
84835
 
  int nColumn;          /* Number of columns in the data */
84836
 
  int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
84837
 
  int baseCur = 0;      /* VDBE Cursor number for pTab */
84838
 
  int keyColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
84839
 
  int endOfLoop;        /* Label for the end of the insertion loop */
84840
 
  int useTempTable = 0; /* Store SELECT results in intermediate table */
84841
 
  int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
84842
 
  int addrInsTop = 0;   /* Jump to label "D" */
84843
 
  int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
84844
 
  int addrSelect = 0;   /* Address of coroutine that implements the SELECT */
84845
 
  SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
84846
 
  int iDb;              /* Index of database holding TABLE */
84847
 
  Db *pDb;              /* The database containing table being inserted into */
84848
 
  int appendFlag = 0;   /* True if the insert is likely to be an append */
84849
 
 
84850
 
  /* Register allocations */
84851
 
  int regFromSelect = 0;/* Base register for data coming from SELECT */
84852
 
  int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
84853
 
  int regRowCount = 0;  /* Memory cell used for the row counter */
84854
 
  int regIns;           /* Block of regs holding rowid+data being inserted */
84855
 
  int regRowid;         /* registers holding insert rowid */
84856
 
  int regData;          /* register holding first column to insert */
84857
 
  int regEof = 0;       /* Register recording end of SELECT data */
84858
 
  int *aRegIdx = 0;     /* One register allocated to each index */
84859
 
 
84860
 
#ifndef SQLITE_OMIT_TRIGGER
84861
 
  int isView;                 /* True if attempting to insert into a view */
84862
 
  Trigger *pTrigger;          /* List of triggers on pTab, if required */
84863
 
  int tmask;                  /* Mask of trigger times */
84864
 
#endif
84865
 
 
84866
 
  db = pParse->db;
84867
 
  memset(&dest, 0, sizeof(dest));
84868
 
  if( pParse->nErr || db->mallocFailed ){
84869
 
    goto insert_cleanup;
84870
 
  }
84871
 
 
84872
 
  /* Locate the table into which we will be inserting new information.
84873
 
  */
84874
 
  assert( pTabList->nSrc==1 );
84875
 
  zTab = pTabList->a[0].zName;
84876
 
  if( NEVER(zTab==0) ) goto insert_cleanup;
84877
 
  pTab = sqlite3SrcListLookup(pParse, pTabList);
84878
 
  if( pTab==0 ){
84879
 
    goto insert_cleanup;
84880
 
  }
84881
 
  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
84882
 
  assert( iDb<db->nDb );
84883
 
  pDb = &db->aDb[iDb];
84884
 
  zDb = pDb->zName;
84885
 
  if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
84886
 
    goto insert_cleanup;
84887
 
  }
84888
 
 
84889
 
  /* Figure out if we have any triggers and if the table being
84890
 
  ** inserted into is a view
84891
 
  */
84892
 
#ifndef SQLITE_OMIT_TRIGGER
84893
 
  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
84894
 
  isView = pTab->pSelect!=0;
84895
 
#else
84896
 
# define pTrigger 0
84897
 
# define tmask 0
84898
 
# define isView 0
84899
 
#endif
84900
 
#ifdef SQLITE_OMIT_VIEW
84901
 
# undef isView
84902
 
# define isView 0
84903
 
#endif
84904
 
  assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
84905
 
 
84906
 
  /* If pTab is really a view, make sure it has been initialized.
84907
 
  ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual 
84908
 
  ** module table).
84909
 
  */
84910
 
  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
84911
 
    goto insert_cleanup;
84912
 
  }
84913
 
 
84914
 
  /* Ensure that:
84915
 
  *  (a) the table is not read-only, 
84916
 
  *  (b) that if it is a view then ON INSERT triggers exist
84917
 
  */
84918
 
  if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
84919
 
    goto insert_cleanup;
84920
 
  }
84921
 
 
84922
 
  /* Allocate a VDBE
84923
 
  */
84924
 
  v = sqlite3GetVdbe(pParse);
84925
 
  if( v==0 ) goto insert_cleanup;
84926
 
  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
84927
 
  sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
84928
 
 
84929
 
#ifndef SQLITE_OMIT_XFER_OPT
84930
 
  /* If the statement is of the form
84931
 
  **
84932
 
  **       INSERT INTO <table1> SELECT * FROM <table2>;
84933
 
  **
84934
 
  ** Then special optimizations can be applied that make the transfer
84935
 
  ** very fast and which reduce fragmentation of indices.
84936
 
  **
84937
 
  ** This is the 2nd template.
84938
 
  */
84939
 
  if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
84940
 
    assert( !pTrigger );
84941
 
    assert( pList==0 );
84942
 
    goto insert_end;
84943
 
  }
84944
 
#endif /* SQLITE_OMIT_XFER_OPT */
84945
 
 
84946
 
  /* If this is an AUTOINCREMENT table, look up the sequence number in the
84947
 
  ** sqlite_sequence table and store it in memory cell regAutoinc.
84948
 
  */
84949
 
  regAutoinc = autoIncBegin(pParse, iDb, pTab);
84950
 
 
84951
 
  /* Figure out how many columns of data are supplied.  If the data
84952
 
  ** is coming from a SELECT statement, then generate a co-routine that
84953
 
  ** produces a single row of the SELECT on each invocation.  The
84954
 
  ** co-routine is the common header to the 3rd and 4th templates.
84955
 
  */
84956
 
  if( pSelect ){
84957
 
    /* Data is coming from a SELECT.  Generate code to implement that SELECT
84958
 
    ** as a co-routine.  The code is common to both the 3rd and 4th
84959
 
    ** templates:
84960
 
    **
84961
 
    **         EOF <- 0
84962
 
    **         X <- A
84963
 
    **         goto B
84964
 
    **      A: setup for the SELECT
84965
 
    **         loop over the tables in the SELECT
84966
 
    **           load value into register R..R+n
84967
 
    **           yield X
84968
 
    **         end loop
84969
 
    **         cleanup after the SELECT
84970
 
    **         EOF <- 1
84971
 
    **         yield X
84972
 
    **         halt-error
84973
 
    **
84974
 
    ** On each invocation of the co-routine, it puts a single row of the
84975
 
    ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1.
84976
 
    ** (These output registers are allocated by sqlite3Select().)  When
84977
 
    ** the SELECT completes, it sets the EOF flag stored in regEof.
84978
 
    */
84979
 
    int rc, j1;
84980
 
 
84981
 
    regEof = ++pParse->nMem;
84982
 
    sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);      /* EOF <- 0 */
84983
 
    VdbeComment((v, "SELECT eof flag"));
84984
 
    sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem);
84985
 
    addrSelect = sqlite3VdbeCurrentAddr(v)+2;
84986
 
    sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm);
84987
 
    j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
84988
 
    VdbeComment((v, "Jump over SELECT coroutine"));
84989
 
 
84990
 
    /* Resolve the expressions in the SELECT statement and execute it. */
84991
 
    rc = sqlite3Select(pParse, pSelect, &dest);
84992
 
    assert( pParse->nErr==0 || rc );
84993
 
    if( rc || NEVER(pParse->nErr) || db->mallocFailed ){
84994
 
      goto insert_cleanup;
84995
 
    }
84996
 
    sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);         /* EOF <- 1 */
84997
 
    sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);   /* yield X */
84998
 
    sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
84999
 
    VdbeComment((v, "End of SELECT coroutine"));
85000
 
    sqlite3VdbeJumpHere(v, j1);                          /* label B: */
85001
 
 
85002
 
    regFromSelect = dest.iMem;
85003
 
    assert( pSelect->pEList );
85004
 
    nColumn = pSelect->pEList->nExpr;
85005
 
    assert( dest.nMem==nColumn );
85006
 
 
85007
 
    /* Set useTempTable to TRUE if the result of the SELECT statement
85008
 
    ** should be written into a temporary table (template 4).  Set to
85009
 
    ** FALSE if each* row of the SELECT can be written directly into
85010
 
    ** the destination table (template 3).
85011
 
    **
85012
 
    ** A temp table must be used if the table being updated is also one
85013
 
    ** of the tables being read by the SELECT statement.  Also use a 
85014
 
    ** temp table in the case of row triggers.
85015
 
    */
85016
 
    if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){
85017
 
      useTempTable = 1;
85018
 
    }
85019
 
 
85020
 
    if( useTempTable ){
85021
 
      /* Invoke the coroutine to extract information from the SELECT
85022
 
      ** and add it to a transient table srcTab.  The code generated
85023
 
      ** here is from the 4th template:
85024
 
      **
85025
 
      **      B: open temp table
85026
 
      **      L: yield X
85027
 
      **         if EOF goto M
85028
 
      **         insert row from R..R+n into temp table
85029
 
      **         goto L
85030
 
      **      M: ...
85031
 
      */
85032
 
      int regRec;          /* Register to hold packed record */
85033
 
      int regTempRowid;    /* Register to hold temp table ROWID */
85034
 
      int addrTop;         /* Label "L" */
85035
 
      int addrIf;          /* Address of jump to M */
85036
 
 
85037
 
      srcTab = pParse->nTab++;
85038
 
      regRec = sqlite3GetTempReg(pParse);
85039
 
      regTempRowid = sqlite3GetTempReg(pParse);
85040
 
      sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
85041
 
      addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
85042
 
      addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
85043
 
      sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
85044
 
      sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
85045
 
      sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
85046
 
      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
85047
 
      sqlite3VdbeJumpHere(v, addrIf);
85048
 
      sqlite3ReleaseTempReg(pParse, regRec);
85049
 
      sqlite3ReleaseTempReg(pParse, regTempRowid);
85050
 
    }
85051
 
  }else{
85052
 
    /* This is the case if the data for the INSERT is coming from a VALUES
85053
 
    ** clause
85054
 
    */
85055
 
    NameContext sNC;
85056
 
    memset(&sNC, 0, sizeof(sNC));
85057
 
    sNC.pParse = pParse;
85058
 
    srcTab = -1;
85059
 
    assert( useTempTable==0 );
85060
 
    nColumn = pList ? pList->nExpr : 0;
85061
 
    for(i=0; i<nColumn; i++){
85062
 
      if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
85063
 
        goto insert_cleanup;
85064
 
      }
85065
 
    }
85066
 
  }
85067
 
 
85068
 
  /* Make sure the number of columns in the source data matches the number
85069
 
  ** of columns to be inserted into the table.
85070
 
  */
85071
 
  if( IsVirtual(pTab) ){
85072
 
    for(i=0; i<pTab->nCol; i++){
85073
 
      nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
85074
 
    }
85075
 
  }
85076
 
  if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
85077
 
    sqlite3ErrorMsg(pParse, 
85078
 
       "table %S has %d columns but %d values were supplied",
85079
 
       pTabList, 0, pTab->nCol-nHidden, nColumn);
85080
 
    goto insert_cleanup;
85081
 
  }
85082
 
  if( pColumn!=0 && nColumn!=pColumn->nId ){
85083
 
    sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
85084
 
    goto insert_cleanup;
85085
 
  }
85086
 
 
85087
 
  /* If the INSERT statement included an IDLIST term, then make sure
85088
 
  ** all elements of the IDLIST really are columns of the table and 
85089
 
  ** remember the column indices.
85090
 
  **
85091
 
  ** If the table has an INTEGER PRIMARY KEY column and that column
85092
 
  ** is named in the IDLIST, then record in the keyColumn variable
85093
 
  ** the index into IDLIST of the primary key column.  keyColumn is
85094
 
  ** the index of the primary key as it appears in IDLIST, not as
85095
 
  ** is appears in the original table.  (The index of the primary
85096
 
  ** key in the original table is pTab->iPKey.)
85097
 
  */
85098
 
  if( pColumn ){
85099
 
    for(i=0; i<pColumn->nId; i++){
85100
 
      pColumn->a[i].idx = -1;
85101
 
    }
85102
 
    for(i=0; i<pColumn->nId; i++){
85103
 
      for(j=0; j<pTab->nCol; j++){
85104
 
        if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
85105
 
          pColumn->a[i].idx = j;
85106
 
          if( j==pTab->iPKey ){
85107
 
            keyColumn = i;
85108
 
          }
85109
 
          break;
85110
 
        }
85111
 
      }
85112
 
      if( j>=pTab->nCol ){
85113
 
        if( sqlite3IsRowid(pColumn->a[i].zName) ){
85114
 
          keyColumn = i;
85115
 
        }else{
85116
 
          sqlite3ErrorMsg(pParse, "table %S has no column named %s",
85117
 
              pTabList, 0, pColumn->a[i].zName);
85118
 
          pParse->checkSchema = 1;
85119
 
          goto insert_cleanup;
85120
 
        }
85121
 
      }
85122
 
    }
85123
 
  }
85124
 
 
85125
 
  /* If there is no IDLIST term but the table has an integer primary
85126
 
  ** key, the set the keyColumn variable to the primary key column index
85127
 
  ** in the original table definition.
85128
 
  */
85129
 
  if( pColumn==0 && nColumn>0 ){
85130
 
    keyColumn = pTab->iPKey;
85131
 
  }
85132
 
    
85133
 
  /* Initialize the count of rows to be inserted
85134
 
  */
85135
 
  if( db->flags & SQLITE_CountRows ){
85136
 
    regRowCount = ++pParse->nMem;
85137
 
    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
85138
 
  }
85139
 
 
85140
 
  /* If this is not a view, open the table and and all indices */
85141
 
  if( !isView ){
85142
 
    int nIdx;
85143
 
 
85144
 
    baseCur = pParse->nTab;
85145
 
    nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite);
85146
 
    aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
85147
 
    if( aRegIdx==0 ){
85148
 
      goto insert_cleanup;
85149
 
    }
85150
 
    for(i=0; i<nIdx; i++){
85151
 
      aRegIdx[i] = ++pParse->nMem;
85152
 
    }
85153
 
  }
85154
 
 
85155
 
  /* This is the top of the main insertion loop */
85156
 
  if( useTempTable ){
85157
 
    /* This block codes the top of loop only.  The complete loop is the
85158
 
    ** following pseudocode (template 4):
85159
 
    **
85160
 
    **         rewind temp table
85161
 
    **      C: loop over rows of intermediate table
85162
 
    **           transfer values form intermediate table into <table>
85163
 
    **         end loop
85164
 
    **      D: ...
85165
 
    */
85166
 
    addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
85167
 
    addrCont = sqlite3VdbeCurrentAddr(v);
85168
 
  }else if( pSelect ){
85169
 
    /* This block codes the top of loop only.  The complete loop is the
85170
 
    ** following pseudocode (template 3):
85171
 
    **
85172
 
    **      C: yield X
85173
 
    **         if EOF goto D
85174
 
    **         insert the select result into <table> from R..R+n
85175
 
    **         goto C
85176
 
    **      D: ...
85177
 
    */
85178
 
    addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
85179
 
    addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
85180
 
  }
85181
 
 
85182
 
  /* Allocate registers for holding the rowid of the new row,
85183
 
  ** the content of the new row, and the assemblied row record.
85184
 
  */
85185
 
  regRowid = regIns = pParse->nMem+1;
85186
 
  pParse->nMem += pTab->nCol + 1;
85187
 
  if( IsVirtual(pTab) ){
85188
 
    regRowid++;
85189
 
    pParse->nMem++;
85190
 
  }
85191
 
  regData = regRowid+1;
85192
 
 
85193
 
  /* Run the BEFORE and INSTEAD OF triggers, if there are any
85194
 
  */
85195
 
  endOfLoop = sqlite3VdbeMakeLabel(v);
85196
 
  if( tmask & TRIGGER_BEFORE ){
85197
 
    int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
85198
 
 
85199
 
    /* build the NEW.* reference row.  Note that if there is an INTEGER
85200
 
    ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
85201
 
    ** translated into a unique ID for the row.  But on a BEFORE trigger,
85202
 
    ** we do not know what the unique ID will be (because the insert has
85203
 
    ** not happened yet) so we substitute a rowid of -1
85204
 
    */
85205
 
    if( keyColumn<0 ){
85206
 
      sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
85207
 
    }else{
85208
 
      int j1;
85209
 
      if( useTempTable ){
85210
 
        sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regCols);
85211
 
      }else{
85212
 
        assert( pSelect==0 );  /* Otherwise useTempTable is true */
85213
 
        sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regCols);
85214
 
      }
85215
 
      j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols);
85216
 
      sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
85217
 
      sqlite3VdbeJumpHere(v, j1);
85218
 
      sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols);
85219
 
    }
85220
 
 
85221
 
    /* Cannot have triggers on a virtual table. If it were possible,
85222
 
    ** this block would have to account for hidden column.
85223
 
    */
85224
 
    assert( !IsVirtual(pTab) );
85225
 
 
85226
 
    /* Create the new column data
85227
 
    */
85228
 
    for(i=0; i<pTab->nCol; i++){
85229
 
      if( pColumn==0 ){
85230
 
        j = i;
85231
 
      }else{
85232
 
        for(j=0; j<pColumn->nId; j++){
85233
 
          if( pColumn->a[j].idx==i ) break;
85234
 
        }
85235
 
      }
85236
 
      if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){
85237
 
        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
85238
 
      }else if( useTempTable ){
85239
 
        sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); 
85240
 
      }else{
85241
 
        assert( pSelect==0 ); /* Otherwise useTempTable is true */
85242
 
        sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
85243
 
      }
85244
 
    }
85245
 
 
85246
 
    /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
85247
 
    ** do not attempt any conversions before assembling the record.
85248
 
    ** If this is a real table, attempt conversions as required by the
85249
 
    ** table column affinities.
85250
 
    */
85251
 
    if( !isView ){
85252
 
      sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
85253
 
      sqlite3TableAffinityStr(v, pTab);
85254
 
    }
85255
 
 
85256
 
    /* Fire BEFORE or INSTEAD OF triggers */
85257
 
    sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, 
85258
 
        pTab, regCols-pTab->nCol-1, onError, endOfLoop);
85259
 
 
85260
 
    sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
85261
 
  }
85262
 
 
85263
 
  /* Push the record number for the new entry onto the stack.  The
85264
 
  ** record number is a randomly generate integer created by NewRowid
85265
 
  ** except when the table has an INTEGER PRIMARY KEY column, in which
85266
 
  ** case the record number is the same as that column. 
85267
 
  */
85268
 
  if( !isView ){
85269
 
    if( IsVirtual(pTab) ){
85270
 
      /* The row that the VUpdate opcode will delete: none */
85271
 
      sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
85272
 
    }
85273
 
    if( keyColumn>=0 ){
85274
 
      if( useTempTable ){
85275
 
        sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid);
85276
 
      }else if( pSelect ){
85277
 
        sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid);
85278
 
      }else{
85279
 
        VdbeOp *pOp;
85280
 
        sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid);
85281
 
        pOp = sqlite3VdbeGetOp(v, -1);
85282
 
        if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
85283
 
          appendFlag = 1;
85284
 
          pOp->opcode = OP_NewRowid;
85285
 
          pOp->p1 = baseCur;
85286
 
          pOp->p2 = regRowid;
85287
 
          pOp->p3 = regAutoinc;
85288
 
        }
85289
 
      }
85290
 
      /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
85291
 
      ** to generate a unique primary key value.
85292
 
      */
85293
 
      if( !appendFlag ){
85294
 
        int j1;
85295
 
        if( !IsVirtual(pTab) ){
85296
 
          j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
85297
 
          sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
85298
 
          sqlite3VdbeJumpHere(v, j1);
85299
 
        }else{
85300
 
          j1 = sqlite3VdbeCurrentAddr(v);
85301
 
          sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
85302
 
        }
85303
 
        sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
85304
 
      }
85305
 
    }else if( IsVirtual(pTab) ){
85306
 
      sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
85307
 
    }else{
85308
 
      sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
85309
 
      appendFlag = 1;
85310
 
    }
85311
 
    autoIncStep(pParse, regAutoinc, regRowid);
85312
 
 
85313
 
    /* Push onto the stack, data for all columns of the new entry, beginning
85314
 
    ** with the first column.
85315
 
    */
85316
 
    nHidden = 0;
85317
 
    for(i=0; i<pTab->nCol; i++){
85318
 
      int iRegStore = regRowid+1+i;
85319
 
      if( i==pTab->iPKey ){
85320
 
        /* The value of the INTEGER PRIMARY KEY column is always a NULL.
85321
 
        ** Whenever this column is read, the record number will be substituted
85322
 
        ** in its place.  So will fill this column with a NULL to avoid
85323
 
        ** taking up data space with information that will never be used. */
85324
 
        sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
85325
 
        continue;
85326
 
      }
85327
 
      if( pColumn==0 ){
85328
 
        if( IsHiddenColumn(&pTab->aCol[i]) ){
85329
 
          assert( IsVirtual(pTab) );
85330
 
          j = -1;
85331
 
          nHidden++;
85332
 
        }else{
85333
 
          j = i - nHidden;
85334
 
        }
85335
 
      }else{
85336
 
        for(j=0; j<pColumn->nId; j++){
85337
 
          if( pColumn->a[j].idx==i ) break;
85338
 
        }
85339
 
      }
85340
 
      if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
85341
 
        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
85342
 
      }else if( useTempTable ){
85343
 
        sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); 
85344
 
      }else if( pSelect ){
85345
 
        sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
85346
 
      }else{
85347
 
        sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
85348
 
      }
85349
 
    }
85350
 
 
85351
 
    /* Generate code to check constraints and generate index keys and
85352
 
    ** do the insertion.
85353
 
    */
85354
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
85355
 
    if( IsVirtual(pTab) ){
85356
 
      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
85357
 
      sqlite3VtabMakeWritable(pParse, pTab);
85358
 
      sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
85359
 
      sqlite3MayAbort(pParse);
85360
 
    }else
85361
 
#endif
85362
 
    {
85363
 
      int isReplace;    /* Set to true if constraints may cause a replace */
85364
 
      sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx,
85365
 
          keyColumn>=0, 0, onError, endOfLoop, &isReplace
85366
 
      );
85367
 
      sqlite3FkCheck(pParse, pTab, 0, regIns);
85368
 
      sqlite3CompleteInsertion(
85369
 
          pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0
85370
 
      );
85371
 
    }
85372
 
  }
85373
 
 
85374
 
  /* Update the count of rows that are inserted
85375
 
  */
85376
 
  if( (db->flags & SQLITE_CountRows)!=0 ){
85377
 
    sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
85378
 
  }
85379
 
 
85380
 
  if( pTrigger ){
85381
 
    /* Code AFTER triggers */
85382
 
    sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, 
85383
 
        pTab, regData-2-pTab->nCol, onError, endOfLoop);
85384
 
  }
85385
 
 
85386
 
  /* The bottom of the main insertion loop, if the data source
85387
 
  ** is a SELECT statement.
85388
 
  */
85389
 
  sqlite3VdbeResolveLabel(v, endOfLoop);
85390
 
  if( useTempTable ){
85391
 
    sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
85392
 
    sqlite3VdbeJumpHere(v, addrInsTop);
85393
 
    sqlite3VdbeAddOp1(v, OP_Close, srcTab);
85394
 
  }else if( pSelect ){
85395
 
    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
85396
 
    sqlite3VdbeJumpHere(v, addrInsTop);
85397
 
  }
85398
 
 
85399
 
  if( !IsVirtual(pTab) && !isView ){
85400
 
    /* Close all tables opened */
85401
 
    sqlite3VdbeAddOp1(v, OP_Close, baseCur);
85402
 
    for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
85403
 
      sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
85404
 
    }
85405
 
  }
85406
 
 
85407
 
insert_end:
85408
 
  /* Update the sqlite_sequence table by storing the content of the
85409
 
  ** maximum rowid counter values recorded while inserting into
85410
 
  ** autoincrement tables.
85411
 
  */
85412
 
  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
85413
 
    sqlite3AutoincrementEnd(pParse);
85414
 
  }
85415
 
 
85416
 
  /*
85417
 
  ** Return the number of rows inserted. If this routine is 
85418
 
  ** generating code because of a call to sqlite3NestedParse(), do not
85419
 
  ** invoke the callback function.
85420
 
  */
85421
 
  if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
85422
 
    sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
85423
 
    sqlite3VdbeSetNumCols(v, 1);
85424
 
    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
85425
 
  }
85426
 
 
85427
 
insert_cleanup:
85428
 
  sqlite3SrcListDelete(db, pTabList);
85429
 
  sqlite3ExprListDelete(db, pList);
85430
 
  sqlite3SelectDelete(db, pSelect);
85431
 
  sqlite3IdListDelete(db, pColumn);
85432
 
  sqlite3DbFree(db, aRegIdx);
85433
 
}
85434
 
 
85435
 
/* Make sure "isView" and other macros defined above are undefined. Otherwise
85436
 
** thely may interfere with compilation of other functions in this file
85437
 
** (or in another file, if this file becomes part of the amalgamation).  */
85438
 
#ifdef isView
85439
 
 #undef isView
85440
 
#endif
85441
 
#ifdef pTrigger
85442
 
 #undef pTrigger
85443
 
#endif
85444
 
#ifdef tmask
85445
 
 #undef tmask
85446
 
#endif
85447
 
 
85448
 
 
85449
 
/*
85450
 
** Generate code to do constraint checks prior to an INSERT or an UPDATE.
85451
 
**
85452
 
** The input is a range of consecutive registers as follows:
85453
 
**
85454
 
**    1.  The rowid of the row after the update.
85455
 
**
85456
 
**    2.  The data in the first column of the entry after the update.
85457
 
**
85458
 
**    i.  Data from middle columns...
85459
 
**
85460
 
**    N.  The data in the last column of the entry after the update.
85461
 
**
85462
 
** The regRowid parameter is the index of the register containing (1).
85463
 
**
85464
 
** If isUpdate is true and rowidChng is non-zero, then rowidChng contains
85465
 
** the address of a register containing the rowid before the update takes
85466
 
** place. isUpdate is true for UPDATEs and false for INSERTs. If isUpdate
85467
 
** is false, indicating an INSERT statement, then a non-zero rowidChng 
85468
 
** indicates that the rowid was explicitly specified as part of the
85469
 
** INSERT statement. If rowidChng is false, it means that  the rowid is
85470
 
** computed automatically in an insert or that the rowid value is not 
85471
 
** modified by an update.
85472
 
**
85473
 
** The code generated by this routine store new index entries into
85474
 
** registers identified by aRegIdx[].  No index entry is created for
85475
 
** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
85476
 
** the same as the order of indices on the linked list of indices
85477
 
** attached to the table.
85478
 
**
85479
 
** This routine also generates code to check constraints.  NOT NULL,
85480
 
** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
85481
 
** then the appropriate action is performed.  There are five possible
85482
 
** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
85483
 
**
85484
 
**  Constraint type  Action       What Happens
85485
 
**  ---------------  ----------   ----------------------------------------
85486
 
**  any              ROLLBACK     The current transaction is rolled back and
85487
 
**                                sqlite3_exec() returns immediately with a
85488
 
**                                return code of SQLITE_CONSTRAINT.
85489
 
**
85490
 
**  any              ABORT        Back out changes from the current command
85491
 
**                                only (do not do a complete rollback) then
85492
 
**                                cause sqlite3_exec() to return immediately
85493
 
**                                with SQLITE_CONSTRAINT.
85494
 
**
85495
 
**  any              FAIL         Sqlite_exec() returns immediately with a
85496
 
**                                return code of SQLITE_CONSTRAINT.  The
85497
 
**                                transaction is not rolled back and any
85498
 
**                                prior changes are retained.
85499
 
**
85500
 
**  any              IGNORE       The record number and data is popped from
85501
 
**                                the stack and there is an immediate jump
85502
 
**                                to label ignoreDest.
85503
 
**
85504
 
**  NOT NULL         REPLACE      The NULL value is replace by the default
85505
 
**                                value for that column.  If the default value
85506
 
**                                is NULL, the action is the same as ABORT.
85507
 
**
85508
 
**  UNIQUE           REPLACE      The other row that conflicts with the row
85509
 
**                                being inserted is removed.
85510
 
**
85511
 
**  CHECK            REPLACE      Illegal.  The results in an exception.
85512
 
**
85513
 
** Which action to take is determined by the overrideError parameter.
85514
 
** Or if overrideError==OE_Default, then the pParse->onError parameter
85515
 
** is used.  Or if pParse->onError==OE_Default then the onError value
85516
 
** for the constraint is used.
85517
 
**
85518
 
** The calling routine must open a read/write cursor for pTab with
85519
 
** cursor number "baseCur".  All indices of pTab must also have open
85520
 
** read/write cursors with cursor number baseCur+i for the i-th cursor.
85521
 
** Except, if there is no possibility of a REPLACE action then
85522
 
** cursors do not need to be open for indices where aRegIdx[i]==0.
85523
 
*/
85524
 
SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
85525
 
  Parse *pParse,      /* The parser context */
85526
 
  Table *pTab,        /* the table into which we are inserting */
85527
 
  int baseCur,        /* Index of a read/write cursor pointing at pTab */
85528
 
  int regRowid,       /* Index of the range of input registers */
85529
 
  int *aRegIdx,       /* Register used by each index.  0 for unused indices */
85530
 
  int rowidChng,      /* True if the rowid might collide with existing entry */
85531
 
  int isUpdate,       /* True for UPDATE, False for INSERT */
85532
 
  int overrideError,  /* Override onError to this if not OE_Default */
85533
 
  int ignoreDest,     /* Jump to this label on an OE_Ignore resolution */
85534
 
  int *pbMayReplace   /* OUT: Set to true if constraint may cause a replace */
85535
 
){
85536
 
  int i;              /* loop counter */
85537
 
  Vdbe *v;            /* VDBE under constrution */
85538
 
  int nCol;           /* Number of columns */
85539
 
  int onError;        /* Conflict resolution strategy */
85540
 
  int j1;             /* Addresss of jump instruction */
85541
 
  int j2 = 0, j3;     /* Addresses of jump instructions */
85542
 
  int regData;        /* Register containing first data column */
85543
 
  int iCur;           /* Table cursor number */
85544
 
  Index *pIdx;         /* Pointer to one of the indices */
85545
 
  int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
85546
 
  int regOldRowid = (rowidChng && isUpdate) ? rowidChng : regRowid;
85547
 
 
85548
 
  v = sqlite3GetVdbe(pParse);
85549
 
  assert( v!=0 );
85550
 
  assert( pTab->pSelect==0 );  /* This table is not a VIEW */
85551
 
  nCol = pTab->nCol;
85552
 
  regData = regRowid + 1;
85553
 
 
85554
 
  /* Test all NOT NULL constraints.
85555
 
  */
85556
 
  for(i=0; i<nCol; i++){
85557
 
    if( i==pTab->iPKey ){
85558
 
      continue;
85559
 
    }
85560
 
    onError = pTab->aCol[i].notNull;
85561
 
    if( onError==OE_None ) continue;
85562
 
    if( overrideError!=OE_Default ){
85563
 
      onError = overrideError;
85564
 
    }else if( onError==OE_Default ){
85565
 
      onError = OE_Abort;
85566
 
    }
85567
 
    if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
85568
 
      onError = OE_Abort;
85569
 
    }
85570
 
    assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
85571
 
        || onError==OE_Ignore || onError==OE_Replace );
85572
 
    switch( onError ){
85573
 
      case OE_Abort:
85574
 
        sqlite3MayAbort(pParse);
85575
 
      case OE_Rollback:
85576
 
      case OE_Fail: {
85577
 
        char *zMsg;
85578
 
        sqlite3VdbeAddOp3(v, OP_HaltIfNull,
85579
 
                                  SQLITE_CONSTRAINT, onError, regData+i);
85580
 
        zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL",
85581
 
                              pTab->zName, pTab->aCol[i].zName);
85582
 
        sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
85583
 
        break;
85584
 
      }
85585
 
      case OE_Ignore: {
85586
 
        sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest);
85587
 
        break;
85588
 
      }
85589
 
      default: {
85590
 
        assert( onError==OE_Replace );
85591
 
        j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i);
85592
 
        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i);
85593
 
        sqlite3VdbeJumpHere(v, j1);
85594
 
        break;
85595
 
      }
85596
 
    }
85597
 
  }
85598
 
 
85599
 
  /* Test all CHECK constraints
85600
 
  */
85601
 
#ifndef SQLITE_OMIT_CHECK
85602
 
  if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
85603
 
    int allOk = sqlite3VdbeMakeLabel(v);
85604
 
    pParse->ckBase = regData;
85605
 
    sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL);
85606
 
    onError = overrideError!=OE_Default ? overrideError : OE_Abort;
85607
 
    if( onError==OE_Ignore ){
85608
 
      sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
85609
 
    }else{
85610
 
      if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
85611
 
      sqlite3HaltConstraint(pParse, onError, 0, 0);
85612
 
    }
85613
 
    sqlite3VdbeResolveLabel(v, allOk);
85614
 
  }
85615
 
#endif /* !defined(SQLITE_OMIT_CHECK) */
85616
 
 
85617
 
  /* If we have an INTEGER PRIMARY KEY, make sure the primary key
85618
 
  ** of the new record does not previously exist.  Except, if this
85619
 
  ** is an UPDATE and the primary key is not changing, that is OK.
85620
 
  */
85621
 
  if( rowidChng ){
85622
 
    onError = pTab->keyConf;
85623
 
    if( overrideError!=OE_Default ){
85624
 
      onError = overrideError;
85625
 
    }else if( onError==OE_Default ){
85626
 
      onError = OE_Abort;
85627
 
    }
85628
 
    
85629
 
    if( isUpdate ){
85630
 
      j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng);
85631
 
    }
85632
 
    j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
85633
 
    switch( onError ){
85634
 
      default: {
85635
 
        onError = OE_Abort;
85636
 
        /* Fall thru into the next case */
85637
 
      }
85638
 
      case OE_Rollback:
85639
 
      case OE_Abort:
85640
 
      case OE_Fail: {
85641
 
        sqlite3HaltConstraint(
85642
 
          pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
85643
 
        break;
85644
 
      }
85645
 
      case OE_Replace: {
85646
 
        /* If there are DELETE triggers on this table and the
85647
 
        ** recursive-triggers flag is set, call GenerateRowDelete() to
85648
 
        ** remove the conflicting row from the the table. This will fire
85649
 
        ** the triggers and remove both the table and index b-tree entries.
85650
 
        **
85651
 
        ** Otherwise, if there are no triggers or the recursive-triggers
85652
 
        ** flag is not set, but the table has one or more indexes, call 
85653
 
        ** GenerateRowIndexDelete(). This removes the index b-tree entries 
85654
 
        ** only. The table b-tree entry will be replaced by the new entry 
85655
 
        ** when it is inserted.  
85656
 
        **
85657
 
        ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
85658
 
        ** also invoke MultiWrite() to indicate that this VDBE may require
85659
 
        ** statement rollback (if the statement is aborted after the delete
85660
 
        ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
85661
 
        ** but being more selective here allows statements like:
85662
 
        **
85663
 
        **   REPLACE INTO t(rowid) VALUES($newrowid)
85664
 
        **
85665
 
        ** to run without a statement journal if there are no indexes on the
85666
 
        ** table.
85667
 
        */
85668
 
        Trigger *pTrigger = 0;
85669
 
        if( pParse->db->flags&SQLITE_RecTriggers ){
85670
 
          pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
85671
 
        }
85672
 
        if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
85673
 
          sqlite3MultiWrite(pParse);
85674
 
          sqlite3GenerateRowDelete(
85675
 
              pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
85676
 
          );
85677
 
        }else if( pTab->pIndex ){
85678
 
          sqlite3MultiWrite(pParse);
85679
 
          sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
85680
 
        }
85681
 
        seenReplace = 1;
85682
 
        break;
85683
 
      }
85684
 
      case OE_Ignore: {
85685
 
        assert( seenReplace==0 );
85686
 
        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
85687
 
        break;
85688
 
      }
85689
 
    }
85690
 
    sqlite3VdbeJumpHere(v, j3);
85691
 
    if( isUpdate ){
85692
 
      sqlite3VdbeJumpHere(v, j2);
85693
 
    }
85694
 
  }
85695
 
 
85696
 
  /* Test all UNIQUE constraints by creating entries for each UNIQUE
85697
 
  ** index and making sure that duplicate entries do not already exist.
85698
 
  ** Add the new records to the indices as we go.
85699
 
  */
85700
 
  for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
85701
 
    int regIdx;
85702
 
    int regR;
85703
 
 
85704
 
    if( aRegIdx[iCur]==0 ) continue;  /* Skip unused indices */
85705
 
 
85706
 
    /* Create a key for accessing the index entry */
85707
 
    regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
85708
 
    for(i=0; i<pIdx->nColumn; i++){
85709
 
      int idx = pIdx->aiColumn[i];
85710
 
      if( idx==pTab->iPKey ){
85711
 
        sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
85712
 
      }else{
85713
 
        sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
85714
 
      }
85715
 
    }
85716
 
    sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
85717
 
    sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
85718
 
    sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
85719
 
    sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
85720
 
 
85721
 
    /* Find out what action to take in case there is an indexing conflict */
85722
 
    onError = pIdx->onError;
85723
 
    if( onError==OE_None ){ 
85724
 
      sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
85725
 
      continue;  /* pIdx is not a UNIQUE index */
85726
 
    }
85727
 
    if( overrideError!=OE_Default ){
85728
 
      onError = overrideError;
85729
 
    }else if( onError==OE_Default ){
85730
 
      onError = OE_Abort;
85731
 
    }
85732
 
    if( seenReplace ){
85733
 
      if( onError==OE_Ignore ) onError = OE_Replace;
85734
 
      else if( onError==OE_Fail ) onError = OE_Abort;
85735
 
    }
85736
 
    
85737
 
    /* Check to see if the new index entry will be unique */
85738
 
    regR = sqlite3GetTempReg(pParse);
85739
 
    sqlite3VdbeAddOp2(v, OP_SCopy, regOldRowid, regR);
85740
 
    j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0,
85741
 
                           regR, SQLITE_INT_TO_PTR(regIdx),
85742
 
                           P4_INT32);
85743
 
    sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
85744
 
 
85745
 
    /* Generate code that executes if the new index entry is not unique */
85746
 
    assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
85747
 
        || onError==OE_Ignore || onError==OE_Replace );
85748
 
    switch( onError ){
85749
 
      case OE_Rollback:
85750
 
      case OE_Abort:
85751
 
      case OE_Fail: {
85752
 
        int j;
85753
 
        StrAccum errMsg;
85754
 
        const char *zSep;
85755
 
        char *zErr;
85756
 
 
85757
 
        sqlite3StrAccumInit(&errMsg, 0, 0, 200);
85758
 
        errMsg.db = pParse->db;
85759
 
        zSep = pIdx->nColumn>1 ? "columns " : "column ";
85760
 
        for(j=0; j<pIdx->nColumn; j++){
85761
 
          char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
85762
 
          sqlite3StrAccumAppend(&errMsg, zSep, -1);
85763
 
          zSep = ", ";
85764
 
          sqlite3StrAccumAppend(&errMsg, zCol, -1);
85765
 
        }
85766
 
        sqlite3StrAccumAppend(&errMsg,
85767
 
            pIdx->nColumn>1 ? " are not unique" : " is not unique", -1);
85768
 
        zErr = sqlite3StrAccumFinish(&errMsg);
85769
 
        sqlite3HaltConstraint(pParse, onError, zErr, 0);
85770
 
        sqlite3DbFree(errMsg.db, zErr);
85771
 
        break;
85772
 
      }
85773
 
      case OE_Ignore: {
85774
 
        assert( seenReplace==0 );
85775
 
        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
85776
 
        break;
85777
 
      }
85778
 
      default: {
85779
 
        Trigger *pTrigger = 0;
85780
 
        assert( onError==OE_Replace );
85781
 
        sqlite3MultiWrite(pParse);
85782
 
        if( pParse->db->flags&SQLITE_RecTriggers ){
85783
 
          pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
85784
 
        }
85785
 
        sqlite3GenerateRowDelete(
85786
 
            pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace
85787
 
        );
85788
 
        seenReplace = 1;
85789
 
        break;
85790
 
      }
85791
 
    }
85792
 
    sqlite3VdbeJumpHere(v, j3);
85793
 
    sqlite3ReleaseTempReg(pParse, regR);
85794
 
  }
85795
 
  
85796
 
  if( pbMayReplace ){
85797
 
    *pbMayReplace = seenReplace;
85798
 
  }
85799
 
}
85800
 
 
85801
 
/*
85802
 
** This routine generates code to finish the INSERT or UPDATE operation
85803
 
** that was started by a prior call to sqlite3GenerateConstraintChecks.
85804
 
** A consecutive range of registers starting at regRowid contains the
85805
 
** rowid and the content to be inserted.
85806
 
**
85807
 
** The arguments to this routine should be the same as the first six
85808
 
** arguments to sqlite3GenerateConstraintChecks.
85809
 
*/
85810
 
SQLITE_PRIVATE void sqlite3CompleteInsertion(
85811
 
  Parse *pParse,      /* The parser context */
85812
 
  Table *pTab,        /* the table into which we are inserting */
85813
 
  int baseCur,        /* Index of a read/write cursor pointing at pTab */
85814
 
  int regRowid,       /* Range of content */
85815
 
  int *aRegIdx,       /* Register used by each index.  0 for unused indices */
85816
 
  int isUpdate,       /* True for UPDATE, False for INSERT */
85817
 
  int appendBias,     /* True if this is likely to be an append */
85818
 
  int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
85819
 
){
85820
 
  int i;
85821
 
  Vdbe *v;
85822
 
  int nIdx;
85823
 
  Index *pIdx;
85824
 
  u8 pik_flags;
85825
 
  int regData;
85826
 
  int regRec;
85827
 
 
85828
 
  v = sqlite3GetVdbe(pParse);
85829
 
  assert( v!=0 );
85830
 
  assert( pTab->pSelect==0 );  /* This table is not a VIEW */
85831
 
  for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
85832
 
  for(i=nIdx-1; i>=0; i--){
85833
 
    if( aRegIdx[i]==0 ) continue;
85834
 
    sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]);
85835
 
    if( useSeekResult ){
85836
 
      sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
85837
 
    }
85838
 
  }
85839
 
  regData = regRowid + 1;
85840
 
  regRec = sqlite3GetTempReg(pParse);
85841
 
  sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
85842
 
  sqlite3TableAffinityStr(v, pTab);
85843
 
  sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
85844
 
  if( pParse->nested ){
85845
 
    pik_flags = 0;
85846
 
  }else{
85847
 
    pik_flags = OPFLAG_NCHANGE;
85848
 
    pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
85849
 
  }
85850
 
  if( appendBias ){
85851
 
    pik_flags |= OPFLAG_APPEND;
85852
 
  }
85853
 
  if( useSeekResult ){
85854
 
    pik_flags |= OPFLAG_USESEEKRESULT;
85855
 
  }
85856
 
  sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid);
85857
 
  if( !pParse->nested ){
85858
 
    sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
85859
 
  }
85860
 
  sqlite3VdbeChangeP5(v, pik_flags);
85861
 
}
85862
 
 
85863
 
/*
85864
 
** Generate code that will open cursors for a table and for all
85865
 
** indices of that table.  The "baseCur" parameter is the cursor number used
85866
 
** for the table.  Indices are opened on subsequent cursors.
85867
 
**
85868
 
** Return the number of indices on the table.
85869
 
*/
85870
 
SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
85871
 
  Parse *pParse,   /* Parsing context */
85872
 
  Table *pTab,     /* Table to be opened */
85873
 
  int baseCur,     /* Cursor number assigned to the table */
85874
 
  int op           /* OP_OpenRead or OP_OpenWrite */
85875
 
){
85876
 
  int i;
85877
 
  int iDb;
85878
 
  Index *pIdx;
85879
 
  Vdbe *v;
85880
 
 
85881
 
  if( IsVirtual(pTab) ) return 0;
85882
 
  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
85883
 
  v = sqlite3GetVdbe(pParse);
85884
 
  assert( v!=0 );
85885
 
  sqlite3OpenTable(pParse, baseCur, iDb, pTab, op);
85886
 
  for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
85887
 
    KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
85888
 
    assert( pIdx->pSchema==pTab->pSchema );
85889
 
    sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb,
85890
 
                      (char*)pKey, P4_KEYINFO_HANDOFF);
85891
 
    VdbeComment((v, "%s", pIdx->zName));
85892
 
  }
85893
 
  if( pParse->nTab<baseCur+i ){
85894
 
    pParse->nTab = baseCur+i;
85895
 
  }
85896
 
  return i-1;
85897
 
}
85898
 
 
85899
 
 
85900
 
#ifdef SQLITE_TEST
85901
 
/*
85902
 
** The following global variable is incremented whenever the
85903
 
** transfer optimization is used.  This is used for testing
85904
 
** purposes only - to make sure the transfer optimization really
85905
 
** is happening when it is suppose to.
85906
 
*/
85907
 
SQLITE_API int sqlite3_xferopt_count;
85908
 
#endif /* SQLITE_TEST */
85909
 
 
85910
 
 
85911
 
#ifndef SQLITE_OMIT_XFER_OPT
85912
 
/*
85913
 
** Check to collation names to see if they are compatible.
85914
 
*/
85915
 
static int xferCompatibleCollation(const char *z1, const char *z2){
85916
 
  if( z1==0 ){
85917
 
    return z2==0;
85918
 
  }
85919
 
  if( z2==0 ){
85920
 
    return 0;
85921
 
  }
85922
 
  return sqlite3StrICmp(z1, z2)==0;
85923
 
}
85924
 
 
85925
 
 
85926
 
/*
85927
 
** Check to see if index pSrc is compatible as a source of data
85928
 
** for index pDest in an insert transfer optimization.  The rules
85929
 
** for a compatible index:
85930
 
**
85931
 
**    *   The index is over the same set of columns
85932
 
**    *   The same DESC and ASC markings occurs on all columns
85933
 
**    *   The same onError processing (OE_Abort, OE_Ignore, etc)
85934
 
**    *   The same collating sequence on each column
85935
 
*/
85936
 
static int xferCompatibleIndex(Index *pDest, Index *pSrc){
85937
 
  int i;
85938
 
  assert( pDest && pSrc );
85939
 
  assert( pDest->pTable!=pSrc->pTable );
85940
 
  if( pDest->nColumn!=pSrc->nColumn ){
85941
 
    return 0;   /* Different number of columns */
85942
 
  }
85943
 
  if( pDest->onError!=pSrc->onError ){
85944
 
    return 0;   /* Different conflict resolution strategies */
85945
 
  }
85946
 
  for(i=0; i<pSrc->nColumn; i++){
85947
 
    if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
85948
 
      return 0;   /* Different columns indexed */
85949
 
    }
85950
 
    if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
85951
 
      return 0;   /* Different sort orders */
85952
 
    }
85953
 
    if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
85954
 
      return 0;   /* Different collating sequences */
85955
 
    }
85956
 
  }
85957
 
 
85958
 
  /* If no test above fails then the indices must be compatible */
85959
 
  return 1;
85960
 
}
85961
 
 
85962
 
/*
85963
 
** Attempt the transfer optimization on INSERTs of the form
85964
 
**
85965
 
**     INSERT INTO tab1 SELECT * FROM tab2;
85966
 
**
85967
 
** This optimization is only attempted if
85968
 
**
85969
 
**    (1)  tab1 and tab2 have identical schemas including all the
85970
 
**         same indices and constraints
85971
 
**
85972
 
**    (2)  tab1 and tab2 are different tables
85973
 
**
85974
 
**    (3)  There must be no triggers on tab1
85975
 
**
85976
 
**    (4)  The result set of the SELECT statement is "*"
85977
 
**
85978
 
**    (5)  The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY,
85979
 
**         or LIMIT clause.
85980
 
**
85981
 
**    (6)  The SELECT statement is a simple (not a compound) select that
85982
 
**         contains only tab2 in its FROM clause
85983
 
**
85984
 
** This method for implementing the INSERT transfers raw records from
85985
 
** tab2 over to tab1.  The columns are not decoded.  Raw records from
85986
 
** the indices of tab2 are transfered to tab1 as well.  In so doing,
85987
 
** the resulting tab1 has much less fragmentation.
85988
 
**
85989
 
** This routine returns TRUE if the optimization is attempted.  If any
85990
 
** of the conditions above fail so that the optimization should not
85991
 
** be attempted, then this routine returns FALSE.
85992
 
*/
85993
 
static int xferOptimization(
85994
 
  Parse *pParse,        /* Parser context */
85995
 
  Table *pDest,         /* The table we are inserting into */
85996
 
  Select *pSelect,      /* A SELECT statement to use as the data source */
85997
 
  int onError,          /* How to handle constraint errors */
85998
 
  int iDbDest           /* The database of pDest */
85999
 
){
86000
 
  ExprList *pEList;                /* The result set of the SELECT */
86001
 
  Table *pSrc;                     /* The table in the FROM clause of SELECT */
86002
 
  Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
86003
 
  struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
86004
 
  int i;                           /* Loop counter */
86005
 
  int iDbSrc;                      /* The database of pSrc */
86006
 
  int iSrc, iDest;                 /* Cursors from source and destination */
86007
 
  int addr1, addr2;                /* Loop addresses */
86008
 
  int emptyDestTest;               /* Address of test for empty pDest */
86009
 
  int emptySrcTest;                /* Address of test for empty pSrc */
86010
 
  Vdbe *v;                         /* The VDBE we are building */
86011
 
  KeyInfo *pKey;                   /* Key information for an index */
86012
 
  int regAutoinc;                  /* Memory register used by AUTOINC */
86013
 
  int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
86014
 
  int regData, regRowid;           /* Registers holding data and rowid */
86015
 
 
86016
 
  if( pSelect==0 ){
86017
 
    return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
86018
 
  }
86019
 
  if( sqlite3TriggerList(pParse, pDest) ){
86020
 
    return 0;   /* tab1 must not have triggers */
86021
 
  }
86022
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
86023
 
  if( pDest->tabFlags & TF_Virtual ){
86024
 
    return 0;   /* tab1 must not be a virtual table */
86025
 
  }
86026
 
#endif
86027
 
  if( onError==OE_Default ){
86028
 
    onError = OE_Abort;
86029
 
  }
86030
 
  if( onError!=OE_Abort && onError!=OE_Rollback ){
86031
 
    return 0;   /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */
86032
 
  }
86033
 
  assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
86034
 
  if( pSelect->pSrc->nSrc!=1 ){
86035
 
    return 0;   /* FROM clause must have exactly one term */
86036
 
  }
86037
 
  if( pSelect->pSrc->a[0].pSelect ){
86038
 
    return 0;   /* FROM clause cannot contain a subquery */
86039
 
  }
86040
 
  if( pSelect->pWhere ){
86041
 
    return 0;   /* SELECT may not have a WHERE clause */
86042
 
  }
86043
 
  if( pSelect->pOrderBy ){
86044
 
    return 0;   /* SELECT may not have an ORDER BY clause */
86045
 
  }
86046
 
  /* Do not need to test for a HAVING clause.  If HAVING is present but
86047
 
  ** there is no ORDER BY, we will get an error. */
86048
 
  if( pSelect->pGroupBy ){
86049
 
    return 0;   /* SELECT may not have a GROUP BY clause */
86050
 
  }
86051
 
  if( pSelect->pLimit ){
86052
 
    return 0;   /* SELECT may not have a LIMIT clause */
86053
 
  }
86054
 
  assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
86055
 
  if( pSelect->pPrior ){
86056
 
    return 0;   /* SELECT may not be a compound query */
86057
 
  }
86058
 
  if( pSelect->selFlags & SF_Distinct ){
86059
 
    return 0;   /* SELECT may not be DISTINCT */
86060
 
  }
86061
 
  pEList = pSelect->pEList;
86062
 
  assert( pEList!=0 );
86063
 
  if( pEList->nExpr!=1 ){
86064
 
    return 0;   /* The result set must have exactly one column */
86065
 
  }
86066
 
  assert( pEList->a[0].pExpr );
86067
 
  if( pEList->a[0].pExpr->op!=TK_ALL ){
86068
 
    return 0;   /* The result set must be the special operator "*" */
86069
 
  }
86070
 
 
86071
 
  /* At this point we have established that the statement is of the
86072
 
  ** correct syntactic form to participate in this optimization.  Now
86073
 
  ** we have to check the semantics.
86074
 
  */
86075
 
  pItem = pSelect->pSrc->a;
86076
 
  pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
86077
 
  if( pSrc==0 ){
86078
 
    return 0;   /* FROM clause does not contain a real table */
86079
 
  }
86080
 
  if( pSrc==pDest ){
86081
 
    return 0;   /* tab1 and tab2 may not be the same table */
86082
 
  }
86083
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
86084
 
  if( pSrc->tabFlags & TF_Virtual ){
86085
 
    return 0;   /* tab2 must not be a virtual table */
86086
 
  }
86087
 
#endif
86088
 
  if( pSrc->pSelect ){
86089
 
    return 0;   /* tab2 may not be a view */
86090
 
  }
86091
 
  if( pDest->nCol!=pSrc->nCol ){
86092
 
    return 0;   /* Number of columns must be the same in tab1 and tab2 */
86093
 
  }
86094
 
  if( pDest->iPKey!=pSrc->iPKey ){
86095
 
    return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
86096
 
  }
86097
 
  for(i=0; i<pDest->nCol; i++){
86098
 
    if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
86099
 
      return 0;    /* Affinity must be the same on all columns */
86100
 
    }
86101
 
    if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
86102
 
      return 0;    /* Collating sequence must be the same on all columns */
86103
 
    }
86104
 
    if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
86105
 
      return 0;    /* tab2 must be NOT NULL if tab1 is */
86106
 
    }
86107
 
  }
86108
 
  for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
86109
 
    if( pDestIdx->onError!=OE_None ){
86110
 
      destHasUniqueIdx = 1;
86111
 
    }
86112
 
    for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
86113
 
      if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
86114
 
    }
86115
 
    if( pSrcIdx==0 ){
86116
 
      return 0;    /* pDestIdx has no corresponding index in pSrc */
86117
 
    }
86118
 
  }
86119
 
#ifndef SQLITE_OMIT_CHECK
86120
 
  if( pDest->pCheck && sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){
86121
 
    return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
86122
 
  }
86123
 
#endif
86124
 
 
86125
 
  /* If we get this far, it means either:
86126
 
  **
86127
 
  **    *   We can always do the transfer if the table contains an
86128
 
  **        an integer primary key
86129
 
  **
86130
 
  **    *   We can conditionally do the transfer if the destination
86131
 
  **        table is empty.
86132
 
  */
86133
 
#ifdef SQLITE_TEST
86134
 
  sqlite3_xferopt_count++;
86135
 
#endif
86136
 
  iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
86137
 
  v = sqlite3GetVdbe(pParse);
86138
 
  sqlite3CodeVerifySchema(pParse, iDbSrc);
86139
 
  iSrc = pParse->nTab++;
86140
 
  iDest = pParse->nTab++;
86141
 
  regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
86142
 
  sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
86143
 
  if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){
86144
 
    /* If tables do not have an INTEGER PRIMARY KEY and there
86145
 
    ** are indices to be copied and the destination is not empty,
86146
 
    ** we have to disallow the transfer optimization because the
86147
 
    ** the rowids might change which will mess up indexing.
86148
 
    **
86149
 
    ** Or if the destination has a UNIQUE index and is not empty,
86150
 
    ** we also disallow the transfer optimization because we cannot
86151
 
    ** insure that all entries in the union of DEST and SRC will be
86152
 
    ** unique.
86153
 
    */
86154
 
    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
86155
 
    emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
86156
 
    sqlite3VdbeJumpHere(v, addr1);
86157
 
  }else{
86158
 
    emptyDestTest = 0;
86159
 
  }
86160
 
  sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
86161
 
  emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
86162
 
  regData = sqlite3GetTempReg(pParse);
86163
 
  regRowid = sqlite3GetTempReg(pParse);
86164
 
  if( pDest->iPKey>=0 ){
86165
 
    addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
86166
 
    addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
86167
 
    sqlite3HaltConstraint(
86168
 
        pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
86169
 
    sqlite3VdbeJumpHere(v, addr2);
86170
 
    autoIncStep(pParse, regAutoinc, regRowid);
86171
 
  }else if( pDest->pIndex==0 ){
86172
 
    addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
86173
 
  }else{
86174
 
    addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
86175
 
    assert( (pDest->tabFlags & TF_Autoincrement)==0 );
86176
 
  }
86177
 
  sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
86178
 
  sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
86179
 
  sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
86180
 
  sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
86181
 
  sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
86182
 
  for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
86183
 
    for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
86184
 
      if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
86185
 
    }
86186
 
    assert( pSrcIdx );
86187
 
    sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
86188
 
    sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
86189
 
    pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
86190
 
    sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc,
86191
 
                      (char*)pKey, P4_KEYINFO_HANDOFF);
86192
 
    VdbeComment((v, "%s", pSrcIdx->zName));
86193
 
    pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
86194
 
    sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest,
86195
 
                      (char*)pKey, P4_KEYINFO_HANDOFF);
86196
 
    VdbeComment((v, "%s", pDestIdx->zName));
86197
 
    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
86198
 
    sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
86199
 
    sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
86200
 
    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
86201
 
    sqlite3VdbeJumpHere(v, addr1);
86202
 
  }
86203
 
  sqlite3VdbeJumpHere(v, emptySrcTest);
86204
 
  sqlite3ReleaseTempReg(pParse, regRowid);
86205
 
  sqlite3ReleaseTempReg(pParse, regData);
86206
 
  sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
86207
 
  sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
86208
 
  if( emptyDestTest ){
86209
 
    sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
86210
 
    sqlite3VdbeJumpHere(v, emptyDestTest);
86211
 
    sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
86212
 
    return 0;
86213
 
  }else{
86214
 
    return 1;
86215
 
  }
86216
 
}
86217
 
#endif /* SQLITE_OMIT_XFER_OPT */
86218
 
 
86219
 
/************** End of insert.c **********************************************/
86220
 
/************** Begin file legacy.c ******************************************/
86221
 
/*
86222
 
** 2001 September 15
86223
 
**
86224
 
** The author disclaims copyright to this source code.  In place of
86225
 
** a legal notice, here is a blessing:
86226
 
**
86227
 
**    May you do good and not evil.
86228
 
**    May you find forgiveness for yourself and forgive others.
86229
 
**    May you share freely, never taking more than you give.
86230
 
**
86231
 
*************************************************************************
86232
 
** Main file for the SQLite library.  The routines in this file
86233
 
** implement the programmer interface to the library.  Routines in
86234
 
** other files are for internal use by SQLite and should not be
86235
 
** accessed by users of the library.
86236
 
*/
86237
 
 
86238
 
 
86239
 
/*
86240
 
** Execute SQL code.  Return one of the SQLITE_ success/failure
86241
 
** codes.  Also write an error message into memory obtained from
86242
 
** malloc() and make *pzErrMsg point to that message.
86243
 
**
86244
 
** If the SQL is a query, then for each row in the query result
86245
 
** the xCallback() function is called.  pArg becomes the first
86246
 
** argument to xCallback().  If xCallback=NULL then no callback
86247
 
** is invoked, even for queries.
86248
 
*/
86249
 
SQLITE_API int sqlite3_exec(
86250
 
  sqlite3 *db,                /* The database on which the SQL executes */
86251
 
  const char *zSql,           /* The SQL to be executed */
86252
 
  sqlite3_callback xCallback, /* Invoke this callback routine */
86253
 
  void *pArg,                 /* First argument to xCallback() */
86254
 
  char **pzErrMsg             /* Write error messages here */
86255
 
){
86256
 
  int rc = SQLITE_OK;         /* Return code */
86257
 
  const char *zLeftover;      /* Tail of unprocessed SQL */
86258
 
  sqlite3_stmt *pStmt = 0;    /* The current SQL statement */
86259
 
  char **azCols = 0;          /* Names of result columns */
86260
 
  int nRetry = 0;             /* Number of retry attempts */
86261
 
  int callbackIsInit;         /* True if callback data is initialized */
86262
 
 
86263
 
  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
86264
 
  if( zSql==0 ) zSql = "";
86265
 
 
86266
 
  sqlite3_mutex_enter(db->mutex);
86267
 
  sqlite3Error(db, SQLITE_OK, 0);
86268
 
  while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
86269
 
    int nCol;
86270
 
    char **azVals = 0;
86271
 
 
86272
 
    pStmt = 0;
86273
 
    rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
86274
 
    assert( rc==SQLITE_OK || pStmt==0 );
86275
 
    if( rc!=SQLITE_OK ){
86276
 
      continue;
86277
 
    }
86278
 
    if( !pStmt ){
86279
 
      /* this happens for a comment or white-space */
86280
 
      zSql = zLeftover;
86281
 
      continue;
86282
 
    }
86283
 
 
86284
 
    callbackIsInit = 0;
86285
 
    nCol = sqlite3_column_count(pStmt);
86286
 
 
86287
 
    for(;;) {
86288
 
      int i;
86289
 
      rc = sqlite3_step(pStmt);
86290
 
 
86291
 
      /* Invoke the callback function if required */
86292
 
      if( xCallback && (SQLITE_ROW==rc || 
86293
 
          (SQLITE_DONE==rc && !callbackIsInit
86294
 
                           && db->flags&SQLITE_NullCallback)) ){
86295
 
        if( !callbackIsInit ){
86296
 
          azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
86297
 
          if( azCols==0 ){
86298
 
            goto exec_out;
86299
 
          }
86300
 
          for(i=0; i<nCol; i++){
86301
 
            azCols[i] = (char *)sqlite3_column_name(pStmt, i);
86302
 
            /* sqlite3VdbeSetColName() installs column names as UTF8
86303
 
            ** strings so there is no way for sqlite3_column_name() to fail. */
86304
 
            assert( azCols[i]!=0 );
86305
 
          }
86306
 
          callbackIsInit = 1;
86307
 
        }
86308
 
        if( rc==SQLITE_ROW ){
86309
 
          azVals = &azCols[nCol];
86310
 
          for(i=0; i<nCol; i++){
86311
 
            azVals[i] = (char *)sqlite3_column_text(pStmt, i);
86312
 
            if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
86313
 
              db->mallocFailed = 1;
86314
 
              goto exec_out;
86315
 
            }
86316
 
          }
86317
 
        }
86318
 
        if( xCallback(pArg, nCol, azVals, azCols) ){
86319
 
          rc = SQLITE_ABORT;
86320
 
          sqlite3VdbeFinalize((Vdbe *)pStmt);
86321
 
          pStmt = 0;
86322
 
          sqlite3Error(db, SQLITE_ABORT, 0);
86323
 
          goto exec_out;
86324
 
        }
86325
 
      }
86326
 
 
86327
 
      if( rc!=SQLITE_ROW ){
86328
 
        rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
86329
 
        pStmt = 0;
86330
 
        if( rc!=SQLITE_SCHEMA ){
86331
 
          nRetry = 0;
86332
 
          zSql = zLeftover;
86333
 
          while( sqlite3Isspace(zSql[0]) ) zSql++;
86334
 
        }
86335
 
        break;
86336
 
      }
86337
 
    }
86338
 
 
86339
 
    sqlite3DbFree(db, azCols);
86340
 
    azCols = 0;
86341
 
  }
86342
 
 
86343
 
exec_out:
86344
 
  if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
86345
 
  sqlite3DbFree(db, azCols);
86346
 
 
86347
 
  rc = sqlite3ApiExit(db, rc);
86348
 
  if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
86349
 
    int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
86350
 
    *pzErrMsg = sqlite3Malloc(nErrMsg);
86351
 
    if( *pzErrMsg ){
86352
 
      memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
86353
 
    }else{
86354
 
      rc = SQLITE_NOMEM;
86355
 
      sqlite3Error(db, SQLITE_NOMEM, 0);
86356
 
    }
86357
 
  }else if( pzErrMsg ){
86358
 
    *pzErrMsg = 0;
86359
 
  }
86360
 
 
86361
 
  assert( (rc&db->errMask)==rc );
86362
 
  sqlite3_mutex_leave(db->mutex);
86363
 
  return rc;
86364
 
}
86365
 
 
86366
 
/************** End of legacy.c **********************************************/
86367
 
/************** Begin file loadext.c *****************************************/
86368
 
/*
86369
 
** 2006 June 7
86370
 
**
86371
 
** The author disclaims copyright to this source code.  In place of
86372
 
** a legal notice, here is a blessing:
86373
 
**
86374
 
**    May you do good and not evil.
86375
 
**    May you find forgiveness for yourself and forgive others.
86376
 
**    May you share freely, never taking more than you give.
86377
 
**
86378
 
*************************************************************************
86379
 
** This file contains code used to dynamically load extensions into
86380
 
** the SQLite library.
86381
 
*/
86382
 
 
86383
 
#ifndef SQLITE_CORE
86384
 
  #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
86385
 
#endif
86386
 
/************** Include sqlite3ext.h in the middle of loadext.c **************/
86387
 
/************** Begin file sqlite3ext.h **************************************/
86388
 
/*
86389
 
** 2006 June 7
86390
 
**
86391
 
** The author disclaims copyright to this source code.  In place of
86392
 
** a legal notice, here is a blessing:
86393
 
**
86394
 
**    May you do good and not evil.
86395
 
**    May you find forgiveness for yourself and forgive others.
86396
 
**    May you share freely, never taking more than you give.
86397
 
**
86398
 
*************************************************************************
86399
 
** This header file defines the SQLite interface for use by
86400
 
** shared libraries that want to be imported as extensions into
86401
 
** an SQLite instance.  Shared libraries that intend to be loaded
86402
 
** as extensions by SQLite should #include this file instead of 
86403
 
** sqlite3.h.
86404
 
*/
86405
 
#ifndef _SQLITE3EXT_H_
86406
 
#define _SQLITE3EXT_H_
86407
 
 
86408
 
typedef struct sqlite3_api_routines sqlite3_api_routines;
86409
 
 
86410
 
/*
86411
 
** The following structure holds pointers to all of the SQLite API
86412
 
** routines.
86413
 
**
86414
 
** WARNING:  In order to maintain backwards compatibility, add new
86415
 
** interfaces to the end of this structure only.  If you insert new
86416
 
** interfaces in the middle of this structure, then older different
86417
 
** versions of SQLite will not be able to load each others' shared
86418
 
** libraries!
86419
 
*/
86420
 
struct sqlite3_api_routines {
86421
 
  void * (*aggregate_context)(sqlite3_context*,int nBytes);
86422
 
  int  (*aggregate_count)(sqlite3_context*);
86423
 
  int  (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
86424
 
  int  (*bind_double)(sqlite3_stmt*,int,double);
86425
 
  int  (*bind_int)(sqlite3_stmt*,int,int);
86426
 
  int  (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
86427
 
  int  (*bind_null)(sqlite3_stmt*,int);
86428
 
  int  (*bind_parameter_count)(sqlite3_stmt*);
86429
 
  int  (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
86430
 
  const char * (*bind_parameter_name)(sqlite3_stmt*,int);
86431
 
  int  (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
86432
 
  int  (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
86433
 
  int  (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
86434
 
  int  (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
86435
 
  int  (*busy_timeout)(sqlite3*,int ms);
86436
 
  int  (*changes)(sqlite3*);
86437
 
  int  (*close)(sqlite3*);
86438
 
  int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const char*));
86439
 
  int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const void*));
86440
 
  const void * (*column_blob)(sqlite3_stmt*,int iCol);
86441
 
  int  (*column_bytes)(sqlite3_stmt*,int iCol);
86442
 
  int  (*column_bytes16)(sqlite3_stmt*,int iCol);
86443
 
  int  (*column_count)(sqlite3_stmt*pStmt);
86444
 
  const char * (*column_database_name)(sqlite3_stmt*,int);
86445
 
  const void * (*column_database_name16)(sqlite3_stmt*,int);
86446
 
  const char * (*column_decltype)(sqlite3_stmt*,int i);
86447
 
  const void * (*column_decltype16)(sqlite3_stmt*,int);
86448
 
  double  (*column_double)(sqlite3_stmt*,int iCol);
86449
 
  int  (*column_int)(sqlite3_stmt*,int iCol);
86450
 
  sqlite_int64  (*column_int64)(sqlite3_stmt*,int iCol);
86451
 
  const char * (*column_name)(sqlite3_stmt*,int);
86452
 
  const void * (*column_name16)(sqlite3_stmt*,int);
86453
 
  const char * (*column_origin_name)(sqlite3_stmt*,int);
86454
 
  const void * (*column_origin_name16)(sqlite3_stmt*,int);
86455
 
  const char * (*column_table_name)(sqlite3_stmt*,int);
86456
 
  const void * (*column_table_name16)(sqlite3_stmt*,int);
86457
 
  const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
86458
 
  const void * (*column_text16)(sqlite3_stmt*,int iCol);
86459
 
  int  (*column_type)(sqlite3_stmt*,int iCol);
86460
 
  sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
86461
 
  void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
86462
 
  int  (*complete)(const char*sql);
86463
 
  int  (*complete16)(const void*sql);
86464
 
  int  (*create_collation)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*));
86465
 
  int  (*create_collation16)(sqlite3*,const void*,int,void*,int(*)(void*,int,const void*,int,const void*));
86466
 
  int  (*create_function)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*));
86467
 
  int  (*create_function16)(sqlite3*,const void*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*));
86468
 
  int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
86469
 
  int  (*data_count)(sqlite3_stmt*pStmt);
86470
 
  sqlite3 * (*db_handle)(sqlite3_stmt*);
86471
 
  int (*declare_vtab)(sqlite3*,const char*);
86472
 
  int  (*enable_shared_cache)(int);
86473
 
  int  (*errcode)(sqlite3*db);
86474
 
  const char * (*errmsg)(sqlite3*);
86475
 
  const void * (*errmsg16)(sqlite3*);
86476
 
  int  (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
86477
 
  int  (*expired)(sqlite3_stmt*);
86478
 
  int  (*finalize)(sqlite3_stmt*pStmt);
86479
 
  void  (*free)(void*);
86480
 
  void  (*free_table)(char**result);
86481
 
  int  (*get_autocommit)(sqlite3*);
86482
 
  void * (*get_auxdata)(sqlite3_context*,int);
86483
 
  int  (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
86484
 
  int  (*global_recover)(void);
86485
 
  void  (*interruptx)(sqlite3*);
86486
 
  sqlite_int64  (*last_insert_rowid)(sqlite3*);
86487
 
  const char * (*libversion)(void);
86488
 
  int  (*libversion_number)(void);
86489
 
  void *(*malloc)(int);
86490
 
  char * (*mprintf)(const char*,...);
86491
 
  int  (*open)(const char*,sqlite3**);
86492
 
  int  (*open16)(const void*,sqlite3**);
86493
 
  int  (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
86494
 
  int  (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
86495
 
  void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
86496
 
  void  (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
86497
 
  void *(*realloc)(void*,int);
86498
 
  int  (*reset)(sqlite3_stmt*pStmt);
86499
 
  void  (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
86500
 
  void  (*result_double)(sqlite3_context*,double);
86501
 
  void  (*result_error)(sqlite3_context*,const char*,int);
86502
 
  void  (*result_error16)(sqlite3_context*,const void*,int);
86503
 
  void  (*result_int)(sqlite3_context*,int);
86504
 
  void  (*result_int64)(sqlite3_context*,sqlite_int64);
86505
 
  void  (*result_null)(sqlite3_context*);
86506
 
  void  (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
86507
 
  void  (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
86508
 
  void  (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
86509
 
  void  (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
86510
 
  void  (*result_value)(sqlite3_context*,sqlite3_value*);
86511
 
  void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
86512
 
  int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,const char*,const char*),void*);
86513
 
  void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
86514
 
  char * (*snprintf)(int,char*,const char*,...);
86515
 
  int  (*step)(sqlite3_stmt*);
86516
 
  int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,char const**,char const**,int*,int*,int*);
86517
 
  void  (*thread_cleanup)(void);
86518
 
  int  (*total_changes)(sqlite3*);
86519
 
  void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
86520
 
  int  (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
86521
 
  void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,sqlite_int64),void*);
86522
 
  void * (*user_data)(sqlite3_context*);
86523
 
  const void * (*value_blob)(sqlite3_value*);
86524
 
  int  (*value_bytes)(sqlite3_value*);
86525
 
  int  (*value_bytes16)(sqlite3_value*);
86526
 
  double  (*value_double)(sqlite3_value*);
86527
 
  int  (*value_int)(sqlite3_value*);
86528
 
  sqlite_int64  (*value_int64)(sqlite3_value*);
86529
 
  int  (*value_numeric_type)(sqlite3_value*);
86530
 
  const unsigned char * (*value_text)(sqlite3_value*);
86531
 
  const void * (*value_text16)(sqlite3_value*);
86532
 
  const void * (*value_text16be)(sqlite3_value*);
86533
 
  const void * (*value_text16le)(sqlite3_value*);
86534
 
  int  (*value_type)(sqlite3_value*);
86535
 
  char *(*vmprintf)(const char*,va_list);
86536
 
  /* Added ??? */
86537
 
  int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
86538
 
  /* Added by 3.3.13 */
86539
 
  int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
86540
 
  int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
86541
 
  int (*clear_bindings)(sqlite3_stmt*);
86542
 
  /* Added by 3.4.1 */
86543
 
  int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,void (*xDestroy)(void *));
86544
 
  /* Added by 3.5.0 */
86545
 
  int (*bind_zeroblob)(sqlite3_stmt*,int,int);
86546
 
  int (*blob_bytes)(sqlite3_blob*);
86547
 
  int (*blob_close)(sqlite3_blob*);
86548
 
  int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,int,sqlite3_blob**);
86549
 
  int (*blob_read)(sqlite3_blob*,void*,int,int);
86550
 
  int (*blob_write)(sqlite3_blob*,const void*,int,int);
86551
 
  int (*create_collation_v2)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*),void(*)(void*));
86552
 
  int (*file_control)(sqlite3*,const char*,int,void*);
86553
 
  sqlite3_int64 (*memory_highwater)(int);
86554
 
  sqlite3_int64 (*memory_used)(void);
86555
 
  sqlite3_mutex *(*mutex_alloc)(int);
86556
 
  void (*mutex_enter)(sqlite3_mutex*);
86557
 
  void (*mutex_free)(sqlite3_mutex*);
86558
 
  void (*mutex_leave)(sqlite3_mutex*);
86559
 
  int (*mutex_try)(sqlite3_mutex*);
86560
 
  int (*open_v2)(const char*,sqlite3**,int,const char*);
86561
 
  int (*release_memory)(int);
86562
 
  void (*result_error_nomem)(sqlite3_context*);
86563
 
  void (*result_error_toobig)(sqlite3_context*);
86564
 
  int (*sleep)(int);
86565
 
  void (*soft_heap_limit)(int);
86566
 
  sqlite3_vfs *(*vfs_find)(const char*);
86567
 
  int (*vfs_register)(sqlite3_vfs*,int);
86568
 
  int (*vfs_unregister)(sqlite3_vfs*);
86569
 
  int (*xthreadsafe)(void);
86570
 
  void (*result_zeroblob)(sqlite3_context*,int);
86571
 
  void (*result_error_code)(sqlite3_context*,int);
86572
 
  int (*test_control)(int, ...);
86573
 
  void (*randomness)(int,void*);
86574
 
  sqlite3 *(*context_db_handle)(sqlite3_context*);
86575
 
  int (*extended_result_codes)(sqlite3*,int);
86576
 
  int (*limit)(sqlite3*,int,int);
86577
 
  sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
86578
 
  const char *(*sql)(sqlite3_stmt*);
86579
 
  int (*status)(int,int*,int*,int);
86580
 
  int (*backup_finish)(sqlite3_backup*);
86581
 
  sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
86582
 
  int (*backup_pagecount)(sqlite3_backup*);
86583
 
  int (*backup_remaining)(sqlite3_backup*);
86584
 
  int (*backup_step)(sqlite3_backup*,int);
86585
 
  const char *(*compileoption_get)(int);
86586
 
  int (*compileoption_used)(const char*);
86587
 
  int (*create_function_v2)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*),void(*xDestroy)(void*));
86588
 
  int (*db_config)(sqlite3*,int,...);
86589
 
  sqlite3_mutex *(*db_mutex)(sqlite3*);
86590
 
  int (*db_status)(sqlite3*,int,int*,int*,int);
86591
 
  int (*extended_errcode)(sqlite3*);
86592
 
  void (*log)(int,const char*,...);
86593
 
  sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
86594
 
  const char *(*sourceid)(void);
86595
 
  int (*stmt_status)(sqlite3_stmt*,int,int);
86596
 
  int (*strnicmp)(const char*,const char*,int);
86597
 
  int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
86598
 
  int (*wal_autocheckpoint)(sqlite3*,int);
86599
 
  int (*wal_checkpoint)(sqlite3*,const char*);
86600
 
  void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
86601
 
};
86602
 
 
86603
 
/*
86604
 
** The following macros redefine the API routines so that they are
86605
 
** redirected throught the global sqlite3_api structure.
86606
 
**
86607
 
** This header file is also used by the loadext.c source file
86608
 
** (part of the main SQLite library - not an extension) so that
86609
 
** it can get access to the sqlite3_api_routines structure
86610
 
** definition.  But the main library does not want to redefine
86611
 
** the API.  So the redefinition macros are only valid if the
86612
 
** SQLITE_CORE macros is undefined.
86613
 
*/
86614
 
#ifndef SQLITE_CORE
86615
 
#define sqlite3_aggregate_context      sqlite3_api->aggregate_context
86616
 
#ifndef SQLITE_OMIT_DEPRECATED
86617
 
#define sqlite3_aggregate_count        sqlite3_api->aggregate_count
86618
 
#endif
86619
 
#define sqlite3_bind_blob              sqlite3_api->bind_blob
86620
 
#define sqlite3_bind_double            sqlite3_api->bind_double
86621
 
#define sqlite3_bind_int               sqlite3_api->bind_int
86622
 
#define sqlite3_bind_int64             sqlite3_api->bind_int64
86623
 
#define sqlite3_bind_null              sqlite3_api->bind_null
86624
 
#define sqlite3_bind_parameter_count   sqlite3_api->bind_parameter_count
86625
 
#define sqlite3_bind_parameter_index   sqlite3_api->bind_parameter_index
86626
 
#define sqlite3_bind_parameter_name    sqlite3_api->bind_parameter_name
86627
 
#define sqlite3_bind_text              sqlite3_api->bind_text
86628
 
#define sqlite3_bind_text16            sqlite3_api->bind_text16
86629
 
#define sqlite3_bind_value             sqlite3_api->bind_value
86630
 
#define sqlite3_busy_handler           sqlite3_api->busy_handler
86631
 
#define sqlite3_busy_timeout           sqlite3_api->busy_timeout
86632
 
#define sqlite3_changes                sqlite3_api->changes
86633
 
#define sqlite3_close                  sqlite3_api->close
86634
 
#define sqlite3_collation_needed       sqlite3_api->collation_needed
86635
 
#define sqlite3_collation_needed16     sqlite3_api->collation_needed16
86636
 
#define sqlite3_column_blob            sqlite3_api->column_blob
86637
 
#define sqlite3_column_bytes           sqlite3_api->column_bytes
86638
 
#define sqlite3_column_bytes16         sqlite3_api->column_bytes16
86639
 
#define sqlite3_column_count           sqlite3_api->column_count
86640
 
#define sqlite3_column_database_name   sqlite3_api->column_database_name
86641
 
#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
86642
 
#define sqlite3_column_decltype        sqlite3_api->column_decltype
86643
 
#define sqlite3_column_decltype16      sqlite3_api->column_decltype16
86644
 
#define sqlite3_column_double          sqlite3_api->column_double
86645
 
#define sqlite3_column_int             sqlite3_api->column_int
86646
 
#define sqlite3_column_int64           sqlite3_api->column_int64
86647
 
#define sqlite3_column_name            sqlite3_api->column_name
86648
 
#define sqlite3_column_name16          sqlite3_api->column_name16
86649
 
#define sqlite3_column_origin_name     sqlite3_api->column_origin_name
86650
 
#define sqlite3_column_origin_name16   sqlite3_api->column_origin_name16
86651
 
#define sqlite3_column_table_name      sqlite3_api->column_table_name
86652
 
#define sqlite3_column_table_name16    sqlite3_api->column_table_name16
86653
 
#define sqlite3_column_text            sqlite3_api->column_text
86654
 
#define sqlite3_column_text16          sqlite3_api->column_text16
86655
 
#define sqlite3_column_type            sqlite3_api->column_type
86656
 
#define sqlite3_column_value           sqlite3_api->column_value
86657
 
#define sqlite3_commit_hook            sqlite3_api->commit_hook
86658
 
#define sqlite3_complete               sqlite3_api->complete
86659
 
#define sqlite3_complete16             sqlite3_api->complete16
86660
 
#define sqlite3_create_collation       sqlite3_api->create_collation
86661
 
#define sqlite3_create_collation16     sqlite3_api->create_collation16
86662
 
#define sqlite3_create_function        sqlite3_api->create_function
86663
 
#define sqlite3_create_function16      sqlite3_api->create_function16
86664
 
#define sqlite3_create_module          sqlite3_api->create_module
86665
 
#define sqlite3_create_module_v2       sqlite3_api->create_module_v2
86666
 
#define sqlite3_data_count             sqlite3_api->data_count
86667
 
#define sqlite3_db_handle              sqlite3_api->db_handle
86668
 
#define sqlite3_declare_vtab           sqlite3_api->declare_vtab
86669
 
#define sqlite3_enable_shared_cache    sqlite3_api->enable_shared_cache
86670
 
#define sqlite3_errcode                sqlite3_api->errcode
86671
 
#define sqlite3_errmsg                 sqlite3_api->errmsg
86672
 
#define sqlite3_errmsg16               sqlite3_api->errmsg16
86673
 
#define sqlite3_exec                   sqlite3_api->exec
86674
 
#ifndef SQLITE_OMIT_DEPRECATED
86675
 
#define sqlite3_expired                sqlite3_api->expired
86676
 
#endif
86677
 
#define sqlite3_finalize               sqlite3_api->finalize
86678
 
#define sqlite3_free                   sqlite3_api->free
86679
 
#define sqlite3_free_table             sqlite3_api->free_table
86680
 
#define sqlite3_get_autocommit         sqlite3_api->get_autocommit
86681
 
#define sqlite3_get_auxdata            sqlite3_api->get_auxdata
86682
 
#define sqlite3_get_table              sqlite3_api->get_table
86683
 
#ifndef SQLITE_OMIT_DEPRECATED
86684
 
#define sqlite3_global_recover         sqlite3_api->global_recover
86685
 
#endif
86686
 
#define sqlite3_interrupt              sqlite3_api->interruptx
86687
 
#define sqlite3_last_insert_rowid      sqlite3_api->last_insert_rowid
86688
 
#define sqlite3_libversion             sqlite3_api->libversion
86689
 
#define sqlite3_libversion_number      sqlite3_api->libversion_number
86690
 
#define sqlite3_malloc                 sqlite3_api->malloc
86691
 
#define sqlite3_mprintf                sqlite3_api->mprintf
86692
 
#define sqlite3_open                   sqlite3_api->open
86693
 
#define sqlite3_open16                 sqlite3_api->open16
86694
 
#define sqlite3_prepare                sqlite3_api->prepare
86695
 
#define sqlite3_prepare16              sqlite3_api->prepare16
86696
 
#define sqlite3_prepare_v2             sqlite3_api->prepare_v2
86697
 
#define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
86698
 
#define sqlite3_profile                sqlite3_api->profile
86699
 
#define sqlite3_progress_handler       sqlite3_api->progress_handler
86700
 
#define sqlite3_realloc                sqlite3_api->realloc
86701
 
#define sqlite3_reset                  sqlite3_api->reset
86702
 
#define sqlite3_result_blob            sqlite3_api->result_blob
86703
 
#define sqlite3_result_double          sqlite3_api->result_double
86704
 
#define sqlite3_result_error           sqlite3_api->result_error
86705
 
#define sqlite3_result_error16         sqlite3_api->result_error16
86706
 
#define sqlite3_result_int             sqlite3_api->result_int
86707
 
#define sqlite3_result_int64           sqlite3_api->result_int64
86708
 
#define sqlite3_result_null            sqlite3_api->result_null
86709
 
#define sqlite3_result_text            sqlite3_api->result_text
86710
 
#define sqlite3_result_text16          sqlite3_api->result_text16
86711
 
#define sqlite3_result_text16be        sqlite3_api->result_text16be
86712
 
#define sqlite3_result_text16le        sqlite3_api->result_text16le
86713
 
#define sqlite3_result_value           sqlite3_api->result_value
86714
 
#define sqlite3_rollback_hook          sqlite3_api->rollback_hook
86715
 
#define sqlite3_set_authorizer         sqlite3_api->set_authorizer
86716
 
#define sqlite3_set_auxdata            sqlite3_api->set_auxdata
86717
 
#define sqlite3_snprintf               sqlite3_api->snprintf
86718
 
#define sqlite3_step                   sqlite3_api->step
86719
 
#define sqlite3_table_column_metadata  sqlite3_api->table_column_metadata
86720
 
#define sqlite3_thread_cleanup         sqlite3_api->thread_cleanup
86721
 
#define sqlite3_total_changes          sqlite3_api->total_changes
86722
 
#define sqlite3_trace                  sqlite3_api->trace
86723
 
#ifndef SQLITE_OMIT_DEPRECATED
86724
 
#define sqlite3_transfer_bindings      sqlite3_api->transfer_bindings
86725
 
#endif
86726
 
#define sqlite3_update_hook            sqlite3_api->update_hook
86727
 
#define sqlite3_user_data              sqlite3_api->user_data
86728
 
#define sqlite3_value_blob             sqlite3_api->value_blob
86729
 
#define sqlite3_value_bytes            sqlite3_api->value_bytes
86730
 
#define sqlite3_value_bytes16          sqlite3_api->value_bytes16
86731
 
#define sqlite3_value_double           sqlite3_api->value_double
86732
 
#define sqlite3_value_int              sqlite3_api->value_int
86733
 
#define sqlite3_value_int64            sqlite3_api->value_int64
86734
 
#define sqlite3_value_numeric_type     sqlite3_api->value_numeric_type
86735
 
#define sqlite3_value_text             sqlite3_api->value_text
86736
 
#define sqlite3_value_text16           sqlite3_api->value_text16
86737
 
#define sqlite3_value_text16be         sqlite3_api->value_text16be
86738
 
#define sqlite3_value_text16le         sqlite3_api->value_text16le
86739
 
#define sqlite3_value_type             sqlite3_api->value_type
86740
 
#define sqlite3_vmprintf               sqlite3_api->vmprintf
86741
 
#define sqlite3_overload_function      sqlite3_api->overload_function
86742
 
#define sqlite3_prepare_v2             sqlite3_api->prepare_v2
86743
 
#define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
86744
 
#define sqlite3_clear_bindings         sqlite3_api->clear_bindings
86745
 
#define sqlite3_bind_zeroblob          sqlite3_api->bind_zeroblob
86746
 
#define sqlite3_blob_bytes             sqlite3_api->blob_bytes
86747
 
#define sqlite3_blob_close             sqlite3_api->blob_close
86748
 
#define sqlite3_blob_open              sqlite3_api->blob_open
86749
 
#define sqlite3_blob_read              sqlite3_api->blob_read
86750
 
#define sqlite3_blob_write             sqlite3_api->blob_write
86751
 
#define sqlite3_create_collation_v2    sqlite3_api->create_collation_v2
86752
 
#define sqlite3_file_control           sqlite3_api->file_control
86753
 
#define sqlite3_memory_highwater       sqlite3_api->memory_highwater
86754
 
#define sqlite3_memory_used            sqlite3_api->memory_used
86755
 
#define sqlite3_mutex_alloc            sqlite3_api->mutex_alloc
86756
 
#define sqlite3_mutex_enter            sqlite3_api->mutex_enter
86757
 
#define sqlite3_mutex_free             sqlite3_api->mutex_free
86758
 
#define sqlite3_mutex_leave            sqlite3_api->mutex_leave
86759
 
#define sqlite3_mutex_try              sqlite3_api->mutex_try
86760
 
#define sqlite3_open_v2                sqlite3_api->open_v2
86761
 
#define sqlite3_release_memory         sqlite3_api->release_memory
86762
 
#define sqlite3_result_error_nomem     sqlite3_api->result_error_nomem
86763
 
#define sqlite3_result_error_toobig    sqlite3_api->result_error_toobig
86764
 
#define sqlite3_sleep                  sqlite3_api->sleep
86765
 
#define sqlite3_soft_heap_limit        sqlite3_api->soft_heap_limit
86766
 
#define sqlite3_vfs_find               sqlite3_api->vfs_find
86767
 
#define sqlite3_vfs_register           sqlite3_api->vfs_register
86768
 
#define sqlite3_vfs_unregister         sqlite3_api->vfs_unregister
86769
 
#define sqlite3_threadsafe             sqlite3_api->xthreadsafe
86770
 
#define sqlite3_result_zeroblob        sqlite3_api->result_zeroblob
86771
 
#define sqlite3_result_error_code      sqlite3_api->result_error_code
86772
 
#define sqlite3_test_control           sqlite3_api->test_control
86773
 
#define sqlite3_randomness             sqlite3_api->randomness
86774
 
#define sqlite3_context_db_handle      sqlite3_api->context_db_handle
86775
 
#define sqlite3_extended_result_codes  sqlite3_api->extended_result_codes
86776
 
#define sqlite3_limit                  sqlite3_api->limit
86777
 
#define sqlite3_next_stmt              sqlite3_api->next_stmt
86778
 
#define sqlite3_sql                    sqlite3_api->sql
86779
 
#define sqlite3_status                 sqlite3_api->status
86780
 
#define sqlite3_backup_finish          sqlite3_api->backup_finish
86781
 
#define sqlite3_backup_init            sqlite3_api->backup_init
86782
 
#define sqlite3_backup_pagecount       sqlite3_api->backup_pagecount
86783
 
#define sqlite3_backup_remaining       sqlite3_api->backup_remaining
86784
 
#define sqlite3_backup_step            sqlite3_api->backup_step
86785
 
#define sqlite3_compileoption_get      sqlite3_api->compileoption_get
86786
 
#define sqlite3_compileoption_used     sqlite3_api->compileoption_used
86787
 
#define sqlite3_create_function_v2     sqlite3_api->create_function_v2
86788
 
#define sqlite3_db_config              sqlite3_api->db_config
86789
 
#define sqlite3_db_mutex               sqlite3_api->db_mutex
86790
 
#define sqlite3_db_status              sqlite3_api->db_status
86791
 
#define sqlite3_extended_errcode       sqlite3_api->extended_errcode
86792
 
#define sqlite3_log                    sqlite3_api->log
86793
 
#define sqlite3_soft_heap_limit64      sqlite3_api->soft_heap_limit64
86794
 
#define sqlite3_sourceid               sqlite3_api->sourceid
86795
 
#define sqlite3_stmt_status            sqlite3_api->stmt_status
86796
 
#define sqlite3_strnicmp               sqlite3_api->strnicmp
86797
 
#define sqlite3_unlock_notify          sqlite3_api->unlock_notify
86798
 
#define sqlite3_wal_autocheckpoint     sqlite3_api->wal_autocheckpoint
86799
 
#define sqlite3_wal_checkpoint         sqlite3_api->wal_checkpoint
86800
 
#define sqlite3_wal_hook               sqlite3_api->wal_hook
86801
 
#endif /* SQLITE_CORE */
86802
 
 
86803
 
#define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api = 0;
86804
 
#define SQLITE_EXTENSION_INIT2(v)  sqlite3_api = v;
86805
 
 
86806
 
#endif /* _SQLITE3EXT_H_ */
86807
 
 
86808
 
/************** End of sqlite3ext.h ******************************************/
86809
 
/************** Continuing where we left off in loadext.c ********************/
86810
 
 
86811
 
#ifndef SQLITE_OMIT_LOAD_EXTENSION
86812
 
 
86813
 
/*
86814
 
** Some API routines are omitted when various features are
86815
 
** excluded from a build of SQLite.  Substitute a NULL pointer
86816
 
** for any missing APIs.
86817
 
*/
86818
 
#ifndef SQLITE_ENABLE_COLUMN_METADATA
86819
 
# define sqlite3_column_database_name   0
86820
 
# define sqlite3_column_database_name16 0
86821
 
# define sqlite3_column_table_name      0
86822
 
# define sqlite3_column_table_name16    0
86823
 
# define sqlite3_column_origin_name     0
86824
 
# define sqlite3_column_origin_name16   0
86825
 
# define sqlite3_table_column_metadata  0
86826
 
#endif
86827
 
 
86828
 
#ifdef SQLITE_OMIT_AUTHORIZATION
86829
 
# define sqlite3_set_authorizer         0
86830
 
#endif
86831
 
 
86832
 
#ifdef SQLITE_OMIT_UTF16
86833
 
# define sqlite3_bind_text16            0
86834
 
# define sqlite3_collation_needed16     0
86835
 
# define sqlite3_column_decltype16      0
86836
 
# define sqlite3_column_name16          0
86837
 
# define sqlite3_column_text16          0
86838
 
# define sqlite3_complete16             0
86839
 
# define sqlite3_create_collation16     0
86840
 
# define sqlite3_create_function16      0
86841
 
# define sqlite3_errmsg16               0
86842
 
# define sqlite3_open16                 0
86843
 
# define sqlite3_prepare16              0
86844
 
# define sqlite3_prepare16_v2           0
86845
 
# define sqlite3_result_error16         0
86846
 
# define sqlite3_result_text16          0
86847
 
# define sqlite3_result_text16be        0
86848
 
# define sqlite3_result_text16le        0
86849
 
# define sqlite3_value_text16           0
86850
 
# define sqlite3_value_text16be         0
86851
 
# define sqlite3_value_text16le         0
86852
 
# define sqlite3_column_database_name16 0
86853
 
# define sqlite3_column_table_name16    0
86854
 
# define sqlite3_column_origin_name16   0
86855
 
#endif
86856
 
 
86857
 
#ifdef SQLITE_OMIT_COMPLETE
86858
 
# define sqlite3_complete 0
86859
 
# define sqlite3_complete16 0
86860
 
#endif
86861
 
 
86862
 
#ifdef SQLITE_OMIT_DECLTYPE
86863
 
# define sqlite3_column_decltype16      0
86864
 
# define sqlite3_column_decltype        0
86865
 
#endif
86866
 
 
86867
 
#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
86868
 
# define sqlite3_progress_handler 0
86869
 
#endif
86870
 
 
86871
 
#ifdef SQLITE_OMIT_VIRTUALTABLE
86872
 
# define sqlite3_create_module 0
86873
 
# define sqlite3_create_module_v2 0
86874
 
# define sqlite3_declare_vtab 0
86875
 
#endif
86876
 
 
86877
 
#ifdef SQLITE_OMIT_SHARED_CACHE
86878
 
# define sqlite3_enable_shared_cache 0
86879
 
#endif
86880
 
 
86881
 
#ifdef SQLITE_OMIT_TRACE
86882
 
# define sqlite3_profile       0
86883
 
# define sqlite3_trace         0
86884
 
#endif
86885
 
 
86886
 
#ifdef SQLITE_OMIT_GET_TABLE
86887
 
# define sqlite3_free_table    0
86888
 
# define sqlite3_get_table     0
86889
 
#endif
86890
 
 
86891
 
#ifdef SQLITE_OMIT_INCRBLOB
86892
 
#define sqlite3_bind_zeroblob  0
86893
 
#define sqlite3_blob_bytes     0
86894
 
#define sqlite3_blob_close     0
86895
 
#define sqlite3_blob_open      0
86896
 
#define sqlite3_blob_read      0
86897
 
#define sqlite3_blob_write     0
86898
 
#endif
86899
 
 
86900
 
/*
86901
 
** The following structure contains pointers to all SQLite API routines.
86902
 
** A pointer to this structure is passed into extensions when they are
86903
 
** loaded so that the extension can make calls back into the SQLite
86904
 
** library.
86905
 
**
86906
 
** When adding new APIs, add them to the bottom of this structure
86907
 
** in order to preserve backwards compatibility.
86908
 
**
86909
 
** Extensions that use newer APIs should first call the
86910
 
** sqlite3_libversion_number() to make sure that the API they
86911
 
** intend to use is supported by the library.  Extensions should
86912
 
** also check to make sure that the pointer to the function is
86913
 
** not NULL before calling it.
86914
 
*/
86915
 
static const sqlite3_api_routines sqlite3Apis = {
86916
 
  sqlite3_aggregate_context,
86917
 
#ifndef SQLITE_OMIT_DEPRECATED
86918
 
  sqlite3_aggregate_count,
86919
 
#else
86920
 
  0,
86921
 
#endif
86922
 
  sqlite3_bind_blob,
86923
 
  sqlite3_bind_double,
86924
 
  sqlite3_bind_int,
86925
 
  sqlite3_bind_int64,
86926
 
  sqlite3_bind_null,
86927
 
  sqlite3_bind_parameter_count,
86928
 
  sqlite3_bind_parameter_index,
86929
 
  sqlite3_bind_parameter_name,
86930
 
  sqlite3_bind_text,
86931
 
  sqlite3_bind_text16,
86932
 
  sqlite3_bind_value,
86933
 
  sqlite3_busy_handler,
86934
 
  sqlite3_busy_timeout,
86935
 
  sqlite3_changes,
86936
 
  sqlite3_close,
86937
 
  sqlite3_collation_needed,
86938
 
  sqlite3_collation_needed16,
86939
 
  sqlite3_column_blob,
86940
 
  sqlite3_column_bytes,
86941
 
  sqlite3_column_bytes16,
86942
 
  sqlite3_column_count,
86943
 
  sqlite3_column_database_name,
86944
 
  sqlite3_column_database_name16,
86945
 
  sqlite3_column_decltype,
86946
 
  sqlite3_column_decltype16,
86947
 
  sqlite3_column_double,
86948
 
  sqlite3_column_int,
86949
 
  sqlite3_column_int64,
86950
 
  sqlite3_column_name,
86951
 
  sqlite3_column_name16,
86952
 
  sqlite3_column_origin_name,
86953
 
  sqlite3_column_origin_name16,
86954
 
  sqlite3_column_table_name,
86955
 
  sqlite3_column_table_name16,
86956
 
  sqlite3_column_text,
86957
 
  sqlite3_column_text16,
86958
 
  sqlite3_column_type,
86959
 
  sqlite3_column_value,
86960
 
  sqlite3_commit_hook,
86961
 
  sqlite3_complete,
86962
 
  sqlite3_complete16,
86963
 
  sqlite3_create_collation,
86964
 
  sqlite3_create_collation16,
86965
 
  sqlite3_create_function,
86966
 
  sqlite3_create_function16,
86967
 
  sqlite3_create_module,
86968
 
  sqlite3_data_count,
86969
 
  sqlite3_db_handle,
86970
 
  sqlite3_declare_vtab,
86971
 
  sqlite3_enable_shared_cache,
86972
 
  sqlite3_errcode,
86973
 
  sqlite3_errmsg,
86974
 
  sqlite3_errmsg16,
86975
 
  sqlite3_exec,
86976
 
#ifndef SQLITE_OMIT_DEPRECATED
86977
 
  sqlite3_expired,
86978
 
#else
86979
 
  0,
86980
 
#endif
86981
 
  sqlite3_finalize,
86982
 
  sqlite3_free,
86983
 
  sqlite3_free_table,
86984
 
  sqlite3_get_autocommit,
86985
 
  sqlite3_get_auxdata,
86986
 
  sqlite3_get_table,
86987
 
  0,     /* Was sqlite3_global_recover(), but that function is deprecated */
86988
 
  sqlite3_interrupt,
86989
 
  sqlite3_last_insert_rowid,
86990
 
  sqlite3_libversion,
86991
 
  sqlite3_libversion_number,
86992
 
  sqlite3_malloc,
86993
 
  sqlite3_mprintf,
86994
 
  sqlite3_open,
86995
 
  sqlite3_open16,
86996
 
  sqlite3_prepare,
86997
 
  sqlite3_prepare16,
86998
 
  sqlite3_profile,
86999
 
  sqlite3_progress_handler,
87000
 
  sqlite3_realloc,
87001
 
  sqlite3_reset,
87002
 
  sqlite3_result_blob,
87003
 
  sqlite3_result_double,
87004
 
  sqlite3_result_error,
87005
 
  sqlite3_result_error16,
87006
 
  sqlite3_result_int,
87007
 
  sqlite3_result_int64,
87008
 
  sqlite3_result_null,
87009
 
  sqlite3_result_text,
87010
 
  sqlite3_result_text16,
87011
 
  sqlite3_result_text16be,
87012
 
  sqlite3_result_text16le,
87013
 
  sqlite3_result_value,
87014
 
  sqlite3_rollback_hook,
87015
 
  sqlite3_set_authorizer,
87016
 
  sqlite3_set_auxdata,
87017
 
  sqlite3_snprintf,
87018
 
  sqlite3_step,
87019
 
  sqlite3_table_column_metadata,
87020
 
#ifndef SQLITE_OMIT_DEPRECATED
87021
 
  sqlite3_thread_cleanup,
87022
 
#else
87023
 
  0,
87024
 
#endif
87025
 
  sqlite3_total_changes,
87026
 
  sqlite3_trace,
87027
 
#ifndef SQLITE_OMIT_DEPRECATED
87028
 
  sqlite3_transfer_bindings,
87029
 
#else
87030
 
  0,
87031
 
#endif
87032
 
  sqlite3_update_hook,
87033
 
  sqlite3_user_data,
87034
 
  sqlite3_value_blob,
87035
 
  sqlite3_value_bytes,
87036
 
  sqlite3_value_bytes16,
87037
 
  sqlite3_value_double,
87038
 
  sqlite3_value_int,
87039
 
  sqlite3_value_int64,
87040
 
  sqlite3_value_numeric_type,
87041
 
  sqlite3_value_text,
87042
 
  sqlite3_value_text16,
87043
 
  sqlite3_value_text16be,
87044
 
  sqlite3_value_text16le,
87045
 
  sqlite3_value_type,
87046
 
  sqlite3_vmprintf,
87047
 
  /*
87048
 
  ** The original API set ends here.  All extensions can call any
87049
 
  ** of the APIs above provided that the pointer is not NULL.  But
87050
 
  ** before calling APIs that follow, extension should check the
87051
 
  ** sqlite3_libversion_number() to make sure they are dealing with
87052
 
  ** a library that is new enough to support that API.
87053
 
  *************************************************************************
87054
 
  */
87055
 
  sqlite3_overload_function,
87056
 
 
87057
 
  /*
87058
 
  ** Added after 3.3.13
87059
 
  */
87060
 
  sqlite3_prepare_v2,
87061
 
  sqlite3_prepare16_v2,
87062
 
  sqlite3_clear_bindings,
87063
 
 
87064
 
  /*
87065
 
  ** Added for 3.4.1
87066
 
  */
87067
 
  sqlite3_create_module_v2,
87068
 
 
87069
 
  /*
87070
 
  ** Added for 3.5.0
87071
 
  */
87072
 
  sqlite3_bind_zeroblob,
87073
 
  sqlite3_blob_bytes,
87074
 
  sqlite3_blob_close,
87075
 
  sqlite3_blob_open,
87076
 
  sqlite3_blob_read,
87077
 
  sqlite3_blob_write,
87078
 
  sqlite3_create_collation_v2,
87079
 
  sqlite3_file_control,
87080
 
  sqlite3_memory_highwater,
87081
 
  sqlite3_memory_used,
87082
 
#ifdef SQLITE_MUTEX_OMIT
87083
 
  0, 
87084
 
  0, 
87085
 
  0,
87086
 
  0,
87087
 
  0,
87088
 
#else
87089
 
  sqlite3_mutex_alloc,
87090
 
  sqlite3_mutex_enter,
87091
 
  sqlite3_mutex_free,
87092
 
  sqlite3_mutex_leave,
87093
 
  sqlite3_mutex_try,
87094
 
#endif
87095
 
  sqlite3_open_v2,
87096
 
  sqlite3_release_memory,
87097
 
  sqlite3_result_error_nomem,
87098
 
  sqlite3_result_error_toobig,
87099
 
  sqlite3_sleep,
87100
 
  sqlite3_soft_heap_limit,
87101
 
  sqlite3_vfs_find,
87102
 
  sqlite3_vfs_register,
87103
 
  sqlite3_vfs_unregister,
87104
 
 
87105
 
  /*
87106
 
  ** Added for 3.5.8
87107
 
  */
87108
 
  sqlite3_threadsafe,
87109
 
  sqlite3_result_zeroblob,
87110
 
  sqlite3_result_error_code,
87111
 
  sqlite3_test_control,
87112
 
  sqlite3_randomness,
87113
 
  sqlite3_context_db_handle,
87114
 
 
87115
 
  /*
87116
 
  ** Added for 3.6.0
87117
 
  */
87118
 
  sqlite3_extended_result_codes,
87119
 
  sqlite3_limit,
87120
 
  sqlite3_next_stmt,
87121
 
  sqlite3_sql,
87122
 
  sqlite3_status,
87123
 
 
87124
 
  /*
87125
 
  ** Added for 3.7.4
87126
 
  */
87127
 
  sqlite3_backup_finish,
87128
 
  sqlite3_backup_init,
87129
 
  sqlite3_backup_pagecount,
87130
 
  sqlite3_backup_remaining,
87131
 
  sqlite3_backup_step,
87132
 
#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
87133
 
  sqlite3_compileoption_get,
87134
 
  sqlite3_compileoption_used,
87135
 
#else
87136
 
  0,
87137
 
  0,
87138
 
#endif
87139
 
  sqlite3_create_function_v2,
87140
 
  sqlite3_db_config,
87141
 
  sqlite3_db_mutex,
87142
 
  sqlite3_db_status,
87143
 
  sqlite3_extended_errcode,
87144
 
  sqlite3_log,
87145
 
  sqlite3_soft_heap_limit64,
87146
 
  sqlite3_sourceid,
87147
 
  sqlite3_stmt_status,
87148
 
  sqlite3_strnicmp,
87149
 
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
87150
 
  sqlite3_unlock_notify,
87151
 
#else
87152
 
  0,
87153
 
#endif
87154
 
#ifndef SQLITE_OMIT_WAL
87155
 
  sqlite3_wal_autocheckpoint,
87156
 
  sqlite3_wal_checkpoint,
87157
 
  sqlite3_wal_hook,
87158
 
#else
87159
 
  0,
87160
 
  0,
87161
 
  0,
87162
 
#endif
87163
 
};
87164
 
 
87165
 
/*
87166
 
** Attempt to load an SQLite extension library contained in the file
87167
 
** zFile.  The entry point is zProc.  zProc may be 0 in which case a
87168
 
** default entry point name (sqlite3_extension_init) is used.  Use
87169
 
** of the default name is recommended.
87170
 
**
87171
 
** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
87172
 
**
87173
 
** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with 
87174
 
** error message text.  The calling function should free this memory
87175
 
** by calling sqlite3DbFree(db, ).
87176
 
*/
87177
 
static int sqlite3LoadExtension(
87178
 
  sqlite3 *db,          /* Load the extension into this database connection */
87179
 
  const char *zFile,    /* Name of the shared library containing extension */
87180
 
  const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
87181
 
  char **pzErrMsg       /* Put error message here if not 0 */
87182
 
){
87183
 
  sqlite3_vfs *pVfs = db->pVfs;
87184
 
  void *handle;
87185
 
  int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
87186
 
  char *zErrmsg = 0;
87187
 
  void **aHandle;
87188
 
  const int nMsg = 300;
87189
 
 
87190
 
  if( pzErrMsg ) *pzErrMsg = 0;
87191
 
 
87192
 
  /* Ticket #1863.  To avoid a creating security problems for older
87193
 
  ** applications that relink against newer versions of SQLite, the
87194
 
  ** ability to run load_extension is turned off by default.  One
87195
 
  ** must call sqlite3_enable_load_extension() to turn on extension
87196
 
  ** loading.  Otherwise you get the following error.
87197
 
  */
87198
 
  if( (db->flags & SQLITE_LoadExtension)==0 ){
87199
 
    if( pzErrMsg ){
87200
 
      *pzErrMsg = sqlite3_mprintf("not authorized");
87201
 
    }
87202
 
    return SQLITE_ERROR;
87203
 
  }
87204
 
 
87205
 
  if( zProc==0 ){
87206
 
    zProc = "sqlite3_extension_init";
87207
 
  }
87208
 
 
87209
 
  handle = sqlite3OsDlOpen(pVfs, zFile);
87210
 
  if( handle==0 ){
87211
 
    if( pzErrMsg ){
87212
 
      *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
87213
 
      if( zErrmsg ){
87214
 
        sqlite3_snprintf(nMsg, zErrmsg, 
87215
 
            "unable to open shared library [%s]", zFile);
87216
 
        sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
87217
 
      }
87218
 
    }
87219
 
    return SQLITE_ERROR;
87220
 
  }
87221
 
  xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
87222
 
                   sqlite3OsDlSym(pVfs, handle, zProc);
87223
 
  if( xInit==0 ){
87224
 
    if( pzErrMsg ){
87225
 
      *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
87226
 
      if( zErrmsg ){
87227
 
        sqlite3_snprintf(nMsg, zErrmsg,
87228
 
            "no entry point [%s] in shared library [%s]", zProc,zFile);
87229
 
        sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
87230
 
      }
87231
 
      sqlite3OsDlClose(pVfs, handle);
87232
 
    }
87233
 
    return SQLITE_ERROR;
87234
 
  }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){
87235
 
    if( pzErrMsg ){
87236
 
      *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
87237
 
    }
87238
 
    sqlite3_free(zErrmsg);
87239
 
    sqlite3OsDlClose(pVfs, handle);
87240
 
    return SQLITE_ERROR;
87241
 
  }
87242
 
 
87243
 
  /* Append the new shared library handle to the db->aExtension array. */
87244
 
  aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
87245
 
  if( aHandle==0 ){
87246
 
    return SQLITE_NOMEM;
87247
 
  }
87248
 
  if( db->nExtension>0 ){
87249
 
    memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
87250
 
  }
87251
 
  sqlite3DbFree(db, db->aExtension);
87252
 
  db->aExtension = aHandle;
87253
 
 
87254
 
  db->aExtension[db->nExtension++] = handle;
87255
 
  return SQLITE_OK;
87256
 
}
87257
 
SQLITE_API int sqlite3_load_extension(
87258
 
  sqlite3 *db,          /* Load the extension into this database connection */
87259
 
  const char *zFile,    /* Name of the shared library containing extension */
87260
 
  const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
87261
 
  char **pzErrMsg       /* Put error message here if not 0 */
87262
 
){
87263
 
  int rc;
87264
 
  sqlite3_mutex_enter(db->mutex);
87265
 
  rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
87266
 
  rc = sqlite3ApiExit(db, rc);
87267
 
  sqlite3_mutex_leave(db->mutex);
87268
 
  return rc;
87269
 
}
87270
 
 
87271
 
/*
87272
 
** Call this routine when the database connection is closing in order
87273
 
** to clean up loaded extensions
87274
 
*/
87275
 
SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
87276
 
  int i;
87277
 
  assert( sqlite3_mutex_held(db->mutex) );
87278
 
  for(i=0; i<db->nExtension; i++){
87279
 
    sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
87280
 
  }
87281
 
  sqlite3DbFree(db, db->aExtension);
87282
 
}
87283
 
 
87284
 
/*
87285
 
** Enable or disable extension loading.  Extension loading is disabled by
87286
 
** default so as not to open security holes in older applications.
87287
 
*/
87288
 
SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
87289
 
  sqlite3_mutex_enter(db->mutex);
87290
 
  if( onoff ){
87291
 
    db->flags |= SQLITE_LoadExtension;
87292
 
  }else{
87293
 
    db->flags &= ~SQLITE_LoadExtension;
87294
 
  }
87295
 
  sqlite3_mutex_leave(db->mutex);
87296
 
  return SQLITE_OK;
87297
 
}
87298
 
 
87299
 
#endif /* SQLITE_OMIT_LOAD_EXTENSION */
87300
 
 
87301
 
/*
87302
 
** The auto-extension code added regardless of whether or not extension
87303
 
** loading is supported.  We need a dummy sqlite3Apis pointer for that
87304
 
** code if regular extension loading is not available.  This is that
87305
 
** dummy pointer.
87306
 
*/
87307
 
#ifdef SQLITE_OMIT_LOAD_EXTENSION
87308
 
static const sqlite3_api_routines sqlite3Apis = { 0 };
87309
 
#endif
87310
 
 
87311
 
 
87312
 
/*
87313
 
** The following object holds the list of automatically loaded
87314
 
** extensions.
87315
 
**
87316
 
** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
87317
 
** mutex must be held while accessing this list.
87318
 
*/
87319
 
typedef struct sqlite3AutoExtList sqlite3AutoExtList;
87320
 
static SQLITE_WSD struct sqlite3AutoExtList {
87321
 
  int nExt;              /* Number of entries in aExt[] */          
87322
 
  void (**aExt)(void);   /* Pointers to the extension init functions */
87323
 
} sqlite3Autoext = { 0, 0 };
87324
 
 
87325
 
/* The "wsdAutoext" macro will resolve to the autoextension
87326
 
** state vector.  If writable static data is unsupported on the target,
87327
 
** we have to locate the state vector at run-time.  In the more common
87328
 
** case where writable static data is supported, wsdStat can refer directly
87329
 
** to the "sqlite3Autoext" state vector declared above.
87330
 
*/
87331
 
#ifdef SQLITE_OMIT_WSD
87332
 
# define wsdAutoextInit \
87333
 
  sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
87334
 
# define wsdAutoext x[0]
87335
 
#else
87336
 
# define wsdAutoextInit
87337
 
# define wsdAutoext sqlite3Autoext
87338
 
#endif
87339
 
 
87340
 
 
87341
 
/*
87342
 
** Register a statically linked extension that is automatically
87343
 
** loaded by every new database connection.
87344
 
*/
87345
 
SQLITE_API int sqlite3_auto_extension(void (*xInit)(void)){
87346
 
  int rc = SQLITE_OK;
87347
 
#ifndef SQLITE_OMIT_AUTOINIT
87348
 
  rc = sqlite3_initialize();
87349
 
  if( rc ){
87350
 
    return rc;
87351
 
  }else
87352
 
#endif
87353
 
  {
87354
 
    int i;
87355
 
#if SQLITE_THREADSAFE
87356
 
    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
87357
 
#endif
87358
 
    wsdAutoextInit;
87359
 
    sqlite3_mutex_enter(mutex);
87360
 
    for(i=0; i<wsdAutoext.nExt; i++){
87361
 
      if( wsdAutoext.aExt[i]==xInit ) break;
87362
 
    }
87363
 
    if( i==wsdAutoext.nExt ){
87364
 
      int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
87365
 
      void (**aNew)(void);
87366
 
      aNew = sqlite3_realloc(wsdAutoext.aExt, nByte);
87367
 
      if( aNew==0 ){
87368
 
        rc = SQLITE_NOMEM;
87369
 
      }else{
87370
 
        wsdAutoext.aExt = aNew;
87371
 
        wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
87372
 
        wsdAutoext.nExt++;
87373
 
      }
87374
 
    }
87375
 
    sqlite3_mutex_leave(mutex);
87376
 
    assert( (rc&0xff)==rc );
87377
 
    return rc;
87378
 
  }
87379
 
}
87380
 
 
87381
 
/*
87382
 
** Reset the automatic extension loading mechanism.
87383
 
*/
87384
 
SQLITE_API void sqlite3_reset_auto_extension(void){
87385
 
#ifndef SQLITE_OMIT_AUTOINIT
87386
 
  if( sqlite3_initialize()==SQLITE_OK )
87387
 
#endif
87388
 
  {
87389
 
#if SQLITE_THREADSAFE
87390
 
    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
87391
 
#endif
87392
 
    wsdAutoextInit;
87393
 
    sqlite3_mutex_enter(mutex);
87394
 
    sqlite3_free(wsdAutoext.aExt);
87395
 
    wsdAutoext.aExt = 0;
87396
 
    wsdAutoext.nExt = 0;
87397
 
    sqlite3_mutex_leave(mutex);
87398
 
  }
87399
 
}
87400
 
 
87401
 
/*
87402
 
** Load all automatic extensions.
87403
 
**
87404
 
** If anything goes wrong, set an error in the database connection.
87405
 
*/
87406
 
SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
87407
 
  int i;
87408
 
  int go = 1;
87409
 
  int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
87410
 
 
87411
 
  wsdAutoextInit;
87412
 
  if( wsdAutoext.nExt==0 ){
87413
 
    /* Common case: early out without every having to acquire a mutex */
87414
 
    return;
87415
 
  }
87416
 
  for(i=0; go; i++){
87417
 
    char *zErrmsg;
87418
 
#if SQLITE_THREADSAFE
87419
 
    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
87420
 
#endif
87421
 
    sqlite3_mutex_enter(mutex);
87422
 
    if( i>=wsdAutoext.nExt ){
87423
 
      xInit = 0;
87424
 
      go = 0;
87425
 
    }else{
87426
 
      xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
87427
 
              wsdAutoext.aExt[i];
87428
 
    }
87429
 
    sqlite3_mutex_leave(mutex);
87430
 
    zErrmsg = 0;
87431
 
    if( xInit && xInit(db, &zErrmsg, &sqlite3Apis) ){
87432
 
      sqlite3Error(db, SQLITE_ERROR,
87433
 
            "automatic extension loading failed: %s", zErrmsg);
87434
 
      go = 0;
87435
 
    }
87436
 
    sqlite3_free(zErrmsg);
87437
 
  }
87438
 
}
87439
 
 
87440
 
/************** End of loadext.c *********************************************/
87441
 
/************** Begin file pragma.c ******************************************/
87442
 
/*
87443
 
** 2003 April 6
87444
 
**
87445
 
** The author disclaims copyright to this source code.  In place of
87446
 
** a legal notice, here is a blessing:
87447
 
**
87448
 
**    May you do good and not evil.
87449
 
**    May you find forgiveness for yourself and forgive others.
87450
 
**    May you share freely, never taking more than you give.
87451
 
**
87452
 
*************************************************************************
87453
 
** This file contains code used to implement the PRAGMA command.
87454
 
*/
87455
 
 
87456
 
/* Ignore this whole file if pragmas are disabled
87457
 
*/
87458
 
#if !defined(SQLITE_OMIT_PRAGMA)
87459
 
 
87460
 
/*
87461
 
** Interpret the given string as a safety level.  Return 0 for OFF,
87462
 
** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or 
87463
 
** unrecognized string argument.
87464
 
**
87465
 
** Note that the values returned are one less that the values that
87466
 
** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
87467
 
** to support legacy SQL code.  The safety level used to be boolean
87468
 
** and older scripts may have used numbers 0 for OFF and 1 for ON.
87469
 
*/
87470
 
static u8 getSafetyLevel(const char *z){
87471
 
                             /* 123456789 123456789 */
87472
 
  static const char zText[] = "onoffalseyestruefull";
87473
 
  static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
87474
 
  static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
87475
 
  static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
87476
 
  int i, n;
87477
 
  if( sqlite3Isdigit(*z) ){
87478
 
    return (u8)sqlite3Atoi(z);
87479
 
  }
87480
 
  n = sqlite3Strlen30(z);
87481
 
  for(i=0; i<ArraySize(iLength); i++){
87482
 
    if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
87483
 
      return iValue[i];
87484
 
    }
87485
 
  }
87486
 
  return 1;
87487
 
}
87488
 
 
87489
 
/*
87490
 
** Interpret the given string as a boolean value.
87491
 
*/
87492
 
static u8 getBoolean(const char *z){
87493
 
  return getSafetyLevel(z)&1;
87494
 
}
87495
 
 
87496
 
/*
87497
 
** Interpret the given string as a locking mode value.
87498
 
*/
87499
 
static int getLockingMode(const char *z){
87500
 
  if( z ){
87501
 
    if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
87502
 
    if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
87503
 
  }
87504
 
  return PAGER_LOCKINGMODE_QUERY;
87505
 
}
87506
 
 
87507
 
#ifndef SQLITE_OMIT_AUTOVACUUM
87508
 
/*
87509
 
** Interpret the given string as an auto-vacuum mode value.
87510
 
**
87511
 
** The following strings, "none", "full" and "incremental" are 
87512
 
** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
87513
 
*/
87514
 
static int getAutoVacuum(const char *z){
87515
 
  int i;
87516
 
  if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
87517
 
  if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
87518
 
  if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
87519
 
  i = sqlite3Atoi(z);
87520
 
  return (u8)((i>=0&&i<=2)?i:0);
87521
 
}
87522
 
#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
87523
 
 
87524
 
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
87525
 
/*
87526
 
** Interpret the given string as a temp db location. Return 1 for file
87527
 
** backed temporary databases, 2 for the Red-Black tree in memory database
87528
 
** and 0 to use the compile-time default.
87529
 
*/
87530
 
static int getTempStore(const char *z){
87531
 
  if( z[0]>='0' && z[0]<='2' ){
87532
 
    return z[0] - '0';
87533
 
  }else if( sqlite3StrICmp(z, "file")==0 ){
87534
 
    return 1;
87535
 
  }else if( sqlite3StrICmp(z, "memory")==0 ){
87536
 
    return 2;
87537
 
  }else{
87538
 
    return 0;
87539
 
  }
87540
 
}
87541
 
#endif /* SQLITE_PAGER_PRAGMAS */
87542
 
 
87543
 
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
87544
 
/*
87545
 
** Invalidate temp storage, either when the temp storage is changed
87546
 
** from default, or when 'file' and the temp_store_directory has changed
87547
 
*/
87548
 
static int invalidateTempStorage(Parse *pParse){
87549
 
  sqlite3 *db = pParse->db;
87550
 
  if( db->aDb[1].pBt!=0 ){
87551
 
    if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
87552
 
      sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
87553
 
        "from within a transaction");
87554
 
      return SQLITE_ERROR;
87555
 
    }
87556
 
    sqlite3BtreeClose(db->aDb[1].pBt);
87557
 
    db->aDb[1].pBt = 0;
87558
 
    sqlite3ResetInternalSchema(db, -1);
87559
 
  }
87560
 
  return SQLITE_OK;
87561
 
}
87562
 
#endif /* SQLITE_PAGER_PRAGMAS */
87563
 
 
87564
 
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
87565
 
/*
87566
 
** If the TEMP database is open, close it and mark the database schema
87567
 
** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
87568
 
** or DEFAULT_TEMP_STORE pragmas.
87569
 
*/
87570
 
static int changeTempStorage(Parse *pParse, const char *zStorageType){
87571
 
  int ts = getTempStore(zStorageType);
87572
 
  sqlite3 *db = pParse->db;
87573
 
  if( db->temp_store==ts ) return SQLITE_OK;
87574
 
  if( invalidateTempStorage( pParse ) != SQLITE_OK ){
87575
 
    return SQLITE_ERROR;
87576
 
  }
87577
 
  db->temp_store = (u8)ts;
87578
 
  return SQLITE_OK;
87579
 
}
87580
 
#endif /* SQLITE_PAGER_PRAGMAS */
87581
 
 
87582
 
/*
87583
 
** Generate code to return a single integer value.
87584
 
*/
87585
 
static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
87586
 
  Vdbe *v = sqlite3GetVdbe(pParse);
87587
 
  int mem = ++pParse->nMem;
87588
 
  i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
87589
 
  if( pI64 ){
87590
 
    memcpy(pI64, &value, sizeof(value));
87591
 
  }
87592
 
  sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
87593
 
  sqlite3VdbeSetNumCols(v, 1);
87594
 
  sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
87595
 
  sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
87596
 
}
87597
 
 
87598
 
#ifndef SQLITE_OMIT_FLAG_PRAGMAS
87599
 
/*
87600
 
** Check to see if zRight and zLeft refer to a pragma that queries
87601
 
** or changes one of the flags in db->flags.  Return 1 if so and 0 if not.
87602
 
** Also, implement the pragma.
87603
 
*/
87604
 
static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
87605
 
  static const struct sPragmaType {
87606
 
    const char *zName;  /* Name of the pragma */
87607
 
    int mask;           /* Mask for the db->flags value */
87608
 
  } aPragma[] = {
87609
 
    { "full_column_names",        SQLITE_FullColNames  },
87610
 
    { "short_column_names",       SQLITE_ShortColNames },
87611
 
    { "count_changes",            SQLITE_CountRows     },
87612
 
    { "empty_result_callbacks",   SQLITE_NullCallback  },
87613
 
    { "legacy_file_format",       SQLITE_LegacyFileFmt },
87614
 
    { "fullfsync",                SQLITE_FullFSync     },
87615
 
    { "checkpoint_fullfsync",     SQLITE_CkptFullFSync },
87616
 
    { "reverse_unordered_selects", SQLITE_ReverseOrder  },
87617
 
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
87618
 
    { "automatic_index",          SQLITE_AutoIndex     },
87619
 
#endif
87620
 
#ifdef SQLITE_DEBUG
87621
 
    { "sql_trace",                SQLITE_SqlTrace      },
87622
 
    { "vdbe_listing",             SQLITE_VdbeListing   },
87623
 
    { "vdbe_trace",               SQLITE_VdbeTrace     },
87624
 
#endif
87625
 
#ifndef SQLITE_OMIT_CHECK
87626
 
    { "ignore_check_constraints", SQLITE_IgnoreChecks  },
87627
 
#endif
87628
 
    /* The following is VERY experimental */
87629
 
    { "writable_schema",          SQLITE_WriteSchema|SQLITE_RecoveryMode },
87630
 
    { "omit_readlock",            SQLITE_NoReadlock    },
87631
 
 
87632
 
    /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
87633
 
    ** flag if there are any active statements. */
87634
 
    { "read_uncommitted",         SQLITE_ReadUncommitted },
87635
 
    { "recursive_triggers",       SQLITE_RecTriggers },
87636
 
 
87637
 
    /* This flag may only be set if both foreign-key and trigger support
87638
 
    ** are present in the build.  */
87639
 
#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
87640
 
    { "foreign_keys",             SQLITE_ForeignKeys },
87641
 
#endif
87642
 
  };
87643
 
  int i;
87644
 
  const struct sPragmaType *p;
87645
 
  for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
87646
 
    if( sqlite3StrICmp(zLeft, p->zName)==0 ){
87647
 
      sqlite3 *db = pParse->db;
87648
 
      Vdbe *v;
87649
 
      v = sqlite3GetVdbe(pParse);
87650
 
      assert( v!=0 );  /* Already allocated by sqlite3Pragma() */
87651
 
      if( ALWAYS(v) ){
87652
 
        if( zRight==0 ){
87653
 
          returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
87654
 
        }else{
87655
 
          int mask = p->mask;          /* Mask of bits to set or clear. */
87656
 
          if( db->autoCommit==0 ){
87657
 
            /* Foreign key support may not be enabled or disabled while not
87658
 
            ** in auto-commit mode.  */
87659
 
            mask &= ~(SQLITE_ForeignKeys);
87660
 
          }
87661
 
 
87662
 
          if( getBoolean(zRight) ){
87663
 
            db->flags |= mask;
87664
 
          }else{
87665
 
            db->flags &= ~mask;
87666
 
          }
87667
 
 
87668
 
          /* Many of the flag-pragmas modify the code generated by the SQL 
87669
 
          ** compiler (eg. count_changes). So add an opcode to expire all
87670
 
          ** compiled SQL statements after modifying a pragma value.
87671
 
          */
87672
 
          sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
87673
 
        }
87674
 
      }
87675
 
 
87676
 
      return 1;
87677
 
    }
87678
 
  }
87679
 
  return 0;
87680
 
}
87681
 
#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
87682
 
 
87683
 
/*
87684
 
** Return a human-readable name for a constraint resolution action.
87685
 
*/
87686
 
#ifndef SQLITE_OMIT_FOREIGN_KEY
87687
 
static const char *actionName(u8 action){
87688
 
  const char *zName;
87689
 
  switch( action ){
87690
 
    case OE_SetNull:  zName = "SET NULL";        break;
87691
 
    case OE_SetDflt:  zName = "SET DEFAULT";     break;
87692
 
    case OE_Cascade:  zName = "CASCADE";         break;
87693
 
    case OE_Restrict: zName = "RESTRICT";        break;
87694
 
    default:          zName = "NO ACTION";  
87695
 
                      assert( action==OE_None ); break;
87696
 
  }
87697
 
  return zName;
87698
 
}
87699
 
#endif
87700
 
 
87701
 
 
87702
 
/*
87703
 
** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
87704
 
** defined in pager.h. This function returns the associated lowercase
87705
 
** journal-mode name.
87706
 
*/
87707
 
SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
87708
 
  static char * const azModeName[] = {
87709
 
    "delete", "persist", "off", "truncate", "memory"
87710
 
#ifndef SQLITE_OMIT_WAL
87711
 
     , "wal"
87712
 
#endif
87713
 
  };
87714
 
  assert( PAGER_JOURNALMODE_DELETE==0 );
87715
 
  assert( PAGER_JOURNALMODE_PERSIST==1 );
87716
 
  assert( PAGER_JOURNALMODE_OFF==2 );
87717
 
  assert( PAGER_JOURNALMODE_TRUNCATE==3 );
87718
 
  assert( PAGER_JOURNALMODE_MEMORY==4 );
87719
 
  assert( PAGER_JOURNALMODE_WAL==5 );
87720
 
  assert( eMode>=0 && eMode<=ArraySize(azModeName) );
87721
 
 
87722
 
  if( eMode==ArraySize(azModeName) ) return 0;
87723
 
  return azModeName[eMode];
87724
 
}
87725
 
 
87726
 
/*
87727
 
** Process a pragma statement.  
87728
 
**
87729
 
** Pragmas are of this form:
87730
 
**
87731
 
**      PRAGMA [database.]id [= value]
87732
 
**
87733
 
** The identifier might also be a string.  The value is a string, and
87734
 
** identifier, or a number.  If minusFlag is true, then the value is
87735
 
** a number that was preceded by a minus sign.
87736
 
**
87737
 
** If the left side is "database.id" then pId1 is the database name
87738
 
** and pId2 is the id.  If the left side is just "id" then pId1 is the
87739
 
** id and pId2 is any empty string.
87740
 
*/
87741
 
SQLITE_PRIVATE void sqlite3Pragma(
87742
 
  Parse *pParse, 
87743
 
  Token *pId1,        /* First part of [database.]id field */
87744
 
  Token *pId2,        /* Second part of [database.]id field, or NULL */
87745
 
  Token *pValue,      /* Token for <value>, or NULL */
87746
 
  int minusFlag       /* True if a '-' sign preceded <value> */
87747
 
){
87748
 
  char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
87749
 
  char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
87750
 
  const char *zDb = 0;   /* The database name */
87751
 
  Token *pId;            /* Pointer to <id> token */
87752
 
  int iDb;               /* Database index for <database> */
87753
 
  sqlite3 *db = pParse->db;
87754
 
  Db *pDb;
87755
 
  Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
87756
 
  if( v==0 ) return;
87757
 
  sqlite3VdbeRunOnlyOnce(v);
87758
 
  pParse->nMem = 2;
87759
 
 
87760
 
  /* Interpret the [database.] part of the pragma statement. iDb is the
87761
 
  ** index of the database this pragma is being applied to in db.aDb[]. */
87762
 
  iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
87763
 
  if( iDb<0 ) return;
87764
 
  pDb = &db->aDb[iDb];
87765
 
 
87766
 
  /* If the temp database has been explicitly named as part of the 
87767
 
  ** pragma, make sure it is open. 
87768
 
  */
87769
 
  if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
87770
 
    return;
87771
 
  }
87772
 
 
87773
 
  zLeft = sqlite3NameFromToken(db, pId);
87774
 
  if( !zLeft ) return;
87775
 
  if( minusFlag ){
87776
 
    zRight = sqlite3MPrintf(db, "-%T", pValue);
87777
 
  }else{
87778
 
    zRight = sqlite3NameFromToken(db, pValue);
87779
 
  }
87780
 
 
87781
 
  assert( pId2 );
87782
 
  zDb = pId2->n>0 ? pDb->zName : 0;
87783
 
  if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
87784
 
    goto pragma_out;
87785
 
  }
87786
 
 
87787
 
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
87788
 
  /*
87789
 
  **  PRAGMA [database.]default_cache_size
87790
 
  **  PRAGMA [database.]default_cache_size=N
87791
 
  **
87792
 
  ** The first form reports the current persistent setting for the
87793
 
  ** page cache size.  The value returned is the maximum number of
87794
 
  ** pages in the page cache.  The second form sets both the current
87795
 
  ** page cache size value and the persistent page cache size value
87796
 
  ** stored in the database file.
87797
 
  **
87798
 
  ** Older versions of SQLite would set the default cache size to a
87799
 
  ** negative number to indicate synchronous=OFF.  These days, synchronous
87800
 
  ** is always on by default regardless of the sign of the default cache
87801
 
  ** size.  But continue to take the absolute value of the default cache
87802
 
  ** size of historical compatibility.
87803
 
  */
87804
 
  if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
87805
 
    static const VdbeOpList getCacheSize[] = {
87806
 
      { OP_Transaction, 0, 0,        0},                         /* 0 */
87807
 
      { OP_ReadCookie,  0, 1,        BTREE_DEFAULT_CACHE_SIZE},  /* 1 */
87808
 
      { OP_IfPos,       1, 7,        0},
87809
 
      { OP_Integer,     0, 2,        0},
87810
 
      { OP_Subtract,    1, 2,        1},
87811
 
      { OP_IfPos,       1, 7,        0},
87812
 
      { OP_Integer,     0, 1,        0},                         /* 6 */
87813
 
      { OP_ResultRow,   1, 1,        0},
87814
 
    };
87815
 
    int addr;
87816
 
    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
87817
 
    sqlite3VdbeUsesBtree(v, iDb);
87818
 
    if( !zRight ){
87819
 
      sqlite3VdbeSetNumCols(v, 1);
87820
 
      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
87821
 
      pParse->nMem += 2;
87822
 
      addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
87823
 
      sqlite3VdbeChangeP1(v, addr, iDb);
87824
 
      sqlite3VdbeChangeP1(v, addr+1, iDb);
87825
 
      sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
87826
 
    }else{
87827
 
      int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
87828
 
      sqlite3BeginWriteOperation(pParse, 0, iDb);
87829
 
      sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
87830
 
      sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
87831
 
      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
87832
 
      pDb->pSchema->cache_size = size;
87833
 
      sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
87834
 
    }
87835
 
  }else
87836
 
 
87837
 
  /*
87838
 
  **  PRAGMA [database.]page_size
87839
 
  **  PRAGMA [database.]page_size=N
87840
 
  **
87841
 
  ** The first form reports the current setting for the
87842
 
  ** database page size in bytes.  The second form sets the
87843
 
  ** database page size value.  The value can only be set if
87844
 
  ** the database has not yet been created.
87845
 
  */
87846
 
  if( sqlite3StrICmp(zLeft,"page_size")==0 ){
87847
 
    Btree *pBt = pDb->pBt;
87848
 
    assert( pBt!=0 );
87849
 
    if( !zRight ){
87850
 
      int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
87851
 
      returnSingleInt(pParse, "page_size", size);
87852
 
    }else{
87853
 
      /* Malloc may fail when setting the page-size, as there is an internal
87854
 
      ** buffer that the pager module resizes using sqlite3_realloc().
87855
 
      */
87856
 
      db->nextPagesize = sqlite3Atoi(zRight);
87857
 
      if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
87858
 
        db->mallocFailed = 1;
87859
 
      }
87860
 
    }
87861
 
  }else
87862
 
 
87863
 
  /*
87864
 
  **  PRAGMA [database.]secure_delete
87865
 
  **  PRAGMA [database.]secure_delete=ON/OFF
87866
 
  **
87867
 
  ** The first form reports the current setting for the
87868
 
  ** secure_delete flag.  The second form changes the secure_delete
87869
 
  ** flag setting and reports thenew value.
87870
 
  */
87871
 
  if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){
87872
 
    Btree *pBt = pDb->pBt;
87873
 
    int b = -1;
87874
 
    assert( pBt!=0 );
87875
 
    if( zRight ){
87876
 
      b = getBoolean(zRight);
87877
 
    }
87878
 
    if( pId2->n==0 && b>=0 ){
87879
 
      int ii;
87880
 
      for(ii=0; ii<db->nDb; ii++){
87881
 
        sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
87882
 
      }
87883
 
    }
87884
 
    b = sqlite3BtreeSecureDelete(pBt, b);
87885
 
    returnSingleInt(pParse, "secure_delete", b);
87886
 
  }else
87887
 
 
87888
 
  /*
87889
 
  **  PRAGMA [database.]max_page_count
87890
 
  **  PRAGMA [database.]max_page_count=N
87891
 
  **
87892
 
  ** The first form reports the current setting for the
87893
 
  ** maximum number of pages in the database file.  The 
87894
 
  ** second form attempts to change this setting.  Both
87895
 
  ** forms return the current setting.
87896
 
  **
87897
 
  **  PRAGMA [database.]page_count
87898
 
  **
87899
 
  ** Return the number of pages in the specified database.
87900
 
  */
87901
 
  if( sqlite3StrICmp(zLeft,"page_count")==0
87902
 
   || sqlite3StrICmp(zLeft,"max_page_count")==0
87903
 
  ){
87904
 
    int iReg;
87905
 
    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
87906
 
    sqlite3CodeVerifySchema(pParse, iDb);
87907
 
    iReg = ++pParse->nMem;
87908
 
    if( zLeft[0]=='p' ){
87909
 
      sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
87910
 
    }else{
87911
 
      sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, sqlite3Atoi(zRight));
87912
 
    }
87913
 
    sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
87914
 
    sqlite3VdbeSetNumCols(v, 1);
87915
 
    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
87916
 
  }else
87917
 
 
87918
 
  /*
87919
 
  **  PRAGMA [database.]locking_mode
87920
 
  **  PRAGMA [database.]locking_mode = (normal|exclusive)
87921
 
  */
87922
 
  if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
87923
 
    const char *zRet = "normal";
87924
 
    int eMode = getLockingMode(zRight);
87925
 
 
87926
 
    if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
87927
 
      /* Simple "PRAGMA locking_mode;" statement. This is a query for
87928
 
      ** the current default locking mode (which may be different to
87929
 
      ** the locking-mode of the main database).
87930
 
      */
87931
 
      eMode = db->dfltLockMode;
87932
 
    }else{
87933
 
      Pager *pPager;
87934
 
      if( pId2->n==0 ){
87935
 
        /* This indicates that no database name was specified as part
87936
 
        ** of the PRAGMA command. In this case the locking-mode must be
87937
 
        ** set on all attached databases, as well as the main db file.
87938
 
        **
87939
 
        ** Also, the sqlite3.dfltLockMode variable is set so that
87940
 
        ** any subsequently attached databases also use the specified
87941
 
        ** locking mode.
87942
 
        */
87943
 
        int ii;
87944
 
        assert(pDb==&db->aDb[0]);
87945
 
        for(ii=2; ii<db->nDb; ii++){
87946
 
          pPager = sqlite3BtreePager(db->aDb[ii].pBt);
87947
 
          sqlite3PagerLockingMode(pPager, eMode);
87948
 
        }
87949
 
        db->dfltLockMode = (u8)eMode;
87950
 
      }
87951
 
      pPager = sqlite3BtreePager(pDb->pBt);
87952
 
      eMode = sqlite3PagerLockingMode(pPager, eMode);
87953
 
    }
87954
 
 
87955
 
    assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
87956
 
    if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
87957
 
      zRet = "exclusive";
87958
 
    }
87959
 
    sqlite3VdbeSetNumCols(v, 1);
87960
 
    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
87961
 
    sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
87962
 
    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
87963
 
  }else
87964
 
 
87965
 
  /*
87966
 
  **  PRAGMA [database.]journal_mode
87967
 
  **  PRAGMA [database.]journal_mode =
87968
 
  **                      (delete|persist|off|truncate|memory|wal|off)
87969
 
  */
87970
 
  if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
87971
 
    int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
87972
 
    int ii;           /* Loop counter */
87973
 
 
87974
 
    /* Force the schema to be loaded on all databases.  This cases all
87975
 
    ** database files to be opened and the journal_modes set. */
87976
 
    if( sqlite3ReadSchema(pParse) ){
87977
 
      goto pragma_out;
87978
 
    }
87979
 
 
87980
 
    sqlite3VdbeSetNumCols(v, 1);
87981
 
    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
87982
 
 
87983
 
    if( zRight==0 ){
87984
 
      /* If there is no "=MODE" part of the pragma, do a query for the
87985
 
      ** current mode */
87986
 
      eMode = PAGER_JOURNALMODE_QUERY;
87987
 
    }else{
87988
 
      const char *zMode;
87989
 
      int n = sqlite3Strlen30(zRight);
87990
 
      for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
87991
 
        if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
87992
 
      }
87993
 
      if( !zMode ){
87994
 
        /* If the "=MODE" part does not match any known journal mode,
87995
 
        ** then do a query */
87996
 
        eMode = PAGER_JOURNALMODE_QUERY;
87997
 
      }
87998
 
    }
87999
 
    if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
88000
 
      /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
88001
 
      iDb = 0;
88002
 
      pId2->n = 1;
88003
 
    }
88004
 
    for(ii=db->nDb-1; ii>=0; ii--){
88005
 
      if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
88006
 
        sqlite3VdbeUsesBtree(v, ii);
88007
 
        sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
88008
 
      }
88009
 
    }
88010
 
    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
88011
 
  }else
88012
 
 
88013
 
  /*
88014
 
  **  PRAGMA [database.]journal_size_limit
88015
 
  **  PRAGMA [database.]journal_size_limit=N
88016
 
  **
88017
 
  ** Get or set the size limit on rollback journal files.
88018
 
  */
88019
 
  if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
88020
 
    Pager *pPager = sqlite3BtreePager(pDb->pBt);
88021
 
    i64 iLimit = -2;
88022
 
    if( zRight ){
88023
 
      sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
88024
 
      if( iLimit<-1 ) iLimit = -1;
88025
 
    }
88026
 
    iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
88027
 
    returnSingleInt(pParse, "journal_size_limit", iLimit);
88028
 
  }else
88029
 
 
88030
 
#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
88031
 
 
88032
 
  /*
88033
 
  **  PRAGMA [database.]auto_vacuum
88034
 
  **  PRAGMA [database.]auto_vacuum=N
88035
 
  **
88036
 
  ** Get or set the value of the database 'auto-vacuum' parameter.
88037
 
  ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
88038
 
  */
88039
 
#ifndef SQLITE_OMIT_AUTOVACUUM
88040
 
  if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
88041
 
    Btree *pBt = pDb->pBt;
88042
 
    assert( pBt!=0 );
88043
 
    if( sqlite3ReadSchema(pParse) ){
88044
 
      goto pragma_out;
88045
 
    }
88046
 
    if( !zRight ){
88047
 
      int auto_vacuum;
88048
 
      if( ALWAYS(pBt) ){
88049
 
         auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
88050
 
      }else{
88051
 
         auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
88052
 
      }
88053
 
      returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
88054
 
    }else{
88055
 
      int eAuto = getAutoVacuum(zRight);
88056
 
      assert( eAuto>=0 && eAuto<=2 );
88057
 
      db->nextAutovac = (u8)eAuto;
88058
 
      if( ALWAYS(eAuto>=0) ){
88059
 
        /* Call SetAutoVacuum() to set initialize the internal auto and
88060
 
        ** incr-vacuum flags. This is required in case this connection
88061
 
        ** creates the database file. It is important that it is created
88062
 
        ** as an auto-vacuum capable db.
88063
 
        */
88064
 
        int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
88065
 
        if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
88066
 
          /* When setting the auto_vacuum mode to either "full" or 
88067
 
          ** "incremental", write the value of meta[6] in the database
88068
 
          ** file. Before writing to meta[6], check that meta[3] indicates
88069
 
          ** that this really is an auto-vacuum capable database.
88070
 
          */
88071
 
          static const VdbeOpList setMeta6[] = {
88072
 
            { OP_Transaction,    0,         1,                 0},    /* 0 */
88073
 
            { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
88074
 
            { OP_If,             1,         0,                 0},    /* 2 */
88075
 
            { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
88076
 
            { OP_Integer,        0,         1,                 0},    /* 4 */
88077
 
            { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 1},    /* 5 */
88078
 
          };
88079
 
          int iAddr;
88080
 
          iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
88081
 
          sqlite3VdbeChangeP1(v, iAddr, iDb);
88082
 
          sqlite3VdbeChangeP1(v, iAddr+1, iDb);
88083
 
          sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
88084
 
          sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
88085
 
          sqlite3VdbeChangeP1(v, iAddr+5, iDb);
88086
 
          sqlite3VdbeUsesBtree(v, iDb);
88087
 
        }
88088
 
      }
88089
 
    }
88090
 
  }else
88091
 
#endif
88092
 
 
88093
 
  /*
88094
 
  **  PRAGMA [database.]incremental_vacuum(N)
88095
 
  **
88096
 
  ** Do N steps of incremental vacuuming on a database.
88097
 
  */
88098
 
#ifndef SQLITE_OMIT_AUTOVACUUM
88099
 
  if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
88100
 
    int iLimit, addr;
88101
 
    if( sqlite3ReadSchema(pParse) ){
88102
 
      goto pragma_out;
88103
 
    }
88104
 
    if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
88105
 
      iLimit = 0x7fffffff;
88106
 
    }
88107
 
    sqlite3BeginWriteOperation(pParse, 0, iDb);
88108
 
    sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
88109
 
    addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
88110
 
    sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
88111
 
    sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
88112
 
    sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
88113
 
    sqlite3VdbeJumpHere(v, addr);
88114
 
  }else
88115
 
#endif
88116
 
 
88117
 
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
88118
 
  /*
88119
 
  **  PRAGMA [database.]cache_size
88120
 
  **  PRAGMA [database.]cache_size=N
88121
 
  **
88122
 
  ** The first form reports the current local setting for the
88123
 
  ** page cache size.  The local setting can be different from
88124
 
  ** the persistent cache size value that is stored in the database
88125
 
  ** file itself.  The value returned is the maximum number of
88126
 
  ** pages in the page cache.  The second form sets the local
88127
 
  ** page cache size value.  It does not change the persistent
88128
 
  ** cache size stored on the disk so the cache size will revert
88129
 
  ** to its default value when the database is closed and reopened.
88130
 
  ** N should be a positive integer.
88131
 
  */
88132
 
  if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
88133
 
    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88134
 
    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
88135
 
    if( !zRight ){
88136
 
      returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
88137
 
    }else{
88138
 
      int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
88139
 
      pDb->pSchema->cache_size = size;
88140
 
      sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
88141
 
    }
88142
 
  }else
88143
 
 
88144
 
  /*
88145
 
  **   PRAGMA temp_store
88146
 
  **   PRAGMA temp_store = "default"|"memory"|"file"
88147
 
  **
88148
 
  ** Return or set the local value of the temp_store flag.  Changing
88149
 
  ** the local value does not make changes to the disk file and the default
88150
 
  ** value will be restored the next time the database is opened.
88151
 
  **
88152
 
  ** Note that it is possible for the library compile-time options to
88153
 
  ** override this setting
88154
 
  */
88155
 
  if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
88156
 
    if( !zRight ){
88157
 
      returnSingleInt(pParse, "temp_store", db->temp_store);
88158
 
    }else{
88159
 
      changeTempStorage(pParse, zRight);
88160
 
    }
88161
 
  }else
88162
 
 
88163
 
  /*
88164
 
  **   PRAGMA temp_store_directory
88165
 
  **   PRAGMA temp_store_directory = ""|"directory_name"
88166
 
  **
88167
 
  ** Return or set the local value of the temp_store_directory flag.  Changing
88168
 
  ** the value sets a specific directory to be used for temporary files.
88169
 
  ** Setting to a null string reverts to the default temporary directory search.
88170
 
  ** If temporary directory is changed, then invalidateTempStorage.
88171
 
  **
88172
 
  */
88173
 
  if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
88174
 
    if( !zRight ){
88175
 
      if( sqlite3_temp_directory ){
88176
 
        sqlite3VdbeSetNumCols(v, 1);
88177
 
        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
88178
 
            "temp_store_directory", SQLITE_STATIC);
88179
 
        sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
88180
 
        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
88181
 
      }
88182
 
    }else{
88183
 
#ifndef SQLITE_OMIT_WSD
88184
 
      if( zRight[0] ){
88185
 
        int rc;
88186
 
        int res;
88187
 
        rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
88188
 
        if( rc!=SQLITE_OK || res==0 ){
88189
 
          sqlite3ErrorMsg(pParse, "not a writable directory");
88190
 
          goto pragma_out;
88191
 
        }
88192
 
      }
88193
 
                {
88194
 
                        int cond = SQLITE_TEMP_STORE || (SQLITE_TEMP_STORE==1 && db->temp_store<=1) || (SQLITE_TEMP_STORE==2 && db->temp_store==1);
88195
 
                        if(cond)
88196
 
                        {
88197
 
                          invalidateTempStorage(pParse);
88198
 
                        }
88199
 
                }
88200
 
      sqlite3_free(sqlite3_temp_directory);
88201
 
      if( zRight[0] ){
88202
 
        sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
88203
 
      }else{
88204
 
        sqlite3_temp_directory = 0;
88205
 
      }
88206
 
#endif /* SQLITE_OMIT_WSD */
88207
 
    }
88208
 
  }else
88209
 
 
88210
 
#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
88211
 
#  if defined(__APPLE__)
88212
 
#    define SQLITE_ENABLE_LOCKING_STYLE 1
88213
 
#  else
88214
 
#    define SQLITE_ENABLE_LOCKING_STYLE 0
88215
 
#  endif
88216
 
#endif
88217
 
#if SQLITE_ENABLE_LOCKING_STYLE
88218
 
  /*
88219
 
   **   PRAGMA [database.]lock_proxy_file
88220
 
   **   PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
88221
 
   **
88222
 
   ** Return or set the value of the lock_proxy_file flag.  Changing
88223
 
   ** the value sets a specific file to be used for database access locks.
88224
 
   **
88225
 
   */
88226
 
  if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
88227
 
    if( !zRight ){
88228
 
      Pager *pPager = sqlite3BtreePager(pDb->pBt);
88229
 
      char *proxy_file_path = NULL;
88230
 
      sqlite3_file *pFile = sqlite3PagerFile(pPager);
88231
 
      sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE, 
88232
 
                           &proxy_file_path);
88233
 
      
88234
 
      if( proxy_file_path ){
88235
 
        sqlite3VdbeSetNumCols(v, 1);
88236
 
        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
88237
 
                              "lock_proxy_file", SQLITE_STATIC);
88238
 
        sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
88239
 
        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
88240
 
      }
88241
 
    }else{
88242
 
      Pager *pPager = sqlite3BtreePager(pDb->pBt);
88243
 
      sqlite3_file *pFile = sqlite3PagerFile(pPager);
88244
 
      int res;
88245
 
      if( zRight[0] ){
88246
 
        res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 
88247
 
                                     zRight);
88248
 
      } else {
88249
 
        res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 
88250
 
                                     NULL);
88251
 
      }
88252
 
      if( res!=SQLITE_OK ){
88253
 
        sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
88254
 
        goto pragma_out;
88255
 
      }
88256
 
    }
88257
 
  }else
88258
 
#endif /* SQLITE_ENABLE_LOCKING_STYLE */      
88259
 
    
88260
 
  /*
88261
 
  **   PRAGMA [database.]synchronous
88262
 
  **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
88263
 
  **
88264
 
  ** Return or set the local value of the synchronous flag.  Changing
88265
 
  ** the local value does not make changes to the disk file and the
88266
 
  ** default value will be restored the next time the database is
88267
 
  ** opened.
88268
 
  */
88269
 
  if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
88270
 
    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88271
 
    if( !zRight ){
88272
 
      returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
88273
 
    }else{
88274
 
      if( !db->autoCommit ){
88275
 
        sqlite3ErrorMsg(pParse, 
88276
 
            "Safety level may not be changed inside a transaction");
88277
 
      }else{
88278
 
        pDb->safety_level = getSafetyLevel(zRight)+1;
88279
 
      }
88280
 
    }
88281
 
  }else
88282
 
#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
88283
 
 
88284
 
#ifndef SQLITE_OMIT_FLAG_PRAGMAS
88285
 
  if( flagPragma(pParse, zLeft, zRight) ){
88286
 
    /* The flagPragma() subroutine also generates any necessary code
88287
 
    ** there is nothing more to do here */
88288
 
  }else
88289
 
#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
88290
 
 
88291
 
#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
88292
 
  /*
88293
 
  **   PRAGMA table_info(<table>)
88294
 
  **
88295
 
  ** Return a single row for each column of the named table. The columns of
88296
 
  ** the returned data set are:
88297
 
  **
88298
 
  ** cid:        Column id (numbered from left to right, starting at 0)
88299
 
  ** name:       Column name
88300
 
  ** type:       Column declaration type.
88301
 
  ** notnull:    True if 'NOT NULL' is part of column declaration
88302
 
  ** dflt_value: The default value for the column, if any.
88303
 
  */
88304
 
  if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
88305
 
    Table *pTab;
88306
 
    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88307
 
    pTab = sqlite3FindTable(db, zRight, zDb);
88308
 
    if( pTab ){
88309
 
      int i;
88310
 
      int nHidden = 0;
88311
 
      Column *pCol;
88312
 
      sqlite3VdbeSetNumCols(v, 6);
88313
 
      pParse->nMem = 6;
88314
 
      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
88315
 
      sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
88316
 
      sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
88317
 
      sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
88318
 
      sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
88319
 
      sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
88320
 
      sqlite3ViewGetColumnNames(pParse, pTab);
88321
 
      for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
88322
 
        if( IsHiddenColumn(pCol) ){
88323
 
          nHidden++;
88324
 
          continue;
88325
 
        }
88326
 
        sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
88327
 
        sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
88328
 
        sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
88329
 
           pCol->zType ? pCol->zType : "", 0);
88330
 
        sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
88331
 
        if( pCol->zDflt ){
88332
 
          sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
88333
 
        }else{
88334
 
          sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
88335
 
        }
88336
 
        sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
88337
 
        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
88338
 
      }
88339
 
    }
88340
 
  }else
88341
 
 
88342
 
  if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
88343
 
    Index *pIdx;
88344
 
    Table *pTab;
88345
 
    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88346
 
    pIdx = sqlite3FindIndex(db, zRight, zDb);
88347
 
    if( pIdx ){
88348
 
      int i;
88349
 
      pTab = pIdx->pTable;
88350
 
      sqlite3VdbeSetNumCols(v, 3);
88351
 
      pParse->nMem = 3;
88352
 
      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
88353
 
      sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
88354
 
      sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
88355
 
      for(i=0; i<pIdx->nColumn; i++){
88356
 
        int cnum = pIdx->aiColumn[i];
88357
 
        sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
88358
 
        sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
88359
 
        assert( pTab->nCol>cnum );
88360
 
        sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
88361
 
        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
88362
 
      }
88363
 
    }
88364
 
  }else
88365
 
 
88366
 
  if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
88367
 
    Index *pIdx;
88368
 
    Table *pTab;
88369
 
    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88370
 
    pTab = sqlite3FindTable(db, zRight, zDb);
88371
 
    if( pTab ){
88372
 
      v = sqlite3GetVdbe(pParse);
88373
 
      pIdx = pTab->pIndex;
88374
 
      if( pIdx ){
88375
 
        int i = 0; 
88376
 
        sqlite3VdbeSetNumCols(v, 3);
88377
 
        pParse->nMem = 3;
88378
 
        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
88379
 
        sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
88380
 
        sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
88381
 
        while(pIdx){
88382
 
          sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
88383
 
          sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
88384
 
          sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
88385
 
          sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
88386
 
          ++i;
88387
 
          pIdx = pIdx->pNext;
88388
 
        }
88389
 
      }
88390
 
    }
88391
 
  }else
88392
 
 
88393
 
  if( sqlite3StrICmp(zLeft, "database_list")==0 ){
88394
 
    int i;
88395
 
    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88396
 
    sqlite3VdbeSetNumCols(v, 3);
88397
 
    pParse->nMem = 3;
88398
 
    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
88399
 
    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
88400
 
    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
88401
 
    for(i=0; i<db->nDb; i++){
88402
 
      if( db->aDb[i].pBt==0 ) continue;
88403
 
      assert( db->aDb[i].zName!=0 );
88404
 
      sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
88405
 
      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
88406
 
      sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
88407
 
           sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
88408
 
      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
88409
 
    }
88410
 
  }else
88411
 
 
88412
 
  if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
88413
 
    int i = 0;
88414
 
    HashElem *p;
88415
 
    sqlite3VdbeSetNumCols(v, 2);
88416
 
    pParse->nMem = 2;
88417
 
    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
88418
 
    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
88419
 
    for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
88420
 
      CollSeq *pColl = (CollSeq *)sqliteHashData(p);
88421
 
      sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
88422
 
      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
88423
 
      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
88424
 
    }
88425
 
  }else
88426
 
#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
88427
 
 
88428
 
#ifndef SQLITE_OMIT_FOREIGN_KEY
88429
 
  if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
88430
 
    FKey *pFK;
88431
 
    Table *pTab;
88432
 
    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88433
 
    pTab = sqlite3FindTable(db, zRight, zDb);
88434
 
    if( pTab ){
88435
 
      v = sqlite3GetVdbe(pParse);
88436
 
      pFK = pTab->pFKey;
88437
 
      if( pFK ){
88438
 
        int i = 0; 
88439
 
        sqlite3VdbeSetNumCols(v, 8);
88440
 
        pParse->nMem = 8;
88441
 
        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
88442
 
        sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
88443
 
        sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
88444
 
        sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
88445
 
        sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
88446
 
        sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
88447
 
        sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
88448
 
        sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
88449
 
        while(pFK){
88450
 
          int j;
88451
 
          for(j=0; j<pFK->nCol; j++){
88452
 
            char *zCol = pFK->aCol[j].zCol;
88453
 
            char *zOnDelete = (char *)actionName(pFK->aAction[0]);
88454
 
            char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
88455
 
            sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
88456
 
            sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
88457
 
            sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
88458
 
            sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
88459
 
                              pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
88460
 
            sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
88461
 
            sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
88462
 
            sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
88463
 
            sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
88464
 
            sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
88465
 
          }
88466
 
          ++i;
88467
 
          pFK = pFK->pNextFrom;
88468
 
        }
88469
 
      }
88470
 
    }
88471
 
  }else
88472
 
#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
88473
 
 
88474
 
#ifndef NDEBUG
88475
 
  if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
88476
 
    if( zRight ){
88477
 
      if( getBoolean(zRight) ){
88478
 
        sqlite3ParserTrace(stderr, "parser: ");
88479
 
      }else{
88480
 
        sqlite3ParserTrace(0, 0);
88481
 
      }
88482
 
    }
88483
 
  }else
88484
 
#endif
88485
 
 
88486
 
  /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
88487
 
  ** used will be case sensitive or not depending on the RHS.
88488
 
  */
88489
 
  if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
88490
 
    if( zRight ){
88491
 
      sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
88492
 
    }
88493
 
  }else
88494
 
 
88495
 
#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
88496
 
# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
88497
 
#endif
88498
 
 
88499
 
#ifndef SQLITE_OMIT_INTEGRITY_CHECK
88500
 
  /* Pragma "quick_check" is an experimental reduced version of 
88501
 
  ** integrity_check designed to detect most database corruption
88502
 
  ** without most of the overhead of a full integrity-check.
88503
 
  */
88504
 
  if( sqlite3StrICmp(zLeft, "integrity_check")==0
88505
 
   || sqlite3StrICmp(zLeft, "quick_check")==0 
88506
 
  ){
88507
 
    int i, j, addr, mxErr;
88508
 
 
88509
 
    /* Code that appears at the end of the integrity check.  If no error
88510
 
    ** messages have been generated, output OK.  Otherwise output the
88511
 
    ** error message
88512
 
    */
88513
 
    static const VdbeOpList endCode[] = {
88514
 
      { OP_AddImm,      1, 0,        0},    /* 0 */
88515
 
      { OP_IfNeg,       1, 0,        0},    /* 1 */
88516
 
      { OP_String8,     0, 3,        0},    /* 2 */
88517
 
      { OP_ResultRow,   3, 1,        0},
88518
 
    };
88519
 
 
88520
 
    int isQuick = (zLeft[0]=='q');
88521
 
 
88522
 
    /* Initialize the VDBE program */
88523
 
    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88524
 
    pParse->nMem = 6;
88525
 
    sqlite3VdbeSetNumCols(v, 1);
88526
 
    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
88527
 
 
88528
 
    /* Set the maximum error count */
88529
 
    mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
88530
 
    if( zRight ){
88531
 
      sqlite3GetInt32(zRight, &mxErr);
88532
 
      if( mxErr<=0 ){
88533
 
        mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
88534
 
      }
88535
 
    }
88536
 
    sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
88537
 
 
88538
 
    /* Do an integrity check on each database file */
88539
 
    for(i=0; i<db->nDb; i++){
88540
 
      HashElem *x;
88541
 
      Hash *pTbls;
88542
 
      int cnt = 0;
88543
 
 
88544
 
#if (OMIT_TEMPDB)
88545
 
      if( i==1 ) continue;
88546
 
#endif
88547
 
 
88548
 
      sqlite3CodeVerifySchema(pParse, i);
88549
 
      addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
88550
 
      sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
88551
 
      sqlite3VdbeJumpHere(v, addr);
88552
 
 
88553
 
      /* Do an integrity check of the B-Tree
88554
 
      **
88555
 
      ** Begin by filling registers 2, 3, ... with the root pages numbers
88556
 
      ** for all tables and indices in the database.
88557
 
      */
88558
 
      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
88559
 
      pTbls = &db->aDb[i].pSchema->tblHash;
88560
 
      for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
88561
 
        Table *pTab = sqliteHashData(x);
88562
 
        Index *pIdx;
88563
 
        sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
88564
 
        cnt++;
88565
 
        for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
88566
 
          sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
88567
 
          cnt++;
88568
 
        }
88569
 
      }
88570
 
 
88571
 
      /* Make sure sufficient number of registers have been allocated */
88572
 
      if( pParse->nMem < cnt+4 ){
88573
 
        pParse->nMem = cnt+4;
88574
 
      }
88575
 
 
88576
 
      /* Do the b-tree integrity checks */
88577
 
      sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
88578
 
      sqlite3VdbeChangeP5(v, (u8)i);
88579
 
      addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
88580
 
      sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
88581
 
         sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
88582
 
         P4_DYNAMIC);
88583
 
      sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
88584
 
      sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
88585
 
      sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
88586
 
      sqlite3VdbeJumpHere(v, addr);
88587
 
 
88588
 
      /* Make sure all the indices are constructed correctly.
88589
 
      */
88590
 
      for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
88591
 
        Table *pTab = sqliteHashData(x);
88592
 
        Index *pIdx;
88593
 
        int loopTop;
88594
 
 
88595
 
        if( pTab->pIndex==0 ) continue;
88596
 
        addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
88597
 
        sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
88598
 
        sqlite3VdbeJumpHere(v, addr);
88599
 
        sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
88600
 
        sqlite3VdbeAddOp2(v, OP_Integer, 0, 2);  /* reg(2) will count entries */
88601
 
        loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
88602
 
        sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1);   /* increment entry count */
88603
 
        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
88604
 
          int jmp2;
88605
 
          int r1;
88606
 
          static const VdbeOpList idxErr[] = {
88607
 
            { OP_AddImm,      1, -1,  0},
88608
 
            { OP_String8,     0,  3,  0},    /* 1 */
88609
 
            { OP_Rowid,       1,  4,  0},
88610
 
            { OP_String8,     0,  5,  0},    /* 3 */
88611
 
            { OP_String8,     0,  6,  0},    /* 4 */
88612
 
            { OP_Concat,      4,  3,  3},
88613
 
            { OP_Concat,      5,  3,  3},
88614
 
            { OP_Concat,      6,  3,  3},
88615
 
            { OP_ResultRow,   3,  1,  0},
88616
 
            { OP_IfPos,       1,  0,  0},    /* 9 */
88617
 
            { OP_Halt,        0,  0,  0},
88618
 
          };
88619
 
          r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
88620
 
          jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
88621
 
          addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
88622
 
          sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
88623
 
          sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
88624
 
          sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
88625
 
          sqlite3VdbeJumpHere(v, addr+9);
88626
 
          sqlite3VdbeJumpHere(v, jmp2);
88627
 
        }
88628
 
        sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
88629
 
        sqlite3VdbeJumpHere(v, loopTop);
88630
 
        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
88631
 
          static const VdbeOpList cntIdx[] = {
88632
 
             { OP_Integer,      0,  3,  0},
88633
 
             { OP_Rewind,       0,  0,  0},  /* 1 */
88634
 
             { OP_AddImm,       3,  1,  0},
88635
 
             { OP_Next,         0,  0,  0},  /* 3 */
88636
 
             { OP_Eq,           2,  0,  3},  /* 4 */
88637
 
             { OP_AddImm,       1, -1,  0},
88638
 
             { OP_String8,      0,  2,  0},  /* 6 */
88639
 
             { OP_String8,      0,  3,  0},  /* 7 */
88640
 
             { OP_Concat,       3,  2,  2},
88641
 
             { OP_ResultRow,    2,  1,  0},
88642
 
          };
88643
 
          addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
88644
 
          sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
88645
 
          sqlite3VdbeJumpHere(v, addr);
88646
 
          addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
88647
 
          sqlite3VdbeChangeP1(v, addr+1, j+2);
88648
 
          sqlite3VdbeChangeP2(v, addr+1, addr+4);
88649
 
          sqlite3VdbeChangeP1(v, addr+3, j+2);
88650
 
          sqlite3VdbeChangeP2(v, addr+3, addr+2);
88651
 
          sqlite3VdbeJumpHere(v, addr+4);
88652
 
          sqlite3VdbeChangeP4(v, addr+6, 
88653
 
                     "wrong # of entries in index ", P4_STATIC);
88654
 
          sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_TRANSIENT);
88655
 
        }
88656
 
      } 
88657
 
    }
88658
 
    addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
88659
 
    sqlite3VdbeChangeP2(v, addr, -mxErr);
88660
 
    sqlite3VdbeJumpHere(v, addr+1);
88661
 
    sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
88662
 
  }else
88663
 
#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
88664
 
 
88665
 
#ifndef SQLITE_OMIT_UTF16
88666
 
  /*
88667
 
  **   PRAGMA encoding
88668
 
  **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
88669
 
  **
88670
 
  ** In its first form, this pragma returns the encoding of the main
88671
 
  ** database. If the database is not initialized, it is initialized now.
88672
 
  **
88673
 
  ** The second form of this pragma is a no-op if the main database file
88674
 
  ** has not already been initialized. In this case it sets the default
88675
 
  ** encoding that will be used for the main database file if a new file
88676
 
  ** is created. If an existing main database file is opened, then the
88677
 
  ** default text encoding for the existing database is used.
88678
 
  ** 
88679
 
  ** In all cases new databases created using the ATTACH command are
88680
 
  ** created to use the same default text encoding as the main database. If
88681
 
  ** the main database has not been initialized and/or created when ATTACH
88682
 
  ** is executed, this is done before the ATTACH operation.
88683
 
  **
88684
 
  ** In the second form this pragma sets the text encoding to be used in
88685
 
  ** new database files created using this database handle. It is only
88686
 
  ** useful if invoked immediately after the main database i
88687
 
  */
88688
 
  if( sqlite3StrICmp(zLeft, "encoding")==0 ){
88689
 
    static const struct EncName {
88690
 
      char *zName;
88691
 
      u8 enc;
88692
 
    } encnames[] = {
88693
 
      { "UTF8",     SQLITE_UTF8        },
88694
 
      { "UTF-8",    SQLITE_UTF8        },  /* Must be element [1] */
88695
 
      { "UTF-16le", SQLITE_UTF16LE     },  /* Must be element [2] */
88696
 
      { "UTF-16be", SQLITE_UTF16BE     },  /* Must be element [3] */
88697
 
      { "UTF16le",  SQLITE_UTF16LE     },
88698
 
      { "UTF16be",  SQLITE_UTF16BE     },
88699
 
      { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
88700
 
      { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
88701
 
      { 0, 0 }
88702
 
    };
88703
 
    const struct EncName *pEnc;
88704
 
    if( !zRight ){    /* "PRAGMA encoding" */
88705
 
      if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88706
 
      sqlite3VdbeSetNumCols(v, 1);
88707
 
      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
88708
 
      sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
88709
 
      assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
88710
 
      assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
88711
 
      assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
88712
 
      sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
88713
 
      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
88714
 
    }else{                        /* "PRAGMA encoding = XXX" */
88715
 
      /* Only change the value of sqlite.enc if the database handle is not
88716
 
      ** initialized. If the main database exists, the new sqlite.enc value
88717
 
      ** will be overwritten when the schema is next loaded. If it does not
88718
 
      ** already exists, it will be created to use the new encoding value.
88719
 
      */
88720
 
      if( 
88721
 
        !(DbHasProperty(db, 0, DB_SchemaLoaded)) || 
88722
 
        DbHasProperty(db, 0, DB_Empty) 
88723
 
      ){
88724
 
        for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
88725
 
          if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
88726
 
            ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
88727
 
            break;
88728
 
          }
88729
 
        }
88730
 
        if( !pEnc->zName ){
88731
 
          sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
88732
 
        }
88733
 
      }
88734
 
    }
88735
 
  }else
88736
 
#endif /* SQLITE_OMIT_UTF16 */
88737
 
 
88738
 
#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
88739
 
  /*
88740
 
  **   PRAGMA [database.]schema_version
88741
 
  **   PRAGMA [database.]schema_version = <integer>
88742
 
  **
88743
 
  **   PRAGMA [database.]user_version
88744
 
  **   PRAGMA [database.]user_version = <integer>
88745
 
  **
88746
 
  ** The pragma's schema_version and user_version are used to set or get
88747
 
  ** the value of the schema-version and user-version, respectively. Both
88748
 
  ** the schema-version and the user-version are 32-bit signed integers
88749
 
  ** stored in the database header.
88750
 
  **
88751
 
  ** The schema-cookie is usually only manipulated internally by SQLite. It
88752
 
  ** is incremented by SQLite whenever the database schema is modified (by
88753
 
  ** creating or dropping a table or index). The schema version is used by
88754
 
  ** SQLite each time a query is executed to ensure that the internal cache
88755
 
  ** of the schema used when compiling the SQL query matches the schema of
88756
 
  ** the database against which the compiled query is actually executed.
88757
 
  ** Subverting this mechanism by using "PRAGMA schema_version" to modify
88758
 
  ** the schema-version is potentially dangerous and may lead to program
88759
 
  ** crashes or database corruption. Use with caution!
88760
 
  **
88761
 
  ** The user-version is not used internally by SQLite. It may be used by
88762
 
  ** applications for any purpose.
88763
 
  */
88764
 
  if( sqlite3StrICmp(zLeft, "schema_version")==0 
88765
 
   || sqlite3StrICmp(zLeft, "user_version")==0 
88766
 
   || sqlite3StrICmp(zLeft, "freelist_count")==0 
88767
 
  ){
88768
 
    int iCookie;   /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
88769
 
    sqlite3VdbeUsesBtree(v, iDb);
88770
 
    switch( zLeft[0] ){
88771
 
      case 'f': case 'F':
88772
 
        iCookie = BTREE_FREE_PAGE_COUNT;
88773
 
        break;
88774
 
      case 's': case 'S':
88775
 
        iCookie = BTREE_SCHEMA_VERSION;
88776
 
        break;
88777
 
      default:
88778
 
        iCookie = BTREE_USER_VERSION;
88779
 
        break;
88780
 
    }
88781
 
 
88782
 
    if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
88783
 
      /* Write the specified cookie value */
88784
 
      static const VdbeOpList setCookie[] = {
88785
 
        { OP_Transaction,    0,  1,  0},    /* 0 */
88786
 
        { OP_Integer,        0,  1,  0},    /* 1 */
88787
 
        { OP_SetCookie,      0,  0,  1},    /* 2 */
88788
 
      };
88789
 
      int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
88790
 
      sqlite3VdbeChangeP1(v, addr, iDb);
88791
 
      sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
88792
 
      sqlite3VdbeChangeP1(v, addr+2, iDb);
88793
 
      sqlite3VdbeChangeP2(v, addr+2, iCookie);
88794
 
    }else{
88795
 
      /* Read the specified cookie value */
88796
 
      static const VdbeOpList readCookie[] = {
88797
 
        { OP_Transaction,     0,  0,  0},    /* 0 */
88798
 
        { OP_ReadCookie,      0,  1,  0},    /* 1 */
88799
 
        { OP_ResultRow,       1,  1,  0}
88800
 
      };
88801
 
      int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
88802
 
      sqlite3VdbeChangeP1(v, addr, iDb);
88803
 
      sqlite3VdbeChangeP1(v, addr+1, iDb);
88804
 
      sqlite3VdbeChangeP3(v, addr+1, iCookie);
88805
 
      sqlite3VdbeSetNumCols(v, 1);
88806
 
      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
88807
 
    }
88808
 
  }else
88809
 
#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
88810
 
 
88811
 
#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
88812
 
  /*
88813
 
  **   PRAGMA compile_options
88814
 
  **
88815
 
  ** Return the names of all compile-time options used in this build,
88816
 
  ** one option per row.
88817
 
  */
88818
 
  if( sqlite3StrICmp(zLeft, "compile_options")==0 ){
88819
 
    int i = 0;
88820
 
    const char *zOpt;
88821
 
    sqlite3VdbeSetNumCols(v, 1);
88822
 
    pParse->nMem = 1;
88823
 
    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
88824
 
    while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
88825
 
      sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
88826
 
      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
88827
 
    }
88828
 
  }else
88829
 
#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
88830
 
 
88831
 
#ifndef SQLITE_OMIT_WAL
88832
 
  /*
88833
 
  **   PRAGMA [database.]wal_checkpoint = passive|full|restart
88834
 
  **
88835
 
  ** Checkpoint the database.
88836
 
  */
88837
 
  if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
88838
 
    int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
88839
 
    int eMode = SQLITE_CHECKPOINT_PASSIVE;
88840
 
    if( zRight ){
88841
 
      if( sqlite3StrICmp(zRight, "full")==0 ){
88842
 
        eMode = SQLITE_CHECKPOINT_FULL;
88843
 
      }else if( sqlite3StrICmp(zRight, "restart")==0 ){
88844
 
        eMode = SQLITE_CHECKPOINT_RESTART;
88845
 
      }
88846
 
    }
88847
 
    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
88848
 
    sqlite3VdbeSetNumCols(v, 3);
88849
 
    pParse->nMem = 3;
88850
 
    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
88851
 
    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
88852
 
    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
88853
 
 
88854
 
    sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
88855
 
    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
88856
 
  }else
88857
 
 
88858
 
  /*
88859
 
  **   PRAGMA wal_autocheckpoint
88860
 
  **   PRAGMA wal_autocheckpoint = N
88861
 
  **
88862
 
  ** Configure a database connection to automatically checkpoint a database
88863
 
  ** after accumulating N frames in the log. Or query for the current value
88864
 
  ** of N.
88865
 
  */
88866
 
  if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
88867
 
    if( zRight ){
88868
 
      sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
88869
 
    }
88870
 
    returnSingleInt(pParse, "wal_autocheckpoint", 
88871
 
       db->xWalCallback==sqlite3WalDefaultHook ? 
88872
 
           SQLITE_PTR_TO_INT(db->pWalArg) : 0);
88873
 
  }else
88874
 
#endif
88875
 
 
88876
 
#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
88877
 
  /*
88878
 
  ** Report the current state of file logs for all databases
88879
 
  */
88880
 
  if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
88881
 
    static const char *const azLockName[] = {
88882
 
      "unlocked", "shared", "reserved", "pending", "exclusive"
88883
 
    };
88884
 
    int i;
88885
 
    sqlite3VdbeSetNumCols(v, 2);
88886
 
    pParse->nMem = 2;
88887
 
    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
88888
 
    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
88889
 
    for(i=0; i<db->nDb; i++){
88890
 
      Btree *pBt;
88891
 
      Pager *pPager;
88892
 
      const char *zState = "unknown";
88893
 
      int j;
88894
 
      if( db->aDb[i].zName==0 ) continue;
88895
 
      sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
88896
 
      pBt = db->aDb[i].pBt;
88897
 
      if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
88898
 
        zState = "closed";
88899
 
      }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, 
88900
 
                                     SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
88901
 
         zState = azLockName[j];
88902
 
      }
88903
 
      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
88904
 
      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
88905
 
    }
88906
 
 
88907
 
  }else
88908
 
#endif
88909
 
 
88910
 
#ifdef SQLITE_HAS_CODEC
88911
 
  if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
88912
 
    sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
88913
 
  }else
88914
 
  if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
88915
 
    sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
88916
 
  }else
88917
 
  if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
88918
 
                 sqlite3StrICmp(zLeft, "hexrekey")==0) ){
88919
 
    int i, h1, h2;
88920
 
    char zKey[40];
88921
 
    for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
88922
 
      h1 += 9*(1&(h1>>6));
88923
 
      h2 += 9*(1&(h2>>6));
88924
 
      zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
88925
 
    }
88926
 
    if( (zLeft[3] & 0xf)==0xb ){
88927
 
      sqlite3_key(db, zKey, i/2);
88928
 
    }else{
88929
 
      sqlite3_rekey(db, zKey, i/2);
88930
 
    }
88931
 
  }else
88932
 
#endif
88933
 
#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
88934
 
  if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
88935
 
#ifdef SQLITE_HAS_CODEC
88936
 
    if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
88937
 
      sqlite3_activate_see(&zRight[4]);
88938
 
    }
88939
 
#endif
88940
 
#ifdef SQLITE_ENABLE_CEROD
88941
 
    if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
88942
 
      sqlite3_activate_cerod(&zRight[6]);
88943
 
    }
88944
 
#endif
88945
 
  }else
88946
 
#endif
88947
 
 
88948
 
 
88949
 
  {/* Empty ELSE clause */}
88950
 
 
88951
 
  /*
88952
 
  ** Reset the safety level, in case the fullfsync flag or synchronous
88953
 
  ** setting changed.
88954
 
  */
88955
 
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
88956
 
  if( db->autoCommit ){
88957
 
    sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
88958
 
               (db->flags&SQLITE_FullFSync)!=0,
88959
 
               (db->flags&SQLITE_CkptFullFSync)!=0);
88960
 
  }
88961
 
#endif
88962
 
pragma_out:
88963
 
  sqlite3DbFree(db, zLeft);
88964
 
  sqlite3DbFree(db, zRight);
88965
 
}
88966
 
 
88967
 
#endif /* SQLITE_OMIT_PRAGMA */
88968
 
 
88969
 
/************** End of pragma.c **********************************************/
88970
 
/************** Begin file prepare.c *****************************************/
88971
 
/*
88972
 
** 2005 May 25
88973
 
**
88974
 
** The author disclaims copyright to this source code.  In place of
88975
 
** a legal notice, here is a blessing:
88976
 
**
88977
 
**    May you do good and not evil.
88978
 
**    May you find forgiveness for yourself and forgive others.
88979
 
**    May you share freely, never taking more than you give.
88980
 
**
88981
 
*************************************************************************
88982
 
** This file contains the implementation of the sqlite3_prepare()
88983
 
** interface, and routines that contribute to loading the database schema
88984
 
** from disk.
88985
 
*/
88986
 
 
88987
 
/*
88988
 
** Fill the InitData structure with an error message that indicates
88989
 
** that the database is corrupt.
88990
 
*/
88991
 
static void corruptSchema(
88992
 
  InitData *pData,     /* Initialization context */
88993
 
  const char *zObj,    /* Object being parsed at the point of error */
88994
 
  const char *zExtra   /* Error information */
88995
 
){
88996
 
  sqlite3 *db = pData->db;
88997
 
  if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
88998
 
    if( zObj==0 ) zObj = "?";
88999
 
    sqlite3SetString(pData->pzErrMsg, db,
89000
 
      "malformed database schema (%s)", zObj);
89001
 
    if( zExtra ){
89002
 
      *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg, 
89003
 
                                 "%s - %s", *pData->pzErrMsg, zExtra);
89004
 
    }
89005
 
  }
89006
 
  pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
89007
 
}
89008
 
 
89009
 
/*
89010
 
** This is the callback routine for the code that initializes the
89011
 
** database.  See sqlite3Init() below for additional information.
89012
 
** This routine is also called from the OP_ParseSchema opcode of the VDBE.
89013
 
**
89014
 
** Each callback contains the following information:
89015
 
**
89016
 
**     argv[0] = name of thing being created
89017
 
**     argv[1] = root page number for table or index. 0 for trigger or view.
89018
 
**     argv[2] = SQL text for the CREATE statement.
89019
 
**
89020
 
*/
89021
 
SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
89022
 
  InitData *pData = (InitData*)pInit;
89023
 
  sqlite3 *db = pData->db;
89024
 
  int iDb = pData->iDb;
89025
 
 
89026
 
  assert( argc==3 );
89027
 
  UNUSED_PARAMETER2(NotUsed, argc);
89028
 
  assert( sqlite3_mutex_held(db->mutex) );
89029
 
  DbClearProperty(db, iDb, DB_Empty);
89030
 
  if( db->mallocFailed ){
89031
 
    corruptSchema(pData, argv[0], 0);
89032
 
    return 1;
89033
 
  }
89034
 
 
89035
 
  assert( iDb>=0 && iDb<db->nDb );
89036
 
  if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
89037
 
  if( argv[1]==0 ){
89038
 
    corruptSchema(pData, argv[0], 0);
89039
 
  }else if( argv[2] && argv[2][0] ){
89040
 
    /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
89041
 
    ** But because db->init.busy is set to 1, no VDBE code is generated
89042
 
    ** or executed.  All the parser does is build the internal data
89043
 
    ** structures that describe the table, index, or view.
89044
 
    */
89045
 
    int rc;
89046
 
    sqlite3_stmt *pStmt;
89047
 
    TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
89048
 
 
89049
 
    assert( db->init.busy );
89050
 
    db->init.iDb = iDb;
89051
 
    db->init.newTnum = sqlite3Atoi(argv[1]);
89052
 
    db->init.orphanTrigger = 0;
89053
 
    TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
89054
 
    rc = db->errCode;
89055
 
    assert( (rc&0xFF)==(rcp&0xFF) );
89056
 
    db->init.iDb = 0;
89057
 
    if( SQLITE_OK!=rc ){
89058
 
      if( db->init.orphanTrigger ){
89059
 
        assert( iDb==1 );
89060
 
      }else{
89061
 
        pData->rc = rc;
89062
 
        if( rc==SQLITE_NOMEM ){
89063
 
          db->mallocFailed = 1;
89064
 
        }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
89065
 
          corruptSchema(pData, argv[0], sqlite3_errmsg(db));
89066
 
        }
89067
 
      }
89068
 
    }
89069
 
    sqlite3_finalize(pStmt);
89070
 
  }else if( argv[0]==0 ){
89071
 
    corruptSchema(pData, 0, 0);
89072
 
  }else{
89073
 
    /* If the SQL column is blank it means this is an index that
89074
 
    ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
89075
 
    ** constraint for a CREATE TABLE.  The index should have already
89076
 
    ** been created when we processed the CREATE TABLE.  All we have
89077
 
    ** to do here is record the root page number for that index.
89078
 
    */
89079
 
    Index *pIndex;
89080
 
    pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
89081
 
    if( pIndex==0 ){
89082
 
      /* This can occur if there exists an index on a TEMP table which
89083
 
      ** has the same name as another index on a permanent index.  Since
89084
 
      ** the permanent table is hidden by the TEMP table, we can also
89085
 
      ** safely ignore the index on the permanent table.
89086
 
      */
89087
 
      /* Do Nothing */;
89088
 
    }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
89089
 
      corruptSchema(pData, argv[0], "invalid rootpage");
89090
 
    }
89091
 
  }
89092
 
  return 0;
89093
 
}
89094
 
 
89095
 
/*
89096
 
** Attempt to read the database schema and initialize internal
89097
 
** data structures for a single database file.  The index of the
89098
 
** database file is given by iDb.  iDb==0 is used for the main
89099
 
** database.  iDb==1 should never be used.  iDb>=2 is used for
89100
 
** auxiliary databases.  Return one of the SQLITE_ error codes to
89101
 
** indicate success or failure.
89102
 
*/
89103
 
static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
89104
 
  int rc;
89105
 
  int i;
89106
 
  int size;
89107
 
  Table *pTab;
89108
 
  Db *pDb;
89109
 
  char const *azArg[4];
89110
 
  int meta[5];
89111
 
  InitData initData;
89112
 
  char const *zMasterSchema;
89113
 
  char const *zMasterName;
89114
 
  int openedTransaction = 0;
89115
 
 
89116
 
  /*
89117
 
  ** The master database table has a structure like this
89118
 
  */
89119
 
  static const char master_schema[] = 
89120
 
     "CREATE TABLE sqlite_master(\n"
89121
 
     "  type text,\n"
89122
 
     "  name text,\n"
89123
 
     "  tbl_name text,\n"
89124
 
     "  rootpage integer,\n"
89125
 
     "  sql text\n"
89126
 
     ")"
89127
 
  ;
89128
 
#ifndef SQLITE_OMIT_TEMPDB
89129
 
  static const char temp_master_schema[] = 
89130
 
     "CREATE TEMP TABLE sqlite_temp_master(\n"
89131
 
     "  type text,\n"
89132
 
     "  name text,\n"
89133
 
     "  tbl_name text,\n"
89134
 
     "  rootpage integer,\n"
89135
 
     "  sql text\n"
89136
 
     ")"
89137
 
  ;
89138
 
#else
89139
 
  #define temp_master_schema 0
89140
 
#endif
89141
 
 
89142
 
  assert( iDb>=0 && iDb<db->nDb );
89143
 
  assert( db->aDb[iDb].pSchema );
89144
 
  assert( sqlite3_mutex_held(db->mutex) );
89145
 
  assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
89146
 
 
89147
 
  /* zMasterSchema and zInitScript are set to point at the master schema
89148
 
  ** and initialisation script appropriate for the database being
89149
 
  ** initialised. zMasterName is the name of the master table.
89150
 
  */
89151
 
  #if (!OMIT_TEMPDB)
89152
 
  if( iDb==1 ){
89153
 
    zMasterSchema = temp_master_schema;
89154
 
  }else{
89155
 
    zMasterSchema = master_schema;
89156
 
  }
89157
 
  #else
89158
 
        zMasterSchema = master_schema;
89159
 
  #endif
89160
 
  zMasterName = SCHEMA_TABLE(iDb);
89161
 
 
89162
 
  /* Construct the schema tables.  */
89163
 
  azArg[0] = zMasterName;
89164
 
  azArg[1] = "1";
89165
 
  azArg[2] = zMasterSchema;
89166
 
  azArg[3] = 0;
89167
 
  initData.db = db;
89168
 
  initData.iDb = iDb;
89169
 
  initData.rc = SQLITE_OK;
89170
 
  initData.pzErrMsg = pzErrMsg;
89171
 
  sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
89172
 
  if( initData.rc ){
89173
 
    rc = initData.rc;
89174
 
    goto error_out;
89175
 
  }
89176
 
  pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
89177
 
  if( ALWAYS(pTab) ){
89178
 
    pTab->tabFlags |= TF_Readonly;
89179
 
  }
89180
 
 
89181
 
  /* Create a cursor to hold the database open
89182
 
  */
89183
 
  pDb = &db->aDb[iDb];
89184
 
  if( pDb->pBt==0 ){
89185
 
         #if (!OMIT_TEMPDB)
89186
 
    if( ALWAYS(iDb==1) ){
89187
 
      DbSetProperty(db, 1, DB_SchemaLoaded);
89188
 
    }
89189
 
         #endif
89190
 
    return SQLITE_OK;
89191
 
  }
89192
 
 
89193
 
  /* If there is not already a read-only (or read-write) transaction opened
89194
 
  ** on the b-tree database, open one now. If a transaction is opened, it 
89195
 
  ** will be closed before this function returns.  */
89196
 
  sqlite3BtreeEnter(pDb->pBt);
89197
 
  if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
89198
 
    rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
89199
 
    if( rc!=SQLITE_OK ){
89200
 
      sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
89201
 
      goto initone_error_out;
89202
 
    }
89203
 
    openedTransaction = 1;
89204
 
  }
89205
 
 
89206
 
  /* Get the database meta information.
89207
 
  **
89208
 
  ** Meta values are as follows:
89209
 
  **    meta[0]   Schema cookie.  Changes with each schema change.
89210
 
  **    meta[1]   File format of schema layer.
89211
 
  **    meta[2]   Size of the page cache.
89212
 
  **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
89213
 
  **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
89214
 
  **    meta[5]   User version
89215
 
  **    meta[6]   Incremental vacuum mode
89216
 
  **    meta[7]   unused
89217
 
  **    meta[8]   unused
89218
 
  **    meta[9]   unused
89219
 
  **
89220
 
  ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
89221
 
  ** the possible values of meta[4].
89222
 
  */
89223
 
  for(i=0; i<ArraySize(meta); i++){
89224
 
    sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
89225
 
  }
89226
 
  pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
89227
 
 
89228
 
  /* If opening a non-empty database, check the text encoding. For the
89229
 
  ** main database, set sqlite3.enc to the encoding of the main database.
89230
 
  ** For an attached db, it is an error if the encoding is not the same
89231
 
  ** as sqlite3.enc.
89232
 
  */
89233
 
  if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */
89234
 
    if( iDb==0 ){
89235
 
      u8 encoding;
89236
 
      /* If opening the main database, set ENC(db). */
89237
 
      encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
89238
 
      if( encoding==0 ) encoding = SQLITE_UTF8;
89239
 
      ENC(db) = encoding;
89240
 
      db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
89241
 
    }else{
89242
 
      /* If opening an attached database, the encoding much match ENC(db) */
89243
 
      if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
89244
 
        sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
89245
 
            " text encoding as main database");
89246
 
        rc = SQLITE_ERROR;
89247
 
        goto initone_error_out;
89248
 
      }
89249
 
    }
89250
 
  }else{
89251
 
    DbSetProperty(db, iDb, DB_Empty);
89252
 
  }
89253
 
  pDb->pSchema->enc = ENC(db);
89254
 
 
89255
 
  if( pDb->pSchema->cache_size==0 ){
89256
 
    size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
89257
 
    if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
89258
 
    pDb->pSchema->cache_size = size;
89259
 
    sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
89260
 
  }
89261
 
 
89262
 
  /*
89263
 
  ** file_format==1    Version 3.0.0.
89264
 
  ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
89265
 
  ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
89266
 
  ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
89267
 
  */
89268
 
  pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
89269
 
  if( pDb->pSchema->file_format==0 ){
89270
 
    pDb->pSchema->file_format = 1;
89271
 
  }
89272
 
  if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
89273
 
    sqlite3SetString(pzErrMsg, db, "unsupported file format");
89274
 
    rc = SQLITE_ERROR;
89275
 
    goto initone_error_out;
89276
 
  }
89277
 
 
89278
 
  /* Ticket #2804:  When we open a database in the newer file format,
89279
 
  ** clear the legacy_file_format pragma flag so that a VACUUM will
89280
 
  ** not downgrade the database and thus invalidate any descending
89281
 
  ** indices that the user might have created.
89282
 
  */
89283
 
  if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
89284
 
    db->flags &= ~SQLITE_LegacyFileFmt;
89285
 
  }
89286
 
 
89287
 
  /* Read the schema information out of the schema tables
89288
 
  */
89289
 
  assert( db->init.busy );
89290
 
  {
89291
 
    char *zSql;
89292
 
    zSql = sqlite3MPrintf(db, 
89293
 
        "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
89294
 
        db->aDb[iDb].zName, zMasterName);
89295
 
#ifndef SQLITE_OMIT_AUTHORIZATION
89296
 
    {
89297
 
      int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
89298
 
      xAuth = db->xAuth;
89299
 
      db->xAuth = 0;
89300
 
#endif
89301
 
      rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
89302
 
#ifndef SQLITE_OMIT_AUTHORIZATION
89303
 
      db->xAuth = xAuth;
89304
 
    }
89305
 
#endif
89306
 
    if( rc==SQLITE_OK ) rc = initData.rc;
89307
 
    sqlite3DbFree(db, zSql);
89308
 
#ifndef SQLITE_OMIT_ANALYZE
89309
 
    if( rc==SQLITE_OK ){
89310
 
      sqlite3AnalysisLoad(db, iDb);
89311
 
    }
89312
 
#endif
89313
 
  }
89314
 
  if( db->mallocFailed ){
89315
 
    rc = SQLITE_NOMEM;
89316
 
    sqlite3ResetInternalSchema(db, -1);
89317
 
  }
89318
 
  if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
89319
 
    /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
89320
 
    ** the schema loaded, even if errors occurred. In this situation the 
89321
 
    ** current sqlite3_prepare() operation will fail, but the following one
89322
 
    ** will attempt to compile the supplied statement against whatever subset
89323
 
    ** of the schema was loaded before the error occurred. The primary
89324
 
    ** purpose of this is to allow access to the sqlite_master table
89325
 
    ** even when its contents have been corrupted.
89326
 
    */
89327
 
    DbSetProperty(db, iDb, DB_SchemaLoaded);
89328
 
    rc = SQLITE_OK;
89329
 
  }
89330
 
 
89331
 
  /* Jump here for an error that occurs after successfully allocating
89332
 
  ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
89333
 
  ** before that point, jump to error_out.
89334
 
  */
89335
 
initone_error_out:
89336
 
  if( openedTransaction ){
89337
 
    sqlite3BtreeCommit(pDb->pBt);
89338
 
  }
89339
 
  sqlite3BtreeLeave(pDb->pBt);
89340
 
 
89341
 
error_out:
89342
 
  if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
89343
 
    db->mallocFailed = 1;
89344
 
  }
89345
 
  return rc;
89346
 
}
89347
 
 
89348
 
/*
89349
 
** Initialize all database files - the main database file, the file
89350
 
** used to store temporary tables, and any additional database files
89351
 
** created using ATTACH statements.  Return a success code.  If an
89352
 
** error occurs, write an error message into *pzErrMsg.
89353
 
**
89354
 
** After a database is initialized, the DB_SchemaLoaded bit is set
89355
 
** bit is set in the flags field of the Db structure. If the database
89356
 
** file was of zero-length, then the DB_Empty flag is also set.
89357
 
*/
89358
 
SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
89359
 
  int i, rc;
89360
 
  int commit_internal = !(db->flags&SQLITE_InternChanges);
89361
 
  
89362
 
  assert( sqlite3_mutex_held(db->mutex) );
89363
 
  rc = SQLITE_OK;
89364
 
  db->init.busy = 1;
89365
 
  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
89366
 
    if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
89367
 
    rc = sqlite3InitOne(db, i, pzErrMsg);
89368
 
    if( rc ){
89369
 
      sqlite3ResetInternalSchema(db, i);
89370
 
    }
89371
 
  }
89372
 
 
89373
 
  /* Once all the other databases have been initialised, load the schema
89374
 
  ** for the TEMP database. This is loaded last, as the TEMP database
89375
 
  ** schema may contain references to objects in other databases.
89376
 
  */
89377
 
#ifndef SQLITE_OMIT_TEMPDB
89378
 
  if( rc==SQLITE_OK && ALWAYS(db->nDb>1)
89379
 
                    && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
89380
 
    rc = sqlite3InitOne(db, 1, pzErrMsg);
89381
 
    if( rc ){
89382
 
      sqlite3ResetInternalSchema(db, 1);
89383
 
    }
89384
 
  }
89385
 
#endif
89386
 
 
89387
 
  db->init.busy = 0;
89388
 
  if( rc==SQLITE_OK && commit_internal ){
89389
 
    sqlite3CommitInternalChanges(db);
89390
 
  }
89391
 
 
89392
 
  return rc; 
89393
 
}
89394
 
 
89395
 
/*
89396
 
** This routine is a no-op if the database schema is already initialised.
89397
 
** Otherwise, the schema is loaded. An error code is returned.
89398
 
*/
89399
 
SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
89400
 
  int rc = SQLITE_OK;
89401
 
  sqlite3 *db = pParse->db;
89402
 
  assert( sqlite3_mutex_held(db->mutex) );
89403
 
  if( !db->init.busy ){
89404
 
    rc = sqlite3Init(db, &pParse->zErrMsg);
89405
 
  }
89406
 
  if( rc!=SQLITE_OK ){
89407
 
    pParse->rc = rc;
89408
 
    pParse->nErr++;
89409
 
  }
89410
 
  return rc;
89411
 
}
89412
 
 
89413
 
 
89414
 
/*
89415
 
** Check schema cookies in all databases.  If any cookie is out
89416
 
** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
89417
 
** make no changes to pParse->rc.
89418
 
*/
89419
 
static void schemaIsValid(Parse *pParse){
89420
 
  sqlite3 *db = pParse->db;
89421
 
  int iDb;
89422
 
  int rc;
89423
 
  int cookie;
89424
 
 
89425
 
  assert( pParse->checkSchema );
89426
 
  assert( sqlite3_mutex_held(db->mutex) );
89427
 
  for(iDb=0; iDb<db->nDb; iDb++){
89428
 
    int openedTransaction = 0;         /* True if a transaction is opened */
89429
 
    Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */
89430
 
    if( pBt==0 ) continue;
89431
 
 
89432
 
    /* If there is not already a read-only (or read-write) transaction opened
89433
 
    ** on the b-tree database, open one now. If a transaction is opened, it 
89434
 
    ** will be closed immediately after reading the meta-value. */
89435
 
    if( !sqlite3BtreeIsInReadTrans(pBt) ){
89436
 
      rc = sqlite3BtreeBeginTrans(pBt, 0);
89437
 
      if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
89438
 
        db->mallocFailed = 1;
89439
 
      }
89440
 
      if( rc!=SQLITE_OK ) return;
89441
 
      openedTransaction = 1;
89442
 
    }
89443
 
 
89444
 
    /* Read the schema cookie from the database. If it does not match the 
89445
 
    ** value stored as part of the in-memory schema representation,
89446
 
    ** set Parse.rc to SQLITE_SCHEMA. */
89447
 
    sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
89448
 
    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
89449
 
    if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
89450
 
      sqlite3ResetInternalSchema(db, iDb);
89451
 
      pParse->rc = SQLITE_SCHEMA;
89452
 
    }
89453
 
 
89454
 
    /* Close the transaction, if one was opened. */
89455
 
    if( openedTransaction ){
89456
 
      sqlite3BtreeCommit(pBt);
89457
 
    }
89458
 
  }
89459
 
}
89460
 
 
89461
 
/*
89462
 
** Convert a schema pointer into the iDb index that indicates
89463
 
** which database file in db->aDb[] the schema refers to.
89464
 
**
89465
 
** If the same database is attached more than once, the first
89466
 
** attached database is returned.
89467
 
*/
89468
 
SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
89469
 
  int i = -1000000;
89470
 
 
89471
 
  /* If pSchema is NULL, then return -1000000. This happens when code in 
89472
 
  ** expr.c is trying to resolve a reference to a transient table (i.e. one
89473
 
  ** created by a sub-select). In this case the return value of this 
89474
 
  ** function should never be used.
89475
 
  **
89476
 
  ** We return -1000000 instead of the more usual -1 simply because using
89477
 
  ** -1000000 as the incorrect index into db->aDb[] is much 
89478
 
  ** more likely to cause a segfault than -1 (of course there are assert()
89479
 
  ** statements too, but it never hurts to play the odds).
89480
 
  */
89481
 
  assert( sqlite3_mutex_held(db->mutex) );
89482
 
  if( pSchema ){
89483
 
    for(i=0; ALWAYS(i<db->nDb); i++){
89484
 
      if( db->aDb[i].pSchema==pSchema ){
89485
 
        break;
89486
 
      }
89487
 
    }
89488
 
    assert( i>=0 && i<db->nDb );
89489
 
  }
89490
 
  return i;
89491
 
}
89492
 
 
89493
 
/*
89494
 
** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
89495
 
*/
89496
 
static int sqlite3Prepare(
89497
 
  sqlite3 *db,              /* Database handle. */
89498
 
  const char *zSql,         /* UTF-8 encoded SQL statement. */
89499
 
  int nBytes,               /* Length of zSql in bytes. */
89500
 
  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
89501
 
  Vdbe *pReprepare,         /* VM being reprepared */
89502
 
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
89503
 
  const char **pzTail       /* OUT: End of parsed string */
89504
 
){
89505
 
  Parse *pParse;            /* Parsing context */
89506
 
  char *zErrMsg = 0;        /* Error message */
89507
 
  int rc = SQLITE_OK;       /* Result code */
89508
 
  int i;                    /* Loop counter */
89509
 
 
89510
 
  /* Allocate the parsing context */
89511
 
  pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
89512
 
  if( pParse==0 ){
89513
 
    rc = SQLITE_NOMEM;
89514
 
    goto end_prepare;
89515
 
  }
89516
 
  pParse->pReprepare = pReprepare;
89517
 
  assert( ppStmt && *ppStmt==0 );
89518
 
  assert( !db->mallocFailed );
89519
 
  assert( sqlite3_mutex_held(db->mutex) );
89520
 
 
89521
 
  /* Check to verify that it is possible to get a read lock on all
89522
 
  ** database schemas.  The inability to get a read lock indicates that
89523
 
  ** some other database connection is holding a write-lock, which in
89524
 
  ** turn means that the other connection has made uncommitted changes
89525
 
  ** to the schema.
89526
 
  **
89527
 
  ** Were we to proceed and prepare the statement against the uncommitted
89528
 
  ** schema changes and if those schema changes are subsequently rolled
89529
 
  ** back and different changes are made in their place, then when this
89530
 
  ** prepared statement goes to run the schema cookie would fail to detect
89531
 
  ** the schema change.  Disaster would follow.
89532
 
  **
89533
 
  ** This thread is currently holding mutexes on all Btrees (because
89534
 
  ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
89535
 
  ** is not possible for another thread to start a new schema change
89536
 
  ** while this routine is running.  Hence, we do not need to hold 
89537
 
  ** locks on the schema, we just need to make sure nobody else is 
89538
 
  ** holding them.
89539
 
  **
89540
 
  ** Note that setting READ_UNCOMMITTED overrides most lock detection,
89541
 
  ** but it does *not* override schema lock detection, so this all still
89542
 
  ** works even if READ_UNCOMMITTED is set.
89543
 
  */
89544
 
  for(i=0; i<db->nDb; i++) {
89545
 
    Btree *pBt = db->aDb[i].pBt;
89546
 
    if( pBt ){
89547
 
      assert( sqlite3BtreeHoldsMutex(pBt) );
89548
 
      rc = sqlite3BtreeSchemaLocked(pBt);
89549
 
      if( rc ){
89550
 
        const char *zDb = db->aDb[i].zName;
89551
 
        sqlite3Error(db, rc, "database schema is locked: %s", zDb);
89552
 
        testcase( db->flags & SQLITE_ReadUncommitted );
89553
 
        goto end_prepare;
89554
 
      }
89555
 
    }
89556
 
  }
89557
 
 
89558
 
  sqlite3VtabUnlockList(db);
89559
 
 
89560
 
  pParse->db = db;
89561
 
  pParse->nQueryLoop = (double)1;
89562
 
  if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
89563
 
    char *zSqlCopy;
89564
 
    int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
89565
 
    testcase( nBytes==mxLen );
89566
 
    testcase( nBytes==mxLen+1 );
89567
 
    if( nBytes>mxLen ){
89568
 
      sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
89569
 
      rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
89570
 
      goto end_prepare;
89571
 
    }
89572
 
    zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
89573
 
    if( zSqlCopy ){
89574
 
      sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
89575
 
      sqlite3DbFree(db, zSqlCopy);
89576
 
      pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
89577
 
    }else{
89578
 
      pParse->zTail = &zSql[nBytes];
89579
 
    }
89580
 
  }else{
89581
 
    sqlite3RunParser(pParse, zSql, &zErrMsg);
89582
 
  }
89583
 
  assert( 1==(int)pParse->nQueryLoop );
89584
 
 
89585
 
  if( db->mallocFailed ){
89586
 
    pParse->rc = SQLITE_NOMEM;
89587
 
  }
89588
 
  if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
89589
 
  if( pParse->checkSchema ){
89590
 
    schemaIsValid(pParse);
89591
 
  }
89592
 
  if( db->mallocFailed ){
89593
 
    pParse->rc = SQLITE_NOMEM;
89594
 
  }
89595
 
  if( pzTail ){
89596
 
    *pzTail = pParse->zTail;
89597
 
  }
89598
 
  rc = pParse->rc;
89599
 
 
89600
 
#ifndef SQLITE_OMIT_EXPLAIN
89601
 
  if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
89602
 
    static const char * const azColName[] = {
89603
 
       "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
89604
 
       "selectid", "order", "from", "detail"
89605
 
    };
89606
 
    int iFirst, mx;
89607
 
    if( pParse->explain==2 ){
89608
 
      sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
89609
 
      iFirst = 8;
89610
 
      mx = 12;
89611
 
    }else{
89612
 
      sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
89613
 
      iFirst = 0;
89614
 
      mx = 8;
89615
 
    }
89616
 
    for(i=iFirst; i<mx; i++){
89617
 
      sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
89618
 
                            azColName[i], SQLITE_STATIC);
89619
 
    }
89620
 
  }
89621
 
#endif
89622
 
 
89623
 
  assert( db->init.busy==0 || saveSqlFlag==0 );
89624
 
  if( db->init.busy==0 ){
89625
 
    Vdbe *pVdbe = pParse->pVdbe;
89626
 
    sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
89627
 
  }
89628
 
  if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
89629
 
    sqlite3VdbeFinalize(pParse->pVdbe);
89630
 
    assert(!(*ppStmt));
89631
 
  }else{
89632
 
    *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
89633
 
  }
89634
 
 
89635
 
  if( zErrMsg ){
89636
 
    sqlite3Error(db, rc, "%s", zErrMsg);
89637
 
    sqlite3DbFree(db, zErrMsg);
89638
 
  }else{
89639
 
    sqlite3Error(db, rc, 0);
89640
 
  }
89641
 
 
89642
 
  /* Delete any TriggerPrg structures allocated while parsing this statement. */
89643
 
  while( pParse->pTriggerPrg ){
89644
 
    TriggerPrg *pT = pParse->pTriggerPrg;
89645
 
    pParse->pTriggerPrg = pT->pNext;
89646
 
    sqlite3DbFree(db, pT);
89647
 
  }
89648
 
 
89649
 
end_prepare:
89650
 
 
89651
 
  sqlite3StackFree(db, pParse);
89652
 
  rc = sqlite3ApiExit(db, rc);
89653
 
  assert( (rc&db->errMask)==rc );
89654
 
  return rc;
89655
 
}
89656
 
static int sqlite3LockAndPrepare(
89657
 
  sqlite3 *db,              /* Database handle. */
89658
 
  const char *zSql,         /* UTF-8 encoded SQL statement. */
89659
 
  int nBytes,               /* Length of zSql in bytes. */
89660
 
  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
89661
 
  Vdbe *pOld,               /* VM being reprepared */
89662
 
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
89663
 
  const char **pzTail       /* OUT: End of parsed string */
89664
 
){
89665
 
  int rc;
89666
 
  assert( ppStmt!=0 );
89667
 
  *ppStmt = 0;
89668
 
  if( !sqlite3SafetyCheckOk(db) ){
89669
 
    return SQLITE_MISUSE_BKPT;
89670
 
  }
89671
 
  sqlite3_mutex_enter(db->mutex);
89672
 
  sqlite3BtreeEnterAll(db);
89673
 
  rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
89674
 
  if( rc==SQLITE_SCHEMA ){
89675
 
    sqlite3_finalize(*ppStmt);
89676
 
    rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
89677
 
  }
89678
 
  sqlite3BtreeLeaveAll(db);
89679
 
  sqlite3_mutex_leave(db->mutex);
89680
 
  return rc;
89681
 
}
89682
 
 
89683
 
/*
89684
 
** Rerun the compilation of a statement after a schema change.
89685
 
**
89686
 
** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
89687
 
** if the statement cannot be recompiled because another connection has
89688
 
** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
89689
 
** occurs, return SQLITE_SCHEMA.
89690
 
*/
89691
 
SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
89692
 
  int rc;
89693
 
  sqlite3_stmt *pNew;
89694
 
  const char *zSql;
89695
 
  sqlite3 *db;
89696
 
 
89697
 
  assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
89698
 
  zSql = sqlite3_sql((sqlite3_stmt *)p);
89699
 
  assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */
89700
 
  db = sqlite3VdbeDb(p);
89701
 
  assert( sqlite3_mutex_held(db->mutex) );
89702
 
  rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
89703
 
  if( rc ){
89704
 
    if( rc==SQLITE_NOMEM ){
89705
 
      db->mallocFailed = 1;
89706
 
    }
89707
 
    assert( pNew==0 );
89708
 
    return rc;
89709
 
  }else{
89710
 
    assert( pNew!=0 );
89711
 
  }
89712
 
  sqlite3VdbeSwap((Vdbe*)pNew, p);
89713
 
  sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
89714
 
  sqlite3VdbeResetStepResult((Vdbe*)pNew);
89715
 
  sqlite3VdbeFinalize((Vdbe*)pNew);
89716
 
  return SQLITE_OK;
89717
 
}
89718
 
 
89719
 
 
89720
 
/*
89721
 
** Two versions of the official API.  Legacy and new use.  In the legacy
89722
 
** version, the original SQL text is not saved in the prepared statement
89723
 
** and so if a schema change occurs, SQLITE_SCHEMA is returned by
89724
 
** sqlite3_step().  In the new version, the original SQL text is retained
89725
 
** and the statement is automatically recompiled if an schema change
89726
 
** occurs.
89727
 
*/
89728
 
SQLITE_API int sqlite3_prepare(
89729
 
  sqlite3 *db,              /* Database handle. */
89730
 
  const char *zSql,         /* UTF-8 encoded SQL statement. */
89731
 
  int nBytes,               /* Length of zSql in bytes. */
89732
 
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
89733
 
  const char **pzTail       /* OUT: End of parsed string */
89734
 
){
89735
 
  int rc;
89736
 
  rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
89737
 
  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
89738
 
  return rc;
89739
 
}
89740
 
SQLITE_API int sqlite3_prepare_v2(
89741
 
  sqlite3 *db,              /* Database handle. */
89742
 
  const char *zSql,         /* UTF-8 encoded SQL statement. */
89743
 
  int nBytes,               /* Length of zSql in bytes. */
89744
 
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
89745
 
  const char **pzTail       /* OUT: End of parsed string */
89746
 
){
89747
 
  int rc;
89748
 
  rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail);
89749
 
  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
89750
 
  return rc;
89751
 
}
89752
 
 
89753
 
 
89754
 
#ifndef SQLITE_OMIT_UTF16
89755
 
/*
89756
 
** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
89757
 
*/
89758
 
static int sqlite3Prepare16(
89759
 
  sqlite3 *db,              /* Database handle. */ 
89760
 
  const void *zSql,         /* UTF-16 encoded SQL statement. */
89761
 
  int nBytes,               /* Length of zSql in bytes. */
89762
 
  int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
89763
 
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
89764
 
  const void **pzTail       /* OUT: End of parsed string */
89765
 
){
89766
 
  /* This function currently works by first transforming the UTF-16
89767
 
  ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
89768
 
  ** tricky bit is figuring out the pointer to return in *pzTail.
89769
 
  */
89770
 
  char *zSql8;
89771
 
  const char *zTail8 = 0;
89772
 
  int rc = SQLITE_OK;
89773
 
 
89774
 
  assert( ppStmt );
89775
 
  *ppStmt = 0;
89776
 
  if( !sqlite3SafetyCheckOk(db) ){
89777
 
    return SQLITE_MISUSE_BKPT;
89778
 
  }
89779
 
  sqlite3_mutex_enter(db->mutex);
89780
 
  zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
89781
 
  if( zSql8 ){
89782
 
    rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
89783
 
  }
89784
 
 
89785
 
  if( zTail8 && pzTail ){
89786
 
    /* If sqlite3_prepare returns a tail pointer, we calculate the
89787
 
    ** equivalent pointer into the UTF-16 string by counting the unicode
89788
 
    ** characters between zSql8 and zTail8, and then returning a pointer
89789
 
    ** the same number of characters into the UTF-16 string.
89790
 
    */
89791
 
    int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
89792
 
    *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
89793
 
  }
89794
 
  sqlite3DbFree(db, zSql8); 
89795
 
  rc = sqlite3ApiExit(db, rc);
89796
 
  sqlite3_mutex_leave(db->mutex);
89797
 
  return rc;
89798
 
}
89799
 
 
89800
 
/*
89801
 
** Two versions of the official API.  Legacy and new use.  In the legacy
89802
 
** version, the original SQL text is not saved in the prepared statement
89803
 
** and so if a schema change occurs, SQLITE_SCHEMA is returned by
89804
 
** sqlite3_step().  In the new version, the original SQL text is retained
89805
 
** and the statement is automatically recompiled if an schema change
89806
 
** occurs.
89807
 
*/
89808
 
SQLITE_API int sqlite3_prepare16(
89809
 
  sqlite3 *db,              /* Database handle. */ 
89810
 
  const void *zSql,         /* UTF-16 encoded SQL statement. */
89811
 
  int nBytes,               /* Length of zSql in bytes. */
89812
 
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
89813
 
  const void **pzTail       /* OUT: End of parsed string */
89814
 
){
89815
 
  int rc;
89816
 
  rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
89817
 
  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
89818
 
  return rc;
89819
 
}
89820
 
SQLITE_API int sqlite3_prepare16_v2(
89821
 
  sqlite3 *db,              /* Database handle. */ 
89822
 
  const void *zSql,         /* UTF-16 encoded SQL statement. */
89823
 
  int nBytes,               /* Length of zSql in bytes. */
89824
 
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
89825
 
  const void **pzTail       /* OUT: End of parsed string */
89826
 
){
89827
 
  int rc;
89828
 
  rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
89829
 
  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
89830
 
  return rc;
89831
 
}
89832
 
 
89833
 
#endif /* SQLITE_OMIT_UTF16 */
89834
 
 
89835
 
/************** End of prepare.c *********************************************/
89836
 
/************** Begin file select.c ******************************************/
89837
 
/*
89838
 
** 2001 September 15
89839
 
**
89840
 
** The author disclaims copyright to this source code.  In place of
89841
 
** a legal notice, here is a blessing:
89842
 
**
89843
 
**    May you do good and not evil.
89844
 
**    May you find forgiveness for yourself and forgive others.
89845
 
**    May you share freely, never taking more than you give.
89846
 
**
89847
 
*************************************************************************
89848
 
** This file contains C code routines that are called by the parser
89849
 
** to handle SELECT statements in SQLite.
89850
 
*/
89851
 
 
89852
 
 
89853
 
/*
89854
 
** Delete all the content of a Select structure but do not deallocate
89855
 
** the select structure itself.
89856
 
*/
89857
 
static void clearSelect(sqlite3 *db, Select *p){
89858
 
  sqlite3ExprListDelete(db, p->pEList);
89859
 
  sqlite3SrcListDelete(db, p->pSrc);
89860
 
  sqlite3ExprDelete(db, p->pWhere);
89861
 
  sqlite3ExprListDelete(db, p->pGroupBy);
89862
 
  sqlite3ExprDelete(db, p->pHaving);
89863
 
  sqlite3ExprListDelete(db, p->pOrderBy);
89864
 
  sqlite3SelectDelete(db, p->pPrior);
89865
 
  sqlite3ExprDelete(db, p->pLimit);
89866
 
  sqlite3ExprDelete(db, p->pOffset);
89867
 
}
89868
 
 
89869
 
/*
89870
 
** Initialize a SelectDest structure.
89871
 
*/
89872
 
SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
89873
 
  pDest->eDest = (u8)eDest;
89874
 
  pDest->iParm = iParm;
89875
 
  pDest->affinity = 0;
89876
 
  pDest->iMem = 0;
89877
 
  pDest->nMem = 0;
89878
 
}
89879
 
 
89880
 
 
89881
 
/*
89882
 
** Allocate a new Select structure and return a pointer to that
89883
 
** structure.
89884
 
*/
89885
 
SQLITE_PRIVATE Select *sqlite3SelectNew(
89886
 
  Parse *pParse,        /* Parsing context */
89887
 
  ExprList *pEList,     /* which columns to include in the result */
89888
 
  SrcList *pSrc,        /* the FROM clause -- which tables to scan */
89889
 
  Expr *pWhere,         /* the WHERE clause */
89890
 
  ExprList *pGroupBy,   /* the GROUP BY clause */
89891
 
  Expr *pHaving,        /* the HAVING clause */
89892
 
  ExprList *pOrderBy,   /* the ORDER BY clause */
89893
 
  int isDistinct,       /* true if the DISTINCT keyword is present */
89894
 
  Expr *pLimit,         /* LIMIT value.  NULL means not used */
89895
 
  Expr *pOffset         /* OFFSET value.  NULL means no offset */
89896
 
){
89897
 
  Select *pNew;
89898
 
  Select standin;
89899
 
  sqlite3 *db = pParse->db;
89900
 
  pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
89901
 
  assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
89902
 
  if( pNew==0 ){
89903
 
    pNew = &standin;
89904
 
    memset(pNew, 0, sizeof(*pNew));
89905
 
  }
89906
 
  if( pEList==0 ){
89907
 
    pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
89908
 
  }
89909
 
  pNew->pEList = pEList;
89910
 
  pNew->pSrc = pSrc;
89911
 
  pNew->pWhere = pWhere;
89912
 
  pNew->pGroupBy = pGroupBy;
89913
 
  pNew->pHaving = pHaving;
89914
 
  pNew->pOrderBy = pOrderBy;
89915
 
  pNew->selFlags = isDistinct ? SF_Distinct : 0;
89916
 
  pNew->op = TK_SELECT;
89917
 
  pNew->pLimit = pLimit;
89918
 
  pNew->pOffset = pOffset;
89919
 
  assert( pOffset==0 || pLimit!=0 );
89920
 
  pNew->addrOpenEphm[0] = -1;
89921
 
  pNew->addrOpenEphm[1] = -1;
89922
 
  pNew->addrOpenEphm[2] = -1;
89923
 
  if( db->mallocFailed ) {
89924
 
    clearSelect(db, pNew);
89925
 
    if( pNew!=&standin ) sqlite3DbFree(db, pNew);
89926
 
    pNew = 0;
89927
 
  }
89928
 
  return pNew;
89929
 
}
89930
 
 
89931
 
/*
89932
 
** Delete the given Select structure and all of its substructures.
89933
 
*/
89934
 
SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
89935
 
  if( p ){
89936
 
    clearSelect(db, p);
89937
 
    sqlite3DbFree(db, p);
89938
 
  }
89939
 
}
89940
 
 
89941
 
/*
89942
 
** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
89943
 
** type of join.  Return an integer constant that expresses that type
89944
 
** in terms of the following bit values:
89945
 
**
89946
 
**     JT_INNER
89947
 
**     JT_CROSS
89948
 
**     JT_OUTER
89949
 
**     JT_NATURAL
89950
 
**     JT_LEFT
89951
 
**     JT_RIGHT
89952
 
**
89953
 
** A full outer join is the combination of JT_LEFT and JT_RIGHT.
89954
 
**
89955
 
** If an illegal or unsupported join type is seen, then still return
89956
 
** a join type, but put an error in the pParse structure.
89957
 
*/
89958
 
SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
89959
 
  int jointype = 0;
89960
 
  Token *apAll[3];
89961
 
  Token *p;
89962
 
                             /*   0123456789 123456789 123456789 123 */
89963
 
  static const char zKeyText[] = "naturaleftouterightfullinnercross";
89964
 
  static const struct {
89965
 
    u8 i;        /* Beginning of keyword text in zKeyText[] */
89966
 
    u8 nChar;    /* Length of the keyword in characters */
89967
 
    u8 code;     /* Join type mask */
89968
 
  } aKeyword[] = {
89969
 
    /* natural */ { 0,  7, JT_NATURAL                },
89970
 
    /* left    */ { 6,  4, JT_LEFT|JT_OUTER          },
89971
 
    /* outer   */ { 10, 5, JT_OUTER                  },
89972
 
    /* right   */ { 14, 5, JT_RIGHT|JT_OUTER         },
89973
 
    /* full    */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
89974
 
    /* inner   */ { 23, 5, JT_INNER                  },
89975
 
    /* cross   */ { 28, 5, JT_INNER|JT_CROSS         },
89976
 
  };
89977
 
  int i, j;
89978
 
  apAll[0] = pA;
89979
 
  apAll[1] = pB;
89980
 
  apAll[2] = pC;
89981
 
  for(i=0; i<3 && apAll[i]; i++){
89982
 
    p = apAll[i];
89983
 
    for(j=0; j<ArraySize(aKeyword); j++){
89984
 
      if( p->n==aKeyword[j].nChar 
89985
 
          && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
89986
 
        jointype |= aKeyword[j].code;
89987
 
        break;
89988
 
      }
89989
 
    }
89990
 
    testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
89991
 
    if( j>=ArraySize(aKeyword) ){
89992
 
      jointype |= JT_ERROR;
89993
 
      break;
89994
 
    }
89995
 
  }
89996
 
  if(
89997
 
     (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
89998
 
     (jointype & JT_ERROR)!=0
89999
 
  ){
90000
 
    const char *zSp = " ";
90001
 
    assert( pB!=0 );
90002
 
    if( pC==0 ){ zSp++; }
90003
 
    sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
90004
 
       "%T %T%s%T", pA, pB, zSp, pC);
90005
 
    jointype = JT_INNER;
90006
 
  }else if( (jointype & JT_OUTER)!=0 
90007
 
         && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
90008
 
    sqlite3ErrorMsg(pParse, 
90009
 
      "RIGHT and FULL OUTER JOINs are not currently supported");
90010
 
    jointype = JT_INNER;
90011
 
  }
90012
 
  return jointype;
90013
 
}
90014
 
 
90015
 
/*
90016
 
** Return the index of a column in a table.  Return -1 if the column
90017
 
** is not contained in the table.
90018
 
*/
90019
 
static int columnIndex(Table *pTab, const char *zCol){
90020
 
  int i;
90021
 
  for(i=0; i<pTab->nCol; i++){
90022
 
    if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
90023
 
  }
90024
 
  return -1;
90025
 
}
90026
 
 
90027
 
/*
90028
 
** Search the first N tables in pSrc, from left to right, looking for a
90029
 
** table that has a column named zCol.  
90030
 
**
90031
 
** When found, set *piTab and *piCol to the table index and column index
90032
 
** of the matching column and return TRUE.
90033
 
**
90034
 
** If not found, return FALSE.
90035
 
*/
90036
 
static int tableAndColumnIndex(
90037
 
  SrcList *pSrc,       /* Array of tables to search */
90038
 
  int N,               /* Number of tables in pSrc->a[] to search */
90039
 
  const char *zCol,    /* Name of the column we are looking for */
90040
 
  int *piTab,          /* Write index of pSrc->a[] here */
90041
 
  int *piCol           /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
90042
 
){
90043
 
  int i;               /* For looping over tables in pSrc */
90044
 
  int iCol;            /* Index of column matching zCol */
90045
 
 
90046
 
  assert( (piTab==0)==(piCol==0) );  /* Both or neither are NULL */
90047
 
  for(i=0; i<N; i++){
90048
 
    iCol = columnIndex(pSrc->a[i].pTab, zCol);
90049
 
    if( iCol>=0 ){
90050
 
      if( piTab ){
90051
 
        *piTab = i;
90052
 
        *piCol = iCol;
90053
 
      }
90054
 
      return 1;
90055
 
    }
90056
 
  }
90057
 
  return 0;
90058
 
}
90059
 
 
90060
 
/*
90061
 
** This function is used to add terms implied by JOIN syntax to the
90062
 
** WHERE clause expression of a SELECT statement. The new term, which
90063
 
** is ANDed with the existing WHERE clause, is of the form:
90064
 
**
90065
 
**    (tab1.col1 = tab2.col2)
90066
 
**
90067
 
** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the 
90068
 
** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
90069
 
** column iColRight of tab2.
90070
 
*/
90071
 
static void addWhereTerm(
90072
 
  Parse *pParse,                  /* Parsing context */
90073
 
  SrcList *pSrc,                  /* List of tables in FROM clause */
90074
 
  int iLeft,                      /* Index of first table to join in pSrc */
90075
 
  int iColLeft,                   /* Index of column in first table */
90076
 
  int iRight,                     /* Index of second table in pSrc */
90077
 
  int iColRight,                  /* Index of column in second table */
90078
 
  int isOuterJoin,                /* True if this is an OUTER join */
90079
 
  Expr **ppWhere                  /* IN/OUT: The WHERE clause to add to */
90080
 
){
90081
 
  sqlite3 *db = pParse->db;
90082
 
  Expr *pE1;
90083
 
  Expr *pE2;
90084
 
  Expr *pEq;
90085
 
 
90086
 
  assert( iLeft<iRight );
90087
 
  assert( pSrc->nSrc>iRight );
90088
 
  assert( pSrc->a[iLeft].pTab );
90089
 
  assert( pSrc->a[iRight].pTab );
90090
 
 
90091
 
  pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
90092
 
  pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
90093
 
 
90094
 
  pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0);
90095
 
  if( pEq && isOuterJoin ){
90096
 
    ExprSetProperty(pEq, EP_FromJoin);
90097
 
    assert( !ExprHasAnyProperty(pEq, EP_TokenOnly|EP_Reduced) );
90098
 
    ExprSetIrreducible(pEq);
90099
 
    pEq->iRightJoinTable = (i16)pE2->iTable;
90100
 
  }
90101
 
  *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
90102
 
}
90103
 
 
90104
 
/*
90105
 
** Set the EP_FromJoin property on all terms of the given expression.
90106
 
** And set the Expr.iRightJoinTable to iTable for every term in the
90107
 
** expression.
90108
 
**
90109
 
** The EP_FromJoin property is used on terms of an expression to tell
90110
 
** the LEFT OUTER JOIN processing logic that this term is part of the
90111
 
** join restriction specified in the ON or USING clause and not a part
90112
 
** of the more general WHERE clause.  These terms are moved over to the
90113
 
** WHERE clause during join processing but we need to remember that they
90114
 
** originated in the ON or USING clause.
90115
 
**
90116
 
** The Expr.iRightJoinTable tells the WHERE clause processing that the
90117
 
** expression depends on table iRightJoinTable even if that table is not
90118
 
** explicitly mentioned in the expression.  That information is needed
90119
 
** for cases like this:
90120
 
**
90121
 
**    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
90122
 
**
90123
 
** The where clause needs to defer the handling of the t1.x=5
90124
 
** term until after the t2 loop of the join.  In that way, a
90125
 
** NULL t2 row will be inserted whenever t1.x!=5.  If we do not
90126
 
** defer the handling of t1.x=5, it will be processed immediately
90127
 
** after the t1 loop and rows with t1.x!=5 will never appear in
90128
 
** the output, which is incorrect.
90129
 
*/
90130
 
static void setJoinExpr(Expr *p, int iTable){
90131
 
  while( p ){
90132
 
    ExprSetProperty(p, EP_FromJoin);
90133
 
    assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
90134
 
    ExprSetIrreducible(p);
90135
 
    p->iRightJoinTable = (i16)iTable;
90136
 
    setJoinExpr(p->pLeft, iTable);
90137
 
    p = p->pRight;
90138
 
  } 
90139
 
}
90140
 
 
90141
 
/*
90142
 
** This routine processes the join information for a SELECT statement.
90143
 
** ON and USING clauses are converted into extra terms of the WHERE clause.
90144
 
** NATURAL joins also create extra WHERE clause terms.
90145
 
**
90146
 
** The terms of a FROM clause are contained in the Select.pSrc structure.
90147
 
** The left most table is the first entry in Select.pSrc.  The right-most
90148
 
** table is the last entry.  The join operator is held in the entry to
90149
 
** the left.  Thus entry 0 contains the join operator for the join between
90150
 
** entries 0 and 1.  Any ON or USING clauses associated with the join are
90151
 
** also attached to the left entry.
90152
 
**
90153
 
** This routine returns the number of errors encountered.
90154
 
*/
90155
 
static int sqliteProcessJoin(Parse *pParse, Select *p){
90156
 
  SrcList *pSrc;                  /* All tables in the FROM clause */
90157
 
  int i, j;                       /* Loop counters */
90158
 
  struct SrcList_item *pLeft;     /* Left table being joined */
90159
 
  struct SrcList_item *pRight;    /* Right table being joined */
90160
 
 
90161
 
  pSrc = p->pSrc;
90162
 
  pLeft = &pSrc->a[0];
90163
 
  pRight = &pLeft[1];
90164
 
  for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
90165
 
    Table *pLeftTab = pLeft->pTab;
90166
 
    Table *pRightTab = pRight->pTab;
90167
 
    int isOuter;
90168
 
 
90169
 
    if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
90170
 
    isOuter = (pRight->jointype & JT_OUTER)!=0;
90171
 
 
90172
 
    /* When the NATURAL keyword is present, add WHERE clause terms for
90173
 
    ** every column that the two tables have in common.
90174
 
    */
90175
 
    if( pRight->jointype & JT_NATURAL ){
90176
 
      if( pRight->pOn || pRight->pUsing ){
90177
 
        sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
90178
 
           "an ON or USING clause", 0);
90179
 
        return 1;
90180
 
      }
90181
 
      for(j=0; j<pRightTab->nCol; j++){
90182
 
        char *zName;   /* Name of column in the right table */
90183
 
        int iLeft;     /* Matching left table */
90184
 
        int iLeftCol;  /* Matching column in the left table */
90185
 
 
90186
 
        zName = pRightTab->aCol[j].zName;
90187
 
        if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
90188
 
          addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
90189
 
                       isOuter, &p->pWhere);
90190
 
        }
90191
 
      }
90192
 
    }
90193
 
 
90194
 
    /* Disallow both ON and USING clauses in the same join
90195
 
    */
90196
 
    if( pRight->pOn && pRight->pUsing ){
90197
 
      sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
90198
 
        "clauses in the same join");
90199
 
      return 1;
90200
 
    }
90201
 
 
90202
 
    /* Add the ON clause to the end of the WHERE clause, connected by
90203
 
    ** an AND operator.
90204
 
    */
90205
 
    if( pRight->pOn ){
90206
 
      if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
90207
 
      p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
90208
 
      pRight->pOn = 0;
90209
 
    }
90210
 
 
90211
 
    /* Create extra terms on the WHERE clause for each column named
90212
 
    ** in the USING clause.  Example: If the two tables to be joined are 
90213
 
    ** A and B and the USING clause names X, Y, and Z, then add this
90214
 
    ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
90215
 
    ** Report an error if any column mentioned in the USING clause is
90216
 
    ** not contained in both tables to be joined.
90217
 
    */
90218
 
    if( pRight->pUsing ){
90219
 
      IdList *pList = pRight->pUsing;
90220
 
      for(j=0; j<pList->nId; j++){
90221
 
        char *zName;     /* Name of the term in the USING clause */
90222
 
        int iLeft;       /* Table on the left with matching column name */
90223
 
        int iLeftCol;    /* Column number of matching column on the left */
90224
 
        int iRightCol;   /* Column number of matching column on the right */
90225
 
 
90226
 
        zName = pList->a[j].zName;
90227
 
        iRightCol = columnIndex(pRightTab, zName);
90228
 
        if( iRightCol<0
90229
 
         || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
90230
 
        ){
90231
 
          sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
90232
 
            "not present in both tables", zName);
90233
 
          return 1;
90234
 
        }
90235
 
        addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
90236
 
                     isOuter, &p->pWhere);
90237
 
      }
90238
 
    }
90239
 
  }
90240
 
  return 0;
90241
 
}
90242
 
 
90243
 
/*
90244
 
** Insert code into "v" that will push the record on the top of the
90245
 
** stack into the sorter.
90246
 
*/
90247
 
static void pushOntoSorter(
90248
 
  Parse *pParse,         /* Parser context */
90249
 
  ExprList *pOrderBy,    /* The ORDER BY clause */
90250
 
  Select *pSelect,       /* The whole SELECT statement */
90251
 
  int regData            /* Register holding data to be sorted */
90252
 
){
90253
 
  Vdbe *v = pParse->pVdbe;
90254
 
  int nExpr = pOrderBy->nExpr;
90255
 
  int regBase = sqlite3GetTempRange(pParse, nExpr+2);
90256
 
  int regRecord = sqlite3GetTempReg(pParse);
90257
 
  sqlite3ExprCacheClear(pParse);
90258
 
  sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
90259
 
  sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
90260
 
  sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
90261
 
  sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
90262
 
  sqlite3VdbeAddOp2(v, OP_IdxInsert, pOrderBy->iECursor, regRecord);
90263
 
  sqlite3ReleaseTempReg(pParse, regRecord);
90264
 
  sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
90265
 
  if( pSelect->iLimit ){
90266
 
    int addr1, addr2;
90267
 
    int iLimit;
90268
 
    if( pSelect->iOffset ){
90269
 
      iLimit = pSelect->iOffset+1;
90270
 
    }else{
90271
 
      iLimit = pSelect->iLimit;
90272
 
    }
90273
 
    addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit);
90274
 
    sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1);
90275
 
    addr2 = sqlite3VdbeAddOp0(v, OP_Goto);
90276
 
    sqlite3VdbeJumpHere(v, addr1);
90277
 
    sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
90278
 
    sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
90279
 
    sqlite3VdbeJumpHere(v, addr2);
90280
 
  }
90281
 
}
90282
 
 
90283
 
/*
90284
 
** Add code to implement the OFFSET
90285
 
*/
90286
 
static void codeOffset(
90287
 
  Vdbe *v,          /* Generate code into this VM */
90288
 
  Select *p,        /* The SELECT statement being coded */
90289
 
  int iContinue     /* Jump here to skip the current record */
90290
 
){
90291
 
  if( p->iOffset && iContinue!=0 ){
90292
 
    int addr;
90293
 
    sqlite3VdbeAddOp2(v, OP_AddImm, p->iOffset, -1);
90294
 
    addr = sqlite3VdbeAddOp1(v, OP_IfNeg, p->iOffset);
90295
 
    sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
90296
 
    VdbeComment((v, "skip OFFSET records"));
90297
 
    sqlite3VdbeJumpHere(v, addr);
90298
 
  }
90299
 
}
90300
 
 
90301
 
/*
90302
 
** Add code that will check to make sure the N registers starting at iMem
90303
 
** form a distinct entry.  iTab is a sorting index that holds previously
90304
 
** seen combinations of the N values.  A new entry is made in iTab
90305
 
** if the current N values are new.
90306
 
**
90307
 
** A jump to addrRepeat is made and the N+1 values are popped from the
90308
 
** stack if the top N elements are not distinct.
90309
 
*/
90310
 
static void codeDistinct(
90311
 
  Parse *pParse,     /* Parsing and code generating context */
90312
 
  int iTab,          /* A sorting index used to test for distinctness */
90313
 
  int addrRepeat,    /* Jump to here if not distinct */
90314
 
  int N,             /* Number of elements */
90315
 
  int iMem           /* First element */
90316
 
){
90317
 
  Vdbe *v;
90318
 
  int r1;
90319
 
 
90320
 
  v = pParse->pVdbe;
90321
 
  r1 = sqlite3GetTempReg(pParse);
90322
 
  sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N);
90323
 
  sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
90324
 
  sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
90325
 
  sqlite3ReleaseTempReg(pParse, r1);
90326
 
}
90327
 
 
90328
 
#ifndef SQLITE_OMIT_SUBQUERY
90329
 
/*
90330
 
** Generate an error message when a SELECT is used within a subexpression
90331
 
** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
90332
 
** column.  We do this in a subroutine because the error used to occur
90333
 
** in multiple places.  (The error only occurs in one place now, but we
90334
 
** retain the subroutine to minimize code disruption.)
90335
 
*/
90336
 
static int checkForMultiColumnSelectError(
90337
 
  Parse *pParse,       /* Parse context. */
90338
 
  SelectDest *pDest,   /* Destination of SELECT results */
90339
 
  int nExpr            /* Number of result columns returned by SELECT */
90340
 
){
90341
 
  int eDest = pDest->eDest;
90342
 
  if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
90343
 
    sqlite3ErrorMsg(pParse, "only a single result allowed for "
90344
 
       "a SELECT that is part of an expression");
90345
 
    return 1;
90346
 
  }else{
90347
 
    return 0;
90348
 
  }
90349
 
}
90350
 
#endif
90351
 
 
90352
 
/*
90353
 
** This routine generates the code for the inside of the inner loop
90354
 
** of a SELECT.
90355
 
**
90356
 
** If srcTab and nColumn are both zero, then the pEList expressions
90357
 
** are evaluated in order to get the data for this row.  If nColumn>0
90358
 
** then data is pulled from srcTab and pEList is used only to get the
90359
 
** datatypes for each column.
90360
 
*/
90361
 
static void selectInnerLoop(
90362
 
  Parse *pParse,          /* The parser context */
90363
 
  Select *p,              /* The complete select statement being coded */
90364
 
  ExprList *pEList,       /* List of values being extracted */
90365
 
  int srcTab,             /* Pull data from this table */
90366
 
  int nColumn,            /* Number of columns in the source table */
90367
 
  ExprList *pOrderBy,     /* If not NULL, sort results using this key */
90368
 
  int distinct,           /* If >=0, make sure results are distinct */
90369
 
  SelectDest *pDest,      /* How to dispose of the results */
90370
 
  int iContinue,          /* Jump here to continue with next row */
90371
 
  int iBreak              /* Jump here to break out of the inner loop */
90372
 
){
90373
 
  Vdbe *v = pParse->pVdbe;
90374
 
  int i;
90375
 
  int hasDistinct;        /* True if the DISTINCT keyword is present */
90376
 
  int regResult;              /* Start of memory holding result set */
90377
 
  int eDest = pDest->eDest;   /* How to dispose of results */
90378
 
  int iParm = pDest->iParm;   /* First argument to disposal method */
90379
 
  int nResultCol;             /* Number of result columns */
90380
 
 
90381
 
  assert( v );
90382
 
  if( NEVER(v==0) ) return;
90383
 
  assert( pEList!=0 );
90384
 
  hasDistinct = distinct>=0;
90385
 
  if( pOrderBy==0 && !hasDistinct ){
90386
 
    codeOffset(v, p, iContinue);
90387
 
  }
90388
 
 
90389
 
  /* Pull the requested columns.
90390
 
  */
90391
 
  if( nColumn>0 ){
90392
 
    nResultCol = nColumn;
90393
 
  }else{
90394
 
    nResultCol = pEList->nExpr;
90395
 
  }
90396
 
  if( pDest->iMem==0 ){
90397
 
    pDest->iMem = pParse->nMem+1;
90398
 
    pDest->nMem = nResultCol;
90399
 
    pParse->nMem += nResultCol;
90400
 
  }else{ 
90401
 
    assert( pDest->nMem==nResultCol );
90402
 
  }
90403
 
  regResult = pDest->iMem;
90404
 
  if( nColumn>0 ){
90405
 
    for(i=0; i<nColumn; i++){
90406
 
      sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
90407
 
    }
90408
 
  }else if( eDest!=SRT_Exists ){
90409
 
    /* If the destination is an EXISTS(...) expression, the actual
90410
 
    ** values returned by the SELECT are not required.
90411
 
    */
90412
 
    sqlite3ExprCacheClear(pParse);
90413
 
    sqlite3ExprCodeExprList(pParse, pEList, regResult, eDest==SRT_Output);
90414
 
  }
90415
 
  nColumn = nResultCol;
90416
 
 
90417
 
  /* If the DISTINCT keyword was present on the SELECT statement
90418
 
  ** and this row has been seen before, then do not make this row
90419
 
  ** part of the result.
90420
 
  */
90421
 
  if( hasDistinct ){
90422
 
    assert( pEList!=0 );
90423
 
    assert( pEList->nExpr==nColumn );
90424
 
    codeDistinct(pParse, distinct, iContinue, nColumn, regResult);
90425
 
    if( pOrderBy==0 ){
90426
 
      codeOffset(v, p, iContinue);
90427
 
    }
90428
 
  }
90429
 
 
90430
 
  switch( eDest ){
90431
 
    /* In this mode, write each query result to the key of the temporary
90432
 
    ** table iParm.
90433
 
    */
90434
 
#ifndef SQLITE_OMIT_COMPOUND_SELECT
90435
 
    case SRT_Union: {
90436
 
      int r1;
90437
 
      r1 = sqlite3GetTempReg(pParse);
90438
 
      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
90439
 
      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
90440
 
      sqlite3ReleaseTempReg(pParse, r1);
90441
 
      break;
90442
 
    }
90443
 
 
90444
 
    /* Construct a record from the query result, but instead of
90445
 
    ** saving that record, use it as a key to delete elements from
90446
 
    ** the temporary table iParm.
90447
 
    */
90448
 
    case SRT_Except: {
90449
 
      sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nColumn);
90450
 
      break;
90451
 
    }
90452
 
#endif
90453
 
 
90454
 
    /* Store the result as data using a unique key.
90455
 
    */
90456
 
    case SRT_Table:
90457
 
    case SRT_EphemTab: {
90458
 
      int r1 = sqlite3GetTempReg(pParse);
90459
 
      testcase( eDest==SRT_Table );
90460
 
      testcase( eDest==SRT_EphemTab );
90461
 
      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
90462
 
      if( pOrderBy ){
90463
 
        pushOntoSorter(pParse, pOrderBy, p, r1);
90464
 
      }else{
90465
 
        int r2 = sqlite3GetTempReg(pParse);
90466
 
        sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
90467
 
        sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
90468
 
        sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
90469
 
        sqlite3ReleaseTempReg(pParse, r2);
90470
 
      }
90471
 
      sqlite3ReleaseTempReg(pParse, r1);
90472
 
      break;
90473
 
    }
90474
 
 
90475
 
#ifndef SQLITE_OMIT_SUBQUERY
90476
 
    /* If we are creating a set for an "expr IN (SELECT ...)" construct,
90477
 
    ** then there should be a single item on the stack.  Write this
90478
 
    ** item into the set table with bogus data.
90479
 
    */
90480
 
    case SRT_Set: {
90481
 
      assert( nColumn==1 );
90482
 
      p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affinity);
90483
 
      if( pOrderBy ){
90484
 
        /* At first glance you would think we could optimize out the
90485
 
        ** ORDER BY in this case since the order of entries in the set
90486
 
        ** does not matter.  But there might be a LIMIT clause, in which
90487
 
        ** case the order does matter */
90488
 
        pushOntoSorter(pParse, pOrderBy, p, regResult);
90489
 
      }else{
90490
 
        int r1 = sqlite3GetTempReg(pParse);
90491
 
        sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, 1, r1, &p->affinity, 1);
90492
 
        sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
90493
 
        sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
90494
 
        sqlite3ReleaseTempReg(pParse, r1);
90495
 
      }
90496
 
      break;
90497
 
    }
90498
 
 
90499
 
    /* If any row exist in the result set, record that fact and abort.
90500
 
    */
90501
 
    case SRT_Exists: {
90502
 
      sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
90503
 
      /* The LIMIT clause will terminate the loop for us */
90504
 
      break;
90505
 
    }
90506
 
 
90507
 
    /* If this is a scalar select that is part of an expression, then
90508
 
    ** store the results in the appropriate memory cell and break out
90509
 
    ** of the scan loop.
90510
 
    */
90511
 
    case SRT_Mem: {
90512
 
      assert( nColumn==1 );
90513
 
      if( pOrderBy ){
90514
 
        pushOntoSorter(pParse, pOrderBy, p, regResult);
90515
 
      }else{
90516
 
        sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
90517
 
        /* The LIMIT clause will jump out of the loop for us */
90518
 
      }
90519
 
      break;
90520
 
    }
90521
 
#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
90522
 
 
90523
 
    /* Send the data to the callback function or to a subroutine.  In the
90524
 
    ** case of a subroutine, the subroutine itself is responsible for
90525
 
    ** popping the data from the stack.
90526
 
    */
90527
 
    case SRT_Coroutine:
90528
 
    case SRT_Output: {
90529
 
      testcase( eDest==SRT_Coroutine );
90530
 
      testcase( eDest==SRT_Output );
90531
 
      if( pOrderBy ){
90532
 
        int r1 = sqlite3GetTempReg(pParse);
90533
 
        sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
90534
 
        pushOntoSorter(pParse, pOrderBy, p, r1);
90535
 
        sqlite3ReleaseTempReg(pParse, r1);
90536
 
      }else if( eDest==SRT_Coroutine ){
90537
 
        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
90538
 
      }else{
90539
 
        sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn);
90540
 
        sqlite3ExprCacheAffinityChange(pParse, regResult, nColumn);
90541
 
      }
90542
 
      break;
90543
 
    }
90544
 
 
90545
 
#if !defined(SQLITE_OMIT_TRIGGER)
90546
 
    /* Discard the results.  This is used for SELECT statements inside
90547
 
    ** the body of a TRIGGER.  The purpose of such selects is to call
90548
 
    ** user-defined functions that have side effects.  We do not care
90549
 
    ** about the actual results of the select.
90550
 
    */
90551
 
    default: {
90552
 
      assert( eDest==SRT_Discard );
90553
 
      break;
90554
 
    }
90555
 
#endif
90556
 
  }
90557
 
 
90558
 
  /* Jump to the end of the loop if the LIMIT is reached.  Except, if
90559
 
  ** there is a sorter, in which case the sorter has already limited
90560
 
  ** the output for us.
90561
 
  */
90562
 
  if( pOrderBy==0 && p->iLimit ){
90563
 
    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
90564
 
  }
90565
 
}
90566
 
 
90567
 
/*
90568
 
** Given an expression list, generate a KeyInfo structure that records
90569
 
** the collating sequence for each expression in that expression list.
90570
 
**
90571
 
** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
90572
 
** KeyInfo structure is appropriate for initializing a virtual index to
90573
 
** implement that clause.  If the ExprList is the result set of a SELECT
90574
 
** then the KeyInfo structure is appropriate for initializing a virtual
90575
 
** index to implement a DISTINCT test.
90576
 
**
90577
 
** Space to hold the KeyInfo structure is obtain from malloc.  The calling
90578
 
** function is responsible for seeing that this structure is eventually
90579
 
** freed.  Add the KeyInfo structure to the P4 field of an opcode using
90580
 
** P4_KEYINFO_HANDOFF is the usual way of dealing with this.
90581
 
*/
90582
 
static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){
90583
 
  sqlite3 *db = pParse->db;
90584
 
  int nExpr;
90585
 
  KeyInfo *pInfo;
90586
 
  struct ExprList_item *pItem;
90587
 
  int i;
90588
 
 
90589
 
  nExpr = pList->nExpr;
90590
 
  pInfo = sqlite3DbMallocZero(db, sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) );
90591
 
  if( pInfo ){
90592
 
    pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr];
90593
 
    pInfo->nField = (u16)nExpr;
90594
 
    pInfo->enc = ENC(db);
90595
 
    pInfo->db = db;
90596
 
    for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
90597
 
      CollSeq *pColl;
90598
 
      pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
90599
 
      if( !pColl ){
90600
 
        pColl = db->pDfltColl;
90601
 
      }
90602
 
      pInfo->aColl[i] = pColl;
90603
 
      pInfo->aSortOrder[i] = pItem->sortOrder;
90604
 
    }
90605
 
  }
90606
 
  return pInfo;
90607
 
}
90608
 
 
90609
 
#ifndef SQLITE_OMIT_COMPOUND_SELECT
90610
 
/*
90611
 
** Name of the connection operator, used for error messages.
90612
 
*/
90613
 
static const char *selectOpName(int id){
90614
 
  char *z;
90615
 
  switch( id ){
90616
 
    case TK_ALL:       z = "UNION ALL";   break;
90617
 
    case TK_INTERSECT: z = "INTERSECT";   break;
90618
 
    case TK_EXCEPT:    z = "EXCEPT";      break;
90619
 
    default:           z = "UNION";       break;
90620
 
  }
90621
 
  return z;
90622
 
}
90623
 
#endif /* SQLITE_OMIT_COMPOUND_SELECT */
90624
 
 
90625
 
#ifndef SQLITE_OMIT_EXPLAIN
90626
 
/*
90627
 
** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
90628
 
** is a no-op. Otherwise, it adds a single row of output to the EQP result,
90629
 
** where the caption is of the form:
90630
 
**
90631
 
**   "USE TEMP B-TREE FOR xxx"
90632
 
**
90633
 
** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
90634
 
** is determined by the zUsage argument.
90635
 
*/
90636
 
static void explainTempTable(Parse *pParse, const char *zUsage){
90637
 
  if( pParse->explain==2 ){
90638
 
    Vdbe *v = pParse->pVdbe;
90639
 
    char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
90640
 
    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
90641
 
  }
90642
 
}
90643
 
 
90644
 
/*
90645
 
** Assign expression b to lvalue a. A second, no-op, version of this macro
90646
 
** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
90647
 
** in sqlite3Select() to assign values to structure member variables that
90648
 
** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
90649
 
** code with #ifndef directives.
90650
 
*/
90651
 
# define explainSetInteger(a, b) a = b
90652
 
 
90653
 
#else
90654
 
/* No-op versions of the explainXXX() functions and macros. */
90655
 
# define explainTempTable(y,z)
90656
 
# define explainSetInteger(y,z)
90657
 
#endif
90658
 
 
90659
 
#if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
90660
 
/*
90661
 
** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
90662
 
** is a no-op. Otherwise, it adds a single row of output to the EQP result,
90663
 
** where the caption is of one of the two forms:
90664
 
**
90665
 
**   "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
90666
 
**   "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
90667
 
**
90668
 
** where iSub1 and iSub2 are the integers passed as the corresponding
90669
 
** function parameters, and op is the text representation of the parameter
90670
 
** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
90671
 
** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is 
90672
 
** false, or the second form if it is true.
90673
 
*/
90674
 
static void explainComposite(
90675
 
  Parse *pParse,                  /* Parse context */
90676
 
  int op,                         /* One of TK_UNION, TK_EXCEPT etc. */
90677
 
  int iSub1,                      /* Subquery id 1 */
90678
 
  int iSub2,                      /* Subquery id 2 */
90679
 
  int bUseTmp                     /* True if a temp table was used */
90680
 
){
90681
 
  assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
90682
 
  if( pParse->explain==2 ){
90683
 
    Vdbe *v = pParse->pVdbe;
90684
 
    char *zMsg = sqlite3MPrintf(
90685
 
        pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
90686
 
        bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
90687
 
    );
90688
 
    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
90689
 
  }
90690
 
}
90691
 
#else
90692
 
/* No-op versions of the explainXXX() functions and macros. */
90693
 
# define explainComposite(v,w,x,y,z)
90694
 
#endif
90695
 
 
90696
 
/*
90697
 
** If the inner loop was generated using a non-null pOrderBy argument,
90698
 
** then the results were placed in a sorter.  After the loop is terminated
90699
 
** we need to run the sorter and output the results.  The following
90700
 
** routine generates the code needed to do that.
90701
 
*/
90702
 
static void generateSortTail(
90703
 
  Parse *pParse,    /* Parsing context */
90704
 
  Select *p,        /* The SELECT statement */
90705
 
  Vdbe *v,          /* Generate code into this VDBE */
90706
 
  int nColumn,      /* Number of columns of data */
90707
 
  SelectDest *pDest /* Write the sorted results here */
90708
 
){
90709
 
  int addrBreak = sqlite3VdbeMakeLabel(v);     /* Jump here to exit loop */
90710
 
  int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
90711
 
  int addr;
90712
 
  int iTab;
90713
 
  int pseudoTab = 0;
90714
 
  ExprList *pOrderBy = p->pOrderBy;
90715
 
 
90716
 
  int eDest = pDest->eDest;
90717
 
  int iParm = pDest->iParm;
90718
 
 
90719
 
  int regRow;
90720
 
  int regRowid;
90721
 
 
90722
 
  iTab = pOrderBy->iECursor;
90723
 
  regRow = sqlite3GetTempReg(pParse);
90724
 
  if( eDest==SRT_Output || eDest==SRT_Coroutine ){
90725
 
    pseudoTab = pParse->nTab++;
90726
 
    sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
90727
 
    regRowid = 0;
90728
 
  }else{
90729
 
    regRowid = sqlite3GetTempReg(pParse);
90730
 
  }
90731
 
  addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
90732
 
  codeOffset(v, p, addrContinue);
90733
 
  sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr + 1, regRow);
90734
 
  switch( eDest ){
90735
 
    case SRT_Table:
90736
 
    case SRT_EphemTab: {
90737
 
      testcase( eDest==SRT_Table );
90738
 
      testcase( eDest==SRT_EphemTab );
90739
 
      sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
90740
 
      sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
90741
 
      sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
90742
 
      break;
90743
 
    }
90744
 
#ifndef SQLITE_OMIT_SUBQUERY
90745
 
    case SRT_Set: {
90746
 
      assert( nColumn==1 );
90747
 
      sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, &p->affinity, 1);
90748
 
      sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
90749
 
      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
90750
 
      break;
90751
 
    }
90752
 
    case SRT_Mem: {
90753
 
      assert( nColumn==1 );
90754
 
      sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
90755
 
      /* The LIMIT clause will terminate the loop for us */
90756
 
      break;
90757
 
    }
90758
 
#endif
90759
 
    default: {
90760
 
      int i;
90761
 
      assert( eDest==SRT_Output || eDest==SRT_Coroutine ); 
90762
 
      testcase( eDest==SRT_Output );
90763
 
      testcase( eDest==SRT_Coroutine );
90764
 
      for(i=0; i<nColumn; i++){
90765
 
        assert( regRow!=pDest->iMem+i );
90766
 
        sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iMem+i);
90767
 
        if( i==0 ){
90768
 
          sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
90769
 
        }
90770
 
      }
90771
 
      if( eDest==SRT_Output ){
90772
 
        sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iMem, nColumn);
90773
 
        sqlite3ExprCacheAffinityChange(pParse, pDest->iMem, nColumn);
90774
 
      }else{
90775
 
        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
90776
 
      }
90777
 
      break;
90778
 
    }
90779
 
  }
90780
 
  sqlite3ReleaseTempReg(pParse, regRow);
90781
 
  sqlite3ReleaseTempReg(pParse, regRowid);
90782
 
 
90783
 
  /* The bottom of the loop
90784
 
  */
90785
 
  sqlite3VdbeResolveLabel(v, addrContinue);
90786
 
  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
90787
 
  sqlite3VdbeResolveLabel(v, addrBreak);
90788
 
  if( eDest==SRT_Output || eDest==SRT_Coroutine ){
90789
 
    sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
90790
 
  }
90791
 
}
90792
 
 
90793
 
/*
90794
 
** Return a pointer to a string containing the 'declaration type' of the
90795
 
** expression pExpr. The string may be treated as static by the caller.
90796
 
**
90797
 
** The declaration type is the exact datatype definition extracted from the
90798
 
** original CREATE TABLE statement if the expression is a column. The
90799
 
** declaration type for a ROWID field is INTEGER. Exactly when an expression
90800
 
** is considered a column can be complex in the presence of subqueries. The
90801
 
** result-set expression in all of the following SELECT statements is 
90802
 
** considered a column by this function.
90803
 
**
90804
 
**   SELECT col FROM tbl;
90805
 
**   SELECT (SELECT col FROM tbl;
90806
 
**   SELECT (SELECT col FROM tbl);
90807
 
**   SELECT abc FROM (SELECT col AS abc FROM tbl);
90808
 
** 
90809
 
** The declaration type for any expression other than a column is NULL.
90810
 
*/
90811
 
static const char *columnType(
90812
 
  NameContext *pNC, 
90813
 
  Expr *pExpr,
90814
 
  const char **pzOriginDb,
90815
 
  const char **pzOriginTab,
90816
 
  const char **pzOriginCol
90817
 
){
90818
 
  char const *zType = 0;
90819
 
  char const *zOriginDb = 0;
90820
 
  char const *zOriginTab = 0;
90821
 
  char const *zOriginCol = 0;
90822
 
  int j;
90823
 
  if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
90824
 
 
90825
 
  switch( pExpr->op ){
90826
 
    case TK_AGG_COLUMN:
90827
 
    case TK_COLUMN: {
90828
 
      /* The expression is a column. Locate the table the column is being
90829
 
      ** extracted from in NameContext.pSrcList. This table may be real
90830
 
      ** database table or a subquery.
90831
 
      */
90832
 
      Table *pTab = 0;            /* Table structure column is extracted from */
90833
 
      Select *pS = 0;             /* Select the column is extracted from */
90834
 
      int iCol = pExpr->iColumn;  /* Index of column in pTab */
90835
 
      testcase( pExpr->op==TK_AGG_COLUMN );
90836
 
      testcase( pExpr->op==TK_COLUMN );
90837
 
      while( pNC && !pTab ){
90838
 
        SrcList *pTabList = pNC->pSrcList;
90839
 
        for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
90840
 
        if( j<pTabList->nSrc ){
90841
 
          pTab = pTabList->a[j].pTab;
90842
 
          pS = pTabList->a[j].pSelect;
90843
 
        }else{
90844
 
          pNC = pNC->pNext;
90845
 
        }
90846
 
      }
90847
 
 
90848
 
      if( pTab==0 ){
90849
 
        /* At one time, code such as "SELECT new.x" within a trigger would
90850
 
        ** cause this condition to run.  Since then, we have restructured how
90851
 
        ** trigger code is generated and so this condition is no longer 
90852
 
        ** possible. However, it can still be true for statements like
90853
 
        ** the following:
90854
 
        **
90855
 
        **   CREATE TABLE t1(col INTEGER);
90856
 
        **   SELECT (SELECT t1.col) FROM FROM t1;
90857
 
        **
90858
 
        ** when columnType() is called on the expression "t1.col" in the 
90859
 
        ** sub-select. In this case, set the column type to NULL, even
90860
 
        ** though it should really be "INTEGER".
90861
 
        **
90862
 
        ** This is not a problem, as the column type of "t1.col" is never
90863
 
        ** used. When columnType() is called on the expression 
90864
 
        ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
90865
 
        ** branch below.  */
90866
 
        break;
90867
 
      }
90868
 
 
90869
 
      assert( pTab && pExpr->pTab==pTab );
90870
 
      if( pS ){
90871
 
        /* The "table" is actually a sub-select or a view in the FROM clause
90872
 
        ** of the SELECT statement. Return the declaration type and origin
90873
 
        ** data for the result-set column of the sub-select.
90874
 
        */
90875
 
        if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
90876
 
          /* If iCol is less than zero, then the expression requests the
90877
 
          ** rowid of the sub-select or view. This expression is legal (see 
90878
 
          ** test case misc2.2.2) - it always evaluates to NULL.
90879
 
          */
90880
 
          NameContext sNC;
90881
 
          Expr *p = pS->pEList->a[iCol].pExpr;
90882
 
          sNC.pSrcList = pS->pSrc;
90883
 
          sNC.pNext = pNC;
90884
 
          sNC.pParse = pNC->pParse;
90885
 
          zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 
90886
 
        }
90887
 
      }else if( ALWAYS(pTab->pSchema) ){
90888
 
        /* A real table */
90889
 
        assert( !pS );
90890
 
        if( iCol<0 ) iCol = pTab->iPKey;
90891
 
        assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
90892
 
        if( iCol<0 ){
90893
 
          zType = "INTEGER";
90894
 
          zOriginCol = "rowid";
90895
 
        }else{
90896
 
          zType = pTab->aCol[iCol].zType;
90897
 
          zOriginCol = pTab->aCol[iCol].zName;
90898
 
        }
90899
 
        zOriginTab = pTab->zName;
90900
 
        if( pNC->pParse ){
90901
 
          int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
90902
 
          zOriginDb = pNC->pParse->db->aDb[iDb].zName;
90903
 
        }
90904
 
      }
90905
 
      break;
90906
 
    }
90907
 
#ifndef SQLITE_OMIT_SUBQUERY
90908
 
    case TK_SELECT: {
90909
 
      /* The expression is a sub-select. Return the declaration type and
90910
 
      ** origin info for the single column in the result set of the SELECT
90911
 
      ** statement.
90912
 
      */
90913
 
      NameContext sNC;
90914
 
      Select *pS = pExpr->x.pSelect;
90915
 
      Expr *p = pS->pEList->a[0].pExpr;
90916
 
      assert( ExprHasProperty(pExpr, EP_xIsSelect) );
90917
 
      sNC.pSrcList = pS->pSrc;
90918
 
      sNC.pNext = pNC;
90919
 
      sNC.pParse = pNC->pParse;
90920
 
      zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 
90921
 
      break;
90922
 
    }
90923
 
#endif
90924
 
  }
90925
 
  
90926
 
  if( pzOriginDb ){
90927
 
    assert( pzOriginTab && pzOriginCol );
90928
 
    *pzOriginDb = zOriginDb;
90929
 
    *pzOriginTab = zOriginTab;
90930
 
    *pzOriginCol = zOriginCol;
90931
 
  }
90932
 
  return zType;
90933
 
}
90934
 
 
90935
 
/*
90936
 
** Generate code that will tell the VDBE the declaration types of columns
90937
 
** in the result set.
90938
 
*/
90939
 
static void generateColumnTypes(
90940
 
  Parse *pParse,      /* Parser context */
90941
 
  SrcList *pTabList,  /* List of tables */
90942
 
  ExprList *pEList    /* Expressions defining the result set */
90943
 
){
90944
 
#ifndef SQLITE_OMIT_DECLTYPE
90945
 
  Vdbe *v = pParse->pVdbe;
90946
 
  int i;
90947
 
  NameContext sNC;
90948
 
  sNC.pSrcList = pTabList;
90949
 
  sNC.pParse = pParse;
90950
 
  for(i=0; i<pEList->nExpr; i++){
90951
 
    Expr *p = pEList->a[i].pExpr;
90952
 
    const char *zType;
90953
 
#ifdef SQLITE_ENABLE_COLUMN_METADATA
90954
 
    const char *zOrigDb = 0;
90955
 
    const char *zOrigTab = 0;
90956
 
    const char *zOrigCol = 0;
90957
 
    zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
90958
 
 
90959
 
    /* The vdbe must make its own copy of the column-type and other 
90960
 
    ** column specific strings, in case the schema is reset before this
90961
 
    ** virtual machine is deleted.
90962
 
    */
90963
 
    sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
90964
 
    sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
90965
 
    sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
90966
 
#else
90967
 
    zType = columnType(&sNC, p, 0, 0, 0);
90968
 
#endif
90969
 
    sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
90970
 
  }
90971
 
#endif /* SQLITE_OMIT_DECLTYPE */
90972
 
}
90973
 
 
90974
 
/*
90975
 
** Generate code that will tell the VDBE the names of columns
90976
 
** in the result set.  This information is used to provide the
90977
 
** azCol[] values in the callback.
90978
 
*/
90979
 
static void generateColumnNames(
90980
 
  Parse *pParse,      /* Parser context */
90981
 
  SrcList *pTabList,  /* List of tables */
90982
 
  ExprList *pEList    /* Expressions defining the result set */
90983
 
){
90984
 
  Vdbe *v = pParse->pVdbe;
90985
 
  int i, j;
90986
 
  sqlite3 *db = pParse->db;
90987
 
  int fullNames, shortNames;
90988
 
 
90989
 
#ifndef SQLITE_OMIT_EXPLAIN
90990
 
  /* If this is an EXPLAIN, skip this step */
90991
 
  if( pParse->explain ){
90992
 
    return;
90993
 
  }
90994
 
#endif
90995
 
 
90996
 
  if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return;
90997
 
  pParse->colNamesSet = 1;
90998
 
  fullNames = (db->flags & SQLITE_FullColNames)!=0;
90999
 
  shortNames = (db->flags & SQLITE_ShortColNames)!=0;
91000
 
  sqlite3VdbeSetNumCols(v, pEList->nExpr);
91001
 
  for(i=0; i<pEList->nExpr; i++){
91002
 
    Expr *p;
91003
 
    p = pEList->a[i].pExpr;
91004
 
    if( NEVER(p==0) ) continue;
91005
 
    if( pEList->a[i].zName ){
91006
 
      char *zName = pEList->a[i].zName;
91007
 
      sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
91008
 
    }else if( (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && pTabList ){
91009
 
      Table *pTab;
91010
 
      char *zCol;
91011
 
      int iCol = p->iColumn;
91012
 
      for(j=0; ALWAYS(j<pTabList->nSrc); j++){
91013
 
        if( pTabList->a[j].iCursor==p->iTable ) break;
91014
 
      }
91015
 
      assert( j<pTabList->nSrc );
91016
 
      pTab = pTabList->a[j].pTab;
91017
 
      if( iCol<0 ) iCol = pTab->iPKey;
91018
 
      assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
91019
 
      if( iCol<0 ){
91020
 
        zCol = "rowid";
91021
 
      }else{
91022
 
        zCol = pTab->aCol[iCol].zName;
91023
 
      }
91024
 
      if( !shortNames && !fullNames ){
91025
 
        sqlite3VdbeSetColName(v, i, COLNAME_NAME, 
91026
 
            sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
91027
 
      }else if( fullNames ){
91028
 
        char *zName = 0;
91029
 
        zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
91030
 
        sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
91031
 
      }else{
91032
 
        sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
91033
 
      }
91034
 
    }else{
91035
 
      sqlite3VdbeSetColName(v, i, COLNAME_NAME, 
91036
 
          sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
91037
 
    }
91038
 
  }
91039
 
  generateColumnTypes(pParse, pTabList, pEList);
91040
 
}
91041
 
 
91042
 
/*
91043
 
** Given a an expression list (which is really the list of expressions
91044
 
** that form the result set of a SELECT statement) compute appropriate
91045
 
** column names for a table that would hold the expression list.
91046
 
**
91047
 
** All column names will be unique.
91048
 
**
91049
 
** Only the column names are computed.  Column.zType, Column.zColl,
91050
 
** and other fields of Column are zeroed.
91051
 
**
91052
 
** Return SQLITE_OK on success.  If a memory allocation error occurs,
91053
 
** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
91054
 
*/
91055
 
static int selectColumnsFromExprList(
91056
 
  Parse *pParse,          /* Parsing context */
91057
 
  ExprList *pEList,       /* Expr list from which to derive column names */
91058
 
  int *pnCol,             /* Write the number of columns here */
91059
 
  Column **paCol          /* Write the new column list here */
91060
 
){
91061
 
  sqlite3 *db = pParse->db;   /* Database connection */
91062
 
  int i, j;                   /* Loop counters */
91063
 
  int cnt;                    /* Index added to make the name unique */
91064
 
  Column *aCol, *pCol;        /* For looping over result columns */
91065
 
  int nCol;                   /* Number of columns in the result set */
91066
 
  Expr *p;                    /* Expression for a single result column */
91067
 
  char *zName;                /* Column name */
91068
 
  int nName;                  /* Size of name in zName[] */
91069
 
 
91070
 
  *pnCol = nCol = pEList->nExpr;
91071
 
  aCol = *paCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
91072
 
  if( aCol==0 ) return SQLITE_NOMEM;
91073
 
  for(i=0, pCol=aCol; i<nCol; i++, pCol++){
91074
 
    /* Get an appropriate name for the column
91075
 
    */
91076
 
    p = pEList->a[i].pExpr;
91077
 
    assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
91078
 
               || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
91079
 
    if( (zName = pEList->a[i].zName)!=0 ){
91080
 
      /* If the column contains an "AS <name>" phrase, use <name> as the name */
91081
 
      zName = sqlite3DbStrDup(db, zName);
91082
 
    }else{
91083
 
      Expr *pColExpr = p;  /* The expression that is the result column name */
91084
 
      Table *pTab;         /* Table associated with this expression */
91085
 
      while( pColExpr->op==TK_DOT ) pColExpr = pColExpr->pRight;
91086
 
      if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
91087
 
        /* For columns use the column name name */
91088
 
        int iCol = pColExpr->iColumn;
91089
 
        pTab = pColExpr->pTab;
91090
 
        if( iCol<0 ) iCol = pTab->iPKey;
91091
 
        zName = sqlite3MPrintf(db, "%s",
91092
 
                 iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
91093
 
      }else if( pColExpr->op==TK_ID ){
91094
 
        assert( !ExprHasProperty(pColExpr, EP_IntValue) );
91095
 
        zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
91096
 
      }else{
91097
 
        /* Use the original text of the column expression as its name */
91098
 
        zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
91099
 
      }
91100
 
    }
91101
 
    if( db->mallocFailed ){
91102
 
      sqlite3DbFree(db, zName);
91103
 
      break;
91104
 
    }
91105
 
 
91106
 
    /* Make sure the column name is unique.  If the name is not unique,
91107
 
    ** append a integer to the name so that it becomes unique.
91108
 
    */
91109
 
    nName = sqlite3Strlen30(zName);
91110
 
    for(j=cnt=0; j<i; j++){
91111
 
      if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
91112
 
        char *zNewName;
91113
 
        zName[nName] = 0;
91114
 
        zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
91115
 
        sqlite3DbFree(db, zName);
91116
 
        zName = zNewName;
91117
 
        j = -1;
91118
 
        if( zName==0 ) break;
91119
 
      }
91120
 
    }
91121
 
    pCol->zName = zName;
91122
 
  }
91123
 
  if( db->mallocFailed ){
91124
 
    for(j=0; j<i; j++){
91125
 
      sqlite3DbFree(db, aCol[j].zName);
91126
 
    }
91127
 
    sqlite3DbFree(db, aCol);
91128
 
    *paCol = 0;
91129
 
    *pnCol = 0;
91130
 
    return SQLITE_NOMEM;
91131
 
  }
91132
 
  return SQLITE_OK;
91133
 
}
91134
 
 
91135
 
/*
91136
 
** Add type and collation information to a column list based on
91137
 
** a SELECT statement.
91138
 
** 
91139
 
** The column list presumably came from selectColumnNamesFromExprList().
91140
 
** The column list has only names, not types or collations.  This
91141
 
** routine goes through and adds the types and collations.
91142
 
**
91143
 
** This routine requires that all identifiers in the SELECT
91144
 
** statement be resolved.
91145
 
*/
91146
 
static void selectAddColumnTypeAndCollation(
91147
 
  Parse *pParse,        /* Parsing contexts */
91148
 
  int nCol,             /* Number of columns */
91149
 
  Column *aCol,         /* List of columns */
91150
 
  Select *pSelect       /* SELECT used to determine types and collations */
91151
 
){
91152
 
  sqlite3 *db = pParse->db;
91153
 
  NameContext sNC;
91154
 
  Column *pCol;
91155
 
  CollSeq *pColl;
91156
 
  int i;
91157
 
  Expr *p;
91158
 
  struct ExprList_item *a;
91159
 
 
91160
 
  assert( pSelect!=0 );
91161
 
  assert( (pSelect->selFlags & SF_Resolved)!=0 );
91162
 
  assert( nCol==pSelect->pEList->nExpr || db->mallocFailed );
91163
 
  if( db->mallocFailed ) return;
91164
 
  memset(&sNC, 0, sizeof(sNC));
91165
 
  sNC.pSrcList = pSelect->pSrc;
91166
 
  a = pSelect->pEList->a;
91167
 
  for(i=0, pCol=aCol; i<nCol; i++, pCol++){
91168
 
    p = a[i].pExpr;
91169
 
    pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0));
91170
 
    pCol->affinity = sqlite3ExprAffinity(p);
91171
 
    if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
91172
 
    pColl = sqlite3ExprCollSeq(pParse, p);
91173
 
    if( pColl ){
91174
 
      pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
91175
 
    }
91176
 
  }
91177
 
}
91178
 
 
91179
 
/*
91180
 
** Given a SELECT statement, generate a Table structure that describes
91181
 
** the result set of that SELECT.
91182
 
*/
91183
 
SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
91184
 
  Table *pTab;
91185
 
  sqlite3 *db = pParse->db;
91186
 
  int savedFlags;
91187
 
 
91188
 
  savedFlags = db->flags;
91189
 
  db->flags &= ~SQLITE_FullColNames;
91190
 
  db->flags |= SQLITE_ShortColNames;
91191
 
  sqlite3SelectPrep(pParse, pSelect, 0);
91192
 
  if( pParse->nErr ) return 0;
91193
 
  while( pSelect->pPrior ) pSelect = pSelect->pPrior;
91194
 
  db->flags = savedFlags;
91195
 
  pTab = sqlite3DbMallocZero(db, sizeof(Table) );
91196
 
  if( pTab==0 ){
91197
 
    return 0;
91198
 
  }
91199
 
  /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
91200
 
  ** is disabled */
91201
 
  assert( db->lookaside.bEnabled==0 );
91202
 
  pTab->nRef = 1;
91203
 
  pTab->zName = 0;
91204
 
  pTab->nRowEst = 1000000;
91205
 
  selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
91206
 
  selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSelect);
91207
 
  pTab->iPKey = -1;
91208
 
  if( db->mallocFailed ){
91209
 
    sqlite3DeleteTable(db, pTab);
91210
 
    return 0;
91211
 
  }
91212
 
  return pTab;
91213
 
}
91214
 
 
91215
 
/*
91216
 
** Get a VDBE for the given parser context.  Create a new one if necessary.
91217
 
** If an error occurs, return NULL and leave a message in pParse.
91218
 
*/
91219
 
SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
91220
 
  Vdbe *v = pParse->pVdbe;
91221
 
  if( v==0 ){
91222
 
    v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db);
91223
 
#ifndef SQLITE_OMIT_TRACE
91224
 
    if( v ){
91225
 
      sqlite3VdbeAddOp0(v, OP_Trace);
91226
 
    }
91227
 
#endif
91228
 
  }
91229
 
  return v;
91230
 
}
91231
 
 
91232
 
 
91233
 
/*
91234
 
** Compute the iLimit and iOffset fields of the SELECT based on the
91235
 
** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
91236
 
** that appear in the original SQL statement after the LIMIT and OFFSET
91237
 
** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset 
91238
 
** are the integer memory register numbers for counters used to compute 
91239
 
** the limit and offset.  If there is no limit and/or offset, then 
91240
 
** iLimit and iOffset are negative.
91241
 
**
91242
 
** This routine changes the values of iLimit and iOffset only if
91243
 
** a limit or offset is defined by pLimit and pOffset.  iLimit and
91244
 
** iOffset should have been preset to appropriate default values
91245
 
** (usually but not always -1) prior to calling this routine.
91246
 
** Only if pLimit!=0 or pOffset!=0 do the limit registers get
91247
 
** redefined.  The UNION ALL operator uses this property to force
91248
 
** the reuse of the same limit and offset registers across multiple
91249
 
** SELECT statements.
91250
 
*/
91251
 
static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
91252
 
  Vdbe *v = 0;
91253
 
  int iLimit = 0;
91254
 
  int iOffset;
91255
 
  int addr1, n;
91256
 
  if( p->iLimit ) return;
91257
 
 
91258
 
  /* 
91259
 
  ** "LIMIT -1" always shows all rows.  There is some
91260
 
  ** contraversy about what the correct behavior should be.
91261
 
  ** The current implementation interprets "LIMIT 0" to mean
91262
 
  ** no rows.
91263
 
  */
91264
 
  sqlite3ExprCacheClear(pParse);
91265
 
  assert( p->pOffset==0 || p->pLimit!=0 );
91266
 
  if( p->pLimit ){
91267
 
    p->iLimit = iLimit = ++pParse->nMem;
91268
 
    v = sqlite3GetVdbe(pParse);
91269
 
    if( NEVER(v==0) ) return;  /* VDBE should have already been allocated */
91270
 
    if( sqlite3ExprIsInteger(p->pLimit, &n) ){
91271
 
      sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
91272
 
      VdbeComment((v, "LIMIT counter"));
91273
 
      if( n==0 ){
91274
 
        sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
91275
 
      }else{
91276
 
        if( p->nSelectRow > (double)n ) p->nSelectRow = (double)n;
91277
 
      }
91278
 
    }else{
91279
 
      sqlite3ExprCode(pParse, p->pLimit, iLimit);
91280
 
      sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit);
91281
 
      VdbeComment((v, "LIMIT counter"));
91282
 
      sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak);
91283
 
    }
91284
 
    if( p->pOffset ){
91285
 
      p->iOffset = iOffset = ++pParse->nMem;
91286
 
      pParse->nMem++;   /* Allocate an extra register for limit+offset */
91287
 
      sqlite3ExprCode(pParse, p->pOffset, iOffset);
91288
 
      sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset);
91289
 
      VdbeComment((v, "OFFSET counter"));
91290
 
      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset);
91291
 
      sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
91292
 
      sqlite3VdbeJumpHere(v, addr1);
91293
 
      sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
91294
 
      VdbeComment((v, "LIMIT+OFFSET"));
91295
 
      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit);
91296
 
      sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
91297
 
      sqlite3VdbeJumpHere(v, addr1);
91298
 
    }
91299
 
  }
91300
 
}
91301
 
 
91302
 
#ifndef SQLITE_OMIT_COMPOUND_SELECT
91303
 
/*
91304
 
** Return the appropriate collating sequence for the iCol-th column of
91305
 
** the result set for the compound-select statement "p".  Return NULL if
91306
 
** the column has no default collating sequence.
91307
 
**
91308
 
** The collating sequence for the compound select is taken from the
91309
 
** left-most term of the select that has a collating sequence.
91310
 
*/
91311
 
static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
91312
 
  CollSeq *pRet;
91313
 
  if( p->pPrior ){
91314
 
    pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
91315
 
  }else{
91316
 
    pRet = 0;
91317
 
  }
91318
 
  assert( iCol>=0 );
91319
 
  if( pRet==0 && iCol<p->pEList->nExpr ){
91320
 
    pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
91321
 
  }
91322
 
  return pRet;
91323
 
}
91324
 
#endif /* SQLITE_OMIT_COMPOUND_SELECT */
91325
 
 
91326
 
/* Forward reference */
91327
 
static int multiSelectOrderBy(
91328
 
  Parse *pParse,        /* Parsing context */
91329
 
  Select *p,            /* The right-most of SELECTs to be coded */
91330
 
  SelectDest *pDest     /* What to do with query results */
91331
 
);
91332
 
 
91333
 
 
91334
 
#ifndef SQLITE_OMIT_COMPOUND_SELECT
91335
 
/*
91336
 
** This routine is called to process a compound query form from
91337
 
** two or more separate queries using UNION, UNION ALL, EXCEPT, or
91338
 
** INTERSECT
91339
 
**
91340
 
** "p" points to the right-most of the two queries.  the query on the
91341
 
** left is p->pPrior.  The left query could also be a compound query
91342
 
** in which case this routine will be called recursively. 
91343
 
**
91344
 
** The results of the total query are to be written into a destination
91345
 
** of type eDest with parameter iParm.
91346
 
**
91347
 
** Example 1:  Consider a three-way compound SQL statement.
91348
 
**
91349
 
**     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
91350
 
**
91351
 
** This statement is parsed up as follows:
91352
 
**
91353
 
**     SELECT c FROM t3
91354
 
**      |
91355
 
**      `----->  SELECT b FROM t2
91356
 
**                |
91357
 
**                `------>  SELECT a FROM t1
91358
 
**
91359
 
** The arrows in the diagram above represent the Select.pPrior pointer.
91360
 
** So if this routine is called with p equal to the t3 query, then
91361
 
** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
91362
 
**
91363
 
** Notice that because of the way SQLite parses compound SELECTs, the
91364
 
** individual selects always group from left to right.
91365
 
*/
91366
 
static int multiSelect(
91367
 
  Parse *pParse,        /* Parsing context */
91368
 
  Select *p,            /* The right-most of SELECTs to be coded */
91369
 
  SelectDest *pDest     /* What to do with query results */
91370
 
){
91371
 
  int rc = SQLITE_OK;   /* Success code from a subroutine */
91372
 
  Select *pPrior;       /* Another SELECT immediately to our left */
91373
 
  Vdbe *v;              /* Generate code to this VDBE */
91374
 
  SelectDest dest;      /* Alternative data destination */
91375
 
  Select *pDelete = 0;  /* Chain of simple selects to delete */
91376
 
  sqlite3 *db;          /* Database connection */
91377
 
#ifndef SQLITE_OMIT_EXPLAIN
91378
 
  int iSub1;            /* EQP id of left-hand query */
91379
 
  int iSub2;            /* EQP id of right-hand query */
91380
 
#endif
91381
 
 
91382
 
  /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
91383
 
  ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
91384
 
  */
91385
 
  assert( p && p->pPrior );  /* Calling function guarantees this much */
91386
 
  db = pParse->db;
91387
 
  pPrior = p->pPrior;
91388
 
  assert( pPrior->pRightmost!=pPrior );
91389
 
  assert( pPrior->pRightmost==p->pRightmost );
91390
 
  dest = *pDest;
91391
 
  if( pPrior->pOrderBy ){
91392
 
    sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
91393
 
      selectOpName(p->op));
91394
 
    rc = 1;
91395
 
    goto multi_select_end;
91396
 
  }
91397
 
  if( pPrior->pLimit ){
91398
 
    sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
91399
 
      selectOpName(p->op));
91400
 
    rc = 1;
91401
 
    goto multi_select_end;
91402
 
  }
91403
 
 
91404
 
  v = sqlite3GetVdbe(pParse);
91405
 
  assert( v!=0 );  /* The VDBE already created by calling function */
91406
 
 
91407
 
  /* Create the destination temporary table if necessary
91408
 
  */
91409
 
  if( dest.eDest==SRT_EphemTab ){
91410
 
    assert( p->pEList );
91411
 
    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iParm, p->pEList->nExpr);
91412
 
    sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
91413
 
    dest.eDest = SRT_Table;
91414
 
  }
91415
 
 
91416
 
  /* Make sure all SELECTs in the statement have the same number of elements
91417
 
  ** in their result sets.
91418
 
  */
91419
 
  assert( p->pEList && pPrior->pEList );
91420
 
  if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
91421
 
    sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
91422
 
      " do not have the same number of result columns", selectOpName(p->op));
91423
 
    rc = 1;
91424
 
    goto multi_select_end;
91425
 
  }
91426
 
 
91427
 
  /* Compound SELECTs that have an ORDER BY clause are handled separately.
91428
 
  */
91429
 
  if( p->pOrderBy ){
91430
 
    return multiSelectOrderBy(pParse, p, pDest);
91431
 
  }
91432
 
 
91433
 
  /* Generate code for the left and right SELECT statements.
91434
 
  */
91435
 
  switch( p->op ){
91436
 
    case TK_ALL: {
91437
 
      int addr = 0;
91438
 
      int nLimit;
91439
 
      assert( !pPrior->pLimit );
91440
 
      pPrior->pLimit = p->pLimit;
91441
 
      pPrior->pOffset = p->pOffset;
91442
 
      explainSetInteger(iSub1, pParse->iNextSelectId);
91443
 
      rc = sqlite3Select(pParse, pPrior, &dest);
91444
 
      p->pLimit = 0;
91445
 
      p->pOffset = 0;
91446
 
      if( rc ){
91447
 
        goto multi_select_end;
91448
 
      }
91449
 
      p->pPrior = 0;
91450
 
      p->iLimit = pPrior->iLimit;
91451
 
      p->iOffset = pPrior->iOffset;
91452
 
      if( p->iLimit ){
91453
 
        addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit);
91454
 
        VdbeComment((v, "Jump ahead if LIMIT reached"));
91455
 
      }
91456
 
      explainSetInteger(iSub2, pParse->iNextSelectId);
91457
 
      rc = sqlite3Select(pParse, p, &dest);
91458
 
      testcase( rc!=SQLITE_OK );
91459
 
      pDelete = p->pPrior;
91460
 
      p->pPrior = pPrior;
91461
 
      p->nSelectRow += pPrior->nSelectRow;
91462
 
      if( pPrior->pLimit
91463
 
       && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
91464
 
       && p->nSelectRow > (double)nLimit 
91465
 
      ){
91466
 
        p->nSelectRow = (double)nLimit;
91467
 
      }
91468
 
      if( addr ){
91469
 
        sqlite3VdbeJumpHere(v, addr);
91470
 
      }
91471
 
      break;
91472
 
    }
91473
 
    case TK_EXCEPT:
91474
 
    case TK_UNION: {
91475
 
      int unionTab;    /* Cursor number of the temporary table holding result */
91476
 
      u8 op = 0;       /* One of the SRT_ operations to apply to self */
91477
 
      int priorOp;     /* The SRT_ operation to apply to prior selects */
91478
 
      Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
91479
 
      int addr;
91480
 
      SelectDest uniondest;
91481
 
 
91482
 
      testcase( p->op==TK_EXCEPT );
91483
 
      testcase( p->op==TK_UNION );
91484
 
      priorOp = SRT_Union;
91485
 
      if( dest.eDest==priorOp && ALWAYS(!p->pLimit &&!p->pOffset) ){
91486
 
        /* We can reuse a temporary table generated by a SELECT to our
91487
 
        ** right.
91488
 
        */
91489
 
        assert( p->pRightmost!=p );  /* Can only happen for leftward elements
91490
 
                                     ** of a 3-way or more compound */
91491
 
        assert( p->pLimit==0 );      /* Not allowed on leftward elements */
91492
 
        assert( p->pOffset==0 );     /* Not allowed on leftward elements */
91493
 
        unionTab = dest.iParm;
91494
 
      }else{
91495
 
        /* We will need to create our own temporary table to hold the
91496
 
        ** intermediate results.
91497
 
        */
91498
 
        unionTab = pParse->nTab++;
91499
 
        assert( p->pOrderBy==0 );
91500
 
        addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
91501
 
        assert( p->addrOpenEphm[0] == -1 );
91502
 
        p->addrOpenEphm[0] = addr;
91503
 
        p->pRightmost->selFlags |= SF_UsesEphemeral;
91504
 
        assert( p->pEList );
91505
 
      }
91506
 
 
91507
 
      /* Code the SELECT statements to our left
91508
 
      */
91509
 
      assert( !pPrior->pOrderBy );
91510
 
      sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
91511
 
      explainSetInteger(iSub1, pParse->iNextSelectId);
91512
 
      rc = sqlite3Select(pParse, pPrior, &uniondest);
91513
 
      if( rc ){
91514
 
        goto multi_select_end;
91515
 
      }
91516
 
 
91517
 
      /* Code the current SELECT statement
91518
 
      */
91519
 
      if( p->op==TK_EXCEPT ){
91520
 
        op = SRT_Except;
91521
 
      }else{
91522
 
        assert( p->op==TK_UNION );
91523
 
        op = SRT_Union;
91524
 
      }
91525
 
      p->pPrior = 0;
91526
 
      pLimit = p->pLimit;
91527
 
      p->pLimit = 0;
91528
 
      pOffset = p->pOffset;
91529
 
      p->pOffset = 0;
91530
 
      uniondest.eDest = op;
91531
 
      explainSetInteger(iSub2, pParse->iNextSelectId);
91532
 
      rc = sqlite3Select(pParse, p, &uniondest);
91533
 
      testcase( rc!=SQLITE_OK );
91534
 
      /* Query flattening in sqlite3Select() might refill p->pOrderBy.
91535
 
      ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
91536
 
      sqlite3ExprListDelete(db, p->pOrderBy);
91537
 
      pDelete = p->pPrior;
91538
 
      p->pPrior = pPrior;
91539
 
      p->pOrderBy = 0;
91540
 
      if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow;
91541
 
      sqlite3ExprDelete(db, p->pLimit);
91542
 
      p->pLimit = pLimit;
91543
 
      p->pOffset = pOffset;
91544
 
      p->iLimit = 0;
91545
 
      p->iOffset = 0;
91546
 
 
91547
 
      /* Convert the data in the temporary table into whatever form
91548
 
      ** it is that we currently need.
91549
 
      */
91550
 
      assert( unionTab==dest.iParm || dest.eDest!=priorOp );
91551
 
      if( dest.eDest!=priorOp ){
91552
 
        int iCont, iBreak, iStart;
91553
 
        assert( p->pEList );
91554
 
        if( dest.eDest==SRT_Output ){
91555
 
          Select *pFirst = p;
91556
 
          while( pFirst->pPrior ) pFirst = pFirst->pPrior;
91557
 
          generateColumnNames(pParse, 0, pFirst->pEList);
91558
 
        }
91559
 
        iBreak = sqlite3VdbeMakeLabel(v);
91560
 
        iCont = sqlite3VdbeMakeLabel(v);
91561
 
        computeLimitRegisters(pParse, p, iBreak);
91562
 
        sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
91563
 
        iStart = sqlite3VdbeCurrentAddr(v);
91564
 
        selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
91565
 
                        0, -1, &dest, iCont, iBreak);
91566
 
        sqlite3VdbeResolveLabel(v, iCont);
91567
 
        sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
91568
 
        sqlite3VdbeResolveLabel(v, iBreak);
91569
 
        sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
91570
 
      }
91571
 
      break;
91572
 
    }
91573
 
    default: assert( p->op==TK_INTERSECT ); {
91574
 
      int tab1, tab2;
91575
 
      int iCont, iBreak, iStart;
91576
 
      Expr *pLimit, *pOffset;
91577
 
      int addr;
91578
 
      SelectDest intersectdest;
91579
 
      int r1;
91580
 
 
91581
 
      /* INTERSECT is different from the others since it requires
91582
 
      ** two temporary tables.  Hence it has its own case.  Begin
91583
 
      ** by allocating the tables we will need.
91584
 
      */
91585
 
      tab1 = pParse->nTab++;
91586
 
      tab2 = pParse->nTab++;
91587
 
      assert( p->pOrderBy==0 );
91588
 
 
91589
 
      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
91590
 
      assert( p->addrOpenEphm[0] == -1 );
91591
 
      p->addrOpenEphm[0] = addr;
91592
 
      p->pRightmost->selFlags |= SF_UsesEphemeral;
91593
 
      assert( p->pEList );
91594
 
 
91595
 
      /* Code the SELECTs to our left into temporary table "tab1".
91596
 
      */
91597
 
      sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
91598
 
      explainSetInteger(iSub1, pParse->iNextSelectId);
91599
 
      rc = sqlite3Select(pParse, pPrior, &intersectdest);
91600
 
      if( rc ){
91601
 
        goto multi_select_end;
91602
 
      }
91603
 
 
91604
 
      /* Code the current SELECT into temporary table "tab2"
91605
 
      */
91606
 
      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
91607
 
      assert( p->addrOpenEphm[1] == -1 );
91608
 
      p->addrOpenEphm[1] = addr;
91609
 
      p->pPrior = 0;
91610
 
      pLimit = p->pLimit;
91611
 
      p->pLimit = 0;
91612
 
      pOffset = p->pOffset;
91613
 
      p->pOffset = 0;
91614
 
      intersectdest.iParm = tab2;
91615
 
      explainSetInteger(iSub2, pParse->iNextSelectId);
91616
 
      rc = sqlite3Select(pParse, p, &intersectdest);
91617
 
      testcase( rc!=SQLITE_OK );
91618
 
      pDelete = p->pPrior;
91619
 
      p->pPrior = pPrior;
91620
 
      if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
91621
 
      sqlite3ExprDelete(db, p->pLimit);
91622
 
      p->pLimit = pLimit;
91623
 
      p->pOffset = pOffset;
91624
 
 
91625
 
      /* Generate code to take the intersection of the two temporary
91626
 
      ** tables.
91627
 
      */
91628
 
      assert( p->pEList );
91629
 
      if( dest.eDest==SRT_Output ){
91630
 
        Select *pFirst = p;
91631
 
        while( pFirst->pPrior ) pFirst = pFirst->pPrior;
91632
 
        generateColumnNames(pParse, 0, pFirst->pEList);
91633
 
      }
91634
 
      iBreak = sqlite3VdbeMakeLabel(v);
91635
 
      iCont = sqlite3VdbeMakeLabel(v);
91636
 
      computeLimitRegisters(pParse, p, iBreak);
91637
 
      sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak);
91638
 
      r1 = sqlite3GetTempReg(pParse);
91639
 
      iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
91640
 
      sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
91641
 
      sqlite3ReleaseTempReg(pParse, r1);
91642
 
      selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
91643
 
                      0, -1, &dest, iCont, iBreak);
91644
 
      sqlite3VdbeResolveLabel(v, iCont);
91645
 
      sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
91646
 
      sqlite3VdbeResolveLabel(v, iBreak);
91647
 
      sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
91648
 
      sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
91649
 
      break;
91650
 
    }
91651
 
  }
91652
 
 
91653
 
  explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
91654
 
 
91655
 
  /* Compute collating sequences used by 
91656
 
  ** temporary tables needed to implement the compound select.
91657
 
  ** Attach the KeyInfo structure to all temporary tables.
91658
 
  **
91659
 
  ** This section is run by the right-most SELECT statement only.
91660
 
  ** SELECT statements to the left always skip this part.  The right-most
91661
 
  ** SELECT might also skip this part if it has no ORDER BY clause and
91662
 
  ** no temp tables are required.
91663
 
  */
91664
 
  if( p->selFlags & SF_UsesEphemeral ){
91665
 
    int i;                        /* Loop counter */
91666
 
    KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
91667
 
    Select *pLoop;                /* For looping through SELECT statements */
91668
 
    CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
91669
 
    int nCol;                     /* Number of columns in result set */
91670
 
 
91671
 
    assert( p->pRightmost==p );
91672
 
    nCol = p->pEList->nExpr;
91673
 
    pKeyInfo = sqlite3DbMallocZero(db,
91674
 
                       sizeof(*pKeyInfo)+nCol*(sizeof(CollSeq*) + 1));
91675
 
    if( !pKeyInfo ){
91676
 
      rc = SQLITE_NOMEM;
91677
 
      goto multi_select_end;
91678
 
    }
91679
 
 
91680
 
    pKeyInfo->enc = ENC(db);
91681
 
    pKeyInfo->nField = (u16)nCol;
91682
 
 
91683
 
    for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
91684
 
      *apColl = multiSelectCollSeq(pParse, p, i);
91685
 
      if( 0==*apColl ){
91686
 
        *apColl = db->pDfltColl;
91687
 
      }
91688
 
    }
91689
 
 
91690
 
    for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
91691
 
      for(i=0; i<2; i++){
91692
 
        int addr = pLoop->addrOpenEphm[i];
91693
 
        if( addr<0 ){
91694
 
          /* If [0] is unused then [1] is also unused.  So we can
91695
 
          ** always safely abort as soon as the first unused slot is found */
91696
 
          assert( pLoop->addrOpenEphm[1]<0 );
91697
 
          break;
91698
 
        }
91699
 
        sqlite3VdbeChangeP2(v, addr, nCol);
91700
 
        sqlite3VdbeChangeP4(v, addr, (char*)pKeyInfo, P4_KEYINFO);
91701
 
        pLoop->addrOpenEphm[i] = -1;
91702
 
      }
91703
 
    }
91704
 
    sqlite3DbFree(db, pKeyInfo);
91705
 
  }
91706
 
 
91707
 
multi_select_end:
91708
 
  pDest->iMem = dest.iMem;
91709
 
  pDest->nMem = dest.nMem;
91710
 
  sqlite3SelectDelete(db, pDelete);
91711
 
  return rc;
91712
 
}
91713
 
#endif /* SQLITE_OMIT_COMPOUND_SELECT */
91714
 
 
91715
 
/*
91716
 
** Code an output subroutine for a coroutine implementation of a
91717
 
** SELECT statment.
91718
 
**
91719
 
** The data to be output is contained in pIn->iMem.  There are
91720
 
** pIn->nMem columns to be output.  pDest is where the output should
91721
 
** be sent.
91722
 
**
91723
 
** regReturn is the number of the register holding the subroutine
91724
 
** return address.
91725
 
**
91726
 
** If regPrev>0 then it is the first register in a vector that
91727
 
** records the previous output.  mem[regPrev] is a flag that is false
91728
 
** if there has been no previous output.  If regPrev>0 then code is
91729
 
** generated to suppress duplicates.  pKeyInfo is used for comparing
91730
 
** keys.
91731
 
**
91732
 
** If the LIMIT found in p->iLimit is reached, jump immediately to
91733
 
** iBreak.
91734
 
*/
91735
 
static int generateOutputSubroutine(
91736
 
  Parse *pParse,          /* Parsing context */
91737
 
  Select *p,              /* The SELECT statement */
91738
 
  SelectDest *pIn,        /* Coroutine supplying data */
91739
 
  SelectDest *pDest,      /* Where to send the data */
91740
 
  int regReturn,          /* The return address register */
91741
 
  int regPrev,            /* Previous result register.  No uniqueness if 0 */
91742
 
  KeyInfo *pKeyInfo,      /* For comparing with previous entry */
91743
 
  int p4type,             /* The p4 type for pKeyInfo */
91744
 
  int iBreak              /* Jump here if we hit the LIMIT */
91745
 
){
91746
 
  Vdbe *v = pParse->pVdbe;
91747
 
  int iContinue;
91748
 
  int addr;
91749
 
 
91750
 
  addr = sqlite3VdbeCurrentAddr(v);
91751
 
  iContinue = sqlite3VdbeMakeLabel(v);
91752
 
 
91753
 
  /* Suppress duplicates for UNION, EXCEPT, and INTERSECT 
91754
 
  */
91755
 
  if( regPrev ){
91756
 
    int j1, j2;
91757
 
    j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
91758
 
    j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iMem, regPrev+1, pIn->nMem,
91759
 
                              (char*)pKeyInfo, p4type);
91760
 
    sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
91761
 
    sqlite3VdbeJumpHere(v, j1);
91762
 
    sqlite3ExprCodeCopy(pParse, pIn->iMem, regPrev+1, pIn->nMem);
91763
 
    sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
91764
 
  }
91765
 
  if( pParse->db->mallocFailed ) return 0;
91766
 
 
91767
 
  /* Suppress the the first OFFSET entries if there is an OFFSET clause
91768
 
  */
91769
 
  codeOffset(v, p, iContinue);
91770
 
 
91771
 
  switch( pDest->eDest ){
91772
 
    /* Store the result as data using a unique key.
91773
 
    */
91774
 
    case SRT_Table:
91775
 
    case SRT_EphemTab: {
91776
 
      int r1 = sqlite3GetTempReg(pParse);
91777
 
      int r2 = sqlite3GetTempReg(pParse);
91778
 
      testcase( pDest->eDest==SRT_Table );
91779
 
      testcase( pDest->eDest==SRT_EphemTab );
91780
 
      sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iMem, pIn->nMem, r1);
91781
 
      sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iParm, r2);
91782
 
      sqlite3VdbeAddOp3(v, OP_Insert, pDest->iParm, r1, r2);
91783
 
      sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
91784
 
      sqlite3ReleaseTempReg(pParse, r2);
91785
 
      sqlite3ReleaseTempReg(pParse, r1);
91786
 
      break;
91787
 
    }
91788
 
 
91789
 
#ifndef SQLITE_OMIT_SUBQUERY
91790
 
    /* If we are creating a set for an "expr IN (SELECT ...)" construct,
91791
 
    ** then there should be a single item on the stack.  Write this
91792
 
    ** item into the set table with bogus data.
91793
 
    */
91794
 
    case SRT_Set: {
91795
 
      int r1;
91796
 
      assert( pIn->nMem==1 );
91797
 
      p->affinity = 
91798
 
         sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affinity);
91799
 
      r1 = sqlite3GetTempReg(pParse);
91800
 
      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iMem, 1, r1, &p->affinity, 1);
91801
 
      sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, 1);
91802
 
      sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iParm, r1);
91803
 
      sqlite3ReleaseTempReg(pParse, r1);
91804
 
      break;
91805
 
    }
91806
 
 
91807
 
#if 0  /* Never occurs on an ORDER BY query */
91808
 
    /* If any row exist in the result set, record that fact and abort.
91809
 
    */
91810
 
    case SRT_Exists: {
91811
 
      sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iParm);
91812
 
      /* The LIMIT clause will terminate the loop for us */
91813
 
      break;
91814
 
    }
91815
 
#endif
91816
 
 
91817
 
    /* If this is a scalar select that is part of an expression, then
91818
 
    ** store the results in the appropriate memory cell and break out
91819
 
    ** of the scan loop.
91820
 
    */
91821
 
    case SRT_Mem: {
91822
 
      assert( pIn->nMem==1 );
91823
 
      sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iParm, 1);
91824
 
      /* The LIMIT clause will jump out of the loop for us */
91825
 
      break;
91826
 
    }
91827
 
#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
91828
 
 
91829
 
    /* The results are stored in a sequence of registers
91830
 
    ** starting at pDest->iMem.  Then the co-routine yields.
91831
 
    */
91832
 
    case SRT_Coroutine: {
91833
 
      if( pDest->iMem==0 ){
91834
 
        pDest->iMem = sqlite3GetTempRange(pParse, pIn->nMem);
91835
 
        pDest->nMem = pIn->nMem;
91836
 
      }
91837
 
      sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iMem, pDest->nMem);
91838
 
      sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
91839
 
      break;
91840
 
    }
91841
 
 
91842
 
    /* If none of the above, then the result destination must be
91843
 
    ** SRT_Output.  This routine is never called with any other
91844
 
    ** destination other than the ones handled above or SRT_Output.
91845
 
    **
91846
 
    ** For SRT_Output, results are stored in a sequence of registers.  
91847
 
    ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
91848
 
    ** return the next row of result.
91849
 
    */
91850
 
    default: {
91851
 
      assert( pDest->eDest==SRT_Output );
91852
 
      sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iMem, pIn->nMem);
91853
 
      sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, pIn->nMem);
91854
 
      break;
91855
 
    }
91856
 
  }
91857
 
 
91858
 
  /* Jump to the end of the loop if the LIMIT is reached.
91859
 
  */
91860
 
  if( p->iLimit ){
91861
 
    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
91862
 
  }
91863
 
 
91864
 
  /* Generate the subroutine return
91865
 
  */
91866
 
  sqlite3VdbeResolveLabel(v, iContinue);
91867
 
  sqlite3VdbeAddOp1(v, OP_Return, regReturn);
91868
 
 
91869
 
  return addr;
91870
 
}
91871
 
 
91872
 
/*
91873
 
** Alternative compound select code generator for cases when there
91874
 
** is an ORDER BY clause.
91875
 
**
91876
 
** We assume a query of the following form:
91877
 
**
91878
 
**      <selectA>  <operator>  <selectB>  ORDER BY <orderbylist>
91879
 
**
91880
 
** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT.  The idea
91881
 
** is to code both <selectA> and <selectB> with the ORDER BY clause as
91882
 
** co-routines.  Then run the co-routines in parallel and merge the results
91883
 
** into the output.  In addition to the two coroutines (called selectA and
91884
 
** selectB) there are 7 subroutines:
91885
 
**
91886
 
**    outA:    Move the output of the selectA coroutine into the output
91887
 
**             of the compound query.
91888
 
**
91889
 
**    outB:    Move the output of the selectB coroutine into the output
91890
 
**             of the compound query.  (Only generated for UNION and
91891
 
**             UNION ALL.  EXCEPT and INSERTSECT never output a row that
91892
 
**             appears only in B.)
91893
 
**
91894
 
**    AltB:    Called when there is data from both coroutines and A<B.
91895
 
**
91896
 
**    AeqB:    Called when there is data from both coroutines and A==B.
91897
 
**
91898
 
**    AgtB:    Called when there is data from both coroutines and A>B.
91899
 
**
91900
 
**    EofA:    Called when data is exhausted from selectA.
91901
 
**
91902
 
**    EofB:    Called when data is exhausted from selectB.
91903
 
**
91904
 
** The implementation of the latter five subroutines depend on which 
91905
 
** <operator> is used:
91906
 
**
91907
 
**
91908
 
**             UNION ALL         UNION            EXCEPT          INTERSECT
91909
 
**          -------------  -----------------  --------------  -----------------
91910
 
**   AltB:   outA, nextA      outA, nextA       outA, nextA         nextA
91911
 
**
91912
 
**   AeqB:   outA, nextA         nextA             nextA         outA, nextA
91913
 
**
91914
 
**   AgtB:   outB, nextB      outB, nextB          nextB            nextB
91915
 
**
91916
 
**   EofA:   outB, nextB      outB, nextB          halt             halt
91917
 
**
91918
 
**   EofB:   outA, nextA      outA, nextA       outA, nextA         halt
91919
 
**
91920
 
** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
91921
 
** causes an immediate jump to EofA and an EOF on B following nextB causes
91922
 
** an immediate jump to EofB.  Within EofA and EofB, and EOF on entry or
91923
 
** following nextX causes a jump to the end of the select processing.
91924
 
**
91925
 
** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
91926
 
** within the output subroutine.  The regPrev register set holds the previously
91927
 
** output value.  A comparison is made against this value and the output
91928
 
** is skipped if the next results would be the same as the previous.
91929
 
**
91930
 
** The implementation plan is to implement the two coroutines and seven
91931
 
** subroutines first, then put the control logic at the bottom.  Like this:
91932
 
**
91933
 
**          goto Init
91934
 
**     coA: coroutine for left query (A)
91935
 
**     coB: coroutine for right query (B)
91936
 
**    outA: output one row of A
91937
 
**    outB: output one row of B (UNION and UNION ALL only)
91938
 
**    EofA: ...
91939
 
**    EofB: ...
91940
 
**    AltB: ...
91941
 
**    AeqB: ...
91942
 
**    AgtB: ...
91943
 
**    Init: initialize coroutine registers
91944
 
**          yield coA
91945
 
**          if eof(A) goto EofA
91946
 
**          yield coB
91947
 
**          if eof(B) goto EofB
91948
 
**    Cmpr: Compare A, B
91949
 
**          Jump AltB, AeqB, AgtB
91950
 
**     End: ...
91951
 
**
91952
 
** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
91953
 
** actually called using Gosub and they do not Return.  EofA and EofB loop
91954
 
** until all data is exhausted then jump to the "end" labe.  AltB, AeqB,
91955
 
** and AgtB jump to either L2 or to one of EofA or EofB.
91956
 
*/
91957
 
#ifndef SQLITE_OMIT_COMPOUND_SELECT
91958
 
static int multiSelectOrderBy(
91959
 
  Parse *pParse,        /* Parsing context */
91960
 
  Select *p,            /* The right-most of SELECTs to be coded */
91961
 
  SelectDest *pDest     /* What to do with query results */
91962
 
){
91963
 
  int i, j;             /* Loop counters */
91964
 
  Select *pPrior;       /* Another SELECT immediately to our left */
91965
 
  Vdbe *v;              /* Generate code to this VDBE */
91966
 
  SelectDest destA;     /* Destination for coroutine A */
91967
 
  SelectDest destB;     /* Destination for coroutine B */
91968
 
  int regAddrA;         /* Address register for select-A coroutine */
91969
 
  int regEofA;          /* Flag to indicate when select-A is complete */
91970
 
  int regAddrB;         /* Address register for select-B coroutine */
91971
 
  int regEofB;          /* Flag to indicate when select-B is complete */
91972
 
  int addrSelectA;      /* Address of the select-A coroutine */
91973
 
  int addrSelectB;      /* Address of the select-B coroutine */
91974
 
  int regOutA;          /* Address register for the output-A subroutine */
91975
 
  int regOutB;          /* Address register for the output-B subroutine */
91976
 
  int addrOutA;         /* Address of the output-A subroutine */
91977
 
  int addrOutB = 0;     /* Address of the output-B subroutine */
91978
 
  int addrEofA;         /* Address of the select-A-exhausted subroutine */
91979
 
  int addrEofB;         /* Address of the select-B-exhausted subroutine */
91980
 
  int addrAltB;         /* Address of the A<B subroutine */
91981
 
  int addrAeqB;         /* Address of the A==B subroutine */
91982
 
  int addrAgtB;         /* Address of the A>B subroutine */
91983
 
  int regLimitA;        /* Limit register for select-A */
91984
 
  int regLimitB;        /* Limit register for select-A */
91985
 
  int regPrev;          /* A range of registers to hold previous output */
91986
 
  int savedLimit;       /* Saved value of p->iLimit */
91987
 
  int savedOffset;      /* Saved value of p->iOffset */
91988
 
  int labelCmpr;        /* Label for the start of the merge algorithm */
91989
 
  int labelEnd;         /* Label for the end of the overall SELECT stmt */
91990
 
  int j1;               /* Jump instructions that get retargetted */
91991
 
  int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
91992
 
  KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
91993
 
  KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
91994
 
  sqlite3 *db;          /* Database connection */
91995
 
  ExprList *pOrderBy;   /* The ORDER BY clause */
91996
 
  int nOrderBy;         /* Number of terms in the ORDER BY clause */
91997
 
  int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
91998
 
#ifndef SQLITE_OMIT_EXPLAIN
91999
 
  int iSub1;            /* EQP id of left-hand query */
92000
 
  int iSub2;            /* EQP id of right-hand query */
92001
 
#endif
92002
 
 
92003
 
  assert( p->pOrderBy!=0 );
92004
 
  assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
92005
 
  db = pParse->db;
92006
 
  v = pParse->pVdbe;
92007
 
  assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
92008
 
  labelEnd = sqlite3VdbeMakeLabel(v);
92009
 
  labelCmpr = sqlite3VdbeMakeLabel(v);
92010
 
 
92011
 
 
92012
 
  /* Patch up the ORDER BY clause
92013
 
  */
92014
 
  op = p->op;  
92015
 
  pPrior = p->pPrior;
92016
 
  assert( pPrior->pOrderBy==0 );
92017
 
  pOrderBy = p->pOrderBy;
92018
 
  assert( pOrderBy );
92019
 
  nOrderBy = pOrderBy->nExpr;
92020
 
 
92021
 
  /* For operators other than UNION ALL we have to make sure that
92022
 
  ** the ORDER BY clause covers every term of the result set.  Add
92023
 
  ** terms to the ORDER BY clause as necessary.
92024
 
  */
92025
 
  if( op!=TK_ALL ){
92026
 
    for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
92027
 
      struct ExprList_item *pItem;
92028
 
      for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
92029
 
        assert( pItem->iCol>0 );
92030
 
        if( pItem->iCol==i ) break;
92031
 
      }
92032
 
      if( j==nOrderBy ){
92033
 
        Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
92034
 
        if( pNew==0 ) return SQLITE_NOMEM;
92035
 
        pNew->flags |= EP_IntValue;
92036
 
        pNew->u.iValue = i;
92037
 
        pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
92038
 
        pOrderBy->a[nOrderBy++].iCol = (u16)i;
92039
 
      }
92040
 
    }
92041
 
  }
92042
 
 
92043
 
  /* Compute the comparison permutation and keyinfo that is used with
92044
 
  ** the permutation used to determine if the next
92045
 
  ** row of results comes from selectA or selectB.  Also add explicit
92046
 
  ** collations to the ORDER BY clause terms so that when the subqueries
92047
 
  ** to the right and the left are evaluated, they use the correct
92048
 
  ** collation.
92049
 
  */
92050
 
  aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
92051
 
  if( aPermute ){
92052
 
    struct ExprList_item *pItem;
92053
 
    for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
92054
 
      assert( pItem->iCol>0  && pItem->iCol<=p->pEList->nExpr );
92055
 
      aPermute[i] = pItem->iCol - 1;
92056
 
    }
92057
 
    pKeyMerge =
92058
 
      sqlite3DbMallocRaw(db, sizeof(*pKeyMerge)+nOrderBy*(sizeof(CollSeq*)+1));
92059
 
    if( pKeyMerge ){
92060
 
      pKeyMerge->aSortOrder = (u8*)&pKeyMerge->aColl[nOrderBy];
92061
 
      pKeyMerge->nField = (u16)nOrderBy;
92062
 
      pKeyMerge->enc = ENC(db);
92063
 
      for(i=0; i<nOrderBy; i++){
92064
 
        CollSeq *pColl;
92065
 
        Expr *pTerm = pOrderBy->a[i].pExpr;
92066
 
        if( pTerm->flags & EP_ExpCollate ){
92067
 
          pColl = pTerm->pColl;
92068
 
        }else{
92069
 
          pColl = multiSelectCollSeq(pParse, p, aPermute[i]);
92070
 
          pTerm->flags |= EP_ExpCollate;
92071
 
          pTerm->pColl = pColl;
92072
 
        }
92073
 
        pKeyMerge->aColl[i] = pColl;
92074
 
        pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder;
92075
 
      }
92076
 
    }
92077
 
  }else{
92078
 
    pKeyMerge = 0;
92079
 
  }
92080
 
 
92081
 
  /* Reattach the ORDER BY clause to the query.
92082
 
  */
92083
 
  p->pOrderBy = pOrderBy;
92084
 
  pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
92085
 
 
92086
 
  /* Allocate a range of temporary registers and the KeyInfo needed
92087
 
  ** for the logic that removes duplicate result rows when the
92088
 
  ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
92089
 
  */
92090
 
  if( op==TK_ALL ){
92091
 
    regPrev = 0;
92092
 
  }else{
92093
 
    int nExpr = p->pEList->nExpr;
92094
 
    assert( nOrderBy>=nExpr || db->mallocFailed );
92095
 
    regPrev = sqlite3GetTempRange(pParse, nExpr+1);
92096
 
    sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
92097
 
    pKeyDup = sqlite3DbMallocZero(db,
92098
 
                  sizeof(*pKeyDup) + nExpr*(sizeof(CollSeq*)+1) );
92099
 
    if( pKeyDup ){
92100
 
      pKeyDup->aSortOrder = (u8*)&pKeyDup->aColl[nExpr];
92101
 
      pKeyDup->nField = (u16)nExpr;
92102
 
      pKeyDup->enc = ENC(db);
92103
 
      for(i=0; i<nExpr; i++){
92104
 
        pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
92105
 
        pKeyDup->aSortOrder[i] = 0;
92106
 
      }
92107
 
    }
92108
 
  }
92109
 
 
92110
 
  /* Separate the left and the right query from one another
92111
 
  */
92112
 
  p->pPrior = 0;
92113
 
  sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
92114
 
  if( pPrior->pPrior==0 ){
92115
 
    sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
92116
 
  }
92117
 
 
92118
 
  /* Compute the limit registers */
92119
 
  computeLimitRegisters(pParse, p, labelEnd);
92120
 
  if( p->iLimit && op==TK_ALL ){
92121
 
    regLimitA = ++pParse->nMem;
92122
 
    regLimitB = ++pParse->nMem;
92123
 
    sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
92124
 
                                  regLimitA);
92125
 
    sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
92126
 
  }else{
92127
 
    regLimitA = regLimitB = 0;
92128
 
  }
92129
 
  sqlite3ExprDelete(db, p->pLimit);
92130
 
  p->pLimit = 0;
92131
 
  sqlite3ExprDelete(db, p->pOffset);
92132
 
  p->pOffset = 0;
92133
 
 
92134
 
  regAddrA = ++pParse->nMem;
92135
 
  regEofA = ++pParse->nMem;
92136
 
  regAddrB = ++pParse->nMem;
92137
 
  regEofB = ++pParse->nMem;
92138
 
  regOutA = ++pParse->nMem;
92139
 
  regOutB = ++pParse->nMem;
92140
 
  sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
92141
 
  sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
92142
 
 
92143
 
  /* Jump past the various subroutines and coroutines to the main
92144
 
  ** merge loop
92145
 
  */
92146
 
  j1 = sqlite3VdbeAddOp0(v, OP_Goto);
92147
 
  addrSelectA = sqlite3VdbeCurrentAddr(v);
92148
 
 
92149
 
 
92150
 
  /* Generate a coroutine to evaluate the SELECT statement to the
92151
 
  ** left of the compound operator - the "A" select.
92152
 
  */
92153
 
  VdbeNoopComment((v, "Begin coroutine for left SELECT"));
92154
 
  pPrior->iLimit = regLimitA;
92155
 
  explainSetInteger(iSub1, pParse->iNextSelectId);
92156
 
  sqlite3Select(pParse, pPrior, &destA);
92157
 
  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofA);
92158
 
  sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
92159
 
  VdbeNoopComment((v, "End coroutine for left SELECT"));
92160
 
 
92161
 
  /* Generate a coroutine to evaluate the SELECT statement on 
92162
 
  ** the right - the "B" select
92163
 
  */
92164
 
  addrSelectB = sqlite3VdbeCurrentAddr(v);
92165
 
  VdbeNoopComment((v, "Begin coroutine for right SELECT"));
92166
 
  savedLimit = p->iLimit;
92167
 
  savedOffset = p->iOffset;
92168
 
  p->iLimit = regLimitB;
92169
 
  p->iOffset = 0;  
92170
 
  explainSetInteger(iSub2, pParse->iNextSelectId);
92171
 
  sqlite3Select(pParse, p, &destB);
92172
 
  p->iLimit = savedLimit;
92173
 
  p->iOffset = savedOffset;
92174
 
  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofB);
92175
 
  sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
92176
 
  VdbeNoopComment((v, "End coroutine for right SELECT"));
92177
 
 
92178
 
  /* Generate a subroutine that outputs the current row of the A
92179
 
  ** select as the next output row of the compound select.
92180
 
  */
92181
 
  VdbeNoopComment((v, "Output routine for A"));
92182
 
  addrOutA = generateOutputSubroutine(pParse,
92183
 
                 p, &destA, pDest, regOutA,
92184
 
                 regPrev, pKeyDup, P4_KEYINFO_HANDOFF, labelEnd);
92185
 
  
92186
 
  /* Generate a subroutine that outputs the current row of the B
92187
 
  ** select as the next output row of the compound select.
92188
 
  */
92189
 
  if( op==TK_ALL || op==TK_UNION ){
92190
 
    VdbeNoopComment((v, "Output routine for B"));
92191
 
    addrOutB = generateOutputSubroutine(pParse,
92192
 
                 p, &destB, pDest, regOutB,
92193
 
                 regPrev, pKeyDup, P4_KEYINFO_STATIC, labelEnd);
92194
 
  }
92195
 
 
92196
 
  /* Generate a subroutine to run when the results from select A
92197
 
  ** are exhausted and only data in select B remains.
92198
 
  */
92199
 
  VdbeNoopComment((v, "eof-A subroutine"));
92200
 
  if( op==TK_EXCEPT || op==TK_INTERSECT ){
92201
 
    addrEofA = sqlite3VdbeAddOp2(v, OP_Goto, 0, labelEnd);
92202
 
  }else{  
92203
 
    addrEofA = sqlite3VdbeAddOp2(v, OP_If, regEofB, labelEnd);
92204
 
    sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
92205
 
    sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
92206
 
    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
92207
 
    p->nSelectRow += pPrior->nSelectRow;
92208
 
  }
92209
 
 
92210
 
  /* Generate a subroutine to run when the results from select B
92211
 
  ** are exhausted and only data in select A remains.
92212
 
  */
92213
 
  if( op==TK_INTERSECT ){
92214
 
    addrEofB = addrEofA;
92215
 
    if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
92216
 
  }else{  
92217
 
    VdbeNoopComment((v, "eof-B subroutine"));
92218
 
    addrEofB = sqlite3VdbeAddOp2(v, OP_If, regEofA, labelEnd);
92219
 
    sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
92220
 
    sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
92221
 
    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
92222
 
  }
92223
 
 
92224
 
  /* Generate code to handle the case of A<B
92225
 
  */
92226
 
  VdbeNoopComment((v, "A-lt-B subroutine"));
92227
 
  addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
92228
 
  sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
92229
 
  sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
92230
 
  sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
92231
 
 
92232
 
  /* Generate code to handle the case of A==B
92233
 
  */
92234
 
  if( op==TK_ALL ){
92235
 
    addrAeqB = addrAltB;
92236
 
  }else if( op==TK_INTERSECT ){
92237
 
    addrAeqB = addrAltB;
92238
 
    addrAltB++;
92239
 
  }else{
92240
 
    VdbeNoopComment((v, "A-eq-B subroutine"));
92241
 
    addrAeqB =
92242
 
    sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
92243
 
    sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
92244
 
    sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
92245
 
  }
92246
 
 
92247
 
  /* Generate code to handle the case of A>B
92248
 
  */
92249
 
  VdbeNoopComment((v, "A-gt-B subroutine"));
92250
 
  addrAgtB = sqlite3VdbeCurrentAddr(v);
92251
 
  if( op==TK_ALL || op==TK_UNION ){
92252
 
    sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
92253
 
  }
92254
 
  sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
92255
 
  sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
92256
 
  sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
92257
 
 
92258
 
  /* This code runs once to initialize everything.
92259
 
  */
92260
 
  sqlite3VdbeJumpHere(v, j1);
92261
 
  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofA);
92262
 
  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofB);
92263
 
  sqlite3VdbeAddOp2(v, OP_Gosub, regAddrA, addrSelectA);
92264
 
  sqlite3VdbeAddOp2(v, OP_Gosub, regAddrB, addrSelectB);
92265
 
  sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
92266
 
  sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
92267
 
 
92268
 
  /* Implement the main merge loop
92269
 
  */
92270
 
  sqlite3VdbeResolveLabel(v, labelCmpr);
92271
 
  sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
92272
 
  sqlite3VdbeAddOp4(v, OP_Compare, destA.iMem, destB.iMem, nOrderBy,
92273
 
                         (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
92274
 
  sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
92275
 
 
92276
 
  /* Release temporary registers
92277
 
  */
92278
 
  if( regPrev ){
92279
 
    sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1);
92280
 
  }
92281
 
 
92282
 
  /* Jump to the this point in order to terminate the query.
92283
 
  */
92284
 
  sqlite3VdbeResolveLabel(v, labelEnd);
92285
 
 
92286
 
  /* Set the number of output columns
92287
 
  */
92288
 
  if( pDest->eDest==SRT_Output ){
92289
 
    Select *pFirst = pPrior;
92290
 
    while( pFirst->pPrior ) pFirst = pFirst->pPrior;
92291
 
    generateColumnNames(pParse, 0, pFirst->pEList);
92292
 
  }
92293
 
 
92294
 
  /* Reassembly the compound query so that it will be freed correctly
92295
 
  ** by the calling function */
92296
 
  if( p->pPrior ){
92297
 
    sqlite3SelectDelete(db, p->pPrior);
92298
 
  }
92299
 
  p->pPrior = pPrior;
92300
 
 
92301
 
  /*** TBD:  Insert subroutine calls to close cursors on incomplete
92302
 
  **** subqueries ****/
92303
 
  explainComposite(pParse, p->op, iSub1, iSub2, 0);
92304
 
  return SQLITE_OK;
92305
 
}
92306
 
#endif
92307
 
 
92308
 
#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
92309
 
/* Forward Declarations */
92310
 
static void substExprList(sqlite3*, ExprList*, int, ExprList*);
92311
 
static void substSelect(sqlite3*, Select *, int, ExprList *);
92312
 
 
92313
 
/*
92314
 
** Scan through the expression pExpr.  Replace every reference to
92315
 
** a column in table number iTable with a copy of the iColumn-th
92316
 
** entry in pEList.  (But leave references to the ROWID column 
92317
 
** unchanged.)
92318
 
**
92319
 
** This routine is part of the flattening procedure.  A subquery
92320
 
** whose result set is defined by pEList appears as entry in the
92321
 
** FROM clause of a SELECT such that the VDBE cursor assigned to that
92322
 
** FORM clause entry is iTable.  This routine make the necessary 
92323
 
** changes to pExpr so that it refers directly to the source table
92324
 
** of the subquery rather the result set of the subquery.
92325
 
*/
92326
 
static Expr *substExpr(
92327
 
  sqlite3 *db,        /* Report malloc errors to this connection */
92328
 
  Expr *pExpr,        /* Expr in which substitution occurs */
92329
 
  int iTable,         /* Table to be substituted */
92330
 
  ExprList *pEList    /* Substitute expressions */
92331
 
){
92332
 
  if( pExpr==0 ) return 0;
92333
 
  if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
92334
 
    if( pExpr->iColumn<0 ){
92335
 
      pExpr->op = TK_NULL;
92336
 
    }else{
92337
 
      Expr *pNew;
92338
 
      assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
92339
 
      assert( pExpr->pLeft==0 && pExpr->pRight==0 );
92340
 
      pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
92341
 
      if( pNew && pExpr->pColl ){
92342
 
        pNew->pColl = pExpr->pColl;
92343
 
      }
92344
 
      sqlite3ExprDelete(db, pExpr);
92345
 
      pExpr = pNew;
92346
 
    }
92347
 
  }else{
92348
 
    pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
92349
 
    pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
92350
 
    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
92351
 
      substSelect(db, pExpr->x.pSelect, iTable, pEList);
92352
 
    }else{
92353
 
      substExprList(db, pExpr->x.pList, iTable, pEList);
92354
 
    }
92355
 
  }
92356
 
  return pExpr;
92357
 
}
92358
 
static void substExprList(
92359
 
  sqlite3 *db,         /* Report malloc errors here */
92360
 
  ExprList *pList,     /* List to scan and in which to make substitutes */
92361
 
  int iTable,          /* Table to be substituted */
92362
 
  ExprList *pEList     /* Substitute values */
92363
 
){
92364
 
  int i;
92365
 
  if( pList==0 ) return;
92366
 
  for(i=0; i<pList->nExpr; i++){
92367
 
    pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
92368
 
  }
92369
 
}
92370
 
static void substSelect(
92371
 
  sqlite3 *db,         /* Report malloc errors here */
92372
 
  Select *p,           /* SELECT statement in which to make substitutions */
92373
 
  int iTable,          /* Table to be replaced */
92374
 
  ExprList *pEList     /* Substitute values */
92375
 
){
92376
 
  SrcList *pSrc;
92377
 
  struct SrcList_item *pItem;
92378
 
  int i;
92379
 
  if( !p ) return;
92380
 
  substExprList(db, p->pEList, iTable, pEList);
92381
 
  substExprList(db, p->pGroupBy, iTable, pEList);
92382
 
  substExprList(db, p->pOrderBy, iTable, pEList);
92383
 
  p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
92384
 
  p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
92385
 
  substSelect(db, p->pPrior, iTable, pEList);
92386
 
  pSrc = p->pSrc;
92387
 
  assert( pSrc );  /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */
92388
 
  if( ALWAYS(pSrc) ){
92389
 
    for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
92390
 
      substSelect(db, pItem->pSelect, iTable, pEList);
92391
 
    }
92392
 
  }
92393
 
}
92394
 
#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
92395
 
 
92396
 
#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
92397
 
/*
92398
 
** This routine attempts to flatten subqueries in order to speed
92399
 
** execution.  It returns 1 if it makes changes and 0 if no flattening
92400
 
** occurs.
92401
 
**
92402
 
** To understand the concept of flattening, consider the following
92403
 
** query:
92404
 
**
92405
 
**     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
92406
 
**
92407
 
** The default way of implementing this query is to execute the
92408
 
** subquery first and store the results in a temporary table, then
92409
 
** run the outer query on that temporary table.  This requires two
92410
 
** passes over the data.  Furthermore, because the temporary table
92411
 
** has no indices, the WHERE clause on the outer query cannot be
92412
 
** optimized.
92413
 
**
92414
 
** This routine attempts to rewrite queries such as the above into
92415
 
** a single flat select, like this:
92416
 
**
92417
 
**     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
92418
 
**
92419
 
** The code generated for this simpification gives the same result
92420
 
** but only has to scan the data once.  And because indices might 
92421
 
** exist on the table t1, a complete scan of the data might be
92422
 
** avoided.
92423
 
**
92424
 
** Flattening is only attempted if all of the following are true:
92425
 
**
92426
 
**   (1)  The subquery and the outer query do not both use aggregates.
92427
 
**
92428
 
**   (2)  The subquery is not an aggregate or the outer query is not a join.
92429
 
**
92430
 
**   (3)  The subquery is not the right operand of a left outer join
92431
 
**        (Originally ticket #306.  Strengthened by ticket #3300)
92432
 
**
92433
 
**   (4)  The subquery is not DISTINCT.
92434
 
**
92435
 
**  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
92436
 
**        sub-queries that were excluded from this optimization. Restriction 
92437
 
**        (4) has since been expanded to exclude all DISTINCT subqueries.
92438
 
**
92439
 
**   (6)  The subquery does not use aggregates or the outer query is not
92440
 
**        DISTINCT.
92441
 
**
92442
 
**   (7)  The subquery has a FROM clause.
92443
 
**
92444
 
**   (8)  The subquery does not use LIMIT or the outer query is not a join.
92445
 
**
92446
 
**   (9)  The subquery does not use LIMIT or the outer query does not use
92447
 
**        aggregates.
92448
 
**
92449
 
**  (10)  The subquery does not use aggregates or the outer query does not
92450
 
**        use LIMIT.
92451
 
**
92452
 
**  (11)  The subquery and the outer query do not both have ORDER BY clauses.
92453
 
**
92454
 
**  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
92455
 
**        a separate restriction deriving from ticket #350.
92456
 
**
92457
 
**  (13)  The subquery and outer query do not both use LIMIT.
92458
 
**
92459
 
**  (14)  The subquery does not use OFFSET.
92460
 
**
92461
 
**  (15)  The outer query is not part of a compound select or the
92462
 
**        subquery does not have a LIMIT clause.
92463
 
**        (See ticket #2339 and ticket [02a8e81d44]).
92464
 
**
92465
 
**  (16)  The outer query is not an aggregate or the subquery does
92466
 
**        not contain ORDER BY.  (Ticket #2942)  This used to not matter
92467
 
**        until we introduced the group_concat() function.  
92468
 
**
92469
 
**  (17)  The sub-query is not a compound select, or it is a UNION ALL 
92470
 
**        compound clause made up entirely of non-aggregate queries, and 
92471
 
**        the parent query:
92472
 
**
92473
 
**          * is not itself part of a compound select,
92474
 
**          * is not an aggregate or DISTINCT query, and
92475
 
**          * has no other tables or sub-selects in the FROM clause.
92476
 
**
92477
 
**        The parent and sub-query may contain WHERE clauses. Subject to
92478
 
**        rules (11), (13) and (14), they may also contain ORDER BY,
92479
 
**        LIMIT and OFFSET clauses.
92480
 
**
92481
 
**  (18)  If the sub-query is a compound select, then all terms of the
92482
 
**        ORDER by clause of the parent must be simple references to 
92483
 
**        columns of the sub-query.
92484
 
**
92485
 
**  (19)  The subquery does not use LIMIT or the outer query does not
92486
 
**        have a WHERE clause.
92487
 
**
92488
 
**  (20)  If the sub-query is a compound select, then it must not use
92489
 
**        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
92490
 
**        somewhat by saying that the terms of the ORDER BY clause must
92491
 
**        appear as unmodified result columns in the outer query.  But
92492
 
**        have other optimizations in mind to deal with that case.
92493
 
**
92494
 
**  (21)  The subquery does not use LIMIT or the outer query is not
92495
 
**        DISTINCT.  (See ticket [752e1646fc]).
92496
 
**
92497
 
** In this routine, the "p" parameter is a pointer to the outer query.
92498
 
** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
92499
 
** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
92500
 
**
92501
 
** If flattening is not attempted, this routine is a no-op and returns 0.
92502
 
** If flattening is attempted this routine returns 1.
92503
 
**
92504
 
** All of the expression analysis must occur on both the outer query and
92505
 
** the subquery before this routine runs.
92506
 
*/
92507
 
static int flattenSubquery(
92508
 
  Parse *pParse,       /* Parsing context */
92509
 
  Select *p,           /* The parent or outer SELECT statement */
92510
 
  int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
92511
 
  int isAgg,           /* True if outer SELECT uses aggregate functions */
92512
 
  int subqueryIsAgg    /* True if the subquery uses aggregate functions */
92513
 
){
92514
 
  const char *zSavedAuthContext = pParse->zAuthContext;
92515
 
  Select *pParent;
92516
 
  Select *pSub;       /* The inner query or "subquery" */
92517
 
  Select *pSub1;      /* Pointer to the rightmost select in sub-query */
92518
 
  SrcList *pSrc;      /* The FROM clause of the outer query */
92519
 
  SrcList *pSubSrc;   /* The FROM clause of the subquery */
92520
 
  ExprList *pList;    /* The result set of the outer query */
92521
 
  int iParent;        /* VDBE cursor number of the pSub result set temp table */
92522
 
  int i;              /* Loop counter */
92523
 
  Expr *pWhere;                    /* The WHERE clause */
92524
 
  struct SrcList_item *pSubitem;   /* The subquery */
92525
 
  sqlite3 *db = pParse->db;
92526
 
 
92527
 
  /* Check to see if flattening is permitted.  Return 0 if not.
92528
 
  */
92529
 
  assert( p!=0 );
92530
 
  assert( p->pPrior==0 );  /* Unable to flatten compound queries */
92531
 
  if( db->flags & SQLITE_QueryFlattener ) return 0;
92532
 
  pSrc = p->pSrc;
92533
 
  assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
92534
 
  pSubitem = &pSrc->a[iFrom];
92535
 
  iParent = pSubitem->iCursor;
92536
 
  pSub = pSubitem->pSelect;
92537
 
  assert( pSub!=0 );
92538
 
  if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
92539
 
  if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;          /* Restriction (2)  */
92540
 
  pSubSrc = pSub->pSrc;
92541
 
  assert( pSubSrc );
92542
 
  /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
92543
 
  ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
92544
 
  ** because they could be computed at compile-time.  But when LIMIT and OFFSET
92545
 
  ** became arbitrary expressions, we were forced to add restrictions (13)
92546
 
  ** and (14). */
92547
 
  if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
92548
 
  if( pSub->pOffset ) return 0;                          /* Restriction (14) */
92549
 
  if( p->pRightmost && pSub->pLimit ){
92550
 
    return 0;                                            /* Restriction (15) */
92551
 
  }
92552
 
  if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
92553
 
  if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
92554
 
  if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
92555
 
     return 0;         /* Restrictions (8)(9) */
92556
 
  }
92557
 
  if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
92558
 
     return 0;         /* Restriction (6)  */
92559
 
  }
92560
 
  if( p->pOrderBy && pSub->pOrderBy ){
92561
 
     return 0;                                           /* Restriction (11) */
92562
 
  }
92563
 
  if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
92564
 
  if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
92565
 
  if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
92566
 
     return 0;         /* Restriction (21) */
92567
 
  }
92568
 
 
92569
 
  /* OBSOLETE COMMENT 1:
92570
 
  ** Restriction 3:  If the subquery is a join, make sure the subquery is 
92571
 
  ** not used as the right operand of an outer join.  Examples of why this
92572
 
  ** is not allowed:
92573
 
  **
92574
 
  **         t1 LEFT OUTER JOIN (t2 JOIN t3)
92575
 
  **
92576
 
  ** If we flatten the above, we would get
92577
 
  **
92578
 
  **         (t1 LEFT OUTER JOIN t2) JOIN t3
92579
 
  **
92580
 
  ** which is not at all the same thing.
92581
 
  **
92582
 
  ** OBSOLETE COMMENT 2:
92583
 
  ** Restriction 12:  If the subquery is the right operand of a left outer
92584
 
  ** join, make sure the subquery has no WHERE clause.
92585
 
  ** An examples of why this is not allowed:
92586
 
  **
92587
 
  **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
92588
 
  **
92589
 
  ** If we flatten the above, we would get
92590
 
  **
92591
 
  **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
92592
 
  **
92593
 
  ** But the t2.x>0 test will always fail on a NULL row of t2, which
92594
 
  ** effectively converts the OUTER JOIN into an INNER JOIN.
92595
 
  **
92596
 
  ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
92597
 
  ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
92598
 
  ** is fraught with danger.  Best to avoid the whole thing.  If the
92599
 
  ** subquery is the right term of a LEFT JOIN, then do not flatten.
92600
 
  */
92601
 
  if( (pSubitem->jointype & JT_OUTER)!=0 ){
92602
 
    return 0;
92603
 
  }
92604
 
 
92605
 
  /* Restriction 17: If the sub-query is a compound SELECT, then it must
92606
 
  ** use only the UNION ALL operator. And none of the simple select queries
92607
 
  ** that make up the compound SELECT are allowed to be aggregate or distinct
92608
 
  ** queries.
92609
 
  */
92610
 
  if( pSub->pPrior ){
92611
 
    if( pSub->pOrderBy ){
92612
 
      return 0;  /* Restriction 20 */
92613
 
    }
92614
 
    if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
92615
 
      return 0;
92616
 
    }
92617
 
    for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
92618
 
      testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
92619
 
      testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
92620
 
      if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
92621
 
       || (pSub1->pPrior && pSub1->op!=TK_ALL) 
92622
 
       || NEVER(pSub1->pSrc==0) || pSub1->pSrc->nSrc!=1
92623
 
      ){
92624
 
        return 0;
92625
 
      }
92626
 
    }
92627
 
 
92628
 
    /* Restriction 18. */
92629
 
    if( p->pOrderBy ){
92630
 
      int ii;
92631
 
      for(ii=0; ii<p->pOrderBy->nExpr; ii++){
92632
 
        if( p->pOrderBy->a[ii].iCol==0 ) return 0;
92633
 
      }
92634
 
    }
92635
 
  }
92636
 
 
92637
 
  /***** If we reach this point, flattening is permitted. *****/
92638
 
 
92639
 
  /* Authorize the subquery */
92640
 
  pParse->zAuthContext = pSubitem->zName;
92641
 
  sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
92642
 
  pParse->zAuthContext = zSavedAuthContext;
92643
 
 
92644
 
  /* If the sub-query is a compound SELECT statement, then (by restrictions
92645
 
  ** 17 and 18 above) it must be a UNION ALL and the parent query must 
92646
 
  ** be of the form:
92647
 
  **
92648
 
  **     SELECT <expr-list> FROM (<sub-query>) <where-clause> 
92649
 
  **
92650
 
  ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
92651
 
  ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or 
92652
 
  ** OFFSET clauses and joins them to the left-hand-side of the original
92653
 
  ** using UNION ALL operators. In this case N is the number of simple
92654
 
  ** select statements in the compound sub-query.
92655
 
  **
92656
 
  ** Example:
92657
 
  **
92658
 
  **     SELECT a+1 FROM (
92659
 
  **        SELECT x FROM tab
92660
 
  **        UNION ALL
92661
 
  **        SELECT y FROM tab
92662
 
  **        UNION ALL
92663
 
  **        SELECT abs(z*2) FROM tab2
92664
 
  **     ) WHERE a!=5 ORDER BY 1
92665
 
  **
92666
 
  ** Transformed into:
92667
 
  **
92668
 
  **     SELECT x+1 FROM tab WHERE x+1!=5
92669
 
  **     UNION ALL
92670
 
  **     SELECT y+1 FROM tab WHERE y+1!=5
92671
 
  **     UNION ALL
92672
 
  **     SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
92673
 
  **     ORDER BY 1
92674
 
  **
92675
 
  ** We call this the "compound-subquery flattening".
92676
 
  */
92677
 
  for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
92678
 
    Select *pNew;
92679
 
    ExprList *pOrderBy = p->pOrderBy;
92680
 
    Expr *pLimit = p->pLimit;
92681
 
    Select *pPrior = p->pPrior;
92682
 
    p->pOrderBy = 0;
92683
 
    p->pSrc = 0;
92684
 
    p->pPrior = 0;
92685
 
    p->pLimit = 0;
92686
 
    pNew = sqlite3SelectDup(db, p, 0);
92687
 
    p->pLimit = pLimit;
92688
 
    p->pOrderBy = pOrderBy;
92689
 
    p->pSrc = pSrc;
92690
 
    p->op = TK_ALL;
92691
 
    p->pRightmost = 0;
92692
 
    if( pNew==0 ){
92693
 
      pNew = pPrior;
92694
 
    }else{
92695
 
      pNew->pPrior = pPrior;
92696
 
      pNew->pRightmost = 0;
92697
 
    }
92698
 
    p->pPrior = pNew;
92699
 
    if( db->mallocFailed ) return 1;
92700
 
  }
92701
 
 
92702
 
  /* Begin flattening the iFrom-th entry of the FROM clause 
92703
 
  ** in the outer query.
92704
 
  */
92705
 
  pSub = pSub1 = pSubitem->pSelect;
92706
 
 
92707
 
  /* Delete the transient table structure associated with the
92708
 
  ** subquery
92709
 
  */
92710
 
  sqlite3DbFree(db, pSubitem->zDatabase);
92711
 
  sqlite3DbFree(db, pSubitem->zName);
92712
 
  sqlite3DbFree(db, pSubitem->zAlias);
92713
 
  pSubitem->zDatabase = 0;
92714
 
  pSubitem->zName = 0;
92715
 
  pSubitem->zAlias = 0;
92716
 
  pSubitem->pSelect = 0;
92717
 
 
92718
 
  /* Defer deleting the Table object associated with the
92719
 
  ** subquery until code generation is
92720
 
  ** complete, since there may still exist Expr.pTab entries that
92721
 
  ** refer to the subquery even after flattening.  Ticket #3346.
92722
 
  **
92723
 
  ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
92724
 
  */
92725
 
  if( ALWAYS(pSubitem->pTab!=0) ){
92726
 
    Table *pTabToDel = pSubitem->pTab;
92727
 
    if( pTabToDel->nRef==1 ){
92728
 
      Parse *pToplevel = sqlite3ParseToplevel(pParse);
92729
 
      pTabToDel->pNextZombie = pToplevel->pZombieTab;
92730
 
      pToplevel->pZombieTab = pTabToDel;
92731
 
    }else{
92732
 
      pTabToDel->nRef--;
92733
 
    }
92734
 
    pSubitem->pTab = 0;
92735
 
  }
92736
 
 
92737
 
  /* The following loop runs once for each term in a compound-subquery
92738
 
  ** flattening (as described above).  If we are doing a different kind
92739
 
  ** of flattening - a flattening other than a compound-subquery flattening -
92740
 
  ** then this loop only runs once.
92741
 
  **
92742
 
  ** This loop moves all of the FROM elements of the subquery into the
92743
 
  ** the FROM clause of the outer query.  Before doing this, remember
92744
 
  ** the cursor number for the original outer query FROM element in
92745
 
  ** iParent.  The iParent cursor will never be used.  Subsequent code
92746
 
  ** will scan expressions looking for iParent references and replace
92747
 
  ** those references with expressions that resolve to the subquery FROM
92748
 
  ** elements we are now copying in.
92749
 
  */
92750
 
  for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
92751
 
    int nSubSrc;
92752
 
    u8 jointype = 0;
92753
 
    pSubSrc = pSub->pSrc;     /* FROM clause of subquery */
92754
 
    nSubSrc = pSubSrc->nSrc;  /* Number of terms in subquery FROM clause */
92755
 
    pSrc = pParent->pSrc;     /* FROM clause of the outer query */
92756
 
 
92757
 
    if( pSrc ){
92758
 
      assert( pParent==p );  /* First time through the loop */
92759
 
      jointype = pSubitem->jointype;
92760
 
    }else{
92761
 
      assert( pParent!=p );  /* 2nd and subsequent times through the loop */
92762
 
      pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
92763
 
      if( pSrc==0 ){
92764
 
        assert( db->mallocFailed );
92765
 
        break;
92766
 
      }
92767
 
    }
92768
 
 
92769
 
    /* The subquery uses a single slot of the FROM clause of the outer
92770
 
    ** query.  If the subquery has more than one element in its FROM clause,
92771
 
    ** then expand the outer query to make space for it to hold all elements
92772
 
    ** of the subquery.
92773
 
    **
92774
 
    ** Example:
92775
 
    **
92776
 
    **    SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
92777
 
    **
92778
 
    ** The outer query has 3 slots in its FROM clause.  One slot of the
92779
 
    ** outer query (the middle slot) is used by the subquery.  The next
92780
 
    ** block of code will expand the out query to 4 slots.  The middle
92781
 
    ** slot is expanded to two slots in order to make space for the
92782
 
    ** two elements in the FROM clause of the subquery.
92783
 
    */
92784
 
    if( nSubSrc>1 ){
92785
 
      pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
92786
 
      if( db->mallocFailed ){
92787
 
        break;
92788
 
      }
92789
 
    }
92790
 
 
92791
 
    /* Transfer the FROM clause terms from the subquery into the
92792
 
    ** outer query.
92793
 
    */
92794
 
    for(i=0; i<nSubSrc; i++){
92795
 
      sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
92796
 
      pSrc->a[i+iFrom] = pSubSrc->a[i];
92797
 
      memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
92798
 
    }
92799
 
    pSrc->a[iFrom].jointype = jointype;
92800
 
  
92801
 
    /* Now begin substituting subquery result set expressions for 
92802
 
    ** references to the iParent in the outer query.
92803
 
    ** 
92804
 
    ** Example:
92805
 
    **
92806
 
    **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
92807
 
    **   \                     \_____________ subquery __________/          /
92808
 
    **    \_____________________ outer query ______________________________/
92809
 
    **
92810
 
    ** We look at every expression in the outer query and every place we see
92811
 
    ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
92812
 
    */
92813
 
    pList = pParent->pEList;
92814
 
    for(i=0; i<pList->nExpr; i++){
92815
 
      if( pList->a[i].zName==0 ){
92816
 
        const char *zSpan = pList->a[i].zSpan;
92817
 
        if( ALWAYS(zSpan) ){
92818
 
          pList->a[i].zName = sqlite3DbStrDup(db, zSpan);
92819
 
        }
92820
 
      }
92821
 
    }
92822
 
    substExprList(db, pParent->pEList, iParent, pSub->pEList);
92823
 
    if( isAgg ){
92824
 
      substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
92825
 
      pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
92826
 
    }
92827
 
    if( pSub->pOrderBy ){
92828
 
      assert( pParent->pOrderBy==0 );
92829
 
      pParent->pOrderBy = pSub->pOrderBy;
92830
 
      pSub->pOrderBy = 0;
92831
 
    }else if( pParent->pOrderBy ){
92832
 
      substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
92833
 
    }
92834
 
    if( pSub->pWhere ){
92835
 
      pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
92836
 
    }else{
92837
 
      pWhere = 0;
92838
 
    }
92839
 
    if( subqueryIsAgg ){
92840
 
      assert( pParent->pHaving==0 );
92841
 
      pParent->pHaving = pParent->pWhere;
92842
 
      pParent->pWhere = pWhere;
92843
 
      pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
92844
 
      pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving, 
92845
 
                                  sqlite3ExprDup(db, pSub->pHaving, 0));
92846
 
      assert( pParent->pGroupBy==0 );
92847
 
      pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
92848
 
    }else{
92849
 
      pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList);
92850
 
      pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
92851
 
    }
92852
 
  
92853
 
    /* The flattened query is distinct if either the inner or the
92854
 
    ** outer query is distinct. 
92855
 
    */
92856
 
    pParent->selFlags |= pSub->selFlags & SF_Distinct;
92857
 
  
92858
 
    /*
92859
 
    ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
92860
 
    **
92861
 
    ** One is tempted to try to add a and b to combine the limits.  But this
92862
 
    ** does not work if either limit is negative.
92863
 
    */
92864
 
    if( pSub->pLimit ){
92865
 
      pParent->pLimit = pSub->pLimit;
92866
 
      pSub->pLimit = 0;
92867
 
    }
92868
 
  }
92869
 
 
92870
 
  /* Finially, delete what is left of the subquery and return
92871
 
  ** success.
92872
 
  */
92873
 
  sqlite3SelectDelete(db, pSub1);
92874
 
 
92875
 
  return 1;
92876
 
}
92877
 
#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
92878
 
 
92879
 
/*
92880
 
** Analyze the SELECT statement passed as an argument to see if it
92881
 
** is a min() or max() query. Return WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX if 
92882
 
** it is, or 0 otherwise. At present, a query is considered to be
92883
 
** a min()/max() query if:
92884
 
**
92885
 
**   1. There is a single object in the FROM clause.
92886
 
**
92887
 
**   2. There is a single expression in the result set, and it is
92888
 
**      either min(x) or max(x), where x is a column reference.
92889
 
*/
92890
 
static u8 minMaxQuery(Select *p){
92891
 
  Expr *pExpr;
92892
 
  ExprList *pEList = p->pEList;
92893
 
 
92894
 
  if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
92895
 
  pExpr = pEList->a[0].pExpr;
92896
 
  if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
92897
 
  if( NEVER(ExprHasProperty(pExpr, EP_xIsSelect)) ) return 0;
92898
 
  pEList = pExpr->x.pList;
92899
 
  if( pEList==0 || pEList->nExpr!=1 ) return 0;
92900
 
  if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return WHERE_ORDERBY_NORMAL;
92901
 
  assert( !ExprHasProperty(pExpr, EP_IntValue) );
92902
 
  if( sqlite3StrICmp(pExpr->u.zToken,"min")==0 ){
92903
 
    return WHERE_ORDERBY_MIN;
92904
 
  }else if( sqlite3StrICmp(pExpr->u.zToken,"max")==0 ){
92905
 
    return WHERE_ORDERBY_MAX;
92906
 
  }
92907
 
  return WHERE_ORDERBY_NORMAL;
92908
 
}
92909
 
 
92910
 
/*
92911
 
** The select statement passed as the first argument is an aggregate query.
92912
 
** The second argment is the associated aggregate-info object. This 
92913
 
** function tests if the SELECT is of the form:
92914
 
**
92915
 
**   SELECT count(*) FROM <tbl>
92916
 
**
92917
 
** where table is a database table, not a sub-select or view. If the query
92918
 
** does match this pattern, then a pointer to the Table object representing
92919
 
** <tbl> is returned. Otherwise, 0 is returned.
92920
 
*/
92921
 
static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
92922
 
  Table *pTab;
92923
 
  Expr *pExpr;
92924
 
 
92925
 
  assert( !p->pGroupBy );
92926
 
 
92927
 
  if( p->pWhere || p->pEList->nExpr!=1 
92928
 
   || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
92929
 
  ){
92930
 
    return 0;
92931
 
  }
92932
 
  pTab = p->pSrc->a[0].pTab;
92933
 
  pExpr = p->pEList->a[0].pExpr;
92934
 
  assert( pTab && !pTab->pSelect && pExpr );
92935
 
 
92936
 
  if( IsVirtual(pTab) ) return 0;
92937
 
  if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
92938
 
  if( (pAggInfo->aFunc[0].pFunc->flags&SQLITE_FUNC_COUNT)==0 ) return 0;
92939
 
  if( pExpr->flags&EP_Distinct ) return 0;
92940
 
 
92941
 
  return pTab;
92942
 
}
92943
 
 
92944
 
/*
92945
 
** If the source-list item passed as an argument was augmented with an
92946
 
** INDEXED BY clause, then try to locate the specified index. If there
92947
 
** was such a clause and the named index cannot be found, return 
92948
 
** SQLITE_ERROR and leave an error in pParse. Otherwise, populate 
92949
 
** pFrom->pIndex and return SQLITE_OK.
92950
 
*/
92951
 
SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
92952
 
  if( pFrom->pTab && pFrom->zIndex ){
92953
 
    Table *pTab = pFrom->pTab;
92954
 
    char *zIndex = pFrom->zIndex;
92955
 
    Index *pIdx;
92956
 
    for(pIdx=pTab->pIndex; 
92957
 
        pIdx && sqlite3StrICmp(pIdx->zName, zIndex); 
92958
 
        pIdx=pIdx->pNext
92959
 
    );
92960
 
    if( !pIdx ){
92961
 
      sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
92962
 
      pParse->checkSchema = 1;
92963
 
      return SQLITE_ERROR;
92964
 
    }
92965
 
    pFrom->pIndex = pIdx;
92966
 
  }
92967
 
  return SQLITE_OK;
92968
 
}
92969
 
 
92970
 
/*
92971
 
** This routine is a Walker callback for "expanding" a SELECT statement.
92972
 
** "Expanding" means to do the following:
92973
 
**
92974
 
**    (1)  Make sure VDBE cursor numbers have been assigned to every
92975
 
**         element of the FROM clause.
92976
 
**
92977
 
**    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that 
92978
 
**         defines FROM clause.  When views appear in the FROM clause,
92979
 
**         fill pTabList->a[].pSelect with a copy of the SELECT statement
92980
 
**         that implements the view.  A copy is made of the view's SELECT
92981
 
**         statement so that we can freely modify or delete that statement
92982
 
**         without worrying about messing up the presistent representation
92983
 
**         of the view.
92984
 
**
92985
 
**    (3)  Add terms to the WHERE clause to accomodate the NATURAL keyword
92986
 
**         on joins and the ON and USING clause of joins.
92987
 
**
92988
 
**    (4)  Scan the list of columns in the result set (pEList) looking
92989
 
**         for instances of the "*" operator or the TABLE.* operator.
92990
 
**         If found, expand each "*" to be every column in every table
92991
 
**         and TABLE.* to be every column in TABLE.
92992
 
**
92993
 
*/
92994
 
static int selectExpander(Walker *pWalker, Select *p){
92995
 
  Parse *pParse = pWalker->pParse;
92996
 
  int i, j, k;
92997
 
  SrcList *pTabList;
92998
 
  ExprList *pEList;
92999
 
  struct SrcList_item *pFrom;
93000
 
  sqlite3 *db = pParse->db;
93001
 
 
93002
 
  if( db->mallocFailed  ){
93003
 
    return WRC_Abort;
93004
 
  }
93005
 
  if( NEVER(p->pSrc==0) || (p->selFlags & SF_Expanded)!=0 ){
93006
 
    return WRC_Prune;
93007
 
  }
93008
 
  p->selFlags |= SF_Expanded;
93009
 
  pTabList = p->pSrc;
93010
 
  pEList = p->pEList;
93011
 
 
93012
 
  /* Make sure cursor numbers have been assigned to all entries in
93013
 
  ** the FROM clause of the SELECT statement.
93014
 
  */
93015
 
  sqlite3SrcListAssignCursors(pParse, pTabList);
93016
 
 
93017
 
  /* Look up every table named in the FROM clause of the select.  If
93018
 
  ** an entry of the FROM clause is a subquery instead of a table or view,
93019
 
  ** then create a transient table structure to describe the subquery.
93020
 
  */
93021
 
  for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
93022
 
    Table *pTab;
93023
 
    if( pFrom->pTab!=0 ){
93024
 
      /* This statement has already been prepared.  There is no need
93025
 
      ** to go further. */
93026
 
      assert( i==0 );
93027
 
      return WRC_Prune;
93028
 
    }
93029
 
    if( pFrom->zName==0 ){
93030
 
#ifndef SQLITE_OMIT_SUBQUERY
93031
 
      Select *pSel = pFrom->pSelect;
93032
 
      /* A sub-query in the FROM clause of a SELECT */
93033
 
      assert( pSel!=0 );
93034
 
      assert( pFrom->pTab==0 );
93035
 
      sqlite3WalkSelect(pWalker, pSel);
93036
 
      pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
93037
 
      if( pTab==0 ) return WRC_Abort;
93038
 
      pTab->nRef = 1;
93039
 
      pTab->zName = sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pTab);
93040
 
      while( pSel->pPrior ){ pSel = pSel->pPrior; }
93041
 
      selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
93042
 
      pTab->iPKey = -1;
93043
 
      pTab->nRowEst = 1000000;
93044
 
      pTab->tabFlags |= TF_Ephemeral;
93045
 
#endif
93046
 
    }else{
93047
 
      /* An ordinary table or view name in the FROM clause */
93048
 
      assert( pFrom->pTab==0 );
93049
 
      pFrom->pTab = pTab = 
93050
 
        sqlite3LocateTable(pParse,0,pFrom->zName,pFrom->zDatabase);
93051
 
      if( pTab==0 ) return WRC_Abort;
93052
 
      pTab->nRef++;
93053
 
#if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
93054
 
      if( pTab->pSelect || IsVirtual(pTab) ){
93055
 
        /* We reach here if the named table is a really a view */
93056
 
        if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
93057
 
        assert( pFrom->pSelect==0 );
93058
 
        pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
93059
 
        sqlite3WalkSelect(pWalker, pFrom->pSelect);
93060
 
      }
93061
 
#endif
93062
 
    }
93063
 
 
93064
 
    /* Locate the index named by the INDEXED BY clause, if any. */
93065
 
    if( sqlite3IndexedByLookup(pParse, pFrom) ){
93066
 
      return WRC_Abort;
93067
 
    }
93068
 
  }
93069
 
 
93070
 
  /* Process NATURAL keywords, and ON and USING clauses of joins.
93071
 
  */
93072
 
  if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
93073
 
    return WRC_Abort;
93074
 
  }
93075
 
 
93076
 
  /* For every "*" that occurs in the column list, insert the names of
93077
 
  ** all columns in all tables.  And for every TABLE.* insert the names
93078
 
  ** of all columns in TABLE.  The parser inserted a special expression
93079
 
  ** with the TK_ALL operator for each "*" that it found in the column list.
93080
 
  ** The following code just has to locate the TK_ALL expressions and expand
93081
 
  ** each one to the list of all columns in all tables.
93082
 
  **
93083
 
  ** The first loop just checks to see if there are any "*" operators
93084
 
  ** that need expanding.
93085
 
  */
93086
 
  for(k=0; k<pEList->nExpr; k++){
93087
 
    Expr *pE = pEList->a[k].pExpr;
93088
 
    if( pE->op==TK_ALL ) break;
93089
 
    assert( pE->op!=TK_DOT || pE->pRight!=0 );
93090
 
    assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
93091
 
    if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
93092
 
  }
93093
 
  if( k<pEList->nExpr ){
93094
 
    /*
93095
 
    ** If we get here it means the result set contains one or more "*"
93096
 
    ** operators that need to be expanded.  Loop through each expression
93097
 
    ** in the result set and expand them one by one.
93098
 
    */
93099
 
    struct ExprList_item *a = pEList->a;
93100
 
    ExprList *pNew = 0;
93101
 
    int flags = pParse->db->flags;
93102
 
    int longNames = (flags & SQLITE_FullColNames)!=0
93103
 
                      && (flags & SQLITE_ShortColNames)==0;
93104
 
 
93105
 
    for(k=0; k<pEList->nExpr; k++){
93106
 
      Expr *pE = a[k].pExpr;
93107
 
      assert( pE->op!=TK_DOT || pE->pRight!=0 );
93108
 
      if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pE->pRight->op!=TK_ALL) ){
93109
 
        /* This particular expression does not need to be expanded.
93110
 
        */
93111
 
        pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
93112
 
        if( pNew ){
93113
 
          pNew->a[pNew->nExpr-1].zName = a[k].zName;
93114
 
          pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
93115
 
          a[k].zName = 0;
93116
 
          a[k].zSpan = 0;
93117
 
        }
93118
 
        a[k].pExpr = 0;
93119
 
      }else{
93120
 
        /* This expression is a "*" or a "TABLE.*" and needs to be
93121
 
        ** expanded. */
93122
 
        int tableSeen = 0;      /* Set to 1 when TABLE matches */
93123
 
        char *zTName;            /* text of name of TABLE */
93124
 
        if( pE->op==TK_DOT ){
93125
 
          assert( pE->pLeft!=0 );
93126
 
          assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
93127
 
          zTName = pE->pLeft->u.zToken;
93128
 
        }else{
93129
 
          zTName = 0;
93130
 
        }
93131
 
        for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
93132
 
          Table *pTab = pFrom->pTab;
93133
 
          char *zTabName = pFrom->zAlias;
93134
 
          if( zTabName==0 ){
93135
 
            zTabName = pTab->zName;
93136
 
          }
93137
 
          if( db->mallocFailed ) break;
93138
 
          if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
93139
 
            continue;
93140
 
          }
93141
 
          tableSeen = 1;
93142
 
          for(j=0; j<pTab->nCol; j++){
93143
 
            Expr *pExpr, *pRight;
93144
 
            char *zName = pTab->aCol[j].zName;
93145
 
            char *zColname;  /* The computed column name */
93146
 
            char *zToFree;   /* Malloced string that needs to be freed */
93147
 
            Token sColname;  /* Computed column name as a token */
93148
 
 
93149
 
            /* If a column is marked as 'hidden' (currently only possible
93150
 
            ** for virtual tables), do not include it in the expanded
93151
 
            ** result-set list.
93152
 
            */
93153
 
            if( IsHiddenColumn(&pTab->aCol[j]) ){
93154
 
              assert(IsVirtual(pTab));
93155
 
              continue;
93156
 
            }
93157
 
 
93158
 
            if( i>0 && zTName==0 ){
93159
 
              if( (pFrom->jointype & JT_NATURAL)!=0
93160
 
                && tableAndColumnIndex(pTabList, i, zName, 0, 0)
93161
 
              ){
93162
 
                /* In a NATURAL join, omit the join columns from the 
93163
 
                ** table to the right of the join */
93164
 
                continue;
93165
 
              }
93166
 
              if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
93167
 
                /* In a join with a USING clause, omit columns in the
93168
 
                ** using clause from the table on the right. */
93169
 
                continue;
93170
 
              }
93171
 
            }
93172
 
            pRight = sqlite3Expr(db, TK_ID, zName);
93173
 
            zColname = zName;
93174
 
            zToFree = 0;
93175
 
            if( longNames || pTabList->nSrc>1 ){
93176
 
              Expr *pLeft;
93177
 
              pLeft = sqlite3Expr(db, TK_ID, zTabName);
93178
 
              pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
93179
 
              if( longNames ){
93180
 
                zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
93181
 
                zToFree = zColname;
93182
 
              }
93183
 
            }else{
93184
 
              pExpr = pRight;
93185
 
            }
93186
 
            pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
93187
 
            sColname.z = zColname;
93188
 
            sColname.n = sqlite3Strlen30(zColname);
93189
 
            sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
93190
 
            sqlite3DbFree(db, zToFree);
93191
 
          }
93192
 
        }
93193
 
        if( !tableSeen ){
93194
 
          if( zTName ){
93195
 
            sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
93196
 
          }else{
93197
 
            sqlite3ErrorMsg(pParse, "no tables specified");
93198
 
          }
93199
 
        }
93200
 
      }
93201
 
    }
93202
 
    sqlite3ExprListDelete(db, pEList);
93203
 
    p->pEList = pNew;
93204
 
  }
93205
 
#if SQLITE_MAX_COLUMN
93206
 
  if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
93207
 
    sqlite3ErrorMsg(pParse, "too many columns in result set");
93208
 
  }
93209
 
#endif
93210
 
  return WRC_Continue;
93211
 
}
93212
 
 
93213
 
/*
93214
 
** No-op routine for the parse-tree walker.
93215
 
**
93216
 
** When this routine is the Walker.xExprCallback then expression trees
93217
 
** are walked without any actions being taken at each node.  Presumably,
93218
 
** when this routine is used for Walker.xExprCallback then 
93219
 
** Walker.xSelectCallback is set to do something useful for every 
93220
 
** subquery in the parser tree.
93221
 
*/
93222
 
static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
93223
 
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
93224
 
  return WRC_Continue;
93225
 
}
93226
 
 
93227
 
/*
93228
 
** This routine "expands" a SELECT statement and all of its subqueries.
93229
 
** For additional information on what it means to "expand" a SELECT
93230
 
** statement, see the comment on the selectExpand worker callback above.
93231
 
**
93232
 
** Expanding a SELECT statement is the first step in processing a
93233
 
** SELECT statement.  The SELECT statement must be expanded before
93234
 
** name resolution is performed.
93235
 
**
93236
 
** If anything goes wrong, an error message is written into pParse.
93237
 
** The calling function can detect the problem by looking at pParse->nErr
93238
 
** and/or pParse->db->mallocFailed.
93239
 
*/
93240
 
static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
93241
 
  Walker w;
93242
 
  w.xSelectCallback = selectExpander;
93243
 
  w.xExprCallback = exprWalkNoop;
93244
 
  w.pParse = pParse;
93245
 
  sqlite3WalkSelect(&w, pSelect);
93246
 
}
93247
 
 
93248
 
 
93249
 
#ifndef SQLITE_OMIT_SUBQUERY
93250
 
/*
93251
 
** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
93252
 
** interface.
93253
 
**
93254
 
** For each FROM-clause subquery, add Column.zType and Column.zColl
93255
 
** information to the Table structure that represents the result set
93256
 
** of that subquery.
93257
 
**
93258
 
** The Table structure that represents the result set was constructed
93259
 
** by selectExpander() but the type and collation information was omitted
93260
 
** at that point because identifiers had not yet been resolved.  This
93261
 
** routine is called after identifier resolution.
93262
 
*/
93263
 
static int selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
93264
 
  Parse *pParse;
93265
 
  int i;
93266
 
  SrcList *pTabList;
93267
 
  struct SrcList_item *pFrom;
93268
 
 
93269
 
  assert( p->selFlags & SF_Resolved );
93270
 
  if( (p->selFlags & SF_HasTypeInfo)==0 ){
93271
 
    p->selFlags |= SF_HasTypeInfo;
93272
 
    pParse = pWalker->pParse;
93273
 
    pTabList = p->pSrc;
93274
 
    for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
93275
 
      Table *pTab = pFrom->pTab;
93276
 
      if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
93277
 
        /* A sub-query in the FROM clause of a SELECT */
93278
 
        Select *pSel = pFrom->pSelect;
93279
 
        assert( pSel );
93280
 
        while( pSel->pPrior ) pSel = pSel->pPrior;
93281
 
        selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel);
93282
 
      }
93283
 
    }
93284
 
  }
93285
 
  return WRC_Continue;
93286
 
}
93287
 
#endif
93288
 
 
93289
 
 
93290
 
/*
93291
 
** This routine adds datatype and collating sequence information to
93292
 
** the Table structures of all FROM-clause subqueries in a
93293
 
** SELECT statement.
93294
 
**
93295
 
** Use this routine after name resolution.
93296
 
*/
93297
 
static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
93298
 
#ifndef SQLITE_OMIT_SUBQUERY
93299
 
  Walker w;
93300
 
  w.xSelectCallback = selectAddSubqueryTypeInfo;
93301
 
  w.xExprCallback = exprWalkNoop;
93302
 
  w.pParse = pParse;
93303
 
  sqlite3WalkSelect(&w, pSelect);
93304
 
#endif
93305
 
}
93306
 
 
93307
 
 
93308
 
/*
93309
 
** This routine sets of a SELECT statement for processing.  The
93310
 
** following is accomplished:
93311
 
**
93312
 
**     *  VDBE Cursor numbers are assigned to all FROM-clause terms.
93313
 
**     *  Ephemeral Table objects are created for all FROM-clause subqueries.
93314
 
**     *  ON and USING clauses are shifted into WHERE statements
93315
 
**     *  Wildcards "*" and "TABLE.*" in result sets are expanded.
93316
 
**     *  Identifiers in expression are matched to tables.
93317
 
**
93318
 
** This routine acts recursively on all subqueries within the SELECT.
93319
 
*/
93320
 
SQLITE_PRIVATE void sqlite3SelectPrep(
93321
 
  Parse *pParse,         /* The parser context */
93322
 
  Select *p,             /* The SELECT statement being coded. */
93323
 
  NameContext *pOuterNC  /* Name context for container */
93324
 
){
93325
 
  sqlite3 *db;
93326
 
  if( NEVER(p==0) ) return;
93327
 
  db = pParse->db;
93328
 
  if( p->selFlags & SF_HasTypeInfo ) return;
93329
 
  sqlite3SelectExpand(pParse, p);
93330
 
  if( pParse->nErr || db->mallocFailed ) return;
93331
 
  sqlite3ResolveSelectNames(pParse, p, pOuterNC);
93332
 
  if( pParse->nErr || db->mallocFailed ) return;
93333
 
  sqlite3SelectAddTypeInfo(pParse, p);
93334
 
}
93335
 
 
93336
 
/*
93337
 
** Reset the aggregate accumulator.
93338
 
**
93339
 
** The aggregate accumulator is a set of memory cells that hold
93340
 
** intermediate results while calculating an aggregate.  This
93341
 
** routine simply stores NULLs in all of those memory cells.
93342
 
*/
93343
 
static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
93344
 
  Vdbe *v = pParse->pVdbe;
93345
 
  int i;
93346
 
  struct AggInfo_func *pFunc;
93347
 
  if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){
93348
 
    return;
93349
 
  }
93350
 
  for(i=0; i<pAggInfo->nColumn; i++){
93351
 
    sqlite3VdbeAddOp2(v, OP_Null, 0, pAggInfo->aCol[i].iMem);
93352
 
  }
93353
 
  for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
93354
 
    sqlite3VdbeAddOp2(v, OP_Null, 0, pFunc->iMem);
93355
 
    if( pFunc->iDistinct>=0 ){
93356
 
      Expr *pE = pFunc->pExpr;
93357
 
      assert( !ExprHasProperty(pE, EP_xIsSelect) );
93358
 
      if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
93359
 
        sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
93360
 
           "argument");
93361
 
        pFunc->iDistinct = -1;
93362
 
      }else{
93363
 
        KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList);
93364
 
        sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
93365
 
                          (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
93366
 
      }
93367
 
    }
93368
 
  }
93369
 
}
93370
 
 
93371
 
/*
93372
 
** Invoke the OP_AggFinalize opcode for every aggregate function
93373
 
** in the AggInfo structure.
93374
 
*/
93375
 
static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
93376
 
  Vdbe *v = pParse->pVdbe;
93377
 
  int i;
93378
 
  struct AggInfo_func *pF;
93379
 
  for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
93380
 
    ExprList *pList = pF->pExpr->x.pList;
93381
 
    assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
93382
 
    sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
93383
 
                      (void*)pF->pFunc, P4_FUNCDEF);
93384
 
  }
93385
 
}
93386
 
 
93387
 
/*
93388
 
** Update the accumulator memory cells for an aggregate based on
93389
 
** the current cursor position.
93390
 
*/
93391
 
static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
93392
 
  Vdbe *v = pParse->pVdbe;
93393
 
  int i;
93394
 
  struct AggInfo_func *pF;
93395
 
  struct AggInfo_col *pC;
93396
 
 
93397
 
  pAggInfo->directMode = 1;
93398
 
  sqlite3ExprCacheClear(pParse);
93399
 
  for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
93400
 
    int nArg;
93401
 
    int addrNext = 0;
93402
 
    int regAgg;
93403
 
    ExprList *pList = pF->pExpr->x.pList;
93404
 
    assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
93405
 
    if( pList ){
93406
 
      nArg = pList->nExpr;
93407
 
      regAgg = sqlite3GetTempRange(pParse, nArg);
93408
 
      sqlite3ExprCodeExprList(pParse, pList, regAgg, 1);
93409
 
    }else{
93410
 
      nArg = 0;
93411
 
      regAgg = 0;
93412
 
    }
93413
 
    if( pF->iDistinct>=0 ){
93414
 
      addrNext = sqlite3VdbeMakeLabel(v);
93415
 
      assert( nArg==1 );
93416
 
      codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
93417
 
    }
93418
 
    if( pF->pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
93419
 
      CollSeq *pColl = 0;
93420
 
      struct ExprList_item *pItem;
93421
 
      int j;
93422
 
      assert( pList!=0 );  /* pList!=0 if pF->pFunc has NEEDCOLL */
93423
 
      for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
93424
 
        pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
93425
 
      }
93426
 
      if( !pColl ){
93427
 
        pColl = pParse->db->pDfltColl;
93428
 
      }
93429
 
      sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
93430
 
    }
93431
 
    sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
93432
 
                      (void*)pF->pFunc, P4_FUNCDEF);
93433
 
    sqlite3VdbeChangeP5(v, (u8)nArg);
93434
 
    sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
93435
 
    sqlite3ReleaseTempRange(pParse, regAgg, nArg);
93436
 
    if( addrNext ){
93437
 
      sqlite3VdbeResolveLabel(v, addrNext);
93438
 
      sqlite3ExprCacheClear(pParse);
93439
 
    }
93440
 
  }
93441
 
 
93442
 
  /* Before populating the accumulator registers, clear the column cache.
93443
 
  ** Otherwise, if any of the required column values are already present 
93444
 
  ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
93445
 
  ** to pC->iMem. But by the time the value is used, the original register
93446
 
  ** may have been used, invalidating the underlying buffer holding the
93447
 
  ** text or blob value. See ticket [883034dcb5].
93448
 
  **
93449
 
  ** Another solution would be to change the OP_SCopy used to copy cached
93450
 
  ** values to an OP_Copy.
93451
 
  */
93452
 
  sqlite3ExprCacheClear(pParse);
93453
 
  for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
93454
 
    sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
93455
 
  }
93456
 
  pAggInfo->directMode = 0;
93457
 
  sqlite3ExprCacheClear(pParse);
93458
 
}
93459
 
 
93460
 
/*
93461
 
** Add a single OP_Explain instruction to the VDBE to explain a simple
93462
 
** count(*) query ("SELECT count(*) FROM pTab").
93463
 
*/
93464
 
#ifndef SQLITE_OMIT_EXPLAIN
93465
 
static void explainSimpleCount(
93466
 
  Parse *pParse,                  /* Parse context */
93467
 
  Table *pTab,                    /* Table being queried */
93468
 
  Index *pIdx                     /* Index used to optimize scan, or NULL */
93469
 
){
93470
 
  if( pParse->explain==2 ){
93471
 
    char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s %s%s(~%d rows)",
93472
 
        pTab->zName, 
93473
 
        pIdx ? "USING COVERING INDEX " : "",
93474
 
        pIdx ? pIdx->zName : "",
93475
 
        pTab->nRowEst
93476
 
    );
93477
 
    sqlite3VdbeAddOp4(
93478
 
        pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
93479
 
    );
93480
 
  }
93481
 
}
93482
 
#else
93483
 
# define explainSimpleCount(a,b,c)
93484
 
#endif
93485
 
 
93486
 
/*
93487
 
** Generate code for the SELECT statement given in the p argument.  
93488
 
**
93489
 
** The results are distributed in various ways depending on the
93490
 
** contents of the SelectDest structure pointed to by argument pDest
93491
 
** as follows:
93492
 
**
93493
 
**     pDest->eDest    Result
93494
 
**     ------------    -------------------------------------------
93495
 
**     SRT_Output      Generate a row of output (using the OP_ResultRow
93496
 
**                     opcode) for each row in the result set.
93497
 
**
93498
 
**     SRT_Mem         Only valid if the result is a single column.
93499
 
**                     Store the first column of the first result row
93500
 
**                     in register pDest->iParm then abandon the rest
93501
 
**                     of the query.  This destination implies "LIMIT 1".
93502
 
**
93503
 
**     SRT_Set         The result must be a single column.  Store each
93504
 
**                     row of result as the key in table pDest->iParm. 
93505
 
**                     Apply the affinity pDest->affinity before storing
93506
 
**                     results.  Used to implement "IN (SELECT ...)".
93507
 
**
93508
 
**     SRT_Union       Store results as a key in a temporary table pDest->iParm.
93509
 
**
93510
 
**     SRT_Except      Remove results from the temporary table pDest->iParm.
93511
 
**
93512
 
**     SRT_Table       Store results in temporary table pDest->iParm.
93513
 
**                     This is like SRT_EphemTab except that the table
93514
 
**                     is assumed to already be open.
93515
 
**
93516
 
**     SRT_EphemTab    Create an temporary table pDest->iParm and store
93517
 
**                     the result there. The cursor is left open after
93518
 
**                     returning.  This is like SRT_Table except that
93519
 
**                     this destination uses OP_OpenEphemeral to create
93520
 
**                     the table first.
93521
 
**
93522
 
**     SRT_Coroutine   Generate a co-routine that returns a new row of
93523
 
**                     results each time it is invoked.  The entry point
93524
 
**                     of the co-routine is stored in register pDest->iParm.
93525
 
**
93526
 
**     SRT_Exists      Store a 1 in memory cell pDest->iParm if the result
93527
 
**                     set is not empty.
93528
 
**
93529
 
**     SRT_Discard     Throw the results away.  This is used by SELECT
93530
 
**                     statements within triggers whose only purpose is
93531
 
**                     the side-effects of functions.
93532
 
**
93533
 
** This routine returns the number of errors.  If any errors are
93534
 
** encountered, then an appropriate error message is left in
93535
 
** pParse->zErrMsg.
93536
 
**
93537
 
** This routine does NOT free the Select structure passed in.  The
93538
 
** calling function needs to do that.
93539
 
*/
93540
 
SQLITE_PRIVATE int sqlite3Select(
93541
 
  Parse *pParse,         /* The parser context */
93542
 
  Select *p,             /* The SELECT statement being coded. */
93543
 
  SelectDest *pDest      /* What to do with the query results */
93544
 
){
93545
 
  int i, j;              /* Loop counters */
93546
 
  WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
93547
 
  Vdbe *v;               /* The virtual machine under construction */
93548
 
  int isAgg;             /* True for select lists like "count(*)" */
93549
 
  ExprList *pEList;      /* List of columns to extract. */
93550
 
  SrcList *pTabList;     /* List of tables to select from */
93551
 
  Expr *pWhere;          /* The WHERE clause.  May be NULL */
93552
 
  ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
93553
 
  ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
93554
 
  Expr *pHaving;         /* The HAVING clause.  May be NULL */
93555
 
  int isDistinct;        /* True if the DISTINCT keyword is present */
93556
 
  int distinct;          /* Table to use for the distinct set */
93557
 
  int rc = 1;            /* Value to return from this function */
93558
 
  int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
93559
 
  AggInfo sAggInfo;      /* Information used by aggregate queries */
93560
 
  int iEnd;              /* Address of the end of the query */
93561
 
  sqlite3 *db;           /* The database connection */
93562
 
 
93563
 
#ifndef SQLITE_OMIT_EXPLAIN
93564
 
  int iRestoreSelectId = pParse->iSelectId;
93565
 
  pParse->iSelectId = pParse->iNextSelectId++;
93566
 
#endif
93567
 
 
93568
 
  db = pParse->db;
93569
 
  if( p==0 || db->mallocFailed || pParse->nErr ){
93570
 
    return 1;
93571
 
  }
93572
 
  if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
93573
 
  memset(&sAggInfo, 0, sizeof(sAggInfo));
93574
 
 
93575
 
  if( IgnorableOrderby(pDest) ){
93576
 
    assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union || 
93577
 
           pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard);
93578
 
    /* If ORDER BY makes no difference in the output then neither does
93579
 
    ** DISTINCT so it can be removed too. */
93580
 
    sqlite3ExprListDelete(db, p->pOrderBy);
93581
 
    p->pOrderBy = 0;
93582
 
    p->selFlags &= ~SF_Distinct;
93583
 
  }
93584
 
  sqlite3SelectPrep(pParse, p, 0);
93585
 
  pOrderBy = p->pOrderBy;
93586
 
  pTabList = p->pSrc;
93587
 
  pEList = p->pEList;
93588
 
  if( pParse->nErr || db->mallocFailed ){
93589
 
    goto select_end;
93590
 
  }
93591
 
  isAgg = (p->selFlags & SF_Aggregate)!=0;
93592
 
  assert( pEList!=0 );
93593
 
 
93594
 
  /* Begin generating code.
93595
 
  */
93596
 
  v = sqlite3GetVdbe(pParse);
93597
 
  if( v==0 ) goto select_end;
93598
 
 
93599
 
  /* If writing to memory or generating a set
93600
 
  ** only a single column may be output.
93601
 
  */
93602
 
#ifndef SQLITE_OMIT_SUBQUERY
93603
 
  if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
93604
 
    goto select_end;
93605
 
  }
93606
 
#endif
93607
 
 
93608
 
  /* Generate code for all sub-queries in the FROM clause
93609
 
  */
93610
 
#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
93611
 
  for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
93612
 
    struct SrcList_item *pItem = &pTabList->a[i];
93613
 
    SelectDest dest;
93614
 
    Select *pSub = pItem->pSelect;
93615
 
    int isAggSub;
93616
 
 
93617
 
    if( pSub==0 || pItem->isPopulated ) continue;
93618
 
 
93619
 
    /* Increment Parse.nHeight by the height of the largest expression
93620
 
    ** tree refered to by this, the parent select. The child select
93621
 
    ** may contain expression trees of at most
93622
 
    ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
93623
 
    ** more conservative than necessary, but much easier than enforcing
93624
 
    ** an exact limit.
93625
 
    */
93626
 
    pParse->nHeight += sqlite3SelectExprHeight(p);
93627
 
 
93628
 
    /* Check to see if the subquery can be absorbed into the parent. */
93629
 
    isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
93630
 
    if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
93631
 
      if( isAggSub ){
93632
 
        isAgg = 1;
93633
 
        p->selFlags |= SF_Aggregate;
93634
 
      }
93635
 
      i = -1;
93636
 
    }else{
93637
 
      sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
93638
 
      assert( pItem->isPopulated==0 );
93639
 
      explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
93640
 
      sqlite3Select(pParse, pSub, &dest);
93641
 
      pItem->isPopulated = 1;
93642
 
      pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
93643
 
    }
93644
 
    if( /*pParse->nErr ||*/ db->mallocFailed ){
93645
 
      goto select_end;
93646
 
    }
93647
 
    pParse->nHeight -= sqlite3SelectExprHeight(p);
93648
 
    pTabList = p->pSrc;
93649
 
    if( !IgnorableOrderby(pDest) ){
93650
 
      pOrderBy = p->pOrderBy;
93651
 
    }
93652
 
  }
93653
 
  pEList = p->pEList;
93654
 
#endif
93655
 
  pWhere = p->pWhere;
93656
 
  pGroupBy = p->pGroupBy;
93657
 
  pHaving = p->pHaving;
93658
 
  isDistinct = (p->selFlags & SF_Distinct)!=0;
93659
 
 
93660
 
#ifndef SQLITE_OMIT_COMPOUND_SELECT
93661
 
  /* If there is are a sequence of queries, do the earlier ones first.
93662
 
  */
93663
 
  if( p->pPrior ){
93664
 
    if( p->pRightmost==0 ){
93665
 
      Select *pLoop, *pRight = 0;
93666
 
      int cnt = 0;
93667
 
      int mxSelect;
93668
 
      for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
93669
 
        pLoop->pRightmost = p;
93670
 
        pLoop->pNext = pRight;
93671
 
        pRight = pLoop;
93672
 
      }
93673
 
      mxSelect = db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
93674
 
      if( mxSelect && cnt>mxSelect ){
93675
 
        sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
93676
 
        goto select_end;
93677
 
      }
93678
 
    }
93679
 
    rc = multiSelect(pParse, p, pDest);
93680
 
    explainSetInteger(pParse->iSelectId, iRestoreSelectId);
93681
 
    return rc;
93682
 
  }
93683
 
#endif
93684
 
 
93685
 
  /* If possible, rewrite the query to use GROUP BY instead of DISTINCT.
93686
 
  ** GROUP BY might use an index, DISTINCT never does.
93687
 
  */
93688
 
  assert( p->pGroupBy==0 || (p->selFlags & SF_Aggregate)!=0 );
93689
 
  if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ){
93690
 
    p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
93691
 
    pGroupBy = p->pGroupBy;
93692
 
    p->selFlags &= ~SF_Distinct;
93693
 
  }
93694
 
 
93695
 
  /* If there is both a GROUP BY and an ORDER BY clause and they are
93696
 
  ** identical, then disable the ORDER BY clause since the GROUP BY
93697
 
  ** will cause elements to come out in the correct order.  This is
93698
 
  ** an optimization - the correct answer should result regardless.
93699
 
  ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
93700
 
  ** to disable this optimization for testing purposes.
93701
 
  */
93702
 
  if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
93703
 
         && (db->flags & SQLITE_GroupByOrder)==0 ){
93704
 
    pOrderBy = 0;
93705
 
  }
93706
 
 
93707
 
  /* If there is an ORDER BY clause, then this sorting
93708
 
  ** index might end up being unused if the data can be 
93709
 
  ** extracted in pre-sorted order.  If that is the case, then the
93710
 
  ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
93711
 
  ** we figure out that the sorting index is not needed.  The addrSortIndex
93712
 
  ** variable is used to facilitate that change.
93713
 
  */
93714
 
  if( pOrderBy ){
93715
 
    KeyInfo *pKeyInfo;
93716
 
    pKeyInfo = keyInfoFromExprList(pParse, pOrderBy);
93717
 
    pOrderBy->iECursor = pParse->nTab++;
93718
 
    p->addrOpenEphm[2] = addrSortIndex =
93719
 
      sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
93720
 
                           pOrderBy->iECursor, pOrderBy->nExpr+2, 0,
93721
 
                           (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
93722
 
  }else{
93723
 
    addrSortIndex = -1;
93724
 
  }
93725
 
 
93726
 
  /* If the output is destined for a temporary table, open that table.
93727
 
  */
93728
 
  if( pDest->eDest==SRT_EphemTab ){
93729
 
    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iParm, pEList->nExpr);
93730
 
  }
93731
 
 
93732
 
  /* Set the limiter.
93733
 
  */
93734
 
  iEnd = sqlite3VdbeMakeLabel(v);
93735
 
  p->nSelectRow = (double)LARGEST_INT64;
93736
 
  computeLimitRegisters(pParse, p, iEnd);
93737
 
 
93738
 
  /* Open a virtual index to use for the distinct set.
93739
 
  */
93740
 
  if( p->selFlags & SF_Distinct ){
93741
 
    KeyInfo *pKeyInfo;
93742
 
    assert( isAgg || pGroupBy );
93743
 
    distinct = pParse->nTab++;
93744
 
    pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
93745
 
    sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0,
93746
 
                        (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
93747
 
    sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
93748
 
  }else{
93749
 
    distinct = -1;
93750
 
  }
93751
 
 
93752
 
  /* Aggregate and non-aggregate queries are handled differently */
93753
 
  if( !isAgg && pGroupBy==0 ){
93754
 
    /* This case is for non-aggregate queries
93755
 
    ** Begin the database scan
93756
 
    */
93757
 
    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, 0);
93758
 
    if( pWInfo==0 ) goto select_end;
93759
 
    if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
93760
 
 
93761
 
    /* If sorting index that was created by a prior OP_OpenEphemeral 
93762
 
    ** instruction ended up not being needed, then change the OP_OpenEphemeral
93763
 
    ** into an OP_Noop.
93764
 
    */
93765
 
    if( addrSortIndex>=0 && pOrderBy==0 ){
93766
 
      sqlite3VdbeChangeToNoop(v, addrSortIndex, 1);
93767
 
      p->addrOpenEphm[2] = -1;
93768
 
    }
93769
 
 
93770
 
    /* Use the standard inner loop
93771
 
    */
93772
 
    assert(!isDistinct);
93773
 
    selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, -1, pDest,
93774
 
                    pWInfo->iContinue, pWInfo->iBreak);
93775
 
 
93776
 
    /* End the database scan loop.
93777
 
    */
93778
 
    sqlite3WhereEnd(pWInfo);
93779
 
  }else{
93780
 
    /* This is the processing for aggregate queries */
93781
 
    NameContext sNC;    /* Name context for processing aggregate information */
93782
 
    int iAMem;          /* First Mem address for storing current GROUP BY */
93783
 
    int iBMem;          /* First Mem address for previous GROUP BY */
93784
 
    int iUseFlag;       /* Mem address holding flag indicating that at least
93785
 
                        ** one row of the input to the aggregator has been
93786
 
                        ** processed */
93787
 
    int iAbortFlag;     /* Mem address which causes query abort if positive */
93788
 
    int groupBySort;    /* Rows come from source in GROUP BY order */
93789
 
    int addrEnd;        /* End of processing for this SELECT */
93790
 
 
93791
 
    /* Remove any and all aliases between the result set and the
93792
 
    ** GROUP BY clause.
93793
 
    */
93794
 
    if( pGroupBy ){
93795
 
      int k;                        /* Loop counter */
93796
 
      struct ExprList_item *pItem;  /* For looping over expression in a list */
93797
 
 
93798
 
      for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
93799
 
        pItem->iAlias = 0;
93800
 
      }
93801
 
      for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
93802
 
        pItem->iAlias = 0;
93803
 
      }
93804
 
      if( p->nSelectRow>(double)100 ) p->nSelectRow = (double)100;
93805
 
    }else{
93806
 
      p->nSelectRow = (double)1;
93807
 
    }
93808
 
 
93809
 
 
93810
 
    /* Create a label to jump to when we want to abort the query */
93811
 
    addrEnd = sqlite3VdbeMakeLabel(v);
93812
 
 
93813
 
    /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
93814
 
    ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
93815
 
    ** SELECT statement.
93816
 
    */
93817
 
    memset(&sNC, 0, sizeof(sNC));
93818
 
    sNC.pParse = pParse;
93819
 
    sNC.pSrcList = pTabList;
93820
 
    sNC.pAggInfo = &sAggInfo;
93821
 
    sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
93822
 
    sAggInfo.pGroupBy = pGroupBy;
93823
 
    sqlite3ExprAnalyzeAggList(&sNC, pEList);
93824
 
    sqlite3ExprAnalyzeAggList(&sNC, pOrderBy);
93825
 
    if( pHaving ){
93826
 
      sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
93827
 
    }
93828
 
    sAggInfo.nAccumulator = sAggInfo.nColumn;
93829
 
    for(i=0; i<sAggInfo.nFunc; i++){
93830
 
      assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
93831
 
      sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
93832
 
    }
93833
 
    if( db->mallocFailed ) goto select_end;
93834
 
 
93835
 
    /* Processing for aggregates with GROUP BY is very different and
93836
 
    ** much more complex than aggregates without a GROUP BY.
93837
 
    */
93838
 
    if( pGroupBy ){
93839
 
      KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
93840
 
      int j1;             /* A-vs-B comparision jump */
93841
 
      int addrOutputRow;  /* Start of subroutine that outputs a result row */
93842
 
      int regOutputRow;   /* Return address register for output subroutine */
93843
 
      int addrSetAbort;   /* Set the abort flag and return */
93844
 
      int addrTopOfLoop;  /* Top of the input loop */
93845
 
      int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
93846
 
      int addrReset;      /* Subroutine for resetting the accumulator */
93847
 
      int regReset;       /* Return address register for reset subroutine */
93848
 
 
93849
 
      /* If there is a GROUP BY clause we might need a sorting index to
93850
 
      ** implement it.  Allocate that sorting index now.  If it turns out
93851
 
      ** that we do not need it after all, the OpenEphemeral instruction
93852
 
      ** will be converted into a Noop.  
93853
 
      */
93854
 
      sAggInfo.sortingIdx = pParse->nTab++;
93855
 
      pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
93856
 
      addrSortingIdx = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, 
93857
 
          sAggInfo.sortingIdx, sAggInfo.nSortingColumn, 
93858
 
          0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
93859
 
 
93860
 
      /* Initialize memory locations used by GROUP BY aggregate processing
93861
 
      */
93862
 
      iUseFlag = ++pParse->nMem;
93863
 
      iAbortFlag = ++pParse->nMem;
93864
 
      regOutputRow = ++pParse->nMem;
93865
 
      addrOutputRow = sqlite3VdbeMakeLabel(v);
93866
 
      regReset = ++pParse->nMem;
93867
 
      addrReset = sqlite3VdbeMakeLabel(v);
93868
 
      iAMem = pParse->nMem + 1;
93869
 
      pParse->nMem += pGroupBy->nExpr;
93870
 
      iBMem = pParse->nMem + 1;
93871
 
      pParse->nMem += pGroupBy->nExpr;
93872
 
      sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
93873
 
      VdbeComment((v, "clear abort flag"));
93874
 
      sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
93875
 
      VdbeComment((v, "indicate accumulator empty"));
93876
 
 
93877
 
      /* Begin a loop that will extract all source rows in GROUP BY order.
93878
 
      ** This might involve two separate loops with an OP_Sort in between, or
93879
 
      ** it might be a single loop that uses an index to extract information
93880
 
      ** in the right order to begin with.
93881
 
      */
93882
 
      sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
93883
 
      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0);
93884
 
      if( pWInfo==0 ) goto select_end;
93885
 
      if( pGroupBy==0 ){
93886
 
        /* The optimizer is able to deliver rows in group by order so
93887
 
        ** we do not have to sort.  The OP_OpenEphemeral table will be
93888
 
        ** cancelled later because we still need to use the pKeyInfo
93889
 
        */
93890
 
        pGroupBy = p->pGroupBy;
93891
 
        groupBySort = 0;
93892
 
      }else{
93893
 
        /* Rows are coming out in undetermined order.  We have to push
93894
 
        ** each row into a sorting index, terminate the first loop,
93895
 
        ** then loop over the sorting index in order to get the output
93896
 
        ** in sorted order
93897
 
        */
93898
 
        int regBase;
93899
 
        int regRecord;
93900
 
        int nCol;
93901
 
        int nGroupBy;
93902
 
 
93903
 
        explainTempTable(pParse, 
93904
 
            isDistinct && !(p->selFlags&SF_Distinct)?"DISTINCT":"GROUP BY");
93905
 
 
93906
 
        groupBySort = 1;
93907
 
        nGroupBy = pGroupBy->nExpr;
93908
 
        nCol = nGroupBy + 1;
93909
 
        j = nGroupBy+1;
93910
 
        for(i=0; i<sAggInfo.nColumn; i++){
93911
 
          if( sAggInfo.aCol[i].iSorterColumn>=j ){
93912
 
            nCol++;
93913
 
            j++;
93914
 
          }
93915
 
        }
93916
 
        regBase = sqlite3GetTempRange(pParse, nCol);
93917
 
        sqlite3ExprCacheClear(pParse);
93918
 
        sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0);
93919
 
        sqlite3VdbeAddOp2(v, OP_Sequence, sAggInfo.sortingIdx,regBase+nGroupBy);
93920
 
        j = nGroupBy+1;
93921
 
        for(i=0; i<sAggInfo.nColumn; i++){
93922
 
          struct AggInfo_col *pCol = &sAggInfo.aCol[i];
93923
 
          if( pCol->iSorterColumn>=j ){
93924
 
            int r1 = j + regBase;
93925
 
            int r2;
93926
 
 
93927
 
            r2 = sqlite3ExprCodeGetColumn(pParse, 
93928
 
                               pCol->pTab, pCol->iColumn, pCol->iTable, r1);
93929
 
            if( r1!=r2 ){
93930
 
              sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
93931
 
            }
93932
 
            j++;
93933
 
          }
93934
 
        }
93935
 
        regRecord = sqlite3GetTempReg(pParse);
93936
 
        sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
93937
 
        sqlite3VdbeAddOp2(v, OP_IdxInsert, sAggInfo.sortingIdx, regRecord);
93938
 
        sqlite3ReleaseTempReg(pParse, regRecord);
93939
 
        sqlite3ReleaseTempRange(pParse, regBase, nCol);
93940
 
        sqlite3WhereEnd(pWInfo);
93941
 
        sqlite3VdbeAddOp2(v, OP_Sort, sAggInfo.sortingIdx, addrEnd);
93942
 
        VdbeComment((v, "GROUP BY sort"));
93943
 
        sAggInfo.useSortingIdx = 1;
93944
 
        sqlite3ExprCacheClear(pParse);
93945
 
      }
93946
 
 
93947
 
      /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
93948
 
      ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
93949
 
      ** Then compare the current GROUP BY terms against the GROUP BY terms
93950
 
      ** from the previous row currently stored in a0, a1, a2...
93951
 
      */
93952
 
      addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
93953
 
      sqlite3ExprCacheClear(pParse);
93954
 
      for(j=0; j<pGroupBy->nExpr; j++){
93955
 
        if( groupBySort ){
93956
 
          sqlite3VdbeAddOp3(v, OP_Column, sAggInfo.sortingIdx, j, iBMem+j);
93957
 
        }else{
93958
 
          sAggInfo.directMode = 1;
93959
 
          sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
93960
 
        }
93961
 
      }
93962
 
      sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
93963
 
                          (char*)pKeyInfo, P4_KEYINFO);
93964
 
      j1 = sqlite3VdbeCurrentAddr(v);
93965
 
      sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1);
93966
 
 
93967
 
      /* Generate code that runs whenever the GROUP BY changes.
93968
 
      ** Changes in the GROUP BY are detected by the previous code
93969
 
      ** block.  If there were no changes, this block is skipped.
93970
 
      **
93971
 
      ** This code copies current group by terms in b0,b1,b2,...
93972
 
      ** over to a0,a1,a2.  It then calls the output subroutine
93973
 
      ** and resets the aggregate accumulator registers in preparation
93974
 
      ** for the next GROUP BY batch.
93975
 
      */
93976
 
      sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
93977
 
      sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
93978
 
      VdbeComment((v, "output one row"));
93979
 
      sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd);
93980
 
      VdbeComment((v, "check abort flag"));
93981
 
      sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
93982
 
      VdbeComment((v, "reset accumulator"));
93983
 
 
93984
 
      /* Update the aggregate accumulators based on the content of
93985
 
      ** the current row
93986
 
      */
93987
 
      sqlite3VdbeJumpHere(v, j1);
93988
 
      updateAccumulator(pParse, &sAggInfo);
93989
 
      sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
93990
 
      VdbeComment((v, "indicate data in accumulator"));
93991
 
 
93992
 
      /* End of the loop
93993
 
      */
93994
 
      if( groupBySort ){
93995
 
        sqlite3VdbeAddOp2(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop);
93996
 
      }else{
93997
 
        sqlite3WhereEnd(pWInfo);
93998
 
        sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1);
93999
 
      }
94000
 
 
94001
 
      /* Output the final row of result
94002
 
      */
94003
 
      sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
94004
 
      VdbeComment((v, "output final row"));
94005
 
 
94006
 
      /* Jump over the subroutines
94007
 
      */
94008
 
      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEnd);
94009
 
 
94010
 
      /* Generate a subroutine that outputs a single row of the result
94011
 
      ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
94012
 
      ** is less than or equal to zero, the subroutine is a no-op.  If
94013
 
      ** the processing calls for the query to abort, this subroutine
94014
 
      ** increments the iAbortFlag memory location before returning in
94015
 
      ** order to signal the caller to abort.
94016
 
      */
94017
 
      addrSetAbort = sqlite3VdbeCurrentAddr(v);
94018
 
      sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
94019
 
      VdbeComment((v, "set abort flag"));
94020
 
      sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
94021
 
      sqlite3VdbeResolveLabel(v, addrOutputRow);
94022
 
      addrOutputRow = sqlite3VdbeCurrentAddr(v);
94023
 
      sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
94024
 
      VdbeComment((v, "Groupby result generator entry point"));
94025
 
      sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
94026
 
      finalizeAggFunctions(pParse, &sAggInfo);
94027
 
      sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
94028
 
      selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
94029
 
                      distinct, pDest,
94030
 
                      addrOutputRow+1, addrSetAbort);
94031
 
      sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
94032
 
      VdbeComment((v, "end groupby result generator"));
94033
 
 
94034
 
      /* Generate a subroutine that will reset the group-by accumulator
94035
 
      */
94036
 
      sqlite3VdbeResolveLabel(v, addrReset);
94037
 
      resetAccumulator(pParse, &sAggInfo);
94038
 
      sqlite3VdbeAddOp1(v, OP_Return, regReset);
94039
 
     
94040
 
    } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
94041
 
    else {
94042
 
      ExprList *pDel = 0;
94043
 
#ifndef SQLITE_OMIT_BTREECOUNT
94044
 
      Table *pTab;
94045
 
      if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
94046
 
        /* If isSimpleCount() returns a pointer to a Table structure, then
94047
 
        ** the SQL statement is of the form:
94048
 
        **
94049
 
        **   SELECT count(*) FROM <tbl>
94050
 
        **
94051
 
        ** where the Table structure returned represents table <tbl>.
94052
 
        **
94053
 
        ** This statement is so common that it is optimized specially. The
94054
 
        ** OP_Count instruction is executed either on the intkey table that
94055
 
        ** contains the data for table <tbl> or on one of its indexes. It
94056
 
        ** is better to execute the op on an index, as indexes are almost
94057
 
        ** always spread across less pages than their corresponding tables.
94058
 
        */
94059
 
        const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
94060
 
        const int iCsr = pParse->nTab++;     /* Cursor to scan b-tree */
94061
 
        Index *pIdx;                         /* Iterator variable */
94062
 
        KeyInfo *pKeyInfo = 0;               /* Keyinfo for scanned index */
94063
 
        Index *pBest = 0;                    /* Best index found so far */
94064
 
        int iRoot = pTab->tnum;              /* Root page of scanned b-tree */
94065
 
 
94066
 
        sqlite3CodeVerifySchema(pParse, iDb);
94067
 
        sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
94068
 
 
94069
 
        /* Search for the index that has the least amount of columns. If
94070
 
        ** there is such an index, and it has less columns than the table
94071
 
        ** does, then we can assume that it consumes less space on disk and
94072
 
        ** will therefore be cheaper to scan to determine the query result.
94073
 
        ** In this case set iRoot to the root page number of the index b-tree
94074
 
        ** and pKeyInfo to the KeyInfo structure required to navigate the
94075
 
        ** index.
94076
 
        **
94077
 
        ** In practice the KeyInfo structure will not be used. It is only 
94078
 
        ** passed to keep OP_OpenRead happy.
94079
 
        */
94080
 
        for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
94081
 
          if( !pBest || pIdx->nColumn<pBest->nColumn ){
94082
 
            pBest = pIdx;
94083
 
          }
94084
 
        }
94085
 
        if( pBest && pBest->nColumn<pTab->nCol ){
94086
 
          iRoot = pBest->tnum;
94087
 
          pKeyInfo = sqlite3IndexKeyinfo(pParse, pBest);
94088
 
        }
94089
 
 
94090
 
        /* Open a read-only cursor, execute the OP_Count, close the cursor. */
94091
 
        sqlite3VdbeAddOp3(v, OP_OpenRead, iCsr, iRoot, iDb);
94092
 
        if( pKeyInfo ){
94093
 
          sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO_HANDOFF);
94094
 
        }
94095
 
        sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
94096
 
        sqlite3VdbeAddOp1(v, OP_Close, iCsr);
94097
 
        explainSimpleCount(pParse, pTab, pBest);
94098
 
      }else
94099
 
#endif /* SQLITE_OMIT_BTREECOUNT */
94100
 
      {
94101
 
        /* Check if the query is of one of the following forms:
94102
 
        **
94103
 
        **   SELECT min(x) FROM ...
94104
 
        **   SELECT max(x) FROM ...
94105
 
        **
94106
 
        ** If it is, then ask the code in where.c to attempt to sort results
94107
 
        ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause. 
94108
 
        ** If where.c is able to produce results sorted in this order, then
94109
 
        ** add vdbe code to break out of the processing loop after the 
94110
 
        ** first iteration (since the first iteration of the loop is 
94111
 
        ** guaranteed to operate on the row with the minimum or maximum 
94112
 
        ** value of x, the only row required).
94113
 
        **
94114
 
        ** A special flag must be passed to sqlite3WhereBegin() to slightly
94115
 
        ** modify behaviour as follows:
94116
 
        **
94117
 
        **   + If the query is a "SELECT min(x)", then the loop coded by
94118
 
        **     where.c should not iterate over any values with a NULL value
94119
 
        **     for x.
94120
 
        **
94121
 
        **   + The optimizer code in where.c (the thing that decides which
94122
 
        **     index or indices to use) should place a different priority on 
94123
 
        **     satisfying the 'ORDER BY' clause than it does in other cases.
94124
 
        **     Refer to code and comments in where.c for details.
94125
 
        */
94126
 
        ExprList *pMinMax = 0;
94127
 
        u8 flag = minMaxQuery(p);
94128
 
        if( flag ){
94129
 
          assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
94130
 
          pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
94131
 
          pDel = pMinMax;
94132
 
          if( pMinMax && !db->mallocFailed ){
94133
 
            pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
94134
 
            pMinMax->a[0].pExpr->op = TK_COLUMN;
94135
 
          }
94136
 
        }
94137
 
  
94138
 
        /* This case runs if the aggregate has no GROUP BY clause.  The
94139
 
        ** processing is much simpler since there is only a single row
94140
 
        ** of output.
94141
 
        */
94142
 
        resetAccumulator(pParse, &sAggInfo);
94143
 
        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax, flag);
94144
 
        if( pWInfo==0 ){
94145
 
          sqlite3ExprListDelete(db, pDel);
94146
 
          goto select_end;
94147
 
        }
94148
 
        updateAccumulator(pParse, &sAggInfo);
94149
 
        if( !pMinMax && flag ){
94150
 
          sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
94151
 
          VdbeComment((v, "%s() by index",
94152
 
                (flag==WHERE_ORDERBY_MIN?"min":"max")));
94153
 
        }
94154
 
        sqlite3WhereEnd(pWInfo);
94155
 
        finalizeAggFunctions(pParse, &sAggInfo);
94156
 
      }
94157
 
 
94158
 
      pOrderBy = 0;
94159
 
      sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
94160
 
      selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1, 
94161
 
                      pDest, addrEnd, addrEnd);
94162
 
      sqlite3ExprListDelete(db, pDel);
94163
 
    }
94164
 
    sqlite3VdbeResolveLabel(v, addrEnd);
94165
 
    
94166
 
  } /* endif aggregate query */
94167
 
 
94168
 
  if( distinct>=0 ){
94169
 
    explainTempTable(pParse, "DISTINCT");
94170
 
  }
94171
 
 
94172
 
  /* If there is an ORDER BY clause, then we need to sort the results
94173
 
  ** and send them to the callback one by one.
94174
 
  */
94175
 
  if( pOrderBy ){
94176
 
    explainTempTable(pParse, "ORDER BY");
94177
 
    generateSortTail(pParse, p, v, pEList->nExpr, pDest);
94178
 
  }
94179
 
 
94180
 
  /* Jump here to skip this query
94181
 
  */
94182
 
  sqlite3VdbeResolveLabel(v, iEnd);
94183
 
 
94184
 
  /* The SELECT was successfully coded.   Set the return code to 0
94185
 
  ** to indicate no errors.
94186
 
  */
94187
 
  rc = 0;
94188
 
 
94189
 
  /* Control jumps to here if an error is encountered above, or upon
94190
 
  ** successful coding of the SELECT.
94191
 
  */
94192
 
select_end:
94193
 
  explainSetInteger(pParse->iSelectId, iRestoreSelectId);
94194
 
 
94195
 
  /* Identify column names if results of the SELECT are to be output.
94196
 
  */
94197
 
  if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
94198
 
    generateColumnNames(pParse, pTabList, pEList);
94199
 
  }
94200
 
 
94201
 
  sqlite3DbFree(db, sAggInfo.aCol);
94202
 
  sqlite3DbFree(db, sAggInfo.aFunc);
94203
 
  return rc;
94204
 
}
94205
 
 
94206
 
#if defined(SQLITE_DEBUG)
94207
 
/*
94208
 
*******************************************************************************
94209
 
** The following code is used for testing and debugging only.  The code
94210
 
** that follows does not appear in normal builds.
94211
 
**
94212
 
** These routines are used to print out the content of all or part of a 
94213
 
** parse structures such as Select or Expr.  Such printouts are useful
94214
 
** for helping to understand what is happening inside the code generator
94215
 
** during the execution of complex SELECT statements.
94216
 
**
94217
 
** These routine are not called anywhere from within the normal
94218
 
** code base.  Then are intended to be called from within the debugger
94219
 
** or from temporary "printf" statements inserted for debugging.
94220
 
*/
94221
 
SQLITE_PRIVATE void sqlite3PrintExpr(Expr *p){
94222
 
  if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
94223
 
    sqlite3DebugPrintf("(%s", p->u.zToken);
94224
 
  }else{
94225
 
    sqlite3DebugPrintf("(%d", p->op);
94226
 
  }
94227
 
  if( p->pLeft ){
94228
 
    sqlite3DebugPrintf(" ");
94229
 
    sqlite3PrintExpr(p->pLeft);
94230
 
  }
94231
 
  if( p->pRight ){
94232
 
    sqlite3DebugPrintf(" ");
94233
 
    sqlite3PrintExpr(p->pRight);
94234
 
  }
94235
 
  sqlite3DebugPrintf(")");
94236
 
}
94237
 
SQLITE_PRIVATE void sqlite3PrintExprList(ExprList *pList){
94238
 
  int i;
94239
 
  for(i=0; i<pList->nExpr; i++){
94240
 
    sqlite3PrintExpr(pList->a[i].pExpr);
94241
 
    if( i<pList->nExpr-1 ){
94242
 
      sqlite3DebugPrintf(", ");
94243
 
    }
94244
 
  }
94245
 
}
94246
 
SQLITE_PRIVATE void sqlite3PrintSelect(Select *p, int indent){
94247
 
  sqlite3DebugPrintf("%*sSELECT(%p) ", indent, "", p);
94248
 
  sqlite3PrintExprList(p->pEList);
94249
 
  sqlite3DebugPrintf("\n");
94250
 
  if( p->pSrc ){
94251
 
    char *zPrefix;
94252
 
    int i;
94253
 
    zPrefix = "FROM";
94254
 
    for(i=0; i<p->pSrc->nSrc; i++){
94255
 
      struct SrcList_item *pItem = &p->pSrc->a[i];
94256
 
      sqlite3DebugPrintf("%*s ", indent+6, zPrefix);
94257
 
      zPrefix = "";
94258
 
      if( pItem->pSelect ){
94259
 
        sqlite3DebugPrintf("(\n");
94260
 
        sqlite3PrintSelect(pItem->pSelect, indent+10);
94261
 
        sqlite3DebugPrintf("%*s)", indent+8, "");
94262
 
      }else if( pItem->zName ){
94263
 
        sqlite3DebugPrintf("%s", pItem->zName);
94264
 
      }
94265
 
      if( pItem->pTab ){
94266
 
        sqlite3DebugPrintf("(table: %s)", pItem->pTab->zName);
94267
 
      }
94268
 
      if( pItem->zAlias ){
94269
 
        sqlite3DebugPrintf(" AS %s", pItem->zAlias);
94270
 
      }
94271
 
      if( i<p->pSrc->nSrc-1 ){
94272
 
        sqlite3DebugPrintf(",");
94273
 
      }
94274
 
      sqlite3DebugPrintf("\n");
94275
 
    }
94276
 
  }
94277
 
  if( p->pWhere ){
94278
 
    sqlite3DebugPrintf("%*s WHERE ", indent, "");
94279
 
    sqlite3PrintExpr(p->pWhere);
94280
 
    sqlite3DebugPrintf("\n");
94281
 
  }
94282
 
  if( p->pGroupBy ){
94283
 
    sqlite3DebugPrintf("%*s GROUP BY ", indent, "");
94284
 
    sqlite3PrintExprList(p->pGroupBy);
94285
 
    sqlite3DebugPrintf("\n");
94286
 
  }
94287
 
  if( p->pHaving ){
94288
 
    sqlite3DebugPrintf("%*s HAVING ", indent, "");
94289
 
    sqlite3PrintExpr(p->pHaving);
94290
 
    sqlite3DebugPrintf("\n");
94291
 
  }
94292
 
  if( p->pOrderBy ){
94293
 
    sqlite3DebugPrintf("%*s ORDER BY ", indent, "");
94294
 
    sqlite3PrintExprList(p->pOrderBy);
94295
 
    sqlite3DebugPrintf("\n");
94296
 
  }
94297
 
}
94298
 
/* End of the structure debug printing code
94299
 
*****************************************************************************/
94300
 
#endif /* defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
94301
 
 
94302
 
/************** End of select.c **********************************************/
94303
 
/************** Begin file table.c *******************************************/
94304
 
/*
94305
 
** 2001 September 15
94306
 
**
94307
 
** The author disclaims copyright to this source code.  In place of
94308
 
** a legal notice, here is a blessing:
94309
 
**
94310
 
**    May you do good and not evil.
94311
 
**    May you find forgiveness for yourself and forgive others.
94312
 
**    May you share freely, never taking more than you give.
94313
 
**
94314
 
*************************************************************************
94315
 
** This file contains the sqlite3_get_table() and sqlite3_free_table()
94316
 
** interface routines.  These are just wrappers around the main
94317
 
** interface routine of sqlite3_exec().
94318
 
**
94319
 
** These routines are in a separate files so that they will not be linked
94320
 
** if they are not used.
94321
 
*/
94322
 
 
94323
 
#ifndef SQLITE_OMIT_GET_TABLE
94324
 
 
94325
 
/*
94326
 
** This structure is used to pass data from sqlite3_get_table() through
94327
 
** to the callback function is uses to build the result.
94328
 
*/
94329
 
typedef struct TabResult {
94330
 
  char **azResult;   /* Accumulated output */
94331
 
  char *zErrMsg;     /* Error message text, if an error occurs */
94332
 
  int nAlloc;        /* Slots allocated for azResult[] */
94333
 
  int nRow;          /* Number of rows in the result */
94334
 
  int nColumn;       /* Number of columns in the result */
94335
 
  int nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
94336
 
  int rc;            /* Return code from sqlite3_exec() */
94337
 
} TabResult;
94338
 
 
94339
 
/*
94340
 
** This routine is called once for each row in the result table.  Its job
94341
 
** is to fill in the TabResult structure appropriately, allocating new
94342
 
** memory as necessary.
94343
 
*/
94344
 
static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
94345
 
  TabResult *p = (TabResult*)pArg;  /* Result accumulator */
94346
 
  int need;                         /* Slots needed in p->azResult[] */
94347
 
  int i;                            /* Loop counter */
94348
 
  char *z;                          /* A single column of result */
94349
 
 
94350
 
  /* Make sure there is enough space in p->azResult to hold everything
94351
 
  ** we need to remember from this invocation of the callback.
94352
 
  */
94353
 
  if( p->nRow==0 && argv!=0 ){
94354
 
    need = nCol*2;
94355
 
  }else{
94356
 
    need = nCol;
94357
 
  }
94358
 
  if( p->nData + need > p->nAlloc ){
94359
 
    char **azNew;
94360
 
    p->nAlloc = p->nAlloc*2 + need;
94361
 
    azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
94362
 
    if( azNew==0 ) goto malloc_failed;
94363
 
    p->azResult = azNew;
94364
 
  }
94365
 
 
94366
 
  /* If this is the first row, then generate an extra row containing
94367
 
  ** the names of all columns.
94368
 
  */
94369
 
  if( p->nRow==0 ){
94370
 
    p->nColumn = nCol;
94371
 
    for(i=0; i<nCol; i++){
94372
 
      z = sqlite3_mprintf("%s", colv[i]);
94373
 
      if( z==0 ) goto malloc_failed;
94374
 
      p->azResult[p->nData++] = z;
94375
 
    }
94376
 
  }else if( p->nColumn!=nCol ){
94377
 
    sqlite3_free(p->zErrMsg);
94378
 
    p->zErrMsg = sqlite3_mprintf(
94379
 
       "sqlite3_get_table() called with two or more incompatible queries"
94380
 
    );
94381
 
    p->rc = SQLITE_ERROR;
94382
 
    return 1;
94383
 
  }
94384
 
 
94385
 
  /* Copy over the row data
94386
 
  */
94387
 
  if( argv!=0 ){
94388
 
    for(i=0; i<nCol; i++){
94389
 
      if( argv[i]==0 ){
94390
 
        z = 0;
94391
 
      }else{
94392
 
        int n = sqlite3Strlen30(argv[i])+1;
94393
 
        z = sqlite3_malloc( n );
94394
 
        if( z==0 ) goto malloc_failed;
94395
 
        memcpy(z, argv[i], n);
94396
 
      }
94397
 
      p->azResult[p->nData++] = z;
94398
 
    }
94399
 
    p->nRow++;
94400
 
  }
94401
 
  return 0;
94402
 
 
94403
 
malloc_failed:
94404
 
  p->rc = SQLITE_NOMEM;
94405
 
  return 1;
94406
 
}
94407
 
 
94408
 
/*
94409
 
** Query the database.  But instead of invoking a callback for each row,
94410
 
** malloc() for space to hold the result and return the entire results
94411
 
** at the conclusion of the call.
94412
 
**
94413
 
** The result that is written to ***pazResult is held in memory obtained
94414
 
** from malloc().  But the caller cannot free this memory directly.  
94415
 
** Instead, the entire table should be passed to sqlite3_free_table() when
94416
 
** the calling procedure is finished using it.
94417
 
*/
94418
 
SQLITE_API int sqlite3_get_table(
94419
 
  sqlite3 *db,                /* The database on which the SQL executes */
94420
 
  const char *zSql,           /* The SQL to be executed */
94421
 
  char ***pazResult,          /* Write the result table here */
94422
 
  int *pnRow,                 /* Write the number of rows in the result here */
94423
 
  int *pnColumn,              /* Write the number of columns of result here */
94424
 
  char **pzErrMsg             /* Write error messages here */
94425
 
){
94426
 
  int rc;
94427
 
  TabResult res;
94428
 
 
94429
 
  *pazResult = 0;
94430
 
  if( pnColumn ) *pnColumn = 0;
94431
 
  if( pnRow ) *pnRow = 0;
94432
 
  if( pzErrMsg ) *pzErrMsg = 0;
94433
 
  res.zErrMsg = 0;
94434
 
  res.nRow = 0;
94435
 
  res.nColumn = 0;
94436
 
  res.nData = 1;
94437
 
  res.nAlloc = 20;
94438
 
  res.rc = SQLITE_OK;
94439
 
  res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
94440
 
  if( res.azResult==0 ){
94441
 
     db->errCode = SQLITE_NOMEM;
94442
 
     return SQLITE_NOMEM;
94443
 
  }
94444
 
  res.azResult[0] = 0;
94445
 
  rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
94446
 
  assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
94447
 
  res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
94448
 
  if( (rc&0xff)==SQLITE_ABORT ){
94449
 
    sqlite3_free_table(&res.azResult[1]);
94450
 
    if( res.zErrMsg ){
94451
 
      if( pzErrMsg ){
94452
 
        sqlite3_free(*pzErrMsg);
94453
 
        *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
94454
 
      }
94455
 
      sqlite3_free(res.zErrMsg);
94456
 
    }
94457
 
    db->errCode = res.rc;  /* Assume 32-bit assignment is atomic */
94458
 
    return res.rc;
94459
 
  }
94460
 
  sqlite3_free(res.zErrMsg);
94461
 
  if( rc!=SQLITE_OK ){
94462
 
    sqlite3_free_table(&res.azResult[1]);
94463
 
    return rc;
94464
 
  }
94465
 
  if( res.nAlloc>res.nData ){
94466
 
    char **azNew;
94467
 
    azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
94468
 
    if( azNew==0 ){
94469
 
      sqlite3_free_table(&res.azResult[1]);
94470
 
      db->errCode = SQLITE_NOMEM;
94471
 
      return SQLITE_NOMEM;
94472
 
    }
94473
 
    res.azResult = azNew;
94474
 
  }
94475
 
  *pazResult = &res.azResult[1];
94476
 
  if( pnColumn ) *pnColumn = res.nColumn;
94477
 
  if( pnRow ) *pnRow = res.nRow;
94478
 
  return rc;
94479
 
}
94480
 
 
94481
 
/*
94482
 
** This routine frees the space the sqlite3_get_table() malloced.
94483
 
*/
94484
 
SQLITE_API void sqlite3_free_table(
94485
 
  char **azResult            /* Result returned from from sqlite3_get_table() */
94486
 
){
94487
 
  if( azResult ){
94488
 
    int i, n;
94489
 
    azResult--;
94490
 
    assert( azResult!=0 );
94491
 
    n = SQLITE_PTR_TO_INT(azResult[0]);
94492
 
    for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
94493
 
    sqlite3_free(azResult);
94494
 
  }
94495
 
}
94496
 
 
94497
 
#endif /* SQLITE_OMIT_GET_TABLE */
94498
 
 
94499
 
/************** End of table.c ***********************************************/
94500
 
/************** Begin file trigger.c *****************************************/
94501
 
/*
94502
 
**
94503
 
** The author disclaims copyright to this source code.  In place of
94504
 
** a legal notice, here is a blessing:
94505
 
**
94506
 
**    May you do good and not evil.
94507
 
**    May you find forgiveness for yourself and forgive others.
94508
 
**    May you share freely, never taking more than you give.
94509
 
**
94510
 
*************************************************************************
94511
 
** This file contains the implementation for TRIGGERs
94512
 
*/
94513
 
 
94514
 
#ifndef SQLITE_OMIT_TRIGGER
94515
 
/*
94516
 
** Delete a linked list of TriggerStep structures.
94517
 
*/
94518
 
SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
94519
 
  while( pTriggerStep ){
94520
 
    TriggerStep * pTmp = pTriggerStep;
94521
 
    pTriggerStep = pTriggerStep->pNext;
94522
 
 
94523
 
    sqlite3ExprDelete(db, pTmp->pWhere);
94524
 
    sqlite3ExprListDelete(db, pTmp->pExprList);
94525
 
    sqlite3SelectDelete(db, pTmp->pSelect);
94526
 
    sqlite3IdListDelete(db, pTmp->pIdList);
94527
 
 
94528
 
    sqlite3DbFree(db, pTmp);
94529
 
  }
94530
 
}
94531
 
 
94532
 
/*
94533
 
** Given table pTab, return a list of all the triggers attached to 
94534
 
** the table. The list is connected by Trigger.pNext pointers.
94535
 
**
94536
 
** All of the triggers on pTab that are in the same database as pTab
94537
 
** are already attached to pTab->pTrigger.  But there might be additional
94538
 
** triggers on pTab in the TEMP schema.  This routine prepends all
94539
 
** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
94540
 
** and returns the combined list.
94541
 
**
94542
 
** To state it another way:  This routine returns a list of all triggers
94543
 
** that fire off of pTab.  The list will include any TEMP triggers on
94544
 
** pTab as well as the triggers lised in pTab->pTrigger.
94545
 
*/
94546
 
SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
94547
 
  Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
94548
 
  Trigger *pList = 0;                  /* List of triggers to return */
94549
 
 
94550
 
  if( pParse->disableTriggers ){
94551
 
    return 0;
94552
 
  }
94553
 
 
94554
 
  if( pTmpSchema!=pTab->pSchema ){
94555
 
    HashElem *p;
94556
 
    assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
94557
 
    for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
94558
 
      Trigger *pTrig = (Trigger *)sqliteHashData(p);
94559
 
      if( pTrig->pTabSchema==pTab->pSchema
94560
 
       && 0==sqlite3StrICmp(pTrig->table, pTab->zName) 
94561
 
      ){
94562
 
        pTrig->pNext = (pList ? pList : pTab->pTrigger);
94563
 
        pList = pTrig;
94564
 
      }
94565
 
    }
94566
 
  }
94567
 
 
94568
 
  return (pList ? pList : pTab->pTrigger);
94569
 
}
94570
 
 
94571
 
/*
94572
 
** This is called by the parser when it sees a CREATE TRIGGER statement
94573
 
** up to the point of the BEGIN before the trigger actions.  A Trigger
94574
 
** structure is generated based on the information available and stored
94575
 
** in pParse->pNewTrigger.  After the trigger actions have been parsed, the
94576
 
** sqlite3FinishTrigger() function is called to complete the trigger
94577
 
** construction process.
94578
 
*/
94579
 
SQLITE_PRIVATE void sqlite3BeginTrigger(
94580
 
  Parse *pParse,      /* The parse context of the CREATE TRIGGER statement */
94581
 
  Token *pName1,      /* The name of the trigger */
94582
 
  Token *pName2,      /* The name of the trigger */
94583
 
  int tr_tm,          /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
94584
 
  int op,             /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
94585
 
  IdList *pColumns,   /* column list if this is an UPDATE OF trigger */
94586
 
  SrcList *pTableName,/* The name of the table/view the trigger applies to */
94587
 
  Expr *pWhen,        /* WHEN clause */
94588
 
  int isTemp,         /* True if the TEMPORARY keyword is present */
94589
 
  int noErr           /* Suppress errors if the trigger already exists */
94590
 
){
94591
 
  Trigger *pTrigger = 0;  /* The new trigger */
94592
 
  Table *pTab;            /* Table that the trigger fires off of */
94593
 
  char *zName = 0;        /* Name of the trigger */
94594
 
  sqlite3 *db = pParse->db;  /* The database connection */
94595
 
  int iDb;                /* The database to store the trigger in */
94596
 
  Token *pName;           /* The unqualified db name */
94597
 
  DbFixer sFix;           /* State vector for the DB fixer */
94598
 
  int iTabDb;             /* Index of the database holding pTab */
94599
 
 
94600
 
  assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
94601
 
  assert( pName2!=0 );
94602
 
  assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
94603
 
  assert( op>0 && op<0xff );
94604
 
  if( isTemp ){
94605
 
    /* If TEMP was specified, then the trigger name may not be qualified. */
94606
 
    if( pName2->n>0 ){
94607
 
      sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
94608
 
      goto trigger_cleanup;
94609
 
    }
94610
 
    iDb = 1;
94611
 
    pName = pName1;
94612
 
  }else{
94613
 
    /* Figure out the db that the the trigger will be created in */
94614
 
    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
94615
 
    if( iDb<0 ){
94616
 
      goto trigger_cleanup;
94617
 
    }
94618
 
  }
94619
 
 
94620
 
  /* If the trigger name was unqualified, and the table is a temp table,
94621
 
  ** then set iDb to 1 to create the trigger in the temporary database.
94622
 
  ** If sqlite3SrcListLookup() returns 0, indicating the table does not
94623
 
  ** exist, the error is caught by the block below.
94624
 
  */
94625
 
  if( !pTableName || db->mallocFailed ){
94626
 
    goto trigger_cleanup;
94627
 
  }
94628
 
  pTab = sqlite3SrcListLookup(pParse, pTableName);
94629
 
  if( db->init.busy==0 && pName2->n==0 && pTab
94630
 
        && pTab->pSchema==db->aDb[1].pSchema ){
94631
 
    iDb = 1;
94632
 
  }
94633
 
 
94634
 
  /* Ensure the table name matches database name and that the table exists */
94635
 
  if( db->mallocFailed ) goto trigger_cleanup;
94636
 
  assert( pTableName->nSrc==1 );
94637
 
  if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && 
94638
 
      sqlite3FixSrcList(&sFix, pTableName) ){
94639
 
    goto trigger_cleanup;
94640
 
  }
94641
 
  pTab = sqlite3SrcListLookup(pParse, pTableName);
94642
 
  if( !pTab ){
94643
 
    /* The table does not exist. */
94644
 
    if( db->init.iDb==1 ){
94645
 
      /* Ticket #3810.
94646
 
      ** Normally, whenever a table is dropped, all associated triggers are
94647
 
      ** dropped too.  But if a TEMP trigger is created on a non-TEMP table
94648
 
      ** and the table is dropped by a different database connection, the
94649
 
      ** trigger is not visible to the database connection that does the
94650
 
      ** drop so the trigger cannot be dropped.  This results in an
94651
 
      ** "orphaned trigger" - a trigger whose associated table is missing.
94652
 
      */
94653
 
      db->init.orphanTrigger = 1;
94654
 
    }
94655
 
    goto trigger_cleanup;
94656
 
  }
94657
 
  if( IsVirtual(pTab) ){
94658
 
    sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
94659
 
    goto trigger_cleanup;
94660
 
  }
94661
 
 
94662
 
  /* Check that the trigger name is not reserved and that no trigger of the
94663
 
  ** specified name exists */
94664
 
  zName = sqlite3NameFromToken(db, pName);
94665
 
  if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
94666
 
    goto trigger_cleanup;
94667
 
  }
94668
 
  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
94669
 
  if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
94670
 
                      zName, sqlite3Strlen30(zName)) ){
94671
 
    if( !noErr ){
94672
 
      sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
94673
 
    }else{
94674
 
      assert( !db->init.busy );
94675
 
      sqlite3CodeVerifySchema(pParse, iDb);
94676
 
    }
94677
 
    goto trigger_cleanup;
94678
 
  }
94679
 
 
94680
 
  /* Do not create a trigger on a system table */
94681
 
  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
94682
 
    sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
94683
 
    pParse->nErr++;
94684
 
    goto trigger_cleanup;
94685
 
  }
94686
 
 
94687
 
  /* INSTEAD of triggers are only for views and views only support INSTEAD
94688
 
  ** of triggers.
94689
 
  */
94690
 
  if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
94691
 
    sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S", 
94692
 
        (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
94693
 
    goto trigger_cleanup;
94694
 
  }
94695
 
  if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
94696
 
    sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
94697
 
        " trigger on table: %S", pTableName, 0);
94698
 
    goto trigger_cleanup;
94699
 
  }
94700
 
  iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
94701
 
 
94702
 
#ifndef SQLITE_OMIT_AUTHORIZATION
94703
 
  {
94704
 
    int code = SQLITE_CREATE_TRIGGER;
94705
 
    const char *zDb = db->aDb[iTabDb].zName;
94706
 
    const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
94707
 
    if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
94708
 
    if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
94709
 
      goto trigger_cleanup;
94710
 
    }
94711
 
    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
94712
 
      goto trigger_cleanup;
94713
 
    }
94714
 
  }
94715
 
#endif
94716
 
 
94717
 
  /* INSTEAD OF triggers can only appear on views and BEFORE triggers
94718
 
  ** cannot appear on views.  So we might as well translate every
94719
 
  ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
94720
 
  ** elsewhere.
94721
 
  */
94722
 
  if (tr_tm == TK_INSTEAD){
94723
 
    tr_tm = TK_BEFORE;
94724
 
  }
94725
 
 
94726
 
  /* Build the Trigger object */
94727
 
  pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
94728
 
  if( pTrigger==0 ) goto trigger_cleanup;
94729
 
  pTrigger->zName = zName;
94730
 
  zName = 0;
94731
 
  pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
94732
 
  pTrigger->pSchema = db->aDb[iDb].pSchema;
94733
 
  pTrigger->pTabSchema = pTab->pSchema;
94734
 
  pTrigger->op = (u8)op;
94735
 
  pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
94736
 
  pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
94737
 
  pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
94738
 
  assert( pParse->pNewTrigger==0 );
94739
 
  pParse->pNewTrigger = pTrigger;
94740
 
 
94741
 
trigger_cleanup:
94742
 
  sqlite3DbFree(db, zName);
94743
 
  sqlite3SrcListDelete(db, pTableName);
94744
 
  sqlite3IdListDelete(db, pColumns);
94745
 
  sqlite3ExprDelete(db, pWhen);
94746
 
  if( !pParse->pNewTrigger ){
94747
 
    sqlite3DeleteTrigger(db, pTrigger);
94748
 
  }else{
94749
 
    assert( pParse->pNewTrigger==pTrigger );
94750
 
  }
94751
 
}
94752
 
 
94753
 
/*
94754
 
** This routine is called after all of the trigger actions have been parsed
94755
 
** in order to complete the process of building the trigger.
94756
 
*/
94757
 
SQLITE_PRIVATE void sqlite3FinishTrigger(
94758
 
  Parse *pParse,          /* Parser context */
94759
 
  TriggerStep *pStepList, /* The triggered program */
94760
 
  Token *pAll             /* Token that describes the complete CREATE TRIGGER */
94761
 
){
94762
 
  Trigger *pTrig = pParse->pNewTrigger;   /* Trigger being finished */
94763
 
  char *zName;                            /* Name of trigger */
94764
 
  sqlite3 *db = pParse->db;               /* The database */
94765
 
  DbFixer sFix;                           /* Fixer object */
94766
 
  int iDb;                                /* Database containing the trigger */
94767
 
  Token nameToken;                        /* Trigger name for error reporting */
94768
 
 
94769
 
  pParse->pNewTrigger = 0;
94770
 
  if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
94771
 
  zName = pTrig->zName;
94772
 
  iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
94773
 
  pTrig->step_list = pStepList;
94774
 
  while( pStepList ){
94775
 
    pStepList->pTrig = pTrig;
94776
 
    pStepList = pStepList->pNext;
94777
 
  }
94778
 
  nameToken.z = pTrig->zName;
94779
 
  nameToken.n = sqlite3Strlen30(nameToken.z);
94780
 
  if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken) 
94781
 
          && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
94782
 
    goto triggerfinish_cleanup;
94783
 
  }
94784
 
 
94785
 
  /* if we are not initializing,
94786
 
  ** build the sqlite_master entry
94787
 
  */
94788
 
  if( !db->init.busy ){
94789
 
    Vdbe *v;
94790
 
    char *z;
94791
 
 
94792
 
    /* Make an entry in the sqlite_master table */
94793
 
    v = sqlite3GetVdbe(pParse);
94794
 
    if( v==0 ) goto triggerfinish_cleanup;
94795
 
    sqlite3BeginWriteOperation(pParse, 0, iDb);
94796
 
    z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
94797
 
    sqlite3NestedParse(pParse,
94798
 
       "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
94799
 
       db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
94800
 
       pTrig->table, z);
94801
 
    sqlite3DbFree(db, z);
94802
 
    sqlite3ChangeCookie(pParse, iDb);
94803
 
    sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
94804
 
        db, "type='trigger' AND name='%q'", zName), P4_DYNAMIC
94805
 
    );
94806
 
  }
94807
 
 
94808
 
  if( db->init.busy ){
94809
 
    Trigger *pLink = pTrig;
94810
 
    Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
94811
 
    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
94812
 
    pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
94813
 
    if( pTrig ){
94814
 
      db->mallocFailed = 1;
94815
 
    }else if( pLink->pSchema==pLink->pTabSchema ){
94816
 
      Table *pTab;
94817
 
      int n = sqlite3Strlen30(pLink->table);
94818
 
      pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
94819
 
      assert( pTab!=0 );
94820
 
      pLink->pNext = pTab->pTrigger;
94821
 
      pTab->pTrigger = pLink;
94822
 
    }
94823
 
  }
94824
 
 
94825
 
triggerfinish_cleanup:
94826
 
  sqlite3DeleteTrigger(db, pTrig);
94827
 
  assert( !pParse->pNewTrigger );
94828
 
  sqlite3DeleteTriggerStep(db, pStepList);
94829
 
}
94830
 
 
94831
 
/*
94832
 
** Turn a SELECT statement (that the pSelect parameter points to) into
94833
 
** a trigger step.  Return a pointer to a TriggerStep structure.
94834
 
**
94835
 
** The parser calls this routine when it finds a SELECT statement in
94836
 
** body of a TRIGGER.  
94837
 
*/
94838
 
SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
94839
 
  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
94840
 
  if( pTriggerStep==0 ) {
94841
 
    sqlite3SelectDelete(db, pSelect);
94842
 
    return 0;
94843
 
  }
94844
 
  pTriggerStep->op = TK_SELECT;
94845
 
  pTriggerStep->pSelect = pSelect;
94846
 
  pTriggerStep->orconf = OE_Default;
94847
 
  return pTriggerStep;
94848
 
}
94849
 
 
94850
 
/*
94851
 
** Allocate space to hold a new trigger step.  The allocated space
94852
 
** holds both the TriggerStep object and the TriggerStep.target.z string.
94853
 
**
94854
 
** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
94855
 
*/
94856
 
static TriggerStep *triggerStepAllocate(
94857
 
  sqlite3 *db,                /* Database connection */
94858
 
  u8 op,                      /* Trigger opcode */
94859
 
  Token *pName                /* The target name */
94860
 
){
94861
 
  TriggerStep *pTriggerStep;
94862
 
 
94863
 
  pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
94864
 
  if( pTriggerStep ){
94865
 
    char *z = (char*)&pTriggerStep[1];
94866
 
    memcpy(z, pName->z, pName->n);
94867
 
    pTriggerStep->target.z = z;
94868
 
    pTriggerStep->target.n = pName->n;
94869
 
    pTriggerStep->op = op;
94870
 
  }
94871
 
  return pTriggerStep;
94872
 
}
94873
 
 
94874
 
/*
94875
 
** Build a trigger step out of an INSERT statement.  Return a pointer
94876
 
** to the new trigger step.
94877
 
**
94878
 
** The parser calls this routine when it sees an INSERT inside the
94879
 
** body of a trigger.
94880
 
*/
94881
 
SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
94882
 
  sqlite3 *db,        /* The database connection */
94883
 
  Token *pTableName,  /* Name of the table into which we insert */
94884
 
  IdList *pColumn,    /* List of columns in pTableName to insert into */
94885
 
  ExprList *pEList,   /* The VALUE clause: a list of values to be inserted */
94886
 
  Select *pSelect,    /* A SELECT statement that supplies values */
94887
 
  u8 orconf           /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
94888
 
){
94889
 
  TriggerStep *pTriggerStep;
94890
 
 
94891
 
  assert(pEList == 0 || pSelect == 0);
94892
 
  assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
94893
 
 
94894
 
  pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
94895
 
  if( pTriggerStep ){
94896
 
    pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
94897
 
    pTriggerStep->pIdList = pColumn;
94898
 
    pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
94899
 
    pTriggerStep->orconf = orconf;
94900
 
  }else{
94901
 
    sqlite3IdListDelete(db, pColumn);
94902
 
  }
94903
 
  sqlite3ExprListDelete(db, pEList);
94904
 
  sqlite3SelectDelete(db, pSelect);
94905
 
 
94906
 
  return pTriggerStep;
94907
 
}
94908
 
 
94909
 
/*
94910
 
** Construct a trigger step that implements an UPDATE statement and return
94911
 
** a pointer to that trigger step.  The parser calls this routine when it
94912
 
** sees an UPDATE statement inside the body of a CREATE TRIGGER.
94913
 
*/
94914
 
SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
94915
 
  sqlite3 *db,         /* The database connection */
94916
 
  Token *pTableName,   /* Name of the table to be updated */
94917
 
  ExprList *pEList,    /* The SET clause: list of column and new values */
94918
 
  Expr *pWhere,        /* The WHERE clause */
94919
 
  u8 orconf            /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
94920
 
){
94921
 
  TriggerStep *pTriggerStep;
94922
 
 
94923
 
  pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
94924
 
  if( pTriggerStep ){
94925
 
    pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
94926
 
    pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
94927
 
    pTriggerStep->orconf = orconf;
94928
 
  }
94929
 
  sqlite3ExprListDelete(db, pEList);
94930
 
  sqlite3ExprDelete(db, pWhere);
94931
 
  return pTriggerStep;
94932
 
}
94933
 
 
94934
 
/*
94935
 
** Construct a trigger step that implements a DELETE statement and return
94936
 
** a pointer to that trigger step.  The parser calls this routine when it
94937
 
** sees a DELETE statement inside the body of a CREATE TRIGGER.
94938
 
*/
94939
 
SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
94940
 
  sqlite3 *db,            /* Database connection */
94941
 
  Token *pTableName,      /* The table from which rows are deleted */
94942
 
  Expr *pWhere            /* The WHERE clause */
94943
 
){
94944
 
  TriggerStep *pTriggerStep;
94945
 
 
94946
 
  pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
94947
 
  if( pTriggerStep ){
94948
 
    pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
94949
 
    pTriggerStep->orconf = OE_Default;
94950
 
  }
94951
 
  sqlite3ExprDelete(db, pWhere);
94952
 
  return pTriggerStep;
94953
 
}
94954
 
 
94955
 
/* 
94956
 
** Recursively delete a Trigger structure
94957
 
*/
94958
 
SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
94959
 
  if( pTrigger==0 ) return;
94960
 
  sqlite3DeleteTriggerStep(db, pTrigger->step_list);
94961
 
  sqlite3DbFree(db, pTrigger->zName);
94962
 
  sqlite3DbFree(db, pTrigger->table);
94963
 
  sqlite3ExprDelete(db, pTrigger->pWhen);
94964
 
  sqlite3IdListDelete(db, pTrigger->pColumns);
94965
 
  sqlite3DbFree(db, pTrigger);
94966
 
}
94967
 
 
94968
 
/*
94969
 
** This function is called to drop a trigger from the database schema. 
94970
 
**
94971
 
** This may be called directly from the parser and therefore identifies
94972
 
** the trigger by name.  The sqlite3DropTriggerPtr() routine does the
94973
 
** same job as this routine except it takes a pointer to the trigger
94974
 
** instead of the trigger name.
94975
 
**/
94976
 
SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
94977
 
  Trigger *pTrigger = 0;
94978
 
  int i;
94979
 
  const char *zDb;
94980
 
  const char *zName;
94981
 
  int nName;
94982
 
  sqlite3 *db = pParse->db;
94983
 
 
94984
 
  if( db->mallocFailed ) goto drop_trigger_cleanup;
94985
 
  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
94986
 
    goto drop_trigger_cleanup;
94987
 
  }
94988
 
 
94989
 
  assert( pName->nSrc==1 );
94990
 
  zDb = pName->a[0].zDatabase;
94991
 
  zName = pName->a[0].zName;
94992
 
  nName = sqlite3Strlen30(zName);
94993
 
  assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
94994
 
  for(i=OMIT_TEMPDB; i<db->nDb; i++){
94995
 
    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
94996
 
    if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
94997
 
    assert( sqlite3SchemaMutexHeld(db, j, 0) );
94998
 
    pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
94999
 
    if( pTrigger ) break;
95000
 
  }
95001
 
  if( !pTrigger ){
95002
 
    if( !noErr ){
95003
 
      sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
95004
 
    }else{
95005
 
      sqlite3CodeVerifyNamedSchema(pParse, zDb);
95006
 
    }
95007
 
    pParse->checkSchema = 1;
95008
 
    goto drop_trigger_cleanup;
95009
 
  }
95010
 
  sqlite3DropTriggerPtr(pParse, pTrigger);
95011
 
 
95012
 
drop_trigger_cleanup:
95013
 
  sqlite3SrcListDelete(db, pName);
95014
 
}
95015
 
 
95016
 
/*
95017
 
** Return a pointer to the Table structure for the table that a trigger
95018
 
** is set on.
95019
 
*/
95020
 
static Table *tableOfTrigger(Trigger *pTrigger){
95021
 
  int n = sqlite3Strlen30(pTrigger->table);
95022
 
  return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
95023
 
}
95024
 
 
95025
 
 
95026
 
/*
95027
 
** Drop a trigger given a pointer to that trigger. 
95028
 
*/
95029
 
SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
95030
 
  Table   *pTable;
95031
 
  Vdbe *v;
95032
 
  sqlite3 *db = pParse->db;
95033
 
  int iDb;
95034
 
 
95035
 
  iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
95036
 
  assert( iDb>=0 && iDb<db->nDb );
95037
 
  pTable = tableOfTrigger(pTrigger);
95038
 
  assert( pTable );
95039
 
  assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
95040
 
#ifndef SQLITE_OMIT_AUTHORIZATION
95041
 
  {
95042
 
    int code = SQLITE_DROP_TRIGGER;
95043
 
    const char *zDb = db->aDb[iDb].zName;
95044
 
    const char *zTab = SCHEMA_TABLE(iDb);
95045
 
    if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
95046
 
    if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
95047
 
      sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
95048
 
      return;
95049
 
    }
95050
 
  }
95051
 
#endif
95052
 
 
95053
 
  /* Generate code to destroy the database record of the trigger.
95054
 
  */
95055
 
  assert( pTable!=0 );
95056
 
  if( (v = sqlite3GetVdbe(pParse))!=0 ){
95057
 
    int base;
95058
 
    static const VdbeOpList dropTrigger[] = {
95059
 
      { OP_Rewind,     0, ADDR(9),  0},
95060
 
      { OP_String8,    0, 1,        0}, /* 1 */
95061
 
      { OP_Column,     0, 1,        2},
95062
 
      { OP_Ne,         2, ADDR(8),  1},
95063
 
      { OP_String8,    0, 1,        0}, /* 4: "trigger" */
95064
 
      { OP_Column,     0, 0,        2},
95065
 
      { OP_Ne,         2, ADDR(8),  1},
95066
 
      { OP_Delete,     0, 0,        0},
95067
 
      { OP_Next,       0, ADDR(1),  0}, /* 8 */
95068
 
    };
95069
 
 
95070
 
    sqlite3BeginWriteOperation(pParse, 0, iDb);
95071
 
    sqlite3OpenMasterTable(pParse, iDb);
95072
 
    base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger);
95073
 
    sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
95074
 
    sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
95075
 
    sqlite3ChangeCookie(pParse, iDb);
95076
 
    sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
95077
 
    sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
95078
 
    if( pParse->nMem<3 ){
95079
 
      pParse->nMem = 3;
95080
 
    }
95081
 
  }
95082
 
}
95083
 
 
95084
 
/*
95085
 
** Remove a trigger from the hash tables of the sqlite* pointer.
95086
 
*/
95087
 
SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
95088
 
  Trigger *pTrigger;
95089
 
  Hash *pHash;
95090
 
 
95091
 
  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
95092
 
  pHash = &(db->aDb[iDb].pSchema->trigHash);
95093
 
  pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
95094
 
  if( ALWAYS(pTrigger) ){
95095
 
    if( pTrigger->pSchema==pTrigger->pTabSchema ){
95096
 
      Table *pTab = tableOfTrigger(pTrigger);
95097
 
      Trigger **pp;
95098
 
      for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
95099
 
      *pp = (*pp)->pNext;
95100
 
    }
95101
 
    sqlite3DeleteTrigger(db, pTrigger);
95102
 
    db->flags |= SQLITE_InternChanges;
95103
 
  }
95104
 
}
95105
 
 
95106
 
/*
95107
 
** pEList is the SET clause of an UPDATE statement.  Each entry
95108
 
** in pEList is of the format <id>=<expr>.  If any of the entries
95109
 
** in pEList have an <id> which matches an identifier in pIdList,
95110
 
** then return TRUE.  If pIdList==NULL, then it is considered a
95111
 
** wildcard that matches anything.  Likewise if pEList==NULL then
95112
 
** it matches anything so always return true.  Return false only
95113
 
** if there is no match.
95114
 
*/
95115
 
static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
95116
 
  int e;
95117
 
  if( pIdList==0 || NEVER(pEList==0) ) return 1;
95118
 
  for(e=0; e<pEList->nExpr; e++){
95119
 
    if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
95120
 
  }
95121
 
  return 0; 
95122
 
}
95123
 
 
95124
 
/*
95125
 
** Return a list of all triggers on table pTab if there exists at least
95126
 
** one trigger that must be fired when an operation of type 'op' is 
95127
 
** performed on the table, and, if that operation is an UPDATE, if at
95128
 
** least one of the columns in pChanges is being modified.
95129
 
*/
95130
 
SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
95131
 
  Parse *pParse,          /* Parse context */
95132
 
  Table *pTab,            /* The table the contains the triggers */
95133
 
  int op,                 /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
95134
 
  ExprList *pChanges,     /* Columns that change in an UPDATE statement */
95135
 
  int *pMask              /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
95136
 
){
95137
 
  int mask = 0;
95138
 
  Trigger *pList = 0;
95139
 
  Trigger *p;
95140
 
 
95141
 
  if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
95142
 
    pList = sqlite3TriggerList(pParse, pTab);
95143
 
  }
95144
 
  assert( pList==0 || IsVirtual(pTab)==0 );
95145
 
  for(p=pList; p; p=p->pNext){
95146
 
    if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
95147
 
      mask |= p->tr_tm;
95148
 
    }
95149
 
  }
95150
 
  if( pMask ){
95151
 
    *pMask = mask;
95152
 
  }
95153
 
  return (mask ? pList : 0);
95154
 
}
95155
 
 
95156
 
/*
95157
 
** Convert the pStep->target token into a SrcList and return a pointer
95158
 
** to that SrcList.
95159
 
**
95160
 
** This routine adds a specific database name, if needed, to the target when
95161
 
** forming the SrcList.  This prevents a trigger in one database from
95162
 
** referring to a target in another database.  An exception is when the
95163
 
** trigger is in TEMP in which case it can refer to any other database it
95164
 
** wants.
95165
 
*/
95166
 
static SrcList *targetSrcList(
95167
 
  Parse *pParse,       /* The parsing context */
95168
 
  TriggerStep *pStep   /* The trigger containing the target token */
95169
 
){
95170
 
  int iDb;             /* Index of the database to use */
95171
 
  SrcList *pSrc;       /* SrcList to be returned */
95172
 
 
95173
 
  pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
95174
 
  if( pSrc ){
95175
 
    assert( pSrc->nSrc>0 );
95176
 
    assert( pSrc->a!=0 );
95177
 
    iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
95178
 
    if( iDb==0 || iDb>=2 ){
95179
 
      sqlite3 *db = pParse->db;
95180
 
      assert( iDb<pParse->db->nDb );
95181
 
      pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
95182
 
    }
95183
 
  }
95184
 
  return pSrc;
95185
 
}
95186
 
 
95187
 
/*
95188
 
** Generate VDBE code for the statements inside the body of a single 
95189
 
** trigger.
95190
 
*/
95191
 
static int codeTriggerProgram(
95192
 
  Parse *pParse,            /* The parser context */
95193
 
  TriggerStep *pStepList,   /* List of statements inside the trigger body */
95194
 
  int orconf                /* Conflict algorithm. (OE_Abort, etc) */  
95195
 
){
95196
 
  TriggerStep *pStep;
95197
 
  Vdbe *v = pParse->pVdbe;
95198
 
  sqlite3 *db = pParse->db;
95199
 
 
95200
 
  assert( pParse->pTriggerTab && pParse->pToplevel );
95201
 
  assert( pStepList );
95202
 
  assert( v!=0 );
95203
 
  for(pStep=pStepList; pStep; pStep=pStep->pNext){
95204
 
    /* Figure out the ON CONFLICT policy that will be used for this step
95205
 
    ** of the trigger program. If the statement that caused this trigger
95206
 
    ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
95207
 
    ** the ON CONFLICT policy that was specified as part of the trigger
95208
 
    ** step statement. Example:
95209
 
    **
95210
 
    **   CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
95211
 
    **     INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
95212
 
    **   END;
95213
 
    **
95214
 
    **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
95215
 
    **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
95216
 
    */
95217
 
    pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
95218
 
 
95219
 
    switch( pStep->op ){
95220
 
      case TK_UPDATE: {
95221
 
        sqlite3Update(pParse, 
95222
 
          targetSrcList(pParse, pStep),
95223
 
          sqlite3ExprListDup(db, pStep->pExprList, 0), 
95224
 
          sqlite3ExprDup(db, pStep->pWhere, 0), 
95225
 
          pParse->eOrconf
95226
 
        );
95227
 
        break;
95228
 
      }
95229
 
      case TK_INSERT: {
95230
 
        sqlite3Insert(pParse, 
95231
 
          targetSrcList(pParse, pStep),
95232
 
          sqlite3ExprListDup(db, pStep->pExprList, 0), 
95233
 
          sqlite3SelectDup(db, pStep->pSelect, 0), 
95234
 
          sqlite3IdListDup(db, pStep->pIdList), 
95235
 
          pParse->eOrconf
95236
 
        );
95237
 
        break;
95238
 
      }
95239
 
      case TK_DELETE: {
95240
 
        sqlite3DeleteFrom(pParse, 
95241
 
          targetSrcList(pParse, pStep),
95242
 
          sqlite3ExprDup(db, pStep->pWhere, 0)
95243
 
        );
95244
 
        break;
95245
 
      }
95246
 
      default: assert( pStep->op==TK_SELECT ); {
95247
 
        SelectDest sDest;
95248
 
        Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
95249
 
        sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
95250
 
        sqlite3Select(pParse, pSelect, &sDest);
95251
 
        sqlite3SelectDelete(db, pSelect);
95252
 
        break;
95253
 
      }
95254
 
    } 
95255
 
    if( pStep->op!=TK_SELECT ){
95256
 
      sqlite3VdbeAddOp0(v, OP_ResetCount);
95257
 
    }
95258
 
  }
95259
 
 
95260
 
  return 0;
95261
 
}
95262
 
 
95263
 
#ifdef SQLITE_DEBUG
95264
 
/*
95265
 
** This function is used to add VdbeComment() annotations to a VDBE
95266
 
** program. It is not used in production code, only for debugging.
95267
 
*/
95268
 
static const char *onErrorText(int onError){
95269
 
  switch( onError ){
95270
 
    case OE_Abort:    return "abort";
95271
 
    case OE_Rollback: return "rollback";
95272
 
    case OE_Fail:     return "fail";
95273
 
    case OE_Replace:  return "replace";
95274
 
    case OE_Ignore:   return "ignore";
95275
 
    case OE_Default:  return "default";
95276
 
  }
95277
 
  return "n/a";
95278
 
}
95279
 
#endif
95280
 
 
95281
 
/*
95282
 
** Parse context structure pFrom has just been used to create a sub-vdbe
95283
 
** (trigger program). If an error has occurred, transfer error information
95284
 
** from pFrom to pTo.
95285
 
*/
95286
 
static void transferParseError(Parse *pTo, Parse *pFrom){
95287
 
  assert( pFrom->zErrMsg==0 || pFrom->nErr );
95288
 
  assert( pTo->zErrMsg==0 || pTo->nErr );
95289
 
  if( pTo->nErr==0 ){
95290
 
    pTo->zErrMsg = pFrom->zErrMsg;
95291
 
    pTo->nErr = pFrom->nErr;
95292
 
  }else{
95293
 
    sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
95294
 
  }
95295
 
}
95296
 
 
95297
 
/*
95298
 
** Create and populate a new TriggerPrg object with a sub-program 
95299
 
** implementing trigger pTrigger with ON CONFLICT policy orconf.
95300
 
*/
95301
 
static TriggerPrg *codeRowTrigger(
95302
 
  Parse *pParse,       /* Current parse context */
95303
 
  Trigger *pTrigger,   /* Trigger to code */
95304
 
  Table *pTab,         /* The table pTrigger is attached to */
95305
 
  int orconf           /* ON CONFLICT policy to code trigger program with */
95306
 
){
95307
 
  Parse *pTop = sqlite3ParseToplevel(pParse);
95308
 
  sqlite3 *db = pParse->db;   /* Database handle */
95309
 
  TriggerPrg *pPrg;           /* Value to return */
95310
 
  Expr *pWhen = 0;            /* Duplicate of trigger WHEN expression */
95311
 
  Vdbe *v;                    /* Temporary VM */
95312
 
  NameContext sNC;            /* Name context for sub-vdbe */
95313
 
  SubProgram *pProgram = 0;   /* Sub-vdbe for trigger program */
95314
 
  Parse *pSubParse;           /* Parse context for sub-vdbe */
95315
 
  int iEndTrigger = 0;        /* Label to jump to if WHEN is false */
95316
 
 
95317
 
  assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
95318
 
  assert( pTop->pVdbe );
95319
 
 
95320
 
  /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
95321
 
  ** are freed if an error occurs, link them into the Parse.pTriggerPrg 
95322
 
  ** list of the top-level Parse object sooner rather than later.  */
95323
 
  pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
95324
 
  if( !pPrg ) return 0;
95325
 
  pPrg->pNext = pTop->pTriggerPrg;
95326
 
  pTop->pTriggerPrg = pPrg;
95327
 
  pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
95328
 
  if( !pProgram ) return 0;
95329
 
  sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
95330
 
  pPrg->pTrigger = pTrigger;
95331
 
  pPrg->orconf = orconf;
95332
 
  pPrg->aColmask[0] = 0xffffffff;
95333
 
  pPrg->aColmask[1] = 0xffffffff;
95334
 
 
95335
 
  /* Allocate and populate a new Parse context to use for coding the 
95336
 
  ** trigger sub-program.  */
95337
 
  pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
95338
 
  if( !pSubParse ) return 0;
95339
 
  memset(&sNC, 0, sizeof(sNC));
95340
 
  sNC.pParse = pSubParse;
95341
 
  pSubParse->db = db;
95342
 
  pSubParse->pTriggerTab = pTab;
95343
 
  pSubParse->pToplevel = pTop;
95344
 
  pSubParse->zAuthContext = pTrigger->zName;
95345
 
  pSubParse->eTriggerOp = pTrigger->op;
95346
 
  pSubParse->nQueryLoop = pParse->nQueryLoop;
95347
 
 
95348
 
  v = sqlite3GetVdbe(pSubParse);
95349
 
  if( v ){
95350
 
    VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", 
95351
 
      pTrigger->zName, onErrorText(orconf),
95352
 
      (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
95353
 
        (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
95354
 
        (pTrigger->op==TK_INSERT ? "INSERT" : ""),
95355
 
        (pTrigger->op==TK_DELETE ? "DELETE" : ""),
95356
 
      pTab->zName
95357
 
    ));
95358
 
#ifndef SQLITE_OMIT_TRACE
95359
 
    sqlite3VdbeChangeP4(v, -1, 
95360
 
      sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
95361
 
    );
95362
 
#endif
95363
 
 
95364
 
    /* If one was specified, code the WHEN clause. If it evaluates to false
95365
 
    ** (or NULL) the sub-vdbe is immediately halted by jumping to the 
95366
 
    ** OP_Halt inserted at the end of the program.  */
95367
 
    if( pTrigger->pWhen ){
95368
 
      pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
95369
 
      if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen) 
95370
 
       && db->mallocFailed==0 
95371
 
      ){
95372
 
        iEndTrigger = sqlite3VdbeMakeLabel(v);
95373
 
        sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
95374
 
      }
95375
 
      sqlite3ExprDelete(db, pWhen);
95376
 
    }
95377
 
 
95378
 
    /* Code the trigger program into the sub-vdbe. */
95379
 
    codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
95380
 
 
95381
 
    /* Insert an OP_Halt at the end of the sub-program. */
95382
 
    if( iEndTrigger ){
95383
 
      sqlite3VdbeResolveLabel(v, iEndTrigger);
95384
 
    }
95385
 
    sqlite3VdbeAddOp0(v, OP_Halt);
95386
 
    VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
95387
 
 
95388
 
    transferParseError(pParse, pSubParse);
95389
 
    if( db->mallocFailed==0 ){
95390
 
      pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
95391
 
    }
95392
 
    pProgram->nMem = pSubParse->nMem;
95393
 
    pProgram->nCsr = pSubParse->nTab;
95394
 
    pProgram->token = (void *)pTrigger;
95395
 
    pPrg->aColmask[0] = pSubParse->oldmask;
95396
 
    pPrg->aColmask[1] = pSubParse->newmask;
95397
 
    sqlite3VdbeDelete(v);
95398
 
  }
95399
 
 
95400
 
  assert( !pSubParse->pAinc       && !pSubParse->pZombieTab );
95401
 
  assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
95402
 
  sqlite3StackFree(db, pSubParse);
95403
 
 
95404
 
  return pPrg;
95405
 
}
95406
 
    
95407
 
/*
95408
 
** Return a pointer to a TriggerPrg object containing the sub-program for
95409
 
** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
95410
 
** TriggerPrg object exists, a new object is allocated and populated before
95411
 
** being returned.
95412
 
*/
95413
 
static TriggerPrg *getRowTrigger(
95414
 
  Parse *pParse,       /* Current parse context */
95415
 
  Trigger *pTrigger,   /* Trigger to code */
95416
 
  Table *pTab,         /* The table trigger pTrigger is attached to */
95417
 
  int orconf           /* ON CONFLICT algorithm. */
95418
 
){
95419
 
  Parse *pRoot = sqlite3ParseToplevel(pParse);
95420
 
  TriggerPrg *pPrg;
95421
 
 
95422
 
  assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
95423
 
 
95424
 
  /* It may be that this trigger has already been coded (or is in the
95425
 
  ** process of being coded). If this is the case, then an entry with
95426
 
  ** a matching TriggerPrg.pTrigger field will be present somewhere
95427
 
  ** in the Parse.pTriggerPrg list. Search for such an entry.  */
95428
 
  for(pPrg=pRoot->pTriggerPrg; 
95429
 
      pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf); 
95430
 
      pPrg=pPrg->pNext
95431
 
  );
95432
 
 
95433
 
  /* If an existing TriggerPrg could not be located, create a new one. */
95434
 
  if( !pPrg ){
95435
 
    pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
95436
 
  }
95437
 
 
95438
 
  return pPrg;
95439
 
}
95440
 
 
95441
 
/*
95442
 
** Generate code for the trigger program associated with trigger p on 
95443
 
** table pTab. The reg, orconf and ignoreJump parameters passed to this
95444
 
** function are the same as those described in the header function for
95445
 
** sqlite3CodeRowTrigger()
95446
 
*/
95447
 
SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
95448
 
  Parse *pParse,       /* Parse context */
95449
 
  Trigger *p,          /* Trigger to code */
95450
 
  Table *pTab,         /* The table to code triggers from */
95451
 
  int reg,             /* Reg array containing OLD.* and NEW.* values */
95452
 
  int orconf,          /* ON CONFLICT policy */
95453
 
  int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
95454
 
){
95455
 
  Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
95456
 
  TriggerPrg *pPrg;
95457
 
  pPrg = getRowTrigger(pParse, p, pTab, orconf);
95458
 
  assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
95459
 
 
95460
 
  /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program 
95461
 
  ** is a pointer to the sub-vdbe containing the trigger program.  */
95462
 
  if( pPrg ){
95463
 
    int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
95464
 
 
95465
 
    sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
95466
 
    sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
95467
 
    VdbeComment(
95468
 
        (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
95469
 
 
95470
 
    /* Set the P5 operand of the OP_Program instruction to non-zero if
95471
 
    ** recursive invocation of this trigger program is disallowed. Recursive
95472
 
    ** invocation is disallowed if (a) the sub-program is really a trigger,
95473
 
    ** not a foreign key action, and (b) the flag to enable recursive triggers
95474
 
    ** is clear.  */
95475
 
    sqlite3VdbeChangeP5(v, (u8)bRecursive);
95476
 
  }
95477
 
}
95478
 
 
95479
 
/*
95480
 
** This is called to code the required FOR EACH ROW triggers for an operation
95481
 
** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
95482
 
** is given by the op paramater. The tr_tm parameter determines whether the
95483
 
** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
95484
 
** parameter pChanges is passed the list of columns being modified.
95485
 
**
95486
 
** If there are no triggers that fire at the specified time for the specified
95487
 
** operation on pTab, this function is a no-op.
95488
 
**
95489
 
** The reg argument is the address of the first in an array of registers 
95490
 
** that contain the values substituted for the new.* and old.* references
95491
 
** in the trigger program. If N is the number of columns in table pTab
95492
 
** (a copy of pTab->nCol), then registers are populated as follows:
95493
 
**
95494
 
**   Register       Contains
95495
 
**   ------------------------------------------------------
95496
 
**   reg+0          OLD.rowid
95497
 
**   reg+1          OLD.* value of left-most column of pTab
95498
 
**   ...            ...
95499
 
**   reg+N          OLD.* value of right-most column of pTab
95500
 
**   reg+N+1        NEW.rowid
95501
 
**   reg+N+2        OLD.* value of left-most column of pTab
95502
 
**   ...            ...
95503
 
**   reg+N+N+1      NEW.* value of right-most column of pTab
95504
 
**
95505
 
** For ON DELETE triggers, the registers containing the NEW.* values will
95506
 
** never be accessed by the trigger program, so they are not allocated or 
95507
 
** populated by the caller (there is no data to populate them with anyway). 
95508
 
** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
95509
 
** are never accessed, and so are not allocated by the caller. So, for an
95510
 
** ON INSERT trigger, the value passed to this function as parameter reg
95511
 
** is not a readable register, although registers (reg+N) through 
95512
 
** (reg+N+N+1) are.
95513
 
**
95514
 
** Parameter orconf is the default conflict resolution algorithm for the
95515
 
** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
95516
 
** is the instruction that control should jump to if a trigger program
95517
 
** raises an IGNORE exception.
95518
 
*/
95519
 
SQLITE_PRIVATE void sqlite3CodeRowTrigger(
95520
 
  Parse *pParse,       /* Parse context */
95521
 
  Trigger *pTrigger,   /* List of triggers on table pTab */
95522
 
  int op,              /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
95523
 
  ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
95524
 
  int tr_tm,           /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
95525
 
  Table *pTab,         /* The table to code triggers from */
95526
 
  int reg,             /* The first in an array of registers (see above) */
95527
 
  int orconf,          /* ON CONFLICT policy */
95528
 
  int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
95529
 
){
95530
 
  Trigger *p;          /* Used to iterate through pTrigger list */
95531
 
 
95532
 
  assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
95533
 
  assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
95534
 
  assert( (op==TK_UPDATE)==(pChanges!=0) );
95535
 
 
95536
 
  for(p=pTrigger; p; p=p->pNext){
95537
 
 
95538
 
    /* Sanity checking:  The schema for the trigger and for the table are
95539
 
    ** always defined.  The trigger must be in the same schema as the table
95540
 
    ** or else it must be a TEMP trigger. */
95541
 
    assert( p->pSchema!=0 );
95542
 
    assert( p->pTabSchema!=0 );
95543
 
    assert( p->pSchema==p->pTabSchema 
95544
 
         || p->pSchema==pParse->db->aDb[1].pSchema );
95545
 
 
95546
 
    /* Determine whether we should code this trigger */
95547
 
    if( p->op==op 
95548
 
     && p->tr_tm==tr_tm 
95549
 
     && checkColumnOverlap(p->pColumns, pChanges)
95550
 
    ){
95551
 
      sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
95552
 
    }
95553
 
  }
95554
 
}
95555
 
 
95556
 
/*
95557
 
** Triggers may access values stored in the old.* or new.* pseudo-table. 
95558
 
** This function returns a 32-bit bitmask indicating which columns of the 
95559
 
** old.* or new.* tables actually are used by triggers. This information 
95560
 
** may be used by the caller, for example, to avoid having to load the entire
95561
 
** old.* record into memory when executing an UPDATE or DELETE command.
95562
 
**
95563
 
** Bit 0 of the returned mask is set if the left-most column of the
95564
 
** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
95565
 
** the second leftmost column value is required, and so on. If there
95566
 
** are more than 32 columns in the table, and at least one of the columns
95567
 
** with an index greater than 32 may be accessed, 0xffffffff is returned.
95568
 
**
95569
 
** It is not possible to determine if the old.rowid or new.rowid column is 
95570
 
** accessed by triggers. The caller must always assume that it is.
95571
 
**
95572
 
** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
95573
 
** applies to the old.* table. If 1, the new.* table.
95574
 
**
95575
 
** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
95576
 
** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
95577
 
** included in the returned mask if the TRIGGER_BEFORE bit is set in the
95578
 
** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
95579
 
** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
95580
 
*/
95581
 
SQLITE_PRIVATE u32 sqlite3TriggerColmask(
95582
 
  Parse *pParse,       /* Parse context */
95583
 
  Trigger *pTrigger,   /* List of triggers on table pTab */
95584
 
  ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
95585
 
  int isNew,           /* 1 for new.* ref mask, 0 for old.* ref mask */
95586
 
  int tr_tm,           /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
95587
 
  Table *pTab,         /* The table to code triggers from */
95588
 
  int orconf           /* Default ON CONFLICT policy for trigger steps */
95589
 
){
95590
 
  const int op = pChanges ? TK_UPDATE : TK_DELETE;
95591
 
  u32 mask = 0;
95592
 
  Trigger *p;
95593
 
 
95594
 
  assert( isNew==1 || isNew==0 );
95595
 
  for(p=pTrigger; p; p=p->pNext){
95596
 
    if( p->op==op && (tr_tm&p->tr_tm)
95597
 
     && checkColumnOverlap(p->pColumns,pChanges)
95598
 
    ){
95599
 
      TriggerPrg *pPrg;
95600
 
      pPrg = getRowTrigger(pParse, p, pTab, orconf);
95601
 
      if( pPrg ){
95602
 
        mask |= pPrg->aColmask[isNew];
95603
 
      }
95604
 
    }
95605
 
  }
95606
 
 
95607
 
  return mask;
95608
 
}
95609
 
 
95610
 
#endif /* !defined(SQLITE_OMIT_TRIGGER) */
95611
 
 
95612
 
/************** End of trigger.c *********************************************/
95613
 
/************** Begin file update.c ******************************************/
95614
 
/*
95615
 
** 2001 September 15
95616
 
**
95617
 
** The author disclaims copyright to this source code.  In place of
95618
 
** a legal notice, here is a blessing:
95619
 
**
95620
 
**    May you do good and not evil.
95621
 
**    May you find forgiveness for yourself and forgive others.
95622
 
**    May you share freely, never taking more than you give.
95623
 
**
95624
 
*************************************************************************
95625
 
** This file contains C code routines that are called by the parser
95626
 
** to handle UPDATE statements.
95627
 
*/
95628
 
 
95629
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
95630
 
/* Forward declaration */
95631
 
static void updateVirtualTable(
95632
 
  Parse *pParse,       /* The parsing context */
95633
 
  SrcList *pSrc,       /* The virtual table to be modified */
95634
 
  Table *pTab,         /* The virtual table */
95635
 
  ExprList *pChanges,  /* The columns to change in the UPDATE statement */
95636
 
  Expr *pRowidExpr,    /* Expression used to recompute the rowid */
95637
 
  int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
95638
 
  Expr *pWhere         /* WHERE clause of the UPDATE statement */
95639
 
);
95640
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
95641
 
 
95642
 
/*
95643
 
** The most recently coded instruction was an OP_Column to retrieve the
95644
 
** i-th column of table pTab. This routine sets the P4 parameter of the 
95645
 
** OP_Column to the default value, if any.
95646
 
**
95647
 
** The default value of a column is specified by a DEFAULT clause in the 
95648
 
** column definition. This was either supplied by the user when the table
95649
 
** was created, or added later to the table definition by an ALTER TABLE
95650
 
** command. If the latter, then the row-records in the table btree on disk
95651
 
** may not contain a value for the column and the default value, taken
95652
 
** from the P4 parameter of the OP_Column instruction, is returned instead.
95653
 
** If the former, then all row-records are guaranteed to include a value
95654
 
** for the column and the P4 value is not required.
95655
 
**
95656
 
** Column definitions created by an ALTER TABLE command may only have 
95657
 
** literal default values specified: a number, null or a string. (If a more
95658
 
** complicated default expression value was provided, it is evaluated 
95659
 
** when the ALTER TABLE is executed and one of the literal values written
95660
 
** into the sqlite_master table.)
95661
 
**
95662
 
** Therefore, the P4 parameter is only required if the default value for
95663
 
** the column is a literal number, string or null. The sqlite3ValueFromExpr()
95664
 
** function is capable of transforming these types of expressions into
95665
 
** sqlite3_value objects.
95666
 
**
95667
 
** If parameter iReg is not negative, code an OP_RealAffinity instruction
95668
 
** on register iReg. This is used when an equivalent integer value is 
95669
 
** stored in place of an 8-byte floating point value in order to save 
95670
 
** space.
95671
 
*/
95672
 
SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
95673
 
  assert( pTab!=0 );
95674
 
  if( !pTab->pSelect ){
95675
 
    sqlite3_value *pValue;
95676
 
    u8 enc = ENC(sqlite3VdbeDb(v));
95677
 
    Column *pCol = &pTab->aCol[i];
95678
 
    VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
95679
 
    assert( i<pTab->nCol );
95680
 
    sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, 
95681
 
                         pCol->affinity, &pValue);
95682
 
    if( pValue ){
95683
 
      sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
95684
 
    }
95685
 
#ifndef SQLITE_OMIT_FLOATING_POINT
95686
 
    if( iReg>=0 && pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
95687
 
      sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
95688
 
    }
95689
 
#endif
95690
 
  }
95691
 
}
95692
 
 
95693
 
/*
95694
 
** Process an UPDATE statement.
95695
 
**
95696
 
**   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
95697
 
**          \_______/ \________/     \______/       \________________/
95698
 
*            onError   pTabList      pChanges             pWhere
95699
 
*/
95700
 
SQLITE_PRIVATE void sqlite3Update(
95701
 
  Parse *pParse,         /* The parser context */
95702
 
  SrcList *pTabList,     /* The table in which we should change things */
95703
 
  ExprList *pChanges,    /* Things to be changed */
95704
 
  Expr *pWhere,          /* The WHERE clause.  May be null */
95705
 
  int onError            /* How to handle constraint errors */
95706
 
){
95707
 
  int i, j;              /* Loop counters */
95708
 
  Table *pTab;           /* The table to be updated */
95709
 
  int addr = 0;          /* VDBE instruction address of the start of the loop */
95710
 
  WhereInfo *pWInfo;     /* Information about the WHERE clause */
95711
 
  Vdbe *v;               /* The virtual database engine */
95712
 
  Index *pIdx;           /* For looping over indices */
95713
 
  int nIdx;              /* Number of indices that need updating */
95714
 
  int iCur;              /* VDBE Cursor number of pTab */
95715
 
  sqlite3 *db;           /* The database structure */
95716
 
  int *aRegIdx = 0;      /* One register assigned to each index to be updated */
95717
 
  int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
95718
 
                         ** an expression for the i-th column of the table.
95719
 
                         ** aXRef[i]==-1 if the i-th column is not changed. */
95720
 
  int chngRowid;         /* True if the record number is being changed */
95721
 
  Expr *pRowidExpr = 0;  /* Expression defining the new record number */
95722
 
  int openAll = 0;       /* True if all indices need to be opened */
95723
 
  AuthContext sContext;  /* The authorization context */
95724
 
  NameContext sNC;       /* The name-context to resolve expressions in */
95725
 
  int iDb;               /* Database containing the table being updated */
95726
 
  int okOnePass;         /* True for one-pass algorithm without the FIFO */
95727
 
  int hasFK;             /* True if foreign key processing is required */
95728
 
 
95729
 
#ifndef SQLITE_OMIT_TRIGGER
95730
 
  int isView;            /* True when updating a view (INSTEAD OF trigger) */
95731
 
  Trigger *pTrigger;     /* List of triggers on pTab, if required */
95732
 
  int tmask;             /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
95733
 
#endif
95734
 
  int newmask;           /* Mask of NEW.* columns accessed by BEFORE triggers */
95735
 
 
95736
 
  /* Register Allocations */
95737
 
  int regRowCount = 0;   /* A count of rows changed */
95738
 
  int regOldRowid;       /* The old rowid */
95739
 
  int regNewRowid;       /* The new rowid */
95740
 
  int regNew;
95741
 
  int regOld = 0;
95742
 
  int regRowSet = 0;     /* Rowset of rows to be updated */
95743
 
 
95744
 
  memset(&sContext, 0, sizeof(sContext));
95745
 
  db = pParse->db;
95746
 
  if( pParse->nErr || db->mallocFailed ){
95747
 
    goto update_cleanup;
95748
 
  }
95749
 
  assert( pTabList->nSrc==1 );
95750
 
 
95751
 
  /* Locate the table which we want to update. 
95752
 
  */
95753
 
  pTab = sqlite3SrcListLookup(pParse, pTabList);
95754
 
  if( pTab==0 ) goto update_cleanup;
95755
 
  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
95756
 
 
95757
 
  /* Figure out if we have any triggers and if the table being
95758
 
  ** updated is a view.
95759
 
  */
95760
 
#ifndef SQLITE_OMIT_TRIGGER
95761
 
  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
95762
 
  isView = pTab->pSelect!=0;
95763
 
  assert( pTrigger || tmask==0 );
95764
 
#else
95765
 
# define pTrigger 0
95766
 
# define isView 0
95767
 
# define tmask 0
95768
 
#endif
95769
 
#ifdef SQLITE_OMIT_VIEW
95770
 
# undef isView
95771
 
# define isView 0
95772
 
#endif
95773
 
 
95774
 
  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
95775
 
    goto update_cleanup;
95776
 
  }
95777
 
  if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
95778
 
    goto update_cleanup;
95779
 
  }
95780
 
  aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
95781
 
  if( aXRef==0 ) goto update_cleanup;
95782
 
  for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
95783
 
 
95784
 
  /* Allocate a cursors for the main database table and for all indices.
95785
 
  ** The index cursors might not be used, but if they are used they
95786
 
  ** need to occur right after the database cursor.  So go ahead and
95787
 
  ** allocate enough space, just in case.
95788
 
  */
95789
 
  pTabList->a[0].iCursor = iCur = pParse->nTab++;
95790
 
  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
95791
 
    pParse->nTab++;
95792
 
  }
95793
 
 
95794
 
  /* Initialize the name-context */
95795
 
  memset(&sNC, 0, sizeof(sNC));
95796
 
  sNC.pParse = pParse;
95797
 
  sNC.pSrcList = pTabList;
95798
 
 
95799
 
  /* Resolve the column names in all the expressions of the
95800
 
  ** of the UPDATE statement.  Also find the column index
95801
 
  ** for each column to be updated in the pChanges array.  For each
95802
 
  ** column to be updated, make sure we have authorization to change
95803
 
  ** that column.
95804
 
  */
95805
 
  chngRowid = 0;
95806
 
  for(i=0; i<pChanges->nExpr; i++){
95807
 
    if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
95808
 
      goto update_cleanup;
95809
 
    }
95810
 
    for(j=0; j<pTab->nCol; j++){
95811
 
      if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
95812
 
        if( j==pTab->iPKey ){
95813
 
          chngRowid = 1;
95814
 
          pRowidExpr = pChanges->a[i].pExpr;
95815
 
        }
95816
 
        aXRef[j] = i;
95817
 
        break;
95818
 
      }
95819
 
    }
95820
 
    if( j>=pTab->nCol ){
95821
 
      if( sqlite3IsRowid(pChanges->a[i].zName) ){
95822
 
        chngRowid = 1;
95823
 
        pRowidExpr = pChanges->a[i].pExpr;
95824
 
      }else{
95825
 
        sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
95826
 
        pParse->checkSchema = 1;
95827
 
        goto update_cleanup;
95828
 
      }
95829
 
    }
95830
 
#ifndef SQLITE_OMIT_AUTHORIZATION
95831
 
    {
95832
 
      int rc;
95833
 
      rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
95834
 
                           pTab->aCol[j].zName, db->aDb[iDb].zName);
95835
 
      if( rc==SQLITE_DENY ){
95836
 
        goto update_cleanup;
95837
 
      }else if( rc==SQLITE_IGNORE ){
95838
 
        aXRef[j] = -1;
95839
 
      }
95840
 
    }
95841
 
#endif
95842
 
  }
95843
 
 
95844
 
  hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngRowid);
95845
 
 
95846
 
  /* Allocate memory for the array aRegIdx[].  There is one entry in the
95847
 
  ** array for each index associated with table being updated.  Fill in
95848
 
  ** the value with a register number for indices that are to be used
95849
 
  ** and with zero for unused indices.
95850
 
  */
95851
 
  for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
95852
 
  if( nIdx>0 ){
95853
 
    aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
95854
 
    if( aRegIdx==0 ) goto update_cleanup;
95855
 
  }
95856
 
  for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
95857
 
    int reg;
95858
 
    if( chngRowid ){
95859
 
      reg = ++pParse->nMem;
95860
 
    }else{
95861
 
      reg = 0;
95862
 
      for(i=0; i<pIdx->nColumn; i++){
95863
 
        if( aXRef[pIdx->aiColumn[i]]>=0 ){
95864
 
          reg = ++pParse->nMem;
95865
 
          break;
95866
 
        }
95867
 
      }
95868
 
    }
95869
 
    aRegIdx[j] = reg;
95870
 
  }
95871
 
 
95872
 
  /* Begin generating code. */
95873
 
  v = sqlite3GetVdbe(pParse);
95874
 
  if( v==0 ) goto update_cleanup;
95875
 
  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
95876
 
  sqlite3BeginWriteOperation(pParse, 1, iDb);
95877
 
 
95878
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
95879
 
  /* Virtual tables must be handled separately */
95880
 
  if( IsVirtual(pTab) ){
95881
 
    updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
95882
 
                       pWhere);
95883
 
    pWhere = 0;
95884
 
    pTabList = 0;
95885
 
    goto update_cleanup;
95886
 
  }
95887
 
#endif
95888
 
 
95889
 
  /* Allocate required registers. */
95890
 
  regOldRowid = regNewRowid = ++pParse->nMem;
95891
 
  if( pTrigger || hasFK ){
95892
 
    regOld = pParse->nMem + 1;
95893
 
    pParse->nMem += pTab->nCol;
95894
 
  }
95895
 
  if( chngRowid || pTrigger || hasFK ){
95896
 
    regNewRowid = ++pParse->nMem;
95897
 
  }
95898
 
  regNew = pParse->nMem + 1;
95899
 
  pParse->nMem += pTab->nCol;
95900
 
 
95901
 
  /* Start the view context. */
95902
 
  if( isView ){
95903
 
    sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
95904
 
  }
95905
 
 
95906
 
  /* If we are trying to update a view, realize that view into
95907
 
  ** a ephemeral table.
95908
 
  */
95909
 
#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
95910
 
  if( isView ){
95911
 
    sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
95912
 
  }
95913
 
#endif
95914
 
 
95915
 
  /* Resolve the column names in all the expressions in the
95916
 
  ** WHERE clause.
95917
 
  */
95918
 
  if( sqlite3ResolveExprNames(&sNC, pWhere) ){
95919
 
    goto update_cleanup;
95920
 
  }
95921
 
 
95922
 
  /* Begin the database scan
95923
 
  */
95924
 
  sqlite3VdbeAddOp2(v, OP_Null, 0, regOldRowid);
95925
 
  pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0, WHERE_ONEPASS_DESIRED);
95926
 
  if( pWInfo==0 ) goto update_cleanup;
95927
 
  okOnePass = pWInfo->okOnePass;
95928
 
 
95929
 
  /* Remember the rowid of every item to be updated.
95930
 
  */
95931
 
  sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
95932
 
  if( !okOnePass ){
95933
 
    regRowSet = ++pParse->nMem;
95934
 
    sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
95935
 
  }
95936
 
 
95937
 
  /* End the database scan loop.
95938
 
  */
95939
 
  sqlite3WhereEnd(pWInfo);
95940
 
 
95941
 
  /* Initialize the count of updated rows
95942
 
  */
95943
 
  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
95944
 
    regRowCount = ++pParse->nMem;
95945
 
    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
95946
 
  }
95947
 
 
95948
 
  if( !isView ){
95949
 
    /* 
95950
 
    ** Open every index that needs updating.  Note that if any
95951
 
    ** index could potentially invoke a REPLACE conflict resolution 
95952
 
    ** action, then we need to open all indices because we might need
95953
 
    ** to be deleting some records.
95954
 
    */
95955
 
    if( !okOnePass ) sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); 
95956
 
    if( onError==OE_Replace ){
95957
 
      openAll = 1;
95958
 
    }else{
95959
 
      openAll = 0;
95960
 
      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
95961
 
        if( pIdx->onError==OE_Replace ){
95962
 
          openAll = 1;
95963
 
          break;
95964
 
        }
95965
 
      }
95966
 
    }
95967
 
    for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
95968
 
      if( openAll || aRegIdx[i]>0 ){
95969
 
        KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
95970
 
        sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
95971
 
                       (char*)pKey, P4_KEYINFO_HANDOFF);
95972
 
        assert( pParse->nTab>iCur+i+1 );
95973
 
      }
95974
 
    }
95975
 
  }
95976
 
 
95977
 
  /* Top of the update loop */
95978
 
  if( okOnePass ){
95979
 
    int a1 = sqlite3VdbeAddOp1(v, OP_NotNull, regOldRowid);
95980
 
    addr = sqlite3VdbeAddOp0(v, OP_Goto);
95981
 
    sqlite3VdbeJumpHere(v, a1);
95982
 
  }else{
95983
 
    addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, 0, regOldRowid);
95984
 
  }
95985
 
 
95986
 
  /* Make cursor iCur point to the record that is being updated. If
95987
 
  ** this record does not exist for some reason (deleted by a trigger,
95988
 
  ** for example, then jump to the next iteration of the RowSet loop.  */
95989
 
  sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
95990
 
 
95991
 
  /* If the record number will change, set register regNewRowid to
95992
 
  ** contain the new value. If the record number is not being modified,
95993
 
  ** then regNewRowid is the same register as regOldRowid, which is
95994
 
  ** already populated.  */
95995
 
  assert( chngRowid || pTrigger || hasFK || regOldRowid==regNewRowid );
95996
 
  if( chngRowid ){
95997
 
    sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
95998
 
    sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
95999
 
  }
96000
 
 
96001
 
  /* If there are triggers on this table, populate an array of registers 
96002
 
  ** with the required old.* column data.  */
96003
 
  if( hasFK || pTrigger ){
96004
 
    u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
96005
 
    oldmask |= sqlite3TriggerColmask(pParse, 
96006
 
        pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
96007
 
    );
96008
 
    for(i=0; i<pTab->nCol; i++){
96009
 
      if( aXRef[i]<0 || oldmask==0xffffffff || (i<32 && (oldmask & (1<<i))) ){
96010
 
        sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOld+i);
96011
 
      }else{
96012
 
        sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
96013
 
      }
96014
 
    }
96015
 
    if( chngRowid==0 ){
96016
 
      sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
96017
 
    }
96018
 
  }
96019
 
 
96020
 
  /* Populate the array of registers beginning at regNew with the new
96021
 
  ** row data. This array is used to check constaints, create the new
96022
 
  ** table and index records, and as the values for any new.* references
96023
 
  ** made by triggers.
96024
 
  **
96025
 
  ** If there are one or more BEFORE triggers, then do not populate the
96026
 
  ** registers associated with columns that are (a) not modified by
96027
 
  ** this UPDATE statement and (b) not accessed by new.* references. The
96028
 
  ** values for registers not modified by the UPDATE must be reloaded from 
96029
 
  ** the database after the BEFORE triggers are fired anyway (as the trigger 
96030
 
  ** may have modified them). So not loading those that are not going to
96031
 
  ** be used eliminates some redundant opcodes.
96032
 
  */
96033
 
  newmask = sqlite3TriggerColmask(
96034
 
      pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
96035
 
  );
96036
 
  for(i=0; i<pTab->nCol; i++){
96037
 
    if( i==pTab->iPKey ){
96038
 
      sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
96039
 
    }else{
96040
 
      j = aXRef[i];
96041
 
      if( j>=0 ){
96042
 
        sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
96043
 
      }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask&(1<<i)) ){
96044
 
        /* This branch loads the value of a column that will not be changed 
96045
 
        ** into a register. This is done if there are no BEFORE triggers, or
96046
 
        ** if there are one or more BEFORE triggers that use this value via
96047
 
        ** a new.* reference in a trigger program.
96048
 
        */
96049
 
        testcase( i==31 );
96050
 
        testcase( i==32 );
96051
 
        sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
96052
 
        sqlite3ColumnDefault(v, pTab, i, regNew+i);
96053
 
      }
96054
 
    }
96055
 
  }
96056
 
 
96057
 
  /* Fire any BEFORE UPDATE triggers. This happens before constraints are
96058
 
  ** verified. One could argue that this is wrong.
96059
 
  */
96060
 
  if( tmask&TRIGGER_BEFORE ){
96061
 
    sqlite3VdbeAddOp2(v, OP_Affinity, regNew, pTab->nCol);
96062
 
    sqlite3TableAffinityStr(v, pTab);
96063
 
    sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
96064
 
        TRIGGER_BEFORE, pTab, regOldRowid, onError, addr);
96065
 
 
96066
 
    /* The row-trigger may have deleted the row being updated. In this
96067
 
    ** case, jump to the next row. No updates or AFTER triggers are 
96068
 
    ** required. This behaviour - what happens when the row being updated
96069
 
    ** is deleted or renamed by a BEFORE trigger - is left undefined in the
96070
 
    ** documentation.
96071
 
    */
96072
 
    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
96073
 
 
96074
 
    /* If it did not delete it, the row-trigger may still have modified 
96075
 
    ** some of the columns of the row being updated. Load the values for 
96076
 
    ** all columns not modified by the update statement into their 
96077
 
    ** registers in case this has happened.
96078
 
    */
96079
 
    for(i=0; i<pTab->nCol; i++){
96080
 
      if( aXRef[i]<0 && i!=pTab->iPKey ){
96081
 
        sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
96082
 
        sqlite3ColumnDefault(v, pTab, i, regNew+i);
96083
 
      }
96084
 
    }
96085
 
  }
96086
 
 
96087
 
  if( !isView ){
96088
 
    int j1;                       /* Address of jump instruction */
96089
 
 
96090
 
    /* Do constraint checks. */
96091
 
    sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
96092
 
        aRegIdx, (chngRowid?regOldRowid:0), 1, onError, addr, 0);
96093
 
 
96094
 
    /* Do FK constraint checks. */
96095
 
    if( hasFK ){
96096
 
      sqlite3FkCheck(pParse, pTab, regOldRowid, 0);
96097
 
    }
96098
 
 
96099
 
    /* Delete the index entries associated with the current record.  */
96100
 
    j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid);
96101
 
    sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
96102
 
  
96103
 
    /* If changing the record number, delete the old record.  */
96104
 
    if( hasFK || chngRowid ){
96105
 
      sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0);
96106
 
    }
96107
 
    sqlite3VdbeJumpHere(v, j1);
96108
 
 
96109
 
    if( hasFK ){
96110
 
      sqlite3FkCheck(pParse, pTab, 0, regNewRowid);
96111
 
    }
96112
 
  
96113
 
    /* Insert the new index entries and the new record. */
96114
 
    sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, aRegIdx, 1, 0, 0);
96115
 
 
96116
 
    /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
96117
 
    ** handle rows (possibly in other tables) that refer via a foreign key
96118
 
    ** to the row just updated. */ 
96119
 
    if( hasFK ){
96120
 
      sqlite3FkActions(pParse, pTab, pChanges, regOldRowid);
96121
 
    }
96122
 
  }
96123
 
 
96124
 
  /* Increment the row counter 
96125
 
  */
96126
 
  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
96127
 
    sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
96128
 
  }
96129
 
 
96130
 
  sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
96131
 
      TRIGGER_AFTER, pTab, regOldRowid, onError, addr);
96132
 
 
96133
 
  /* Repeat the above with the next record to be updated, until
96134
 
  ** all record selected by the WHERE clause have been updated.
96135
 
  */
96136
 
  sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
96137
 
  sqlite3VdbeJumpHere(v, addr);
96138
 
 
96139
 
  /* Close all tables */
96140
 
  for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
96141
 
    if( openAll || aRegIdx[i]>0 ){
96142
 
      sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
96143
 
    }
96144
 
  }
96145
 
  sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
96146
 
 
96147
 
  /* Update the sqlite_sequence table by storing the content of the
96148
 
  ** maximum rowid counter values recorded while inserting into
96149
 
  ** autoincrement tables.
96150
 
  */
96151
 
  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
96152
 
    sqlite3AutoincrementEnd(pParse);
96153
 
  }
96154
 
 
96155
 
  /*
96156
 
  ** Return the number of rows that were changed. If this routine is 
96157
 
  ** generating code because of a call to sqlite3NestedParse(), do not
96158
 
  ** invoke the callback function.
96159
 
  */
96160
 
  if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
96161
 
    sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
96162
 
    sqlite3VdbeSetNumCols(v, 1);
96163
 
    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
96164
 
  }
96165
 
 
96166
 
update_cleanup:
96167
 
  sqlite3AuthContextPop(&sContext);
96168
 
  sqlite3DbFree(db, aRegIdx);
96169
 
  sqlite3DbFree(db, aXRef);
96170
 
  sqlite3SrcListDelete(db, pTabList);
96171
 
  sqlite3ExprListDelete(db, pChanges);
96172
 
  sqlite3ExprDelete(db, pWhere);
96173
 
  return;
96174
 
}
96175
 
/* Make sure "isView" and other macros defined above are undefined. Otherwise
96176
 
** thely may interfere with compilation of other functions in this file
96177
 
** (or in another file, if this file becomes part of the amalgamation).  */
96178
 
#ifdef isView
96179
 
 #undef isView
96180
 
#endif
96181
 
#ifdef pTrigger
96182
 
 #undef pTrigger
96183
 
#endif
96184
 
 
96185
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
96186
 
/*
96187
 
** Generate code for an UPDATE of a virtual table.
96188
 
**
96189
 
** The strategy is that we create an ephemerial table that contains
96190
 
** for each row to be changed:
96191
 
**
96192
 
**   (A)  The original rowid of that row.
96193
 
**   (B)  The revised rowid for the row. (note1)
96194
 
**   (C)  The content of every column in the row.
96195
 
**
96196
 
** Then we loop over this ephemeral table and for each row in
96197
 
** the ephermeral table call VUpdate.
96198
 
**
96199
 
** When finished, drop the ephemeral table.
96200
 
**
96201
 
** (note1) Actually, if we know in advance that (A) is always the same
96202
 
** as (B) we only store (A), then duplicate (A) when pulling
96203
 
** it out of the ephemeral table before calling VUpdate.
96204
 
*/
96205
 
static void updateVirtualTable(
96206
 
  Parse *pParse,       /* The parsing context */
96207
 
  SrcList *pSrc,       /* The virtual table to be modified */
96208
 
  Table *pTab,         /* The virtual table */
96209
 
  ExprList *pChanges,  /* The columns to change in the UPDATE statement */
96210
 
  Expr *pRowid,        /* Expression used to recompute the rowid */
96211
 
  int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
96212
 
  Expr *pWhere         /* WHERE clause of the UPDATE statement */
96213
 
){
96214
 
  Vdbe *v = pParse->pVdbe;  /* Virtual machine under construction */
96215
 
  ExprList *pEList = 0;     /* The result set of the SELECT statement */
96216
 
  Select *pSelect = 0;      /* The SELECT statement */
96217
 
  Expr *pExpr;              /* Temporary expression */
96218
 
  int ephemTab;             /* Table holding the result of the SELECT */
96219
 
  int i;                    /* Loop counter */
96220
 
  int addr;                 /* Address of top of loop */
96221
 
  int iReg;                 /* First register in set passed to OP_VUpdate */
96222
 
  sqlite3 *db = pParse->db; /* Database connection */
96223
 
  const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
96224
 
  SelectDest dest;
96225
 
 
96226
 
  /* Construct the SELECT statement that will find the new values for
96227
 
  ** all updated rows. 
96228
 
  */
96229
 
  pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, "_rowid_"));
96230
 
  if( pRowid ){
96231
 
    pEList = sqlite3ExprListAppend(pParse, pEList,
96232
 
                                   sqlite3ExprDup(db, pRowid, 0));
96233
 
  }
96234
 
  assert( pTab->iPKey<0 );
96235
 
  for(i=0; i<pTab->nCol; i++){
96236
 
    if( aXRef[i]>=0 ){
96237
 
      pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
96238
 
    }else{
96239
 
      pExpr = sqlite3Expr(db, TK_ID, pTab->aCol[i].zName);
96240
 
    }
96241
 
    pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
96242
 
  }
96243
 
  pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
96244
 
  
96245
 
  /* Create the ephemeral table into which the update results will
96246
 
  ** be stored.
96247
 
  */
96248
 
  assert( v );
96249
 
  ephemTab = pParse->nTab++;
96250
 
  sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
96251
 
  sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
96252
 
 
96253
 
  /* fill the ephemeral table 
96254
 
  */
96255
 
  sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
96256
 
  sqlite3Select(pParse, pSelect, &dest);
96257
 
 
96258
 
  /* Generate code to scan the ephemeral table and call VUpdate. */
96259
 
  iReg = ++pParse->nMem;
96260
 
  pParse->nMem += pTab->nCol+1;
96261
 
  addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
96262
 
  sqlite3VdbeAddOp3(v, OP_Column,  ephemTab, 0, iReg);
96263
 
  sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
96264
 
  for(i=0; i<pTab->nCol; i++){
96265
 
    sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
96266
 
  }
96267
 
  sqlite3VtabMakeWritable(pParse, pTab);
96268
 
  sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
96269
 
  sqlite3MayAbort(pParse);
96270
 
  sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1);
96271
 
  sqlite3VdbeJumpHere(v, addr);
96272
 
  sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
96273
 
 
96274
 
  /* Cleanup */
96275
 
  sqlite3SelectDelete(db, pSelect);  
96276
 
}
96277
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
96278
 
 
96279
 
/************** End of update.c **********************************************/
96280
 
/************** Begin file vacuum.c ******************************************/
96281
 
/*
96282
 
** 2003 April 6
96283
 
**
96284
 
** The author disclaims copyright to this source code.  In place of
96285
 
** a legal notice, here is a blessing:
96286
 
**
96287
 
**    May you do good and not evil.
96288
 
**    May you find forgiveness for yourself and forgive others.
96289
 
**    May you share freely, never taking more than you give.
96290
 
**
96291
 
*************************************************************************
96292
 
** This file contains code used to implement the VACUUM command.
96293
 
**
96294
 
** Most of the code in this file may be omitted by defining the
96295
 
** SQLITE_OMIT_VACUUM macro.
96296
 
*/
96297
 
 
96298
 
#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
96299
 
/*
96300
 
** Finalize a prepared statement.  If there was an error, store the
96301
 
** text of the error message in *pzErrMsg.  Return the result code.
96302
 
*/
96303
 
static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
96304
 
  int rc;
96305
 
  rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
96306
 
  if( rc ){
96307
 
    sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
96308
 
  }
96309
 
  return rc;
96310
 
}
96311
 
 
96312
 
/*
96313
 
** Execute zSql on database db. Return an error code.
96314
 
*/
96315
 
static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
96316
 
  sqlite3_stmt *pStmt;
96317
 
  VVA_ONLY( int rc; )
96318
 
  if( !zSql ){
96319
 
    return SQLITE_NOMEM;
96320
 
  }
96321
 
  if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
96322
 
    sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
96323
 
    return sqlite3_errcode(db);
96324
 
  }
96325
 
  VVA_ONLY( rc = ) sqlite3_step(pStmt);
96326
 
  assert( rc!=SQLITE_ROW );
96327
 
  return vacuumFinalize(db, pStmt, pzErrMsg);
96328
 
}
96329
 
 
96330
 
/*
96331
 
** Execute zSql on database db. The statement returns exactly
96332
 
** one column. Execute this as SQL on the same database.
96333
 
*/
96334
 
static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
96335
 
  sqlite3_stmt *pStmt;
96336
 
  int rc;
96337
 
 
96338
 
  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
96339
 
  if( rc!=SQLITE_OK ) return rc;
96340
 
 
96341
 
  while( SQLITE_ROW==sqlite3_step(pStmt) ){
96342
 
    rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
96343
 
    if( rc!=SQLITE_OK ){
96344
 
      vacuumFinalize(db, pStmt, pzErrMsg);
96345
 
      return rc;
96346
 
    }
96347
 
  }
96348
 
 
96349
 
  return vacuumFinalize(db, pStmt, pzErrMsg);
96350
 
}
96351
 
 
96352
 
/*
96353
 
** The non-standard VACUUM command is used to clean up the database,
96354
 
** collapse free space, etc.  It is modelled after the VACUUM command
96355
 
** in PostgreSQL.
96356
 
**
96357
 
** In version 1.0.x of SQLite, the VACUUM command would call
96358
 
** gdbm_reorganize() on all the database tables.  But beginning
96359
 
** with 2.0.0, SQLite no longer uses GDBM so this command has
96360
 
** become a no-op.
96361
 
*/
96362
 
SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){
96363
 
  Vdbe *v = sqlite3GetVdbe(pParse);
96364
 
  if( v ){
96365
 
    sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
96366
 
  }
96367
 
  return;
96368
 
}
96369
 
 
96370
 
/*
96371
 
** This routine implements the OP_Vacuum opcode of the VDBE.
96372
 
*/
96373
 
SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
96374
 
  int rc = SQLITE_OK;     /* Return code from service routines */
96375
 
  Btree *pMain;           /* The database being vacuumed */
96376
 
  Btree *pTemp;           /* The temporary database we vacuum into */
96377
 
  char *zSql = 0;         /* SQL statements */
96378
 
  int saved_flags;        /* Saved value of the db->flags */
96379
 
  int saved_nChange;      /* Saved value of db->nChange */
96380
 
  int saved_nTotalChange; /* Saved value of db->nTotalChange */
96381
 
  void (*saved_xTrace)(void*,const char*);  /* Saved db->xTrace */
96382
 
  Db *pDb = 0;            /* Database to detach at end of vacuum */
96383
 
  int isMemDb;            /* True if vacuuming a :memory: database */
96384
 
  int nRes;               /* Bytes of reserved space at the end of each page */
96385
 
  int nDb;                /* Number of attached databases */
96386
 
 
96387
 
  if( !db->autoCommit ){
96388
 
    sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
96389
 
    return SQLITE_ERROR;
96390
 
  }
96391
 
  if( db->activeVdbeCnt>1 ){
96392
 
    sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
96393
 
    return SQLITE_ERROR;
96394
 
  }
96395
 
 
96396
 
  /* Save the current value of the database flags so that it can be 
96397
 
  ** restored before returning. Then set the writable-schema flag, and
96398
 
  ** disable CHECK and foreign key constraints.  */
96399
 
  saved_flags = db->flags;
96400
 
  saved_nChange = db->nChange;
96401
 
  saved_nTotalChange = db->nTotalChange;
96402
 
  saved_xTrace = db->xTrace;
96403
 
  db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
96404
 
  db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
96405
 
  db->xTrace = 0;
96406
 
 
96407
 
  pMain = db->aDb[0].pBt;
96408
 
  isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
96409
 
 
96410
 
  /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
96411
 
  ** can be set to 'off' for this file, as it is not recovered if a crash
96412
 
  ** occurs anyway. The integrity of the database is maintained by a
96413
 
  ** (possibly synchronous) transaction opened on the main database before
96414
 
  ** sqlite3BtreeCopyFile() is called.
96415
 
  **
96416
 
  ** An optimisation would be to use a non-journaled pager.
96417
 
  ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
96418
 
  ** that actually made the VACUUM run slower.  Very little journalling
96419
 
  ** actually occurs when doing a vacuum since the vacuum_db is initially
96420
 
  ** empty.  Only the journal header is written.  Apparently it takes more
96421
 
  ** time to parse and run the PRAGMA to turn journalling off than it does
96422
 
  ** to write the journal header file.
96423
 
  */
96424
 
  nDb = db->nDb;
96425
 
  if( sqlite3TempInMemory(db) ){
96426
 
    zSql = "ATTACH ':memory:' AS vacuum_db;";
96427
 
  }else{
96428
 
    zSql = "ATTACH '' AS vacuum_db;";
96429
 
  }
96430
 
  rc = execSql(db, pzErrMsg, zSql);
96431
 
  if( db->nDb>nDb ){
96432
 
    pDb = &db->aDb[db->nDb-1];
96433
 
    assert( strcmp(pDb->zName,"vacuum_db")==0 );
96434
 
  }
96435
 
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
96436
 
  pTemp = db->aDb[db->nDb-1].pBt;
96437
 
 
96438
 
  /* The call to execSql() to attach the temp database has left the file
96439
 
  ** locked (as there was more than one active statement when the transaction
96440
 
  ** to read the schema was concluded. Unlock it here so that this doesn't
96441
 
  ** cause problems for the call to BtreeSetPageSize() below.  */
96442
 
  sqlite3BtreeCommit(pTemp);
96443
 
 
96444
 
  nRes = sqlite3BtreeGetReserve(pMain);
96445
 
 
96446
 
  /* A VACUUM cannot change the pagesize of an encrypted database. */
96447
 
#ifdef SQLITE_HAS_CODEC
96448
 
  if( db->nextPagesize ){
96449
 
    extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
96450
 
    int nKey;
96451
 
    char *zKey;
96452
 
    sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
96453
 
    if( nKey ) db->nextPagesize = 0;
96454
 
  }
96455
 
#endif
96456
 
 
96457
 
  /* Do not attempt to change the page size for a WAL database */
96458
 
  if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
96459
 
                                               ==PAGER_JOURNALMODE_WAL ){
96460
 
    db->nextPagesize = 0;
96461
 
  }
96462
 
 
96463
 
  if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
96464
 
   || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
96465
 
   || NEVER(db->mallocFailed)
96466
 
  ){
96467
 
    rc = SQLITE_NOMEM;
96468
 
    goto end_of_vacuum;
96469
 
  }
96470
 
  rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
96471
 
  if( rc!=SQLITE_OK ){
96472
 
    goto end_of_vacuum;
96473
 
  }
96474
 
 
96475
 
#ifndef SQLITE_OMIT_AUTOVACUUM
96476
 
  sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
96477
 
                                           sqlite3BtreeGetAutoVacuum(pMain));
96478
 
#endif
96479
 
 
96480
 
  /* Begin a transaction */
96481
 
  rc = execSql(db, pzErrMsg, "BEGIN EXCLUSIVE;");
96482
 
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
96483
 
 
96484
 
  /* Query the schema of the main database. Create a mirror schema
96485
 
  ** in the temporary database.
96486
 
  */
96487
 
  rc = execExecSql(db, pzErrMsg,
96488
 
      "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
96489
 
      "  FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
96490
 
      "   AND rootpage>0"
96491
 
  );
96492
 
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
96493
 
  rc = execExecSql(db, pzErrMsg,
96494
 
      "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
96495
 
      "  FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
96496
 
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
96497
 
  rc = execExecSql(db, pzErrMsg,
96498
 
      "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
96499
 
      "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
96500
 
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
96501
 
 
96502
 
  /* Loop through the tables in the main database. For each, do
96503
 
  ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
96504
 
  ** the contents to the temporary database.
96505
 
  */
96506
 
  rc = execExecSql(db, pzErrMsg,
96507
 
      "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
96508
 
      "|| ' SELECT * FROM main.' || quote(name) || ';'"
96509
 
      "FROM main.sqlite_master "
96510
 
      "WHERE type = 'table' AND name!='sqlite_sequence' "
96511
 
      "  AND rootpage>0"
96512
 
  );
96513
 
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
96514
 
 
96515
 
  /* Copy over the sequence table
96516
 
  */
96517
 
  rc = execExecSql(db, pzErrMsg,
96518
 
      "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
96519
 
      "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
96520
 
  );
96521
 
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
96522
 
  rc = execExecSql(db, pzErrMsg,
96523
 
      "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
96524
 
      "|| ' SELECT * FROM main.' || quote(name) || ';' "
96525
 
      "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
96526
 
  );
96527
 
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
96528
 
 
96529
 
 
96530
 
  /* Copy the triggers, views, and virtual tables from the main database
96531
 
  ** over to the temporary database.  None of these objects has any
96532
 
  ** associated storage, so all we have to do is copy their entries
96533
 
  ** from the SQLITE_MASTER table.
96534
 
  */
96535
 
  rc = execSql(db, pzErrMsg,
96536
 
      "INSERT INTO vacuum_db.sqlite_master "
96537
 
      "  SELECT type, name, tbl_name, rootpage, sql"
96538
 
      "    FROM main.sqlite_master"
96539
 
      "   WHERE type='view' OR type='trigger'"
96540
 
      "      OR (type='table' AND rootpage=0)"
96541
 
  );
96542
 
  if( rc ) goto end_of_vacuum;
96543
 
 
96544
 
  /* At this point, unless the main db was completely empty, there is now a
96545
 
  ** transaction open on the vacuum database, but not on the main database.
96546
 
  ** Open a btree level transaction on the main database. This allows a
96547
 
  ** call to sqlite3BtreeCopyFile(). The main database btree level
96548
 
  ** transaction is then committed, so the SQL level never knows it was
96549
 
  ** opened for writing. This way, the SQL transaction used to create the
96550
 
  ** temporary database never needs to be committed.
96551
 
  */
96552
 
  {
96553
 
    u32 meta;
96554
 
    int i;
96555
 
 
96556
 
    /* This array determines which meta meta values are preserved in the
96557
 
    ** vacuum.  Even entries are the meta value number and odd entries
96558
 
    ** are an increment to apply to the meta value after the vacuum.
96559
 
    ** The increment is used to increase the schema cookie so that other
96560
 
    ** connections to the same database will know to reread the schema.
96561
 
    */
96562
 
    static const unsigned char aCopy[] = {
96563
 
       BTREE_SCHEMA_VERSION,     1,  /* Add one to the old schema cookie */
96564
 
       BTREE_DEFAULT_CACHE_SIZE, 0,  /* Preserve the default page cache size */
96565
 
       BTREE_TEXT_ENCODING,      0,  /* Preserve the text encoding */
96566
 
       BTREE_USER_VERSION,       0,  /* Preserve the user version */
96567
 
    };
96568
 
 
96569
 
    assert( 1==sqlite3BtreeIsInTrans(pTemp) );
96570
 
    assert( 1==sqlite3BtreeIsInTrans(pMain) );
96571
 
 
96572
 
    /* Copy Btree meta values */
96573
 
    for(i=0; i<ArraySize(aCopy); i+=2){
96574
 
      /* GetMeta() and UpdateMeta() cannot fail in this context because
96575
 
      ** we already have page 1 loaded into cache and marked dirty. */
96576
 
      sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
96577
 
      rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
96578
 
      if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
96579
 
    }
96580
 
 
96581
 
    rc = sqlite3BtreeCopyFile(pMain, pTemp);
96582
 
    if( rc!=SQLITE_OK ) goto end_of_vacuum;
96583
 
    rc = sqlite3BtreeCommit(pTemp);
96584
 
    if( rc!=SQLITE_OK ) goto end_of_vacuum;
96585
 
#ifndef SQLITE_OMIT_AUTOVACUUM
96586
 
    sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
96587
 
#endif
96588
 
  }
96589
 
 
96590
 
  assert( rc==SQLITE_OK );
96591
 
  rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
96592
 
 
96593
 
end_of_vacuum:
96594
 
  /* Restore the original value of db->flags */
96595
 
  db->flags = saved_flags;
96596
 
  db->nChange = saved_nChange;
96597
 
  db->nTotalChange = saved_nTotalChange;
96598
 
  db->xTrace = saved_xTrace;
96599
 
  sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
96600
 
 
96601
 
  /* Currently there is an SQL level transaction open on the vacuum
96602
 
  ** database. No locks are held on any other files (since the main file
96603
 
  ** was committed at the btree level). So it safe to end the transaction
96604
 
  ** by manually setting the autoCommit flag to true and detaching the
96605
 
  ** vacuum database. The vacuum_db journal file is deleted when the pager
96606
 
  ** is closed by the DETACH.
96607
 
  */
96608
 
  db->autoCommit = 1;
96609
 
 
96610
 
  if( pDb ){
96611
 
    sqlite3BtreeClose(pDb->pBt);
96612
 
    pDb->pBt = 0;
96613
 
    pDb->pSchema = 0;
96614
 
  }
96615
 
 
96616
 
  /* This both clears the schemas and reduces the size of the db->aDb[]
96617
 
  ** array. */ 
96618
 
  sqlite3ResetInternalSchema(db, -1);
96619
 
 
96620
 
  return rc;
96621
 
}
96622
 
 
96623
 
#endif  /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
96624
 
 
96625
 
/************** End of vacuum.c **********************************************/
96626
 
/************** Begin file vtab.c ********************************************/
96627
 
/*
96628
 
** 2006 June 10
96629
 
**
96630
 
** The author disclaims copyright to this source code.  In place of
96631
 
** a legal notice, here is a blessing:
96632
 
**
96633
 
**    May you do good and not evil.
96634
 
**    May you find forgiveness for yourself and forgive others.
96635
 
**    May you share freely, never taking more than you give.
96636
 
**
96637
 
*************************************************************************
96638
 
** This file contains code used to help implement virtual tables.
96639
 
*/
96640
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
96641
 
 
96642
 
/*
96643
 
** The actual function that does the work of creating a new module.
96644
 
** This function implements the sqlite3_create_module() and
96645
 
** sqlite3_create_module_v2() interfaces.
96646
 
*/
96647
 
static int createModule(
96648
 
  sqlite3 *db,                    /* Database in which module is registered */
96649
 
  const char *zName,              /* Name assigned to this module */
96650
 
  const sqlite3_module *pModule,  /* The definition of the module */
96651
 
  void *pAux,                     /* Context pointer for xCreate/xConnect */
96652
 
  void (*xDestroy)(void *)        /* Module destructor function */
96653
 
){
96654
 
  int rc, nName;
96655
 
  Module *pMod;
96656
 
 
96657
 
  sqlite3_mutex_enter(db->mutex);
96658
 
  nName = sqlite3Strlen30(zName);
96659
 
  pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
96660
 
  if( pMod ){
96661
 
    Module *pDel;
96662
 
    char *zCopy = (char *)(&pMod[1]);
96663
 
    memcpy(zCopy, zName, nName+1);
96664
 
    pMod->zName = zCopy;
96665
 
    pMod->pModule = pModule;
96666
 
    pMod->pAux = pAux;
96667
 
    pMod->xDestroy = xDestroy;
96668
 
    pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
96669
 
    if( pDel && pDel->xDestroy ){
96670
 
      pDel->xDestroy(pDel->pAux);
96671
 
    }
96672
 
    sqlite3DbFree(db, pDel);
96673
 
    if( pDel==pMod ){
96674
 
      db->mallocFailed = 1;
96675
 
    }
96676
 
    sqlite3ResetInternalSchema(db, -1);
96677
 
  }else if( xDestroy ){
96678
 
    xDestroy(pAux);
96679
 
  }
96680
 
  rc = sqlite3ApiExit(db, SQLITE_OK);
96681
 
  sqlite3_mutex_leave(db->mutex);
96682
 
  return rc;
96683
 
}
96684
 
 
96685
 
 
96686
 
/*
96687
 
** External API function used to create a new virtual-table module.
96688
 
*/
96689
 
SQLITE_API int sqlite3_create_module(
96690
 
  sqlite3 *db,                    /* Database in which module is registered */
96691
 
  const char *zName,              /* Name assigned to this module */
96692
 
  const sqlite3_module *pModule,  /* The definition of the module */
96693
 
  void *pAux                      /* Context pointer for xCreate/xConnect */
96694
 
){
96695
 
  return createModule(db, zName, pModule, pAux, 0);
96696
 
}
96697
 
 
96698
 
/*
96699
 
** External API function used to create a new virtual-table module.
96700
 
*/
96701
 
SQLITE_API int sqlite3_create_module_v2(
96702
 
  sqlite3 *db,                    /* Database in which module is registered */
96703
 
  const char *zName,              /* Name assigned to this module */
96704
 
  const sqlite3_module *pModule,  /* The definition of the module */
96705
 
  void *pAux,                     /* Context pointer for xCreate/xConnect */
96706
 
  void (*xDestroy)(void *)        /* Module destructor function */
96707
 
){
96708
 
  return createModule(db, zName, pModule, pAux, xDestroy);
96709
 
}
96710
 
 
96711
 
/*
96712
 
** Lock the virtual table so that it cannot be disconnected.
96713
 
** Locks nest.  Every lock should have a corresponding unlock.
96714
 
** If an unlock is omitted, resources leaks will occur.  
96715
 
**
96716
 
** If a disconnect is attempted while a virtual table is locked,
96717
 
** the disconnect is deferred until all locks have been removed.
96718
 
*/
96719
 
SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
96720
 
  pVTab->nRef++;
96721
 
}
96722
 
 
96723
 
 
96724
 
/*
96725
 
** pTab is a pointer to a Table structure representing a virtual-table.
96726
 
** Return a pointer to the VTable object used by connection db to access 
96727
 
** this virtual-table, if one has been created, or NULL otherwise.
96728
 
*/
96729
 
SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
96730
 
  VTable *pVtab;
96731
 
  assert( IsVirtual(pTab) );
96732
 
  for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
96733
 
  return pVtab;
96734
 
}
96735
 
 
96736
 
/*
96737
 
** Decrement the ref-count on a virtual table object. When the ref-count
96738
 
** reaches zero, call the xDisconnect() method to delete the object.
96739
 
*/
96740
 
SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
96741
 
  sqlite3 *db = pVTab->db;
96742
 
 
96743
 
  assert( db );
96744
 
  assert( pVTab->nRef>0 );
96745
 
  assert( sqlite3SafetyCheckOk(db) );
96746
 
 
96747
 
  pVTab->nRef--;
96748
 
  if( pVTab->nRef==0 ){
96749
 
    sqlite3_vtab *p = pVTab->pVtab;
96750
 
    if( p ){
96751
 
      p->pModule->xDisconnect(p);
96752
 
    }
96753
 
    sqlite3DbFree(db, pVTab);
96754
 
  }
96755
 
}
96756
 
 
96757
 
/*
96758
 
** Table p is a virtual table. This function moves all elements in the
96759
 
** p->pVTable list to the sqlite3.pDisconnect lists of their associated
96760
 
** database connections to be disconnected at the next opportunity. 
96761
 
** Except, if argument db is not NULL, then the entry associated with
96762
 
** connection db is left in the p->pVTable list.
96763
 
*/
96764
 
static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
96765
 
  VTable *pRet = 0;
96766
 
  VTable *pVTable = p->pVTable;
96767
 
  p->pVTable = 0;
96768
 
 
96769
 
  /* Assert that the mutex (if any) associated with the BtShared database 
96770
 
  ** that contains table p is held by the caller. See header comments 
96771
 
  ** above function sqlite3VtabUnlockList() for an explanation of why
96772
 
  ** this makes it safe to access the sqlite3.pDisconnect list of any
96773
 
  ** database connection that may have an entry in the p->pVTable list.
96774
 
  */
96775
 
  assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
96776
 
 
96777
 
  while( pVTable ){
96778
 
    sqlite3 *db2 = pVTable->db;
96779
 
    VTable *pNext = pVTable->pNext;
96780
 
    assert( db2 );
96781
 
    if( db2==db ){
96782
 
      pRet = pVTable;
96783
 
      p->pVTable = pRet;
96784
 
      pRet->pNext = 0;
96785
 
    }else{
96786
 
      pVTable->pNext = db2->pDisconnect;
96787
 
      db2->pDisconnect = pVTable;
96788
 
    }
96789
 
    pVTable = pNext;
96790
 
  }
96791
 
 
96792
 
  assert( !db || pRet );
96793
 
  return pRet;
96794
 
}
96795
 
 
96796
 
 
96797
 
/*
96798
 
** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
96799
 
**
96800
 
** This function may only be called when the mutexes associated with all
96801
 
** shared b-tree databases opened using connection db are held by the 
96802
 
** caller. This is done to protect the sqlite3.pDisconnect list. The
96803
 
** sqlite3.pDisconnect list is accessed only as follows:
96804
 
**
96805
 
**   1) By this function. In this case, all BtShared mutexes and the mutex
96806
 
**      associated with the database handle itself must be held.
96807
 
**
96808
 
**   2) By function vtabDisconnectAll(), when it adds a VTable entry to
96809
 
**      the sqlite3.pDisconnect list. In this case either the BtShared mutex
96810
 
**      associated with the database the virtual table is stored in is held
96811
 
**      or, if the virtual table is stored in a non-sharable database, then
96812
 
**      the database handle mutex is held.
96813
 
**
96814
 
** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously 
96815
 
** by multiple threads. It is thread-safe.
96816
 
*/
96817
 
SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
96818
 
  VTable *p = db->pDisconnect;
96819
 
  db->pDisconnect = 0;
96820
 
 
96821
 
  assert( sqlite3BtreeHoldsAllMutexes(db) );
96822
 
  assert( sqlite3_mutex_held(db->mutex) );
96823
 
 
96824
 
  if( p ){
96825
 
    sqlite3ExpirePreparedStatements(db);
96826
 
    do {
96827
 
      VTable *pNext = p->pNext;
96828
 
      sqlite3VtabUnlock(p);
96829
 
      p = pNext;
96830
 
    }while( p );
96831
 
  }
96832
 
}
96833
 
 
96834
 
/*
96835
 
** Clear any and all virtual-table information from the Table record.
96836
 
** This routine is called, for example, just before deleting the Table
96837
 
** record.
96838
 
**
96839
 
** Since it is a virtual-table, the Table structure contains a pointer
96840
 
** to the head of a linked list of VTable structures. Each VTable 
96841
 
** structure is associated with a single sqlite3* user of the schema.
96842
 
** The reference count of the VTable structure associated with database 
96843
 
** connection db is decremented immediately (which may lead to the 
96844
 
** structure being xDisconnected and free). Any other VTable structures
96845
 
** in the list are moved to the sqlite3.pDisconnect list of the associated 
96846
 
** database connection.
96847
 
*/
96848
 
SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
96849
 
  if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
96850
 
  if( p->azModuleArg ){
96851
 
    int i;
96852
 
    for(i=0; i<p->nModuleArg; i++){
96853
 
      sqlite3DbFree(db, p->azModuleArg[i]);
96854
 
    }
96855
 
    sqlite3DbFree(db, p->azModuleArg);
96856
 
  }
96857
 
}
96858
 
 
96859
 
/*
96860
 
** Add a new module argument to pTable->azModuleArg[].
96861
 
** The string is not copied - the pointer is stored.  The
96862
 
** string will be freed automatically when the table is
96863
 
** deleted.
96864
 
*/
96865
 
static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
96866
 
  int i = pTable->nModuleArg++;
96867
 
  int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
96868
 
  char **azModuleArg;
96869
 
  azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
96870
 
  if( azModuleArg==0 ){
96871
 
    int j;
96872
 
    for(j=0; j<i; j++){
96873
 
      sqlite3DbFree(db, pTable->azModuleArg[j]);
96874
 
    }
96875
 
    sqlite3DbFree(db, zArg);
96876
 
    sqlite3DbFree(db, pTable->azModuleArg);
96877
 
    pTable->nModuleArg = 0;
96878
 
  }else{
96879
 
    azModuleArg[i] = zArg;
96880
 
    azModuleArg[i+1] = 0;
96881
 
  }
96882
 
  pTable->azModuleArg = azModuleArg;
96883
 
}
96884
 
 
96885
 
/*
96886
 
** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
96887
 
** statement.  The module name has been parsed, but the optional list
96888
 
** of parameters that follow the module name are still pending.
96889
 
*/
96890
 
SQLITE_PRIVATE void sqlite3VtabBeginParse(
96891
 
  Parse *pParse,        /* Parsing context */
96892
 
  Token *pName1,        /* Name of new table, or database name */
96893
 
  Token *pName2,        /* Name of new table or NULL */
96894
 
  Token *pModuleName    /* Name of the module for the virtual table */
96895
 
){
96896
 
  int iDb;              /* The database the table is being created in */
96897
 
  Table *pTable;        /* The new virtual table */
96898
 
  sqlite3 *db;          /* Database connection */
96899
 
 
96900
 
  sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
96901
 
  pTable = pParse->pNewTable;
96902
 
  if( pTable==0 ) return;
96903
 
  assert( 0==pTable->pIndex );
96904
 
 
96905
 
  db = pParse->db;
96906
 
  iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
96907
 
  assert( iDb>=0 );
96908
 
 
96909
 
  pTable->tabFlags |= TF_Virtual;
96910
 
  pTable->nModuleArg = 0;
96911
 
  addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
96912
 
  addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
96913
 
  addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
96914
 
  pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
96915
 
 
96916
 
#ifndef SQLITE_OMIT_AUTHORIZATION
96917
 
  /* Creating a virtual table invokes the authorization callback twice.
96918
 
  ** The first invocation, to obtain permission to INSERT a row into the
96919
 
  ** sqlite_master table, has already been made by sqlite3StartTable().
96920
 
  ** The second call, to obtain permission to create the table, is made now.
96921
 
  */
96922
 
  if( pTable->azModuleArg ){
96923
 
    sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, 
96924
 
            pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
96925
 
  }
96926
 
#endif
96927
 
}
96928
 
 
96929
 
/*
96930
 
** This routine takes the module argument that has been accumulating
96931
 
** in pParse->zArg[] and appends it to the list of arguments on the
96932
 
** virtual table currently under construction in pParse->pTable.
96933
 
*/
96934
 
static void addArgumentToVtab(Parse *pParse){
96935
 
  if( pParse->sArg.z && ALWAYS(pParse->pNewTable) ){
96936
 
    const char *z = (const char*)pParse->sArg.z;
96937
 
    int n = pParse->sArg.n;
96938
 
    sqlite3 *db = pParse->db;
96939
 
    addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
96940
 
  }
96941
 
}
96942
 
 
96943
 
/*
96944
 
** The parser calls this routine after the CREATE VIRTUAL TABLE statement
96945
 
** has been completely parsed.
96946
 
*/
96947
 
SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
96948
 
  Table *pTab = pParse->pNewTable;  /* The table being constructed */
96949
 
  sqlite3 *db = pParse->db;         /* The database connection */
96950
 
 
96951
 
  if( pTab==0 ) return;
96952
 
  addArgumentToVtab(pParse);
96953
 
  pParse->sArg.z = 0;
96954
 
  if( pTab->nModuleArg<1 ) return;
96955
 
  
96956
 
  /* If the CREATE VIRTUAL TABLE statement is being entered for the
96957
 
  ** first time (in other words if the virtual table is actually being
96958
 
  ** created now instead of just being read out of sqlite_master) then
96959
 
  ** do additional initialization work and store the statement text
96960
 
  ** in the sqlite_master table.
96961
 
  */
96962
 
  if( !db->init.busy ){
96963
 
    char *zStmt;
96964
 
    char *zWhere;
96965
 
    int iDb;
96966
 
    Vdbe *v;
96967
 
 
96968
 
    /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
96969
 
    if( pEnd ){
96970
 
      pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
96971
 
    }
96972
 
    zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
96973
 
 
96974
 
    /* A slot for the record has already been allocated in the 
96975
 
    ** SQLITE_MASTER table.  We just need to update that slot with all
96976
 
    ** the information we've collected.  
96977
 
    **
96978
 
    ** The VM register number pParse->regRowid holds the rowid of an
96979
 
    ** entry in the sqlite_master table tht was created for this vtab
96980
 
    ** by sqlite3StartTable().
96981
 
    */
96982
 
    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
96983
 
    sqlite3NestedParse(pParse,
96984
 
      "UPDATE %Q.%s "
96985
 
         "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
96986
 
       "WHERE rowid=#%d",
96987
 
      db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
96988
 
      pTab->zName,
96989
 
      pTab->zName,
96990
 
      zStmt,
96991
 
      pParse->regRowid
96992
 
    );
96993
 
    sqlite3DbFree(db, zStmt);
96994
 
    v = sqlite3GetVdbe(pParse);
96995
 
    sqlite3ChangeCookie(pParse, iDb);
96996
 
 
96997
 
    sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
96998
 
    zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
96999
 
    sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
97000
 
    sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, 
97001
 
                         pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
97002
 
  }
97003
 
 
97004
 
  /* If we are rereading the sqlite_master table create the in-memory
97005
 
  ** record of the table. The xConnect() method is not called until
97006
 
  ** the first time the virtual table is used in an SQL statement. This
97007
 
  ** allows a schema that contains virtual tables to be loaded before
97008
 
  ** the required virtual table implementations are registered.  */
97009
 
  else {
97010
 
    Table *pOld;
97011
 
    Schema *pSchema = pTab->pSchema;
97012
 
    const char *zName = pTab->zName;
97013
 
    int nName = sqlite3Strlen30(zName);
97014
 
    assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
97015
 
    pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
97016
 
    if( pOld ){
97017
 
      db->mallocFailed = 1;
97018
 
      assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
97019
 
      return;
97020
 
    }
97021
 
    pParse->pNewTable = 0;
97022
 
  }
97023
 
}
97024
 
 
97025
 
/*
97026
 
** The parser calls this routine when it sees the first token
97027
 
** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
97028
 
*/
97029
 
SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
97030
 
  addArgumentToVtab(pParse);
97031
 
  pParse->sArg.z = 0;
97032
 
  pParse->sArg.n = 0;
97033
 
}
97034
 
 
97035
 
/*
97036
 
** The parser calls this routine for each token after the first token
97037
 
** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
97038
 
*/
97039
 
SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
97040
 
  Token *pArg = &pParse->sArg;
97041
 
  if( pArg->z==0 ){
97042
 
    pArg->z = p->z;
97043
 
    pArg->n = p->n;
97044
 
  }else{
97045
 
    assert(pArg->z < p->z);
97046
 
    pArg->n = (int)(&p->z[p->n] - pArg->z);
97047
 
  }
97048
 
}
97049
 
 
97050
 
/*
97051
 
** Invoke a virtual table constructor (either xCreate or xConnect). The
97052
 
** pointer to the function to invoke is passed as the fourth parameter
97053
 
** to this procedure.
97054
 
*/
97055
 
static int vtabCallConstructor(
97056
 
  sqlite3 *db, 
97057
 
  Table *pTab,
97058
 
  Module *pMod,
97059
 
  int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
97060
 
  char **pzErr
97061
 
){
97062
 
  VTable *pVTable;
97063
 
  int rc;
97064
 
  const char *const*azArg = (const char *const*)pTab->azModuleArg;
97065
 
  int nArg = pTab->nModuleArg;
97066
 
  char *zErr = 0;
97067
 
  char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
97068
 
 
97069
 
  if( !zModuleName ){
97070
 
    return SQLITE_NOMEM;
97071
 
  }
97072
 
 
97073
 
  pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
97074
 
  if( !pVTable ){
97075
 
    sqlite3DbFree(db, zModuleName);
97076
 
    return SQLITE_NOMEM;
97077
 
  }
97078
 
  pVTable->db = db;
97079
 
  pVTable->pMod = pMod;
97080
 
 
97081
 
  assert( !db->pVTab );
97082
 
  assert( xConstruct );
97083
 
  db->pVTab = pTab;
97084
 
 
97085
 
  /* Invoke the virtual table constructor */
97086
 
  rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
97087
 
  if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
97088
 
 
97089
 
  if( SQLITE_OK!=rc ){
97090
 
    if( zErr==0 ){
97091
 
      *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
97092
 
    }else {
97093
 
      *pzErr = sqlite3MPrintf(db, "%s", zErr);
97094
 
      sqlite3_free(zErr);
97095
 
    }
97096
 
    sqlite3DbFree(db, pVTable);
97097
 
  }else if( ALWAYS(pVTable->pVtab) ){
97098
 
    /* Justification of ALWAYS():  A correct vtab constructor must allocate
97099
 
    ** the sqlite3_vtab object if successful.  */
97100
 
    pVTable->pVtab->pModule = pMod->pModule;
97101
 
    pVTable->nRef = 1;
97102
 
    if( db->pVTab ){
97103
 
      const char *zFormat = "vtable constructor did not declare schema: %s";
97104
 
      *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
97105
 
      sqlite3VtabUnlock(pVTable);
97106
 
      rc = SQLITE_ERROR;
97107
 
    }else{
97108
 
      int iCol;
97109
 
      /* If everything went according to plan, link the new VTable structure
97110
 
      ** into the linked list headed by pTab->pVTable. Then loop through the 
97111
 
      ** columns of the table to see if any of them contain the token "hidden".
97112
 
      ** If so, set the Column.isHidden flag and remove the token from
97113
 
      ** the type string.  */
97114
 
      pVTable->pNext = pTab->pVTable;
97115
 
      pTab->pVTable = pVTable;
97116
 
 
97117
 
      for(iCol=0; iCol<pTab->nCol; iCol++){
97118
 
        char *zType = pTab->aCol[iCol].zType;
97119
 
        int nType;
97120
 
        int i = 0;
97121
 
        if( !zType ) continue;
97122
 
        nType = sqlite3Strlen30(zType);
97123
 
        if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
97124
 
          for(i=0; i<nType; i++){
97125
 
            if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
97126
 
             && (zType[i+7]=='\0' || zType[i+7]==' ')
97127
 
            ){
97128
 
              i++;
97129
 
              break;
97130
 
            }
97131
 
          }
97132
 
        }
97133
 
        if( i<nType ){
97134
 
          int j;
97135
 
          int nDel = 6 + (zType[i+6] ? 1 : 0);
97136
 
          for(j=i; (j+nDel)<=nType; j++){
97137
 
            zType[j] = zType[j+nDel];
97138
 
          }
97139
 
          if( zType[i]=='\0' && i>0 ){
97140
 
            assert(zType[i-1]==' ');
97141
 
            zType[i-1] = '\0';
97142
 
          }
97143
 
          pTab->aCol[iCol].isHidden = 1;
97144
 
        }
97145
 
      }
97146
 
    }
97147
 
  }
97148
 
 
97149
 
  sqlite3DbFree(db, zModuleName);
97150
 
  db->pVTab = 0;
97151
 
  return rc;
97152
 
}
97153
 
 
97154
 
/*
97155
 
** This function is invoked by the parser to call the xConnect() method
97156
 
** of the virtual table pTab. If an error occurs, an error code is returned 
97157
 
** and an error left in pParse.
97158
 
**
97159
 
** This call is a no-op if table pTab is not a virtual table.
97160
 
*/
97161
 
SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
97162
 
  sqlite3 *db = pParse->db;
97163
 
  const char *zMod;
97164
 
  Module *pMod;
97165
 
  int rc;
97166
 
 
97167
 
  assert( pTab );
97168
 
  if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
97169
 
    return SQLITE_OK;
97170
 
  }
97171
 
 
97172
 
  /* Locate the required virtual table module */
97173
 
  zMod = pTab->azModuleArg[0];
97174
 
  pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
97175
 
 
97176
 
  if( !pMod ){
97177
 
    const char *zModule = pTab->azModuleArg[0];
97178
 
    sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
97179
 
    rc = SQLITE_ERROR;
97180
 
  }else{
97181
 
    char *zErr = 0;
97182
 
    rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
97183
 
    if( rc!=SQLITE_OK ){
97184
 
      sqlite3ErrorMsg(pParse, "%s", zErr);
97185
 
    }
97186
 
    sqlite3DbFree(db, zErr);
97187
 
  }
97188
 
 
97189
 
  return rc;
97190
 
}
97191
 
 
97192
 
/*
97193
 
** Add the virtual table pVTab to the array sqlite3.aVTrans[].
97194
 
*/
97195
 
static int addToVTrans(sqlite3 *db, VTable *pVTab){
97196
 
  const int ARRAY_INCR = 5;
97197
 
 
97198
 
  /* Grow the sqlite3.aVTrans array if required */
97199
 
  if( (db->nVTrans%ARRAY_INCR)==0 ){
97200
 
    VTable **aVTrans;
97201
 
    int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
97202
 
    aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
97203
 
    if( !aVTrans ){
97204
 
      return SQLITE_NOMEM;
97205
 
    }
97206
 
    memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
97207
 
    db->aVTrans = aVTrans;
97208
 
  }
97209
 
 
97210
 
  /* Add pVtab to the end of sqlite3.aVTrans */
97211
 
  db->aVTrans[db->nVTrans++] = pVTab;
97212
 
  sqlite3VtabLock(pVTab);
97213
 
  return SQLITE_OK;
97214
 
}
97215
 
 
97216
 
/*
97217
 
** This function is invoked by the vdbe to call the xCreate method
97218
 
** of the virtual table named zTab in database iDb. 
97219
 
**
97220
 
** If an error occurs, *pzErr is set to point an an English language
97221
 
** description of the error and an SQLITE_XXX error code is returned.
97222
 
** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
97223
 
*/
97224
 
SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
97225
 
  int rc = SQLITE_OK;
97226
 
  Table *pTab;
97227
 
  Module *pMod;
97228
 
  const char *zMod;
97229
 
 
97230
 
  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
97231
 
  assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
97232
 
 
97233
 
  /* Locate the required virtual table module */
97234
 
  zMod = pTab->azModuleArg[0];
97235
 
  pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
97236
 
 
97237
 
  /* If the module has been registered and includes a Create method, 
97238
 
  ** invoke it now. If the module has not been registered, return an 
97239
 
  ** error. Otherwise, do nothing.
97240
 
  */
97241
 
  if( !pMod ){
97242
 
    *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
97243
 
    rc = SQLITE_ERROR;
97244
 
  }else{
97245
 
    rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
97246
 
  }
97247
 
 
97248
 
  /* Justification of ALWAYS():  The xConstructor method is required to
97249
 
  ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
97250
 
  if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
97251
 
      rc = addToVTrans(db, sqlite3GetVTable(db, pTab));
97252
 
  }
97253
 
 
97254
 
  return rc;
97255
 
}
97256
 
 
97257
 
/*
97258
 
** This function is used to set the schema of a virtual table.  It is only
97259
 
** valid to call this function from within the xCreate() or xConnect() of a
97260
 
** virtual table module.
97261
 
*/
97262
 
SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
97263
 
  Parse *pParse;
97264
 
 
97265
 
  int rc = SQLITE_OK;
97266
 
  Table *pTab;
97267
 
  char *zErr = 0;
97268
 
 
97269
 
  sqlite3_mutex_enter(db->mutex);
97270
 
  pTab = db->pVTab;
97271
 
  if( !pTab ){
97272
 
    sqlite3Error(db, SQLITE_MISUSE, 0);
97273
 
    sqlite3_mutex_leave(db->mutex);
97274
 
    return SQLITE_MISUSE_BKPT;
97275
 
  }
97276
 
  assert( (pTab->tabFlags & TF_Virtual)!=0 );
97277
 
 
97278
 
  pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
97279
 
  if( pParse==0 ){
97280
 
    rc = SQLITE_NOMEM;
97281
 
  }else{
97282
 
    pParse->declareVtab = 1;
97283
 
    pParse->db = db;
97284
 
    pParse->nQueryLoop = 1;
97285
 
  
97286
 
    if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) 
97287
 
     && pParse->pNewTable
97288
 
     && !db->mallocFailed
97289
 
     && !pParse->pNewTable->pSelect
97290
 
     && (pParse->pNewTable->tabFlags & TF_Virtual)==0
97291
 
    ){
97292
 
      if( !pTab->aCol ){
97293
 
        pTab->aCol = pParse->pNewTable->aCol;
97294
 
        pTab->nCol = pParse->pNewTable->nCol;
97295
 
        pParse->pNewTable->nCol = 0;
97296
 
        pParse->pNewTable->aCol = 0;
97297
 
      }
97298
 
      db->pVTab = 0;
97299
 
    }else{
97300
 
      sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
97301
 
      sqlite3DbFree(db, zErr);
97302
 
      rc = SQLITE_ERROR;
97303
 
    }
97304
 
    pParse->declareVtab = 0;
97305
 
  
97306
 
    if( pParse->pVdbe ){
97307
 
      sqlite3VdbeFinalize(pParse->pVdbe);
97308
 
    }
97309
 
    sqlite3DeleteTable(db, pParse->pNewTable);
97310
 
    sqlite3StackFree(db, pParse);
97311
 
  }
97312
 
 
97313
 
  assert( (rc&0xff)==rc );
97314
 
  rc = sqlite3ApiExit(db, rc);
97315
 
  sqlite3_mutex_leave(db->mutex);
97316
 
  return rc;
97317
 
}
97318
 
 
97319
 
/*
97320
 
** This function is invoked by the vdbe to call the xDestroy method
97321
 
** of the virtual table named zTab in database iDb. This occurs
97322
 
** when a DROP TABLE is mentioned.
97323
 
**
97324
 
** This call is a no-op if zTab is not a virtual table.
97325
 
*/
97326
 
SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
97327
 
  int rc = SQLITE_OK;
97328
 
  Table *pTab;
97329
 
 
97330
 
  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
97331
 
  if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
97332
 
    VTable *p = vtabDisconnectAll(db, pTab);
97333
 
 
97334
 
    assert( rc==SQLITE_OK );
97335
 
    rc = p->pMod->pModule->xDestroy(p->pVtab);
97336
 
 
97337
 
    /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
97338
 
    if( rc==SQLITE_OK ){
97339
 
      assert( pTab->pVTable==p && p->pNext==0 );
97340
 
      p->pVtab = 0;
97341
 
      pTab->pVTable = 0;
97342
 
      sqlite3VtabUnlock(p);
97343
 
    }
97344
 
  }
97345
 
 
97346
 
  return rc;
97347
 
}
97348
 
 
97349
 
/*
97350
 
** This function invokes either the xRollback or xCommit method
97351
 
** of each of the virtual tables in the sqlite3.aVTrans array. The method
97352
 
** called is identified by the second argument, "offset", which is
97353
 
** the offset of the method to call in the sqlite3_module structure.
97354
 
**
97355
 
** The array is cleared after invoking the callbacks. 
97356
 
*/
97357
 
static void callFinaliser(sqlite3 *db, int offset){
97358
 
  int i;
97359
 
  if( db->aVTrans ){
97360
 
    for(i=0; i<db->nVTrans; i++){
97361
 
      VTable *pVTab = db->aVTrans[i];
97362
 
      sqlite3_vtab *p = pVTab->pVtab;
97363
 
      if( p ){
97364
 
        int (*x)(sqlite3_vtab *);
97365
 
        x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
97366
 
        if( x ) x(p);
97367
 
      }
97368
 
      sqlite3VtabUnlock(pVTab);
97369
 
    }
97370
 
    sqlite3DbFree(db, db->aVTrans);
97371
 
    db->nVTrans = 0;
97372
 
    db->aVTrans = 0;
97373
 
  }
97374
 
}
97375
 
 
97376
 
/*
97377
 
** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
97378
 
** array. Return the error code for the first error that occurs, or
97379
 
** SQLITE_OK if all xSync operations are successful.
97380
 
**
97381
 
** Set *pzErrmsg to point to a buffer that should be released using 
97382
 
** sqlite3DbFree() containing an error message, if one is available.
97383
 
*/
97384
 
SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
97385
 
  int i;
97386
 
  int rc = SQLITE_OK;
97387
 
  VTable **aVTrans = db->aVTrans;
97388
 
 
97389
 
  db->aVTrans = 0;
97390
 
  for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
97391
 
    int (*x)(sqlite3_vtab *);
97392
 
    sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
97393
 
    if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
97394
 
      rc = x(pVtab);
97395
 
      sqlite3DbFree(db, *pzErrmsg);
97396
 
      *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
97397
 
      sqlite3_free(pVtab->zErrMsg);
97398
 
    }
97399
 
  }
97400
 
  db->aVTrans = aVTrans;
97401
 
  return rc;
97402
 
}
97403
 
 
97404
 
/*
97405
 
** Invoke the xRollback method of all virtual tables in the 
97406
 
** sqlite3.aVTrans array. Then clear the array itself.
97407
 
*/
97408
 
SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
97409
 
  callFinaliser(db, offsetof(sqlite3_module,xRollback));
97410
 
  return SQLITE_OK;
97411
 
}
97412
 
 
97413
 
/*
97414
 
** Invoke the xCommit method of all virtual tables in the 
97415
 
** sqlite3.aVTrans array. Then clear the array itself.
97416
 
*/
97417
 
SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
97418
 
  callFinaliser(db, offsetof(sqlite3_module,xCommit));
97419
 
  return SQLITE_OK;
97420
 
}
97421
 
 
97422
 
/*
97423
 
** If the virtual table pVtab supports the transaction interface
97424
 
** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
97425
 
** not currently open, invoke the xBegin method now.
97426
 
**
97427
 
** If the xBegin call is successful, place the sqlite3_vtab pointer
97428
 
** in the sqlite3.aVTrans array.
97429
 
*/
97430
 
SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
97431
 
  int rc = SQLITE_OK;
97432
 
  const sqlite3_module *pModule;
97433
 
 
97434
 
  /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
97435
 
  ** than zero, then this function is being called from within a
97436
 
  ** virtual module xSync() callback. It is illegal to write to 
97437
 
  ** virtual module tables in this case, so return SQLITE_LOCKED.
97438
 
  */
97439
 
  if( sqlite3VtabInSync(db) ){
97440
 
    return SQLITE_LOCKED;
97441
 
  }
97442
 
  if( !pVTab ){
97443
 
    return SQLITE_OK;
97444
 
  } 
97445
 
  pModule = pVTab->pVtab->pModule;
97446
 
 
97447
 
  if( pModule->xBegin ){
97448
 
    int i;
97449
 
 
97450
 
 
97451
 
    /* If pVtab is already in the aVTrans array, return early */
97452
 
    for(i=0; i<db->nVTrans; i++){
97453
 
      if( db->aVTrans[i]==pVTab ){
97454
 
        return SQLITE_OK;
97455
 
      }
97456
 
    }
97457
 
 
97458
 
    /* Invoke the xBegin method */
97459
 
    rc = pModule->xBegin(pVTab->pVtab);
97460
 
    if( rc==SQLITE_OK ){
97461
 
      rc = addToVTrans(db, pVTab);
97462
 
    }
97463
 
  }
97464
 
  return rc;
97465
 
}
97466
 
 
97467
 
/*
97468
 
** The first parameter (pDef) is a function implementation.  The
97469
 
** second parameter (pExpr) is the first argument to this function.
97470
 
** If pExpr is a column in a virtual table, then let the virtual
97471
 
** table implementation have an opportunity to overload the function.
97472
 
**
97473
 
** This routine is used to allow virtual table implementations to
97474
 
** overload MATCH, LIKE, GLOB, and REGEXP operators.
97475
 
**
97476
 
** Return either the pDef argument (indicating no change) or a 
97477
 
** new FuncDef structure that is marked as ephemeral using the
97478
 
** SQLITE_FUNC_EPHEM flag.
97479
 
*/
97480
 
SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
97481
 
  sqlite3 *db,    /* Database connection for reporting malloc problems */
97482
 
  FuncDef *pDef,  /* Function to possibly overload */
97483
 
  int nArg,       /* Number of arguments to the function */
97484
 
  Expr *pExpr     /* First argument to the function */
97485
 
){
97486
 
  Table *pTab;
97487
 
  sqlite3_vtab *pVtab;
97488
 
  sqlite3_module *pMod;
97489
 
  void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
97490
 
  void *pArg = 0;
97491
 
  FuncDef *pNew;
97492
 
  int rc = 0;
97493
 
  char *zLowerName;
97494
 
  unsigned char *z;
97495
 
 
97496
 
 
97497
 
  /* Check to see the left operand is a column in a virtual table */
97498
 
  if( NEVER(pExpr==0) ) return pDef;
97499
 
  if( pExpr->op!=TK_COLUMN ) return pDef;
97500
 
  pTab = pExpr->pTab;
97501
 
  if( NEVER(pTab==0) ) return pDef;
97502
 
  if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
97503
 
  pVtab = sqlite3GetVTable(db, pTab)->pVtab;
97504
 
  assert( pVtab!=0 );
97505
 
  assert( pVtab->pModule!=0 );
97506
 
  pMod = (sqlite3_module *)pVtab->pModule;
97507
 
  if( pMod->xFindFunction==0 ) return pDef;
97508
 
 
97509
 
  /* Call the xFindFunction method on the virtual table implementation
97510
 
  ** to see if the implementation wants to overload this function 
97511
 
  */
97512
 
  zLowerName = sqlite3DbStrDup(db, pDef->zName);
97513
 
  if( zLowerName ){
97514
 
    for(z=(unsigned char*)zLowerName; *z; z++){
97515
 
      *z = sqlite3UpperToLower[*z];
97516
 
    }
97517
 
    rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
97518
 
    sqlite3DbFree(db, zLowerName);
97519
 
  }
97520
 
  if( rc==0 ){
97521
 
    return pDef;
97522
 
  }
97523
 
 
97524
 
  /* Create a new ephemeral function definition for the overloaded
97525
 
  ** function */
97526
 
  pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
97527
 
                             + sqlite3Strlen30(pDef->zName) + 1);
97528
 
  if( pNew==0 ){
97529
 
    return pDef;
97530
 
  }
97531
 
  *pNew = *pDef;
97532
 
  pNew->zName = (char *)&pNew[1];
97533
 
  memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
97534
 
  pNew->xFunc = xFunc;
97535
 
  pNew->pUserData = pArg;
97536
 
  pNew->flags |= SQLITE_FUNC_EPHEM;
97537
 
  return pNew;
97538
 
}
97539
 
 
97540
 
/*
97541
 
** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
97542
 
** array so that an OP_VBegin will get generated for it.  Add pTab to the
97543
 
** array if it is missing.  If pTab is already in the array, this routine
97544
 
** is a no-op.
97545
 
*/
97546
 
SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
97547
 
  Parse *pToplevel = sqlite3ParseToplevel(pParse);
97548
 
  int i, n;
97549
 
  Table **apVtabLock;
97550
 
 
97551
 
  assert( IsVirtual(pTab) );
97552
 
  for(i=0; i<pToplevel->nVtabLock; i++){
97553
 
    if( pTab==pToplevel->apVtabLock[i] ) return;
97554
 
  }
97555
 
  n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
97556
 
  apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
97557
 
  if( apVtabLock ){
97558
 
    pToplevel->apVtabLock = apVtabLock;
97559
 
    pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
97560
 
  }else{
97561
 
    pToplevel->db->mallocFailed = 1;
97562
 
  }
97563
 
}
97564
 
 
97565
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
97566
 
 
97567
 
/************** End of vtab.c ************************************************/
97568
 
/************** Begin file where.c *******************************************/
97569
 
/*
97570
 
** 2001 September 15
97571
 
**
97572
 
** The author disclaims copyright to this source code.  In place of
97573
 
** a legal notice, here is a blessing:
97574
 
**
97575
 
**    May you do good and not evil.
97576
 
**    May you find forgiveness for yourself and forgive others.
97577
 
**    May you share freely, never taking more than you give.
97578
 
**
97579
 
*************************************************************************
97580
 
** This module contains C code that generates VDBE code used to process
97581
 
** the WHERE clause of SQL statements.  This module is responsible for
97582
 
** generating the code that loops through a table looking for applicable
97583
 
** rows.  Indices are selected and used to speed the search when doing
97584
 
** so is applicable.  Because this module is responsible for selecting
97585
 
** indices, you might also think of this module as the "query optimizer".
97586
 
*/
97587
 
 
97588
 
 
97589
 
/*
97590
 
** Trace output macros
97591
 
*/
97592
 
#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
97593
 
SQLITE_PRIVATE int sqlite3WhereTrace = 0;
97594
 
#endif
97595
 
#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
97596
 
# define WHERETRACE(X)  if(sqlite3WhereTrace) sqlite3DebugPrintf X
97597
 
#else
97598
 
# define WHERETRACE(X)
97599
 
#endif
97600
 
 
97601
 
/* Forward reference
97602
 
*/
97603
 
typedef struct WhereClause WhereClause;
97604
 
typedef struct WhereMaskSet WhereMaskSet;
97605
 
typedef struct WhereOrInfo WhereOrInfo;
97606
 
typedef struct WhereAndInfo WhereAndInfo;
97607
 
typedef struct WhereCost WhereCost;
97608
 
 
97609
 
/*
97610
 
** The query generator uses an array of instances of this structure to
97611
 
** help it analyze the subexpressions of the WHERE clause.  Each WHERE
97612
 
** clause subexpression is separated from the others by AND operators,
97613
 
** usually, or sometimes subexpressions separated by OR.
97614
 
**
97615
 
** All WhereTerms are collected into a single WhereClause structure.  
97616
 
** The following identity holds:
97617
 
**
97618
 
**        WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
97619
 
**
97620
 
** When a term is of the form:
97621
 
**
97622
 
**              X <op> <expr>
97623
 
**
97624
 
** where X is a column name and <op> is one of certain operators,
97625
 
** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
97626
 
** cursor number and column number for X.  WhereTerm.eOperator records
97627
 
** the <op> using a bitmask encoding defined by WO_xxx below.  The
97628
 
** use of a bitmask encoding for the operator allows us to search
97629
 
** quickly for terms that match any of several different operators.
97630
 
**
97631
 
** A WhereTerm might also be two or more subterms connected by OR:
97632
 
**
97633
 
**         (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
97634
 
**
97635
 
** In this second case, wtFlag as the TERM_ORINFO set and eOperator==WO_OR
97636
 
** and the WhereTerm.u.pOrInfo field points to auxiliary information that
97637
 
** is collected about the
97638
 
**
97639
 
** If a term in the WHERE clause does not match either of the two previous
97640
 
** categories, then eOperator==0.  The WhereTerm.pExpr field is still set
97641
 
** to the original subexpression content and wtFlags is set up appropriately
97642
 
** but no other fields in the WhereTerm object are meaningful.
97643
 
**
97644
 
** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
97645
 
** but they do so indirectly.  A single WhereMaskSet structure translates
97646
 
** cursor number into bits and the translated bit is stored in the prereq
97647
 
** fields.  The translation is used in order to maximize the number of
97648
 
** bits that will fit in a Bitmask.  The VDBE cursor numbers might be
97649
 
** spread out over the non-negative integers.  For example, the cursor
97650
 
** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45.  The WhereMaskSet
97651
 
** translates these sparse cursor numbers into consecutive integers
97652
 
** beginning with 0 in order to make the best possible use of the available
97653
 
** bits in the Bitmask.  So, in the example above, the cursor numbers
97654
 
** would be mapped into integers 0 through 7.
97655
 
**
97656
 
** The number of terms in a join is limited by the number of bits
97657
 
** in prereqRight and prereqAll.  The default is 64 bits, hence SQLite
97658
 
** is only able to process joins with 64 or fewer tables.
97659
 
*/
97660
 
typedef struct WhereTerm WhereTerm;
97661
 
struct WhereTerm {
97662
 
  Expr *pExpr;            /* Pointer to the subexpression that is this term */
97663
 
  int iParent;            /* Disable pWC->a[iParent] when this term disabled */
97664
 
  int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
97665
 
  union {
97666
 
    int leftColumn;         /* Column number of X in "X <op> <expr>" */
97667
 
    WhereOrInfo *pOrInfo;   /* Extra information if eOperator==WO_OR */
97668
 
    WhereAndInfo *pAndInfo; /* Extra information if eOperator==WO_AND */
97669
 
  } u;
97670
 
  u16 eOperator;          /* A WO_xx value describing <op> */
97671
 
  u8 wtFlags;             /* TERM_xxx bit flags.  See below */
97672
 
  u8 nChild;              /* Number of children that must disable us */
97673
 
  WhereClause *pWC;       /* The clause this term is part of */
97674
 
  Bitmask prereqRight;    /* Bitmask of tables used by pExpr->pRight */
97675
 
  Bitmask prereqAll;      /* Bitmask of tables referenced by pExpr */
97676
 
};
97677
 
 
97678
 
/*
97679
 
** Allowed values of WhereTerm.wtFlags
97680
 
*/
97681
 
#define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
97682
 
#define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
97683
 
#define TERM_CODED      0x04   /* This term is already coded */
97684
 
#define TERM_COPIED     0x08   /* Has a child */
97685
 
#define TERM_ORINFO     0x10   /* Need to free the WhereTerm.u.pOrInfo object */
97686
 
#define TERM_ANDINFO    0x20   /* Need to free the WhereTerm.u.pAndInfo obj */
97687
 
#define TERM_OR_OK      0x40   /* Used during OR-clause processing */
97688
 
#ifdef SQLITE_ENABLE_STAT2
97689
 
#  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
97690
 
#else
97691
 
#  define TERM_VNULL    0x00   /* Disabled if not using stat2 */
97692
 
#endif
97693
 
 
97694
 
/*
97695
 
** An instance of the following structure holds all information about a
97696
 
** WHERE clause.  Mostly this is a container for one or more WhereTerms.
97697
 
*/
97698
 
struct WhereClause {
97699
 
  Parse *pParse;           /* The parser context */
97700
 
  WhereMaskSet *pMaskSet;  /* Mapping of table cursor numbers to bitmasks */
97701
 
  Bitmask vmask;           /* Bitmask identifying virtual table cursors */
97702
 
  u8 op;                   /* Split operator.  TK_AND or TK_OR */
97703
 
  int nTerm;               /* Number of terms */
97704
 
  int nSlot;               /* Number of entries in a[] */
97705
 
  WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
97706
 
#if defined(SQLITE_SMALL_STACK)
97707
 
  WhereTerm aStatic[1];    /* Initial static space for a[] */
97708
 
#else
97709
 
  WhereTerm aStatic[8];    /* Initial static space for a[] */
97710
 
#endif
97711
 
};
97712
 
 
97713
 
/*
97714
 
** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
97715
 
** a dynamically allocated instance of the following structure.
97716
 
*/
97717
 
struct WhereOrInfo {
97718
 
  WhereClause wc;          /* Decomposition into subterms */
97719
 
  Bitmask indexable;       /* Bitmask of all indexable tables in the clause */
97720
 
};
97721
 
 
97722
 
/*
97723
 
** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
97724
 
** a dynamically allocated instance of the following structure.
97725
 
*/
97726
 
struct WhereAndInfo {
97727
 
  WhereClause wc;          /* The subexpression broken out */
97728
 
};
97729
 
 
97730
 
/*
97731
 
** An instance of the following structure keeps track of a mapping
97732
 
** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
97733
 
**
97734
 
** The VDBE cursor numbers are small integers contained in 
97735
 
** SrcList_item.iCursor and Expr.iTable fields.  For any given WHERE 
97736
 
** clause, the cursor numbers might not begin with 0 and they might
97737
 
** contain gaps in the numbering sequence.  But we want to make maximum
97738
 
** use of the bits in our bitmasks.  This structure provides a mapping
97739
 
** from the sparse cursor numbers into consecutive integers beginning
97740
 
** with 0.
97741
 
**
97742
 
** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
97743
 
** corresponds VDBE cursor number B.  The A-th bit of a bitmask is 1<<A.
97744
 
**
97745
 
** For example, if the WHERE clause expression used these VDBE
97746
 
** cursors:  4, 5, 8, 29, 57, 73.  Then the  WhereMaskSet structure
97747
 
** would map those cursor numbers into bits 0 through 5.
97748
 
**
97749
 
** Note that the mapping is not necessarily ordered.  In the example
97750
 
** above, the mapping might go like this:  4->3, 5->1, 8->2, 29->0,
97751
 
** 57->5, 73->4.  Or one of 719 other combinations might be used. It
97752
 
** does not really matter.  What is important is that sparse cursor
97753
 
** numbers all get mapped into bit numbers that begin with 0 and contain
97754
 
** no gaps.
97755
 
*/
97756
 
struct WhereMaskSet {
97757
 
  int n;                        /* Number of assigned cursor values */
97758
 
  int ix[BMS];                  /* Cursor assigned to each bit */
97759
 
};
97760
 
 
97761
 
/*
97762
 
** A WhereCost object records a lookup strategy and the estimated
97763
 
** cost of pursuing that strategy.
97764
 
*/
97765
 
struct WhereCost {
97766
 
  WherePlan plan;    /* The lookup strategy */
97767
 
  double rCost;      /* Overall cost of pursuing this search strategy */
97768
 
  Bitmask used;      /* Bitmask of cursors used by this plan */
97769
 
};
97770
 
 
97771
 
/*
97772
 
** Bitmasks for the operators that indices are able to exploit.  An
97773
 
** OR-ed combination of these values can be used when searching for
97774
 
** terms in the where clause.
97775
 
*/
97776
 
#define WO_IN     0x001
97777
 
#define WO_EQ     0x002
97778
 
#define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
97779
 
#define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
97780
 
#define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
97781
 
#define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
97782
 
#define WO_MATCH  0x040
97783
 
#define WO_ISNULL 0x080
97784
 
#define WO_OR     0x100       /* Two or more OR-connected terms */
97785
 
#define WO_AND    0x200       /* Two or more AND-connected terms */
97786
 
#define WO_NOOP   0x800       /* This term does not restrict search space */
97787
 
 
97788
 
#define WO_ALL    0xfff       /* Mask of all possible WO_* values */
97789
 
#define WO_SINGLE 0x0ff       /* Mask of all non-compound WO_* values */
97790
 
 
97791
 
/*
97792
 
** Value for wsFlags returned by bestIndex() and stored in
97793
 
** WhereLevel.wsFlags.  These flags determine which search
97794
 
** strategies are appropriate.
97795
 
**
97796
 
** The least significant 12 bits is reserved as a mask for WO_ values above.
97797
 
** The WhereLevel.wsFlags field is usually set to WO_IN|WO_EQ|WO_ISNULL.
97798
 
** But if the table is the right table of a left join, WhereLevel.wsFlags
97799
 
** is set to WO_IN|WO_EQ.  The WhereLevel.wsFlags field can then be used as
97800
 
** the "op" parameter to findTerm when we are resolving equality constraints.
97801
 
** ISNULL constraints will then not be used on the right table of a left
97802
 
** join.  Tickets #2177 and #2189.
97803
 
*/
97804
 
#define WHERE_ROWID_EQ     0x00001000  /* rowid=EXPR or rowid IN (...) */
97805
 
#define WHERE_ROWID_RANGE  0x00002000  /* rowid<EXPR and/or rowid>EXPR */
97806
 
#define WHERE_COLUMN_EQ    0x00010000  /* x=EXPR or x IN (...) or x IS NULL */
97807
 
#define WHERE_COLUMN_RANGE 0x00020000  /* x<EXPR and/or x>EXPR */
97808
 
#define WHERE_COLUMN_IN    0x00040000  /* x IN (...) */
97809
 
#define WHERE_COLUMN_NULL  0x00080000  /* x IS NULL */
97810
 
#define WHERE_INDEXED      0x000f0000  /* Anything that uses an index */
97811
 
#define WHERE_NOT_FULLSCAN 0x100f3000  /* Does not do a full table scan */
97812
 
#define WHERE_IN_ABLE      0x000f1000  /* Able to support an IN operator */
97813
 
#define WHERE_TOP_LIMIT    0x00100000  /* x<EXPR or x<=EXPR constraint */
97814
 
#define WHERE_BTM_LIMIT    0x00200000  /* x>EXPR or x>=EXPR constraint */
97815
 
#define WHERE_BOTH_LIMIT   0x00300000  /* Both x>EXPR and x<EXPR */
97816
 
#define WHERE_IDX_ONLY     0x00800000  /* Use index only - omit table */
97817
 
#define WHERE_ORDERBY      0x01000000  /* Output will appear in correct order */
97818
 
#define WHERE_REVERSE      0x02000000  /* Scan in reverse order */
97819
 
#define WHERE_UNIQUE       0x04000000  /* Selects no more than one row */
97820
 
#define WHERE_VIRTUALTABLE 0x08000000  /* Use virtual-table processing */
97821
 
#define WHERE_MULTI_OR     0x10000000  /* OR using multiple indices */
97822
 
#define WHERE_TEMP_INDEX   0x20000000  /* Uses an ephemeral index */
97823
 
 
97824
 
/*
97825
 
** Initialize a preallocated WhereClause structure.
97826
 
*/
97827
 
static void whereClauseInit(
97828
 
  WhereClause *pWC,        /* The WhereClause to be initialized */
97829
 
  Parse *pParse,           /* The parsing context */
97830
 
  WhereMaskSet *pMaskSet   /* Mapping from table cursor numbers to bitmasks */
97831
 
){
97832
 
  pWC->pParse = pParse;
97833
 
  pWC->pMaskSet = pMaskSet;
97834
 
  pWC->nTerm = 0;
97835
 
  pWC->nSlot = ArraySize(pWC->aStatic);
97836
 
  pWC->a = pWC->aStatic;
97837
 
  pWC->vmask = 0;
97838
 
}
97839
 
 
97840
 
/* Forward reference */
97841
 
static void whereClauseClear(WhereClause*);
97842
 
 
97843
 
/*
97844
 
** Deallocate all memory associated with a WhereOrInfo object.
97845
 
*/
97846
 
static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
97847
 
  whereClauseClear(&p->wc);
97848
 
  sqlite3DbFree(db, p);
97849
 
}
97850
 
 
97851
 
/*
97852
 
** Deallocate all memory associated with a WhereAndInfo object.
97853
 
*/
97854
 
static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
97855
 
  whereClauseClear(&p->wc);
97856
 
  sqlite3DbFree(db, p);
97857
 
}
97858
 
 
97859
 
/*
97860
 
** Deallocate a WhereClause structure.  The WhereClause structure
97861
 
** itself is not freed.  This routine is the inverse of whereClauseInit().
97862
 
*/
97863
 
static void whereClauseClear(WhereClause *pWC){
97864
 
  int i;
97865
 
  WhereTerm *a;
97866
 
  sqlite3 *db = pWC->pParse->db;
97867
 
  for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
97868
 
    if( a->wtFlags & TERM_DYNAMIC ){
97869
 
      sqlite3ExprDelete(db, a->pExpr);
97870
 
    }
97871
 
    if( a->wtFlags & TERM_ORINFO ){
97872
 
      whereOrInfoDelete(db, a->u.pOrInfo);
97873
 
    }else if( a->wtFlags & TERM_ANDINFO ){
97874
 
      whereAndInfoDelete(db, a->u.pAndInfo);
97875
 
    }
97876
 
  }
97877
 
  if( pWC->a!=pWC->aStatic ){
97878
 
    sqlite3DbFree(db, pWC->a);
97879
 
  }
97880
 
}
97881
 
 
97882
 
/*
97883
 
** Add a single new WhereTerm entry to the WhereClause object pWC.
97884
 
** The new WhereTerm object is constructed from Expr p and with wtFlags.
97885
 
** The index in pWC->a[] of the new WhereTerm is returned on success.
97886
 
** 0 is returned if the new WhereTerm could not be added due to a memory
97887
 
** allocation error.  The memory allocation failure will be recorded in
97888
 
** the db->mallocFailed flag so that higher-level functions can detect it.
97889
 
**
97890
 
** This routine will increase the size of the pWC->a[] array as necessary.
97891
 
**
97892
 
** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
97893
 
** for freeing the expression p is assumed by the WhereClause object pWC.
97894
 
** This is true even if this routine fails to allocate a new WhereTerm.
97895
 
**
97896
 
** WARNING:  This routine might reallocate the space used to store
97897
 
** WhereTerms.  All pointers to WhereTerms should be invalidated after
97898
 
** calling this routine.  Such pointers may be reinitialized by referencing
97899
 
** the pWC->a[] array.
97900
 
*/
97901
 
static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
97902
 
  WhereTerm *pTerm;
97903
 
  int idx;
97904
 
  testcase( wtFlags & TERM_VIRTUAL );  /* EV: R-00211-15100 */
97905
 
  if( pWC->nTerm>=pWC->nSlot ){
97906
 
    WhereTerm *pOld = pWC->a;
97907
 
    sqlite3 *db = pWC->pParse->db;
97908
 
    pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
97909
 
    if( pWC->a==0 ){
97910
 
      if( wtFlags & TERM_DYNAMIC ){
97911
 
        sqlite3ExprDelete(db, p);
97912
 
      }
97913
 
      pWC->a = pOld;
97914
 
      return 0;
97915
 
    }
97916
 
    memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
97917
 
    if( pOld!=pWC->aStatic ){
97918
 
      sqlite3DbFree(db, pOld);
97919
 
    }
97920
 
    pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
97921
 
  }
97922
 
  pTerm = &pWC->a[idx = pWC->nTerm++];
97923
 
  pTerm->pExpr = p;
97924
 
  pTerm->wtFlags = wtFlags;
97925
 
  pTerm->pWC = pWC;
97926
 
  pTerm->iParent = -1;
97927
 
  return idx;
97928
 
}
97929
 
 
97930
 
/*
97931
 
** This routine identifies subexpressions in the WHERE clause where
97932
 
** each subexpression is separated by the AND operator or some other
97933
 
** operator specified in the op parameter.  The WhereClause structure
97934
 
** is filled with pointers to subexpressions.  For example:
97935
 
**
97936
 
**    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
97937
 
**           \________/     \_______________/     \________________/
97938
 
**            slot[0]            slot[1]               slot[2]
97939
 
**
97940
 
** The original WHERE clause in pExpr is unaltered.  All this routine
97941
 
** does is make slot[] entries point to substructure within pExpr.
97942
 
**
97943
 
** In the previous sentence and in the diagram, "slot[]" refers to
97944
 
** the WhereClause.a[] array.  The slot[] array grows as needed to contain
97945
 
** all terms of the WHERE clause.
97946
 
*/
97947
 
static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){
97948
 
  pWC->op = (u8)op;
97949
 
  if( pExpr==0 ) return;
97950
 
  if( pExpr->op!=op ){
97951
 
    whereClauseInsert(pWC, pExpr, 0);
97952
 
  }else{
97953
 
    whereSplit(pWC, pExpr->pLeft, op);
97954
 
    whereSplit(pWC, pExpr->pRight, op);
97955
 
  }
97956
 
}
97957
 
 
97958
 
/*
97959
 
** Initialize an expression mask set (a WhereMaskSet object)
97960
 
*/
97961
 
#define initMaskSet(P)  memset(P, 0, sizeof(*P))
97962
 
 
97963
 
/*
97964
 
** Return the bitmask for the given cursor number.  Return 0 if
97965
 
** iCursor is not in the set.
97966
 
*/
97967
 
static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
97968
 
  int i;
97969
 
  assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
97970
 
  for(i=0; i<pMaskSet->n; i++){
97971
 
    if( pMaskSet->ix[i]==iCursor ){
97972
 
      return ((Bitmask)1)<<i;
97973
 
    }
97974
 
  }
97975
 
  return 0;
97976
 
}
97977
 
 
97978
 
/*
97979
 
** Create a new mask for cursor iCursor.
97980
 
**
97981
 
** There is one cursor per table in the FROM clause.  The number of
97982
 
** tables in the FROM clause is limited by a test early in the
97983
 
** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
97984
 
** array will never overflow.
97985
 
*/
97986
 
static void createMask(WhereMaskSet *pMaskSet, int iCursor){
97987
 
  assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
97988
 
  pMaskSet->ix[pMaskSet->n++] = iCursor;
97989
 
}
97990
 
 
97991
 
/*
97992
 
** This routine walks (recursively) an expression tree and generates
97993
 
** a bitmask indicating which tables are used in that expression
97994
 
** tree.
97995
 
**
97996
 
** In order for this routine to work, the calling function must have
97997
 
** previously invoked sqlite3ResolveExprNames() on the expression.  See
97998
 
** the header comment on that routine for additional information.
97999
 
** The sqlite3ResolveExprNames() routines looks for column names and
98000
 
** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
98001
 
** the VDBE cursor number of the table.  This routine just has to
98002
 
** translate the cursor numbers into bitmask values and OR all
98003
 
** the bitmasks together.
98004
 
*/
98005
 
static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
98006
 
static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
98007
 
static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
98008
 
  Bitmask mask = 0;
98009
 
  if( p==0 ) return 0;
98010
 
  if( p->op==TK_COLUMN ){
98011
 
    mask = getMask(pMaskSet, p->iTable);
98012
 
    return mask;
98013
 
  }
98014
 
  mask = exprTableUsage(pMaskSet, p->pRight);
98015
 
  mask |= exprTableUsage(pMaskSet, p->pLeft);
98016
 
  if( ExprHasProperty(p, EP_xIsSelect) ){
98017
 
    mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
98018
 
  }else{
98019
 
    mask |= exprListTableUsage(pMaskSet, p->x.pList);
98020
 
  }
98021
 
  return mask;
98022
 
}
98023
 
static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
98024
 
  int i;
98025
 
  Bitmask mask = 0;
98026
 
  if( pList ){
98027
 
    for(i=0; i<pList->nExpr; i++){
98028
 
      mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
98029
 
    }
98030
 
  }
98031
 
  return mask;
98032
 
}
98033
 
static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
98034
 
  Bitmask mask = 0;
98035
 
  while( pS ){
98036
 
    mask |= exprListTableUsage(pMaskSet, pS->pEList);
98037
 
    mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
98038
 
    mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
98039
 
    mask |= exprTableUsage(pMaskSet, pS->pWhere);
98040
 
    mask |= exprTableUsage(pMaskSet, pS->pHaving);
98041
 
    pS = pS->pPrior;
98042
 
  }
98043
 
  return mask;
98044
 
}
98045
 
 
98046
 
/*
98047
 
** Return TRUE if the given operator is one of the operators that is
98048
 
** allowed for an indexable WHERE clause term.  The allowed operators are
98049
 
** "=", "<", ">", "<=", ">=", and "IN".
98050
 
**
98051
 
** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be
98052
 
** of one of the following forms: column = expression column > expression
98053
 
** column >= expression column < expression column <= expression
98054
 
** expression = column expression > column expression >= column
98055
 
** expression < column expression <= column column IN
98056
 
** (expression-list) column IN (subquery) column IS NULL
98057
 
*/
98058
 
static int allowedOp(int op){
98059
 
  assert( TK_GT>TK_EQ && TK_GT<TK_GE );
98060
 
  assert( TK_LT>TK_EQ && TK_LT<TK_GE );
98061
 
  assert( TK_LE>TK_EQ && TK_LE<TK_GE );
98062
 
  assert( TK_GE==TK_EQ+4 );
98063
 
  return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
98064
 
}
98065
 
 
98066
 
/*
98067
 
** Swap two objects of type TYPE.
98068
 
*/
98069
 
#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
98070
 
 
98071
 
/*
98072
 
** Commute a comparison operator.  Expressions of the form "X op Y"
98073
 
** are converted into "Y op X".
98074
 
**
98075
 
** If a collation sequence is associated with either the left or right
98076
 
** side of the comparison, it remains associated with the same side after
98077
 
** the commutation. So "Y collate NOCASE op X" becomes 
98078
 
** "X collate NOCASE op Y". This is because any collation sequence on
98079
 
** the left hand side of a comparison overrides any collation sequence 
98080
 
** attached to the right. For the same reason the EP_ExpCollate flag
98081
 
** is not commuted.
98082
 
*/
98083
 
static void exprCommute(Parse *pParse, Expr *pExpr){
98084
 
  u16 expRight = (pExpr->pRight->flags & EP_ExpCollate);
98085
 
  u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate);
98086
 
  assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
98087
 
  pExpr->pRight->pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
98088
 
  pExpr->pLeft->pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
98089
 
  SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
98090
 
  pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft;
98091
 
  pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight;
98092
 
  SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
98093
 
  if( pExpr->op>=TK_GT ){
98094
 
    assert( TK_LT==TK_GT+2 );
98095
 
    assert( TK_GE==TK_LE+2 );
98096
 
    assert( TK_GT>TK_EQ );
98097
 
    assert( TK_GT<TK_LE );
98098
 
    assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
98099
 
    pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
98100
 
  }
98101
 
}
98102
 
 
98103
 
/*
98104
 
** Translate from TK_xx operator to WO_xx bitmask.
98105
 
*/
98106
 
static u16 operatorMask(int op){
98107
 
  u16 c;
98108
 
  assert( allowedOp(op) );
98109
 
  if( op==TK_IN ){
98110
 
    c = WO_IN;
98111
 
  }else if( op==TK_ISNULL ){
98112
 
    c = WO_ISNULL;
98113
 
  }else{
98114
 
    assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
98115
 
    c = (u16)(WO_EQ<<(op-TK_EQ));
98116
 
  }
98117
 
  assert( op!=TK_ISNULL || c==WO_ISNULL );
98118
 
  assert( op!=TK_IN || c==WO_IN );
98119
 
  assert( op!=TK_EQ || c==WO_EQ );
98120
 
  assert( op!=TK_LT || c==WO_LT );
98121
 
  assert( op!=TK_LE || c==WO_LE );
98122
 
  assert( op!=TK_GT || c==WO_GT );
98123
 
  assert( op!=TK_GE || c==WO_GE );
98124
 
  return c;
98125
 
}
98126
 
 
98127
 
/*
98128
 
** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
98129
 
** where X is a reference to the iColumn of table iCur and <op> is one of
98130
 
** the WO_xx operator codes specified by the op parameter.
98131
 
** Return a pointer to the term.  Return 0 if not found.
98132
 
*/
98133
 
static WhereTerm *findTerm(
98134
 
  WhereClause *pWC,     /* The WHERE clause to be searched */
98135
 
  int iCur,             /* Cursor number of LHS */
98136
 
  int iColumn,          /* Column number of LHS */
98137
 
  Bitmask notReady,     /* RHS must not overlap with this mask */
98138
 
  u32 op,               /* Mask of WO_xx values describing operator */
98139
 
  Index *pIdx           /* Must be compatible with this index, if not NULL */
98140
 
){
98141
 
  WhereTerm *pTerm;
98142
 
  int k;
98143
 
  assert( iCur>=0 );
98144
 
  op &= WO_ALL;
98145
 
  for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
98146
 
    if( pTerm->leftCursor==iCur
98147
 
       && (pTerm->prereqRight & notReady)==0
98148
 
       && pTerm->u.leftColumn==iColumn
98149
 
       && (pTerm->eOperator & op)!=0
98150
 
    ){
98151
 
      if( pIdx && pTerm->eOperator!=WO_ISNULL ){
98152
 
        Expr *pX = pTerm->pExpr;
98153
 
        CollSeq *pColl;
98154
 
        char idxaff;
98155
 
        int j;
98156
 
        Parse *pParse = pWC->pParse;
98157
 
 
98158
 
        idxaff = pIdx->pTable->aCol[iColumn].affinity;
98159
 
        if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
98160
 
 
98161
 
        /* Figure out the collation sequence required from an index for
98162
 
        ** it to be useful for optimising expression pX. Store this
98163
 
        ** value in variable pColl.
98164
 
        */
98165
 
        assert(pX->pLeft);
98166
 
        pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
98167
 
        assert(pColl || pParse->nErr);
98168
 
 
98169
 
        for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
98170
 
          if( NEVER(j>=pIdx->nColumn) ) return 0;
98171
 
        }
98172
 
        if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
98173
 
      }
98174
 
      return pTerm;
98175
 
    }
98176
 
  }
98177
 
  return 0;
98178
 
}
98179
 
 
98180
 
/* Forward reference */
98181
 
static void exprAnalyze(SrcList*, WhereClause*, int);
98182
 
 
98183
 
/*
98184
 
** Call exprAnalyze on all terms in a WHERE clause.  
98185
 
**
98186
 
**
98187
 
*/
98188
 
static void exprAnalyzeAll(
98189
 
  SrcList *pTabList,       /* the FROM clause */
98190
 
  WhereClause *pWC         /* the WHERE clause to be analyzed */
98191
 
){
98192
 
  int i;
98193
 
  for(i=pWC->nTerm-1; i>=0; i--){
98194
 
    exprAnalyze(pTabList, pWC, i);
98195
 
  }
98196
 
}
98197
 
 
98198
 
#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
98199
 
/*
98200
 
** Check to see if the given expression is a LIKE or GLOB operator that
98201
 
** can be optimized using inequality constraints.  Return TRUE if it is
98202
 
** so and false if not.
98203
 
**
98204
 
** In order for the operator to be optimizible, the RHS must be a string
98205
 
** literal that does not begin with a wildcard.  
98206
 
*/
98207
 
static int isLikeOrGlob(
98208
 
  Parse *pParse,    /* Parsing and code generating context */
98209
 
  Expr *pExpr,      /* Test this expression */
98210
 
  Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
98211
 
  int *pisComplete, /* True if the only wildcard is % in the last character */
98212
 
  int *pnoCase      /* True if uppercase is equivalent to lowercase */
98213
 
){
98214
 
  const char *z = 0;         /* String on RHS of LIKE operator */
98215
 
  Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
98216
 
  ExprList *pList;           /* List of operands to the LIKE operator */
98217
 
  int c;                     /* One character in z[] */
98218
 
  int cnt;                   /* Number of non-wildcard prefix characters */
98219
 
  char wc[3];                /* Wildcard characters */
98220
 
  sqlite3 *db = pParse->db;  /* Database connection */
98221
 
  sqlite3_value *pVal = 0;
98222
 
  int op;                    /* Opcode of pRight */
98223
 
 
98224
 
  if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
98225
 
    return 0;
98226
 
  }
98227
 
#ifdef SQLITE_EBCDIC
98228
 
  if( *pnoCase ) return 0;
98229
 
#endif
98230
 
  pList = pExpr->x.pList;
98231
 
  pLeft = pList->a[1].pExpr;
98232
 
  if( pLeft->op!=TK_COLUMN || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT ){
98233
 
    /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
98234
 
    ** be the name of an indexed column with TEXT affinity. */
98235
 
    return 0;
98236
 
  }
98237
 
  assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
98238
 
 
98239
 
  pRight = pList->a[0].pExpr;
98240
 
  op = pRight->op;
98241
 
  if( op==TK_REGISTER ){
98242
 
    op = pRight->op2;
98243
 
  }
98244
 
  if( op==TK_VARIABLE ){
98245
 
    Vdbe *pReprepare = pParse->pReprepare;
98246
 
    int iCol = pRight->iColumn;
98247
 
    pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE);
98248
 
    if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
98249
 
      z = (char *)sqlite3_value_text(pVal);
98250
 
    }
98251
 
    sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-23257-02778 */
98252
 
    assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
98253
 
  }else if( op==TK_STRING ){
98254
 
    z = pRight->u.zToken;
98255
 
  }
98256
 
  if( z ){
98257
 
    cnt = 0;
98258
 
    while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
98259
 
      cnt++;
98260
 
    }
98261
 
    if( cnt!=0 && 255!=(u8)z[cnt-1] ){
98262
 
      Expr *pPrefix;
98263
 
      *pisComplete = c==wc[0] && z[cnt+1]==0;
98264
 
      pPrefix = sqlite3Expr(db, TK_STRING, z);
98265
 
      if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
98266
 
      *ppPrefix = pPrefix;
98267
 
      if( op==TK_VARIABLE ){
98268
 
        Vdbe *v = pParse->pVdbe;
98269
 
        sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-23257-02778 */
98270
 
        if( *pisComplete && pRight->u.zToken[1] ){
98271
 
          /* If the rhs of the LIKE expression is a variable, and the current
98272
 
          ** value of the variable means there is no need to invoke the LIKE
98273
 
          ** function, then no OP_Variable will be added to the program.
98274
 
          ** This causes problems for the sqlite3_bind_parameter_name()
98275
 
          ** API. To workaround them, add a dummy OP_Variable here.
98276
 
          */ 
98277
 
          int r1 = sqlite3GetTempReg(pParse);
98278
 
          sqlite3ExprCodeTarget(pParse, pRight, r1);
98279
 
          sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
98280
 
          sqlite3ReleaseTempReg(pParse, r1);
98281
 
        }
98282
 
      }
98283
 
    }else{
98284
 
      z = 0;
98285
 
    }
98286
 
  }
98287
 
 
98288
 
  sqlite3ValueFree(pVal);
98289
 
  return (z!=0);
98290
 
}
98291
 
#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
98292
 
 
98293
 
 
98294
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
98295
 
/*
98296
 
** Check to see if the given expression is of the form
98297
 
**
98298
 
**         column MATCH expr
98299
 
**
98300
 
** If it is then return TRUE.  If not, return FALSE.
98301
 
*/
98302
 
static int isMatchOfColumn(
98303
 
  Expr *pExpr      /* Test this expression */
98304
 
){
98305
 
  ExprList *pList;
98306
 
 
98307
 
  if( pExpr->op!=TK_FUNCTION ){
98308
 
    return 0;
98309
 
  }
98310
 
  if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
98311
 
    return 0;
98312
 
  }
98313
 
  pList = pExpr->x.pList;
98314
 
  if( pList->nExpr!=2 ){
98315
 
    return 0;
98316
 
  }
98317
 
  if( pList->a[1].pExpr->op != TK_COLUMN ){
98318
 
    return 0;
98319
 
  }
98320
 
  return 1;
98321
 
}
98322
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
98323
 
 
98324
 
/*
98325
 
** If the pBase expression originated in the ON or USING clause of
98326
 
** a join, then transfer the appropriate markings over to derived.
98327
 
*/
98328
 
static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
98329
 
  pDerived->flags |= pBase->flags & EP_FromJoin;
98330
 
  pDerived->iRightJoinTable = pBase->iRightJoinTable;
98331
 
}
98332
 
 
98333
 
#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
98334
 
/*
98335
 
** Analyze a term that consists of two or more OR-connected
98336
 
** subterms.  So in:
98337
 
**
98338
 
**     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
98339
 
**                          ^^^^^^^^^^^^^^^^^^^^
98340
 
**
98341
 
** This routine analyzes terms such as the middle term in the above example.
98342
 
** A WhereOrTerm object is computed and attached to the term under
98343
 
** analysis, regardless of the outcome of the analysis.  Hence:
98344
 
**
98345
 
**     WhereTerm.wtFlags   |=  TERM_ORINFO
98346
 
**     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
98347
 
**
98348
 
** The term being analyzed must have two or more of OR-connected subterms.
98349
 
** A single subterm might be a set of AND-connected sub-subterms.
98350
 
** Examples of terms under analysis:
98351
 
**
98352
 
**     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
98353
 
**     (B)     x=expr1 OR expr2=x OR x=expr3
98354
 
**     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
98355
 
**     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
98356
 
**     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
98357
 
**
98358
 
** CASE 1:
98359
 
**
98360
 
** If all subterms are of the form T.C=expr for some single column of C
98361
 
** a single table T (as shown in example B above) then create a new virtual
98362
 
** term that is an equivalent IN expression.  In other words, if the term
98363
 
** being analyzed is:
98364
 
**
98365
 
**      x = expr1  OR  expr2 = x  OR  x = expr3
98366
 
**
98367
 
** then create a new virtual term like this:
98368
 
**
98369
 
**      x IN (expr1,expr2,expr3)
98370
 
**
98371
 
** CASE 2:
98372
 
**
98373
 
** If all subterms are indexable by a single table T, then set
98374
 
**
98375
 
**     WhereTerm.eOperator              =  WO_OR
98376
 
**     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
98377
 
**
98378
 
** A subterm is "indexable" if it is of the form
98379
 
** "T.C <op> <expr>" where C is any column of table T and 
98380
 
** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
98381
 
** A subterm is also indexable if it is an AND of two or more
98382
 
** subsubterms at least one of which is indexable.  Indexable AND 
98383
 
** subterms have their eOperator set to WO_AND and they have
98384
 
** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
98385
 
**
98386
 
** From another point of view, "indexable" means that the subterm could
98387
 
** potentially be used with an index if an appropriate index exists.
98388
 
** This analysis does not consider whether or not the index exists; that
98389
 
** is something the bestIndex() routine will determine.  This analysis
98390
 
** only looks at whether subterms appropriate for indexing exist.
98391
 
**
98392
 
** All examples A through E above all satisfy case 2.  But if a term
98393
 
** also statisfies case 1 (such as B) we know that the optimizer will
98394
 
** always prefer case 1, so in that case we pretend that case 2 is not
98395
 
** satisfied.
98396
 
**
98397
 
** It might be the case that multiple tables are indexable.  For example,
98398
 
** (E) above is indexable on tables P, Q, and R.
98399
 
**
98400
 
** Terms that satisfy case 2 are candidates for lookup by using
98401
 
** separate indices to find rowids for each subterm and composing
98402
 
** the union of all rowids using a RowSet object.  This is similar
98403
 
** to "bitmap indices" in other database engines.
98404
 
**
98405
 
** OTHERWISE:
98406
 
**
98407
 
** If neither case 1 nor case 2 apply, then leave the eOperator set to
98408
 
** zero.  This term is not useful for search.
98409
 
*/
98410
 
static void exprAnalyzeOrTerm(
98411
 
  SrcList *pSrc,            /* the FROM clause */
98412
 
  WhereClause *pWC,         /* the complete WHERE clause */
98413
 
  int idxTerm               /* Index of the OR-term to be analyzed */
98414
 
){
98415
 
  Parse *pParse = pWC->pParse;            /* Parser context */
98416
 
  sqlite3 *db = pParse->db;               /* Database connection */
98417
 
  WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
98418
 
  Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
98419
 
  WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */
98420
 
  int i;                                  /* Loop counters */
98421
 
  WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
98422
 
  WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
98423
 
  WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
98424
 
  Bitmask chngToIN;         /* Tables that might satisfy case 1 */
98425
 
  Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
98426
 
 
98427
 
  /*
98428
 
  ** Break the OR clause into its separate subterms.  The subterms are
98429
 
  ** stored in a WhereClause structure containing within the WhereOrInfo
98430
 
  ** object that is attached to the original OR clause term.
98431
 
  */
98432
 
  assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
98433
 
  assert( pExpr->op==TK_OR );
98434
 
  pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
98435
 
  if( pOrInfo==0 ) return;
98436
 
  pTerm->wtFlags |= TERM_ORINFO;
98437
 
  pOrWc = &pOrInfo->wc;
98438
 
  whereClauseInit(pOrWc, pWC->pParse, pMaskSet);
98439
 
  whereSplit(pOrWc, pExpr, TK_OR);
98440
 
  exprAnalyzeAll(pSrc, pOrWc);
98441
 
  if( db->mallocFailed ) return;
98442
 
  assert( pOrWc->nTerm>=2 );
98443
 
 
98444
 
  /*
98445
 
  ** Compute the set of tables that might satisfy cases 1 or 2.
98446
 
  */
98447
 
  indexable = ~(Bitmask)0;
98448
 
  chngToIN = ~(pWC->vmask);
98449
 
  for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
98450
 
    if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
98451
 
      WhereAndInfo *pAndInfo;
98452
 
      assert( pOrTerm->eOperator==0 );
98453
 
      assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
98454
 
      chngToIN = 0;
98455
 
      pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
98456
 
      if( pAndInfo ){
98457
 
        WhereClause *pAndWC;
98458
 
        WhereTerm *pAndTerm;
98459
 
        int j;
98460
 
        Bitmask b = 0;
98461
 
        pOrTerm->u.pAndInfo = pAndInfo;
98462
 
        pOrTerm->wtFlags |= TERM_ANDINFO;
98463
 
        pOrTerm->eOperator = WO_AND;
98464
 
        pAndWC = &pAndInfo->wc;
98465
 
        whereClauseInit(pAndWC, pWC->pParse, pMaskSet);
98466
 
        whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
98467
 
        exprAnalyzeAll(pSrc, pAndWC);
98468
 
        testcase( db->mallocFailed );
98469
 
        if( !db->mallocFailed ){
98470
 
          for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
98471
 
            assert( pAndTerm->pExpr );
98472
 
            if( allowedOp(pAndTerm->pExpr->op) ){
98473
 
              b |= getMask(pMaskSet, pAndTerm->leftCursor);
98474
 
            }
98475
 
          }
98476
 
        }
98477
 
        indexable &= b;
98478
 
      }
98479
 
    }else if( pOrTerm->wtFlags & TERM_COPIED ){
98480
 
      /* Skip this term for now.  We revisit it when we process the
98481
 
      ** corresponding TERM_VIRTUAL term */
98482
 
    }else{
98483
 
      Bitmask b;
98484
 
      b = getMask(pMaskSet, pOrTerm->leftCursor);
98485
 
      if( pOrTerm->wtFlags & TERM_VIRTUAL ){
98486
 
        WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
98487
 
        b |= getMask(pMaskSet, pOther->leftCursor);
98488
 
      }
98489
 
      indexable &= b;
98490
 
      if( pOrTerm->eOperator!=WO_EQ ){
98491
 
        chngToIN = 0;
98492
 
      }else{
98493
 
        chngToIN &= b;
98494
 
      }
98495
 
    }
98496
 
  }
98497
 
 
98498
 
  /*
98499
 
  ** Record the set of tables that satisfy case 2.  The set might be
98500
 
  ** empty.
98501
 
  */
98502
 
  pOrInfo->indexable = indexable;
98503
 
  pTerm->eOperator = indexable==0 ? 0 : WO_OR;
98504
 
 
98505
 
  /*
98506
 
  ** chngToIN holds a set of tables that *might* satisfy case 1.  But
98507
 
  ** we have to do some additional checking to see if case 1 really
98508
 
  ** is satisfied.
98509
 
  **
98510
 
  ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
98511
 
  ** that there is no possibility of transforming the OR clause into an
98512
 
  ** IN operator because one or more terms in the OR clause contain
98513
 
  ** something other than == on a column in the single table.  The 1-bit
98514
 
  ** case means that every term of the OR clause is of the form
98515
 
  ** "table.column=expr" for some single table.  The one bit that is set
98516
 
  ** will correspond to the common table.  We still need to check to make
98517
 
  ** sure the same column is used on all terms.  The 2-bit case is when
98518
 
  ** the all terms are of the form "table1.column=table2.column".  It
98519
 
  ** might be possible to form an IN operator with either table1.column
98520
 
  ** or table2.column as the LHS if either is common to every term of
98521
 
  ** the OR clause.
98522
 
  **
98523
 
  ** Note that terms of the form "table.column1=table.column2" (the
98524
 
  ** same table on both sizes of the ==) cannot be optimized.
98525
 
  */
98526
 
  if( chngToIN ){
98527
 
    int okToChngToIN = 0;     /* True if the conversion to IN is valid */
98528
 
    int iColumn = -1;         /* Column index on lhs of IN operator */
98529
 
    int iCursor = -1;         /* Table cursor common to all terms */
98530
 
    int j = 0;                /* Loop counter */
98531
 
 
98532
 
    /* Search for a table and column that appears on one side or the
98533
 
    ** other of the == operator in every subterm.  That table and column
98534
 
    ** will be recorded in iCursor and iColumn.  There might not be any
98535
 
    ** such table and column.  Set okToChngToIN if an appropriate table
98536
 
    ** and column is found but leave okToChngToIN false if not found.
98537
 
    */
98538
 
    for(j=0; j<2 && !okToChngToIN; j++){
98539
 
      pOrTerm = pOrWc->a;
98540
 
      for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
98541
 
        assert( pOrTerm->eOperator==WO_EQ );
98542
 
        pOrTerm->wtFlags &= ~TERM_OR_OK;
98543
 
        if( pOrTerm->leftCursor==iCursor ){
98544
 
          /* This is the 2-bit case and we are on the second iteration and
98545
 
          ** current term is from the first iteration.  So skip this term. */
98546
 
          assert( j==1 );
98547
 
          continue;
98548
 
        }
98549
 
        if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){
98550
 
          /* This term must be of the form t1.a==t2.b where t2 is in the
98551
 
          ** chngToIN set but t1 is not.  This term will be either preceeded
98552
 
          ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
98553
 
          ** and use its inversion. */
98554
 
          testcase( pOrTerm->wtFlags & TERM_COPIED );
98555
 
          testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
98556
 
          assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
98557
 
          continue;
98558
 
        }
98559
 
        iColumn = pOrTerm->u.leftColumn;
98560
 
        iCursor = pOrTerm->leftCursor;
98561
 
        break;
98562
 
      }
98563
 
      if( i<0 ){
98564
 
        /* No candidate table+column was found.  This can only occur
98565
 
        ** on the second iteration */
98566
 
        assert( j==1 );
98567
 
        assert( (chngToIN&(chngToIN-1))==0 );
98568
 
        assert( chngToIN==getMask(pMaskSet, iCursor) );
98569
 
        break;
98570
 
      }
98571
 
      testcase( j==1 );
98572
 
 
98573
 
      /* We have found a candidate table and column.  Check to see if that
98574
 
      ** table and column is common to every term in the OR clause */
98575
 
      okToChngToIN = 1;
98576
 
      for(; i>=0 && okToChngToIN; i--, pOrTerm++){
98577
 
        assert( pOrTerm->eOperator==WO_EQ );
98578
 
        if( pOrTerm->leftCursor!=iCursor ){
98579
 
          pOrTerm->wtFlags &= ~TERM_OR_OK;
98580
 
        }else if( pOrTerm->u.leftColumn!=iColumn ){
98581
 
          okToChngToIN = 0;
98582
 
        }else{
98583
 
          int affLeft, affRight;
98584
 
          /* If the right-hand side is also a column, then the affinities
98585
 
          ** of both right and left sides must be such that no type
98586
 
          ** conversions are required on the right.  (Ticket #2249)
98587
 
          */
98588
 
          affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
98589
 
          affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
98590
 
          if( affRight!=0 && affRight!=affLeft ){
98591
 
            okToChngToIN = 0;
98592
 
          }else{
98593
 
            pOrTerm->wtFlags |= TERM_OR_OK;
98594
 
          }
98595
 
        }
98596
 
      }
98597
 
    }
98598
 
 
98599
 
    /* At this point, okToChngToIN is true if original pTerm satisfies
98600
 
    ** case 1.  In that case, construct a new virtual term that is 
98601
 
    ** pTerm converted into an IN operator.
98602
 
    **
98603
 
    ** EV: R-00211-15100
98604
 
    */
98605
 
    if( okToChngToIN ){
98606
 
      Expr *pDup;            /* A transient duplicate expression */
98607
 
      ExprList *pList = 0;   /* The RHS of the IN operator */
98608
 
      Expr *pLeft = 0;       /* The LHS of the IN operator */
98609
 
      Expr *pNew;            /* The complete IN operator */
98610
 
 
98611
 
      for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
98612
 
        if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
98613
 
        assert( pOrTerm->eOperator==WO_EQ );
98614
 
        assert( pOrTerm->leftCursor==iCursor );
98615
 
        assert( pOrTerm->u.leftColumn==iColumn );
98616
 
        pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
98617
 
        pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup);
98618
 
        pLeft = pOrTerm->pExpr->pLeft;
98619
 
      }
98620
 
      assert( pLeft!=0 );
98621
 
      pDup = sqlite3ExprDup(db, pLeft, 0);
98622
 
      pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
98623
 
      if( pNew ){
98624
 
        int idxNew;
98625
 
        transferJoinMarkings(pNew, pExpr);
98626
 
        assert( !ExprHasProperty(pNew, EP_xIsSelect) );
98627
 
        pNew->x.pList = pList;
98628
 
        idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
98629
 
        testcase( idxNew==0 );
98630
 
        exprAnalyze(pSrc, pWC, idxNew);
98631
 
        pTerm = &pWC->a[idxTerm];
98632
 
        pWC->a[idxNew].iParent = idxTerm;
98633
 
        pTerm->nChild = 1;
98634
 
      }else{
98635
 
        sqlite3ExprListDelete(db, pList);
98636
 
      }
98637
 
      pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 2 */
98638
 
    }
98639
 
  }
98640
 
}
98641
 
#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
98642
 
 
98643
 
 
98644
 
/*
98645
 
** The input to this routine is an WhereTerm structure with only the
98646
 
** "pExpr" field filled in.  The job of this routine is to analyze the
98647
 
** subexpression and populate all the other fields of the WhereTerm
98648
 
** structure.
98649
 
**
98650
 
** If the expression is of the form "<expr> <op> X" it gets commuted
98651
 
** to the standard form of "X <op> <expr>".
98652
 
**
98653
 
** If the expression is of the form "X <op> Y" where both X and Y are
98654
 
** columns, then the original expression is unchanged and a new virtual
98655
 
** term of the form "Y <op> X" is added to the WHERE clause and
98656
 
** analyzed separately.  The original term is marked with TERM_COPIED
98657
 
** and the new term is marked with TERM_DYNAMIC (because it's pExpr
98658
 
** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
98659
 
** is a commuted copy of a prior term.)  The original term has nChild=1
98660
 
** and the copy has idxParent set to the index of the original term.
98661
 
*/
98662
 
static void exprAnalyze(
98663
 
  SrcList *pSrc,            /* the FROM clause */
98664
 
  WhereClause *pWC,         /* the WHERE clause */
98665
 
  int idxTerm               /* Index of the term to be analyzed */
98666
 
){
98667
 
  WhereTerm *pTerm;                /* The term to be analyzed */
98668
 
  WhereMaskSet *pMaskSet;          /* Set of table index masks */
98669
 
  Expr *pExpr;                     /* The expression to be analyzed */
98670
 
  Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
98671
 
  Bitmask prereqAll;               /* Prerequesites of pExpr */
98672
 
  Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
98673
 
  Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
98674
 
  int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
98675
 
  int noCase = 0;                  /* LIKE/GLOB distinguishes case */
98676
 
  int op;                          /* Top-level operator.  pExpr->op */
98677
 
  Parse *pParse = pWC->pParse;     /* Parsing context */
98678
 
  sqlite3 *db = pParse->db;        /* Database connection */
98679
 
 
98680
 
  if( db->mallocFailed ){
98681
 
    return;
98682
 
  }
98683
 
  pTerm = &pWC->a[idxTerm];
98684
 
  pMaskSet = pWC->pMaskSet;
98685
 
  pExpr = pTerm->pExpr;
98686
 
  prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
98687
 
  op = pExpr->op;
98688
 
  if( op==TK_IN ){
98689
 
    assert( pExpr->pRight==0 );
98690
 
    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
98691
 
      pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
98692
 
    }else{
98693
 
      pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
98694
 
    }
98695
 
  }else if( op==TK_ISNULL ){
98696
 
    pTerm->prereqRight = 0;
98697
 
  }else{
98698
 
    pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
98699
 
  }
98700
 
  prereqAll = exprTableUsage(pMaskSet, pExpr);
98701
 
  if( ExprHasProperty(pExpr, EP_FromJoin) ){
98702
 
    Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
98703
 
    prereqAll |= x;
98704
 
    extraRight = x-1;  /* ON clause terms may not be used with an index
98705
 
                       ** on left table of a LEFT JOIN.  Ticket #3015 */
98706
 
  }
98707
 
  pTerm->prereqAll = prereqAll;
98708
 
  pTerm->leftCursor = -1;
98709
 
  pTerm->iParent = -1;
98710
 
  pTerm->eOperator = 0;
98711
 
  if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){
98712
 
    Expr *pLeft = pExpr->pLeft;
98713
 
    Expr *pRight = pExpr->pRight;
98714
 
    if( pLeft->op==TK_COLUMN ){
98715
 
      pTerm->leftCursor = pLeft->iTable;
98716
 
      pTerm->u.leftColumn = pLeft->iColumn;
98717
 
      pTerm->eOperator = operatorMask(op);
98718
 
    }
98719
 
    if( pRight && pRight->op==TK_COLUMN ){
98720
 
      WhereTerm *pNew;
98721
 
      Expr *pDup;
98722
 
      if( pTerm->leftCursor>=0 ){
98723
 
        int idxNew;
98724
 
        pDup = sqlite3ExprDup(db, pExpr, 0);
98725
 
        if( db->mallocFailed ){
98726
 
          sqlite3ExprDelete(db, pDup);
98727
 
          return;
98728
 
        }
98729
 
        idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
98730
 
        if( idxNew==0 ) return;
98731
 
        pNew = &pWC->a[idxNew];
98732
 
        pNew->iParent = idxTerm;
98733
 
        pTerm = &pWC->a[idxTerm];
98734
 
        pTerm->nChild = 1;
98735
 
        pTerm->wtFlags |= TERM_COPIED;
98736
 
      }else{
98737
 
        pDup = pExpr;
98738
 
        pNew = pTerm;
98739
 
      }
98740
 
      exprCommute(pParse, pDup);
98741
 
      pLeft = pDup->pLeft;
98742
 
      pNew->leftCursor = pLeft->iTable;
98743
 
      pNew->u.leftColumn = pLeft->iColumn;
98744
 
      testcase( (prereqLeft | extraRight) != prereqLeft );
98745
 
      pNew->prereqRight = prereqLeft | extraRight;
98746
 
      pNew->prereqAll = prereqAll;
98747
 
      pNew->eOperator = operatorMask(pDup->op);
98748
 
    }
98749
 
  }
98750
 
 
98751
 
#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
98752
 
  /* If a term is the BETWEEN operator, create two new virtual terms
98753
 
  ** that define the range that the BETWEEN implements.  For example:
98754
 
  **
98755
 
  **      a BETWEEN b AND c
98756
 
  **
98757
 
  ** is converted into:
98758
 
  **
98759
 
  **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
98760
 
  **
98761
 
  ** The two new terms are added onto the end of the WhereClause object.
98762
 
  ** The new terms are "dynamic" and are children of the original BETWEEN
98763
 
  ** term.  That means that if the BETWEEN term is coded, the children are
98764
 
  ** skipped.  Or, if the children are satisfied by an index, the original
98765
 
  ** BETWEEN term is skipped.
98766
 
  */
98767
 
  else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
98768
 
    ExprList *pList = pExpr->x.pList;
98769
 
    int i;
98770
 
    static const u8 ops[] = {TK_GE, TK_LE};
98771
 
    assert( pList!=0 );
98772
 
    assert( pList->nExpr==2 );
98773
 
    for(i=0; i<2; i++){
98774
 
      Expr *pNewExpr;
98775
 
      int idxNew;
98776
 
      pNewExpr = sqlite3PExpr(pParse, ops[i], 
98777
 
                             sqlite3ExprDup(db, pExpr->pLeft, 0),
98778
 
                             sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
98779
 
      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
98780
 
      testcase( idxNew==0 );
98781
 
      exprAnalyze(pSrc, pWC, idxNew);
98782
 
      pTerm = &pWC->a[idxTerm];
98783
 
      pWC->a[idxNew].iParent = idxTerm;
98784
 
    }
98785
 
    pTerm->nChild = 2;
98786
 
  }
98787
 
#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
98788
 
 
98789
 
#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
98790
 
  /* Analyze a term that is composed of two or more subterms connected by
98791
 
  ** an OR operator.
98792
 
  */
98793
 
  else if( pExpr->op==TK_OR ){
98794
 
    assert( pWC->op==TK_AND );
98795
 
    exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
98796
 
    pTerm = &pWC->a[idxTerm];
98797
 
  }
98798
 
#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
98799
 
 
98800
 
#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
98801
 
  /* Add constraints to reduce the search space on a LIKE or GLOB
98802
 
  ** operator.
98803
 
  **
98804
 
  ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints
98805
 
  **
98806
 
  **          x>='abc' AND x<'abd' AND x LIKE 'abc%'
98807
 
  **
98808
 
  ** The last character of the prefix "abc" is incremented to form the
98809
 
  ** termination condition "abd".
98810
 
  */
98811
 
  if( pWC->op==TK_AND 
98812
 
   && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
98813
 
  ){
98814
 
    Expr *pLeft;       /* LHS of LIKE/GLOB operator */
98815
 
    Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
98816
 
    Expr *pNewExpr1;
98817
 
    Expr *pNewExpr2;
98818
 
    int idxNew1;
98819
 
    int idxNew2;
98820
 
    CollSeq *pColl;    /* Collating sequence to use */
98821
 
 
98822
 
    pLeft = pExpr->x.pList->a[1].pExpr;
98823
 
    pStr2 = sqlite3ExprDup(db, pStr1, 0);
98824
 
    if( !db->mallocFailed ){
98825
 
      u8 c, *pC;       /* Last character before the first wildcard */
98826
 
      pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
98827
 
      c = *pC;
98828
 
      if( noCase ){
98829
 
        /* The point is to increment the last character before the first
98830
 
        ** wildcard.  But if we increment '@', that will push it into the
98831
 
        ** alphabetic range where case conversions will mess up the 
98832
 
        ** inequality.  To avoid this, make sure to also run the full
98833
 
        ** LIKE on all candidate expressions by clearing the isComplete flag
98834
 
        */
98835
 
        if( c=='A'-1 ) isComplete = 0;   /* EV: R-64339-08207 */
98836
 
 
98837
 
 
98838
 
        c = sqlite3UpperToLower[c];
98839
 
      }
98840
 
      *pC = c + 1;
98841
 
    }
98842
 
    pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, noCase ? "NOCASE" : "BINARY",0);
98843
 
    pNewExpr1 = sqlite3PExpr(pParse, TK_GE, 
98844
 
                     sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
98845
 
                     pStr1, 0);
98846
 
    idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
98847
 
    testcase( idxNew1==0 );
98848
 
    exprAnalyze(pSrc, pWC, idxNew1);
98849
 
    pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
98850
 
                     sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
98851
 
                     pStr2, 0);
98852
 
    idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
98853
 
    testcase( idxNew2==0 );
98854
 
    exprAnalyze(pSrc, pWC, idxNew2);
98855
 
    pTerm = &pWC->a[idxTerm];
98856
 
    if( isComplete ){
98857
 
      pWC->a[idxNew1].iParent = idxTerm;
98858
 
      pWC->a[idxNew2].iParent = idxTerm;
98859
 
      pTerm->nChild = 2;
98860
 
    }
98861
 
  }
98862
 
#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
98863
 
 
98864
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
98865
 
  /* Add a WO_MATCH auxiliary term to the constraint set if the
98866
 
  ** current expression is of the form:  column MATCH expr.
98867
 
  ** This information is used by the xBestIndex methods of
98868
 
  ** virtual tables.  The native query optimizer does not attempt
98869
 
  ** to do anything with MATCH functions.
98870
 
  */
98871
 
  if( isMatchOfColumn(pExpr) ){
98872
 
    int idxNew;
98873
 
    Expr *pRight, *pLeft;
98874
 
    WhereTerm *pNewTerm;
98875
 
    Bitmask prereqColumn, prereqExpr;
98876
 
 
98877
 
    pRight = pExpr->x.pList->a[0].pExpr;
98878
 
    pLeft = pExpr->x.pList->a[1].pExpr;
98879
 
    prereqExpr = exprTableUsage(pMaskSet, pRight);
98880
 
    prereqColumn = exprTableUsage(pMaskSet, pLeft);
98881
 
    if( (prereqExpr & prereqColumn)==0 ){
98882
 
      Expr *pNewExpr;
98883
 
      pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
98884
 
                              0, sqlite3ExprDup(db, pRight, 0), 0);
98885
 
      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
98886
 
      testcase( idxNew==0 );
98887
 
      pNewTerm = &pWC->a[idxNew];
98888
 
      pNewTerm->prereqRight = prereqExpr;
98889
 
      pNewTerm->leftCursor = pLeft->iTable;
98890
 
      pNewTerm->u.leftColumn = pLeft->iColumn;
98891
 
      pNewTerm->eOperator = WO_MATCH;
98892
 
      pNewTerm->iParent = idxTerm;
98893
 
      pTerm = &pWC->a[idxTerm];
98894
 
      pTerm->nChild = 1;
98895
 
      pTerm->wtFlags |= TERM_COPIED;
98896
 
      pNewTerm->prereqAll = pTerm->prereqAll;
98897
 
    }
98898
 
  }
98899
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
98900
 
 
98901
 
#ifdef SQLITE_ENABLE_STAT2
98902
 
  /* When sqlite_stat2 histogram data is available an operator of the
98903
 
  ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
98904
 
  ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
98905
 
  ** virtual term of that form.
98906
 
  **
98907
 
  ** Note that the virtual term must be tagged with TERM_VNULL.  This
98908
 
  ** TERM_VNULL tag will suppress the not-null check at the beginning
98909
 
  ** of the loop.  Without the TERM_VNULL flag, the not-null check at
98910
 
  ** the start of the loop will prevent any results from being returned.
98911
 
  */
98912
 
  if( pExpr->op==TK_NOTNULL
98913
 
   && pExpr->pLeft->op==TK_COLUMN
98914
 
   && pExpr->pLeft->iColumn>=0
98915
 
  ){
98916
 
    Expr *pNewExpr;
98917
 
    Expr *pLeft = pExpr->pLeft;
98918
 
    int idxNew;
98919
 
    WhereTerm *pNewTerm;
98920
 
 
98921
 
    pNewExpr = sqlite3PExpr(pParse, TK_GT,
98922
 
                            sqlite3ExprDup(db, pLeft, 0),
98923
 
                            sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
98924
 
 
98925
 
    idxNew = whereClauseInsert(pWC, pNewExpr,
98926
 
                              TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
98927
 
    if( idxNew ){
98928
 
      pNewTerm = &pWC->a[idxNew];
98929
 
      pNewTerm->prereqRight = 0;
98930
 
      pNewTerm->leftCursor = pLeft->iTable;
98931
 
      pNewTerm->u.leftColumn = pLeft->iColumn;
98932
 
      pNewTerm->eOperator = WO_GT;
98933
 
      pNewTerm->iParent = idxTerm;
98934
 
      pTerm = &pWC->a[idxTerm];
98935
 
      pTerm->nChild = 1;
98936
 
      pTerm->wtFlags |= TERM_COPIED;
98937
 
      pNewTerm->prereqAll = pTerm->prereqAll;
98938
 
    }
98939
 
  }
98940
 
#endif /* SQLITE_ENABLE_STAT2 */
98941
 
 
98942
 
  /* Prevent ON clause terms of a LEFT JOIN from being used to drive
98943
 
  ** an index for tables to the left of the join.
98944
 
  */
98945
 
  pTerm->prereqRight |= extraRight;
98946
 
}
98947
 
 
98948
 
/*
98949
 
** Return TRUE if any of the expressions in pList->a[iFirst...] contain
98950
 
** a reference to any table other than the iBase table.
98951
 
*/
98952
 
static int referencesOtherTables(
98953
 
  ExprList *pList,          /* Search expressions in ths list */
98954
 
  WhereMaskSet *pMaskSet,   /* Mapping from tables to bitmaps */
98955
 
  int iFirst,               /* Be searching with the iFirst-th expression */
98956
 
  int iBase                 /* Ignore references to this table */
98957
 
){
98958
 
  Bitmask allowed = ~getMask(pMaskSet, iBase);
98959
 
  while( iFirst<pList->nExpr ){
98960
 
    if( (exprTableUsage(pMaskSet, pList->a[iFirst++].pExpr)&allowed)!=0 ){
98961
 
      return 1;
98962
 
    }
98963
 
  }
98964
 
  return 0;
98965
 
}
98966
 
 
98967
 
 
98968
 
/*
98969
 
** This routine decides if pIdx can be used to satisfy the ORDER BY
98970
 
** clause.  If it can, it returns 1.  If pIdx cannot satisfy the
98971
 
** ORDER BY clause, this routine returns 0.
98972
 
**
98973
 
** pOrderBy is an ORDER BY clause from a SELECT statement.  pTab is the
98974
 
** left-most table in the FROM clause of that same SELECT statement and
98975
 
** the table has a cursor number of "base".  pIdx is an index on pTab.
98976
 
**
98977
 
** nEqCol is the number of columns of pIdx that are used as equality
98978
 
** constraints.  Any of these columns may be missing from the ORDER BY
98979
 
** clause and the match can still be a success.
98980
 
**
98981
 
** All terms of the ORDER BY that match against the index must be either
98982
 
** ASC or DESC.  (Terms of the ORDER BY clause past the end of a UNIQUE
98983
 
** index do not need to satisfy this constraint.)  The *pbRev value is
98984
 
** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
98985
 
** the ORDER BY clause is all ASC.
98986
 
*/
98987
 
static int isSortingIndex(
98988
 
  Parse *pParse,          /* Parsing context */
98989
 
  WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmaps */
98990
 
  Index *pIdx,            /* The index we are testing */
98991
 
  int base,               /* Cursor number for the table to be sorted */
98992
 
  ExprList *pOrderBy,     /* The ORDER BY clause */
98993
 
  int nEqCol,             /* Number of index columns with == constraints */
98994
 
  int wsFlags,            /* Index usages flags */
98995
 
  int *pbRev              /* Set to 1 if ORDER BY is DESC */
98996
 
){
98997
 
  int i, j;                       /* Loop counters */
98998
 
  int sortOrder = 0;              /* XOR of index and ORDER BY sort direction */
98999
 
  int nTerm;                      /* Number of ORDER BY terms */
99000
 
  struct ExprList_item *pTerm;    /* A term of the ORDER BY clause */
99001
 
  sqlite3 *db = pParse->db;
99002
 
 
99003
 
  assert( pOrderBy!=0 );
99004
 
  nTerm = pOrderBy->nExpr;
99005
 
  assert( nTerm>0 );
99006
 
 
99007
 
  /* Argument pIdx must either point to a 'real' named index structure, 
99008
 
  ** or an index structure allocated on the stack by bestBtreeIndex() to
99009
 
  ** represent the rowid index that is part of every table.  */
99010
 
  assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
99011
 
 
99012
 
  /* Match terms of the ORDER BY clause against columns of
99013
 
  ** the index.
99014
 
  **
99015
 
  ** Note that indices have pIdx->nColumn regular columns plus
99016
 
  ** one additional column containing the rowid.  The rowid column
99017
 
  ** of the index is also allowed to match against the ORDER BY
99018
 
  ** clause.
99019
 
  */
99020
 
  for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<=pIdx->nColumn; i++){
99021
 
    Expr *pExpr;       /* The expression of the ORDER BY pTerm */
99022
 
    CollSeq *pColl;    /* The collating sequence of pExpr */
99023
 
    int termSortOrder; /* Sort order for this term */
99024
 
    int iColumn;       /* The i-th column of the index.  -1 for rowid */
99025
 
    int iSortOrder;    /* 1 for DESC, 0 for ASC on the i-th index term */
99026
 
    const char *zColl; /* Name of the collating sequence for i-th index term */
99027
 
 
99028
 
    pExpr = pTerm->pExpr;
99029
 
    if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
99030
 
      /* Can not use an index sort on anything that is not a column in the
99031
 
      ** left-most table of the FROM clause */
99032
 
      break;
99033
 
    }
99034
 
    pColl = sqlite3ExprCollSeq(pParse, pExpr);
99035
 
    if( !pColl ){
99036
 
      pColl = db->pDfltColl;
99037
 
    }
99038
 
    if( pIdx->zName && i<pIdx->nColumn ){
99039
 
      iColumn = pIdx->aiColumn[i];
99040
 
      if( iColumn==pIdx->pTable->iPKey ){
99041
 
        iColumn = -1;
99042
 
      }
99043
 
      iSortOrder = pIdx->aSortOrder[i];
99044
 
      zColl = pIdx->azColl[i];
99045
 
    }else{
99046
 
      iColumn = -1;
99047
 
      iSortOrder = 0;
99048
 
      zColl = pColl->zName;
99049
 
    }
99050
 
    if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){
99051
 
      /* Term j of the ORDER BY clause does not match column i of the index */
99052
 
      if( i<nEqCol ){
99053
 
        /* If an index column that is constrained by == fails to match an
99054
 
        ** ORDER BY term, that is OK.  Just ignore that column of the index
99055
 
        */
99056
 
        continue;
99057
 
      }else if( i==pIdx->nColumn ){
99058
 
        /* Index column i is the rowid.  All other terms match. */
99059
 
        break;
99060
 
      }else{
99061
 
        /* If an index column fails to match and is not constrained by ==
99062
 
        ** then the index cannot satisfy the ORDER BY constraint.
99063
 
        */
99064
 
        return 0;
99065
 
      }
99066
 
    }
99067
 
    assert( pIdx->aSortOrder!=0 || iColumn==-1 );
99068
 
    assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
99069
 
    assert( iSortOrder==0 || iSortOrder==1 );
99070
 
    termSortOrder = iSortOrder ^ pTerm->sortOrder;
99071
 
    if( i>nEqCol ){
99072
 
      if( termSortOrder!=sortOrder ){
99073
 
        /* Indices can only be used if all ORDER BY terms past the
99074
 
        ** equality constraints are all either DESC or ASC. */
99075
 
        return 0;
99076
 
      }
99077
 
    }else{
99078
 
      sortOrder = termSortOrder;
99079
 
    }
99080
 
    j++;
99081
 
    pTerm++;
99082
 
    if( iColumn<0 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
99083
 
      /* If the indexed column is the primary key and everything matches
99084
 
      ** so far and none of the ORDER BY terms to the right reference other
99085
 
      ** tables in the join, then we are assured that the index can be used 
99086
 
      ** to sort because the primary key is unique and so none of the other
99087
 
      ** columns will make any difference
99088
 
      */
99089
 
      j = nTerm;
99090
 
    }
99091
 
  }
99092
 
 
99093
 
  *pbRev = sortOrder!=0;
99094
 
  if( j>=nTerm ){
99095
 
    /* All terms of the ORDER BY clause are covered by this index so
99096
 
    ** this index can be used for sorting. */
99097
 
    return 1;
99098
 
  }
99099
 
  if( pIdx->onError!=OE_None && i==pIdx->nColumn
99100
 
      && (wsFlags & WHERE_COLUMN_NULL)==0
99101
 
      && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
99102
 
    /* All terms of this index match some prefix of the ORDER BY clause
99103
 
    ** and the index is UNIQUE and no terms on the tail of the ORDER BY
99104
 
    ** clause reference other tables in a join.  If this is all true then
99105
 
    ** the order by clause is superfluous.  Not that if the matching
99106
 
    ** condition is IS NULL then the result is not necessarily unique
99107
 
    ** even on a UNIQUE index, so disallow those cases. */
99108
 
    return 1;
99109
 
  }
99110
 
  return 0;
99111
 
}
99112
 
 
99113
 
/*
99114
 
** Prepare a crude estimate of the logarithm of the input value.
99115
 
** The results need not be exact.  This is only used for estimating
99116
 
** the total cost of performing operations with O(logN) or O(NlogN)
99117
 
** complexity.  Because N is just a guess, it is no great tragedy if
99118
 
** logN is a little off.
99119
 
*/
99120
 
static double estLog(double N){
99121
 
  double logN = 1;
99122
 
  double x = 10;
99123
 
  while( N>x ){
99124
 
    logN += 1;
99125
 
    x *= 10;
99126
 
  }
99127
 
  return logN;
99128
 
}
99129
 
 
99130
 
/*
99131
 
** Two routines for printing the content of an sqlite3_index_info
99132
 
** structure.  Used for testing and debugging only.  If neither
99133
 
** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
99134
 
** are no-ops.
99135
 
*/
99136
 
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG)
99137
 
static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
99138
 
  int i;
99139
 
  if( !sqlite3WhereTrace ) return;
99140
 
  for(i=0; i<p->nConstraint; i++){
99141
 
    sqlite3DebugPrintf("  constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
99142
 
       i,
99143
 
       p->aConstraint[i].iColumn,
99144
 
       p->aConstraint[i].iTermOffset,
99145
 
       p->aConstraint[i].op,
99146
 
       p->aConstraint[i].usable);
99147
 
  }
99148
 
  for(i=0; i<p->nOrderBy; i++){
99149
 
    sqlite3DebugPrintf("  orderby[%d]: col=%d desc=%d\n",
99150
 
       i,
99151
 
       p->aOrderBy[i].iColumn,
99152
 
       p->aOrderBy[i].desc);
99153
 
  }
99154
 
}
99155
 
static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
99156
 
  int i;
99157
 
  if( !sqlite3WhereTrace ) return;
99158
 
  for(i=0; i<p->nConstraint; i++){
99159
 
    sqlite3DebugPrintf("  usage[%d]: argvIdx=%d omit=%d\n",
99160
 
       i,
99161
 
       p->aConstraintUsage[i].argvIndex,
99162
 
       p->aConstraintUsage[i].omit);
99163
 
  }
99164
 
  sqlite3DebugPrintf("  idxNum=%d\n", p->idxNum);
99165
 
  sqlite3DebugPrintf("  idxStr=%s\n", p->idxStr);
99166
 
  sqlite3DebugPrintf("  orderByConsumed=%d\n", p->orderByConsumed);
99167
 
  sqlite3DebugPrintf("  estimatedCost=%g\n", p->estimatedCost);
99168
 
}
99169
 
#else
99170
 
#define TRACE_IDX_INPUTS(A)
99171
 
#define TRACE_IDX_OUTPUTS(A)
99172
 
#endif
99173
 
 
99174
 
/* 
99175
 
** Required because bestIndex() is called by bestOrClauseIndex() 
99176
 
*/
99177
 
static void bestIndex(
99178
 
    Parse*, WhereClause*, struct SrcList_item*,
99179
 
    Bitmask, Bitmask, ExprList*, WhereCost*);
99180
 
 
99181
 
/*
99182
 
** This routine attempts to find an scanning strategy that can be used 
99183
 
** to optimize an 'OR' expression that is part of a WHERE clause. 
99184
 
**
99185
 
** The table associated with FROM clause term pSrc may be either a
99186
 
** regular B-Tree table or a virtual table.
99187
 
*/
99188
 
static void bestOrClauseIndex(
99189
 
  Parse *pParse,              /* The parsing context */
99190
 
  WhereClause *pWC,           /* The WHERE clause */
99191
 
  struct SrcList_item *pSrc,  /* The FROM clause term to search */
99192
 
  Bitmask notReady,           /* Mask of cursors not available for indexing */
99193
 
  Bitmask notValid,           /* Cursors not available for any purpose */
99194
 
  ExprList *pOrderBy,         /* The ORDER BY clause */
99195
 
  WhereCost *pCost            /* Lowest cost query plan */
99196
 
){
99197
 
#ifndef SQLITE_OMIT_OR_OPTIMIZATION
99198
 
  const int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
99199
 
  const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur);  /* Bitmask for pSrc */
99200
 
  WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm];        /* End of pWC->a[] */
99201
 
  WhereTerm *pTerm;                 /* A single term of the WHERE clause */
99202
 
 
99203
 
  /* No OR-clause optimization allowed if the INDEXED BY or NOT INDEXED clauses
99204
 
  ** are used */
99205
 
  if( pSrc->notIndexed || pSrc->pIndex!=0 ){
99206
 
    return;
99207
 
  }
99208
 
 
99209
 
  /* Search the WHERE clause terms for a usable WO_OR term. */
99210
 
  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
99211
 
    if( pTerm->eOperator==WO_OR 
99212
 
     && ((pTerm->prereqAll & ~maskSrc) & notReady)==0
99213
 
     && (pTerm->u.pOrInfo->indexable & maskSrc)!=0 
99214
 
    ){
99215
 
      WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
99216
 
      WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
99217
 
      WhereTerm *pOrTerm;
99218
 
      int flags = WHERE_MULTI_OR;
99219
 
      double rTotal = 0;
99220
 
      double nRow = 0;
99221
 
      Bitmask used = 0;
99222
 
 
99223
 
      for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
99224
 
        WhereCost sTermCost;
99225
 
        WHERETRACE(("... Multi-index OR testing for term %d of %d....\n", 
99226
 
          (pOrTerm - pOrWC->a), (pTerm - pWC->a)
99227
 
        ));
99228
 
        if( pOrTerm->eOperator==WO_AND ){
99229
 
          WhereClause *pAndWC = &pOrTerm->u.pAndInfo->wc;
99230
 
          bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
99231
 
        }else if( pOrTerm->leftCursor==iCur ){
99232
 
          WhereClause tempWC;
99233
 
          tempWC.pParse = pWC->pParse;
99234
 
          tempWC.pMaskSet = pWC->pMaskSet;
99235
 
          tempWC.op = TK_AND;
99236
 
          tempWC.a = pOrTerm;
99237
 
          tempWC.nTerm = 1;
99238
 
          bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
99239
 
        }else{
99240
 
          continue;
99241
 
        }
99242
 
        rTotal += sTermCost.rCost;
99243
 
        nRow += sTermCost.plan.nRow;
99244
 
        used |= sTermCost.used;
99245
 
        if( rTotal>=pCost->rCost ) break;
99246
 
      }
99247
 
 
99248
 
      /* If there is an ORDER BY clause, increase the scan cost to account 
99249
 
      ** for the cost of the sort. */
99250
 
      if( pOrderBy!=0 ){
99251
 
        WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
99252
 
                    rTotal, rTotal+nRow*estLog(nRow)));
99253
 
        rTotal += nRow*estLog(nRow);
99254
 
      }
99255
 
 
99256
 
      /* If the cost of scanning using this OR term for optimization is
99257
 
      ** less than the current cost stored in pCost, replace the contents
99258
 
      ** of pCost. */
99259
 
      WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
99260
 
      if( rTotal<pCost->rCost ){
99261
 
        pCost->rCost = rTotal;
99262
 
        pCost->used = used;
99263
 
        pCost->plan.nRow = nRow;
99264
 
        pCost->plan.wsFlags = flags;
99265
 
        pCost->plan.u.pTerm = pTerm;
99266
 
      }
99267
 
    }
99268
 
  }
99269
 
#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
99270
 
}
99271
 
 
99272
 
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
99273
 
/*
99274
 
** Return TRUE if the WHERE clause term pTerm is of a form where it
99275
 
** could be used with an index to access pSrc, assuming an appropriate
99276
 
** index existed.
99277
 
*/
99278
 
static int termCanDriveIndex(
99279
 
  WhereTerm *pTerm,              /* WHERE clause term to check */
99280
 
  struct SrcList_item *pSrc,     /* Table we are trying to access */
99281
 
  Bitmask notReady               /* Tables in outer loops of the join */
99282
 
){
99283
 
  char aff;
99284
 
  if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
99285
 
  if( pTerm->eOperator!=WO_EQ ) return 0;
99286
 
  if( (pTerm->prereqRight & notReady)!=0 ) return 0;
99287
 
  aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
99288
 
  if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
99289
 
  return 1;
99290
 
}
99291
 
#endif
99292
 
 
99293
 
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
99294
 
/*
99295
 
** If the query plan for pSrc specified in pCost is a full table scan
99296
 
** and indexing is allows (if there is no NOT INDEXED clause) and it
99297
 
** possible to construct a transient index that would perform better
99298
 
** than a full table scan even when the cost of constructing the index
99299
 
** is taken into account, then alter the query plan to use the
99300
 
** transient index.
99301
 
*/
99302
 
static void bestAutomaticIndex(
99303
 
  Parse *pParse,              /* The parsing context */
99304
 
  WhereClause *pWC,           /* The WHERE clause */
99305
 
  struct SrcList_item *pSrc,  /* The FROM clause term to search */
99306
 
  Bitmask notReady,           /* Mask of cursors that are not available */
99307
 
  WhereCost *pCost            /* Lowest cost query plan */
99308
 
){
99309
 
  double nTableRow;           /* Rows in the input table */
99310
 
  double logN;                /* log(nTableRow) */
99311
 
  double costTempIdx;         /* per-query cost of the transient index */
99312
 
  WhereTerm *pTerm;           /* A single term of the WHERE clause */
99313
 
  WhereTerm *pWCEnd;          /* End of pWC->a[] */
99314
 
  Table *pTable;              /* Table tht might be indexed */
99315
 
 
99316
 
  if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){
99317
 
    /* Automatic indices are disabled at run-time */
99318
 
    return;
99319
 
  }
99320
 
  if( (pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){
99321
 
    /* We already have some kind of index in use for this query. */
99322
 
    return;
99323
 
  }
99324
 
  if( pSrc->notIndexed ){
99325
 
    /* The NOT INDEXED clause appears in the SQL. */
99326
 
    return;
99327
 
  }
99328
 
 
99329
 
  assert( pParse->nQueryLoop >= (double)1 );
99330
 
  pTable = pSrc->pTab;
99331
 
  nTableRow = pTable->nRowEst;
99332
 
  logN = estLog(nTableRow);
99333
 
  costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
99334
 
  if( costTempIdx>=pCost->rCost ){
99335
 
    /* The cost of creating the transient table would be greater than
99336
 
    ** doing the full table scan */
99337
 
    return;
99338
 
  }
99339
 
 
99340
 
  /* Search for any equality comparison term */
99341
 
  pWCEnd = &pWC->a[pWC->nTerm];
99342
 
  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
99343
 
    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
99344
 
      WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
99345
 
                    pCost->rCost, costTempIdx));
99346
 
      pCost->rCost = costTempIdx;
99347
 
      pCost->plan.nRow = logN + 1;
99348
 
      pCost->plan.wsFlags = WHERE_TEMP_INDEX;
99349
 
      pCost->used = pTerm->prereqRight;
99350
 
      break;
99351
 
    }
99352
 
  }
99353
 
}
99354
 
#else
99355
 
# define bestAutomaticIndex(A,B,C,D,E)  /* no-op */
99356
 
#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
99357
 
 
99358
 
 
99359
 
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
99360
 
/*
99361
 
** Generate code to construct the Index object for an automatic index
99362
 
** and to set up the WhereLevel object pLevel so that the code generator
99363
 
** makes use of the automatic index.
99364
 
*/
99365
 
static void constructAutomaticIndex(
99366
 
  Parse *pParse,              /* The parsing context */
99367
 
  WhereClause *pWC,           /* The WHERE clause */
99368
 
  struct SrcList_item *pSrc,  /* The FROM clause term to get the next index */
99369
 
  Bitmask notReady,           /* Mask of cursors that are not available */
99370
 
  WhereLevel *pLevel          /* Write new index here */
99371
 
){
99372
 
  int nColumn;                /* Number of columns in the constructed index */
99373
 
  WhereTerm *pTerm;           /* A single term of the WHERE clause */
99374
 
  WhereTerm *pWCEnd;          /* End of pWC->a[] */
99375
 
  int nByte;                  /* Byte of memory needed for pIdx */
99376
 
  Index *pIdx;                /* Object describing the transient index */
99377
 
  Vdbe *v;                    /* Prepared statement under construction */
99378
 
  int regIsInit;              /* Register set by initialization */
99379
 
  int addrInit;               /* Address of the initialization bypass jump */
99380
 
  Table *pTable;              /* The table being indexed */
99381
 
  KeyInfo *pKeyinfo;          /* Key information for the index */   
99382
 
  int addrTop;                /* Top of the index fill loop */
99383
 
  int regRecord;              /* Register holding an index record */
99384
 
  int n;                      /* Column counter */
99385
 
  int i;                      /* Loop counter */
99386
 
  int mxBitCol;               /* Maximum column in pSrc->colUsed */
99387
 
  CollSeq *pColl;             /* Collating sequence to on a column */
99388
 
  Bitmask idxCols;            /* Bitmap of columns used for indexing */
99389
 
  Bitmask extraCols;          /* Bitmap of additional columns */
99390
 
 
99391
 
  /* Generate code to skip over the creation and initialization of the
99392
 
  ** transient index on 2nd and subsequent iterations of the loop. */
99393
 
  v = pParse->pVdbe;
99394
 
  assert( v!=0 );
99395
 
  regIsInit = ++pParse->nMem;
99396
 
  addrInit = sqlite3VdbeAddOp1(v, OP_If, regIsInit);
99397
 
  sqlite3VdbeAddOp2(v, OP_Integer, 1, regIsInit);
99398
 
 
99399
 
  /* Count the number of columns that will be added to the index
99400
 
  ** and used to match WHERE clause constraints */
99401
 
  nColumn = 0;
99402
 
  pTable = pSrc->pTab;
99403
 
  pWCEnd = &pWC->a[pWC->nTerm];
99404
 
  idxCols = 0;
99405
 
  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
99406
 
    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
99407
 
      int iCol = pTerm->u.leftColumn;
99408
 
      Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
99409
 
      testcase( iCol==BMS );
99410
 
      testcase( iCol==BMS-1 );
99411
 
      if( (idxCols & cMask)==0 ){
99412
 
        nColumn++;
99413
 
        idxCols |= cMask;
99414
 
      }
99415
 
    }
99416
 
  }
99417
 
  assert( nColumn>0 );
99418
 
  pLevel->plan.nEq = nColumn;
99419
 
 
99420
 
  /* Count the number of additional columns needed to create a
99421
 
  ** covering index.  A "covering index" is an index that contains all
99422
 
  ** columns that are needed by the query.  With a covering index, the
99423
 
  ** original table never needs to be accessed.  Automatic indices must
99424
 
  ** be a covering index because the index will not be updated if the
99425
 
  ** original table changes and the index and table cannot both be used
99426
 
  ** if they go out of sync.
99427
 
  */
99428
 
  extraCols = pSrc->colUsed & (~idxCols | (((Bitmask)1)<<(BMS-1)));
99429
 
  mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
99430
 
  testcase( pTable->nCol==BMS-1 );
99431
 
  testcase( pTable->nCol==BMS-2 );
99432
 
  for(i=0; i<mxBitCol; i++){
99433
 
    if( extraCols & (((Bitmask)1)<<i) ) nColumn++;
99434
 
  }
99435
 
  if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
99436
 
    nColumn += pTable->nCol - BMS + 1;
99437
 
  }
99438
 
  pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ;
99439
 
 
99440
 
  /* Construct the Index object to describe this index */
99441
 
  nByte = sizeof(Index);
99442
 
  nByte += nColumn*sizeof(int);     /* Index.aiColumn */
99443
 
  nByte += nColumn*sizeof(char*);   /* Index.azColl */
99444
 
  nByte += nColumn;                 /* Index.aSortOrder */
99445
 
  pIdx = sqlite3DbMallocZero(pParse->db, nByte);
99446
 
  if( pIdx==0 ) return;
99447
 
  pLevel->plan.u.pIdx = pIdx;
99448
 
  pIdx->azColl = (char**)&pIdx[1];
99449
 
  pIdx->aiColumn = (int*)&pIdx->azColl[nColumn];
99450
 
  pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn];
99451
 
  pIdx->zName = "auto-index";
99452
 
  pIdx->nColumn = nColumn;
99453
 
  pIdx->pTable = pTable;
99454
 
  n = 0;
99455
 
  idxCols = 0;
99456
 
  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
99457
 
    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
99458
 
      int iCol = pTerm->u.leftColumn;
99459
 
      Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
99460
 
      if( (idxCols & cMask)==0 ){
99461
 
        Expr *pX = pTerm->pExpr;
99462
 
        idxCols |= cMask;
99463
 
        pIdx->aiColumn[n] = pTerm->u.leftColumn;
99464
 
        pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
99465
 
        pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
99466
 
        n++;
99467
 
      }
99468
 
    }
99469
 
  }
99470
 
  assert( (u32)n==pLevel->plan.nEq );
99471
 
 
99472
 
  /* Add additional columns needed to make the automatic index into
99473
 
  ** a covering index */
99474
 
  for(i=0; i<mxBitCol; i++){
99475
 
    if( extraCols & (((Bitmask)1)<<i) ){
99476
 
      pIdx->aiColumn[n] = i;
99477
 
      pIdx->azColl[n] = "BINARY";
99478
 
      n++;
99479
 
    }
99480
 
  }
99481
 
  if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
99482
 
    for(i=BMS-1; i<pTable->nCol; i++){
99483
 
      pIdx->aiColumn[n] = i;
99484
 
      pIdx->azColl[n] = "BINARY";
99485
 
      n++;
99486
 
    }
99487
 
  }
99488
 
  assert( n==nColumn );
99489
 
 
99490
 
  /* Create the automatic index */
99491
 
  pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx);
99492
 
  assert( pLevel->iIdxCur>=0 );
99493
 
  sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0,
99494
 
                    (char*)pKeyinfo, P4_KEYINFO_HANDOFF);
99495
 
  VdbeComment((v, "for %s", pTable->zName));
99496
 
 
99497
 
  /* Fill the automatic index with content */
99498
 
  addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur);
99499
 
  regRecord = sqlite3GetTempReg(pParse);
99500
 
  sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1);
99501
 
  sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
99502
 
  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
99503
 
  sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
99504
 
  sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
99505
 
  sqlite3VdbeJumpHere(v, addrTop);
99506
 
  sqlite3ReleaseTempReg(pParse, regRecord);
99507
 
  
99508
 
  /* Jump here when skipping the initialization */
99509
 
  sqlite3VdbeJumpHere(v, addrInit);
99510
 
}
99511
 
#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
99512
 
 
99513
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
99514
 
/*
99515
 
** Allocate and populate an sqlite3_index_info structure. It is the 
99516
 
** responsibility of the caller to eventually release the structure
99517
 
** by passing the pointer returned by this function to sqlite3_free().
99518
 
*/
99519
 
static sqlite3_index_info *allocateIndexInfo(
99520
 
  Parse *pParse, 
99521
 
  WhereClause *pWC,
99522
 
  struct SrcList_item *pSrc,
99523
 
  ExprList *pOrderBy
99524
 
){
99525
 
  int i, j;
99526
 
  int nTerm;
99527
 
  struct sqlite3_index_constraint *pIdxCons;
99528
 
  struct sqlite3_index_orderby *pIdxOrderBy;
99529
 
  struct sqlite3_index_constraint_usage *pUsage;
99530
 
  WhereTerm *pTerm;
99531
 
  int nOrderBy;
99532
 
  sqlite3_index_info *pIdxInfo;
99533
 
 
99534
 
  WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName));
99535
 
 
99536
 
  /* Count the number of possible WHERE clause constraints referring
99537
 
  ** to this virtual table */
99538
 
  for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
99539
 
    if( pTerm->leftCursor != pSrc->iCursor ) continue;
99540
 
    assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
99541
 
    testcase( pTerm->eOperator==WO_IN );
99542
 
    testcase( pTerm->eOperator==WO_ISNULL );
99543
 
    if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
99544
 
    nTerm++;
99545
 
  }
99546
 
 
99547
 
  /* If the ORDER BY clause contains only columns in the current 
99548
 
  ** virtual table then allocate space for the aOrderBy part of
99549
 
  ** the sqlite3_index_info structure.
99550
 
  */
99551
 
  nOrderBy = 0;
99552
 
  if( pOrderBy ){
99553
 
    for(i=0; i<pOrderBy->nExpr; i++){
99554
 
      Expr *pExpr = pOrderBy->a[i].pExpr;
99555
 
      if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
99556
 
    }
99557
 
    if( i==pOrderBy->nExpr ){
99558
 
      nOrderBy = pOrderBy->nExpr;
99559
 
    }
99560
 
  }
99561
 
 
99562
 
  /* Allocate the sqlite3_index_info structure
99563
 
  */
99564
 
  pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
99565
 
                           + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
99566
 
                           + sizeof(*pIdxOrderBy)*nOrderBy );
99567
 
  if( pIdxInfo==0 ){
99568
 
    sqlite3ErrorMsg(pParse, "out of memory");
99569
 
    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
99570
 
    return 0;
99571
 
  }
99572
 
 
99573
 
  /* Initialize the structure.  The sqlite3_index_info structure contains
99574
 
  ** many fields that are declared "const" to prevent xBestIndex from
99575
 
  ** changing them.  We have to do some funky casting in order to
99576
 
  ** initialize those fields.
99577
 
  */
99578
 
  pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
99579
 
  pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
99580
 
  pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
99581
 
  *(int*)&pIdxInfo->nConstraint = nTerm;
99582
 
  *(int*)&pIdxInfo->nOrderBy = nOrderBy;
99583
 
  *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
99584
 
  *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
99585
 
  *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
99586
 
                                                                   pUsage;
99587
 
 
99588
 
  for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
99589
 
    if( pTerm->leftCursor != pSrc->iCursor ) continue;
99590
 
    assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
99591
 
    testcase( pTerm->eOperator==WO_IN );
99592
 
    testcase( pTerm->eOperator==WO_ISNULL );
99593
 
    if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
99594
 
    pIdxCons[j].iColumn = pTerm->u.leftColumn;
99595
 
    pIdxCons[j].iTermOffset = i;
99596
 
    pIdxCons[j].op = (u8)pTerm->eOperator;
99597
 
    /* The direct assignment in the previous line is possible only because
99598
 
    ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
99599
 
    ** following asserts verify this fact. */
99600
 
    assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
99601
 
    assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
99602
 
    assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
99603
 
    assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
99604
 
    assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
99605
 
    assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
99606
 
    assert( pTerm->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
99607
 
    j++;
99608
 
  }
99609
 
  for(i=0; i<nOrderBy; i++){
99610
 
    Expr *pExpr = pOrderBy->a[i].pExpr;
99611
 
    pIdxOrderBy[i].iColumn = pExpr->iColumn;
99612
 
    pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
99613
 
  }
99614
 
 
99615
 
  return pIdxInfo;
99616
 
}
99617
 
 
99618
 
/*
99619
 
** The table object reference passed as the second argument to this function
99620
 
** must represent a virtual table. This function invokes the xBestIndex()
99621
 
** method of the virtual table with the sqlite3_index_info pointer passed
99622
 
** as the argument.
99623
 
**
99624
 
** If an error occurs, pParse is populated with an error message and a
99625
 
** non-zero value is returned. Otherwise, 0 is returned and the output
99626
 
** part of the sqlite3_index_info structure is left populated.
99627
 
**
99628
 
** Whether or not an error is returned, it is the responsibility of the
99629
 
** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
99630
 
** that this is required.
99631
 
*/
99632
 
static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
99633
 
  sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
99634
 
  int i;
99635
 
  int rc;
99636
 
 
99637
 
  WHERETRACE(("xBestIndex for %s\n", pTab->zName));
99638
 
  TRACE_IDX_INPUTS(p);
99639
 
  rc = pVtab->pModule->xBestIndex(pVtab, p);
99640
 
  TRACE_IDX_OUTPUTS(p);
99641
 
 
99642
 
  if( rc!=SQLITE_OK ){
99643
 
    if( rc==SQLITE_NOMEM ){
99644
 
      pParse->db->mallocFailed = 1;
99645
 
    }else if( !pVtab->zErrMsg ){
99646
 
      sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
99647
 
    }else{
99648
 
      sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
99649
 
    }
99650
 
  }
99651
 
  sqlite3_free(pVtab->zErrMsg);
99652
 
  pVtab->zErrMsg = 0;
99653
 
 
99654
 
  for(i=0; i<p->nConstraint; i++){
99655
 
    if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
99656
 
      sqlite3ErrorMsg(pParse, 
99657
 
          "table %s: xBestIndex returned an invalid plan", pTab->zName);
99658
 
    }
99659
 
  }
99660
 
 
99661
 
  return pParse->nErr;
99662
 
}
99663
 
 
99664
 
 
99665
 
/*
99666
 
** Compute the best index for a virtual table.
99667
 
**
99668
 
** The best index is computed by the xBestIndex method of the virtual
99669
 
** table module.  This routine is really just a wrapper that sets up
99670
 
** the sqlite3_index_info structure that is used to communicate with
99671
 
** xBestIndex.
99672
 
**
99673
 
** In a join, this routine might be called multiple times for the
99674
 
** same virtual table.  The sqlite3_index_info structure is created
99675
 
** and initialized on the first invocation and reused on all subsequent
99676
 
** invocations.  The sqlite3_index_info structure is also used when
99677
 
** code is generated to access the virtual table.  The whereInfoDelete() 
99678
 
** routine takes care of freeing the sqlite3_index_info structure after
99679
 
** everybody has finished with it.
99680
 
*/
99681
 
static void bestVirtualIndex(
99682
 
  Parse *pParse,                  /* The parsing context */
99683
 
  WhereClause *pWC,               /* The WHERE clause */
99684
 
  struct SrcList_item *pSrc,      /* The FROM clause term to search */
99685
 
  Bitmask notReady,               /* Mask of cursors not available for index */
99686
 
  Bitmask notValid,               /* Cursors not valid for any purpose */
99687
 
  ExprList *pOrderBy,             /* The order by clause */
99688
 
  WhereCost *pCost,               /* Lowest cost query plan */
99689
 
  sqlite3_index_info **ppIdxInfo  /* Index information passed to xBestIndex */
99690
 
){
99691
 
  Table *pTab = pSrc->pTab;
99692
 
  sqlite3_index_info *pIdxInfo;
99693
 
  struct sqlite3_index_constraint *pIdxCons;
99694
 
  struct sqlite3_index_constraint_usage *pUsage;
99695
 
  WhereTerm *pTerm;
99696
 
  int i, j;
99697
 
  int nOrderBy;
99698
 
  double rCost;
99699
 
 
99700
 
  /* Make sure wsFlags is initialized to some sane value. Otherwise, if the 
99701
 
  ** malloc in allocateIndexInfo() fails and this function returns leaving
99702
 
  ** wsFlags in an uninitialized state, the caller may behave unpredictably.
99703
 
  */
99704
 
  memset(pCost, 0, sizeof(*pCost));
99705
 
  pCost->plan.wsFlags = WHERE_VIRTUALTABLE;
99706
 
 
99707
 
  /* If the sqlite3_index_info structure has not been previously
99708
 
  ** allocated and initialized, then allocate and initialize it now.
99709
 
  */
99710
 
  pIdxInfo = *ppIdxInfo;
99711
 
  if( pIdxInfo==0 ){
99712
 
    *ppIdxInfo = pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pOrderBy);
99713
 
  }
99714
 
  if( pIdxInfo==0 ){
99715
 
    return;
99716
 
  }
99717
 
 
99718
 
  /* At this point, the sqlite3_index_info structure that pIdxInfo points
99719
 
  ** to will have been initialized, either during the current invocation or
99720
 
  ** during some prior invocation.  Now we just have to customize the
99721
 
  ** details of pIdxInfo for the current invocation and pass it to
99722
 
  ** xBestIndex.
99723
 
  */
99724
 
 
99725
 
  /* The module name must be defined. Also, by this point there must
99726
 
  ** be a pointer to an sqlite3_vtab structure. Otherwise
99727
 
  ** sqlite3ViewGetColumnNames() would have picked up the error. 
99728
 
  */
99729
 
  assert( pTab->azModuleArg && pTab->azModuleArg[0] );
99730
 
  assert( sqlite3GetVTable(pParse->db, pTab) );
99731
 
 
99732
 
  /* Set the aConstraint[].usable fields and initialize all 
99733
 
  ** output variables to zero.
99734
 
  **
99735
 
  ** aConstraint[].usable is true for constraints where the right-hand
99736
 
  ** side contains only references to tables to the left of the current
99737
 
  ** table.  In other words, if the constraint is of the form:
99738
 
  **
99739
 
  **           column = expr
99740
 
  **
99741
 
  ** and we are evaluating a join, then the constraint on column is 
99742
 
  ** only valid if all tables referenced in expr occur to the left
99743
 
  ** of the table containing column.
99744
 
  **
99745
 
  ** The aConstraints[] array contains entries for all constraints
99746
 
  ** on the current table.  That way we only have to compute it once
99747
 
  ** even though we might try to pick the best index multiple times.
99748
 
  ** For each attempt at picking an index, the order of tables in the
99749
 
  ** join might be different so we have to recompute the usable flag
99750
 
  ** each time.
99751
 
  */
99752
 
  pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
99753
 
  pUsage = pIdxInfo->aConstraintUsage;
99754
 
  for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
99755
 
    j = pIdxCons->iTermOffset;
99756
 
    pTerm = &pWC->a[j];
99757
 
    pIdxCons->usable = (pTerm->prereqRight&notReady) ? 0 : 1;
99758
 
  }
99759
 
  memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
99760
 
  if( pIdxInfo->needToFreeIdxStr ){
99761
 
    sqlite3_free(pIdxInfo->idxStr);
99762
 
  }
99763
 
  pIdxInfo->idxStr = 0;
99764
 
  pIdxInfo->idxNum = 0;
99765
 
  pIdxInfo->needToFreeIdxStr = 0;
99766
 
  pIdxInfo->orderByConsumed = 0;
99767
 
  /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
99768
 
  pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
99769
 
  nOrderBy = pIdxInfo->nOrderBy;
99770
 
  if( !pOrderBy ){
99771
 
    pIdxInfo->nOrderBy = 0;
99772
 
  }
99773
 
 
99774
 
  if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
99775
 
    return;
99776
 
  }
99777
 
 
99778
 
  pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
99779
 
  for(i=0; i<pIdxInfo->nConstraint; i++){
99780
 
    if( pUsage[i].argvIndex>0 ){
99781
 
      pCost->used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
99782
 
    }
99783
 
  }
99784
 
 
99785
 
  /* If there is an ORDER BY clause, and the selected virtual table index
99786
 
  ** does not satisfy it, increase the cost of the scan accordingly. This
99787
 
  ** matches the processing for non-virtual tables in bestBtreeIndex().
99788
 
  */
99789
 
  rCost = pIdxInfo->estimatedCost;
99790
 
  if( pOrderBy && pIdxInfo->orderByConsumed==0 ){
99791
 
    rCost += estLog(rCost)*rCost;
99792
 
  }
99793
 
 
99794
 
  /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
99795
 
  ** inital value of lowestCost in this loop. If it is, then the
99796
 
  ** (cost<lowestCost) test below will never be true.
99797
 
  ** 
99798
 
  ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT 
99799
 
  ** is defined.
99800
 
  */
99801
 
  if( (SQLITE_BIG_DBL/((double)2))<rCost ){
99802
 
    pCost->rCost = (SQLITE_BIG_DBL/((double)2));
99803
 
  }else{
99804
 
    pCost->rCost = rCost;
99805
 
  }
99806
 
  pCost->plan.u.pVtabIdx = pIdxInfo;
99807
 
  if( pIdxInfo->orderByConsumed ){
99808
 
    pCost->plan.wsFlags |= WHERE_ORDERBY;
99809
 
  }
99810
 
  pCost->plan.nEq = 0;
99811
 
  pIdxInfo->nOrderBy = nOrderBy;
99812
 
 
99813
 
  /* Try to find a more efficient access pattern by using multiple indexes
99814
 
  ** to optimize an OR expression within the WHERE clause. 
99815
 
  */
99816
 
  bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
99817
 
}
99818
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
99819
 
 
99820
 
/*
99821
 
** Argument pIdx is a pointer to an index structure that has an array of
99822
 
** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
99823
 
** stored in Index.aSample. These samples divide the domain of values stored
99824
 
** the index into (SQLITE_INDEX_SAMPLES+1) regions.
99825
 
** Region 0 contains all values less than the first sample value. Region
99826
 
** 1 contains values between the first and second samples.  Region 2 contains
99827
 
** values between samples 2 and 3.  And so on.  Region SQLITE_INDEX_SAMPLES
99828
 
** contains values larger than the last sample.
99829
 
**
99830
 
** If the index contains many duplicates of a single value, then it is
99831
 
** possible that two or more adjacent samples can hold the same value.
99832
 
** When that is the case, the smallest possible region code is returned
99833
 
** when roundUp is false and the largest possible region code is returned
99834
 
** when roundUp is true.
99835
 
**
99836
 
** If successful, this function determines which of the regions value 
99837
 
** pVal lies in, sets *piRegion to the region index (a value between 0
99838
 
** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
99839
 
** Or, if an OOM occurs while converting text values between encodings,
99840
 
** SQLITE_NOMEM is returned and *piRegion is undefined.
99841
 
*/
99842
 
#ifdef SQLITE_ENABLE_STAT2
99843
 
static int whereRangeRegion(
99844
 
  Parse *pParse,              /* Database connection */
99845
 
  Index *pIdx,                /* Index to consider domain of */
99846
 
  sqlite3_value *pVal,        /* Value to consider */
99847
 
  int roundUp,                /* Return largest valid region if true */
99848
 
  int *piRegion               /* OUT: Region of domain in which value lies */
99849
 
){
99850
 
  assert( roundUp==0 || roundUp==1 );
99851
 
  if( ALWAYS(pVal) ){
99852
 
    IndexSample *aSample = pIdx->aSample;
99853
 
    int i = 0;
99854
 
    int eType = sqlite3_value_type(pVal);
99855
 
 
99856
 
    if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
99857
 
      double r = sqlite3_value_double(pVal);
99858
 
      for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
99859
 
        if( aSample[i].eType==SQLITE_NULL ) continue;
99860
 
        if( aSample[i].eType>=SQLITE_TEXT ) break;
99861
 
        if( roundUp ){
99862
 
          if( aSample[i].u.r>r ) break;
99863
 
        }else{
99864
 
          if( aSample[i].u.r>=r ) break;
99865
 
        }
99866
 
      }
99867
 
    }else if( eType==SQLITE_NULL ){
99868
 
      i = 0;
99869
 
      if( roundUp ){
99870
 
        while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++;
99871
 
      }
99872
 
    }else{ 
99873
 
      sqlite3 *db = pParse->db;
99874
 
      CollSeq *pColl;
99875
 
      const u8 *z;
99876
 
      int n;
99877
 
 
99878
 
      /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */
99879
 
      assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
99880
 
 
99881
 
      if( eType==SQLITE_BLOB ){
99882
 
        z = (const u8 *)sqlite3_value_blob(pVal);
99883
 
        pColl = db->pDfltColl;
99884
 
        assert( pColl->enc==SQLITE_UTF8 );
99885
 
      }else{
99886
 
        pColl = sqlite3GetCollSeq(db, SQLITE_UTF8, 0, *pIdx->azColl);
99887
 
        if( pColl==0 ){
99888
 
          sqlite3ErrorMsg(pParse, "no such collation sequence: %s",
99889
 
                          *pIdx->azColl);
99890
 
          return SQLITE_ERROR;
99891
 
        }
99892
 
        z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
99893
 
        if( !z ){
99894
 
          return SQLITE_NOMEM;
99895
 
        }
99896
 
        assert( z && pColl && pColl->xCmp );
99897
 
      }
99898
 
      n = sqlite3ValueBytes(pVal, pColl->enc);
99899
 
 
99900
 
      for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
99901
 
        int c;
99902
 
        int eSampletype = aSample[i].eType;
99903
 
        if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
99904
 
        if( (eSampletype!=eType) ) break;
99905
 
#ifndef SQLITE_OMIT_UTF16
99906
 
        if( pColl->enc!=SQLITE_UTF8 ){
99907
 
          int nSample;
99908
 
          char *zSample = sqlite3Utf8to16(
99909
 
              db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
99910
 
          );
99911
 
          if( !zSample ){
99912
 
            assert( db->mallocFailed );
99913
 
            return SQLITE_NOMEM;
99914
 
          }
99915
 
          c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
99916
 
          sqlite3DbFree(db, zSample);
99917
 
        }else
99918
 
#endif
99919
 
        {
99920
 
          c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
99921
 
        }
99922
 
        if( c-roundUp>=0 ) break;
99923
 
      }
99924
 
    }
99925
 
 
99926
 
    assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
99927
 
    *piRegion = i;
99928
 
  }
99929
 
  return SQLITE_OK;
99930
 
}
99931
 
#endif   /* #ifdef SQLITE_ENABLE_STAT2 */
99932
 
 
99933
 
/*
99934
 
** If expression pExpr represents a literal value, set *pp to point to
99935
 
** an sqlite3_value structure containing the same value, with affinity
99936
 
** aff applied to it, before returning. It is the responsibility of the 
99937
 
** caller to eventually release this structure by passing it to 
99938
 
** sqlite3ValueFree().
99939
 
**
99940
 
** If the current parse is a recompile (sqlite3Reprepare()) and pExpr
99941
 
** is an SQL variable that currently has a non-NULL value bound to it,
99942
 
** create an sqlite3_value structure containing this value, again with
99943
 
** affinity aff applied to it, instead.
99944
 
**
99945
 
** If neither of the above apply, set *pp to NULL.
99946
 
**
99947
 
** If an error occurs, return an error code. Otherwise, SQLITE_OK.
99948
 
*/
99949
 
#ifdef SQLITE_ENABLE_STAT2
99950
 
static int valueFromExpr(
99951
 
  Parse *pParse, 
99952
 
  Expr *pExpr, 
99953
 
  u8 aff, 
99954
 
  sqlite3_value **pp
99955
 
){
99956
 
  if( pExpr->op==TK_VARIABLE
99957
 
   || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
99958
 
  ){
99959
 
    int iVar = pExpr->iColumn;
99960
 
    sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-23257-02778 */
99961
 
    *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff);
99962
 
    return SQLITE_OK;
99963
 
  }
99964
 
  return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
99965
 
}
99966
 
#endif
99967
 
 
99968
 
/*
99969
 
** This function is used to estimate the number of rows that will be visited
99970
 
** by scanning an index for a range of values. The range may have an upper
99971
 
** bound, a lower bound, or both. The WHERE clause terms that set the upper
99972
 
** and lower bounds are represented by pLower and pUpper respectively. For
99973
 
** example, assuming that index p is on t1(a):
99974
 
**
99975
 
**   ... FROM t1 WHERE a > ? AND a < ? ...
99976
 
**                    |_____|   |_____|
99977
 
**                       |         |
99978
 
**                     pLower    pUpper
99979
 
**
99980
 
** If either of the upper or lower bound is not present, then NULL is passed in
99981
 
** place of the corresponding WhereTerm.
99982
 
**
99983
 
** The nEq parameter is passed the index of the index column subject to the
99984
 
** range constraint. Or, equivalently, the number of equality constraints
99985
 
** optimized by the proposed index scan. For example, assuming index p is
99986
 
** on t1(a, b), and the SQL query is:
99987
 
**
99988
 
**   ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
99989
 
**
99990
 
** then nEq should be passed the value 1 (as the range restricted column,
99991
 
** b, is the second left-most column of the index). Or, if the query is:
99992
 
**
99993
 
**   ... FROM t1 WHERE a > ? AND a < ? ...
99994
 
**
99995
 
** then nEq should be passed 0.
99996
 
**
99997
 
** The returned value is an integer between 1 and 100, inclusive. A return
99998
 
** value of 1 indicates that the proposed range scan is expected to visit
99999
 
** approximately 1/100th (1%) of the rows selected by the nEq equality
100000
 
** constraints (if any). A return value of 100 indicates that it is expected
100001
 
** that the range scan will visit every row (100%) selected by the equality
100002
 
** constraints.
100003
 
**
100004
 
** In the absence of sqlite_stat2 ANALYZE data, each range inequality
100005
 
** reduces the search space by 3/4ths.  Hence a single constraint (x>?)
100006
 
** results in a return of 25 and a range constraint (x>? AND x<?) results
100007
 
** in a return of 6.
100008
 
*/
100009
 
static int whereRangeScanEst(
100010
 
  Parse *pParse,       /* Parsing & code generating context */
100011
 
  Index *p,            /* The index containing the range-compared column; "x" */
100012
 
  int nEq,             /* index into p->aCol[] of the range-compared column */
100013
 
  WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
100014
 
  WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
100015
 
  int *piEst           /* OUT: Return value */
100016
 
){
100017
 
  int rc = SQLITE_OK;
100018
 
 
100019
 
#ifdef SQLITE_ENABLE_STAT2
100020
 
 
100021
 
  if( nEq==0 && p->aSample ){
100022
 
    sqlite3_value *pLowerVal = 0;
100023
 
    sqlite3_value *pUpperVal = 0;
100024
 
    int iEst;
100025
 
    int iLower = 0;
100026
 
    int iUpper = SQLITE_INDEX_SAMPLES;
100027
 
    int roundUpUpper = 0;
100028
 
    int roundUpLower = 0;
100029
 
    u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
100030
 
 
100031
 
    if( pLower ){
100032
 
      Expr *pExpr = pLower->pExpr->pRight;
100033
 
      rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
100034
 
      assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
100035
 
      roundUpLower = (pLower->eOperator==WO_GT) ?1:0;
100036
 
    }
100037
 
    if( rc==SQLITE_OK && pUpper ){
100038
 
      Expr *pExpr = pUpper->pExpr->pRight;
100039
 
      rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
100040
 
      assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
100041
 
      roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0;
100042
 
    }
100043
 
 
100044
 
    if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
100045
 
      sqlite3ValueFree(pLowerVal);
100046
 
      sqlite3ValueFree(pUpperVal);
100047
 
      goto range_est_fallback;
100048
 
    }else if( pLowerVal==0 ){
100049
 
      rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
100050
 
      if( pLower ) iLower = iUpper/2;
100051
 
    }else if( pUpperVal==0 ){
100052
 
      rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
100053
 
      if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
100054
 
    }else{
100055
 
      rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
100056
 
      if( rc==SQLITE_OK ){
100057
 
        rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
100058
 
      }
100059
 
    }
100060
 
    WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper));
100061
 
 
100062
 
    iEst = iUpper - iLower;
100063
 
    testcase( iEst==SQLITE_INDEX_SAMPLES );
100064
 
    assert( iEst<=SQLITE_INDEX_SAMPLES );
100065
 
    if( iEst<1 ){
100066
 
      *piEst = 50/SQLITE_INDEX_SAMPLES;
100067
 
    }else{
100068
 
      *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES;
100069
 
    }
100070
 
    sqlite3ValueFree(pLowerVal);
100071
 
    sqlite3ValueFree(pUpperVal);
100072
 
    return rc;
100073
 
  }
100074
 
range_est_fallback:
100075
 
#else
100076
 
  UNUSED_PARAMETER(pParse);
100077
 
  UNUSED_PARAMETER(p);
100078
 
  UNUSED_PARAMETER(nEq);
100079
 
#endif
100080
 
  assert( pLower || pUpper );
100081
 
  *piEst = 100;
100082
 
  if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4;
100083
 
  if( pUpper ) *piEst /= 4;
100084
 
  return rc;
100085
 
}
100086
 
 
100087
 
#ifdef SQLITE_ENABLE_STAT2
100088
 
/*
100089
 
** Estimate the number of rows that will be returned based on
100090
 
** an equality constraint x=VALUE and where that VALUE occurs in
100091
 
** the histogram data.  This only works when x is the left-most
100092
 
** column of an index and sqlite_stat2 histogram data is available
100093
 
** for that index.  When pExpr==NULL that means the constraint is
100094
 
** "x IS NULL" instead of "x=VALUE".
100095
 
**
100096
 
** Write the estimated row count into *pnRow and return SQLITE_OK. 
100097
 
** If unable to make an estimate, leave *pnRow unchanged and return
100098
 
** non-zero.
100099
 
**
100100
 
** This routine can fail if it is unable to load a collating sequence
100101
 
** required for string comparison, or if unable to allocate memory
100102
 
** for a UTF conversion required for comparison.  The error is stored
100103
 
** in the pParse structure.
100104
 
*/
100105
 
static int whereEqualScanEst(
100106
 
  Parse *pParse,       /* Parsing & code generating context */
100107
 
  Index *p,            /* The index whose left-most column is pTerm */
100108
 
  Expr *pExpr,         /* Expression for VALUE in the x=VALUE constraint */
100109
 
  double *pnRow        /* Write the revised row estimate here */
100110
 
){
100111
 
  sqlite3_value *pRhs = 0;  /* VALUE on right-hand side of pTerm */
100112
 
  int iLower, iUpper;       /* Range of histogram regions containing pRhs */
100113
 
  u8 aff;                   /* Column affinity */
100114
 
  int rc;                   /* Subfunction return code */
100115
 
  double nRowEst;           /* New estimate of the number of rows */
100116
 
 
100117
 
  assert( p->aSample!=0 );
100118
 
  aff = p->pTable->aCol[p->aiColumn[0]].affinity;
100119
 
  if( pExpr ){
100120
 
    rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
100121
 
    if( rc ) goto whereEqualScanEst_cancel;
100122
 
  }else{
100123
 
    pRhs = sqlite3ValueNew(pParse->db);
100124
 
  }
100125
 
  if( pRhs==0 ) return SQLITE_NOTFOUND;
100126
 
  rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower);
100127
 
  if( rc ) goto whereEqualScanEst_cancel;
100128
 
  rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper);
100129
 
  if( rc ) goto whereEqualScanEst_cancel;
100130
 
  WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper));
100131
 
  if( iLower>=iUpper ){
100132
 
    nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2);
100133
 
    if( nRowEst<*pnRow ) *pnRow = nRowEst;
100134
 
  }else{
100135
 
    nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES;
100136
 
    *pnRow = nRowEst;
100137
 
  }
100138
 
 
100139
 
whereEqualScanEst_cancel:
100140
 
  sqlite3ValueFree(pRhs);
100141
 
  return rc;
100142
 
}
100143
 
#endif /* defined(SQLITE_ENABLE_STAT2) */
100144
 
 
100145
 
#ifdef SQLITE_ENABLE_STAT2
100146
 
/*
100147
 
** Estimate the number of rows that will be returned based on
100148
 
** an IN constraint where the right-hand side of the IN operator
100149
 
** is a list of values.  Example:
100150
 
**
100151
 
**        WHERE x IN (1,2,3,4)
100152
 
**
100153
 
** Write the estimated row count into *pnRow and return SQLITE_OK. 
100154
 
** If unable to make an estimate, leave *pnRow unchanged and return
100155
 
** non-zero.
100156
 
**
100157
 
** This routine can fail if it is unable to load a collating sequence
100158
 
** required for string comparison, or if unable to allocate memory
100159
 
** for a UTF conversion required for comparison.  The error is stored
100160
 
** in the pParse structure.
100161
 
*/
100162
 
static int whereInScanEst(
100163
 
  Parse *pParse,       /* Parsing & code generating context */
100164
 
  Index *p,            /* The index whose left-most column is pTerm */
100165
 
  ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
100166
 
  double *pnRow        /* Write the revised row estimate here */
100167
 
){
100168
 
  sqlite3_value *pVal = 0;  /* One value from list */
100169
 
  int iLower, iUpper;       /* Range of histogram regions containing pRhs */
100170
 
  u8 aff;                   /* Column affinity */
100171
 
  int rc = SQLITE_OK;       /* Subfunction return code */
100172
 
  double nRowEst;           /* New estimate of the number of rows */
100173
 
  int nSpan = 0;            /* Number of histogram regions spanned */
100174
 
  int nSingle = 0;          /* Histogram regions hit by a single value */
100175
 
  int nNotFound = 0;        /* Count of values that are not constants */
100176
 
  int i;                               /* Loop counter */
100177
 
  u8 aSpan[SQLITE_INDEX_SAMPLES+1];    /* Histogram regions that are spanned */
100178
 
  u8 aSingle[SQLITE_INDEX_SAMPLES+1];  /* Histogram regions hit once */
100179
 
 
100180
 
  assert( p->aSample!=0 );
100181
 
  aff = p->pTable->aCol[p->aiColumn[0]].affinity;
100182
 
  memset(aSpan, 0, sizeof(aSpan));
100183
 
  memset(aSingle, 0, sizeof(aSingle));
100184
 
  for(i=0; i<pList->nExpr; i++){
100185
 
    sqlite3ValueFree(pVal);
100186
 
    rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal);
100187
 
    if( rc ) break;
100188
 
    if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
100189
 
      nNotFound++;
100190
 
      continue;
100191
 
    }
100192
 
    rc = whereRangeRegion(pParse, p, pVal, 0, &iLower);
100193
 
    if( rc ) break;
100194
 
    rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper);
100195
 
    if( rc ) break;
100196
 
    if( iLower>=iUpper ){
100197
 
      aSingle[iLower] = 1;
100198
 
    }else{
100199
 
      assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES );
100200
 
      while( iLower<iUpper ) aSpan[iLower++] = 1;
100201
 
    }
100202
 
  }
100203
 
  if( rc==SQLITE_OK ){
100204
 
    for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){
100205
 
      if( aSpan[i] ){
100206
 
        nSpan++;
100207
 
      }else if( aSingle[i] ){
100208
 
        nSingle++;
100209
 
      }
100210
 
    }
100211
 
    nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES)
100212
 
               + nNotFound*p->aiRowEst[1];
100213
 
    if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
100214
 
    *pnRow = nRowEst;
100215
 
    WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n",
100216
 
                 nSpan, nSingle, nNotFound, nRowEst));
100217
 
  }
100218
 
  sqlite3ValueFree(pVal);
100219
 
  return rc;
100220
 
}
100221
 
#endif /* defined(SQLITE_ENABLE_STAT2) */
100222
 
 
100223
 
 
100224
 
/*
100225
 
** Find the best query plan for accessing a particular table.  Write the
100226
 
** best query plan and its cost into the WhereCost object supplied as the
100227
 
** last parameter.
100228
 
**
100229
 
** The lowest cost plan wins.  The cost is an estimate of the amount of
100230
 
** CPU and disk I/O needed to process the requested result.
100231
 
** Factors that influence cost include:
100232
 
**
100233
 
**    *  The estimated number of rows that will be retrieved.  (The
100234
 
**       fewer the better.)
100235
 
**
100236
 
**    *  Whether or not sorting must occur.
100237
 
**
100238
 
**    *  Whether or not there must be separate lookups in the
100239
 
**       index and in the main table.
100240
 
**
100241
 
** If there was an INDEXED BY clause (pSrc->pIndex) attached to the table in
100242
 
** the SQL statement, then this function only considers plans using the 
100243
 
** named index. If no such plan is found, then the returned cost is
100244
 
** SQLITE_BIG_DBL. If a plan is found that uses the named index, 
100245
 
** then the cost is calculated in the usual way.
100246
 
**
100247
 
** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table 
100248
 
** in the SELECT statement, then no indexes are considered. However, the 
100249
 
** selected plan may still take advantage of the built-in rowid primary key
100250
 
** index.
100251
 
*/
100252
 
static void bestBtreeIndex(
100253
 
  Parse *pParse,              /* The parsing context */
100254
 
  WhereClause *pWC,           /* The WHERE clause */
100255
 
  struct SrcList_item *pSrc,  /* The FROM clause term to search */
100256
 
  Bitmask notReady,           /* Mask of cursors not available for indexing */
100257
 
  Bitmask notValid,           /* Cursors not available for any purpose */
100258
 
  ExprList *pOrderBy,         /* The ORDER BY clause */
100259
 
  WhereCost *pCost            /* Lowest cost query plan */
100260
 
){
100261
 
  int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
100262
 
  Index *pProbe;              /* An index we are evaluating */
100263
 
  Index *pIdx;                /* Copy of pProbe, or zero for IPK index */
100264
 
  int eqTermMask;             /* Current mask of valid equality operators */
100265
 
  int idxEqTermMask;          /* Index mask of valid equality operators */
100266
 
  Index sPk;                  /* A fake index object for the primary key */
100267
 
  unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
100268
 
  int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
100269
 
  int wsFlagMask;             /* Allowed flags in pCost->plan.wsFlag */
100270
 
 
100271
 
  /* Initialize the cost to a worst-case value */
100272
 
  memset(pCost, 0, sizeof(*pCost));
100273
 
  pCost->rCost = SQLITE_BIG_DBL;
100274
 
 
100275
 
  /* If the pSrc table is the right table of a LEFT JOIN then we may not
100276
 
  ** use an index to satisfy IS NULL constraints on that table.  This is
100277
 
  ** because columns might end up being NULL if the table does not match -
100278
 
  ** a circumstance which the index cannot help us discover.  Ticket #2177.
100279
 
  */
100280
 
  if( pSrc->jointype & JT_LEFT ){
100281
 
    idxEqTermMask = WO_EQ|WO_IN;
100282
 
  }else{
100283
 
    idxEqTermMask = WO_EQ|WO_IN|WO_ISNULL;
100284
 
  }
100285
 
 
100286
 
  if( pSrc->pIndex ){
100287
 
    /* An INDEXED BY clause specifies a particular index to use */
100288
 
    pIdx = pProbe = pSrc->pIndex;
100289
 
    wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
100290
 
    eqTermMask = idxEqTermMask;
100291
 
  }else{
100292
 
    /* There is no INDEXED BY clause.  Create a fake Index object in local
100293
 
    ** variable sPk to represent the rowid primary key index.  Make this
100294
 
    ** fake index the first in a chain of Index objects with all of the real
100295
 
    ** indices to follow */
100296
 
    Index *pFirst;                  /* First of real indices on the table */
100297
 
    memset(&sPk, 0, sizeof(Index));
100298
 
    sPk.nColumn = 1;
100299
 
    sPk.aiColumn = &aiColumnPk;
100300
 
    sPk.aiRowEst = aiRowEstPk;
100301
 
    sPk.onError = OE_Replace;
100302
 
    sPk.pTable = pSrc->pTab;
100303
 
    aiRowEstPk[0] = pSrc->pTab->nRowEst;
100304
 
    aiRowEstPk[1] = 1;
100305
 
    pFirst = pSrc->pTab->pIndex;
100306
 
    if( pSrc->notIndexed==0 ){
100307
 
      /* The real indices of the table are only considered if the
100308
 
      ** NOT INDEXED qualifier is omitted from the FROM clause */
100309
 
      sPk.pNext = pFirst;
100310
 
    }
100311
 
    pProbe = &sPk;
100312
 
    wsFlagMask = ~(
100313
 
        WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE
100314
 
    );
100315
 
    eqTermMask = WO_EQ|WO_IN;
100316
 
    pIdx = 0;
100317
 
  }
100318
 
 
100319
 
  /* Loop over all indices looking for the best one to use
100320
 
  */
100321
 
  for(; pProbe; pIdx=pProbe=pProbe->pNext){
100322
 
    const unsigned int * const aiRowEst = pProbe->aiRowEst;
100323
 
    double cost;                /* Cost of using pProbe */
100324
 
    double nRow;                /* Estimated number of rows in result set */
100325
 
    double log10N;              /* base-10 logarithm of nRow (inexact) */
100326
 
    int rev;                    /* True to scan in reverse order */
100327
 
    int wsFlags = 0;
100328
 
    Bitmask used = 0;
100329
 
 
100330
 
    /* The following variables are populated based on the properties of
100331
 
    ** index being evaluated. They are then used to determine the expected
100332
 
    ** cost and number of rows returned.
100333
 
    **
100334
 
    **  nEq: 
100335
 
    **    Number of equality terms that can be implemented using the index.
100336
 
    **    In other words, the number of initial fields in the index that
100337
 
    **    are used in == or IN or NOT NULL constraints of the WHERE clause.
100338
 
    **
100339
 
    **  nInMul:  
100340
 
    **    The "in-multiplier". This is an estimate of how many seek operations 
100341
 
    **    SQLite must perform on the index in question. For example, if the 
100342
 
    **    WHERE clause is:
100343
 
    **
100344
 
    **      WHERE a IN (1, 2, 3) AND b IN (4, 5, 6)
100345
 
    **
100346
 
    **    SQLite must perform 9 lookups on an index on (a, b), so nInMul is 
100347
 
    **    set to 9. Given the same schema and either of the following WHERE 
100348
 
    **    clauses:
100349
 
    **
100350
 
    **      WHERE a =  1
100351
 
    **      WHERE a >= 2
100352
 
    **
100353
 
    **    nInMul is set to 1.
100354
 
    **
100355
 
    **    If there exists a WHERE term of the form "x IN (SELECT ...)", then 
100356
 
    **    the sub-select is assumed to return 25 rows for the purposes of 
100357
 
    **    determining nInMul.
100358
 
    **
100359
 
    **  bInEst:  
100360
 
    **    Set to true if there was at least one "x IN (SELECT ...)" term used 
100361
 
    **    in determining the value of nInMul.  Note that the RHS of the
100362
 
    **    IN operator must be a SELECT, not a value list, for this variable
100363
 
    **    to be true.
100364
 
    **
100365
 
    **  estBound:
100366
 
    **    An estimate on the amount of the table that must be searched.  A
100367
 
    **    value of 100 means the entire table is searched.  Range constraints
100368
 
    **    might reduce this to a value less than 100 to indicate that only
100369
 
    **    a fraction of the table needs searching.  In the absence of
100370
 
    **    sqlite_stat2 ANALYZE data, a single inequality reduces the search
100371
 
    **    space to 1/4rd its original size.  So an x>? constraint reduces
100372
 
    **    estBound to 25.  Two constraints (x>? AND x<?) reduce estBound to 6.
100373
 
    **
100374
 
    **  bSort:   
100375
 
    **    Boolean. True if there is an ORDER BY clause that will require an 
100376
 
    **    external sort (i.e. scanning the index being evaluated will not 
100377
 
    **    correctly order records).
100378
 
    **
100379
 
    **  bLookup: 
100380
 
    **    Boolean. True if a table lookup is required for each index entry
100381
 
    **    visited.  In other words, true if this is not a covering index.
100382
 
    **    This is always false for the rowid primary key index of a table.
100383
 
    **    For other indexes, it is true unless all the columns of the table
100384
 
    **    used by the SELECT statement are present in the index (such an
100385
 
    **    index is sometimes described as a covering index).
100386
 
    **    For example, given the index on (a, b), the second of the following 
100387
 
    **    two queries requires table b-tree lookups in order to find the value
100388
 
    **    of column c, but the first does not because columns a and b are
100389
 
    **    both available in the index.
100390
 
    **
100391
 
    **             SELECT a, b    FROM tbl WHERE a = 1;
100392
 
    **             SELECT a, b, c FROM tbl WHERE a = 1;
100393
 
    */
100394
 
    int nEq;                      /* Number of == or IN terms matching index */
100395
 
    int bInEst = 0;               /* True if "x IN (SELECT...)" seen */
100396
 
    int nInMul = 1;               /* Number of distinct equalities to lookup */
100397
 
    int estBound = 100;           /* Estimated reduction in search space */
100398
 
    int nBound = 0;               /* Number of range constraints seen */
100399
 
    int bSort = 0;                /* True if external sort required */
100400
 
    int bLookup = 0;              /* True if not a covering index */
100401
 
    WhereTerm *pTerm;             /* A single term of the WHERE clause */
100402
 
#ifdef SQLITE_ENABLE_STAT2
100403
 
    WhereTerm *pFirstTerm = 0;    /* First term matching the index */
100404
 
#endif
100405
 
 
100406
 
    /* Determine the values of nEq and nInMul */
100407
 
    for(nEq=0; nEq<pProbe->nColumn; nEq++){
100408
 
      int j = pProbe->aiColumn[nEq];
100409
 
      pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
100410
 
      if( pTerm==0 ) break;
100411
 
      wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
100412
 
      if( pTerm->eOperator & WO_IN ){
100413
 
        Expr *pExpr = pTerm->pExpr;
100414
 
        wsFlags |= WHERE_COLUMN_IN;
100415
 
        if( ExprHasProperty(pExpr, EP_xIsSelect) ){
100416
 
          /* "x IN (SELECT ...)":  Assume the SELECT returns 25 rows */
100417
 
          nInMul *= 25;
100418
 
          bInEst = 1;
100419
 
        }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
100420
 
          /* "x IN (value, value, ...)" */
100421
 
          nInMul *= pExpr->x.pList->nExpr;
100422
 
        }
100423
 
      }else if( pTerm->eOperator & WO_ISNULL ){
100424
 
        wsFlags |= WHERE_COLUMN_NULL;
100425
 
      }
100426
 
#ifdef SQLITE_ENABLE_STAT2
100427
 
      if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
100428
 
#endif
100429
 
      used |= pTerm->prereqRight;
100430
 
    }
100431
 
 
100432
 
    /* Determine the value of estBound. */
100433
 
    if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
100434
 
      int j = pProbe->aiColumn[nEq];
100435
 
      if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
100436
 
        WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
100437
 
        WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
100438
 
        whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound);
100439
 
        if( pTop ){
100440
 
          nBound = 1;
100441
 
          wsFlags |= WHERE_TOP_LIMIT;
100442
 
          used |= pTop->prereqRight;
100443
 
        }
100444
 
        if( pBtm ){
100445
 
          nBound++;
100446
 
          wsFlags |= WHERE_BTM_LIMIT;
100447
 
          used |= pBtm->prereqRight;
100448
 
        }
100449
 
        wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
100450
 
      }
100451
 
    }else if( pProbe->onError!=OE_None ){
100452
 
      testcase( wsFlags & WHERE_COLUMN_IN );
100453
 
      testcase( wsFlags & WHERE_COLUMN_NULL );
100454
 
      if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
100455
 
        wsFlags |= WHERE_UNIQUE;
100456
 
      }
100457
 
    }
100458
 
 
100459
 
    /* If there is an ORDER BY clause and the index being considered will
100460
 
    ** naturally scan rows in the required order, set the appropriate flags
100461
 
    ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
100462
 
    ** will scan rows in a different order, set the bSort variable.  */
100463
 
    if( pOrderBy ){
100464
 
      if( (wsFlags & WHERE_COLUMN_IN)==0
100465
 
        && pProbe->bUnordered==0
100466
 
        && isSortingIndex(pParse, pWC->pMaskSet, pProbe, iCur, pOrderBy,
100467
 
                          nEq, wsFlags, &rev)
100468
 
      ){
100469
 
        wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
100470
 
        wsFlags |= (rev ? WHERE_REVERSE : 0);
100471
 
      }else{
100472
 
        bSort = 1;
100473
 
      }
100474
 
    }
100475
 
 
100476
 
    /* If currently calculating the cost of using an index (not the IPK
100477
 
    ** index), determine if all required column data may be obtained without 
100478
 
    ** using the main table (i.e. if the index is a covering
100479
 
    ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
100480
 
    ** wsFlags. Otherwise, set the bLookup variable to true.  */
100481
 
    if( pIdx && wsFlags ){
100482
 
      Bitmask m = pSrc->colUsed;
100483
 
      int j;
100484
 
      for(j=0; j<pIdx->nColumn; j++){
100485
 
        int x = pIdx->aiColumn[j];
100486
 
        if( x<BMS-1 ){
100487
 
          m &= ~(((Bitmask)1)<<x);
100488
 
        }
100489
 
      }
100490
 
      if( m==0 ){
100491
 
        wsFlags |= WHERE_IDX_ONLY;
100492
 
      }else{
100493
 
        bLookup = 1;
100494
 
      }
100495
 
    }
100496
 
 
100497
 
    /*
100498
 
    ** Estimate the number of rows of output.  For an "x IN (SELECT...)"
100499
 
    ** constraint, do not let the estimate exceed half the rows in the table.
100500
 
    */
100501
 
    nRow = (double)(aiRowEst[nEq] * nInMul);
100502
 
    if( bInEst && nRow*2>aiRowEst[0] ){
100503
 
      nRow = aiRowEst[0]/2;
100504
 
      nInMul = (int)(nRow / aiRowEst[nEq]);
100505
 
    }
100506
 
 
100507
 
#ifdef SQLITE_ENABLE_STAT2
100508
 
    /* If the constraint is of the form x=VALUE and histogram
100509
 
    ** data is available for column x, then it might be possible
100510
 
    ** to get a better estimate on the number of rows based on
100511
 
    ** VALUE and how common that value is according to the histogram.
100512
 
    */
100513
 
    if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 ){
100514
 
      if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
100515
 
        testcase( pFirstTerm->eOperator==WO_EQ );
100516
 
        testcase( pFirstTerm->eOperator==WO_ISNULL );
100517
 
        whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
100518
 
      }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
100519
 
        whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
100520
 
      }
100521
 
    }
100522
 
#endif /* SQLITE_ENABLE_STAT2 */
100523
 
 
100524
 
    /* Adjust the number of output rows and downward to reflect rows
100525
 
    ** that are excluded by range constraints.
100526
 
    */
100527
 
    nRow = (nRow * (double)estBound) / (double)100;
100528
 
    if( nRow<1 ) nRow = 1;
100529
 
 
100530
 
    /* Experiments run on real SQLite databases show that the time needed
100531
 
    ** to do a binary search to locate a row in a table or index is roughly
100532
 
    ** log10(N) times the time to move from one row to the next row within
100533
 
    ** a table or index.  The actual times can vary, with the size of
100534
 
    ** records being an important factor.  Both moves and searches are
100535
 
    ** slower with larger records, presumably because fewer records fit
100536
 
    ** on one page and hence more pages have to be fetched.
100537
 
    **
100538
 
    ** The ANALYZE command and the sqlite_stat1 and sqlite_stat2 tables do
100539
 
    ** not give us data on the relative sizes of table and index records.
100540
 
    ** So this computation assumes table records are about twice as big
100541
 
    ** as index records
100542
 
    */
100543
 
    if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
100544
 
      /* The cost of a full table scan is a number of move operations equal
100545
 
      ** to the number of rows in the table.
100546
 
      **
100547
 
      ** We add an additional 4x penalty to full table scans.  This causes
100548
 
      ** the cost function to err on the side of choosing an index over
100549
 
      ** choosing a full scan.  This 4x full-scan penalty is an arguable
100550
 
      ** decision and one which we expect to revisit in the future.  But
100551
 
      ** it seems to be working well enough at the moment.
100552
 
      */
100553
 
      cost = aiRowEst[0]*4;
100554
 
    }else{
100555
 
      log10N = estLog(aiRowEst[0]);
100556
 
      cost = nRow;
100557
 
      if( pIdx ){
100558
 
        if( bLookup ){
100559
 
          /* For an index lookup followed by a table lookup:
100560
 
          **    nInMul index searches to find the start of each index range
100561
 
          **  + nRow steps through the index
100562
 
          **  + nRow table searches to lookup the table entry using the rowid
100563
 
          */
100564
 
          cost += (nInMul + nRow)*log10N;
100565
 
        }else{
100566
 
          /* For a covering index:
100567
 
          **     nInMul index searches to find the initial entry 
100568
 
          **   + nRow steps through the index
100569
 
          */
100570
 
          cost += nInMul*log10N;
100571
 
        }
100572
 
      }else{
100573
 
        /* For a rowid primary key lookup:
100574
 
        **    nInMult table searches to find the initial entry for each range
100575
 
        **  + nRow steps through the table
100576
 
        */
100577
 
        cost += nInMul*log10N;
100578
 
      }
100579
 
    }
100580
 
 
100581
 
    /* Add in the estimated cost of sorting the result.  Actual experimental
100582
 
    ** measurements of sorting performance in SQLite show that sorting time
100583
 
    ** adds C*N*log10(N) to the cost, where N is the number of rows to be 
100584
 
    ** sorted and C is a factor between 1.95 and 4.3.  We will split the
100585
 
    ** difference and select C of 3.0.
100586
 
    */
100587
 
    if( bSort ){
100588
 
      cost += nRow*estLog(nRow)*3;
100589
 
    }
100590
 
 
100591
 
    /**** Cost of using this index has now been computed ****/
100592
 
 
100593
 
    /* If there are additional constraints on this table that cannot
100594
 
    ** be used with the current index, but which might lower the number
100595
 
    ** of output rows, adjust the nRow value accordingly.  This only 
100596
 
    ** matters if the current index is the least costly, so do not bother
100597
 
    ** with this step if we already know this index will not be chosen.
100598
 
    ** Also, never reduce the output row count below 2 using this step.
100599
 
    **
100600
 
    ** It is critical that the notValid mask be used here instead of
100601
 
    ** the notReady mask.  When computing an "optimal" index, the notReady
100602
 
    ** mask will only have one bit set - the bit for the current table.
100603
 
    ** The notValid mask, on the other hand, always has all bits set for
100604
 
    ** tables that are not in outer loops.  If notReady is used here instead
100605
 
    ** of notValid, then a optimal index that depends on inner joins loops
100606
 
    ** might be selected even when there exists an optimal index that has
100607
 
    ** no such dependency.
100608
 
    */
100609
 
    if( nRow>2 && cost<=pCost->rCost ){
100610
 
      int k;                       /* Loop counter */
100611
 
      int nSkipEq = nEq;           /* Number of == constraints to skip */
100612
 
      int nSkipRange = nBound;     /* Number of < constraints to skip */
100613
 
      Bitmask thisTab;             /* Bitmap for pSrc */
100614
 
 
100615
 
      thisTab = getMask(pWC->pMaskSet, iCur);
100616
 
      for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){
100617
 
        if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
100618
 
        if( (pTerm->prereqAll & notValid)!=thisTab ) continue;
100619
 
        if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
100620
 
          if( nSkipEq ){
100621
 
            /* Ignore the first nEq equality matches since the index
100622
 
            ** has already accounted for these */
100623
 
            nSkipEq--;
100624
 
          }else{
100625
 
            /* Assume each additional equality match reduces the result
100626
 
            ** set size by a factor of 10 */
100627
 
            nRow /= 10;
100628
 
          }
100629
 
        }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
100630
 
          if( nSkipRange ){
100631
 
            /* Ignore the first nSkipRange range constraints since the index
100632
 
            ** has already accounted for these */
100633
 
            nSkipRange--;
100634
 
          }else{
100635
 
            /* Assume each additional range constraint reduces the result
100636
 
            ** set size by a factor of 3.  Indexed range constraints reduce
100637
 
            ** the search space by a larger factor: 4.  We make indexed range
100638
 
            ** more selective intentionally because of the subjective 
100639
 
            ** observation that indexed range constraints really are more
100640
 
            ** selective in practice, on average. */
100641
 
            nRow /= 3;
100642
 
          }
100643
 
        }else if( pTerm->eOperator!=WO_NOOP ){
100644
 
          /* Any other expression lowers the output row count by half */
100645
 
          nRow /= 2;
100646
 
        }
100647
 
      }
100648
 
      if( nRow<2 ) nRow = 2;
100649
 
    }
100650
 
 
100651
 
 
100652
 
    WHERETRACE((
100653
 
      "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
100654
 
      "         notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
100655
 
      pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"), 
100656
 
      nEq, nInMul, estBound, bSort, bLookup, wsFlags,
100657
 
      notReady, log10N, nRow, cost, used
100658
 
    ));
100659
 
 
100660
 
    /* If this index is the best we have seen so far, then record this
100661
 
    ** index and its cost in the pCost structure.
100662
 
    */
100663
 
    if( (!pIdx || wsFlags)
100664
 
     && (cost<pCost->rCost || (cost<=pCost->rCost && nRow<pCost->plan.nRow))
100665
 
    ){
100666
 
      pCost->rCost = cost;
100667
 
      pCost->used = used;
100668
 
      pCost->plan.nRow = nRow;
100669
 
      pCost->plan.wsFlags = (wsFlags&wsFlagMask);
100670
 
      pCost->plan.nEq = nEq;
100671
 
      pCost->plan.u.pIdx = pIdx;
100672
 
    }
100673
 
 
100674
 
    /* If there was an INDEXED BY clause, then only that one index is
100675
 
    ** considered. */
100676
 
    if( pSrc->pIndex ) break;
100677
 
 
100678
 
    /* Reset masks for the next index in the loop */
100679
 
    wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
100680
 
    eqTermMask = idxEqTermMask;
100681
 
  }
100682
 
 
100683
 
  /* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
100684
 
  ** is set, then reverse the order that the index will be scanned
100685
 
  ** in. This is used for application testing, to help find cases
100686
 
  ** where application behaviour depends on the (undefined) order that
100687
 
  ** SQLite outputs rows in in the absence of an ORDER BY clause.  */
100688
 
  if( !pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
100689
 
    pCost->plan.wsFlags |= WHERE_REVERSE;
100690
 
  }
100691
 
 
100692
 
  assert( pOrderBy || (pCost->plan.wsFlags&WHERE_ORDERBY)==0 );
100693
 
  assert( pCost->plan.u.pIdx==0 || (pCost->plan.wsFlags&WHERE_ROWID_EQ)==0 );
100694
 
  assert( pSrc->pIndex==0 
100695
 
       || pCost->plan.u.pIdx==0 
100696
 
       || pCost->plan.u.pIdx==pSrc->pIndex 
100697
 
  );
100698
 
 
100699
 
  WHERETRACE(("best index is: %s\n", 
100700
 
    ((pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" : 
100701
 
         pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk")
100702
 
  ));
100703
 
  
100704
 
  bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
100705
 
  bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost);
100706
 
  pCost->plan.wsFlags |= eqTermMask;
100707
 
}
100708
 
 
100709
 
/*
100710
 
** Find the query plan for accessing table pSrc->pTab. Write the
100711
 
** best query plan and its cost into the WhereCost object supplied 
100712
 
** as the last parameter. This function may calculate the cost of
100713
 
** both real and virtual table scans.
100714
 
*/
100715
 
static void bestIndex(
100716
 
  Parse *pParse,              /* The parsing context */
100717
 
  WhereClause *pWC,           /* The WHERE clause */
100718
 
  struct SrcList_item *pSrc,  /* The FROM clause term to search */
100719
 
  Bitmask notReady,           /* Mask of cursors not available for indexing */
100720
 
  Bitmask notValid,           /* Cursors not available for any purpose */
100721
 
  ExprList *pOrderBy,         /* The ORDER BY clause */
100722
 
  WhereCost *pCost            /* Lowest cost query plan */
100723
 
){
100724
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
100725
 
  if( IsVirtual(pSrc->pTab) ){
100726
 
    sqlite3_index_info *p = 0;
100727
 
    bestVirtualIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost,&p);
100728
 
    if( p->needToFreeIdxStr ){
100729
 
      sqlite3_free(p->idxStr);
100730
 
    }
100731
 
    sqlite3DbFree(pParse->db, p);
100732
 
  }else
100733
 
#endif
100734
 
  {
100735
 
    bestBtreeIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
100736
 
  }
100737
 
}
100738
 
 
100739
 
/*
100740
 
** Disable a term in the WHERE clause.  Except, do not disable the term
100741
 
** if it controls a LEFT OUTER JOIN and it did not originate in the ON
100742
 
** or USING clause of that join.
100743
 
**
100744
 
** Consider the term t2.z='ok' in the following queries:
100745
 
**
100746
 
**   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
100747
 
**   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
100748
 
**   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
100749
 
**
100750
 
** The t2.z='ok' is disabled in the in (2) because it originates
100751
 
** in the ON clause.  The term is disabled in (3) because it is not part
100752
 
** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
100753
 
**
100754
 
** IMPLEMENTATION-OF: R-24597-58655 No tests are done for terms that are
100755
 
** completely satisfied by indices.
100756
 
**
100757
 
** Disabling a term causes that term to not be tested in the inner loop
100758
 
** of the join.  Disabling is an optimization.  When terms are satisfied
100759
 
** by indices, we disable them to prevent redundant tests in the inner
100760
 
** loop.  We would get the correct results if nothing were ever disabled,
100761
 
** but joins might run a little slower.  The trick is to disable as much
100762
 
** as we can without disabling too much.  If we disabled in (1), we'd get
100763
 
** the wrong answer.  See ticket #813.
100764
 
*/
100765
 
static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
100766
 
  if( pTerm
100767
 
      && (pTerm->wtFlags & TERM_CODED)==0
100768
 
      && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
100769
 
  ){
100770
 
    pTerm->wtFlags |= TERM_CODED;
100771
 
    if( pTerm->iParent>=0 ){
100772
 
      WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
100773
 
      if( (--pOther->nChild)==0 ){
100774
 
        disableTerm(pLevel, pOther);
100775
 
      }
100776
 
    }
100777
 
  }
100778
 
}
100779
 
 
100780
 
/*
100781
 
** Code an OP_Affinity opcode to apply the column affinity string zAff
100782
 
** to the n registers starting at base. 
100783
 
**
100784
 
** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
100785
 
** beginning and end of zAff are ignored.  If all entries in zAff are
100786
 
** SQLITE_AFF_NONE, then no code gets generated.
100787
 
**
100788
 
** This routine makes its own copy of zAff so that the caller is free
100789
 
** to modify zAff after this routine returns.
100790
 
*/
100791
 
static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
100792
 
  Vdbe *v = pParse->pVdbe;
100793
 
  if( zAff==0 ){
100794
 
    assert( pParse->db->mallocFailed );
100795
 
    return;
100796
 
  }
100797
 
  assert( v!=0 );
100798
 
 
100799
 
  /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
100800
 
  ** and end of the affinity string.
100801
 
  */
100802
 
  while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
100803
 
    n--;
100804
 
    base++;
100805
 
    zAff++;
100806
 
  }
100807
 
  while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
100808
 
    n--;
100809
 
  }
100810
 
 
100811
 
  /* Code the OP_Affinity opcode if there is anything left to do. */
100812
 
  if( n>0 ){
100813
 
    sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
100814
 
    sqlite3VdbeChangeP4(v, -1, zAff, n);
100815
 
    sqlite3ExprCacheAffinityChange(pParse, base, n);
100816
 
  }
100817
 
}
100818
 
 
100819
 
 
100820
 
/*
100821
 
** Generate code for a single equality term of the WHERE clause.  An equality
100822
 
** term can be either X=expr or X IN (...).   pTerm is the term to be 
100823
 
** coded.
100824
 
**
100825
 
** The current value for the constraint is left in register iReg.
100826
 
**
100827
 
** For a constraint of the form X=expr, the expression is evaluated and its
100828
 
** result is left on the stack.  For constraints of the form X IN (...)
100829
 
** this routine sets up a loop that will iterate over all values of X.
100830
 
*/
100831
 
static int codeEqualityTerm(
100832
 
  Parse *pParse,      /* The parsing context */
100833
 
  WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
100834
 
  WhereLevel *pLevel, /* When level of the FROM clause we are working on */
100835
 
  int iTarget         /* Attempt to leave results in this register */
100836
 
){
100837
 
  Expr *pX = pTerm->pExpr;
100838
 
  Vdbe *v = pParse->pVdbe;
100839
 
  int iReg;                  /* Register holding results */
100840
 
 
100841
 
  assert( iTarget>0 );
100842
 
  if( pX->op==TK_EQ ){
100843
 
    iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
100844
 
  }else if( pX->op==TK_ISNULL ){
100845
 
    iReg = iTarget;
100846
 
    sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
100847
 
#ifndef SQLITE_OMIT_SUBQUERY
100848
 
  }else{
100849
 
    int eType;
100850
 
    int iTab;
100851
 
    struct InLoop *pIn;
100852
 
 
100853
 
    assert( pX->op==TK_IN );
100854
 
    iReg = iTarget;
100855
 
    eType = sqlite3FindInIndex(pParse, pX, 0);
100856
 
    iTab = pX->iTable;
100857
 
    sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
100858
 
    assert( pLevel->plan.wsFlags & WHERE_IN_ABLE );
100859
 
    if( pLevel->u.in.nIn==0 ){
100860
 
      pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
100861
 
    }
100862
 
    pLevel->u.in.nIn++;
100863
 
    pLevel->u.in.aInLoop =
100864
 
       sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
100865
 
                              sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
100866
 
    pIn = pLevel->u.in.aInLoop;
100867
 
    if( pIn ){
100868
 
      pIn += pLevel->u.in.nIn - 1;
100869
 
      pIn->iCur = iTab;
100870
 
      if( eType==IN_INDEX_ROWID ){
100871
 
        pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
100872
 
      }else{
100873
 
        pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
100874
 
      }
100875
 
      sqlite3VdbeAddOp1(v, OP_IsNull, iReg);
100876
 
    }else{
100877
 
      pLevel->u.in.nIn = 0;
100878
 
    }
100879
 
#endif
100880
 
  }
100881
 
  disableTerm(pLevel, pTerm);
100882
 
  return iReg;
100883
 
}
100884
 
 
100885
 
/*
100886
 
** Generate code that will evaluate all == and IN constraints for an
100887
 
** index.
100888
 
**
100889
 
** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
100890
 
** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
100891
 
** The index has as many as three equality constraints, but in this
100892
 
** example, the third "c" value is an inequality.  So only two 
100893
 
** constraints are coded.  This routine will generate code to evaluate
100894
 
** a==5 and b IN (1,2,3).  The current values for a and b will be stored
100895
 
** in consecutive registers and the index of the first register is returned.
100896
 
**
100897
 
** In the example above nEq==2.  But this subroutine works for any value
100898
 
** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
100899
 
** The only thing it does is allocate the pLevel->iMem memory cell and
100900
 
** compute the affinity string.
100901
 
**
100902
 
** This routine always allocates at least one memory cell and returns
100903
 
** the index of that memory cell. The code that
100904
 
** calls this routine will use that memory cell to store the termination
100905
 
** key value of the loop.  If one or more IN operators appear, then
100906
 
** this routine allocates an additional nEq memory cells for internal
100907
 
** use.
100908
 
**
100909
 
** Before returning, *pzAff is set to point to a buffer containing a
100910
 
** copy of the column affinity string of the index allocated using
100911
 
** sqlite3DbMalloc(). Except, entries in the copy of the string associated
100912
 
** with equality constraints that use NONE affinity are set to
100913
 
** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
100914
 
**
100915
 
**   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
100916
 
**   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
100917
 
**
100918
 
** In the example above, the index on t1(a) has TEXT affinity. But since
100919
 
** the right hand side of the equality constraint (t2.b) has NONE affinity,
100920
 
** no conversion should be attempted before using a t2.b value as part of
100921
 
** a key to search the index. Hence the first byte in the returned affinity
100922
 
** string in this example would be set to SQLITE_AFF_NONE.
100923
 
*/
100924
 
static int codeAllEqualityTerms(
100925
 
  Parse *pParse,        /* Parsing context */
100926
 
  WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
100927
 
  WhereClause *pWC,     /* The WHERE clause */
100928
 
  Bitmask notReady,     /* Which parts of FROM have not yet been coded */
100929
 
  int nExtraReg,        /* Number of extra registers to allocate */
100930
 
  char **pzAff          /* OUT: Set to point to affinity string */
100931
 
){
100932
 
  int nEq = pLevel->plan.nEq;   /* The number of == or IN constraints to code */
100933
 
  Vdbe *v = pParse->pVdbe;      /* The vm under construction */
100934
 
  Index *pIdx;                  /* The index being used for this loop */
100935
 
  int iCur = pLevel->iTabCur;   /* The cursor of the table */
100936
 
  WhereTerm *pTerm;             /* A single constraint term */
100937
 
  int j;                        /* Loop counter */
100938
 
  int regBase;                  /* Base register */
100939
 
  int nReg;                     /* Number of registers to allocate */
100940
 
  char *zAff;                   /* Affinity string to return */
100941
 
 
100942
 
  /* This module is only called on query plans that use an index. */
100943
 
  assert( pLevel->plan.wsFlags & WHERE_INDEXED );
100944
 
  pIdx = pLevel->plan.u.pIdx;
100945
 
 
100946
 
  /* Figure out how many memory cells we will need then allocate them.
100947
 
  */
100948
 
  regBase = pParse->nMem + 1;
100949
 
  nReg = pLevel->plan.nEq + nExtraReg;
100950
 
  pParse->nMem += nReg;
100951
 
 
100952
 
  zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
100953
 
  if( !zAff ){
100954
 
    pParse->db->mallocFailed = 1;
100955
 
  }
100956
 
 
100957
 
  /* Evaluate the equality constraints
100958
 
  */
100959
 
  assert( pIdx->nColumn>=nEq );
100960
 
  for(j=0; j<nEq; j++){
100961
 
    int r1;
100962
 
    int k = pIdx->aiColumn[j];
100963
 
    pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
100964
 
    if( NEVER(pTerm==0) ) break;
100965
 
    /* The following true for indices with redundant columns. 
100966
 
    ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
100967
 
    testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
100968
 
    testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
100969
 
    r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
100970
 
    if( r1!=regBase+j ){
100971
 
      if( nReg==1 ){
100972
 
        sqlite3ReleaseTempReg(pParse, regBase);
100973
 
        regBase = r1;
100974
 
      }else{
100975
 
        sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
100976
 
      }
100977
 
    }
100978
 
    testcase( pTerm->eOperator & WO_ISNULL );
100979
 
    testcase( pTerm->eOperator & WO_IN );
100980
 
    if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
100981
 
      Expr *pRight = pTerm->pExpr->pRight;
100982
 
      sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk);
100983
 
      if( zAff ){
100984
 
        if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
100985
 
          zAff[j] = SQLITE_AFF_NONE;
100986
 
        }
100987
 
        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
100988
 
          zAff[j] = SQLITE_AFF_NONE;
100989
 
        }
100990
 
      }
100991
 
    }
100992
 
  }
100993
 
  *pzAff = zAff;
100994
 
  return regBase;
100995
 
}
100996
 
 
100997
 
#ifndef SQLITE_OMIT_EXPLAIN
100998
 
/*
100999
 
** This routine is a helper for explainIndexRange() below
101000
 
**
101001
 
** pStr holds the text of an expression that we are building up one term
101002
 
** at a time.  This routine adds a new term to the end of the expression.
101003
 
** Terms are separated by AND so add the "AND" text for second and subsequent
101004
 
** terms only.
101005
 
*/
101006
 
static void explainAppendTerm(
101007
 
  StrAccum *pStr,             /* The text expression being built */
101008
 
  int iTerm,                  /* Index of this term.  First is zero */
101009
 
  const char *zColumn,        /* Name of the column */
101010
 
  const char *zOp             /* Name of the operator */
101011
 
){
101012
 
  if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
101013
 
  sqlite3StrAccumAppend(pStr, zColumn, -1);
101014
 
  sqlite3StrAccumAppend(pStr, zOp, 1);
101015
 
  sqlite3StrAccumAppend(pStr, "?", 1);
101016
 
}
101017
 
 
101018
 
/*
101019
 
** Argument pLevel describes a strategy for scanning table pTab. This 
101020
 
** function returns a pointer to a string buffer containing a description
101021
 
** of the subset of table rows scanned by the strategy in the form of an
101022
 
** SQL expression. Or, if all rows are scanned, NULL is returned.
101023
 
**
101024
 
** For example, if the query:
101025
 
**
101026
 
**   SELECT * FROM t1 WHERE a=1 AND b>2;
101027
 
**
101028
 
** is run and there is an index on (a, b), then this function returns a
101029
 
** string similar to:
101030
 
**
101031
 
**   "a=? AND b>?"
101032
 
**
101033
 
** The returned pointer points to memory obtained from sqlite3DbMalloc().
101034
 
** It is the responsibility of the caller to free the buffer when it is
101035
 
** no longer required.
101036
 
*/
101037
 
static char *explainIndexRange(sqlite3 *db, WhereLevel *pLevel, Table *pTab){
101038
 
  WherePlan *pPlan = &pLevel->plan;
101039
 
  Index *pIndex = pPlan->u.pIdx;
101040
 
  int nEq = pPlan->nEq;
101041
 
  int i, j;
101042
 
  Column *aCol = pTab->aCol;
101043
 
  int *aiColumn = pIndex->aiColumn;
101044
 
  StrAccum txt;
101045
 
 
101046
 
  if( nEq==0 && (pPlan->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){
101047
 
    return 0;
101048
 
  }
101049
 
  sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
101050
 
  txt.db = db;
101051
 
  sqlite3StrAccumAppend(&txt, " (", 2);
101052
 
  for(i=0; i<nEq; i++){
101053
 
    explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "=");
101054
 
  }
101055
 
 
101056
 
  j = i;
101057
 
  if( pPlan->wsFlags&WHERE_BTM_LIMIT ){
101058
 
    explainAppendTerm(&txt, i++, aCol[aiColumn[j]].zName, ">");
101059
 
  }
101060
 
  if( pPlan->wsFlags&WHERE_TOP_LIMIT ){
101061
 
    explainAppendTerm(&txt, i, aCol[aiColumn[j]].zName, "<");
101062
 
  }
101063
 
  sqlite3StrAccumAppend(&txt, ")", 1);
101064
 
  return sqlite3StrAccumFinish(&txt);
101065
 
}
101066
 
 
101067
 
/*
101068
 
** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
101069
 
** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single
101070
 
** record is added to the output to describe the table scan strategy in 
101071
 
** pLevel.
101072
 
*/
101073
 
static void explainOneScan(
101074
 
  Parse *pParse,                  /* Parse context */
101075
 
  SrcList *pTabList,              /* Table list this loop refers to */
101076
 
  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
101077
 
  int iLevel,                     /* Value for "level" column of output */
101078
 
  int iFrom,                      /* Value for "from" column of output */
101079
 
  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
101080
 
){
101081
 
  if( pParse->explain==2 ){
101082
 
    u32 flags = pLevel->plan.wsFlags;
101083
 
    struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
101084
 
    Vdbe *v = pParse->pVdbe;      /* VM being constructed */
101085
 
    sqlite3 *db = pParse->db;     /* Database handle */
101086
 
    char *zMsg;                   /* Text to add to EQP output */
101087
 
    sqlite3_int64 nRow;           /* Expected number of rows visited by scan */
101088
 
    int iId = pParse->iSelectId;  /* Select id (left-most output column) */
101089
 
    int isSearch;                 /* True for a SEARCH. False for SCAN. */
101090
 
 
101091
 
    if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return;
101092
 
 
101093
 
    isSearch = (pLevel->plan.nEq>0)
101094
 
             || (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
101095
 
             || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
101096
 
 
101097
 
    zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN");
101098
 
    if( pItem->pSelect ){
101099
 
      zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId);
101100
 
    }else{
101101
 
      zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName);
101102
 
    }
101103
 
 
101104
 
    if( pItem->zAlias ){
101105
 
      zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
101106
 
    }
101107
 
    if( (flags & WHERE_INDEXED)!=0 ){
101108
 
      char *zWhere = explainIndexRange(db, pLevel, pItem->pTab);
101109
 
      zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg, 
101110
 
          ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""),
101111
 
          ((flags & WHERE_IDX_ONLY)?"COVERING ":""),
101112
 
          ((flags & WHERE_TEMP_INDEX)?"":" "),
101113
 
          ((flags & WHERE_TEMP_INDEX)?"": pLevel->plan.u.pIdx->zName),
101114
 
          zWhere
101115
 
      );
101116
 
      sqlite3DbFree(db, zWhere);
101117
 
    }else if( flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
101118
 
      zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
101119
 
 
101120
 
      if( flags&WHERE_ROWID_EQ ){
101121
 
        zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg);
101122
 
      }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
101123
 
        zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg);
101124
 
      }else if( flags&WHERE_BTM_LIMIT ){
101125
 
        zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg);
101126
 
      }else if( flags&WHERE_TOP_LIMIT ){
101127
 
        zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg);
101128
 
      }
101129
 
    }
101130
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
101131
 
    else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
101132
 
      sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
101133
 
      zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg,
101134
 
                  pVtabIdx->idxNum, pVtabIdx->idxStr);
101135
 
    }
101136
 
#endif
101137
 
    if( wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) ){
101138
 
      testcase( wctrlFlags & WHERE_ORDERBY_MIN );
101139
 
      nRow = 1;
101140
 
    }else{
101141
 
      nRow = (sqlite3_int64)pLevel->plan.nRow;
101142
 
    }
101143
 
    zMsg = sqlite3MAppendf(db, zMsg, "%s (~%lld rows)", zMsg, nRow);
101144
 
    sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC);
101145
 
  }
101146
 
}
101147
 
#else
101148
 
# define explainOneScan(u,v,w,x,y,z)
101149
 
#endif /* SQLITE_OMIT_EXPLAIN */
101150
 
 
101151
 
 
101152
 
/*
101153
 
** Generate code for the start of the iLevel-th loop in the WHERE clause
101154
 
** implementation described by pWInfo.
101155
 
*/
101156
 
static Bitmask codeOneLoopStart(
101157
 
  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
101158
 
  int iLevel,          /* Which level of pWInfo->a[] should be coded */
101159
 
  u16 wctrlFlags,      /* One of the WHERE_* flags defined in sqliteInt.h */
101160
 
  Bitmask notReady     /* Which tables are currently available */
101161
 
){
101162
 
  int j, k;            /* Loop counters */
101163
 
  int iCur;            /* The VDBE cursor for the table */
101164
 
  int addrNxt;         /* Where to jump to continue with the next IN case */
101165
 
  int omitTable;       /* True if we use the index only */
101166
 
  int bRev;            /* True if we need to scan in reverse order */
101167
 
  WhereLevel *pLevel;  /* The where level to be coded */
101168
 
  WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
101169
 
  WhereTerm *pTerm;               /* A WHERE clause term */
101170
 
  Parse *pParse;                  /* Parsing context */
101171
 
  Vdbe *v;                        /* The prepared stmt under constructions */
101172
 
  struct SrcList_item *pTabItem;  /* FROM clause term being coded */
101173
 
  int addrBrk;                    /* Jump here to break out of the loop */
101174
 
  int addrCont;                   /* Jump here to continue with next cycle */
101175
 
  int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
101176
 
  int iReleaseReg = 0;      /* Temp register to free before returning */
101177
 
 
101178
 
  pParse = pWInfo->pParse;
101179
 
  v = pParse->pVdbe;
101180
 
  pWC = pWInfo->pWC;
101181
 
  pLevel = &pWInfo->a[iLevel];
101182
 
  pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
101183
 
  iCur = pTabItem->iCursor;
101184
 
  bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
101185
 
  omitTable = (pLevel->plan.wsFlags & WHERE_IDX_ONLY)!=0 
101186
 
           && (wctrlFlags & WHERE_FORCE_TABLE)==0;
101187
 
 
101188
 
  /* Create labels for the "break" and "continue" instructions
101189
 
  ** for the current loop.  Jump to addrBrk to break out of a loop.
101190
 
  ** Jump to cont to go immediately to the next iteration of the
101191
 
  ** loop.
101192
 
  **
101193
 
  ** When there is an IN operator, we also have a "addrNxt" label that
101194
 
  ** means to continue with the next IN value combination.  When
101195
 
  ** there are no IN operators in the constraints, the "addrNxt" label
101196
 
  ** is the same as "addrBrk".
101197
 
  */
101198
 
  addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
101199
 
  addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
101200
 
 
101201
 
  /* If this is the right table of a LEFT OUTER JOIN, allocate and
101202
 
  ** initialize a memory cell that records if this table matches any
101203
 
  ** row of the left table of the join.
101204
 
  */
101205
 
  if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
101206
 
    pLevel->iLeftJoin = ++pParse->nMem;
101207
 
    sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
101208
 
    VdbeComment((v, "init LEFT JOIN no-match flag"));
101209
 
  }
101210
 
 
101211
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
101212
 
  if(  (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
101213
 
    /* Case 0:  The table is a virtual-table.  Use the VFilter and VNext
101214
 
    **          to access the data.
101215
 
    */
101216
 
    int iReg;   /* P3 Value for OP_VFilter */
101217
 
    sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
101218
 
    int nConstraint = pVtabIdx->nConstraint;
101219
 
    struct sqlite3_index_constraint_usage *aUsage =
101220
 
                                                pVtabIdx->aConstraintUsage;
101221
 
    const struct sqlite3_index_constraint *aConstraint =
101222
 
                                                pVtabIdx->aConstraint;
101223
 
 
101224
 
    sqlite3ExprCachePush(pParse);
101225
 
    iReg = sqlite3GetTempRange(pParse, nConstraint+2);
101226
 
    for(j=1; j<=nConstraint; j++){
101227
 
      for(k=0; k<nConstraint; k++){
101228
 
        if( aUsage[k].argvIndex==j ){
101229
 
          int iTerm = aConstraint[k].iTermOffset;
101230
 
          sqlite3ExprCode(pParse, pWC->a[iTerm].pExpr->pRight, iReg+j+1);
101231
 
          break;
101232
 
        }
101233
 
      }
101234
 
      if( k==nConstraint ) break;
101235
 
    }
101236
 
    sqlite3VdbeAddOp2(v, OP_Integer, pVtabIdx->idxNum, iReg);
101237
 
    sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1);
101238
 
    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrBrk, iReg, pVtabIdx->idxStr,
101239
 
                      pVtabIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC);
101240
 
    pVtabIdx->needToFreeIdxStr = 0;
101241
 
    for(j=0; j<nConstraint; j++){
101242
 
      if( aUsage[j].omit ){
101243
 
        int iTerm = aConstraint[j].iTermOffset;
101244
 
        disableTerm(pLevel, &pWC->a[iTerm]);
101245
 
      }
101246
 
    }
101247
 
    pLevel->op = OP_VNext;
101248
 
    pLevel->p1 = iCur;
101249
 
    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
101250
 
    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
101251
 
    sqlite3ExprCachePop(pParse, 1);
101252
 
  }else
101253
 
#endif /* SQLITE_OMIT_VIRTUALTABLE */
101254
 
 
101255
 
  if( pLevel->plan.wsFlags & WHERE_ROWID_EQ ){
101256
 
    /* Case 1:  We can directly reference a single row using an
101257
 
    **          equality comparison against the ROWID field.  Or
101258
 
    **          we reference multiple rows using a "rowid IN (...)"
101259
 
    **          construct.
101260
 
    */
101261
 
    iReleaseReg = sqlite3GetTempReg(pParse);
101262
 
    pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
101263
 
    assert( pTerm!=0 );
101264
 
    assert( pTerm->pExpr!=0 );
101265
 
    assert( pTerm->leftCursor==iCur );
101266
 
    assert( omitTable==0 );
101267
 
    testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
101268
 
    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, iReleaseReg);
101269
 
    addrNxt = pLevel->addrNxt;
101270
 
    sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
101271
 
    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
101272
 
    sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
101273
 
    VdbeComment((v, "pk"));
101274
 
    pLevel->op = OP_Noop;
101275
 
  }else if( pLevel->plan.wsFlags & WHERE_ROWID_RANGE ){
101276
 
    /* Case 2:  We have an inequality comparison against the ROWID field.
101277
 
    */
101278
 
    int testOp = OP_Noop;
101279
 
    int start;
101280
 
    int memEndValue = 0;
101281
 
    WhereTerm *pStart, *pEnd;
101282
 
 
101283
 
    assert( omitTable==0 );
101284
 
    pStart = findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0);
101285
 
    pEnd = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0);
101286
 
    if( bRev ){
101287
 
      pTerm = pStart;
101288
 
      pStart = pEnd;
101289
 
      pEnd = pTerm;
101290
 
    }
101291
 
    if( pStart ){
101292
 
      Expr *pX;             /* The expression that defines the start bound */
101293
 
      int r1, rTemp;        /* Registers for holding the start boundary */
101294
 
 
101295
 
      /* The following constant maps TK_xx codes into corresponding 
101296
 
      ** seek opcodes.  It depends on a particular ordering of TK_xx
101297
 
      */
101298
 
      const u8 aMoveOp[] = {
101299
 
           /* TK_GT */  OP_SeekGt,
101300
 
           /* TK_LE */  OP_SeekLe,
101301
 
           /* TK_LT */  OP_SeekLt,
101302
 
           /* TK_GE */  OP_SeekGe
101303
 
      };
101304
 
      assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
101305
 
      assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
101306
 
      assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
101307
 
 
101308
 
      testcase( pStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
101309
 
      pX = pStart->pExpr;
101310
 
      assert( pX!=0 );
101311
 
      assert( pStart->leftCursor==iCur );
101312
 
      r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
101313
 
      sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
101314
 
      VdbeComment((v, "pk"));
101315
 
      sqlite3ExprCacheAffinityChange(pParse, r1, 1);
101316
 
      sqlite3ReleaseTempReg(pParse, rTemp);
101317
 
      disableTerm(pLevel, pStart);
101318
 
    }else{
101319
 
      sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
101320
 
    }
101321
 
    if( pEnd ){
101322
 
      Expr *pX;
101323
 
      pX = pEnd->pExpr;
101324
 
      assert( pX!=0 );
101325
 
      assert( pEnd->leftCursor==iCur );
101326
 
      testcase( pEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
101327
 
      memEndValue = ++pParse->nMem;
101328
 
      sqlite3ExprCode(pParse, pX->pRight, memEndValue);
101329
 
      if( pX->op==TK_LT || pX->op==TK_GT ){
101330
 
        testOp = bRev ? OP_Le : OP_Ge;
101331
 
      }else{
101332
 
        testOp = bRev ? OP_Lt : OP_Gt;
101333
 
      }
101334
 
      disableTerm(pLevel, pEnd);
101335
 
    }
101336
 
    start = sqlite3VdbeCurrentAddr(v);
101337
 
    pLevel->op = bRev ? OP_Prev : OP_Next;
101338
 
    pLevel->p1 = iCur;
101339
 
    pLevel->p2 = start;
101340
 
    if( pStart==0 && pEnd==0 ){
101341
 
      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
101342
 
    }else{
101343
 
      assert( pLevel->p5==0 );
101344
 
    }
101345
 
    if( testOp!=OP_Noop ){
101346
 
      iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
101347
 
      sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
101348
 
      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
101349
 
      sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
101350
 
      sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
101351
 
    }
101352
 
  }else if( pLevel->plan.wsFlags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){
101353
 
    /* Case 3: A scan using an index.
101354
 
    **
101355
 
    **         The WHERE clause may contain zero or more equality 
101356
 
    **         terms ("==" or "IN" operators) that refer to the N
101357
 
    **         left-most columns of the index. It may also contain
101358
 
    **         inequality constraints (>, <, >= or <=) on the indexed
101359
 
    **         column that immediately follows the N equalities. Only 
101360
 
    **         the right-most column can be an inequality - the rest must
101361
 
    **         use the "==" and "IN" operators. For example, if the 
101362
 
    **         index is on (x,y,z), then the following clauses are all 
101363
 
    **         optimized:
101364
 
    **
101365
 
    **            x=5
101366
 
    **            x=5 AND y=10
101367
 
    **            x=5 AND y<10
101368
 
    **            x=5 AND y>5 AND y<10
101369
 
    **            x=5 AND y=5 AND z<=10
101370
 
    **
101371
 
    **         The z<10 term of the following cannot be used, only
101372
 
    **         the x=5 term:
101373
 
    **
101374
 
    **            x=5 AND z<10
101375
 
    **
101376
 
    **         N may be zero if there are inequality constraints.
101377
 
    **         If there are no inequality constraints, then N is at
101378
 
    **         least one.
101379
 
    **
101380
 
    **         This case is also used when there are no WHERE clause
101381
 
    **         constraints but an index is selected anyway, in order
101382
 
    **         to force the output order to conform to an ORDER BY.
101383
 
    */  
101384
 
    static const u8 aStartOp[] = {
101385
 
      0,
101386
 
      0,
101387
 
      OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
101388
 
      OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
101389
 
      OP_SeekGt,           /* 4: (start_constraints  && !startEq && !bRev) */
101390
 
      OP_SeekLt,           /* 5: (start_constraints  && !startEq &&  bRev) */
101391
 
      OP_SeekGe,           /* 6: (start_constraints  &&  startEq && !bRev) */
101392
 
      OP_SeekLe            /* 7: (start_constraints  &&  startEq &&  bRev) */
101393
 
    };
101394
 
    static const u8 aEndOp[] = {
101395
 
      OP_Noop,             /* 0: (!end_constraints) */
101396
 
      OP_IdxGE,            /* 1: (end_constraints && !bRev) */
101397
 
      OP_IdxLT             /* 2: (end_constraints && bRev) */
101398
 
    };
101399
 
    int nEq = pLevel->plan.nEq;  /* Number of == or IN terms */
101400
 
    int isMinQuery = 0;          /* If this is an optimized SELECT min(x).. */
101401
 
    int regBase;                 /* Base register holding constraint values */
101402
 
    int r1;                      /* Temp register */
101403
 
    WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
101404
 
    WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
101405
 
    int startEq;                 /* True if range start uses ==, >= or <= */
101406
 
    int endEq;                   /* True if range end uses ==, >= or <= */
101407
 
    int start_constraints;       /* Start of range is constrained */
101408
 
    int nConstraint;             /* Number of constraint terms */
101409
 
    Index *pIdx;                 /* The index we will be using */
101410
 
    int iIdxCur;                 /* The VDBE cursor for the index */
101411
 
    int nExtraReg = 0;           /* Number of extra registers needed */
101412
 
    int op;                      /* Instruction opcode */
101413
 
    char *zStartAff;             /* Affinity for start of range constraint */
101414
 
    char *zEndAff;               /* Affinity for end of range constraint */
101415
 
 
101416
 
    pIdx = pLevel->plan.u.pIdx;
101417
 
    iIdxCur = pLevel->iIdxCur;
101418
 
    k = pIdx->aiColumn[nEq];     /* Column for inequality constraints */
101419
 
 
101420
 
    /* If this loop satisfies a sort order (pOrderBy) request that 
101421
 
    ** was passed to this function to implement a "SELECT min(x) ..." 
101422
 
    ** query, then the caller will only allow the loop to run for
101423
 
    ** a single iteration. This means that the first row returned
101424
 
    ** should not have a NULL value stored in 'x'. If column 'x' is
101425
 
    ** the first one after the nEq equality constraints in the index,
101426
 
    ** this requires some special handling.
101427
 
    */
101428
 
    if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
101429
 
     && (pLevel->plan.wsFlags&WHERE_ORDERBY)
101430
 
     && (pIdx->nColumn>nEq)
101431
 
    ){
101432
 
      /* assert( pOrderBy->nExpr==1 ); */
101433
 
      /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */
101434
 
      isMinQuery = 1;
101435
 
      nExtraReg = 1;
101436
 
    }
101437
 
 
101438
 
    /* Find any inequality constraint terms for the start and end 
101439
 
    ** of the range. 
101440
 
    */
101441
 
    if( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ){
101442
 
      pRangeEnd = findTerm(pWC, iCur, k, notReady, (WO_LT|WO_LE), pIdx);
101443
 
      nExtraReg = 1;
101444
 
    }
101445
 
    if( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ){
101446
 
      pRangeStart = findTerm(pWC, iCur, k, notReady, (WO_GT|WO_GE), pIdx);
101447
 
      nExtraReg = 1;
101448
 
    }
101449
 
 
101450
 
    /* Generate code to evaluate all constraint terms using == or IN
101451
 
    ** and store the values of those terms in an array of registers
101452
 
    ** starting at regBase.
101453
 
    */
101454
 
    regBase = codeAllEqualityTerms(
101455
 
        pParse, pLevel, pWC, notReady, nExtraReg, &zStartAff
101456
 
    );
101457
 
    zEndAff = sqlite3DbStrDup(pParse->db, zStartAff);
101458
 
    addrNxt = pLevel->addrNxt;
101459
 
 
101460
 
    /* If we are doing a reverse order scan on an ascending index, or
101461
 
    ** a forward order scan on a descending index, interchange the 
101462
 
    ** start and end terms (pRangeStart and pRangeEnd).
101463
 
    */
101464
 
    if( nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC) ){
101465
 
      SWAP(WhereTerm *, pRangeEnd, pRangeStart);
101466
 
    }
101467
 
 
101468
 
    testcase( pRangeStart && pRangeStart->eOperator & WO_LE );
101469
 
    testcase( pRangeStart && pRangeStart->eOperator & WO_GE );
101470
 
    testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE );
101471
 
    testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE );
101472
 
    startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
101473
 
    endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
101474
 
    start_constraints = pRangeStart || nEq>0;
101475
 
 
101476
 
    /* Seek the index cursor to the start of the range. */
101477
 
    nConstraint = nEq;
101478
 
    if( pRangeStart ){
101479
 
      Expr *pRight = pRangeStart->pExpr->pRight;
101480
 
      sqlite3ExprCode(pParse, pRight, regBase+nEq);
101481
 
      if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){
101482
 
        sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
101483
 
      }
101484
 
      if( zStartAff ){
101485
 
        if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
101486
 
          /* Since the comparison is to be performed with no conversions
101487
 
          ** applied to the operands, set the affinity to apply to pRight to 
101488
 
          ** SQLITE_AFF_NONE.  */
101489
 
          zStartAff[nEq] = SQLITE_AFF_NONE;
101490
 
        }
101491
 
        if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
101492
 
          zStartAff[nEq] = SQLITE_AFF_NONE;
101493
 
        }
101494
 
      }  
101495
 
      nConstraint++;
101496
 
      testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
101497
 
    }else if( isMinQuery ){
101498
 
      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
101499
 
      nConstraint++;
101500
 
      startEq = 0;
101501
 
      start_constraints = 1;
101502
 
    }
101503
 
    codeApplyAffinity(pParse, regBase, nConstraint, zStartAff);
101504
 
    op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
101505
 
    assert( op!=0 );
101506
 
    testcase( op==OP_Rewind );
101507
 
    testcase( op==OP_Last );
101508
 
    testcase( op==OP_SeekGt );
101509
 
    testcase( op==OP_SeekGe );
101510
 
    testcase( op==OP_SeekLe );
101511
 
    testcase( op==OP_SeekLt );
101512
 
    sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
101513
 
 
101514
 
    /* Load the value for the inequality constraint at the end of the
101515
 
    ** range (if any).
101516
 
    */
101517
 
    nConstraint = nEq;
101518
 
    if( pRangeEnd ){
101519
 
      Expr *pRight = pRangeEnd->pExpr->pRight;
101520
 
      sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
101521
 
      sqlite3ExprCode(pParse, pRight, regBase+nEq);
101522
 
      if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){
101523
 
        sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
101524
 
      }
101525
 
      if( zEndAff ){
101526
 
        if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){
101527
 
          /* Since the comparison is to be performed with no conversions
101528
 
          ** applied to the operands, set the affinity to apply to pRight to 
101529
 
          ** SQLITE_AFF_NONE.  */
101530
 
          zEndAff[nEq] = SQLITE_AFF_NONE;
101531
 
        }
101532
 
        if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){
101533
 
          zEndAff[nEq] = SQLITE_AFF_NONE;
101534
 
        }
101535
 
      }  
101536
 
      codeApplyAffinity(pParse, regBase, nEq+1, zEndAff);
101537
 
      nConstraint++;
101538
 
      testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
101539
 
    }
101540
 
    sqlite3DbFree(pParse->db, zStartAff);
101541
 
    sqlite3DbFree(pParse->db, zEndAff);
101542
 
 
101543
 
    /* Top of the loop body */
101544
 
    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
101545
 
 
101546
 
    /* Check if the index cursor is past the end of the range. */
101547
 
    op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)];
101548
 
    testcase( op==OP_Noop );
101549
 
    testcase( op==OP_IdxGE );
101550
 
    testcase( op==OP_IdxLT );
101551
 
    if( op!=OP_Noop ){
101552
 
      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
101553
 
      sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0);
101554
 
    }
101555
 
 
101556
 
    /* If there are inequality constraints, check that the value
101557
 
    ** of the table column that the inequality contrains is not NULL.
101558
 
    ** If it is, jump to the next iteration of the loop.
101559
 
    */
101560
 
    r1 = sqlite3GetTempReg(pParse);
101561
 
    testcase( pLevel->plan.wsFlags & WHERE_BTM_LIMIT );
101562
 
    testcase( pLevel->plan.wsFlags & WHERE_TOP_LIMIT );
101563
 
    if( (pLevel->plan.wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){
101564
 
      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
101565
 
      sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont);
101566
 
    }
101567
 
    sqlite3ReleaseTempReg(pParse, r1);
101568
 
 
101569
 
    /* Seek the table cursor, if required */
101570
 
    disableTerm(pLevel, pRangeStart);
101571
 
    disableTerm(pLevel, pRangeEnd);
101572
 
    if( !omitTable ){
101573
 
      iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
101574
 
      sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
101575
 
      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
101576
 
      sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
101577
 
    }
101578
 
 
101579
 
    /* Record the instruction used to terminate the loop. Disable 
101580
 
    ** WHERE clause terms made redundant by the index range scan.
101581
 
    */
101582
 
    if( pLevel->plan.wsFlags & WHERE_UNIQUE ){
101583
 
      pLevel->op = OP_Noop;
101584
 
    }else if( bRev ){
101585
 
      pLevel->op = OP_Prev;
101586
 
    }else{
101587
 
      pLevel->op = OP_Next;
101588
 
    }
101589
 
    pLevel->p1 = iIdxCur;
101590
 
  }else
101591
 
 
101592
 
#ifndef SQLITE_OMIT_OR_OPTIMIZATION
101593
 
  if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
101594
 
    /* Case 4:  Two or more separately indexed terms connected by OR
101595
 
    **
101596
 
    ** Example:
101597
 
    **
101598
 
    **   CREATE TABLE t1(a,b,c,d);
101599
 
    **   CREATE INDEX i1 ON t1(a);
101600
 
    **   CREATE INDEX i2 ON t1(b);
101601
 
    **   CREATE INDEX i3 ON t1(c);
101602
 
    **
101603
 
    **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
101604
 
    **
101605
 
    ** In the example, there are three indexed terms connected by OR.
101606
 
    ** The top of the loop looks like this:
101607
 
    **
101608
 
    **          Null       1                # Zero the rowset in reg 1
101609
 
    **
101610
 
    ** Then, for each indexed term, the following. The arguments to
101611
 
    ** RowSetTest are such that the rowid of the current row is inserted
101612
 
    ** into the RowSet. If it is already present, control skips the
101613
 
    ** Gosub opcode and jumps straight to the code generated by WhereEnd().
101614
 
    **
101615
 
    **        sqlite3WhereBegin(<term>)
101616
 
    **          RowSetTest                  # Insert rowid into rowset
101617
 
    **          Gosub      2 A
101618
 
    **        sqlite3WhereEnd()
101619
 
    **
101620
 
    ** Following the above, code to terminate the loop. Label A, the target
101621
 
    ** of the Gosub above, jumps to the instruction right after the Goto.
101622
 
    **
101623
 
    **          Null       1                # Zero the rowset in reg 1
101624
 
    **          Goto       B                # The loop is finished.
101625
 
    **
101626
 
    **       A: <loop body>                 # Return data, whatever.
101627
 
    **
101628
 
    **          Return     2                # Jump back to the Gosub
101629
 
    **
101630
 
    **       B: <after the loop>
101631
 
    **
101632
 
    */
101633
 
    WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
101634
 
    SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
101635
 
 
101636
 
    int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
101637
 
    int regRowset = 0;                        /* Register for RowSet object */
101638
 
    int regRowid = 0;                         /* Register holding rowid */
101639
 
    int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
101640
 
    int iRetInit;                             /* Address of regReturn init */
101641
 
    int untestedTerms = 0;             /* Some terms not completely tested */
101642
 
    int ii;
101643
 
   
101644
 
    pTerm = pLevel->plan.u.pTerm;
101645
 
    assert( pTerm!=0 );
101646
 
    assert( pTerm->eOperator==WO_OR );
101647
 
    assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
101648
 
    pOrWc = &pTerm->u.pOrInfo->wc;
101649
 
    pLevel->op = OP_Return;
101650
 
    pLevel->p1 = regReturn;
101651
 
 
101652
 
    /* Set up a new SrcList ni pOrTab containing the table being scanned
101653
 
    ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
101654
 
    ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
101655
 
    */
101656
 
    if( pWInfo->nLevel>1 ){
101657
 
      int nNotReady;                 /* The number of notReady tables */
101658
 
      struct SrcList_item *origSrc;     /* Original list of tables */
101659
 
      nNotReady = pWInfo->nLevel - iLevel - 1;
101660
 
      pOrTab = sqlite3StackAllocRaw(pParse->db,
101661
 
                            sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
101662
 
      if( pOrTab==0 ) return notReady;
101663
 
      pOrTab->nAlloc = (i16)(nNotReady + 1);
101664
 
      pOrTab->nSrc = pOrTab->nAlloc;
101665
 
      memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
101666
 
      origSrc = pWInfo->pTabList->a;
101667
 
      for(k=1; k<=nNotReady; k++){
101668
 
        memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
101669
 
      }
101670
 
    }else{
101671
 
      pOrTab = pWInfo->pTabList;
101672
 
    }
101673
 
 
101674
 
    /* Initialize the rowset register to contain NULL. An SQL NULL is 
101675
 
    ** equivalent to an empty rowset.
101676
 
    **
101677
 
    ** Also initialize regReturn to contain the address of the instruction 
101678
 
    ** immediately following the OP_Return at the bottom of the loop. This
101679
 
    ** is required in a few obscure LEFT JOIN cases where control jumps
101680
 
    ** over the top of the loop into the body of it. In this case the 
101681
 
    ** correct response for the end-of-loop code (the OP_Return) is to 
101682
 
    ** fall through to the next instruction, just as an OP_Next does if
101683
 
    ** called on an uninitialized cursor.
101684
 
    */
101685
 
    if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
101686
 
      regRowset = ++pParse->nMem;
101687
 
      regRowid = ++pParse->nMem;
101688
 
      sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
101689
 
    }
101690
 
    iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
101691
 
 
101692
 
    for(ii=0; ii<pOrWc->nTerm; ii++){
101693
 
      WhereTerm *pOrTerm = &pOrWc->a[ii];
101694
 
      if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
101695
 
        WhereInfo *pSubWInfo;          /* Info for single OR-term scan */
101696
 
        /* Loop through table entries that match term pOrTerm. */
101697
 
        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrTerm->pExpr, 0,
101698
 
                        WHERE_OMIT_OPEN | WHERE_OMIT_CLOSE |
101699
 
                        WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY);
101700
 
        if( pSubWInfo ){
101701
 
          explainOneScan(
101702
 
              pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
101703
 
          );
101704
 
          if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
101705
 
            int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
101706
 
            int r;
101707
 
            r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, 
101708
 
                                         regRowid);
101709
 
            sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
101710
 
                                 sqlite3VdbeCurrentAddr(v)+2, r, iSet);
101711
 
          }
101712
 
          sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
101713
 
 
101714
 
          /* The pSubWInfo->untestedTerms flag means that this OR term
101715
 
          ** contained one or more AND term from a notReady table.  The
101716
 
          ** terms from the notReady table could not be tested and will
101717
 
          ** need to be tested later.
101718
 
          */
101719
 
          if( pSubWInfo->untestedTerms ) untestedTerms = 1;
101720
 
 
101721
 
          /* Finish the loop through table entries that match term pOrTerm. */
101722
 
          sqlite3WhereEnd(pSubWInfo);
101723
 
        }
101724
 
      }
101725
 
    }
101726
 
    sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
101727
 
    sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
101728
 
    sqlite3VdbeResolveLabel(v, iLoopBody);
101729
 
 
101730
 
    if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab);
101731
 
    if( !untestedTerms ) disableTerm(pLevel, pTerm);
101732
 
  }else
101733
 
#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
101734
 
 
101735
 
  {
101736
 
    /* Case 5:  There is no usable index.  We must do a complete
101737
 
    **          scan of the entire table.
101738
 
    */
101739
 
    static const u8 aStep[] = { OP_Next, OP_Prev };
101740
 
    static const u8 aStart[] = { OP_Rewind, OP_Last };
101741
 
    assert( bRev==0 || bRev==1 );
101742
 
    assert( omitTable==0 );
101743
 
    pLevel->op = aStep[bRev];
101744
 
    pLevel->p1 = iCur;
101745
 
    pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
101746
 
    pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
101747
 
  }
101748
 
  notReady &= ~getMask(pWC->pMaskSet, iCur);
101749
 
 
101750
 
  /* Insert code to test every subexpression that can be completely
101751
 
  ** computed using the current set of tables.
101752
 
  **
101753
 
  ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through
101754
 
  ** the use of indices become tests that are evaluated against each row of
101755
 
  ** the relevant input tables.
101756
 
  */
101757
 
  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
101758
 
    Expr *pE;
101759
 
    testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
101760
 
    testcase( pTerm->wtFlags & TERM_CODED );
101761
 
    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
101762
 
    if( (pTerm->prereqAll & notReady)!=0 ){
101763
 
      testcase( pWInfo->untestedTerms==0
101764
 
               && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
101765
 
      pWInfo->untestedTerms = 1;
101766
 
      continue;
101767
 
    }
101768
 
    pE = pTerm->pExpr;
101769
 
    assert( pE!=0 );
101770
 
    if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
101771
 
      continue;
101772
 
    }
101773
 
    sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
101774
 
    pTerm->wtFlags |= TERM_CODED;
101775
 
  }
101776
 
 
101777
 
  /* For a LEFT OUTER JOIN, generate code that will record the fact that
101778
 
  ** at least one row of the right table has matched the left table.  
101779
 
  */
101780
 
  if( pLevel->iLeftJoin ){
101781
 
    pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
101782
 
    sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
101783
 
    VdbeComment((v, "record LEFT JOIN hit"));
101784
 
    sqlite3ExprCacheClear(pParse);
101785
 
    for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
101786
 
      testcase( pTerm->wtFlags & TERM_VIRTUAL );  /* IMP: R-30575-11662 */
101787
 
      testcase( pTerm->wtFlags & TERM_CODED );
101788
 
      if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
101789
 
      if( (pTerm->prereqAll & notReady)!=0 ){
101790
 
        assert( pWInfo->untestedTerms );
101791
 
        continue;
101792
 
      }
101793
 
      assert( pTerm->pExpr );
101794
 
      sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
101795
 
      pTerm->wtFlags |= TERM_CODED;
101796
 
    }
101797
 
  }
101798
 
  sqlite3ReleaseTempReg(pParse, iReleaseReg);
101799
 
 
101800
 
  return notReady;
101801
 
}
101802
 
 
101803
 
#if defined(SQLITE_TEST)
101804
 
/*
101805
 
** The following variable holds a text description of query plan generated
101806
 
** by the most recent call to sqlite3WhereBegin().  Each call to WhereBegin
101807
 
** overwrites the previous.  This information is used for testing and
101808
 
** analysis only.
101809
 
*/
101810
 
SQLITE_API char sqlite3_query_plan[BMS*2*40];  /* Text of the join */
101811
 
static int nQPlan = 0;              /* Next free slow in _query_plan[] */
101812
 
 
101813
 
#endif /* SQLITE_TEST */
101814
 
 
101815
 
 
101816
 
/*
101817
 
** Free a WhereInfo structure
101818
 
*/
101819
 
static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
101820
 
  if( ALWAYS(pWInfo) ){
101821
 
    int i;
101822
 
    for(i=0; i<pWInfo->nLevel; i++){
101823
 
      sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
101824
 
      if( pInfo ){
101825
 
        /* assert( pInfo->needToFreeIdxStr==0 || db->mallocFailed ); */
101826
 
        if( pInfo->needToFreeIdxStr ){
101827
 
          sqlite3_free(pInfo->idxStr);
101828
 
        }
101829
 
        sqlite3DbFree(db, pInfo);
101830
 
      }
101831
 
      if( pWInfo->a[i].plan.wsFlags & WHERE_TEMP_INDEX ){
101832
 
        Index *pIdx = pWInfo->a[i].plan.u.pIdx;
101833
 
        if( pIdx ){
101834
 
          sqlite3DbFree(db, pIdx->zColAff);
101835
 
          sqlite3DbFree(db, pIdx);
101836
 
        }
101837
 
      }
101838
 
    }
101839
 
    whereClauseClear(pWInfo->pWC);
101840
 
    sqlite3DbFree(db, pWInfo);
101841
 
  }
101842
 
}
101843
 
 
101844
 
 
101845
 
/*
101846
 
** Generate the beginning of the loop used for WHERE clause processing.
101847
 
** The return value is a pointer to an opaque structure that contains
101848
 
** information needed to terminate the loop.  Later, the calling routine
101849
 
** should invoke sqlite3WhereEnd() with the return value of this function
101850
 
** in order to complete the WHERE clause processing.
101851
 
**
101852
 
** If an error occurs, this routine returns NULL.
101853
 
**
101854
 
** The basic idea is to do a nested loop, one loop for each table in
101855
 
** the FROM clause of a select.  (INSERT and UPDATE statements are the
101856
 
** same as a SELECT with only a single table in the FROM clause.)  For
101857
 
** example, if the SQL is this:
101858
 
**
101859
 
**       SELECT * FROM t1, t2, t3 WHERE ...;
101860
 
**
101861
 
** Then the code generated is conceptually like the following:
101862
 
**
101863
 
**      foreach row1 in t1 do       \    Code generated
101864
 
**        foreach row2 in t2 do      |-- by sqlite3WhereBegin()
101865
 
**          foreach row3 in t3 do   /
101866
 
**            ...
101867
 
**          end                     \    Code generated
101868
 
**        end                        |-- by sqlite3WhereEnd()
101869
 
**      end                         /
101870
 
**
101871
 
** Note that the loops might not be nested in the order in which they
101872
 
** appear in the FROM clause if a different order is better able to make
101873
 
** use of indices.  Note also that when the IN operator appears in
101874
 
** the WHERE clause, it might result in additional nested loops for
101875
 
** scanning through all values on the right-hand side of the IN.
101876
 
**
101877
 
** There are Btree cursors associated with each table.  t1 uses cursor
101878
 
** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
101879
 
** And so forth.  This routine generates code to open those VDBE cursors
101880
 
** and sqlite3WhereEnd() generates the code to close them.
101881
 
**
101882
 
** The code that sqlite3WhereBegin() generates leaves the cursors named
101883
 
** in pTabList pointing at their appropriate entries.  The [...] code
101884
 
** can use OP_Column and OP_Rowid opcodes on these cursors to extract
101885
 
** data from the various tables of the loop.
101886
 
**
101887
 
** If the WHERE clause is empty, the foreach loops must each scan their
101888
 
** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
101889
 
** the tables have indices and there are terms in the WHERE clause that
101890
 
** refer to those indices, a complete table scan can be avoided and the
101891
 
** code will run much faster.  Most of the work of this routine is checking
101892
 
** to see if there are indices that can be used to speed up the loop.
101893
 
**
101894
 
** Terms of the WHERE clause are also used to limit which rows actually
101895
 
** make it to the "..." in the middle of the loop.  After each "foreach",
101896
 
** terms of the WHERE clause that use only terms in that loop and outer
101897
 
** loops are evaluated and if false a jump is made around all subsequent
101898
 
** inner loops (or around the "..." if the test occurs within the inner-
101899
 
** most loop)
101900
 
**
101901
 
** OUTER JOINS
101902
 
**
101903
 
** An outer join of tables t1 and t2 is conceptally coded as follows:
101904
 
**
101905
 
**    foreach row1 in t1 do
101906
 
**      flag = 0
101907
 
**      foreach row2 in t2 do
101908
 
**        start:
101909
 
**          ...
101910
 
**          flag = 1
101911
 
**      end
101912
 
**      if flag==0 then
101913
 
**        move the row2 cursor to a null row
101914
 
**        goto start
101915
 
**      fi
101916
 
**    end
101917
 
**
101918
 
** ORDER BY CLAUSE PROCESSING
101919
 
**
101920
 
** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
101921
 
** if there is one.  If there is no ORDER BY clause or if this routine
101922
 
** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
101923
 
**
101924
 
** If an index can be used so that the natural output order of the table
101925
 
** scan is correct for the ORDER BY clause, then that index is used and
101926
 
** *ppOrderBy is set to NULL.  This is an optimization that prevents an
101927
 
** unnecessary sort of the result set if an index appropriate for the
101928
 
** ORDER BY clause already exists.
101929
 
**
101930
 
** If the where clause loops cannot be arranged to provide the correct
101931
 
** output order, then the *ppOrderBy is unchanged.
101932
 
*/
101933
 
SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
101934
 
  Parse *pParse,        /* The parser context */
101935
 
  SrcList *pTabList,    /* A list of all tables to be scanned */
101936
 
  Expr *pWhere,         /* The WHERE clause */
101937
 
  ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */
101938
 
  u16 wctrlFlags        /* One of the WHERE_* flags defined in sqliteInt.h */
101939
 
){
101940
 
  int i;                     /* Loop counter */
101941
 
  int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
101942
 
  int nTabList;              /* Number of elements in pTabList */
101943
 
  WhereInfo *pWInfo;         /* Will become the return value of this function */
101944
 
  Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
101945
 
  Bitmask notReady;          /* Cursors that are not yet positioned */
101946
 
  WhereMaskSet *pMaskSet;    /* The expression mask set */
101947
 
  WhereClause *pWC;               /* Decomposition of the WHERE clause */
101948
 
  struct SrcList_item *pTabItem;  /* A single entry from pTabList */
101949
 
  WhereLevel *pLevel;             /* A single level in the pWInfo list */
101950
 
  int iFrom;                      /* First unused FROM clause element */
101951
 
  int andFlags;              /* AND-ed combination of all pWC->a[].wtFlags */
101952
 
  sqlite3 *db;               /* Database connection */
101953
 
 
101954
 
  /* The number of tables in the FROM clause is limited by the number of
101955
 
  ** bits in a Bitmask 
101956
 
  */
101957
 
  testcase( pTabList->nSrc==BMS );
101958
 
  if( pTabList->nSrc>BMS ){
101959
 
    sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
101960
 
    return 0;
101961
 
  }
101962
 
 
101963
 
  /* This function normally generates a nested loop for all tables in 
101964
 
  ** pTabList.  But if the WHERE_ONETABLE_ONLY flag is set, then we should
101965
 
  ** only generate code for the first table in pTabList and assume that
101966
 
  ** any cursors associated with subsequent tables are uninitialized.
101967
 
  */
101968
 
  nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc;
101969
 
 
101970
 
  /* Allocate and initialize the WhereInfo structure that will become the
101971
 
  ** return value. A single allocation is used to store the WhereInfo
101972
 
  ** struct, the contents of WhereInfo.a[], the WhereClause structure
101973
 
  ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
101974
 
  ** field (type Bitmask) it must be aligned on an 8-byte boundary on
101975
 
  ** some architectures. Hence the ROUND8() below.
101976
 
  */
101977
 
  db = pParse->db;
101978
 
  nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
101979
 
  pWInfo = sqlite3DbMallocZero(db, 
101980
 
      nByteWInfo + 
101981
 
      sizeof(WhereClause) +
101982
 
      sizeof(WhereMaskSet)
101983
 
  );
101984
 
  if( db->mallocFailed ){
101985
 
    sqlite3DbFree(db, pWInfo);
101986
 
    pWInfo = 0;
101987
 
    goto whereBeginError;
101988
 
  }
101989
 
  pWInfo->nLevel = nTabList;
101990
 
  pWInfo->pParse = pParse;
101991
 
  pWInfo->pTabList = pTabList;
101992
 
  pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
101993
 
  pWInfo->pWC = pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
101994
 
  pWInfo->wctrlFlags = wctrlFlags;
101995
 
  pWInfo->savedNQueryLoop = pParse->nQueryLoop;
101996
 
  pMaskSet = (WhereMaskSet*)&pWC[1];
101997
 
 
101998
 
  /* Split the WHERE clause into separate subexpressions where each
101999
 
  ** subexpression is separated by an AND operator.
102000
 
  */
102001
 
  initMaskSet(pMaskSet);
102002
 
  whereClauseInit(pWC, pParse, pMaskSet);
102003
 
  sqlite3ExprCodeConstants(pParse, pWhere);
102004
 
  whereSplit(pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
102005
 
    
102006
 
  /* Special case: a WHERE clause that is constant.  Evaluate the
102007
 
  ** expression and either jump over all of the code or fall thru.
102008
 
  */
102009
 
  if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
102010
 
    sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
102011
 
    pWhere = 0;
102012
 
  }
102013
 
 
102014
 
  /* Assign a bit from the bitmask to every term in the FROM clause.
102015
 
  **
102016
 
  ** When assigning bitmask values to FROM clause cursors, it must be
102017
 
  ** the case that if X is the bitmask for the N-th FROM clause term then
102018
 
  ** the bitmask for all FROM clause terms to the left of the N-th term
102019
 
  ** is (X-1).   An expression from the ON clause of a LEFT JOIN can use
102020
 
  ** its Expr.iRightJoinTable value to find the bitmask of the right table
102021
 
  ** of the join.  Subtracting one from the right table bitmask gives a
102022
 
  ** bitmask for all tables to the left of the join.  Knowing the bitmask
102023
 
  ** for all tables to the left of a left join is important.  Ticket #3015.
102024
 
  **
102025
 
  ** Configure the WhereClause.vmask variable so that bits that correspond
102026
 
  ** to virtual table cursors are set. This is used to selectively disable 
102027
 
  ** the OR-to-IN transformation in exprAnalyzeOrTerm(). It is not helpful 
102028
 
  ** with virtual tables.
102029
 
  **
102030
 
  ** Note that bitmasks are created for all pTabList->nSrc tables in
102031
 
  ** pTabList, not just the first nTabList tables.  nTabList is normally
102032
 
  ** equal to pTabList->nSrc but might be shortened to 1 if the
102033
 
  ** WHERE_ONETABLE_ONLY flag is set.
102034
 
  */
102035
 
  assert( pWC->vmask==0 && pMaskSet->n==0 );
102036
 
  for(i=0; i<pTabList->nSrc; i++){
102037
 
    createMask(pMaskSet, pTabList->a[i].iCursor);
102038
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
102039
 
    if( ALWAYS(pTabList->a[i].pTab) && IsVirtual(pTabList->a[i].pTab) ){
102040
 
      pWC->vmask |= ((Bitmask)1 << i);
102041
 
    }
102042
 
#endif
102043
 
  }
102044
 
#ifndef NDEBUG
102045
 
  {
102046
 
    Bitmask toTheLeft = 0;
102047
 
    for(i=0; i<pTabList->nSrc; i++){
102048
 
      Bitmask m = getMask(pMaskSet, pTabList->a[i].iCursor);
102049
 
      assert( (m-1)==toTheLeft );
102050
 
      toTheLeft |= m;
102051
 
    }
102052
 
  }
102053
 
#endif
102054
 
 
102055
 
  /* Analyze all of the subexpressions.  Note that exprAnalyze() might
102056
 
  ** add new virtual terms onto the end of the WHERE clause.  We do not
102057
 
  ** want to analyze these virtual terms, so start analyzing at the end
102058
 
  ** and work forward so that the added virtual terms are never processed.
102059
 
  */
102060
 
  exprAnalyzeAll(pTabList, pWC);
102061
 
  if( db->mallocFailed ){
102062
 
    goto whereBeginError;
102063
 
  }
102064
 
 
102065
 
  /* Chose the best index to use for each table in the FROM clause.
102066
 
  **
102067
 
  ** This loop fills in the following fields:
102068
 
  **
102069
 
  **   pWInfo->a[].pIdx      The index to use for this level of the loop.
102070
 
  **   pWInfo->a[].wsFlags   WHERE_xxx flags associated with pIdx
102071
 
  **   pWInfo->a[].nEq       The number of == and IN constraints
102072
 
  **   pWInfo->a[].iFrom     Which term of the FROM clause is being coded
102073
 
  **   pWInfo->a[].iTabCur   The VDBE cursor for the database table
102074
 
  **   pWInfo->a[].iIdxCur   The VDBE cursor for the index
102075
 
  **   pWInfo->a[].pTerm     When wsFlags==WO_OR, the OR-clause term
102076
 
  **
102077
 
  ** This loop also figures out the nesting order of tables in the FROM
102078
 
  ** clause.
102079
 
  */
102080
 
  notReady = ~(Bitmask)0;
102081
 
  andFlags = ~0;
102082
 
  WHERETRACE(("*** Optimizer Start ***\n"));
102083
 
  for(i=iFrom=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
102084
 
    WhereCost bestPlan;         /* Most efficient plan seen so far */
102085
 
    Index *pIdx;                /* Index for FROM table at pTabItem */
102086
 
    int j;                      /* For looping over FROM tables */
102087
 
    int bestJ = -1;             /* The value of j */
102088
 
    Bitmask m;                  /* Bitmask value for j or bestJ */
102089
 
    int isOptimal;              /* Iterator for optimal/non-optimal search */
102090
 
    int nUnconstrained;         /* Number tables without INDEXED BY */
102091
 
    Bitmask notIndexed;         /* Mask of tables that cannot use an index */
102092
 
 
102093
 
    memset(&bestPlan, 0, sizeof(bestPlan));
102094
 
    bestPlan.rCost = SQLITE_BIG_DBL;
102095
 
    WHERETRACE(("*** Begin search for loop %d ***\n", i));
102096
 
 
102097
 
    /* Loop through the remaining entries in the FROM clause to find the
102098
 
    ** next nested loop. The loop tests all FROM clause entries
102099
 
    ** either once or twice. 
102100
 
    **
102101
 
    ** The first test is always performed if there are two or more entries
102102
 
    ** remaining and never performed if there is only one FROM clause entry
102103
 
    ** to choose from.  The first test looks for an "optimal" scan.  In
102104
 
    ** this context an optimal scan is one that uses the same strategy
102105
 
    ** for the given FROM clause entry as would be selected if the entry
102106
 
    ** were used as the innermost nested loop.  In other words, a table
102107
 
    ** is chosen such that the cost of running that table cannot be reduced
102108
 
    ** by waiting for other tables to run first.  This "optimal" test works
102109
 
    ** by first assuming that the FROM clause is on the inner loop and finding
102110
 
    ** its query plan, then checking to see if that query plan uses any
102111
 
    ** other FROM clause terms that are notReady.  If no notReady terms are
102112
 
    ** used then the "optimal" query plan works.
102113
 
    **
102114
 
    ** Note that the WhereCost.nRow parameter for an optimal scan might
102115
 
    ** not be as small as it would be if the table really were the innermost
102116
 
    ** join.  The nRow value can be reduced by WHERE clause constraints
102117
 
    ** that do not use indices.  But this nRow reduction only happens if the
102118
 
    ** table really is the innermost join.  
102119
 
    **
102120
 
    ** The second loop iteration is only performed if no optimal scan
102121
 
    ** strategies were found by the first iteration. This second iteration
102122
 
    ** is used to search for the lowest cost scan overall.
102123
 
    **
102124
 
    ** Previous versions of SQLite performed only the second iteration -
102125
 
    ** the next outermost loop was always that with the lowest overall
102126
 
    ** cost. However, this meant that SQLite could select the wrong plan
102127
 
    ** for scripts such as the following:
102128
 
    **   
102129
 
    **   CREATE TABLE t1(a, b); 
102130
 
    **   CREATE TABLE t2(c, d);
102131
 
    **   SELECT * FROM t2, t1 WHERE t2.rowid = t1.a;
102132
 
    **
102133
 
    ** The best strategy is to iterate through table t1 first. However it
102134
 
    ** is not possible to determine this with a simple greedy algorithm.
102135
 
    ** Since the cost of a linear scan through table t2 is the same 
102136
 
    ** as the cost of a linear scan through table t1, a simple greedy 
102137
 
    ** algorithm may choose to use t2 for the outer loop, which is a much
102138
 
    ** costlier approach.
102139
 
    */
102140
 
    nUnconstrained = 0;
102141
 
    notIndexed = 0;
102142
 
    for(isOptimal=(iFrom<nTabList-1); isOptimal>=0 && bestJ<0; isOptimal--){
102143
 
      Bitmask mask;             /* Mask of tables not yet ready */
102144
 
      for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){
102145
 
        int doNotReorder;    /* True if this table should not be reordered */
102146
 
        WhereCost sCost;     /* Cost information from best[Virtual]Index() */
102147
 
        ExprList *pOrderBy;  /* ORDER BY clause for index to optimize */
102148
 
  
102149
 
        doNotReorder =  (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0;
102150
 
        if( j!=iFrom && doNotReorder ) break;
102151
 
        m = getMask(pMaskSet, pTabItem->iCursor);
102152
 
        if( (m & notReady)==0 ){
102153
 
          if( j==iFrom ) iFrom++;
102154
 
          continue;
102155
 
        }
102156
 
        mask = (isOptimal ? m : notReady);
102157
 
        pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0);
102158
 
        if( pTabItem->pIndex==0 ) nUnconstrained++;
102159
 
  
102160
 
        WHERETRACE(("=== trying table %d with isOptimal=%d ===\n",
102161
 
                    j, isOptimal));
102162
 
        assert( pTabItem->pTab );
102163
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
102164
 
        if( IsVirtual(pTabItem->pTab) ){
102165
 
          sqlite3_index_info **pp = &pWInfo->a[j].pIdxInfo;
102166
 
          bestVirtualIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
102167
 
                           &sCost, pp);
102168
 
        }else 
102169
 
#endif
102170
 
        {
102171
 
          bestBtreeIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
102172
 
                         &sCost);
102173
 
        }
102174
 
        assert( isOptimal || (sCost.used&notReady)==0 );
102175
 
 
102176
 
        /* If an INDEXED BY clause is present, then the plan must use that
102177
 
        ** index if it uses any index at all */
102178
 
        assert( pTabItem->pIndex==0 
102179
 
                  || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
102180
 
                  || sCost.plan.u.pIdx==pTabItem->pIndex );
102181
 
 
102182
 
        if( isOptimal && (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
102183
 
          notIndexed |= m;
102184
 
        }
102185
 
 
102186
 
        /* Conditions under which this table becomes the best so far:
102187
 
        **
102188
 
        **   (1) The table must not depend on other tables that have not
102189
 
        **       yet run.
102190
 
        **
102191
 
        **   (2) A full-table-scan plan cannot supercede indexed plan unless
102192
 
        **       the full-table-scan is an "optimal" plan as defined above.
102193
 
        **
102194
 
        **   (3) All tables have an INDEXED BY clause or this table lacks an
102195
 
        **       INDEXED BY clause or this table uses the specific
102196
 
        **       index specified by its INDEXED BY clause.  This rule ensures
102197
 
        **       that a best-so-far is always selected even if an impossible
102198
 
        **       combination of INDEXED BY clauses are given.  The error
102199
 
        **       will be detected and relayed back to the application later.
102200
 
        **       The NEVER() comes about because rule (2) above prevents
102201
 
        **       An indexable full-table-scan from reaching rule (3).
102202
 
        **
102203
 
        **   (4) The plan cost must be lower than prior plans or else the
102204
 
        **       cost must be the same and the number of rows must be lower.
102205
 
        */
102206
 
        if( (sCost.used&notReady)==0                       /* (1) */
102207
 
            && (bestJ<0 || (notIndexed&m)!=0               /* (2) */
102208
 
                || (bestPlan.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
102209
 
                || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
102210
 
            && (nUnconstrained==0 || pTabItem->pIndex==0   /* (3) */
102211
 
                || NEVER((sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
102212
 
            && (bestJ<0 || sCost.rCost<bestPlan.rCost      /* (4) */
102213
 
                || (sCost.rCost<=bestPlan.rCost 
102214
 
                 && sCost.plan.nRow<bestPlan.plan.nRow))
102215
 
        ){
102216
 
          WHERETRACE(("=== table %d is best so far"
102217
 
                      " with cost=%g and nRow=%g\n",
102218
 
                      j, sCost.rCost, sCost.plan.nRow));
102219
 
          bestPlan = sCost;
102220
 
          bestJ = j;
102221
 
        }
102222
 
        if( doNotReorder ) break;
102223
 
      }
102224
 
    }
102225
 
    assert( bestJ>=0 );
102226
 
    assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
102227
 
    WHERETRACE(("*** Optimizer selects table %d for loop %d"
102228
 
                " with cost=%g and nRow=%g\n",
102229
 
                bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow));
102230
 
    if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){
102231
 
      *ppOrderBy = 0;
102232
 
    }
102233
 
    andFlags &= bestPlan.plan.wsFlags;
102234
 
    pLevel->plan = bestPlan.plan;
102235
 
    testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
102236
 
    testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
102237
 
    if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){
102238
 
      pLevel->iIdxCur = pParse->nTab++;
102239
 
    }else{
102240
 
      pLevel->iIdxCur = -1;
102241
 
    }
102242
 
    notReady &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
102243
 
    pLevel->iFrom = (u8)bestJ;
102244
 
    if( bestPlan.plan.nRow>=(double)1 ){
102245
 
      pParse->nQueryLoop *= bestPlan.plan.nRow;
102246
 
    }
102247
 
 
102248
 
    /* Check that if the table scanned by this loop iteration had an
102249
 
    ** INDEXED BY clause attached to it, that the named index is being
102250
 
    ** used for the scan. If not, then query compilation has failed.
102251
 
    ** Return an error.
102252
 
    */
102253
 
    pIdx = pTabList->a[bestJ].pIndex;
102254
 
    if( pIdx ){
102255
 
      if( (bestPlan.plan.wsFlags & WHERE_INDEXED)==0 ){
102256
 
        sqlite3ErrorMsg(pParse, "cannot use index: %s", pIdx->zName);
102257
 
        goto whereBeginError;
102258
 
      }else{
102259
 
        /* If an INDEXED BY clause is used, the bestIndex() function is
102260
 
        ** guaranteed to find the index specified in the INDEXED BY clause
102261
 
        ** if it find an index at all. */
102262
 
        assert( bestPlan.plan.u.pIdx==pIdx );
102263
 
      }
102264
 
    }
102265
 
  }
102266
 
  WHERETRACE(("*** Optimizer Finished ***\n"));
102267
 
  if( pParse->nErr || db->mallocFailed ){
102268
 
    goto whereBeginError;
102269
 
  }
102270
 
 
102271
 
  /* If the total query only selects a single row, then the ORDER BY
102272
 
  ** clause is irrelevant.
102273
 
  */
102274
 
  if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
102275
 
    *ppOrderBy = 0;
102276
 
  }
102277
 
 
102278
 
  /* If the caller is an UPDATE or DELETE statement that is requesting
102279
 
  ** to use a one-pass algorithm, determine if this is appropriate.
102280
 
  ** The one-pass algorithm only works if the WHERE clause constraints
102281
 
  ** the statement to update a single row.
102282
 
  */
102283
 
  assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
102284
 
  if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){
102285
 
    pWInfo->okOnePass = 1;
102286
 
    pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY;
102287
 
  }
102288
 
 
102289
 
  /* Open all tables in the pTabList and any indices selected for
102290
 
  ** searching those tables.
102291
 
  */
102292
 
  sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
102293
 
  notReady = ~(Bitmask)0;
102294
 
  pWInfo->nRowOut = (double)1;
102295
 
  for(i=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
102296
 
    Table *pTab;     /* Table to open */
102297
 
    int iDb;         /* Index of database containing table/index */
102298
 
 
102299
 
    pTabItem = &pTabList->a[pLevel->iFrom];
102300
 
    pTab = pTabItem->pTab;
102301
 
    pLevel->iTabCur = pTabItem->iCursor;
102302
 
    pWInfo->nRowOut *= pLevel->plan.nRow;
102303
 
    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
102304
 
    if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
102305
 
      /* Do nothing */
102306
 
    }else
102307
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
102308
 
    if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
102309
 
      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
102310
 
      int iCur = pTabItem->iCursor;
102311
 
      sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
102312
 
    }else
102313
 
#endif
102314
 
    if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
102315
 
         && (wctrlFlags & WHERE_OMIT_OPEN)==0 ){
102316
 
      int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
102317
 
      sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
102318
 
      testcase( pTab->nCol==BMS-1 );
102319
 
      testcase( pTab->nCol==BMS );
102320
 
      if( !pWInfo->okOnePass && pTab->nCol<BMS ){
102321
 
        Bitmask b = pTabItem->colUsed;
102322
 
        int n = 0;
102323
 
        for(; b; b=b>>1, n++){}
102324
 
        sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, 
102325
 
                            SQLITE_INT_TO_PTR(n), P4_INT32);
102326
 
        assert( n<=pTab->nCol );
102327
 
      }
102328
 
    }else{
102329
 
      sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
102330
 
    }
102331
 
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
102332
 
    if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){
102333
 
      constructAutomaticIndex(pParse, pWC, pTabItem, notReady, pLevel);
102334
 
    }else
102335
 
#endif
102336
 
    if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
102337
 
      Index *pIx = pLevel->plan.u.pIdx;
102338
 
      KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
102339
 
      int iIdxCur = pLevel->iIdxCur;
102340
 
      assert( pIx->pSchema==pTab->pSchema );
102341
 
      assert( iIdxCur>=0 );
102342
 
      sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIx->tnum, iDb,
102343
 
                        (char*)pKey, P4_KEYINFO_HANDOFF);
102344
 
      VdbeComment((v, "%s", pIx->zName));
102345
 
    }
102346
 
    sqlite3CodeVerifySchema(pParse, iDb);
102347
 
    notReady &= ~getMask(pWC->pMaskSet, pTabItem->iCursor);
102348
 
  }
102349
 
  pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
102350
 
  if( db->mallocFailed ) goto whereBeginError;
102351
 
 
102352
 
  /* Generate the code to do the search.  Each iteration of the for
102353
 
  ** loop below generates code for a single nested loop of the VM
102354
 
  ** program.
102355
 
  */
102356
 
  notReady = ~(Bitmask)0;
102357
 
  for(i=0; i<nTabList; i++){
102358
 
    pLevel = &pWInfo->a[i];
102359
 
    explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags);
102360
 
    notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady);
102361
 
    pWInfo->iContinue = pLevel->addrCont;
102362
 
  }
102363
 
 
102364
 
#ifdef SQLITE_TEST  /* For testing and debugging use only */
102365
 
  /* Record in the query plan information about the current table
102366
 
  ** and the index used to access it (if any).  If the table itself
102367
 
  ** is not used, its name is just '{}'.  If no index is used
102368
 
  ** the index is listed as "{}".  If the primary key is used the
102369
 
  ** index name is '*'.
102370
 
  */
102371
 
  for(i=0; i<nTabList; i++){
102372
 
    char *z;
102373
 
    int n;
102374
 
    pLevel = &pWInfo->a[i];
102375
 
    pTabItem = &pTabList->a[pLevel->iFrom];
102376
 
    z = pTabItem->zAlias;
102377
 
    if( z==0 ) z = pTabItem->pTab->zName;
102378
 
    n = sqlite3Strlen30(z);
102379
 
    if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
102380
 
      if( pLevel->plan.wsFlags & WHERE_IDX_ONLY ){
102381
 
        memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
102382
 
        nQPlan += 2;
102383
 
      }else{
102384
 
        memcpy(&sqlite3_query_plan[nQPlan], z, n);
102385
 
        nQPlan += n;
102386
 
      }
102387
 
      sqlite3_query_plan[nQPlan++] = ' ';
102388
 
    }
102389
 
    testcase( pLevel->plan.wsFlags & WHERE_ROWID_EQ );
102390
 
    testcase( pLevel->plan.wsFlags & WHERE_ROWID_RANGE );
102391
 
    if( pLevel->plan.wsFlags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
102392
 
      memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
102393
 
      nQPlan += 2;
102394
 
    }else if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
102395
 
      n = sqlite3Strlen30(pLevel->plan.u.pIdx->zName);
102396
 
      if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
102397
 
        memcpy(&sqlite3_query_plan[nQPlan], pLevel->plan.u.pIdx->zName, n);
102398
 
        nQPlan += n;
102399
 
        sqlite3_query_plan[nQPlan++] = ' ';
102400
 
      }
102401
 
    }else{
102402
 
      memcpy(&sqlite3_query_plan[nQPlan], "{} ", 3);
102403
 
      nQPlan += 3;
102404
 
    }
102405
 
  }
102406
 
  while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
102407
 
    sqlite3_query_plan[--nQPlan] = 0;
102408
 
  }
102409
 
  sqlite3_query_plan[nQPlan] = 0;
102410
 
  nQPlan = 0;
102411
 
#endif /* SQLITE_TEST // Testing and debugging use only */
102412
 
 
102413
 
  /* Record the continuation address in the WhereInfo structure.  Then
102414
 
  ** clean up and return.
102415
 
  */
102416
 
  return pWInfo;
102417
 
 
102418
 
  /* Jump here if malloc fails */
102419
 
whereBeginError:
102420
 
  if( pWInfo ){
102421
 
    pParse->nQueryLoop = pWInfo->savedNQueryLoop;
102422
 
    whereInfoFree(db, pWInfo);
102423
 
  }
102424
 
  return 0;
102425
 
}
102426
 
 
102427
 
/*
102428
 
** Generate the end of the WHERE loop.  See comments on 
102429
 
** sqlite3WhereBegin() for additional information.
102430
 
*/
102431
 
SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
102432
 
  Parse *pParse = pWInfo->pParse;
102433
 
  Vdbe *v = pParse->pVdbe;
102434
 
  int i;
102435
 
  WhereLevel *pLevel;
102436
 
  SrcList *pTabList = pWInfo->pTabList;
102437
 
  sqlite3 *db = pParse->db;
102438
 
 
102439
 
  /* Generate loop termination code.
102440
 
  */
102441
 
  sqlite3ExprCacheClear(pParse);
102442
 
  for(i=pWInfo->nLevel-1; i>=0; i--){
102443
 
    pLevel = &pWInfo->a[i];
102444
 
    sqlite3VdbeResolveLabel(v, pLevel->addrCont);
102445
 
    if( pLevel->op!=OP_Noop ){
102446
 
      sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2);
102447
 
      sqlite3VdbeChangeP5(v, pLevel->p5);
102448
 
    }
102449
 
    if( pLevel->plan.wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
102450
 
      struct InLoop *pIn;
102451
 
      int j;
102452
 
      sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
102453
 
      for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
102454
 
        sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
102455
 
        sqlite3VdbeAddOp2(v, OP_Next, pIn->iCur, pIn->addrInTop);
102456
 
        sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
102457
 
      }
102458
 
      sqlite3DbFree(db, pLevel->u.in.aInLoop);
102459
 
    }
102460
 
    sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
102461
 
    if( pLevel->iLeftJoin ){
102462
 
      int addr;
102463
 
      addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin);
102464
 
      assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
102465
 
           || (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 );
102466
 
      if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 ){
102467
 
        sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
102468
 
      }
102469
 
      if( pLevel->iIdxCur>=0 ){
102470
 
        sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
102471
 
      }
102472
 
      if( pLevel->op==OP_Return ){
102473
 
        sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
102474
 
      }else{
102475
 
        sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
102476
 
      }
102477
 
      sqlite3VdbeJumpHere(v, addr);
102478
 
    }
102479
 
  }
102480
 
 
102481
 
  /* The "break" point is here, just past the end of the outer loop.
102482
 
  ** Set it.
102483
 
  */
102484
 
  sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
102485
 
 
102486
 
  /* Close all of the cursors that were opened by sqlite3WhereBegin.
102487
 
  */
102488
 
  assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc );
102489
 
  for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
102490
 
    struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
102491
 
    Table *pTab = pTabItem->pTab;
102492
 
    assert( pTab!=0 );
102493
 
    if( (pTab->tabFlags & TF_Ephemeral)==0
102494
 
     && pTab->pSelect==0
102495
 
     && (pWInfo->wctrlFlags & WHERE_OMIT_CLOSE)==0
102496
 
    ){
102497
 
      int ws = pLevel->plan.wsFlags;
102498
 
      if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
102499
 
        sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
102500
 
      }
102501
 
      if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){
102502
 
        sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
102503
 
      }
102504
 
    }
102505
 
 
102506
 
    /* If this scan uses an index, make code substitutions to read data
102507
 
    ** from the index in preference to the table. Sometimes, this means
102508
 
    ** the table need never be read from. This is a performance boost,
102509
 
    ** as the vdbe level waits until the table is read before actually
102510
 
    ** seeking the table cursor to the record corresponding to the current
102511
 
    ** position in the index.
102512
 
    ** 
102513
 
    ** Calls to the code generator in between sqlite3WhereBegin and
102514
 
    ** sqlite3WhereEnd will have created code that references the table
102515
 
    ** directly.  This loop scans all that code looking for opcodes
102516
 
    ** that reference the table and converts them into opcodes that
102517
 
    ** reference the index.
102518
 
    */
102519
 
    if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 && !db->mallocFailed){
102520
 
      int k, j, last;
102521
 
      VdbeOp *pOp;
102522
 
      Index *pIdx = pLevel->plan.u.pIdx;
102523
 
 
102524
 
      assert( pIdx!=0 );
102525
 
      pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
102526
 
      last = sqlite3VdbeCurrentAddr(v);
102527
 
      for(k=pWInfo->iTop; k<last; k++, pOp++){
102528
 
        if( pOp->p1!=pLevel->iTabCur ) continue;
102529
 
        if( pOp->opcode==OP_Column ){
102530
 
          for(j=0; j<pIdx->nColumn; j++){
102531
 
            if( pOp->p2==pIdx->aiColumn[j] ){
102532
 
              pOp->p2 = j;
102533
 
              pOp->p1 = pLevel->iIdxCur;
102534
 
              break;
102535
 
            }
102536
 
          }
102537
 
          assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
102538
 
               || j<pIdx->nColumn );
102539
 
        }else if( pOp->opcode==OP_Rowid ){
102540
 
          pOp->p1 = pLevel->iIdxCur;
102541
 
          pOp->opcode = OP_IdxRowid;
102542
 
        }
102543
 
      }
102544
 
    }
102545
 
  }
102546
 
 
102547
 
  /* Final cleanup
102548
 
  */
102549
 
  pParse->nQueryLoop = pWInfo->savedNQueryLoop;
102550
 
  whereInfoFree(db, pWInfo);
102551
 
  return;
102552
 
}
102553
 
 
102554
 
/************** End of where.c ***********************************************/
102555
 
/************** Begin file parse.c *******************************************/
102556
 
/* Driver template for the LEMON parser generator.
102557
 
** The author disclaims copyright to this source code.
102558
 
**
102559
 
** This version of "lempar.c" is modified, slightly, for use by SQLite.
102560
 
** The only modifications are the addition of a couple of NEVER()
102561
 
** macros to disable tests that are needed in the case of a general
102562
 
** LALR(1) grammar but which are always false in the
102563
 
** specific grammar used by SQLite.
102564
 
*/
102565
 
/* First off, code is included that follows the "include" declaration
102566
 
** in the input grammar file. */
102567
 
 
102568
 
 
102569
 
/*
102570
 
** Disable all error recovery processing in the parser push-down
102571
 
** automaton.
102572
 
*/
102573
 
#define YYNOERRORRECOVERY 1
102574
 
 
102575
 
/*
102576
 
** Make yytestcase() the same as testcase()
102577
 
*/
102578
 
#define yytestcase(X) testcase(X)
102579
 
 
102580
 
/*
102581
 
** An instance of this structure holds information about the
102582
 
** LIMIT clause of a SELECT statement.
102583
 
*/
102584
 
struct LimitVal {
102585
 
  Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
102586
 
  Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
102587
 
};
102588
 
 
102589
 
/*
102590
 
** An instance of this structure is used to store the LIKE,
102591
 
** GLOB, NOT LIKE, and NOT GLOB operators.
102592
 
*/
102593
 
struct LikeOp {
102594
 
  Token eOperator;  /* "like" or "glob" or "regexp" */
102595
 
  int not;         /* True if the NOT keyword is present */
102596
 
};
102597
 
 
102598
 
/*
102599
 
** An instance of the following structure describes the event of a
102600
 
** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
102601
 
** TK_DELETE, or TK_INSTEAD.  If the event is of the form
102602
 
**
102603
 
**      UPDATE ON (a,b,c)
102604
 
**
102605
 
** Then the "b" IdList records the list "a,b,c".
102606
 
*/
102607
 
struct TrigEvent { int a; IdList * b; };
102608
 
 
102609
 
/*
102610
 
** An instance of this structure holds the ATTACH key and the key type.
102611
 
*/
102612
 
struct AttachKey { int type;  Token key; };
102613
 
 
102614
 
 
102615
 
  /* This is a utility routine used to set the ExprSpan.zStart and
102616
 
  ** ExprSpan.zEnd values of pOut so that the span covers the complete
102617
 
  ** range of text beginning with pStart and going to the end of pEnd.
102618
 
  */
102619
 
  static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
102620
 
    pOut->zStart = pStart->z;
102621
 
    pOut->zEnd = &pEnd->z[pEnd->n];
102622
 
  }
102623
 
 
102624
 
  /* Construct a new Expr object from a single identifier.  Use the
102625
 
  ** new Expr to populate pOut.  Set the span of pOut to be the identifier
102626
 
  ** that created the expression.
102627
 
  */
102628
 
  static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
102629
 
    pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
102630
 
    pOut->zStart = pValue->z;
102631
 
    pOut->zEnd = &pValue->z[pValue->n];
102632
 
  }
102633
 
 
102634
 
  /* This routine constructs a binary expression node out of two ExprSpan
102635
 
  ** objects and uses the result to populate a new ExprSpan object.
102636
 
  */
102637
 
  static void spanBinaryExpr(
102638
 
    ExprSpan *pOut,     /* Write the result here */
102639
 
    Parse *pParse,      /* The parsing context.  Errors accumulate here */
102640
 
    int op,             /* The binary operation */
102641
 
    ExprSpan *pLeft,    /* The left operand */
102642
 
    ExprSpan *pRight    /* The right operand */
102643
 
  ){
102644
 
    pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
102645
 
    pOut->zStart = pLeft->zStart;
102646
 
    pOut->zEnd = pRight->zEnd;
102647
 
  }
102648
 
 
102649
 
  /* Construct an expression node for a unary postfix operator
102650
 
  */
102651
 
  static void spanUnaryPostfix(
102652
 
    ExprSpan *pOut,        /* Write the new expression node here */
102653
 
    Parse *pParse,         /* Parsing context to record errors */
102654
 
    int op,                /* The operator */
102655
 
    ExprSpan *pOperand,    /* The operand */
102656
 
    Token *pPostOp         /* The operand token for setting the span */
102657
 
  ){
102658
 
    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
102659
 
    pOut->zStart = pOperand->zStart;
102660
 
    pOut->zEnd = &pPostOp->z[pPostOp->n];
102661
 
  }                           
102662
 
 
102663
 
  /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
102664
 
  ** unary TK_ISNULL or TK_NOTNULL expression. */
102665
 
  static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
102666
 
    sqlite3 *db = pParse->db;
102667
 
    if( db->mallocFailed==0 && pY->op==TK_NULL ){
102668
 
      pA->op = (u8)op;
102669
 
      sqlite3ExprDelete(db, pA->pRight);
102670
 
      pA->pRight = 0;
102671
 
    }
102672
 
  }
102673
 
 
102674
 
  /* Construct an expression node for a unary prefix operator
102675
 
  */
102676
 
  static void spanUnaryPrefix(
102677
 
    ExprSpan *pOut,        /* Write the new expression node here */
102678
 
    Parse *pParse,         /* Parsing context to record errors */
102679
 
    int op,                /* The operator */
102680
 
    ExprSpan *pOperand,    /* The operand */
102681
 
    Token *pPreOp         /* The operand token for setting the span */
102682
 
  ){
102683
 
    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
102684
 
    pOut->zStart = pPreOp->z;
102685
 
    pOut->zEnd = pOperand->zEnd;
102686
 
  }
102687
 
/* Next is all token values, in a form suitable for use by makeheaders.
102688
 
** This section will be null unless lemon is run with the -m switch.
102689
 
*/
102690
 
/* 
102691
 
** These constants (all generated automatically by the parser generator)
102692
 
** specify the various kinds of tokens (terminals) that the parser
102693
 
** understands. 
102694
 
**
102695
 
** Each symbol here is a terminal symbol in the grammar.
102696
 
*/
102697
 
/* Make sure the INTERFACE macro is defined.
102698
 
*/
102699
 
#ifndef INTERFACE
102700
 
# define INTERFACE 1
102701
 
#endif
102702
 
/* The next thing included is series of defines which control
102703
 
** various aspects of the generated parser.
102704
 
**    YYCODETYPE         is the data type used for storing terminal
102705
 
**                       and nonterminal numbers.  "unsigned char" is
102706
 
**                       used if there are fewer than 250 terminals
102707
 
**                       and nonterminals.  "int" is used otherwise.
102708
 
**    YYNOCODE           is a number of type YYCODETYPE which corresponds
102709
 
**                       to no legal terminal or nonterminal number.  This
102710
 
**                       number is used to fill in empty slots of the hash 
102711
 
**                       table.
102712
 
**    YYFALLBACK         If defined, this indicates that one or more tokens
102713
 
**                       have fall-back values which should be used if the
102714
 
**                       original value of the token will not parse.
102715
 
**    YYACTIONTYPE       is the data type used for storing terminal
102716
 
**                       and nonterminal numbers.  "unsigned char" is
102717
 
**                       used if there are fewer than 250 rules and
102718
 
**                       states combined.  "int" is used otherwise.
102719
 
**    sqlite3ParserTOKENTYPE     is the data type used for minor tokens given 
102720
 
**                       directly to the parser from the tokenizer.
102721
 
**    YYMINORTYPE        is the data type used for all minor tokens.
102722
 
**                       This is typically a union of many types, one of
102723
 
**                       which is sqlite3ParserTOKENTYPE.  The entry in the union
102724
 
**                       for base tokens is called "yy0".
102725
 
**    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
102726
 
**                       zero the stack is dynamically sized using realloc()
102727
 
**    sqlite3ParserARG_SDECL     A static variable declaration for the %extra_argument
102728
 
**    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
102729
 
**    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
102730
 
**    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
102731
 
**    YYNSTATE           the combined number of states.
102732
 
**    YYNRULE            the number of rules in the grammar
102733
 
**    YYERRORSYMBOL      is the code number of the error symbol.  If not
102734
 
**                       defined, then do no error processing.
102735
 
*/
102736
 
#define YYCODETYPE unsigned char
102737
 
#define YYNOCODE 253
102738
 
#define YYACTIONTYPE unsigned short int
102739
 
#define YYWILDCARD 67
102740
 
#define sqlite3ParserTOKENTYPE Token
102741
 
typedef union {
102742
 
  int yyinit;
102743
 
  sqlite3ParserTOKENTYPE yy0;
102744
 
  int yy4;
102745
 
  struct TrigEvent yy90;
102746
 
  ExprSpan yy118;
102747
 
  TriggerStep* yy203;
102748
 
  u8 yy210;
102749
 
  struct {int value; int mask;} yy215;
102750
 
  SrcList* yy259;
102751
 
  struct LimitVal yy292;
102752
 
  Expr* yy314;
102753
 
  ExprList* yy322;
102754
 
  struct LikeOp yy342;
102755
 
  IdList* yy384;
102756
 
  Select* yy387;
102757
 
} YYMINORTYPE;
102758
 
#ifndef YYSTACKDEPTH
102759
 
#define YYSTACKDEPTH 100
102760
 
#endif
102761
 
#define sqlite3ParserARG_SDECL Parse *pParse;
102762
 
#define sqlite3ParserARG_PDECL ,Parse *pParse
102763
 
#define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
102764
 
#define sqlite3ParserARG_STORE yypParser->pParse = pParse
102765
 
#define YYNSTATE 630
102766
 
#define YYNRULE 329
102767
 
#define YYFALLBACK 1
102768
 
#define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
102769
 
#define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
102770
 
#define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
102771
 
 
102772
 
/* The yyzerominor constant is used to initialize instances of
102773
 
** YYMINORTYPE objects to zero. */
102774
 
static const YYMINORTYPE yyzerominor = { 0 };
102775
 
 
102776
 
/* Define the yytestcase() macro to be a no-op if is not already defined
102777
 
** otherwise.
102778
 
**
102779
 
** Applications can choose to define yytestcase() in the %include section
102780
 
** to a macro that can assist in verifying code coverage.  For production
102781
 
** code the yytestcase() macro should be turned off.  But it is useful
102782
 
** for testing.
102783
 
*/
102784
 
#ifndef yytestcase
102785
 
# define yytestcase(X)
102786
 
#endif
102787
 
 
102788
 
 
102789
 
/* Next are the tables used to determine what action to take based on the
102790
 
** current state and lookahead token.  These tables are used to implement
102791
 
** functions that take a state number and lookahead value and return an
102792
 
** action integer.  
102793
 
**
102794
 
** Suppose the action integer is N.  Then the action is determined as
102795
 
** follows
102796
 
**
102797
 
**   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
102798
 
**                                      token onto the stack and goto state N.
102799
 
**
102800
 
**   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
102801
 
**
102802
 
**   N == YYNSTATE+YYNRULE              A syntax error has occurred.
102803
 
**
102804
 
**   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
102805
 
**
102806
 
**   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
102807
 
**                                      slots in the yy_action[] table.
102808
 
**
102809
 
** The action table is constructed as a single large table named yy_action[].
102810
 
** Given state S and lookahead X, the action is computed as
102811
 
**
102812
 
**      yy_action[ yy_shift_ofst[S] + X ]
102813
 
**
102814
 
** If the index value yy_shift_ofst[S]+X is out of range or if the value
102815
 
** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
102816
 
** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
102817
 
** and that yy_default[S] should be used instead.  
102818
 
**
102819
 
** The formula above is for computing the action when the lookahead is
102820
 
** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
102821
 
** a reduce action) then the yy_reduce_ofst[] array is used in place of
102822
 
** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
102823
 
** YY_SHIFT_USE_DFLT.
102824
 
**
102825
 
** The following are the tables generated in this section:
102826
 
**
102827
 
**  yy_action[]        A single table containing all actions.
102828
 
**  yy_lookahead[]     A table containing the lookahead for each entry in
102829
 
**                     yy_action.  Used to detect hash collisions.
102830
 
**  yy_shift_ofst[]    For each state, the offset into yy_action for
102831
 
**                     shifting terminals.
102832
 
**  yy_reduce_ofst[]   For each state, the offset into yy_action for
102833
 
**                     shifting non-terminals after a reduce.
102834
 
**  yy_default[]       Default action for each state.
102835
 
*/
102836
 
#define YY_ACTTAB_COUNT (1557)
102837
 
static const YYACTIONTYPE yy_action[] = {
102838
 
 /*     0 */   313,  960,  186,  419,    2,  172,  627,  597,   55,   55,
102839
 
 /*    10 */    55,   55,   48,   53,   53,   53,   53,   52,   52,   51,
102840
 
 /*    20 */    51,   51,   50,  238,  302,  283,  623,  622,  516,  515,
102841
 
 /*    30 */   590,  584,   55,   55,   55,   55,  282,   53,   53,   53,
102842
 
 /*    40 */    53,   52,   52,   51,   51,   51,   50,  238,    6,   56,
102843
 
 /*    50 */    57,   47,  582,  581,  583,  583,   54,   54,   55,   55,
102844
 
 /*    60 */    55,   55,  608,   53,   53,   53,   53,   52,   52,   51,
102845
 
 /*    70 */    51,   51,   50,  238,  313,  597,  409,  330,  579,  579,
102846
 
 /*    80 */    32,   53,   53,   53,   53,   52,   52,   51,   51,   51,
102847
 
 /*    90 */    50,  238,  330,  217,  620,  619,  166,  411,  624,  382,
102848
 
 /*   100 */   379,  378,    7,  491,  590,  584,  200,  199,  198,   58,
102849
 
 /*   110 */   377,  300,  414,  621,  481,   66,  623,  622,  621,  580,
102850
 
 /*   120 */   254,  601,   94,   56,   57,   47,  582,  581,  583,  583,
102851
 
 /*   130 */    54,   54,   55,   55,   55,   55,  671,   53,   53,   53,
102852
 
 /*   140 */    53,   52,   52,   51,   51,   51,   50,  238,  313,  532,
102853
 
 /*   150 */   226,  506,  507,  133,  177,  139,  284,  385,  279,  384,
102854
 
 /*   160 */   169,  197,  342,  398,  251,  226,  253,  275,  388,  167,
102855
 
 /*   170 */   139,  284,  385,  279,  384,  169,  570,  236,  590,  584,
102856
 
 /*   180 */   672,  240,  275,  157,  620,  619,  554,  437,   51,   51,
102857
 
 /*   190 */    51,   50,  238,  343,  439,  553,  438,   56,   57,   47,
102858
 
 /*   200 */   582,  581,  583,  583,   54,   54,   55,   55,   55,   55,
102859
 
 /*   210 */   465,   53,   53,   53,   53,   52,   52,   51,   51,   51,
102860
 
 /*   220 */    50,  238,  313,  390,   52,   52,   51,   51,   51,   50,
102861
 
 /*   230 */   238,  391,  166,  491,  566,  382,  379,  378,  409,  440,
102862
 
 /*   240 */   579,  579,  252,  440,  607,   66,  377,  513,  621,   49,
102863
 
 /*   250 */    46,  147,  590,  584,  621,   16,  466,  189,  621,  441,
102864
 
 /*   260 */   442,  673,  526,  441,  340,  577,  595,   64,  194,  482,
102865
 
 /*   270 */   434,   56,   57,   47,  582,  581,  583,  583,   54,   54,
102866
 
 /*   280 */    55,   55,   55,   55,   30,   53,   53,   53,   53,   52,
102867
 
 /*   290 */    52,   51,   51,   51,   50,  238,  313,  593,  593,  593,
102868
 
 /*   300 */   387,  578,  606,  493,  259,  351,  258,  411,    1,  623,
102869
 
 /*   310 */   622,  496,  623,  622,   65,  240,  623,  622,  597,  443,
102870
 
 /*   320 */   237,  239,  414,  341,  237,  602,  590,  584,   18,  603,
102871
 
 /*   330 */   166,  601,   87,  382,  379,  378,   67,  623,  622,   38,
102872
 
 /*   340 */   623,  622,  176,  270,  377,   56,   57,   47,  582,  581,
102873
 
 /*   350 */   583,  583,   54,   54,   55,   55,   55,   55,  175,   53,
102874
 
 /*   360 */    53,   53,   53,   52,   52,   51,   51,   51,   50,  238,
102875
 
 /*   370 */   313,  396,  233,  411,  531,  565,  317,  620,  619,   44,
102876
 
 /*   380 */   620,  619,  240,  206,  620,  619,  597,  266,  414,  268,
102877
 
 /*   390 */   409,  597,  579,  579,  352,  184,  505,  601,   73,  533,
102878
 
 /*   400 */   590,  584,  466,  548,  190,  620,  619,  576,  620,  619,
102879
 
 /*   410 */   547,  383,  551,   35,  332,  575,  574,  600,  504,   56,
102880
 
 /*   420 */    57,   47,  582,  581,  583,  583,   54,   54,   55,   55,
102881
 
 /*   430 */    55,   55,  567,   53,   53,   53,   53,   52,   52,   51,
102882
 
 /*   440 */    51,   51,   50,  238,  313,  411,  561,  561,  528,  364,
102883
 
 /*   450 */   259,  351,  258,  183,  361,  549,  524,  374,  411,  597,
102884
 
 /*   460 */   414,  240,  560,  560,  409,  604,  579,  579,  328,  601,
102885
 
 /*   470 */    93,  623,  622,  414,  590,  584,  237,  564,  559,  559,
102886
 
 /*   480 */   520,  402,  601,   87,  409,  210,  579,  579,  168,  421,
102887
 
 /*   490 */   950,  519,  950,   56,   57,   47,  582,  581,  583,  583,
102888
 
 /*   500 */    54,   54,   55,   55,   55,   55,  192,   53,   53,   53,
102889
 
 /*   510 */    53,   52,   52,   51,   51,   51,   50,  238,  313,  600,
102890
 
 /*   520 */   293,  563,  511,  234,  357,  146,  475,  475,  367,  411,
102891
 
 /*   530 */   562,  411,  358,  542,  425,  171,  411,  215,  144,  620,
102892
 
 /*   540 */   619,  544,  318,  353,  414,  203,  414,  275,  590,  584,
102893
 
 /*   550 */   549,  414,  174,  601,   94,  601,   79,  558,  471,   61,
102894
 
 /*   560 */   601,   79,  421,  949,  350,  949,   34,   56,   57,   47,
102895
 
 /*   570 */   582,  581,  583,  583,   54,   54,   55,   55,   55,   55,
102896
 
 /*   580 */   535,   53,   53,   53,   53,   52,   52,   51,   51,   51,
102897
 
 /*   590 */    50,  238,  313,  307,  424,  394,  272,   49,   46,  147,
102898
 
 /*   600 */   349,  322,    4,  411,  491,  312,  321,  425,  568,  492,
102899
 
 /*   610 */   216,  264,  407,  575,  574,  429,   66,  549,  414,  621,
102900
 
 /*   620 */   540,  602,  590,  584,   13,  603,  621,  601,   72,   12,
102901
 
 /*   630 */   618,  617,  616,  202,  210,  621,  546,  469,  422,  319,
102902
 
 /*   640 */   148,   56,   57,   47,  582,  581,  583,  583,   54,   54,
102903
 
 /*   650 */    55,   55,   55,   55,  338,   53,   53,   53,   53,   52,
102904
 
 /*   660 */    52,   51,   51,   51,   50,  238,  313,  600,  600,  411,
102905
 
 /*   670 */    39,   21,   37,  170,  237,  875,  411,  572,  572,  201,
102906
 
 /*   680 */   144,  473,  538,  331,  414,  474,  143,  146,  630,  628,
102907
 
 /*   690 */   334,  414,  353,  601,   68,  168,  590,  584,  132,  365,
102908
 
 /*   700 */   601,   96,  307,  423,  530,  336,   49,   46,  147,  568,
102909
 
 /*   710 */   406,  216,  549,  360,  529,   56,   57,   47,  582,  581,
102910
 
 /*   720 */   583,  583,   54,   54,   55,   55,   55,   55,  411,   53,
102911
 
 /*   730 */    53,   53,   53,   52,   52,   51,   51,   51,   50,  238,
102912
 
 /*   740 */   313,  411,  605,  414,  484,  510,  172,  422,  597,  318,
102913
 
 /*   750 */   496,  485,  601,   99,  411,  142,  414,  411,  231,  411,
102914
 
 /*   760 */   540,  411,  359,  629,    2,  601,   97,  426,  308,  414,
102915
 
 /*   770 */   590,  584,  414,   20,  414,  621,  414,  621,  601,  106,
102916
 
 /*   780 */   503,  601,  105,  601,  108,  601,  109,  204,   28,   56,
102917
 
 /*   790 */    57,   47,  582,  581,  583,  583,   54,   54,   55,   55,
102918
 
 /*   800 */    55,   55,  411,   53,   53,   53,   53,   52,   52,   51,
102919
 
 /*   810 */    51,   51,   50,  238,  313,  411,  597,  414,  411,  276,
102920
 
 /*   820 */   214,  600,  411,  366,  213,  381,  601,  134,  274,  500,
102921
 
 /*   830 */   414,  167,  130,  414,  621,  411,  354,  414,  376,  601,
102922
 
 /*   840 */   135,  129,  601,  100,  590,  584,  601,  104,  522,  521,
102923
 
 /*   850 */   414,  621,  224,  273,  600,  167,  327,  282,  600,  601,
102924
 
 /*   860 */   103,  468,  521,   56,   57,   47,  582,  581,  583,  583,
102925
 
 /*   870 */    54,   54,   55,   55,   55,   55,  411,   53,   53,   53,
102926
 
 /*   880 */    53,   52,   52,   51,   51,   51,   50,  238,  313,  411,
102927
 
 /*   890 */    27,  414,  411,  375,  276,  167,  359,  544,   50,  238,
102928
 
 /*   900 */   601,   95,  128,  223,  414,  411,  165,  414,  411,  621,
102929
 
 /*   910 */   411,  621,  612,  601,  102,  372,  601,   76,  590,  584,
102930
 
 /*   920 */   414,  570,  236,  414,  470,  414,  167,  621,  188,  601,
102931
 
 /*   930 */    98,  225,  601,  138,  601,  137,  232,   56,   45,   47,
102932
 
 /*   940 */   582,  581,  583,  583,   54,   54,   55,   55,   55,   55,
102933
 
 /*   950 */   411,   53,   53,   53,   53,   52,   52,   51,   51,   51,
102934
 
 /*   960 */    50,  238,  313,  276,  276,  414,  411,  276,  544,  459,
102935
 
 /*   970 */   359,  171,  209,  479,  601,  136,  628,  334,  621,  621,
102936
 
 /*   980 */   125,  414,  621,  368,  411,  621,  257,  540,  589,  588,
102937
 
 /*   990 */   601,   75,  590,  584,  458,  446,   23,   23,  124,  414,
102938
 
 /*  1000 */   326,  325,  621,  427,  324,  309,  600,  288,  601,   92,
102939
 
 /*  1010 */   586,  585,   57,   47,  582,  581,  583,  583,   54,   54,
102940
 
 /*  1020 */    55,   55,   55,   55,  411,   53,   53,   53,   53,   52,
102941
 
 /*  1030 */    52,   51,   51,   51,   50,  238,  313,  587,  411,  414,
102942
 
 /*  1040 */   411,  207,  611,  476,  171,  472,  160,  123,  601,   91,
102943
 
 /*  1050 */   323,  261,   15,  414,  464,  414,  411,  621,  411,  354,
102944
 
 /*  1060 */   222,  411,  601,   74,  601,   90,  590,  584,  159,  264,
102945
 
 /*  1070 */   158,  414,  461,  414,  621,  600,  414,  121,  120,   25,
102946
 
 /*  1080 */   601,   89,  601,  101,  621,  601,   88,   47,  582,  581,
102947
 
 /*  1090 */   583,  583,   54,   54,   55,   55,   55,   55,  544,   53,
102948
 
 /*  1100 */    53,   53,   53,   52,   52,   51,   51,   51,   50,  238,
102949
 
 /*  1110 */    43,  405,  263,    3,  610,  264,  140,  415,  622,   24,
102950
 
 /*  1120 */   410,   11,  456,  594,  118,  155,  219,  452,  408,  621,
102951
 
 /*  1130 */   621,  621,  156,   43,  405,  621,    3,  286,  621,  113,
102952
 
 /*  1140 */   415,  622,  111,  445,  411,  400,  557,  403,  545,   10,
102953
 
 /*  1150 */   411,  408,  264,  110,  205,  436,  541,  566,  453,  414,
102954
 
 /*  1160 */   621,  621,   63,  621,  435,  414,  411,  621,  601,   94,
102955
 
 /*  1170 */   403,  621,  411,  337,  601,   86,  150,   40,   41,  534,
102956
 
 /*  1180 */   566,  414,  242,  264,   42,  413,  412,  414,  600,  595,
102957
 
 /*  1190 */   601,   85,  191,  333,  107,  451,  601,   84,  621,  539,
102958
 
 /*  1200 */    40,   41,  420,  230,  411,  149,  316,   42,  413,  412,
102959
 
 /*  1210 */   398,  127,  595,  315,  621,  399,  278,  625,  181,  414,
102960
 
 /*  1220 */   593,  593,  593,  592,  591,   14,  450,  411,  601,   71,
102961
 
 /*  1230 */   240,  621,   43,  405,  264,    3,  615,  180,  264,  415,
102962
 
 /*  1240 */   622,  614,  414,  593,  593,  593,  592,  591,   14,  621,
102963
 
 /*  1250 */   408,  601,   70,  621,  417,   33,  405,  613,    3,  411,
102964
 
 /*  1260 */   264,  411,  415,  622,  418,  626,  178,  509,    8,  403,
102965
 
 /*  1270 */   241,  416,  126,  408,  414,  621,  414,  449,  208,  566,
102966
 
 /*  1280 */   240,  221,  621,  601,   83,  601,   82,  599,  297,  277,
102967
 
 /*  1290 */   296,   30,  403,   31,  395,  264,  295,  397,  489,   40,
102968
 
 /*  1300 */    41,  411,  566,  220,  621,  294,   42,  413,  412,  271,
102969
 
 /*  1310 */   621,  595,  600,  621,   59,   60,  414,  269,  267,  623,
102970
 
 /*  1320 */   622,   36,   40,   41,  621,  601,   81,  598,  235,   42,
102971
 
 /*  1330 */   413,  412,  621,  621,  595,  265,  344,  411,  248,  556,
102972
 
 /*  1340 */   173,  185,  593,  593,  593,  592,  591,   14,  218,   29,
102973
 
 /*  1350 */   621,  543,  414,  305,  304,  303,  179,  301,  411,  566,
102974
 
 /*  1360 */   454,  601,   80,  289,  335,  593,  593,  593,  592,  591,
102975
 
 /*  1370 */    14,  411,  287,  414,  151,  392,  246,  260,  411,  196,
102976
 
 /*  1380 */   195,  523,  601,   69,  411,  245,  414,  526,  537,  285,
102977
 
 /*  1390 */   389,  595,  621,  414,  536,  601,   17,  362,  153,  414,
102978
 
 /*  1400 */   466,  463,  601,   78,  154,  414,  462,  152,  601,   77,
102979
 
 /*  1410 */   355,  255,  621,  455,  601,    9,  621,  386,  444,  517,
102980
 
 /*  1420 */   247,  621,  593,  593,  593,  621,  621,  244,  621,  243,
102981
 
 /*  1430 */   430,  518,  292,  621,  329,  621,  145,  393,  280,  513,
102982
 
 /*  1440 */   291,  131,  621,  514,  621,  621,  311,  621,  259,  346,
102983
 
 /*  1450 */   249,  621,  621,  229,  314,  621,  228,  512,  227,  240,
102984
 
 /*  1460 */   494,  488,  310,  164,  487,  486,  373,  480,  163,  262,
102985
 
 /*  1470 */   369,  371,  162,   26,  212,  478,  477,  161,  141,  363,
102986
 
 /*  1480 */   467,  122,  339,  187,  119,  348,  347,  117,  116,  115,
102987
 
 /*  1490 */   114,  112,  182,  457,  320,   22,  433,  432,  448,   19,
102988
 
 /*  1500 */   609,  431,  428,   62,  193,  596,  573,  298,  555,  552,
102989
 
 /*  1510 */   571,  404,  290,  380,  498,  510,  495,  306,  281,  499,
102990
 
 /*  1520 */   250,    5,  497,  460,  345,  447,  569,  550,  238,  299,
102991
 
 /*  1530 */   527,  525,  508,  961,  502,  501,  961,  401,  961,  211,
102992
 
 /*  1540 */   490,  356,  256,  961,  483,  961,  961,  961,  961,  961,
102993
 
 /*  1550 */   961,  961,  961,  961,  961,  961,  370,
102994
 
};
102995
 
static const YYCODETYPE yy_lookahead[] = {
102996
 
 /*     0 */    19,  142,  143,  144,  145,   24,    1,   26,   77,   78,
102997
 
 /*    10 */    79,   80,   81,   82,   83,   84,   85,   86,   87,   88,
102998
 
 /*    20 */    89,   90,   91,   92,   15,   98,   26,   27,    7,    8,
102999
 
 /*    30 */    49,   50,   77,   78,   79,   80,  109,   82,   83,   84,
103000
 
 /*    40 */    85,   86,   87,   88,   89,   90,   91,   92,   22,   68,
103001
 
 /*    50 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
103002
 
 /*    60 */    79,   80,   23,   82,   83,   84,   85,   86,   87,   88,
103003
 
 /*    70 */    89,   90,   91,   92,   19,   94,  112,   19,  114,  115,
103004
 
 /*    80 */    25,   82,   83,   84,   85,   86,   87,   88,   89,   90,
103005
 
 /*    90 */    91,   92,   19,   22,   94,   95,   96,  150,  150,   99,
103006
 
 /*   100 */   100,  101,   76,  150,   49,   50,  105,  106,  107,   54,
103007
 
 /*   110 */   110,  158,  165,  165,  161,  162,   26,   27,  165,  113,
103008
 
 /*   120 */    16,  174,  175,   68,   69,   70,   71,   72,   73,   74,
103009
 
 /*   130 */    75,   76,   77,   78,   79,   80,  118,   82,   83,   84,
103010
 
 /*   140 */    85,   86,   87,   88,   89,   90,   91,   92,   19,   23,
103011
 
 /*   150 */    92,   97,   98,   24,   96,   97,   98,   99,  100,  101,
103012
 
 /*   160 */   102,   25,   97,  216,   60,   92,   62,  109,  221,   25,
103013
 
 /*   170 */    97,   98,   99,  100,  101,  102,   86,   87,   49,   50,
103014
 
 /*   180 */   118,  116,  109,   25,   94,   95,   32,   97,   88,   89,
103015
 
 /*   190 */    90,   91,   92,  128,  104,   41,  106,   68,   69,   70,
103016
 
 /*   200 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
103017
 
 /*   210 */    11,   82,   83,   84,   85,   86,   87,   88,   89,   90,
103018
 
 /*   220 */    91,   92,   19,   19,   86,   87,   88,   89,   90,   91,
103019
 
 /*   230 */    92,   27,   96,  150,   66,   99,  100,  101,  112,  150,
103020
 
 /*   240 */   114,  115,  138,  150,  161,  162,  110,  103,  165,  222,
103021
 
 /*   250 */   223,  224,   49,   50,  165,   22,   57,   24,  165,  170,
103022
 
 /*   260 */   171,  118,   94,  170,  171,   23,   98,   25,  185,  186,
103023
 
 /*   270 */   243,   68,   69,   70,   71,   72,   73,   74,   75,   76,
103024
 
 /*   280 */    77,   78,   79,   80,  126,   82,   83,   84,   85,   86,
103025
 
 /*   290 */    87,   88,   89,   90,   91,   92,   19,  129,  130,  131,
103026
 
 /*   300 */    88,   23,  172,  173,  105,  106,  107,  150,   22,   26,
103027
 
 /*   310 */    27,  181,   26,   27,   22,  116,   26,   27,   26,  230,
103028
 
 /*   320 */   231,  197,  165,  230,  231,  113,   49,   50,  204,  117,
103029
 
 /*   330 */    96,  174,  175,   99,  100,  101,   22,   26,   27,  136,
103030
 
 /*   340 */    26,   27,  118,   16,  110,   68,   69,   70,   71,   72,
103031
 
 /*   350 */    73,   74,   75,   76,   77,   78,   79,   80,  118,   82,
103032
 
 /*   360 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
103033
 
 /*   370 */    19,  214,  215,  150,   23,   23,  155,   94,   95,   22,
103034
 
 /*   380 */    94,   95,  116,  160,   94,   95,   94,   60,  165,   62,
103035
 
 /*   390 */   112,   26,  114,  115,  128,   23,   36,  174,  175,   88,
103036
 
 /*   400 */    49,   50,   57,  120,   22,   94,   95,   23,   94,   95,
103037
 
 /*   410 */   120,   51,   25,  136,  169,  170,  171,  194,   58,   68,
103038
 
 /*   420 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
103039
 
 /*   430 */    79,   80,   23,   82,   83,   84,   85,   86,   87,   88,
103040
 
 /*   440 */    89,   90,   91,   92,   19,  150,   12,   12,   23,  228,
103041
 
 /*   450 */   105,  106,  107,   23,  233,   25,  165,   19,  150,   94,
103042
 
 /*   460 */   165,  116,   28,   28,  112,  174,  114,  115,  108,  174,
103043
 
 /*   470 */   175,   26,   27,  165,   49,   50,  231,   11,   44,   44,
103044
 
 /*   480 */    46,   46,  174,  175,  112,  160,  114,  115,   50,   22,
103045
 
 /*   490 */    23,   57,   25,   68,   69,   70,   71,   72,   73,   74,
103046
 
 /*   500 */    75,   76,   77,   78,   79,   80,  119,   82,   83,   84,
103047
 
 /*   510 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  194,
103048
 
 /*   520 */   225,   23,   23,  215,   19,   95,  105,  106,  107,  150,
103049
 
 /*   530 */    23,  150,   27,   23,   67,   25,  150,  206,  207,   94,
103050
 
 /*   540 */    95,  166,  104,  218,  165,   22,  165,  109,   49,   50,
103051
 
 /*   550 */   120,  165,   25,  174,  175,  174,  175,   23,   21,  234,
103052
 
 /*   560 */   174,  175,   22,   23,  239,   25,   25,   68,   69,   70,
103053
 
 /*   570 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
103054
 
 /*   580 */   205,   82,   83,   84,   85,   86,   87,   88,   89,   90,
103055
 
 /*   590 */    91,   92,   19,   22,   23,  216,   23,  222,  223,  224,
103056
 
 /*   600 */    63,  220,   35,  150,  150,  163,  220,   67,  166,  167,
103057
 
 /*   610 */   168,  150,  169,  170,  171,  161,  162,   25,  165,  165,
103058
 
 /*   620 */   150,  113,   49,   50,   25,  117,  165,  174,  175,   35,
103059
 
 /*   630 */     7,    8,    9,  160,  160,  165,  120,  100,   67,  247,
103060
 
 /*   640 */   248,   68,   69,   70,   71,   72,   73,   74,   75,   76,
103061
 
 /*   650 */    77,   78,   79,   80,  193,   82,   83,   84,   85,   86,
103062
 
 /*   660 */    87,   88,   89,   90,   91,   92,   19,  194,  194,  150,
103063
 
 /*   670 */   135,   24,  137,   35,  231,  138,  150,  129,  130,  206,
103064
 
 /*   680 */   207,   30,   27,  213,  165,   34,  118,   95,    0,    1,
103065
 
 /*   690 */     2,  165,  218,  174,  175,   50,   49,   50,   22,   48,
103066
 
 /*   700 */   174,  175,   22,   23,   23,  244,  222,  223,  224,  166,
103067
 
 /*   710 */   167,  168,  120,  239,   23,   68,   69,   70,   71,   72,
103068
 
 /*   720 */    73,   74,   75,   76,   77,   78,   79,   80,  150,   82,
103069
 
 /*   730 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
103070
 
 /*   740 */    19,  150,  173,  165,  181,  182,   24,   67,   26,  104,
103071
 
 /*   750 */   181,  188,  174,  175,  150,   39,  165,  150,   52,  150,
103072
 
 /*   760 */   150,  150,  150,  144,  145,  174,  175,  249,  250,  165,
103073
 
 /*   770 */    49,   50,  165,   52,  165,  165,  165,  165,  174,  175,
103074
 
 /*   780 */    29,  174,  175,  174,  175,  174,  175,  160,   22,   68,
103075
 
 /*   790 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
103076
 
 /*   800 */    79,   80,  150,   82,   83,   84,   85,   86,   87,   88,
103077
 
 /*   810 */    89,   90,   91,   92,   19,  150,   94,  165,  150,  150,
103078
 
 /*   820 */   160,  194,  150,  213,  160,   52,  174,  175,   23,   23,
103079
 
 /*   830 */   165,   25,   22,  165,  165,  150,  150,  165,   52,  174,
103080
 
 /*   840 */   175,   22,  174,  175,   49,   50,  174,  175,  190,  191,
103081
 
 /*   850 */   165,  165,  240,   23,  194,   25,  187,  109,  194,  174,
103082
 
 /*   860 */   175,  190,  191,   68,   69,   70,   71,   72,   73,   74,
103083
 
 /*   870 */    75,   76,   77,   78,   79,   80,  150,   82,   83,   84,
103084
 
 /*   880 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  150,
103085
 
 /*   890 */    22,  165,  150,   23,  150,   25,  150,  166,   91,   92,
103086
 
 /*   900 */   174,  175,   22,  217,  165,  150,  102,  165,  150,  165,
103087
 
 /*   910 */   150,  165,  150,  174,  175,   19,  174,  175,   49,   50,
103088
 
 /*   920 */   165,   86,   87,  165,   23,  165,   25,  165,   24,  174,
103089
 
 /*   930 */   175,  187,  174,  175,  174,  175,  205,   68,   69,   70,
103090
 
 /*   940 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
103091
 
 /*   950 */   150,   82,   83,   84,   85,   86,   87,   88,   89,   90,
103092
 
 /*   960 */    91,   92,   19,  150,  150,  165,  150,  150,  166,   23,
103093
 
 /*   970 */   150,   25,  160,   20,  174,  175,    1,    2,  165,  165,
103094
 
 /*   980 */   104,  165,  165,   43,  150,  165,  240,  150,   49,   50,
103095
 
 /*   990 */   174,  175,   49,   50,   23,   23,   25,   25,   53,  165,
103096
 
 /*  1000 */   187,  187,  165,   23,  187,   25,  194,  205,  174,  175,
103097
 
 /*  1010 */    71,   72,   69,   70,   71,   72,   73,   74,   75,   76,
103098
 
 /*  1020 */    77,   78,   79,   80,  150,   82,   83,   84,   85,   86,
103099
 
 /*  1030 */    87,   88,   89,   90,   91,   92,   19,   98,  150,  165,
103100
 
 /*  1040 */   150,  160,  150,   59,   25,   53,  104,   22,  174,  175,
103101
 
 /*  1050 */   213,  138,    5,  165,    1,  165,  150,  165,  150,  150,
103102
 
 /*  1060 */   240,  150,  174,  175,  174,  175,   49,   50,  118,  150,
103103
 
 /*  1070 */    35,  165,   27,  165,  165,  194,  165,  108,  127,   76,
103104
 
 /*  1080 */   174,  175,  174,  175,  165,  174,  175,   70,   71,   72,
103105
 
 /*  1090 */    73,   74,   75,   76,   77,   78,   79,   80,  166,   82,
103106
 
 /*  1100 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
103107
 
 /*  1110 */    19,   20,  193,   22,  150,  150,  150,   26,   27,   76,
103108
 
 /*  1120 */   150,   22,    1,  150,  119,  121,  217,   20,   37,  165,
103109
 
 /*  1130 */   165,  165,   16,   19,   20,  165,   22,  205,  165,  119,
103110
 
 /*  1140 */    26,   27,  108,  128,  150,  150,  150,   56,  150,   22,
103111
 
 /*  1150 */   150,   37,  150,  127,  160,   23,  150,   66,  193,  165,
103112
 
 /*  1160 */   165,  165,   16,  165,   23,  165,  150,  165,  174,  175,
103113
 
 /*  1170 */    56,  165,  150,   65,  174,  175,   15,   86,   87,   88,
103114
 
 /*  1180 */    66,  165,  140,  150,   93,   94,   95,  165,  194,   98,
103115
 
 /*  1190 */   174,  175,   22,    3,  164,  193,  174,  175,  165,  150,
103116
 
 /*  1200 */    86,   87,    4,  180,  150,  248,  251,   93,   94,   95,
103117
 
 /*  1210 */   216,  180,   98,  251,  165,  221,  150,  149,    6,  165,
103118
 
 /*  1220 */   129,  130,  131,  132,  133,  134,  193,  150,  174,  175,
103119
 
 /*  1230 */   116,  165,   19,   20,  150,   22,  149,  151,  150,   26,
103120
 
 /*  1240 */    27,  149,  165,  129,  130,  131,  132,  133,  134,  165,
103121
 
 /*  1250 */    37,  174,  175,  165,  149,   19,   20,   13,   22,  150,
103122
 
 /*  1260 */   150,  150,   26,   27,  146,  147,  151,  150,   25,   56,
103123
 
 /*  1270 */   152,  159,  154,   37,  165,  165,  165,  193,  160,   66,
103124
 
 /*  1280 */   116,  193,  165,  174,  175,  174,  175,  194,  199,  150,
103125
 
 /*  1290 */   200,  126,   56,  124,  123,  150,  201,  122,  150,   86,
103126
 
 /*  1300 */    87,  150,   66,  193,  165,  202,   93,   94,   95,  150,
103127
 
 /*  1310 */   165,   98,  194,  165,  125,   22,  165,  150,  150,   26,
103128
 
 /*  1320 */    27,  135,   86,   87,  165,  174,  175,  203,  226,   93,
103129
 
 /*  1330 */    94,   95,  165,  165,   98,  150,  218,  150,  193,  157,
103130
 
 /*  1340 */   118,  157,  129,  130,  131,  132,  133,  134,    5,  104,
103131
 
 /*  1350 */   165,  211,  165,   10,   11,   12,   13,   14,  150,   66,
103132
 
 /*  1360 */    17,  174,  175,  210,  246,  129,  130,  131,  132,  133,
103133
 
 /*  1370 */   134,  150,  210,  165,   31,  121,   33,  150,  150,   86,
103134
 
 /*  1380 */    87,  176,  174,  175,  150,   42,  165,   94,  211,  210,
103135
 
 /*  1390 */   150,   98,  165,  165,  211,  174,  175,  150,   55,  165,
103136
 
 /*  1400 */    57,  150,  174,  175,   61,  165,  150,   64,  174,  175,
103137
 
 /*  1410 */   150,  150,  165,  150,  174,  175,  165,  104,  150,  184,
103138
 
 /*  1420 */   150,  165,  129,  130,  131,  165,  165,  150,  165,  150,
103139
 
 /*  1430 */   150,  176,  150,  165,   47,  165,  150,  150,  176,  103,
103140
 
 /*  1440 */   150,   22,  165,  178,  165,  165,  179,  165,  105,  106,
103141
 
 /*  1450 */   107,  165,  165,  229,  111,  165,   92,  176,  229,  116,
103142
 
 /*  1460 */   184,  176,  179,  156,  176,  176,   18,  157,  156,  237,
103143
 
 /*  1470 */    45,  157,  156,  135,  157,  157,  238,  156,   68,  157,
103144
 
 /*  1480 */   189,  189,  139,  219,   22,  157,   18,  192,  192,  192,
103145
 
 /*  1490 */   192,  189,  219,  199,  157,  242,   40,  157,  199,  242,
103146
 
 /*  1500 */   153,  157,   38,  245,  196,  166,  232,  198,  177,  177,
103147
 
 /*  1510 */   232,  227,  209,  178,  166,  182,  166,  148,  177,  177,
103148
 
 /*  1520 */   209,  196,  177,  199,  209,  199,  166,  208,   92,  195,
103149
 
 /*  1530 */   174,  174,  183,  252,  183,  183,  252,  191,  252,  235,
103150
 
 /*  1540 */   186,  241,  241,  252,  186,  252,  252,  252,  252,  252,
103151
 
 /*  1550 */   252,  252,  252,  252,  252,  252,  236,
103152
 
};
103153
 
#define YY_SHIFT_USE_DFLT (-74)
103154
 
#define YY_SHIFT_COUNT (418)
103155
 
#define YY_SHIFT_MIN   (-73)
103156
 
#define YY_SHIFT_MAX   (1468)
103157
 
static const short yy_shift_ofst[] = {
103158
 
 /*     0 */   975, 1114, 1343, 1114, 1213, 1213,   90,   90,    0,  -19,
103159
 
 /*    10 */  1213, 1213, 1213, 1213, 1213,  345,  445,  721, 1091, 1213,
103160
 
 /*    20 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213,
103161
 
 /*    30 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213,
103162
 
 /*    40 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1236, 1213, 1213,
103163
 
 /*    50 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213,
103164
 
 /*    60 */  1213,  199,  445,  445,  835,  835,  365, 1164,   55,  647,
103165
 
 /*    70 */   573,  499,  425,  351,  277,  203,  129,  795,  795,  795,
103166
 
 /*    80 */   795,  795,  795,  795,  795,  795,  795,  795,  795,  795,
103167
 
 /*    90 */   795,  795,  795,  795,  795,  869,  795,  943, 1017, 1017,
103168
 
 /*   100 */   -69,  -45,  -45,  -45,  -45,  -45,   -1,   58,  138,  100,
103169
 
 /*   110 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
103170
 
 /*   120 */   445,  445,  445,  445,  445,  445,  537,  438,  445,  445,
103171
 
 /*   130 */   445,  445,  445,  365,  807, 1436,  -74,  -74,  -74, 1293,
103172
 
 /*   140 */    73,  434,  434,  311,  314,  290,  283,  286,  540,  467,
103173
 
 /*   150 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
103174
 
 /*   160 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
103175
 
 /*   170 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
103176
 
 /*   180 */   445,  445,   65,  722,  722,  722,  688,  266, 1164, 1164,
103177
 
 /*   190 */  1164,  -74,  -74,  -74,  136,  168,  168,  234,  360,  360,
103178
 
 /*   200 */   360,  430,  372,  435,  352,  278,  126,  -36,  -36,  -36,
103179
 
 /*   210 */   -36,  421,  651,  -36,  -36,  592,  292,  212,  623,  158,
103180
 
 /*   220 */   204,  204,  505,  158,  505,  144,  365,  154,  365,  154,
103181
 
 /*   230 */   645,  154,  204,  154,  154,  535,  548,  548,  365,  387,
103182
 
 /*   240 */   508,  233, 1464, 1222, 1222, 1456, 1456, 1222, 1462, 1410,
103183
 
 /*   250 */  1165, 1468, 1468, 1468, 1468, 1222, 1165, 1462, 1410, 1410,
103184
 
 /*   260 */  1222, 1448, 1338, 1425, 1222, 1222, 1448, 1222, 1448, 1222,
103185
 
 /*   270 */  1448, 1419, 1313, 1313, 1313, 1387, 1364, 1364, 1419, 1313,
103186
 
 /*   280 */  1336, 1313, 1387, 1313, 1313, 1254, 1245, 1254, 1245, 1254,
103187
 
 /*   290 */  1245, 1222, 1222, 1186, 1189, 1175, 1169, 1171, 1165, 1164,
103188
 
 /*   300 */  1243, 1244, 1244, 1212, 1212, 1212, 1212,  -74,  -74,  -74,
103189
 
 /*   310 */   -74,  -74,  -74,  939,  104,  680,  571,  327,    1,  980,
103190
 
 /*   320 */    26,  972,  971,  946,  901,  870,  830,  806,   54,   21,
103191
 
 /*   330 */   -73,  510,  242, 1198, 1190, 1170, 1042, 1161, 1108, 1146,
103192
 
 /*   340 */  1141, 1132, 1015, 1127, 1026, 1034, 1020, 1107, 1004, 1116,
103193
 
 /*   350 */  1121, 1005, 1099,  951, 1043, 1003,  969, 1045, 1035,  950,
103194
 
 /*   360 */  1053, 1047, 1025,  942,  913,  992, 1019,  945,  984,  940,
103195
 
 /*   370 */   876,  904,  953,  896,  748,  804,  880,  786,  868,  819,
103196
 
 /*   380 */   805,  810,  773,  751,  766,  706,  716,  691,  681,  568,
103197
 
 /*   390 */   655,  638,  676,  516,  541,  594,  599,  567,  541,  534,
103198
 
 /*   400 */   507,  527,  498,  523,  466,  382,  409,  384,  357,    6,
103199
 
 /*   410 */   240,  224,  143,   62,   18,   71,   39,    9,    5,
103200
 
};
103201
 
#define YY_REDUCE_USE_DFLT (-142)
103202
 
#define YY_REDUCE_COUNT (312)
103203
 
#define YY_REDUCE_MIN   (-141)
103204
 
#define YY_REDUCE_MAX   (1369)
103205
 
static const short yy_reduce_ofst[] = {
103206
 
 /*     0 */  -141,  994, 1118,  223,  157,  -53,   93,   89,   83,  375,
103207
 
 /*    10 */   386,  381,  379,  308,  295,  325,  -47,   27, 1240, 1234,
103208
 
 /*    20 */  1228, 1221, 1208, 1187, 1151, 1111, 1109, 1077, 1054, 1022,
103209
 
 /*    30 */  1016, 1000,  911,  908,  906,  890,  888,  874,  834,  816,
103210
 
 /*    40 */   800,  760,  758,  755,  742,  739,  726,  685,  672,  668,
103211
 
 /*    50 */   665,  652,  611,  609,  607,  604,  591,  578,  526,  519,
103212
 
 /*    60 */   453,  474,  454,  461,  443,  245,  442,  473,  484,  484,
103213
 
 /*    70 */   484,  484,  484,  484,  484,  484,  484,  484,  484,  484,
103214
 
 /*    80 */   484,  484,  484,  484,  484,  484,  484,  484,  484,  484,
103215
 
 /*    90 */   484,  484,  484,  484,  484,  484,  484,  484,  484,  484,
103216
 
 /*   100 */   484,  484,  484,  484,  484,  484,  484,  130,  484,  484,
103217
 
 /*   110 */  1145,  909, 1110, 1088, 1084, 1033, 1002,  965,  820,  837,
103218
 
 /*   120 */   746,  686,  612,  817,  610,  919,  221,  563,  814,  813,
103219
 
 /*   130 */   744,  669,  470,  543,  484,  484,  484,  484,  484,  291,
103220
 
 /*   140 */   569,  671,  658,  970, 1290, 1287, 1286, 1282,  518,  518,
103221
 
 /*   150 */  1280, 1279, 1277, 1270, 1268, 1263, 1261, 1260, 1256, 1251,
103222
 
 /*   160 */  1247, 1227, 1185, 1168, 1167, 1159, 1148, 1139, 1117, 1066,
103223
 
 /*   170 */  1049, 1006,  998,  996,  995,  973,  970,  966,  964,  892,
103224
 
 /*   180 */   762,  -52,  881,  932,  802,  731,  619,  812,  664,  660,
103225
 
 /*   190 */   627,  392,  331,  124, 1358, 1357, 1356, 1354, 1352, 1351,
103226
 
 /*   200 */  1349, 1319, 1334, 1346, 1334, 1334, 1334, 1334, 1334, 1334,
103227
 
 /*   210 */  1334, 1320, 1304, 1334, 1334, 1319, 1360, 1325, 1369, 1326,
103228
 
 /*   220 */  1315, 1311, 1301, 1324, 1300, 1335, 1350, 1345, 1348, 1342,
103229
 
 /*   230 */  1333, 1341, 1303, 1332, 1331, 1284, 1278, 1274, 1339, 1309,
103230
 
 /*   240 */  1308, 1347, 1258, 1344, 1340, 1257, 1253, 1337, 1273, 1302,
103231
 
 /*   250 */  1299, 1298, 1297, 1296, 1295, 1328, 1294, 1264, 1292, 1291,
103232
 
 /*   260 */  1322, 1321, 1238, 1232, 1318, 1317, 1316, 1314, 1312, 1310,
103233
 
 /*   270 */  1307, 1283, 1289, 1288, 1285, 1276, 1229, 1224, 1267, 1281,
103234
 
 /*   280 */  1265, 1262, 1235, 1255, 1205, 1183, 1179, 1177, 1162, 1140,
103235
 
 /*   290 */  1153, 1184, 1182, 1102, 1124, 1103, 1095, 1090, 1089, 1093,
103236
 
 /*   300 */  1112, 1115, 1086, 1105, 1092, 1087, 1068,  962,  955,  957,
103237
 
 /*   310 */  1031, 1023, 1030,
103238
 
};
103239
 
static const YYACTIONTYPE yy_default[] = {
103240
 
 /*     0 */   635,  870,  959,  959,  959,  870,  899,  899,  959,  759,
103241
 
 /*    10 */   959,  959,  959,  959,  868,  959,  959,  933,  959,  959,
103242
 
 /*    20 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
103243
 
 /*    30 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
103244
 
 /*    40 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
103245
 
 /*    50 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
103246
 
 /*    60 */   959,  959,  959,  959,  899,  899,  674,  763,  794,  959,
103247
 
 /*    70 */   959,  959,  959,  959,  959,  959,  959,  932,  934,  809,
103248
 
 /*    80 */   808,  802,  801,  912,  774,  799,  792,  785,  796,  871,
103249
 
 /*    90 */   864,  865,  863,  867,  872,  959,  795,  831,  848,  830,
103250
 
 /*   100 */   842,  847,  854,  846,  843,  833,  832,  666,  834,  835,
103251
 
 /*   110 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
103252
 
 /*   120 */   959,  959,  959,  959,  959,  959,  661,  728,  959,  959,
103253
 
 /*   130 */   959,  959,  959,  959,  836,  837,  851,  850,  849,  959,
103254
 
 /*   140 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
103255
 
 /*   150 */   959,  939,  937,  959,  883,  959,  959,  959,  959,  959,
103256
 
 /*   160 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
103257
 
 /*   170 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
103258
 
 /*   180 */   959,  641,  959,  759,  759,  759,  635,  959,  959,  959,
103259
 
 /*   190 */   959,  951,  763,  753,  719,  959,  959,  959,  959,  959,
103260
 
 /*   200 */   959,  959,  959,  959,  959,  959,  959,  804,  742,  922,
103261
 
 /*   210 */   924,  959,  905,  740,  663,  761,  676,  751,  643,  798,
103262
 
 /*   220 */   776,  776,  917,  798,  917,  700,  959,  788,  959,  788,
103263
 
 /*   230 */   697,  788,  776,  788,  788,  866,  959,  959,  959,  760,
103264
 
 /*   240 */   751,  959,  944,  767,  767,  936,  936,  767,  810,  732,
103265
 
 /*   250 */   798,  739,  739,  739,  739,  767,  798,  810,  732,  732,
103266
 
 /*   260 */   767,  658,  911,  909,  767,  767,  658,  767,  658,  767,
103267
 
 /*   270 */   658,  876,  730,  730,  730,  715,  880,  880,  876,  730,
103268
 
 /*   280 */   700,  730,  715,  730,  730,  780,  775,  780,  775,  780,
103269
 
 /*   290 */   775,  767,  767,  959,  793,  781,  791,  789,  798,  959,
103270
 
 /*   300 */   718,  651,  651,  640,  640,  640,  640,  956,  956,  951,
103271
 
 /*   310 */   702,  702,  684,  959,  959,  959,  959,  959,  959,  959,
103272
 
 /*   320 */   885,  959,  959,  959,  959,  959,  959,  959,  959,  959,
103273
 
 /*   330 */   959,  959,  959,  959,  636,  946,  959,  959,  943,  959,
103274
 
 /*   340 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
103275
 
 /*   350 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  915,
103276
 
 /*   360 */   959,  959,  959,  959,  959,  959,  908,  907,  959,  959,
103277
 
 /*   370 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
103278
 
 /*   380 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
103279
 
 /*   390 */   959,  959,  959,  959,  790,  959,  782,  959,  869,  959,
103280
 
 /*   400 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  745,
103281
 
 /*   410 */   819,  959,  818,  822,  817,  668,  959,  649,  959,  632,
103282
 
 /*   420 */   637,  955,  958,  957,  954,  953,  952,  947,  945,  942,
103283
 
 /*   430 */   941,  940,  938,  935,  931,  889,  887,  894,  893,  892,
103284
 
 /*   440 */   891,  890,  888,  886,  884,  805,  803,  800,  797,  930,
103285
 
 /*   450 */   882,  741,  738,  737,  657,  948,  914,  923,  921,  811,
103286
 
 /*   460 */   920,  919,  918,  916,  913,  900,  807,  806,  733,  874,
103287
 
 /*   470 */   873,  660,  904,  903,  902,  906,  910,  901,  769,  659,
103288
 
 /*   480 */   656,  665,  722,  721,  729,  727,  726,  725,  724,  723,
103289
 
 /*   490 */   720,  667,  675,  686,  714,  699,  698,  879,  881,  878,
103290
 
 /*   500 */   877,  707,  706,  712,  711,  710,  709,  708,  705,  704,
103291
 
 /*   510 */   703,  696,  695,  701,  694,  717,  716,  713,  693,  736,
103292
 
 /*   520 */   735,  734,  731,  692,  691,  690,  822,  689,  688,  828,
103293
 
 /*   530 */   827,  815,  858,  756,  755,  754,  766,  765,  778,  777,
103294
 
 /*   540 */   813,  812,  779,  764,  758,  757,  773,  772,  771,  770,
103295
 
 /*   550 */   762,  752,  784,  787,  786,  783,  860,  768,  857,  929,
103296
 
 /*   560 */   928,  927,  926,  925,  862,  861,  829,  826,  679,  680,
103297
 
 /*   570 */   898,  896,  897,  895,  682,  681,  678,  677,  859,  747,
103298
 
 /*   580 */   746,  855,  852,  844,  840,  856,  853,  845,  841,  839,
103299
 
 /*   590 */   838,  824,  823,  821,  820,  816,  825,  670,  748,  744,
103300
 
 /*   600 */   743,  814,  750,  749,  687,  685,  683,  664,  662,  655,
103301
 
 /*   610 */   653,  652,  654,  650,  648,  647,  646,  645,  644,  673,
103302
 
 /*   620 */   672,  671,  669,  668,  642,  639,  638,  634,  633,  631,
103303
 
};
103304
 
 
103305
 
/* The next table maps tokens into fallback tokens.  If a construct
103306
 
** like the following:
103307
 
** 
103308
 
**      %fallback ID X Y Z.
103309
 
**
103310
 
** appears in the grammar, then ID becomes a fallback token for X, Y,
103311
 
** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
103312
 
** but it does not parse, the type of the token is changed to ID and
103313
 
** the parse is retried before an error is thrown.
103314
 
*/
103315
 
#ifdef YYFALLBACK
103316
 
static const YYCODETYPE yyFallback[] = {
103317
 
    0,  /*          $ => nothing */
103318
 
    0,  /*       SEMI => nothing */
103319
 
   26,  /*    EXPLAIN => ID */
103320
 
   26,  /*      QUERY => ID */
103321
 
   26,  /*       PLAN => ID */
103322
 
   26,  /*      BEGIN => ID */
103323
 
    0,  /* TRANSACTION => nothing */
103324
 
   26,  /*   DEFERRED => ID */
103325
 
   26,  /*  IMMEDIATE => ID */
103326
 
   26,  /*  EXCLUSIVE => ID */
103327
 
    0,  /*     COMMIT => nothing */
103328
 
   26,  /*        END => ID */
103329
 
   26,  /*   ROLLBACK => ID */
103330
 
   26,  /*  SAVEPOINT => ID */
103331
 
   26,  /*    RELEASE => ID */
103332
 
    0,  /*         TO => nothing */
103333
 
    0,  /*      TABLE => nothing */
103334
 
    0,  /*     CREATE => nothing */
103335
 
   26,  /*         IF => ID */
103336
 
    0,  /*        NOT => nothing */
103337
 
    0,  /*     EXISTS => nothing */
103338
 
   26,  /*       TEMP => ID */
103339
 
    0,  /*         LP => nothing */
103340
 
    0,  /*         RP => nothing */
103341
 
    0,  /*         AS => nothing */
103342
 
    0,  /*      COMMA => nothing */
103343
 
    0,  /*         ID => nothing */
103344
 
    0,  /*    INDEXED => nothing */
103345
 
   26,  /*      ABORT => ID */
103346
 
   26,  /*     ACTION => ID */
103347
 
   26,  /*      AFTER => ID */
103348
 
   26,  /*    ANALYZE => ID */
103349
 
   26,  /*        ASC => ID */
103350
 
   26,  /*     ATTACH => ID */
103351
 
   26,  /*     BEFORE => ID */
103352
 
   26,  /*         BY => ID */
103353
 
   26,  /*    CASCADE => ID */
103354
 
   26,  /*       CAST => ID */
103355
 
   26,  /*   COLUMNKW => ID */
103356
 
   26,  /*   CONFLICT => ID */
103357
 
   26,  /*   DATABASE => ID */
103358
 
   26,  /*       DESC => ID */
103359
 
   26,  /*     DETACH => ID */
103360
 
   26,  /*       EACH => ID */
103361
 
   26,  /*       FAIL => ID */
103362
 
   26,  /*        FOR => ID */
103363
 
   26,  /*     IGNORE => ID */
103364
 
   26,  /*  INITIALLY => ID */
103365
 
   26,  /*    INSTEAD => ID */
103366
 
   26,  /*    LIKE_KW => ID */
103367
 
   26,  /*      MATCH => ID */
103368
 
   26,  /*         NO => ID */
103369
 
   26,  /*        KEY => ID */
103370
 
   26,  /*         OF => ID */
103371
 
   26,  /*     OFFSET => ID */
103372
 
   26,  /*     PRAGMA => ID */
103373
 
   26,  /*      RAISE => ID */
103374
 
   26,  /*    REPLACE => ID */
103375
 
   26,  /*   RESTRICT => ID */
103376
 
   26,  /*        ROW => ID */
103377
 
   26,  /*    TRIGGER => ID */
103378
 
   26,  /*     VACUUM => ID */
103379
 
   26,  /*       VIEW => ID */
103380
 
   26,  /*    VIRTUAL => ID */
103381
 
   26,  /*    REINDEX => ID */
103382
 
   26,  /*     RENAME => ID */
103383
 
   26,  /*   CTIME_KW => ID */
103384
 
};
103385
 
#endif /* YYFALLBACK */
103386
 
 
103387
 
/* The following structure represents a single element of the
103388
 
** parser's stack.  Information stored includes:
103389
 
**
103390
 
**   +  The state number for the parser at this level of the stack.
103391
 
**
103392
 
**   +  The value of the token stored at this level of the stack.
103393
 
**      (In other words, the "major" token.)
103394
 
**
103395
 
**   +  The semantic value stored at this level of the stack.  This is
103396
 
**      the information used by the action routines in the grammar.
103397
 
**      It is sometimes called the "minor" token.
103398
 
*/
103399
 
struct yyStackEntry {
103400
 
  YYACTIONTYPE stateno;  /* The state-number */
103401
 
  YYCODETYPE major;      /* The major token value.  This is the code
103402
 
                         ** number for the token at this stack level */
103403
 
  YYMINORTYPE minor;     /* The user-supplied minor token value.  This
103404
 
                         ** is the value of the token  */
103405
 
};
103406
 
typedef struct yyStackEntry yyStackEntry;
103407
 
 
103408
 
/* The state of the parser is completely contained in an instance of
103409
 
** the following structure */
103410
 
struct yyParser {
103411
 
  int yyidx;                    /* Index of top element in stack */
103412
 
#ifdef YYTRACKMAXSTACKDEPTH
103413
 
  int yyidxMax;                 /* Maximum value of yyidx */
103414
 
#endif
103415
 
  int yyerrcnt;                 /* Shifts left before out of the error */
103416
 
  sqlite3ParserARG_SDECL                /* A place to hold %extra_argument */
103417
 
#if YYSTACKDEPTH<=0
103418
 
  int yystksz;                  /* Current side of the stack */
103419
 
  yyStackEntry *yystack;        /* The parser's stack */
103420
 
#else
103421
 
  yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
103422
 
#endif
103423
 
};
103424
 
typedef struct yyParser yyParser;
103425
 
 
103426
 
#ifndef NDEBUG
103427
 
static FILE *yyTraceFILE = 0;
103428
 
static char *yyTracePrompt = 0;
103429
 
#endif /* NDEBUG */
103430
 
 
103431
 
#ifndef NDEBUG
103432
 
/* 
103433
 
** Turn parser tracing on by giving a stream to which to write the trace
103434
 
** and a prompt to preface each trace message.  Tracing is turned off
103435
 
** by making either argument NULL 
103436
 
**
103437
 
** Inputs:
103438
 
** <ul>
103439
 
** <li> A FILE* to which trace output should be written.
103440
 
**      If NULL, then tracing is turned off.
103441
 
** <li> A prefix string written at the beginning of every
103442
 
**      line of trace output.  If NULL, then tracing is
103443
 
**      turned off.
103444
 
** </ul>
103445
 
**
103446
 
** Outputs:
103447
 
** None.
103448
 
*/
103449
 
SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
103450
 
  yyTraceFILE = TraceFILE;
103451
 
  yyTracePrompt = zTracePrompt;
103452
 
  if( yyTraceFILE==0 ) yyTracePrompt = 0;
103453
 
  else if( yyTracePrompt==0 ) yyTraceFILE = 0;
103454
 
}
103455
 
#endif /* NDEBUG */
103456
 
 
103457
 
#ifndef NDEBUG
103458
 
/* For tracing shifts, the names of all terminals and nonterminals
103459
 
** are required.  The following table supplies these names */
103460
 
static const char *const yyTokenName[] = { 
103461
 
  "$",             "SEMI",          "EXPLAIN",       "QUERY",       
103462
 
  "PLAN",          "BEGIN",         "TRANSACTION",   "DEFERRED",    
103463
 
  "IMMEDIATE",     "EXCLUSIVE",     "COMMIT",        "END",         
103464
 
  "ROLLBACK",      "SAVEPOINT",     "RELEASE",       "TO",          
103465
 
  "TABLE",         "CREATE",        "IF",            "NOT",         
103466
 
  "EXISTS",        "TEMP",          "LP",            "RP",          
103467
 
  "AS",            "COMMA",         "ID",            "INDEXED",     
103468
 
  "ABORT",         "ACTION",        "AFTER",         "ANALYZE",     
103469
 
  "ASC",           "ATTACH",        "BEFORE",        "BY",          
103470
 
  "CASCADE",       "CAST",          "COLUMNKW",      "CONFLICT",    
103471
 
  "DATABASE",      "DESC",          "DETACH",        "EACH",        
103472
 
  "FAIL",          "FOR",           "IGNORE",        "INITIALLY",   
103473
 
  "INSTEAD",       "LIKE_KW",       "MATCH",         "NO",          
103474
 
  "KEY",           "OF",            "OFFSET",        "PRAGMA",      
103475
 
  "RAISE",         "REPLACE",       "RESTRICT",      "ROW",         
103476
 
  "TRIGGER",       "VACUUM",        "VIEW",          "VIRTUAL",     
103477
 
  "REINDEX",       "RENAME",        "CTIME_KW",      "ANY",         
103478
 
  "OR",            "AND",           "IS",            "BETWEEN",     
103479
 
  "IN",            "ISNULL",        "NOTNULL",       "NE",          
103480
 
  "EQ",            "GT",            "LE",            "LT",          
103481
 
  "GE",            "ESCAPE",        "BITAND",        "BITOR",       
103482
 
  "LSHIFT",        "RSHIFT",        "PLUS",          "MINUS",       
103483
 
  "STAR",          "SLASH",         "REM",           "CONCAT",      
103484
 
  "COLLATE",       "BITNOT",        "STRING",        "JOIN_KW",     
103485
 
  "CONSTRAINT",    "DEFAULT",       "NULL",          "PRIMARY",     
103486
 
  "UNIQUE",        "CHECK",         "REFERENCES",    "AUTOINCR",    
103487
 
  "ON",            "INSERT",        "DELETE",        "UPDATE",      
103488
 
  "SET",           "DEFERRABLE",    "FOREIGN",       "DROP",        
103489
 
  "UNION",         "ALL",           "EXCEPT",        "INTERSECT",   
103490
 
  "SELECT",        "DISTINCT",      "DOT",           "FROM",        
103491
 
  "JOIN",          "USING",         "ORDER",         "GROUP",       
103492
 
  "HAVING",        "LIMIT",         "WHERE",         "INTO",        
103493
 
  "VALUES",        "INTEGER",       "FLOAT",         "BLOB",        
103494
 
  "REGISTER",      "VARIABLE",      "CASE",          "WHEN",        
103495
 
  "THEN",          "ELSE",          "INDEX",         "ALTER",       
103496
 
  "ADD",           "error",         "input",         "cmdlist",     
103497
 
  "ecmd",          "explain",       "cmdx",          "cmd",         
103498
 
  "transtype",     "trans_opt",     "nm",            "savepoint_opt",
103499
 
  "create_table",  "create_table_args",  "createkw",      "temp",        
103500
 
  "ifnotexists",   "dbnm",          "columnlist",    "conslist_opt",
103501
 
  "select",        "column",        "columnid",      "type",        
103502
 
  "carglist",      "id",            "ids",           "typetoken",   
103503
 
  "typename",      "signed",        "plus_num",      "minus_num",   
103504
 
  "carg",          "ccons",         "term",          "expr",        
103505
 
  "onconf",        "sortorder",     "autoinc",       "idxlist_opt", 
103506
 
  "refargs",       "defer_subclause",  "refarg",        "refact",      
103507
 
  "init_deferred_pred_opt",  "conslist",      "tcons",         "idxlist",     
103508
 
  "defer_subclause_opt",  "orconf",        "resolvetype",   "raisetype",   
103509
 
  "ifexists",      "fullname",      "oneselect",     "multiselect_op",
103510
 
  "distinct",      "selcollist",    "from",          "where_opt",   
103511
 
  "groupby_opt",   "having_opt",    "orderby_opt",   "limit_opt",   
103512
 
  "sclp",          "as",            "seltablist",    "stl_prefix",  
103513
 
  "joinop",        "indexed_opt",   "on_opt",        "using_opt",   
103514
 
  "joinop2",       "inscollist",    "sortlist",      "sortitem",    
103515
 
  "nexprlist",     "setlist",       "insert_cmd",    "inscollist_opt",
103516
 
  "itemlist",      "exprlist",      "likeop",        "between_op",  
103517
 
  "in_op",         "case_operand",  "case_exprlist",  "case_else",   
103518
 
  "uniqueflag",    "collate",       "nmnum",         "plus_opt",    
103519
 
  "number",        "trigger_decl",  "trigger_cmd_list",  "trigger_time",
103520
 
  "trigger_event",  "foreach_clause",  "when_clause",   "trigger_cmd", 
103521
 
  "trnm",          "tridxby",       "database_kw_opt",  "key_opt",     
103522
 
  "add_column_fullname",  "kwcolumn_opt",  "create_vtab",   "vtabarglist", 
103523
 
  "vtabarg",       "vtabargtoken",  "lp",            "anylist",     
103524
 
};
103525
 
#endif /* NDEBUG */
103526
 
 
103527
 
#ifndef NDEBUG
103528
 
/* For tracing reduce actions, the names of all rules are required.
103529
 
*/
103530
 
static const char *const yyRuleName[] = {
103531
 
 /*   0 */ "input ::= cmdlist",
103532
 
 /*   1 */ "cmdlist ::= cmdlist ecmd",
103533
 
 /*   2 */ "cmdlist ::= ecmd",
103534
 
 /*   3 */ "ecmd ::= SEMI",
103535
 
 /*   4 */ "ecmd ::= explain cmdx SEMI",
103536
 
 /*   5 */ "explain ::=",
103537
 
 /*   6 */ "explain ::= EXPLAIN",
103538
 
 /*   7 */ "explain ::= EXPLAIN QUERY PLAN",
103539
 
 /*   8 */ "cmdx ::= cmd",
103540
 
 /*   9 */ "cmd ::= BEGIN transtype trans_opt",
103541
 
 /*  10 */ "trans_opt ::=",
103542
 
 /*  11 */ "trans_opt ::= TRANSACTION",
103543
 
 /*  12 */ "trans_opt ::= TRANSACTION nm",
103544
 
 /*  13 */ "transtype ::=",
103545
 
 /*  14 */ "transtype ::= DEFERRED",
103546
 
 /*  15 */ "transtype ::= IMMEDIATE",
103547
 
 /*  16 */ "transtype ::= EXCLUSIVE",
103548
 
 /*  17 */ "cmd ::= COMMIT trans_opt",
103549
 
 /*  18 */ "cmd ::= END trans_opt",
103550
 
 /*  19 */ "cmd ::= ROLLBACK trans_opt",
103551
 
 /*  20 */ "savepoint_opt ::= SAVEPOINT",
103552
 
 /*  21 */ "savepoint_opt ::=",
103553
 
 /*  22 */ "cmd ::= SAVEPOINT nm",
103554
 
 /*  23 */ "cmd ::= RELEASE savepoint_opt nm",
103555
 
 /*  24 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
103556
 
 /*  25 */ "cmd ::= create_table create_table_args",
103557
 
 /*  26 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
103558
 
 /*  27 */ "createkw ::= CREATE",
103559
 
 /*  28 */ "ifnotexists ::=",
103560
 
 /*  29 */ "ifnotexists ::= IF NOT EXISTS",
103561
 
 /*  30 */ "temp ::= TEMP",
103562
 
 /*  31 */ "temp ::=",
103563
 
 /*  32 */ "create_table_args ::= LP columnlist conslist_opt RP",
103564
 
 /*  33 */ "create_table_args ::= AS select",
103565
 
 /*  34 */ "columnlist ::= columnlist COMMA column",
103566
 
 /*  35 */ "columnlist ::= column",
103567
 
 /*  36 */ "column ::= columnid type carglist",
103568
 
 /*  37 */ "columnid ::= nm",
103569
 
 /*  38 */ "id ::= ID",
103570
 
 /*  39 */ "id ::= INDEXED",
103571
 
 /*  40 */ "ids ::= ID|STRING",
103572
 
 /*  41 */ "nm ::= id",
103573
 
 /*  42 */ "nm ::= STRING",
103574
 
 /*  43 */ "nm ::= JOIN_KW",
103575
 
 /*  44 */ "type ::=",
103576
 
 /*  45 */ "type ::= typetoken",
103577
 
 /*  46 */ "typetoken ::= typename",
103578
 
 /*  47 */ "typetoken ::= typename LP signed RP",
103579
 
 /*  48 */ "typetoken ::= typename LP signed COMMA signed RP",
103580
 
 /*  49 */ "typename ::= ids",
103581
 
 /*  50 */ "typename ::= typename ids",
103582
 
 /*  51 */ "signed ::= plus_num",
103583
 
 /*  52 */ "signed ::= minus_num",
103584
 
 /*  53 */ "carglist ::= carglist carg",
103585
 
 /*  54 */ "carglist ::=",
103586
 
 /*  55 */ "carg ::= CONSTRAINT nm ccons",
103587
 
 /*  56 */ "carg ::= ccons",
103588
 
 /*  57 */ "ccons ::= DEFAULT term",
103589
 
 /*  58 */ "ccons ::= DEFAULT LP expr RP",
103590
 
 /*  59 */ "ccons ::= DEFAULT PLUS term",
103591
 
 /*  60 */ "ccons ::= DEFAULT MINUS term",
103592
 
 /*  61 */ "ccons ::= DEFAULT id",
103593
 
 /*  62 */ "ccons ::= NULL onconf",
103594
 
 /*  63 */ "ccons ::= NOT NULL onconf",
103595
 
 /*  64 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
103596
 
 /*  65 */ "ccons ::= UNIQUE onconf",
103597
 
 /*  66 */ "ccons ::= CHECK LP expr RP",
103598
 
 /*  67 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
103599
 
 /*  68 */ "ccons ::= defer_subclause",
103600
 
 /*  69 */ "ccons ::= COLLATE ids",
103601
 
 /*  70 */ "autoinc ::=",
103602
 
 /*  71 */ "autoinc ::= AUTOINCR",
103603
 
 /*  72 */ "refargs ::=",
103604
 
 /*  73 */ "refargs ::= refargs refarg",
103605
 
 /*  74 */ "refarg ::= MATCH nm",
103606
 
 /*  75 */ "refarg ::= ON INSERT refact",
103607
 
 /*  76 */ "refarg ::= ON DELETE refact",
103608
 
 /*  77 */ "refarg ::= ON UPDATE refact",
103609
 
 /*  78 */ "refact ::= SET NULL",
103610
 
 /*  79 */ "refact ::= SET DEFAULT",
103611
 
 /*  80 */ "refact ::= CASCADE",
103612
 
 /*  81 */ "refact ::= RESTRICT",
103613
 
 /*  82 */ "refact ::= NO ACTION",
103614
 
 /*  83 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
103615
 
 /*  84 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
103616
 
 /*  85 */ "init_deferred_pred_opt ::=",
103617
 
 /*  86 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
103618
 
 /*  87 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
103619
 
 /*  88 */ "conslist_opt ::=",
103620
 
 /*  89 */ "conslist_opt ::= COMMA conslist",
103621
 
 /*  90 */ "conslist ::= conslist COMMA tcons",
103622
 
 /*  91 */ "conslist ::= conslist tcons",
103623
 
 /*  92 */ "conslist ::= tcons",
103624
 
 /*  93 */ "tcons ::= CONSTRAINT nm",
103625
 
 /*  94 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
103626
 
 /*  95 */ "tcons ::= UNIQUE LP idxlist RP onconf",
103627
 
 /*  96 */ "tcons ::= CHECK LP expr RP onconf",
103628
 
 /*  97 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
103629
 
 /*  98 */ "defer_subclause_opt ::=",
103630
 
 /*  99 */ "defer_subclause_opt ::= defer_subclause",
103631
 
 /* 100 */ "onconf ::=",
103632
 
 /* 101 */ "onconf ::= ON CONFLICT resolvetype",
103633
 
 /* 102 */ "orconf ::=",
103634
 
 /* 103 */ "orconf ::= OR resolvetype",
103635
 
 /* 104 */ "resolvetype ::= raisetype",
103636
 
 /* 105 */ "resolvetype ::= IGNORE",
103637
 
 /* 106 */ "resolvetype ::= REPLACE",
103638
 
 /* 107 */ "cmd ::= DROP TABLE ifexists fullname",
103639
 
 /* 108 */ "ifexists ::= IF EXISTS",
103640
 
 /* 109 */ "ifexists ::=",
103641
 
 /* 110 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select",
103642
 
 /* 111 */ "cmd ::= DROP VIEW ifexists fullname",
103643
 
 /* 112 */ "cmd ::= select",
103644
 
 /* 113 */ "select ::= oneselect",
103645
 
 /* 114 */ "select ::= select multiselect_op oneselect",
103646
 
 /* 115 */ "multiselect_op ::= UNION",
103647
 
 /* 116 */ "multiselect_op ::= UNION ALL",
103648
 
 /* 117 */ "multiselect_op ::= EXCEPT|INTERSECT",
103649
 
 /* 118 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
103650
 
 /* 119 */ "distinct ::= DISTINCT",
103651
 
 /* 120 */ "distinct ::= ALL",
103652
 
 /* 121 */ "distinct ::=",
103653
 
 /* 122 */ "sclp ::= selcollist COMMA",
103654
 
 /* 123 */ "sclp ::=",
103655
 
 /* 124 */ "selcollist ::= sclp expr as",
103656
 
 /* 125 */ "selcollist ::= sclp STAR",
103657
 
 /* 126 */ "selcollist ::= sclp nm DOT STAR",
103658
 
 /* 127 */ "as ::= AS nm",
103659
 
 /* 128 */ "as ::= ids",
103660
 
 /* 129 */ "as ::=",
103661
 
 /* 130 */ "from ::=",
103662
 
 /* 131 */ "from ::= FROM seltablist",
103663
 
 /* 132 */ "stl_prefix ::= seltablist joinop",
103664
 
 /* 133 */ "stl_prefix ::=",
103665
 
 /* 134 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
103666
 
 /* 135 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
103667
 
 /* 136 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
103668
 
 /* 137 */ "dbnm ::=",
103669
 
 /* 138 */ "dbnm ::= DOT nm",
103670
 
 /* 139 */ "fullname ::= nm dbnm",
103671
 
 /* 140 */ "joinop ::= COMMA|JOIN",
103672
 
 /* 141 */ "joinop ::= JOIN_KW JOIN",
103673
 
 /* 142 */ "joinop ::= JOIN_KW nm JOIN",
103674
 
 /* 143 */ "joinop ::= JOIN_KW nm nm JOIN",
103675
 
 /* 144 */ "on_opt ::= ON expr",
103676
 
 /* 145 */ "on_opt ::=",
103677
 
 /* 146 */ "indexed_opt ::=",
103678
 
 /* 147 */ "indexed_opt ::= INDEXED BY nm",
103679
 
 /* 148 */ "indexed_opt ::= NOT INDEXED",
103680
 
 /* 149 */ "using_opt ::= USING LP inscollist RP",
103681
 
 /* 150 */ "using_opt ::=",
103682
 
 /* 151 */ "orderby_opt ::=",
103683
 
 /* 152 */ "orderby_opt ::= ORDER BY sortlist",
103684
 
 /* 153 */ "sortlist ::= sortlist COMMA sortitem sortorder",
103685
 
 /* 154 */ "sortlist ::= sortitem sortorder",
103686
 
 /* 155 */ "sortitem ::= expr",
103687
 
 /* 156 */ "sortorder ::= ASC",
103688
 
 /* 157 */ "sortorder ::= DESC",
103689
 
 /* 158 */ "sortorder ::=",
103690
 
 /* 159 */ "groupby_opt ::=",
103691
 
 /* 160 */ "groupby_opt ::= GROUP BY nexprlist",
103692
 
 /* 161 */ "having_opt ::=",
103693
 
 /* 162 */ "having_opt ::= HAVING expr",
103694
 
 /* 163 */ "limit_opt ::=",
103695
 
 /* 164 */ "limit_opt ::= LIMIT expr",
103696
 
 /* 165 */ "limit_opt ::= LIMIT expr OFFSET expr",
103697
 
 /* 166 */ "limit_opt ::= LIMIT expr COMMA expr",
103698
 
 /* 167 */ "cmd ::= DELETE FROM fullname indexed_opt where_opt",
103699
 
 /* 168 */ "where_opt ::=",
103700
 
 /* 169 */ "where_opt ::= WHERE expr",
103701
 
 /* 170 */ "cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt",
103702
 
 /* 171 */ "setlist ::= setlist COMMA nm EQ expr",
103703
 
 /* 172 */ "setlist ::= nm EQ expr",
103704
 
 /* 173 */ "cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP",
103705
 
 /* 174 */ "cmd ::= insert_cmd INTO fullname inscollist_opt select",
103706
 
 /* 175 */ "cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES",
103707
 
 /* 176 */ "insert_cmd ::= INSERT orconf",
103708
 
 /* 177 */ "insert_cmd ::= REPLACE",
103709
 
 /* 178 */ "itemlist ::= itemlist COMMA expr",
103710
 
 /* 179 */ "itemlist ::= expr",
103711
 
 /* 180 */ "inscollist_opt ::=",
103712
 
 /* 181 */ "inscollist_opt ::= LP inscollist RP",
103713
 
 /* 182 */ "inscollist ::= inscollist COMMA nm",
103714
 
 /* 183 */ "inscollist ::= nm",
103715
 
 /* 184 */ "expr ::= term",
103716
 
 /* 185 */ "expr ::= LP expr RP",
103717
 
 /* 186 */ "term ::= NULL",
103718
 
 /* 187 */ "expr ::= id",
103719
 
 /* 188 */ "expr ::= JOIN_KW",
103720
 
 /* 189 */ "expr ::= nm DOT nm",
103721
 
 /* 190 */ "expr ::= nm DOT nm DOT nm",
103722
 
 /* 191 */ "term ::= INTEGER|FLOAT|BLOB",
103723
 
 /* 192 */ "term ::= STRING",
103724
 
 /* 193 */ "expr ::= REGISTER",
103725
 
 /* 194 */ "expr ::= VARIABLE",
103726
 
 /* 195 */ "expr ::= expr COLLATE ids",
103727
 
 /* 196 */ "expr ::= CAST LP expr AS typetoken RP",
103728
 
 /* 197 */ "expr ::= ID LP distinct exprlist RP",
103729
 
 /* 198 */ "expr ::= ID LP STAR RP",
103730
 
 /* 199 */ "term ::= CTIME_KW",
103731
 
 /* 200 */ "expr ::= expr AND expr",
103732
 
 /* 201 */ "expr ::= expr OR expr",
103733
 
 /* 202 */ "expr ::= expr LT|GT|GE|LE expr",
103734
 
 /* 203 */ "expr ::= expr EQ|NE expr",
103735
 
 /* 204 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
103736
 
 /* 205 */ "expr ::= expr PLUS|MINUS expr",
103737
 
 /* 206 */ "expr ::= expr STAR|SLASH|REM expr",
103738
 
 /* 207 */ "expr ::= expr CONCAT expr",
103739
 
 /* 208 */ "likeop ::= LIKE_KW",
103740
 
 /* 209 */ "likeop ::= NOT LIKE_KW",
103741
 
 /* 210 */ "likeop ::= MATCH",
103742
 
 /* 211 */ "likeop ::= NOT MATCH",
103743
 
 /* 212 */ "expr ::= expr likeop expr",
103744
 
 /* 213 */ "expr ::= expr likeop expr ESCAPE expr",
103745
 
 /* 214 */ "expr ::= expr ISNULL|NOTNULL",
103746
 
 /* 215 */ "expr ::= expr NOT NULL",
103747
 
 /* 216 */ "expr ::= expr IS expr",
103748
 
 /* 217 */ "expr ::= expr IS NOT expr",
103749
 
 /* 218 */ "expr ::= NOT expr",
103750
 
 /* 219 */ "expr ::= BITNOT expr",
103751
 
 /* 220 */ "expr ::= MINUS expr",
103752
 
 /* 221 */ "expr ::= PLUS expr",
103753
 
 /* 222 */ "between_op ::= BETWEEN",
103754
 
 /* 223 */ "between_op ::= NOT BETWEEN",
103755
 
 /* 224 */ "expr ::= expr between_op expr AND expr",
103756
 
 /* 225 */ "in_op ::= IN",
103757
 
 /* 226 */ "in_op ::= NOT IN",
103758
 
 /* 227 */ "expr ::= expr in_op LP exprlist RP",
103759
 
 /* 228 */ "expr ::= LP select RP",
103760
 
 /* 229 */ "expr ::= expr in_op LP select RP",
103761
 
 /* 230 */ "expr ::= expr in_op nm dbnm",
103762
 
 /* 231 */ "expr ::= EXISTS LP select RP",
103763
 
 /* 232 */ "expr ::= CASE case_operand case_exprlist case_else END",
103764
 
 /* 233 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
103765
 
 /* 234 */ "case_exprlist ::= WHEN expr THEN expr",
103766
 
 /* 235 */ "case_else ::= ELSE expr",
103767
 
 /* 236 */ "case_else ::=",
103768
 
 /* 237 */ "case_operand ::= expr",
103769
 
 /* 238 */ "case_operand ::=",
103770
 
 /* 239 */ "exprlist ::= nexprlist",
103771
 
 /* 240 */ "exprlist ::=",
103772
 
 /* 241 */ "nexprlist ::= nexprlist COMMA expr",
103773
 
 /* 242 */ "nexprlist ::= expr",
103774
 
 /* 243 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP",
103775
 
 /* 244 */ "uniqueflag ::= UNIQUE",
103776
 
 /* 245 */ "uniqueflag ::=",
103777
 
 /* 246 */ "idxlist_opt ::=",
103778
 
 /* 247 */ "idxlist_opt ::= LP idxlist RP",
103779
 
 /* 248 */ "idxlist ::= idxlist COMMA nm collate sortorder",
103780
 
 /* 249 */ "idxlist ::= nm collate sortorder",
103781
 
 /* 250 */ "collate ::=",
103782
 
 /* 251 */ "collate ::= COLLATE ids",
103783
 
 /* 252 */ "cmd ::= DROP INDEX ifexists fullname",
103784
 
 /* 253 */ "cmd ::= VACUUM",
103785
 
 /* 254 */ "cmd ::= VACUUM nm",
103786
 
 /* 255 */ "cmd ::= PRAGMA nm dbnm",
103787
 
 /* 256 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
103788
 
 /* 257 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
103789
 
 /* 258 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
103790
 
 /* 259 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
103791
 
 /* 260 */ "nmnum ::= plus_num",
103792
 
 /* 261 */ "nmnum ::= nm",
103793
 
 /* 262 */ "nmnum ::= ON",
103794
 
 /* 263 */ "nmnum ::= DELETE",
103795
 
 /* 264 */ "nmnum ::= DEFAULT",
103796
 
 /* 265 */ "plus_num ::= plus_opt number",
103797
 
 /* 266 */ "minus_num ::= MINUS number",
103798
 
 /* 267 */ "number ::= INTEGER|FLOAT",
103799
 
 /* 268 */ "plus_opt ::= PLUS",
103800
 
 /* 269 */ "plus_opt ::=",
103801
 
 /* 270 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
103802
 
 /* 271 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
103803
 
 /* 272 */ "trigger_time ::= BEFORE",
103804
 
 /* 273 */ "trigger_time ::= AFTER",
103805
 
 /* 274 */ "trigger_time ::= INSTEAD OF",
103806
 
 /* 275 */ "trigger_time ::=",
103807
 
 /* 276 */ "trigger_event ::= DELETE|INSERT",
103808
 
 /* 277 */ "trigger_event ::= UPDATE",
103809
 
 /* 278 */ "trigger_event ::= UPDATE OF inscollist",
103810
 
 /* 279 */ "foreach_clause ::=",
103811
 
 /* 280 */ "foreach_clause ::= FOR EACH ROW",
103812
 
 /* 281 */ "when_clause ::=",
103813
 
 /* 282 */ "when_clause ::= WHEN expr",
103814
 
 /* 283 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
103815
 
 /* 284 */ "trigger_cmd_list ::= trigger_cmd SEMI",
103816
 
 /* 285 */ "trnm ::= nm",
103817
 
 /* 286 */ "trnm ::= nm DOT nm",
103818
 
 /* 287 */ "tridxby ::=",
103819
 
 /* 288 */ "tridxby ::= INDEXED BY nm",
103820
 
 /* 289 */ "tridxby ::= NOT INDEXED",
103821
 
 /* 290 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
103822
 
 /* 291 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt VALUES LP itemlist RP",
103823
 
 /* 292 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select",
103824
 
 /* 293 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
103825
 
 /* 294 */ "trigger_cmd ::= select",
103826
 
 /* 295 */ "expr ::= RAISE LP IGNORE RP",
103827
 
 /* 296 */ "expr ::= RAISE LP raisetype COMMA nm RP",
103828
 
 /* 297 */ "raisetype ::= ROLLBACK",
103829
 
 /* 298 */ "raisetype ::= ABORT",
103830
 
 /* 299 */ "raisetype ::= FAIL",
103831
 
 /* 300 */ "cmd ::= DROP TRIGGER ifexists fullname",
103832
 
 /* 301 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
103833
 
 /* 302 */ "cmd ::= DETACH database_kw_opt expr",
103834
 
 /* 303 */ "key_opt ::=",
103835
 
 /* 304 */ "key_opt ::= KEY expr",
103836
 
 /* 305 */ "database_kw_opt ::= DATABASE",
103837
 
 /* 306 */ "database_kw_opt ::=",
103838
 
 /* 307 */ "cmd ::= REINDEX",
103839
 
 /* 308 */ "cmd ::= REINDEX nm dbnm",
103840
 
 /* 309 */ "cmd ::= ANALYZE",
103841
 
 /* 310 */ "cmd ::= ANALYZE nm dbnm",
103842
 
 /* 311 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
103843
 
 /* 312 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column",
103844
 
 /* 313 */ "add_column_fullname ::= fullname",
103845
 
 /* 314 */ "kwcolumn_opt ::=",
103846
 
 /* 315 */ "kwcolumn_opt ::= COLUMNKW",
103847
 
 /* 316 */ "cmd ::= create_vtab",
103848
 
 /* 317 */ "cmd ::= create_vtab LP vtabarglist RP",
103849
 
 /* 318 */ "create_vtab ::= createkw VIRTUAL TABLE nm dbnm USING nm",
103850
 
 /* 319 */ "vtabarglist ::= vtabarg",
103851
 
 /* 320 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
103852
 
 /* 321 */ "vtabarg ::=",
103853
 
 /* 322 */ "vtabarg ::= vtabarg vtabargtoken",
103854
 
 /* 323 */ "vtabargtoken ::= ANY",
103855
 
 /* 324 */ "vtabargtoken ::= lp anylist RP",
103856
 
 /* 325 */ "lp ::= LP",
103857
 
 /* 326 */ "anylist ::=",
103858
 
 /* 327 */ "anylist ::= anylist LP anylist RP",
103859
 
 /* 328 */ "anylist ::= anylist ANY",
103860
 
};
103861
 
#endif /* NDEBUG */
103862
 
 
103863
 
 
103864
 
#if YYSTACKDEPTH<=0
103865
 
/*
103866
 
** Try to increase the size of the parser stack.
103867
 
*/
103868
 
static void yyGrowStack(yyParser *p){
103869
 
  int newSize;
103870
 
  yyStackEntry *pNew;
103871
 
 
103872
 
  newSize = p->yystksz*2 + 100;
103873
 
  pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
103874
 
  if( pNew ){
103875
 
    p->yystack = pNew;
103876
 
    p->yystksz = newSize;
103877
 
#ifndef NDEBUG
103878
 
    if( yyTraceFILE ){
103879
 
      fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
103880
 
              yyTracePrompt, p->yystksz);
103881
 
    }
103882
 
#endif
103883
 
  }
103884
 
}
103885
 
#endif
103886
 
 
103887
 
/* 
103888
 
** This function allocates a new parser.
103889
 
** The only argument is a pointer to a function which works like
103890
 
** malloc.
103891
 
**
103892
 
** Inputs:
103893
 
** A pointer to the function used to allocate memory.
103894
 
**
103895
 
** Outputs:
103896
 
** A pointer to a parser.  This pointer is used in subsequent calls
103897
 
** to sqlite3Parser and sqlite3ParserFree.
103898
 
*/
103899
 
SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(size_t)){
103900
 
  yyParser *pParser;
103901
 
  pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
103902
 
  if( pParser ){
103903
 
    pParser->yyidx = -1;
103904
 
#ifdef YYTRACKMAXSTACKDEPTH
103905
 
    pParser->yyidxMax = 0;
103906
 
#endif
103907
 
#if YYSTACKDEPTH<=0
103908
 
    pParser->yystack = NULL;
103909
 
    pParser->yystksz = 0;
103910
 
    yyGrowStack(pParser);
103911
 
#endif
103912
 
  }
103913
 
  return pParser;
103914
 
}
103915
 
 
103916
 
/* The following function deletes the value associated with a
103917
 
** symbol.  The symbol can be either a terminal or nonterminal.
103918
 
** "yymajor" is the symbol code, and "yypminor" is a pointer to
103919
 
** the value.
103920
 
*/
103921
 
static void yy_destructor(
103922
 
  yyParser *yypParser,    /* The parser */
103923
 
  YYCODETYPE yymajor,     /* Type code for object to destroy */
103924
 
  YYMINORTYPE *yypminor   /* The object to be destroyed */
103925
 
){
103926
 
  sqlite3ParserARG_FETCH;
103927
 
  switch( yymajor ){
103928
 
    /* Here is inserted the actions which take place when a
103929
 
    ** terminal or non-terminal is destroyed.  This can happen
103930
 
    ** when the symbol is popped from the stack during a
103931
 
    ** reduce or during error processing or when a parser is 
103932
 
    ** being destroyed before it is finished parsing.
103933
 
    **
103934
 
    ** Note: during a reduce, the only symbols destroyed are those
103935
 
    ** which appear on the RHS of the rule, but which are not used
103936
 
    ** inside the C code.
103937
 
    */
103938
 
    case 160: /* select */
103939
 
    case 194: /* oneselect */
103940
 
{
103941
 
sqlite3SelectDelete(pParse->db, (yypminor->yy387));
103942
 
}
103943
 
      break;
103944
 
    case 174: /* term */
103945
 
    case 175: /* expr */
103946
 
{
103947
 
sqlite3ExprDelete(pParse->db, (yypminor->yy118).pExpr);
103948
 
}
103949
 
      break;
103950
 
    case 179: /* idxlist_opt */
103951
 
    case 187: /* idxlist */
103952
 
    case 197: /* selcollist */
103953
 
    case 200: /* groupby_opt */
103954
 
    case 202: /* orderby_opt */
103955
 
    case 204: /* sclp */
103956
 
    case 214: /* sortlist */
103957
 
    case 216: /* nexprlist */
103958
 
    case 217: /* setlist */
103959
 
    case 220: /* itemlist */
103960
 
    case 221: /* exprlist */
103961
 
    case 226: /* case_exprlist */
103962
 
{
103963
 
sqlite3ExprListDelete(pParse->db, (yypminor->yy322));
103964
 
}
103965
 
      break;
103966
 
    case 193: /* fullname */
103967
 
    case 198: /* from */
103968
 
    case 206: /* seltablist */
103969
 
    case 207: /* stl_prefix */
103970
 
{
103971
 
sqlite3SrcListDelete(pParse->db, (yypminor->yy259));
103972
 
}
103973
 
      break;
103974
 
    case 199: /* where_opt */
103975
 
    case 201: /* having_opt */
103976
 
    case 210: /* on_opt */
103977
 
    case 215: /* sortitem */
103978
 
    case 225: /* case_operand */
103979
 
    case 227: /* case_else */
103980
 
    case 238: /* when_clause */
103981
 
    case 243: /* key_opt */
103982
 
{
103983
 
sqlite3ExprDelete(pParse->db, (yypminor->yy314));
103984
 
}
103985
 
      break;
103986
 
    case 211: /* using_opt */
103987
 
    case 213: /* inscollist */
103988
 
    case 219: /* inscollist_opt */
103989
 
{
103990
 
sqlite3IdListDelete(pParse->db, (yypminor->yy384));
103991
 
}
103992
 
      break;
103993
 
    case 234: /* trigger_cmd_list */
103994
 
    case 239: /* trigger_cmd */
103995
 
{
103996
 
sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy203));
103997
 
}
103998
 
      break;
103999
 
    case 236: /* trigger_event */
104000
 
{
104001
 
sqlite3IdListDelete(pParse->db, (yypminor->yy90).b);
104002
 
}
104003
 
      break;
104004
 
    default:  break;   /* If no destructor action specified: do nothing */
104005
 
  }
104006
 
}
104007
 
 
104008
 
/*
104009
 
** Pop the parser's stack once.
104010
 
**
104011
 
** If there is a destructor routine associated with the token which
104012
 
** is popped from the stack, then call it.
104013
 
**
104014
 
** Return the major token number for the symbol popped.
104015
 
*/
104016
 
static int yy_pop_parser_stack(yyParser *pParser){
104017
 
  YYCODETYPE yymajor;
104018
 
  yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
104019
 
 
104020
 
  /* There is no mechanism by which the parser stack can be popped below
104021
 
  ** empty in SQLite.  */
104022
 
  if( NEVER(pParser->yyidx<0) ) return 0;
104023
 
#ifndef NDEBUG
104024
 
  if( yyTraceFILE && pParser->yyidx>=0 ){
104025
 
    fprintf(yyTraceFILE,"%sPopping %s\n",
104026
 
      yyTracePrompt,
104027
 
      yyTokenName[yytos->major]);
104028
 
  }
104029
 
#endif
104030
 
  yymajor = yytos->major;
104031
 
  yy_destructor(pParser, yymajor, &yytos->minor);
104032
 
  pParser->yyidx--;
104033
 
  return yymajor;
104034
 
}
104035
 
 
104036
 
/* 
104037
 
** Deallocate and destroy a parser.  Destructors are all called for
104038
 
** all stack elements before shutting the parser down.
104039
 
**
104040
 
** Inputs:
104041
 
** <ul>
104042
 
** <li>  A pointer to the parser.  This should be a pointer
104043
 
**       obtained from sqlite3ParserAlloc.
104044
 
** <li>  A pointer to a function used to reclaim memory obtained
104045
 
**       from malloc.
104046
 
** </ul>
104047
 
*/
104048
 
SQLITE_PRIVATE void sqlite3ParserFree(
104049
 
  void *p,                    /* The parser to be deleted */
104050
 
  void (*freeProc)(void*)     /* Function used to reclaim memory */
104051
 
){
104052
 
  yyParser *pParser = (yyParser*)p;
104053
 
  /* In SQLite, we never try to destroy a parser that was not successfully
104054
 
  ** created in the first place. */
104055
 
  if( NEVER(pParser==0) ) return;
104056
 
  while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
104057
 
#if YYSTACKDEPTH<=0
104058
 
  free(pParser->yystack);
104059
 
#endif
104060
 
  (*freeProc)((void*)pParser);
104061
 
}
104062
 
 
104063
 
/*
104064
 
** Return the peak depth of the stack for a parser.
104065
 
*/
104066
 
#ifdef YYTRACKMAXSTACKDEPTH
104067
 
SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
104068
 
  yyParser *pParser = (yyParser*)p;
104069
 
  return pParser->yyidxMax;
104070
 
}
104071
 
#endif
104072
 
 
104073
 
/*
104074
 
** Find the appropriate action for a parser given the terminal
104075
 
** look-ahead token iLookAhead.
104076
 
**
104077
 
** If the look-ahead token is YYNOCODE, then check to see if the action is
104078
 
** independent of the look-ahead.  If it is, return the action, otherwise
104079
 
** return YY_NO_ACTION.
104080
 
*/
104081
 
static int yy_find_shift_action(
104082
 
  yyParser *pParser,        /* The parser */
104083
 
  YYCODETYPE iLookAhead     /* The look-ahead token */
104084
 
){
104085
 
  int i;
104086
 
  int stateno = pParser->yystack[pParser->yyidx].stateno;
104087
 
 
104088
 
  if( stateno>YY_SHIFT_COUNT
104089
 
   || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
104090
 
    return yy_default[stateno];
104091
 
  }
104092
 
  assert( iLookAhead!=YYNOCODE );
104093
 
  i += iLookAhead;
104094
 
  if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
104095
 
    if( iLookAhead>0 ){
104096
 
#ifdef YYFALLBACK
104097
 
      YYCODETYPE iFallback;            /* Fallback token */
104098
 
      if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
104099
 
             && (iFallback = yyFallback[iLookAhead])!=0 ){
104100
 
#ifndef NDEBUG
104101
 
        if( yyTraceFILE ){
104102
 
          fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
104103
 
             yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
104104
 
        }
104105
 
#endif
104106
 
        return yy_find_shift_action(pParser, iFallback);
104107
 
      }
104108
 
#endif
104109
 
#ifdef YYWILDCARD
104110
 
      {
104111
 
        int j = i - iLookAhead + YYWILDCARD;
104112
 
        if( 
104113
 
#if YY_SHIFT_MIN+YYWILDCARD<0
104114
 
          j>=0 &&
104115
 
#endif
104116
 
#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
104117
 
          j<YY_ACTTAB_COUNT &&
104118
 
#endif
104119
 
          yy_lookahead[j]==YYWILDCARD
104120
 
        ){
104121
 
#ifndef NDEBUG
104122
 
          if( yyTraceFILE ){
104123
 
            fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
104124
 
               yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
104125
 
          }
104126
 
#endif /* NDEBUG */
104127
 
          return yy_action[j];
104128
 
        }
104129
 
      }
104130
 
#endif /* YYWILDCARD */
104131
 
    }
104132
 
    return yy_default[stateno];
104133
 
  }else{
104134
 
    return yy_action[i];
104135
 
  }
104136
 
}
104137
 
 
104138
 
/*
104139
 
** Find the appropriate action for a parser given the non-terminal
104140
 
** look-ahead token iLookAhead.
104141
 
**
104142
 
** If the look-ahead token is YYNOCODE, then check to see if the action is
104143
 
** independent of the look-ahead.  If it is, return the action, otherwise
104144
 
** return YY_NO_ACTION.
104145
 
*/
104146
 
static int yy_find_reduce_action(
104147
 
  int stateno,              /* Current state number */
104148
 
  YYCODETYPE iLookAhead     /* The look-ahead token */
104149
 
){
104150
 
  int i;
104151
 
#ifdef YYERRORSYMBOL
104152
 
  if( stateno>YY_REDUCE_COUNT ){
104153
 
    return yy_default[stateno];
104154
 
  }
104155
 
#else
104156
 
  assert( stateno<=YY_REDUCE_COUNT );
104157
 
#endif
104158
 
  i = yy_reduce_ofst[stateno];
104159
 
  assert( i!=YY_REDUCE_USE_DFLT );
104160
 
  assert( iLookAhead!=YYNOCODE );
104161
 
  i += iLookAhead;
104162
 
#ifdef YYERRORSYMBOL
104163
 
  if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
104164
 
    return yy_default[stateno];
104165
 
  }
104166
 
#else
104167
 
  assert( i>=0 && i<YY_ACTTAB_COUNT );
104168
 
  assert( yy_lookahead[i]==iLookAhead );
104169
 
#endif
104170
 
  return yy_action[i];
104171
 
}
104172
 
 
104173
 
/*
104174
 
** The following routine is called if the stack overflows.
104175
 
*/
104176
 
static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
104177
 
   sqlite3ParserARG_FETCH;
104178
 
   yypParser->yyidx--;
104179
 
#ifndef NDEBUG
104180
 
   if( yyTraceFILE ){
104181
 
     fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
104182
 
   }
104183
 
#endif
104184
 
   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
104185
 
   /* Here code is inserted which will execute if the parser
104186
 
   ** stack every overflows */
104187
 
 
104188
 
  UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
104189
 
  sqlite3ErrorMsg(pParse, "parser stack overflow");
104190
 
  pParse->parseError = 1;
104191
 
   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
104192
 
}
104193
 
 
104194
 
/*
104195
 
** Perform a shift action.
104196
 
*/
104197
 
static void yy_shift(
104198
 
  yyParser *yypParser,          /* The parser to be shifted */
104199
 
  int yyNewState,               /* The new state to shift in */
104200
 
  int yyMajor,                  /* The major token to shift in */
104201
 
  YYMINORTYPE *yypMinor         /* Pointer to the minor token to shift in */
104202
 
){
104203
 
  yyStackEntry *yytos;
104204
 
  yypParser->yyidx++;
104205
 
#ifdef YYTRACKMAXSTACKDEPTH
104206
 
  if( yypParser->yyidx>yypParser->yyidxMax ){
104207
 
    yypParser->yyidxMax = yypParser->yyidx;
104208
 
  }
104209
 
#endif
104210
 
#if YYSTACKDEPTH>0 
104211
 
  if( yypParser->yyidx>=YYSTACKDEPTH ){
104212
 
    yyStackOverflow(yypParser, yypMinor);
104213
 
    return;
104214
 
  }
104215
 
#else
104216
 
  if( yypParser->yyidx>=yypParser->yystksz ){
104217
 
    yyGrowStack(yypParser);
104218
 
    if( yypParser->yyidx>=yypParser->yystksz ){
104219
 
      yyStackOverflow(yypParser, yypMinor);
104220
 
      return;
104221
 
    }
104222
 
  }
104223
 
#endif
104224
 
  yytos = &yypParser->yystack[yypParser->yyidx];
104225
 
  yytos->stateno = (YYACTIONTYPE)yyNewState;
104226
 
  yytos->major = (YYCODETYPE)yyMajor;
104227
 
  yytos->minor = *yypMinor;
104228
 
#ifndef NDEBUG
104229
 
  if( yyTraceFILE && yypParser->yyidx>0 ){
104230
 
    int i;
104231
 
    fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
104232
 
    fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
104233
 
    for(i=1; i<=yypParser->yyidx; i++)
104234
 
      fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
104235
 
    fprintf(yyTraceFILE,"\n");
104236
 
  }
104237
 
#endif
104238
 
}
104239
 
 
104240
 
/* The following table contains information about every rule that
104241
 
** is used during the reduce.
104242
 
*/
104243
 
static const struct {
104244
 
  YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
104245
 
  unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
104246
 
} yyRuleInfo[] = {
104247
 
  { 142, 1 },
104248
 
  { 143, 2 },
104249
 
  { 143, 1 },
104250
 
  { 144, 1 },
104251
 
  { 144, 3 },
104252
 
  { 145, 0 },
104253
 
  { 145, 1 },
104254
 
  { 145, 3 },
104255
 
  { 146, 1 },
104256
 
  { 147, 3 },
104257
 
  { 149, 0 },
104258
 
  { 149, 1 },
104259
 
  { 149, 2 },
104260
 
  { 148, 0 },
104261
 
  { 148, 1 },
104262
 
  { 148, 1 },
104263
 
  { 148, 1 },
104264
 
  { 147, 2 },
104265
 
  { 147, 2 },
104266
 
  { 147, 2 },
104267
 
  { 151, 1 },
104268
 
  { 151, 0 },
104269
 
  { 147, 2 },
104270
 
  { 147, 3 },
104271
 
  { 147, 5 },
104272
 
  { 147, 2 },
104273
 
  { 152, 6 },
104274
 
  { 154, 1 },
104275
 
  { 156, 0 },
104276
 
  { 156, 3 },
104277
 
  { 155, 1 },
104278
 
  { 155, 0 },
104279
 
  { 153, 4 },
104280
 
  { 153, 2 },
104281
 
  { 158, 3 },
104282
 
  { 158, 1 },
104283
 
  { 161, 3 },
104284
 
  { 162, 1 },
104285
 
  { 165, 1 },
104286
 
  { 165, 1 },
104287
 
  { 166, 1 },
104288
 
  { 150, 1 },
104289
 
  { 150, 1 },
104290
 
  { 150, 1 },
104291
 
  { 163, 0 },
104292
 
  { 163, 1 },
104293
 
  { 167, 1 },
104294
 
  { 167, 4 },
104295
 
  { 167, 6 },
104296
 
  { 168, 1 },
104297
 
  { 168, 2 },
104298
 
  { 169, 1 },
104299
 
  { 169, 1 },
104300
 
  { 164, 2 },
104301
 
  { 164, 0 },
104302
 
  { 172, 3 },
104303
 
  { 172, 1 },
104304
 
  { 173, 2 },
104305
 
  { 173, 4 },
104306
 
  { 173, 3 },
104307
 
  { 173, 3 },
104308
 
  { 173, 2 },
104309
 
  { 173, 2 },
104310
 
  { 173, 3 },
104311
 
  { 173, 5 },
104312
 
  { 173, 2 },
104313
 
  { 173, 4 },
104314
 
  { 173, 4 },
104315
 
  { 173, 1 },
104316
 
  { 173, 2 },
104317
 
  { 178, 0 },
104318
 
  { 178, 1 },
104319
 
  { 180, 0 },
104320
 
  { 180, 2 },
104321
 
  { 182, 2 },
104322
 
  { 182, 3 },
104323
 
  { 182, 3 },
104324
 
  { 182, 3 },
104325
 
  { 183, 2 },
104326
 
  { 183, 2 },
104327
 
  { 183, 1 },
104328
 
  { 183, 1 },
104329
 
  { 183, 2 },
104330
 
  { 181, 3 },
104331
 
  { 181, 2 },
104332
 
  { 184, 0 },
104333
 
  { 184, 2 },
104334
 
  { 184, 2 },
104335
 
  { 159, 0 },
104336
 
  { 159, 2 },
104337
 
  { 185, 3 },
104338
 
  { 185, 2 },
104339
 
  { 185, 1 },
104340
 
  { 186, 2 },
104341
 
  { 186, 7 },
104342
 
  { 186, 5 },
104343
 
  { 186, 5 },
104344
 
  { 186, 10 },
104345
 
  { 188, 0 },
104346
 
  { 188, 1 },
104347
 
  { 176, 0 },
104348
 
  { 176, 3 },
104349
 
  { 189, 0 },
104350
 
  { 189, 2 },
104351
 
  { 190, 1 },
104352
 
  { 190, 1 },
104353
 
  { 190, 1 },
104354
 
  { 147, 4 },
104355
 
  { 192, 2 },
104356
 
  { 192, 0 },
104357
 
  { 147, 8 },
104358
 
  { 147, 4 },
104359
 
  { 147, 1 },
104360
 
  { 160, 1 },
104361
 
  { 160, 3 },
104362
 
  { 195, 1 },
104363
 
  { 195, 2 },
104364
 
  { 195, 1 },
104365
 
  { 194, 9 },
104366
 
  { 196, 1 },
104367
 
  { 196, 1 },
104368
 
  { 196, 0 },
104369
 
  { 204, 2 },
104370
 
  { 204, 0 },
104371
 
  { 197, 3 },
104372
 
  { 197, 2 },
104373
 
  { 197, 4 },
104374
 
  { 205, 2 },
104375
 
  { 205, 1 },
104376
 
  { 205, 0 },
104377
 
  { 198, 0 },
104378
 
  { 198, 2 },
104379
 
  { 207, 2 },
104380
 
  { 207, 0 },
104381
 
  { 206, 7 },
104382
 
  { 206, 7 },
104383
 
  { 206, 7 },
104384
 
  { 157, 0 },
104385
 
  { 157, 2 },
104386
 
  { 193, 2 },
104387
 
  { 208, 1 },
104388
 
  { 208, 2 },
104389
 
  { 208, 3 },
104390
 
  { 208, 4 },
104391
 
  { 210, 2 },
104392
 
  { 210, 0 },
104393
 
  { 209, 0 },
104394
 
  { 209, 3 },
104395
 
  { 209, 2 },
104396
 
  { 211, 4 },
104397
 
  { 211, 0 },
104398
 
  { 202, 0 },
104399
 
  { 202, 3 },
104400
 
  { 214, 4 },
104401
 
  { 214, 2 },
104402
 
  { 215, 1 },
104403
 
  { 177, 1 },
104404
 
  { 177, 1 },
104405
 
  { 177, 0 },
104406
 
  { 200, 0 },
104407
 
  { 200, 3 },
104408
 
  { 201, 0 },
104409
 
  { 201, 2 },
104410
 
  { 203, 0 },
104411
 
  { 203, 2 },
104412
 
  { 203, 4 },
104413
 
  { 203, 4 },
104414
 
  { 147, 5 },
104415
 
  { 199, 0 },
104416
 
  { 199, 2 },
104417
 
  { 147, 7 },
104418
 
  { 217, 5 },
104419
 
  { 217, 3 },
104420
 
  { 147, 8 },
104421
 
  { 147, 5 },
104422
 
  { 147, 6 },
104423
 
  { 218, 2 },
104424
 
  { 218, 1 },
104425
 
  { 220, 3 },
104426
 
  { 220, 1 },
104427
 
  { 219, 0 },
104428
 
  { 219, 3 },
104429
 
  { 213, 3 },
104430
 
  { 213, 1 },
104431
 
  { 175, 1 },
104432
 
  { 175, 3 },
104433
 
  { 174, 1 },
104434
 
  { 175, 1 },
104435
 
  { 175, 1 },
104436
 
  { 175, 3 },
104437
 
  { 175, 5 },
104438
 
  { 174, 1 },
104439
 
  { 174, 1 },
104440
 
  { 175, 1 },
104441
 
  { 175, 1 },
104442
 
  { 175, 3 },
104443
 
  { 175, 6 },
104444
 
  { 175, 5 },
104445
 
  { 175, 4 },
104446
 
  { 174, 1 },
104447
 
  { 175, 3 },
104448
 
  { 175, 3 },
104449
 
  { 175, 3 },
104450
 
  { 175, 3 },
104451
 
  { 175, 3 },
104452
 
  { 175, 3 },
104453
 
  { 175, 3 },
104454
 
  { 175, 3 },
104455
 
  { 222, 1 },
104456
 
  { 222, 2 },
104457
 
  { 222, 1 },
104458
 
  { 222, 2 },
104459
 
  { 175, 3 },
104460
 
  { 175, 5 },
104461
 
  { 175, 2 },
104462
 
  { 175, 3 },
104463
 
  { 175, 3 },
104464
 
  { 175, 4 },
104465
 
  { 175, 2 },
104466
 
  { 175, 2 },
104467
 
  { 175, 2 },
104468
 
  { 175, 2 },
104469
 
  { 223, 1 },
104470
 
  { 223, 2 },
104471
 
  { 175, 5 },
104472
 
  { 224, 1 },
104473
 
  { 224, 2 },
104474
 
  { 175, 5 },
104475
 
  { 175, 3 },
104476
 
  { 175, 5 },
104477
 
  { 175, 4 },
104478
 
  { 175, 4 },
104479
 
  { 175, 5 },
104480
 
  { 226, 5 },
104481
 
  { 226, 4 },
104482
 
  { 227, 2 },
104483
 
  { 227, 0 },
104484
 
  { 225, 1 },
104485
 
  { 225, 0 },
104486
 
  { 221, 1 },
104487
 
  { 221, 0 },
104488
 
  { 216, 3 },
104489
 
  { 216, 1 },
104490
 
  { 147, 11 },
104491
 
  { 228, 1 },
104492
 
  { 228, 0 },
104493
 
  { 179, 0 },
104494
 
  { 179, 3 },
104495
 
  { 187, 5 },
104496
 
  { 187, 3 },
104497
 
  { 229, 0 },
104498
 
  { 229, 2 },
104499
 
  { 147, 4 },
104500
 
  { 147, 1 },
104501
 
  { 147, 2 },
104502
 
  { 147, 3 },
104503
 
  { 147, 5 },
104504
 
  { 147, 6 },
104505
 
  { 147, 5 },
104506
 
  { 147, 6 },
104507
 
  { 230, 1 },
104508
 
  { 230, 1 },
104509
 
  { 230, 1 },
104510
 
  { 230, 1 },
104511
 
  { 230, 1 },
104512
 
  { 170, 2 },
104513
 
  { 171, 2 },
104514
 
  { 232, 1 },
104515
 
  { 231, 1 },
104516
 
  { 231, 0 },
104517
 
  { 147, 5 },
104518
 
  { 233, 11 },
104519
 
  { 235, 1 },
104520
 
  { 235, 1 },
104521
 
  { 235, 2 },
104522
 
  { 235, 0 },
104523
 
  { 236, 1 },
104524
 
  { 236, 1 },
104525
 
  { 236, 3 },
104526
 
  { 237, 0 },
104527
 
  { 237, 3 },
104528
 
  { 238, 0 },
104529
 
  { 238, 2 },
104530
 
  { 234, 3 },
104531
 
  { 234, 2 },
104532
 
  { 240, 1 },
104533
 
  { 240, 3 },
104534
 
  { 241, 0 },
104535
 
  { 241, 3 },
104536
 
  { 241, 2 },
104537
 
  { 239, 7 },
104538
 
  { 239, 8 },
104539
 
  { 239, 5 },
104540
 
  { 239, 5 },
104541
 
  { 239, 1 },
104542
 
  { 175, 4 },
104543
 
  { 175, 6 },
104544
 
  { 191, 1 },
104545
 
  { 191, 1 },
104546
 
  { 191, 1 },
104547
 
  { 147, 4 },
104548
 
  { 147, 6 },
104549
 
  { 147, 3 },
104550
 
  { 243, 0 },
104551
 
  { 243, 2 },
104552
 
  { 242, 1 },
104553
 
  { 242, 0 },
104554
 
  { 147, 1 },
104555
 
  { 147, 3 },
104556
 
  { 147, 1 },
104557
 
  { 147, 3 },
104558
 
  { 147, 6 },
104559
 
  { 147, 6 },
104560
 
  { 244, 1 },
104561
 
  { 245, 0 },
104562
 
  { 245, 1 },
104563
 
  { 147, 1 },
104564
 
  { 147, 4 },
104565
 
  { 246, 7 },
104566
 
  { 247, 1 },
104567
 
  { 247, 3 },
104568
 
  { 248, 0 },
104569
 
  { 248, 2 },
104570
 
  { 249, 1 },
104571
 
  { 249, 3 },
104572
 
  { 250, 1 },
104573
 
  { 251, 0 },
104574
 
  { 251, 4 },
104575
 
  { 251, 2 },
104576
 
};
104577
 
 
104578
 
static void yy_accept(yyParser*);  /* Forward Declaration */
104579
 
 
104580
 
/*
104581
 
** Perform a reduce action and the shift that must immediately
104582
 
** follow the reduce.
104583
 
*/
104584
 
static void yy_reduce(
104585
 
  yyParser *yypParser,         /* The parser */
104586
 
  int yyruleno                 /* Number of the rule by which to reduce */
104587
 
){
104588
 
  int yygoto;                     /* The next state */
104589
 
  int yyact;                      /* The next action */
104590
 
  YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
104591
 
  yyStackEntry *yymsp;            /* The top of the parser's stack */
104592
 
  int yysize;                     /* Amount to pop the stack */
104593
 
  sqlite3ParserARG_FETCH;
104594
 
  yymsp = &yypParser->yystack[yypParser->yyidx];
104595
 
#ifndef NDEBUG
104596
 
  if( yyTraceFILE && yyruleno>=0 
104597
 
        && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
104598
 
    fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
104599
 
      yyRuleName[yyruleno]);
104600
 
  }
104601
 
#endif /* NDEBUG */
104602
 
 
104603
 
  /* Silence complaints from purify about yygotominor being uninitialized
104604
 
  ** in some cases when it is copied into the stack after the following
104605
 
  ** switch.  yygotominor is uninitialized when a rule reduces that does
104606
 
  ** not set the value of its left-hand side nonterminal.  Leaving the
104607
 
  ** value of the nonterminal uninitialized is utterly harmless as long
104608
 
  ** as the value is never used.  So really the only thing this code
104609
 
  ** accomplishes is to quieten purify.  
104610
 
  **
104611
 
  ** 2007-01-16:  The wireshark project (www.wireshark.org) reports that
104612
 
  ** without this code, their parser segfaults.  I'm not sure what there
104613
 
  ** parser is doing to make this happen.  This is the second bug report
104614
 
  ** from wireshark this week.  Clearly they are stressing Lemon in ways
104615
 
  ** that it has not been previously stressed...  (SQLite ticket #2172)
104616
 
  */
104617
 
  /*memset(&yygotominor, 0, sizeof(yygotominor));*/
104618
 
  yygotominor = yyzerominor;
104619
 
 
104620
 
 
104621
 
  switch( yyruleno ){
104622
 
  /* Beginning here are the reduction cases.  A typical example
104623
 
  ** follows:
104624
 
  **   case 0:
104625
 
  **  #line <lineno> <grammarfile>
104626
 
  **     { ... }           // User supplied code
104627
 
  **  #line <lineno> <thisfile>
104628
 
  **     break;
104629
 
  */
104630
 
      case 5: /* explain ::= */
104631
 
{ sqlite3BeginParse(pParse, 0); }
104632
 
        break;
104633
 
      case 6: /* explain ::= EXPLAIN */
104634
 
{ sqlite3BeginParse(pParse, 1); }
104635
 
        break;
104636
 
      case 7: /* explain ::= EXPLAIN QUERY PLAN */
104637
 
{ sqlite3BeginParse(pParse, 2); }
104638
 
        break;
104639
 
      case 8: /* cmdx ::= cmd */
104640
 
{ sqlite3FinishCoding(pParse); }
104641
 
        break;
104642
 
      case 9: /* cmd ::= BEGIN transtype trans_opt */
104643
 
{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy4);}
104644
 
        break;
104645
 
      case 13: /* transtype ::= */
104646
 
{yygotominor.yy4 = TK_DEFERRED;}
104647
 
        break;
104648
 
      case 14: /* transtype ::= DEFERRED */
104649
 
      case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
104650
 
      case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
104651
 
      case 115: /* multiselect_op ::= UNION */ yytestcase(yyruleno==115);
104652
 
      case 117: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==117);
104653
 
{yygotominor.yy4 = yymsp[0].major;}
104654
 
        break;
104655
 
      case 17: /* cmd ::= COMMIT trans_opt */
104656
 
      case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
104657
 
{sqlite3CommitTransaction(pParse);}
104658
 
        break;
104659
 
      case 19: /* cmd ::= ROLLBACK trans_opt */
104660
 
{sqlite3RollbackTransaction(pParse);}
104661
 
        break;
104662
 
      case 22: /* cmd ::= SAVEPOINT nm */
104663
 
{
104664
 
  sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
104665
 
}
104666
 
        break;
104667
 
      case 23: /* cmd ::= RELEASE savepoint_opt nm */
104668
 
{
104669
 
  sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
104670
 
}
104671
 
        break;
104672
 
      case 24: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
104673
 
{
104674
 
  sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
104675
 
}
104676
 
        break;
104677
 
      case 26: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
104678
 
{
104679
 
   sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy4,0,0,yymsp[-2].minor.yy4);
104680
 
}
104681
 
        break;
104682
 
      case 27: /* createkw ::= CREATE */
104683
 
{
104684
 
  pParse->db->lookaside.bEnabled = 0;
104685
 
  yygotominor.yy0 = yymsp[0].minor.yy0;
104686
 
}
104687
 
        break;
104688
 
      case 28: /* ifnotexists ::= */
104689
 
      case 31: /* temp ::= */ yytestcase(yyruleno==31);
104690
 
      case 70: /* autoinc ::= */ yytestcase(yyruleno==70);
104691
 
      case 83: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==83);
104692
 
      case 85: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==85);
104693
 
      case 87: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==87);
104694
 
      case 98: /* defer_subclause_opt ::= */ yytestcase(yyruleno==98);
104695
 
      case 109: /* ifexists ::= */ yytestcase(yyruleno==109);
104696
 
      case 120: /* distinct ::= ALL */ yytestcase(yyruleno==120);
104697
 
      case 121: /* distinct ::= */ yytestcase(yyruleno==121);
104698
 
      case 222: /* between_op ::= BETWEEN */ yytestcase(yyruleno==222);
104699
 
      case 225: /* in_op ::= IN */ yytestcase(yyruleno==225);
104700
 
{yygotominor.yy4 = 0;}
104701
 
        break;
104702
 
      case 29: /* ifnotexists ::= IF NOT EXISTS */
104703
 
      case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
104704
 
      case 71: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==71);
104705
 
      case 86: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==86);
104706
 
      case 108: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==108);
104707
 
      case 119: /* distinct ::= DISTINCT */ yytestcase(yyruleno==119);
104708
 
      case 223: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==223);
104709
 
      case 226: /* in_op ::= NOT IN */ yytestcase(yyruleno==226);
104710
 
{yygotominor.yy4 = 1;}
104711
 
        break;
104712
 
      case 32: /* create_table_args ::= LP columnlist conslist_opt RP */
104713
 
{
104714
 
  sqlite3EndTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0);
104715
 
}
104716
 
        break;
104717
 
      case 33: /* create_table_args ::= AS select */
104718
 
{
104719
 
  sqlite3EndTable(pParse,0,0,yymsp[0].minor.yy387);
104720
 
  sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
104721
 
}
104722
 
        break;
104723
 
      case 36: /* column ::= columnid type carglist */
104724
 
{
104725
 
  yygotominor.yy0.z = yymsp[-2].minor.yy0.z;
104726
 
  yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n;
104727
 
}
104728
 
        break;
104729
 
      case 37: /* columnid ::= nm */
104730
 
{
104731
 
  sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
104732
 
  yygotominor.yy0 = yymsp[0].minor.yy0;
104733
 
}
104734
 
        break;
104735
 
      case 38: /* id ::= ID */
104736
 
      case 39: /* id ::= INDEXED */ yytestcase(yyruleno==39);
104737
 
      case 40: /* ids ::= ID|STRING */ yytestcase(yyruleno==40);
104738
 
      case 41: /* nm ::= id */ yytestcase(yyruleno==41);
104739
 
      case 42: /* nm ::= STRING */ yytestcase(yyruleno==42);
104740
 
      case 43: /* nm ::= JOIN_KW */ yytestcase(yyruleno==43);
104741
 
      case 46: /* typetoken ::= typename */ yytestcase(yyruleno==46);
104742
 
      case 49: /* typename ::= ids */ yytestcase(yyruleno==49);
104743
 
      case 127: /* as ::= AS nm */ yytestcase(yyruleno==127);
104744
 
      case 128: /* as ::= ids */ yytestcase(yyruleno==128);
104745
 
      case 138: /* dbnm ::= DOT nm */ yytestcase(yyruleno==138);
104746
 
      case 147: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==147);
104747
 
      case 251: /* collate ::= COLLATE ids */ yytestcase(yyruleno==251);
104748
 
      case 260: /* nmnum ::= plus_num */ yytestcase(yyruleno==260);
104749
 
      case 261: /* nmnum ::= nm */ yytestcase(yyruleno==261);
104750
 
      case 262: /* nmnum ::= ON */ yytestcase(yyruleno==262);
104751
 
      case 263: /* nmnum ::= DELETE */ yytestcase(yyruleno==263);
104752
 
      case 264: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==264);
104753
 
      case 265: /* plus_num ::= plus_opt number */ yytestcase(yyruleno==265);
104754
 
      case 266: /* minus_num ::= MINUS number */ yytestcase(yyruleno==266);
104755
 
      case 267: /* number ::= INTEGER|FLOAT */ yytestcase(yyruleno==267);
104756
 
      case 285: /* trnm ::= nm */ yytestcase(yyruleno==285);
104757
 
{yygotominor.yy0 = yymsp[0].minor.yy0;}
104758
 
        break;
104759
 
      case 45: /* type ::= typetoken */
104760
 
{sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
104761
 
        break;
104762
 
      case 47: /* typetoken ::= typename LP signed RP */
104763
 
{
104764
 
  yygotominor.yy0.z = yymsp[-3].minor.yy0.z;
104765
 
  yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
104766
 
}
104767
 
        break;
104768
 
      case 48: /* typetoken ::= typename LP signed COMMA signed RP */
104769
 
{
104770
 
  yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
104771
 
  yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
104772
 
}
104773
 
        break;
104774
 
      case 50: /* typename ::= typename ids */
104775
 
{yygotominor.yy0.z=yymsp[-1].minor.yy0.z; yygotominor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
104776
 
        break;
104777
 
      case 57: /* ccons ::= DEFAULT term */
104778
 
      case 59: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==59);
104779
 
{sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy118);}
104780
 
        break;
104781
 
      case 58: /* ccons ::= DEFAULT LP expr RP */
104782
 
{sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy118);}
104783
 
        break;
104784
 
      case 60: /* ccons ::= DEFAULT MINUS term */
104785
 
{
104786
 
  ExprSpan v;
104787
 
  v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy118.pExpr, 0, 0);
104788
 
  v.zStart = yymsp[-1].minor.yy0.z;
104789
 
  v.zEnd = yymsp[0].minor.yy118.zEnd;
104790
 
  sqlite3AddDefaultValue(pParse,&v);
104791
 
}
104792
 
        break;
104793
 
      case 61: /* ccons ::= DEFAULT id */
104794
 
{
104795
 
  ExprSpan v;
104796
 
  spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
104797
 
  sqlite3AddDefaultValue(pParse,&v);
104798
 
}
104799
 
        break;
104800
 
      case 63: /* ccons ::= NOT NULL onconf */
104801
 
{sqlite3AddNotNull(pParse, yymsp[0].minor.yy4);}
104802
 
        break;
104803
 
      case 64: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
104804
 
{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy4,yymsp[0].minor.yy4,yymsp[-2].minor.yy4);}
104805
 
        break;
104806
 
      case 65: /* ccons ::= UNIQUE onconf */
104807
 
{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy4,0,0,0,0);}
104808
 
        break;
104809
 
      case 66: /* ccons ::= CHECK LP expr RP */
104810
 
{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy118.pExpr);}
104811
 
        break;
104812
 
      case 67: /* ccons ::= REFERENCES nm idxlist_opt refargs */
104813
 
{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy322,yymsp[0].minor.yy4);}
104814
 
        break;
104815
 
      case 68: /* ccons ::= defer_subclause */
104816
 
{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy4);}
104817
 
        break;
104818
 
      case 69: /* ccons ::= COLLATE ids */
104819
 
{sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
104820
 
        break;
104821
 
      case 72: /* refargs ::= */
104822
 
{ yygotominor.yy4 = OE_None*0x0101; /* EV: R-19803-45884 */}
104823
 
        break;
104824
 
      case 73: /* refargs ::= refargs refarg */
104825
 
{ yygotominor.yy4 = (yymsp[-1].minor.yy4 & ~yymsp[0].minor.yy215.mask) | yymsp[0].minor.yy215.value; }
104826
 
        break;
104827
 
      case 74: /* refarg ::= MATCH nm */
104828
 
      case 75: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==75);
104829
 
{ yygotominor.yy215.value = 0;     yygotominor.yy215.mask = 0x000000; }
104830
 
        break;
104831
 
      case 76: /* refarg ::= ON DELETE refact */
104832
 
{ yygotominor.yy215.value = yymsp[0].minor.yy4;     yygotominor.yy215.mask = 0x0000ff; }
104833
 
        break;
104834
 
      case 77: /* refarg ::= ON UPDATE refact */
104835
 
{ yygotominor.yy215.value = yymsp[0].minor.yy4<<8;  yygotominor.yy215.mask = 0x00ff00; }
104836
 
        break;
104837
 
      case 78: /* refact ::= SET NULL */
104838
 
{ yygotominor.yy4 = OE_SetNull;  /* EV: R-33326-45252 */}
104839
 
        break;
104840
 
      case 79: /* refact ::= SET DEFAULT */
104841
 
{ yygotominor.yy4 = OE_SetDflt;  /* EV: R-33326-45252 */}
104842
 
        break;
104843
 
      case 80: /* refact ::= CASCADE */
104844
 
{ yygotominor.yy4 = OE_Cascade;  /* EV: R-33326-45252 */}
104845
 
        break;
104846
 
      case 81: /* refact ::= RESTRICT */
104847
 
{ yygotominor.yy4 = OE_Restrict; /* EV: R-33326-45252 */}
104848
 
        break;
104849
 
      case 82: /* refact ::= NO ACTION */
104850
 
{ yygotominor.yy4 = OE_None;     /* EV: R-33326-45252 */}
104851
 
        break;
104852
 
      case 84: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
104853
 
      case 99: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==99);
104854
 
      case 101: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==101);
104855
 
      case 104: /* resolvetype ::= raisetype */ yytestcase(yyruleno==104);
104856
 
{yygotominor.yy4 = yymsp[0].minor.yy4;}
104857
 
        break;
104858
 
      case 88: /* conslist_opt ::= */
104859
 
{yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
104860
 
        break;
104861
 
      case 89: /* conslist_opt ::= COMMA conslist */
104862
 
{yygotominor.yy0 = yymsp[-1].minor.yy0;}
104863
 
        break;
104864
 
      case 94: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */
104865
 
{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy322,yymsp[0].minor.yy4,yymsp[-2].minor.yy4,0);}
104866
 
        break;
104867
 
      case 95: /* tcons ::= UNIQUE LP idxlist RP onconf */
104868
 
{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy322,yymsp[0].minor.yy4,0,0,0,0);}
104869
 
        break;
104870
 
      case 96: /* tcons ::= CHECK LP expr RP onconf */
104871
 
{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy118.pExpr);}
104872
 
        break;
104873
 
      case 97: /* tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt */
104874
 
{
104875
 
    sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy322, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322, yymsp[-1].minor.yy4);
104876
 
    sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy4);
104877
 
}
104878
 
        break;
104879
 
      case 100: /* onconf ::= */
104880
 
{yygotominor.yy4 = OE_Default;}
104881
 
        break;
104882
 
      case 102: /* orconf ::= */
104883
 
{yygotominor.yy210 = OE_Default;}
104884
 
        break;
104885
 
      case 103: /* orconf ::= OR resolvetype */
104886
 
{yygotominor.yy210 = (u8)yymsp[0].minor.yy4;}
104887
 
        break;
104888
 
      case 105: /* resolvetype ::= IGNORE */
104889
 
{yygotominor.yy4 = OE_Ignore;}
104890
 
        break;
104891
 
      case 106: /* resolvetype ::= REPLACE */
104892
 
{yygotominor.yy4 = OE_Replace;}
104893
 
        break;
104894
 
      case 107: /* cmd ::= DROP TABLE ifexists fullname */
104895
 
{
104896
 
  sqlite3DropTable(pParse, yymsp[0].minor.yy259, 0, yymsp[-1].minor.yy4);
104897
 
}
104898
 
        break;
104899
 
      case 110: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select */
104900
 
{
104901
 
  sqlite3CreateView(pParse, &yymsp[-7].minor.yy0, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, yymsp[0].minor.yy387, yymsp[-6].minor.yy4, yymsp[-4].minor.yy4);
104902
 
}
104903
 
        break;
104904
 
      case 111: /* cmd ::= DROP VIEW ifexists fullname */
104905
 
{
104906
 
  sqlite3DropTable(pParse, yymsp[0].minor.yy259, 1, yymsp[-1].minor.yy4);
104907
 
}
104908
 
        break;
104909
 
      case 112: /* cmd ::= select */
104910
 
{
104911
 
  SelectDest dest = {SRT_Output, 0, 0, 0, 0};
104912
 
  sqlite3Select(pParse, yymsp[0].minor.yy387, &dest);
104913
 
  sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
104914
 
}
104915
 
        break;
104916
 
      case 113: /* select ::= oneselect */
104917
 
{yygotominor.yy387 = yymsp[0].minor.yy387;}
104918
 
        break;
104919
 
      case 114: /* select ::= select multiselect_op oneselect */
104920
 
{
104921
 
  if( yymsp[0].minor.yy387 ){
104922
 
    yymsp[0].minor.yy387->op = (u8)yymsp[-1].minor.yy4;
104923
 
    yymsp[0].minor.yy387->pPrior = yymsp[-2].minor.yy387;
104924
 
  }else{
104925
 
    sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy387);
104926
 
  }
104927
 
  yygotominor.yy387 = yymsp[0].minor.yy387;
104928
 
}
104929
 
        break;
104930
 
      case 116: /* multiselect_op ::= UNION ALL */
104931
 
{yygotominor.yy4 = TK_ALL;}
104932
 
        break;
104933
 
      case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
104934
 
{
104935
 
  yygotominor.yy387 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy322,yymsp[-5].minor.yy259,yymsp[-4].minor.yy314,yymsp[-3].minor.yy322,yymsp[-2].minor.yy314,yymsp[-1].minor.yy322,yymsp[-7].minor.yy4,yymsp[0].minor.yy292.pLimit,yymsp[0].minor.yy292.pOffset);
104936
 
}
104937
 
        break;
104938
 
      case 122: /* sclp ::= selcollist COMMA */
104939
 
      case 247: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==247);
104940
 
{yygotominor.yy322 = yymsp[-1].minor.yy322;}
104941
 
        break;
104942
 
      case 123: /* sclp ::= */
104943
 
      case 151: /* orderby_opt ::= */ yytestcase(yyruleno==151);
104944
 
      case 159: /* groupby_opt ::= */ yytestcase(yyruleno==159);
104945
 
      case 240: /* exprlist ::= */ yytestcase(yyruleno==240);
104946
 
      case 246: /* idxlist_opt ::= */ yytestcase(yyruleno==246);
104947
 
{yygotominor.yy322 = 0;}
104948
 
        break;
104949
 
      case 124: /* selcollist ::= sclp expr as */
104950
 
{
104951
 
   yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy322, yymsp[-1].minor.yy118.pExpr);
104952
 
   if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[0].minor.yy0, 1);
104953
 
   sqlite3ExprListSetSpan(pParse,yygotominor.yy322,&yymsp[-1].minor.yy118);
104954
 
}
104955
 
        break;
104956
 
      case 125: /* selcollist ::= sclp STAR */
104957
 
{
104958
 
  Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
104959
 
  yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy322, p);
104960
 
}
104961
 
        break;
104962
 
      case 126: /* selcollist ::= sclp nm DOT STAR */
104963
 
{
104964
 
  Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &yymsp[0].minor.yy0);
104965
 
  Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
104966
 
  Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
104967
 
  yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322, pDot);
104968
 
}
104969
 
        break;
104970
 
      case 129: /* as ::= */
104971
 
{yygotominor.yy0.n = 0;}
104972
 
        break;
104973
 
      case 130: /* from ::= */
104974
 
{yygotominor.yy259 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy259));}
104975
 
        break;
104976
 
      case 131: /* from ::= FROM seltablist */
104977
 
{
104978
 
  yygotominor.yy259 = yymsp[0].minor.yy259;
104979
 
  sqlite3SrcListShiftJoinType(yygotominor.yy259);
104980
 
}
104981
 
        break;
104982
 
      case 132: /* stl_prefix ::= seltablist joinop */
104983
 
{
104984
 
   yygotominor.yy259 = yymsp[-1].minor.yy259;
104985
 
   if( ALWAYS(yygotominor.yy259 && yygotominor.yy259->nSrc>0) ) yygotominor.yy259->a[yygotominor.yy259->nSrc-1].jointype = (u8)yymsp[0].minor.yy4;
104986
 
}
104987
 
        break;
104988
 
      case 133: /* stl_prefix ::= */
104989
 
{yygotominor.yy259 = 0;}
104990
 
        break;
104991
 
      case 134: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
104992
 
{
104993
 
  yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor.yy314,yymsp[0].minor.yy384);
104994
 
  sqlite3SrcListIndexedBy(pParse, yygotominor.yy259, &yymsp[-2].minor.yy0);
104995
 
}
104996
 
        break;
104997
 
      case 135: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
104998
 
{
104999
 
    yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy387,yymsp[-1].minor.yy314,yymsp[0].minor.yy384);
105000
 
  }
105001
 
        break;
105002
 
      case 136: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
105003
 
{
105004
 
    if( yymsp[-6].minor.yy259==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy314==0 && yymsp[0].minor.yy384==0 ){
105005
 
      yygotominor.yy259 = yymsp[-4].minor.yy259;
105006
 
    }else{
105007
 
      Select *pSubquery;
105008
 
      sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy259);
105009
 
      pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy259,0,0,0,0,0,0,0);
105010
 
      yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy314,yymsp[0].minor.yy384);
105011
 
    }
105012
 
  }
105013
 
        break;
105014
 
      case 137: /* dbnm ::= */
105015
 
      case 146: /* indexed_opt ::= */ yytestcase(yyruleno==146);
105016
 
{yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
105017
 
        break;
105018
 
      case 139: /* fullname ::= nm dbnm */
105019
 
{yygotominor.yy259 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
105020
 
        break;
105021
 
      case 140: /* joinop ::= COMMA|JOIN */
105022
 
{ yygotominor.yy4 = JT_INNER; }
105023
 
        break;
105024
 
      case 141: /* joinop ::= JOIN_KW JOIN */
105025
 
{ yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); }
105026
 
        break;
105027
 
      case 142: /* joinop ::= JOIN_KW nm JOIN */
105028
 
{ yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); }
105029
 
        break;
105030
 
      case 143: /* joinop ::= JOIN_KW nm nm JOIN */
105031
 
{ yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
105032
 
        break;
105033
 
      case 144: /* on_opt ::= ON expr */
105034
 
      case 155: /* sortitem ::= expr */ yytestcase(yyruleno==155);
105035
 
      case 162: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==162);
105036
 
      case 169: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==169);
105037
 
      case 235: /* case_else ::= ELSE expr */ yytestcase(yyruleno==235);
105038
 
      case 237: /* case_operand ::= expr */ yytestcase(yyruleno==237);
105039
 
{yygotominor.yy314 = yymsp[0].minor.yy118.pExpr;}
105040
 
        break;
105041
 
      case 145: /* on_opt ::= */
105042
 
      case 161: /* having_opt ::= */ yytestcase(yyruleno==161);
105043
 
      case 168: /* where_opt ::= */ yytestcase(yyruleno==168);
105044
 
      case 236: /* case_else ::= */ yytestcase(yyruleno==236);
105045
 
      case 238: /* case_operand ::= */ yytestcase(yyruleno==238);
105046
 
{yygotominor.yy314 = 0;}
105047
 
        break;
105048
 
      case 148: /* indexed_opt ::= NOT INDEXED */
105049
 
{yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
105050
 
        break;
105051
 
      case 149: /* using_opt ::= USING LP inscollist RP */
105052
 
      case 181: /* inscollist_opt ::= LP inscollist RP */ yytestcase(yyruleno==181);
105053
 
{yygotominor.yy384 = yymsp[-1].minor.yy384;}
105054
 
        break;
105055
 
      case 150: /* using_opt ::= */
105056
 
      case 180: /* inscollist_opt ::= */ yytestcase(yyruleno==180);
105057
 
{yygotominor.yy384 = 0;}
105058
 
        break;
105059
 
      case 152: /* orderby_opt ::= ORDER BY sortlist */
105060
 
      case 160: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==160);
105061
 
      case 239: /* exprlist ::= nexprlist */ yytestcase(yyruleno==239);
105062
 
{yygotominor.yy322 = yymsp[0].minor.yy322;}
105063
 
        break;
105064
 
      case 153: /* sortlist ::= sortlist COMMA sortitem sortorder */
105065
 
{
105066
 
  yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322,yymsp[-1].minor.yy314);
105067
 
  if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4;
105068
 
}
105069
 
        break;
105070
 
      case 154: /* sortlist ::= sortitem sortorder */
105071
 
{
105072
 
  yygotominor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy314);
105073
 
  if( yygotominor.yy322 && ALWAYS(yygotominor.yy322->a) ) yygotominor.yy322->a[0].sortOrder = (u8)yymsp[0].minor.yy4;
105074
 
}
105075
 
        break;
105076
 
      case 156: /* sortorder ::= ASC */
105077
 
      case 158: /* sortorder ::= */ yytestcase(yyruleno==158);
105078
 
{yygotominor.yy4 = SQLITE_SO_ASC;}
105079
 
        break;
105080
 
      case 157: /* sortorder ::= DESC */
105081
 
{yygotominor.yy4 = SQLITE_SO_DESC;}
105082
 
        break;
105083
 
      case 163: /* limit_opt ::= */
105084
 
{yygotominor.yy292.pLimit = 0; yygotominor.yy292.pOffset = 0;}
105085
 
        break;
105086
 
      case 164: /* limit_opt ::= LIMIT expr */
105087
 
{yygotominor.yy292.pLimit = yymsp[0].minor.yy118.pExpr; yygotominor.yy292.pOffset = 0;}
105088
 
        break;
105089
 
      case 165: /* limit_opt ::= LIMIT expr OFFSET expr */
105090
 
{yygotominor.yy292.pLimit = yymsp[-2].minor.yy118.pExpr; yygotominor.yy292.pOffset = yymsp[0].minor.yy118.pExpr;}
105091
 
        break;
105092
 
      case 166: /* limit_opt ::= LIMIT expr COMMA expr */
105093
 
{yygotominor.yy292.pOffset = yymsp[-2].minor.yy118.pExpr; yygotominor.yy292.pLimit = yymsp[0].minor.yy118.pExpr;}
105094
 
        break;
105095
 
      case 167: /* cmd ::= DELETE FROM fullname indexed_opt where_opt */
105096
 
{
105097
 
  sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy259, &yymsp[-1].minor.yy0);
105098
 
  sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy259,yymsp[0].minor.yy314);
105099
 
}
105100
 
        break;
105101
 
      case 170: /* cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt */
105102
 
{
105103
 
  sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy259, &yymsp[-3].minor.yy0);
105104
 
  sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy322,"set list"); 
105105
 
  sqlite3Update(pParse,yymsp[-4].minor.yy259,yymsp[-1].minor.yy322,yymsp[0].minor.yy314,yymsp[-5].minor.yy210);
105106
 
}
105107
 
        break;
105108
 
      case 171: /* setlist ::= setlist COMMA nm EQ expr */
105109
 
{
105110
 
  yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[0].minor.yy118.pExpr);
105111
 
  sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
105112
 
}
105113
 
        break;
105114
 
      case 172: /* setlist ::= nm EQ expr */
105115
 
{
105116
 
  yygotominor.yy322 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy118.pExpr);
105117
 
  sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
105118
 
}
105119
 
        break;
105120
 
      case 173: /* cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP */
105121
 
{sqlite3Insert(pParse, yymsp[-5].minor.yy259, yymsp[-1].minor.yy322, 0, yymsp[-4].minor.yy384, yymsp[-7].minor.yy210);}
105122
 
        break;
105123
 
      case 174: /* cmd ::= insert_cmd INTO fullname inscollist_opt select */
105124
 
{sqlite3Insert(pParse, yymsp[-2].minor.yy259, 0, yymsp[0].minor.yy387, yymsp[-1].minor.yy384, yymsp[-4].minor.yy210);}
105125
 
        break;
105126
 
      case 175: /* cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
105127
 
{sqlite3Insert(pParse, yymsp[-3].minor.yy259, 0, 0, yymsp[-2].minor.yy384, yymsp[-5].minor.yy210);}
105128
 
        break;
105129
 
      case 176: /* insert_cmd ::= INSERT orconf */
105130
 
{yygotominor.yy210 = yymsp[0].minor.yy210;}
105131
 
        break;
105132
 
      case 177: /* insert_cmd ::= REPLACE */
105133
 
{yygotominor.yy210 = OE_Replace;}
105134
 
        break;
105135
 
      case 178: /* itemlist ::= itemlist COMMA expr */
105136
 
      case 241: /* nexprlist ::= nexprlist COMMA expr */ yytestcase(yyruleno==241);
105137
 
{yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy322,yymsp[0].minor.yy118.pExpr);}
105138
 
        break;
105139
 
      case 179: /* itemlist ::= expr */
105140
 
      case 242: /* nexprlist ::= expr */ yytestcase(yyruleno==242);
105141
 
{yygotominor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy118.pExpr);}
105142
 
        break;
105143
 
      case 182: /* inscollist ::= inscollist COMMA nm */
105144
 
{yygotominor.yy384 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy384,&yymsp[0].minor.yy0);}
105145
 
        break;
105146
 
      case 183: /* inscollist ::= nm */
105147
 
{yygotominor.yy384 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
105148
 
        break;
105149
 
      case 184: /* expr ::= term */
105150
 
{yygotominor.yy118 = yymsp[0].minor.yy118;}
105151
 
        break;
105152
 
      case 185: /* expr ::= LP expr RP */
105153
 
{yygotominor.yy118.pExpr = yymsp[-1].minor.yy118.pExpr; spanSet(&yygotominor.yy118,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
105154
 
        break;
105155
 
      case 186: /* term ::= NULL */
105156
 
      case 191: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==191);
105157
 
      case 192: /* term ::= STRING */ yytestcase(yyruleno==192);
105158
 
{spanExpr(&yygotominor.yy118, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
105159
 
        break;
105160
 
      case 187: /* expr ::= id */
105161
 
      case 188: /* expr ::= JOIN_KW */ yytestcase(yyruleno==188);
105162
 
{spanExpr(&yygotominor.yy118, pParse, TK_ID, &yymsp[0].minor.yy0);}
105163
 
        break;
105164
 
      case 189: /* expr ::= nm DOT nm */
105165
 
{
105166
 
  Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
105167
 
  Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
105168
 
  yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
105169
 
  spanSet(&yygotominor.yy118,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
105170
 
}
105171
 
        break;
105172
 
      case 190: /* expr ::= nm DOT nm DOT nm */
105173
 
{
105174
 
  Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0);
105175
 
  Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
105176
 
  Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
105177
 
  Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
105178
 
  yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
105179
 
  spanSet(&yygotominor.yy118,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
105180
 
}
105181
 
        break;
105182
 
      case 193: /* expr ::= REGISTER */
105183
 
{
105184
 
  /* When doing a nested parse, one can include terms in an expression
105185
 
  ** that look like this:   #1 #2 ...  These terms refer to registers
105186
 
  ** in the virtual machine.  #N is the N-th register. */
105187
 
  if( pParse->nested==0 ){
105188
 
    sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &yymsp[0].minor.yy0);
105189
 
    yygotominor.yy118.pExpr = 0;
105190
 
  }else{
105191
 
    yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &yymsp[0].minor.yy0);
105192
 
    if( yygotominor.yy118.pExpr ) sqlite3GetInt32(&yymsp[0].minor.yy0.z[1], &yygotominor.yy118.pExpr->iTable);
105193
 
  }
105194
 
  spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
105195
 
}
105196
 
        break;
105197
 
      case 194: /* expr ::= VARIABLE */
105198
 
{
105199
 
  spanExpr(&yygotominor.yy118, pParse, TK_VARIABLE, &yymsp[0].minor.yy0);
105200
 
  sqlite3ExprAssignVarNumber(pParse, yygotominor.yy118.pExpr);
105201
 
  spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
105202
 
}
105203
 
        break;
105204
 
      case 195: /* expr ::= expr COLLATE ids */
105205
 
{
105206
 
  yygotominor.yy118.pExpr = sqlite3ExprSetCollByToken(pParse, yymsp[-2].minor.yy118.pExpr, &yymsp[0].minor.yy0);
105207
 
  yygotominor.yy118.zStart = yymsp[-2].minor.yy118.zStart;
105208
 
  yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
105209
 
}
105210
 
        break;
105211
 
      case 196: /* expr ::= CAST LP expr AS typetoken RP */
105212
 
{
105213
 
  yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy118.pExpr, 0, &yymsp[-1].minor.yy0);
105214
 
  spanSet(&yygotominor.yy118,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
105215
 
}
105216
 
        break;
105217
 
      case 197: /* expr ::= ID LP distinct exprlist RP */
105218
 
{
105219
 
  if( yymsp[-1].minor.yy322 && yymsp[-1].minor.yy322->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
105220
 
    sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
105221
 
  }
105222
 
  yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy322, &yymsp[-4].minor.yy0);
105223
 
  spanSet(&yygotominor.yy118,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
105224
 
  if( yymsp[-2].minor.yy4 && yygotominor.yy118.pExpr ){
105225
 
    yygotominor.yy118.pExpr->flags |= EP_Distinct;
105226
 
  }
105227
 
}
105228
 
        break;
105229
 
      case 198: /* expr ::= ID LP STAR RP */
105230
 
{
105231
 
  yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
105232
 
  spanSet(&yygotominor.yy118,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
105233
 
}
105234
 
        break;
105235
 
      case 199: /* term ::= CTIME_KW */
105236
 
{
105237
 
  /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
105238
 
  ** treated as functions that return constants */
105239
 
  yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, 0,&yymsp[0].minor.yy0);
105240
 
  if( yygotominor.yy118.pExpr ){
105241
 
    yygotominor.yy118.pExpr->op = TK_CONST_FUNC;  
105242
 
  }
105243
 
  spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
105244
 
}
105245
 
        break;
105246
 
      case 200: /* expr ::= expr AND expr */
105247
 
      case 201: /* expr ::= expr OR expr */ yytestcase(yyruleno==201);
105248
 
      case 202: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==202);
105249
 
      case 203: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==203);
105250
 
      case 204: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==204);
105251
 
      case 205: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==205);
105252
 
      case 206: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==206);
105253
 
      case 207: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==207);
105254
 
{spanBinaryExpr(&yygotominor.yy118,pParse,yymsp[-1].major,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy118);}
105255
 
        break;
105256
 
      case 208: /* likeop ::= LIKE_KW */
105257
 
      case 210: /* likeop ::= MATCH */ yytestcase(yyruleno==210);
105258
 
{yygotominor.yy342.eOperator = yymsp[0].minor.yy0; yygotominor.yy342.not = 0;}
105259
 
        break;
105260
 
      case 209: /* likeop ::= NOT LIKE_KW */
105261
 
      case 211: /* likeop ::= NOT MATCH */ yytestcase(yyruleno==211);
105262
 
{yygotominor.yy342.eOperator = yymsp[0].minor.yy0; yygotominor.yy342.not = 1;}
105263
 
        break;
105264
 
      case 212: /* expr ::= expr likeop expr */
105265
 
{
105266
 
  ExprList *pList;
105267
 
  pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy118.pExpr);
105268
 
  pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy118.pExpr);
105269
 
  yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy342.eOperator);
105270
 
  if( yymsp[-1].minor.yy342.not ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
105271
 
  yygotominor.yy118.zStart = yymsp[-2].minor.yy118.zStart;
105272
 
  yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
105273
 
  if( yygotominor.yy118.pExpr ) yygotominor.yy118.pExpr->flags |= EP_InfixFunc;
105274
 
}
105275
 
        break;
105276
 
      case 213: /* expr ::= expr likeop expr ESCAPE expr */
105277
 
{
105278
 
  ExprList *pList;
105279
 
  pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
105280
 
  pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy118.pExpr);
105281
 
  pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy118.pExpr);
105282
 
  yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy342.eOperator);
105283
 
  if( yymsp[-3].minor.yy342.not ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
105284
 
  yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
105285
 
  yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
105286
 
  if( yygotominor.yy118.pExpr ) yygotominor.yy118.pExpr->flags |= EP_InfixFunc;
105287
 
}
105288
 
        break;
105289
 
      case 214: /* expr ::= expr ISNULL|NOTNULL */
105290
 
{spanUnaryPostfix(&yygotominor.yy118,pParse,yymsp[0].major,&yymsp[-1].minor.yy118,&yymsp[0].minor.yy0);}
105291
 
        break;
105292
 
      case 215: /* expr ::= expr NOT NULL */
105293
 
{spanUnaryPostfix(&yygotominor.yy118,pParse,TK_NOTNULL,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy0);}
105294
 
        break;
105295
 
      case 216: /* expr ::= expr IS expr */
105296
 
{
105297
 
  spanBinaryExpr(&yygotominor.yy118,pParse,TK_IS,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy118);
105298
 
  binaryToUnaryIfNull(pParse, yymsp[0].minor.yy118.pExpr, yygotominor.yy118.pExpr, TK_ISNULL);
105299
 
}
105300
 
        break;
105301
 
      case 217: /* expr ::= expr IS NOT expr */
105302
 
{
105303
 
  spanBinaryExpr(&yygotominor.yy118,pParse,TK_ISNOT,&yymsp[-3].minor.yy118,&yymsp[0].minor.yy118);
105304
 
  binaryToUnaryIfNull(pParse, yymsp[0].minor.yy118.pExpr, yygotominor.yy118.pExpr, TK_NOTNULL);
105305
 
}
105306
 
        break;
105307
 
      case 218: /* expr ::= NOT expr */
105308
 
      case 219: /* expr ::= BITNOT expr */ yytestcase(yyruleno==219);
105309
 
{spanUnaryPrefix(&yygotominor.yy118,pParse,yymsp[-1].major,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);}
105310
 
        break;
105311
 
      case 220: /* expr ::= MINUS expr */
105312
 
{spanUnaryPrefix(&yygotominor.yy118,pParse,TK_UMINUS,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);}
105313
 
        break;
105314
 
      case 221: /* expr ::= PLUS expr */
105315
 
{spanUnaryPrefix(&yygotominor.yy118,pParse,TK_UPLUS,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);}
105316
 
        break;
105317
 
      case 224: /* expr ::= expr between_op expr AND expr */
105318
 
{
105319
 
  ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
105320
 
  pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy118.pExpr);
105321
 
  yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy118.pExpr, 0, 0);
105322
 
  if( yygotominor.yy118.pExpr ){
105323
 
    yygotominor.yy118.pExpr->x.pList = pList;
105324
 
  }else{
105325
 
    sqlite3ExprListDelete(pParse->db, pList);
105326
 
  } 
105327
 
  if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
105328
 
  yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
105329
 
  yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
105330
 
}
105331
 
        break;
105332
 
      case 227: /* expr ::= expr in_op LP exprlist RP */
105333
 
{
105334
 
    if( yymsp[-1].minor.yy322==0 ){
105335
 
      /* Expressions of the form
105336
 
      **
105337
 
      **      expr1 IN ()
105338
 
      **      expr1 NOT IN ()
105339
 
      **
105340
 
      ** simplify to constants 0 (false) and 1 (true), respectively,
105341
 
      ** regardless of the value of expr1.
105342
 
      */
105343
 
      yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[yymsp[-3].minor.yy4]);
105344
 
      sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy118.pExpr);
105345
 
    }else{
105346
 
      yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy118.pExpr, 0, 0);
105347
 
      if( yygotominor.yy118.pExpr ){
105348
 
        yygotominor.yy118.pExpr->x.pList = yymsp[-1].minor.yy322;
105349
 
        sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
105350
 
      }else{
105351
 
        sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
105352
 
      }
105353
 
      if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
105354
 
    }
105355
 
    yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
105356
 
    yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
105357
 
  }
105358
 
        break;
105359
 
      case 228: /* expr ::= LP select RP */
105360
 
{
105361
 
    yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
105362
 
    if( yygotominor.yy118.pExpr ){
105363
 
      yygotominor.yy118.pExpr->x.pSelect = yymsp[-1].minor.yy387;
105364
 
      ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect);
105365
 
      sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
105366
 
    }else{
105367
 
      sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
105368
 
    }
105369
 
    yygotominor.yy118.zStart = yymsp[-2].minor.yy0.z;
105370
 
    yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
105371
 
  }
105372
 
        break;
105373
 
      case 229: /* expr ::= expr in_op LP select RP */
105374
 
{
105375
 
    yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy118.pExpr, 0, 0);
105376
 
    if( yygotominor.yy118.pExpr ){
105377
 
      yygotominor.yy118.pExpr->x.pSelect = yymsp[-1].minor.yy387;
105378
 
      ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect);
105379
 
      sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
105380
 
    }else{
105381
 
      sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
105382
 
    }
105383
 
    if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
105384
 
    yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
105385
 
    yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
105386
 
  }
105387
 
        break;
105388
 
      case 230: /* expr ::= expr in_op nm dbnm */
105389
 
{
105390
 
    SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);
105391
 
    yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy118.pExpr, 0, 0);
105392
 
    if( yygotominor.yy118.pExpr ){
105393
 
      yygotominor.yy118.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
105394
 
      ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect);
105395
 
      sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
105396
 
    }else{
105397
 
      sqlite3SrcListDelete(pParse->db, pSrc);
105398
 
    }
105399
 
    if( yymsp[-2].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
105400
 
    yygotominor.yy118.zStart = yymsp[-3].minor.yy118.zStart;
105401
 
    yygotominor.yy118.zEnd = yymsp[0].minor.yy0.z ? &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] : &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n];
105402
 
  }
105403
 
        break;
105404
 
      case 231: /* expr ::= EXISTS LP select RP */
105405
 
{
105406
 
    Expr *p = yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
105407
 
    if( p ){
105408
 
      p->x.pSelect = yymsp[-1].minor.yy387;
105409
 
      ExprSetProperty(p, EP_xIsSelect);
105410
 
      sqlite3ExprSetHeight(pParse, p);
105411
 
    }else{
105412
 
      sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
105413
 
    }
105414
 
    yygotominor.yy118.zStart = yymsp[-3].minor.yy0.z;
105415
 
    yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
105416
 
  }
105417
 
        break;
105418
 
      case 232: /* expr ::= CASE case_operand case_exprlist case_else END */
105419
 
{
105420
 
  yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy314, yymsp[-1].minor.yy314, 0);
105421
 
  if( yygotominor.yy118.pExpr ){
105422
 
    yygotominor.yy118.pExpr->x.pList = yymsp[-2].minor.yy322;
105423
 
    sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
105424
 
  }else{
105425
 
    sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy322);
105426
 
  }
105427
 
  yygotominor.yy118.zStart = yymsp[-4].minor.yy0.z;
105428
 
  yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
105429
 
}
105430
 
        break;
105431
 
      case 233: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
105432
 
{
105433
 
  yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[-2].minor.yy118.pExpr);
105434
 
  yygotominor.yy322 = sqlite3ExprListAppend(pParse,yygotominor.yy322, yymsp[0].minor.yy118.pExpr);
105435
 
}
105436
 
        break;
105437
 
      case 234: /* case_exprlist ::= WHEN expr THEN expr */
105438
 
{
105439
 
  yygotominor.yy322 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
105440
 
  yygotominor.yy322 = sqlite3ExprListAppend(pParse,yygotominor.yy322, yymsp[0].minor.yy118.pExpr);
105441
 
}
105442
 
        break;
105443
 
      case 243: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP */
105444
 
{
105445
 
  sqlite3CreateIndex(pParse, &yymsp[-6].minor.yy0, &yymsp[-5].minor.yy0, 
105446
 
                     sqlite3SrcListAppend(pParse->db,0,&yymsp[-3].minor.yy0,0), yymsp[-1].minor.yy322, yymsp[-9].minor.yy4,
105447
 
                      &yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy4);
105448
 
}
105449
 
        break;
105450
 
      case 244: /* uniqueflag ::= UNIQUE */
105451
 
      case 298: /* raisetype ::= ABORT */ yytestcase(yyruleno==298);
105452
 
{yygotominor.yy4 = OE_Abort;}
105453
 
        break;
105454
 
      case 245: /* uniqueflag ::= */
105455
 
{yygotominor.yy4 = OE_None;}
105456
 
        break;
105457
 
      case 248: /* idxlist ::= idxlist COMMA nm collate sortorder */
105458
 
{
105459
 
  Expr *p = 0;
105460
 
  if( yymsp[-1].minor.yy0.n>0 ){
105461
 
    p = sqlite3Expr(pParse->db, TK_COLUMN, 0);
105462
 
    sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0);
105463
 
  }
105464
 
  yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, p);
105465
 
  sqlite3ExprListSetName(pParse,yygotominor.yy322,&yymsp[-2].minor.yy0,1);
105466
 
  sqlite3ExprListCheckLength(pParse, yygotominor.yy322, "index");
105467
 
  if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4;
105468
 
}
105469
 
        break;
105470
 
      case 249: /* idxlist ::= nm collate sortorder */
105471
 
{
105472
 
  Expr *p = 0;
105473
 
  if( yymsp[-1].minor.yy0.n>0 ){
105474
 
    p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
105475
 
    sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0);
105476
 
  }
105477
 
  yygotominor.yy322 = sqlite3ExprListAppend(pParse,0, p);
105478
 
  sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
105479
 
  sqlite3ExprListCheckLength(pParse, yygotominor.yy322, "index");
105480
 
  if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4;
105481
 
}
105482
 
        break;
105483
 
      case 250: /* collate ::= */
105484
 
{yygotominor.yy0.z = 0; yygotominor.yy0.n = 0;}
105485
 
        break;
105486
 
      case 252: /* cmd ::= DROP INDEX ifexists fullname */
105487
 
{sqlite3DropIndex(pParse, yymsp[0].minor.yy259, yymsp[-1].minor.yy4);}
105488
 
        break;
105489
 
      case 253: /* cmd ::= VACUUM */
105490
 
      case 254: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==254);
105491
 
{sqlite3Vacuum(pParse);}
105492
 
        break;
105493
 
      case 255: /* cmd ::= PRAGMA nm dbnm */
105494
 
{sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
105495
 
        break;
105496
 
      case 256: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
105497
 
{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
105498
 
        break;
105499
 
      case 257: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
105500
 
{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
105501
 
        break;
105502
 
      case 258: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
105503
 
{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
105504
 
        break;
105505
 
      case 259: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
105506
 
{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
105507
 
        break;
105508
 
      case 270: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
105509
 
{
105510
 
  Token all;
105511
 
  all.z = yymsp[-3].minor.yy0.z;
105512
 
  all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
105513
 
  sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy203, &all);
105514
 
}
105515
 
        break;
105516
 
      case 271: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
105517
 
{
105518
 
  sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy4, yymsp[-4].minor.yy90.a, yymsp[-4].minor.yy90.b, yymsp[-2].minor.yy259, yymsp[0].minor.yy314, yymsp[-10].minor.yy4, yymsp[-8].minor.yy4);
105519
 
  yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
105520
 
}
105521
 
        break;
105522
 
      case 272: /* trigger_time ::= BEFORE */
105523
 
      case 275: /* trigger_time ::= */ yytestcase(yyruleno==275);
105524
 
{ yygotominor.yy4 = TK_BEFORE; }
105525
 
        break;
105526
 
      case 273: /* trigger_time ::= AFTER */
105527
 
{ yygotominor.yy4 = TK_AFTER;  }
105528
 
        break;
105529
 
      case 274: /* trigger_time ::= INSTEAD OF */
105530
 
{ yygotominor.yy4 = TK_INSTEAD;}
105531
 
        break;
105532
 
      case 276: /* trigger_event ::= DELETE|INSERT */
105533
 
      case 277: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==277);
105534
 
{yygotominor.yy90.a = yymsp[0].major; yygotominor.yy90.b = 0;}
105535
 
        break;
105536
 
      case 278: /* trigger_event ::= UPDATE OF inscollist */
105537
 
{yygotominor.yy90.a = TK_UPDATE; yygotominor.yy90.b = yymsp[0].minor.yy384;}
105538
 
        break;
105539
 
      case 281: /* when_clause ::= */
105540
 
      case 303: /* key_opt ::= */ yytestcase(yyruleno==303);
105541
 
{ yygotominor.yy314 = 0; }
105542
 
        break;
105543
 
      case 282: /* when_clause ::= WHEN expr */
105544
 
      case 304: /* key_opt ::= KEY expr */ yytestcase(yyruleno==304);
105545
 
{ yygotominor.yy314 = yymsp[0].minor.yy118.pExpr; }
105546
 
        break;
105547
 
      case 283: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
105548
 
{
105549
 
  assert( yymsp[-2].minor.yy203!=0 );
105550
 
  yymsp[-2].minor.yy203->pLast->pNext = yymsp[-1].minor.yy203;
105551
 
  yymsp[-2].minor.yy203->pLast = yymsp[-1].minor.yy203;
105552
 
  yygotominor.yy203 = yymsp[-2].minor.yy203;
105553
 
}
105554
 
        break;
105555
 
      case 284: /* trigger_cmd_list ::= trigger_cmd SEMI */
105556
 
105557
 
  assert( yymsp[-1].minor.yy203!=0 );
105558
 
  yymsp[-1].minor.yy203->pLast = yymsp[-1].minor.yy203;
105559
 
  yygotominor.yy203 = yymsp[-1].minor.yy203;
105560
 
}
105561
 
        break;
105562
 
      case 286: /* trnm ::= nm DOT nm */
105563
 
{
105564
 
  yygotominor.yy0 = yymsp[0].minor.yy0;
105565
 
  sqlite3ErrorMsg(pParse, 
105566
 
        "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
105567
 
        "statements within triggers");
105568
 
}
105569
 
        break;
105570
 
      case 288: /* tridxby ::= INDEXED BY nm */
105571
 
{
105572
 
  sqlite3ErrorMsg(pParse,
105573
 
        "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
105574
 
        "within triggers");
105575
 
}
105576
 
        break;
105577
 
      case 289: /* tridxby ::= NOT INDEXED */
105578
 
{
105579
 
  sqlite3ErrorMsg(pParse,
105580
 
        "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
105581
 
        "within triggers");
105582
 
}
105583
 
        break;
105584
 
      case 290: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */
105585
 
{ yygotominor.yy203 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy322, yymsp[0].minor.yy314, yymsp[-5].minor.yy210); }
105586
 
        break;
105587
 
      case 291: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt VALUES LP itemlist RP */
105588
 
{yygotominor.yy203 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy384, yymsp[-1].minor.yy322, 0, yymsp[-7].minor.yy210);}
105589
 
        break;
105590
 
      case 292: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select */
105591
 
{yygotominor.yy203 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy384, 0, yymsp[0].minor.yy387, yymsp[-4].minor.yy210);}
105592
 
        break;
105593
 
      case 293: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
105594
 
{yygotominor.yy203 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy314);}
105595
 
        break;
105596
 
      case 294: /* trigger_cmd ::= select */
105597
 
{yygotominor.yy203 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy387); }
105598
 
        break;
105599
 
      case 295: /* expr ::= RAISE LP IGNORE RP */
105600
 
{
105601
 
  yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); 
105602
 
  if( yygotominor.yy118.pExpr ){
105603
 
    yygotominor.yy118.pExpr->affinity = OE_Ignore;
105604
 
  }
105605
 
  yygotominor.yy118.zStart = yymsp[-3].minor.yy0.z;
105606
 
  yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
105607
 
}
105608
 
        break;
105609
 
      case 296: /* expr ::= RAISE LP raisetype COMMA nm RP */
105610
 
{
105611
 
  yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy0); 
105612
 
  if( yygotominor.yy118.pExpr ) {
105613
 
    yygotominor.yy118.pExpr->affinity = (char)yymsp[-3].minor.yy4;
105614
 
  }
105615
 
  yygotominor.yy118.zStart = yymsp[-5].minor.yy0.z;
105616
 
  yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
105617
 
}
105618
 
        break;
105619
 
      case 297: /* raisetype ::= ROLLBACK */
105620
 
{yygotominor.yy4 = OE_Rollback;}
105621
 
        break;
105622
 
      case 299: /* raisetype ::= FAIL */
105623
 
{yygotominor.yy4 = OE_Fail;}
105624
 
        break;
105625
 
      case 300: /* cmd ::= DROP TRIGGER ifexists fullname */
105626
 
{
105627
 
  sqlite3DropTrigger(pParse,yymsp[0].minor.yy259,yymsp[-1].minor.yy4);
105628
 
}
105629
 
        break;
105630
 
      case 301: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
105631
 
{
105632
 
  sqlite3Attach(pParse, yymsp[-3].minor.yy118.pExpr, yymsp[-1].minor.yy118.pExpr, yymsp[0].minor.yy314);
105633
 
}
105634
 
        break;
105635
 
      case 302: /* cmd ::= DETACH database_kw_opt expr */
105636
 
{
105637
 
  sqlite3Detach(pParse, yymsp[0].minor.yy118.pExpr);
105638
 
}
105639
 
        break;
105640
 
      case 307: /* cmd ::= REINDEX */
105641
 
{sqlite3Reindex(pParse, 0, 0);}
105642
 
        break;
105643
 
      case 308: /* cmd ::= REINDEX nm dbnm */
105644
 
{sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
105645
 
        break;
105646
 
      case 309: /* cmd ::= ANALYZE */
105647
 
{sqlite3Analyze(pParse, 0, 0);}
105648
 
        break;
105649
 
      case 310: /* cmd ::= ANALYZE nm dbnm */
105650
 
{sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
105651
 
        break;
105652
 
      case 311: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
105653
 
{
105654
 
  sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy259,&yymsp[0].minor.yy0);
105655
 
}
105656
 
        break;
105657
 
      case 312: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column */
105658
 
{
105659
 
  sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
105660
 
}
105661
 
        break;
105662
 
      case 313: /* add_column_fullname ::= fullname */
105663
 
{
105664
 
  pParse->db->lookaside.bEnabled = 0;
105665
 
  sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy259);
105666
 
}
105667
 
        break;
105668
 
      case 316: /* cmd ::= create_vtab */
105669
 
{sqlite3VtabFinishParse(pParse,0);}
105670
 
        break;
105671
 
      case 317: /* cmd ::= create_vtab LP vtabarglist RP */
105672
 
{sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
105673
 
        break;
105674
 
      case 318: /* create_vtab ::= createkw VIRTUAL TABLE nm dbnm USING nm */
105675
 
{
105676
 
    sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
105677
 
}
105678
 
        break;
105679
 
      case 321: /* vtabarg ::= */
105680
 
{sqlite3VtabArgInit(pParse);}
105681
 
        break;
105682
 
      case 323: /* vtabargtoken ::= ANY */
105683
 
      case 324: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==324);
105684
 
      case 325: /* lp ::= LP */ yytestcase(yyruleno==325);
105685
 
{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
105686
 
        break;
105687
 
      default:
105688
 
      /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
105689
 
      /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
105690
 
      /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
105691
 
      /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
105692
 
      /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
105693
 
      /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
105694
 
      /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
105695
 
      /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
105696
 
      /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
105697
 
      /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
105698
 
      /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
105699
 
      /* (34) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==34);
105700
 
      /* (35) columnlist ::= column */ yytestcase(yyruleno==35);
105701
 
      /* (44) type ::= */ yytestcase(yyruleno==44);
105702
 
      /* (51) signed ::= plus_num */ yytestcase(yyruleno==51);
105703
 
      /* (52) signed ::= minus_num */ yytestcase(yyruleno==52);
105704
 
      /* (53) carglist ::= carglist carg */ yytestcase(yyruleno==53);
105705
 
      /* (54) carglist ::= */ yytestcase(yyruleno==54);
105706
 
      /* (55) carg ::= CONSTRAINT nm ccons */ yytestcase(yyruleno==55);
105707
 
      /* (56) carg ::= ccons */ yytestcase(yyruleno==56);
105708
 
      /* (62) ccons ::= NULL onconf */ yytestcase(yyruleno==62);
105709
 
      /* (90) conslist ::= conslist COMMA tcons */ yytestcase(yyruleno==90);
105710
 
      /* (91) conslist ::= conslist tcons */ yytestcase(yyruleno==91);
105711
 
      /* (92) conslist ::= tcons */ yytestcase(yyruleno==92);
105712
 
      /* (93) tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==93);
105713
 
      /* (268) plus_opt ::= PLUS */ yytestcase(yyruleno==268);
105714
 
      /* (269) plus_opt ::= */ yytestcase(yyruleno==269);
105715
 
      /* (279) foreach_clause ::= */ yytestcase(yyruleno==279);
105716
 
      /* (280) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==280);
105717
 
      /* (287) tridxby ::= */ yytestcase(yyruleno==287);
105718
 
      /* (305) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==305);
105719
 
      /* (306) database_kw_opt ::= */ yytestcase(yyruleno==306);
105720
 
      /* (314) kwcolumn_opt ::= */ yytestcase(yyruleno==314);
105721
 
      /* (315) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==315);
105722
 
      /* (319) vtabarglist ::= vtabarg */ yytestcase(yyruleno==319);
105723
 
      /* (320) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==320);
105724
 
      /* (322) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==322);
105725
 
      /* (326) anylist ::= */ yytestcase(yyruleno==326);
105726
 
      /* (327) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==327);
105727
 
      /* (328) anylist ::= anylist ANY */ yytestcase(yyruleno==328);
105728
 
        break;
105729
 
  };
105730
 
  yygoto = yyRuleInfo[yyruleno].lhs;
105731
 
  yysize = yyRuleInfo[yyruleno].nrhs;
105732
 
  yypParser->yyidx -= yysize;
105733
 
  yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
105734
 
  if( yyact < YYNSTATE ){
105735
 
#ifdef NDEBUG
105736
 
    /* If we are not debugging and the reduce action popped at least
105737
 
    ** one element off the stack, then we can push the new element back
105738
 
    ** onto the stack here, and skip the stack overflow test in yy_shift().
105739
 
    ** That gives a significant speed improvement. */
105740
 
    if( yysize ){
105741
 
      yypParser->yyidx++;
105742
 
      yymsp -= yysize-1;
105743
 
      yymsp->stateno = (YYACTIONTYPE)yyact;
105744
 
      yymsp->major = (YYCODETYPE)yygoto;
105745
 
      yymsp->minor = yygotominor;
105746
 
    }else
105747
 
#endif
105748
 
    {
105749
 
      yy_shift(yypParser,yyact,yygoto,&yygotominor);
105750
 
    }
105751
 
  }else{
105752
 
    assert( yyact == YYNSTATE + YYNRULE + 1 );
105753
 
    yy_accept(yypParser);
105754
 
  }
105755
 
}
105756
 
 
105757
 
/*
105758
 
** The following code executes when the parse fails
105759
 
*/
105760
 
#ifndef YYNOERRORRECOVERY
105761
 
static void yy_parse_failed(
105762
 
  yyParser *yypParser           /* The parser */
105763
 
){
105764
 
  sqlite3ParserARG_FETCH;
105765
 
#ifndef NDEBUG
105766
 
  if( yyTraceFILE ){
105767
 
    fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
105768
 
  }
105769
 
#endif
105770
 
  while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
105771
 
  /* Here code is inserted which will be executed whenever the
105772
 
  ** parser fails */
105773
 
  sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
105774
 
}
105775
 
#endif /* YYNOERRORRECOVERY */
105776
 
 
105777
 
/*
105778
 
** The following code executes when a syntax error first occurs.
105779
 
*/
105780
 
static void yy_syntax_error(
105781
 
  yyParser *yypParser,           /* The parser */
105782
 
  int yymajor,                   /* The major type of the error token */
105783
 
  YYMINORTYPE yyminor            /* The minor type of the error token */
105784
 
){
105785
 
  sqlite3ParserARG_FETCH;
105786
 
#define TOKEN (yyminor.yy0)
105787
 
 
105788
 
  UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
105789
 
  assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
105790
 
  sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
105791
 
  pParse->parseError = 1;
105792
 
  sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
105793
 
}
105794
 
 
105795
 
/*
105796
 
** The following is executed when the parser accepts
105797
 
*/
105798
 
static void yy_accept(
105799
 
  yyParser *yypParser           /* The parser */
105800
 
){
105801
 
  sqlite3ParserARG_FETCH;
105802
 
#ifndef NDEBUG
105803
 
  if( yyTraceFILE ){
105804
 
    fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
105805
 
  }
105806
 
#endif
105807
 
  while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
105808
 
  /* Here code is inserted which will be executed whenever the
105809
 
  ** parser accepts */
105810
 
  sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
105811
 
}
105812
 
 
105813
 
/* The main parser program.
105814
 
** The first argument is a pointer to a structure obtained from
105815
 
** "sqlite3ParserAlloc" which describes the current state of the parser.
105816
 
** The second argument is the major token number.  The third is
105817
 
** the minor token.  The fourth optional argument is whatever the
105818
 
** user wants (and specified in the grammar) and is available for
105819
 
** use by the action routines.
105820
 
**
105821
 
** Inputs:
105822
 
** <ul>
105823
 
** <li> A pointer to the parser (an opaque structure.)
105824
 
** <li> The major token number.
105825
 
** <li> The minor token number.
105826
 
** <li> An option argument of a grammar-specified type.
105827
 
** </ul>
105828
 
**
105829
 
** Outputs:
105830
 
** None.
105831
 
*/
105832
 
SQLITE_PRIVATE void sqlite3Parser(
105833
 
  void *yyp,                   /* The parser */
105834
 
  int yymajor,                 /* The major token code number */
105835
 
  sqlite3ParserTOKENTYPE yyminor       /* The value for the token */
105836
 
  sqlite3ParserARG_PDECL               /* Optional %extra_argument parameter */
105837
 
){
105838
 
  YYMINORTYPE yyminorunion;
105839
 
  int yyact;            /* The parser action. */
105840
 
  int yyendofinput;     /* True if we are at the end of input */
105841
 
#ifdef YYERRORSYMBOL
105842
 
  int yyerrorhit = 0;   /* True if yymajor has invoked an error */
105843
 
#endif
105844
 
  yyParser *yypParser;  /* The parser */
105845
 
 
105846
 
  /* (re)initialize the parser, if necessary */
105847
 
  yypParser = (yyParser*)yyp;
105848
 
  if( yypParser->yyidx<0 ){
105849
 
#if YYSTACKDEPTH<=0
105850
 
    if( yypParser->yystksz <=0 ){
105851
 
      /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
105852
 
      yyminorunion = yyzerominor;
105853
 
      yyStackOverflow(yypParser, &yyminorunion);
105854
 
      return;
105855
 
    }
105856
 
#endif
105857
 
    yypParser->yyidx = 0;
105858
 
    yypParser->yyerrcnt = -1;
105859
 
    yypParser->yystack[0].stateno = 0;
105860
 
    yypParser->yystack[0].major = 0;
105861
 
  }
105862
 
  yyminorunion.yy0 = yyminor;
105863
 
  yyendofinput = (yymajor==0);
105864
 
  sqlite3ParserARG_STORE;
105865
 
 
105866
 
#ifndef NDEBUG
105867
 
  if( yyTraceFILE ){
105868
 
    fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
105869
 
  }
105870
 
#endif
105871
 
 
105872
 
  do{
105873
 
    yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
105874
 
    if( yyact<YYNSTATE ){
105875
 
      assert( !yyendofinput );  /* Impossible to shift the $ token */
105876
 
      yy_shift(yypParser,yyact,yymajor,&yyminorunion);
105877
 
      yypParser->yyerrcnt--;
105878
 
      yymajor = YYNOCODE;
105879
 
    }else if( yyact < YYNSTATE + YYNRULE ){
105880
 
      yy_reduce(yypParser,yyact-YYNSTATE);
105881
 
    }else{
105882
 
      assert( yyact == YY_ERROR_ACTION );
105883
 
#ifdef YYERRORSYMBOL
105884
 
      int yymx;
105885
 
#endif
105886
 
#ifndef NDEBUG
105887
 
      if( yyTraceFILE ){
105888
 
        fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
105889
 
      }
105890
 
#endif
105891
 
#ifdef YYERRORSYMBOL
105892
 
      /* A syntax error has occurred.
105893
 
      ** The response to an error depends upon whether or not the
105894
 
      ** grammar defines an error token "ERROR".  
105895
 
      **
105896
 
      ** This is what we do if the grammar does define ERROR:
105897
 
      **
105898
 
      **  * Call the %syntax_error function.
105899
 
      **
105900
 
      **  * Begin popping the stack until we enter a state where
105901
 
      **    it is legal to shift the error symbol, then shift
105902
 
      **    the error symbol.
105903
 
      **
105904
 
      **  * Set the error count to three.
105905
 
      **
105906
 
      **  * Begin accepting and shifting new tokens.  No new error
105907
 
      **    processing will occur until three tokens have been
105908
 
      **    shifted successfully.
105909
 
      **
105910
 
      */
105911
 
      if( yypParser->yyerrcnt<0 ){
105912
 
        yy_syntax_error(yypParser,yymajor,yyminorunion);
105913
 
      }
105914
 
      yymx = yypParser->yystack[yypParser->yyidx].major;
105915
 
      if( yymx==YYERRORSYMBOL || yyerrorhit ){
105916
 
#ifndef NDEBUG
105917
 
        if( yyTraceFILE ){
105918
 
          fprintf(yyTraceFILE,"%sDiscard input token %s\n",
105919
 
             yyTracePrompt,yyTokenName[yymajor]);
105920
 
        }
105921
 
#endif
105922
 
        yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
105923
 
        yymajor = YYNOCODE;
105924
 
      }else{
105925
 
         while(
105926
 
          yypParser->yyidx >= 0 &&
105927
 
          yymx != YYERRORSYMBOL &&
105928
 
          (yyact = yy_find_reduce_action(
105929
 
                        yypParser->yystack[yypParser->yyidx].stateno,
105930
 
                        YYERRORSYMBOL)) >= YYNSTATE
105931
 
        ){
105932
 
          yy_pop_parser_stack(yypParser);
105933
 
        }
105934
 
        if( yypParser->yyidx < 0 || yymajor==0 ){
105935
 
          yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
105936
 
          yy_parse_failed(yypParser);
105937
 
          yymajor = YYNOCODE;
105938
 
        }else if( yymx!=YYERRORSYMBOL ){
105939
 
          YYMINORTYPE u2;
105940
 
          u2.YYERRSYMDT = 0;
105941
 
          yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
105942
 
        }
105943
 
      }
105944
 
      yypParser->yyerrcnt = 3;
105945
 
      yyerrorhit = 1;
105946
 
#elif defined(YYNOERRORRECOVERY)
105947
 
      /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
105948
 
      ** do any kind of error recovery.  Instead, simply invoke the syntax
105949
 
      ** error routine and continue going as if nothing had happened.
105950
 
      **
105951
 
      ** Applications can set this macro (for example inside %include) if
105952
 
      ** they intend to abandon the parse upon the first syntax error seen.
105953
 
      */
105954
 
      yy_syntax_error(yypParser,yymajor,yyminorunion);
105955
 
      yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
105956
 
      yymajor = YYNOCODE;
105957
 
      
105958
 
#else  /* YYERRORSYMBOL is not defined */
105959
 
      /* This is what we do if the grammar does not define ERROR:
105960
 
      **
105961
 
      **  * Report an error message, and throw away the input token.
105962
 
      **
105963
 
      **  * If the input token is $, then fail the parse.
105964
 
      **
105965
 
      ** As before, subsequent error messages are suppressed until
105966
 
      ** three input tokens have been successfully shifted.
105967
 
      */
105968
 
      if( yypParser->yyerrcnt<=0 ){
105969
 
        yy_syntax_error(yypParser,yymajor,yyminorunion);
105970
 
      }
105971
 
      yypParser->yyerrcnt = 3;
105972
 
      yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
105973
 
      if( yyendofinput ){
105974
 
        yy_parse_failed(yypParser);
105975
 
      }
105976
 
      yymajor = YYNOCODE;
105977
 
#endif
105978
 
    }
105979
 
  }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
105980
 
  return;
105981
 
}
105982
 
 
105983
 
/************** End of parse.c ***********************************************/
105984
 
/************** Begin file tokenize.c ****************************************/
105985
 
/*
105986
 
** 2001 September 15
105987
 
**
105988
 
** The author disclaims copyright to this source code.  In place of
105989
 
** a legal notice, here is a blessing:
105990
 
**
105991
 
**    May you do good and not evil.
105992
 
**    May you find forgiveness for yourself and forgive others.
105993
 
**    May you share freely, never taking more than you give.
105994
 
**
105995
 
*************************************************************************
105996
 
** An tokenizer for SQL
105997
 
**
105998
 
** This file contains C code that splits an SQL input string up into
105999
 
** individual tokens and sends those tokens one-by-one over to the
106000
 
** parser for analysis.
106001
 
*/
106002
 
 
106003
 
/*
106004
 
** The charMap() macro maps alphabetic characters into their
106005
 
** lower-case ASCII equivalent.  On ASCII machines, this is just
106006
 
** an upper-to-lower case map.  On EBCDIC machines we also need
106007
 
** to adjust the encoding.  Only alphabetic characters and underscores
106008
 
** need to be translated.
106009
 
*/
106010
 
#ifdef SQLITE_ASCII
106011
 
# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
106012
 
#endif
106013
 
#ifdef SQLITE_EBCDIC
106014
 
# define charMap(X) ebcdicToAscii[(unsigned char)X]
106015
 
const unsigned char ebcdicToAscii[] = {
106016
 
/* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
106017
 
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
106018
 
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
106019
 
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
106020
 
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
106021
 
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
106022
 
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
106023
 
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
106024
 
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
106025
 
   0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
106026
 
   0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
106027
 
   0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
106028
 
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
106029
 
   0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
106030
 
   0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
106031
 
   0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
106032
 
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
106033
 
};
106034
 
#endif
106035
 
 
106036
 
/*
106037
 
** The sqlite3KeywordCode function looks up an identifier to determine if
106038
 
** it is a keyword.  If it is a keyword, the token code of that keyword is 
106039
 
** returned.  If the input is not a keyword, TK_ID is returned.
106040
 
**
106041
 
** The implementation of this routine was generated by a program,
106042
 
** mkkeywordhash.h, located in the tool subdirectory of the distribution.
106043
 
** The output of the mkkeywordhash.c program is written into a file
106044
 
** named keywordhash.h and then included into this source file by
106045
 
** the #include below.
106046
 
*/
106047
 
/************** Include keywordhash.h in the middle of tokenize.c ************/
106048
 
/************** Begin file keywordhash.h *************************************/
106049
 
/***** This file contains automatically generated code ******
106050
 
**
106051
 
** The code in this file has been automatically generated by
106052
 
**
106053
 
**   sqlite/tool/mkkeywordhash.c
106054
 
**
106055
 
** The code in this file implements a function that determines whether
106056
 
** or not a given identifier is really an SQL keyword.  The same thing
106057
 
** might be implemented more directly using a hand-written hash table.
106058
 
** But by using this automatically generated code, the size of the code
106059
 
** is substantially reduced.  This is important for embedded applications
106060
 
** on platforms with limited memory.
106061
 
*/
106062
 
/* Hash score: 175 */
106063
 
static int keywordCode(const char *z, int n){
106064
 
  /* zText[] encodes 811 bytes of keywords in 541 bytes */
106065
 
  /*   REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT       */
106066
 
  /*   ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE         */
106067
 
  /*   XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY         */
106068
 
  /*   UNIQUERYATTACHAVINGROUPDATEBEGINNERELEASEBETWEENOTNULLIKE          */
106069
 
  /*   CASCADELETECASECOLLATECREATECURRENT_DATEDETACHIMMEDIATEJOIN        */
106070
 
  /*   SERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHENWHERENAME         */
106071
 
  /*   AFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMITCONFLICTCROSS     */
106072
 
  /*   CURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAILFROMFULLGLOBYIF      */
106073
 
  /*   ISNULLORDERESTRICTOUTERIGHTROLLBACKROWUNIONUSINGVACUUMVIEW         */
106074
 
  /*   INITIALLY                                                          */
106075
 
  static const char zText[540] = {
106076
 
    'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
106077
 
    'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
106078
 
    'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
106079
 
    'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
106080
 
    'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
106081
 
    'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
106082
 
    'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
106083
 
    'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
106084
 
    'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
106085
 
    'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
106086
 
    'U','E','R','Y','A','T','T','A','C','H','A','V','I','N','G','R','O','U',
106087
 
    'P','D','A','T','E','B','E','G','I','N','N','E','R','E','L','E','A','S',
106088
 
    'E','B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C',
106089
 
    'A','S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L',
106090
 
    'A','T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D',
106091
 
    'A','T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E',
106092
 
    'J','O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A',
106093
 
    'L','Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U',
106094
 
    'E','S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W',
106095
 
    'H','E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C',
106096
 
    'E','A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R',
106097
 
    'E','M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M',
106098
 
    'M','I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U',
106099
 
    'R','R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M',
106100
 
    'A','R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T',
106101
 
    'D','R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L',
106102
 
    'O','B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S',
106103
 
    'T','R','I','C','T','O','U','T','E','R','I','G','H','T','R','O','L','L',
106104
 
    'B','A','C','K','R','O','W','U','N','I','O','N','U','S','I','N','G','V',
106105
 
    'A','C','U','U','M','V','I','E','W','I','N','I','T','I','A','L','L','Y',
106106
 
  };
106107
 
  static const unsigned char aHash[127] = {
106108
 
      72, 101, 114,  70,   0,  45,   0,   0,  78,   0,  73,   0,   0,
106109
 
      42,  12,  74,  15,   0, 113,  81,  50, 108,   0,  19,   0,   0,
106110
 
     118,   0, 116, 111,   0,  22,  89,   0,   9,   0,   0,  66,  67,
106111
 
       0,  65,   6,   0,  48,  86,  98,   0, 115,  97,   0,   0,  44,
106112
 
       0,  99,  24,   0,  17,   0, 119,  49,  23,   0,   5, 106,  25,
106113
 
      92,   0,   0, 121, 102,  56, 120,  53,  28,  51,   0,  87,   0,
106114
 
      96,  26,   0,  95,   0,   0,   0,  91,  88,  93,  84, 105,  14,
106115
 
      39, 104,   0,  77,   0,  18,  85, 107,  32,   0, 117,  76, 109,
106116
 
      58,  46,  80,   0,   0,  90,  40,   0, 112,   0,  36,   0,   0,
106117
 
      29,   0,  82,  59,  60,   0,  20,  57,   0,  52,
106118
 
  };
106119
 
  static const unsigned char aNext[121] = {
106120
 
       0,   0,   0,   0,   4,   0,   0,   0,   0,   0,   0,   0,   0,
106121
 
       0,   2,   0,   0,   0,   0,   0,   0,  13,   0,   0,   0,   0,
106122
 
       0,   7,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
106123
 
       0,   0,   0,   0,  33,   0,  21,   0,   0,   0,  43,   3,  47,
106124
 
       0,   0,   0,   0,  30,   0,  54,   0,  38,   0,   0,   0,   1,
106125
 
      62,   0,   0,  63,   0,  41,   0,   0,   0,   0,   0,   0,   0,
106126
 
      61,   0,   0,   0,   0,  31,  55,  16,  34,  10,   0,   0,   0,
106127
 
       0,   0,   0,   0,  11,  68,  75,   0,   8,   0, 100,  94,   0,
106128
 
     103,   0,  83,   0,  71,   0,   0, 110,  27,  37,  69,  79,   0,
106129
 
      35,  64,   0,   0,
106130
 
  };
106131
 
  static const unsigned char aLen[121] = {
106132
 
       7,   7,   5,   4,   6,   4,   5,   3,   6,   7,   3,   6,   6,
106133
 
       7,   7,   3,   8,   2,   6,   5,   4,   4,   3,  10,   4,   6,
106134
 
      11,   6,   2,   7,   5,   5,   9,   6,   9,   9,   7,  10,  10,
106135
 
       4,   6,   2,   3,   9,   4,   2,   6,   5,   6,   6,   5,   6,
106136
 
       5,   5,   7,   7,   7,   3,   2,   4,   4,   7,   3,   6,   4,
106137
 
       7,   6,  12,   6,   9,   4,   6,   5,   4,   7,   6,   5,   6,
106138
 
       7,   5,   4,   5,   6,   5,   7,   3,   7,  13,   2,   2,   4,
106139
 
       6,   6,   8,   5,  17,  12,   7,   8,   8,   2,   4,   4,   4,
106140
 
       4,   4,   2,   2,   6,   5,   8,   5,   5,   8,   3,   5,   5,
106141
 
       6,   4,   9,   3,
106142
 
  };
106143
 
  static const unsigned short int aOffset[121] = {
106144
 
       0,   2,   2,   8,   9,  14,  16,  20,  23,  25,  25,  29,  33,
106145
 
      36,  41,  46,  48,  53,  54,  59,  62,  65,  67,  69,  78,  81,
106146
 
      86,  91,  95,  96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
106147
 
     159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 189, 194, 197,
106148
 
     203, 206, 210, 217, 223, 223, 223, 226, 229, 233, 234, 238, 244,
106149
 
     248, 255, 261, 273, 279, 288, 290, 296, 301, 303, 310, 315, 320,
106150
 
     326, 332, 337, 341, 344, 350, 354, 361, 363, 370, 372, 374, 383,
106151
 
     387, 393, 399, 407, 412, 412, 428, 435, 442, 443, 450, 454, 458,
106152
 
     462, 466, 469, 471, 473, 479, 483, 491, 495, 500, 508, 511, 516,
106153
 
     521, 527, 531, 536,
106154
 
  };
106155
 
  static const unsigned char aCode[121] = {
106156
 
    TK_REINDEX,    TK_INDEXED,    TK_INDEX,      TK_DESC,       TK_ESCAPE,     
106157
 
    TK_EACH,       TK_CHECK,      TK_KEY,        TK_BEFORE,     TK_FOREIGN,    
106158
 
    TK_FOR,        TK_IGNORE,     TK_LIKE_KW,    TK_EXPLAIN,    TK_INSTEAD,    
106159
 
    TK_ADD,        TK_DATABASE,   TK_AS,         TK_SELECT,     TK_TABLE,      
106160
 
    TK_JOIN_KW,    TK_THEN,       TK_END,        TK_DEFERRABLE, TK_ELSE,       
106161
 
    TK_EXCEPT,     TK_TRANSACTION,TK_ACTION,     TK_ON,         TK_JOIN_KW,    
106162
 
    TK_ALTER,      TK_RAISE,      TK_EXCLUSIVE,  TK_EXISTS,     TK_SAVEPOINT,  
106163
 
    TK_INTERSECT,  TK_TRIGGER,    TK_REFERENCES, TK_CONSTRAINT, TK_INTO,       
106164
 
    TK_OFFSET,     TK_OF,         TK_SET,        TK_TEMP,       TK_TEMP,       
106165
 
    TK_OR,         TK_UNIQUE,     TK_QUERY,      TK_ATTACH,     TK_HAVING,     
106166
 
    TK_GROUP,      TK_UPDATE,     TK_BEGIN,      TK_JOIN_KW,    TK_RELEASE,    
106167
 
    TK_BETWEEN,    TK_NOTNULL,    TK_NOT,        TK_NO,         TK_NULL,       
106168
 
    TK_LIKE_KW,    TK_CASCADE,    TK_ASC,        TK_DELETE,     TK_CASE,       
106169
 
    TK_COLLATE,    TK_CREATE,     TK_CTIME_KW,   TK_DETACH,     TK_IMMEDIATE,  
106170
 
    TK_JOIN,       TK_INSERT,     TK_MATCH,      TK_PLAN,       TK_ANALYZE,    
106171
 
    TK_PRAGMA,     TK_ABORT,      TK_VALUES,     TK_VIRTUAL,    TK_LIMIT,      
106172
 
    TK_WHEN,       TK_WHERE,      TK_RENAME,     TK_AFTER,      TK_REPLACE,    
106173
 
    TK_AND,        TK_DEFAULT,    TK_AUTOINCR,   TK_TO,         TK_IN,         
106174
 
    TK_CAST,       TK_COLUMNKW,   TK_COMMIT,     TK_CONFLICT,   TK_JOIN_KW,    
106175
 
    TK_CTIME_KW,   TK_CTIME_KW,   TK_PRIMARY,    TK_DEFERRED,   TK_DISTINCT,   
106176
 
    TK_IS,         TK_DROP,       TK_FAIL,       TK_FROM,       TK_JOIN_KW,    
106177
 
    TK_LIKE_KW,    TK_BY,         TK_IF,         TK_ISNULL,     TK_ORDER,      
106178
 
    TK_RESTRICT,   TK_JOIN_KW,    TK_JOIN_KW,    TK_ROLLBACK,   TK_ROW,        
106179
 
    TK_UNION,      TK_USING,      TK_VACUUM,     TK_VIEW,       TK_INITIALLY,  
106180
 
    TK_ALL,        
106181
 
  };
106182
 
  int h, i;
106183
 
  if( n<2 ) return TK_ID;
106184
 
  h = ((charMap(z[0])*4) ^
106185
 
      (charMap(z[n-1])*3) ^
106186
 
      n) % 127;
106187
 
  for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
106188
 
    if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
106189
 
      testcase( i==0 ); /* REINDEX */
106190
 
      testcase( i==1 ); /* INDEXED */
106191
 
      testcase( i==2 ); /* INDEX */
106192
 
      testcase( i==3 ); /* DESC */
106193
 
      testcase( i==4 ); /* ESCAPE */
106194
 
      testcase( i==5 ); /* EACH */
106195
 
      testcase( i==6 ); /* CHECK */
106196
 
      testcase( i==7 ); /* KEY */
106197
 
      testcase( i==8 ); /* BEFORE */
106198
 
      testcase( i==9 ); /* FOREIGN */
106199
 
      testcase( i==10 ); /* FOR */
106200
 
      testcase( i==11 ); /* IGNORE */
106201
 
      testcase( i==12 ); /* REGEXP */
106202
 
      testcase( i==13 ); /* EXPLAIN */
106203
 
      testcase( i==14 ); /* INSTEAD */
106204
 
      testcase( i==15 ); /* ADD */
106205
 
      testcase( i==16 ); /* DATABASE */
106206
 
      testcase( i==17 ); /* AS */
106207
 
      testcase( i==18 ); /* SELECT */
106208
 
      testcase( i==19 ); /* TABLE */
106209
 
      testcase( i==20 ); /* LEFT */
106210
 
      testcase( i==21 ); /* THEN */
106211
 
      testcase( i==22 ); /* END */
106212
 
      testcase( i==23 ); /* DEFERRABLE */
106213
 
      testcase( i==24 ); /* ELSE */
106214
 
      testcase( i==25 ); /* EXCEPT */
106215
 
      testcase( i==26 ); /* TRANSACTION */
106216
 
      testcase( i==27 ); /* ACTION */
106217
 
      testcase( i==28 ); /* ON */
106218
 
      testcase( i==29 ); /* NATURAL */
106219
 
      testcase( i==30 ); /* ALTER */
106220
 
      testcase( i==31 ); /* RAISE */
106221
 
      testcase( i==32 ); /* EXCLUSIVE */
106222
 
      testcase( i==33 ); /* EXISTS */
106223
 
      testcase( i==34 ); /* SAVEPOINT */
106224
 
      testcase( i==35 ); /* INTERSECT */
106225
 
      testcase( i==36 ); /* TRIGGER */
106226
 
      testcase( i==37 ); /* REFERENCES */
106227
 
      testcase( i==38 ); /* CONSTRAINT */
106228
 
      testcase( i==39 ); /* INTO */
106229
 
      testcase( i==40 ); /* OFFSET */
106230
 
      testcase( i==41 ); /* OF */
106231
 
      testcase( i==42 ); /* SET */
106232
 
      testcase( i==43 ); /* TEMPORARY */
106233
 
      testcase( i==44 ); /* TEMP */
106234
 
      testcase( i==45 ); /* OR */
106235
 
      testcase( i==46 ); /* UNIQUE */
106236
 
      testcase( i==47 ); /* QUERY */
106237
 
      testcase( i==48 ); /* ATTACH */
106238
 
      testcase( i==49 ); /* HAVING */
106239
 
      testcase( i==50 ); /* GROUP */
106240
 
      testcase( i==51 ); /* UPDATE */
106241
 
      testcase( i==52 ); /* BEGIN */
106242
 
      testcase( i==53 ); /* INNER */
106243
 
      testcase( i==54 ); /* RELEASE */
106244
 
      testcase( i==55 ); /* BETWEEN */
106245
 
      testcase( i==56 ); /* NOTNULL */
106246
 
      testcase( i==57 ); /* NOT */
106247
 
      testcase( i==58 ); /* NO */
106248
 
      testcase( i==59 ); /* NULL */
106249
 
      testcase( i==60 ); /* LIKE */
106250
 
      testcase( i==61 ); /* CASCADE */
106251
 
      testcase( i==62 ); /* ASC */
106252
 
      testcase( i==63 ); /* DELETE */
106253
 
      testcase( i==64 ); /* CASE */
106254
 
      testcase( i==65 ); /* COLLATE */
106255
 
      testcase( i==66 ); /* CREATE */
106256
 
      testcase( i==67 ); /* CURRENT_DATE */
106257
 
      testcase( i==68 ); /* DETACH */
106258
 
      testcase( i==69 ); /* IMMEDIATE */
106259
 
      testcase( i==70 ); /* JOIN */
106260
 
      testcase( i==71 ); /* INSERT */
106261
 
      testcase( i==72 ); /* MATCH */
106262
 
      testcase( i==73 ); /* PLAN */
106263
 
      testcase( i==74 ); /* ANALYZE */
106264
 
      testcase( i==75 ); /* PRAGMA */
106265
 
      testcase( i==76 ); /* ABORT */
106266
 
      testcase( i==77 ); /* VALUES */
106267
 
      testcase( i==78 ); /* VIRTUAL */
106268
 
      testcase( i==79 ); /* LIMIT */
106269
 
      testcase( i==80 ); /* WHEN */
106270
 
      testcase( i==81 ); /* WHERE */
106271
 
      testcase( i==82 ); /* RENAME */
106272
 
      testcase( i==83 ); /* AFTER */
106273
 
      testcase( i==84 ); /* REPLACE */
106274
 
      testcase( i==85 ); /* AND */
106275
 
      testcase( i==86 ); /* DEFAULT */
106276
 
      testcase( i==87 ); /* AUTOINCREMENT */
106277
 
      testcase( i==88 ); /* TO */
106278
 
      testcase( i==89 ); /* IN */
106279
 
      testcase( i==90 ); /* CAST */
106280
 
      testcase( i==91 ); /* COLUMN */
106281
 
      testcase( i==92 ); /* COMMIT */
106282
 
      testcase( i==93 ); /* CONFLICT */
106283
 
      testcase( i==94 ); /* CROSS */
106284
 
      testcase( i==95 ); /* CURRENT_TIMESTAMP */
106285
 
      testcase( i==96 ); /* CURRENT_TIME */
106286
 
      testcase( i==97 ); /* PRIMARY */
106287
 
      testcase( i==98 ); /* DEFERRED */
106288
 
      testcase( i==99 ); /* DISTINCT */
106289
 
      testcase( i==100 ); /* IS */
106290
 
      testcase( i==101 ); /* DROP */
106291
 
      testcase( i==102 ); /* FAIL */
106292
 
      testcase( i==103 ); /* FROM */
106293
 
      testcase( i==104 ); /* FULL */
106294
 
      testcase( i==105 ); /* GLOB */
106295
 
      testcase( i==106 ); /* BY */
106296
 
      testcase( i==107 ); /* IF */
106297
 
      testcase( i==108 ); /* ISNULL */
106298
 
      testcase( i==109 ); /* ORDER */
106299
 
      testcase( i==110 ); /* RESTRICT */
106300
 
      testcase( i==111 ); /* OUTER */
106301
 
      testcase( i==112 ); /* RIGHT */
106302
 
      testcase( i==113 ); /* ROLLBACK */
106303
 
      testcase( i==114 ); /* ROW */
106304
 
      testcase( i==115 ); /* UNION */
106305
 
      testcase( i==116 ); /* USING */
106306
 
      testcase( i==117 ); /* VACUUM */
106307
 
      testcase( i==118 ); /* VIEW */
106308
 
      testcase( i==119 ); /* INITIALLY */
106309
 
      testcase( i==120 ); /* ALL */
106310
 
      return aCode[i];
106311
 
    }
106312
 
  }
106313
 
  return TK_ID;
106314
 
}
106315
 
SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
106316
 
  return keywordCode((char*)z, n);
106317
 
}
106318
 
#define SQLITE_N_KEYWORD 121
106319
 
 
106320
 
/************** End of keywordhash.h *****************************************/
106321
 
/************** Continuing where we left off in tokenize.c *******************/
106322
 
 
106323
 
 
106324
 
/*
106325
 
** If X is a character that can be used in an identifier then
106326
 
** IdChar(X) will be true.  Otherwise it is false.
106327
 
**
106328
 
** For ASCII, any character with the high-order bit set is
106329
 
** allowed in an identifier.  For 7-bit characters, 
106330
 
** sqlite3IsIdChar[X] must be 1.
106331
 
**
106332
 
** For EBCDIC, the rules are more complex but have the same
106333
 
** end result.
106334
 
**
106335
 
** Ticket #1066.  the SQL standard does not allow '$' in the
106336
 
** middle of identfiers.  But many SQL implementations do. 
106337
 
** SQLite will allow '$' in identifiers for compatibility.
106338
 
** But the feature is undocumented.
106339
 
*/
106340
 
#ifdef SQLITE_ASCII
106341
 
#define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
106342
 
#endif
106343
 
#ifdef SQLITE_EBCDIC
106344
 
SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
106345
 
/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
106346
 
    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
106347
 
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
106348
 
    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
106349
 
    0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
106350
 
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
106351
 
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
106352
 
    1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
106353
 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
106354
 
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
106355
 
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
106356
 
    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
106357
 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
106358
 
};
106359
 
#define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
106360
 
#endif
106361
 
 
106362
 
 
106363
 
/*
106364
 
** Return the length of the token that begins at z[0]. 
106365
 
** Store the token type in *tokenType before returning.
106366
 
*/
106367
 
SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
106368
 
  int i, c;
106369
 
  switch( *z ){
106370
 
    case ' ': case '\t': case '\n': case '\f': case '\r': {
106371
 
      testcase( z[0]==' ' );
106372
 
      testcase( z[0]=='\t' );
106373
 
      testcase( z[0]=='\n' );
106374
 
      testcase( z[0]=='\f' );
106375
 
      testcase( z[0]=='\r' );
106376
 
      for(i=1; sqlite3Isspace(z[i]); i++){}
106377
 
      *tokenType = TK_SPACE;
106378
 
      return i;
106379
 
    }
106380
 
    case '-': {
106381
 
      if( z[1]=='-' ){
106382
 
        /* IMP: R-15891-05542 -- syntax diagram for comments */
106383
 
        for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
106384
 
        *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
106385
 
        return i;
106386
 
      }
106387
 
      *tokenType = TK_MINUS;
106388
 
      return 1;
106389
 
    }
106390
 
    case '(': {
106391
 
      *tokenType = TK_LP;
106392
 
      return 1;
106393
 
    }
106394
 
    case ')': {
106395
 
      *tokenType = TK_RP;
106396
 
      return 1;
106397
 
    }
106398
 
    case ';': {
106399
 
      *tokenType = TK_SEMI;
106400
 
      return 1;
106401
 
    }
106402
 
    case '+': {
106403
 
      *tokenType = TK_PLUS;
106404
 
      return 1;
106405
 
    }
106406
 
    case '*': {
106407
 
      *tokenType = TK_STAR;
106408
 
      return 1;
106409
 
    }
106410
 
    case '/': {
106411
 
      if( z[1]!='*' || z[2]==0 ){
106412
 
        *tokenType = TK_SLASH;
106413
 
        return 1;
106414
 
      }
106415
 
      /* IMP: R-15891-05542 -- syntax diagram for comments */
106416
 
      for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
106417
 
      if( c ) i++;
106418
 
      *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
106419
 
      return i;
106420
 
    }
106421
 
    case '%': {
106422
 
      *tokenType = TK_REM;
106423
 
      return 1;
106424
 
    }
106425
 
    case '=': {
106426
 
      *tokenType = TK_EQ;
106427
 
      return 1 + (z[1]=='=');
106428
 
    }
106429
 
    case '<': {
106430
 
      if( (c=z[1])=='=' ){
106431
 
        *tokenType = TK_LE;
106432
 
        return 2;
106433
 
      }else if( c=='>' ){
106434
 
        *tokenType = TK_NE;
106435
 
        return 2;
106436
 
      }else if( c=='<' ){
106437
 
        *tokenType = TK_LSHIFT;
106438
 
        return 2;
106439
 
      }else{
106440
 
        *tokenType = TK_LT;
106441
 
        return 1;
106442
 
      }
106443
 
    }
106444
 
    case '>': {
106445
 
      if( (c=z[1])=='=' ){
106446
 
        *tokenType = TK_GE;
106447
 
        return 2;
106448
 
      }else if( c=='>' ){
106449
 
        *tokenType = TK_RSHIFT;
106450
 
        return 2;
106451
 
      }else{
106452
 
        *tokenType = TK_GT;
106453
 
        return 1;
106454
 
      }
106455
 
    }
106456
 
    case '!': {
106457
 
      if( z[1]!='=' ){
106458
 
        *tokenType = TK_ILLEGAL;
106459
 
        return 2;
106460
 
      }else{
106461
 
        *tokenType = TK_NE;
106462
 
        return 2;
106463
 
      }
106464
 
    }
106465
 
    case '|': {
106466
 
      if( z[1]!='|' ){
106467
 
        *tokenType = TK_BITOR;
106468
 
        return 1;
106469
 
      }else{
106470
 
        *tokenType = TK_CONCAT;
106471
 
        return 2;
106472
 
      }
106473
 
    }
106474
 
    case ',': {
106475
 
      *tokenType = TK_COMMA;
106476
 
      return 1;
106477
 
    }
106478
 
    case '&': {
106479
 
      *tokenType = TK_BITAND;
106480
 
      return 1;
106481
 
    }
106482
 
    case '~': {
106483
 
      *tokenType = TK_BITNOT;
106484
 
      return 1;
106485
 
    }
106486
 
    case '`':
106487
 
    case '\'':
106488
 
    case '"': {
106489
 
      int delim = z[0];
106490
 
      testcase( delim=='`' );
106491
 
      testcase( delim=='\'' );
106492
 
      testcase( delim=='"' );
106493
 
      for(i=1; (c=z[i])!=0; i++){
106494
 
        if( c==delim ){
106495
 
          if( z[i+1]==delim ){
106496
 
            i++;
106497
 
          }else{
106498
 
            break;
106499
 
          }
106500
 
        }
106501
 
      }
106502
 
      if( c=='\'' ){
106503
 
        *tokenType = TK_STRING;
106504
 
        return i+1;
106505
 
      }else if( c!=0 ){
106506
 
        *tokenType = TK_ID;
106507
 
        return i+1;
106508
 
      }else{
106509
 
        *tokenType = TK_ILLEGAL;
106510
 
        return i;
106511
 
      }
106512
 
    }
106513
 
    case '.': {
106514
 
#ifndef SQLITE_OMIT_FLOATING_POINT
106515
 
      if( !sqlite3Isdigit(z[1]) )
106516
 
#endif
106517
 
      {
106518
 
        *tokenType = TK_DOT;
106519
 
        return 1;
106520
 
      }
106521
 
      /* If the next character is a digit, this is a floating point
106522
 
      ** number that begins with ".".  Fall thru into the next case */
106523
 
    }
106524
 
    case '0': case '1': case '2': case '3': case '4':
106525
 
    case '5': case '6': case '7': case '8': case '9': {
106526
 
      testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
106527
 
      testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
106528
 
      testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
106529
 
      testcase( z[0]=='9' );
106530
 
      *tokenType = TK_INTEGER;
106531
 
      for(i=0; sqlite3Isdigit(z[i]); i++){}
106532
 
#ifndef SQLITE_OMIT_FLOATING_POINT
106533
 
      if( z[i]=='.' ){
106534
 
        i++;
106535
 
        while( sqlite3Isdigit(z[i]) ){ i++; }
106536
 
        *tokenType = TK_FLOAT;
106537
 
      }
106538
 
      if( (z[i]=='e' || z[i]=='E') &&
106539
 
           ( sqlite3Isdigit(z[i+1]) 
106540
 
            || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
106541
 
           )
106542
 
      ){
106543
 
        i += 2;
106544
 
        while( sqlite3Isdigit(z[i]) ){ i++; }
106545
 
        *tokenType = TK_FLOAT;
106546
 
      }
106547
 
#endif
106548
 
      while( IdChar(z[i]) ){
106549
 
        *tokenType = TK_ILLEGAL;
106550
 
        i++;
106551
 
      }
106552
 
      return i;
106553
 
    }
106554
 
    case '[': {
106555
 
      for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
106556
 
      *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
106557
 
      return i;
106558
 
    }
106559
 
    case '?': {
106560
 
      *tokenType = TK_VARIABLE;
106561
 
      for(i=1; sqlite3Isdigit(z[i]); i++){}
106562
 
      return i;
106563
 
    }
106564
 
    case '#': {
106565
 
      for(i=1; sqlite3Isdigit(z[i]); i++){}
106566
 
      if( i>1 ){
106567
 
        /* Parameters of the form #NNN (where NNN is a number) are used
106568
 
        ** internally by sqlite3NestedParse.  */
106569
 
        *tokenType = TK_REGISTER;
106570
 
        return i;
106571
 
      }
106572
 
      /* Fall through into the next case if the '#' is not followed by
106573
 
      ** a digit. Try to match #AAAA where AAAA is a parameter name. */
106574
 
    }
106575
 
#ifndef SQLITE_OMIT_TCL_VARIABLE
106576
 
    case '$':
106577
 
#endif
106578
 
    case '@':  /* For compatibility with MS SQL Server */
106579
 
    case ':': {
106580
 
      int n = 0;
106581
 
      testcase( z[0]=='$' );  testcase( z[0]=='@' );  testcase( z[0]==':' );
106582
 
      *tokenType = TK_VARIABLE;
106583
 
      for(i=1; (c=z[i])!=0; i++){
106584
 
        if( IdChar(c) ){
106585
 
          n++;
106586
 
#ifndef SQLITE_OMIT_TCL_VARIABLE
106587
 
        }else if( c=='(' && n>0 ){
106588
 
          do{
106589
 
            i++;
106590
 
          }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
106591
 
          if( c==')' ){
106592
 
            i++;
106593
 
          }else{
106594
 
            *tokenType = TK_ILLEGAL;
106595
 
          }
106596
 
          break;
106597
 
        }else if( c==':' && z[i+1]==':' ){
106598
 
          i++;
106599
 
#endif
106600
 
        }else{
106601
 
          break;
106602
 
        }
106603
 
      }
106604
 
      if( n==0 ) *tokenType = TK_ILLEGAL;
106605
 
      return i;
106606
 
    }
106607
 
#ifndef SQLITE_OMIT_BLOB_LITERAL
106608
 
    case 'x': case 'X': {
106609
 
      testcase( z[0]=='x' ); testcase( z[0]=='X' );
106610
 
      if( z[1]=='\'' ){
106611
 
        *tokenType = TK_BLOB;
106612
 
        for(i=2; (c=z[i])!=0 && c!='\''; i++){
106613
 
          if( !sqlite3Isxdigit(c) ){
106614
 
            *tokenType = TK_ILLEGAL;
106615
 
          }
106616
 
        }
106617
 
        if( i%2 || !c ) *tokenType = TK_ILLEGAL;
106618
 
        if( c ) i++;
106619
 
        return i;
106620
 
      }
106621
 
      /* Otherwise fall through to the next case */
106622
 
    }
106623
 
#endif
106624
 
    default: {
106625
 
      if( !IdChar(*z) ){
106626
 
        break;
106627
 
      }
106628
 
      for(i=1; IdChar(z[i]); i++){}
106629
 
      *tokenType = keywordCode((char*)z, i);
106630
 
      return i;
106631
 
    }
106632
 
  }
106633
 
  *tokenType = TK_ILLEGAL;
106634
 
  return 1;
106635
 
}
106636
 
 
106637
 
/*
106638
 
** Run the parser on the given SQL string.  The parser structure is
106639
 
** passed in.  An SQLITE_ status code is returned.  If an error occurs
106640
 
** then an and attempt is made to write an error message into 
106641
 
** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
106642
 
** error message.
106643
 
*/
106644
 
SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
106645
 
  int nErr = 0;                   /* Number of errors encountered */
106646
 
  int i;                          /* Loop counter */
106647
 
  void *pEngine;                  /* The LEMON-generated LALR(1) parser */
106648
 
  int tokenType;                  /* type of the next token */
106649
 
  int lastTokenParsed = -1;       /* type of the previous token */
106650
 
  u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
106651
 
  sqlite3 *db = pParse->db;       /* The database connection */
106652
 
  int mxSqlLen;                   /* Max length of an SQL string */
106653
 
 
106654
 
 
106655
 
  mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
106656
 
  if( db->activeVdbeCnt==0 ){
106657
 
    db->u1.isInterrupted = 0;
106658
 
  }
106659
 
  pParse->rc = SQLITE_OK;
106660
 
  pParse->zTail = zSql;
106661
 
  i = 0;
106662
 
  assert( pzErrMsg!=0 );
106663
 
  pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
106664
 
  if( pEngine==0 ){
106665
 
    db->mallocFailed = 1;
106666
 
    return SQLITE_NOMEM;
106667
 
  }
106668
 
  assert( pParse->pNewTable==0 );
106669
 
  assert( pParse->pNewTrigger==0 );
106670
 
  assert( pParse->nVar==0 );
106671
 
  assert( pParse->nVarExpr==0 );
106672
 
  assert( pParse->nVarExprAlloc==0 );
106673
 
  assert( pParse->apVarExpr==0 );
106674
 
  enableLookaside = db->lookaside.bEnabled;
106675
 
  if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
106676
 
  while( !db->mallocFailed && zSql[i]!=0 ){
106677
 
    assert( i>=0 );
106678
 
    pParse->sLastToken.z = &zSql[i];
106679
 
    pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
106680
 
    i += pParse->sLastToken.n;
106681
 
    if( i>mxSqlLen ){
106682
 
      pParse->rc = SQLITE_TOOBIG;
106683
 
      break;
106684
 
    }
106685
 
    switch( tokenType ){
106686
 
      case TK_SPACE: {
106687
 
        if( db->u1.isInterrupted ){
106688
 
          sqlite3ErrorMsg(pParse, "interrupt");
106689
 
          pParse->rc = SQLITE_INTERRUPT;
106690
 
          goto abort_parse;
106691
 
        }
106692
 
        break;
106693
 
      }
106694
 
      case TK_ILLEGAL: {
106695
 
        sqlite3DbFree(db, *pzErrMsg);
106696
 
        *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
106697
 
                        &pParse->sLastToken);
106698
 
        nErr++;
106699
 
        goto abort_parse;
106700
 
      }
106701
 
      case TK_SEMI: {
106702
 
        pParse->zTail = &zSql[i];
106703
 
        /* Fall thru into the default case */
106704
 
      }
106705
 
      default: {
106706
 
        sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
106707
 
        lastTokenParsed = tokenType;
106708
 
        if( pParse->rc!=SQLITE_OK ){
106709
 
          goto abort_parse;
106710
 
        }
106711
 
        break;
106712
 
      }
106713
 
    }
106714
 
  }
106715
 
abort_parse:
106716
 
  if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
106717
 
    if( lastTokenParsed!=TK_SEMI ){
106718
 
      sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
106719
 
      pParse->zTail = &zSql[i];
106720
 
    }
106721
 
    sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
106722
 
  }
106723
 
#ifdef YYTRACKMAXSTACKDEPTH
106724
 
  sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
106725
 
      sqlite3ParserStackPeak(pEngine)
106726
 
  );
106727
 
#endif /* YYDEBUG */
106728
 
  sqlite3ParserFree(pEngine, sqlite3_free);
106729
 
  db->lookaside.bEnabled = enableLookaside;
106730
 
  if( db->mallocFailed ){
106731
 
    pParse->rc = SQLITE_NOMEM;
106732
 
  }
106733
 
  if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
106734
 
    sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
106735
 
  }
106736
 
  assert( pzErrMsg!=0 );
106737
 
  if( pParse->zErrMsg ){
106738
 
    *pzErrMsg = pParse->zErrMsg;
106739
 
    sqlite3_log(pParse->rc, "%s", *pzErrMsg);
106740
 
    pParse->zErrMsg = 0;
106741
 
    nErr++;
106742
 
  }
106743
 
  if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
106744
 
    sqlite3VdbeDelete(pParse->pVdbe);
106745
 
    pParse->pVdbe = 0;
106746
 
  }
106747
 
#ifndef SQLITE_OMIT_SHARED_CACHE
106748
 
  if( pParse->nested==0 ){
106749
 
    sqlite3DbFree(db, pParse->aTableLock);
106750
 
    pParse->aTableLock = 0;
106751
 
    pParse->nTableLock = 0;
106752
 
  }
106753
 
#endif
106754
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
106755
 
  sqlite3_free(pParse->apVtabLock);
106756
 
#endif
106757
 
 
106758
 
  if( !IN_DECLARE_VTAB ){
106759
 
    /* If the pParse->declareVtab flag is set, do not delete any table 
106760
 
    ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
106761
 
    ** will take responsibility for freeing the Table structure.
106762
 
    */
106763
 
    sqlite3DeleteTable(db, pParse->pNewTable);
106764
 
  }
106765
 
 
106766
 
  sqlite3DeleteTrigger(db, pParse->pNewTrigger);
106767
 
  sqlite3DbFree(db, pParse->apVarExpr);
106768
 
  sqlite3DbFree(db, pParse->aAlias);
106769
 
  while( pParse->pAinc ){
106770
 
    AutoincInfo *p = pParse->pAinc;
106771
 
    pParse->pAinc = p->pNext;
106772
 
    sqlite3DbFree(db, p);
106773
 
  }
106774
 
  while( pParse->pZombieTab ){
106775
 
    Table *p = pParse->pZombieTab;
106776
 
    pParse->pZombieTab = p->pNextZombie;
106777
 
    sqlite3DeleteTable(db, p);
106778
 
  }
106779
 
  if( nErr>0 && pParse->rc==SQLITE_OK ){
106780
 
    pParse->rc = SQLITE_ERROR;
106781
 
  }
106782
 
  return nErr;
106783
 
}
106784
 
 
106785
 
/************** End of tokenize.c ********************************************/
106786
 
/************** Begin file complete.c ****************************************/
106787
 
/*
106788
 
** 2001 September 15
106789
 
**
106790
 
** The author disclaims copyright to this source code.  In place of
106791
 
** a legal notice, here is a blessing:
106792
 
**
106793
 
**    May you do good and not evil.
106794
 
**    May you find forgiveness for yourself and forgive others.
106795
 
**    May you share freely, never taking more than you give.
106796
 
**
106797
 
*************************************************************************
106798
 
** An tokenizer for SQL
106799
 
**
106800
 
** This file contains C code that implements the sqlite3_complete() API.
106801
 
** This code used to be part of the tokenizer.c source file.  But by
106802
 
** separating it out, the code will be automatically omitted from
106803
 
** static links that do not use it.
106804
 
*/
106805
 
#ifndef SQLITE_OMIT_COMPLETE
106806
 
 
106807
 
/*
106808
 
** This is defined in tokenize.c.  We just have to import the definition.
106809
 
*/
106810
 
#ifndef SQLITE_AMALGAMATION
106811
 
#ifdef SQLITE_ASCII
106812
 
#define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
106813
 
#endif
106814
 
#ifdef SQLITE_EBCDIC
106815
 
SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
106816
 
#define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
106817
 
#endif
106818
 
#endif /* SQLITE_AMALGAMATION */
106819
 
 
106820
 
 
106821
 
/*
106822
 
** Token types used by the sqlite3_complete() routine.  See the header
106823
 
** comments on that procedure for additional information.
106824
 
*/
106825
 
#define tkSEMI    0
106826
 
#define tkWS      1
106827
 
#define tkOTHER   2
106828
 
#ifndef SQLITE_OMIT_TRIGGER
106829
 
#define tkEXPLAIN 3
106830
 
#define tkCREATE  4
106831
 
#define tkTEMP    5
106832
 
#define tkTRIGGER 6
106833
 
#define tkEND     7
106834
 
#endif
106835
 
 
106836
 
/*
106837
 
** Return TRUE if the given SQL string ends in a semicolon.
106838
 
**
106839
 
** Special handling is require for CREATE TRIGGER statements.
106840
 
** Whenever the CREATE TRIGGER keywords are seen, the statement
106841
 
** must end with ";END;".
106842
 
**
106843
 
** This implementation uses a state machine with 8 states:
106844
 
**
106845
 
**   (0) INVALID   We have not yet seen a non-whitespace character.
106846
 
**
106847
 
**   (1) START     At the beginning or end of an SQL statement.  This routine
106848
 
**                 returns 1 if it ends in the START state and 0 if it ends
106849
 
**                 in any other state.
106850
 
**
106851
 
**   (2) NORMAL    We are in the middle of statement which ends with a single
106852
 
**                 semicolon.
106853
 
**
106854
 
**   (3) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of 
106855
 
**                 a statement.
106856
 
**
106857
 
**   (4) CREATE    The keyword CREATE has been seen at the beginning of a
106858
 
**                 statement, possibly preceeded by EXPLAIN and/or followed by
106859
 
**                 TEMP or TEMPORARY
106860
 
**
106861
 
**   (5) TRIGGER   We are in the middle of a trigger definition that must be
106862
 
**                 ended by a semicolon, the keyword END, and another semicolon.
106863
 
**
106864
 
**   (6) SEMI      We've seen the first semicolon in the ";END;" that occurs at
106865
 
**                 the end of a trigger definition.
106866
 
**
106867
 
**   (7) END       We've seen the ";END" of the ";END;" that occurs at the end
106868
 
**                 of a trigger difinition.
106869
 
**
106870
 
** Transitions between states above are determined by tokens extracted
106871
 
** from the input.  The following tokens are significant:
106872
 
**
106873
 
**   (0) tkSEMI      A semicolon.
106874
 
**   (1) tkWS        Whitespace.
106875
 
**   (2) tkOTHER     Any other SQL token.
106876
 
**   (3) tkEXPLAIN   The "explain" keyword.
106877
 
**   (4) tkCREATE    The "create" keyword.
106878
 
**   (5) tkTEMP      The "temp" or "temporary" keyword.
106879
 
**   (6) tkTRIGGER   The "trigger" keyword.
106880
 
**   (7) tkEND       The "end" keyword.
106881
 
**
106882
 
** Whitespace never causes a state transition and is always ignored.
106883
 
** This means that a SQL string of all whitespace is invalid.
106884
 
**
106885
 
** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
106886
 
** to recognize the end of a trigger can be omitted.  All we have to do
106887
 
** is look for a semicolon that is not part of an string or comment.
106888
 
*/
106889
 
SQLITE_API int sqlite3_complete(const char *zSql){
106890
 
  u8 state = 0;   /* Current state, using numbers defined in header comment */
106891
 
  u8 token;       /* Value of the next token */
106892
 
 
106893
 
#ifndef SQLITE_OMIT_TRIGGER
106894
 
  /* A complex statement machine used to detect the end of a CREATE TRIGGER
106895
 
  ** statement.  This is the normal case.
106896
 
  */
106897
 
  static const u8 trans[8][8] = {
106898
 
                     /* Token:                                                */
106899
 
     /* State:       **  SEMI  WS  OTHER  EXPLAIN  CREATE  TEMP  TRIGGER  END */
106900
 
     /* 0 INVALID: */ {    1,  0,     2,       3,      4,    2,       2,   2, },
106901
 
     /* 1   START: */ {    1,  1,     2,       3,      4,    2,       2,   2, },
106902
 
     /* 2  NORMAL: */ {    1,  2,     2,       2,      2,    2,       2,   2, },
106903
 
     /* 3 EXPLAIN: */ {    1,  3,     3,       2,      4,    2,       2,   2, },
106904
 
     /* 4  CREATE: */ {    1,  4,     2,       2,      2,    4,       5,   2, },
106905
 
     /* 5 TRIGGER: */ {    6,  5,     5,       5,      5,    5,       5,   5, },
106906
 
     /* 6    SEMI: */ {    6,  6,     5,       5,      5,    5,       5,   7, },
106907
 
     /* 7     END: */ {    1,  7,     5,       5,      5,    5,       5,   5, },
106908
 
  };
106909
 
#else
106910
 
  /* If triggers are not supported by this compile then the statement machine
106911
 
  ** used to detect the end of a statement is much simplier
106912
 
  */
106913
 
  static const u8 trans[3][3] = {
106914
 
                     /* Token:           */
106915
 
     /* State:       **  SEMI  WS  OTHER */
106916
 
     /* 0 INVALID: */ {    1,  0,     2, },
106917
 
     /* 1   START: */ {    1,  1,     2, },
106918
 
     /* 2  NORMAL: */ {    1,  2,     2, },
106919
 
  };
106920
 
#endif /* SQLITE_OMIT_TRIGGER */
106921
 
 
106922
 
  while( *zSql ){
106923
 
    switch( *zSql ){
106924
 
      case ';': {  /* A semicolon */
106925
 
        token = tkSEMI;
106926
 
        break;
106927
 
      }
106928
 
      case ' ':
106929
 
      case '\r':
106930
 
      case '\t':
106931
 
      case '\n':
106932
 
      case '\f': {  /* White space is ignored */
106933
 
        token = tkWS;
106934
 
        break;
106935
 
      }
106936
 
      case '/': {   /* C-style comments */
106937
 
        if( zSql[1]!='*' ){
106938
 
          token = tkOTHER;
106939
 
          break;
106940
 
        }
106941
 
        zSql += 2;
106942
 
        while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
106943
 
        if( zSql[0]==0 ) return 0;
106944
 
        zSql++;
106945
 
        token = tkWS;
106946
 
        break;
106947
 
      }
106948
 
      case '-': {   /* SQL-style comments from "--" to end of line */
106949
 
        if( zSql[1]!='-' ){
106950
 
          token = tkOTHER;
106951
 
          break;
106952
 
        }
106953
 
        while( *zSql && *zSql!='\n' ){ zSql++; }
106954
 
        if( *zSql==0 ) return state==1;
106955
 
        token = tkWS;
106956
 
        break;
106957
 
      }
106958
 
      case '[': {   /* Microsoft-style identifiers in [...] */
106959
 
        zSql++;
106960
 
        while( *zSql && *zSql!=']' ){ zSql++; }
106961
 
        if( *zSql==0 ) return 0;
106962
 
        token = tkOTHER;
106963
 
        break;
106964
 
      }
106965
 
      case '`':     /* Grave-accent quoted symbols used by MySQL */
106966
 
      case '"':     /* single- and double-quoted strings */
106967
 
      case '\'': {
106968
 
        int c = *zSql;
106969
 
        zSql++;
106970
 
        while( *zSql && *zSql!=c ){ zSql++; }
106971
 
        if( *zSql==0 ) return 0;
106972
 
        token = tkOTHER;
106973
 
        break;
106974
 
      }
106975
 
      default: {
106976
 
#ifdef SQLITE_EBCDIC
106977
 
        unsigned char c;
106978
 
#endif
106979
 
        if( IdChar((u8)*zSql) ){
106980
 
          /* Keywords and unquoted identifiers */
106981
 
          int nId;
106982
 
          for(nId=1; IdChar(zSql[nId]); nId++){}
106983
 
#ifdef SQLITE_OMIT_TRIGGER
106984
 
          token = tkOTHER;
106985
 
#else
106986
 
          switch( *zSql ){
106987
 
            case 'c': case 'C': {
106988
 
              if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
106989
 
                token = tkCREATE;
106990
 
              }else{
106991
 
                token = tkOTHER;
106992
 
              }
106993
 
              break;
106994
 
            }
106995
 
            case 't': case 'T': {
106996
 
              if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
106997
 
                token = tkTRIGGER;
106998
 
              }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
106999
 
                token = tkTEMP;
107000
 
              }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
107001
 
                token = tkTEMP;
107002
 
              }else{
107003
 
                token = tkOTHER;
107004
 
              }
107005
 
              break;
107006
 
            }
107007
 
            case 'e':  case 'E': {
107008
 
              if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
107009
 
                token = tkEND;
107010
 
              }else
107011
 
#ifndef SQLITE_OMIT_EXPLAIN
107012
 
              if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
107013
 
                token = tkEXPLAIN;
107014
 
              }else
107015
 
#endif
107016
 
              {
107017
 
                token = tkOTHER;
107018
 
              }
107019
 
              break;
107020
 
            }
107021
 
            default: {
107022
 
              token = tkOTHER;
107023
 
              break;
107024
 
            }
107025
 
          }
107026
 
#endif /* SQLITE_OMIT_TRIGGER */
107027
 
          zSql += nId-1;
107028
 
        }else{
107029
 
          /* Operators and special symbols */
107030
 
          token = tkOTHER;
107031
 
        }
107032
 
        break;
107033
 
      }
107034
 
    }
107035
 
    state = trans[state][token];
107036
 
    zSql++;
107037
 
  }
107038
 
  return state==1;
107039
 
}
107040
 
 
107041
 
#ifndef SQLITE_OMIT_UTF16
107042
 
/*
107043
 
** This routine is the same as the sqlite3_complete() routine described
107044
 
** above, except that the parameter is required to be UTF-16 encoded, not
107045
 
** UTF-8.
107046
 
*/
107047
 
SQLITE_API int sqlite3_complete16(const void *zSql){
107048
 
  sqlite3_value *pVal;
107049
 
  char const *zSql8;
107050
 
  int rc = SQLITE_NOMEM;
107051
 
 
107052
 
#ifndef SQLITE_OMIT_AUTOINIT
107053
 
  rc = sqlite3_initialize();
107054
 
  if( rc ) return rc;
107055
 
#endif
107056
 
  pVal = sqlite3ValueNew(0);
107057
 
  sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
107058
 
  zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
107059
 
  if( zSql8 ){
107060
 
    rc = sqlite3_complete(zSql8);
107061
 
  }else{
107062
 
    rc = SQLITE_NOMEM;
107063
 
  }
107064
 
  sqlite3ValueFree(pVal);
107065
 
  return sqlite3ApiExit(0, rc);
107066
 
}
107067
 
#endif /* SQLITE_OMIT_UTF16 */
107068
 
#endif /* SQLITE_OMIT_COMPLETE */
107069
 
 
107070
 
/************** End of complete.c ********************************************/
107071
 
/************** Begin file main.c ********************************************/
107072
 
/*
107073
 
** 2001 September 15
107074
 
**
107075
 
** The author disclaims copyright to this source code.  In place of
107076
 
** a legal notice, here is a blessing:
107077
 
**
107078
 
**    May you do good and not evil.
107079
 
**    May you find forgiveness for yourself and forgive others.
107080
 
**    May you share freely, never taking more than you give.
107081
 
**
107082
 
*************************************************************************
107083
 
** Main file for the SQLite library.  The routines in this file
107084
 
** implement the programmer interface to the library.  Routines in
107085
 
** other files are for internal use by SQLite and should not be
107086
 
** accessed by users of the library.
107087
 
*/
107088
 
 
107089
 
#ifdef SQLITE_ENABLE_FTS3
107090
 
/************** Include fts3.h in the middle of main.c ***********************/
107091
 
/************** Begin file fts3.h ********************************************/
107092
 
/*
107093
 
** 2006 Oct 10
107094
 
**
107095
 
** The author disclaims copyright to this source code.  In place of
107096
 
** a legal notice, here is a blessing:
107097
 
**
107098
 
**    May you do good and not evil.
107099
 
**    May you find forgiveness for yourself and forgive others.
107100
 
**    May you share freely, never taking more than you give.
107101
 
**
107102
 
******************************************************************************
107103
 
**
107104
 
** This header file is used by programs that want to link against the
107105
 
** FTS3 library.  All it does is declare the sqlite3Fts3Init() interface.
107106
 
*/
107107
 
 
107108
 
#if 0
107109
 
extern "C" {
107110
 
#endif  /* __cplusplus */
107111
 
 
107112
 
SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
107113
 
 
107114
 
#if 0
107115
 
}  /* extern "C" */
107116
 
#endif  /* __cplusplus */
107117
 
 
107118
 
/************** End of fts3.h ************************************************/
107119
 
/************** Continuing where we left off in main.c ***********************/
107120
 
#endif
107121
 
#ifdef SQLITE_ENABLE_RTREE
107122
 
/************** Include rtree.h in the middle of main.c **********************/
107123
 
/************** Begin file rtree.h *******************************************/
107124
 
/*
107125
 
** 2008 May 26
107126
 
**
107127
 
** The author disclaims copyright to this source code.  In place of
107128
 
** a legal notice, here is a blessing:
107129
 
**
107130
 
**    May you do good and not evil.
107131
 
**    May you find forgiveness for yourself and forgive others.
107132
 
**    May you share freely, never taking more than you give.
107133
 
**
107134
 
******************************************************************************
107135
 
**
107136
 
** This header file is used by programs that want to link against the
107137
 
** RTREE library.  All it does is declare the sqlite3RtreeInit() interface.
107138
 
*/
107139
 
 
107140
 
#if 0
107141
 
extern "C" {
107142
 
#endif  /* __cplusplus */
107143
 
 
107144
 
SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
107145
 
 
107146
 
#if 0
107147
 
}  /* extern "C" */
107148
 
#endif  /* __cplusplus */
107149
 
 
107150
 
/************** End of rtree.h ***********************************************/
107151
 
/************** Continuing where we left off in main.c ***********************/
107152
 
#endif
107153
 
#ifdef SQLITE_ENABLE_ICU
107154
 
/************** Include sqliteicu.h in the middle of main.c ******************/
107155
 
/************** Begin file sqliteicu.h ***************************************/
107156
 
/*
107157
 
** 2008 May 26
107158
 
**
107159
 
** The author disclaims copyright to this source code.  In place of
107160
 
** a legal notice, here is a blessing:
107161
 
**
107162
 
**    May you do good and not evil.
107163
 
**    May you find forgiveness for yourself and forgive others.
107164
 
**    May you share freely, never taking more than you give.
107165
 
**
107166
 
******************************************************************************
107167
 
**
107168
 
** This header file is used by programs that want to link against the
107169
 
** ICU extension.  All it does is declare the sqlite3IcuInit() interface.
107170
 
*/
107171
 
 
107172
 
#if 0
107173
 
extern "C" {
107174
 
#endif  /* __cplusplus */
107175
 
 
107176
 
SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
107177
 
 
107178
 
#if 0
107179
 
}  /* extern "C" */
107180
 
#endif  /* __cplusplus */
107181
 
 
107182
 
 
107183
 
/************** End of sqliteicu.h *******************************************/
107184
 
/************** Continuing where we left off in main.c ***********************/
107185
 
#endif
107186
 
 
107187
 
#ifndef SQLITE_AMALGAMATION
107188
 
/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
107189
 
** contains the text of SQLITE_VERSION macro. 
107190
 
*/
107191
 
SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
107192
 
#endif
107193
 
 
107194
 
/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
107195
 
** a pointer to the to the sqlite3_version[] string constant. 
107196
 
*/
107197
 
SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
107198
 
 
107199
 
/* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
107200
 
** pointer to a string constant whose value is the same as the
107201
 
** SQLITE_SOURCE_ID C preprocessor macro. 
107202
 
*/
107203
 
SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
107204
 
 
107205
 
/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
107206
 
** returns an integer equal to SQLITE_VERSION_NUMBER.
107207
 
*/
107208
 
SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
107209
 
 
107210
 
/* IMPLEMENTATION-OF: R-54823-41343 The sqlite3_threadsafe() function returns
107211
 
** zero if and only if SQLite was compiled mutexing code omitted due to
107212
 
** the SQLITE_THREADSAFE compile-time option being set to 0.
107213
 
*/
107214
 
SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
107215
 
 
107216
 
#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
107217
 
/*
107218
 
** If the following function pointer is not NULL and if
107219
 
** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
107220
 
** I/O active are written using this function.  These messages
107221
 
** are intended for debugging activity only.
107222
 
*/
107223
 
SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*, ...) = 0;
107224
 
#endif
107225
 
 
107226
 
/*
107227
 
** If the following global variable points to a string which is the
107228
 
** name of a directory, then that directory will be used to store
107229
 
** temporary files.
107230
 
**
107231
 
** See also the "PRAGMA temp_store_directory" SQL command.
107232
 
*/
107233
 
SQLITE_API char *sqlite3_temp_directory = 0;
107234
 
 
107235
 
/*
107236
 
** Initialize SQLite.  
107237
 
**
107238
 
** This routine must be called to initialize the memory allocation,
107239
 
** VFS, and mutex subsystems prior to doing any serious work with
107240
 
** SQLite.  But as long as you do not compile with SQLITE_OMIT_AUTOINIT
107241
 
** this routine will be called automatically by key routines such as
107242
 
** sqlite3_open().  
107243
 
**
107244
 
** This routine is a no-op except on its very first call for the process,
107245
 
** or for the first call after a call to sqlite3_shutdown.
107246
 
**
107247
 
** The first thread to call this routine runs the initialization to
107248
 
** completion.  If subsequent threads call this routine before the first
107249
 
** thread has finished the initialization process, then the subsequent
107250
 
** threads must block until the first thread finishes with the initialization.
107251
 
**
107252
 
** The first thread might call this routine recursively.  Recursive
107253
 
** calls to this routine should not block, of course.  Otherwise the
107254
 
** initialization process would never complete.
107255
 
**
107256
 
** Let X be the first thread to enter this routine.  Let Y be some other
107257
 
** thread.  Then while the initial invocation of this routine by X is
107258
 
** incomplete, it is required that:
107259
 
**
107260
 
**    *  Calls to this routine from Y must block until the outer-most
107261
 
**       call by X completes.
107262
 
**
107263
 
**    *  Recursive calls to this routine from thread X return immediately
107264
 
**       without blocking.
107265
 
*/
107266
 
SQLITE_API int sqlite3_initialize(void){
107267
 
  sqlite3_mutex *pMaster;                      /* The main static mutex */
107268
 
  int rc;                                      /* Result code */
107269
 
 
107270
 
#ifdef SQLITE_OMIT_WSD
107271
 
  rc = sqlite3_wsd_init(4096, 24);
107272
 
  if( rc!=SQLITE_OK ){
107273
 
    return rc;
107274
 
  }
107275
 
#endif
107276
 
 
107277
 
  /* If SQLite is already completely initialized, then this call
107278
 
  ** to sqlite3_initialize() should be a no-op.  But the initialization
107279
 
  ** must be complete.  So isInit must not be set until the very end
107280
 
  ** of this routine.
107281
 
  */
107282
 
  if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
107283
 
 
107284
 
  /* Make sure the mutex subsystem is initialized.  If unable to 
107285
 
  ** initialize the mutex subsystem, return early with the error.
107286
 
  ** If the system is so sick that we are unable to allocate a mutex,
107287
 
  ** there is not much SQLite is going to be able to do.
107288
 
  **
107289
 
  ** The mutex subsystem must take care of serializing its own
107290
 
  ** initialization.
107291
 
  */
107292
 
  rc = sqlite3MutexInit();
107293
 
  if( rc ) return rc;
107294
 
 
107295
 
  /* Initialize the malloc() system and the recursive pInitMutex mutex.
107296
 
  ** This operation is protected by the STATIC_MASTER mutex.  Note that
107297
 
  ** MutexAlloc() is called for a static mutex prior to initializing the
107298
 
  ** malloc subsystem - this implies that the allocation of a static
107299
 
  ** mutex must not require support from the malloc subsystem.
107300
 
  */
107301
 
  pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
107302
 
  sqlite3_mutex_enter(pMaster);
107303
 
  sqlite3GlobalConfig.isMutexInit = 1;
107304
 
  if( !sqlite3GlobalConfig.isMallocInit ){
107305
 
    rc = sqlite3MallocInit();
107306
 
  }
107307
 
  if( rc==SQLITE_OK ){
107308
 
    sqlite3GlobalConfig.isMallocInit = 1;
107309
 
    if( !sqlite3GlobalConfig.pInitMutex ){
107310
 
      sqlite3GlobalConfig.pInitMutex =
107311
 
           sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
107312
 
      if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
107313
 
        rc = SQLITE_NOMEM;
107314
 
      }
107315
 
    }
107316
 
  }
107317
 
  if( rc==SQLITE_OK ){
107318
 
    sqlite3GlobalConfig.nRefInitMutex++;
107319
 
  }
107320
 
  sqlite3_mutex_leave(pMaster);
107321
 
 
107322
 
  /* If rc is not SQLITE_OK at this point, then either the malloc
107323
 
  ** subsystem could not be initialized or the system failed to allocate
107324
 
  ** the pInitMutex mutex. Return an error in either case.  */
107325
 
  if( rc!=SQLITE_OK ){
107326
 
    return rc;
107327
 
  }
107328
 
 
107329
 
  /* Do the rest of the initialization under the recursive mutex so
107330
 
  ** that we will be able to handle recursive calls into
107331
 
  ** sqlite3_initialize().  The recursive calls normally come through
107332
 
  ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
107333
 
  ** recursive calls might also be possible.
107334
 
  **
107335
 
  ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
107336
 
  ** to the xInit method, so the xInit method need not be threadsafe.
107337
 
  **
107338
 
  ** The following mutex is what serializes access to the appdef pcache xInit
107339
 
  ** methods.  The sqlite3_pcache_methods.xInit() all is embedded in the
107340
 
  ** call to sqlite3PcacheInitialize().
107341
 
  */
107342
 
  sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
107343
 
  if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
107344
 
    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
107345
 
    sqlite3GlobalConfig.inProgress = 1;
107346
 
    memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
107347
 
    sqlite3RegisterGlobalFunctions();
107348
 
    if( sqlite3GlobalConfig.isPCacheInit==0 ){
107349
 
      rc = sqlite3PcacheInitialize();
107350
 
    }
107351
 
    if( rc==SQLITE_OK ){
107352
 
      sqlite3GlobalConfig.isPCacheInit = 1;
107353
 
      rc = sqlite3OsInit();
107354
 
    }
107355
 
    if( rc==SQLITE_OK ){
107356
 
      sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, 
107357
 
          sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
107358
 
      sqlite3GlobalConfig.isInit = 1;
107359
 
    }
107360
 
    sqlite3GlobalConfig.inProgress = 0;
107361
 
  }
107362
 
  sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
107363
 
 
107364
 
  /* Go back under the static mutex and clean up the recursive
107365
 
  ** mutex to prevent a resource leak.
107366
 
  */
107367
 
  sqlite3_mutex_enter(pMaster);
107368
 
  sqlite3GlobalConfig.nRefInitMutex--;
107369
 
  if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
107370
 
    assert( sqlite3GlobalConfig.nRefInitMutex==0 );
107371
 
    sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
107372
 
    sqlite3GlobalConfig.pInitMutex = 0;
107373
 
  }
107374
 
  sqlite3_mutex_leave(pMaster);
107375
 
 
107376
 
  /* The following is just a sanity check to make sure SQLite has
107377
 
  ** been compiled correctly.  It is important to run this code, but
107378
 
  ** we don't want to run it too often and soak up CPU cycles for no
107379
 
  ** reason.  So we run it once during initialization.
107380
 
  */
107381
 
#ifndef NDEBUG
107382
 
#ifndef SQLITE_OMIT_FLOATING_POINT
107383
 
  /* This section of code's only "output" is via assert() statements. */
107384
 
  if ( rc==SQLITE_OK ){
107385
 
    u64 x = (((u64)1)<<63)-1;
107386
 
    double y;
107387
 
    assert(sizeof(x)==8);
107388
 
    assert(sizeof(x)==sizeof(y));
107389
 
    memcpy(&y, &x, 8);
107390
 
    assert( sqlite3IsNaN(y) );
107391
 
  }
107392
 
#endif
107393
 
#endif
107394
 
 
107395
 
  return rc;
107396
 
}
107397
 
 
107398
 
/*
107399
 
** Undo the effects of sqlite3_initialize().  Must not be called while
107400
 
** there are outstanding database connections or memory allocations or
107401
 
** while any part of SQLite is otherwise in use in any thread.  This
107402
 
** routine is not threadsafe.  But it is safe to invoke this routine
107403
 
** on when SQLite is already shut down.  If SQLite is already shut down
107404
 
** when this routine is invoked, then this routine is a harmless no-op.
107405
 
*/
107406
 
SQLITE_API int sqlite3_shutdown(void){
107407
 
  if( sqlite3GlobalConfig.isInit ){
107408
 
    sqlite3_os_end();
107409
 
    sqlite3_reset_auto_extension();
107410
 
    sqlite3GlobalConfig.isInit = 0;
107411
 
  }
107412
 
  if( sqlite3GlobalConfig.isPCacheInit ){
107413
 
    sqlite3PcacheShutdown();
107414
 
    sqlite3GlobalConfig.isPCacheInit = 0;
107415
 
  }
107416
 
  if( sqlite3GlobalConfig.isMallocInit ){
107417
 
    sqlite3MallocEnd();
107418
 
    sqlite3GlobalConfig.isMallocInit = 0;
107419
 
  }
107420
 
  if( sqlite3GlobalConfig.isMutexInit ){
107421
 
    sqlite3MutexEnd();
107422
 
    sqlite3GlobalConfig.isMutexInit = 0;
107423
 
  }
107424
 
 
107425
 
  return SQLITE_OK;
107426
 
}
107427
 
 
107428
 
/*
107429
 
** This API allows applications to modify the global configuration of
107430
 
** the SQLite library at run-time.
107431
 
**
107432
 
** This routine should only be called when there are no outstanding
107433
 
** database connections or memory allocations.  This routine is not
107434
 
** threadsafe.  Failure to heed these warnings can lead to unpredictable
107435
 
** behavior.
107436
 
*/
107437
 
SQLITE_API int sqlite3_config(int op, ...){
107438
 
  va_list ap;
107439
 
  int rc = SQLITE_OK;
107440
 
 
107441
 
  /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
107442
 
  ** the SQLite library is in use. */
107443
 
  if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
107444
 
 
107445
 
  va_start(ap, op);
107446
 
  switch( op ){
107447
 
 
107448
 
    /* Mutex configuration options are only available in a threadsafe
107449
 
    ** compile. 
107450
 
    */
107451
 
#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0
107452
 
    case SQLITE_CONFIG_SINGLETHREAD: {
107453
 
      /* Disable all mutexing */
107454
 
      sqlite3GlobalConfig.bCoreMutex = 0;
107455
 
      sqlite3GlobalConfig.bFullMutex = 0;
107456
 
      break;
107457
 
    }
107458
 
    case SQLITE_CONFIG_MULTITHREAD: {
107459
 
      /* Disable mutexing of database connections */
107460
 
      /* Enable mutexing of core data structures */
107461
 
      sqlite3GlobalConfig.bCoreMutex = 1;
107462
 
      sqlite3GlobalConfig.bFullMutex = 0;
107463
 
      break;
107464
 
    }
107465
 
    case SQLITE_CONFIG_SERIALIZED: {
107466
 
      /* Enable all mutexing */
107467
 
      sqlite3GlobalConfig.bCoreMutex = 1;
107468
 
      sqlite3GlobalConfig.bFullMutex = 1;
107469
 
      break;
107470
 
    }
107471
 
    case SQLITE_CONFIG_MUTEX: {
107472
 
      /* Specify an alternative mutex implementation */
107473
 
      sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
107474
 
      break;
107475
 
    }
107476
 
    case SQLITE_CONFIG_GETMUTEX: {
107477
 
      /* Retrieve the current mutex implementation */
107478
 
      *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
107479
 
      break;
107480
 
    }
107481
 
#endif
107482
 
 
107483
 
 
107484
 
    case SQLITE_CONFIG_MALLOC: {
107485
 
      /* Specify an alternative malloc implementation */
107486
 
      sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
107487
 
      break;
107488
 
    }
107489
 
    case SQLITE_CONFIG_GETMALLOC: {
107490
 
      /* Retrieve the current malloc() implementation */
107491
 
      if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
107492
 
      *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
107493
 
      break;
107494
 
    }
107495
 
    case SQLITE_CONFIG_MEMSTATUS: {
107496
 
      /* Enable or disable the malloc status collection */
107497
 
      sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
107498
 
      break;
107499
 
    }
107500
 
    case SQLITE_CONFIG_SCRATCH: {
107501
 
      /* Designate a buffer for scratch memory space */
107502
 
      sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
107503
 
      sqlite3GlobalConfig.szScratch = va_arg(ap, int);
107504
 
      sqlite3GlobalConfig.nScratch = va_arg(ap, int);
107505
 
      break;
107506
 
    }
107507
 
    case SQLITE_CONFIG_PAGECACHE: {
107508
 
      /* Designate a buffer for page cache memory space */
107509
 
      sqlite3GlobalConfig.pPage = va_arg(ap, void*);
107510
 
      sqlite3GlobalConfig.szPage = va_arg(ap, int);
107511
 
      sqlite3GlobalConfig.nPage = va_arg(ap, int);
107512
 
      break;
107513
 
    }
107514
 
 
107515
 
    case SQLITE_CONFIG_PCACHE: {
107516
 
      /* Specify an alternative page cache implementation */
107517
 
      sqlite3GlobalConfig.pcache = *va_arg(ap, sqlite3_pcache_methods*);
107518
 
      break;
107519
 
    }
107520
 
 
107521
 
    case SQLITE_CONFIG_GETPCACHE: {
107522
 
      if( sqlite3GlobalConfig.pcache.xInit==0 ){
107523
 
        sqlite3PCacheSetDefault();
107524
 
      }
107525
 
      *va_arg(ap, sqlite3_pcache_methods*) = sqlite3GlobalConfig.pcache;
107526
 
      break;
107527
 
    }
107528
 
 
107529
 
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
107530
 
    case SQLITE_CONFIG_HEAP: {
107531
 
      /* Designate a buffer for heap memory space */
107532
 
      sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
107533
 
      sqlite3GlobalConfig.nHeap = va_arg(ap, int);
107534
 
      sqlite3GlobalConfig.mnReq = va_arg(ap, int);
107535
 
 
107536
 
      if( sqlite3GlobalConfig.mnReq<1 ){
107537
 
        sqlite3GlobalConfig.mnReq = 1;
107538
 
      }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
107539
 
        /* cap min request size at 2^12 */
107540
 
        sqlite3GlobalConfig.mnReq = (1<<12);
107541
 
      }
107542
 
 
107543
 
      if( sqlite3GlobalConfig.pHeap==0 ){
107544
 
        /* If the heap pointer is NULL, then restore the malloc implementation
107545
 
        ** back to NULL pointers too.  This will cause the malloc to go
107546
 
        ** back to its default implementation when sqlite3_initialize() is
107547
 
        ** run.
107548
 
        */
107549
 
        memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
107550
 
      }else{
107551
 
        /* The heap pointer is not NULL, then install one of the
107552
 
        ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
107553
 
        ** ENABLE_MEMSYS5 is defined, return an error.
107554
 
        */
107555
 
#ifdef SQLITE_ENABLE_MEMSYS3
107556
 
        sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
107557
 
#endif
107558
 
#ifdef SQLITE_ENABLE_MEMSYS5
107559
 
        sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
107560
 
#endif
107561
 
      }
107562
 
      break;
107563
 
    }
107564
 
#endif
107565
 
 
107566
 
    case SQLITE_CONFIG_LOOKASIDE: {
107567
 
      sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
107568
 
      sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
107569
 
      break;
107570
 
    }
107571
 
    
107572
 
    /* Record a pointer to the logger funcction and its first argument.
107573
 
    ** The default is NULL.  Logging is disabled if the function pointer is
107574
 
    ** NULL.
107575
 
    */
107576
 
    case SQLITE_CONFIG_LOG: {
107577
 
      /* MSVC is picky about pulling func ptrs from va lists.
107578
 
      ** http://support.microsoft.com/kb/47961
107579
 
      ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
107580
 
      */
107581
 
      typedef void(*LOGFUNC_t)(void*,int,const char*);
107582
 
      sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
107583
 
      sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
107584
 
      break;
107585
 
    }
107586
 
 
107587
 
    default: {
107588
 
      rc = SQLITE_ERROR;
107589
 
      break;
107590
 
    }
107591
 
  }
107592
 
  va_end(ap);
107593
 
  return rc;
107594
 
}
107595
 
 
107596
 
/*
107597
 
** Set up the lookaside buffers for a database connection.
107598
 
** Return SQLITE_OK on success.  
107599
 
** If lookaside is already active, return SQLITE_BUSY.
107600
 
**
107601
 
** The sz parameter is the number of bytes in each lookaside slot.
107602
 
** The cnt parameter is the number of slots.  If pStart is NULL the
107603
 
** space for the lookaside memory is obtained from sqlite3_malloc().
107604
 
** If pStart is not NULL then it is sz*cnt bytes of memory to use for
107605
 
** the lookaside memory.
107606
 
*/
107607
 
static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
107608
 
  void *pStart;
107609
 
  if( db->lookaside.nOut ){
107610
 
    return SQLITE_BUSY;
107611
 
  }
107612
 
  /* Free any existing lookaside buffer for this handle before
107613
 
  ** allocating a new one so we don't have to have space for 
107614
 
  ** both at the same time.
107615
 
  */
107616
 
  if( db->lookaside.bMalloced ){
107617
 
    sqlite3_free(db->lookaside.pStart);
107618
 
  }
107619
 
  /* The size of a lookaside slot needs to be larger than a pointer
107620
 
  ** to be useful.
107621
 
  */
107622
 
  if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
107623
 
  if( cnt<0 ) cnt = 0;
107624
 
  if( sz==0 || cnt==0 ){
107625
 
    sz = 0;
107626
 
    pStart = 0;
107627
 
  }else if( pBuf==0 ){
107628
 
    sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
107629
 
    sqlite3BeginBenignMalloc();
107630
 
    pStart = sqlite3Malloc( sz*cnt );  /* IMP: R-61949-35727 */
107631
 
    sqlite3EndBenignMalloc();
107632
 
  }else{
107633
 
    sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
107634
 
    pStart = pBuf;
107635
 
  }
107636
 
  db->lookaside.pStart = pStart;
107637
 
  db->lookaside.pFree = 0;
107638
 
  db->lookaside.sz = (u16)sz;
107639
 
  if( pStart ){
107640
 
    int i;
107641
 
    LookasideSlot *p;
107642
 
    assert( sz > (int)sizeof(LookasideSlot*) );
107643
 
    p = (LookasideSlot*)pStart;
107644
 
    for(i=cnt-1; i>=0; i--){
107645
 
      p->pNext = db->lookaside.pFree;
107646
 
      db->lookaside.pFree = p;
107647
 
      p = (LookasideSlot*)&((u8*)p)[sz];
107648
 
    }
107649
 
    db->lookaside.pEnd = p;
107650
 
    db->lookaside.bEnabled = 1;
107651
 
    db->lookaside.bMalloced = pBuf==0 ?1:0;
107652
 
  }else{
107653
 
    db->lookaside.pEnd = 0;
107654
 
    db->lookaside.bEnabled = 0;
107655
 
    db->lookaside.bMalloced = 0;
107656
 
  }
107657
 
  return SQLITE_OK;
107658
 
}
107659
 
 
107660
 
/*
107661
 
** Return the mutex associated with a database connection.
107662
 
*/
107663
 
SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
107664
 
  return db->mutex;
107665
 
}
107666
 
 
107667
 
/*
107668
 
** Configuration settings for an individual database connection
107669
 
*/
107670
 
SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
107671
 
  va_list ap;
107672
 
  int rc;
107673
 
  va_start(ap, op);
107674
 
  switch( op ){
107675
 
    case SQLITE_DBCONFIG_LOOKASIDE: {
107676
 
      void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
107677
 
      int sz = va_arg(ap, int);       /* IMP: R-47871-25994 */
107678
 
      int cnt = va_arg(ap, int);      /* IMP: R-04460-53386 */
107679
 
      rc = setupLookaside(db, pBuf, sz, cnt);
107680
 
      break;
107681
 
    }
107682
 
    default: {
107683
 
      static const struct {
107684
 
        int op;      /* The opcode */
107685
 
        u32 mask;    /* Mask of the bit in sqlite3.flags to set/clear */
107686
 
      } aFlagOp[] = {
107687
 
        { SQLITE_DBCONFIG_ENABLE_FKEY,    SQLITE_ForeignKeys    },
107688
 
        { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger  },
107689
 
      };
107690
 
      unsigned int i;
107691
 
      rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
107692
 
      for(i=0; i<ArraySize(aFlagOp); i++){
107693
 
        if( aFlagOp[i].op==op ){
107694
 
          int onoff = va_arg(ap, int);
107695
 
          int *pRes = va_arg(ap, int*);
107696
 
          int oldFlags = db->flags;
107697
 
          if( onoff>0 ){
107698
 
            db->flags |= aFlagOp[i].mask;
107699
 
          }else if( onoff==0 ){
107700
 
            db->flags &= ~aFlagOp[i].mask;
107701
 
          }
107702
 
          if( oldFlags!=db->flags ){
107703
 
            sqlite3ExpirePreparedStatements(db);
107704
 
          }
107705
 
          if( pRes ){
107706
 
            *pRes = (db->flags & aFlagOp[i].mask)!=0;
107707
 
          }
107708
 
          rc = SQLITE_OK;
107709
 
          break;
107710
 
        }
107711
 
      }
107712
 
      break;
107713
 
    }
107714
 
  }
107715
 
  va_end(ap);
107716
 
  return rc;
107717
 
}
107718
 
 
107719
 
 
107720
 
/*
107721
 
** Return true if the buffer z[0..n-1] contains all spaces.
107722
 
*/
107723
 
static int allSpaces(const char *z, int n){
107724
 
  while( n>0 && z[n-1]==' ' ){ n--; }
107725
 
  return n==0;
107726
 
}
107727
 
 
107728
 
/*
107729
 
** This is the default collating function named "BINARY" which is always
107730
 
** available.
107731
 
**
107732
 
** If the padFlag argument is not NULL then space padding at the end
107733
 
** of strings is ignored.  This implements the RTRIM collation.
107734
 
*/
107735
 
static int binCollFunc(
107736
 
  void *padFlag,
107737
 
  int nKey1, const void *pKey1,
107738
 
  int nKey2, const void *pKey2
107739
 
){
107740
 
  int rc, n;
107741
 
  n = nKey1<nKey2 ? nKey1 : nKey2;
107742
 
  rc = memcmp(pKey1, pKey2, n);
107743
 
  if( rc==0 ){
107744
 
    if( padFlag
107745
 
     && allSpaces(((char*)pKey1)+n, nKey1-n)
107746
 
     && allSpaces(((char*)pKey2)+n, nKey2-n)
107747
 
    ){
107748
 
      /* Leave rc unchanged at 0 */
107749
 
    }else{
107750
 
      rc = nKey1 - nKey2;
107751
 
    }
107752
 
  }
107753
 
  return rc;
107754
 
}
107755
 
 
107756
 
/*
107757
 
** Another built-in collating sequence: NOCASE. 
107758
 
**
107759
 
** This collating sequence is intended to be used for "case independant
107760
 
** comparison". SQLite's knowledge of upper and lower case equivalents
107761
 
** extends only to the 26 characters used in the English language.
107762
 
**
107763
 
** At the moment there is only a UTF-8 implementation.
107764
 
*/
107765
 
static int nocaseCollatingFunc(
107766
 
  void *NotUsed,
107767
 
  int nKey1, const void *pKey1,
107768
 
  int nKey2, const void *pKey2
107769
 
){
107770
 
  int r = sqlite3StrNICmp(
107771
 
      (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
107772
 
  UNUSED_PARAMETER(NotUsed);
107773
 
  if( 0==r ){
107774
 
    r = nKey1-nKey2;
107775
 
  }
107776
 
  return r;
107777
 
}
107778
 
 
107779
 
/*
107780
 
** Return the ROWID of the most recent insert
107781
 
*/
107782
 
SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
107783
 
  return db->lastRowid;
107784
 
}
107785
 
 
107786
 
/*
107787
 
** Return the number of changes in the most recent call to sqlite3_exec().
107788
 
*/
107789
 
SQLITE_API int sqlite3_changes(sqlite3 *db){
107790
 
  return db->nChange;
107791
 
}
107792
 
 
107793
 
/*
107794
 
** Return the number of changes since the database handle was opened.
107795
 
*/
107796
 
SQLITE_API int sqlite3_total_changes(sqlite3 *db){
107797
 
  return db->nTotalChange;
107798
 
}
107799
 
 
107800
 
/*
107801
 
** Close all open savepoints. This function only manipulates fields of the
107802
 
** database handle object, it does not close any savepoints that may be open
107803
 
** at the b-tree/pager level.
107804
 
*/
107805
 
SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
107806
 
  while( db->pSavepoint ){
107807
 
    Savepoint *pTmp = db->pSavepoint;
107808
 
    db->pSavepoint = pTmp->pNext;
107809
 
    sqlite3DbFree(db, pTmp);
107810
 
  }
107811
 
  db->nSavepoint = 0;
107812
 
  db->nStatement = 0;
107813
 
  db->isTransactionSavepoint = 0;
107814
 
}
107815
 
 
107816
 
/*
107817
 
** Invoke the destructor function associated with FuncDef p, if any. Except,
107818
 
** if this is not the last copy of the function, do not invoke it. Multiple
107819
 
** copies of a single function are created when create_function() is called
107820
 
** with SQLITE_ANY as the encoding.
107821
 
*/
107822
 
static void functionDestroy(sqlite3 *db, FuncDef *p){
107823
 
  FuncDestructor *pDestructor = p->pDestructor;
107824
 
  if( pDestructor ){
107825
 
    pDestructor->nRef--;
107826
 
    if( pDestructor->nRef==0 ){
107827
 
      pDestructor->xDestroy(pDestructor->pUserData);
107828
 
      sqlite3DbFree(db, pDestructor);
107829
 
    }
107830
 
  }
107831
 
}
107832
 
 
107833
 
/*
107834
 
** Close an existing SQLite database
107835
 
*/
107836
 
SQLITE_API int sqlite3_close(sqlite3 *db){
107837
 
  HashElem *i;                    /* Hash table iterator */
107838
 
  int j;
107839
 
 
107840
 
  if( !db ){
107841
 
    return SQLITE_OK;
107842
 
  }
107843
 
  if( !sqlite3SafetyCheckSickOrOk(db) ){
107844
 
    return SQLITE_MISUSE_BKPT;
107845
 
  }
107846
 
  sqlite3_mutex_enter(db->mutex);
107847
 
 
107848
 
  /* Force xDestroy calls on all virtual tables */
107849
 
  sqlite3ResetInternalSchema(db, -1);
107850
 
 
107851
 
  /* If a transaction is open, the ResetInternalSchema() call above
107852
 
  ** will not have called the xDisconnect() method on any virtual
107853
 
  ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
107854
 
  ** call will do so. We need to do this before the check for active
107855
 
  ** SQL statements below, as the v-table implementation may be storing
107856
 
  ** some prepared statements internally.
107857
 
  */
107858
 
  sqlite3VtabRollback(db);
107859
 
 
107860
 
  /* If there are any outstanding VMs, return SQLITE_BUSY. */
107861
 
  if( db->pVdbe ){
107862
 
    sqlite3Error(db, SQLITE_BUSY, 
107863
 
        "unable to close due to unfinalised statements");
107864
 
    sqlite3_mutex_leave(db->mutex);
107865
 
    return SQLITE_BUSY;
107866
 
  }
107867
 
  assert( sqlite3SafetyCheckSickOrOk(db) );
107868
 
 
107869
 
  for(j=0; j<db->nDb; j++){
107870
 
    Btree *pBt = db->aDb[j].pBt;
107871
 
    if( pBt && sqlite3BtreeIsInBackup(pBt) ){
107872
 
      sqlite3Error(db, SQLITE_BUSY, 
107873
 
          "unable to close due to unfinished backup operation");
107874
 
      sqlite3_mutex_leave(db->mutex);
107875
 
      return SQLITE_BUSY;
107876
 
    }
107877
 
  }
107878
 
 
107879
 
  /* Free any outstanding Savepoint structures. */
107880
 
  sqlite3CloseSavepoints(db);
107881
 
 
107882
 
  for(j=0; j<db->nDb; j++){
107883
 
    struct Db *pDb = &db->aDb[j];
107884
 
    if( pDb->pBt ){
107885
 
      sqlite3BtreeClose(pDb->pBt);
107886
 
      pDb->pBt = 0;
107887
 
      if( j!=1 ){
107888
 
        pDb->pSchema = 0;
107889
 
      }
107890
 
    }
107891
 
  }
107892
 
  sqlite3ResetInternalSchema(db, -1);
107893
 
 
107894
 
  /* Tell the code in notify.c that the connection no longer holds any
107895
 
  ** locks and does not require any further unlock-notify callbacks.
107896
 
  */
107897
 
  sqlite3ConnectionClosed(db);
107898
 
 
107899
 
  assert( db->nDb<=2 );
107900
 
  assert( db->aDb==db->aDbStatic );
107901
 
  for(j=0; j<ArraySize(db->aFunc.a); j++){
107902
 
    FuncDef *pNext, *pHash, *p;
107903
 
    for(p=db->aFunc.a[j]; p; p=pHash){
107904
 
      pHash = p->pHash;
107905
 
      while( p ){
107906
 
        functionDestroy(db, p);
107907
 
        pNext = p->pNext;
107908
 
        sqlite3DbFree(db, p);
107909
 
        p = pNext;
107910
 
      }
107911
 
    }
107912
 
  }
107913
 
  for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
107914
 
    CollSeq *pColl = (CollSeq *)sqliteHashData(i);
107915
 
    /* Invoke any destructors registered for collation sequence user data. */
107916
 
    for(j=0; j<3; j++){
107917
 
      if( pColl[j].xDel ){
107918
 
        pColl[j].xDel(pColl[j].pUser);
107919
 
      }
107920
 
    }
107921
 
    sqlite3DbFree(db, pColl);
107922
 
  }
107923
 
  sqlite3HashClear(&db->aCollSeq);
107924
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
107925
 
  for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
107926
 
    Module *pMod = (Module *)sqliteHashData(i);
107927
 
    if( pMod->xDestroy ){
107928
 
      pMod->xDestroy(pMod->pAux);
107929
 
    }
107930
 
    sqlite3DbFree(db, pMod);
107931
 
  }
107932
 
  sqlite3HashClear(&db->aModule);
107933
 
#endif
107934
 
 
107935
 
  sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
107936
 
  if( db->pErr ){
107937
 
    sqlite3ValueFree(db->pErr);
107938
 
  }
107939
 
  sqlite3CloseExtensions(db);
107940
 
 
107941
 
  db->magic = SQLITE_MAGIC_ERROR;
107942
 
 
107943
 
  /* The temp-database schema is allocated differently from the other schema
107944
 
  ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
107945
 
  ** So it needs to be freed here. Todo: Why not roll the temp schema into
107946
 
  ** the same sqliteMalloc() as the one that allocates the database 
107947
 
  ** structure?
107948
 
  */
107949
 
  sqlite3DbFree(db, db->aDb[1].pSchema);
107950
 
  sqlite3_mutex_leave(db->mutex);
107951
 
  db->magic = SQLITE_MAGIC_CLOSED;
107952
 
  sqlite3_mutex_free(db->mutex);
107953
 
  assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
107954
 
  if( db->lookaside.bMalloced ){
107955
 
    sqlite3_free(db->lookaside.pStart);
107956
 
  }
107957
 
  sqlite3_free(db);
107958
 
  return SQLITE_OK;
107959
 
}
107960
 
 
107961
 
/*
107962
 
** Rollback all database files.
107963
 
*/
107964
 
SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db){
107965
 
  int i;
107966
 
  int inTrans = 0;
107967
 
  assert( sqlite3_mutex_held(db->mutex) );
107968
 
  sqlite3BeginBenignMalloc();
107969
 
  for(i=0; i<db->nDb; i++){
107970
 
    if( db->aDb[i].pBt ){
107971
 
      if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){
107972
 
        inTrans = 1;
107973
 
      }
107974
 
      sqlite3BtreeRollback(db->aDb[i].pBt);
107975
 
      db->aDb[i].inTrans = 0;
107976
 
    }
107977
 
  }
107978
 
  sqlite3VtabRollback(db);
107979
 
  sqlite3EndBenignMalloc();
107980
 
 
107981
 
  if( db->flags&SQLITE_InternChanges ){
107982
 
    sqlite3ExpirePreparedStatements(db);
107983
 
    sqlite3ResetInternalSchema(db, -1);
107984
 
  }
107985
 
 
107986
 
  /* Any deferred constraint violations have now been resolved. */
107987
 
  db->nDeferredCons = 0;
107988
 
 
107989
 
  /* If one has been configured, invoke the rollback-hook callback */
107990
 
  if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
107991
 
    db->xRollbackCallback(db->pRollbackArg);
107992
 
  }
107993
 
}
107994
 
 
107995
 
/*
107996
 
** Return a static string that describes the kind of error specified in the
107997
 
** argument.
107998
 
*/
107999
 
SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
108000
 
  static const char* const aMsg[] = {
108001
 
    /* SQLITE_OK          */ "not an error",
108002
 
    /* SQLITE_ERROR       */ "SQL logic error or missing database",
108003
 
    /* SQLITE_INTERNAL    */ 0,
108004
 
    /* SQLITE_PERM        */ "access permission denied",
108005
 
    /* SQLITE_ABORT       */ "callback requested query abort",
108006
 
    /* SQLITE_BUSY        */ "database is locked",
108007
 
    /* SQLITE_LOCKED      */ "database table is locked",
108008
 
    /* SQLITE_NOMEM       */ "out of memory",
108009
 
    /* SQLITE_READONLY    */ "attempt to write a readonly database",
108010
 
    /* SQLITE_INTERRUPT   */ "interrupted",
108011
 
    /* SQLITE_IOERR       */ "disk I/O error",
108012
 
    /* SQLITE_CORRUPT     */ "database disk image is malformed",
108013
 
    /* SQLITE_NOTFOUND    */ "unknown operation",
108014
 
    /* SQLITE_FULL        */ "database or disk is full",
108015
 
    /* SQLITE_CANTOPEN    */ "unable to open database file",
108016
 
    /* SQLITE_PROTOCOL    */ "locking protocol",
108017
 
    /* SQLITE_EMPTY       */ "table contains no data",
108018
 
    /* SQLITE_SCHEMA      */ "database schema has changed",
108019
 
    /* SQLITE_TOOBIG      */ "string or blob too big",
108020
 
    /* SQLITE_CONSTRAINT  */ "constraint failed",
108021
 
    /* SQLITE_MISMATCH    */ "datatype mismatch",
108022
 
    /* SQLITE_MISUSE      */ "library routine called out of sequence",
108023
 
    /* SQLITE_NOLFS       */ "large file support is disabled",
108024
 
    /* SQLITE_AUTH        */ "authorization denied",
108025
 
    /* SQLITE_FORMAT      */ "auxiliary database format error",
108026
 
    /* SQLITE_RANGE       */ "bind or column index out of range",
108027
 
    /* SQLITE_NOTADB      */ "file is encrypted or is not a database",
108028
 
  };
108029
 
  rc &= 0xff;
108030
 
  if( ALWAYS(rc>=0) && rc<(int)(sizeof(aMsg)/sizeof(aMsg[0])) && aMsg[rc]!=0 ){
108031
 
    return aMsg[rc];
108032
 
  }else{
108033
 
    return "unknown error";
108034
 
  }
108035
 
}
108036
 
 
108037
 
/*
108038
 
** This routine implements a busy callback that sleeps and tries
108039
 
** again until a timeout value is reached.  The timeout value is
108040
 
** an integer number of milliseconds passed in as the first
108041
 
** argument.
108042
 
*/
108043
 
static int sqliteDefaultBusyCallback(
108044
 
 void *ptr,               /* Database connection */
108045
 
 int count                /* Number of times table has been busy */
108046
 
){
108047
 
#if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
108048
 
  static const u8 delays[] =
108049
 
     { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
108050
 
  static const u8 totals[] =
108051
 
     { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
108052
 
# define NDELAY ArraySize(delays)
108053
 
  sqlite3 *db = (sqlite3 *)ptr;
108054
 
  int timeout = db->busyTimeout;
108055
 
  int delay, prior;
108056
 
 
108057
 
  assert( count>=0 );
108058
 
  if( count < NDELAY ){
108059
 
    delay = delays[count];
108060
 
    prior = totals[count];
108061
 
  }else{
108062
 
    delay = delays[NDELAY-1];
108063
 
    prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
108064
 
  }
108065
 
  if( prior + delay > timeout ){
108066
 
    delay = timeout - prior;
108067
 
    if( delay<=0 ) return 0;
108068
 
  }
108069
 
  sqlite3OsSleep(db->pVfs, delay*1000);
108070
 
  return 1;
108071
 
#else
108072
 
  sqlite3 *db = (sqlite3 *)ptr;
108073
 
  int timeout = ((sqlite3 *)ptr)->busyTimeout;
108074
 
  if( (count+1)*1000 > timeout ){
108075
 
    return 0;
108076
 
  }
108077
 
  sqlite3OsSleep(db->pVfs, 1000000);
108078
 
  return 1;
108079
 
#endif
108080
 
}
108081
 
 
108082
 
/*
108083
 
** Invoke the given busy handler.
108084
 
**
108085
 
** This routine is called when an operation failed with a lock.
108086
 
** If this routine returns non-zero, the lock is retried.  If it
108087
 
** returns 0, the operation aborts with an SQLITE_BUSY error.
108088
 
*/
108089
 
SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
108090
 
  int rc;
108091
 
  if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
108092
 
  rc = p->xFunc(p->pArg, p->nBusy);
108093
 
  if( rc==0 ){
108094
 
    p->nBusy = -1;
108095
 
  }else{
108096
 
    p->nBusy++;
108097
 
  }
108098
 
  return rc; 
108099
 
}
108100
 
 
108101
 
/*
108102
 
** This routine sets the busy callback for an Sqlite database to the
108103
 
** given callback function with the given argument.
108104
 
*/
108105
 
SQLITE_API int sqlite3_busy_handler(
108106
 
  sqlite3 *db,
108107
 
  int (*xBusy)(void*,int),
108108
 
  void *pArg
108109
 
){
108110
 
  sqlite3_mutex_enter(db->mutex);
108111
 
  db->busyHandler.xFunc = xBusy;
108112
 
  db->busyHandler.pArg = pArg;
108113
 
  db->busyHandler.nBusy = 0;
108114
 
  sqlite3_mutex_leave(db->mutex);
108115
 
  return SQLITE_OK;
108116
 
}
108117
 
 
108118
 
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
108119
 
/*
108120
 
** This routine sets the progress callback for an Sqlite database to the
108121
 
** given callback function with the given argument. The progress callback will
108122
 
** be invoked every nOps opcodes.
108123
 
*/
108124
 
SQLITE_API void sqlite3_progress_handler(
108125
 
  sqlite3 *db, 
108126
 
  int nOps,
108127
 
  int (*xProgress)(void*), 
108128
 
  void *pArg
108129
 
){
108130
 
  sqlite3_mutex_enter(db->mutex);
108131
 
  if( nOps>0 ){
108132
 
    db->xProgress = xProgress;
108133
 
    db->nProgressOps = nOps;
108134
 
    db->pProgressArg = pArg;
108135
 
  }else{
108136
 
    db->xProgress = 0;
108137
 
    db->nProgressOps = 0;
108138
 
    db->pProgressArg = 0;
108139
 
  }
108140
 
  sqlite3_mutex_leave(db->mutex);
108141
 
}
108142
 
#endif
108143
 
 
108144
 
 
108145
 
/*
108146
 
** This routine installs a default busy handler that waits for the
108147
 
** specified number of milliseconds before returning 0.
108148
 
*/
108149
 
SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
108150
 
  if( ms>0 ){
108151
 
    db->busyTimeout = ms;
108152
 
    sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
108153
 
  }else{
108154
 
    sqlite3_busy_handler(db, 0, 0);
108155
 
  }
108156
 
  return SQLITE_OK;
108157
 
}
108158
 
 
108159
 
/*
108160
 
** Cause any pending operation to stop at its earliest opportunity.
108161
 
*/
108162
 
SQLITE_API void sqlite3_interrupt(sqlite3 *db){
108163
 
  db->u1.isInterrupted = 1;
108164
 
}
108165
 
 
108166
 
 
108167
 
/*
108168
 
** This function is exactly the same as sqlite3_create_function(), except
108169
 
** that it is designed to be called by internal code. The difference is
108170
 
** that if a malloc() fails in sqlite3_create_function(), an error code
108171
 
** is returned and the mallocFailed flag cleared. 
108172
 
*/
108173
 
SQLITE_PRIVATE int sqlite3CreateFunc(
108174
 
  sqlite3 *db,
108175
 
  const char *zFunctionName,
108176
 
  int nArg,
108177
 
  int enc,
108178
 
  void *pUserData,
108179
 
  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
108180
 
  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
108181
 
  void (*xFinal)(sqlite3_context*),
108182
 
  FuncDestructor *pDestructor
108183
 
){
108184
 
  FuncDef *p;
108185
 
  int nName;
108186
 
 
108187
 
  assert( sqlite3_mutex_held(db->mutex) );
108188
 
  if( zFunctionName==0 ||
108189
 
      (xFunc && (xFinal || xStep)) || 
108190
 
      (!xFunc && (xFinal && !xStep)) ||
108191
 
      (!xFunc && (!xFinal && xStep)) ||
108192
 
      (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
108193
 
      (255<(nName = sqlite3Strlen30( zFunctionName))) ){
108194
 
    return SQLITE_MISUSE_BKPT;
108195
 
  }
108196
 
  
108197
 
#ifndef SQLITE_OMIT_UTF16
108198
 
  /* If SQLITE_UTF16 is specified as the encoding type, transform this
108199
 
  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
108200
 
  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
108201
 
  **
108202
 
  ** If SQLITE_ANY is specified, add three versions of the function
108203
 
  ** to the hash table.
108204
 
  */
108205
 
  if( enc==SQLITE_UTF16 ){
108206
 
    enc = SQLITE_UTF16NATIVE;
108207
 
  }else if( enc==SQLITE_ANY ){
108208
 
    int rc;
108209
 
    rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
108210
 
         pUserData, xFunc, xStep, xFinal, pDestructor);
108211
 
    if( rc==SQLITE_OK ){
108212
 
      rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
108213
 
          pUserData, xFunc, xStep, xFinal, pDestructor);
108214
 
    }
108215
 
    if( rc!=SQLITE_OK ){
108216
 
      return rc;
108217
 
    }
108218
 
    enc = SQLITE_UTF16BE;
108219
 
  }
108220
 
#else
108221
 
  enc = SQLITE_UTF8;
108222
 
#endif
108223
 
  
108224
 
  /* Check if an existing function is being overridden or deleted. If so,
108225
 
  ** and there are active VMs, then return SQLITE_BUSY. If a function
108226
 
  ** is being overridden/deleted but there are no active VMs, allow the
108227
 
  ** operation to continue but invalidate all precompiled statements.
108228
 
  */
108229
 
  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
108230
 
  if( p && p->iPrefEnc==enc && p->nArg==nArg ){
108231
 
    if( db->activeVdbeCnt ){
108232
 
      sqlite3Error(db, SQLITE_BUSY, 
108233
 
        "unable to delete/modify user-function due to active statements");
108234
 
      assert( !db->mallocFailed );
108235
 
      return SQLITE_BUSY;
108236
 
    }else{
108237
 
      sqlite3ExpirePreparedStatements(db);
108238
 
    }
108239
 
  }
108240
 
 
108241
 
  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
108242
 
  assert(p || db->mallocFailed);
108243
 
  if( !p ){
108244
 
    return SQLITE_NOMEM;
108245
 
  }
108246
 
 
108247
 
  /* If an older version of the function with a configured destructor is
108248
 
  ** being replaced invoke the destructor function here. */
108249
 
  functionDestroy(db, p);
108250
 
 
108251
 
  if( pDestructor ){
108252
 
    pDestructor->nRef++;
108253
 
  }
108254
 
  p->pDestructor = pDestructor;
108255
 
  p->flags = 0;
108256
 
  p->xFunc = xFunc;
108257
 
  p->xStep = xStep;
108258
 
  p->xFinalize = xFinal;
108259
 
  p->pUserData = pUserData;
108260
 
  p->nArg = (u16)nArg;
108261
 
  return SQLITE_OK;
108262
 
}
108263
 
 
108264
 
/*
108265
 
** Create new user functions.
108266
 
*/
108267
 
SQLITE_API int sqlite3_create_function(
108268
 
  sqlite3 *db,
108269
 
  const char *zFunc,
108270
 
  int nArg,
108271
 
  int enc,
108272
 
  void *p,
108273
 
  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
108274
 
  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
108275
 
  void (*xFinal)(sqlite3_context*)
108276
 
){
108277
 
  return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep,
108278
 
                                    xFinal, 0);
108279
 
}
108280
 
 
108281
 
SQLITE_API int sqlite3_create_function_v2(
108282
 
  sqlite3 *db,
108283
 
  const char *zFunc,
108284
 
  int nArg,
108285
 
  int enc,
108286
 
  void *p,
108287
 
  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
108288
 
  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
108289
 
  void (*xFinal)(sqlite3_context*),
108290
 
  void (*xDestroy)(void *)
108291
 
){
108292
 
  int rc = SQLITE_ERROR;
108293
 
  FuncDestructor *pArg = 0;
108294
 
  sqlite3_mutex_enter(db->mutex);
108295
 
  if( xDestroy ){
108296
 
    pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
108297
 
    if( !pArg ){
108298
 
      xDestroy(p);
108299
 
      goto out;
108300
 
    }
108301
 
    pArg->xDestroy = xDestroy;
108302
 
    pArg->pUserData = p;
108303
 
  }
108304
 
  rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg);
108305
 
  if( pArg && pArg->nRef==0 ){
108306
 
    assert( rc!=SQLITE_OK );
108307
 
    xDestroy(p);
108308
 
    sqlite3DbFree(db, pArg);
108309
 
  }
108310
 
 
108311
 
 out:
108312
 
  rc = sqlite3ApiExit(db, rc);
108313
 
  sqlite3_mutex_leave(db->mutex);
108314
 
  return rc;
108315
 
}
108316
 
 
108317
 
#ifndef SQLITE_OMIT_UTF16
108318
 
SQLITE_API int sqlite3_create_function16(
108319
 
  sqlite3 *db,
108320
 
  const void *zFunctionName,
108321
 
  int nArg,
108322
 
  int eTextRep,
108323
 
  void *p,
108324
 
  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
108325
 
  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
108326
 
  void (*xFinal)(sqlite3_context*)
108327
 
){
108328
 
  int rc;
108329
 
  char *zFunc8;
108330
 
  sqlite3_mutex_enter(db->mutex);
108331
 
  assert( !db->mallocFailed );
108332
 
  zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
108333
 
  rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
108334
 
  sqlite3DbFree(db, zFunc8);
108335
 
  rc = sqlite3ApiExit(db, rc);
108336
 
  sqlite3_mutex_leave(db->mutex);
108337
 
  return rc;
108338
 
}
108339
 
#endif
108340
 
 
108341
 
 
108342
 
/*
108343
 
** Declare that a function has been overloaded by a virtual table.
108344
 
**
108345
 
** If the function already exists as a regular global function, then
108346
 
** this routine is a no-op.  If the function does not exist, then create
108347
 
** a new one that always throws a run-time error.  
108348
 
**
108349
 
** When virtual tables intend to provide an overloaded function, they
108350
 
** should call this routine to make sure the global function exists.
108351
 
** A global function must exist in order for name resolution to work
108352
 
** properly.
108353
 
*/
108354
 
SQLITE_API int sqlite3_overload_function(
108355
 
  sqlite3 *db,
108356
 
  const char *zName,
108357
 
  int nArg
108358
 
){
108359
 
  int nName = sqlite3Strlen30(zName);
108360
 
  int rc;
108361
 
  sqlite3_mutex_enter(db->mutex);
108362
 
  if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
108363
 
    sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
108364
 
                      0, sqlite3InvalidFunction, 0, 0, 0);
108365
 
  }
108366
 
  rc = sqlite3ApiExit(db, SQLITE_OK);
108367
 
  sqlite3_mutex_leave(db->mutex);
108368
 
  return rc;
108369
 
}
108370
 
 
108371
 
#ifndef SQLITE_OMIT_TRACE
108372
 
/*
108373
 
** Register a trace function.  The pArg from the previously registered trace
108374
 
** is returned.  
108375
 
**
108376
 
** A NULL trace function means that no tracing is executes.  A non-NULL
108377
 
** trace is a pointer to a function that is invoked at the start of each
108378
 
** SQL statement.
108379
 
*/
108380
 
SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
108381
 
  void *pOld;
108382
 
  sqlite3_mutex_enter(db->mutex);
108383
 
  pOld = db->pTraceArg;
108384
 
  db->xTrace = xTrace;
108385
 
  db->pTraceArg = pArg;
108386
 
  sqlite3_mutex_leave(db->mutex);
108387
 
  return pOld;
108388
 
}
108389
 
/*
108390
 
** Register a profile function.  The pArg from the previously registered 
108391
 
** profile function is returned.  
108392
 
**
108393
 
** A NULL profile function means that no profiling is executes.  A non-NULL
108394
 
** profile is a pointer to a function that is invoked at the conclusion of
108395
 
** each SQL statement that is run.
108396
 
*/
108397
 
SQLITE_API void *sqlite3_profile(
108398
 
  sqlite3 *db,
108399
 
  void (*xProfile)(void*,const char*,sqlite_uint64),
108400
 
  void *pArg
108401
 
){
108402
 
  void *pOld;
108403
 
  sqlite3_mutex_enter(db->mutex);
108404
 
  pOld = db->pProfileArg;
108405
 
  db->xProfile = xProfile;
108406
 
  db->pProfileArg = pArg;
108407
 
  sqlite3_mutex_leave(db->mutex);
108408
 
  return pOld;
108409
 
}
108410
 
#endif /* SQLITE_OMIT_TRACE */
108411
 
 
108412
 
/*** EXPERIMENTAL ***
108413
 
**
108414
 
** Register a function to be invoked when a transaction comments.
108415
 
** If the invoked function returns non-zero, then the commit becomes a
108416
 
** rollback.
108417
 
*/
108418
 
SQLITE_API void *sqlite3_commit_hook(
108419
 
  sqlite3 *db,              /* Attach the hook to this database */
108420
 
  int (*xCallback)(void*),  /* Function to invoke on each commit */
108421
 
  void *pArg                /* Argument to the function */
108422
 
){
108423
 
  void *pOld;
108424
 
  sqlite3_mutex_enter(db->mutex);
108425
 
  pOld = db->pCommitArg;
108426
 
  db->xCommitCallback = xCallback;
108427
 
  db->pCommitArg = pArg;
108428
 
  sqlite3_mutex_leave(db->mutex);
108429
 
  return pOld;
108430
 
}
108431
 
 
108432
 
/*
108433
 
** Register a callback to be invoked each time a row is updated,
108434
 
** inserted or deleted using this database connection.
108435
 
*/
108436
 
SQLITE_API void *sqlite3_update_hook(
108437
 
  sqlite3 *db,              /* Attach the hook to this database */
108438
 
  void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
108439
 
  void *pArg                /* Argument to the function */
108440
 
){
108441
 
  void *pRet;
108442
 
  sqlite3_mutex_enter(db->mutex);
108443
 
  pRet = db->pUpdateArg;
108444
 
  db->xUpdateCallback = xCallback;
108445
 
  db->pUpdateArg = pArg;
108446
 
  sqlite3_mutex_leave(db->mutex);
108447
 
  return pRet;
108448
 
}
108449
 
 
108450
 
/*
108451
 
** Register a callback to be invoked each time a transaction is rolled
108452
 
** back by this database connection.
108453
 
*/
108454
 
SQLITE_API void *sqlite3_rollback_hook(
108455
 
  sqlite3 *db,              /* Attach the hook to this database */
108456
 
  void (*xCallback)(void*), /* Callback function */
108457
 
  void *pArg                /* Argument to the function */
108458
 
){
108459
 
  void *pRet;
108460
 
  sqlite3_mutex_enter(db->mutex);
108461
 
  pRet = db->pRollbackArg;
108462
 
  db->xRollbackCallback = xCallback;
108463
 
  db->pRollbackArg = pArg;
108464
 
  sqlite3_mutex_leave(db->mutex);
108465
 
  return pRet;
108466
 
}
108467
 
 
108468
 
#ifndef SQLITE_OMIT_WAL
108469
 
/*
108470
 
** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
108471
 
** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
108472
 
** is greater than sqlite3.pWalArg cast to an integer (the value configured by
108473
 
** wal_autocheckpoint()).
108474
 
*/ 
108475
 
SQLITE_PRIVATE int sqlite3WalDefaultHook(
108476
 
  void *pClientData,     /* Argument */
108477
 
  sqlite3 *db,           /* Connection */
108478
 
  const char *zDb,       /* Database */
108479
 
  int nFrame             /* Size of WAL */
108480
 
){
108481
 
  if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
108482
 
    sqlite3BeginBenignMalloc();
108483
 
    sqlite3_wal_checkpoint(db, zDb);
108484
 
    sqlite3EndBenignMalloc();
108485
 
  }
108486
 
  return SQLITE_OK;
108487
 
}
108488
 
#endif /* SQLITE_OMIT_WAL */
108489
 
 
108490
 
/*
108491
 
** Configure an sqlite3_wal_hook() callback to automatically checkpoint
108492
 
** a database after committing a transaction if there are nFrame or
108493
 
** more frames in the log file. Passing zero or a negative value as the
108494
 
** nFrame parameter disables automatic checkpoints entirely.
108495
 
**
108496
 
** The callback registered by this function replaces any existing callback
108497
 
** registered using sqlite3_wal_hook(). Likewise, registering a callback
108498
 
** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
108499
 
** configured by this function.
108500
 
*/
108501
 
SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
108502
 
#ifdef SQLITE_OMIT_WAL
108503
 
  UNUSED_PARAMETER(db);
108504
 
  UNUSED_PARAMETER(nFrame);
108505
 
#else
108506
 
  if( nFrame>0 ){
108507
 
    sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
108508
 
  }else{
108509
 
    sqlite3_wal_hook(db, 0, 0);
108510
 
  }
108511
 
#endif
108512
 
  return SQLITE_OK;
108513
 
}
108514
 
 
108515
 
/*
108516
 
** Register a callback to be invoked each time a transaction is written
108517
 
** into the write-ahead-log by this database connection.
108518
 
*/
108519
 
SQLITE_API void *sqlite3_wal_hook(
108520
 
  sqlite3 *db,                    /* Attach the hook to this db handle */
108521
 
  int(*xCallback)(void *, sqlite3*, const char*, int),
108522
 
  void *pArg                      /* First argument passed to xCallback() */
108523
 
){
108524
 
#ifndef SQLITE_OMIT_WAL
108525
 
  void *pRet;
108526
 
  sqlite3_mutex_enter(db->mutex);
108527
 
  pRet = db->pWalArg;
108528
 
  db->xWalCallback = xCallback;
108529
 
  db->pWalArg = pArg;
108530
 
  sqlite3_mutex_leave(db->mutex);
108531
 
  return pRet;
108532
 
#else
108533
 
  return 0;
108534
 
#endif
108535
 
}
108536
 
 
108537
 
/*
108538
 
** Checkpoint database zDb.
108539
 
*/
108540
 
SQLITE_API int sqlite3_wal_checkpoint_v2(
108541
 
  sqlite3 *db,                    /* Database handle */
108542
 
  const char *zDb,                /* Name of attached database (or NULL) */
108543
 
  int eMode,                      /* SQLITE_CHECKPOINT_* value */
108544
 
  int *pnLog,                     /* OUT: Size of WAL log in frames */
108545
 
  int *pnCkpt                     /* OUT: Total number of frames checkpointed */
108546
 
){
108547
 
#ifdef SQLITE_OMIT_WAL
108548
 
  return SQLITE_OK;
108549
 
#else
108550
 
  int rc;                         /* Return code */
108551
 
  int iDb = SQLITE_MAX_ATTACHED;  /* sqlite3.aDb[] index of db to checkpoint */
108552
 
 
108553
 
  /* Initialize the output variables to -1 in case an error occurs. */
108554
 
  if( pnLog ) *pnLog = -1;
108555
 
  if( pnCkpt ) *pnCkpt = -1;
108556
 
 
108557
 
  assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE );
108558
 
  assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART );
108559
 
  assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART );
108560
 
  if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){
108561
 
    return SQLITE_MISUSE;
108562
 
  }
108563
 
 
108564
 
  sqlite3_mutex_enter(db->mutex);
108565
 
  if( zDb && zDb[0] ){
108566
 
    iDb = sqlite3FindDbName(db, zDb);
108567
 
  }
108568
 
  if( iDb<0 ){
108569
 
    rc = SQLITE_ERROR;
108570
 
    sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
108571
 
  }else{
108572
 
    rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
108573
 
    sqlite3Error(db, rc, 0);
108574
 
  }
108575
 
  rc = sqlite3ApiExit(db, rc);
108576
 
  sqlite3_mutex_leave(db->mutex);
108577
 
  return rc;
108578
 
#endif
108579
 
}
108580
 
 
108581
 
 
108582
 
/*
108583
 
** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
108584
 
** to contains a zero-length string, all attached databases are 
108585
 
** checkpointed.
108586
 
*/
108587
 
SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
108588
 
  return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0);
108589
 
}
108590
 
 
108591
 
#ifndef SQLITE_OMIT_WAL
108592
 
/*
108593
 
** Run a checkpoint on database iDb. This is a no-op if database iDb is
108594
 
** not currently open in WAL mode.
108595
 
**
108596
 
** If a transaction is open on the database being checkpointed, this 
108597
 
** function returns SQLITE_LOCKED and a checkpoint is not attempted. If 
108598
 
** an error occurs while running the checkpoint, an SQLite error code is 
108599
 
** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
108600
 
**
108601
 
** The mutex on database handle db should be held by the caller. The mutex
108602
 
** associated with the specific b-tree being checkpointed is taken by
108603
 
** this function while the checkpoint is running.
108604
 
**
108605
 
** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
108606
 
** checkpointed. If an error is encountered it is returned immediately -
108607
 
** no attempt is made to checkpoint any remaining databases.
108608
 
**
108609
 
** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
108610
 
*/
108611
 
SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
108612
 
  int rc = SQLITE_OK;             /* Return code */
108613
 
  int i;                          /* Used to iterate through attached dbs */
108614
 
  int bBusy = 0;                  /* True if SQLITE_BUSY has been encountered */
108615
 
 
108616
 
  assert( sqlite3_mutex_held(db->mutex) );
108617
 
  assert( !pnLog || *pnLog==-1 );
108618
 
  assert( !pnCkpt || *pnCkpt==-1 );
108619
 
 
108620
 
  for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
108621
 
    if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
108622
 
      rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
108623
 
      pnLog = 0;
108624
 
      pnCkpt = 0;
108625
 
      if( rc==SQLITE_BUSY ){
108626
 
        bBusy = 1;
108627
 
        rc = SQLITE_OK;
108628
 
      }
108629
 
    }
108630
 
  }
108631
 
 
108632
 
  return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
108633
 
}
108634
 
#endif /* SQLITE_OMIT_WAL */
108635
 
 
108636
 
/*
108637
 
** This function returns true if main-memory should be used instead of
108638
 
** a temporary file for transient pager files and statement journals.
108639
 
** The value returned depends on the value of db->temp_store (runtime
108640
 
** parameter) and the compile time value of SQLITE_TEMP_STORE. The
108641
 
** following table describes the relationship between these two values
108642
 
** and this functions return value.
108643
 
**
108644
 
**   SQLITE_TEMP_STORE     db->temp_store     Location of temporary database
108645
 
**   -----------------     --------------     ------------------------------
108646
 
**   0                     any                file      (return 0)
108647
 
**   1                     1                  file      (return 0)
108648
 
**   1                     2                  memory    (return 1)
108649
 
**   1                     0                  file      (return 0)
108650
 
**   2                     1                  file      (return 0)
108651
 
**   2                     2                  memory    (return 1)
108652
 
**   2                     0                  memory    (return 1)
108653
 
**   3                     any                memory    (return 1)
108654
 
*/
108655
 
SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
108656
 
#if SQLITE_TEMP_STORE==1
108657
 
  return ( db->temp_store==2 );
108658
 
#endif
108659
 
#if SQLITE_TEMP_STORE==2
108660
 
  return ( db->temp_store!=1 );
108661
 
#endif
108662
 
#if SQLITE_TEMP_STORE==3
108663
 
  return 1;
108664
 
#endif
108665
 
#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
108666
 
  return 0;
108667
 
#endif
108668
 
}
108669
 
 
108670
 
/*
108671
 
** Return UTF-8 encoded English language explanation of the most recent
108672
 
** error.
108673
 
*/
108674
 
SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
108675
 
  const char *z;
108676
 
  if( !db ){
108677
 
    return sqlite3ErrStr(SQLITE_NOMEM);
108678
 
  }
108679
 
  if( !sqlite3SafetyCheckSickOrOk(db) ){
108680
 
    return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
108681
 
  }
108682
 
  sqlite3_mutex_enter(db->mutex);
108683
 
  if( db->mallocFailed ){
108684
 
    z = sqlite3ErrStr(SQLITE_NOMEM);
108685
 
  }else{
108686
 
    z = (char*)sqlite3_value_text(db->pErr);
108687
 
    assert( !db->mallocFailed );
108688
 
    if( z==0 ){
108689
 
      z = sqlite3ErrStr(db->errCode);
108690
 
    }
108691
 
  }
108692
 
  sqlite3_mutex_leave(db->mutex);
108693
 
  return z;
108694
 
}
108695
 
 
108696
 
#ifndef SQLITE_OMIT_UTF16
108697
 
/*
108698
 
** Return UTF-16 encoded English language explanation of the most recent
108699
 
** error.
108700
 
*/
108701
 
SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
108702
 
  static const u16 outOfMem[] = {
108703
 
    'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
108704
 
  };
108705
 
  static const u16 misuse[] = {
108706
 
    'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ', 
108707
 
    'r', 'o', 'u', 't', 'i', 'n', 'e', ' ', 
108708
 
    'c', 'a', 'l', 'l', 'e', 'd', ' ', 
108709
 
    'o', 'u', 't', ' ', 
108710
 
    'o', 'f', ' ', 
108711
 
    's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
108712
 
  };
108713
 
 
108714
 
  const void *z;
108715
 
  if( !db ){
108716
 
    return (void *)outOfMem;
108717
 
  }
108718
 
  if( !sqlite3SafetyCheckSickOrOk(db) ){
108719
 
    return (void *)misuse;
108720
 
  }
108721
 
  sqlite3_mutex_enter(db->mutex);
108722
 
  if( db->mallocFailed ){
108723
 
    z = (void *)outOfMem;
108724
 
  }else{
108725
 
    z = sqlite3_value_text16(db->pErr);
108726
 
    if( z==0 ){
108727
 
      sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
108728
 
           SQLITE_UTF8, SQLITE_STATIC);
108729
 
      z = sqlite3_value_text16(db->pErr);
108730
 
    }
108731
 
    /* A malloc() may have failed within the call to sqlite3_value_text16()
108732
 
    ** above. If this is the case, then the db->mallocFailed flag needs to
108733
 
    ** be cleared before returning. Do this directly, instead of via
108734
 
    ** sqlite3ApiExit(), to avoid setting the database handle error message.
108735
 
    */
108736
 
    db->mallocFailed = 0;
108737
 
  }
108738
 
  sqlite3_mutex_leave(db->mutex);
108739
 
  return z;
108740
 
}
108741
 
#endif /* SQLITE_OMIT_UTF16 */
108742
 
 
108743
 
/*
108744
 
** Return the most recent error code generated by an SQLite routine. If NULL is
108745
 
** passed to this function, we assume a malloc() failed during sqlite3_open().
108746
 
*/
108747
 
SQLITE_API int sqlite3_errcode(sqlite3 *db){
108748
 
  if( db && !sqlite3SafetyCheckSickOrOk(db) ){
108749
 
    return SQLITE_MISUSE_BKPT;
108750
 
  }
108751
 
  if( !db || db->mallocFailed ){
108752
 
    return SQLITE_NOMEM;
108753
 
  }
108754
 
  return db->errCode & db->errMask;
108755
 
}
108756
 
SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
108757
 
  if( db && !sqlite3SafetyCheckSickOrOk(db) ){
108758
 
    return SQLITE_MISUSE_BKPT;
108759
 
  }
108760
 
  if( !db || db->mallocFailed ){
108761
 
    return SQLITE_NOMEM;
108762
 
  }
108763
 
  return db->errCode;
108764
 
}
108765
 
 
108766
 
/*
108767
 
** Create a new collating function for database "db".  The name is zName
108768
 
** and the encoding is enc.
108769
 
*/
108770
 
static int createCollation(
108771
 
  sqlite3* db,
108772
 
  const char *zName, 
108773
 
  u8 enc,
108774
 
  u8 collType,
108775
 
  void* pCtx,
108776
 
  int(*xCompare)(void*,int,const void*,int,const void*),
108777
 
  void(*xDel)(void*)
108778
 
){
108779
 
  CollSeq *pColl;
108780
 
  int enc2;
108781
 
  int nName = sqlite3Strlen30(zName);
108782
 
  
108783
 
  assert( sqlite3_mutex_held(db->mutex) );
108784
 
 
108785
 
  /* If SQLITE_UTF16 is specified as the encoding type, transform this
108786
 
  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
108787
 
  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
108788
 
  */
108789
 
  enc2 = enc;
108790
 
  testcase( enc2==SQLITE_UTF16 );
108791
 
  testcase( enc2==SQLITE_UTF16_ALIGNED );
108792
 
  if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
108793
 
    enc2 = SQLITE_UTF16NATIVE;
108794
 
  }
108795
 
  if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
108796
 
    return SQLITE_MISUSE_BKPT;
108797
 
  }
108798
 
 
108799
 
  /* Check if this call is removing or replacing an existing collation 
108800
 
  ** sequence. If so, and there are active VMs, return busy. If there
108801
 
  ** are no active VMs, invalidate any pre-compiled statements.
108802
 
  */
108803
 
  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
108804
 
  if( pColl && pColl->xCmp ){
108805
 
    if( db->activeVdbeCnt ){
108806
 
      sqlite3Error(db, SQLITE_BUSY, 
108807
 
        "unable to delete/modify collation sequence due to active statements");
108808
 
      return SQLITE_BUSY;
108809
 
    }
108810
 
    sqlite3ExpirePreparedStatements(db);
108811
 
 
108812
 
    /* If collation sequence pColl was created directly by a call to
108813
 
    ** sqlite3_create_collation, and not generated by synthCollSeq(),
108814
 
    ** then any copies made by synthCollSeq() need to be invalidated.
108815
 
    ** Also, collation destructor - CollSeq.xDel() - function may need
108816
 
    ** to be called.
108817
 
    */ 
108818
 
    if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
108819
 
      CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
108820
 
      int j;
108821
 
      for(j=0; j<3; j++){
108822
 
        CollSeq *p = &aColl[j];
108823
 
        if( p->enc==pColl->enc ){
108824
 
          if( p->xDel ){
108825
 
            p->xDel(p->pUser);
108826
 
          }
108827
 
          p->xCmp = 0;
108828
 
        }
108829
 
      }
108830
 
    }
108831
 
  }
108832
 
 
108833
 
  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
108834
 
  if( pColl==0 ) return SQLITE_NOMEM;
108835
 
  pColl->xCmp = xCompare;
108836
 
  pColl->pUser = pCtx;
108837
 
  pColl->xDel = xDel;
108838
 
  pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
108839
 
  pColl->type = collType;
108840
 
  sqlite3Error(db, SQLITE_OK, 0);
108841
 
  return SQLITE_OK;
108842
 
}
108843
 
 
108844
 
 
108845
 
/*
108846
 
** This array defines hard upper bounds on limit values.  The
108847
 
** initializer must be kept in sync with the SQLITE_LIMIT_*
108848
 
** #defines in sqlite3.h.
108849
 
*/
108850
 
static const int aHardLimit[] = {
108851
 
  SQLITE_MAX_LENGTH,
108852
 
  SQLITE_MAX_SQL_LENGTH,
108853
 
  SQLITE_MAX_COLUMN,
108854
 
  SQLITE_MAX_EXPR_DEPTH,
108855
 
  SQLITE_MAX_COMPOUND_SELECT,
108856
 
  SQLITE_MAX_VDBE_OP,
108857
 
  SQLITE_MAX_FUNCTION_ARG,
108858
 
  SQLITE_MAX_ATTACHED,
108859
 
  SQLITE_MAX_LIKE_PATTERN_LENGTH,
108860
 
  SQLITE_MAX_VARIABLE_NUMBER,
108861
 
  SQLITE_MAX_TRIGGER_DEPTH,
108862
 
};
108863
 
 
108864
 
/*
108865
 
** Make sure the hard limits are set to reasonable values
108866
 
*/
108867
 
#if SQLITE_MAX_LENGTH<100
108868
 
# error SQLITE_MAX_LENGTH must be at least 100
108869
 
#endif
108870
 
#if SQLITE_MAX_SQL_LENGTH<100
108871
 
# error SQLITE_MAX_SQL_LENGTH must be at least 100
108872
 
#endif
108873
 
#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
108874
 
# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
108875
 
#endif
108876
 
#if SQLITE_MAX_COMPOUND_SELECT<2
108877
 
# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
108878
 
#endif
108879
 
#if SQLITE_MAX_VDBE_OP<40
108880
 
# error SQLITE_MAX_VDBE_OP must be at least 40
108881
 
#endif
108882
 
#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
108883
 
# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
108884
 
#endif
108885
 
#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62
108886
 
# error SQLITE_MAX_ATTACHED must be between 0 and 62
108887
 
#endif
108888
 
#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
108889
 
# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
108890
 
#endif
108891
 
#if SQLITE_MAX_COLUMN>32767
108892
 
# error SQLITE_MAX_COLUMN must not exceed 32767
108893
 
#endif
108894
 
#if SQLITE_MAX_TRIGGER_DEPTH<1
108895
 
# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
108896
 
#endif
108897
 
 
108898
 
 
108899
 
/*
108900
 
** Change the value of a limit.  Report the old value.
108901
 
** If an invalid limit index is supplied, report -1.
108902
 
** Make no changes but still report the old value if the
108903
 
** new limit is negative.
108904
 
**
108905
 
** A new lower limit does not shrink existing constructs.
108906
 
** It merely prevents new constructs that exceed the limit
108907
 
** from forming.
108908
 
*/
108909
 
SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
108910
 
  int oldLimit;
108911
 
 
108912
 
 
108913
 
  /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
108914
 
  ** there is a hard upper bound set at compile-time by a C preprocessor
108915
 
  ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
108916
 
  ** "_MAX_".)
108917
 
  */
108918
 
  assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
108919
 
  assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
108920
 
  assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
108921
 
  assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
108922
 
  assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
108923
 
  assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
108924
 
  assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
108925
 
  assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
108926
 
  assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
108927
 
                                               SQLITE_MAX_LIKE_PATTERN_LENGTH );
108928
 
  assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
108929
 
  assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
108930
 
  assert( SQLITE_LIMIT_TRIGGER_DEPTH==(SQLITE_N_LIMIT-1) );
108931
 
 
108932
 
 
108933
 
  if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
108934
 
    return -1;
108935
 
  }
108936
 
  oldLimit = db->aLimit[limitId];
108937
 
  if( newLimit>=0 ){                   /* IMP: R-52476-28732 */
108938
 
    if( newLimit>aHardLimit[limitId] ){
108939
 
      newLimit = aHardLimit[limitId];  /* IMP: R-51463-25634 */
108940
 
    }
108941
 
    db->aLimit[limitId] = newLimit;
108942
 
  }
108943
 
  return oldLimit;                     /* IMP: R-53341-35419 */
108944
 
}
108945
 
 
108946
 
/*
108947
 
** This routine does the work of opening a database on behalf of
108948
 
** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"  
108949
 
** is UTF-8 encoded.
108950
 
*/
108951
 
static int openDatabase(
108952
 
  const char *zFilename, /* Database filename UTF-8 encoded */
108953
 
  sqlite3 **ppDb,        /* OUT: Returned database handle */
108954
 
  unsigned flags,        /* Operational flags */
108955
 
  const char *zVfs       /* Name of the VFS to use */
108956
 
){
108957
 
  sqlite3 *db;
108958
 
  int rc;
108959
 
  int isThreadsafe;
108960
 
 
108961
 
  *ppDb = 0;
108962
 
#ifndef SQLITE_OMIT_AUTOINIT
108963
 
  rc = sqlite3_initialize();
108964
 
  if( rc ) return rc;
108965
 
#endif
108966
 
 
108967
 
  /* Only allow sensible combinations of bits in the flags argument.  
108968
 
  ** Throw an error if any non-sense combination is used.  If we
108969
 
  ** do not block illegal combinations here, it could trigger
108970
 
  ** assert() statements in deeper layers.  Sensible combinations
108971
 
  ** are:
108972
 
  **
108973
 
  **  1:  SQLITE_OPEN_READONLY
108974
 
  **  2:  SQLITE_OPEN_READWRITE
108975
 
  **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
108976
 
  */
108977
 
  assert( SQLITE_OPEN_READONLY  == 0x01 );
108978
 
  assert( SQLITE_OPEN_READWRITE == 0x02 );
108979
 
  assert( SQLITE_OPEN_CREATE    == 0x04 );
108980
 
  testcase( (1<<(flags&7))==0x02 ); /* READONLY */
108981
 
  testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
108982
 
  testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
108983
 
  if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE;
108984
 
 
108985
 
  if( sqlite3GlobalConfig.bCoreMutex==0 ){
108986
 
    isThreadsafe = 0;
108987
 
  }else if( flags & SQLITE_OPEN_NOMUTEX ){
108988
 
    isThreadsafe = 0;
108989
 
  }else if( flags & SQLITE_OPEN_FULLMUTEX ){
108990
 
    isThreadsafe = 1;
108991
 
  }else{
108992
 
    isThreadsafe = sqlite3GlobalConfig.bFullMutex;
108993
 
  }
108994
 
  if( flags & SQLITE_OPEN_PRIVATECACHE ){
108995
 
    flags &= ~SQLITE_OPEN_SHAREDCACHE;
108996
 
  }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
108997
 
    flags |= SQLITE_OPEN_SHAREDCACHE;
108998
 
  }
108999
 
 
109000
 
  /* Remove harmful bits from the flags parameter
109001
 
  **
109002
 
  ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
109003
 
  ** dealt with in the previous code block.  Besides these, the only
109004
 
  ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
109005
 
  ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
109006
 
  ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits.  Silently mask
109007
 
  ** off all other flags.
109008
 
  */
109009
 
  flags &=  ~( SQLITE_OPEN_DELETEONCLOSE |
109010
 
               SQLITE_OPEN_EXCLUSIVE |
109011
 
               SQLITE_OPEN_MAIN_DB |
109012
 
               SQLITE_OPEN_TEMP_DB | 
109013
 
               SQLITE_OPEN_TRANSIENT_DB | 
109014
 
               SQLITE_OPEN_MAIN_JOURNAL | 
109015
 
               SQLITE_OPEN_TEMP_JOURNAL | 
109016
 
               SQLITE_OPEN_SUBJOURNAL | 
109017
 
               SQLITE_OPEN_MASTER_JOURNAL |
109018
 
               SQLITE_OPEN_NOMUTEX |
109019
 
               SQLITE_OPEN_FULLMUTEX |
109020
 
               SQLITE_OPEN_WAL
109021
 
             );
109022
 
 
109023
 
  /* Allocate the sqlite data structure */
109024
 
  db = sqlite3MallocZero( sizeof(sqlite3) );
109025
 
  if( db==0 ) goto opendb_out;
109026
 
  if( isThreadsafe ){
109027
 
    db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
109028
 
    if( db->mutex==0 ){
109029
 
      sqlite3_free(db);
109030
 
      db = 0;
109031
 
      goto opendb_out;
109032
 
    }
109033
 
  }
109034
 
  sqlite3_mutex_enter(db->mutex);
109035
 
  db->errMask = 0xff;
109036
 
  db->nDb = 2;
109037
 
  db->magic = SQLITE_MAGIC_BUSY;
109038
 
  db->aDb = db->aDbStatic;
109039
 
 
109040
 
  assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
109041
 
  memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
109042
 
  db->autoCommit = 1;
109043
 
  db->nextAutovac = -1;
109044
 
  db->nextPagesize = 0;
109045
 
  db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex | SQLITE_EnableTrigger
109046
 
#if SQLITE_DEFAULT_FILE_FORMAT<4
109047
 
                 | SQLITE_LegacyFileFmt
109048
 
#endif
109049
 
#ifdef SQLITE_ENABLE_LOAD_EXTENSION
109050
 
                 | SQLITE_LoadExtension
109051
 
#endif
109052
 
#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
109053
 
                 | SQLITE_RecTriggers
109054
 
#endif
109055
 
#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
109056
 
                 | SQLITE_ForeignKeys
109057
 
#endif
109058
 
      ;
109059
 
  sqlite3HashInit(&db->aCollSeq);
109060
 
#ifndef SQLITE_OMIT_VIRTUALTABLE
109061
 
  sqlite3HashInit(&db->aModule);
109062
 
#endif
109063
 
 
109064
 
  db->pVfs = sqlite3_vfs_find(zVfs);
109065
 
  if( !db->pVfs ){
109066
 
    rc = SQLITE_ERROR;
109067
 
    sqlite3Error(db, rc, "no such vfs: %s", zVfs);
109068
 
    goto opendb_out;
109069
 
  }
109070
 
 
109071
 
  /* Add the default collation sequence BINARY. BINARY works for both UTF-8
109072
 
  ** and UTF-16, so add a version for each to avoid any unnecessary
109073
 
  ** conversions. The only error that can occur here is a malloc() failure.
109074
 
  */
109075
 
  createCollation(db, "BINARY", SQLITE_UTF8, SQLITE_COLL_BINARY, 0,
109076
 
                  binCollFunc, 0);
109077
 
  createCollation(db, "BINARY", SQLITE_UTF16BE, SQLITE_COLL_BINARY, 0,
109078
 
                  binCollFunc, 0);
109079
 
  createCollation(db, "BINARY", SQLITE_UTF16LE, SQLITE_COLL_BINARY, 0,
109080
 
                  binCollFunc, 0);
109081
 
  createCollation(db, "RTRIM", SQLITE_UTF8, SQLITE_COLL_USER, (void*)1,
109082
 
                  binCollFunc, 0);
109083
 
  if( db->mallocFailed ){
109084
 
    goto opendb_out;
109085
 
  }
109086
 
  db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
109087
 
  assert( db->pDfltColl!=0 );
109088
 
 
109089
 
  /* Also add a UTF-8 case-insensitive collation sequence. */
109090
 
  createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0,
109091
 
                  nocaseCollatingFunc, 0);
109092
 
 
109093
 
  /* Open the backend database driver */
109094
 
  db->openFlags = flags;
109095
 
  rc = sqlite3BtreeOpen(zFilename, db, &db->aDb[0].pBt, 0,
109096
 
                        flags | SQLITE_OPEN_MAIN_DB);
109097
 
  if( rc!=SQLITE_OK ){
109098
 
    if( rc==SQLITE_IOERR_NOMEM ){
109099
 
      rc = SQLITE_NOMEM;
109100
 
    }
109101
 
    sqlite3Error(db, rc, 0);
109102
 
    goto opendb_out;
109103
 
  }
109104
 
  db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
109105
 
  db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
109106
 
 
109107
 
 
109108
 
  /* The default safety_level for the main database is 'full'; for the temp
109109
 
  ** database it is 'NONE'. This matches the pager layer defaults.  
109110
 
  */
109111
 
  db->aDb[0].zName = "main";
109112
 
  db->aDb[0].safety_level = 3;
109113
 
  db->aDb[1].zName = "temp";
109114
 
  db->aDb[1].safety_level = 1;
109115
 
 
109116
 
  db->magic = SQLITE_MAGIC_OPEN;
109117
 
  if( db->mallocFailed ){
109118
 
    goto opendb_out;
109119
 
  }
109120
 
 
109121
 
  /* Register all built-in functions, but do not attempt to read the
109122
 
  ** database schema yet. This is delayed until the first time the database
109123
 
  ** is accessed.
109124
 
  */
109125
 
  sqlite3Error(db, SQLITE_OK, 0);
109126
 
  sqlite3RegisterBuiltinFunctions(db);
109127
 
 
109128
 
  /* Load automatic extensions - extensions that have been registered
109129
 
  ** using the sqlite3_automatic_extension() API.
109130
 
  */
109131
 
  sqlite3AutoLoadExtensions(db);
109132
 
  rc = sqlite3_errcode(db);
109133
 
  if( rc!=SQLITE_OK ){
109134
 
    goto opendb_out;
109135
 
  }
109136
 
 
109137
 
#ifdef SQLITE_ENABLE_FTS1
109138
 
  if( !db->mallocFailed ){
109139
 
    extern int sqlite3Fts1Init(sqlite3*);
109140
 
    rc = sqlite3Fts1Init(db);
109141
 
  }
109142
 
#endif
109143
 
 
109144
 
#ifdef SQLITE_ENABLE_FTS2
109145
 
  if( !db->mallocFailed && rc==SQLITE_OK ){
109146
 
    extern int sqlite3Fts2Init(sqlite3*);
109147
 
    rc = sqlite3Fts2Init(db);
109148
 
  }
109149
 
#endif
109150
 
 
109151
 
#ifdef SQLITE_ENABLE_FTS3
109152
 
  if( !db->mallocFailed && rc==SQLITE_OK ){
109153
 
    rc = sqlite3Fts3Init(db);
109154
 
  }
109155
 
#endif
109156
 
 
109157
 
#ifdef SQLITE_ENABLE_ICU
109158
 
  if( !db->mallocFailed && rc==SQLITE_OK ){
109159
 
    rc = sqlite3IcuInit(db);
109160
 
  }
109161
 
#endif
109162
 
 
109163
 
#ifdef SQLITE_ENABLE_RTREE
109164
 
  if( !db->mallocFailed && rc==SQLITE_OK){
109165
 
    rc = sqlite3RtreeInit(db);
109166
 
  }
109167
 
#endif
109168
 
 
109169
 
  sqlite3Error(db, rc, 0);
109170
 
 
109171
 
  /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
109172
 
  ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
109173
 
  ** mode.  Doing nothing at all also makes NORMAL the default.
109174
 
  */
109175
 
#ifdef SQLITE_DEFAULT_LOCKING_MODE
109176
 
  db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
109177
 
  sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
109178
 
                          SQLITE_DEFAULT_LOCKING_MODE);
109179
 
#endif
109180
 
 
109181
 
  /* Enable the lookaside-malloc subsystem */
109182
 
  setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
109183
 
                        sqlite3GlobalConfig.nLookaside);
109184
 
 
109185
 
  sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
109186
 
 
109187
 
opendb_out:
109188
 
  if( db ){
109189
 
    assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
109190
 
    sqlite3_mutex_leave(db->mutex);
109191
 
  }
109192
 
  rc = sqlite3_errcode(db);
109193
 
  if( rc==SQLITE_NOMEM ){
109194
 
    sqlite3_close(db);
109195
 
    db = 0;
109196
 
  }else if( rc!=SQLITE_OK ){
109197
 
    db->magic = SQLITE_MAGIC_SICK;
109198
 
  }
109199
 
  *ppDb = db;
109200
 
  return sqlite3ApiExit(0, rc);
109201
 
}
109202
 
 
109203
 
/*
109204
 
** Open a new database handle.
109205
 
*/
109206
 
SQLITE_API int sqlite3_open(
109207
 
  const char *zFilename, 
109208
 
  sqlite3 **ppDb 
109209
 
){
109210
 
  return openDatabase(zFilename, ppDb,
109211
 
                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
109212
 
}
109213
 
SQLITE_API int sqlite3_open_v2(
109214
 
  const char *filename,   /* Database filename (UTF-8) */
109215
 
  sqlite3 **ppDb,         /* OUT: SQLite db handle */
109216
 
  int flags,              /* Flags */
109217
 
  const char *zVfs        /* Name of VFS module to use */
109218
 
){
109219
 
  return openDatabase(filename, ppDb, flags, zVfs);
109220
 
}
109221
 
 
109222
 
#ifndef SQLITE_OMIT_UTF16
109223
 
/*
109224
 
** Open a new database handle.
109225
 
*/
109226
 
SQLITE_API int sqlite3_open16(
109227
 
  const void *zFilename, 
109228
 
  sqlite3 **ppDb
109229
 
){
109230
 
  char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
109231
 
  sqlite3_value *pVal;
109232
 
  int rc;
109233
 
 
109234
 
  assert( zFilename );
109235
 
  assert( ppDb );
109236
 
  *ppDb = 0;
109237
 
#ifndef SQLITE_OMIT_AUTOINIT
109238
 
  rc = sqlite3_initialize();
109239
 
  if( rc ) return rc;
109240
 
#endif
109241
 
  pVal = sqlite3ValueNew(0);
109242
 
  sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
109243
 
  zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
109244
 
  if( zFilename8 ){
109245
 
    rc = openDatabase(zFilename8, ppDb,
109246
 
                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
109247
 
    assert( *ppDb || rc==SQLITE_NOMEM );
109248
 
    if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
109249
 
      ENC(*ppDb) = SQLITE_UTF16NATIVE;
109250
 
    }
109251
 
  }else{
109252
 
    rc = SQLITE_NOMEM;
109253
 
  }
109254
 
  sqlite3ValueFree(pVal);
109255
 
 
109256
 
  return sqlite3ApiExit(0, rc);
109257
 
}
109258
 
#endif /* SQLITE_OMIT_UTF16 */
109259
 
 
109260
 
/*
109261
 
** Register a new collation sequence with the database handle db.
109262
 
*/
109263
 
SQLITE_API int sqlite3_create_collation(
109264
 
  sqlite3* db, 
109265
 
  const char *zName, 
109266
 
  int enc, 
109267
 
  void* pCtx,
109268
 
  int(*xCompare)(void*,int,const void*,int,const void*)
109269
 
){
109270
 
  int rc;
109271
 
  sqlite3_mutex_enter(db->mutex);
109272
 
  assert( !db->mallocFailed );
109273
 
  rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
109274
 
  rc = sqlite3ApiExit(db, rc);
109275
 
  sqlite3_mutex_leave(db->mutex);
109276
 
  return rc;
109277
 
}
109278
 
 
109279
 
/*
109280
 
** Register a new collation sequence with the database handle db.
109281
 
*/
109282
 
SQLITE_API int sqlite3_create_collation_v2(
109283
 
  sqlite3* db, 
109284
 
  const char *zName, 
109285
 
  int enc, 
109286
 
  void* pCtx,
109287
 
  int(*xCompare)(void*,int,const void*,int,const void*),
109288
 
  void(*xDel)(void*)
109289
 
){
109290
 
  int rc;
109291
 
  sqlite3_mutex_enter(db->mutex);
109292
 
  assert( !db->mallocFailed );
109293
 
  rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, xDel);
109294
 
  rc = sqlite3ApiExit(db, rc);
109295
 
  sqlite3_mutex_leave(db->mutex);
109296
 
  return rc;
109297
 
}
109298
 
 
109299
 
#ifndef SQLITE_OMIT_UTF16
109300
 
/*
109301
 
** Register a new collation sequence with the database handle db.
109302
 
*/
109303
 
SQLITE_API int sqlite3_create_collation16(
109304
 
  sqlite3* db, 
109305
 
  const void *zName,
109306
 
  int enc, 
109307
 
  void* pCtx,
109308
 
  int(*xCompare)(void*,int,const void*,int,const void*)
109309
 
){
109310
 
  int rc = SQLITE_OK;
109311
 
  char *zName8;
109312
 
  sqlite3_mutex_enter(db->mutex);
109313
 
  assert( !db->mallocFailed );
109314
 
  zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
109315
 
  if( zName8 ){
109316
 
    rc = createCollation(db, zName8, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
109317
 
    sqlite3DbFree(db, zName8);
109318
 
  }
109319
 
  rc = sqlite3ApiExit(db, rc);
109320
 
  sqlite3_mutex_leave(db->mutex);
109321
 
  return rc;
109322
 
}
109323
 
#endif /* SQLITE_OMIT_UTF16 */
109324
 
 
109325
 
/*
109326
 
** Register a collation sequence factory callback with the database handle
109327
 
** db. Replace any previously installed collation sequence factory.
109328
 
*/
109329
 
SQLITE_API int sqlite3_collation_needed(
109330
 
  sqlite3 *db, 
109331
 
  void *pCollNeededArg, 
109332
 
  void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
109333
 
){
109334
 
  sqlite3_mutex_enter(db->mutex);
109335
 
  db->xCollNeeded = xCollNeeded;
109336
 
  db->xCollNeeded16 = 0;
109337
 
  db->pCollNeededArg = pCollNeededArg;
109338
 
  sqlite3_mutex_leave(db->mutex);
109339
 
  return SQLITE_OK;
109340
 
}
109341
 
 
109342
 
#ifndef SQLITE_OMIT_UTF16
109343
 
/*
109344
 
** Register a collation sequence factory callback with the database handle
109345
 
** db. Replace any previously installed collation sequence factory.
109346
 
*/
109347
 
SQLITE_API int sqlite3_collation_needed16(
109348
 
  sqlite3 *db, 
109349
 
  void *pCollNeededArg, 
109350
 
  void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
109351
 
){
109352
 
  sqlite3_mutex_enter(db->mutex);
109353
 
  db->xCollNeeded = 0;
109354
 
  db->xCollNeeded16 = xCollNeeded16;
109355
 
  db->pCollNeededArg = pCollNeededArg;
109356
 
  sqlite3_mutex_leave(db->mutex);
109357
 
  return SQLITE_OK;
109358
 
}
109359
 
#endif /* SQLITE_OMIT_UTF16 */
109360
 
 
109361
 
#ifndef SQLITE_OMIT_DEPRECATED
109362
 
/*
109363
 
** This function is now an anachronism. It used to be used to recover from a
109364
 
** malloc() failure, but SQLite now does this automatically.
109365
 
*/
109366
 
SQLITE_API int sqlite3_global_recover(void){
109367
 
  return SQLITE_OK;
109368
 
}
109369
 
#endif
109370
 
 
109371
 
/*
109372
 
** Test to see whether or not the database connection is in autocommit
109373
 
** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
109374
 
** by default.  Autocommit is disabled by a BEGIN statement and reenabled
109375
 
** by the next COMMIT or ROLLBACK.
109376
 
**
109377
 
******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
109378
 
*/
109379
 
SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
109380
 
  return db->autoCommit;
109381
 
}
109382
 
 
109383
 
/*
109384
 
** The following routines are subtitutes for constants SQLITE_CORRUPT,
109385
 
** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
109386
 
** constants.  They server two purposes:
109387
 
**
109388
 
**   1.  Serve as a convenient place to set a breakpoint in a debugger
109389
 
**       to detect when version error conditions occurs.
109390
 
**
109391
 
**   2.  Invoke sqlite3_log() to provide the source code location where
109392
 
**       a low-level error is first detected.
109393
 
*/
109394
 
SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
109395
 
  testcase( sqlite3GlobalConfig.xLog!=0 );
109396
 
  sqlite3_log(SQLITE_CORRUPT,
109397
 
              "database corruption at line %d of [%.10s]",
109398
 
              lineno, 20+sqlite3_sourceid());
109399
 
  return SQLITE_CORRUPT;
109400
 
}
109401
 
SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
109402
 
  testcase( sqlite3GlobalConfig.xLog!=0 );
109403
 
  sqlite3_log(SQLITE_MISUSE, 
109404
 
              "misuse at line %d of [%.10s]",
109405
 
              lineno, 20+sqlite3_sourceid());
109406
 
  return SQLITE_MISUSE;
109407
 
}
109408
 
SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
109409
 
  testcase( sqlite3GlobalConfig.xLog!=0 );
109410
 
  sqlite3_log(SQLITE_CANTOPEN, 
109411
 
              "cannot open file at line %d of [%.10s]",
109412
 
              lineno, 20+sqlite3_sourceid());
109413
 
  return SQLITE_CANTOPEN;
109414
 
}
109415
 
 
109416
 
 
109417
 
#ifndef SQLITE_OMIT_DEPRECATED
109418
 
/*
109419
 
** This is a convenience routine that makes sure that all thread-specific
109420
 
** data for this thread has been deallocated.
109421
 
**
109422
 
** SQLite no longer uses thread-specific data so this routine is now a
109423
 
** no-op.  It is retained for historical compatibility.
109424
 
*/
109425
 
SQLITE_API void sqlite3_thread_cleanup(void){
109426
 
}
109427
 
#endif
109428
 
 
109429
 
/*
109430
 
** Return meta information about a specific column of a database table.
109431
 
** See comment in sqlite3.h (sqlite.h.in) for details.
109432
 
*/
109433
 
#ifdef SQLITE_ENABLE_COLUMN_METADATA
109434
 
SQLITE_API int sqlite3_table_column_metadata(
109435
 
  sqlite3 *db,                /* Connection handle */
109436
 
  const char *zDbName,        /* Database name or NULL */
109437
 
  const char *zTableName,     /* Table name */
109438
 
  const char *zColumnName,    /* Column name */
109439
 
  char const **pzDataType,    /* OUTPUT: Declared data type */
109440
 
  char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
109441
 
  int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
109442
 
  int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
109443
 
  int *pAutoinc               /* OUTPUT: True if column is auto-increment */
109444
 
){
109445
 
  int rc;
109446
 
  char *zErrMsg = 0;
109447
 
  Table *pTab = 0;
109448
 
  Column *pCol = 0;
109449
 
  int iCol;
109450
 
 
109451
 
  char const *zDataType = 0;
109452
 
  char const *zCollSeq = 0;
109453
 
  int notnull = 0;
109454
 
  int primarykey = 0;
109455
 
  int autoinc = 0;
109456
 
 
109457
 
  /* Ensure the database schema has been loaded */
109458
 
  sqlite3_mutex_enter(db->mutex);
109459
 
  sqlite3BtreeEnterAll(db);
109460
 
  rc = sqlite3Init(db, &zErrMsg);
109461
 
  if( SQLITE_OK!=rc ){
109462
 
    goto error_out;
109463
 
  }
109464
 
 
109465
 
  /* Locate the table in question */
109466
 
  pTab = sqlite3FindTable(db, zTableName, zDbName);
109467
 
  if( !pTab || pTab->pSelect ){
109468
 
    pTab = 0;
109469
 
    goto error_out;
109470
 
  }
109471
 
 
109472
 
  /* Find the column for which info is requested */
109473
 
  if( sqlite3IsRowid(zColumnName) ){
109474
 
    iCol = pTab->iPKey;
109475
 
    if( iCol>=0 ){
109476
 
      pCol = &pTab->aCol[iCol];
109477
 
    }
109478
 
  }else{
109479
 
    for(iCol=0; iCol<pTab->nCol; iCol++){
109480
 
      pCol = &pTab->aCol[iCol];
109481
 
      if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
109482
 
        break;
109483
 
      }
109484
 
    }
109485
 
    if( iCol==pTab->nCol ){
109486
 
      pTab = 0;
109487
 
      goto error_out;
109488
 
    }
109489
 
  }
109490
 
 
109491
 
  /* The following block stores the meta information that will be returned
109492
 
  ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
109493
 
  ** and autoinc. At this point there are two possibilities:
109494
 
  ** 
109495
 
  **     1. The specified column name was rowid", "oid" or "_rowid_" 
109496
 
  **        and there is no explicitly declared IPK column. 
109497
 
  **
109498
 
  **     2. The table is not a view and the column name identified an 
109499
 
  **        explicitly declared column. Copy meta information from *pCol.
109500
 
  */ 
109501
 
  if( pCol ){
109502
 
    zDataType = pCol->zType;
109503
 
    zCollSeq = pCol->zColl;
109504
 
    notnull = pCol->notNull!=0;
109505
 
    primarykey  = pCol->isPrimKey!=0;
109506
 
    autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
109507
 
  }else{
109508
 
    zDataType = "INTEGER";
109509
 
    primarykey = 1;
109510
 
  }
109511
 
  if( !zCollSeq ){
109512
 
    zCollSeq = "BINARY";
109513
 
  }
109514
 
 
109515
 
error_out:
109516
 
  sqlite3BtreeLeaveAll(db);
109517
 
 
109518
 
  /* Whether the function call succeeded or failed, set the output parameters
109519
 
  ** to whatever their local counterparts contain. If an error did occur,
109520
 
  ** this has the effect of zeroing all output parameters.
109521
 
  */
109522
 
  if( pzDataType ) *pzDataType = zDataType;
109523
 
  if( pzCollSeq ) *pzCollSeq = zCollSeq;
109524
 
  if( pNotNull ) *pNotNull = notnull;
109525
 
  if( pPrimaryKey ) *pPrimaryKey = primarykey;
109526
 
  if( pAutoinc ) *pAutoinc = autoinc;
109527
 
 
109528
 
  if( SQLITE_OK==rc && !pTab ){
109529
 
    sqlite3DbFree(db, zErrMsg);
109530
 
    zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
109531
 
        zColumnName);
109532
 
    rc = SQLITE_ERROR;
109533
 
  }
109534
 
  sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
109535
 
  sqlite3DbFree(db, zErrMsg);
109536
 
  rc = sqlite3ApiExit(db, rc);
109537
 
  sqlite3_mutex_leave(db->mutex);
109538
 
  return rc;
109539
 
}
109540
 
#endif
109541
 
 
109542
 
/*
109543
 
** Sleep for a little while.  Return the amount of time slept.
109544
 
*/
109545
 
SQLITE_API int sqlite3_sleep(int ms){
109546
 
  sqlite3_vfs *pVfs;
109547
 
  int rc;
109548
 
  pVfs = sqlite3_vfs_find(0);
109549
 
  if( pVfs==0 ) return 0;
109550
 
 
109551
 
  /* This function works in milliseconds, but the underlying OsSleep() 
109552
 
  ** API uses microseconds. Hence the 1000's.
109553
 
  */
109554
 
  rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
109555
 
  return rc;
109556
 
}
109557
 
 
109558
 
/*
109559
 
** Enable or disable the extended result codes.
109560
 
*/
109561
 
SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
109562
 
  sqlite3_mutex_enter(db->mutex);
109563
 
  db->errMask = onoff ? 0xffffffff : 0xff;
109564
 
  sqlite3_mutex_leave(db->mutex);
109565
 
  return SQLITE_OK;
109566
 
}
109567
 
 
109568
 
/*
109569
 
** Invoke the xFileControl method on a particular database.
109570
 
*/
109571
 
SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
109572
 
  int rc = SQLITE_ERROR;
109573
 
  int iDb;
109574
 
  sqlite3_mutex_enter(db->mutex);
109575
 
  if( zDbName==0 ){
109576
 
    iDb = 0;
109577
 
  }else{
109578
 
    for(iDb=0; iDb<db->nDb; iDb++){
109579
 
      if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break;
109580
 
    }
109581
 
  }
109582
 
  if( iDb<db->nDb ){
109583
 
    Btree *pBtree = db->aDb[iDb].pBt;
109584
 
    if( pBtree ){
109585
 
      Pager *pPager;
109586
 
      sqlite3_file *fd;
109587
 
      sqlite3BtreeEnter(pBtree);
109588
 
      pPager = sqlite3BtreePager(pBtree);
109589
 
      assert( pPager!=0 );
109590
 
      fd = sqlite3PagerFile(pPager);
109591
 
      assert( fd!=0 );
109592
 
      if( op==SQLITE_FCNTL_FILE_POINTER ){
109593
 
        *(sqlite3_file**)pArg = fd;
109594
 
        rc = SQLITE_OK;
109595
 
      }else if( fd->pMethods ){
109596
 
        rc = sqlite3OsFileControl(fd, op, pArg);
109597
 
      }else{
109598
 
        rc = SQLITE_NOTFOUND;
109599
 
      }
109600
 
      sqlite3BtreeLeave(pBtree);
109601
 
    }
109602
 
  }
109603
 
  sqlite3_mutex_leave(db->mutex);
109604
 
  return rc;   
109605
 
}
109606
 
 
109607
 
/*
109608
 
** Interface to the testing logic.
109609
 
*/
109610
 
SQLITE_API int sqlite3_test_control(int op, ...){
109611
 
  int rc = 0;
109612
 
#ifndef SQLITE_OMIT_BUILTIN_TEST
109613
 
  va_list ap;
109614
 
  va_start(ap, op);
109615
 
  switch( op ){
109616
 
 
109617
 
    /*
109618
 
    ** Save the current state of the PRNG.
109619
 
    */
109620
 
    case SQLITE_TESTCTRL_PRNG_SAVE: {
109621
 
      sqlite3PrngSaveState();
109622
 
      break;
109623
 
    }
109624
 
 
109625
 
    /*
109626
 
    ** Restore the state of the PRNG to the last state saved using
109627
 
    ** PRNG_SAVE.  If PRNG_SAVE has never before been called, then
109628
 
    ** this verb acts like PRNG_RESET.
109629
 
    */
109630
 
    case SQLITE_TESTCTRL_PRNG_RESTORE: {
109631
 
      sqlite3PrngRestoreState();
109632
 
      break;
109633
 
    }
109634
 
 
109635
 
    /*
109636
 
    ** Reset the PRNG back to its uninitialized state.  The next call
109637
 
    ** to sqlite3_randomness() will reseed the PRNG using a single call
109638
 
    ** to the xRandomness method of the default VFS.
109639
 
    */
109640
 
    case SQLITE_TESTCTRL_PRNG_RESET: {
109641
 
      sqlite3PrngResetState();
109642
 
      break;
109643
 
    }
109644
 
 
109645
 
    /*
109646
 
    **  sqlite3_test_control(BITVEC_TEST, size, program)
109647
 
    **
109648
 
    ** Run a test against a Bitvec object of size.  The program argument
109649
 
    ** is an array of integers that defines the test.  Return -1 on a
109650
 
    ** memory allocation error, 0 on success, or non-zero for an error.
109651
 
    ** See the sqlite3BitvecBuiltinTest() for additional information.
109652
 
    */
109653
 
    case SQLITE_TESTCTRL_BITVEC_TEST: {
109654
 
      int sz = va_arg(ap, int);
109655
 
      int *aProg = va_arg(ap, int*);
109656
 
      rc = sqlite3BitvecBuiltinTest(sz, aProg);
109657
 
      break;
109658
 
    }
109659
 
 
109660
 
    /*
109661
 
    **  sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
109662
 
    **
109663
 
    ** Register hooks to call to indicate which malloc() failures 
109664
 
    ** are benign.
109665
 
    */
109666
 
    case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
109667
 
      typedef void (*void_function)(void);
109668
 
      void_function xBenignBegin;
109669
 
      void_function xBenignEnd;
109670
 
      xBenignBegin = va_arg(ap, void_function);
109671
 
      xBenignEnd = va_arg(ap, void_function);
109672
 
      sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
109673
 
      break;
109674
 
    }
109675
 
 
109676
 
    /*
109677
 
    **  sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
109678
 
    **
109679
 
    ** Set the PENDING byte to the value in the argument, if X>0.
109680
 
    ** Make no changes if X==0.  Return the value of the pending byte
109681
 
    ** as it existing before this routine was called.
109682
 
    **
109683
 
    ** IMPORTANT:  Changing the PENDING byte from 0x40000000 results in
109684
 
    ** an incompatible database file format.  Changing the PENDING byte
109685
 
    ** while any database connection is open results in undefined and
109686
 
    ** dileterious behavior.
109687
 
    */
109688
 
    case SQLITE_TESTCTRL_PENDING_BYTE: {
109689
 
      rc = PENDING_BYTE;
109690
 
#ifndef SQLITE_OMIT_WSD
109691
 
      {
109692
 
        unsigned int newVal = va_arg(ap, unsigned int);
109693
 
        if( newVal ) sqlite3PendingByte = newVal;
109694
 
      }
109695
 
#endif
109696
 
      break;
109697
 
    }
109698
 
 
109699
 
    /*
109700
 
    **  sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
109701
 
    **
109702
 
    ** This action provides a run-time test to see whether or not
109703
 
    ** assert() was enabled at compile-time.  If X is true and assert()
109704
 
    ** is enabled, then the return value is true.  If X is true and
109705
 
    ** assert() is disabled, then the return value is zero.  If X is
109706
 
    ** false and assert() is enabled, then the assertion fires and the
109707
 
    ** process aborts.  If X is false and assert() is disabled, then the
109708
 
    ** return value is zero.
109709
 
    */
109710
 
    case SQLITE_TESTCTRL_ASSERT: {
109711
 
      volatile int x = 0;
109712
 
      assert( (x = va_arg(ap,int))!=0 );
109713
 
      rc = x;
109714
 
      break;
109715
 
    }
109716
 
 
109717
 
 
109718
 
    /*
109719
 
    **  sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
109720
 
    **
109721
 
    ** This action provides a run-time test to see how the ALWAYS and
109722
 
    ** NEVER macros were defined at compile-time.
109723
 
    **
109724
 
    ** The return value is ALWAYS(X).  
109725
 
    **
109726
 
    ** The recommended test is X==2.  If the return value is 2, that means
109727
 
    ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
109728
 
    ** default setting.  If the return value is 1, then ALWAYS() is either
109729
 
    ** hard-coded to true or else it asserts if its argument is false.
109730
 
    ** The first behavior (hard-coded to true) is the case if
109731
 
    ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
109732
 
    ** behavior (assert if the argument to ALWAYS() is false) is the case if
109733
 
    ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
109734
 
    **
109735
 
    ** The run-time test procedure might look something like this:
109736
 
    **
109737
 
    **    if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
109738
 
    **      // ALWAYS() and NEVER() are no-op pass-through macros
109739
 
    **    }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
109740
 
    **      // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
109741
 
    **    }else{
109742
 
    **      // ALWAYS(x) is a constant 1.  NEVER(x) is a constant 0.
109743
 
    **    }
109744
 
    */
109745
 
    case SQLITE_TESTCTRL_ALWAYS: {
109746
 
      int x = va_arg(ap,int);
109747
 
      rc = ALWAYS(x);
109748
 
      break;
109749
 
    }
109750
 
 
109751
 
    /*   sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
109752
 
    **
109753
 
    ** Set the nReserve size to N for the main database on the database
109754
 
    ** connection db.
109755
 
    */
109756
 
    case SQLITE_TESTCTRL_RESERVE: {
109757
 
      sqlite3 *db = va_arg(ap, sqlite3*);
109758
 
      int x = va_arg(ap,int);
109759
 
      sqlite3_mutex_enter(db->mutex);
109760
 
      sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
109761
 
      sqlite3_mutex_leave(db->mutex);
109762
 
      break;
109763
 
    }
109764
 
 
109765
 
    /*  sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
109766
 
    **
109767
 
    ** Enable or disable various optimizations for testing purposes.  The 
109768
 
    ** argument N is a bitmask of optimizations to be disabled.  For normal
109769
 
    ** operation N should be 0.  The idea is that a test program (like the
109770
 
    ** SQL Logic Test or SLT test module) can run the same SQL multiple times
109771
 
    ** with various optimizations disabled to verify that the same answer
109772
 
    ** is obtained in every case.
109773
 
    */
109774
 
    case SQLITE_TESTCTRL_OPTIMIZATIONS: {
109775
 
      sqlite3 *db = va_arg(ap, sqlite3*);
109776
 
      int x = va_arg(ap,int);
109777
 
      db->flags = (x & SQLITE_OptMask) | (db->flags & ~SQLITE_OptMask);
109778
 
      break;
109779
 
    }
109780
 
 
109781
 
#ifdef SQLITE_N_KEYWORD
109782
 
    /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
109783
 
    **
109784
 
    ** If zWord is a keyword recognized by the parser, then return the
109785
 
    ** number of keywords.  Or if zWord is not a keyword, return 0.
109786
 
    ** 
109787
 
    ** This test feature is only available in the amalgamation since
109788
 
    ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
109789
 
    ** is built using separate source files.
109790
 
    */
109791
 
    case SQLITE_TESTCTRL_ISKEYWORD: {
109792
 
      const char *zWord = va_arg(ap, const char*);
109793
 
      int n = sqlite3Strlen30(zWord);
109794
 
      rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
109795
 
      break;
109796
 
    }
109797
 
#endif 
109798
 
 
109799
 
    /* sqlite3_test_control(SQLITE_TESTCTRL_PGHDRSZ)
109800
 
    **
109801
 
    ** Return the size of a pcache header in bytes.
109802
 
    */
109803
 
    case SQLITE_TESTCTRL_PGHDRSZ: {
109804
 
      rc = sizeof(PgHdr);
109805
 
      break;
109806
 
    }
109807
 
 
109808
 
    /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
109809
 
    **
109810
 
    ** Pass pFree into sqlite3ScratchFree(). 
109811
 
    ** If sz>0 then allocate a scratch buffer into pNew.  
109812
 
    */
109813
 
    case SQLITE_TESTCTRL_SCRATCHMALLOC: {
109814
 
      void *pFree, **ppNew;
109815
 
      int sz;
109816
 
      sz = va_arg(ap, int);
109817
 
      ppNew = va_arg(ap, void**);
109818
 
      pFree = va_arg(ap, void*);
109819
 
      if( sz ) *ppNew = sqlite3ScratchMalloc(sz);
109820
 
      sqlite3ScratchFree(pFree);
109821
 
      break;
109822
 
    }
109823
 
 
109824
 
  }
109825
 
  va_end(ap);
109826
 
#endif /* SQLITE_OMIT_BUILTIN_TEST */
109827
 
  return rc;
109828
 
}
109829
 
 
109830
 
/************** End of main.c ************************************************/
109831
 
/************** Begin file notify.c ******************************************/
109832
 
/*
109833
 
** 2009 March 3
109834
 
**
109835
 
** The author disclaims copyright to this source code.  In place of
109836
 
** a legal notice, here is a blessing:
109837
 
**
109838
 
**    May you do good and not evil.
109839
 
**    May you find forgiveness for yourself and forgive others.
109840
 
**    May you share freely, never taking more than you give.
109841
 
**
109842
 
*************************************************************************
109843
 
**
109844
 
** This file contains the implementation of the sqlite3_unlock_notify()
109845
 
** API method and its associated functionality.
109846
 
*/
109847
 
 
109848
 
/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
109849
 
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
109850
 
 
109851
 
/*
109852
 
** Public interfaces:
109853
 
**
109854
 
**   sqlite3ConnectionBlocked()
109855
 
**   sqlite3ConnectionUnlocked()
109856
 
**   sqlite3ConnectionClosed()
109857
 
**   sqlite3_unlock_notify()
109858
 
*/
109859
 
 
109860
 
#define assertMutexHeld() \
109861
 
  assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
109862
 
 
109863
 
/*
109864
 
** Head of a linked list of all sqlite3 objects created by this process
109865
 
** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
109866
 
** is not NULL. This variable may only accessed while the STATIC_MASTER
109867
 
** mutex is held.
109868
 
*/
109869
 
static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
109870
 
 
109871
 
#ifndef NDEBUG
109872
 
/*
109873
 
** This function is a complex assert() that verifies the following 
109874
 
** properties of the blocked connections list:
109875
 
**
109876
 
**   1) Each entry in the list has a non-NULL value for either 
109877
 
**      pUnlockConnection or pBlockingConnection, or both.
109878
 
**
109879
 
**   2) All entries in the list that share a common value for 
109880
 
**      xUnlockNotify are grouped together.
109881
 
**
109882
 
**   3) If the argument db is not NULL, then none of the entries in the
109883
 
**      blocked connections list have pUnlockConnection or pBlockingConnection
109884
 
**      set to db. This is used when closing connection db.
109885
 
*/
109886
 
static void checkListProperties(sqlite3 *db){
109887
 
  sqlite3 *p;
109888
 
  for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
109889
 
    int seen = 0;
109890
 
    sqlite3 *p2;
109891
 
 
109892
 
    /* Verify property (1) */
109893
 
    assert( p->pUnlockConnection || p->pBlockingConnection );
109894
 
 
109895
 
    /* Verify property (2) */
109896
 
    for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
109897
 
      if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
109898
 
      assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
109899
 
      assert( db==0 || p->pUnlockConnection!=db );
109900
 
      assert( db==0 || p->pBlockingConnection!=db );
109901
 
    }
109902
 
  }
109903
 
}
109904
 
#else
109905
 
# define checkListProperties(x)
109906
 
#endif
109907
 
 
109908
 
/*
109909
 
** Remove connection db from the blocked connections list. If connection
109910
 
** db is not currently a part of the list, this function is a no-op.
109911
 
*/
109912
 
static void removeFromBlockedList(sqlite3 *db){
109913
 
  sqlite3 **pp;
109914
 
  assertMutexHeld();
109915
 
  for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
109916
 
    if( *pp==db ){
109917
 
      *pp = (*pp)->pNextBlocked;
109918
 
      break;
109919
 
    }
109920
 
  }
109921
 
}
109922
 
 
109923
 
/*
109924
 
** Add connection db to the blocked connections list. It is assumed
109925
 
** that it is not already a part of the list.
109926
 
*/
109927
 
static void addToBlockedList(sqlite3 *db){
109928
 
  sqlite3 **pp;
109929
 
  assertMutexHeld();
109930
 
  for(
109931
 
    pp=&sqlite3BlockedList; 
109932
 
    *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify; 
109933
 
    pp=&(*pp)->pNextBlocked
109934
 
  );
109935
 
  db->pNextBlocked = *pp;
109936
 
  *pp = db;
109937
 
}
109938
 
 
109939
 
/*
109940
 
** Obtain the STATIC_MASTER mutex.
109941
 
*/
109942
 
static void enterMutex(void){
109943
 
  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
109944
 
  checkListProperties(0);
109945
 
}
109946
 
 
109947
 
/*
109948
 
** Release the STATIC_MASTER mutex.
109949
 
*/
109950
 
static void leaveMutex(void){
109951
 
  assertMutexHeld();
109952
 
  checkListProperties(0);
109953
 
  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
109954
 
}
109955
 
 
109956
 
/*
109957
 
** Register an unlock-notify callback.
109958
 
**
109959
 
** This is called after connection "db" has attempted some operation
109960
 
** but has received an SQLITE_LOCKED error because another connection
109961
 
** (call it pOther) in the same process was busy using the same shared
109962
 
** cache.  pOther is found by looking at db->pBlockingConnection.
109963
 
**
109964
 
** If there is no blocking connection, the callback is invoked immediately,
109965
 
** before this routine returns.
109966
 
**
109967
 
** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
109968
 
** a deadlock.
109969
 
**
109970
 
** Otherwise, make arrangements to invoke xNotify when pOther drops
109971
 
** its locks.
109972
 
**
109973
 
** Each call to this routine overrides any prior callbacks registered
109974
 
** on the same "db".  If xNotify==0 then any prior callbacks are immediately
109975
 
** cancelled.
109976
 
*/
109977
 
SQLITE_API int sqlite3_unlock_notify(
109978
 
  sqlite3 *db,
109979
 
  void (*xNotify)(void **, int),
109980
 
  void *pArg
109981
 
){
109982
 
  int rc = SQLITE_OK;
109983
 
 
109984
 
  sqlite3_mutex_enter(db->mutex);
109985
 
  enterMutex();
109986
 
 
109987
 
  if( xNotify==0 ){
109988
 
    removeFromBlockedList(db);
109989
 
    db->pBlockingConnection = 0;
109990
 
    db->pUnlockConnection = 0;
109991
 
    db->xUnlockNotify = 0;
109992
 
    db->pUnlockArg = 0;
109993
 
  }else if( 0==db->pBlockingConnection ){
109994
 
    /* The blocking transaction has been concluded. Or there never was a 
109995
 
    ** blocking transaction. In either case, invoke the notify callback
109996
 
    ** immediately. 
109997
 
    */
109998
 
    xNotify(&pArg, 1);
109999
 
  }else{
110000
 
    sqlite3 *p;
110001
 
 
110002
 
    for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
110003
 
    if( p ){
110004
 
      rc = SQLITE_LOCKED;              /* Deadlock detected. */
110005
 
    }else{
110006
 
      db->pUnlockConnection = db->pBlockingConnection;
110007
 
      db->xUnlockNotify = xNotify;
110008
 
      db->pUnlockArg = pArg;
110009
 
      removeFromBlockedList(db);
110010
 
      addToBlockedList(db);
110011
 
    }
110012
 
  }
110013
 
 
110014
 
  leaveMutex();
110015
 
  assert( !db->mallocFailed );
110016
 
  sqlite3Error(db, rc, (rc?"database is deadlocked":0));
110017
 
  sqlite3_mutex_leave(db->mutex);
110018
 
  return rc;
110019
 
}
110020
 
 
110021
 
/*
110022
 
** This function is called while stepping or preparing a statement 
110023
 
** associated with connection db. The operation will return SQLITE_LOCKED
110024
 
** to the user because it requires a lock that will not be available
110025
 
** until connection pBlocker concludes its current transaction.
110026
 
*/
110027
 
SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
110028
 
  enterMutex();
110029
 
  if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
110030
 
    addToBlockedList(db);
110031
 
  }
110032
 
  db->pBlockingConnection = pBlocker;
110033
 
  leaveMutex();
110034
 
}
110035
 
 
110036
 
/*
110037
 
** This function is called when
110038
 
** the transaction opened by database db has just finished. Locks held 
110039
 
** by database connection db have been released.
110040
 
**
110041
 
** This function loops through each entry in the blocked connections
110042
 
** list and does the following:
110043
 
**
110044
 
**   1) If the sqlite3.pBlockingConnection member of a list entry is
110045
 
**      set to db, then set pBlockingConnection=0.
110046
 
**
110047
 
**   2) If the sqlite3.pUnlockConnection member of a list entry is
110048
 
**      set to db, then invoke the configured unlock-notify callback and
110049
 
**      set pUnlockConnection=0.
110050
 
**
110051
 
**   3) If the two steps above mean that pBlockingConnection==0 and
110052
 
**      pUnlockConnection==0, remove the entry from the blocked connections
110053
 
**      list.
110054
 
*/
110055
 
SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
110056
 
  void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
110057
 
  int nArg = 0;                            /* Number of entries in aArg[] */
110058
 
  sqlite3 **pp;                            /* Iterator variable */
110059
 
  void **aArg;               /* Arguments to the unlock callback */
110060
 
  void **aDyn = 0;           /* Dynamically allocated space for aArg[] */
110061
 
  void *aStatic[16];         /* Starter space for aArg[].  No malloc required */
110062
 
 
110063
 
  aArg = aStatic;
110064
 
  enterMutex();         /* Enter STATIC_MASTER mutex */
110065
 
 
110066
 
  /* This loop runs once for each entry in the blocked-connections list. */
110067
 
  for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
110068
 
    sqlite3 *p = *pp;
110069
 
 
110070
 
    /* Step 1. */
110071
 
    if( p->pBlockingConnection==db ){
110072
 
      p->pBlockingConnection = 0;
110073
 
    }
110074
 
 
110075
 
    /* Step 2. */
110076
 
    if( p->pUnlockConnection==db ){
110077
 
      assert( p->xUnlockNotify );
110078
 
      if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
110079
 
        xUnlockNotify(aArg, nArg);
110080
 
        nArg = 0;
110081
 
      }
110082
 
 
110083
 
      sqlite3BeginBenignMalloc();
110084
 
      assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
110085
 
      assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
110086
 
      if( (!aDyn && nArg==(int)ArraySize(aStatic))
110087
 
       || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
110088
 
      ){
110089
 
        /* The aArg[] array needs to grow. */
110090
 
        void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
110091
 
        if( pNew ){
110092
 
          memcpy(pNew, aArg, nArg*sizeof(void *));
110093
 
          sqlite3_free(aDyn);
110094
 
          aDyn = aArg = pNew;
110095
 
        }else{
110096
 
          /* This occurs when the array of context pointers that need to
110097
 
          ** be passed to the unlock-notify callback is larger than the
110098
 
          ** aStatic[] array allocated on the stack and the attempt to 
110099
 
          ** allocate a larger array from the heap has failed.
110100
 
          **
110101
 
          ** This is a difficult situation to handle. Returning an error
110102
 
          ** code to the caller is insufficient, as even if an error code
110103
 
          ** is returned the transaction on connection db will still be
110104
 
          ** closed and the unlock-notify callbacks on blocked connections
110105
 
          ** will go unissued. This might cause the application to wait
110106
 
          ** indefinitely for an unlock-notify callback that will never 
110107
 
          ** arrive.
110108
 
          **
110109
 
          ** Instead, invoke the unlock-notify callback with the context
110110
 
          ** array already accumulated. We can then clear the array and
110111
 
          ** begin accumulating any further context pointers without 
110112
 
          ** requiring any dynamic allocation. This is sub-optimal because
110113
 
          ** it means that instead of one callback with a large array of
110114
 
          ** context pointers the application will receive two or more
110115
 
          ** callbacks with smaller arrays of context pointers, which will
110116
 
          ** reduce the applications ability to prioritize multiple 
110117
 
          ** connections. But it is the best that can be done under the
110118
 
          ** circumstances.
110119
 
          */
110120
 
          xUnlockNotify(aArg, nArg);
110121
 
          nArg = 0;
110122
 
        }
110123
 
      }
110124
 
      sqlite3EndBenignMalloc();
110125
 
 
110126
 
      aArg[nArg++] = p->pUnlockArg;
110127
 
      xUnlockNotify = p->xUnlockNotify;
110128
 
      p->pUnlockConnection = 0;
110129
 
      p->xUnlockNotify = 0;
110130
 
      p->pUnlockArg = 0;
110131
 
    }
110132
 
 
110133
 
    /* Step 3. */
110134
 
    if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
110135
 
      /* Remove connection p from the blocked connections list. */
110136
 
      *pp = p->pNextBlocked;
110137
 
      p->pNextBlocked = 0;
110138
 
    }else{
110139
 
      pp = &p->pNextBlocked;
110140
 
    }
110141
 
  }
110142
 
 
110143
 
  if( nArg!=0 ){
110144
 
    xUnlockNotify(aArg, nArg);
110145
 
  }
110146
 
  sqlite3_free(aDyn);
110147
 
  leaveMutex();         /* Leave STATIC_MASTER mutex */
110148
 
}
110149
 
 
110150
 
/*
110151
 
** This is called when the database connection passed as an argument is 
110152
 
** being closed. The connection is removed from the blocked list.
110153
 
*/
110154
 
SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
110155
 
  sqlite3ConnectionUnlocked(db);
110156
 
  enterMutex();
110157
 
  removeFromBlockedList(db);
110158
 
  checkListProperties(db);
110159
 
  leaveMutex();
110160
 
}
110161
 
#endif
110162
 
 
110163
 
/************** End of notify.c **********************************************/
110164
 
/************** Begin file fts3.c ********************************************/
110165
 
/*
110166
 
** 2006 Oct 10
110167
 
**
110168
 
** The author disclaims copyright to this source code.  In place of
110169
 
** a legal notice, here is a blessing:
110170
 
**
110171
 
**    May you do good and not evil.
110172
 
**    May you find forgiveness for yourself and forgive others.
110173
 
**    May you share freely, never taking more than you give.
110174
 
**
110175
 
******************************************************************************
110176
 
**
110177
 
** This is an SQLite module implementing full-text search.
110178
 
*/
110179
 
 
110180
 
/*
110181
 
** The code in this file is only compiled if:
110182
 
**
110183
 
**     * The FTS3 module is being built as an extension
110184
 
**       (in which case SQLITE_CORE is not defined), or
110185
 
**
110186
 
**     * The FTS3 module is being built into the core of
110187
 
**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
110188
 
*/
110189
 
 
110190
 
/* The full-text index is stored in a series of b+tree (-like)
110191
 
** structures called segments which map terms to doclists.  The
110192
 
** structures are like b+trees in layout, but are constructed from the
110193
 
** bottom up in optimal fashion and are not updatable.  Since trees
110194
 
** are built from the bottom up, things will be described from the
110195
 
** bottom up.
110196
 
**
110197
 
**
110198
 
**** Varints ****
110199
 
** The basic unit of encoding is a variable-length integer called a
110200
 
** varint.  We encode variable-length integers in little-endian order
110201
 
** using seven bits * per byte as follows:
110202
 
**
110203
 
** KEY:
110204
 
**         A = 0xxxxxxx    7 bits of data and one flag bit
110205
 
**         B = 1xxxxxxx    7 bits of data and one flag bit
110206
 
**
110207
 
**  7 bits - A
110208
 
** 14 bits - BA
110209
 
** 21 bits - BBA
110210
 
** and so on.
110211
 
**
110212
 
** This is similar in concept to how sqlite encodes "varints" but
110213
 
** the encoding is not the same.  SQLite varints are big-endian
110214
 
** are are limited to 9 bytes in length whereas FTS3 varints are
110215
 
** little-endian and can be up to 10 bytes in length (in theory).
110216
 
**
110217
 
** Example encodings:
110218
 
**
110219
 
**     1:    0x01
110220
 
**   127:    0x7f
110221
 
**   128:    0x81 0x00
110222
 
**
110223
 
**
110224
 
**** Document lists ****
110225
 
** A doclist (document list) holds a docid-sorted list of hits for a
110226
 
** given term.  Doclists hold docids and associated token positions.
110227
 
** A docid is the unique integer identifier for a single document.
110228
 
** A position is the index of a word within the document.  The first 
110229
 
** word of the document has a position of 0.
110230
 
**
110231
 
** FTS3 used to optionally store character offsets using a compile-time
110232
 
** option.  But that functionality is no longer supported.
110233
 
**
110234
 
** A doclist is stored like this:
110235
 
**
110236
 
** array {
110237
 
**   varint docid;
110238
 
**   array {                (position list for column 0)
110239
 
**     varint position;     (2 more than the delta from previous position)
110240
 
**   }
110241
 
**   array {
110242
 
**     varint POS_COLUMN;   (marks start of position list for new column)
110243
 
**     varint column;       (index of new column)
110244
 
**     array {
110245
 
**       varint position;   (2 more than the delta from previous position)
110246
 
**     }
110247
 
**   }
110248
 
**   varint POS_END;        (marks end of positions for this document.
110249
 
** }
110250
 
**
110251
 
** Here, array { X } means zero or more occurrences of X, adjacent in
110252
 
** memory.  A "position" is an index of a token in the token stream
110253
 
** generated by the tokenizer. Note that POS_END and POS_COLUMN occur 
110254
 
** in the same logical place as the position element, and act as sentinals
110255
 
** ending a position list array.  POS_END is 0.  POS_COLUMN is 1.
110256
 
** The positions numbers are not stored literally but rather as two more
110257
 
** than the difference from the prior position, or the just the position plus
110258
 
** 2 for the first position.  Example:
110259
 
**
110260
 
**   label:       A B C D E  F  G H   I  J K
110261
 
**   value:     123 5 9 1 1 14 35 0 234 72 0
110262
 
**
110263
 
** The 123 value is the first docid.  For column zero in this document
110264
 
** there are two matches at positions 3 and 10 (5-2 and 9-2+3).  The 1
110265
 
** at D signals the start of a new column; the 1 at E indicates that the
110266
 
** new column is column number 1.  There are two positions at 12 and 45
110267
 
** (14-2 and 35-2+12).  The 0 at H indicate the end-of-document.  The
110268
 
** 234 at I is the next docid.  It has one position 72 (72-2) and then
110269
 
** terminates with the 0 at K.
110270
 
**
110271
 
** A "position-list" is the list of positions for multiple columns for
110272
 
** a single docid.  A "column-list" is the set of positions for a single
110273
 
** column.  Hence, a position-list consists of one or more column-lists,
110274
 
** a document record consists of a docid followed by a position-list and
110275
 
** a doclist consists of one or more document records.
110276
 
**
110277
 
** A bare doclist omits the position information, becoming an 
110278
 
** array of varint-encoded docids.
110279
 
**
110280
 
**** Segment leaf nodes ****
110281
 
** Segment leaf nodes store terms and doclists, ordered by term.  Leaf
110282
 
** nodes are written using LeafWriter, and read using LeafReader (to
110283
 
** iterate through a single leaf node's data) and LeavesReader (to
110284
 
** iterate through a segment's entire leaf layer).  Leaf nodes have
110285
 
** the format:
110286
 
**
110287
 
** varint iHeight;             (height from leaf level, always 0)
110288
 
** varint nTerm;               (length of first term)
110289
 
** char pTerm[nTerm];          (content of first term)
110290
 
** varint nDoclist;            (length of term's associated doclist)
110291
 
** char pDoclist[nDoclist];    (content of doclist)
110292
 
** array {
110293
 
**                             (further terms are delta-encoded)
110294
 
**   varint nPrefix;           (length of prefix shared with previous term)
110295
 
**   varint nSuffix;           (length of unshared suffix)
110296
 
**   char pTermSuffix[nSuffix];(unshared suffix of next term)
110297
 
**   varint nDoclist;          (length of term's associated doclist)
110298
 
**   char pDoclist[nDoclist];  (content of doclist)
110299
 
** }
110300
 
**
110301
 
** Here, array { X } means zero or more occurrences of X, adjacent in
110302
 
** memory.
110303
 
**
110304
 
** Leaf nodes are broken into blocks which are stored contiguously in
110305
 
** the %_segments table in sorted order.  This means that when the end
110306
 
** of a node is reached, the next term is in the node with the next
110307
 
** greater node id.
110308
 
**
110309
 
** New data is spilled to a new leaf node when the current node
110310
 
** exceeds LEAF_MAX bytes (default 2048).  New data which itself is
110311
 
** larger than STANDALONE_MIN (default 1024) is placed in a standalone
110312
 
** node (a leaf node with a single term and doclist).  The goal of
110313
 
** these settings is to pack together groups of small doclists while
110314
 
** making it efficient to directly access large doclists.  The
110315
 
** assumption is that large doclists represent terms which are more
110316
 
** likely to be query targets.
110317
 
**
110318
 
** TODO(shess) It may be useful for blocking decisions to be more
110319
 
** dynamic.  For instance, it may make more sense to have a 2.5k leaf
110320
 
** node rather than splitting into 2k and .5k nodes.  My intuition is
110321
 
** that this might extend through 2x or 4x the pagesize.
110322
 
**
110323
 
**
110324
 
**** Segment interior nodes ****
110325
 
** Segment interior nodes store blockids for subtree nodes and terms
110326
 
** to describe what data is stored by the each subtree.  Interior
110327
 
** nodes are written using InteriorWriter, and read using
110328
 
** InteriorReader.  InteriorWriters are created as needed when
110329
 
** SegmentWriter creates new leaf nodes, or when an interior node
110330
 
** itself grows too big and must be split.  The format of interior
110331
 
** nodes:
110332
 
**
110333
 
** varint iHeight;           (height from leaf level, always >0)
110334
 
** varint iBlockid;          (block id of node's leftmost subtree)
110335
 
** optional {
110336
 
**   varint nTerm;           (length of first term)
110337
 
**   char pTerm[nTerm];      (content of first term)
110338
 
**   array {
110339
 
**                                (further terms are delta-encoded)
110340
 
**     varint nPrefix;            (length of shared prefix with previous term)
110341
 
**     varint nSuffix;            (length of unshared suffix)
110342
 
**     char pTermSuffix[nSuffix]; (unshared suffix of next term)
110343
 
**   }
110344
 
** }
110345
 
**
110346
 
** Here, optional { X } means an optional element, while array { X }
110347
 
** means zero or more occurrences of X, adjacent in memory.
110348
 
**
110349
 
** An interior node encodes n terms separating n+1 subtrees.  The
110350
 
** subtree blocks are contiguous, so only the first subtree's blockid
110351
 
** is encoded.  The subtree at iBlockid will contain all terms less
110352
 
** than the first term encoded (or all terms if no term is encoded).
110353
 
** Otherwise, for terms greater than or equal to pTerm[i] but less
110354
 
** than pTerm[i+1], the subtree for that term will be rooted at
110355
 
** iBlockid+i.  Interior nodes only store enough term data to
110356
 
** distinguish adjacent children (if the rightmost term of the left
110357
 
** child is "something", and the leftmost term of the right child is
110358
 
** "wicked", only "w" is stored).
110359
 
**
110360
 
** New data is spilled to a new interior node at the same height when
110361
 
** the current node exceeds INTERIOR_MAX bytes (default 2048).
110362
 
** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
110363
 
** interior nodes and making the tree too skinny.  The interior nodes
110364
 
** at a given height are naturally tracked by interior nodes at
110365
 
** height+1, and so on.
110366
 
**
110367
 
**
110368
 
**** Segment directory ****
110369
 
** The segment directory in table %_segdir stores meta-information for
110370
 
** merging and deleting segments, and also the root node of the
110371
 
** segment's tree.
110372
 
**
110373
 
** The root node is the top node of the segment's tree after encoding
110374
 
** the entire segment, restricted to ROOT_MAX bytes (default 1024).
110375
 
** This could be either a leaf node or an interior node.  If the top
110376
 
** node requires more than ROOT_MAX bytes, it is flushed to %_segments
110377
 
** and a new root interior node is generated (which should always fit
110378
 
** within ROOT_MAX because it only needs space for 2 varints, the
110379
 
** height and the blockid of the previous root).
110380
 
**
110381
 
** The meta-information in the segment directory is:
110382
 
**   level               - segment level (see below)
110383
 
**   idx                 - index within level
110384
 
**                       - (level,idx uniquely identify a segment)
110385
 
**   start_block         - first leaf node
110386
 
**   leaves_end_block    - last leaf node
110387
 
**   end_block           - last block (including interior nodes)
110388
 
**   root                - contents of root node
110389
 
**
110390
 
** If the root node is a leaf node, then start_block,
110391
 
** leaves_end_block, and end_block are all 0.
110392
 
**
110393
 
**
110394
 
**** Segment merging ****
110395
 
** To amortize update costs, segments are grouped into levels and
110396
 
** merged in batches.  Each increase in level represents exponentially
110397
 
** more documents.
110398
 
**
110399
 
** New documents (actually, document updates) are tokenized and
110400
 
** written individually (using LeafWriter) to a level 0 segment, with
110401
 
** incrementing idx.  When idx reaches MERGE_COUNT (default 16), all
110402
 
** level 0 segments are merged into a single level 1 segment.  Level 1
110403
 
** is populated like level 0, and eventually MERGE_COUNT level 1
110404
 
** segments are merged to a single level 2 segment (representing
110405
 
** MERGE_COUNT^2 updates), and so on.
110406
 
**
110407
 
** A segment merge traverses all segments at a given level in
110408
 
** parallel, performing a straightforward sorted merge.  Since segment
110409
 
** leaf nodes are written in to the %_segments table in order, this
110410
 
** merge traverses the underlying sqlite disk structures efficiently.
110411
 
** After the merge, all segment blocks from the merged level are
110412
 
** deleted.
110413
 
**
110414
 
** MERGE_COUNT controls how often we merge segments.  16 seems to be
110415
 
** somewhat of a sweet spot for insertion performance.  32 and 64 show
110416
 
** very similar performance numbers to 16 on insertion, though they're
110417
 
** a tiny bit slower (perhaps due to more overhead in merge-time
110418
 
** sorting).  8 is about 20% slower than 16, 4 about 50% slower than
110419
 
** 16, 2 about 66% slower than 16.
110420
 
**
110421
 
** At query time, high MERGE_COUNT increases the number of segments
110422
 
** which need to be scanned and merged.  For instance, with 100k docs
110423
 
** inserted:
110424
 
**
110425
 
**    MERGE_COUNT   segments
110426
 
**       16           25
110427
 
**        8           12
110428
 
**        4           10
110429
 
**        2            6
110430
 
**
110431
 
** This appears to have only a moderate impact on queries for very
110432
 
** frequent terms (which are somewhat dominated by segment merge
110433
 
** costs), and infrequent and non-existent terms still seem to be fast
110434
 
** even with many segments.
110435
 
**
110436
 
** TODO(shess) That said, it would be nice to have a better query-side
110437
 
** argument for MERGE_COUNT of 16.  Also, it is possible/likely that
110438
 
** optimizations to things like doclist merging will swing the sweet
110439
 
** spot around.
110440
 
**
110441
 
**
110442
 
**
110443
 
**** Handling of deletions and updates ****
110444
 
** Since we're using a segmented structure, with no docid-oriented
110445
 
** index into the term index, we clearly cannot simply update the term
110446
 
** index when a document is deleted or updated.  For deletions, we
110447
 
** write an empty doclist (varint(docid) varint(POS_END)), for updates
110448
 
** we simply write the new doclist.  Segment merges overwrite older
110449
 
** data for a particular docid with newer data, so deletes or updates
110450
 
** will eventually overtake the earlier data and knock it out.  The
110451
 
** query logic likewise merges doclists so that newer data knocks out
110452
 
** older data.
110453
 
**
110454
 
** TODO(shess) Provide a VACUUM type operation to clear out all
110455
 
** deletions and duplications.  This would basically be a forced merge
110456
 
** into a single segment.
110457
 
*/
110458
 
 
110459
 
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
110460
 
 
110461
 
#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
110462
 
# define SQLITE_CORE 1
110463
 
#endif
110464
 
 
110465
 
/************** Include fts3Int.h in the middle of fts3.c ********************/
110466
 
/************** Begin file fts3Int.h *****************************************/
110467
 
/*
110468
 
** 2009 Nov 12
110469
 
**
110470
 
** The author disclaims copyright to this source code.  In place of
110471
 
** a legal notice, here is a blessing:
110472
 
**
110473
 
**    May you do good and not evil.
110474
 
**    May you find forgiveness for yourself and forgive others.
110475
 
**    May you share freely, never taking more than you give.
110476
 
**
110477
 
******************************************************************************
110478
 
**
110479
 
*/
110480
 
 
110481
 
#ifndef _FTSINT_H
110482
 
#define _FTSINT_H
110483
 
 
110484
 
#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
110485
 
# define NDEBUG 1
110486
 
#endif
110487
 
 
110488
 
/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
110489
 
/************** Begin file fts3_tokenizer.h **********************************/
110490
 
/*
110491
 
** 2006 July 10
110492
 
**
110493
 
** The author disclaims copyright to this source code.
110494
 
**
110495
 
*************************************************************************
110496
 
** Defines the interface to tokenizers used by fulltext-search.  There
110497
 
** are three basic components:
110498
 
**
110499
 
** sqlite3_tokenizer_module is a singleton defining the tokenizer
110500
 
** interface functions.  This is essentially the class structure for
110501
 
** tokenizers.
110502
 
**
110503
 
** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
110504
 
** including customization information defined at creation time.
110505
 
**
110506
 
** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
110507
 
** tokens from a particular input.
110508
 
*/
110509
 
#ifndef _FTS3_TOKENIZER_H_
110510
 
#define _FTS3_TOKENIZER_H_
110511
 
 
110512
 
/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
110513
 
** If tokenizers are to be allowed to call sqlite3_*() functions, then
110514
 
** we will need a way to register the API consistently.
110515
 
*/
110516
 
 
110517
 
/*
110518
 
** Structures used by the tokenizer interface. When a new tokenizer
110519
 
** implementation is registered, the caller provides a pointer to
110520
 
** an sqlite3_tokenizer_module containing pointers to the callback
110521
 
** functions that make up an implementation.
110522
 
**
110523
 
** When an fts3 table is created, it passes any arguments passed to
110524
 
** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
110525
 
** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
110526
 
** implementation. The xCreate() function in turn returns an 
110527
 
** sqlite3_tokenizer structure representing the specific tokenizer to
110528
 
** be used for the fts3 table (customized by the tokenizer clause arguments).
110529
 
**
110530
 
** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
110531
 
** method is called. It returns an sqlite3_tokenizer_cursor object
110532
 
** that may be used to tokenize a specific input buffer based on
110533
 
** the tokenization rules supplied by a specific sqlite3_tokenizer
110534
 
** object.
110535
 
*/
110536
 
typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
110537
 
typedef struct sqlite3_tokenizer sqlite3_tokenizer;
110538
 
typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
110539
 
 
110540
 
struct sqlite3_tokenizer_module {
110541
 
 
110542
 
  /*
110543
 
  ** Structure version. Should always be set to 0.
110544
 
  */
110545
 
  int iVersion;
110546
 
 
110547
 
  /*
110548
 
  ** Create a new tokenizer. The values in the argv[] array are the
110549
 
  ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
110550
 
  ** TABLE statement that created the fts3 table. For example, if
110551
 
  ** the following SQL is executed:
110552
 
  **
110553
 
  **   CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
110554
 
  **
110555
 
  ** then argc is set to 2, and the argv[] array contains pointers
110556
 
  ** to the strings "arg1" and "arg2".
110557
 
  **
110558
 
  ** This method should return either SQLITE_OK (0), or an SQLite error 
110559
 
  ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
110560
 
  ** to point at the newly created tokenizer structure. The generic
110561
 
  ** sqlite3_tokenizer.pModule variable should not be initialised by
110562
 
  ** this callback. The caller will do so.
110563
 
  */
110564
 
  int (*xCreate)(
110565
 
    int argc,                           /* Size of argv array */
110566
 
    const char *const*argv,             /* Tokenizer argument strings */
110567
 
    sqlite3_tokenizer **ppTokenizer     /* OUT: Created tokenizer */
110568
 
  );
110569
 
 
110570
 
  /*
110571
 
  ** Destroy an existing tokenizer. The fts3 module calls this method
110572
 
  ** exactly once for each successful call to xCreate().
110573
 
  */
110574
 
  int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
110575
 
 
110576
 
  /*
110577
 
  ** Create a tokenizer cursor to tokenize an input buffer. The caller
110578
 
  ** is responsible for ensuring that the input buffer remains valid
110579
 
  ** until the cursor is closed (using the xClose() method). 
110580
 
  */
110581
 
  int (*xOpen)(
110582
 
    sqlite3_tokenizer *pTokenizer,       /* Tokenizer object */
110583
 
    const char *pInput, int nBytes,      /* Input buffer */
110584
 
    sqlite3_tokenizer_cursor **ppCursor  /* OUT: Created tokenizer cursor */
110585
 
  );
110586
 
 
110587
 
  /*
110588
 
  ** Destroy an existing tokenizer cursor. The fts3 module calls this 
110589
 
  ** method exactly once for each successful call to xOpen().
110590
 
  */
110591
 
  int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
110592
 
 
110593
 
  /*
110594
 
  ** Retrieve the next token from the tokenizer cursor pCursor. This
110595
 
  ** method should either return SQLITE_OK and set the values of the
110596
 
  ** "OUT" variables identified below, or SQLITE_DONE to indicate that
110597
 
  ** the end of the buffer has been reached, or an SQLite error code.
110598
 
  **
110599
 
  ** *ppToken should be set to point at a buffer containing the 
110600
 
  ** normalized version of the token (i.e. after any case-folding and/or
110601
 
  ** stemming has been performed). *pnBytes should be set to the length
110602
 
  ** of this buffer in bytes. The input text that generated the token is
110603
 
  ** identified by the byte offsets returned in *piStartOffset and
110604
 
  ** *piEndOffset. *piStartOffset should be set to the index of the first
110605
 
  ** byte of the token in the input buffer. *piEndOffset should be set
110606
 
  ** to the index of the first byte just past the end of the token in
110607
 
  ** the input buffer.
110608
 
  **
110609
 
  ** The buffer *ppToken is set to point at is managed by the tokenizer
110610
 
  ** implementation. It is only required to be valid until the next call
110611
 
  ** to xNext() or xClose(). 
110612
 
  */
110613
 
  /* TODO(shess) current implementation requires pInput to be
110614
 
  ** nul-terminated.  This should either be fixed, or pInput/nBytes
110615
 
  ** should be converted to zInput.
110616
 
  */
110617
 
  int (*xNext)(
110618
 
    sqlite3_tokenizer_cursor *pCursor,   /* Tokenizer cursor */
110619
 
    const char **ppToken, int *pnBytes,  /* OUT: Normalized text for token */
110620
 
    int *piStartOffset,  /* OUT: Byte offset of token in input buffer */
110621
 
    int *piEndOffset,    /* OUT: Byte offset of end of token in input buffer */
110622
 
    int *piPosition      /* OUT: Number of tokens returned before this one */
110623
 
  );
110624
 
};
110625
 
 
110626
 
struct sqlite3_tokenizer {
110627
 
  const sqlite3_tokenizer_module *pModule;  /* The module for this tokenizer */
110628
 
  /* Tokenizer implementations will typically add additional fields */
110629
 
};
110630
 
 
110631
 
struct sqlite3_tokenizer_cursor {
110632
 
  sqlite3_tokenizer *pTokenizer;       /* Tokenizer for this cursor. */
110633
 
  /* Tokenizer implementations will typically add additional fields */
110634
 
};
110635
 
 
110636
 
int fts3_global_term_cnt(int iTerm, int iCol);
110637
 
int fts3_term_cnt(int iTerm, int iCol);
110638
 
 
110639
 
 
110640
 
#endif /* _FTS3_TOKENIZER_H_ */
110641
 
 
110642
 
/************** End of fts3_tokenizer.h **************************************/
110643
 
/************** Continuing where we left off in fts3Int.h ********************/
110644
 
/************** Include fts3_hash.h in the middle of fts3Int.h ***************/
110645
 
/************** Begin file fts3_hash.h ***************************************/
110646
 
/*
110647
 
** 2001 September 22
110648
 
**
110649
 
** The author disclaims copyright to this source code.  In place of
110650
 
** a legal notice, here is a blessing:
110651
 
**
110652
 
**    May you do good and not evil.
110653
 
**    May you find forgiveness for yourself and forgive others.
110654
 
**    May you share freely, never taking more than you give.
110655
 
**
110656
 
*************************************************************************
110657
 
** This is the header file for the generic hash-table implemenation
110658
 
** used in SQLite.  We've modified it slightly to serve as a standalone
110659
 
** hash table implementation for the full-text indexing module.
110660
 
**
110661
 
*/
110662
 
#ifndef _FTS3_HASH_H_
110663
 
#define _FTS3_HASH_H_
110664
 
 
110665
 
/* Forward declarations of structures. */
110666
 
typedef struct Fts3Hash Fts3Hash;
110667
 
typedef struct Fts3HashElem Fts3HashElem;
110668
 
 
110669
 
/* A complete hash table is an instance of the following structure.
110670
 
** The internals of this structure are intended to be opaque -- client
110671
 
** code should not attempt to access or modify the fields of this structure
110672
 
** directly.  Change this structure only by using the routines below.
110673
 
** However, many of the "procedures" and "functions" for modifying and
110674
 
** accessing this structure are really macros, so we can't really make
110675
 
** this structure opaque.
110676
 
*/
110677
 
struct Fts3Hash {
110678
 
  char keyClass;          /* HASH_INT, _POINTER, _STRING, _BINARY */
110679
 
  char copyKey;           /* True if copy of key made on insert */
110680
 
  int count;              /* Number of entries in this table */
110681
 
  Fts3HashElem *first;    /* The first element of the array */
110682
 
  int htsize;             /* Number of buckets in the hash table */
110683
 
  struct _fts3ht {        /* the hash table */
110684
 
    int count;               /* Number of entries with this hash */
110685
 
    Fts3HashElem *chain;     /* Pointer to first entry with this hash */
110686
 
  } *ht;
110687
 
};
110688
 
 
110689
 
/* Each element in the hash table is an instance of the following 
110690
 
** structure.  All elements are stored on a single doubly-linked list.
110691
 
**
110692
 
** Again, this structure is intended to be opaque, but it can't really
110693
 
** be opaque because it is used by macros.
110694
 
*/
110695
 
struct Fts3HashElem {
110696
 
  Fts3HashElem *next, *prev; /* Next and previous elements in the table */
110697
 
  void *data;                /* Data associated with this element */
110698
 
  void *pKey; int nKey;      /* Key associated with this element */
110699
 
};
110700
 
 
110701
 
/*
110702
 
** There are 2 different modes of operation for a hash table:
110703
 
**
110704
 
**   FTS3_HASH_STRING        pKey points to a string that is nKey bytes long
110705
 
**                           (including the null-terminator, if any).  Case
110706
 
**                           is respected in comparisons.
110707
 
**
110708
 
**   FTS3_HASH_BINARY        pKey points to binary data nKey bytes long. 
110709
 
**                           memcmp() is used to compare keys.
110710
 
**
110711
 
** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.  
110712
 
*/
110713
 
#define FTS3_HASH_STRING    1
110714
 
#define FTS3_HASH_BINARY    2
110715
 
 
110716
 
/*
110717
 
** Access routines.  To delete, insert a NULL pointer.
110718
 
*/
110719
 
SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
110720
 
SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
110721
 
SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
110722
 
SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
110723
 
SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
110724
 
 
110725
 
/*
110726
 
** Shorthand for the functions above
110727
 
*/
110728
 
#define fts3HashInit     sqlite3Fts3HashInit
110729
 
#define fts3HashInsert   sqlite3Fts3HashInsert
110730
 
#define fts3HashFind     sqlite3Fts3HashFind
110731
 
#define fts3HashClear    sqlite3Fts3HashClear
110732
 
#define fts3HashFindElem sqlite3Fts3HashFindElem
110733
 
 
110734
 
/*
110735
 
** Macros for looping over all elements of a hash table.  The idiom is
110736
 
** like this:
110737
 
**
110738
 
**   Fts3Hash h;
110739
 
**   Fts3HashElem *p;
110740
 
**   ...
110741
 
**   for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
110742
 
**     SomeStructure *pData = fts3HashData(p);
110743
 
**     // do something with pData
110744
 
**   }
110745
 
*/
110746
 
#define fts3HashFirst(H)  ((H)->first)
110747
 
#define fts3HashNext(E)   ((E)->next)
110748
 
#define fts3HashData(E)   ((E)->data)
110749
 
#define fts3HashKey(E)    ((E)->pKey)
110750
 
#define fts3HashKeysize(E) ((E)->nKey)
110751
 
 
110752
 
/*
110753
 
** Number of entries in a hash table
110754
 
*/
110755
 
#define fts3HashCount(H)  ((H)->count)
110756
 
 
110757
 
#endif /* _FTS3_HASH_H_ */
110758
 
 
110759
 
/************** End of fts3_hash.h *******************************************/
110760
 
/************** Continuing where we left off in fts3Int.h ********************/
110761
 
 
110762
 
/*
110763
 
** This constant controls how often segments are merged. Once there are
110764
 
** FTS3_MERGE_COUNT segments of level N, they are merged into a single
110765
 
** segment of level N+1.
110766
 
*/
110767
 
#define FTS3_MERGE_COUNT 16
110768
 
 
110769
 
/*
110770
 
** This is the maximum amount of data (in bytes) to store in the 
110771
 
** Fts3Table.pendingTerms hash table. Normally, the hash table is
110772
 
** populated as documents are inserted/updated/deleted in a transaction
110773
 
** and used to create a new segment when the transaction is committed.
110774
 
** However if this limit is reached midway through a transaction, a new 
110775
 
** segment is created and the hash table cleared immediately.
110776
 
*/
110777
 
#define FTS3_MAX_PENDING_DATA (1*1024*1024)
110778
 
 
110779
 
/*
110780
 
** Macro to return the number of elements in an array. SQLite has a
110781
 
** similar macro called ArraySize(). Use a different name to avoid
110782
 
** a collision when building an amalgamation with built-in FTS3.
110783
 
*/
110784
 
#define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
110785
 
 
110786
 
/*
110787
 
** Maximum length of a varint encoded integer. The varint format is different
110788
 
** from that used by SQLite, so the maximum length is 10, not 9.
110789
 
*/
110790
 
#define FTS3_VARINT_MAX 10
110791
 
 
110792
 
/*
110793
 
** The testcase() macro is only used by the amalgamation.  If undefined,
110794
 
** make it a no-op.
110795
 
*/
110796
 
#ifndef testcase
110797
 
# define testcase(X)
110798
 
#endif
110799
 
 
110800
 
/*
110801
 
** Terminator values for position-lists and column-lists.
110802
 
*/
110803
 
#define POS_COLUMN  (1)     /* Column-list terminator */
110804
 
#define POS_END     (0)     /* Position-list terminator */ 
110805
 
 
110806
 
/*
110807
 
** This section provides definitions to allow the
110808
 
** FTS3 extension to be compiled outside of the 
110809
 
** amalgamation.
110810
 
*/
110811
 
#ifndef SQLITE_AMALGAMATION
110812
 
/*
110813
 
** Macros indicating that conditional expressions are always true or
110814
 
** false.
110815
 
*/
110816
 
#ifdef SQLITE_COVERAGE_TEST
110817
 
# define ALWAYS(x) (1)
110818
 
# define NEVER(X)  (0)
110819
 
#else
110820
 
# define ALWAYS(x) (x)
110821
 
# define NEVER(X)  (x)
110822
 
#endif
110823
 
 
110824
 
/*
110825
 
** Internal types used by SQLite.
110826
 
*/
110827
 
typedef unsigned char u8;         /* 1-byte (or larger) unsigned integer */
110828
 
typedef short int i16;            /* 2-byte (or larger) signed integer */
110829
 
typedef unsigned int u32;         /* 4-byte unsigned integer */
110830
 
typedef sqlite3_uint64 u64;       /* 8-byte unsigned integer */
110831
 
/*
110832
 
** Macro used to suppress compiler warnings for unused parameters.
110833
 
*/
110834
 
#define UNUSED_PARAMETER(x) (void)(x)
110835
 
#endif
110836
 
 
110837
 
typedef struct Fts3Table Fts3Table;
110838
 
typedef struct Fts3Cursor Fts3Cursor;
110839
 
typedef struct Fts3Expr Fts3Expr;
110840
 
typedef struct Fts3Phrase Fts3Phrase;
110841
 
typedef struct Fts3PhraseToken Fts3PhraseToken;
110842
 
 
110843
 
typedef struct Fts3SegFilter Fts3SegFilter;
110844
 
typedef struct Fts3DeferredToken Fts3DeferredToken;
110845
 
typedef struct Fts3SegReader Fts3SegReader;
110846
 
typedef struct Fts3SegReaderCursor Fts3SegReaderCursor;
110847
 
 
110848
 
/*
110849
 
** A connection to a fulltext index is an instance of the following
110850
 
** structure. The xCreate and xConnect methods create an instance
110851
 
** of this structure and xDestroy and xDisconnect free that instance.
110852
 
** All other methods receive a pointer to the structure as one of their
110853
 
** arguments.
110854
 
*/
110855
 
struct Fts3Table {
110856
 
  sqlite3_vtab base;              /* Base class used by SQLite core */
110857
 
  sqlite3 *db;                    /* The database connection */
110858
 
  const char *zDb;                /* logical database name */
110859
 
  const char *zName;              /* virtual table name */
110860
 
  int nColumn;                    /* number of named columns in virtual table */
110861
 
  char **azColumn;                /* column names.  malloced */
110862
 
  sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
110863
 
 
110864
 
  /* Precompiled statements used by the implementation. Each of these 
110865
 
  ** statements is run and reset within a single virtual table API call. 
110866
 
  */
110867
 
  sqlite3_stmt *aStmt[24];
110868
 
 
110869
 
  char *zReadExprlist;
110870
 
  char *zWriteExprlist;
110871
 
 
110872
 
  int nNodeSize;                  /* Soft limit for node size */
110873
 
  u8 bHasStat;                    /* True if %_stat table exists */
110874
 
  u8 bHasDocsize;                 /* True if %_docsize table exists */
110875
 
  int nPgsz;                      /* Page size for host database */
110876
 
  char *zSegmentsTbl;             /* Name of %_segments table */
110877
 
  sqlite3_blob *pSegments;        /* Blob handle open on %_segments table */
110878
 
 
110879
 
  /* The following hash table is used to buffer pending index updates during
110880
 
  ** transactions. Variable nPendingData estimates the memory size of the 
110881
 
  ** pending data, including hash table overhead, but not malloc overhead. 
110882
 
  ** When nPendingData exceeds nMaxPendingData, the buffer is flushed 
110883
 
  ** automatically. Variable iPrevDocid is the docid of the most recently
110884
 
  ** inserted record.
110885
 
  */
110886
 
  int nMaxPendingData;
110887
 
  int nPendingData;
110888
 
  sqlite_int64 iPrevDocid;
110889
 
  Fts3Hash pendingTerms;
110890
 
};
110891
 
 
110892
 
/*
110893
 
** When the core wants to read from the virtual table, it creates a
110894
 
** virtual table cursor (an instance of the following structure) using
110895
 
** the xOpen method. Cursors are destroyed using the xClose method.
110896
 
*/
110897
 
struct Fts3Cursor {
110898
 
  sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
110899
 
  i16 eSearch;                    /* Search strategy (see below) */
110900
 
  u8 isEof;                       /* True if at End Of Results */
110901
 
  u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
110902
 
  sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
110903
 
  Fts3Expr *pExpr;                /* Parsed MATCH query string */
110904
 
  int nPhrase;                    /* Number of matchable phrases in query */
110905
 
  Fts3DeferredToken *pDeferred;   /* Deferred search tokens, if any */
110906
 
  sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
110907
 
  char *pNextId;                  /* Pointer into the body of aDoclist */
110908
 
  char *aDoclist;                 /* List of docids for full-text queries */
110909
 
  int nDoclist;                   /* Size of buffer at aDoclist */
110910
 
  int eEvalmode;                  /* An FTS3_EVAL_XX constant */
110911
 
  int nRowAvg;                    /* Average size of database rows, in pages */
110912
 
 
110913
 
  int isMatchinfoNeeded;          /* True when aMatchinfo[] needs filling in */
110914
 
  u32 *aMatchinfo;                /* Information about most recent match */
110915
 
  int nMatchinfo;                 /* Number of elements in aMatchinfo[] */
110916
 
  char *zMatchinfo;               /* Matchinfo specification */
110917
 
};
110918
 
 
110919
 
#define FTS3_EVAL_FILTER    0
110920
 
#define FTS3_EVAL_NEXT      1
110921
 
#define FTS3_EVAL_MATCHINFO 2
110922
 
 
110923
 
/*
110924
 
** The Fts3Cursor.eSearch member is always set to one of the following.
110925
 
** Actualy, Fts3Cursor.eSearch can be greater than or equal to
110926
 
** FTS3_FULLTEXT_SEARCH.  If so, then Fts3Cursor.eSearch - 2 is the index
110927
 
** of the column to be searched.  For example, in
110928
 
**
110929
 
**     CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
110930
 
**     SELECT docid FROM ex1 WHERE b MATCH 'one two three';
110931
 
** 
110932
 
** Because the LHS of the MATCH operator is 2nd column "b",
110933
 
** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1.  (+0 for a,
110934
 
** +1 for b, +2 for c, +3 for d.)  If the LHS of MATCH were "ex1" 
110935
 
** indicating that all columns should be searched,
110936
 
** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
110937
 
*/
110938
 
#define FTS3_FULLSCAN_SEARCH 0    /* Linear scan of %_content table */
110939
 
#define FTS3_DOCID_SEARCH    1    /* Lookup by rowid on %_content table */
110940
 
#define FTS3_FULLTEXT_SEARCH 2    /* Full-text index search */
110941
 
 
110942
 
/*
110943
 
** A "phrase" is a sequence of one or more tokens that must match in
110944
 
** sequence.  A single token is the base case and the most common case.
110945
 
** For a sequence of tokens contained in double-quotes (i.e. "one two three")
110946
 
** nToken will be the number of tokens in the string.
110947
 
**
110948
 
** The nDocMatch and nMatch variables contain data that may be used by the
110949
 
** matchinfo() function. They are populated when the full-text index is 
110950
 
** queried for hits on the phrase. If one or more tokens in the phrase
110951
 
** are deferred, the nDocMatch and nMatch variables are populated based
110952
 
** on the assumption that the 
110953
 
*/
110954
 
struct Fts3PhraseToken {
110955
 
  char *z;                        /* Text of the token */
110956
 
  int n;                          /* Number of bytes in buffer z */
110957
 
  int isPrefix;                   /* True if token ends with a "*" character */
110958
 
  int bFulltext;                  /* True if full-text index was used */
110959
 
  Fts3SegReaderCursor *pSegcsr;   /* Segment-reader for this token */
110960
 
  Fts3DeferredToken *pDeferred;   /* Deferred token object for this token */
110961
 
};
110962
 
 
110963
 
struct Fts3Phrase {
110964
 
  /* Variables populated by fts3_expr.c when parsing a MATCH expression */
110965
 
  int nToken;                /* Number of tokens in the phrase */
110966
 
  int iColumn;               /* Index of column this phrase must match */
110967
 
  int isNot;                 /* Phrase prefixed by unary not (-) operator */
110968
 
  Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
110969
 
};
110970
 
 
110971
 
/*
110972
 
** A tree of these objects forms the RHS of a MATCH operator.
110973
 
**
110974
 
** If Fts3Expr.eType is either FTSQUERY_NEAR or FTSQUERY_PHRASE and isLoaded
110975
 
** is true, then aDoclist points to a malloced buffer, size nDoclist bytes, 
110976
 
** containing the results of the NEAR or phrase query in FTS3 doclist
110977
 
** format. As usual, the initial "Length" field found in doclists stored
110978
 
** on disk is omitted from this buffer.
110979
 
**
110980
 
** Variable pCurrent always points to the start of a docid field within
110981
 
** aDoclist. Since the doclist is usually scanned in docid order, this can
110982
 
** be used to accelerate seeking to the required docid within the doclist.
110983
 
*/
110984
 
struct Fts3Expr {
110985
 
  int eType;                 /* One of the FTSQUERY_XXX values defined below */
110986
 
  int nNear;                 /* Valid if eType==FTSQUERY_NEAR */
110987
 
  Fts3Expr *pParent;         /* pParent->pLeft==this or pParent->pRight==this */
110988
 
  Fts3Expr *pLeft;           /* Left operand */
110989
 
  Fts3Expr *pRight;          /* Right operand */
110990
 
  Fts3Phrase *pPhrase;       /* Valid if eType==FTSQUERY_PHRASE */
110991
 
 
110992
 
  int isLoaded;              /* True if aDoclist/nDoclist are initialized. */
110993
 
  char *aDoclist;            /* Buffer containing doclist */
110994
 
  int nDoclist;              /* Size of aDoclist in bytes */
110995
 
 
110996
 
  sqlite3_int64 iCurrent;
110997
 
  char *pCurrent;
110998
 
};
110999
 
 
111000
 
/*
111001
 
** Candidate values for Fts3Query.eType. Note that the order of the first
111002
 
** four values is in order of precedence when parsing expressions. For 
111003
 
** example, the following:
111004
 
**
111005
 
**   "a OR b AND c NOT d NEAR e"
111006
 
**
111007
 
** is equivalent to:
111008
 
**
111009
 
**   "a OR (b AND (c NOT (d NEAR e)))"
111010
 
*/
111011
 
#define FTSQUERY_NEAR   1
111012
 
#define FTSQUERY_NOT    2
111013
 
#define FTSQUERY_AND    3
111014
 
#define FTSQUERY_OR     4
111015
 
#define FTSQUERY_PHRASE 5
111016
 
 
111017
 
 
111018
 
/* fts3_write.c */
111019
 
SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
111020
 
SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
111021
 
SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
111022
 
SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
111023
 
SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, sqlite3_int64,
111024
 
  sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
111025
 
SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(Fts3Table*,const char*,int,int,Fts3SegReader**);
111026
 
SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
111027
 
SQLITE_PRIVATE int sqlite3Fts3SegReaderCost(Fts3Cursor *, Fts3SegReader *, int *);
111028
 
SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, sqlite3_stmt **);
111029
 
SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *);
111030
 
SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*);
111031
 
 
111032
 
SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
111033
 
SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
111034
 
 
111035
 
SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
111036
 
SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
111037
 
SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
111038
 
SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
111039
 
SQLITE_PRIVATE char *sqlite3Fts3DeferredDoclist(Fts3DeferredToken *, int *);
111040
 
SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
111041
 
 
111042
 
#define FTS3_SEGCURSOR_PENDING -1
111043
 
#define FTS3_SEGCURSOR_ALL     -2
111044
 
 
111045
 
SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3SegReaderCursor*, Fts3SegFilter*);
111046
 
SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3SegReaderCursor *);
111047
 
SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3SegReaderCursor *);
111048
 
SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
111049
 
    Fts3Table *, int, const char *, int, int, int, Fts3SegReaderCursor *);
111050
 
 
111051
 
/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
111052
 
#define FTS3_SEGMENT_REQUIRE_POS   0x00000001
111053
 
#define FTS3_SEGMENT_IGNORE_EMPTY  0x00000002
111054
 
#define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
111055
 
#define FTS3_SEGMENT_PREFIX        0x00000008
111056
 
#define FTS3_SEGMENT_SCAN          0x00000010
111057
 
 
111058
 
/* Type passed as 4th argument to SegmentReaderIterate() */
111059
 
struct Fts3SegFilter {
111060
 
  const char *zTerm;
111061
 
  int nTerm;
111062
 
  int iCol;
111063
 
  int flags;
111064
 
};
111065
 
 
111066
 
struct Fts3SegReaderCursor {
111067
 
  /* Used internally by sqlite3Fts3SegReaderXXX() calls */
111068
 
  Fts3SegReader **apSegment;      /* Array of Fts3SegReader objects */
111069
 
  int nSegment;                   /* Size of apSegment array */
111070
 
  int nAdvance;                   /* How many seg-readers to advance */
111071
 
  Fts3SegFilter *pFilter;         /* Pointer to filter object */
111072
 
  char *aBuffer;                  /* Buffer to merge doclists in */
111073
 
  int nBuffer;                    /* Allocated size of aBuffer[] in bytes */
111074
 
 
111075
 
  /* Cost of running this iterator. Used by fts3.c only. */
111076
 
  int nCost;
111077
 
 
111078
 
  /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
111079
 
  char *zTerm;                    /* Pointer to term buffer */
111080
 
  int nTerm;                      /* Size of zTerm in bytes */
111081
 
  char *aDoclist;                 /* Pointer to doclist buffer */
111082
 
  int nDoclist;                   /* Size of aDoclist[] in bytes */
111083
 
};
111084
 
 
111085
 
/* fts3.c */
111086
 
SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
111087
 
SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
111088
 
SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
111089
 
SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
111090
 
SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
111091
 
 
111092
 
SQLITE_PRIVATE char *sqlite3Fts3FindPositions(Fts3Expr *, sqlite3_int64, int);
111093
 
SQLITE_PRIVATE int sqlite3Fts3ExprLoadDoclist(Fts3Cursor *, Fts3Expr *);
111094
 
SQLITE_PRIVATE int sqlite3Fts3ExprLoadFtDoclist(Fts3Cursor *, Fts3Expr *, char **, int *);
111095
 
SQLITE_PRIVATE int sqlite3Fts3ExprNearTrim(Fts3Expr *, Fts3Expr *, int);
111096
 
 
111097
 
/* fts3_tokenizer.c */
111098
 
SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
111099
 
SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
111100
 
SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *, 
111101
 
    sqlite3_tokenizer **, char **
111102
 
);
111103
 
SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
111104
 
 
111105
 
/* fts3_snippet.c */
111106
 
SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
111107
 
SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
111108
 
  const char *, const char *, int, int
111109
 
);
111110
 
SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
111111
 
 
111112
 
/* fts3_expr.c */
111113
 
SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, 
111114
 
  char **, int, int, const char *, int, Fts3Expr **
111115
 
);
111116
 
SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
111117
 
#ifdef SQLITE_TEST
111118
 
SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
111119
 
#endif
111120
 
 
111121
 
/* fts3_aux.c */
111122
 
SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
111123
 
 
111124
 
#endif /* _FTSINT_H */
111125
 
 
111126
 
/************** End of fts3Int.h *********************************************/
111127
 
/************** Continuing where we left off in fts3.c ***********************/
111128
 
 
111129
 
 
111130
 
#ifndef SQLITE_CORE 
111131
 
  SQLITE_EXTENSION_INIT1
111132
 
#endif
111133
 
 
111134
 
/* 
111135
 
** Write a 64-bit variable-length integer to memory starting at p[0].
111136
 
** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
111137
 
** The number of bytes written is returned.
111138
 
*/
111139
 
SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
111140
 
  unsigned char *q = (unsigned char *) p;
111141
 
  sqlite_uint64 vu = v;
111142
 
  do{
111143
 
    *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
111144
 
    vu >>= 7;
111145
 
  }while( vu!=0 );
111146
 
  q[-1] &= 0x7f;  /* turn off high bit in final byte */
111147
 
  assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
111148
 
  return (int) (q - (unsigned char *)p);
111149
 
}
111150
 
 
111151
 
/* 
111152
 
** Read a 64-bit variable-length integer from memory starting at p[0].
111153
 
** Return the number of bytes read, or 0 on error.
111154
 
** The value is stored in *v.
111155
 
*/
111156
 
SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
111157
 
  const unsigned char *q = (const unsigned char *) p;
111158
 
  sqlite_uint64 x = 0, y = 1;
111159
 
  while( (*q&0x80)==0x80 && q-(unsigned char *)p<FTS3_VARINT_MAX ){
111160
 
    x += y * (*q++ & 0x7f);
111161
 
    y <<= 7;
111162
 
  }
111163
 
  x += y * (*q++);
111164
 
  *v = (sqlite_int64) x;
111165
 
  return (int) (q - (unsigned char *)p);
111166
 
}
111167
 
 
111168
 
/*
111169
 
** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
111170
 
** 32-bit integer before it is returned.
111171
 
*/
111172
 
SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
111173
 
 sqlite_int64 i;
111174
 
 int ret = sqlite3Fts3GetVarint(p, &i);
111175
 
 *pi = (int) i;
111176
 
 return ret;
111177
 
}
111178
 
 
111179
 
/*
111180
 
** Return the number of bytes required to encode v as a varint
111181
 
*/
111182
 
SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
111183
 
  int i = 0;
111184
 
  do{
111185
 
    i++;
111186
 
    v >>= 7;
111187
 
  }while( v!=0 );
111188
 
  return i;
111189
 
}
111190
 
 
111191
 
/*
111192
 
** Convert an SQL-style quoted string into a normal string by removing
111193
 
** the quote characters.  The conversion is done in-place.  If the
111194
 
** input does not begin with a quote character, then this routine
111195
 
** is a no-op.
111196
 
**
111197
 
** Examples:
111198
 
**
111199
 
**     "abc"   becomes   abc
111200
 
**     'xyz'   becomes   xyz
111201
 
**     [pqr]   becomes   pqr
111202
 
**     `mno`   becomes   mno
111203
 
**
111204
 
*/
111205
 
SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
111206
 
  char quote;                     /* Quote character (if any ) */
111207
 
 
111208
 
  quote = z[0];
111209
 
  if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
111210
 
    int iIn = 1;                  /* Index of next byte to read from input */
111211
 
    int iOut = 0;                 /* Index of next byte to write to output */
111212
 
 
111213
 
    /* If the first byte was a '[', then the close-quote character is a ']' */
111214
 
    if( quote=='[' ) quote = ']';  
111215
 
 
111216
 
    while( ALWAYS(z[iIn]) ){
111217
 
      if( z[iIn]==quote ){
111218
 
        if( z[iIn+1]!=quote ) break;
111219
 
        z[iOut++] = quote;
111220
 
        iIn += 2;
111221
 
      }else{
111222
 
        z[iOut++] = z[iIn++];
111223
 
      }
111224
 
    }
111225
 
    z[iOut] = '\0';
111226
 
  }
111227
 
}
111228
 
 
111229
 
/*
111230
 
** Read a single varint from the doclist at *pp and advance *pp to point
111231
 
** to the first byte past the end of the varint.  Add the value of the varint
111232
 
** to *pVal.
111233
 
*/
111234
 
static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
111235
 
  sqlite3_int64 iVal;
111236
 
  *pp += sqlite3Fts3GetVarint(*pp, &iVal);
111237
 
  *pVal += iVal;
111238
 
}
111239
 
 
111240
 
/*
111241
 
** As long as *pp has not reached its end (pEnd), then do the same
111242
 
** as fts3GetDeltaVarint(): read a single varint and add it to *pVal.
111243
 
** But if we have reached the end of the varint, just set *pp=0 and
111244
 
** leave *pVal unchanged.
111245
 
*/
111246
 
static void fts3GetDeltaVarint2(char **pp, char *pEnd, sqlite3_int64 *pVal){
111247
 
  if( *pp>=pEnd ){
111248
 
    *pp = 0;
111249
 
  }else{
111250
 
    fts3GetDeltaVarint(pp, pVal);
111251
 
  }
111252
 
}
111253
 
 
111254
 
/*
111255
 
** The xDisconnect() virtual table method.
111256
 
*/
111257
 
static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
111258
 
  Fts3Table *p = (Fts3Table *)pVtab;
111259
 
  int i;
111260
 
 
111261
 
  assert( p->nPendingData==0 );
111262
 
  assert( p->pSegments==0 );
111263
 
 
111264
 
  /* Free any prepared statements held */
111265
 
  for(i=0; i<SizeofArray(p->aStmt); i++){
111266
 
    sqlite3_finalize(p->aStmt[i]);
111267
 
  }
111268
 
  sqlite3_free(p->zSegmentsTbl);
111269
 
  sqlite3_free(p->zReadExprlist);
111270
 
  sqlite3_free(p->zWriteExprlist);
111271
 
 
111272
 
  /* Invoke the tokenizer destructor to free the tokenizer. */
111273
 
  p->pTokenizer->pModule->xDestroy(p->pTokenizer);
111274
 
 
111275
 
  sqlite3_free(p);
111276
 
  return SQLITE_OK;
111277
 
}
111278
 
 
111279
 
/*
111280
 
** Construct one or more SQL statements from the format string given
111281
 
** and then evaluate those statements. The success code is written
111282
 
** into *pRc.
111283
 
**
111284
 
** If *pRc is initially non-zero then this routine is a no-op.
111285
 
*/
111286
 
static void fts3DbExec(
111287
 
  int *pRc,              /* Success code */
111288
 
  sqlite3 *db,           /* Database in which to run SQL */
111289
 
  const char *zFormat,   /* Format string for SQL */
111290
 
  ...                    /* Arguments to the format string */
111291
 
){
111292
 
  va_list ap;
111293
 
  char *zSql;
111294
 
  if( *pRc ) return;
111295
 
  va_start(ap, zFormat);
111296
 
  zSql = sqlite3_vmprintf(zFormat, ap);
111297
 
  va_end(ap);
111298
 
  if( zSql==0 ){
111299
 
    *pRc = SQLITE_NOMEM;
111300
 
  }else{
111301
 
    *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
111302
 
    sqlite3_free(zSql);
111303
 
  }
111304
 
}
111305
 
 
111306
 
/*
111307
 
** The xDestroy() virtual table method.
111308
 
*/
111309
 
static int fts3DestroyMethod(sqlite3_vtab *pVtab){
111310
 
  int rc = SQLITE_OK;              /* Return code */
111311
 
  Fts3Table *p = (Fts3Table *)pVtab;
111312
 
  sqlite3 *db = p->db;
111313
 
 
111314
 
  /* Drop the shadow tables */
111315
 
  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", p->zDb, p->zName);
111316
 
  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", p->zDb,p->zName);
111317
 
  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", p->zDb, p->zName);
111318
 
  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", p->zDb, p->zName);
111319
 
  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", p->zDb, p->zName);
111320
 
 
111321
 
  /* If everything has worked, invoke fts3DisconnectMethod() to free the
111322
 
  ** memory associated with the Fts3Table structure and return SQLITE_OK.
111323
 
  ** Otherwise, return an SQLite error code.
111324
 
  */
111325
 
  return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
111326
 
}
111327
 
 
111328
 
 
111329
 
/*
111330
 
** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
111331
 
** passed as the first argument. This is done as part of the xConnect()
111332
 
** and xCreate() methods.
111333
 
**
111334
 
** If *pRc is non-zero when this function is called, it is a no-op. 
111335
 
** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
111336
 
** before returning.
111337
 
*/
111338
 
static void fts3DeclareVtab(int *pRc, Fts3Table *p){
111339
 
  if( *pRc==SQLITE_OK ){
111340
 
    int i;                        /* Iterator variable */
111341
 
    int rc;                       /* Return code */
111342
 
    char *zSql;                   /* SQL statement passed to declare_vtab() */
111343
 
    char *zCols;                  /* List of user defined columns */
111344
 
 
111345
 
    /* Create a list of user columns for the virtual table */
111346
 
    zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
111347
 
    for(i=1; zCols && i<p->nColumn; i++){
111348
 
      zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
111349
 
    }
111350
 
 
111351
 
    /* Create the whole "CREATE TABLE" statement to pass to SQLite */
111352
 
    zSql = sqlite3_mprintf(
111353
 
        "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN)", zCols, p->zName
111354
 
    );
111355
 
    if( !zCols || !zSql ){
111356
 
      rc = SQLITE_NOMEM;
111357
 
    }else{
111358
 
      rc = sqlite3_declare_vtab(p->db, zSql);
111359
 
    }
111360
 
 
111361
 
    sqlite3_free(zSql);
111362
 
    sqlite3_free(zCols);
111363
 
    *pRc = rc;
111364
 
  }
111365
 
}
111366
 
 
111367
 
/*
111368
 
** Create the backing store tables (%_content, %_segments and %_segdir)
111369
 
** required by the FTS3 table passed as the only argument. This is done
111370
 
** as part of the vtab xCreate() method.
111371
 
**
111372
 
** If the p->bHasDocsize boolean is true (indicating that this is an
111373
 
** FTS4 table, not an FTS3 table) then also create the %_docsize and
111374
 
** %_stat tables required by FTS4.
111375
 
*/
111376
 
static int fts3CreateTables(Fts3Table *p){
111377
 
  int rc = SQLITE_OK;             /* Return code */
111378
 
  int i;                          /* Iterator variable */
111379
 
  char *zContentCols;             /* Columns of %_content table */
111380
 
  sqlite3 *db = p->db;            /* The database connection */
111381
 
 
111382
 
  /* Create a list of user columns for the content table */
111383
 
  zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
111384
 
  for(i=0; zContentCols && i<p->nColumn; i++){
111385
 
    char *z = p->azColumn[i];
111386
 
    zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
111387
 
  }
111388
 
  if( zContentCols==0 ) rc = SQLITE_NOMEM;
111389
 
 
111390
 
  /* Create the content table */
111391
 
  fts3DbExec(&rc, db, 
111392
 
     "CREATE TABLE %Q.'%q_content'(%s)",
111393
 
     p->zDb, p->zName, zContentCols
111394
 
  );
111395
 
  sqlite3_free(zContentCols);
111396
 
  /* Create other tables */
111397
 
  fts3DbExec(&rc, db, 
111398
 
      "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
111399
 
      p->zDb, p->zName
111400
 
  );
111401
 
  fts3DbExec(&rc, db, 
111402
 
      "CREATE TABLE %Q.'%q_segdir'("
111403
 
        "level INTEGER,"
111404
 
        "idx INTEGER,"
111405
 
        "start_block INTEGER,"
111406
 
        "leaves_end_block INTEGER,"
111407
 
        "end_block INTEGER,"
111408
 
        "root BLOB,"
111409
 
        "PRIMARY KEY(level, idx)"
111410
 
      ");",
111411
 
      p->zDb, p->zName
111412
 
  );
111413
 
  if( p->bHasDocsize ){
111414
 
    fts3DbExec(&rc, db, 
111415
 
        "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
111416
 
        p->zDb, p->zName
111417
 
    );
111418
 
  }
111419
 
  if( p->bHasStat ){
111420
 
    fts3DbExec(&rc, db, 
111421
 
        "CREATE TABLE %Q.'%q_stat'(id INTEGER PRIMARY KEY, value BLOB);",
111422
 
        p->zDb, p->zName
111423
 
    );
111424
 
  }
111425
 
  return rc;
111426
 
}
111427
 
 
111428
 
/*
111429
 
** Store the current database page-size in bytes in p->nPgsz.
111430
 
**
111431
 
** If *pRc is non-zero when this function is called, it is a no-op. 
111432
 
** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
111433
 
** before returning.
111434
 
*/
111435
 
static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
111436
 
  if( *pRc==SQLITE_OK ){
111437
 
    int rc;                       /* Return code */
111438
 
    char *zSql;                   /* SQL text "PRAGMA %Q.page_size" */
111439
 
    sqlite3_stmt *pStmt;          /* Compiled "PRAGMA %Q.page_size" statement */
111440
 
  
111441
 
    zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
111442
 
    if( !zSql ){
111443
 
      rc = SQLITE_NOMEM;
111444
 
    }else{
111445
 
      rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
111446
 
      if( rc==SQLITE_OK ){
111447
 
        sqlite3_step(pStmt);
111448
 
        p->nPgsz = sqlite3_column_int(pStmt, 0);
111449
 
        rc = sqlite3_finalize(pStmt);
111450
 
      }
111451
 
    }
111452
 
    assert( p->nPgsz>0 || rc!=SQLITE_OK );
111453
 
    sqlite3_free(zSql);
111454
 
    *pRc = rc;
111455
 
  }
111456
 
}
111457
 
 
111458
 
/*
111459
 
** "Special" FTS4 arguments are column specifications of the following form:
111460
 
**
111461
 
**   <key> = <value>
111462
 
**
111463
 
** There may not be whitespace surrounding the "=" character. The <value> 
111464
 
** term may be quoted, but the <key> may not.
111465
 
*/
111466
 
static int fts3IsSpecialColumn(
111467
 
  const char *z, 
111468
 
  int *pnKey,
111469
 
  char **pzValue
111470
 
){
111471
 
  char *zValue;
111472
 
  const char *zCsr = z;
111473
 
 
111474
 
  while( *zCsr!='=' ){
111475
 
    if( *zCsr=='\0' ) return 0;
111476
 
    zCsr++;
111477
 
  }
111478
 
 
111479
 
  *pnKey = (int)(zCsr-z);
111480
 
  zValue = sqlite3_mprintf("%s", &zCsr[1]);
111481
 
  if( zValue ){
111482
 
    sqlite3Fts3Dequote(zValue);
111483
 
  }
111484
 
  *pzValue = zValue;
111485
 
  return 1;
111486
 
}
111487
 
 
111488
 
/*
111489
 
** Append the output of a printf() style formatting to an existing string.
111490
 
*/
111491
 
static void fts3Appendf(
111492
 
  int *pRc,                       /* IN/OUT: Error code */
111493
 
  char **pz,                      /* IN/OUT: Pointer to string buffer */
111494
 
  const char *zFormat,            /* Printf format string to append */
111495
 
  ...                             /* Arguments for printf format string */
111496
 
){
111497
 
  if( *pRc==SQLITE_OK ){
111498
 
    va_list ap;
111499
 
    char *z;
111500
 
    va_start(ap, zFormat);
111501
 
    z = sqlite3_vmprintf(zFormat, ap);
111502
 
    if( z && *pz ){
111503
 
      char *z2 = sqlite3_mprintf("%s%s", *pz, z);
111504
 
      sqlite3_free(z);
111505
 
      z = z2;
111506
 
    }
111507
 
    if( z==0 ) *pRc = SQLITE_NOMEM;
111508
 
    sqlite3_free(*pz);
111509
 
    *pz = z;
111510
 
  }
111511
 
}
111512
 
 
111513
 
/*
111514
 
** Return a copy of input string zInput enclosed in double-quotes (") and
111515
 
** with all double quote characters escaped. For example:
111516
 
**
111517
 
**     fts3QuoteId("un \"zip\"")   ->    "un \"\"zip\"\""
111518
 
**
111519
 
** The pointer returned points to memory obtained from sqlite3_malloc(). It
111520
 
** is the callers responsibility to call sqlite3_free() to release this
111521
 
** memory.
111522
 
*/
111523
 
static char *fts3QuoteId(char const *zInput){
111524
 
  int nRet;
111525
 
  char *zRet;
111526
 
  nRet = 2 + strlen(zInput)*2 + 1;
111527
 
  zRet = sqlite3_malloc(nRet);
111528
 
  if( zRet ){
111529
 
    int i;
111530
 
    char *z = zRet;
111531
 
    *(z++) = '"';
111532
 
    for(i=0; zInput[i]; i++){
111533
 
      if( zInput[i]=='"' ) *(z++) = '"';
111534
 
      *(z++) = zInput[i];
111535
 
    }
111536
 
    *(z++) = '"';
111537
 
    *(z++) = '\0';
111538
 
  }
111539
 
  return zRet;
111540
 
}
111541
 
 
111542
 
/*
111543
 
** Return a list of comma separated SQL expressions that could be used
111544
 
** in a SELECT statement such as the following:
111545
 
**
111546
 
**     SELECT <list of expressions> FROM %_content AS x ...
111547
 
**
111548
 
** to return the docid, followed by each column of text data in order
111549
 
** from left to write. If parameter zFunc is not NULL, then instead of
111550
 
** being returned directly each column of text data is passed to an SQL
111551
 
** function named zFunc first. For example, if zFunc is "unzip" and the
111552
 
** table has the three user-defined columns "a", "b", and "c", the following
111553
 
** string is returned:
111554
 
**
111555
 
**     "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c')"
111556
 
**
111557
 
** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
111558
 
** is the responsibility of the caller to eventually free it.
111559
 
**
111560
 
** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
111561
 
** a NULL pointer is returned). Otherwise, if an OOM error is encountered
111562
 
** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
111563
 
** no error occurs, *pRc is left unmodified.
111564
 
*/
111565
 
static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
111566
 
  char *zRet = 0;
111567
 
  char *zFree = 0;
111568
 
  char *zFunction;
111569
 
  int i;
111570
 
 
111571
 
  if( !zFunc ){
111572
 
    zFunction = "";
111573
 
  }else{
111574
 
    zFree = zFunction = fts3QuoteId(zFunc);
111575
 
  }
111576
 
  fts3Appendf(pRc, &zRet, "docid");
111577
 
  for(i=0; i<p->nColumn; i++){
111578
 
    fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
111579
 
  }
111580
 
  sqlite3_free(zFree);
111581
 
  return zRet;
111582
 
}
111583
 
 
111584
 
/*
111585
 
** Return a list of N comma separated question marks, where N is the number
111586
 
** of columns in the %_content table (one for the docid plus one for each
111587
 
** user-defined text column).
111588
 
**
111589
 
** If argument zFunc is not NULL, then all but the first question mark
111590
 
** is preceded by zFunc and an open bracket, and followed by a closed
111591
 
** bracket. For example, if zFunc is "zip" and the FTS3 table has three 
111592
 
** user-defined text columns, the following string is returned:
111593
 
**
111594
 
**     "?, zip(?), zip(?), zip(?)"
111595
 
**
111596
 
** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
111597
 
** is the responsibility of the caller to eventually free it.
111598
 
**
111599
 
** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
111600
 
** a NULL pointer is returned). Otherwise, if an OOM error is encountered
111601
 
** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
111602
 
** no error occurs, *pRc is left unmodified.
111603
 
*/
111604
 
static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
111605
 
  char *zRet = 0;
111606
 
  char *zFree = 0;
111607
 
  char *zFunction;
111608
 
  int i;
111609
 
 
111610
 
  if( !zFunc ){
111611
 
    zFunction = "";
111612
 
  }else{
111613
 
    zFree = zFunction = fts3QuoteId(zFunc);
111614
 
  }
111615
 
  fts3Appendf(pRc, &zRet, "?");
111616
 
  for(i=0; i<p->nColumn; i++){
111617
 
    fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
111618
 
  }
111619
 
  sqlite3_free(zFree);
111620
 
  return zRet;
111621
 
}
111622
 
 
111623
 
/*
111624
 
** This function is the implementation of both the xConnect and xCreate
111625
 
** methods of the FTS3 virtual table.
111626
 
**
111627
 
** The argv[] array contains the following:
111628
 
**
111629
 
**   argv[0]   -> module name  ("fts3" or "fts4")
111630
 
**   argv[1]   -> database name
111631
 
**   argv[2]   -> table name
111632
 
**   argv[...] -> "column name" and other module argument fields.
111633
 
*/
111634
 
static int fts3InitVtab(
111635
 
  int isCreate,                   /* True for xCreate, false for xConnect */
111636
 
  sqlite3 *db,                    /* The SQLite database connection */
111637
 
  void *pAux,                     /* Hash table containing tokenizers */
111638
 
  int argc,                       /* Number of elements in argv array */
111639
 
  const char * const *argv,       /* xCreate/xConnect argument array */
111640
 
  sqlite3_vtab **ppVTab,          /* Write the resulting vtab structure here */
111641
 
  char **pzErr                    /* Write any error message here */
111642
 
){
111643
 
  Fts3Hash *pHash = (Fts3Hash *)pAux;
111644
 
  Fts3Table *p = 0;               /* Pointer to allocated vtab */
111645
 
  int rc = SQLITE_OK;             /* Return code */
111646
 
  int i;                          /* Iterator variable */
111647
 
  int nByte;                      /* Size of allocation used for *p */
111648
 
  int iCol;                       /* Column index */
111649
 
  int nString = 0;                /* Bytes required to hold all column names */
111650
 
  int nCol = 0;                   /* Number of columns in the FTS table */
111651
 
  char *zCsr;                     /* Space for holding column names */
111652
 
  int nDb;                        /* Bytes required to hold database name */
111653
 
  int nName;                      /* Bytes required to hold table name */
111654
 
  int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
111655
 
  int bNoDocsize = 0;             /* True to omit %_docsize table */
111656
 
  const char **aCol;              /* Array of column names */
111657
 
  sqlite3_tokenizer *pTokenizer = 0;        /* Tokenizer for this table */
111658
 
 
111659
 
  char *zCompress = 0;
111660
 
  char *zUncompress = 0;
111661
 
 
111662
 
  assert( strlen(argv[0])==4 );
111663
 
  assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
111664
 
       || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
111665
 
  );
111666
 
 
111667
 
  nDb = (int)strlen(argv[1]) + 1;
111668
 
  nName = (int)strlen(argv[2]) + 1;
111669
 
 
111670
 
  aCol = (const char **)sqlite3_malloc(sizeof(const char *) * (argc-2) );
111671
 
  if( !aCol ) return SQLITE_NOMEM;
111672
 
  memset((void *)aCol, 0, sizeof(const char *) * (argc-2));
111673
 
 
111674
 
  /* Loop through all of the arguments passed by the user to the FTS3/4
111675
 
  ** module (i.e. all the column names and special arguments). This loop
111676
 
  ** does the following:
111677
 
  **
111678
 
  **   + Figures out the number of columns the FTSX table will have, and
111679
 
  **     the number of bytes of space that must be allocated to store copies
111680
 
  **     of the column names.
111681
 
  **
111682
 
  **   + If there is a tokenizer specification included in the arguments,
111683
 
  **     initializes the tokenizer pTokenizer.
111684
 
  */
111685
 
  for(i=3; rc==SQLITE_OK && i<argc; i++){
111686
 
    char const *z = argv[i];
111687
 
    int nKey;
111688
 
    char *zVal;
111689
 
 
111690
 
    /* Check if this is a tokenizer specification */
111691
 
    if( !pTokenizer 
111692
 
     && strlen(z)>8
111693
 
     && 0==sqlite3_strnicmp(z, "tokenize", 8) 
111694
 
     && 0==sqlite3Fts3IsIdChar(z[8])
111695
 
    ){
111696
 
      rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
111697
 
    }
111698
 
 
111699
 
    /* Check if it is an FTS4 special argument. */
111700
 
    else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
111701
 
      if( !zVal ){
111702
 
        rc = SQLITE_NOMEM;
111703
 
        goto fts3_init_out;
111704
 
      }
111705
 
      if( nKey==9 && 0==sqlite3_strnicmp(z, "matchinfo", 9) ){
111706
 
        if( strlen(zVal)==4 && 0==sqlite3_strnicmp(zVal, "fts3", 4) ){
111707
 
          bNoDocsize = 1;
111708
 
        }else{
111709
 
          *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
111710
 
          rc = SQLITE_ERROR;
111711
 
        }
111712
 
      }else if( nKey==8 && 0==sqlite3_strnicmp(z, "compress", 8) ){
111713
 
        zCompress = zVal;
111714
 
        zVal = 0;
111715
 
      }else if( nKey==10 && 0==sqlite3_strnicmp(z, "uncompress", 10) ){
111716
 
        zUncompress = zVal;
111717
 
        zVal = 0;
111718
 
      }else{
111719
 
        *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
111720
 
        rc = SQLITE_ERROR;
111721
 
      }
111722
 
      sqlite3_free(zVal);
111723
 
    }
111724
 
 
111725
 
    /* Otherwise, the argument is a column name. */
111726
 
    else {
111727
 
      nString += (int)(strlen(z) + 1);
111728
 
      aCol[nCol++] = z;
111729
 
    }
111730
 
  }
111731
 
  if( rc!=SQLITE_OK ) goto fts3_init_out;
111732
 
 
111733
 
  if( nCol==0 ){
111734
 
    assert( nString==0 );
111735
 
    aCol[0] = "content";
111736
 
    nString = 8;
111737
 
    nCol = 1;
111738
 
  }
111739
 
 
111740
 
  if( pTokenizer==0 ){
111741
 
    rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
111742
 
    if( rc!=SQLITE_OK ) goto fts3_init_out;
111743
 
  }
111744
 
  assert( pTokenizer );
111745
 
 
111746
 
 
111747
 
  /* Allocate and populate the Fts3Table structure. */
111748
 
  nByte = sizeof(Fts3Table) +              /* Fts3Table */
111749
 
          nCol * sizeof(char *) +              /* azColumn */
111750
 
          nName +                              /* zName */
111751
 
          nDb +                                /* zDb */
111752
 
          nString;                             /* Space for azColumn strings */
111753
 
  p = (Fts3Table*)sqlite3_malloc(nByte);
111754
 
  if( p==0 ){
111755
 
    rc = SQLITE_NOMEM;
111756
 
    goto fts3_init_out;
111757
 
  }
111758
 
  memset(p, 0, nByte);
111759
 
  p->db = db;
111760
 
  p->nColumn = nCol;
111761
 
  p->nPendingData = 0;
111762
 
  p->azColumn = (char **)&p[1];
111763
 
  p->pTokenizer = pTokenizer;
111764
 
  p->nNodeSize = 1000;
111765
 
  p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
111766
 
  p->bHasDocsize = (isFts4 && bNoDocsize==0);
111767
 
  p->bHasStat = isFts4;
111768
 
  fts3HashInit(&p->pendingTerms, FTS3_HASH_STRING, 1);
111769
 
 
111770
 
  /* Fill in the zName and zDb fields of the vtab structure. */
111771
 
  zCsr = (char *)&p->azColumn[nCol];
111772
 
  p->zName = zCsr;
111773
 
  memcpy(zCsr, argv[2], nName);
111774
 
  zCsr += nName;
111775
 
  p->zDb = zCsr;
111776
 
  memcpy(zCsr, argv[1], nDb);
111777
 
  zCsr += nDb;
111778
 
 
111779
 
  /* Fill in the azColumn array */
111780
 
  for(iCol=0; iCol<nCol; iCol++){
111781
 
    char *z; 
111782
 
    int n;
111783
 
    z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
111784
 
    memcpy(zCsr, z, n);
111785
 
    zCsr[n] = '\0';
111786
 
    sqlite3Fts3Dequote(zCsr);
111787
 
    p->azColumn[iCol] = zCsr;
111788
 
    zCsr += n+1;
111789
 
    assert( zCsr <= &((char *)p)[nByte] );
111790
 
  }
111791
 
 
111792
 
  if( (zCompress==0)!=(zUncompress==0) ){
111793
 
    char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
111794
 
    rc = SQLITE_ERROR;
111795
 
    *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss);
111796
 
  }
111797
 
  p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
111798
 
  p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
111799
 
  if( rc!=SQLITE_OK ) goto fts3_init_out;
111800
 
 
111801
 
  /* If this is an xCreate call, create the underlying tables in the 
111802
 
  ** database. TODO: For xConnect(), it could verify that said tables exist.
111803
 
  */
111804
 
  if( isCreate ){
111805
 
    rc = fts3CreateTables(p);
111806
 
  }
111807
 
 
111808
 
  /* Figure out the page-size for the database. This is required in order to
111809
 
  ** estimate the cost of loading large doclists from the database (see 
111810
 
  ** function sqlite3Fts3SegReaderCost() for details).
111811
 
  */
111812
 
  fts3DatabasePageSize(&rc, p);
111813
 
 
111814
 
  /* Declare the table schema to SQLite. */
111815
 
  fts3DeclareVtab(&rc, p);
111816
 
 
111817
 
fts3_init_out:
111818
 
  sqlite3_free(zCompress);
111819
 
  sqlite3_free(zUncompress);
111820
 
  sqlite3_free((void *)aCol);
111821
 
  if( rc!=SQLITE_OK ){
111822
 
    if( p ){
111823
 
      fts3DisconnectMethod((sqlite3_vtab *)p);
111824
 
    }else if( pTokenizer ){
111825
 
      pTokenizer->pModule->xDestroy(pTokenizer);
111826
 
    }
111827
 
  }else{
111828
 
    *ppVTab = &p->base;
111829
 
  }
111830
 
  return rc;
111831
 
}
111832
 
 
111833
 
/*
111834
 
** The xConnect() and xCreate() methods for the virtual table. All the
111835
 
** work is done in function fts3InitVtab().
111836
 
*/
111837
 
static int fts3ConnectMethod(
111838
 
  sqlite3 *db,                    /* Database connection */
111839
 
  void *pAux,                     /* Pointer to tokenizer hash table */
111840
 
  int argc,                       /* Number of elements in argv array */
111841
 
  const char * const *argv,       /* xCreate/xConnect argument array */
111842
 
  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
111843
 
  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
111844
 
){
111845
 
  return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
111846
 
}
111847
 
static int fts3CreateMethod(
111848
 
  sqlite3 *db,                    /* Database connection */
111849
 
  void *pAux,                     /* Pointer to tokenizer hash table */
111850
 
  int argc,                       /* Number of elements in argv array */
111851
 
  const char * const *argv,       /* xCreate/xConnect argument array */
111852
 
  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
111853
 
  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
111854
 
){
111855
 
  return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
111856
 
}
111857
 
 
111858
 
/* 
111859
 
** Implementation of the xBestIndex method for FTS3 tables. There
111860
 
** are three possible strategies, in order of preference:
111861
 
**
111862
 
**   1. Direct lookup by rowid or docid. 
111863
 
**   2. Full-text search using a MATCH operator on a non-docid column.
111864
 
**   3. Linear scan of %_content table.
111865
 
*/
111866
 
static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
111867
 
  Fts3Table *p = (Fts3Table *)pVTab;
111868
 
  int i;                          /* Iterator variable */
111869
 
  int iCons = -1;                 /* Index of constraint to use */
111870
 
 
111871
 
  /* By default use a full table scan. This is an expensive option,
111872
 
  ** so search through the constraints to see if a more efficient 
111873
 
  ** strategy is possible.
111874
 
  */
111875
 
  pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
111876
 
  pInfo->estimatedCost = 500000;
111877
 
  for(i=0; i<pInfo->nConstraint; i++){
111878
 
    struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
111879
 
    if( pCons->usable==0 ) continue;
111880
 
 
111881
 
    /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
111882
 
    if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ 
111883
 
     && (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1 )
111884
 
    ){
111885
 
      pInfo->idxNum = FTS3_DOCID_SEARCH;
111886
 
      pInfo->estimatedCost = 1.0;
111887
 
      iCons = i;
111888
 
    }
111889
 
 
111890
 
    /* A MATCH constraint. Use a full-text search.
111891
 
    **
111892
 
    ** If there is more than one MATCH constraint available, use the first
111893
 
    ** one encountered. If there is both a MATCH constraint and a direct
111894
 
    ** rowid/docid lookup, prefer the MATCH strategy. This is done even 
111895
 
    ** though the rowid/docid lookup is faster than a MATCH query, selecting
111896
 
    ** it would lead to an "unable to use function MATCH in the requested 
111897
 
    ** context" error.
111898
 
    */
111899
 
    if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH 
111900
 
     && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
111901
 
    ){
111902
 
      pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
111903
 
      pInfo->estimatedCost = 2.0;
111904
 
      iCons = i;
111905
 
      break;
111906
 
    }
111907
 
  }
111908
 
 
111909
 
  if( iCons>=0 ){
111910
 
    pInfo->aConstraintUsage[iCons].argvIndex = 1;
111911
 
    pInfo->aConstraintUsage[iCons].omit = 1;
111912
 
  } 
111913
 
  return SQLITE_OK;
111914
 
}
111915
 
 
111916
 
/*
111917
 
** Implementation of xOpen method.
111918
 
*/
111919
 
static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
111920
 
  sqlite3_vtab_cursor *pCsr;               /* Allocated cursor */
111921
 
 
111922
 
  UNUSED_PARAMETER(pVTab);
111923
 
 
111924
 
  /* Allocate a buffer large enough for an Fts3Cursor structure. If the
111925
 
  ** allocation succeeds, zero it and return SQLITE_OK. Otherwise, 
111926
 
  ** if the allocation fails, return SQLITE_NOMEM.
111927
 
  */
111928
 
  *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
111929
 
  if( !pCsr ){
111930
 
    return SQLITE_NOMEM;
111931
 
  }
111932
 
  memset(pCsr, 0, sizeof(Fts3Cursor));
111933
 
  return SQLITE_OK;
111934
 
}
111935
 
 
111936
 
/*
111937
 
** Close the cursor.  For additional information see the documentation
111938
 
** on the xClose method of the virtual table interface.
111939
 
*/
111940
 
static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
111941
 
  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
111942
 
  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
111943
 
  sqlite3_finalize(pCsr->pStmt);
111944
 
  sqlite3Fts3ExprFree(pCsr->pExpr);
111945
 
  sqlite3Fts3FreeDeferredTokens(pCsr);
111946
 
  sqlite3_free(pCsr->aDoclist);
111947
 
  sqlite3_free(pCsr->aMatchinfo);
111948
 
  sqlite3_free(pCsr);
111949
 
  return SQLITE_OK;
111950
 
}
111951
 
 
111952
 
/*
111953
 
** Position the pCsr->pStmt statement so that it is on the row
111954
 
** of the %_content table that contains the last match.  Return
111955
 
** SQLITE_OK on success.  
111956
 
*/
111957
 
static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
111958
 
  if( pCsr->isRequireSeek ){
111959
 
    pCsr->isRequireSeek = 0;
111960
 
    sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
111961
 
    if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
111962
 
      return SQLITE_OK;
111963
 
    }else{
111964
 
      int rc = sqlite3_reset(pCsr->pStmt);
111965
 
      if( rc==SQLITE_OK ){
111966
 
        /* If no row was found and no error has occured, then the %_content
111967
 
        ** table is missing a row that is present in the full-text index.
111968
 
        ** The data structures are corrupt.
111969
 
        */
111970
 
        rc = SQLITE_CORRUPT;
111971
 
      }
111972
 
      pCsr->isEof = 1;
111973
 
      if( pContext ){
111974
 
        sqlite3_result_error_code(pContext, rc);
111975
 
      }
111976
 
      return rc;
111977
 
    }
111978
 
  }else{
111979
 
    return SQLITE_OK;
111980
 
  }
111981
 
}
111982
 
 
111983
 
/*
111984
 
** This function is used to process a single interior node when searching
111985
 
** a b-tree for a term or term prefix. The node data is passed to this 
111986
 
** function via the zNode/nNode parameters. The term to search for is
111987
 
** passed in zTerm/nTerm.
111988
 
**
111989
 
** If piFirst is not NULL, then this function sets *piFirst to the blockid
111990
 
** of the child node that heads the sub-tree that may contain the term.
111991
 
**
111992
 
** If piLast is not NULL, then *piLast is set to the right-most child node
111993
 
** that heads a sub-tree that may contain a term for which zTerm/nTerm is
111994
 
** a prefix.
111995
 
**
111996
 
** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
111997
 
*/
111998
 
static int fts3ScanInteriorNode(
111999
 
  const char *zTerm,              /* Term to select leaves for */
112000
 
  int nTerm,                      /* Size of term zTerm in bytes */
112001
 
  const char *zNode,              /* Buffer containing segment interior node */
112002
 
  int nNode,                      /* Size of buffer at zNode */
112003
 
  sqlite3_int64 *piFirst,         /* OUT: Selected child node */
112004
 
  sqlite3_int64 *piLast           /* OUT: Selected child node */
112005
 
){
112006
 
  int rc = SQLITE_OK;             /* Return code */
112007
 
  const char *zCsr = zNode;       /* Cursor to iterate through node */
112008
 
  const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
112009
 
  char *zBuffer = 0;              /* Buffer to load terms into */
112010
 
  int nAlloc = 0;                 /* Size of allocated buffer */
112011
 
  int isFirstTerm = 1;            /* True when processing first term on page */
112012
 
  sqlite3_int64 iChild;           /* Block id of child node to descend to */
112013
 
 
112014
 
  /* Skip over the 'height' varint that occurs at the start of every 
112015
 
  ** interior node. Then load the blockid of the left-child of the b-tree
112016
 
  ** node into variable iChild.  
112017
 
  **
112018
 
  ** Even if the data structure on disk is corrupted, this (reading two
112019
 
  ** varints from the buffer) does not risk an overread. If zNode is a
112020
 
  ** root node, then the buffer comes from a SELECT statement. SQLite does
112021
 
  ** not make this guarantee explicitly, but in practice there are always
112022
 
  ** either more than 20 bytes of allocated space following the nNode bytes of
112023
 
  ** contents, or two zero bytes. Or, if the node is read from the %_segments
112024
 
  ** table, then there are always 20 bytes of zeroed padding following the
112025
 
  ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
112026
 
  */
112027
 
  zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
112028
 
  zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
112029
 
  if( zCsr>zEnd ){
112030
 
    return SQLITE_CORRUPT;
112031
 
  }
112032
 
  
112033
 
  while( zCsr<zEnd && (piFirst || piLast) ){
112034
 
    int cmp;                      /* memcmp() result */
112035
 
    int nSuffix;                  /* Size of term suffix */
112036
 
    int nPrefix = 0;              /* Size of term prefix */
112037
 
    int nBuffer;                  /* Total term size */
112038
 
  
112039
 
    /* Load the next term on the node into zBuffer. Use realloc() to expand
112040
 
    ** the size of zBuffer if required.  */
112041
 
    if( !isFirstTerm ){
112042
 
      zCsr += sqlite3Fts3GetVarint32(zCsr, &nPrefix);
112043
 
    }
112044
 
    isFirstTerm = 0;
112045
 
    zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix);
112046
 
    
112047
 
    if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
112048
 
      rc = SQLITE_CORRUPT;
112049
 
      goto finish_scan;
112050
 
    }
112051
 
    if( nPrefix+nSuffix>nAlloc ){
112052
 
      char *zNew;
112053
 
      nAlloc = (nPrefix+nSuffix) * 2;
112054
 
      zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
112055
 
      if( !zNew ){
112056
 
        rc = SQLITE_NOMEM;
112057
 
        goto finish_scan;
112058
 
      }
112059
 
      zBuffer = zNew;
112060
 
    }
112061
 
    memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
112062
 
    nBuffer = nPrefix + nSuffix;
112063
 
    zCsr += nSuffix;
112064
 
 
112065
 
    /* Compare the term we are searching for with the term just loaded from
112066
 
    ** the interior node. If the specified term is greater than or equal
112067
 
    ** to the term from the interior node, then all terms on the sub-tree 
112068
 
    ** headed by node iChild are smaller than zTerm. No need to search 
112069
 
    ** iChild.
112070
 
    **
112071
 
    ** If the interior node term is larger than the specified term, then
112072
 
    ** the tree headed by iChild may contain the specified term.
112073
 
    */
112074
 
    cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
112075
 
    if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
112076
 
      *piFirst = iChild;
112077
 
      piFirst = 0;
112078
 
    }
112079
 
 
112080
 
    if( piLast && cmp<0 ){
112081
 
      *piLast = iChild;
112082
 
      piLast = 0;
112083
 
    }
112084
 
 
112085
 
    iChild++;
112086
 
  };
112087
 
 
112088
 
  if( piFirst ) *piFirst = iChild;
112089
 
  if( piLast ) *piLast = iChild;
112090
 
 
112091
 
 finish_scan:
112092
 
  sqlite3_free(zBuffer);
112093
 
  return rc;
112094
 
}
112095
 
 
112096
 
 
112097
 
/*
112098
 
** The buffer pointed to by argument zNode (size nNode bytes) contains an
112099
 
** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
112100
 
** contains a term. This function searches the sub-tree headed by the zNode
112101
 
** node for the range of leaf nodes that may contain the specified term
112102
 
** or terms for which the specified term is a prefix.
112103
 
**
112104
 
** If piLeaf is not NULL, then *piLeaf is set to the blockid of the 
112105
 
** left-most leaf node in the tree that may contain the specified term.
112106
 
** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
112107
 
** right-most leaf node that may contain a term for which the specified
112108
 
** term is a prefix.
112109
 
**
112110
 
** It is possible that the range of returned leaf nodes does not contain 
112111
 
** the specified term or any terms for which it is a prefix. However, if the 
112112
 
** segment does contain any such terms, they are stored within the identified
112113
 
** range. Because this function only inspects interior segment nodes (and
112114
 
** never loads leaf nodes into memory), it is not possible to be sure.
112115
 
**
112116
 
** If an error occurs, an error code other than SQLITE_OK is returned.
112117
 
*/ 
112118
 
static int fts3SelectLeaf(
112119
 
  Fts3Table *p,                   /* Virtual table handle */
112120
 
  const char *zTerm,              /* Term to select leaves for */
112121
 
  int nTerm,                      /* Size of term zTerm in bytes */
112122
 
  const char *zNode,              /* Buffer containing segment interior node */
112123
 
  int nNode,                      /* Size of buffer at zNode */
112124
 
  sqlite3_int64 *piLeaf,          /* Selected leaf node */
112125
 
  sqlite3_int64 *piLeaf2          /* Selected leaf node */
112126
 
){
112127
 
  int rc;                         /* Return code */
112128
 
  int iHeight;                    /* Height of this node in tree */
112129
 
 
112130
 
  assert( piLeaf || piLeaf2 );
112131
 
 
112132
 
  sqlite3Fts3GetVarint32(zNode, &iHeight);
112133
 
  rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
112134
 
  assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
112135
 
 
112136
 
  if( rc==SQLITE_OK && iHeight>1 ){
112137
 
    char *zBlob = 0;              /* Blob read from %_segments table */
112138
 
    int nBlob;                    /* Size of zBlob in bytes */
112139
 
 
112140
 
    if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
112141
 
      rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob);
112142
 
      if( rc==SQLITE_OK ){
112143
 
        rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
112144
 
      }
112145
 
      sqlite3_free(zBlob);
112146
 
      piLeaf = 0;
112147
 
      zBlob = 0;
112148
 
    }
112149
 
 
112150
 
    if( rc==SQLITE_OK ){
112151
 
      rc = sqlite3Fts3ReadBlock(p, piLeaf ? *piLeaf : *piLeaf2, &zBlob, &nBlob);
112152
 
    }
112153
 
    if( rc==SQLITE_OK ){
112154
 
      rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
112155
 
    }
112156
 
    sqlite3_free(zBlob);
112157
 
  }
112158
 
 
112159
 
  return rc;
112160
 
}
112161
 
 
112162
 
/*
112163
 
** This function is used to create delta-encoded serialized lists of FTS3 
112164
 
** varints. Each call to this function appends a single varint to a list.
112165
 
*/
112166
 
static void fts3PutDeltaVarint(
112167
 
  char **pp,                      /* IN/OUT: Output pointer */
112168
 
  sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
112169
 
  sqlite3_int64 iVal              /* Write this value to the list */
112170
 
){
112171
 
  assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
112172
 
  *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
112173
 
  *piPrev = iVal;
112174
 
}
112175
 
 
112176
 
/*
112177
 
** When this function is called, *ppPoslist is assumed to point to the 
112178
 
** start of a position-list. After it returns, *ppPoslist points to the
112179
 
** first byte after the position-list.
112180
 
**
112181
 
** A position list is list of positions (delta encoded) and columns for 
112182
 
** a single document record of a doclist.  So, in other words, this
112183
 
** routine advances *ppPoslist so that it points to the next docid in
112184
 
** the doclist, or to the first byte past the end of the doclist.
112185
 
**
112186
 
** If pp is not NULL, then the contents of the position list are copied
112187
 
** to *pp. *pp is set to point to the first byte past the last byte copied
112188
 
** before this function returns.
112189
 
*/
112190
 
static void fts3PoslistCopy(char **pp, char **ppPoslist){
112191
 
  char *pEnd = *ppPoslist;
112192
 
  char c = 0;
112193
 
 
112194
 
  /* The end of a position list is marked by a zero encoded as an FTS3 
112195
 
  ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
112196
 
  ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
112197
 
  ** of some other, multi-byte, value.
112198
 
  **
112199
 
  ** The following while-loop moves pEnd to point to the first byte that is not 
112200
 
  ** immediately preceded by a byte with the 0x80 bit set. Then increments
112201
 
  ** pEnd once more so that it points to the byte immediately following the
112202
 
  ** last byte in the position-list.
112203
 
  */
112204
 
  while( *pEnd | c ){
112205
 
    c = *pEnd++ & 0x80;
112206
 
    testcase( c!=0 && (*pEnd)==0 );
112207
 
  }
112208
 
  pEnd++;  /* Advance past the POS_END terminator byte */
112209
 
 
112210
 
  if( pp ){
112211
 
    int n = (int)(pEnd - *ppPoslist);
112212
 
    char *p = *pp;
112213
 
    memcpy(p, *ppPoslist, n);
112214
 
    p += n;
112215
 
    *pp = p;
112216
 
  }
112217
 
  *ppPoslist = pEnd;
112218
 
}
112219
 
 
112220
 
/*
112221
 
** When this function is called, *ppPoslist is assumed to point to the 
112222
 
** start of a column-list. After it returns, *ppPoslist points to the
112223
 
** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
112224
 
**
112225
 
** A column-list is list of delta-encoded positions for a single column
112226
 
** within a single document within a doclist.
112227
 
**
112228
 
** The column-list is terminated either by a POS_COLUMN varint (1) or
112229
 
** a POS_END varint (0).  This routine leaves *ppPoslist pointing to
112230
 
** the POS_COLUMN or POS_END that terminates the column-list.
112231
 
**
112232
 
** If pp is not NULL, then the contents of the column-list are copied
112233
 
** to *pp. *pp is set to point to the first byte past the last byte copied
112234
 
** before this function returns.  The POS_COLUMN or POS_END terminator
112235
 
** is not copied into *pp.
112236
 
*/
112237
 
static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
112238
 
  char *pEnd = *ppPoslist;
112239
 
  char c = 0;
112240
 
 
112241
 
  /* A column-list is terminated by either a 0x01 or 0x00 byte that is
112242
 
  ** not part of a multi-byte varint.
112243
 
  */
112244
 
  while( 0xFE & (*pEnd | c) ){
112245
 
    c = *pEnd++ & 0x80;
112246
 
    testcase( c!=0 && ((*pEnd)&0xfe)==0 );
112247
 
  }
112248
 
  if( pp ){
112249
 
    int n = (int)(pEnd - *ppPoslist);
112250
 
    char *p = *pp;
112251
 
    memcpy(p, *ppPoslist, n);
112252
 
    p += n;
112253
 
    *pp = p;
112254
 
  }
112255
 
  *ppPoslist = pEnd;
112256
 
}
112257
 
 
112258
 
/*
112259
 
** Value used to signify the end of an position-list. This is safe because
112260
 
** it is not possible to have a document with 2^31 terms.
112261
 
*/
112262
 
#define POSITION_LIST_END 0x7fffffff
112263
 
 
112264
 
/*
112265
 
** This function is used to help parse position-lists. When this function is
112266
 
** called, *pp may point to the start of the next varint in the position-list
112267
 
** being parsed, or it may point to 1 byte past the end of the position-list
112268
 
** (in which case **pp will be a terminator bytes POS_END (0) or
112269
 
** (1)).
112270
 
**
112271
 
** If *pp points past the end of the current position-list, set *pi to 
112272
 
** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
112273
 
** increment the current value of *pi by the value read, and set *pp to
112274
 
** point to the next value before returning.
112275
 
**
112276
 
** Before calling this routine *pi must be initialized to the value of
112277
 
** the previous position, or zero if we are reading the first position
112278
 
** in the position-list.  Because positions are delta-encoded, the value
112279
 
** of the previous position is needed in order to compute the value of
112280
 
** the next position.
112281
 
*/
112282
 
static void fts3ReadNextPos(
112283
 
  char **pp,                    /* IN/OUT: Pointer into position-list buffer */
112284
 
  sqlite3_int64 *pi             /* IN/OUT: Value read from position-list */
112285
 
){
112286
 
  if( (**pp)&0xFE ){
112287
 
    fts3GetDeltaVarint(pp, pi);
112288
 
    *pi -= 2;
112289
 
  }else{
112290
 
    *pi = POSITION_LIST_END;
112291
 
  }
112292
 
}
112293
 
 
112294
 
/*
112295
 
** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
112296
 
** the value of iCol encoded as a varint to *pp.   This will start a new
112297
 
** column list.
112298
 
**
112299
 
** Set *pp to point to the byte just after the last byte written before 
112300
 
** returning (do not modify it if iCol==0). Return the total number of bytes
112301
 
** written (0 if iCol==0).
112302
 
*/
112303
 
static int fts3PutColNumber(char **pp, int iCol){
112304
 
  int n = 0;                      /* Number of bytes written */
112305
 
  if( iCol ){
112306
 
    char *p = *pp;                /* Output pointer */
112307
 
    n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
112308
 
    *p = 0x01;
112309
 
    *pp = &p[n];
112310
 
  }
112311
 
  return n;
112312
 
}
112313
 
 
112314
 
/*
112315
 
** Compute the union of two position lists.  The output written
112316
 
** into *pp contains all positions of both *pp1 and *pp2 in sorted
112317
 
** order and with any duplicates removed.  All pointers are
112318
 
** updated appropriately.   The caller is responsible for insuring
112319
 
** that there is enough space in *pp to hold the complete output.
112320
 
*/
112321
 
static void fts3PoslistMerge(
112322
 
  char **pp,                      /* Output buffer */
112323
 
  char **pp1,                     /* Left input list */
112324
 
  char **pp2                      /* Right input list */
112325
 
){
112326
 
  char *p = *pp;
112327
 
  char *p1 = *pp1;
112328
 
  char *p2 = *pp2;
112329
 
 
112330
 
  while( *p1 || *p2 ){
112331
 
    int iCol1;         /* The current column index in pp1 */
112332
 
    int iCol2;         /* The current column index in pp2 */
112333
 
 
112334
 
    if( *p1==POS_COLUMN ) sqlite3Fts3GetVarint32(&p1[1], &iCol1);
112335
 
    else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
112336
 
    else iCol1 = 0;
112337
 
 
112338
 
    if( *p2==POS_COLUMN ) sqlite3Fts3GetVarint32(&p2[1], &iCol2);
112339
 
    else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
112340
 
    else iCol2 = 0;
112341
 
 
112342
 
    if( iCol1==iCol2 ){
112343
 
      sqlite3_int64 i1 = 0;       /* Last position from pp1 */
112344
 
      sqlite3_int64 i2 = 0;       /* Last position from pp2 */
112345
 
      sqlite3_int64 iPrev = 0;
112346
 
      int n = fts3PutColNumber(&p, iCol1);
112347
 
      p1 += n;
112348
 
      p2 += n;
112349
 
 
112350
 
      /* At this point, both p1 and p2 point to the start of column-lists
112351
 
      ** for the same column (the column with index iCol1 and iCol2).
112352
 
      ** A column-list is a list of non-negative delta-encoded varints, each 
112353
 
      ** incremented by 2 before being stored. Each list is terminated by a
112354
 
      ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
112355
 
      ** and writes the results to buffer p. p is left pointing to the byte
112356
 
      ** after the list written. No terminator (POS_END or POS_COLUMN) is
112357
 
      ** written to the output.
112358
 
      */
112359
 
      fts3GetDeltaVarint(&p1, &i1);
112360
 
      fts3GetDeltaVarint(&p2, &i2);
112361
 
      do {
112362
 
        fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2); 
112363
 
        iPrev -= 2;
112364
 
        if( i1==i2 ){
112365
 
          fts3ReadNextPos(&p1, &i1);
112366
 
          fts3ReadNextPos(&p2, &i2);
112367
 
        }else if( i1<i2 ){
112368
 
          fts3ReadNextPos(&p1, &i1);
112369
 
        }else{
112370
 
          fts3ReadNextPos(&p2, &i2);
112371
 
        }
112372
 
      }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
112373
 
    }else if( iCol1<iCol2 ){
112374
 
      p1 += fts3PutColNumber(&p, iCol1);
112375
 
      fts3ColumnlistCopy(&p, &p1);
112376
 
    }else{
112377
 
      p2 += fts3PutColNumber(&p, iCol2);
112378
 
      fts3ColumnlistCopy(&p, &p2);
112379
 
    }
112380
 
  }
112381
 
 
112382
 
  *p++ = POS_END;
112383
 
  *pp = p;
112384
 
  *pp1 = p1 + 1;
112385
 
  *pp2 = p2 + 1;
112386
 
}
112387
 
 
112388
 
/*
112389
 
** nToken==1 searches for adjacent positions.
112390
 
**
112391
 
** This function is used to merge two position lists into one. When it is
112392
 
** called, *pp1 and *pp2 must both point to position lists. A position-list is
112393
 
** the part of a doclist that follows each document id. For example, if a row
112394
 
** contains:
112395
 
**
112396
 
**     'a b c'|'x y z'|'a b b a'
112397
 
**
112398
 
** Then the position list for this row for token 'b' would consist of:
112399
 
**
112400
 
**     0x02 0x01 0x02 0x03 0x03 0x00
112401
 
**
112402
 
** When this function returns, both *pp1 and *pp2 are left pointing to the
112403
 
** byte following the 0x00 terminator of their respective position lists.
112404
 
**
112405
 
** If isSaveLeft is 0, an entry is added to the output position list for 
112406
 
** each position in *pp2 for which there exists one or more positions in
112407
 
** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
112408
 
** when the *pp1 token appears before the *pp2 token, but not more than nToken
112409
 
** slots before it.
112410
 
*/
112411
 
static int fts3PoslistPhraseMerge(
112412
 
  char **pp,                      /* IN/OUT: Preallocated output buffer */
112413
 
  int nToken,                     /* Maximum difference in token positions */
112414
 
  int isSaveLeft,                 /* Save the left position */
112415
 
  int isExact,                    /* If *pp1 is exactly nTokens before *pp2 */
112416
 
  char **pp1,                     /* IN/OUT: Left input list */
112417
 
  char **pp2                      /* IN/OUT: Right input list */
112418
 
){
112419
 
  char *p = (pp ? *pp : 0);
112420
 
  char *p1 = *pp1;
112421
 
  char *p2 = *pp2;
112422
 
  int iCol1 = 0;
112423
 
  int iCol2 = 0;
112424
 
 
112425
 
  /* Never set both isSaveLeft and isExact for the same invocation. */
112426
 
  assert( isSaveLeft==0 || isExact==0 );
112427
 
 
112428
 
  assert( *p1!=0 && *p2!=0 );
112429
 
  if( *p1==POS_COLUMN ){ 
112430
 
    p1++;
112431
 
    p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
112432
 
  }
112433
 
  if( *p2==POS_COLUMN ){ 
112434
 
    p2++;
112435
 
    p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
112436
 
  }
112437
 
 
112438
 
  for(;;) {
112439
 
    if( iCol1==iCol2 ){
112440
 
      char *pSave = p;
112441
 
      sqlite3_int64 iPrev = 0;
112442
 
      sqlite3_int64 iPos1 = 0;
112443
 
      sqlite3_int64 iPos2 = 0;
112444
 
 
112445
 
      if( pp && iCol1 ){
112446
 
        *p++ = POS_COLUMN;
112447
 
        p += sqlite3Fts3PutVarint(p, iCol1);
112448
 
      }
112449
 
 
112450
 
      assert( *p1!=POS_END && *p1!=POS_COLUMN );
112451
 
      assert( *p2!=POS_END && *p2!=POS_COLUMN );
112452
 
      fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
112453
 
      fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
112454
 
 
112455
 
      for(;;) {
112456
 
        if( iPos2==iPos1+nToken 
112457
 
         || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken) 
112458
 
        ){
112459
 
          sqlite3_int64 iSave;
112460
 
          if( !pp ){
112461
 
            fts3PoslistCopy(0, &p2);
112462
 
            fts3PoslistCopy(0, &p1);
112463
 
            *pp1 = p1;
112464
 
            *pp2 = p2;
112465
 
            return 1;
112466
 
          }
112467
 
          iSave = isSaveLeft ? iPos1 : iPos2;
112468
 
          fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
112469
 
          pSave = 0;
112470
 
        }
112471
 
        if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
112472
 
          if( (*p2&0xFE)==0 ) break;
112473
 
          fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
112474
 
        }else{
112475
 
          if( (*p1&0xFE)==0 ) break;
112476
 
          fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
112477
 
        }
112478
 
      }
112479
 
 
112480
 
      if( pSave ){
112481
 
        assert( pp && p );
112482
 
        p = pSave;
112483
 
      }
112484
 
 
112485
 
      fts3ColumnlistCopy(0, &p1);
112486
 
      fts3ColumnlistCopy(0, &p2);
112487
 
      assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
112488
 
      if( 0==*p1 || 0==*p2 ) break;
112489
 
 
112490
 
      p1++;
112491
 
      p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
112492
 
      p2++;
112493
 
      p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
112494
 
    }
112495
 
 
112496
 
    /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
112497
 
    ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
112498
 
    ** end of the position list, or the 0x01 that precedes the next 
112499
 
    ** column-number in the position list. 
112500
 
    */
112501
 
    else if( iCol1<iCol2 ){
112502
 
      fts3ColumnlistCopy(0, &p1);
112503
 
      if( 0==*p1 ) break;
112504
 
      p1++;
112505
 
      p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
112506
 
    }else{
112507
 
      fts3ColumnlistCopy(0, &p2);
112508
 
      if( 0==*p2 ) break;
112509
 
      p2++;
112510
 
      p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
112511
 
    }
112512
 
  }
112513
 
 
112514
 
  fts3PoslistCopy(0, &p2);
112515
 
  fts3PoslistCopy(0, &p1);
112516
 
  *pp1 = p1;
112517
 
  *pp2 = p2;
112518
 
  if( !pp || *pp==p ){
112519
 
    return 0;
112520
 
  }
112521
 
  *p++ = 0x00;
112522
 
  *pp = p;
112523
 
  return 1;
112524
 
}
112525
 
 
112526
 
/*
112527
 
** Merge two position-lists as required by the NEAR operator.
112528
 
*/
112529
 
static int fts3PoslistNearMerge(
112530
 
  char **pp,                      /* Output buffer */
112531
 
  char *aTmp,                     /* Temporary buffer space */
112532
 
  int nRight,                     /* Maximum difference in token positions */
112533
 
  int nLeft,                      /* Maximum difference in token positions */
112534
 
  char **pp1,                     /* IN/OUT: Left input list */
112535
 
  char **pp2                      /* IN/OUT: Right input list */
112536
 
){
112537
 
  char *p1 = *pp1;
112538
 
  char *p2 = *pp2;
112539
 
 
112540
 
  if( !pp ){
112541
 
    if( fts3PoslistPhraseMerge(0, nRight, 0, 0, pp1, pp2) ) return 1;
112542
 
    *pp1 = p1;
112543
 
    *pp2 = p2;
112544
 
    return fts3PoslistPhraseMerge(0, nLeft, 0, 0, pp2, pp1);
112545
 
  }else{
112546
 
    char *pTmp1 = aTmp;
112547
 
    char *pTmp2;
112548
 
    char *aTmp2;
112549
 
    int res = 1;
112550
 
 
112551
 
    fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
112552
 
    aTmp2 = pTmp2 = pTmp1;
112553
 
    *pp1 = p1;
112554
 
    *pp2 = p2;
112555
 
    fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
112556
 
    if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
112557
 
      fts3PoslistMerge(pp, &aTmp, &aTmp2);
112558
 
    }else if( pTmp1!=aTmp ){
112559
 
      fts3PoslistCopy(pp, &aTmp);
112560
 
    }else if( pTmp2!=aTmp2 ){
112561
 
      fts3PoslistCopy(pp, &aTmp2);
112562
 
    }else{
112563
 
      res = 0;
112564
 
    }
112565
 
 
112566
 
    return res;
112567
 
  }
112568
 
}
112569
 
 
112570
 
/*
112571
 
** Values that may be used as the first parameter to fts3DoclistMerge().
112572
 
*/
112573
 
#define MERGE_NOT        2        /* D + D -> D */
112574
 
#define MERGE_AND        3        /* D + D -> D */
112575
 
#define MERGE_OR         4        /* D + D -> D */
112576
 
#define MERGE_POS_OR     5        /* P + P -> P */
112577
 
#define MERGE_PHRASE     6        /* P + P -> D */
112578
 
#define MERGE_POS_PHRASE 7        /* P + P -> P */
112579
 
#define MERGE_NEAR       8        /* P + P -> D */
112580
 
#define MERGE_POS_NEAR   9        /* P + P -> P */
112581
 
 
112582
 
/*
112583
 
** Merge the two doclists passed in buffer a1 (size n1 bytes) and a2
112584
 
** (size n2 bytes). The output is written to pre-allocated buffer aBuffer,
112585
 
** which is guaranteed to be large enough to hold the results. The number
112586
 
** of bytes written to aBuffer is stored in *pnBuffer before returning.
112587
 
**
112588
 
** If successful, SQLITE_OK is returned. Otherwise, if a malloc error
112589
 
** occurs while allocating a temporary buffer as part of the merge operation,
112590
 
** SQLITE_NOMEM is returned.
112591
 
*/
112592
 
static int fts3DoclistMerge(
112593
 
  int mergetype,                  /* One of the MERGE_XXX constants */
112594
 
  int nParam1,                    /* Used by MERGE_NEAR and MERGE_POS_NEAR */
112595
 
  int nParam2,                    /* Used by MERGE_NEAR and MERGE_POS_NEAR */
112596
 
  char *aBuffer,                  /* Pre-allocated output buffer */
112597
 
  int *pnBuffer,                  /* OUT: Bytes written to aBuffer */
112598
 
  char *a1,                       /* Buffer containing first doclist */
112599
 
  int n1,                         /* Size of buffer a1 */
112600
 
  char *a2,                       /* Buffer containing second doclist */
112601
 
  int n2,                         /* Size of buffer a2 */
112602
 
  int *pnDoc                      /* OUT: Number of docids in output */
112603
 
){
112604
 
  sqlite3_int64 i1 = 0;
112605
 
  sqlite3_int64 i2 = 0;
112606
 
  sqlite3_int64 iPrev = 0;
112607
 
 
112608
 
  char *p = aBuffer;
112609
 
  char *p1 = a1;
112610
 
  char *p2 = a2;
112611
 
  char *pEnd1 = &a1[n1];
112612
 
  char *pEnd2 = &a2[n2];
112613
 
  int nDoc = 0;
112614
 
 
112615
 
  assert( mergetype==MERGE_OR     || mergetype==MERGE_POS_OR 
112616
 
       || mergetype==MERGE_AND    || mergetype==MERGE_NOT
112617
 
       || mergetype==MERGE_PHRASE || mergetype==MERGE_POS_PHRASE
112618
 
       || mergetype==MERGE_NEAR   || mergetype==MERGE_POS_NEAR
112619
 
  );
112620
 
 
112621
 
  if( !aBuffer ){
112622
 
    *pnBuffer = 0;
112623
 
    return SQLITE_NOMEM;
112624
 
  }
112625
 
 
112626
 
  /* Read the first docid from each doclist */
112627
 
  fts3GetDeltaVarint2(&p1, pEnd1, &i1);
112628
 
  fts3GetDeltaVarint2(&p2, pEnd2, &i2);
112629
 
 
112630
 
  switch( mergetype ){
112631
 
    case MERGE_OR:
112632
 
    case MERGE_POS_OR:
112633
 
      while( p1 || p2 ){
112634
 
        if( p2 && p1 && i1==i2 ){
112635
 
          fts3PutDeltaVarint(&p, &iPrev, i1);
112636
 
          if( mergetype==MERGE_POS_OR ) fts3PoslistMerge(&p, &p1, &p2);
112637
 
          fts3GetDeltaVarint2(&p1, pEnd1, &i1);
112638
 
          fts3GetDeltaVarint2(&p2, pEnd2, &i2);
112639
 
        }else if( !p2 || (p1 && i1<i2) ){
112640
 
          fts3PutDeltaVarint(&p, &iPrev, i1);
112641
 
          if( mergetype==MERGE_POS_OR ) fts3PoslistCopy(&p, &p1);
112642
 
          fts3GetDeltaVarint2(&p1, pEnd1, &i1);
112643
 
        }else{
112644
 
          fts3PutDeltaVarint(&p, &iPrev, i2);
112645
 
          if( mergetype==MERGE_POS_OR ) fts3PoslistCopy(&p, &p2);
112646
 
          fts3GetDeltaVarint2(&p2, pEnd2, &i2);
112647
 
        }
112648
 
      }
112649
 
      break;
112650
 
 
112651
 
    case MERGE_AND:
112652
 
      while( p1 && p2 ){
112653
 
        if( i1==i2 ){
112654
 
          fts3PutDeltaVarint(&p, &iPrev, i1);
112655
 
          fts3GetDeltaVarint2(&p1, pEnd1, &i1);
112656
 
          fts3GetDeltaVarint2(&p2, pEnd2, &i2);
112657
 
          nDoc++;
112658
 
        }else if( i1<i2 ){
112659
 
          fts3GetDeltaVarint2(&p1, pEnd1, &i1);
112660
 
        }else{
112661
 
          fts3GetDeltaVarint2(&p2, pEnd2, &i2);
112662
 
        }
112663
 
      }
112664
 
      break;
112665
 
 
112666
 
    case MERGE_NOT:
112667
 
      while( p1 ){
112668
 
        if( p2 && i1==i2 ){
112669
 
          fts3GetDeltaVarint2(&p1, pEnd1, &i1);
112670
 
          fts3GetDeltaVarint2(&p2, pEnd2, &i2);
112671
 
        }else if( !p2 || i1<i2 ){
112672
 
          fts3PutDeltaVarint(&p, &iPrev, i1);
112673
 
          fts3GetDeltaVarint2(&p1, pEnd1, &i1);
112674
 
        }else{
112675
 
          fts3GetDeltaVarint2(&p2, pEnd2, &i2);
112676
 
        }
112677
 
      }
112678
 
      break;
112679
 
 
112680
 
    case MERGE_POS_PHRASE:
112681
 
    case MERGE_PHRASE: {
112682
 
      char **ppPos = (mergetype==MERGE_PHRASE ? 0 : &p);
112683
 
      while( p1 && p2 ){
112684
 
        if( i1==i2 ){
112685
 
          char *pSave = p;
112686
 
          sqlite3_int64 iPrevSave = iPrev;
112687
 
          fts3PutDeltaVarint(&p, &iPrev, i1);
112688
 
          if( 0==fts3PoslistPhraseMerge(ppPos, nParam1, 0, 1, &p1, &p2) ){
112689
 
            p = pSave;
112690
 
            iPrev = iPrevSave;
112691
 
          }else{
112692
 
            nDoc++;
112693
 
          }
112694
 
          fts3GetDeltaVarint2(&p1, pEnd1, &i1);
112695
 
          fts3GetDeltaVarint2(&p2, pEnd2, &i2);
112696
 
        }else if( i1<i2 ){
112697
 
          fts3PoslistCopy(0, &p1);
112698
 
          fts3GetDeltaVarint2(&p1, pEnd1, &i1);
112699
 
        }else{
112700
 
          fts3PoslistCopy(0, &p2);
112701
 
          fts3GetDeltaVarint2(&p2, pEnd2, &i2);
112702
 
        }
112703
 
      }
112704
 
      break;
112705
 
    }
112706
 
 
112707
 
    default: assert( mergetype==MERGE_POS_NEAR || mergetype==MERGE_NEAR ); {
112708
 
      char *aTmp = 0;
112709
 
      char **ppPos = 0;
112710
 
 
112711
 
      if( mergetype==MERGE_POS_NEAR ){
112712
 
        ppPos = &p;
112713
 
        aTmp = sqlite3_malloc(2*(n1+n2+1));
112714
 
        if( !aTmp ){
112715
 
          return SQLITE_NOMEM;
112716
 
        }
112717
 
      }
112718
 
 
112719
 
      while( p1 && p2 ){
112720
 
        if( i1==i2 ){
112721
 
          char *pSave = p;
112722
 
          sqlite3_int64 iPrevSave = iPrev;
112723
 
          fts3PutDeltaVarint(&p, &iPrev, i1);
112724
 
 
112725
 
          if( !fts3PoslistNearMerge(ppPos, aTmp, nParam1, nParam2, &p1, &p2) ){
112726
 
            iPrev = iPrevSave;
112727
 
            p = pSave;
112728
 
          }
112729
 
 
112730
 
          fts3GetDeltaVarint2(&p1, pEnd1, &i1);
112731
 
          fts3GetDeltaVarint2(&p2, pEnd2, &i2);
112732
 
        }else if( i1<i2 ){
112733
 
          fts3PoslistCopy(0, &p1);
112734
 
          fts3GetDeltaVarint2(&p1, pEnd1, &i1);
112735
 
        }else{
112736
 
          fts3PoslistCopy(0, &p2);
112737
 
          fts3GetDeltaVarint2(&p2, pEnd2, &i2);
112738
 
        }
112739
 
      }
112740
 
      sqlite3_free(aTmp);
112741
 
      break;
112742
 
    }
112743
 
  }
112744
 
 
112745
 
  if( pnDoc ) *pnDoc = nDoc;
112746
 
  *pnBuffer = (int)(p-aBuffer);
112747
 
  return SQLITE_OK;
112748
 
}
112749
 
 
112750
 
/* 
112751
 
** A pointer to an instance of this structure is used as the context 
112752
 
** argument to sqlite3Fts3SegReaderIterate()
112753
 
*/
112754
 
typedef struct TermSelect TermSelect;
112755
 
struct TermSelect {
112756
 
  int isReqPos;
112757
 
  char *aaOutput[16];             /* Malloc'd output buffer */
112758
 
  int anOutput[16];               /* Size of output in bytes */
112759
 
};
112760
 
 
112761
 
/*
112762
 
** Merge all doclists in the TermSelect.aaOutput[] array into a single
112763
 
** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
112764
 
** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
112765
 
**
112766
 
** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
112767
 
** the responsibility of the caller to free any doclists left in the
112768
 
** TermSelect.aaOutput[] array.
112769
 
*/
112770
 
static int fts3TermSelectMerge(TermSelect *pTS){
112771
 
  int mergetype = (pTS->isReqPos ? MERGE_POS_OR : MERGE_OR);
112772
 
  char *aOut = 0;
112773
 
  int nOut = 0;
112774
 
  int i;
112775
 
 
112776
 
  /* Loop through the doclists in the aaOutput[] array. Merge them all
112777
 
  ** into a single doclist.
112778
 
  */
112779
 
  for(i=0; i<SizeofArray(pTS->aaOutput); i++){
112780
 
    if( pTS->aaOutput[i] ){
112781
 
      if( !aOut ){
112782
 
        aOut = pTS->aaOutput[i];
112783
 
        nOut = pTS->anOutput[i];
112784
 
        pTS->aaOutput[i] = 0;
112785
 
      }else{
112786
 
        int nNew = nOut + pTS->anOutput[i];
112787
 
        char *aNew = sqlite3_malloc(nNew);
112788
 
        if( !aNew ){
112789
 
          sqlite3_free(aOut);
112790
 
          return SQLITE_NOMEM;
112791
 
        }
112792
 
        fts3DoclistMerge(mergetype, 0, 0,
112793
 
            aNew, &nNew, pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, 0
112794
 
        );
112795
 
        sqlite3_free(pTS->aaOutput[i]);
112796
 
        sqlite3_free(aOut);
112797
 
        pTS->aaOutput[i] = 0;
112798
 
        aOut = aNew;
112799
 
        nOut = nNew;
112800
 
      }
112801
 
    }
112802
 
  }
112803
 
 
112804
 
  pTS->aaOutput[0] = aOut;
112805
 
  pTS->anOutput[0] = nOut;
112806
 
  return SQLITE_OK;
112807
 
}
112808
 
 
112809
 
/*
112810
 
** This function is used as the sqlite3Fts3SegReaderIterate() callback when
112811
 
** querying the full-text index for a doclist associated with a term or
112812
 
** term-prefix.
112813
 
*/
112814
 
static int fts3TermSelectCb(
112815
 
  Fts3Table *p,                   /* Virtual table object */
112816
 
  void *pContext,                 /* Pointer to TermSelect structure */
112817
 
  char *zTerm,
112818
 
  int nTerm,
112819
 
  char *aDoclist,
112820
 
  int nDoclist
112821
 
){
112822
 
  TermSelect *pTS = (TermSelect *)pContext;
112823
 
 
112824
 
  UNUSED_PARAMETER(p);
112825
 
  UNUSED_PARAMETER(zTerm);
112826
 
  UNUSED_PARAMETER(nTerm);
112827
 
 
112828
 
  if( pTS->aaOutput[0]==0 ){
112829
 
    /* If this is the first term selected, copy the doclist to the output
112830
 
    ** buffer using memcpy(). TODO: Add a way to transfer control of the
112831
 
    ** aDoclist buffer from the caller so as to avoid the memcpy().
112832
 
    */
112833
 
    pTS->aaOutput[0] = sqlite3_malloc(nDoclist);
112834
 
    pTS->anOutput[0] = nDoclist;
112835
 
    if( pTS->aaOutput[0] ){
112836
 
      memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
112837
 
    }else{
112838
 
      return SQLITE_NOMEM;
112839
 
    }
112840
 
  }else{
112841
 
    int mergetype = (pTS->isReqPos ? MERGE_POS_OR : MERGE_OR);
112842
 
    char *aMerge = aDoclist;
112843
 
    int nMerge = nDoclist;
112844
 
    int iOut;
112845
 
 
112846
 
    for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
112847
 
      char *aNew;
112848
 
      int nNew;
112849
 
      if( pTS->aaOutput[iOut]==0 ){
112850
 
        assert( iOut>0 );
112851
 
        pTS->aaOutput[iOut] = aMerge;
112852
 
        pTS->anOutput[iOut] = nMerge;
112853
 
        break;
112854
 
      }
112855
 
 
112856
 
      nNew = nMerge + pTS->anOutput[iOut];
112857
 
      aNew = sqlite3_malloc(nNew);
112858
 
      if( !aNew ){
112859
 
        if( aMerge!=aDoclist ){
112860
 
          sqlite3_free(aMerge);
112861
 
        }
112862
 
        return SQLITE_NOMEM;
112863
 
      }
112864
 
      fts3DoclistMerge(mergetype, 0, 0, aNew, &nNew, 
112865
 
          pTS->aaOutput[iOut], pTS->anOutput[iOut], aMerge, nMerge, 0
112866
 
      );
112867
 
 
112868
 
      if( iOut>0 ) sqlite3_free(aMerge);
112869
 
      sqlite3_free(pTS->aaOutput[iOut]);
112870
 
      pTS->aaOutput[iOut] = 0;
112871
 
 
112872
 
      aMerge = aNew;
112873
 
      nMerge = nNew;
112874
 
      if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
112875
 
        pTS->aaOutput[iOut] = aMerge;
112876
 
        pTS->anOutput[iOut] = nMerge;
112877
 
      }
112878
 
    }
112879
 
  }
112880
 
  return SQLITE_OK;
112881
 
}
112882
 
 
112883
 
static int fts3DeferredTermSelect(
112884
 
  Fts3DeferredToken *pToken,      /* Phrase token */
112885
 
  int isTermPos,                  /* True to include positions */
112886
 
  int *pnOut,                     /* OUT: Size of list */
112887
 
  char **ppOut                    /* OUT: Body of list */
112888
 
){
112889
 
  char *aSource;
112890
 
  int nSource;
112891
 
 
112892
 
  aSource = sqlite3Fts3DeferredDoclist(pToken, &nSource);
112893
 
  if( !aSource ){
112894
 
    *pnOut = 0;
112895
 
    *ppOut = 0;
112896
 
  }else if( isTermPos ){
112897
 
    *ppOut = sqlite3_malloc(nSource);
112898
 
    if( !*ppOut ) return SQLITE_NOMEM;
112899
 
    memcpy(*ppOut, aSource, nSource);
112900
 
    *pnOut = nSource;
112901
 
  }else{
112902
 
    sqlite3_int64 docid;
112903
 
    *pnOut = sqlite3Fts3GetVarint(aSource, &docid);
112904
 
    *ppOut = sqlite3_malloc(*pnOut);
112905
 
    if( !*ppOut ) return SQLITE_NOMEM;
112906
 
    sqlite3Fts3PutVarint(*ppOut, docid);
112907
 
  }
112908
 
 
112909
 
  return SQLITE_OK;
112910
 
}
112911
 
 
112912
 
SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
112913
 
  Fts3Table *p,                   /* FTS3 table handle */
112914
 
  int iLevel,                     /* Level of segments to scan */
112915
 
  const char *zTerm,              /* Term to query for */
112916
 
  int nTerm,                      /* Size of zTerm in bytes */
112917
 
  int isPrefix,                   /* True for a prefix search */
112918
 
  int isScan,                     /* True to scan from zTerm to EOF */
112919
 
  Fts3SegReaderCursor *pCsr       /* Cursor object to populate */
112920
 
){
112921
 
  int rc = SQLITE_OK;
112922
 
  int rc2;
112923
 
  int iAge = 0;
112924
 
  sqlite3_stmt *pStmt = 0;
112925
 
  Fts3SegReader *pPending = 0;
112926
 
 
112927
 
  assert( iLevel==FTS3_SEGCURSOR_ALL 
112928
 
      ||  iLevel==FTS3_SEGCURSOR_PENDING 
112929
 
      ||  iLevel>=0
112930
 
  );
112931
 
  assert( FTS3_SEGCURSOR_PENDING<0 );
112932
 
  assert( FTS3_SEGCURSOR_ALL<0 );
112933
 
  assert( iLevel==FTS3_SEGCURSOR_ALL || (zTerm==0 && isPrefix==1) );
112934
 
  assert( isPrefix==0 || isScan==0 );
112935
 
 
112936
 
 
112937
 
  memset(pCsr, 0, sizeof(Fts3SegReaderCursor));
112938
 
 
112939
 
  /* If iLevel is less than 0, include a seg-reader for the pending-terms. */
112940
 
  assert( isScan==0 || fts3HashCount(&p->pendingTerms)==0 );
112941
 
  if( iLevel<0 && isScan==0 ){
112942
 
    rc = sqlite3Fts3SegReaderPending(p, zTerm, nTerm, isPrefix, &pPending);
112943
 
    if( rc==SQLITE_OK && pPending ){
112944
 
      int nByte = (sizeof(Fts3SegReader *) * 16);
112945
 
      pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
112946
 
      if( pCsr->apSegment==0 ){
112947
 
        rc = SQLITE_NOMEM;
112948
 
      }else{
112949
 
        pCsr->apSegment[0] = pPending;
112950
 
        pCsr->nSegment = 1;
112951
 
        pPending = 0;
112952
 
      }
112953
 
    }
112954
 
  }
112955
 
 
112956
 
  if( iLevel!=FTS3_SEGCURSOR_PENDING ){
112957
 
    if( rc==SQLITE_OK ){
112958
 
      rc = sqlite3Fts3AllSegdirs(p, iLevel, &pStmt);
112959
 
    }
112960
 
    while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
112961
 
 
112962
 
      /* Read the values returned by the SELECT into local variables. */
112963
 
      sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
112964
 
      sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
112965
 
      sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
112966
 
      int nRoot = sqlite3_column_bytes(pStmt, 4);
112967
 
      char const *zRoot = sqlite3_column_blob(pStmt, 4);
112968
 
 
112969
 
      /* If nSegment is a multiple of 16 the array needs to be extended. */
112970
 
      if( (pCsr->nSegment%16)==0 ){
112971
 
        Fts3SegReader **apNew;
112972
 
        int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
112973
 
        apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
112974
 
        if( !apNew ){
112975
 
          rc = SQLITE_NOMEM;
112976
 
          goto finished;
112977
 
        }
112978
 
        pCsr->apSegment = apNew;
112979
 
      }
112980
 
 
112981
 
      /* If zTerm is not NULL, and this segment is not stored entirely on its
112982
 
      ** root node, the range of leaves scanned can be reduced. Do this. */
112983
 
      if( iStartBlock && zTerm ){
112984
 
        sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
112985
 
        rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
112986
 
        if( rc!=SQLITE_OK ) goto finished;
112987
 
        if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
112988
 
      }
112989
 
 
112990
 
      rc = sqlite3Fts3SegReaderNew(iAge, iStartBlock, iLeavesEndBlock,
112991
 
          iEndBlock, zRoot, nRoot, &pCsr->apSegment[pCsr->nSegment]
112992
 
      );
112993
 
      if( rc!=SQLITE_OK ) goto finished;
112994
 
      pCsr->nSegment++;
112995
 
      iAge++;
112996
 
    }
112997
 
  }
112998
 
 
112999
 
 finished:
113000
 
  rc2 = sqlite3_reset(pStmt);
113001
 
  if( rc==SQLITE_DONE ) rc = rc2;
113002
 
  sqlite3Fts3SegReaderFree(pPending);
113003
 
 
113004
 
  return rc;
113005
 
}
113006
 
 
113007
 
 
113008
 
static int fts3TermSegReaderCursor(
113009
 
  Fts3Cursor *pCsr,               /* Virtual table cursor handle */
113010
 
  const char *zTerm,              /* Term to query for */
113011
 
  int nTerm,                      /* Size of zTerm in bytes */
113012
 
  int isPrefix,                   /* True for a prefix search */
113013
 
  Fts3SegReaderCursor **ppSegcsr  /* OUT: Allocated seg-reader cursor */
113014
 
){
113015
 
  Fts3SegReaderCursor *pSegcsr;   /* Object to allocate and return */
113016
 
  int rc = SQLITE_NOMEM;          /* Return code */
113017
 
 
113018
 
  pSegcsr = sqlite3_malloc(sizeof(Fts3SegReaderCursor));
113019
 
  if( pSegcsr ){
113020
 
    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
113021
 
    int i;
113022
 
    int nCost = 0;
113023
 
    rc = sqlite3Fts3SegReaderCursor(
113024
 
        p, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr);
113025
 
  
113026
 
    for(i=0; rc==SQLITE_OK && i<pSegcsr->nSegment; i++){
113027
 
      rc = sqlite3Fts3SegReaderCost(pCsr, pSegcsr->apSegment[i], &nCost);
113028
 
    }
113029
 
    pSegcsr->nCost = nCost;
113030
 
  }
113031
 
 
113032
 
  *ppSegcsr = pSegcsr;
113033
 
  return rc;
113034
 
}
113035
 
 
113036
 
static void fts3SegReaderCursorFree(Fts3SegReaderCursor *pSegcsr){
113037
 
  sqlite3Fts3SegReaderFinish(pSegcsr);
113038
 
  sqlite3_free(pSegcsr);
113039
 
}
113040
 
 
113041
 
/*
113042
 
** This function retreives the doclist for the specified term (or term
113043
 
** prefix) from the database. 
113044
 
**
113045
 
** The returned doclist may be in one of two formats, depending on the 
113046
 
** value of parameter isReqPos. If isReqPos is zero, then the doclist is
113047
 
** a sorted list of delta-compressed docids (a bare doclist). If isReqPos
113048
 
** is non-zero, then the returned list is in the same format as is stored 
113049
 
** in the database without the found length specifier at the start of on-disk
113050
 
** doclists.
113051
 
*/
113052
 
static int fts3TermSelect(
113053
 
  Fts3Table *p,                   /* Virtual table handle */
113054
 
  Fts3PhraseToken *pTok,          /* Token to query for */
113055
 
  int iColumn,                    /* Column to query (or -ve for all columns) */
113056
 
  int isReqPos,                   /* True to include position lists in output */
113057
 
  int *pnOut,                     /* OUT: Size of buffer at *ppOut */
113058
 
  char **ppOut                    /* OUT: Malloced result buffer */
113059
 
){
113060
 
  int rc;                         /* Return code */
113061
 
  Fts3SegReaderCursor *pSegcsr;   /* Seg-reader cursor for this term */
113062
 
  TermSelect tsc;                 /* Context object for fts3TermSelectCb() */
113063
 
  Fts3SegFilter filter;           /* Segment term filter configuration */
113064
 
 
113065
 
  pSegcsr = pTok->pSegcsr;
113066
 
  memset(&tsc, 0, sizeof(TermSelect));
113067
 
  tsc.isReqPos = isReqPos;
113068
 
 
113069
 
  filter.flags = FTS3_SEGMENT_IGNORE_EMPTY 
113070
 
        | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
113071
 
        | (isReqPos ? FTS3_SEGMENT_REQUIRE_POS : 0)
113072
 
        | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
113073
 
  filter.iCol = iColumn;
113074
 
  filter.zTerm = pTok->z;
113075
 
  filter.nTerm = pTok->n;
113076
 
 
113077
 
  rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
113078
 
  while( SQLITE_OK==rc
113079
 
      && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr)) 
113080
 
  ){
113081
 
    rc = fts3TermSelectCb(p, (void *)&tsc, 
113082
 
        pSegcsr->zTerm, pSegcsr->nTerm, pSegcsr->aDoclist, pSegcsr->nDoclist
113083
 
    );
113084
 
  }
113085
 
 
113086
 
  if( rc==SQLITE_OK ){
113087
 
    rc = fts3TermSelectMerge(&tsc);
113088
 
  }
113089
 
  if( rc==SQLITE_OK ){
113090
 
    *ppOut = tsc.aaOutput[0];
113091
 
    *pnOut = tsc.anOutput[0];
113092
 
  }else{
113093
 
    int i;
113094
 
    for(i=0; i<SizeofArray(tsc.aaOutput); i++){
113095
 
      sqlite3_free(tsc.aaOutput[i]);
113096
 
    }
113097
 
  }
113098
 
 
113099
 
  fts3SegReaderCursorFree(pSegcsr);
113100
 
  pTok->pSegcsr = 0;
113101
 
  return rc;
113102
 
}
113103
 
 
113104
 
/*
113105
 
** This function counts the total number of docids in the doclist stored
113106
 
** in buffer aList[], size nList bytes.
113107
 
**
113108
 
** If the isPoslist argument is true, then it is assumed that the doclist
113109
 
** contains a position-list following each docid. Otherwise, it is assumed
113110
 
** that the doclist is simply a list of docids stored as delta encoded 
113111
 
** varints.
113112
 
*/
113113
 
static int fts3DoclistCountDocids(int isPoslist, char *aList, int nList){
113114
 
  int nDoc = 0;                   /* Return value */
113115
 
  if( aList ){
113116
 
    char *aEnd = &aList[nList];   /* Pointer to one byte after EOF */
113117
 
    char *p = aList;              /* Cursor */
113118
 
    if( !isPoslist ){
113119
 
      /* The number of docids in the list is the same as the number of 
113120
 
      ** varints. In FTS3 a varint consists of a single byte with the 0x80 
113121
 
      ** bit cleared and zero or more bytes with the 0x80 bit set. So to
113122
 
      ** count the varints in the buffer, just count the number of bytes
113123
 
      ** with the 0x80 bit clear.  */
113124
 
      while( p<aEnd ) nDoc += (((*p++)&0x80)==0);
113125
 
    }else{
113126
 
      while( p<aEnd ){
113127
 
        nDoc++;
113128
 
        while( (*p++)&0x80 );     /* Skip docid varint */
113129
 
        fts3PoslistCopy(0, &p);   /* Skip over position list */
113130
 
      }
113131
 
    }
113132
 
  }
113133
 
 
113134
 
  return nDoc;
113135
 
}
113136
 
 
113137
 
/*
113138
 
** Call sqlite3Fts3DeferToken() for each token in the expression pExpr.
113139
 
*/
113140
 
static int fts3DeferExpression(Fts3Cursor *pCsr, Fts3Expr *pExpr){
113141
 
  int rc = SQLITE_OK;
113142
 
  if( pExpr ){
113143
 
    rc = fts3DeferExpression(pCsr, pExpr->pLeft);
113144
 
    if( rc==SQLITE_OK ){
113145
 
      rc = fts3DeferExpression(pCsr, pExpr->pRight);
113146
 
    }
113147
 
    if( pExpr->eType==FTSQUERY_PHRASE ){
113148
 
      int iCol = pExpr->pPhrase->iColumn;
113149
 
      int i;
113150
 
      for(i=0; rc==SQLITE_OK && i<pExpr->pPhrase->nToken; i++){
113151
 
        Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
113152
 
        if( pToken->pDeferred==0 ){
113153
 
          rc = sqlite3Fts3DeferToken(pCsr, pToken, iCol);
113154
 
        }
113155
 
      }
113156
 
    }
113157
 
  }
113158
 
  return rc;
113159
 
}
113160
 
 
113161
 
/*
113162
 
** This function removes the position information from a doclist. When
113163
 
** called, buffer aList (size *pnList bytes) contains a doclist that includes
113164
 
** position information. This function removes the position information so
113165
 
** that aList contains only docids, and adjusts *pnList to reflect the new
113166
 
** (possibly reduced) size of the doclist.
113167
 
*/
113168
 
static void fts3DoclistStripPositions(
113169
 
  char *aList,                    /* IN/OUT: Buffer containing doclist */
113170
 
  int *pnList                     /* IN/OUT: Size of doclist in bytes */
113171
 
){
113172
 
  if( aList ){
113173
 
    char *aEnd = &aList[*pnList]; /* Pointer to one byte after EOF */
113174
 
    char *p = aList;              /* Input cursor */
113175
 
    char *pOut = aList;           /* Output cursor */
113176
 
  
113177
 
    while( p<aEnd ){
113178
 
      sqlite3_int64 delta;
113179
 
      p += sqlite3Fts3GetVarint(p, &delta);
113180
 
      fts3PoslistCopy(0, &p);
113181
 
      pOut += sqlite3Fts3PutVarint(pOut, delta);
113182
 
    }
113183
 
 
113184
 
    *pnList = (int)(pOut - aList);
113185
 
  }
113186
 
}
113187
 
 
113188
 
/* 
113189
 
** Return a DocList corresponding to the phrase *pPhrase.
113190
 
**
113191
 
** If this function returns SQLITE_OK, but *pnOut is set to a negative value,
113192
 
** then no tokens in the phrase were looked up in the full-text index. This
113193
 
** is only possible when this function is called from within xFilter(). The
113194
 
** caller should assume that all documents match the phrase. The actual
113195
 
** filtering will take place in xNext().
113196
 
*/
113197
 
static int fts3PhraseSelect(
113198
 
  Fts3Cursor *pCsr,               /* Virtual table cursor handle */
113199
 
  Fts3Phrase *pPhrase,            /* Phrase to return a doclist for */
113200
 
  int isReqPos,                   /* True if output should contain positions */
113201
 
  char **paOut,                   /* OUT: Pointer to malloc'd result buffer */
113202
 
  int *pnOut                      /* OUT: Size of buffer at *paOut */
113203
 
){
113204
 
  char *pOut = 0;
113205
 
  int nOut = 0;
113206
 
  int rc = SQLITE_OK;
113207
 
  int ii;
113208
 
  int iCol = pPhrase->iColumn;
113209
 
  int isTermPos = (pPhrase->nToken>1 || isReqPos);
113210
 
  Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
113211
 
  int isFirst = 1;
113212
 
 
113213
 
  int iPrevTok = 0;
113214
 
  int nDoc = 0;
113215
 
 
113216
 
  /* If this is an xFilter() evaluation, create a segment-reader for each
113217
 
  ** phrase token. Or, if this is an xNext() or snippet/offsets/matchinfo
113218
 
  ** evaluation, only create segment-readers if there are no Fts3DeferredToken
113219
 
  ** objects attached to the phrase-tokens.
113220
 
  */
113221
 
  for(ii=0; ii<pPhrase->nToken; ii++){
113222
 
    Fts3PhraseToken *pTok = &pPhrase->aToken[ii];
113223
 
    if( pTok->pSegcsr==0 ){
113224
 
      if( (pCsr->eEvalmode==FTS3_EVAL_FILTER)
113225
 
       || (pCsr->eEvalmode==FTS3_EVAL_NEXT && pCsr->pDeferred==0) 
113226
 
       || (pCsr->eEvalmode==FTS3_EVAL_MATCHINFO && pTok->bFulltext) 
113227
 
      ){
113228
 
        rc = fts3TermSegReaderCursor(
113229
 
            pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pSegcsr
113230
 
        );
113231
 
        if( rc!=SQLITE_OK ) return rc;
113232
 
      }
113233
 
    }
113234
 
  }
113235
 
 
113236
 
  for(ii=0; ii<pPhrase->nToken; ii++){
113237
 
    Fts3PhraseToken *pTok;        /* Token to find doclist for */
113238
 
    int iTok = 0;                 /* The token being queried this iteration */
113239
 
    char *pList = 0;              /* Pointer to token doclist */
113240
 
    int nList = 0;                /* Size of buffer at pList */
113241
 
 
113242
 
    /* Select a token to process. If this is an xFilter() call, then tokens 
113243
 
    ** are processed in order from least to most costly. Otherwise, tokens 
113244
 
    ** are processed in the order in which they occur in the phrase.
113245
 
    */
113246
 
    if( pCsr->eEvalmode==FTS3_EVAL_MATCHINFO ){
113247
 
      assert( isReqPos );
113248
 
      iTok = ii;
113249
 
      pTok = &pPhrase->aToken[iTok];
113250
 
      if( pTok->bFulltext==0 ) continue;
113251
 
    }else if( pCsr->eEvalmode==FTS3_EVAL_NEXT || isReqPos ){
113252
 
      iTok = ii;
113253
 
      pTok = &pPhrase->aToken[iTok];
113254
 
    }else{
113255
 
      int nMinCost = 0x7FFFFFFF;
113256
 
      int jj;
113257
 
 
113258
 
      /* Find the remaining token with the lowest cost. */
113259
 
      for(jj=0; jj<pPhrase->nToken; jj++){
113260
 
        Fts3SegReaderCursor *pSegcsr = pPhrase->aToken[jj].pSegcsr;
113261
 
        if( pSegcsr && pSegcsr->nCost<nMinCost ){
113262
 
          iTok = jj;
113263
 
          nMinCost = pSegcsr->nCost;
113264
 
        }
113265
 
      }
113266
 
      pTok = &pPhrase->aToken[iTok];
113267
 
 
113268
 
      /* This branch is taken if it is determined that loading the doclist
113269
 
      ** for the next token would require more IO than loading all documents
113270
 
      ** currently identified by doclist pOut/nOut. No further doclists will
113271
 
      ** be loaded from the full-text index for this phrase.
113272
 
      */
113273
 
      if( nMinCost>nDoc && ii>0 ){
113274
 
        rc = fts3DeferExpression(pCsr, pCsr->pExpr);
113275
 
        break;
113276
 
      }
113277
 
    }
113278
 
 
113279
 
    if( pCsr->eEvalmode==FTS3_EVAL_NEXT && pTok->pDeferred ){
113280
 
      rc = fts3DeferredTermSelect(pTok->pDeferred, isTermPos, &nList, &pList);
113281
 
    }else{
113282
 
      if( pTok->pSegcsr ){
113283
 
        rc = fts3TermSelect(p, pTok, iCol, isTermPos, &nList, &pList);
113284
 
      }
113285
 
      pTok->bFulltext = 1;
113286
 
    }
113287
 
    assert( rc!=SQLITE_OK || pCsr->eEvalmode || pTok->pSegcsr==0 );
113288
 
    if( rc!=SQLITE_OK ) break;
113289
 
 
113290
 
    if( isFirst ){
113291
 
      pOut = pList;
113292
 
      nOut = nList;
113293
 
      if( pCsr->eEvalmode==FTS3_EVAL_FILTER && pPhrase->nToken>1 ){
113294
 
        nDoc = fts3DoclistCountDocids(1, pOut, nOut);
113295
 
      }
113296
 
      isFirst = 0;
113297
 
      iPrevTok = iTok;
113298
 
    }else{
113299
 
      /* Merge the new term list and the current output. */
113300
 
      char *aLeft, *aRight;
113301
 
      int nLeft, nRight;
113302
 
      int nDist;
113303
 
      int mt;
113304
 
 
113305
 
      /* If this is the final token of the phrase, and positions were not
113306
 
      ** requested by the caller, use MERGE_PHRASE instead of POS_PHRASE.
113307
 
      ** This drops the position information from the output list.
113308
 
      */
113309
 
      mt = MERGE_POS_PHRASE;
113310
 
      if( ii==pPhrase->nToken-1 && !isReqPos ) mt = MERGE_PHRASE;
113311
 
 
113312
 
      assert( iPrevTok!=iTok );
113313
 
      if( iPrevTok<iTok ){
113314
 
        aLeft = pOut;
113315
 
        nLeft = nOut;
113316
 
        aRight = pList;
113317
 
        nRight = nList;
113318
 
        nDist = iTok-iPrevTok;
113319
 
        iPrevTok = iTok;
113320
 
      }else{
113321
 
        aRight = pOut;
113322
 
        nRight = nOut;
113323
 
        aLeft = pList;
113324
 
        nLeft = nList;
113325
 
        nDist = iPrevTok-iTok;
113326
 
      }
113327
 
      pOut = aRight;
113328
 
      fts3DoclistMerge(
113329
 
          mt, nDist, 0, pOut, &nOut, aLeft, nLeft, aRight, nRight, &nDoc
113330
 
      );
113331
 
      sqlite3_free(aLeft);
113332
 
    }
113333
 
    assert( nOut==0 || pOut!=0 );
113334
 
  }
113335
 
 
113336
 
  if( rc==SQLITE_OK ){
113337
 
    if( ii!=pPhrase->nToken ){
113338
 
      assert( pCsr->eEvalmode==FTS3_EVAL_FILTER && isReqPos==0 );
113339
 
      fts3DoclistStripPositions(pOut, &nOut);
113340
 
    }
113341
 
    *paOut = pOut;
113342
 
    *pnOut = nOut;
113343
 
  }else{
113344
 
    sqlite3_free(pOut);
113345
 
  }
113346
 
  return rc;
113347
 
}
113348
 
 
113349
 
/*
113350
 
** This function merges two doclists according to the requirements of a
113351
 
** NEAR operator.
113352
 
**
113353
 
** Both input doclists must include position information. The output doclist 
113354
 
** includes position information if the first argument to this function
113355
 
** is MERGE_POS_NEAR, or does not if it is MERGE_NEAR.
113356
 
*/
113357
 
static int fts3NearMerge(
113358
 
  int mergetype,                  /* MERGE_POS_NEAR or MERGE_NEAR */
113359
 
  int nNear,                      /* Parameter to NEAR operator */
113360
 
  int nTokenLeft,                 /* Number of tokens in LHS phrase arg */
113361
 
  char *aLeft,                    /* Doclist for LHS (incl. positions) */
113362
 
  int nLeft,                      /* Size of LHS doclist in bytes */
113363
 
  int nTokenRight,                /* As nTokenLeft */
113364
 
  char *aRight,                   /* As aLeft */
113365
 
  int nRight,                     /* As nRight */
113366
 
  char **paOut,                   /* OUT: Results of merge (malloced) */
113367
 
  int *pnOut                      /* OUT: Sized of output buffer */
113368
 
){
113369
 
  char *aOut;                     /* Buffer to write output doclist to */
113370
 
  int rc;                         /* Return code */
113371
 
 
113372
 
  assert( mergetype==MERGE_POS_NEAR || MERGE_NEAR );
113373
 
 
113374
 
  aOut = sqlite3_malloc(nLeft+nRight+1);
113375
 
  if( aOut==0 ){
113376
 
    rc = SQLITE_NOMEM;
113377
 
  }else{
113378
 
    rc = fts3DoclistMerge(mergetype, nNear+nTokenRight, nNear+nTokenLeft, 
113379
 
      aOut, pnOut, aLeft, nLeft, aRight, nRight, 0
113380
 
    );
113381
 
    if( rc!=SQLITE_OK ){
113382
 
      sqlite3_free(aOut);
113383
 
      aOut = 0;
113384
 
    }
113385
 
  }
113386
 
 
113387
 
  *paOut = aOut;
113388
 
  return rc;
113389
 
}
113390
 
 
113391
 
/*
113392
 
** This function is used as part of the processing for the snippet() and
113393
 
** offsets() functions.
113394
 
**
113395
 
** Both pLeft and pRight are expression nodes of type FTSQUERY_PHRASE. Both
113396
 
** have their respective doclists (including position information) loaded
113397
 
** in Fts3Expr.aDoclist/nDoclist. This function removes all entries from
113398
 
** each doclist that are not within nNear tokens of a corresponding entry
113399
 
** in the other doclist.
113400
 
*/
113401
 
SQLITE_PRIVATE int sqlite3Fts3ExprNearTrim(Fts3Expr *pLeft, Fts3Expr *pRight, int nNear){
113402
 
  int rc;                         /* Return code */
113403
 
 
113404
 
  assert( pLeft->eType==FTSQUERY_PHRASE );
113405
 
  assert( pRight->eType==FTSQUERY_PHRASE );
113406
 
  assert( pLeft->isLoaded && pRight->isLoaded );
113407
 
 
113408
 
  if( pLeft->aDoclist==0 || pRight->aDoclist==0 ){
113409
 
    sqlite3_free(pLeft->aDoclist);
113410
 
    sqlite3_free(pRight->aDoclist);
113411
 
    pRight->aDoclist = 0;
113412
 
    pLeft->aDoclist = 0;
113413
 
    rc = SQLITE_OK;
113414
 
  }else{
113415
 
    char *aOut;                   /* Buffer in which to assemble new doclist */
113416
 
    int nOut;                     /* Size of buffer aOut in bytes */
113417
 
 
113418
 
    rc = fts3NearMerge(MERGE_POS_NEAR, nNear, 
113419
 
        pLeft->pPhrase->nToken, pLeft->aDoclist, pLeft->nDoclist,
113420
 
        pRight->pPhrase->nToken, pRight->aDoclist, pRight->nDoclist,
113421
 
        &aOut, &nOut
113422
 
    );
113423
 
    if( rc!=SQLITE_OK ) return rc;
113424
 
    sqlite3_free(pRight->aDoclist);
113425
 
    pRight->aDoclist = aOut;
113426
 
    pRight->nDoclist = nOut;
113427
 
 
113428
 
    rc = fts3NearMerge(MERGE_POS_NEAR, nNear, 
113429
 
        pRight->pPhrase->nToken, pRight->aDoclist, pRight->nDoclist,
113430
 
        pLeft->pPhrase->nToken, pLeft->aDoclist, pLeft->nDoclist,
113431
 
        &aOut, &nOut
113432
 
    );
113433
 
    sqlite3_free(pLeft->aDoclist);
113434
 
    pLeft->aDoclist = aOut;
113435
 
    pLeft->nDoclist = nOut;
113436
 
  }
113437
 
  return rc;
113438
 
}
113439
 
 
113440
 
 
113441
 
/*
113442
 
** Allocate an Fts3SegReaderArray for each token in the expression pExpr. 
113443
 
** The allocated objects are stored in the Fts3PhraseToken.pArray member
113444
 
** variables of each token structure.
113445
 
*/
113446
 
static int fts3ExprAllocateSegReaders(
113447
 
  Fts3Cursor *pCsr,               /* FTS3 table */
113448
 
  Fts3Expr *pExpr,                /* Expression to create seg-readers for */
113449
 
  int *pnExpr                     /* OUT: Number of AND'd expressions */
113450
 
){
113451
 
  int rc = SQLITE_OK;             /* Return code */
113452
 
 
113453
 
  assert( pCsr->eEvalmode==FTS3_EVAL_FILTER );
113454
 
  if( pnExpr && pExpr->eType!=FTSQUERY_AND ){
113455
 
    (*pnExpr)++;
113456
 
    pnExpr = 0;
113457
 
  }
113458
 
 
113459
 
  if( pExpr->eType==FTSQUERY_PHRASE ){
113460
 
    Fts3Phrase *pPhrase = pExpr->pPhrase;
113461
 
    int ii;
113462
 
 
113463
 
    for(ii=0; rc==SQLITE_OK && ii<pPhrase->nToken; ii++){
113464
 
      Fts3PhraseToken *pTok = &pPhrase->aToken[ii];
113465
 
      if( pTok->pSegcsr==0 ){
113466
 
        rc = fts3TermSegReaderCursor(
113467
 
            pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pSegcsr
113468
 
        );
113469
 
      }
113470
 
    }
113471
 
  }else{ 
113472
 
    rc = fts3ExprAllocateSegReaders(pCsr, pExpr->pLeft, pnExpr);
113473
 
    if( rc==SQLITE_OK ){
113474
 
      rc = fts3ExprAllocateSegReaders(pCsr, pExpr->pRight, pnExpr);
113475
 
    }
113476
 
  }
113477
 
  return rc;
113478
 
}
113479
 
 
113480
 
/*
113481
 
** Free the Fts3SegReaderArray objects associated with each token in the
113482
 
** expression pExpr. In other words, this function frees the resources
113483
 
** allocated by fts3ExprAllocateSegReaders().
113484
 
*/
113485
 
static void fts3ExprFreeSegReaders(Fts3Expr *pExpr){
113486
 
  if( pExpr ){
113487
 
    Fts3Phrase *pPhrase = pExpr->pPhrase;
113488
 
    if( pPhrase ){
113489
 
      int kk;
113490
 
      for(kk=0; kk<pPhrase->nToken; kk++){
113491
 
        fts3SegReaderCursorFree(pPhrase->aToken[kk].pSegcsr);
113492
 
        pPhrase->aToken[kk].pSegcsr = 0;
113493
 
      }
113494
 
    }
113495
 
    fts3ExprFreeSegReaders(pExpr->pLeft);
113496
 
    fts3ExprFreeSegReaders(pExpr->pRight);
113497
 
  }
113498
 
}
113499
 
 
113500
 
/*
113501
 
** Return the sum of the costs of all tokens in the expression pExpr. This
113502
 
** function must be called after Fts3SegReaderArrays have been allocated
113503
 
** for all tokens using fts3ExprAllocateSegReaders().
113504
 
*/
113505
 
static int fts3ExprCost(Fts3Expr *pExpr){
113506
 
  int nCost;                      /* Return value */
113507
 
  if( pExpr->eType==FTSQUERY_PHRASE ){
113508
 
    Fts3Phrase *pPhrase = pExpr->pPhrase;
113509
 
    int ii;
113510
 
    nCost = 0;
113511
 
    for(ii=0; ii<pPhrase->nToken; ii++){
113512
 
      Fts3SegReaderCursor *pSegcsr = pPhrase->aToken[ii].pSegcsr;
113513
 
      if( pSegcsr ) nCost += pSegcsr->nCost;
113514
 
    }
113515
 
  }else{
113516
 
    nCost = fts3ExprCost(pExpr->pLeft) + fts3ExprCost(pExpr->pRight);
113517
 
  }
113518
 
  return nCost;
113519
 
}
113520
 
 
113521
 
/*
113522
 
** The following is a helper function (and type) for fts3EvalExpr(). It
113523
 
** must be called after Fts3SegReaders have been allocated for every token
113524
 
** in the expression. See the context it is called from in fts3EvalExpr()
113525
 
** for further explanation.
113526
 
*/
113527
 
typedef struct ExprAndCost ExprAndCost;
113528
 
struct ExprAndCost {
113529
 
  Fts3Expr *pExpr;
113530
 
  int nCost;
113531
 
};
113532
 
static void fts3ExprAssignCosts(
113533
 
  Fts3Expr *pExpr,                /* Expression to create seg-readers for */
113534
 
  ExprAndCost **ppExprCost        /* OUT: Write to *ppExprCost */
113535
 
){
113536
 
  if( pExpr->eType==FTSQUERY_AND ){
113537
 
    fts3ExprAssignCosts(pExpr->pLeft, ppExprCost);
113538
 
    fts3ExprAssignCosts(pExpr->pRight, ppExprCost);
113539
 
  }else{
113540
 
    (*ppExprCost)->pExpr = pExpr;
113541
 
    (*ppExprCost)->nCost = fts3ExprCost(pExpr);
113542
 
    (*ppExprCost)++;
113543
 
  }
113544
 
}
113545
 
 
113546
 
/*
113547
 
** Evaluate the full-text expression pExpr against FTS3 table pTab. Store
113548
 
** the resulting doclist in *paOut and *pnOut. This routine mallocs for
113549
 
** the space needed to store the output. The caller is responsible for
113550
 
** freeing the space when it has finished.
113551
 
**
113552
 
** This function is called in two distinct contexts:
113553
 
**
113554
 
**   * From within the virtual table xFilter() method. In this case, the
113555
 
**     output doclist contains entries for all rows in the table, based on
113556
 
**     data read from the full-text index.
113557
 
**
113558
 
**     In this case, if the query expression contains one or more tokens that 
113559
 
**     are very common, then the returned doclist may contain a superset of 
113560
 
**     the documents that actually match the expression.
113561
 
**
113562
 
**   * From within the virtual table xNext() method. This call is only made
113563
 
**     if the call from within xFilter() found that there were very common 
113564
 
**     tokens in the query expression and did return a superset of the 
113565
 
**     matching documents. In this case the returned doclist contains only
113566
 
**     entries that correspond to the current row of the table. Instead of
113567
 
**     reading the data for each token from the full-text index, the data is
113568
 
**     already available in-memory in the Fts3PhraseToken.pDeferred structures.
113569
 
**     See fts3EvalDeferred() for how it gets there.
113570
 
**
113571
 
** In the first case above, Fts3Cursor.doDeferred==0. In the second (if it is
113572
 
** required) Fts3Cursor.doDeferred==1.
113573
 
**
113574
 
** If the SQLite invokes the snippet(), offsets() or matchinfo() function
113575
 
** as part of a SELECT on an FTS3 table, this function is called on each
113576
 
** individual phrase expression in the query. If there were very common tokens
113577
 
** found in the xFilter() call, then this function is called once for phrase
113578
 
** for each row visited, and the returned doclist contains entries for the
113579
 
** current row only. Otherwise, if there were no very common tokens, then this
113580
 
** function is called once only for each phrase in the query and the returned
113581
 
** doclist contains entries for all rows of the table.
113582
 
**
113583
 
** Fts3Cursor.doDeferred==1 when this function is called on phrases as a
113584
 
** result of a snippet(), offsets() or matchinfo() invocation.
113585
 
*/
113586
 
static int fts3EvalExpr(
113587
 
  Fts3Cursor *p,                  /* Virtual table cursor handle */
113588
 
  Fts3Expr *pExpr,                /* Parsed fts3 expression */
113589
 
  char **paOut,                   /* OUT: Pointer to malloc'd result buffer */
113590
 
  int *pnOut,                     /* OUT: Size of buffer at *paOut */
113591
 
  int isReqPos                    /* Require positions in output buffer */
113592
 
){
113593
 
  int rc = SQLITE_OK;             /* Return code */
113594
 
 
113595
 
  /* Zero the output parameters. */
113596
 
  *paOut = 0;
113597
 
  *pnOut = 0;
113598
 
 
113599
 
  if( pExpr ){
113600
 
    assert( pExpr->eType==FTSQUERY_NEAR   || pExpr->eType==FTSQUERY_OR     
113601
 
         || pExpr->eType==FTSQUERY_AND    || pExpr->eType==FTSQUERY_NOT
113602
 
         || pExpr->eType==FTSQUERY_PHRASE
113603
 
    );
113604
 
    assert( pExpr->eType==FTSQUERY_PHRASE || isReqPos==0 );
113605
 
 
113606
 
    if( pExpr->eType==FTSQUERY_PHRASE ){
113607
 
      rc = fts3PhraseSelect(p, pExpr->pPhrase,
113608
 
          isReqPos || (pExpr->pParent && pExpr->pParent->eType==FTSQUERY_NEAR),
113609
 
          paOut, pnOut
113610
 
      );
113611
 
      fts3ExprFreeSegReaders(pExpr);
113612
 
    }else if( p->eEvalmode==FTS3_EVAL_FILTER && pExpr->eType==FTSQUERY_AND ){
113613
 
      ExprAndCost *aExpr = 0;     /* Array of AND'd expressions and costs */
113614
 
      int nExpr = 0;              /* Size of aExpr[] */
113615
 
      char *aRet = 0;             /* Doclist to return to caller */
113616
 
      int nRet = 0;               /* Length of aRet[] in bytes */
113617
 
      int nDoc = 0x7FFFFFFF;
113618
 
 
113619
 
      assert( !isReqPos );
113620
 
 
113621
 
      rc = fts3ExprAllocateSegReaders(p, pExpr, &nExpr);
113622
 
      if( rc==SQLITE_OK ){
113623
 
        assert( nExpr>1 );
113624
 
        aExpr = sqlite3_malloc(sizeof(ExprAndCost) * nExpr);
113625
 
        if( !aExpr ) rc = SQLITE_NOMEM;
113626
 
      }
113627
 
      if( rc==SQLITE_OK ){
113628
 
        int ii;                   /* Used to iterate through expressions */
113629
 
 
113630
 
        fts3ExprAssignCosts(pExpr, &aExpr);
113631
 
        aExpr -= nExpr;
113632
 
        for(ii=0; ii<nExpr; ii++){
113633
 
          char *aNew;
113634
 
          int nNew;
113635
 
          int jj;
113636
 
          ExprAndCost *pBest = 0;
113637
 
  
113638
 
          for(jj=0; jj<nExpr; jj++){
113639
 
            ExprAndCost *pCand = &aExpr[jj];
113640
 
            if( pCand->pExpr && (pBest==0 || pCand->nCost<pBest->nCost) ){
113641
 
              pBest = pCand;
113642
 
            }
113643
 
          }
113644
 
  
113645
 
          if( pBest->nCost>nDoc ){
113646
 
            rc = fts3DeferExpression(p, p->pExpr);
113647
 
            break;
113648
 
          }else{
113649
 
            rc = fts3EvalExpr(p, pBest->pExpr, &aNew, &nNew, 0);
113650
 
            if( rc!=SQLITE_OK ) break;
113651
 
            pBest->pExpr = 0;
113652
 
            if( ii==0 ){
113653
 
              aRet = aNew;
113654
 
              nRet = nNew;
113655
 
              nDoc = fts3DoclistCountDocids(0, aRet, nRet);
113656
 
            }else{
113657
 
              fts3DoclistMerge(
113658
 
                  MERGE_AND, 0, 0, aRet, &nRet, aRet, nRet, aNew, nNew, &nDoc
113659
 
              );
113660
 
              sqlite3_free(aNew);
113661
 
            }
113662
 
          }
113663
 
        }
113664
 
      }
113665
 
 
113666
 
      if( rc==SQLITE_OK ){
113667
 
        *paOut = aRet;
113668
 
        *pnOut = nRet;
113669
 
      }else{
113670
 
        assert( *paOut==0 );
113671
 
        sqlite3_free(aRet);
113672
 
      }
113673
 
      sqlite3_free(aExpr);
113674
 
      fts3ExprFreeSegReaders(pExpr);
113675
 
 
113676
 
    }else{
113677
 
      char *aLeft;
113678
 
      char *aRight;
113679
 
      int nLeft;
113680
 
      int nRight;
113681
 
 
113682
 
      assert( pExpr->eType==FTSQUERY_NEAR 
113683
 
           || pExpr->eType==FTSQUERY_OR
113684
 
           || pExpr->eType==FTSQUERY_NOT
113685
 
           || (pExpr->eType==FTSQUERY_AND && p->eEvalmode==FTS3_EVAL_NEXT)
113686
 
      );
113687
 
 
113688
 
      if( 0==(rc = fts3EvalExpr(p, pExpr->pRight, &aRight, &nRight, isReqPos))
113689
 
       && 0==(rc = fts3EvalExpr(p, pExpr->pLeft, &aLeft, &nLeft, isReqPos))
113690
 
      ){
113691
 
        switch( pExpr->eType ){
113692
 
          case FTSQUERY_NEAR: {
113693
 
            Fts3Expr *pLeft;
113694
 
            Fts3Expr *pRight;
113695
 
            int mergetype = MERGE_NEAR;
113696
 
            if( pExpr->pParent && pExpr->pParent->eType==FTSQUERY_NEAR ){
113697
 
              mergetype = MERGE_POS_NEAR;
113698
 
            }
113699
 
            pLeft = pExpr->pLeft;
113700
 
            while( pLeft->eType==FTSQUERY_NEAR ){ 
113701
 
              pLeft=pLeft->pRight;
113702
 
            }
113703
 
            pRight = pExpr->pRight;
113704
 
            assert( pRight->eType==FTSQUERY_PHRASE );
113705
 
            assert( pLeft->eType==FTSQUERY_PHRASE );
113706
 
 
113707
 
            rc = fts3NearMerge(mergetype, pExpr->nNear, 
113708
 
                pLeft->pPhrase->nToken, aLeft, nLeft,
113709
 
                pRight->pPhrase->nToken, aRight, nRight,
113710
 
                paOut, pnOut
113711
 
            );
113712
 
            sqlite3_free(aLeft);
113713
 
            break;
113714
 
          }
113715
 
 
113716
 
          case FTSQUERY_OR: {
113717
 
            /* Allocate a buffer for the output. The maximum size is the
113718
 
            ** sum of the sizes of the two input buffers. The +1 term is
113719
 
            ** so that a buffer of zero bytes is never allocated - this can
113720
 
            ** cause fts3DoclistMerge() to incorrectly return SQLITE_NOMEM.
113721
 
            */
113722
 
            char *aBuffer = sqlite3_malloc(nRight+nLeft+1);
113723
 
            rc = fts3DoclistMerge(MERGE_OR, 0, 0, aBuffer, pnOut,
113724
 
                aLeft, nLeft, aRight, nRight, 0
113725
 
            );
113726
 
            *paOut = aBuffer;
113727
 
            sqlite3_free(aLeft);
113728
 
            break;
113729
 
          }
113730
 
 
113731
 
          default: {
113732
 
            assert( FTSQUERY_NOT==MERGE_NOT && FTSQUERY_AND==MERGE_AND );
113733
 
            fts3DoclistMerge(pExpr->eType, 0, 0, aLeft, pnOut,
113734
 
                aLeft, nLeft, aRight, nRight, 0
113735
 
            );
113736
 
            *paOut = aLeft;
113737
 
            break;
113738
 
          }
113739
 
        }
113740
 
      }
113741
 
      sqlite3_free(aRight);
113742
 
    }
113743
 
  }
113744
 
 
113745
 
  assert( rc==SQLITE_OK || *paOut==0 );
113746
 
  return rc;
113747
 
}
113748
 
 
113749
 
/*
113750
 
** This function is called from within xNext() for each row visited by
113751
 
** an FTS3 query. If evaluating the FTS3 query expression within xFilter()
113752
 
** was able to determine the exact set of matching rows, this function sets
113753
 
** *pbRes to true and returns SQLITE_IO immediately.
113754
 
**
113755
 
** Otherwise, if evaluating the query expression within xFilter() returned a
113756
 
** superset of the matching documents instead of an exact set (this happens
113757
 
** when the query includes very common tokens and it is deemed too expensive to
113758
 
** load their doclists from disk), this function tests if the current row
113759
 
** really does match the FTS3 query.
113760
 
**
113761
 
** If an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK
113762
 
** is returned and *pbRes is set to true if the current row matches the
113763
 
** FTS3 query (and should be included in the results returned to SQLite), or
113764
 
** false otherwise.
113765
 
*/
113766
 
static int fts3EvalDeferred(
113767
 
  Fts3Cursor *pCsr,               /* FTS3 cursor pointing at row to test */
113768
 
  int *pbRes                      /* OUT: Set to true if row is a match */
113769
 
){
113770
 
  int rc = SQLITE_OK;
113771
 
  if( pCsr->pDeferred==0 ){
113772
 
    *pbRes = 1;
113773
 
  }else{
113774
 
    rc = fts3CursorSeek(0, pCsr);
113775
 
    if( rc==SQLITE_OK ){
113776
 
      sqlite3Fts3FreeDeferredDoclists(pCsr);
113777
 
      rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
113778
 
    }
113779
 
    if( rc==SQLITE_OK ){
113780
 
      char *a = 0;
113781
 
      int n = 0;
113782
 
      rc = fts3EvalExpr(pCsr, pCsr->pExpr, &a, &n, 0);
113783
 
      assert( n>=0 );
113784
 
      *pbRes = (n>0);
113785
 
      sqlite3_free(a);
113786
 
    }
113787
 
  }
113788
 
  return rc;
113789
 
}
113790
 
 
113791
 
/*
113792
 
** Advance the cursor to the next row in the %_content table that
113793
 
** matches the search criteria.  For a MATCH search, this will be
113794
 
** the next row that matches. For a full-table scan, this will be
113795
 
** simply the next row in the %_content table.  For a docid lookup,
113796
 
** this routine simply sets the EOF flag.
113797
 
**
113798
 
** Return SQLITE_OK if nothing goes wrong.  SQLITE_OK is returned
113799
 
** even if we reach end-of-file.  The fts3EofMethod() will be called
113800
 
** subsequently to determine whether or not an EOF was hit.
113801
 
*/
113802
 
static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
113803
 
  int res;
113804
 
  int rc = SQLITE_OK;             /* Return code */
113805
 
  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
113806
 
 
113807
 
  pCsr->eEvalmode = FTS3_EVAL_NEXT;
113808
 
  do {
113809
 
    if( pCsr->aDoclist==0 ){
113810
 
      if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
113811
 
        pCsr->isEof = 1;
113812
 
        rc = sqlite3_reset(pCsr->pStmt);
113813
 
        break;
113814
 
      }
113815
 
      pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
113816
 
    }else{
113817
 
      if( pCsr->pNextId>=&pCsr->aDoclist[pCsr->nDoclist] ){
113818
 
        pCsr->isEof = 1;
113819
 
        break;
113820
 
      }
113821
 
      sqlite3_reset(pCsr->pStmt);
113822
 
      fts3GetDeltaVarint(&pCsr->pNextId, &pCsr->iPrevId);
113823
 
      pCsr->isRequireSeek = 1;
113824
 
      pCsr->isMatchinfoNeeded = 1;
113825
 
    }
113826
 
  }while( SQLITE_OK==(rc = fts3EvalDeferred(pCsr, &res)) && res==0 );
113827
 
 
113828
 
  return rc;
113829
 
}
113830
 
 
113831
 
/*
113832
 
** This is the xFilter interface for the virtual table.  See
113833
 
** the virtual table xFilter method documentation for additional
113834
 
** information.
113835
 
**
113836
 
** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
113837
 
** the %_content table.
113838
 
**
113839
 
** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
113840
 
** in the %_content table.
113841
 
**
113842
 
** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index.  The
113843
 
** column on the left-hand side of the MATCH operator is column
113844
 
** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed.  argv[0] is the right-hand
113845
 
** side of the MATCH operator.
113846
 
*/
113847
 
static int fts3FilterMethod(
113848
 
  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
113849
 
  int idxNum,                     /* Strategy index */
113850
 
  const char *idxStr,             /* Unused */
113851
 
  int nVal,                       /* Number of elements in apVal */
113852
 
  sqlite3_value **apVal           /* Arguments for the indexing scheme */
113853
 
){
113854
 
  const char *azSql[] = {
113855
 
    "SELECT %s FROM %Q.'%q_content' AS x WHERE docid = ?", /* non-full-scan */
113856
 
    "SELECT %s FROM %Q.'%q_content' AS x ",                /* full-scan */
113857
 
  };
113858
 
  int rc;                         /* Return code */
113859
 
  char *zSql;                     /* SQL statement used to access %_content */
113860
 
  Fts3Table *p = (Fts3Table *)pCursor->pVtab;
113861
 
  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
113862
 
 
113863
 
  UNUSED_PARAMETER(idxStr);
113864
 
  UNUSED_PARAMETER(nVal);
113865
 
 
113866
 
  assert( idxNum>=0 && idxNum<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
113867
 
  assert( nVal==0 || nVal==1 );
113868
 
  assert( (nVal==0)==(idxNum==FTS3_FULLSCAN_SEARCH) );
113869
 
  assert( p->pSegments==0 );
113870
 
 
113871
 
  /* In case the cursor has been used before, clear it now. */
113872
 
  sqlite3_finalize(pCsr->pStmt);
113873
 
  sqlite3_free(pCsr->aDoclist);
113874
 
  sqlite3Fts3ExprFree(pCsr->pExpr);
113875
 
  memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
113876
 
 
113877
 
  if( idxNum!=FTS3_DOCID_SEARCH && idxNum!=FTS3_FULLSCAN_SEARCH ){
113878
 
    int iCol = idxNum-FTS3_FULLTEXT_SEARCH;
113879
 
    const char *zQuery = (const char *)sqlite3_value_text(apVal[0]);
113880
 
 
113881
 
    if( zQuery==0 && sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
113882
 
      return SQLITE_NOMEM;
113883
 
    }
113884
 
 
113885
 
    rc = sqlite3Fts3ExprParse(p->pTokenizer, p->azColumn, p->nColumn, 
113886
 
        iCol, zQuery, -1, &pCsr->pExpr
113887
 
    );
113888
 
    if( rc!=SQLITE_OK ){
113889
 
      if( rc==SQLITE_ERROR ){
113890
 
        p->base.zErrMsg = sqlite3_mprintf("malformed MATCH expression: [%s]",
113891
 
                                          zQuery);
113892
 
      }
113893
 
      return rc;
113894
 
    }
113895
 
 
113896
 
    rc = sqlite3Fts3ReadLock(p);
113897
 
    if( rc!=SQLITE_OK ) return rc;
113898
 
 
113899
 
    rc = fts3EvalExpr(pCsr, pCsr->pExpr, &pCsr->aDoclist, &pCsr->nDoclist, 0);
113900
 
    sqlite3Fts3SegmentsClose(p);
113901
 
    if( rc!=SQLITE_OK ) return rc;
113902
 
    pCsr->pNextId = pCsr->aDoclist;
113903
 
    pCsr->iPrevId = 0;
113904
 
  }
113905
 
 
113906
 
  /* Compile a SELECT statement for this cursor. For a full-table-scan, the
113907
 
  ** statement loops through all rows of the %_content table. For a
113908
 
  ** full-text query or docid lookup, the statement retrieves a single
113909
 
  ** row by docid.
113910
 
  */
113911
 
  zSql = (char *)azSql[idxNum==FTS3_FULLSCAN_SEARCH];
113912
 
  zSql = sqlite3_mprintf(zSql, p->zReadExprlist, p->zDb, p->zName);
113913
 
  if( !zSql ){
113914
 
    rc = SQLITE_NOMEM;
113915
 
  }else{
113916
 
    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
113917
 
    sqlite3_free(zSql);
113918
 
  }
113919
 
  if( rc==SQLITE_OK && idxNum==FTS3_DOCID_SEARCH ){
113920
 
    rc = sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
113921
 
  }
113922
 
  pCsr->eSearch = (i16)idxNum;
113923
 
 
113924
 
  if( rc!=SQLITE_OK ) return rc;
113925
 
  return fts3NextMethod(pCursor);
113926
 
}
113927
 
 
113928
 
/* 
113929
 
** This is the xEof method of the virtual table. SQLite calls this 
113930
 
** routine to find out if it has reached the end of a result set.
113931
 
*/
113932
 
static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
113933
 
  return ((Fts3Cursor *)pCursor)->isEof;
113934
 
}
113935
 
 
113936
 
/* 
113937
 
** This is the xRowid method. The SQLite core calls this routine to
113938
 
** retrieve the rowid for the current row of the result set. fts3
113939
 
** exposes %_content.docid as the rowid for the virtual table. The
113940
 
** rowid should be written to *pRowid.
113941
 
*/
113942
 
static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
113943
 
  Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
113944
 
  if( pCsr->aDoclist ){
113945
 
    *pRowid = pCsr->iPrevId;
113946
 
  }else{
113947
 
    /* This branch runs if the query is implemented using a full-table scan
113948
 
    ** (not using the full-text index). In this case grab the rowid from the
113949
 
    ** SELECT statement.
113950
 
    */
113951
 
    assert( pCsr->isRequireSeek==0 );
113952
 
    *pRowid = sqlite3_column_int64(pCsr->pStmt, 0);
113953
 
  }
113954
 
  return SQLITE_OK;
113955
 
}
113956
 
 
113957
 
/* 
113958
 
** This is the xColumn method, called by SQLite to request a value from
113959
 
** the row that the supplied cursor currently points to.
113960
 
*/
113961
 
static int fts3ColumnMethod(
113962
 
  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
113963
 
  sqlite3_context *pContext,      /* Context for sqlite3_result_xxx() calls */
113964
 
  int iCol                        /* Index of column to read value from */
113965
 
){
113966
 
  int rc;                         /* Return Code */
113967
 
  Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
113968
 
  Fts3Table *p = (Fts3Table *)pCursor->pVtab;
113969
 
 
113970
 
  /* The column value supplied by SQLite must be in range. */
113971
 
  assert( iCol>=0 && iCol<=p->nColumn+1 );
113972
 
 
113973
 
  if( iCol==p->nColumn+1 ){
113974
 
    /* This call is a request for the "docid" column. Since "docid" is an 
113975
 
    ** alias for "rowid", use the xRowid() method to obtain the value.
113976
 
    */
113977
 
    sqlite3_int64 iRowid;
113978
 
    rc = fts3RowidMethod(pCursor, &iRowid);
113979
 
    sqlite3_result_int64(pContext, iRowid);
113980
 
  }else if( iCol==p->nColumn ){
113981
 
    /* The extra column whose name is the same as the table.
113982
 
    ** Return a blob which is a pointer to the cursor.
113983
 
    */
113984
 
    sqlite3_result_blob(pContext, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT);
113985
 
    rc = SQLITE_OK;
113986
 
  }else{
113987
 
    rc = fts3CursorSeek(0, pCsr);
113988
 
    if( rc==SQLITE_OK ){
113989
 
      sqlite3_result_value(pContext, sqlite3_column_value(pCsr->pStmt, iCol+1));
113990
 
    }
113991
 
  }
113992
 
  return rc;
113993
 
}
113994
 
 
113995
 
/* 
113996
 
** This function is the implementation of the xUpdate callback used by 
113997
 
** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
113998
 
** inserted, updated or deleted.
113999
 
*/
114000
 
static int fts3UpdateMethod(
114001
 
  sqlite3_vtab *pVtab,            /* Virtual table handle */
114002
 
  int nArg,                       /* Size of argument array */
114003
 
  sqlite3_value **apVal,          /* Array of arguments */
114004
 
  sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
114005
 
){
114006
 
  return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
114007
 
}
114008
 
 
114009
 
/*
114010
 
** Implementation of xSync() method. Flush the contents of the pending-terms
114011
 
** hash-table to the database.
114012
 
*/
114013
 
static int fts3SyncMethod(sqlite3_vtab *pVtab){
114014
 
  int rc = sqlite3Fts3PendingTermsFlush((Fts3Table *)pVtab);
114015
 
  sqlite3Fts3SegmentsClose((Fts3Table *)pVtab);
114016
 
  return rc;
114017
 
}
114018
 
 
114019
 
/*
114020
 
** Implementation of xBegin() method. This is a no-op.
114021
 
*/
114022
 
static int fts3BeginMethod(sqlite3_vtab *pVtab){
114023
 
  UNUSED_PARAMETER(pVtab);
114024
 
  assert( ((Fts3Table *)pVtab)->nPendingData==0 );
114025
 
  return SQLITE_OK;
114026
 
}
114027
 
 
114028
 
/*
114029
 
** Implementation of xCommit() method. This is a no-op. The contents of
114030
 
** the pending-terms hash-table have already been flushed into the database
114031
 
** by fts3SyncMethod().
114032
 
*/
114033
 
static int fts3CommitMethod(sqlite3_vtab *pVtab){
114034
 
  UNUSED_PARAMETER(pVtab);
114035
 
  assert( ((Fts3Table *)pVtab)->nPendingData==0 );
114036
 
  return SQLITE_OK;
114037
 
}
114038
 
 
114039
 
/*
114040
 
** Implementation of xRollback(). Discard the contents of the pending-terms
114041
 
** hash-table. Any changes made to the database are reverted by SQLite.
114042
 
*/
114043
 
static int fts3RollbackMethod(sqlite3_vtab *pVtab){
114044
 
  sqlite3Fts3PendingTermsClear((Fts3Table *)pVtab);
114045
 
  return SQLITE_OK;
114046
 
}
114047
 
 
114048
 
/*
114049
 
** Load the doclist associated with expression pExpr to pExpr->aDoclist.
114050
 
** The loaded doclist contains positions as well as the document ids.
114051
 
** This is used by the matchinfo(), snippet() and offsets() auxillary
114052
 
** functions.
114053
 
*/
114054
 
SQLITE_PRIVATE int sqlite3Fts3ExprLoadDoclist(Fts3Cursor *pCsr, Fts3Expr *pExpr){
114055
 
  int rc;
114056
 
  assert( pExpr->eType==FTSQUERY_PHRASE && pExpr->pPhrase );
114057
 
  assert( pCsr->eEvalmode==FTS3_EVAL_NEXT );
114058
 
  rc = fts3EvalExpr(pCsr, pExpr, &pExpr->aDoclist, &pExpr->nDoclist, 1);
114059
 
  return rc;
114060
 
}
114061
 
 
114062
 
SQLITE_PRIVATE int sqlite3Fts3ExprLoadFtDoclist(
114063
 
  Fts3Cursor *pCsr, 
114064
 
  Fts3Expr *pExpr,
114065
 
  char **paDoclist,
114066
 
  int *pnDoclist
114067
 
){
114068
 
  int rc;
114069
 
  assert( pCsr->eEvalmode==FTS3_EVAL_NEXT );
114070
 
  assert( pExpr->eType==FTSQUERY_PHRASE && pExpr->pPhrase );
114071
 
  pCsr->eEvalmode = FTS3_EVAL_MATCHINFO;
114072
 
  rc = fts3EvalExpr(pCsr, pExpr, paDoclist, pnDoclist, 1);
114073
 
  pCsr->eEvalmode = FTS3_EVAL_NEXT;
114074
 
  return rc;
114075
 
}
114076
 
 
114077
 
/*
114078
 
** After ExprLoadDoclist() (see above) has been called, this function is
114079
 
** used to iterate/search through the position lists that make up the doclist
114080
 
** stored in pExpr->aDoclist.
114081
 
*/
114082
 
SQLITE_PRIVATE char *sqlite3Fts3FindPositions(
114083
 
  Fts3Expr *pExpr,                /* Access this expressions doclist */
114084
 
  sqlite3_int64 iDocid,           /* Docid associated with requested pos-list */
114085
 
  int iCol                        /* Column of requested pos-list */
114086
 
){
114087
 
  assert( pExpr->isLoaded );
114088
 
  if( pExpr->aDoclist ){
114089
 
    char *pEnd = &pExpr->aDoclist[pExpr->nDoclist];
114090
 
    char *pCsr;
114091
 
 
114092
 
    if( pExpr->pCurrent==0 ){
114093
 
      pExpr->pCurrent = pExpr->aDoclist;
114094
 
      pExpr->iCurrent = 0;
114095
 
      pExpr->pCurrent += sqlite3Fts3GetVarint(pExpr->pCurrent,&pExpr->iCurrent);
114096
 
    }
114097
 
    pCsr = pExpr->pCurrent;
114098
 
    assert( pCsr );
114099
 
 
114100
 
    while( pCsr<pEnd ){
114101
 
      if( pExpr->iCurrent<iDocid ){
114102
 
        fts3PoslistCopy(0, &pCsr);
114103
 
        if( pCsr<pEnd ){
114104
 
          fts3GetDeltaVarint(&pCsr, &pExpr->iCurrent);
114105
 
        }
114106
 
        pExpr->pCurrent = pCsr;
114107
 
      }else{
114108
 
        if( pExpr->iCurrent==iDocid ){
114109
 
          int iThis = 0;
114110
 
          if( iCol<0 ){
114111
 
            /* If iCol is negative, return a pointer to the start of the
114112
 
            ** position-list (instead of a pointer to the start of a list
114113
 
            ** of offsets associated with a specific column).
114114
 
            */
114115
 
            return pCsr;
114116
 
          }
114117
 
          while( iThis<iCol ){
114118
 
            fts3ColumnlistCopy(0, &pCsr);
114119
 
            if( *pCsr==0x00 ) return 0;
114120
 
            pCsr++;
114121
 
            pCsr += sqlite3Fts3GetVarint32(pCsr, &iThis);
114122
 
          }
114123
 
          if( iCol==iThis && (*pCsr&0xFE) ) return pCsr;
114124
 
        }
114125
 
        return 0;
114126
 
      }
114127
 
    }
114128
 
  }
114129
 
 
114130
 
  return 0;
114131
 
}
114132
 
 
114133
 
/*
114134
 
** Helper function used by the implementation of the overloaded snippet(),
114135
 
** offsets() and optimize() SQL functions.
114136
 
**
114137
 
** If the value passed as the third argument is a blob of size
114138
 
** sizeof(Fts3Cursor*), then the blob contents are copied to the 
114139
 
** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
114140
 
** message is written to context pContext and SQLITE_ERROR returned. The
114141
 
** string passed via zFunc is used as part of the error message.
114142
 
*/
114143
 
static int fts3FunctionArg(
114144
 
  sqlite3_context *pContext,      /* SQL function call context */
114145
 
  const char *zFunc,              /* Function name */
114146
 
  sqlite3_value *pVal,            /* argv[0] passed to function */
114147
 
  Fts3Cursor **ppCsr              /* OUT: Store cursor handle here */
114148
 
){
114149
 
  Fts3Cursor *pRet;
114150
 
  if( sqlite3_value_type(pVal)!=SQLITE_BLOB 
114151
 
   || sqlite3_value_bytes(pVal)!=sizeof(Fts3Cursor *)
114152
 
  ){
114153
 
    char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
114154
 
    sqlite3_result_error(pContext, zErr, -1);
114155
 
    sqlite3_free(zErr);
114156
 
    return SQLITE_ERROR;
114157
 
  }
114158
 
  memcpy(&pRet, sqlite3_value_blob(pVal), sizeof(Fts3Cursor *));
114159
 
  *ppCsr = pRet;
114160
 
  return SQLITE_OK;
114161
 
}
114162
 
 
114163
 
/*
114164
 
** Implementation of the snippet() function for FTS3
114165
 
*/
114166
 
static void fts3SnippetFunc(
114167
 
  sqlite3_context *pContext,      /* SQLite function call context */
114168
 
  int nVal,                       /* Size of apVal[] array */
114169
 
  sqlite3_value **apVal           /* Array of arguments */
114170
 
){
114171
 
  Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
114172
 
  const char *zStart = "<b>";
114173
 
  const char *zEnd = "</b>";
114174
 
  const char *zEllipsis = "<b>...</b>";
114175
 
  int iCol = -1;
114176
 
  int nToken = 15;                /* Default number of tokens in snippet */
114177
 
 
114178
 
  /* There must be at least one argument passed to this function (otherwise
114179
 
  ** the non-overloaded version would have been called instead of this one).
114180
 
  */
114181
 
  assert( nVal>=1 );
114182
 
 
114183
 
  if( nVal>6 ){
114184
 
    sqlite3_result_error(pContext, 
114185
 
        "wrong number of arguments to function snippet()", -1);
114186
 
    return;
114187
 
  }
114188
 
  if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
114189
 
 
114190
 
  switch( nVal ){
114191
 
    case 6: nToken = sqlite3_value_int(apVal[5]);
114192
 
    case 5: iCol = sqlite3_value_int(apVal[4]);
114193
 
    case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
114194
 
    case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
114195
 
    case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
114196
 
  }
114197
 
  if( !zEllipsis || !zEnd || !zStart ){
114198
 
    sqlite3_result_error_nomem(pContext);
114199
 
  }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
114200
 
    sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
114201
 
  }
114202
 
}
114203
 
 
114204
 
/*
114205
 
** Implementation of the offsets() function for FTS3
114206
 
*/
114207
 
static void fts3OffsetsFunc(
114208
 
  sqlite3_context *pContext,      /* SQLite function call context */
114209
 
  int nVal,                       /* Size of argument array */
114210
 
  sqlite3_value **apVal           /* Array of arguments */
114211
 
){
114212
 
  Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
114213
 
 
114214
 
  UNUSED_PARAMETER(nVal);
114215
 
 
114216
 
  assert( nVal==1 );
114217
 
  if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
114218
 
  assert( pCsr );
114219
 
  if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
114220
 
    sqlite3Fts3Offsets(pContext, pCsr);
114221
 
  }
114222
 
}
114223
 
 
114224
 
/* 
114225
 
** Implementation of the special optimize() function for FTS3. This 
114226
 
** function merges all segments in the database to a single segment.
114227
 
** Example usage is:
114228
 
**
114229
 
**   SELECT optimize(t) FROM t LIMIT 1;
114230
 
**
114231
 
** where 't' is the name of an FTS3 table.
114232
 
*/
114233
 
static void fts3OptimizeFunc(
114234
 
  sqlite3_context *pContext,      /* SQLite function call context */
114235
 
  int nVal,                       /* Size of argument array */
114236
 
  sqlite3_value **apVal           /* Array of arguments */
114237
 
){
114238
 
  int rc;                         /* Return code */
114239
 
  Fts3Table *p;                   /* Virtual table handle */
114240
 
  Fts3Cursor *pCursor;            /* Cursor handle passed through apVal[0] */
114241
 
 
114242
 
  UNUSED_PARAMETER(nVal);
114243
 
 
114244
 
  assert( nVal==1 );
114245
 
  if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
114246
 
  p = (Fts3Table *)pCursor->base.pVtab;
114247
 
  assert( p );
114248
 
 
114249
 
  rc = sqlite3Fts3Optimize(p);
114250
 
 
114251
 
  switch( rc ){
114252
 
    case SQLITE_OK:
114253
 
      sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
114254
 
      break;
114255
 
    case SQLITE_DONE:
114256
 
      sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
114257
 
      break;
114258
 
    default:
114259
 
      sqlite3_result_error_code(pContext, rc);
114260
 
      break;
114261
 
  }
114262
 
}
114263
 
 
114264
 
/*
114265
 
** Implementation of the matchinfo() function for FTS3
114266
 
*/
114267
 
static void fts3MatchinfoFunc(
114268
 
  sqlite3_context *pContext,      /* SQLite function call context */
114269
 
  int nVal,                       /* Size of argument array */
114270
 
  sqlite3_value **apVal           /* Array of arguments */
114271
 
){
114272
 
  Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
114273
 
  assert( nVal==1 || nVal==2 );
114274
 
  if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
114275
 
    const char *zArg = 0;
114276
 
    if( nVal>1 ){
114277
 
      zArg = (const char *)sqlite3_value_text(apVal[1]);
114278
 
    }
114279
 
    sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
114280
 
  }
114281
 
}
114282
 
 
114283
 
/*
114284
 
** This routine implements the xFindFunction method for the FTS3
114285
 
** virtual table.
114286
 
*/
114287
 
static int fts3FindFunctionMethod(
114288
 
  sqlite3_vtab *pVtab,            /* Virtual table handle */
114289
 
  int nArg,                       /* Number of SQL function arguments */
114290
 
  const char *zName,              /* Name of SQL function */
114291
 
  void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
114292
 
  void **ppArg                    /* Unused */
114293
 
){
114294
 
  struct Overloaded {
114295
 
    const char *zName;
114296
 
    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
114297
 
  } aOverload[] = {
114298
 
    { "snippet", fts3SnippetFunc },
114299
 
    { "offsets", fts3OffsetsFunc },
114300
 
    { "optimize", fts3OptimizeFunc },
114301
 
    { "matchinfo", fts3MatchinfoFunc },
114302
 
  };
114303
 
  int i;                          /* Iterator variable */
114304
 
 
114305
 
  UNUSED_PARAMETER(pVtab);
114306
 
  UNUSED_PARAMETER(nArg);
114307
 
  UNUSED_PARAMETER(ppArg);
114308
 
 
114309
 
  for(i=0; i<SizeofArray(aOverload); i++){
114310
 
    if( strcmp(zName, aOverload[i].zName)==0 ){
114311
 
      *pxFunc = aOverload[i].xFunc;
114312
 
      return 1;
114313
 
    }
114314
 
  }
114315
 
 
114316
 
  /* No function of the specified name was found. Return 0. */
114317
 
  return 0;
114318
 
}
114319
 
 
114320
 
/*
114321
 
** Implementation of FTS3 xRename method. Rename an fts3 table.
114322
 
*/
114323
 
static int fts3RenameMethod(
114324
 
  sqlite3_vtab *pVtab,            /* Virtual table handle */
114325
 
  const char *zName               /* New name of table */
114326
 
){
114327
 
  Fts3Table *p = (Fts3Table *)pVtab;
114328
 
  sqlite3 *db = p->db;            /* Database connection */
114329
 
  int rc;                         /* Return Code */
114330
 
 
114331
 
  rc = sqlite3Fts3PendingTermsFlush(p);
114332
 
  if( rc!=SQLITE_OK ){
114333
 
    return rc;
114334
 
  }
114335
 
 
114336
 
  fts3DbExec(&rc, db,
114337
 
    "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';",
114338
 
    p->zDb, p->zName, zName
114339
 
  );
114340
 
  if( p->bHasDocsize ){
114341
 
    fts3DbExec(&rc, db,
114342
 
      "ALTER TABLE %Q.'%q_docsize'  RENAME TO '%q_docsize';",
114343
 
      p->zDb, p->zName, zName
114344
 
    );
114345
 
  }
114346
 
  if( p->bHasStat ){
114347
 
    fts3DbExec(&rc, db,
114348
 
      "ALTER TABLE %Q.'%q_stat'  RENAME TO '%q_stat';",
114349
 
      p->zDb, p->zName, zName
114350
 
    );
114351
 
  }
114352
 
  fts3DbExec(&rc, db,
114353
 
    "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
114354
 
    p->zDb, p->zName, zName
114355
 
  );
114356
 
  fts3DbExec(&rc, db,
114357
 
    "ALTER TABLE %Q.'%q_segdir'   RENAME TO '%q_segdir';",
114358
 
    p->zDb, p->zName, zName
114359
 
  );
114360
 
  return rc;
114361
 
}
114362
 
 
114363
 
static const sqlite3_module fts3Module = {
114364
 
  /* iVersion      */ 0,
114365
 
  /* xCreate       */ fts3CreateMethod,
114366
 
  /* xConnect      */ fts3ConnectMethod,
114367
 
  /* xBestIndex    */ fts3BestIndexMethod,
114368
 
  /* xDisconnect   */ fts3DisconnectMethod,
114369
 
  /* xDestroy      */ fts3DestroyMethod,
114370
 
  /* xOpen         */ fts3OpenMethod,
114371
 
  /* xClose        */ fts3CloseMethod,
114372
 
  /* xFilter       */ fts3FilterMethod,
114373
 
  /* xNext         */ fts3NextMethod,
114374
 
  /* xEof          */ fts3EofMethod,
114375
 
  /* xColumn       */ fts3ColumnMethod,
114376
 
  /* xRowid        */ fts3RowidMethod,
114377
 
  /* xUpdate       */ fts3UpdateMethod,
114378
 
  /* xBegin        */ fts3BeginMethod,
114379
 
  /* xSync         */ fts3SyncMethod,
114380
 
  /* xCommit       */ fts3CommitMethod,
114381
 
  /* xRollback     */ fts3RollbackMethod,
114382
 
  /* xFindFunction */ fts3FindFunctionMethod,
114383
 
  /* xRename */       fts3RenameMethod,
114384
 
};
114385
 
 
114386
 
/*
114387
 
** This function is registered as the module destructor (called when an
114388
 
** FTS3 enabled database connection is closed). It frees the memory
114389
 
** allocated for the tokenizer hash table.
114390
 
*/
114391
 
static void hashDestroy(void *p){
114392
 
  Fts3Hash *pHash = (Fts3Hash *)p;
114393
 
  sqlite3Fts3HashClear(pHash);
114394
 
  sqlite3_free(pHash);
114395
 
}
114396
 
 
114397
 
/*
114398
 
** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are 
114399
 
** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
114400
 
** respectively. The following three forward declarations are for functions
114401
 
** declared in these files used to retrieve the respective implementations.
114402
 
**
114403
 
** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
114404
 
** to by the argument to point to the "simple" tokenizer implementation.
114405
 
** And so on.
114406
 
*/
114407
 
SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
114408
 
SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
114409
 
#ifdef SQLITE_ENABLE_ICU
114410
 
SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
114411
 
#endif
114412
 
 
114413
 
/*
114414
 
** Initialise the fts3 extension. If this extension is built as part
114415
 
** of the sqlite library, then this function is called directly by
114416
 
** SQLite. If fts3 is built as a dynamically loadable extension, this
114417
 
** function is called by the sqlite3_extension_init() entry point.
114418
 
*/
114419
 
SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
114420
 
  int rc = SQLITE_OK;
114421
 
  Fts3Hash *pHash = 0;
114422
 
  const sqlite3_tokenizer_module *pSimple = 0;
114423
 
  const sqlite3_tokenizer_module *pPorter = 0;
114424
 
 
114425
 
#ifdef SQLITE_ENABLE_ICU
114426
 
  const sqlite3_tokenizer_module *pIcu = 0;
114427
 
  sqlite3Fts3IcuTokenizerModule(&pIcu);
114428
 
#endif
114429
 
 
114430
 
  rc = sqlite3Fts3InitAux(db);
114431
 
  if( rc!=SQLITE_OK ) return rc;
114432
 
 
114433
 
  sqlite3Fts3SimpleTokenizerModule(&pSimple);
114434
 
  sqlite3Fts3PorterTokenizerModule(&pPorter);
114435
 
 
114436
 
  /* Allocate and initialise the hash-table used to store tokenizers. */
114437
 
  pHash = sqlite3_malloc(sizeof(Fts3Hash));
114438
 
  if( !pHash ){
114439
 
    rc = SQLITE_NOMEM;
114440
 
  }else{
114441
 
    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
114442
 
  }
114443
 
 
114444
 
  /* Load the built-in tokenizers into the hash table */
114445
 
  if( rc==SQLITE_OK ){
114446
 
    if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
114447
 
     || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter) 
114448
 
#ifdef SQLITE_ENABLE_ICU
114449
 
     || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
114450
 
#endif
114451
 
    ){
114452
 
      rc = SQLITE_NOMEM;
114453
 
    }
114454
 
  }
114455
 
 
114456
 
#ifdef SQLITE_TEST
114457
 
  if( rc==SQLITE_OK ){
114458
 
    rc = sqlite3Fts3ExprInitTestInterface(db);
114459
 
  }
114460
 
#endif
114461
 
 
114462
 
  /* Create the virtual table wrapper around the hash-table and overload 
114463
 
  ** the two scalar functions. If this is successful, register the
114464
 
  ** module with sqlite.
114465
 
  */
114466
 
  if( SQLITE_OK==rc 
114467
 
   && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
114468
 
   && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
114469
 
   && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
114470
 
   && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
114471
 
   && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
114472
 
   && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
114473
 
  ){
114474
 
    rc = sqlite3_create_module_v2(
114475
 
        db, "fts3", &fts3Module, (void *)pHash, hashDestroy
114476
 
    );
114477
 
    if( rc==SQLITE_OK ){
114478
 
      rc = sqlite3_create_module_v2(
114479
 
          db, "fts4", &fts3Module, (void *)pHash, 0
114480
 
      );
114481
 
    }
114482
 
    return rc;
114483
 
  }
114484
 
 
114485
 
  /* An error has occurred. Delete the hash table and return the error code. */
114486
 
  assert( rc!=SQLITE_OK );
114487
 
  if( pHash ){
114488
 
    sqlite3Fts3HashClear(pHash);
114489
 
    sqlite3_free(pHash);
114490
 
  }
114491
 
  return rc;
114492
 
}
114493
 
 
114494
 
#if !SQLITE_CORE
114495
 
SQLITE_API int sqlite3_extension_init(
114496
 
  sqlite3 *db, 
114497
 
  char **pzErrMsg,
114498
 
  const sqlite3_api_routines *pApi
114499
 
){
114500
 
  SQLITE_EXTENSION_INIT2(pApi)
114501
 
  return sqlite3Fts3Init(db);
114502
 
}
114503
 
#endif
114504
 
 
114505
 
#endif
114506
 
 
114507
 
/************** End of fts3.c ************************************************/
114508
 
/************** Begin file fts3_aux.c ****************************************/
114509
 
/*
114510
 
** 2011 Jan 27
114511
 
**
114512
 
** The author disclaims copyright to this source code.  In place of
114513
 
** a legal notice, here is a blessing:
114514
 
**
114515
 
**    May you do good and not evil.
114516
 
**    May you find forgiveness for yourself and forgive others.
114517
 
**    May you share freely, never taking more than you give.
114518
 
**
114519
 
******************************************************************************
114520
 
**
114521
 
*/
114522
 
 
114523
 
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
114524
 
 
114525
 
 
114526
 
typedef struct Fts3auxTable Fts3auxTable;
114527
 
typedef struct Fts3auxCursor Fts3auxCursor;
114528
 
 
114529
 
struct Fts3auxTable {
114530
 
  sqlite3_vtab base;              /* Base class used by SQLite core */
114531
 
  Fts3Table *pFts3Tab;
114532
 
};
114533
 
 
114534
 
struct Fts3auxCursor {
114535
 
  sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
114536
 
  Fts3SegReaderCursor csr;        /* Must be right after "base" */
114537
 
  Fts3SegFilter filter;
114538
 
  char *zStop;
114539
 
  int nStop;                      /* Byte-length of string zStop */
114540
 
  int isEof;                      /* True if cursor is at EOF */
114541
 
  sqlite3_int64 iRowid;           /* Current rowid */
114542
 
 
114543
 
  int iCol;                       /* Current value of 'col' column */
114544
 
  int nStat;                      /* Size of aStat[] array */
114545
 
  struct Fts3auxColstats {
114546
 
    sqlite3_int64 nDoc;           /* 'documents' values for current csr row */
114547
 
    sqlite3_int64 nOcc;           /* 'occurrences' values for current csr row */
114548
 
  } *aStat;
114549
 
};
114550
 
 
114551
 
/*
114552
 
** Schema of the terms table.
114553
 
*/
114554
 
#define FTS3_TERMS_SCHEMA "CREATE TABLE x(term, col, documents, occurrences)"
114555
 
 
114556
 
/*
114557
 
** This function does all the work for both the xConnect and xCreate methods.
114558
 
** These tables have no persistent representation of their own, so xConnect
114559
 
** and xCreate are identical operations.
114560
 
*/
114561
 
static int fts3auxConnectMethod(
114562
 
  sqlite3 *db,                    /* Database connection */
114563
 
  void *pUnused,                  /* Unused */
114564
 
  int argc,                       /* Number of elements in argv array */
114565
 
  const char * const *argv,       /* xCreate/xConnect argument array */
114566
 
  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
114567
 
  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
114568
 
){
114569
 
  char const *zDb;                /* Name of database (e.g. "main") */
114570
 
  char const *zFts3;              /* Name of fts3 table */
114571
 
  int nDb;                        /* Result of strlen(zDb) */
114572
 
  int nFts3;                      /* Result of strlen(zFts3) */
114573
 
  int nByte;                      /* Bytes of space to allocate here */
114574
 
  int rc;                         /* value returned by declare_vtab() */
114575
 
  Fts3auxTable *p;                /* Virtual table object to return */
114576
 
 
114577
 
  UNUSED_PARAMETER(pUnused);
114578
 
 
114579
 
  /* The user should specify a single argument - the name of an fts3 table. */
114580
 
  if( argc!=4 ){
114581
 
    *pzErr = sqlite3_mprintf(
114582
 
        "wrong number of arguments to fts4aux constructor"
114583
 
    );
114584
 
    return SQLITE_ERROR;
114585
 
  }
114586
 
 
114587
 
  zDb = argv[1]; 
114588
 
  nDb = strlen(zDb);
114589
 
  zFts3 = argv[3];
114590
 
  nFts3 = strlen(zFts3);
114591
 
 
114592
 
  rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA);
114593
 
  if( rc!=SQLITE_OK ) return rc;
114594
 
 
114595
 
  nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
114596
 
  p = (Fts3auxTable *)sqlite3_malloc(nByte);
114597
 
  if( !p ) return SQLITE_NOMEM;
114598
 
  memset(p, 0, nByte);
114599
 
 
114600
 
  p->pFts3Tab = (Fts3Table *)&p[1];
114601
 
  p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
114602
 
  p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
114603
 
  p->pFts3Tab->db = db;
114604
 
 
114605
 
  memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
114606
 
  memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
114607
 
  sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
114608
 
 
114609
 
  *ppVtab = (sqlite3_vtab *)p;
114610
 
  return SQLITE_OK;
114611
 
}
114612
 
 
114613
 
/*
114614
 
** This function does the work for both the xDisconnect and xDestroy methods.
114615
 
** These tables have no persistent representation of their own, so xDisconnect
114616
 
** and xDestroy are identical operations.
114617
 
*/
114618
 
static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
114619
 
  Fts3auxTable *p = (Fts3auxTable *)pVtab;
114620
 
  Fts3Table *pFts3 = p->pFts3Tab;
114621
 
  int i;
114622
 
 
114623
 
  /* Free any prepared statements held */
114624
 
  for(i=0; i<SizeofArray(pFts3->aStmt); i++){
114625
 
    sqlite3_finalize(pFts3->aStmt[i]);
114626
 
  }
114627
 
  sqlite3_free(pFts3->zSegmentsTbl);
114628
 
  sqlite3_free(p);
114629
 
  return SQLITE_OK;
114630
 
}
114631
 
 
114632
 
#define FTS4AUX_EQ_CONSTRAINT 1
114633
 
#define FTS4AUX_GE_CONSTRAINT 2
114634
 
#define FTS4AUX_LE_CONSTRAINT 4
114635
 
 
114636
 
/*
114637
 
** xBestIndex - Analyze a WHERE and ORDER BY clause.
114638
 
*/
114639
 
static int fts3auxBestIndexMethod(
114640
 
  sqlite3_vtab *pVTab, 
114641
 
  sqlite3_index_info *pInfo
114642
 
){
114643
 
  int i;
114644
 
  int iEq = -1;
114645
 
  int iGe = -1;
114646
 
  int iLe = -1;
114647
 
 
114648
 
  UNUSED_PARAMETER(pVTab);
114649
 
 
114650
 
  /* This vtab delivers always results in "ORDER BY term ASC" order. */
114651
 
  if( pInfo->nOrderBy==1 
114652
 
   && pInfo->aOrderBy[0].iColumn==0 
114653
 
   && pInfo->aOrderBy[0].desc==0
114654
 
  ){
114655
 
    pInfo->orderByConsumed = 1;
114656
 
  }
114657
 
 
114658
 
  /* Search for equality and range constraints on the "term" column. */
114659
 
  for(i=0; i<pInfo->nConstraint; i++){
114660
 
    if( pInfo->aConstraint[i].usable && pInfo->aConstraint[i].iColumn==0 ){
114661
 
      int op = pInfo->aConstraint[i].op;
114662
 
      if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
114663
 
      if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
114664
 
      if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
114665
 
      if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
114666
 
      if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
114667
 
    }
114668
 
  }
114669
 
 
114670
 
  if( iEq>=0 ){
114671
 
    pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
114672
 
    pInfo->aConstraintUsage[iEq].argvIndex = 1;
114673
 
    pInfo->estimatedCost = 5;
114674
 
  }else{
114675
 
    pInfo->idxNum = 0;
114676
 
    pInfo->estimatedCost = 20000;
114677
 
    if( iGe>=0 ){
114678
 
      pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
114679
 
      pInfo->aConstraintUsage[iGe].argvIndex = 1;
114680
 
      pInfo->estimatedCost /= 2;
114681
 
    }
114682
 
    if( iLe>=0 ){
114683
 
      pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
114684
 
      pInfo->aConstraintUsage[iLe].argvIndex = 1 + (iGe>=0);
114685
 
      pInfo->estimatedCost /= 2;
114686
 
    }
114687
 
  }
114688
 
 
114689
 
  return SQLITE_OK;
114690
 
}
114691
 
 
114692
 
/*
114693
 
** xOpen - Open a cursor.
114694
 
*/
114695
 
static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
114696
 
  Fts3auxCursor *pCsr;            /* Pointer to cursor object to return */
114697
 
 
114698
 
  UNUSED_PARAMETER(pVTab);
114699
 
 
114700
 
  pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
114701
 
  if( !pCsr ) return SQLITE_NOMEM;
114702
 
  memset(pCsr, 0, sizeof(Fts3auxCursor));
114703
 
 
114704
 
  *ppCsr = (sqlite3_vtab_cursor *)pCsr;
114705
 
  return SQLITE_OK;
114706
 
}
114707
 
 
114708
 
/*
114709
 
** xClose - Close a cursor.
114710
 
*/
114711
 
static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
114712
 
  Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
114713
 
  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
114714
 
 
114715
 
  sqlite3Fts3SegmentsClose(pFts3);
114716
 
  sqlite3Fts3SegReaderFinish(&pCsr->csr);
114717
 
  sqlite3_free((void *)pCsr->filter.zTerm);
114718
 
  sqlite3_free(pCsr->zStop);
114719
 
  sqlite3_free(pCsr->aStat);
114720
 
  sqlite3_free(pCsr);
114721
 
  return SQLITE_OK;
114722
 
}
114723
 
 
114724
 
static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
114725
 
  if( nSize>pCsr->nStat ){
114726
 
    struct Fts3auxColstats *aNew;
114727
 
    aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat, 
114728
 
        sizeof(struct Fts3auxColstats) * nSize
114729
 
    );
114730
 
    if( aNew==0 ) return SQLITE_NOMEM;
114731
 
    memset(&aNew[pCsr->nStat], 0, 
114732
 
        sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
114733
 
    );
114734
 
    pCsr->aStat = aNew;
114735
 
    pCsr->nStat = nSize;
114736
 
  }
114737
 
  return SQLITE_OK;
114738
 
}
114739
 
 
114740
 
/*
114741
 
** xNext - Advance the cursor to the next row, if any.
114742
 
*/
114743
 
static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
114744
 
  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
114745
 
  Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
114746
 
  int rc;
114747
 
 
114748
 
  /* Increment our pretend rowid value. */
114749
 
  pCsr->iRowid++;
114750
 
 
114751
 
  for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
114752
 
    if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
114753
 
  }
114754
 
 
114755
 
  rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
114756
 
  if( rc==SQLITE_ROW ){
114757
 
    int i = 0;
114758
 
    int nDoclist = pCsr->csr.nDoclist;
114759
 
    char *aDoclist = pCsr->csr.aDoclist;
114760
 
    int iCol;
114761
 
 
114762
 
    int eState = 0;
114763
 
 
114764
 
    if( pCsr->zStop ){
114765
 
      int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
114766
 
      int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
114767
 
      if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
114768
 
        pCsr->isEof = 1;
114769
 
        return SQLITE_OK;
114770
 
      }
114771
 
    }
114772
 
 
114773
 
    if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
114774
 
    memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
114775
 
    iCol = 0;
114776
 
 
114777
 
    while( i<nDoclist ){
114778
 
      sqlite3_int64 v = 0;
114779
 
 
114780
 
      i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
114781
 
      switch( eState ){
114782
 
        /* State 0. In this state the integer just read was a docid. */
114783
 
        case 0:
114784
 
          pCsr->aStat[0].nDoc++;
114785
 
          eState = 1;
114786
 
          iCol = 0;
114787
 
          break;
114788
 
 
114789
 
        /* State 1. In this state we are expecting either a 1, indicating
114790
 
        ** that the following integer will be a column number, or the
114791
 
        ** start of a position list for column 0.  
114792
 
        ** 
114793
 
        ** The only difference between state 1 and state 2 is that if the
114794
 
        ** integer encountered in state 1 is not 0 or 1, then we need to
114795
 
        ** increment the column 0 "nDoc" count for this term.
114796
 
        */
114797
 
        case 1:
114798
 
          assert( iCol==0 );
114799
 
          if( v>1 ){
114800
 
            pCsr->aStat[1].nDoc++;
114801
 
          }
114802
 
          eState = 2;
114803
 
          /* fall through */
114804
 
 
114805
 
        case 2:
114806
 
          if( v==0 ){       /* 0x00. Next integer will be a docid. */
114807
 
            eState = 0;
114808
 
          }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
114809
 
            eState = 3;
114810
 
          }else{            /* 2 or greater. A position. */
114811
 
            pCsr->aStat[iCol+1].nOcc++;
114812
 
            pCsr->aStat[0].nOcc++;
114813
 
          }
114814
 
          break;
114815
 
 
114816
 
        /* State 3. The integer just read is a column number. */
114817
 
        default: assert( eState==3 );
114818
 
          iCol = (int)v;
114819
 
          if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
114820
 
          pCsr->aStat[iCol+1].nDoc++;
114821
 
          eState = 2;
114822
 
          break;
114823
 
      }
114824
 
    }
114825
 
 
114826
 
    pCsr->iCol = 0;
114827
 
    rc = SQLITE_OK;
114828
 
  }else{
114829
 
    pCsr->isEof = 1;
114830
 
  }
114831
 
  return rc;
114832
 
}
114833
 
 
114834
 
/*
114835
 
** xFilter - Initialize a cursor to point at the start of its data.
114836
 
*/
114837
 
static int fts3auxFilterMethod(
114838
 
  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
114839
 
  int idxNum,                     /* Strategy index */
114840
 
  const char *idxStr,             /* Unused */
114841
 
  int nVal,                       /* Number of elements in apVal */
114842
 
  sqlite3_value **apVal           /* Arguments for the indexing scheme */
114843
 
){
114844
 
  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
114845
 
  Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
114846
 
  int rc;
114847
 
  int isScan;
114848
 
 
114849
 
  UNUSED_PARAMETER(nVal);
114850
 
 
114851
 
  assert( idxStr==0 );
114852
 
  assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
114853
 
       || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
114854
 
       || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
114855
 
  );
114856
 
  isScan = (idxNum!=FTS4AUX_EQ_CONSTRAINT);
114857
 
 
114858
 
  /* In case this cursor is being reused, close and zero it. */
114859
 
  testcase(pCsr->filter.zTerm);
114860
 
  sqlite3Fts3SegReaderFinish(&pCsr->csr);
114861
 
  sqlite3_free((void *)pCsr->filter.zTerm);
114862
 
  sqlite3_free(pCsr->aStat);
114863
 
  memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
114864
 
 
114865
 
  pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
114866
 
  if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
114867
 
 
114868
 
  if( idxNum&(FTS4AUX_EQ_CONSTRAINT|FTS4AUX_GE_CONSTRAINT) ){
114869
 
    const unsigned char *zStr = sqlite3_value_text(apVal[0]);
114870
 
    if( zStr ){
114871
 
      pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
114872
 
      pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
114873
 
      if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
114874
 
    }
114875
 
  }
114876
 
  if( idxNum&FTS4AUX_LE_CONSTRAINT ){
114877
 
    int iIdx = (idxNum&FTS4AUX_GE_CONSTRAINT) ? 1 : 0;
114878
 
    pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iIdx]));
114879
 
    pCsr->nStop = sqlite3_value_bytes(apVal[iIdx]);
114880
 
    if( pCsr->zStop==0 ) return SQLITE_NOMEM;
114881
 
  }
114882
 
 
114883
 
  rc = sqlite3Fts3SegReaderCursor(pFts3, FTS3_SEGCURSOR_ALL,
114884
 
      pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
114885
 
  );
114886
 
  if( rc==SQLITE_OK ){
114887
 
    rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
114888
 
  }
114889
 
 
114890
 
  if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
114891
 
  return rc;
114892
 
}
114893
 
 
114894
 
/*
114895
 
** xEof - Return true if the cursor is at EOF, or false otherwise.
114896
 
*/
114897
 
static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
114898
 
  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
114899
 
  return pCsr->isEof;
114900
 
}
114901
 
 
114902
 
/*
114903
 
** xColumn - Return a column value.
114904
 
*/
114905
 
static int fts3auxColumnMethod(
114906
 
  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
114907
 
  sqlite3_context *pContext,      /* Context for sqlite3_result_xxx() calls */
114908
 
  int iCol                        /* Index of column to read value from */
114909
 
){
114910
 
  Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
114911
 
 
114912
 
  assert( p->isEof==0 );
114913
 
  if( iCol==0 ){        /* Column "term" */
114914
 
    sqlite3_result_text(pContext, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
114915
 
  }else if( iCol==1 ){  /* Column "col" */
114916
 
    if( p->iCol ){
114917
 
      sqlite3_result_int(pContext, p->iCol-1);
114918
 
    }else{
114919
 
      sqlite3_result_text(pContext, "*", -1, SQLITE_STATIC);
114920
 
    }
114921
 
  }else if( iCol==2 ){  /* Column "documents" */
114922
 
    sqlite3_result_int64(pContext, p->aStat[p->iCol].nDoc);
114923
 
  }else{                /* Column "occurrences" */
114924
 
    sqlite3_result_int64(pContext, p->aStat[p->iCol].nOcc);
114925
 
  }
114926
 
 
114927
 
  return SQLITE_OK;
114928
 
}
114929
 
 
114930
 
/*
114931
 
** xRowid - Return the current rowid for the cursor.
114932
 
*/
114933
 
static int fts3auxRowidMethod(
114934
 
  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
114935
 
  sqlite_int64 *pRowid            /* OUT: Rowid value */
114936
 
){
114937
 
  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
114938
 
  *pRowid = pCsr->iRowid;
114939
 
  return SQLITE_OK;
114940
 
}
114941
 
 
114942
 
/*
114943
 
** Register the fts3aux module with database connection db. Return SQLITE_OK
114944
 
** if successful or an error code if sqlite3_create_module() fails.
114945
 
*/
114946
 
SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
114947
 
  static const sqlite3_module fts3aux_module = {
114948
 
     0,                           /* iVersion      */
114949
 
     fts3auxConnectMethod,        /* xCreate       */
114950
 
     fts3auxConnectMethod,        /* xConnect      */
114951
 
     fts3auxBestIndexMethod,      /* xBestIndex    */
114952
 
     fts3auxDisconnectMethod,     /* xDisconnect   */
114953
 
     fts3auxDisconnectMethod,     /* xDestroy      */
114954
 
     fts3auxOpenMethod,           /* xOpen         */
114955
 
     fts3auxCloseMethod,          /* xClose        */
114956
 
     fts3auxFilterMethod,         /* xFilter       */
114957
 
     fts3auxNextMethod,           /* xNext         */
114958
 
     fts3auxEofMethod,            /* xEof          */
114959
 
     fts3auxColumnMethod,         /* xColumn       */
114960
 
     fts3auxRowidMethod,          /* xRowid        */
114961
 
     0,                           /* xUpdate       */
114962
 
     0,                           /* xBegin        */
114963
 
     0,                           /* xSync         */
114964
 
     0,                           /* xCommit       */
114965
 
     0,                           /* xRollback     */
114966
 
     0,                           /* xFindFunction */
114967
 
     0                            /* xRename       */
114968
 
  };
114969
 
  int rc;                         /* Return code */
114970
 
 
114971
 
  rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
114972
 
  return rc;
114973
 
}
114974
 
 
114975
 
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
114976
 
 
114977
 
/************** End of fts3_aux.c ********************************************/
114978
 
/************** Begin file fts3_expr.c ***************************************/
114979
 
/*
114980
 
** 2008 Nov 28
114981
 
**
114982
 
** The author disclaims copyright to this source code.  In place of
114983
 
** a legal notice, here is a blessing:
114984
 
**
114985
 
**    May you do good and not evil.
114986
 
**    May you find forgiveness for yourself and forgive others.
114987
 
**    May you share freely, never taking more than you give.
114988
 
**
114989
 
******************************************************************************
114990
 
**
114991
 
** This module contains code that implements a parser for fts3 query strings
114992
 
** (the right-hand argument to the MATCH operator). Because the supported 
114993
 
** syntax is relatively simple, the whole tokenizer/parser system is
114994
 
** hand-coded. 
114995
 
*/
114996
 
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
114997
 
 
114998
 
/*
114999
 
** By default, this module parses the legacy syntax that has been 
115000
 
** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
115001
 
** is defined, then it uses the new syntax. The differences between
115002
 
** the new and the old syntaxes are:
115003
 
**
115004
 
**  a) The new syntax supports parenthesis. The old does not.
115005
 
**
115006
 
**  b) The new syntax supports the AND and NOT operators. The old does not.
115007
 
**
115008
 
**  c) The old syntax supports the "-" token qualifier. This is not 
115009
 
**     supported by the new syntax (it is replaced by the NOT operator).
115010
 
**
115011
 
**  d) When using the old syntax, the OR operator has a greater precedence
115012
 
**     than an implicit AND. When using the new, both implicity and explicit
115013
 
**     AND operators have a higher precedence than OR.
115014
 
**
115015
 
** If compiled with SQLITE_TEST defined, then this module exports the
115016
 
** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
115017
 
** to zero causes the module to use the old syntax. If it is set to 
115018
 
** non-zero the new syntax is activated. This is so both syntaxes can
115019
 
** be tested using a single build of testfixture.
115020
 
**
115021
 
** The following describes the syntax supported by the fts3 MATCH
115022
 
** operator in a similar format to that used by the lemon parser
115023
 
** generator. This module does not use actually lemon, it uses a
115024
 
** custom parser.
115025
 
**
115026
 
**   query ::= andexpr (OR andexpr)*.
115027
 
**
115028
 
**   andexpr ::= notexpr (AND? notexpr)*.
115029
 
**
115030
 
**   notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
115031
 
**   notexpr ::= LP query RP.
115032
 
**
115033
 
**   nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
115034
 
**
115035
 
**   distance_opt ::= .
115036
 
**   distance_opt ::= / INTEGER.
115037
 
**
115038
 
**   phrase ::= TOKEN.
115039
 
**   phrase ::= COLUMN:TOKEN.
115040
 
**   phrase ::= "TOKEN TOKEN TOKEN...".
115041
 
*/
115042
 
 
115043
 
#ifdef SQLITE_TEST
115044
 
SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
115045
 
#else
115046
 
# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS 
115047
 
#  define sqlite3_fts3_enable_parentheses 1
115048
 
# else
115049
 
#  define sqlite3_fts3_enable_parentheses 0
115050
 
# endif
115051
 
#endif
115052
 
 
115053
 
/*
115054
 
** Default span for NEAR operators.
115055
 
*/
115056
 
#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
115057
 
 
115058
 
 
115059
 
typedef struct ParseContext ParseContext;
115060
 
struct ParseContext {
115061
 
  sqlite3_tokenizer *pTokenizer;      /* Tokenizer module */
115062
 
  const char **azCol;                 /* Array of column names for fts3 table */
115063
 
  int nCol;                           /* Number of entries in azCol[] */
115064
 
  int iDefaultCol;                    /* Default column to query */
115065
 
  sqlite3_context *pCtx;              /* Write error message here */
115066
 
  int nNest;                          /* Number of nested brackets */
115067
 
};
115068
 
 
115069
 
/*
115070
 
** This function is equivalent to the standard isspace() function. 
115071
 
**
115072
 
** The standard isspace() can be awkward to use safely, because although it
115073
 
** is defined to accept an argument of type int, its behaviour when passed
115074
 
** an integer that falls outside of the range of the unsigned char type
115075
 
** is undefined (and sometimes, "undefined" means segfault). This wrapper
115076
 
** is defined to accept an argument of type char, and always returns 0 for
115077
 
** any values that fall outside of the range of the unsigned char type (i.e.
115078
 
** negative values).
115079
 
*/
115080
 
static int fts3isspace(char c){
115081
 
  return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
115082
 
}
115083
 
 
115084
 
/*
115085
 
** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
115086
 
** zero the memory before returning a pointer to it. If unsuccessful, 
115087
 
** return NULL.
115088
 
*/
115089
 
static void *fts3MallocZero(int nByte){
115090
 
  void *pRet = sqlite3_malloc(nByte);
115091
 
  if( pRet ) memset(pRet, 0, nByte);
115092
 
  return pRet;
115093
 
}
115094
 
 
115095
 
 
115096
 
/*
115097
 
** Extract the next token from buffer z (length n) using the tokenizer
115098
 
** and other information (column names etc.) in pParse. Create an Fts3Expr
115099
 
** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
115100
 
** single token and set *ppExpr to point to it. If the end of the buffer is
115101
 
** reached before a token is found, set *ppExpr to zero. It is the
115102
 
** responsibility of the caller to eventually deallocate the allocated 
115103
 
** Fts3Expr structure (if any) by passing it to sqlite3_free().
115104
 
**
115105
 
** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
115106
 
** fails.
115107
 
*/
115108
 
static int getNextToken(
115109
 
  ParseContext *pParse,                   /* fts3 query parse context */
115110
 
  int iCol,                               /* Value for Fts3Phrase.iColumn */
115111
 
  const char *z, int n,                   /* Input string */
115112
 
  Fts3Expr **ppExpr,                      /* OUT: expression */
115113
 
  int *pnConsumed                         /* OUT: Number of bytes consumed */
115114
 
){
115115
 
  sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
115116
 
  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
115117
 
  int rc;
115118
 
  sqlite3_tokenizer_cursor *pCursor;
115119
 
  Fts3Expr *pRet = 0;
115120
 
  int nConsumed = 0;
115121
 
 
115122
 
  rc = pModule->xOpen(pTokenizer, z, n, &pCursor);
115123
 
  if( rc==SQLITE_OK ){
115124
 
    const char *zToken;
115125
 
    int nToken, iStart, iEnd, iPosition;
115126
 
    int nByte;                               /* total space to allocate */
115127
 
 
115128
 
    pCursor->pTokenizer = pTokenizer;
115129
 
    rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
115130
 
 
115131
 
    if( rc==SQLITE_OK ){
115132
 
      nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
115133
 
      pRet = (Fts3Expr *)fts3MallocZero(nByte);
115134
 
      if( !pRet ){
115135
 
        rc = SQLITE_NOMEM;
115136
 
      }else{
115137
 
        pRet->eType = FTSQUERY_PHRASE;
115138
 
        pRet->pPhrase = (Fts3Phrase *)&pRet[1];
115139
 
        pRet->pPhrase->nToken = 1;
115140
 
        pRet->pPhrase->iColumn = iCol;
115141
 
        pRet->pPhrase->aToken[0].n = nToken;
115142
 
        pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
115143
 
        memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
115144
 
 
115145
 
        if( iEnd<n && z[iEnd]=='*' ){
115146
 
          pRet->pPhrase->aToken[0].isPrefix = 1;
115147
 
          iEnd++;
115148
 
        }
115149
 
        if( !sqlite3_fts3_enable_parentheses && iStart>0 && z[iStart-1]=='-' ){
115150
 
          pRet->pPhrase->isNot = 1;
115151
 
        }
115152
 
      }
115153
 
      nConsumed = iEnd;
115154
 
    }
115155
 
 
115156
 
    pModule->xClose(pCursor);
115157
 
  }
115158
 
  
115159
 
  *pnConsumed = nConsumed;
115160
 
  *ppExpr = pRet;
115161
 
  return rc;
115162
 
}
115163
 
 
115164
 
 
115165
 
/*
115166
 
** Enlarge a memory allocation.  If an out-of-memory allocation occurs,
115167
 
** then free the old allocation.
115168
 
*/
115169
 
static void *fts3ReallocOrFree(void *pOrig, int nNew){
115170
 
  void *pRet = sqlite3_realloc(pOrig, nNew);
115171
 
  if( !pRet ){
115172
 
    sqlite3_free(pOrig);
115173
 
  }
115174
 
  return pRet;
115175
 
}
115176
 
 
115177
 
/*
115178
 
** Buffer zInput, length nInput, contains the contents of a quoted string
115179
 
** that appeared as part of an fts3 query expression. Neither quote character
115180
 
** is included in the buffer. This function attempts to tokenize the entire
115181
 
** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE 
115182
 
** containing the results.
115183
 
**
115184
 
** If successful, SQLITE_OK is returned and *ppExpr set to point at the
115185
 
** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
115186
 
** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
115187
 
** to 0.
115188
 
*/
115189
 
static int getNextString(
115190
 
  ParseContext *pParse,                   /* fts3 query parse context */
115191
 
  const char *zInput, int nInput,         /* Input string */
115192
 
  Fts3Expr **ppExpr                       /* OUT: expression */
115193
 
){
115194
 
  sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
115195
 
  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
115196
 
  int rc;
115197
 
  Fts3Expr *p = 0;
115198
 
  sqlite3_tokenizer_cursor *pCursor = 0;
115199
 
  char *zTemp = 0;
115200
 
  int nTemp = 0;
115201
 
 
115202
 
  rc = pModule->xOpen(pTokenizer, zInput, nInput, &pCursor);
115203
 
  if( rc==SQLITE_OK ){
115204
 
    int ii;
115205
 
    pCursor->pTokenizer = pTokenizer;
115206
 
    for(ii=0; rc==SQLITE_OK; ii++){
115207
 
      const char *zToken;
115208
 
      int nToken, iBegin, iEnd, iPos;
115209
 
      rc = pModule->xNext(pCursor, &zToken, &nToken, &iBegin, &iEnd, &iPos);
115210
 
      if( rc==SQLITE_OK ){
115211
 
        int nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
115212
 
        p = fts3ReallocOrFree(p, nByte+ii*sizeof(Fts3PhraseToken));
115213
 
        zTemp = fts3ReallocOrFree(zTemp, nTemp + nToken);
115214
 
        if( !p || !zTemp ){
115215
 
          goto no_mem;
115216
 
        }
115217
 
        if( ii==0 ){
115218
 
          memset(p, 0, nByte);
115219
 
          p->pPhrase = (Fts3Phrase *)&p[1];
115220
 
        }
115221
 
        p->pPhrase = (Fts3Phrase *)&p[1];
115222
 
        memset(&p->pPhrase->aToken[ii], 0, sizeof(Fts3PhraseToken));
115223
 
        p->pPhrase->nToken = ii+1;
115224
 
        p->pPhrase->aToken[ii].n = nToken;
115225
 
        memcpy(&zTemp[nTemp], zToken, nToken);
115226
 
        nTemp += nToken;
115227
 
        if( iEnd<nInput && zInput[iEnd]=='*' ){
115228
 
          p->pPhrase->aToken[ii].isPrefix = 1;
115229
 
        }else{
115230
 
          p->pPhrase->aToken[ii].isPrefix = 0;
115231
 
        }
115232
 
      }
115233
 
    }
115234
 
 
115235
 
    pModule->xClose(pCursor);
115236
 
    pCursor = 0;
115237
 
  }
115238
 
 
115239
 
  if( rc==SQLITE_DONE ){
115240
 
    int jj;
115241
 
    char *zNew = NULL;
115242
 
    int nNew = 0;
115243
 
    int nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
115244
 
    nByte += (p?(p->pPhrase->nToken-1):0) * sizeof(Fts3PhraseToken);
115245
 
    p = fts3ReallocOrFree(p, nByte + nTemp);
115246
 
    if( !p ){
115247
 
      goto no_mem;
115248
 
    }
115249
 
    if( zTemp ){
115250
 
      zNew = &(((char *)p)[nByte]);
115251
 
      memcpy(zNew, zTemp, nTemp);
115252
 
    }else{
115253
 
      memset(p, 0, nByte+nTemp);
115254
 
    }
115255
 
    p->pPhrase = (Fts3Phrase *)&p[1];
115256
 
    for(jj=0; jj<p->pPhrase->nToken; jj++){
115257
 
      p->pPhrase->aToken[jj].z = &zNew[nNew];
115258
 
      nNew += p->pPhrase->aToken[jj].n;
115259
 
    }
115260
 
    sqlite3_free(zTemp);
115261
 
    p->eType = FTSQUERY_PHRASE;
115262
 
    p->pPhrase->iColumn = pParse->iDefaultCol;
115263
 
    rc = SQLITE_OK;
115264
 
  }
115265
 
 
115266
 
  *ppExpr = p;
115267
 
  return rc;
115268
 
no_mem:
115269
 
 
115270
 
  if( pCursor ){
115271
 
    pModule->xClose(pCursor);
115272
 
  }
115273
 
  sqlite3_free(zTemp);
115274
 
  sqlite3_free(p);
115275
 
  *ppExpr = 0;
115276
 
  return SQLITE_NOMEM;
115277
 
}
115278
 
 
115279
 
/*
115280
 
** Function getNextNode(), which is called by fts3ExprParse(), may itself
115281
 
** call fts3ExprParse(). So this forward declaration is required.
115282
 
*/
115283
 
static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
115284
 
 
115285
 
/*
115286
 
** The output variable *ppExpr is populated with an allocated Fts3Expr 
115287
 
** structure, or set to 0 if the end of the input buffer is reached.
115288
 
**
115289
 
** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
115290
 
** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
115291
 
** If SQLITE_ERROR is returned, pContext is populated with an error message.
115292
 
*/
115293
 
static int getNextNode(
115294
 
  ParseContext *pParse,                   /* fts3 query parse context */
115295
 
  const char *z, int n,                   /* Input string */
115296
 
  Fts3Expr **ppExpr,                      /* OUT: expression */
115297
 
  int *pnConsumed                         /* OUT: Number of bytes consumed */
115298
 
){
115299
 
  static const struct Fts3Keyword {
115300
 
    char *z;                              /* Keyword text */
115301
 
    unsigned char n;                      /* Length of the keyword */
115302
 
    unsigned char parenOnly;              /* Only valid in paren mode */
115303
 
    unsigned char eType;                  /* Keyword code */
115304
 
  } aKeyword[] = {
115305
 
    { "OR" ,  2, 0, FTSQUERY_OR   },
115306
 
    { "AND",  3, 1, FTSQUERY_AND  },
115307
 
    { "NOT",  3, 1, FTSQUERY_NOT  },
115308
 
    { "NEAR", 4, 0, FTSQUERY_NEAR }
115309
 
  };
115310
 
  int ii;
115311
 
  int iCol;
115312
 
  int iColLen;
115313
 
  int rc;
115314
 
  Fts3Expr *pRet = 0;
115315
 
 
115316
 
  const char *zInput = z;
115317
 
  int nInput = n;
115318
 
 
115319
 
  /* Skip over any whitespace before checking for a keyword, an open or
115320
 
  ** close bracket, or a quoted string. 
115321
 
  */
115322
 
  while( nInput>0 && fts3isspace(*zInput) ){
115323
 
    nInput--;
115324
 
    zInput++;
115325
 
  }
115326
 
  if( nInput==0 ){
115327
 
    return SQLITE_DONE;
115328
 
  }
115329
 
 
115330
 
  /* See if we are dealing with a keyword. */
115331
 
  for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
115332
 
    const struct Fts3Keyword *pKey = &aKeyword[ii];
115333
 
 
115334
 
    if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
115335
 
      continue;
115336
 
    }
115337
 
 
115338
 
    if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
115339
 
      int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
115340
 
      int nKey = pKey->n;
115341
 
      char cNext;
115342
 
 
115343
 
      /* If this is a "NEAR" keyword, check for an explicit nearness. */
115344
 
      if( pKey->eType==FTSQUERY_NEAR ){
115345
 
        assert( nKey==4 );
115346
 
        if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
115347
 
          nNear = 0;
115348
 
          for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
115349
 
            nNear = nNear * 10 + (zInput[nKey] - '0');
115350
 
          }
115351
 
        }
115352
 
      }
115353
 
 
115354
 
      /* At this point this is probably a keyword. But for that to be true,
115355
 
      ** the next byte must contain either whitespace, an open or close
115356
 
      ** parenthesis, a quote character, or EOF. 
115357
 
      */
115358
 
      cNext = zInput[nKey];
115359
 
      if( fts3isspace(cNext) 
115360
 
       || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
115361
 
      ){
115362
 
        pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
115363
 
        if( !pRet ){
115364
 
          return SQLITE_NOMEM;
115365
 
        }
115366
 
        pRet->eType = pKey->eType;
115367
 
        pRet->nNear = nNear;
115368
 
        *ppExpr = pRet;
115369
 
        *pnConsumed = (int)((zInput - z) + nKey);
115370
 
        return SQLITE_OK;
115371
 
      }
115372
 
 
115373
 
      /* Turns out that wasn't a keyword after all. This happens if the
115374
 
      ** user has supplied a token such as "ORacle". Continue.
115375
 
      */
115376
 
    }
115377
 
  }
115378
 
 
115379
 
  /* Check for an open bracket. */
115380
 
  if( sqlite3_fts3_enable_parentheses ){
115381
 
    if( *zInput=='(' ){
115382
 
      int nConsumed;
115383
 
      pParse->nNest++;
115384
 
      rc = fts3ExprParse(pParse, &zInput[1], nInput-1, ppExpr, &nConsumed);
115385
 
      if( rc==SQLITE_OK && !*ppExpr ){
115386
 
        rc = SQLITE_DONE;
115387
 
      }
115388
 
      *pnConsumed = (int)((zInput - z) + 1 + nConsumed);
115389
 
      return rc;
115390
 
    }
115391
 
  
115392
 
    /* Check for a close bracket. */
115393
 
    if( *zInput==')' ){
115394
 
      pParse->nNest--;
115395
 
      *pnConsumed = (int)((zInput - z) + 1);
115396
 
      return SQLITE_DONE;
115397
 
    }
115398
 
  }
115399
 
 
115400
 
  /* See if we are dealing with a quoted phrase. If this is the case, then
115401
 
  ** search for the closing quote and pass the whole string to getNextString()
115402
 
  ** for processing. This is easy to do, as fts3 has no syntax for escaping
115403
 
  ** a quote character embedded in a string.
115404
 
  */
115405
 
  if( *zInput=='"' ){
115406
 
    for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
115407
 
    *pnConsumed = (int)((zInput - z) + ii + 1);
115408
 
    if( ii==nInput ){
115409
 
      return SQLITE_ERROR;
115410
 
    }
115411
 
    return getNextString(pParse, &zInput[1], ii-1, ppExpr);
115412
 
  }
115413
 
 
115414
 
 
115415
 
  /* If control flows to this point, this must be a regular token, or 
115416
 
  ** the end of the input. Read a regular token using the sqlite3_tokenizer
115417
 
  ** interface. Before doing so, figure out if there is an explicit
115418
 
  ** column specifier for the token. 
115419
 
  **
115420
 
  ** TODO: Strangely, it is not possible to associate a column specifier
115421
 
  ** with a quoted phrase, only with a single token. Not sure if this was
115422
 
  ** an implementation artifact or an intentional decision when fts3 was
115423
 
  ** first implemented. Whichever it was, this module duplicates the 
115424
 
  ** limitation.
115425
 
  */
115426
 
  iCol = pParse->iDefaultCol;
115427
 
  iColLen = 0;
115428
 
  for(ii=0; ii<pParse->nCol; ii++){
115429
 
    const char *zStr = pParse->azCol[ii];
115430
 
    int nStr = (int)strlen(zStr);
115431
 
    if( nInput>nStr && zInput[nStr]==':' 
115432
 
     && sqlite3_strnicmp(zStr, zInput, nStr)==0 
115433
 
    ){
115434
 
      iCol = ii;
115435
 
      iColLen = (int)((zInput - z) + nStr + 1);
115436
 
      break;
115437
 
    }
115438
 
  }
115439
 
  rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
115440
 
  *pnConsumed += iColLen;
115441
 
  return rc;
115442
 
}
115443
 
 
115444
 
/*
115445
 
** The argument is an Fts3Expr structure for a binary operator (any type
115446
 
** except an FTSQUERY_PHRASE). Return an integer value representing the
115447
 
** precedence of the operator. Lower values have a higher precedence (i.e.
115448
 
** group more tightly). For example, in the C language, the == operator
115449
 
** groups more tightly than ||, and would therefore have a higher precedence.
115450
 
**
115451
 
** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
115452
 
** is defined), the order of the operators in precedence from highest to
115453
 
** lowest is:
115454
 
**
115455
 
**   NEAR
115456
 
**   NOT
115457
 
**   AND (including implicit ANDs)
115458
 
**   OR
115459
 
**
115460
 
** Note that when using the old query syntax, the OR operator has a higher
115461
 
** precedence than the AND operator.
115462
 
*/
115463
 
static int opPrecedence(Fts3Expr *p){
115464
 
  assert( p->eType!=FTSQUERY_PHRASE );
115465
 
  if( sqlite3_fts3_enable_parentheses ){
115466
 
    return p->eType;
115467
 
  }else if( p->eType==FTSQUERY_NEAR ){
115468
 
    return 1;
115469
 
  }else if( p->eType==FTSQUERY_OR ){
115470
 
    return 2;
115471
 
  }
115472
 
  assert( p->eType==FTSQUERY_AND );
115473
 
  return 3;
115474
 
}
115475
 
 
115476
 
/*
115477
 
** Argument ppHead contains a pointer to the current head of a query 
115478
 
** expression tree being parsed. pPrev is the expression node most recently
115479
 
** inserted into the tree. This function adds pNew, which is always a binary
115480
 
** operator node, into the expression tree based on the relative precedence
115481
 
** of pNew and the existing nodes of the tree. This may result in the head
115482
 
** of the tree changing, in which case *ppHead is set to the new root node.
115483
 
*/
115484
 
static void insertBinaryOperator(
115485
 
  Fts3Expr **ppHead,       /* Pointer to the root node of a tree */
115486
 
  Fts3Expr *pPrev,         /* Node most recently inserted into the tree */
115487
 
  Fts3Expr *pNew           /* New binary node to insert into expression tree */
115488
 
){
115489
 
  Fts3Expr *pSplit = pPrev;
115490
 
  while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
115491
 
    pSplit = pSplit->pParent;
115492
 
  }
115493
 
 
115494
 
  if( pSplit->pParent ){
115495
 
    assert( pSplit->pParent->pRight==pSplit );
115496
 
    pSplit->pParent->pRight = pNew;
115497
 
    pNew->pParent = pSplit->pParent;
115498
 
  }else{
115499
 
    *ppHead = pNew;
115500
 
  }
115501
 
  pNew->pLeft = pSplit;
115502
 
  pSplit->pParent = pNew;
115503
 
}
115504
 
 
115505
 
/*
115506
 
** Parse the fts3 query expression found in buffer z, length n. This function
115507
 
** returns either when the end of the buffer is reached or an unmatched 
115508
 
** closing bracket - ')' - is encountered.
115509
 
**
115510
 
** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
115511
 
** parsed form of the expression and *pnConsumed is set to the number of
115512
 
** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
115513
 
** (out of memory error) or SQLITE_ERROR (parse error) is returned.
115514
 
*/
115515
 
static int fts3ExprParse(
115516
 
  ParseContext *pParse,                   /* fts3 query parse context */
115517
 
  const char *z, int n,                   /* Text of MATCH query */
115518
 
  Fts3Expr **ppExpr,                      /* OUT: Parsed query structure */
115519
 
  int *pnConsumed                         /* OUT: Number of bytes consumed */
115520
 
){
115521
 
  Fts3Expr *pRet = 0;
115522
 
  Fts3Expr *pPrev = 0;
115523
 
  Fts3Expr *pNotBranch = 0;               /* Only used in legacy parse mode */
115524
 
  int nIn = n;
115525
 
  const char *zIn = z;
115526
 
  int rc = SQLITE_OK;
115527
 
  int isRequirePhrase = 1;
115528
 
 
115529
 
  while( rc==SQLITE_OK ){
115530
 
    Fts3Expr *p = 0;
115531
 
    int nByte = 0;
115532
 
    rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
115533
 
    if( rc==SQLITE_OK ){
115534
 
      int isPhrase;
115535
 
 
115536
 
      if( !sqlite3_fts3_enable_parentheses 
115537
 
       && p->eType==FTSQUERY_PHRASE && p->pPhrase->isNot 
115538
 
      ){
115539
 
        /* Create an implicit NOT operator. */
115540
 
        Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
115541
 
        if( !pNot ){
115542
 
          sqlite3Fts3ExprFree(p);
115543
 
          rc = SQLITE_NOMEM;
115544
 
          goto exprparse_out;
115545
 
        }
115546
 
        pNot->eType = FTSQUERY_NOT;
115547
 
        pNot->pRight = p;
115548
 
        if( pNotBranch ){
115549
 
          pNot->pLeft = pNotBranch;
115550
 
        }
115551
 
        pNotBranch = pNot;
115552
 
        p = pPrev;
115553
 
      }else{
115554
 
        int eType = p->eType;
115555
 
        assert( eType!=FTSQUERY_PHRASE || !p->pPhrase->isNot );
115556
 
        isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
115557
 
 
115558
 
        /* The isRequirePhrase variable is set to true if a phrase or
115559
 
        ** an expression contained in parenthesis is required. If a
115560
 
        ** binary operator (AND, OR, NOT or NEAR) is encounted when
115561
 
        ** isRequirePhrase is set, this is a syntax error.
115562
 
        */
115563
 
        if( !isPhrase && isRequirePhrase ){
115564
 
          sqlite3Fts3ExprFree(p);
115565
 
          rc = SQLITE_ERROR;
115566
 
          goto exprparse_out;
115567
 
        }
115568
 
  
115569
 
        if( isPhrase && !isRequirePhrase ){
115570
 
          /* Insert an implicit AND operator. */
115571
 
          Fts3Expr *pAnd;
115572
 
          assert( pRet && pPrev );
115573
 
          pAnd = fts3MallocZero(sizeof(Fts3Expr));
115574
 
          if( !pAnd ){
115575
 
            sqlite3Fts3ExprFree(p);
115576
 
            rc = SQLITE_NOMEM;
115577
 
            goto exprparse_out;
115578
 
          }
115579
 
          pAnd->eType = FTSQUERY_AND;
115580
 
          insertBinaryOperator(&pRet, pPrev, pAnd);
115581
 
          pPrev = pAnd;
115582
 
        }
115583
 
 
115584
 
        /* This test catches attempts to make either operand of a NEAR
115585
 
        ** operator something other than a phrase. For example, either of
115586
 
        ** the following:
115587
 
        **
115588
 
        **    (bracketed expression) NEAR phrase
115589
 
        **    phrase NEAR (bracketed expression)
115590
 
        **
115591
 
        ** Return an error in either case.
115592
 
        */
115593
 
        if( pPrev && (
115594
 
            (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
115595
 
         || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
115596
 
        )){
115597
 
          sqlite3Fts3ExprFree(p);
115598
 
          rc = SQLITE_ERROR;
115599
 
          goto exprparse_out;
115600
 
        }
115601
 
  
115602
 
        if( isPhrase ){
115603
 
          if( pRet ){
115604
 
            assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
115605
 
            pPrev->pRight = p;
115606
 
            p->pParent = pPrev;
115607
 
          }else{
115608
 
            pRet = p;
115609
 
          }
115610
 
        }else{
115611
 
          insertBinaryOperator(&pRet, pPrev, p);
115612
 
        }
115613
 
        isRequirePhrase = !isPhrase;
115614
 
      }
115615
 
      assert( nByte>0 );
115616
 
    }
115617
 
    assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
115618
 
    nIn -= nByte;
115619
 
    zIn += nByte;
115620
 
    pPrev = p;
115621
 
  }
115622
 
 
115623
 
  if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
115624
 
    rc = SQLITE_ERROR;
115625
 
  }
115626
 
 
115627
 
  if( rc==SQLITE_DONE ){
115628
 
    rc = SQLITE_OK;
115629
 
    if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
115630
 
      if( !pRet ){
115631
 
        rc = SQLITE_ERROR;
115632
 
      }else{
115633
 
        Fts3Expr *pIter = pNotBranch;
115634
 
        while( pIter->pLeft ){
115635
 
          pIter = pIter->pLeft;
115636
 
        }
115637
 
        pIter->pLeft = pRet;
115638
 
        pRet = pNotBranch;
115639
 
      }
115640
 
    }
115641
 
  }
115642
 
  *pnConsumed = n - nIn;
115643
 
 
115644
 
exprparse_out:
115645
 
  if( rc!=SQLITE_OK ){
115646
 
    sqlite3Fts3ExprFree(pRet);
115647
 
    sqlite3Fts3ExprFree(pNotBranch);
115648
 
    pRet = 0;
115649
 
  }
115650
 
  *ppExpr = pRet;
115651
 
  return rc;
115652
 
}
115653
 
 
115654
 
/*
115655
 
** Parameters z and n contain a pointer to and length of a buffer containing
115656
 
** an fts3 query expression, respectively. This function attempts to parse the
115657
 
** query expression and create a tree of Fts3Expr structures representing the
115658
 
** parsed expression. If successful, *ppExpr is set to point to the head
115659
 
** of the parsed expression tree and SQLITE_OK is returned. If an error
115660
 
** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
115661
 
** error) is returned and *ppExpr is set to 0.
115662
 
**
115663
 
** If parameter n is a negative number, then z is assumed to point to a
115664
 
** nul-terminated string and the length is determined using strlen().
115665
 
**
115666
 
** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
115667
 
** use to normalize query tokens while parsing the expression. The azCol[]
115668
 
** array, which is assumed to contain nCol entries, should contain the names
115669
 
** of each column in the target fts3 table, in order from left to right. 
115670
 
** Column names must be nul-terminated strings.
115671
 
**
115672
 
** The iDefaultCol parameter should be passed the index of the table column
115673
 
** that appears on the left-hand-side of the MATCH operator (the default
115674
 
** column to match against for tokens for which a column name is not explicitly
115675
 
** specified as part of the query string), or -1 if tokens may by default
115676
 
** match any table column.
115677
 
*/
115678
 
SQLITE_PRIVATE int sqlite3Fts3ExprParse(
115679
 
  sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
115680
 
  char **azCol,                       /* Array of column names for fts3 table */
115681
 
  int nCol,                           /* Number of entries in azCol[] */
115682
 
  int iDefaultCol,                    /* Default column to query */
115683
 
  const char *z, int n,               /* Text of MATCH query */
115684
 
  Fts3Expr **ppExpr                   /* OUT: Parsed query structure */
115685
 
){
115686
 
  int nParsed;
115687
 
  int rc;
115688
 
  ParseContext sParse;
115689
 
  sParse.pTokenizer = pTokenizer;
115690
 
  sParse.azCol = (const char **)azCol;
115691
 
  sParse.nCol = nCol;
115692
 
  sParse.iDefaultCol = iDefaultCol;
115693
 
  sParse.nNest = 0;
115694
 
  if( z==0 ){
115695
 
    *ppExpr = 0;
115696
 
    return SQLITE_OK;
115697
 
  }
115698
 
  if( n<0 ){
115699
 
    n = (int)strlen(z);
115700
 
  }
115701
 
  rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
115702
 
 
115703
 
  /* Check for mismatched parenthesis */
115704
 
  if( rc==SQLITE_OK && sParse.nNest ){
115705
 
    rc = SQLITE_ERROR;
115706
 
    sqlite3Fts3ExprFree(*ppExpr);
115707
 
    *ppExpr = 0;
115708
 
  }
115709
 
 
115710
 
  return rc;
115711
 
}
115712
 
 
115713
 
/*
115714
 
** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
115715
 
*/
115716
 
SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *p){
115717
 
  if( p ){
115718
 
    sqlite3Fts3ExprFree(p->pLeft);
115719
 
    sqlite3Fts3ExprFree(p->pRight);
115720
 
    sqlite3_free(p->aDoclist);
115721
 
    sqlite3_free(p);
115722
 
  }
115723
 
}
115724
 
 
115725
 
/****************************************************************************
115726
 
*****************************************************************************
115727
 
** Everything after this point is just test code.
115728
 
*/
115729
 
 
115730
 
#ifdef SQLITE_TEST
115731
 
 
115732
 
 
115733
 
/*
115734
 
** Function to query the hash-table of tokenizers (see README.tokenizers).
115735
 
*/
115736
 
static int queryTestTokenizer(
115737
 
  sqlite3 *db, 
115738
 
  const char *zName,  
115739
 
  const sqlite3_tokenizer_module **pp
115740
 
){
115741
 
  int rc;
115742
 
  sqlite3_stmt *pStmt;
115743
 
  const char zSql[] = "SELECT fts3_tokenizer(?)";
115744
 
 
115745
 
  *pp = 0;
115746
 
  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
115747
 
  if( rc!=SQLITE_OK ){
115748
 
    return rc;
115749
 
  }
115750
 
 
115751
 
  sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
115752
 
  if( SQLITE_ROW==sqlite3_step(pStmt) ){
115753
 
    if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
115754
 
      memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
115755
 
    }
115756
 
  }
115757
 
 
115758
 
  return sqlite3_finalize(pStmt);
115759
 
}
115760
 
 
115761
 
/*
115762
 
** Return a pointer to a buffer containing a text representation of the
115763
 
** expression passed as the first argument. The buffer is obtained from
115764
 
** sqlite3_malloc(). It is the responsibility of the caller to use 
115765
 
** sqlite3_free() to release the memory. If an OOM condition is encountered,
115766
 
** NULL is returned.
115767
 
**
115768
 
** If the second argument is not NULL, then its contents are prepended to 
115769
 
** the returned expression text and then freed using sqlite3_free().
115770
 
*/
115771
 
static char *exprToString(Fts3Expr *pExpr, char *zBuf){
115772
 
  switch( pExpr->eType ){
115773
 
    case FTSQUERY_PHRASE: {
115774
 
      Fts3Phrase *pPhrase = pExpr->pPhrase;
115775
 
      int i;
115776
 
      zBuf = sqlite3_mprintf(
115777
 
          "%zPHRASE %d %d", zBuf, pPhrase->iColumn, pPhrase->isNot);
115778
 
      for(i=0; zBuf && i<pPhrase->nToken; i++){
115779
 
        zBuf = sqlite3_mprintf("%z %.*s%s", zBuf, 
115780
 
            pPhrase->aToken[i].n, pPhrase->aToken[i].z,
115781
 
            (pPhrase->aToken[i].isPrefix?"+":"")
115782
 
        );
115783
 
      }
115784
 
      return zBuf;
115785
 
    }
115786
 
 
115787
 
    case FTSQUERY_NEAR:
115788
 
      zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
115789
 
      break;
115790
 
    case FTSQUERY_NOT:
115791
 
      zBuf = sqlite3_mprintf("%zNOT ", zBuf);
115792
 
      break;
115793
 
    case FTSQUERY_AND:
115794
 
      zBuf = sqlite3_mprintf("%zAND ", zBuf);
115795
 
      break;
115796
 
    case FTSQUERY_OR:
115797
 
      zBuf = sqlite3_mprintf("%zOR ", zBuf);
115798
 
      break;
115799
 
  }
115800
 
 
115801
 
  if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
115802
 
  if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
115803
 
  if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
115804
 
 
115805
 
  if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
115806
 
  if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
115807
 
 
115808
 
  return zBuf;
115809
 
}
115810
 
 
115811
 
/*
115812
 
** This is the implementation of a scalar SQL function used to test the 
115813
 
** expression parser. It should be called as follows:
115814
 
**
115815
 
**   fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
115816
 
**
115817
 
** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
115818
 
** to parse the query expression (see README.tokenizers). The second argument
115819
 
** is the query expression to parse. Each subsequent argument is the name
115820
 
** of a column of the fts3 table that the query expression may refer to.
115821
 
** For example:
115822
 
**
115823
 
**   SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
115824
 
*/
115825
 
static void fts3ExprTest(
115826
 
  sqlite3_context *context,
115827
 
  int argc,
115828
 
  sqlite3_value **argv
115829
 
){
115830
 
  sqlite3_tokenizer_module const *pModule = 0;
115831
 
  sqlite3_tokenizer *pTokenizer = 0;
115832
 
  int rc;
115833
 
  char **azCol = 0;
115834
 
  const char *zExpr;
115835
 
  int nExpr;
115836
 
  int nCol;
115837
 
  int ii;
115838
 
  Fts3Expr *pExpr;
115839
 
  char *zBuf = 0;
115840
 
  sqlite3 *db = sqlite3_context_db_handle(context);
115841
 
 
115842
 
  if( argc<3 ){
115843
 
    sqlite3_result_error(context, 
115844
 
        "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
115845
 
    );
115846
 
    return;
115847
 
  }
115848
 
 
115849
 
  rc = queryTestTokenizer(db,
115850
 
                          (const char *)sqlite3_value_text(argv[0]), &pModule);
115851
 
  if( rc==SQLITE_NOMEM ){
115852
 
    sqlite3_result_error_nomem(context);
115853
 
    goto exprtest_out;
115854
 
  }else if( !pModule ){
115855
 
    sqlite3_result_error(context, "No such tokenizer module", -1);
115856
 
    goto exprtest_out;
115857
 
  }
115858
 
 
115859
 
  rc = pModule->xCreate(0, 0, &pTokenizer);
115860
 
  assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
115861
 
  if( rc==SQLITE_NOMEM ){
115862
 
    sqlite3_result_error_nomem(context);
115863
 
    goto exprtest_out;
115864
 
  }
115865
 
  pTokenizer->pModule = pModule;
115866
 
 
115867
 
  zExpr = (const char *)sqlite3_value_text(argv[1]);
115868
 
  nExpr = sqlite3_value_bytes(argv[1]);
115869
 
  nCol = argc-2;
115870
 
  azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
115871
 
  if( !azCol ){
115872
 
    sqlite3_result_error_nomem(context);
115873
 
    goto exprtest_out;
115874
 
  }
115875
 
  for(ii=0; ii<nCol; ii++){
115876
 
    azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
115877
 
  }
115878
 
 
115879
 
  rc = sqlite3Fts3ExprParse(
115880
 
      pTokenizer, azCol, nCol, nCol, zExpr, nExpr, &pExpr
115881
 
  );
115882
 
  if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
115883
 
    sqlite3_result_error(context, "Error parsing expression", -1);
115884
 
  }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
115885
 
    sqlite3_result_error_nomem(context);
115886
 
  }else{
115887
 
    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
115888
 
    sqlite3_free(zBuf);
115889
 
  }
115890
 
 
115891
 
  sqlite3Fts3ExprFree(pExpr);
115892
 
 
115893
 
exprtest_out:
115894
 
  if( pModule && pTokenizer ){
115895
 
    rc = pModule->xDestroy(pTokenizer);
115896
 
  }
115897
 
  sqlite3_free(azCol);
115898
 
}
115899
 
 
115900
 
/*
115901
 
** Register the query expression parser test function fts3_exprtest() 
115902
 
** with database connection db. 
115903
 
*/
115904
 
SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
115905
 
  return sqlite3_create_function(
115906
 
      db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
115907
 
  );
115908
 
}
115909
 
 
115910
 
#endif
115911
 
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
115912
 
 
115913
 
/************** End of fts3_expr.c *******************************************/
115914
 
/************** Begin file fts3_hash.c ***************************************/
115915
 
/*
115916
 
** 2001 September 22
115917
 
**
115918
 
** The author disclaims copyright to this source code.  In place of
115919
 
** a legal notice, here is a blessing:
115920
 
**
115921
 
**    May you do good and not evil.
115922
 
**    May you find forgiveness for yourself and forgive others.
115923
 
**    May you share freely, never taking more than you give.
115924
 
**
115925
 
*************************************************************************
115926
 
** This is the implementation of generic hash-tables used in SQLite.
115927
 
** We've modified it slightly to serve as a standalone hash table
115928
 
** implementation for the full-text indexing module.
115929
 
*/
115930
 
 
115931
 
/*
115932
 
** The code in this file is only compiled if:
115933
 
**
115934
 
**     * The FTS3 module is being built as an extension
115935
 
**       (in which case SQLITE_CORE is not defined), or
115936
 
**
115937
 
**     * The FTS3 module is being built into the core of
115938
 
**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
115939
 
*/
115940
 
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
115941
 
 
115942
 
 
115943
 
 
115944
 
/*
115945
 
** Malloc and Free functions
115946
 
*/
115947
 
static void *fts3HashMalloc(int n){
115948
 
  void *p = sqlite3_malloc(n);
115949
 
  if( p ){
115950
 
    memset(p, 0, n);
115951
 
  }
115952
 
  return p;
115953
 
}
115954
 
static void fts3HashFree(void *p){
115955
 
  sqlite3_free(p);
115956
 
}
115957
 
 
115958
 
/* Turn bulk memory into a hash table object by initializing the
115959
 
** fields of the Hash structure.
115960
 
**
115961
 
** "pNew" is a pointer to the hash table that is to be initialized.
115962
 
** keyClass is one of the constants 
115963
 
** FTS3_HASH_BINARY or FTS3_HASH_STRING.  The value of keyClass 
115964
 
** determines what kind of key the hash table will use.  "copyKey" is
115965
 
** true if the hash table should make its own private copy of keys and
115966
 
** false if it should just use the supplied pointer.
115967
 
*/
115968
 
SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
115969
 
  assert( pNew!=0 );
115970
 
  assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
115971
 
  pNew->keyClass = keyClass;
115972
 
  pNew->copyKey = copyKey;
115973
 
  pNew->first = 0;
115974
 
  pNew->count = 0;
115975
 
  pNew->htsize = 0;
115976
 
  pNew->ht = 0;
115977
 
}
115978
 
 
115979
 
/* Remove all entries from a hash table.  Reclaim all memory.
115980
 
** Call this routine to delete a hash table or to reset a hash table
115981
 
** to the empty state.
115982
 
*/
115983
 
SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
115984
 
  Fts3HashElem *elem;         /* For looping over all elements of the table */
115985
 
 
115986
 
  assert( pH!=0 );
115987
 
  elem = pH->first;
115988
 
  pH->first = 0;
115989
 
  fts3HashFree(pH->ht);
115990
 
  pH->ht = 0;
115991
 
  pH->htsize = 0;
115992
 
  while( elem ){
115993
 
    Fts3HashElem *next_elem = elem->next;
115994
 
    if( pH->copyKey && elem->pKey ){
115995
 
      fts3HashFree(elem->pKey);
115996
 
    }
115997
 
    fts3HashFree(elem);
115998
 
    elem = next_elem;
115999
 
  }
116000
 
  pH->count = 0;
116001
 
}
116002
 
 
116003
 
/*
116004
 
** Hash and comparison functions when the mode is FTS3_HASH_STRING
116005
 
*/
116006
 
static int fts3StrHash(const void *pKey, int nKey){
116007
 
  const char *z = (const char *)pKey;
116008
 
  int h = 0;
116009
 
  if( nKey<=0 ) nKey = (int) strlen(z);
116010
 
  while( nKey > 0  ){
116011
 
    h = (h<<3) ^ h ^ *z++;
116012
 
    nKey--;
116013
 
  }
116014
 
  return h & 0x7fffffff;
116015
 
}
116016
 
static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
116017
 
  if( n1!=n2 ) return 1;
116018
 
  return strncmp((const char*)pKey1,(const char*)pKey2,n1);
116019
 
}
116020
 
 
116021
 
/*
116022
 
** Hash and comparison functions when the mode is FTS3_HASH_BINARY
116023
 
*/
116024
 
static int fts3BinHash(const void *pKey, int nKey){
116025
 
  int h = 0;
116026
 
  const char *z = (const char *)pKey;
116027
 
  while( nKey-- > 0 ){
116028
 
    h = (h<<3) ^ h ^ *(z++);
116029
 
  }
116030
 
  return h & 0x7fffffff;
116031
 
}
116032
 
static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
116033
 
  if( n1!=n2 ) return 1;
116034
 
  return memcmp(pKey1,pKey2,n1);
116035
 
}
116036
 
 
116037
 
/*
116038
 
** Return a pointer to the appropriate hash function given the key class.
116039
 
**
116040
 
** The C syntax in this function definition may be unfamilar to some 
116041
 
** programmers, so we provide the following additional explanation:
116042
 
**
116043
 
** The name of the function is "ftsHashFunction".  The function takes a
116044
 
** single parameter "keyClass".  The return value of ftsHashFunction()
116045
 
** is a pointer to another function.  Specifically, the return value
116046
 
** of ftsHashFunction() is a pointer to a function that takes two parameters
116047
 
** with types "const void*" and "int" and returns an "int".
116048
 
*/
116049
 
static int (*ftsHashFunction(int keyClass))(const void*,int){
116050
 
  if( keyClass==FTS3_HASH_STRING ){
116051
 
    return &fts3StrHash;
116052
 
  }else{
116053
 
    assert( keyClass==FTS3_HASH_BINARY );
116054
 
    return &fts3BinHash;
116055
 
  }
116056
 
}
116057
 
 
116058
 
/*
116059
 
** Return a pointer to the appropriate hash function given the key class.
116060
 
**
116061
 
** For help in interpreted the obscure C code in the function definition,
116062
 
** see the header comment on the previous function.
116063
 
*/
116064
 
static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
116065
 
  if( keyClass==FTS3_HASH_STRING ){
116066
 
    return &fts3StrCompare;
116067
 
  }else{
116068
 
    assert( keyClass==FTS3_HASH_BINARY );
116069
 
    return &fts3BinCompare;
116070
 
  }
116071
 
}
116072
 
 
116073
 
/* Link an element into the hash table
116074
 
*/
116075
 
static void fts3HashInsertElement(
116076
 
  Fts3Hash *pH,            /* The complete hash table */
116077
 
  struct _fts3ht *pEntry,  /* The entry into which pNew is inserted */
116078
 
  Fts3HashElem *pNew       /* The element to be inserted */
116079
 
){
116080
 
  Fts3HashElem *pHead;     /* First element already in pEntry */
116081
 
  pHead = pEntry->chain;
116082
 
  if( pHead ){
116083
 
    pNew->next = pHead;
116084
 
    pNew->prev = pHead->prev;
116085
 
    if( pHead->prev ){ pHead->prev->next = pNew; }
116086
 
    else             { pH->first = pNew; }
116087
 
    pHead->prev = pNew;
116088
 
  }else{
116089
 
    pNew->next = pH->first;
116090
 
    if( pH->first ){ pH->first->prev = pNew; }
116091
 
    pNew->prev = 0;
116092
 
    pH->first = pNew;
116093
 
  }
116094
 
  pEntry->count++;
116095
 
  pEntry->chain = pNew;
116096
 
}
116097
 
 
116098
 
 
116099
 
/* Resize the hash table so that it cantains "new_size" buckets.
116100
 
** "new_size" must be a power of 2.  The hash table might fail 
116101
 
** to resize if sqliteMalloc() fails.
116102
 
**
116103
 
** Return non-zero if a memory allocation error occurs.
116104
 
*/
116105
 
static int fts3Rehash(Fts3Hash *pH, int new_size){
116106
 
  struct _fts3ht *new_ht;          /* The new hash table */
116107
 
  Fts3HashElem *elem, *next_elem;  /* For looping over existing elements */
116108
 
  int (*xHash)(const void*,int);   /* The hash function */
116109
 
 
116110
 
  assert( (new_size & (new_size-1))==0 );
116111
 
  new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
116112
 
  if( new_ht==0 ) return 1;
116113
 
  fts3HashFree(pH->ht);
116114
 
  pH->ht = new_ht;
116115
 
  pH->htsize = new_size;
116116
 
  xHash = ftsHashFunction(pH->keyClass);
116117
 
  for(elem=pH->first, pH->first=0; elem; elem = next_elem){
116118
 
    int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
116119
 
    next_elem = elem->next;
116120
 
    fts3HashInsertElement(pH, &new_ht[h], elem);
116121
 
  }
116122
 
  return 0;
116123
 
}
116124
 
 
116125
 
/* This function (for internal use only) locates an element in an
116126
 
** hash table that matches the given key.  The hash for this key has
116127
 
** already been computed and is passed as the 4th parameter.
116128
 
*/
116129
 
static Fts3HashElem *fts3FindElementByHash(
116130
 
  const Fts3Hash *pH, /* The pH to be searched */
116131
 
  const void *pKey,   /* The key we are searching for */
116132
 
  int nKey,
116133
 
  int h               /* The hash for this key. */
116134
 
){
116135
 
  Fts3HashElem *elem;            /* Used to loop thru the element list */
116136
 
  int count;                     /* Number of elements left to test */
116137
 
  int (*xCompare)(const void*,int,const void*,int);  /* comparison function */
116138
 
 
116139
 
  if( pH->ht ){
116140
 
    struct _fts3ht *pEntry = &pH->ht[h];
116141
 
    elem = pEntry->chain;
116142
 
    count = pEntry->count;
116143
 
    xCompare = ftsCompareFunction(pH->keyClass);
116144
 
    while( count-- && elem ){
116145
 
      if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ 
116146
 
        return elem;
116147
 
      }
116148
 
      elem = elem->next;
116149
 
    }
116150
 
  }
116151
 
  return 0;
116152
 
}
116153
 
 
116154
 
/* Remove a single entry from the hash table given a pointer to that
116155
 
** element and a hash on the element's key.
116156
 
*/
116157
 
static void fts3RemoveElementByHash(
116158
 
  Fts3Hash *pH,         /* The pH containing "elem" */
116159
 
  Fts3HashElem* elem,   /* The element to be removed from the pH */
116160
 
  int h                 /* Hash value for the element */
116161
 
){
116162
 
  struct _fts3ht *pEntry;
116163
 
  if( elem->prev ){
116164
 
    elem->prev->next = elem->next; 
116165
 
  }else{
116166
 
    pH->first = elem->next;
116167
 
  }
116168
 
  if( elem->next ){
116169
 
    elem->next->prev = elem->prev;
116170
 
  }
116171
 
  pEntry = &pH->ht[h];
116172
 
  if( pEntry->chain==elem ){
116173
 
    pEntry->chain = elem->next;
116174
 
  }
116175
 
  pEntry->count--;
116176
 
  if( pEntry->count<=0 ){
116177
 
    pEntry->chain = 0;
116178
 
  }
116179
 
  if( pH->copyKey && elem->pKey ){
116180
 
    fts3HashFree(elem->pKey);
116181
 
  }
116182
 
  fts3HashFree( elem );
116183
 
  pH->count--;
116184
 
  if( pH->count<=0 ){
116185
 
    assert( pH->first==0 );
116186
 
    assert( pH->count==0 );
116187
 
    fts3HashClear(pH);
116188
 
  }
116189
 
}
116190
 
 
116191
 
SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
116192
 
  const Fts3Hash *pH, 
116193
 
  const void *pKey, 
116194
 
  int nKey
116195
 
){
116196
 
  int h;                          /* A hash on key */
116197
 
  int (*xHash)(const void*,int);  /* The hash function */
116198
 
 
116199
 
  if( pH==0 || pH->ht==0 ) return 0;
116200
 
  xHash = ftsHashFunction(pH->keyClass);
116201
 
  assert( xHash!=0 );
116202
 
  h = (*xHash)(pKey,nKey);
116203
 
  assert( (pH->htsize & (pH->htsize-1))==0 );
116204
 
  return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
116205
 
}
116206
 
 
116207
 
/* 
116208
 
** Attempt to locate an element of the hash table pH with a key
116209
 
** that matches pKey,nKey.  Return the data for this element if it is
116210
 
** found, or NULL if there is no match.
116211
 
*/
116212
 
SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
116213
 
  Fts3HashElem *pElem;            /* The element that matches key (if any) */
116214
 
 
116215
 
  pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
116216
 
  return pElem ? pElem->data : 0;
116217
 
}
116218
 
 
116219
 
/* Insert an element into the hash table pH.  The key is pKey,nKey
116220
 
** and the data is "data".
116221
 
**
116222
 
** If no element exists with a matching key, then a new
116223
 
** element is created.  A copy of the key is made if the copyKey
116224
 
** flag is set.  NULL is returned.
116225
 
**
116226
 
** If another element already exists with the same key, then the
116227
 
** new data replaces the old data and the old data is returned.
116228
 
** The key is not copied in this instance.  If a malloc fails, then
116229
 
** the new data is returned and the hash table is unchanged.
116230
 
**
116231
 
** If the "data" parameter to this function is NULL, then the
116232
 
** element corresponding to "key" is removed from the hash table.
116233
 
*/
116234
 
SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
116235
 
  Fts3Hash *pH,        /* The hash table to insert into */
116236
 
  const void *pKey,    /* The key */
116237
 
  int nKey,            /* Number of bytes in the key */
116238
 
  void *data           /* The data */
116239
 
){
116240
 
  int hraw;                 /* Raw hash value of the key */
116241
 
  int h;                    /* the hash of the key modulo hash table size */
116242
 
  Fts3HashElem *elem;       /* Used to loop thru the element list */
116243
 
  Fts3HashElem *new_elem;   /* New element added to the pH */
116244
 
  int (*xHash)(const void*,int);  /* The hash function */
116245
 
 
116246
 
  assert( pH!=0 );
116247
 
  xHash = ftsHashFunction(pH->keyClass);
116248
 
  assert( xHash!=0 );
116249
 
  hraw = (*xHash)(pKey, nKey);
116250
 
  assert( (pH->htsize & (pH->htsize-1))==0 );
116251
 
  h = hraw & (pH->htsize-1);
116252
 
  elem = fts3FindElementByHash(pH,pKey,nKey,h);
116253
 
  if( elem ){
116254
 
    void *old_data = elem->data;
116255
 
    if( data==0 ){
116256
 
      fts3RemoveElementByHash(pH,elem,h);
116257
 
    }else{
116258
 
      elem->data = data;
116259
 
    }
116260
 
    return old_data;
116261
 
  }
116262
 
  if( data==0 ) return 0;
116263
 
  if( (pH->htsize==0 && fts3Rehash(pH,8))
116264
 
   || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
116265
 
  ){
116266
 
    pH->count = 0;
116267
 
    return data;
116268
 
  }
116269
 
  assert( pH->htsize>0 );
116270
 
  new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
116271
 
  if( new_elem==0 ) return data;
116272
 
  if( pH->copyKey && pKey!=0 ){
116273
 
    new_elem->pKey = fts3HashMalloc( nKey );
116274
 
    if( new_elem->pKey==0 ){
116275
 
      fts3HashFree(new_elem);
116276
 
      return data;
116277
 
    }
116278
 
    memcpy((void*)new_elem->pKey, pKey, nKey);
116279
 
  }else{
116280
 
    new_elem->pKey = (void*)pKey;
116281
 
  }
116282
 
  new_elem->nKey = nKey;
116283
 
  pH->count++;
116284
 
  assert( pH->htsize>0 );
116285
 
  assert( (pH->htsize & (pH->htsize-1))==0 );
116286
 
  h = hraw & (pH->htsize-1);
116287
 
  fts3HashInsertElement(pH, &pH->ht[h], new_elem);
116288
 
  new_elem->data = data;
116289
 
  return 0;
116290
 
}
116291
 
 
116292
 
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
116293
 
 
116294
 
/************** End of fts3_hash.c *******************************************/
116295
 
/************** Begin file fts3_porter.c *************************************/
116296
 
/*
116297
 
** 2006 September 30
116298
 
**
116299
 
** The author disclaims copyright to this source code.  In place of
116300
 
** a legal notice, here is a blessing:
116301
 
**
116302
 
**    May you do good and not evil.
116303
 
**    May you find forgiveness for yourself and forgive others.
116304
 
**    May you share freely, never taking more than you give.
116305
 
**
116306
 
*************************************************************************
116307
 
** Implementation of the full-text-search tokenizer that implements
116308
 
** a Porter stemmer.
116309
 
*/
116310
 
 
116311
 
/*
116312
 
** The code in this file is only compiled if:
116313
 
**
116314
 
**     * The FTS3 module is being built as an extension
116315
 
**       (in which case SQLITE_CORE is not defined), or
116316
 
**
116317
 
**     * The FTS3 module is being built into the core of
116318
 
**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
116319
 
*/
116320
 
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
116321
 
 
116322
 
 
116323
 
 
116324
 
 
116325
 
/*
116326
 
** Class derived from sqlite3_tokenizer
116327
 
*/
116328
 
typedef struct porter_tokenizer {
116329
 
  sqlite3_tokenizer base;      /* Base class */
116330
 
} porter_tokenizer;
116331
 
 
116332
 
/*
116333
 
** Class derived from sqlit3_tokenizer_cursor
116334
 
*/
116335
 
typedef struct porter_tokenizer_cursor {
116336
 
  sqlite3_tokenizer_cursor base;
116337
 
  const char *zInput;          /* input we are tokenizing */
116338
 
  int nInput;                  /* size of the input */
116339
 
  int iOffset;                 /* current position in zInput */
116340
 
  int iToken;                  /* index of next token to be returned */
116341
 
  char *zToken;                /* storage for current token */
116342
 
  int nAllocated;              /* space allocated to zToken buffer */
116343
 
} porter_tokenizer_cursor;
116344
 
 
116345
 
 
116346
 
/*
116347
 
** Create a new tokenizer instance.
116348
 
*/
116349
 
static int porterCreate(
116350
 
  int argc, const char * const *argv,
116351
 
  sqlite3_tokenizer **ppTokenizer
116352
 
){
116353
 
  porter_tokenizer *t;
116354
 
 
116355
 
  UNUSED_PARAMETER(argc);
116356
 
  UNUSED_PARAMETER(argv);
116357
 
 
116358
 
  t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
116359
 
  if( t==NULL ) return SQLITE_NOMEM;
116360
 
  memset(t, 0, sizeof(*t));
116361
 
  *ppTokenizer = &t->base;
116362
 
  return SQLITE_OK;
116363
 
}
116364
 
 
116365
 
/*
116366
 
** Destroy a tokenizer
116367
 
*/
116368
 
static int porterDestroy(sqlite3_tokenizer *pTokenizer){
116369
 
  sqlite3_free(pTokenizer);
116370
 
  return SQLITE_OK;
116371
 
}
116372
 
 
116373
 
/*
116374
 
** Prepare to begin tokenizing a particular string.  The input
116375
 
** string to be tokenized is zInput[0..nInput-1].  A cursor
116376
 
** used to incrementally tokenize this string is returned in 
116377
 
** *ppCursor.
116378
 
*/
116379
 
static int porterOpen(
116380
 
  sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
116381
 
  const char *zInput, int nInput,        /* String to be tokenized */
116382
 
  sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
116383
 
){
116384
 
  porter_tokenizer_cursor *c;
116385
 
 
116386
 
  UNUSED_PARAMETER(pTokenizer);
116387
 
 
116388
 
  c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
116389
 
  if( c==NULL ) return SQLITE_NOMEM;
116390
 
 
116391
 
  c->zInput = zInput;
116392
 
  if( zInput==0 ){
116393
 
    c->nInput = 0;
116394
 
  }else if( nInput<0 ){
116395
 
    c->nInput = (int)strlen(zInput);
116396
 
  }else{
116397
 
    c->nInput = nInput;
116398
 
  }
116399
 
  c->iOffset = 0;                 /* start tokenizing at the beginning */
116400
 
  c->iToken = 0;
116401
 
  c->zToken = NULL;               /* no space allocated, yet. */
116402
 
  c->nAllocated = 0;
116403
 
 
116404
 
  *ppCursor = &c->base;
116405
 
  return SQLITE_OK;
116406
 
}
116407
 
 
116408
 
/*
116409
 
** Close a tokenization cursor previously opened by a call to
116410
 
** porterOpen() above.
116411
 
*/
116412
 
static int porterClose(sqlite3_tokenizer_cursor *pCursor){
116413
 
  porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
116414
 
  sqlite3_free(c->zToken);
116415
 
  sqlite3_free(c);
116416
 
  return SQLITE_OK;
116417
 
}
116418
 
/*
116419
 
** Vowel or consonant
116420
 
*/
116421
 
static const char cType[] = {
116422
 
   0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
116423
 
   1, 1, 1, 2, 1
116424
 
};
116425
 
 
116426
 
/*
116427
 
** isConsonant() and isVowel() determine if their first character in
116428
 
** the string they point to is a consonant or a vowel, according
116429
 
** to Porter ruls.  
116430
 
**
116431
 
** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
116432
 
** 'Y' is a consonant unless it follows another consonant,
116433
 
** in which case it is a vowel.
116434
 
**
116435
 
** In these routine, the letters are in reverse order.  So the 'y' rule
116436
 
** is that 'y' is a consonant unless it is followed by another
116437
 
** consonent.
116438
 
*/
116439
 
static int isVowel(const char*);
116440
 
static int isConsonant(const char *z){
116441
 
  int j;
116442
 
  char x = *z;
116443
 
  if( x==0 ) return 0;
116444
 
  assert( x>='a' && x<='z' );
116445
 
  j = cType[x-'a'];
116446
 
  if( j<2 ) return j;
116447
 
  return z[1]==0 || isVowel(z + 1);
116448
 
}
116449
 
static int isVowel(const char *z){
116450
 
  int j;
116451
 
  char x = *z;
116452
 
  if( x==0 ) return 0;
116453
 
  assert( x>='a' && x<='z' );
116454
 
  j = cType[x-'a'];
116455
 
  if( j<2 ) return 1-j;
116456
 
  return isConsonant(z + 1);
116457
 
}
116458
 
 
116459
 
/*
116460
 
** Let any sequence of one or more vowels be represented by V and let
116461
 
** C be sequence of one or more consonants.  Then every word can be
116462
 
** represented as:
116463
 
**
116464
 
**           [C] (VC){m} [V]
116465
 
**
116466
 
** In prose:  A word is an optional consonant followed by zero or
116467
 
** vowel-consonant pairs followed by an optional vowel.  "m" is the
116468
 
** number of vowel consonant pairs.  This routine computes the value
116469
 
** of m for the first i bytes of a word.
116470
 
**
116471
 
** Return true if the m-value for z is 1 or more.  In other words,
116472
 
** return true if z contains at least one vowel that is followed
116473
 
** by a consonant.
116474
 
**
116475
 
** In this routine z[] is in reverse order.  So we are really looking
116476
 
** for an instance of of a consonant followed by a vowel.
116477
 
*/
116478
 
static int m_gt_0(const char *z){
116479
 
  while( isVowel(z) ){ z++; }
116480
 
  if( *z==0 ) return 0;
116481
 
  while( isConsonant(z) ){ z++; }
116482
 
  return *z!=0;
116483
 
}
116484
 
 
116485
 
/* Like mgt0 above except we are looking for a value of m which is
116486
 
** exactly 1
116487
 
*/
116488
 
static int m_eq_1(const char *z){
116489
 
  while( isVowel(z) ){ z++; }
116490
 
  if( *z==0 ) return 0;
116491
 
  while( isConsonant(z) ){ z++; }
116492
 
  if( *z==0 ) return 0;
116493
 
  while( isVowel(z) ){ z++; }
116494
 
  if( *z==0 ) return 1;
116495
 
  while( isConsonant(z) ){ z++; }
116496
 
  return *z==0;
116497
 
}
116498
 
 
116499
 
/* Like mgt0 above except we are looking for a value of m>1 instead
116500
 
** or m>0
116501
 
*/
116502
 
static int m_gt_1(const char *z){
116503
 
  while( isVowel(z) ){ z++; }
116504
 
  if( *z==0 ) return 0;
116505
 
  while( isConsonant(z) ){ z++; }
116506
 
  if( *z==0 ) return 0;
116507
 
  while( isVowel(z) ){ z++; }
116508
 
  if( *z==0 ) return 0;
116509
 
  while( isConsonant(z) ){ z++; }
116510
 
  return *z!=0;
116511
 
}
116512
 
 
116513
 
/*
116514
 
** Return TRUE if there is a vowel anywhere within z[0..n-1]
116515
 
*/
116516
 
static int hasVowel(const char *z){
116517
 
  while( isConsonant(z) ){ z++; }
116518
 
  return *z!=0;
116519
 
}
116520
 
 
116521
 
/*
116522
 
** Return TRUE if the word ends in a double consonant.
116523
 
**
116524
 
** The text is reversed here. So we are really looking at
116525
 
** the first two characters of z[].
116526
 
*/
116527
 
static int doubleConsonant(const char *z){
116528
 
  return isConsonant(z) && z[0]==z[1];
116529
 
}
116530
 
 
116531
 
/*
116532
 
** Return TRUE if the word ends with three letters which
116533
 
** are consonant-vowel-consonent and where the final consonant
116534
 
** is not 'w', 'x', or 'y'.
116535
 
**
116536
 
** The word is reversed here.  So we are really checking the
116537
 
** first three letters and the first one cannot be in [wxy].
116538
 
*/
116539
 
static int star_oh(const char *z){
116540
 
  return
116541
 
    isConsonant(z) &&
116542
 
    z[0]!='w' && z[0]!='x' && z[0]!='y' &&
116543
 
    isVowel(z+1) &&
116544
 
    isConsonant(z+2);
116545
 
}
116546
 
 
116547
 
/*
116548
 
** If the word ends with zFrom and xCond() is true for the stem
116549
 
** of the word that preceeds the zFrom ending, then change the 
116550
 
** ending to zTo.
116551
 
**
116552
 
** The input word *pz and zFrom are both in reverse order.  zTo
116553
 
** is in normal order. 
116554
 
**
116555
 
** Return TRUE if zFrom matches.  Return FALSE if zFrom does not
116556
 
** match.  Not that TRUE is returned even if xCond() fails and
116557
 
** no substitution occurs.
116558
 
*/
116559
 
static int stem(
116560
 
  char **pz,             /* The word being stemmed (Reversed) */
116561
 
  const char *zFrom,     /* If the ending matches this... (Reversed) */
116562
 
  const char *zTo,       /* ... change the ending to this (not reversed) */
116563
 
  int (*xCond)(const char*)   /* Condition that must be true */
116564
 
){
116565
 
  char *z = *pz;
116566
 
  while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
116567
 
  if( *zFrom!=0 ) return 0;
116568
 
  if( xCond && !xCond(z) ) return 1;
116569
 
  while( *zTo ){
116570
 
    *(--z) = *(zTo++);
116571
 
  }
116572
 
  *pz = z;
116573
 
  return 1;
116574
 
}
116575
 
 
116576
 
/*
116577
 
** This is the fallback stemmer used when the porter stemmer is
116578
 
** inappropriate.  The input word is copied into the output with
116579
 
** US-ASCII case folding.  If the input word is too long (more
116580
 
** than 20 bytes if it contains no digits or more than 6 bytes if
116581
 
** it contains digits) then word is truncated to 20 or 6 bytes
116582
 
** by taking 10 or 3 bytes from the beginning and end.
116583
 
*/
116584
 
static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
116585
 
  int i, mx, j;
116586
 
  int hasDigit = 0;
116587
 
  for(i=0; i<nIn; i++){
116588
 
    char c = zIn[i];
116589
 
    if( c>='A' && c<='Z' ){
116590
 
      zOut[i] = c - 'A' + 'a';
116591
 
    }else{
116592
 
      if( c>='0' && c<='9' ) hasDigit = 1;
116593
 
      zOut[i] = c;
116594
 
    }
116595
 
  }
116596
 
  mx = hasDigit ? 3 : 10;
116597
 
  if( nIn>mx*2 ){
116598
 
    for(j=mx, i=nIn-mx; i<nIn; i++, j++){
116599
 
      zOut[j] = zOut[i];
116600
 
    }
116601
 
    i = j;
116602
 
  }
116603
 
  zOut[i] = 0;
116604
 
  *pnOut = i;
116605
 
}
116606
 
 
116607
 
 
116608
 
/*
116609
 
** Stem the input word zIn[0..nIn-1].  Store the output in zOut.
116610
 
** zOut is at least big enough to hold nIn bytes.  Write the actual
116611
 
** size of the output word (exclusive of the '\0' terminator) into *pnOut.
116612
 
**
116613
 
** Any upper-case characters in the US-ASCII character set ([A-Z])
116614
 
** are converted to lower case.  Upper-case UTF characters are
116615
 
** unchanged.
116616
 
**
116617
 
** Words that are longer than about 20 bytes are stemmed by retaining
116618
 
** a few bytes from the beginning and the end of the word.  If the
116619
 
** word contains digits, 3 bytes are taken from the beginning and
116620
 
** 3 bytes from the end.  For long words without digits, 10 bytes
116621
 
** are taken from each end.  US-ASCII case folding still applies.
116622
 
** 
116623
 
** If the input word contains not digits but does characters not 
116624
 
** in [a-zA-Z] then no stemming is attempted and this routine just 
116625
 
** copies the input into the input into the output with US-ASCII
116626
 
** case folding.
116627
 
**
116628
 
** Stemming never increases the length of the word.  So there is
116629
 
** no chance of overflowing the zOut buffer.
116630
 
*/
116631
 
static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
116632
 
  int i, j;
116633
 
  char zReverse[28];
116634
 
  char *z, *z2;
116635
 
  if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
116636
 
    /* The word is too big or too small for the porter stemmer.
116637
 
    ** Fallback to the copy stemmer */
116638
 
    copy_stemmer(zIn, nIn, zOut, pnOut);
116639
 
    return;
116640
 
  }
116641
 
  for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
116642
 
    char c = zIn[i];
116643
 
    if( c>='A' && c<='Z' ){
116644
 
      zReverse[j] = c + 'a' - 'A';
116645
 
    }else if( c>='a' && c<='z' ){
116646
 
      zReverse[j] = c;
116647
 
    }else{
116648
 
      /* The use of a character not in [a-zA-Z] means that we fallback
116649
 
      ** to the copy stemmer */
116650
 
      copy_stemmer(zIn, nIn, zOut, pnOut);
116651
 
      return;
116652
 
    }
116653
 
  }
116654
 
  memset(&zReverse[sizeof(zReverse)-5], 0, 5);
116655
 
  z = &zReverse[j+1];
116656
 
 
116657
 
 
116658
 
  /* Step 1a */
116659
 
  if( z[0]=='s' ){
116660
 
    if(
116661
 
     !stem(&z, "sess", "ss", 0) &&
116662
 
     !stem(&z, "sei", "i", 0)  &&
116663
 
     !stem(&z, "ss", "ss", 0)
116664
 
    ){
116665
 
      z++;
116666
 
    }
116667
 
  }
116668
 
 
116669
 
  /* Step 1b */  
116670
 
  z2 = z;
116671
 
  if( stem(&z, "dee", "ee", m_gt_0) ){
116672
 
    /* Do nothing.  The work was all in the test */
116673
 
  }else if( 
116674
 
     (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
116675
 
      && z!=z2
116676
 
  ){
116677
 
     if( stem(&z, "ta", "ate", 0) ||
116678
 
         stem(&z, "lb", "ble", 0) ||
116679
 
         stem(&z, "zi", "ize", 0) ){
116680
 
       /* Do nothing.  The work was all in the test */
116681
 
     }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
116682
 
       z++;
116683
 
     }else if( m_eq_1(z) && star_oh(z) ){
116684
 
       *(--z) = 'e';
116685
 
     }
116686
 
  }
116687
 
 
116688
 
  /* Step 1c */
116689
 
  if( z[0]=='y' && hasVowel(z+1) ){
116690
 
    z[0] = 'i';
116691
 
  }
116692
 
 
116693
 
  /* Step 2 */
116694
 
  switch( z[1] ){
116695
 
   case 'a':
116696
 
     stem(&z, "lanoita", "ate", m_gt_0) ||
116697
 
     stem(&z, "lanoit", "tion", m_gt_0);
116698
 
     break;
116699
 
   case 'c':
116700
 
     stem(&z, "icne", "ence", m_gt_0) ||
116701
 
     stem(&z, "icna", "ance", m_gt_0);
116702
 
     break;
116703
 
   case 'e':
116704
 
     stem(&z, "rezi", "ize", m_gt_0);
116705
 
     break;
116706
 
   case 'g':
116707
 
     stem(&z, "igol", "log", m_gt_0);
116708
 
     break;
116709
 
   case 'l':
116710
 
     stem(&z, "ilb", "ble", m_gt_0) ||
116711
 
     stem(&z, "illa", "al", m_gt_0) ||
116712
 
     stem(&z, "iltne", "ent", m_gt_0) ||
116713
 
     stem(&z, "ile", "e", m_gt_0) ||
116714
 
     stem(&z, "ilsuo", "ous", m_gt_0);
116715
 
     break;
116716
 
   case 'o':
116717
 
     stem(&z, "noitazi", "ize", m_gt_0) ||
116718
 
     stem(&z, "noita", "ate", m_gt_0) ||
116719
 
     stem(&z, "rota", "ate", m_gt_0);
116720
 
     break;
116721
 
   case 's':
116722
 
     stem(&z, "msila", "al", m_gt_0) ||
116723
 
     stem(&z, "ssenevi", "ive", m_gt_0) ||
116724
 
     stem(&z, "ssenluf", "ful", m_gt_0) ||
116725
 
     stem(&z, "ssensuo", "ous", m_gt_0);
116726
 
     break;
116727
 
   case 't':
116728
 
     stem(&z, "itila", "al", m_gt_0) ||
116729
 
     stem(&z, "itivi", "ive", m_gt_0) ||
116730
 
     stem(&z, "itilib", "ble", m_gt_0);
116731
 
     break;
116732
 
  }
116733
 
 
116734
 
  /* Step 3 */
116735
 
  switch( z[0] ){
116736
 
   case 'e':
116737
 
     stem(&z, "etaci", "ic", m_gt_0) ||
116738
 
     stem(&z, "evita", "", m_gt_0)   ||
116739
 
     stem(&z, "ezila", "al", m_gt_0);
116740
 
     break;
116741
 
   case 'i':
116742
 
     stem(&z, "itici", "ic", m_gt_0);
116743
 
     break;
116744
 
   case 'l':
116745
 
     stem(&z, "laci", "ic", m_gt_0) ||
116746
 
     stem(&z, "luf", "", m_gt_0);
116747
 
     break;
116748
 
   case 's':
116749
 
     stem(&z, "ssen", "", m_gt_0);
116750
 
     break;
116751
 
  }
116752
 
 
116753
 
  /* Step 4 */
116754
 
  switch( z[1] ){
116755
 
   case 'a':
116756
 
     if( z[0]=='l' && m_gt_1(z+2) ){
116757
 
       z += 2;
116758
 
     }
116759
 
     break;
116760
 
   case 'c':
116761
 
     if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e')  && m_gt_1(z+4)  ){
116762
 
       z += 4;
116763
 
     }
116764
 
     break;
116765
 
   case 'e':
116766
 
     if( z[0]=='r' && m_gt_1(z+2) ){
116767
 
       z += 2;
116768
 
     }
116769
 
     break;
116770
 
   case 'i':
116771
 
     if( z[0]=='c' && m_gt_1(z+2) ){
116772
 
       z += 2;
116773
 
     }
116774
 
     break;
116775
 
   case 'l':
116776
 
     if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
116777
 
       z += 4;
116778
 
     }
116779
 
     break;
116780
 
   case 'n':
116781
 
     if( z[0]=='t' ){
116782
 
       if( z[2]=='a' ){
116783
 
         if( m_gt_1(z+3) ){
116784
 
           z += 3;
116785
 
         }
116786
 
       }else if( z[2]=='e' ){
116787
 
         stem(&z, "tneme", "", m_gt_1) ||
116788
 
         stem(&z, "tnem", "", m_gt_1) ||
116789
 
         stem(&z, "tne", "", m_gt_1);
116790
 
       }
116791
 
     }
116792
 
     break;
116793
 
   case 'o':
116794
 
     if( z[0]=='u' ){
116795
 
       if( m_gt_1(z+2) ){
116796
 
         z += 2;
116797
 
       }
116798
 
     }else if( z[3]=='s' || z[3]=='t' ){
116799
 
       stem(&z, "noi", "", m_gt_1);
116800
 
     }
116801
 
     break;
116802
 
   case 's':
116803
 
     if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
116804
 
       z += 3;
116805
 
     }
116806
 
     break;
116807
 
   case 't':
116808
 
     stem(&z, "eta", "", m_gt_1) ||
116809
 
     stem(&z, "iti", "", m_gt_1);
116810
 
     break;
116811
 
   case 'u':
116812
 
     if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
116813
 
       z += 3;
116814
 
     }
116815
 
     break;
116816
 
   case 'v':
116817
 
   case 'z':
116818
 
     if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
116819
 
       z += 3;
116820
 
     }
116821
 
     break;
116822
 
  }
116823
 
 
116824
 
  /* Step 5a */
116825
 
  if( z[0]=='e' ){
116826
 
    if( m_gt_1(z+1) ){
116827
 
      z++;
116828
 
    }else if( m_eq_1(z+1) && !star_oh(z+1) ){
116829
 
      z++;
116830
 
    }
116831
 
  }
116832
 
 
116833
 
  /* Step 5b */
116834
 
  if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
116835
 
    z++;
116836
 
  }
116837
 
 
116838
 
  /* z[] is now the stemmed word in reverse order.  Flip it back
116839
 
  ** around into forward order and return.
116840
 
  */
116841
 
  *pnOut = i = (int)strlen(z);
116842
 
  zOut[i] = 0;
116843
 
  while( *z ){
116844
 
    zOut[--i] = *(z++);
116845
 
  }
116846
 
}
116847
 
 
116848
 
/*
116849
 
** Characters that can be part of a token.  We assume any character
116850
 
** whose value is greater than 0x80 (any UTF character) can be
116851
 
** part of a token.  In other words, delimiters all must have
116852
 
** values of 0x7f or lower.
116853
 
*/
116854
 
static const char porterIdChar[] = {
116855
 
/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
116856
 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
116857
 
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
116858
 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
116859
 
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
116860
 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
116861
 
};
116862
 
#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
116863
 
 
116864
 
/*
116865
 
** Extract the next token from a tokenization cursor.  The cursor must
116866
 
** have been opened by a prior call to porterOpen().
116867
 
*/
116868
 
static int porterNext(
116869
 
  sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by porterOpen */
116870
 
  const char **pzToken,               /* OUT: *pzToken is the token text */
116871
 
  int *pnBytes,                       /* OUT: Number of bytes in token */
116872
 
  int *piStartOffset,                 /* OUT: Starting offset of token */
116873
 
  int *piEndOffset,                   /* OUT: Ending offset of token */
116874
 
  int *piPosition                     /* OUT: Position integer of token */
116875
 
){
116876
 
  porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
116877
 
  const char *z = c->zInput;
116878
 
 
116879
 
  while( c->iOffset<c->nInput ){
116880
 
    int iStartOffset, ch;
116881
 
 
116882
 
    /* Scan past delimiter characters */
116883
 
    while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
116884
 
      c->iOffset++;
116885
 
    }
116886
 
 
116887
 
    /* Count non-delimiter characters. */
116888
 
    iStartOffset = c->iOffset;
116889
 
    while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
116890
 
      c->iOffset++;
116891
 
    }
116892
 
 
116893
 
    if( c->iOffset>iStartOffset ){
116894
 
      int n = c->iOffset-iStartOffset;
116895
 
      if( n>c->nAllocated ){
116896
 
        char *pNew;
116897
 
        c->nAllocated = n+20;
116898
 
        pNew = sqlite3_realloc(c->zToken, c->nAllocated);
116899
 
        if( !pNew ) return SQLITE_NOMEM;
116900
 
        c->zToken = pNew;
116901
 
      }
116902
 
      porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
116903
 
      *pzToken = c->zToken;
116904
 
      *piStartOffset = iStartOffset;
116905
 
      *piEndOffset = c->iOffset;
116906
 
      *piPosition = c->iToken++;
116907
 
      return SQLITE_OK;
116908
 
    }
116909
 
  }
116910
 
  return SQLITE_DONE;
116911
 
}
116912
 
 
116913
 
/*
116914
 
** The set of routines that implement the porter-stemmer tokenizer
116915
 
*/
116916
 
static const sqlite3_tokenizer_module porterTokenizerModule = {
116917
 
  0,
116918
 
  porterCreate,
116919
 
  porterDestroy,
116920
 
  porterOpen,
116921
 
  porterClose,
116922
 
  porterNext,
116923
 
};
116924
 
 
116925
 
/*
116926
 
** Allocate a new porter tokenizer.  Return a pointer to the new
116927
 
** tokenizer in *ppModule
116928
 
*/
116929
 
SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
116930
 
  sqlite3_tokenizer_module const**ppModule
116931
 
){
116932
 
  *ppModule = &porterTokenizerModule;
116933
 
}
116934
 
 
116935
 
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
116936
 
 
116937
 
/************** End of fts3_porter.c *****************************************/
116938
 
/************** Begin file fts3_tokenizer.c **********************************/
116939
 
/*
116940
 
** 2007 June 22
116941
 
**
116942
 
** The author disclaims copyright to this source code.  In place of
116943
 
** a legal notice, here is a blessing:
116944
 
**
116945
 
**    May you do good and not evil.
116946
 
**    May you find forgiveness for yourself and forgive others.
116947
 
**    May you share freely, never taking more than you give.
116948
 
**
116949
 
******************************************************************************
116950
 
**
116951
 
** This is part of an SQLite module implementing full-text search.
116952
 
** This particular file implements the generic tokenizer interface.
116953
 
*/
116954
 
 
116955
 
/*
116956
 
** The code in this file is only compiled if:
116957
 
**
116958
 
**     * The FTS3 module is being built as an extension
116959
 
**       (in which case SQLITE_CORE is not defined), or
116960
 
**
116961
 
**     * The FTS3 module is being built into the core of
116962
 
**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
116963
 
*/
116964
 
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
116965
 
 
116966
 
#ifndef SQLITE_CORE
116967
 
  SQLITE_EXTENSION_INIT1
116968
 
#endif
116969
 
 
116970
 
 
116971
 
/*
116972
 
** Implementation of the SQL scalar function for accessing the underlying 
116973
 
** hash table. This function may be called as follows:
116974
 
**
116975
 
**   SELECT <function-name>(<key-name>);
116976
 
**   SELECT <function-name>(<key-name>, <pointer>);
116977
 
**
116978
 
** where <function-name> is the name passed as the second argument
116979
 
** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
116980
 
**
116981
 
** If the <pointer> argument is specified, it must be a blob value
116982
 
** containing a pointer to be stored as the hash data corresponding
116983
 
** to the string <key-name>. If <pointer> is not specified, then
116984
 
** the string <key-name> must already exist in the has table. Otherwise,
116985
 
** an error is returned.
116986
 
**
116987
 
** Whether or not the <pointer> argument is specified, the value returned
116988
 
** is a blob containing the pointer stored as the hash data corresponding
116989
 
** to string <key-name> (after the hash-table is updated, if applicable).
116990
 
*/
116991
 
static void scalarFunc(
116992
 
  sqlite3_context *context,
116993
 
  int argc,
116994
 
  sqlite3_value **argv
116995
 
){
116996
 
  Fts3Hash *pHash;
116997
 
  void *pPtr = 0;
116998
 
  const unsigned char *zName;
116999
 
  int nName;
117000
 
 
117001
 
  assert( argc==1 || argc==2 );
117002
 
 
117003
 
  pHash = (Fts3Hash *)sqlite3_user_data(context);
117004
 
 
117005
 
  zName = sqlite3_value_text(argv[0]);
117006
 
  nName = sqlite3_value_bytes(argv[0])+1;
117007
 
 
117008
 
  if( argc==2 ){
117009
 
    void *pOld;
117010
 
    int n = sqlite3_value_bytes(argv[1]);
117011
 
    if( n!=sizeof(pPtr) ){
117012
 
      sqlite3_result_error(context, "argument type mismatch", -1);
117013
 
      return;
117014
 
    }
117015
 
    pPtr = *(void **)sqlite3_value_blob(argv[1]);
117016
 
    pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
117017
 
    if( pOld==pPtr ){
117018
 
      sqlite3_result_error(context, "out of memory", -1);
117019
 
      return;
117020
 
    }
117021
 
  }else{
117022
 
    pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
117023
 
    if( !pPtr ){
117024
 
      char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
117025
 
      sqlite3_result_error(context, zErr, -1);
117026
 
      sqlite3_free(zErr);
117027
 
      return;
117028
 
    }
117029
 
  }
117030
 
 
117031
 
  sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
117032
 
}
117033
 
 
117034
 
SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
117035
 
  static const char isFtsIdChar[] = {
117036
 
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
117037
 
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
117038
 
      0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
117039
 
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
117040
 
      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
117041
 
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
117042
 
      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
117043
 
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
117044
 
  };
117045
 
  return (c&0x80 || isFtsIdChar[(int)(c)]);
117046
 
}
117047
 
 
117048
 
SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
117049
 
  const char *z1;
117050
 
  const char *z2 = 0;
117051
 
 
117052
 
  /* Find the start of the next token. */
117053
 
  z1 = zStr;
117054
 
  while( z2==0 ){
117055
 
    char c = *z1;
117056
 
    switch( c ){
117057
 
      case '\0': return 0;        /* No more tokens here */
117058
 
      case '\'':
117059
 
      case '"':
117060
 
      case '`': {
117061
 
        z2 = z1;
117062
 
        while( *++z2 && (*z2!=c || *++z2==c) );
117063
 
        break;
117064
 
      }
117065
 
      case '[':
117066
 
        z2 = &z1[1];
117067
 
        while( *z2 && z2[0]!=']' ) z2++;
117068
 
        if( *z2 ) z2++;
117069
 
        break;
117070
 
 
117071
 
      default:
117072
 
        if( sqlite3Fts3IsIdChar(*z1) ){
117073
 
          z2 = &z1[1];
117074
 
          while( sqlite3Fts3IsIdChar(*z2) ) z2++;
117075
 
        }else{
117076
 
          z1++;
117077
 
        }
117078
 
    }
117079
 
  }
117080
 
 
117081
 
  *pn = (int)(z2-z1);
117082
 
  return z1;
117083
 
}
117084
 
 
117085
 
SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
117086
 
  Fts3Hash *pHash,                /* Tokenizer hash table */
117087
 
  const char *zArg,               /* Tokenizer name */
117088
 
  sqlite3_tokenizer **ppTok,      /* OUT: Tokenizer (if applicable) */
117089
 
  char **pzErr                    /* OUT: Set to malloced error message */
117090
 
){
117091
 
  int rc;
117092
 
  char *z = (char *)zArg;
117093
 
  int n;
117094
 
  char *zCopy;
117095
 
  char *zEnd;                     /* Pointer to nul-term of zCopy */
117096
 
  sqlite3_tokenizer_module *m;
117097
 
 
117098
 
  zCopy = sqlite3_mprintf("%s", zArg);
117099
 
  if( !zCopy ) return SQLITE_NOMEM;
117100
 
  zEnd = &zCopy[strlen(zCopy)];
117101
 
 
117102
 
  z = (char *)sqlite3Fts3NextToken(zCopy, &n);
117103
 
  z[n] = '\0';
117104
 
  sqlite3Fts3Dequote(z);
117105
 
 
117106
 
  m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
117107
 
  if( !m ){
117108
 
    *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z);
117109
 
    rc = SQLITE_ERROR;
117110
 
  }else{
117111
 
    char const **aArg = 0;
117112
 
    int iArg = 0;
117113
 
    z = &z[n+1];
117114
 
    while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
117115
 
      int nNew = sizeof(char *)*(iArg+1);
117116
 
      char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew);
117117
 
      if( !aNew ){
117118
 
        sqlite3_free(zCopy);
117119
 
        sqlite3_free((void *)aArg);
117120
 
        return SQLITE_NOMEM;
117121
 
      }
117122
 
      aArg = aNew;
117123
 
      aArg[iArg++] = z;
117124
 
      z[n] = '\0';
117125
 
      sqlite3Fts3Dequote(z);
117126
 
      z = &z[n+1];
117127
 
    }
117128
 
    rc = m->xCreate(iArg, aArg, ppTok);
117129
 
    assert( rc!=SQLITE_OK || *ppTok );
117130
 
    if( rc!=SQLITE_OK ){
117131
 
      *pzErr = sqlite3_mprintf("unknown tokenizer");
117132
 
    }else{
117133
 
      (*ppTok)->pModule = m; 
117134
 
    }
117135
 
    sqlite3_free((void *)aArg);
117136
 
  }
117137
 
 
117138
 
  sqlite3_free(zCopy);
117139
 
  return rc;
117140
 
}
117141
 
 
117142
 
 
117143
 
#ifdef SQLITE_TEST
117144
 
 
117145
 
 
117146
 
/*
117147
 
** Implementation of a special SQL scalar function for testing tokenizers 
117148
 
** designed to be used in concert with the Tcl testing framework. This
117149
 
** function must be called with two arguments:
117150
 
**
117151
 
**   SELECT <function-name>(<key-name>, <input-string>);
117152
 
**   SELECT <function-name>(<key-name>, <pointer>);
117153
 
**
117154
 
** where <function-name> is the name passed as the second argument
117155
 
** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
117156
 
** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
117157
 
**
117158
 
** The return value is a string that may be interpreted as a Tcl
117159
 
** list. For each token in the <input-string>, three elements are
117160
 
** added to the returned list. The first is the token position, the 
117161
 
** second is the token text (folded, stemmed, etc.) and the third is the
117162
 
** substring of <input-string> associated with the token. For example, 
117163
 
** using the built-in "simple" tokenizer:
117164
 
**
117165
 
**   SELECT fts_tokenizer_test('simple', 'I don't see how');
117166
 
**
117167
 
** will return the string:
117168
 
**
117169
 
**   "{0 i I 1 dont don't 2 see see 3 how how}"
117170
 
**   
117171
 
*/
117172
 
static void testFunc(
117173
 
  sqlite3_context *context,
117174
 
  int argc,
117175
 
  sqlite3_value **argv
117176
 
){
117177
 
  Fts3Hash *pHash;
117178
 
  sqlite3_tokenizer_module *p;
117179
 
  sqlite3_tokenizer *pTokenizer = 0;
117180
 
  sqlite3_tokenizer_cursor *pCsr = 0;
117181
 
 
117182
 
  const char *zErr = 0;
117183
 
 
117184
 
  const char *zName;
117185
 
  int nName;
117186
 
  const char *zInput;
117187
 
  int nInput;
117188
 
 
117189
 
  const char *zArg = 0;
117190
 
 
117191
 
  const char *zToken;
117192
 
  int nToken;
117193
 
  int iStart;
117194
 
  int iEnd;
117195
 
  int iPos;
117196
 
 
117197
 
  Tcl_Obj *pRet;
117198
 
 
117199
 
  assert( argc==2 || argc==3 );
117200
 
 
117201
 
  nName = sqlite3_value_bytes(argv[0]);
117202
 
  zName = (const char *)sqlite3_value_text(argv[0]);
117203
 
  nInput = sqlite3_value_bytes(argv[argc-1]);
117204
 
  zInput = (const char *)sqlite3_value_text(argv[argc-1]);
117205
 
 
117206
 
  if( argc==3 ){
117207
 
    zArg = (const char *)sqlite3_value_text(argv[1]);
117208
 
  }
117209
 
 
117210
 
  pHash = (Fts3Hash *)sqlite3_user_data(context);
117211
 
  p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
117212
 
 
117213
 
  if( !p ){
117214
 
    char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
117215
 
    sqlite3_result_error(context, zErr, -1);
117216
 
    sqlite3_free(zErr);
117217
 
    return;
117218
 
  }
117219
 
 
117220
 
  pRet = Tcl_NewObj();
117221
 
  Tcl_IncrRefCount(pRet);
117222
 
 
117223
 
  if( SQLITE_OK!=p->xCreate(zArg ? 1 : 0, &zArg, &pTokenizer) ){
117224
 
    zErr = "error in xCreate()";
117225
 
    goto finish;
117226
 
  }
117227
 
  pTokenizer->pModule = p;
117228
 
  if( SQLITE_OK!=p->xOpen(pTokenizer, zInput, nInput, &pCsr) ){
117229
 
    zErr = "error in xOpen()";
117230
 
    goto finish;
117231
 
  }
117232
 
  pCsr->pTokenizer = pTokenizer;
117233
 
 
117234
 
  while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
117235
 
    Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
117236
 
    Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
117237
 
    zToken = &zInput[iStart];
117238
 
    nToken = iEnd-iStart;
117239
 
    Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
117240
 
  }
117241
 
 
117242
 
  if( SQLITE_OK!=p->xClose(pCsr) ){
117243
 
    zErr = "error in xClose()";
117244
 
    goto finish;
117245
 
  }
117246
 
  if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
117247
 
    zErr = "error in xDestroy()";
117248
 
    goto finish;
117249
 
  }
117250
 
 
117251
 
finish:
117252
 
  if( zErr ){
117253
 
    sqlite3_result_error(context, zErr, -1);
117254
 
  }else{
117255
 
    sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
117256
 
  }
117257
 
  Tcl_DecrRefCount(pRet);
117258
 
}
117259
 
 
117260
 
static
117261
 
int registerTokenizer(
117262
 
  sqlite3 *db, 
117263
 
  char *zName, 
117264
 
  const sqlite3_tokenizer_module *p
117265
 
){
117266
 
  int rc;
117267
 
  sqlite3_stmt *pStmt;
117268
 
  const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
117269
 
 
117270
 
  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
117271
 
  if( rc!=SQLITE_OK ){
117272
 
    return rc;
117273
 
  }
117274
 
 
117275
 
  sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
117276
 
  sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
117277
 
  sqlite3_step(pStmt);
117278
 
 
117279
 
  return sqlite3_finalize(pStmt);
117280
 
}
117281
 
 
117282
 
static
117283
 
int queryTokenizer(
117284
 
  sqlite3 *db, 
117285
 
  char *zName,  
117286
 
  const sqlite3_tokenizer_module **pp
117287
 
){
117288
 
  int rc;
117289
 
  sqlite3_stmt *pStmt;
117290
 
  const char zSql[] = "SELECT fts3_tokenizer(?)";
117291
 
 
117292
 
  *pp = 0;
117293
 
  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
117294
 
  if( rc!=SQLITE_OK ){
117295
 
    return rc;
117296
 
  }
117297
 
 
117298
 
  sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
117299
 
  if( SQLITE_ROW==sqlite3_step(pStmt) ){
117300
 
    if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
117301
 
      memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
117302
 
    }
117303
 
  }
117304
 
 
117305
 
  return sqlite3_finalize(pStmt);
117306
 
}
117307
 
 
117308
 
SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
117309
 
 
117310
 
/*
117311
 
** Implementation of the scalar function fts3_tokenizer_internal_test().
117312
 
** This function is used for testing only, it is not included in the
117313
 
** build unless SQLITE_TEST is defined.
117314
 
**
117315
 
** The purpose of this is to test that the fts3_tokenizer() function
117316
 
** can be used as designed by the C-code in the queryTokenizer and
117317
 
** registerTokenizer() functions above. These two functions are repeated
117318
 
** in the README.tokenizer file as an example, so it is important to
117319
 
** test them.
117320
 
**
117321
 
** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
117322
 
** function with no arguments. An assert() will fail if a problem is
117323
 
** detected. i.e.:
117324
 
**
117325
 
**     SELECT fts3_tokenizer_internal_test();
117326
 
**
117327
 
*/
117328
 
static void intTestFunc(
117329
 
  sqlite3_context *context,
117330
 
  int argc,
117331
 
  sqlite3_value **argv
117332
 
){
117333
 
  int rc;
117334
 
  const sqlite3_tokenizer_module *p1;
117335
 
  const sqlite3_tokenizer_module *p2;
117336
 
  sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
117337
 
 
117338
 
  UNUSED_PARAMETER(argc);
117339
 
  UNUSED_PARAMETER(argv);
117340
 
 
117341
 
  /* Test the query function */
117342
 
  sqlite3Fts3SimpleTokenizerModule(&p1);
117343
 
  rc = queryTokenizer(db, "simple", &p2);
117344
 
  assert( rc==SQLITE_OK );
117345
 
  assert( p1==p2 );
117346
 
  rc = queryTokenizer(db, "nosuchtokenizer", &p2);
117347
 
  assert( rc==SQLITE_ERROR );
117348
 
  assert( p2==0 );
117349
 
  assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
117350
 
 
117351
 
  /* Test the storage function */
117352
 
  rc = registerTokenizer(db, "nosuchtokenizer", p1);
117353
 
  assert( rc==SQLITE_OK );
117354
 
  rc = queryTokenizer(db, "nosuchtokenizer", &p2);
117355
 
  assert( rc==SQLITE_OK );
117356
 
  assert( p2==p1 );
117357
 
 
117358
 
  sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
117359
 
}
117360
 
 
117361
 
#endif
117362
 
 
117363
 
/*
117364
 
** Set up SQL objects in database db used to access the contents of
117365
 
** the hash table pointed to by argument pHash. The hash table must
117366
 
** been initialised to use string keys, and to take a private copy 
117367
 
** of the key when a value is inserted. i.e. by a call similar to:
117368
 
**
117369
 
**    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
117370
 
**
117371
 
** This function adds a scalar function (see header comment above
117372
 
** scalarFunc() in this file for details) and, if ENABLE_TABLE is
117373
 
** defined at compilation time, a temporary virtual table (see header 
117374
 
** comment above struct HashTableVtab) to the database schema. Both 
117375
 
** provide read/write access to the contents of *pHash.
117376
 
**
117377
 
** The third argument to this function, zName, is used as the name
117378
 
** of both the scalar and, if created, the virtual table.
117379
 
*/
117380
 
SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
117381
 
  sqlite3 *db, 
117382
 
  Fts3Hash *pHash, 
117383
 
  const char *zName
117384
 
){
117385
 
  int rc = SQLITE_OK;
117386
 
  void *p = (void *)pHash;
117387
 
  const int any = SQLITE_ANY;
117388
 
 
117389
 
#ifdef SQLITE_TEST
117390
 
  char *zTest = 0;
117391
 
  char *zTest2 = 0;
117392
 
  void *pdb = (void *)db;
117393
 
  zTest = sqlite3_mprintf("%s_test", zName);
117394
 
  zTest2 = sqlite3_mprintf("%s_internal_test", zName);
117395
 
  if( !zTest || !zTest2 ){
117396
 
    rc = SQLITE_NOMEM;
117397
 
  }
117398
 
#endif
117399
 
 
117400
 
  if( SQLITE_OK==rc ){
117401
 
    rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0);
117402
 
  }
117403
 
  if( SQLITE_OK==rc ){
117404
 
    rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
117405
 
  }
117406
 
#ifdef SQLITE_TEST
117407
 
  if( SQLITE_OK==rc ){
117408
 
    rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0);
117409
 
  }
117410
 
  if( SQLITE_OK==rc ){
117411
 
    rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0);
117412
 
  }
117413
 
  if( SQLITE_OK==rc ){
117414
 
    rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
117415
 
  }
117416
 
#endif
117417
 
 
117418
 
#ifdef SQLITE_TEST
117419
 
  sqlite3_free(zTest);
117420
 
  sqlite3_free(zTest2);
117421
 
#endif
117422
 
 
117423
 
  return rc;
117424
 
}
117425
 
 
117426
 
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
117427
 
 
117428
 
/************** End of fts3_tokenizer.c **************************************/
117429
 
/************** Begin file fts3_tokenizer1.c *********************************/
117430
 
/*
117431
 
** 2006 Oct 10
117432
 
**
117433
 
** The author disclaims copyright to this source code.  In place of
117434
 
** a legal notice, here is a blessing:
117435
 
**
117436
 
**    May you do good and not evil.
117437
 
**    May you find forgiveness for yourself and forgive others.
117438
 
**    May you share freely, never taking more than you give.
117439
 
**
117440
 
******************************************************************************
117441
 
**
117442
 
** Implementation of the "simple" full-text-search tokenizer.
117443
 
*/
117444
 
 
117445
 
/*
117446
 
** The code in this file is only compiled if:
117447
 
**
117448
 
**     * The FTS3 module is being built as an extension
117449
 
**       (in which case SQLITE_CORE is not defined), or
117450
 
**
117451
 
**     * The FTS3 module is being built into the core of
117452
 
**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
117453
 
*/
117454
 
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
117455
 
 
117456
 
 
117457
 
 
117458
 
 
117459
 
typedef struct simple_tokenizer {
117460
 
  sqlite3_tokenizer base;
117461
 
  char delim[128];             /* flag ASCII delimiters */
117462
 
} simple_tokenizer;
117463
 
 
117464
 
typedef struct simple_tokenizer_cursor {
117465
 
  sqlite3_tokenizer_cursor base;
117466
 
  const char *pInput;          /* input we are tokenizing */
117467
 
  int nBytes;                  /* size of the input */
117468
 
  int iOffset;                 /* current position in pInput */
117469
 
  int iToken;                  /* index of next token to be returned */
117470
 
  char *pToken;                /* storage for current token */
117471
 
  int nTokenAllocated;         /* space allocated to zToken buffer */
117472
 
} simple_tokenizer_cursor;
117473
 
 
117474
 
 
117475
 
static int simpleDelim(simple_tokenizer *t, unsigned char c){
117476
 
  return c<0x80 && t->delim[c];
117477
 
}
117478
 
static int fts3_isalnum(int x){
117479
 
  return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
117480
 
}
117481
 
 
117482
 
/*
117483
 
** Create a new tokenizer instance.
117484
 
*/
117485
 
static int simpleCreate(
117486
 
  int argc, const char * const *argv,
117487
 
  sqlite3_tokenizer **ppTokenizer
117488
 
){
117489
 
  simple_tokenizer *t;
117490
 
 
117491
 
  t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
117492
 
  if( t==NULL ) return SQLITE_NOMEM;
117493
 
  memset(t, 0, sizeof(*t));
117494
 
 
117495
 
  /* TODO(shess) Delimiters need to remain the same from run to run,
117496
 
  ** else we need to reindex.  One solution would be a meta-table to
117497
 
  ** track such information in the database, then we'd only want this
117498
 
  ** information on the initial create.
117499
 
  */
117500
 
  if( argc>1 ){
117501
 
    int i, n = (int)strlen(argv[1]);
117502
 
    for(i=0; i<n; i++){
117503
 
      unsigned char ch = argv[1][i];
117504
 
      /* We explicitly don't support UTF-8 delimiters for now. */
117505
 
      if( ch>=0x80 ){
117506
 
        sqlite3_free(t);
117507
 
        return SQLITE_ERROR;
117508
 
      }
117509
 
      t->delim[ch] = 1;
117510
 
    }
117511
 
  } else {
117512
 
    /* Mark non-alphanumeric ASCII characters as delimiters */
117513
 
    int i;
117514
 
    for(i=1; i<0x80; i++){
117515
 
      t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
117516
 
    }
117517
 
  }
117518
 
 
117519
 
  *ppTokenizer = &t->base;
117520
 
  return SQLITE_OK;
117521
 
}
117522
 
 
117523
 
/*
117524
 
** Destroy a tokenizer
117525
 
*/
117526
 
static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
117527
 
  sqlite3_free(pTokenizer);
117528
 
  return SQLITE_OK;
117529
 
}
117530
 
 
117531
 
/*
117532
 
** Prepare to begin tokenizing a particular string.  The input
117533
 
** string to be tokenized is pInput[0..nBytes-1].  A cursor
117534
 
** used to incrementally tokenize this string is returned in 
117535
 
** *ppCursor.
117536
 
*/
117537
 
static int simpleOpen(
117538
 
  sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
117539
 
  const char *pInput, int nBytes,        /* String to be tokenized */
117540
 
  sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
117541
 
){
117542
 
  simple_tokenizer_cursor *c;
117543
 
 
117544
 
  UNUSED_PARAMETER(pTokenizer);
117545
 
 
117546
 
  c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
117547
 
  if( c==NULL ) return SQLITE_NOMEM;
117548
 
 
117549
 
  c->pInput = pInput;
117550
 
  if( pInput==0 ){
117551
 
    c->nBytes = 0;
117552
 
  }else if( nBytes<0 ){
117553
 
    c->nBytes = (int)strlen(pInput);
117554
 
  }else{
117555
 
    c->nBytes = nBytes;
117556
 
  }
117557
 
  c->iOffset = 0;                 /* start tokenizing at the beginning */
117558
 
  c->iToken = 0;
117559
 
  c->pToken = NULL;               /* no space allocated, yet. */
117560
 
  c->nTokenAllocated = 0;
117561
 
 
117562
 
  *ppCursor = &c->base;
117563
 
  return SQLITE_OK;
117564
 
}
117565
 
 
117566
 
/*
117567
 
** Close a tokenization cursor previously opened by a call to
117568
 
** simpleOpen() above.
117569
 
*/
117570
 
static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
117571
 
  simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
117572
 
  sqlite3_free(c->pToken);
117573
 
  sqlite3_free(c);
117574
 
  return SQLITE_OK;
117575
 
}
117576
 
 
117577
 
/*
117578
 
** Extract the next token from a tokenization cursor.  The cursor must
117579
 
** have been opened by a prior call to simpleOpen().
117580
 
*/
117581
 
static int simpleNext(
117582
 
  sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
117583
 
  const char **ppToken,               /* OUT: *ppToken is the token text */
117584
 
  int *pnBytes,                       /* OUT: Number of bytes in token */
117585
 
  int *piStartOffset,                 /* OUT: Starting offset of token */
117586
 
  int *piEndOffset,                   /* OUT: Ending offset of token */
117587
 
  int *piPosition                     /* OUT: Position integer of token */
117588
 
){
117589
 
  simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
117590
 
  simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
117591
 
  unsigned char *p = (unsigned char *)c->pInput;
117592
 
 
117593
 
  while( c->iOffset<c->nBytes ){
117594
 
    int iStartOffset;
117595
 
 
117596
 
    /* Scan past delimiter characters */
117597
 
    while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
117598
 
      c->iOffset++;
117599
 
    }
117600
 
 
117601
 
    /* Count non-delimiter characters. */
117602
 
    iStartOffset = c->iOffset;
117603
 
    while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
117604
 
      c->iOffset++;
117605
 
    }
117606
 
 
117607
 
    if( c->iOffset>iStartOffset ){
117608
 
      int i, n = c->iOffset-iStartOffset;
117609
 
      if( n>c->nTokenAllocated ){
117610
 
        char *pNew;
117611
 
        c->nTokenAllocated = n+20;
117612
 
        pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
117613
 
        if( !pNew ) return SQLITE_NOMEM;
117614
 
        c->pToken = pNew;
117615
 
      }
117616
 
      for(i=0; i<n; i++){
117617
 
        /* TODO(shess) This needs expansion to handle UTF-8
117618
 
        ** case-insensitivity.
117619
 
        */
117620
 
        unsigned char ch = p[iStartOffset+i];
117621
 
        c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
117622
 
      }
117623
 
      *ppToken = c->pToken;
117624
 
      *pnBytes = n;
117625
 
      *piStartOffset = iStartOffset;
117626
 
      *piEndOffset = c->iOffset;
117627
 
      *piPosition = c->iToken++;
117628
 
 
117629
 
      return SQLITE_OK;
117630
 
    }
117631
 
  }
117632
 
  return SQLITE_DONE;
117633
 
}
117634
 
 
117635
 
/*
117636
 
** The set of routines that implement the simple tokenizer
117637
 
*/
117638
 
static const sqlite3_tokenizer_module simpleTokenizerModule = {
117639
 
  0,
117640
 
  simpleCreate,
117641
 
  simpleDestroy,
117642
 
  simpleOpen,
117643
 
  simpleClose,
117644
 
  simpleNext,
117645
 
};
117646
 
 
117647
 
/*
117648
 
** Allocate a new simple tokenizer.  Return a pointer to the new
117649
 
** tokenizer in *ppModule
117650
 
*/
117651
 
SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
117652
 
  sqlite3_tokenizer_module const**ppModule
117653
 
){
117654
 
  *ppModule = &simpleTokenizerModule;
117655
 
}
117656
 
 
117657
 
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
117658
 
 
117659
 
/************** End of fts3_tokenizer1.c *************************************/
117660
 
/************** Begin file fts3_write.c **************************************/
117661
 
/*
117662
 
** 2009 Oct 23
117663
 
**
117664
 
** The author disclaims copyright to this source code.  In place of
117665
 
** a legal notice, here is a blessing:
117666
 
**
117667
 
**    May you do good and not evil.
117668
 
**    May you find forgiveness for yourself and forgive others.
117669
 
**    May you share freely, never taking more than you give.
117670
 
**
117671
 
******************************************************************************
117672
 
**
117673
 
** This file is part of the SQLite FTS3 extension module. Specifically,
117674
 
** this file contains code to insert, update and delete rows from FTS3
117675
 
** tables. It also contains code to merge FTS3 b-tree segments. Some
117676
 
** of the sub-routines used to merge segments are also used by the query 
117677
 
** code in fts3.c.
117678
 
*/
117679
 
 
117680
 
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
117681
 
 
117682
 
 
117683
 
/*
117684
 
** When full-text index nodes are loaded from disk, the buffer that they
117685
 
** are loaded into has the following number of bytes of padding at the end 
117686
 
** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
117687
 
** of 920 bytes is allocated for it.
117688
 
**
117689
 
** This means that if we have a pointer into a buffer containing node data,
117690
 
** it is always safe to read up to two varints from it without risking an
117691
 
** overread, even if the node data is corrupted.
117692
 
*/
117693
 
#define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
117694
 
 
117695
 
typedef struct PendingList PendingList;
117696
 
typedef struct SegmentNode SegmentNode;
117697
 
typedef struct SegmentWriter SegmentWriter;
117698
 
 
117699
 
/*
117700
 
** Data structure used while accumulating terms in the pending-terms hash
117701
 
** table. The hash table entry maps from term (a string) to a malloc'd
117702
 
** instance of this structure.
117703
 
*/
117704
 
struct PendingList {
117705
 
  int nData;
117706
 
  char *aData;
117707
 
  int nSpace;
117708
 
  sqlite3_int64 iLastDocid;
117709
 
  sqlite3_int64 iLastCol;
117710
 
  sqlite3_int64 iLastPos;
117711
 
};
117712
 
 
117713
 
 
117714
 
/*
117715
 
** Each cursor has a (possibly empty) linked list of the following objects.
117716
 
*/
117717
 
struct Fts3DeferredToken {
117718
 
  Fts3PhraseToken *pToken;        /* Pointer to corresponding expr token */
117719
 
  int iCol;                       /* Column token must occur in */
117720
 
  Fts3DeferredToken *pNext;       /* Next in list of deferred tokens */
117721
 
  PendingList *pList;             /* Doclist is assembled here */
117722
 
};
117723
 
 
117724
 
/*
117725
 
** An instance of this structure is used to iterate through the terms on
117726
 
** a contiguous set of segment b-tree leaf nodes. Although the details of
117727
 
** this structure are only manipulated by code in this file, opaque handles
117728
 
** of type Fts3SegReader* are also used by code in fts3.c to iterate through
117729
 
** terms when querying the full-text index. See functions:
117730
 
**
117731
 
**   sqlite3Fts3SegReaderNew()
117732
 
**   sqlite3Fts3SegReaderFree()
117733
 
**   sqlite3Fts3SegReaderCost()
117734
 
**   sqlite3Fts3SegReaderIterate()
117735
 
**
117736
 
** Methods used to manipulate Fts3SegReader structures:
117737
 
**
117738
 
**   fts3SegReaderNext()
117739
 
**   fts3SegReaderFirstDocid()
117740
 
**   fts3SegReaderNextDocid()
117741
 
*/
117742
 
struct Fts3SegReader {
117743
 
  int iIdx;                       /* Index within level, or 0x7FFFFFFF for PT */
117744
 
 
117745
 
  sqlite3_int64 iStartBlock;      /* Rowid of first leaf block to traverse */
117746
 
  sqlite3_int64 iLeafEndBlock;    /* Rowid of final leaf block to traverse */
117747
 
  sqlite3_int64 iEndBlock;        /* Rowid of final block in segment (or 0) */
117748
 
  sqlite3_int64 iCurrentBlock;    /* Current leaf block (or 0) */
117749
 
 
117750
 
  char *aNode;                    /* Pointer to node data (or NULL) */
117751
 
  int nNode;                      /* Size of buffer at aNode (or 0) */
117752
 
  Fts3HashElem **ppNextElem;
117753
 
 
117754
 
  /* Variables set by fts3SegReaderNext(). These may be read directly
117755
 
  ** by the caller. They are valid from the time SegmentReaderNew() returns
117756
 
  ** until SegmentReaderNext() returns something other than SQLITE_OK
117757
 
  ** (i.e. SQLITE_DONE).
117758
 
  */
117759
 
  int nTerm;                      /* Number of bytes in current term */
117760
 
  char *zTerm;                    /* Pointer to current term */
117761
 
  int nTermAlloc;                 /* Allocated size of zTerm buffer */
117762
 
  char *aDoclist;                 /* Pointer to doclist of current entry */
117763
 
  int nDoclist;                   /* Size of doclist in current entry */
117764
 
 
117765
 
  /* The following variables are used to iterate through the current doclist */
117766
 
  char *pOffsetList;
117767
 
  sqlite3_int64 iDocid;
117768
 
};
117769
 
 
117770
 
#define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
117771
 
#define fts3SegReaderIsRootOnly(p) ((p)->aNode==(char *)&(p)[1])
117772
 
 
117773
 
/*
117774
 
** An instance of this structure is used to create a segment b-tree in the
117775
 
** database. The internal details of this type are only accessed by the
117776
 
** following functions:
117777
 
**
117778
 
**   fts3SegWriterAdd()
117779
 
**   fts3SegWriterFlush()
117780
 
**   fts3SegWriterFree()
117781
 
*/
117782
 
struct SegmentWriter {
117783
 
  SegmentNode *pTree;             /* Pointer to interior tree structure */
117784
 
  sqlite3_int64 iFirst;           /* First slot in %_segments written */
117785
 
  sqlite3_int64 iFree;            /* Next free slot in %_segments */
117786
 
  char *zTerm;                    /* Pointer to previous term buffer */
117787
 
  int nTerm;                      /* Number of bytes in zTerm */
117788
 
  int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
117789
 
  char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
117790
 
  int nSize;                      /* Size of allocation at aData */
117791
 
  int nData;                      /* Bytes of data in aData */
117792
 
  char *aData;                    /* Pointer to block from malloc() */
117793
 
};
117794
 
 
117795
 
/*
117796
 
** Type SegmentNode is used by the following three functions to create
117797
 
** the interior part of the segment b+-tree structures (everything except
117798
 
** the leaf nodes). These functions and type are only ever used by code
117799
 
** within the fts3SegWriterXXX() family of functions described above.
117800
 
**
117801
 
**   fts3NodeAddTerm()
117802
 
**   fts3NodeWrite()
117803
 
**   fts3NodeFree()
117804
 
*/
117805
 
struct SegmentNode {
117806
 
  SegmentNode *pParent;           /* Parent node (or NULL for root node) */
117807
 
  SegmentNode *pRight;            /* Pointer to right-sibling */
117808
 
  SegmentNode *pLeftmost;         /* Pointer to left-most node of this depth */
117809
 
  int nEntry;                     /* Number of terms written to node so far */
117810
 
  char *zTerm;                    /* Pointer to previous term buffer */
117811
 
  int nTerm;                      /* Number of bytes in zTerm */
117812
 
  int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
117813
 
  char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
117814
 
  int nData;                      /* Bytes of valid data so far */
117815
 
  char *aData;                    /* Node data */
117816
 
};
117817
 
 
117818
 
/*
117819
 
** Valid values for the second argument to fts3SqlStmt().
117820
 
*/
117821
 
#define SQL_DELETE_CONTENT             0
117822
 
#define SQL_IS_EMPTY                   1
117823
 
#define SQL_DELETE_ALL_CONTENT         2 
117824
 
#define SQL_DELETE_ALL_SEGMENTS        3
117825
 
#define SQL_DELETE_ALL_SEGDIR          4
117826
 
#define SQL_DELETE_ALL_DOCSIZE         5
117827
 
#define SQL_DELETE_ALL_STAT            6
117828
 
#define SQL_SELECT_CONTENT_BY_ROWID    7
117829
 
#define SQL_NEXT_SEGMENT_INDEX         8
117830
 
#define SQL_INSERT_SEGMENTS            9
117831
 
#define SQL_NEXT_SEGMENTS_ID          10
117832
 
#define SQL_INSERT_SEGDIR             11
117833
 
#define SQL_SELECT_LEVEL              12
117834
 
#define SQL_SELECT_ALL_LEVEL          13
117835
 
#define SQL_SELECT_LEVEL_COUNT        14
117836
 
#define SQL_SELECT_SEGDIR_COUNT_MAX   15
117837
 
#define SQL_DELETE_SEGDIR_BY_LEVEL    16
117838
 
#define SQL_DELETE_SEGMENTS_RANGE     17
117839
 
#define SQL_CONTENT_INSERT            18
117840
 
#define SQL_DELETE_DOCSIZE            19
117841
 
#define SQL_REPLACE_DOCSIZE           20
117842
 
#define SQL_SELECT_DOCSIZE            21
117843
 
#define SQL_SELECT_DOCTOTAL           22
117844
 
#define SQL_REPLACE_DOCTOTAL          23
117845
 
 
117846
 
/*
117847
 
** This function is used to obtain an SQLite prepared statement handle
117848
 
** for the statement identified by the second argument. If successful,
117849
 
** *pp is set to the requested statement handle and SQLITE_OK returned.
117850
 
** Otherwise, an SQLite error code is returned and *pp is set to 0.
117851
 
**
117852
 
** If argument apVal is not NULL, then it must point to an array with
117853
 
** at least as many entries as the requested statement has bound 
117854
 
** parameters. The values are bound to the statements parameters before
117855
 
** returning.
117856
 
*/
117857
 
static int fts3SqlStmt(
117858
 
  Fts3Table *p,                   /* Virtual table handle */
117859
 
  int eStmt,                      /* One of the SQL_XXX constants above */
117860
 
  sqlite3_stmt **pp,              /* OUT: Statement handle */
117861
 
  sqlite3_value **apVal           /* Values to bind to statement */
117862
 
){
117863
 
  const char *azSql[] = {
117864
 
/* 0  */  "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
117865
 
/* 1  */  "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
117866
 
/* 2  */  "DELETE FROM %Q.'%q_content'",
117867
 
/* 3  */  "DELETE FROM %Q.'%q_segments'",
117868
 
/* 4  */  "DELETE FROM %Q.'%q_segdir'",
117869
 
/* 5  */  "DELETE FROM %Q.'%q_docsize'",
117870
 
/* 6  */  "DELETE FROM %Q.'%q_stat'",
117871
 
/* 7  */  "SELECT %s FROM %Q.'%q_content' AS x WHERE rowid=?",
117872
 
/* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
117873
 
/* 9  */  "INSERT INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
117874
 
/* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
117875
 
/* 11 */  "INSERT INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
117876
 
 
117877
 
          /* Return segments in order from oldest to newest.*/ 
117878
 
/* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
117879
 
            "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
117880
 
/* 13 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
117881
 
            "FROM %Q.'%q_segdir' ORDER BY level DESC, idx ASC",
117882
 
 
117883
 
/* 14 */  "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
117884
 
/* 15 */  "SELECT count(*), max(level) FROM %Q.'%q_segdir'",
117885
 
 
117886
 
/* 16 */  "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
117887
 
/* 17 */  "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
117888
 
/* 18 */  "INSERT INTO %Q.'%q_content' VALUES(%s)",
117889
 
/* 19 */  "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
117890
 
/* 20 */  "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
117891
 
/* 21 */  "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
117892
 
/* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=0",
117893
 
/* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(0,?)",
117894
 
  };
117895
 
  int rc = SQLITE_OK;
117896
 
  sqlite3_stmt *pStmt;
117897
 
 
117898
 
  assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
117899
 
  assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
117900
 
  
117901
 
  pStmt = p->aStmt[eStmt];
117902
 
  if( !pStmt ){
117903
 
    char *zSql;
117904
 
    if( eStmt==SQL_CONTENT_INSERT ){
117905
 
      zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
117906
 
    }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
117907
 
      zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist, p->zDb, p->zName);
117908
 
    }else{
117909
 
      zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
117910
 
    }
117911
 
    if( !zSql ){
117912
 
      rc = SQLITE_NOMEM;
117913
 
    }else{
117914
 
      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL);
117915
 
      sqlite3_free(zSql);
117916
 
      assert( rc==SQLITE_OK || pStmt==0 );
117917
 
      p->aStmt[eStmt] = pStmt;
117918
 
    }
117919
 
  }
117920
 
  if( apVal ){
117921
 
    int i;
117922
 
    int nParam = sqlite3_bind_parameter_count(pStmt);
117923
 
    for(i=0; rc==SQLITE_OK && i<nParam; i++){
117924
 
      rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
117925
 
    }
117926
 
  }
117927
 
  *pp = pStmt;
117928
 
  return rc;
117929
 
}
117930
 
 
117931
 
static int fts3SelectDocsize(
117932
 
  Fts3Table *pTab,                /* FTS3 table handle */
117933
 
  int eStmt,                      /* Either SQL_SELECT_DOCSIZE or DOCTOTAL */
117934
 
  sqlite3_int64 iDocid,           /* Docid to bind for SQL_SELECT_DOCSIZE */
117935
 
  sqlite3_stmt **ppStmt           /* OUT: Statement handle */
117936
 
){
117937
 
  sqlite3_stmt *pStmt = 0;        /* Statement requested from fts3SqlStmt() */
117938
 
  int rc;                         /* Return code */
117939
 
 
117940
 
  assert( eStmt==SQL_SELECT_DOCSIZE || eStmt==SQL_SELECT_DOCTOTAL );
117941
 
 
117942
 
  rc = fts3SqlStmt(pTab, eStmt, &pStmt, 0);
117943
 
  if( rc==SQLITE_OK ){
117944
 
    if( eStmt==SQL_SELECT_DOCSIZE ){
117945
 
      sqlite3_bind_int64(pStmt, 1, iDocid);
117946
 
    }
117947
 
    rc = sqlite3_step(pStmt);
117948
 
    if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
117949
 
      rc = sqlite3_reset(pStmt);
117950
 
      if( rc==SQLITE_OK ) rc = SQLITE_CORRUPT;
117951
 
      pStmt = 0;
117952
 
    }else{
117953
 
      rc = SQLITE_OK;
117954
 
    }
117955
 
  }
117956
 
 
117957
 
  *ppStmt = pStmt;
117958
 
  return rc;
117959
 
}
117960
 
 
117961
 
SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
117962
 
  Fts3Table *pTab,                /* Fts3 table handle */
117963
 
  sqlite3_stmt **ppStmt           /* OUT: Statement handle */
117964
 
){
117965
 
  return fts3SelectDocsize(pTab, SQL_SELECT_DOCTOTAL, 0, ppStmt);
117966
 
}
117967
 
 
117968
 
SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
117969
 
  Fts3Table *pTab,                /* Fts3 table handle */
117970
 
  sqlite3_int64 iDocid,           /* Docid to read size data for */
117971
 
  sqlite3_stmt **ppStmt           /* OUT: Statement handle */
117972
 
){
117973
 
  return fts3SelectDocsize(pTab, SQL_SELECT_DOCSIZE, iDocid, ppStmt);
117974
 
}
117975
 
 
117976
 
/*
117977
 
** Similar to fts3SqlStmt(). Except, after binding the parameters in
117978
 
** array apVal[] to the SQL statement identified by eStmt, the statement
117979
 
** is executed.
117980
 
**
117981
 
** Returns SQLITE_OK if the statement is successfully executed, or an
117982
 
** SQLite error code otherwise.
117983
 
*/
117984
 
static void fts3SqlExec(
117985
 
  int *pRC,                /* Result code */
117986
 
  Fts3Table *p,            /* The FTS3 table */
117987
 
  int eStmt,               /* Index of statement to evaluate */
117988
 
  sqlite3_value **apVal    /* Parameters to bind */
117989
 
){
117990
 
  sqlite3_stmt *pStmt;
117991
 
  int rc;
117992
 
  if( *pRC ) return;
117993
 
  rc = fts3SqlStmt(p, eStmt, &pStmt, apVal); 
117994
 
  if( rc==SQLITE_OK ){
117995
 
    sqlite3_step(pStmt);
117996
 
    rc = sqlite3_reset(pStmt);
117997
 
  }
117998
 
  *pRC = rc;
117999
 
}
118000
 
 
118001
 
 
118002
 
/*
118003
 
** This function ensures that the caller has obtained a shared-cache
118004
 
** table-lock on the %_content table. This is required before reading
118005
 
** data from the fts3 table. If this lock is not acquired first, then
118006
 
** the caller may end up holding read-locks on the %_segments and %_segdir
118007
 
** tables, but no read-lock on the %_content table. If this happens 
118008
 
** a second connection will be able to write to the fts3 table, but
118009
 
** attempting to commit those writes might return SQLITE_LOCKED or
118010
 
** SQLITE_LOCKED_SHAREDCACHE (because the commit attempts to obtain 
118011
 
** write-locks on the %_segments and %_segdir ** tables). 
118012
 
**
118013
 
** We try to avoid this because if FTS3 returns any error when committing
118014
 
** a transaction, the whole transaction will be rolled back. And this is
118015
 
** not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. It can
118016
 
** still happen if the user reads data directly from the %_segments or
118017
 
** %_segdir tables instead of going through FTS3 though.
118018
 
*/
118019
 
SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *p){
118020
 
  int rc;                         /* Return code */
118021
 
  sqlite3_stmt *pStmt;            /* Statement used to obtain lock */
118022
 
 
118023
 
  rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pStmt, 0);
118024
 
  if( rc==SQLITE_OK ){
118025
 
    sqlite3_bind_null(pStmt, 1);
118026
 
    sqlite3_step(pStmt);
118027
 
    rc = sqlite3_reset(pStmt);
118028
 
  }
118029
 
  return rc;
118030
 
}
118031
 
 
118032
 
/*
118033
 
** Set *ppStmt to a statement handle that may be used to iterate through
118034
 
** all rows in the %_segdir table, from oldest to newest. If successful,
118035
 
** return SQLITE_OK. If an error occurs while preparing the statement, 
118036
 
** return an SQLite error code.
118037
 
**
118038
 
** There is only ever one instance of this SQL statement compiled for
118039
 
** each FTS3 table.
118040
 
**
118041
 
** The statement returns the following columns from the %_segdir table:
118042
 
**
118043
 
**   0: idx
118044
 
**   1: start_block
118045
 
**   2: leaves_end_block
118046
 
**   3: end_block
118047
 
**   4: root
118048
 
*/
118049
 
SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table *p, int iLevel, sqlite3_stmt **ppStmt){
118050
 
  int rc;
118051
 
  sqlite3_stmt *pStmt = 0;
118052
 
  if( iLevel<0 ){
118053
 
    rc = fts3SqlStmt(p, SQL_SELECT_ALL_LEVEL, &pStmt, 0);
118054
 
  }else{
118055
 
    rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
118056
 
    if( rc==SQLITE_OK ) sqlite3_bind_int(pStmt, 1, iLevel);
118057
 
  }
118058
 
  *ppStmt = pStmt;
118059
 
  return rc;
118060
 
}
118061
 
 
118062
 
 
118063
 
/*
118064
 
** Append a single varint to a PendingList buffer. SQLITE_OK is returned
118065
 
** if successful, or an SQLite error code otherwise.
118066
 
**
118067
 
** This function also serves to allocate the PendingList structure itself.
118068
 
** For example, to create a new PendingList structure containing two
118069
 
** varints:
118070
 
**
118071
 
**   PendingList *p = 0;
118072
 
**   fts3PendingListAppendVarint(&p, 1);
118073
 
**   fts3PendingListAppendVarint(&p, 2);
118074
 
*/
118075
 
static int fts3PendingListAppendVarint(
118076
 
  PendingList **pp,               /* IN/OUT: Pointer to PendingList struct */
118077
 
  sqlite3_int64 i                 /* Value to append to data */
118078
 
){
118079
 
  PendingList *p = *pp;
118080
 
 
118081
 
  /* Allocate or grow the PendingList as required. */
118082
 
  if( !p ){
118083
 
    p = sqlite3_malloc(sizeof(*p) + 100);
118084
 
    if( !p ){
118085
 
      return SQLITE_NOMEM;
118086
 
    }
118087
 
    p->nSpace = 100;
118088
 
    p->aData = (char *)&p[1];
118089
 
    p->nData = 0;
118090
 
  }
118091
 
  else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
118092
 
    int nNew = p->nSpace * 2;
118093
 
    p = sqlite3_realloc(p, sizeof(*p) + nNew);
118094
 
    if( !p ){
118095
 
      sqlite3_free(*pp);
118096
 
      *pp = 0;
118097
 
      return SQLITE_NOMEM;
118098
 
    }
118099
 
    p->nSpace = nNew;
118100
 
    p->aData = (char *)&p[1];
118101
 
  }
118102
 
 
118103
 
  /* Append the new serialized varint to the end of the list. */
118104
 
  p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
118105
 
  p->aData[p->nData] = '\0';
118106
 
  *pp = p;
118107
 
  return SQLITE_OK;
118108
 
}
118109
 
 
118110
 
/*
118111
 
** Add a docid/column/position entry to a PendingList structure. Non-zero
118112
 
** is returned if the structure is sqlite3_realloced as part of adding
118113
 
** the entry. Otherwise, zero.
118114
 
**
118115
 
** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
118116
 
** Zero is always returned in this case. Otherwise, if no OOM error occurs,
118117
 
** it is set to SQLITE_OK.
118118
 
*/
118119
 
static int fts3PendingListAppend(
118120
 
  PendingList **pp,               /* IN/OUT: PendingList structure */
118121
 
  sqlite3_int64 iDocid,           /* Docid for entry to add */
118122
 
  sqlite3_int64 iCol,             /* Column for entry to add */
118123
 
  sqlite3_int64 iPos,             /* Position of term for entry to add */
118124
 
  int *pRc                        /* OUT: Return code */
118125
 
){
118126
 
  PendingList *p = *pp;
118127
 
  int rc = SQLITE_OK;
118128
 
 
118129
 
  assert( !p || p->iLastDocid<=iDocid );
118130
 
 
118131
 
  if( !p || p->iLastDocid!=iDocid ){
118132
 
    sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
118133
 
    if( p ){
118134
 
      assert( p->nData<p->nSpace );
118135
 
      assert( p->aData[p->nData]==0 );
118136
 
      p->nData++;
118137
 
    }
118138
 
    if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
118139
 
      goto pendinglistappend_out;
118140
 
    }
118141
 
    p->iLastCol = -1;
118142
 
    p->iLastPos = 0;
118143
 
    p->iLastDocid = iDocid;
118144
 
  }
118145
 
  if( iCol>0 && p->iLastCol!=iCol ){
118146
 
    if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
118147
 
     || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
118148
 
    ){
118149
 
      goto pendinglistappend_out;
118150
 
    }
118151
 
    p->iLastCol = iCol;
118152
 
    p->iLastPos = 0;
118153
 
  }
118154
 
  if( iCol>=0 ){
118155
 
    assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
118156
 
    rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
118157
 
    if( rc==SQLITE_OK ){
118158
 
      p->iLastPos = iPos;
118159
 
    }
118160
 
  }
118161
 
 
118162
 
 pendinglistappend_out:
118163
 
  *pRc = rc;
118164
 
  if( p!=*pp ){
118165
 
    *pp = p;
118166
 
    return 1;
118167
 
  }
118168
 
  return 0;
118169
 
}
118170
 
 
118171
 
/*
118172
 
** Tokenize the nul-terminated string zText and add all tokens to the
118173
 
** pending-terms hash-table. The docid used is that currently stored in
118174
 
** p->iPrevDocid, and the column is specified by argument iCol.
118175
 
**
118176
 
** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
118177
 
*/
118178
 
static int fts3PendingTermsAdd(
118179
 
  Fts3Table *p,                   /* Table into which text will be inserted */
118180
 
  const char *zText,              /* Text of document to be inserted */
118181
 
  int iCol,                       /* Column into which text is being inserted */
118182
 
  u32 *pnWord                     /* OUT: Number of tokens inserted */
118183
 
){
118184
 
  int rc;
118185
 
  int iStart;
118186
 
  int iEnd;
118187
 
  int iPos;
118188
 
  int nWord = 0;
118189
 
 
118190
 
  char const *zToken;
118191
 
  int nToken;
118192
 
 
118193
 
  sqlite3_tokenizer *pTokenizer = p->pTokenizer;
118194
 
  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
118195
 
  sqlite3_tokenizer_cursor *pCsr;
118196
 
  int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
118197
 
      const char**,int*,int*,int*,int*);
118198
 
 
118199
 
  assert( pTokenizer && pModule );
118200
 
 
118201
 
  rc = pModule->xOpen(pTokenizer, zText, -1, &pCsr);
118202
 
  if( rc!=SQLITE_OK ){
118203
 
    return rc;
118204
 
  }
118205
 
  pCsr->pTokenizer = pTokenizer;
118206
 
 
118207
 
  xNext = pModule->xNext;
118208
 
  while( SQLITE_OK==rc
118209
 
      && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
118210
 
  ){
118211
 
    PendingList *pList;
118212
 
 
118213
 
    if( iPos>=nWord ) nWord = iPos+1;
118214
 
 
118215
 
    /* Positions cannot be negative; we use -1 as a terminator internally.
118216
 
    ** Tokens must have a non-zero length.
118217
 
    */
118218
 
    if( iPos<0 || !zToken || nToken<=0 ){
118219
 
      rc = SQLITE_ERROR;
118220
 
      break;
118221
 
    }
118222
 
 
118223
 
    pList = (PendingList *)fts3HashFind(&p->pendingTerms, zToken, nToken);
118224
 
    if( pList ){
118225
 
      p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
118226
 
    }
118227
 
    if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
118228
 
      if( pList==fts3HashInsert(&p->pendingTerms, zToken, nToken, pList) ){
118229
 
        /* Malloc failed while inserting the new entry. This can only 
118230
 
        ** happen if there was no previous entry for this token.
118231
 
        */
118232
 
        assert( 0==fts3HashFind(&p->pendingTerms, zToken, nToken) );
118233
 
        sqlite3_free(pList);
118234
 
        rc = SQLITE_NOMEM;
118235
 
      }
118236
 
    }
118237
 
    if( rc==SQLITE_OK ){
118238
 
      p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
118239
 
    }
118240
 
  }
118241
 
 
118242
 
  pModule->xClose(pCsr);
118243
 
  *pnWord = nWord;
118244
 
  return (rc==SQLITE_DONE ? SQLITE_OK : rc);
118245
 
}
118246
 
 
118247
 
/* 
118248
 
** Calling this function indicates that subsequent calls to 
118249
 
** fts3PendingTermsAdd() are to add term/position-list pairs for the
118250
 
** contents of the document with docid iDocid.
118251
 
*/
118252
 
static int fts3PendingTermsDocid(Fts3Table *p, sqlite_int64 iDocid){
118253
 
  /* TODO(shess) Explore whether partially flushing the buffer on
118254
 
  ** forced-flush would provide better performance.  I suspect that if
118255
 
  ** we ordered the doclists by size and flushed the largest until the
118256
 
  ** buffer was half empty, that would let the less frequent terms
118257
 
  ** generate longer doclists.
118258
 
  */
118259
 
  if( iDocid<=p->iPrevDocid || p->nPendingData>p->nMaxPendingData ){
118260
 
    int rc = sqlite3Fts3PendingTermsFlush(p);
118261
 
    if( rc!=SQLITE_OK ) return rc;
118262
 
  }
118263
 
  p->iPrevDocid = iDocid;
118264
 
  return SQLITE_OK;
118265
 
}
118266
 
 
118267
 
/*
118268
 
** Discard the contents of the pending-terms hash table. 
118269
 
*/
118270
 
SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
118271
 
  Fts3HashElem *pElem;
118272
 
  for(pElem=fts3HashFirst(&p->pendingTerms); pElem; pElem=fts3HashNext(pElem)){
118273
 
    sqlite3_free(fts3HashData(pElem));
118274
 
  }
118275
 
  fts3HashClear(&p->pendingTerms);
118276
 
  p->nPendingData = 0;
118277
 
}
118278
 
 
118279
 
/*
118280
 
** This function is called by the xUpdate() method as part of an INSERT
118281
 
** operation. It adds entries for each term in the new record to the
118282
 
** pendingTerms hash table.
118283
 
**
118284
 
** Argument apVal is the same as the similarly named argument passed to
118285
 
** fts3InsertData(). Parameter iDocid is the docid of the new row.
118286
 
*/
118287
 
static int fts3InsertTerms(Fts3Table *p, sqlite3_value **apVal, u32 *aSz){
118288
 
  int i;                          /* Iterator variable */
118289
 
  for(i=2; i<p->nColumn+2; i++){
118290
 
    const char *zText = (const char *)sqlite3_value_text(apVal[i]);
118291
 
    if( zText ){
118292
 
      int rc = fts3PendingTermsAdd(p, zText, i-2, &aSz[i-2]);
118293
 
      if( rc!=SQLITE_OK ){
118294
 
        return rc;
118295
 
      }
118296
 
    }
118297
 
    aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
118298
 
  }
118299
 
  return SQLITE_OK;
118300
 
}
118301
 
 
118302
 
/*
118303
 
** This function is called by the xUpdate() method for an INSERT operation.
118304
 
** The apVal parameter is passed a copy of the apVal argument passed by
118305
 
** SQLite to the xUpdate() method. i.e:
118306
 
**
118307
 
**   apVal[0]                Not used for INSERT.
118308
 
**   apVal[1]                rowid
118309
 
**   apVal[2]                Left-most user-defined column
118310
 
**   ...
118311
 
**   apVal[p->nColumn+1]     Right-most user-defined column
118312
 
**   apVal[p->nColumn+2]     Hidden column with same name as table
118313
 
**   apVal[p->nColumn+3]     Hidden "docid" column (alias for rowid)
118314
 
*/
118315
 
static int fts3InsertData(
118316
 
  Fts3Table *p,                   /* Full-text table */
118317
 
  sqlite3_value **apVal,          /* Array of values to insert */
118318
 
  sqlite3_int64 *piDocid          /* OUT: Docid for row just inserted */
118319
 
){
118320
 
  int rc;                         /* Return code */
118321
 
  sqlite3_stmt *pContentInsert;   /* INSERT INTO %_content VALUES(...) */
118322
 
 
118323
 
  /* Locate the statement handle used to insert data into the %_content
118324
 
  ** table. The SQL for this statement is:
118325
 
  **
118326
 
  **   INSERT INTO %_content VALUES(?, ?, ?, ...)
118327
 
  **
118328
 
  ** The statement features N '?' variables, where N is the number of user
118329
 
  ** defined columns in the FTS3 table, plus one for the docid field.
118330
 
  */
118331
 
  rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
118332
 
  if( rc!=SQLITE_OK ){
118333
 
    return rc;
118334
 
  }
118335
 
 
118336
 
  /* There is a quirk here. The users INSERT statement may have specified
118337
 
  ** a value for the "rowid" field, for the "docid" field, or for both.
118338
 
  ** Which is a problem, since "rowid" and "docid" are aliases for the
118339
 
  ** same value. For example:
118340
 
  **
118341
 
  **   INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
118342
 
  **
118343
 
  ** In FTS3, this is an error. It is an error to specify non-NULL values
118344
 
  ** for both docid and some other rowid alias.
118345
 
  */
118346
 
  if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
118347
 
    if( SQLITE_NULL==sqlite3_value_type(apVal[0])
118348
 
     && SQLITE_NULL!=sqlite3_value_type(apVal[1])
118349
 
    ){
118350
 
      /* A rowid/docid conflict. */
118351
 
      return SQLITE_ERROR;
118352
 
    }
118353
 
    rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
118354
 
    if( rc!=SQLITE_OK ) return rc;
118355
 
  }
118356
 
 
118357
 
  /* Execute the statement to insert the record. Set *piDocid to the 
118358
 
  ** new docid value. 
118359
 
  */
118360
 
  sqlite3_step(pContentInsert);
118361
 
  rc = sqlite3_reset(pContentInsert);
118362
 
 
118363
 
  *piDocid = sqlite3_last_insert_rowid(p->db);
118364
 
  return rc;
118365
 
}
118366
 
 
118367
 
 
118368
 
 
118369
 
/*
118370
 
** Remove all data from the FTS3 table. Clear the hash table containing
118371
 
** pending terms.
118372
 
*/
118373
 
static int fts3DeleteAll(Fts3Table *p){
118374
 
  int rc = SQLITE_OK;             /* Return code */
118375
 
 
118376
 
  /* Discard the contents of the pending-terms hash table. */
118377
 
  sqlite3Fts3PendingTermsClear(p);
118378
 
 
118379
 
  /* Delete everything from the %_content, %_segments and %_segdir tables. */
118380
 
  fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
118381
 
  fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
118382
 
  fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
118383
 
  if( p->bHasDocsize ){
118384
 
    fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
118385
 
  }
118386
 
  if( p->bHasStat ){
118387
 
    fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
118388
 
  }
118389
 
  return rc;
118390
 
}
118391
 
 
118392
 
/*
118393
 
** The first element in the apVal[] array is assumed to contain the docid
118394
 
** (an integer) of a row about to be deleted. Remove all terms from the
118395
 
** full-text index.
118396
 
*/
118397
 
static void fts3DeleteTerms( 
118398
 
  int *pRC,               /* Result code */
118399
 
  Fts3Table *p,           /* The FTS table to delete from */
118400
 
  sqlite3_value **apVal,  /* apVal[] contains the docid to be deleted */
118401
 
  u32 *aSz                /* Sizes of deleted document written here */
118402
 
){
118403
 
  int rc;
118404
 
  sqlite3_stmt *pSelect;
118405
 
 
118406
 
  if( *pRC ) return;
118407
 
  rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, apVal);
118408
 
  if( rc==SQLITE_OK ){
118409
 
    if( SQLITE_ROW==sqlite3_step(pSelect) ){
118410
 
      int i;
118411
 
      for(i=1; i<=p->nColumn; i++){
118412
 
        const char *zText = (const char *)sqlite3_column_text(pSelect, i);
118413
 
        rc = fts3PendingTermsAdd(p, zText, -1, &aSz[i-1]);
118414
 
        if( rc!=SQLITE_OK ){
118415
 
          sqlite3_reset(pSelect);
118416
 
          *pRC = rc;
118417
 
          return;
118418
 
        }
118419
 
        aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
118420
 
      }
118421
 
    }
118422
 
    rc = sqlite3_reset(pSelect);
118423
 
  }else{
118424
 
    sqlite3_reset(pSelect);
118425
 
  }
118426
 
  *pRC = rc;
118427
 
}
118428
 
 
118429
 
/*
118430
 
** Forward declaration to account for the circular dependency between
118431
 
** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
118432
 
*/
118433
 
static int fts3SegmentMerge(Fts3Table *, int);
118434
 
 
118435
 
/* 
118436
 
** This function allocates a new level iLevel index in the segdir table.
118437
 
** Usually, indexes are allocated within a level sequentially starting
118438
 
** with 0, so the allocated index is one greater than the value returned
118439
 
** by:
118440
 
**
118441
 
**   SELECT max(idx) FROM %_segdir WHERE level = :iLevel
118442
 
**
118443
 
** However, if there are already FTS3_MERGE_COUNT indexes at the requested
118444
 
** level, they are merged into a single level (iLevel+1) segment and the 
118445
 
** allocated index is 0.
118446
 
**
118447
 
** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
118448
 
** returned. Otherwise, an SQLite error code is returned.
118449
 
*/
118450
 
static int fts3AllocateSegdirIdx(Fts3Table *p, int iLevel, int *piIdx){
118451
 
  int rc;                         /* Return Code */
118452
 
  sqlite3_stmt *pNextIdx;         /* Query for next idx at level iLevel */
118453
 
  int iNext = 0;                  /* Result of query pNextIdx */
118454
 
 
118455
 
  /* Set variable iNext to the next available segdir index at level iLevel. */
118456
 
  rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
118457
 
  if( rc==SQLITE_OK ){
118458
 
    sqlite3_bind_int(pNextIdx, 1, iLevel);
118459
 
    if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
118460
 
      iNext = sqlite3_column_int(pNextIdx, 0);
118461
 
    }
118462
 
    rc = sqlite3_reset(pNextIdx);
118463
 
  }
118464
 
 
118465
 
  if( rc==SQLITE_OK ){
118466
 
    /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
118467
 
    ** full, merge all segments in level iLevel into a single iLevel+1
118468
 
    ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
118469
 
    ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
118470
 
    */
118471
 
    if( iNext>=FTS3_MERGE_COUNT ){
118472
 
      rc = fts3SegmentMerge(p, iLevel);
118473
 
      *piIdx = 0;
118474
 
    }else{
118475
 
      *piIdx = iNext;
118476
 
    }
118477
 
  }
118478
 
 
118479
 
  return rc;
118480
 
}
118481
 
 
118482
 
/*
118483
 
** The %_segments table is declared as follows:
118484
 
**
118485
 
**   CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
118486
 
**
118487
 
** This function reads data from a single row of the %_segments table. The
118488
 
** specific row is identified by the iBlockid parameter. If paBlob is not
118489
 
** NULL, then a buffer is allocated using sqlite3_malloc() and populated
118490
 
** with the contents of the blob stored in the "block" column of the 
118491
 
** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
118492
 
** to the size of the blob in bytes before returning.
118493
 
**
118494
 
** If an error occurs, or the table does not contain the specified row,
118495
 
** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
118496
 
** paBlob is non-NULL, then it is the responsibility of the caller to
118497
 
** eventually free the returned buffer.
118498
 
**
118499
 
** This function may leave an open sqlite3_blob* handle in the
118500
 
** Fts3Table.pSegments variable. This handle is reused by subsequent calls
118501
 
** to this function. The handle may be closed by calling the
118502
 
** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
118503
 
** performance improvement, but the blob handle should always be closed
118504
 
** before control is returned to the user (to prevent a lock being held
118505
 
** on the database file for longer than necessary). Thus, any virtual table
118506
 
** method (xFilter etc.) that may directly or indirectly call this function
118507
 
** must call sqlite3Fts3SegmentsClose() before returning.
118508
 
*/
118509
 
SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
118510
 
  Fts3Table *p,                   /* FTS3 table handle */
118511
 
  sqlite3_int64 iBlockid,         /* Access the row with blockid=$iBlockid */
118512
 
  char **paBlob,                  /* OUT: Blob data in malloc'd buffer */
118513
 
  int *pnBlob                     /* OUT: Size of blob data */
118514
 
){
118515
 
  int rc;                         /* Return code */
118516
 
 
118517
 
  /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
118518
 
  assert( pnBlob);
118519
 
 
118520
 
  if( p->pSegments ){
118521
 
    rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
118522
 
  }else{
118523
 
    if( 0==p->zSegmentsTbl ){
118524
 
      p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
118525
 
      if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
118526
 
    }
118527
 
    rc = sqlite3_blob_open(
118528
 
       p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
118529
 
    );
118530
 
  }
118531
 
 
118532
 
  if( rc==SQLITE_OK ){
118533
 
    int nByte = sqlite3_blob_bytes(p->pSegments);
118534
 
    if( paBlob ){
118535
 
      char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
118536
 
      if( !aByte ){
118537
 
        rc = SQLITE_NOMEM;
118538
 
      }else{
118539
 
        rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
118540
 
        memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
118541
 
        if( rc!=SQLITE_OK ){
118542
 
          sqlite3_free(aByte);
118543
 
          aByte = 0;
118544
 
        }
118545
 
      }
118546
 
      *paBlob = aByte;
118547
 
    }
118548
 
    *pnBlob = nByte;
118549
 
  }
118550
 
 
118551
 
  return rc;
118552
 
}
118553
 
 
118554
 
/*
118555
 
** Close the blob handle at p->pSegments, if it is open. See comments above
118556
 
** the sqlite3Fts3ReadBlock() function for details.
118557
 
*/
118558
 
SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
118559
 
  sqlite3_blob_close(p->pSegments);
118560
 
  p->pSegments = 0;
118561
 
}
118562
 
 
118563
 
/*
118564
 
** Move the iterator passed as the first argument to the next term in the
118565
 
** segment. If successful, SQLITE_OK is returned. If there is no next term,
118566
 
** SQLITE_DONE. Otherwise, an SQLite error code.
118567
 
*/
118568
 
static int fts3SegReaderNext(Fts3Table *p, Fts3SegReader *pReader){
118569
 
  char *pNext;                    /* Cursor variable */
118570
 
  int nPrefix;                    /* Number of bytes in term prefix */
118571
 
  int nSuffix;                    /* Number of bytes in term suffix */
118572
 
 
118573
 
  if( !pReader->aDoclist ){
118574
 
    pNext = pReader->aNode;
118575
 
  }else{
118576
 
    pNext = &pReader->aDoclist[pReader->nDoclist];
118577
 
  }
118578
 
 
118579
 
  if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
118580
 
    int rc;                       /* Return code from Fts3ReadBlock() */
118581
 
 
118582
 
    if( fts3SegReaderIsPending(pReader) ){
118583
 
      Fts3HashElem *pElem = *(pReader->ppNextElem);
118584
 
      if( pElem==0 ){
118585
 
        pReader->aNode = 0;
118586
 
      }else{
118587
 
        PendingList *pList = (PendingList *)fts3HashData(pElem);
118588
 
        pReader->zTerm = (char *)fts3HashKey(pElem);
118589
 
        pReader->nTerm = fts3HashKeysize(pElem);
118590
 
        pReader->nNode = pReader->nDoclist = pList->nData + 1;
118591
 
        pReader->aNode = pReader->aDoclist = pList->aData;
118592
 
        pReader->ppNextElem++;
118593
 
        assert( pReader->aNode );
118594
 
      }
118595
 
      return SQLITE_OK;
118596
 
    }
118597
 
 
118598
 
    if( !fts3SegReaderIsRootOnly(pReader) ){
118599
 
      sqlite3_free(pReader->aNode);
118600
 
    }
118601
 
    pReader->aNode = 0;
118602
 
 
118603
 
    /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf 
118604
 
    ** blocks have already been traversed.  */
118605
 
    assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
118606
 
    if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
118607
 
      return SQLITE_OK;
118608
 
    }
118609
 
 
118610
 
    rc = sqlite3Fts3ReadBlock(
118611
 
        p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode
118612
 
    );
118613
 
    if( rc!=SQLITE_OK ) return rc;
118614
 
    pNext = pReader->aNode;
118615
 
  }
118616
 
  
118617
 
  /* Because of the FTS3_NODE_PADDING bytes of padding, the following is 
118618
 
  ** safe (no risk of overread) even if the node data is corrupted.  
118619
 
  */
118620
 
  pNext += sqlite3Fts3GetVarint32(pNext, &nPrefix);
118621
 
  pNext += sqlite3Fts3GetVarint32(pNext, &nSuffix);
118622
 
  if( nPrefix<0 || nSuffix<=0 
118623
 
   || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] 
118624
 
  ){
118625
 
    return SQLITE_CORRUPT;
118626
 
  }
118627
 
 
118628
 
  if( nPrefix+nSuffix>pReader->nTermAlloc ){
118629
 
    int nNew = (nPrefix+nSuffix)*2;
118630
 
    char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
118631
 
    if( !zNew ){
118632
 
      return SQLITE_NOMEM;
118633
 
    }
118634
 
    pReader->zTerm = zNew;
118635
 
    pReader->nTermAlloc = nNew;
118636
 
  }
118637
 
  memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
118638
 
  pReader->nTerm = nPrefix+nSuffix;
118639
 
  pNext += nSuffix;
118640
 
  pNext += sqlite3Fts3GetVarint32(pNext, &pReader->nDoclist);
118641
 
  pReader->aDoclist = pNext;
118642
 
  pReader->pOffsetList = 0;
118643
 
 
118644
 
  /* Check that the doclist does not appear to extend past the end of the
118645
 
  ** b-tree node. And that the final byte of the doclist is 0x00. If either 
118646
 
  ** of these statements is untrue, then the data structure is corrupt.
118647
 
  */
118648
 
  if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode] 
118649
 
   || pReader->aDoclist[pReader->nDoclist-1]
118650
 
  ){
118651
 
    return SQLITE_CORRUPT;
118652
 
  }
118653
 
  return SQLITE_OK;
118654
 
}
118655
 
 
118656
 
/*
118657
 
** Set the SegReader to point to the first docid in the doclist associated
118658
 
** with the current term.
118659
 
*/
118660
 
static void fts3SegReaderFirstDocid(Fts3SegReader *pReader){
118661
 
  int n;
118662
 
  assert( pReader->aDoclist );
118663
 
  assert( !pReader->pOffsetList );
118664
 
  n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
118665
 
  pReader->pOffsetList = &pReader->aDoclist[n];
118666
 
}
118667
 
 
118668
 
/*
118669
 
** Advance the SegReader to point to the next docid in the doclist
118670
 
** associated with the current term.
118671
 
** 
118672
 
** If arguments ppOffsetList and pnOffsetList are not NULL, then 
118673
 
** *ppOffsetList is set to point to the first column-offset list
118674
 
** in the doclist entry (i.e. immediately past the docid varint).
118675
 
** *pnOffsetList is set to the length of the set of column-offset
118676
 
** lists, not including the nul-terminator byte. For example:
118677
 
*/
118678
 
static void fts3SegReaderNextDocid(
118679
 
  Fts3SegReader *pReader,
118680
 
  char **ppOffsetList,
118681
 
  int *pnOffsetList
118682
 
){
118683
 
  char *p = pReader->pOffsetList;
118684
 
  char c = 0;
118685
 
 
118686
 
  /* Pointer p currently points at the first byte of an offset list. The
118687
 
  ** following two lines advance it to point one byte past the end of
118688
 
  ** the same offset list.
118689
 
  */
118690
 
  while( *p | c ) c = *p++ & 0x80;
118691
 
  p++;
118692
 
 
118693
 
  /* If required, populate the output variables with a pointer to and the
118694
 
  ** size of the previous offset-list.
118695
 
  */
118696
 
  if( ppOffsetList ){
118697
 
    *ppOffsetList = pReader->pOffsetList;
118698
 
    *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
118699
 
  }
118700
 
 
118701
 
  /* If there are no more entries in the doclist, set pOffsetList to
118702
 
  ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
118703
 
  ** Fts3SegReader.pOffsetList to point to the next offset list before
118704
 
  ** returning.
118705
 
  */
118706
 
  if( p>=&pReader->aDoclist[pReader->nDoclist] ){
118707
 
    pReader->pOffsetList = 0;
118708
 
  }else{
118709
 
    sqlite3_int64 iDelta;
118710
 
    pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
118711
 
    pReader->iDocid += iDelta;
118712
 
  }
118713
 
}
118714
 
 
118715
 
/*
118716
 
** This function is called to estimate the amount of data that will be 
118717
 
** loaded from the disk If SegReaderIterate() is called on this seg-reader,
118718
 
** in units of average document size.
118719
 
** 
118720
 
** This can be used as follows: If the caller has a small doclist that 
118721
 
** contains references to N documents, and is considering merging it with
118722
 
** a large doclist (size X "average documents"), it may opt not to load
118723
 
** the large doclist if X>N.
118724
 
*/
118725
 
SQLITE_PRIVATE int sqlite3Fts3SegReaderCost(
118726
 
  Fts3Cursor *pCsr,               /* FTS3 cursor handle */
118727
 
  Fts3SegReader *pReader,         /* Segment-reader handle */
118728
 
  int *pnCost                     /* IN/OUT: Number of bytes read */
118729
 
){
118730
 
  Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
118731
 
  int rc = SQLITE_OK;             /* Return code */
118732
 
  int nCost = 0;                  /* Cost in bytes to return */
118733
 
  int pgsz = p->nPgsz;            /* Database page size */
118734
 
 
118735
 
  /* If this seg-reader is reading the pending-terms table, or if all data
118736
 
  ** for the segment is stored on the root page of the b-tree, then the cost
118737
 
  ** is zero. In this case all required data is already in main memory.
118738
 
  */
118739
 
  if( p->bHasStat 
118740
 
   && !fts3SegReaderIsPending(pReader) 
118741
 
   && !fts3SegReaderIsRootOnly(pReader) 
118742
 
  ){
118743
 
    int nBlob = 0;
118744
 
    sqlite3_int64 iBlock;
118745
 
 
118746
 
    if( pCsr->nRowAvg==0 ){
118747
 
      /* The average document size, which is required to calculate the cost
118748
 
      ** of each doclist, has not yet been determined. Read the required 
118749
 
      ** data from the %_stat table to calculate it.
118750
 
      **
118751
 
      ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3 
118752
 
      ** varints, where nCol is the number of columns in the FTS3 table.
118753
 
      ** The first varint is the number of documents currently stored in
118754
 
      ** the table. The following nCol varints contain the total amount of
118755
 
      ** data stored in all rows of each column of the table, from left
118756
 
      ** to right.
118757
 
      */
118758
 
      sqlite3_stmt *pStmt;
118759
 
      sqlite3_int64 nDoc = 0;
118760
 
      sqlite3_int64 nByte = 0;
118761
 
      const char *pEnd;
118762
 
      const char *a;
118763
 
 
118764
 
      rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
118765
 
      if( rc!=SQLITE_OK ) return rc;
118766
 
      a = sqlite3_column_blob(pStmt, 0);
118767
 
      assert( a );
118768
 
 
118769
 
      pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
118770
 
      a += sqlite3Fts3GetVarint(a, &nDoc);
118771
 
      while( a<pEnd ){
118772
 
        a += sqlite3Fts3GetVarint(a, &nByte);
118773
 
      }
118774
 
      if( nDoc==0 || nByte==0 ){
118775
 
        sqlite3_reset(pStmt);
118776
 
        return SQLITE_CORRUPT;
118777
 
      }
118778
 
 
118779
 
      pCsr->nRowAvg = (int)(((nByte / nDoc) + pgsz) / pgsz);
118780
 
      assert( pCsr->nRowAvg>0 ); 
118781
 
      rc = sqlite3_reset(pStmt);
118782
 
      if( rc!=SQLITE_OK ) return rc;
118783
 
    }
118784
 
 
118785
 
    /* Assume that a blob flows over onto overflow pages if it is larger
118786
 
    ** than (pgsz-35) bytes in size (the file-format documentation
118787
 
    ** confirms this).
118788
 
    */
118789
 
    for(iBlock=pReader->iStartBlock; iBlock<=pReader->iLeafEndBlock; iBlock++){
118790
 
      rc = sqlite3Fts3ReadBlock(p, iBlock, 0, &nBlob);
118791
 
      if( rc!=SQLITE_OK ) break;
118792
 
      if( (nBlob+35)>pgsz ){
118793
 
        int nOvfl = (nBlob + 34)/pgsz;
118794
 
        nCost += ((nOvfl + pCsr->nRowAvg - 1)/pCsr->nRowAvg);
118795
 
      }
118796
 
    }
118797
 
  }
118798
 
 
118799
 
  *pnCost += nCost;
118800
 
  return rc;
118801
 
}
118802
 
 
118803
 
/*
118804
 
** Free all allocations associated with the iterator passed as the 
118805
 
** second argument.
118806
 
*/
118807
 
SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
118808
 
  if( pReader && !fts3SegReaderIsPending(pReader) ){
118809
 
    sqlite3_free(pReader->zTerm);
118810
 
    if( !fts3SegReaderIsRootOnly(pReader) ){
118811
 
      sqlite3_free(pReader->aNode);
118812
 
    }
118813
 
  }
118814
 
  sqlite3_free(pReader);
118815
 
}
118816
 
 
118817
 
/*
118818
 
** Allocate a new SegReader object.
118819
 
*/
118820
 
SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
118821
 
  int iAge,                       /* Segment "age". */
118822
 
  sqlite3_int64 iStartLeaf,       /* First leaf to traverse */
118823
 
  sqlite3_int64 iEndLeaf,         /* Final leaf to traverse */
118824
 
  sqlite3_int64 iEndBlock,        /* Final block of segment */
118825
 
  const char *zRoot,              /* Buffer containing root node */
118826
 
  int nRoot,                      /* Size of buffer containing root node */
118827
 
  Fts3SegReader **ppReader        /* OUT: Allocated Fts3SegReader */
118828
 
){
118829
 
  int rc = SQLITE_OK;             /* Return code */
118830
 
  Fts3SegReader *pReader;         /* Newly allocated SegReader object */
118831
 
  int nExtra = 0;                 /* Bytes to allocate segment root node */
118832
 
 
118833
 
  assert( iStartLeaf<=iEndLeaf );
118834
 
  if( iStartLeaf==0 ){
118835
 
    nExtra = nRoot + FTS3_NODE_PADDING;
118836
 
  }
118837
 
 
118838
 
  pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
118839
 
  if( !pReader ){
118840
 
    return SQLITE_NOMEM;
118841
 
  }
118842
 
  memset(pReader, 0, sizeof(Fts3SegReader));
118843
 
  pReader->iIdx = iAge;
118844
 
  pReader->iStartBlock = iStartLeaf;
118845
 
  pReader->iLeafEndBlock = iEndLeaf;
118846
 
  pReader->iEndBlock = iEndBlock;
118847
 
 
118848
 
  if( nExtra ){
118849
 
    /* The entire segment is stored in the root node. */
118850
 
    pReader->aNode = (char *)&pReader[1];
118851
 
    pReader->nNode = nRoot;
118852
 
    memcpy(pReader->aNode, zRoot, nRoot);
118853
 
    memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
118854
 
  }else{
118855
 
    pReader->iCurrentBlock = iStartLeaf-1;
118856
 
  }
118857
 
 
118858
 
  if( rc==SQLITE_OK ){
118859
 
    *ppReader = pReader;
118860
 
  }else{
118861
 
    sqlite3Fts3SegReaderFree(pReader);
118862
 
  }
118863
 
  return rc;
118864
 
}
118865
 
 
118866
 
/*
118867
 
** This is a comparison function used as a qsort() callback when sorting
118868
 
** an array of pending terms by term. This occurs as part of flushing
118869
 
** the contents of the pending-terms hash table to the database.
118870
 
*/
118871
 
static int fts3CompareElemByTerm(const void *lhs, const void *rhs){
118872
 
  char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
118873
 
  char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
118874
 
  int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
118875
 
  int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
118876
 
 
118877
 
  int n = (n1<n2 ? n1 : n2);
118878
 
  int c = memcmp(z1, z2, n);
118879
 
  if( c==0 ){
118880
 
    c = n1 - n2;
118881
 
  }
118882
 
  return c;
118883
 
}
118884
 
 
118885
 
/*
118886
 
** This function is used to allocate an Fts3SegReader that iterates through
118887
 
** a subset of the terms stored in the Fts3Table.pendingTerms array.
118888
 
*/
118889
 
SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
118890
 
  Fts3Table *p,                   /* Virtual table handle */
118891
 
  const char *zTerm,              /* Term to search for */
118892
 
  int nTerm,                      /* Size of buffer zTerm */
118893
 
  int isPrefix,                   /* True for a term-prefix query */
118894
 
  Fts3SegReader **ppReader        /* OUT: SegReader for pending-terms */
118895
 
){
118896
 
  Fts3SegReader *pReader = 0;     /* Fts3SegReader object to return */
118897
 
  Fts3HashElem **aElem = 0;       /* Array of term hash entries to scan */
118898
 
  int nElem = 0;                  /* Size of array at aElem */
118899
 
  int rc = SQLITE_OK;             /* Return Code */
118900
 
 
118901
 
  if( isPrefix ){
118902
 
    int nAlloc = 0;               /* Size of allocated array at aElem */
118903
 
    Fts3HashElem *pE = 0;         /* Iterator variable */
118904
 
 
118905
 
    for(pE=fts3HashFirst(&p->pendingTerms); pE; pE=fts3HashNext(pE)){
118906
 
      char *zKey = (char *)fts3HashKey(pE);
118907
 
      int nKey = fts3HashKeysize(pE);
118908
 
      if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
118909
 
        if( nElem==nAlloc ){
118910
 
          Fts3HashElem **aElem2;
118911
 
          nAlloc += 16;
118912
 
          aElem2 = (Fts3HashElem **)sqlite3_realloc(
118913
 
              aElem, nAlloc*sizeof(Fts3HashElem *)
118914
 
          );
118915
 
          if( !aElem2 ){
118916
 
            rc = SQLITE_NOMEM;
118917
 
            nElem = 0;
118918
 
            break;
118919
 
          }
118920
 
          aElem = aElem2;
118921
 
        }
118922
 
        aElem[nElem++] = pE;
118923
 
      }
118924
 
    }
118925
 
 
118926
 
    /* If more than one term matches the prefix, sort the Fts3HashElem
118927
 
    ** objects in term order using qsort(). This uses the same comparison
118928
 
    ** callback as is used when flushing terms to disk.
118929
 
    */
118930
 
    if( nElem>1 ){
118931
 
      qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
118932
 
    }
118933
 
 
118934
 
  }else{
118935
 
    Fts3HashElem *pE = fts3HashFindElem(&p->pendingTerms, zTerm, nTerm);
118936
 
    if( pE ){
118937
 
      aElem = &pE;
118938
 
      nElem = 1;
118939
 
    }
118940
 
  }
118941
 
 
118942
 
  if( nElem>0 ){
118943
 
    int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
118944
 
    pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
118945
 
    if( !pReader ){
118946
 
      rc = SQLITE_NOMEM;
118947
 
    }else{
118948
 
      memset(pReader, 0, nByte);
118949
 
      pReader->iIdx = 0x7FFFFFFF;
118950
 
      pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
118951
 
      memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
118952
 
    }
118953
 
  }
118954
 
 
118955
 
  if( isPrefix ){
118956
 
    sqlite3_free(aElem);
118957
 
  }
118958
 
  *ppReader = pReader;
118959
 
  return rc;
118960
 
}
118961
 
 
118962
 
/*
118963
 
** Compare the entries pointed to by two Fts3SegReader structures. 
118964
 
** Comparison is as follows:
118965
 
**
118966
 
**   1) EOF is greater than not EOF.
118967
 
**
118968
 
**   2) The current terms (if any) are compared using memcmp(). If one
118969
 
**      term is a prefix of another, the longer term is considered the
118970
 
**      larger.
118971
 
**
118972
 
**   3) By segment age. An older segment is considered larger.
118973
 
*/
118974
 
static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
118975
 
  int rc;
118976
 
  if( pLhs->aNode && pRhs->aNode ){
118977
 
    int rc2 = pLhs->nTerm - pRhs->nTerm;
118978
 
    if( rc2<0 ){
118979
 
      rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
118980
 
    }else{
118981
 
      rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
118982
 
    }
118983
 
    if( rc==0 ){
118984
 
      rc = rc2;
118985
 
    }
118986
 
  }else{
118987
 
    rc = (pLhs->aNode==0) - (pRhs->aNode==0);
118988
 
  }
118989
 
  if( rc==0 ){
118990
 
    rc = pRhs->iIdx - pLhs->iIdx;
118991
 
  }
118992
 
  assert( rc!=0 );
118993
 
  return rc;
118994
 
}
118995
 
 
118996
 
/*
118997
 
** A different comparison function for SegReader structures. In this
118998
 
** version, it is assumed that each SegReader points to an entry in
118999
 
** a doclist for identical terms. Comparison is made as follows:
119000
 
**
119001
 
**   1) EOF (end of doclist in this case) is greater than not EOF.
119002
 
**
119003
 
**   2) By current docid.
119004
 
**
119005
 
**   3) By segment age. An older segment is considered larger.
119006
 
*/
119007
 
static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
119008
 
  int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
119009
 
  if( rc==0 ){
119010
 
    if( pLhs->iDocid==pRhs->iDocid ){
119011
 
      rc = pRhs->iIdx - pLhs->iIdx;
119012
 
    }else{
119013
 
      rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
119014
 
    }
119015
 
  }
119016
 
  assert( pLhs->aNode && pRhs->aNode );
119017
 
  return rc;
119018
 
}
119019
 
 
119020
 
/*
119021
 
** Compare the term that the Fts3SegReader object passed as the first argument
119022
 
** points to with the term specified by arguments zTerm and nTerm. 
119023
 
**
119024
 
** If the pSeg iterator is already at EOF, return 0. Otherwise, return
119025
 
** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
119026
 
** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
119027
 
*/
119028
 
static int fts3SegReaderTermCmp(
119029
 
  Fts3SegReader *pSeg,            /* Segment reader object */
119030
 
  const char *zTerm,              /* Term to compare to */
119031
 
  int nTerm                       /* Size of term zTerm in bytes */
119032
 
){
119033
 
  int res = 0;
119034
 
  if( pSeg->aNode ){
119035
 
    if( pSeg->nTerm>nTerm ){
119036
 
      res = memcmp(pSeg->zTerm, zTerm, nTerm);
119037
 
    }else{
119038
 
      res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
119039
 
    }
119040
 
    if( res==0 ){
119041
 
      res = pSeg->nTerm-nTerm;
119042
 
    }
119043
 
  }
119044
 
  return res;
119045
 
}
119046
 
 
119047
 
/*
119048
 
** Argument apSegment is an array of nSegment elements. It is known that
119049
 
** the final (nSegment-nSuspect) members are already in sorted order
119050
 
** (according to the comparison function provided). This function shuffles
119051
 
** the array around until all entries are in sorted order.
119052
 
*/
119053
 
static void fts3SegReaderSort(
119054
 
  Fts3SegReader **apSegment,                     /* Array to sort entries of */
119055
 
  int nSegment,                                  /* Size of apSegment array */
119056
 
  int nSuspect,                                  /* Unsorted entry count */
119057
 
  int (*xCmp)(Fts3SegReader *, Fts3SegReader *)  /* Comparison function */
119058
 
){
119059
 
  int i;                          /* Iterator variable */
119060
 
 
119061
 
  assert( nSuspect<=nSegment );
119062
 
 
119063
 
  if( nSuspect==nSegment ) nSuspect--;
119064
 
  for(i=nSuspect-1; i>=0; i--){
119065
 
    int j;
119066
 
    for(j=i; j<(nSegment-1); j++){
119067
 
      Fts3SegReader *pTmp;
119068
 
      if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
119069
 
      pTmp = apSegment[j+1];
119070
 
      apSegment[j+1] = apSegment[j];
119071
 
      apSegment[j] = pTmp;
119072
 
    }
119073
 
  }
119074
 
 
119075
 
#ifndef NDEBUG
119076
 
  /* Check that the list really is sorted now. */
119077
 
  for(i=0; i<(nSuspect-1); i++){
119078
 
    assert( xCmp(apSegment[i], apSegment[i+1])<0 );
119079
 
  }
119080
 
#endif
119081
 
}
119082
 
 
119083
 
/* 
119084
 
** Insert a record into the %_segments table.
119085
 
*/
119086
 
static int fts3WriteSegment(
119087
 
  Fts3Table *p,                   /* Virtual table handle */
119088
 
  sqlite3_int64 iBlock,           /* Block id for new block */
119089
 
  char *z,                        /* Pointer to buffer containing block data */
119090
 
  int n                           /* Size of buffer z in bytes */
119091
 
){
119092
 
  sqlite3_stmt *pStmt;
119093
 
  int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
119094
 
  if( rc==SQLITE_OK ){
119095
 
    sqlite3_bind_int64(pStmt, 1, iBlock);
119096
 
    sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
119097
 
    sqlite3_step(pStmt);
119098
 
    rc = sqlite3_reset(pStmt);
119099
 
  }
119100
 
  return rc;
119101
 
}
119102
 
 
119103
 
/* 
119104
 
** Insert a record into the %_segdir table.
119105
 
*/
119106
 
static int fts3WriteSegdir(
119107
 
  Fts3Table *p,                   /* Virtual table handle */
119108
 
  int iLevel,                     /* Value for "level" field */
119109
 
  int iIdx,                       /* Value for "idx" field */
119110
 
  sqlite3_int64 iStartBlock,      /* Value for "start_block" field */
119111
 
  sqlite3_int64 iLeafEndBlock,    /* Value for "leaves_end_block" field */
119112
 
  sqlite3_int64 iEndBlock,        /* Value for "end_block" field */
119113
 
  char *zRoot,                    /* Blob value for "root" field */
119114
 
  int nRoot                       /* Number of bytes in buffer zRoot */
119115
 
){
119116
 
  sqlite3_stmt *pStmt;
119117
 
  int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
119118
 
  if( rc==SQLITE_OK ){
119119
 
    sqlite3_bind_int(pStmt, 1, iLevel);
119120
 
    sqlite3_bind_int(pStmt, 2, iIdx);
119121
 
    sqlite3_bind_int64(pStmt, 3, iStartBlock);
119122
 
    sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
119123
 
    sqlite3_bind_int64(pStmt, 5, iEndBlock);
119124
 
    sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
119125
 
    sqlite3_step(pStmt);
119126
 
    rc = sqlite3_reset(pStmt);
119127
 
  }
119128
 
  return rc;
119129
 
}
119130
 
 
119131
 
/*
119132
 
** Return the size of the common prefix (if any) shared by zPrev and
119133
 
** zNext, in bytes. For example, 
119134
 
**
119135
 
**   fts3PrefixCompress("abc", 3, "abcdef", 6)   // returns 3
119136
 
**   fts3PrefixCompress("abX", 3, "abcdef", 6)   // returns 2
119137
 
**   fts3PrefixCompress("abX", 3, "Xbcdef", 6)   // returns 0
119138
 
*/
119139
 
static int fts3PrefixCompress(
119140
 
  const char *zPrev,              /* Buffer containing previous term */
119141
 
  int nPrev,                      /* Size of buffer zPrev in bytes */
119142
 
  const char *zNext,              /* Buffer containing next term */
119143
 
  int nNext                       /* Size of buffer zNext in bytes */
119144
 
){
119145
 
  int n;
119146
 
  UNUSED_PARAMETER(nNext);
119147
 
  for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
119148
 
  return n;
119149
 
}
119150
 
 
119151
 
/*
119152
 
** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
119153
 
** (according to memcmp) than the previous term.
119154
 
*/
119155
 
static int fts3NodeAddTerm(
119156
 
  Fts3Table *p,                   /* Virtual table handle */
119157
 
  SegmentNode **ppTree,           /* IN/OUT: SegmentNode handle */ 
119158
 
  int isCopyTerm,                 /* True if zTerm/nTerm is transient */
119159
 
  const char *zTerm,              /* Pointer to buffer containing term */
119160
 
  int nTerm                       /* Size of term in bytes */
119161
 
){
119162
 
  SegmentNode *pTree = *ppTree;
119163
 
  int rc;
119164
 
  SegmentNode *pNew;
119165
 
 
119166
 
  /* First try to append the term to the current node. Return early if 
119167
 
  ** this is possible.
119168
 
  */
119169
 
  if( pTree ){
119170
 
    int nData = pTree->nData;     /* Current size of node in bytes */
119171
 
    int nReq = nData;             /* Required space after adding zTerm */
119172
 
    int nPrefix;                  /* Number of bytes of prefix compression */
119173
 
    int nSuffix;                  /* Suffix length */
119174
 
 
119175
 
    nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
119176
 
    nSuffix = nTerm-nPrefix;
119177
 
 
119178
 
    nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
119179
 
    if( nReq<=p->nNodeSize || !pTree->zTerm ){
119180
 
 
119181
 
      if( nReq>p->nNodeSize ){
119182
 
        /* An unusual case: this is the first term to be added to the node
119183
 
        ** and the static node buffer (p->nNodeSize bytes) is not large
119184
 
        ** enough. Use a separately malloced buffer instead This wastes
119185
 
        ** p->nNodeSize bytes, but since this scenario only comes about when
119186
 
        ** the database contain two terms that share a prefix of almost 2KB, 
119187
 
        ** this is not expected to be a serious problem. 
119188
 
        */
119189
 
        assert( pTree->aData==(char *)&pTree[1] );
119190
 
        pTree->aData = (char *)sqlite3_malloc(nReq);
119191
 
        if( !pTree->aData ){
119192
 
          return SQLITE_NOMEM;
119193
 
        }
119194
 
      }
119195
 
 
119196
 
      if( pTree->zTerm ){
119197
 
        /* There is no prefix-length field for first term in a node */
119198
 
        nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
119199
 
      }
119200
 
 
119201
 
      nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
119202
 
      memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
119203
 
      pTree->nData = nData + nSuffix;
119204
 
      pTree->nEntry++;
119205
 
 
119206
 
      if( isCopyTerm ){
119207
 
        if( pTree->nMalloc<nTerm ){
119208
 
          char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
119209
 
          if( !zNew ){
119210
 
            return SQLITE_NOMEM;
119211
 
          }
119212
 
          pTree->nMalloc = nTerm*2;
119213
 
          pTree->zMalloc = zNew;
119214
 
        }
119215
 
        pTree->zTerm = pTree->zMalloc;
119216
 
        memcpy(pTree->zTerm, zTerm, nTerm);
119217
 
        pTree->nTerm = nTerm;
119218
 
      }else{
119219
 
        pTree->zTerm = (char *)zTerm;
119220
 
        pTree->nTerm = nTerm;
119221
 
      }
119222
 
      return SQLITE_OK;
119223
 
    }
119224
 
  }
119225
 
 
119226
 
  /* If control flows to here, it was not possible to append zTerm to the
119227
 
  ** current node. Create a new node (a right-sibling of the current node).
119228
 
  ** If this is the first node in the tree, the term is added to it.
119229
 
  **
119230
 
  ** Otherwise, the term is not added to the new node, it is left empty for
119231
 
  ** now. Instead, the term is inserted into the parent of pTree. If pTree 
119232
 
  ** has no parent, one is created here.
119233
 
  */
119234
 
  pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
119235
 
  if( !pNew ){
119236
 
    return SQLITE_NOMEM;
119237
 
  }
119238
 
  memset(pNew, 0, sizeof(SegmentNode));
119239
 
  pNew->nData = 1 + FTS3_VARINT_MAX;
119240
 
  pNew->aData = (char *)&pNew[1];
119241
 
 
119242
 
  if( pTree ){
119243
 
    SegmentNode *pParent = pTree->pParent;
119244
 
    rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
119245
 
    if( pTree->pParent==0 ){
119246
 
      pTree->pParent = pParent;
119247
 
    }
119248
 
    pTree->pRight = pNew;
119249
 
    pNew->pLeftmost = pTree->pLeftmost;
119250
 
    pNew->pParent = pParent;
119251
 
    pNew->zMalloc = pTree->zMalloc;
119252
 
    pNew->nMalloc = pTree->nMalloc;
119253
 
    pTree->zMalloc = 0;
119254
 
  }else{
119255
 
    pNew->pLeftmost = pNew;
119256
 
    rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm); 
119257
 
  }
119258
 
 
119259
 
  *ppTree = pNew;
119260
 
  return rc;
119261
 
}
119262
 
 
119263
 
/*
119264
 
** Helper function for fts3NodeWrite().
119265
 
*/
119266
 
static int fts3TreeFinishNode(
119267
 
  SegmentNode *pTree, 
119268
 
  int iHeight, 
119269
 
  sqlite3_int64 iLeftChild
119270
 
){
119271
 
  int nStart;
119272
 
  assert( iHeight>=1 && iHeight<128 );
119273
 
  nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
119274
 
  pTree->aData[nStart] = (char)iHeight;
119275
 
  sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
119276
 
  return nStart;
119277
 
}
119278
 
 
119279
 
/*
119280
 
** Write the buffer for the segment node pTree and all of its peers to the
119281
 
** database. Then call this function recursively to write the parent of 
119282
 
** pTree and its peers to the database. 
119283
 
**
119284
 
** Except, if pTree is a root node, do not write it to the database. Instead,
119285
 
** set output variables *paRoot and *pnRoot to contain the root node.
119286
 
**
119287
 
** If successful, SQLITE_OK is returned and output variable *piLast is
119288
 
** set to the largest blockid written to the database (or zero if no
119289
 
** blocks were written to the db). Otherwise, an SQLite error code is 
119290
 
** returned.
119291
 
*/
119292
 
static int fts3NodeWrite(
119293
 
  Fts3Table *p,                   /* Virtual table handle */
119294
 
  SegmentNode *pTree,             /* SegmentNode handle */
119295
 
  int iHeight,                    /* Height of this node in tree */
119296
 
  sqlite3_int64 iLeaf,            /* Block id of first leaf node */
119297
 
  sqlite3_int64 iFree,            /* Block id of next free slot in %_segments */
119298
 
  sqlite3_int64 *piLast,          /* OUT: Block id of last entry written */
119299
 
  char **paRoot,                  /* OUT: Data for root node */
119300
 
  int *pnRoot                     /* OUT: Size of root node in bytes */
119301
 
){
119302
 
  int rc = SQLITE_OK;
119303
 
 
119304
 
  if( !pTree->pParent ){
119305
 
    /* Root node of the tree. */
119306
 
    int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
119307
 
    *piLast = iFree-1;
119308
 
    *pnRoot = pTree->nData - nStart;
119309
 
    *paRoot = &pTree->aData[nStart];
119310
 
  }else{
119311
 
    SegmentNode *pIter;
119312
 
    sqlite3_int64 iNextFree = iFree;
119313
 
    sqlite3_int64 iNextLeaf = iLeaf;
119314
 
    for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
119315
 
      int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
119316
 
      int nWrite = pIter->nData - nStart;
119317
 
  
119318
 
      rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
119319
 
      iNextFree++;
119320
 
      iNextLeaf += (pIter->nEntry+1);
119321
 
    }
119322
 
    if( rc==SQLITE_OK ){
119323
 
      assert( iNextLeaf==iFree );
119324
 
      rc = fts3NodeWrite(
119325
 
          p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
119326
 
      );
119327
 
    }
119328
 
  }
119329
 
 
119330
 
  return rc;
119331
 
}
119332
 
 
119333
 
/*
119334
 
** Free all memory allocations associated with the tree pTree.
119335
 
*/
119336
 
static void fts3NodeFree(SegmentNode *pTree){
119337
 
  if( pTree ){
119338
 
    SegmentNode *p = pTree->pLeftmost;
119339
 
    fts3NodeFree(p->pParent);
119340
 
    while( p ){
119341
 
      SegmentNode *pRight = p->pRight;
119342
 
      if( p->aData!=(char *)&p[1] ){
119343
 
        sqlite3_free(p->aData);
119344
 
      }
119345
 
      assert( pRight==0 || p->zMalloc==0 );
119346
 
      sqlite3_free(p->zMalloc);
119347
 
      sqlite3_free(p);
119348
 
      p = pRight;
119349
 
    }
119350
 
  }
119351
 
}
119352
 
 
119353
 
/*
119354
 
** Add a term to the segment being constructed by the SegmentWriter object
119355
 
** *ppWriter. When adding the first term to a segment, *ppWriter should
119356
 
** be passed NULL. This function will allocate a new SegmentWriter object
119357
 
** and return it via the input/output variable *ppWriter in this case.
119358
 
**
119359
 
** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
119360
 
*/
119361
 
static int fts3SegWriterAdd(
119362
 
  Fts3Table *p,                   /* Virtual table handle */
119363
 
  SegmentWriter **ppWriter,       /* IN/OUT: SegmentWriter handle */ 
119364
 
  int isCopyTerm,                 /* True if buffer zTerm must be copied */
119365
 
  const char *zTerm,              /* Pointer to buffer containing term */
119366
 
  int nTerm,                      /* Size of term in bytes */
119367
 
  const char *aDoclist,           /* Pointer to buffer containing doclist */
119368
 
  int nDoclist                    /* Size of doclist in bytes */
119369
 
){
119370
 
  int nPrefix;                    /* Size of term prefix in bytes */
119371
 
  int nSuffix;                    /* Size of term suffix in bytes */
119372
 
  int nReq;                       /* Number of bytes required on leaf page */
119373
 
  int nData;
119374
 
  SegmentWriter *pWriter = *ppWriter;
119375
 
 
119376
 
  if( !pWriter ){
119377
 
    int rc;
119378
 
    sqlite3_stmt *pStmt;
119379
 
 
119380
 
    /* Allocate the SegmentWriter structure */
119381
 
    pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
119382
 
    if( !pWriter ) return SQLITE_NOMEM;
119383
 
    memset(pWriter, 0, sizeof(SegmentWriter));
119384
 
    *ppWriter = pWriter;
119385
 
 
119386
 
    /* Allocate a buffer in which to accumulate data */
119387
 
    pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
119388
 
    if( !pWriter->aData ) return SQLITE_NOMEM;
119389
 
    pWriter->nSize = p->nNodeSize;
119390
 
 
119391
 
    /* Find the next free blockid in the %_segments table */
119392
 
    rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
119393
 
    if( rc!=SQLITE_OK ) return rc;
119394
 
    if( SQLITE_ROW==sqlite3_step(pStmt) ){
119395
 
      pWriter->iFree = sqlite3_column_int64(pStmt, 0);
119396
 
      pWriter->iFirst = pWriter->iFree;
119397
 
    }
119398
 
    rc = sqlite3_reset(pStmt);
119399
 
    if( rc!=SQLITE_OK ) return rc;
119400
 
  }
119401
 
  nData = pWriter->nData;
119402
 
 
119403
 
  nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
119404
 
  nSuffix = nTerm-nPrefix;
119405
 
 
119406
 
  /* Figure out how many bytes are required by this new entry */
119407
 
  nReq = sqlite3Fts3VarintLen(nPrefix) +    /* varint containing prefix size */
119408
 
    sqlite3Fts3VarintLen(nSuffix) +         /* varint containing suffix size */
119409
 
    nSuffix +                               /* Term suffix */
119410
 
    sqlite3Fts3VarintLen(nDoclist) +        /* Size of doclist */
119411
 
    nDoclist;                               /* Doclist data */
119412
 
 
119413
 
  if( nData>0 && nData+nReq>p->nNodeSize ){
119414
 
    int rc;
119415
 
 
119416
 
    /* The current leaf node is full. Write it out to the database. */
119417
 
    rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
119418
 
    if( rc!=SQLITE_OK ) return rc;
119419
 
 
119420
 
    /* Add the current term to the interior node tree. The term added to
119421
 
    ** the interior tree must:
119422
 
    **
119423
 
    **   a) be greater than the largest term on the leaf node just written
119424
 
    **      to the database (still available in pWriter->zTerm), and
119425
 
    **
119426
 
    **   b) be less than or equal to the term about to be added to the new
119427
 
    **      leaf node (zTerm/nTerm).
119428
 
    **
119429
 
    ** In other words, it must be the prefix of zTerm 1 byte longer than
119430
 
    ** the common prefix (if any) of zTerm and pWriter->zTerm.
119431
 
    */
119432
 
    assert( nPrefix<nTerm );
119433
 
    rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
119434
 
    if( rc!=SQLITE_OK ) return rc;
119435
 
 
119436
 
    nData = 0;
119437
 
    pWriter->nTerm = 0;
119438
 
 
119439
 
    nPrefix = 0;
119440
 
    nSuffix = nTerm;
119441
 
    nReq = 1 +                              /* varint containing prefix size */
119442
 
      sqlite3Fts3VarintLen(nTerm) +         /* varint containing suffix size */
119443
 
      nTerm +                               /* Term suffix */
119444
 
      sqlite3Fts3VarintLen(nDoclist) +      /* Size of doclist */
119445
 
      nDoclist;                             /* Doclist data */
119446
 
  }
119447
 
 
119448
 
  /* If the buffer currently allocated is too small for this entry, realloc
119449
 
  ** the buffer to make it large enough.
119450
 
  */
119451
 
  if( nReq>pWriter->nSize ){
119452
 
    char *aNew = sqlite3_realloc(pWriter->aData, nReq);
119453
 
    if( !aNew ) return SQLITE_NOMEM;
119454
 
    pWriter->aData = aNew;
119455
 
    pWriter->nSize = nReq;
119456
 
  }
119457
 
  assert( nData+nReq<=pWriter->nSize );
119458
 
 
119459
 
  /* Append the prefix-compressed term and doclist to the buffer. */
119460
 
  nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
119461
 
  nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
119462
 
  memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
119463
 
  nData += nSuffix;
119464
 
  nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
119465
 
  memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
119466
 
  pWriter->nData = nData + nDoclist;
119467
 
 
119468
 
  /* Save the current term so that it can be used to prefix-compress the next.
119469
 
  ** If the isCopyTerm parameter is true, then the buffer pointed to by
119470
 
  ** zTerm is transient, so take a copy of the term data. Otherwise, just
119471
 
  ** store a copy of the pointer.
119472
 
  */
119473
 
  if( isCopyTerm ){
119474
 
    if( nTerm>pWriter->nMalloc ){
119475
 
      char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
119476
 
      if( !zNew ){
119477
 
        return SQLITE_NOMEM;
119478
 
      }
119479
 
      pWriter->nMalloc = nTerm*2;
119480
 
      pWriter->zMalloc = zNew;
119481
 
      pWriter->zTerm = zNew;
119482
 
    }
119483
 
    assert( pWriter->zTerm==pWriter->zMalloc );
119484
 
    memcpy(pWriter->zTerm, zTerm, nTerm);
119485
 
  }else{
119486
 
    pWriter->zTerm = (char *)zTerm;
119487
 
  }
119488
 
  pWriter->nTerm = nTerm;
119489
 
 
119490
 
  return SQLITE_OK;
119491
 
}
119492
 
 
119493
 
/*
119494
 
** Flush all data associated with the SegmentWriter object pWriter to the
119495
 
** database. This function must be called after all terms have been added
119496
 
** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
119497
 
** returned. Otherwise, an SQLite error code.
119498
 
*/
119499
 
static int fts3SegWriterFlush(
119500
 
  Fts3Table *p,                   /* Virtual table handle */
119501
 
  SegmentWriter *pWriter,         /* SegmentWriter to flush to the db */
119502
 
  int iLevel,                     /* Value for 'level' column of %_segdir */
119503
 
  int iIdx                        /* Value for 'idx' column of %_segdir */
119504
 
){
119505
 
  int rc;                         /* Return code */
119506
 
  if( pWriter->pTree ){
119507
 
    sqlite3_int64 iLast = 0;      /* Largest block id written to database */
119508
 
    sqlite3_int64 iLastLeaf;      /* Largest leaf block id written to db */
119509
 
    char *zRoot = NULL;           /* Pointer to buffer containing root node */
119510
 
    int nRoot = 0;                /* Size of buffer zRoot */
119511
 
 
119512
 
    iLastLeaf = pWriter->iFree;
119513
 
    rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
119514
 
    if( rc==SQLITE_OK ){
119515
 
      rc = fts3NodeWrite(p, pWriter->pTree, 1,
119516
 
          pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
119517
 
    }
119518
 
    if( rc==SQLITE_OK ){
119519
 
      rc = fts3WriteSegdir(
119520
 
          p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot);
119521
 
    }
119522
 
  }else{
119523
 
    /* The entire tree fits on the root node. Write it to the segdir table. */
119524
 
    rc = fts3WriteSegdir(
119525
 
        p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData);
119526
 
  }
119527
 
  return rc;
119528
 
}
119529
 
 
119530
 
/*
119531
 
** Release all memory held by the SegmentWriter object passed as the 
119532
 
** first argument.
119533
 
*/
119534
 
static void fts3SegWriterFree(SegmentWriter *pWriter){
119535
 
  if( pWriter ){
119536
 
    sqlite3_free(pWriter->aData);
119537
 
    sqlite3_free(pWriter->zMalloc);
119538
 
    fts3NodeFree(pWriter->pTree);
119539
 
    sqlite3_free(pWriter);
119540
 
  }
119541
 
}
119542
 
 
119543
 
/*
119544
 
** The first value in the apVal[] array is assumed to contain an integer.
119545
 
** This function tests if there exist any documents with docid values that
119546
 
** are different from that integer. i.e. if deleting the document with docid
119547
 
** apVal[0] would mean the FTS3 table were empty.
119548
 
**
119549
 
** If successful, *pisEmpty is set to true if the table is empty except for
119550
 
** document apVal[0], or false otherwise, and SQLITE_OK is returned. If an
119551
 
** error occurs, an SQLite error code is returned.
119552
 
*/
119553
 
static int fts3IsEmpty(Fts3Table *p, sqlite3_value **apVal, int *pisEmpty){
119554
 
  sqlite3_stmt *pStmt;
119555
 
  int rc;
119556
 
  rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, apVal);
119557
 
  if( rc==SQLITE_OK ){
119558
 
    if( SQLITE_ROW==sqlite3_step(pStmt) ){
119559
 
      *pisEmpty = sqlite3_column_int(pStmt, 0);
119560
 
    }
119561
 
    rc = sqlite3_reset(pStmt);
119562
 
  }
119563
 
  return rc;
119564
 
}
119565
 
 
119566
 
/*
119567
 
** Set *pnSegment to the total number of segments in the database. Set
119568
 
** *pnMax to the largest segment level in the database (segment levels
119569
 
** are stored in the 'level' column of the %_segdir table).
119570
 
**
119571
 
** Return SQLITE_OK if successful, or an SQLite error code if not.
119572
 
*/
119573
 
static int fts3SegmentCountMax(Fts3Table *p, int *pnSegment, int *pnMax){
119574
 
  sqlite3_stmt *pStmt;
119575
 
  int rc;
119576
 
 
119577
 
  rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_COUNT_MAX, &pStmt, 0);
119578
 
  if( rc!=SQLITE_OK ) return rc;
119579
 
  if( SQLITE_ROW==sqlite3_step(pStmt) ){
119580
 
    *pnSegment = sqlite3_column_int(pStmt, 0);
119581
 
    *pnMax = sqlite3_column_int(pStmt, 1);
119582
 
  }
119583
 
  return sqlite3_reset(pStmt);
119584
 
}
119585
 
 
119586
 
/*
119587
 
** This function is used after merging multiple segments into a single large
119588
 
** segment to delete the old, now redundant, segment b-trees. Specifically,
119589
 
** it:
119590
 
** 
119591
 
**   1) Deletes all %_segments entries for the segments associated with 
119592
 
**      each of the SegReader objects in the array passed as the third 
119593
 
**      argument, and
119594
 
**
119595
 
**   2) deletes all %_segdir entries with level iLevel, or all %_segdir
119596
 
**      entries regardless of level if (iLevel<0).
119597
 
**
119598
 
** SQLITE_OK is returned if successful, otherwise an SQLite error code.
119599
 
*/
119600
 
static int fts3DeleteSegdir(
119601
 
  Fts3Table *p,                   /* Virtual table handle */
119602
 
  int iLevel,                     /* Level of %_segdir entries to delete */
119603
 
  Fts3SegReader **apSegment,      /* Array of SegReader objects */
119604
 
  int nReader                     /* Size of array apSegment */
119605
 
){
119606
 
  int rc;                         /* Return Code */
119607
 
  int i;                          /* Iterator variable */
119608
 
  sqlite3_stmt *pDelete;          /* SQL statement to delete rows */
119609
 
 
119610
 
  rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
119611
 
  for(i=0; rc==SQLITE_OK && i<nReader; i++){
119612
 
    Fts3SegReader *pSegment = apSegment[i];
119613
 
    if( pSegment->iStartBlock ){
119614
 
      sqlite3_bind_int64(pDelete, 1, pSegment->iStartBlock);
119615
 
      sqlite3_bind_int64(pDelete, 2, pSegment->iEndBlock);
119616
 
      sqlite3_step(pDelete);
119617
 
      rc = sqlite3_reset(pDelete);
119618
 
    }
119619
 
  }
119620
 
  if( rc!=SQLITE_OK ){
119621
 
    return rc;
119622
 
  }
119623
 
 
119624
 
  if( iLevel==FTS3_SEGCURSOR_ALL ){
119625
 
    fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
119626
 
  }else if( iLevel==FTS3_SEGCURSOR_PENDING ){
119627
 
    sqlite3Fts3PendingTermsClear(p);
119628
 
  }else{
119629
 
    assert( iLevel>=0 );
119630
 
    rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_BY_LEVEL, &pDelete, 0);
119631
 
    if( rc==SQLITE_OK ){
119632
 
      sqlite3_bind_int(pDelete, 1, iLevel);
119633
 
      sqlite3_step(pDelete);
119634
 
      rc = sqlite3_reset(pDelete);
119635
 
    }
119636
 
  }
119637
 
 
119638
 
  return rc;
119639
 
}
119640
 
 
119641
 
/*
119642
 
** When this function is called, buffer *ppList (size *pnList bytes) contains 
119643
 
** a position list that may (or may not) feature multiple columns. This
119644
 
** function adjusts the pointer *ppList and the length *pnList so that they
119645
 
** identify the subset of the position list that corresponds to column iCol.
119646
 
**
119647
 
** If there are no entries in the input position list for column iCol, then
119648
 
** *pnList is set to zero before returning.
119649
 
*/
119650
 
static void fts3ColumnFilter(
119651
 
  int iCol,                       /* Column to filter on */
119652
 
  char **ppList,                  /* IN/OUT: Pointer to position list */
119653
 
  int *pnList                     /* IN/OUT: Size of buffer *ppList in bytes */
119654
 
){
119655
 
  char *pList = *ppList;
119656
 
  int nList = *pnList;
119657
 
  char *pEnd = &pList[nList];
119658
 
  int iCurrent = 0;
119659
 
  char *p = pList;
119660
 
 
119661
 
  assert( iCol>=0 );
119662
 
  for(;;) {
119663
 
    char c = 0;
119664
 
    while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
119665
 
  
119666
 
    if( iCol==iCurrent ){
119667
 
      nList = (int)(p - pList);
119668
 
      break;
119669
 
    }
119670
 
 
119671
 
    nList -= (int)(p - pList);
119672
 
    pList = p;
119673
 
    if( nList==0 ){
119674
 
      break;
119675
 
    }
119676
 
    p = &pList[1];
119677
 
    p += sqlite3Fts3GetVarint32(p, &iCurrent);
119678
 
  }
119679
 
 
119680
 
  *ppList = pList;
119681
 
  *pnList = nList;
119682
 
}
119683
 
 
119684
 
SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
119685
 
  Fts3Table *p,                   /* Virtual table handle */
119686
 
  Fts3SegReaderCursor *pCsr,      /* Cursor object */
119687
 
  Fts3SegFilter *pFilter          /* Restrictions on range of iteration */
119688
 
){
119689
 
  int i;
119690
 
 
119691
 
  /* Initialize the cursor object */
119692
 
  pCsr->pFilter = pFilter;
119693
 
 
119694
 
  /* If the Fts3SegFilter defines a specific term (or term prefix) to search 
119695
 
  ** for, then advance each segment iterator until it points to a term of
119696
 
  ** equal or greater value than the specified term. This prevents many
119697
 
  ** unnecessary merge/sort operations for the case where single segment
119698
 
  ** b-tree leaf nodes contain more than one term.
119699
 
  */
119700
 
  for(i=0; i<pCsr->nSegment; i++){
119701
 
    int nTerm = pFilter->nTerm;
119702
 
    const char *zTerm = pFilter->zTerm;
119703
 
    Fts3SegReader *pSeg = pCsr->apSegment[i];
119704
 
    do {
119705
 
      int rc = fts3SegReaderNext(p, pSeg);
119706
 
      if( rc!=SQLITE_OK ) return rc;
119707
 
    }while( zTerm && fts3SegReaderTermCmp(pSeg, zTerm, nTerm)<0 );
119708
 
  }
119709
 
  fts3SegReaderSort(
119710
 
      pCsr->apSegment, pCsr->nSegment, pCsr->nSegment, fts3SegReaderCmp);
119711
 
 
119712
 
  return SQLITE_OK;
119713
 
}
119714
 
 
119715
 
SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
119716
 
  Fts3Table *p,                   /* Virtual table handle */
119717
 
  Fts3SegReaderCursor *pCsr       /* Cursor object */
119718
 
){
119719
 
  int rc = SQLITE_OK;
119720
 
 
119721
 
  int isIgnoreEmpty =  (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
119722
 
  int isRequirePos =   (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
119723
 
  int isColFilter =    (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
119724
 
  int isPrefix =       (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
119725
 
  int isScan =         (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
119726
 
 
119727
 
  Fts3SegReader **apSegment = pCsr->apSegment;
119728
 
  int nSegment = pCsr->nSegment;
119729
 
  Fts3SegFilter *pFilter = pCsr->pFilter;
119730
 
 
119731
 
  if( pCsr->nSegment==0 ) return SQLITE_OK;
119732
 
 
119733
 
  do {
119734
 
    int nMerge;
119735
 
    int i;
119736
 
  
119737
 
    /* Advance the first pCsr->nAdvance entries in the apSegment[] array
119738
 
    ** forward. Then sort the list in order of current term again.  
119739
 
    */
119740
 
    for(i=0; i<pCsr->nAdvance; i++){
119741
 
      rc = fts3SegReaderNext(p, apSegment[i]);
119742
 
      if( rc!=SQLITE_OK ) return rc;
119743
 
    }
119744
 
    fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
119745
 
    pCsr->nAdvance = 0;
119746
 
 
119747
 
    /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
119748
 
    assert( rc==SQLITE_OK );
119749
 
    if( apSegment[0]->aNode==0 ) break;
119750
 
 
119751
 
    pCsr->nTerm = apSegment[0]->nTerm;
119752
 
    pCsr->zTerm = apSegment[0]->zTerm;
119753
 
 
119754
 
    /* If this is a prefix-search, and if the term that apSegment[0] points
119755
 
    ** to does not share a suffix with pFilter->zTerm/nTerm, then all 
119756
 
    ** required callbacks have been made. In this case exit early.
119757
 
    **
119758
 
    ** Similarly, if this is a search for an exact match, and the first term
119759
 
    ** of segment apSegment[0] is not a match, exit early.
119760
 
    */
119761
 
    if( pFilter->zTerm && !isScan ){
119762
 
      if( pCsr->nTerm<pFilter->nTerm 
119763
 
       || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
119764
 
       || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm) 
119765
 
      ){
119766
 
        break;
119767
 
      }
119768
 
    }
119769
 
 
119770
 
    nMerge = 1;
119771
 
    while( nMerge<nSegment 
119772
 
        && apSegment[nMerge]->aNode
119773
 
        && apSegment[nMerge]->nTerm==pCsr->nTerm 
119774
 
        && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
119775
 
    ){
119776
 
      nMerge++;
119777
 
    }
119778
 
 
119779
 
    assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
119780
 
    if( nMerge==1 && !isIgnoreEmpty ){
119781
 
      pCsr->aDoclist = apSegment[0]->aDoclist;
119782
 
      pCsr->nDoclist = apSegment[0]->nDoclist;
119783
 
      rc = SQLITE_ROW;
119784
 
    }else{
119785
 
      int nDoclist = 0;           /* Size of doclist */
119786
 
      sqlite3_int64 iPrev = 0;    /* Previous docid stored in doclist */
119787
 
 
119788
 
      /* The current term of the first nMerge entries in the array
119789
 
      ** of Fts3SegReader objects is the same. The doclists must be merged
119790
 
      ** and a single term returned with the merged doclist.
119791
 
      */
119792
 
      for(i=0; i<nMerge; i++){
119793
 
        fts3SegReaderFirstDocid(apSegment[i]);
119794
 
      }
119795
 
      fts3SegReaderSort(apSegment, nMerge, nMerge, fts3SegReaderDoclistCmp);
119796
 
      while( apSegment[0]->pOffsetList ){
119797
 
        int j;                    /* Number of segments that share a docid */
119798
 
        char *pList;
119799
 
        int nList;
119800
 
        int nByte;
119801
 
        sqlite3_int64 iDocid = apSegment[0]->iDocid;
119802
 
        fts3SegReaderNextDocid(apSegment[0], &pList, &nList);
119803
 
        j = 1;
119804
 
        while( j<nMerge
119805
 
            && apSegment[j]->pOffsetList
119806
 
            && apSegment[j]->iDocid==iDocid
119807
 
        ){
119808
 
          fts3SegReaderNextDocid(apSegment[j], 0, 0);
119809
 
          j++;
119810
 
        }
119811
 
 
119812
 
        if( isColFilter ){
119813
 
          fts3ColumnFilter(pFilter->iCol, &pList, &nList);
119814
 
        }
119815
 
 
119816
 
        if( !isIgnoreEmpty || nList>0 ){
119817
 
          nByte = sqlite3Fts3VarintLen(iDocid-iPrev) + (isRequirePos?nList+1:0);
119818
 
          if( nDoclist+nByte>pCsr->nBuffer ){
119819
 
            char *aNew;
119820
 
            pCsr->nBuffer = (nDoclist+nByte)*2;
119821
 
            aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
119822
 
            if( !aNew ){
119823
 
              return SQLITE_NOMEM;
119824
 
            }
119825
 
            pCsr->aBuffer = aNew;
119826
 
          }
119827
 
          nDoclist += sqlite3Fts3PutVarint(
119828
 
              &pCsr->aBuffer[nDoclist], iDocid-iPrev
119829
 
          );
119830
 
          iPrev = iDocid;
119831
 
          if( isRequirePos ){
119832
 
            memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
119833
 
            nDoclist += nList;
119834
 
            pCsr->aBuffer[nDoclist++] = '\0';
119835
 
          }
119836
 
        }
119837
 
 
119838
 
        fts3SegReaderSort(apSegment, nMerge, j, fts3SegReaderDoclistCmp);
119839
 
      }
119840
 
      if( nDoclist>0 ){
119841
 
        pCsr->aDoclist = pCsr->aBuffer;
119842
 
        pCsr->nDoclist = nDoclist;
119843
 
        rc = SQLITE_ROW;
119844
 
      }
119845
 
    }
119846
 
    pCsr->nAdvance = nMerge;
119847
 
  }while( rc==SQLITE_OK );
119848
 
 
119849
 
  return rc;
119850
 
}
119851
 
 
119852
 
SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
119853
 
  Fts3SegReaderCursor *pCsr       /* Cursor object */
119854
 
){
119855
 
  if( pCsr ){
119856
 
    int i;
119857
 
    for(i=0; i<pCsr->nSegment; i++){
119858
 
      sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
119859
 
    }
119860
 
    sqlite3_free(pCsr->apSegment);
119861
 
    sqlite3_free(pCsr->aBuffer);
119862
 
 
119863
 
    pCsr->nSegment = 0;
119864
 
    pCsr->apSegment = 0;
119865
 
    pCsr->aBuffer = 0;
119866
 
  }
119867
 
}
119868
 
 
119869
 
/*
119870
 
** Merge all level iLevel segments in the database into a single 
119871
 
** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
119872
 
** single segment with a level equal to the numerically largest level 
119873
 
** currently present in the database.
119874
 
**
119875
 
** If this function is called with iLevel<0, but there is only one
119876
 
** segment in the database, SQLITE_DONE is returned immediately. 
119877
 
** Otherwise, if successful, SQLITE_OK is returned. If an error occurs, 
119878
 
** an SQLite error code is returned.
119879
 
*/
119880
 
static int fts3SegmentMerge(Fts3Table *p, int iLevel){
119881
 
  int rc;                         /* Return code */
119882
 
  int iIdx = 0;                   /* Index of new segment */
119883
 
  int iNewLevel = 0;              /* Level to create new segment at */
119884
 
  SegmentWriter *pWriter = 0;     /* Used to write the new, merged, segment */
119885
 
  Fts3SegFilter filter;           /* Segment term filter condition */
119886
 
  Fts3SegReaderCursor csr;        /* Cursor to iterate through level(s) */
119887
 
 
119888
 
  rc = sqlite3Fts3SegReaderCursor(p, iLevel, 0, 0, 1, 0, &csr);
119889
 
  if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
119890
 
 
119891
 
  if( iLevel==FTS3_SEGCURSOR_ALL ){
119892
 
    /* This call is to merge all segments in the database to a single
119893
 
    ** segment. The level of the new segment is equal to the the numerically 
119894
 
    ** greatest segment level currently present in the database. The index
119895
 
    ** of the new segment is always 0.  */
119896
 
    int nDummy; /* TODO: Remove this */
119897
 
    if( csr.nSegment==1 ){
119898
 
      rc = SQLITE_DONE;
119899
 
      goto finished;
119900
 
    }
119901
 
    rc = fts3SegmentCountMax(p, &nDummy, &iNewLevel);
119902
 
  }else{
119903
 
    /* This call is to merge all segments at level iLevel. Find the next
119904
 
    ** available segment index at level iLevel+1. The call to
119905
 
    ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to 
119906
 
    ** a single iLevel+2 segment if necessary.  */
119907
 
    iNewLevel = iLevel+1;
119908
 
    rc = fts3AllocateSegdirIdx(p, iNewLevel, &iIdx);
119909
 
  }
119910
 
  if( rc!=SQLITE_OK ) goto finished;
119911
 
  assert( csr.nSegment>0 );
119912
 
  assert( iNewLevel>=0 );
119913
 
 
119914
 
  memset(&filter, 0, sizeof(Fts3SegFilter));
119915
 
  filter.flags = FTS3_SEGMENT_REQUIRE_POS;
119916
 
  filter.flags |= (iLevel==FTS3_SEGCURSOR_ALL ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
119917
 
 
119918
 
  rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
119919
 
  while( SQLITE_OK==rc ){
119920
 
    rc = sqlite3Fts3SegReaderStep(p, &csr);
119921
 
    if( rc!=SQLITE_ROW ) break;
119922
 
    rc = fts3SegWriterAdd(p, &pWriter, 1, 
119923
 
        csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
119924
 
  }
119925
 
  if( rc!=SQLITE_OK ) goto finished;
119926
 
  assert( pWriter );
119927
 
 
119928
 
  rc = fts3DeleteSegdir(p, iLevel, csr.apSegment, csr.nSegment);
119929
 
  if( rc!=SQLITE_OK ) goto finished;
119930
 
  rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
119931
 
 
119932
 
 finished:
119933
 
  fts3SegWriterFree(pWriter);
119934
 
  sqlite3Fts3SegReaderFinish(&csr);
119935
 
  return rc;
119936
 
}
119937
 
 
119938
 
 
119939
 
/* 
119940
 
** Flush the contents of pendingTerms to a level 0 segment.
119941
 
*/
119942
 
SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
119943
 
  return fts3SegmentMerge(p, FTS3_SEGCURSOR_PENDING);
119944
 
}
119945
 
 
119946
 
/*
119947
 
** Encode N integers as varints into a blob.
119948
 
*/
119949
 
static void fts3EncodeIntArray(
119950
 
  int N,             /* The number of integers to encode */
119951
 
  u32 *a,            /* The integer values */
119952
 
  char *zBuf,        /* Write the BLOB here */
119953
 
  int *pNBuf         /* Write number of bytes if zBuf[] used here */
119954
 
){
119955
 
  int i, j;
119956
 
  for(i=j=0; i<N; i++){
119957
 
    j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
119958
 
  }
119959
 
  *pNBuf = j;
119960
 
}
119961
 
 
119962
 
/*
119963
 
** Decode a blob of varints into N integers
119964
 
*/
119965
 
static void fts3DecodeIntArray(
119966
 
  int N,             /* The number of integers to decode */
119967
 
  u32 *a,            /* Write the integer values */
119968
 
  const char *zBuf,  /* The BLOB containing the varints */
119969
 
  int nBuf           /* size of the BLOB */
119970
 
){
119971
 
  int i, j;
119972
 
  UNUSED_PARAMETER(nBuf);
119973
 
  for(i=j=0; i<N; i++){
119974
 
    sqlite3_int64 x;
119975
 
    j += sqlite3Fts3GetVarint(&zBuf[j], &x);
119976
 
    assert(j<=nBuf);
119977
 
    a[i] = (u32)(x & 0xffffffff);
119978
 
  }
119979
 
}
119980
 
 
119981
 
/*
119982
 
** Insert the sizes (in tokens) for each column of the document
119983
 
** with docid equal to p->iPrevDocid.  The sizes are encoded as
119984
 
** a blob of varints.
119985
 
*/
119986
 
static void fts3InsertDocsize(
119987
 
  int *pRC,         /* Result code */
119988
 
  Fts3Table *p,     /* Table into which to insert */
119989
 
  u32 *aSz          /* Sizes of each column */
119990
 
){
119991
 
  char *pBlob;             /* The BLOB encoding of the document size */
119992
 
  int nBlob;               /* Number of bytes in the BLOB */
119993
 
  sqlite3_stmt *pStmt;     /* Statement used to insert the encoding */
119994
 
  int rc;                  /* Result code from subfunctions */
119995
 
 
119996
 
  if( *pRC ) return;
119997
 
  pBlob = sqlite3_malloc( 10*p->nColumn );
119998
 
  if( pBlob==0 ){
119999
 
    *pRC = SQLITE_NOMEM;
120000
 
    return;
120001
 
  }
120002
 
  fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
120003
 
  rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
120004
 
  if( rc ){
120005
 
    sqlite3_free(pBlob);
120006
 
    *pRC = rc;
120007
 
    return;
120008
 
  }
120009
 
  sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
120010
 
  sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
120011
 
  sqlite3_step(pStmt);
120012
 
  *pRC = sqlite3_reset(pStmt);
120013
 
}
120014
 
 
120015
 
/*
120016
 
** Record 0 of the %_stat table contains a blob consisting of N varints,
120017
 
** where N is the number of user defined columns in the fts3 table plus
120018
 
** two. If nCol is the number of user defined columns, then values of the 
120019
 
** varints are set as follows:
120020
 
**
120021
 
**   Varint 0:       Total number of rows in the table.
120022
 
**
120023
 
**   Varint 1..nCol: For each column, the total number of tokens stored in
120024
 
**                   the column for all rows of the table.
120025
 
**
120026
 
**   Varint 1+nCol:  The total size, in bytes, of all text values in all
120027
 
**                   columns of all rows of the table.
120028
 
**
120029
 
*/
120030
 
static void fts3UpdateDocTotals(
120031
 
  int *pRC,                       /* The result code */
120032
 
  Fts3Table *p,                   /* Table being updated */
120033
 
  u32 *aSzIns,                    /* Size increases */
120034
 
  u32 *aSzDel,                    /* Size decreases */
120035
 
  int nChng                       /* Change in the number of documents */
120036
 
){
120037
 
  char *pBlob;             /* Storage for BLOB written into %_stat */
120038
 
  int nBlob;               /* Size of BLOB written into %_stat */
120039
 
  u32 *a;                  /* Array of integers that becomes the BLOB */
120040
 
  sqlite3_stmt *pStmt;     /* Statement for reading and writing */
120041
 
  int i;                   /* Loop counter */
120042
 
  int rc;                  /* Result code from subfunctions */
120043
 
 
120044
 
  const int nStat = p->nColumn+2;
120045
 
 
120046
 
  if( *pRC ) return;
120047
 
  a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
120048
 
  if( a==0 ){
120049
 
    *pRC = SQLITE_NOMEM;
120050
 
    return;
120051
 
  }
120052
 
  pBlob = (char*)&a[nStat];
120053
 
  rc = fts3SqlStmt(p, SQL_SELECT_DOCTOTAL, &pStmt, 0);
120054
 
  if( rc ){
120055
 
    sqlite3_free(a);
120056
 
    *pRC = rc;
120057
 
    return;
120058
 
  }
120059
 
  if( sqlite3_step(pStmt)==SQLITE_ROW ){
120060
 
    fts3DecodeIntArray(nStat, a,
120061
 
         sqlite3_column_blob(pStmt, 0),
120062
 
         sqlite3_column_bytes(pStmt, 0));
120063
 
  }else{
120064
 
    memset(a, 0, sizeof(u32)*(nStat) );
120065
 
  }
120066
 
  sqlite3_reset(pStmt);
120067
 
  if( nChng<0 && a[0]<(u32)(-nChng) ){
120068
 
    a[0] = 0;
120069
 
  }else{
120070
 
    a[0] += nChng;
120071
 
  }
120072
 
  for(i=0; i<p->nColumn+1; i++){
120073
 
    u32 x = a[i+1];
120074
 
    if( x+aSzIns[i] < aSzDel[i] ){
120075
 
      x = 0;
120076
 
    }else{
120077
 
      x = x + aSzIns[i] - aSzDel[i];
120078
 
    }
120079
 
    a[i+1] = x;
120080
 
  }
120081
 
  fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
120082
 
  rc = fts3SqlStmt(p, SQL_REPLACE_DOCTOTAL, &pStmt, 0);
120083
 
  if( rc ){
120084
 
    sqlite3_free(a);
120085
 
    *pRC = rc;
120086
 
    return;
120087
 
  }
120088
 
  sqlite3_bind_blob(pStmt, 1, pBlob, nBlob, SQLITE_STATIC);
120089
 
  sqlite3_step(pStmt);
120090
 
  *pRC = sqlite3_reset(pStmt);
120091
 
  sqlite3_free(a);
120092
 
}
120093
 
 
120094
 
/*
120095
 
** Handle a 'special' INSERT of the form:
120096
 
**
120097
 
**   "INSERT INTO tbl(tbl) VALUES(<expr>)"
120098
 
**
120099
 
** Argument pVal contains the result of <expr>. Currently the only 
120100
 
** meaningful value to insert is the text 'optimize'.
120101
 
*/
120102
 
static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
120103
 
  int rc;                         /* Return Code */
120104
 
  const char *zVal = (const char *)sqlite3_value_text(pVal);
120105
 
  int nVal = sqlite3_value_bytes(pVal);
120106
 
 
120107
 
  if( !zVal ){
120108
 
    return SQLITE_NOMEM;
120109
 
  }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
120110
 
    rc = fts3SegmentMerge(p, FTS3_SEGCURSOR_ALL);
120111
 
    if( rc==SQLITE_DONE ){
120112
 
      rc = SQLITE_OK;
120113
 
    }else{
120114
 
      sqlite3Fts3PendingTermsClear(p);
120115
 
    }
120116
 
#ifdef SQLITE_TEST
120117
 
  }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
120118
 
    p->nNodeSize = atoi(&zVal[9]);
120119
 
    rc = SQLITE_OK;
120120
 
  }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
120121
 
    p->nMaxPendingData = atoi(&zVal[11]);
120122
 
    rc = SQLITE_OK;
120123
 
#endif
120124
 
  }else{
120125
 
    rc = SQLITE_ERROR;
120126
 
  }
120127
 
 
120128
 
  sqlite3Fts3SegmentsClose(p);
120129
 
  return rc;
120130
 
}
120131
 
 
120132
 
/*
120133
 
** Return the deferred doclist associated with deferred token pDeferred.
120134
 
** This function assumes that sqlite3Fts3CacheDeferredDoclists() has already
120135
 
** been called to allocate and populate the doclist.
120136
 
*/
120137
 
SQLITE_PRIVATE char *sqlite3Fts3DeferredDoclist(Fts3DeferredToken *pDeferred, int *pnByte){
120138
 
  if( pDeferred->pList ){
120139
 
    *pnByte = pDeferred->pList->nData;
120140
 
    return pDeferred->pList->aData;
120141
 
  }
120142
 
  *pnByte = 0;
120143
 
  return 0;
120144
 
}
120145
 
 
120146
 
/*
120147
 
** Helper fucntion for FreeDeferredDoclists(). This function removes all
120148
 
** references to deferred doclists from within the tree of Fts3Expr 
120149
 
** structures headed by 
120150
 
*/
120151
 
static void fts3DeferredDoclistClear(Fts3Expr *pExpr){
120152
 
  if( pExpr ){
120153
 
    fts3DeferredDoclistClear(pExpr->pLeft);
120154
 
    fts3DeferredDoclistClear(pExpr->pRight);
120155
 
    if( pExpr->isLoaded ){
120156
 
      sqlite3_free(pExpr->aDoclist);
120157
 
      pExpr->isLoaded = 0;
120158
 
      pExpr->aDoclist = 0;
120159
 
      pExpr->nDoclist = 0;
120160
 
      pExpr->pCurrent = 0;
120161
 
      pExpr->iCurrent = 0;
120162
 
    }
120163
 
  }
120164
 
}
120165
 
 
120166
 
/*
120167
 
** Delete all cached deferred doclists. Deferred doclists are cached
120168
 
** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
120169
 
*/
120170
 
SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
120171
 
  Fts3DeferredToken *pDef;
120172
 
  for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
120173
 
    sqlite3_free(pDef->pList);
120174
 
    pDef->pList = 0;
120175
 
  }
120176
 
  if( pCsr->pDeferred ){
120177
 
    fts3DeferredDoclistClear(pCsr->pExpr);
120178
 
  }
120179
 
}
120180
 
 
120181
 
/*
120182
 
** Free all entries in the pCsr->pDeffered list. Entries are added to 
120183
 
** this list using sqlite3Fts3DeferToken().
120184
 
*/
120185
 
SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
120186
 
  Fts3DeferredToken *pDef;
120187
 
  Fts3DeferredToken *pNext;
120188
 
  for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
120189
 
    pNext = pDef->pNext;
120190
 
    sqlite3_free(pDef->pList);
120191
 
    sqlite3_free(pDef);
120192
 
  }
120193
 
  pCsr->pDeferred = 0;
120194
 
}
120195
 
 
120196
 
/*
120197
 
** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
120198
 
** based on the row that pCsr currently points to.
120199
 
**
120200
 
** A deferred-doclist is like any other doclist with position information
120201
 
** included, except that it only contains entries for a single row of the
120202
 
** table, not for all rows.
120203
 
*/
120204
 
SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
120205
 
  int rc = SQLITE_OK;             /* Return code */
120206
 
  if( pCsr->pDeferred ){
120207
 
    int i;                        /* Used to iterate through table columns */
120208
 
    sqlite3_int64 iDocid;         /* Docid of the row pCsr points to */
120209
 
    Fts3DeferredToken *pDef;      /* Used to iterate through deferred tokens */
120210
 
  
120211
 
    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
120212
 
    sqlite3_tokenizer *pT = p->pTokenizer;
120213
 
    sqlite3_tokenizer_module const *pModule = pT->pModule;
120214
 
   
120215
 
    assert( pCsr->isRequireSeek==0 );
120216
 
    iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
120217
 
  
120218
 
    for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
120219
 
      const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
120220
 
      sqlite3_tokenizer_cursor *pTC = 0;
120221
 
  
120222
 
      rc = pModule->xOpen(pT, zText, -1, &pTC);
120223
 
      while( rc==SQLITE_OK ){
120224
 
        char const *zToken;       /* Buffer containing token */
120225
 
        int nToken;               /* Number of bytes in token */
120226
 
        int iDum1, iDum2;         /* Dummy variables */
120227
 
        int iPos;                 /* Position of token in zText */
120228
 
  
120229
 
        pTC->pTokenizer = pT;
120230
 
        rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
120231
 
        for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
120232
 
          Fts3PhraseToken *pPT = pDef->pToken;
120233
 
          if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
120234
 
           && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
120235
 
           && (0==memcmp(zToken, pPT->z, pPT->n))
120236
 
          ){
120237
 
            fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
120238
 
          }
120239
 
        }
120240
 
      }
120241
 
      if( pTC ) pModule->xClose(pTC);
120242
 
      if( rc==SQLITE_DONE ) rc = SQLITE_OK;
120243
 
    }
120244
 
  
120245
 
    for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
120246
 
      if( pDef->pList ){
120247
 
        rc = fts3PendingListAppendVarint(&pDef->pList, 0);
120248
 
      }
120249
 
    }
120250
 
  }
120251
 
 
120252
 
  return rc;
120253
 
}
120254
 
 
120255
 
/*
120256
 
** Add an entry for token pToken to the pCsr->pDeferred list.
120257
 
*/
120258
 
SQLITE_PRIVATE int sqlite3Fts3DeferToken(
120259
 
  Fts3Cursor *pCsr,               /* Fts3 table cursor */
120260
 
  Fts3PhraseToken *pToken,        /* Token to defer */
120261
 
  int iCol                        /* Column that token must appear in (or -1) */
120262
 
){
120263
 
  Fts3DeferredToken *pDeferred;
120264
 
  pDeferred = sqlite3_malloc(sizeof(*pDeferred));
120265
 
  if( !pDeferred ){
120266
 
    return SQLITE_NOMEM;
120267
 
  }
120268
 
  memset(pDeferred, 0, sizeof(*pDeferred));
120269
 
  pDeferred->pToken = pToken;
120270
 
  pDeferred->pNext = pCsr->pDeferred; 
120271
 
  pDeferred->iCol = iCol;
120272
 
  pCsr->pDeferred = pDeferred;
120273
 
 
120274
 
  assert( pToken->pDeferred==0 );
120275
 
  pToken->pDeferred = pDeferred;
120276
 
 
120277
 
  return SQLITE_OK;
120278
 
}
120279
 
 
120280
 
 
120281
 
/*
120282
 
** This function does the work for the xUpdate method of FTS3 virtual
120283
 
** tables.
120284
 
*/
120285
 
SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
120286
 
  sqlite3_vtab *pVtab,            /* FTS3 vtab object */
120287
 
  int nArg,                       /* Size of argument array */
120288
 
  sqlite3_value **apVal,          /* Array of arguments */
120289
 
  sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
120290
 
){
120291
 
  Fts3Table *p = (Fts3Table *)pVtab;
120292
 
  int rc = SQLITE_OK;             /* Return Code */
120293
 
  int isRemove = 0;               /* True for an UPDATE or DELETE */
120294
 
  sqlite3_int64 iRemove = 0;      /* Rowid removed by UPDATE or DELETE */
120295
 
  u32 *aSzIns;                    /* Sizes of inserted documents */
120296
 
  u32 *aSzDel;                    /* Sizes of deleted documents */
120297
 
  int nChng = 0;                  /* Net change in number of documents */
120298
 
 
120299
 
  assert( p->pSegments==0 );
120300
 
 
120301
 
  /* Allocate space to hold the change in document sizes */
120302
 
  aSzIns = sqlite3_malloc( sizeof(aSzIns[0])*(p->nColumn+1)*2 );
120303
 
  if( aSzIns==0 ) return SQLITE_NOMEM;
120304
 
  aSzDel = &aSzIns[p->nColumn+1];
120305
 
  memset(aSzIns, 0, sizeof(aSzIns[0])*(p->nColumn+1)*2);
120306
 
 
120307
 
  /* If this is a DELETE or UPDATE operation, remove the old record. */
120308
 
  if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
120309
 
    int isEmpty = 0;
120310
 
    rc = fts3IsEmpty(p, apVal, &isEmpty);
120311
 
    if( rc==SQLITE_OK ){
120312
 
      if( isEmpty ){
120313
 
        /* Deleting this row means the whole table is empty. In this case
120314
 
        ** delete the contents of all three tables and throw away any
120315
 
        ** data in the pendingTerms hash table.
120316
 
        */
120317
 
        rc = fts3DeleteAll(p);
120318
 
      }else{
120319
 
        isRemove = 1;
120320
 
        iRemove = sqlite3_value_int64(apVal[0]);
120321
 
        rc = fts3PendingTermsDocid(p, iRemove);
120322
 
        fts3DeleteTerms(&rc, p, apVal, aSzDel);
120323
 
        fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, apVal);
120324
 
        if( p->bHasDocsize ){
120325
 
          fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, apVal);
120326
 
        }
120327
 
        nChng--;
120328
 
      }
120329
 
    }
120330
 
  }else if( sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL ){
120331
 
    sqlite3_free(aSzIns);
120332
 
    return fts3SpecialInsert(p, apVal[p->nColumn+2]);
120333
 
  }
120334
 
  
120335
 
  /* If this is an INSERT or UPDATE operation, insert the new record. */
120336
 
  if( nArg>1 && rc==SQLITE_OK ){
120337
 
    rc = fts3InsertData(p, apVal, pRowid);
120338
 
    if( rc==SQLITE_OK && (!isRemove || *pRowid!=iRemove) ){
120339
 
      rc = fts3PendingTermsDocid(p, *pRowid);
120340
 
    }
120341
 
    if( rc==SQLITE_OK ){
120342
 
      rc = fts3InsertTerms(p, apVal, aSzIns);
120343
 
    }
120344
 
    if( p->bHasDocsize ){
120345
 
      fts3InsertDocsize(&rc, p, aSzIns);
120346
 
    }
120347
 
    nChng++;
120348
 
  }
120349
 
 
120350
 
  if( p->bHasStat ){
120351
 
    fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
120352
 
  }
120353
 
 
120354
 
  sqlite3_free(aSzIns);
120355
 
  sqlite3Fts3SegmentsClose(p);
120356
 
  return rc;
120357
 
}
120358
 
 
120359
 
/* 
120360
 
** Flush any data in the pending-terms hash table to disk. If successful,
120361
 
** merge all segments in the database (including the new segment, if 
120362
 
** there was any data to flush) into a single segment. 
120363
 
*/
120364
 
SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
120365
 
  int rc;
120366
 
  rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
120367
 
  if( rc==SQLITE_OK ){
120368
 
    rc = fts3SegmentMerge(p, FTS3_SEGCURSOR_ALL);
120369
 
    if( rc==SQLITE_OK ){
120370
 
      rc = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
120371
 
      if( rc==SQLITE_OK ){
120372
 
        sqlite3Fts3PendingTermsClear(p);
120373
 
      }
120374
 
    }else{
120375
 
      sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
120376
 
      sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
120377
 
    }
120378
 
  }
120379
 
  sqlite3Fts3SegmentsClose(p);
120380
 
  return rc;
120381
 
}
120382
 
 
120383
 
#endif
120384
 
 
120385
 
/************** End of fts3_write.c ******************************************/
120386
 
/************** Begin file fts3_snippet.c ************************************/
120387
 
/*
120388
 
** 2009 Oct 23
120389
 
**
120390
 
** The author disclaims copyright to this source code.  In place of
120391
 
** a legal notice, here is a blessing:
120392
 
**
120393
 
**    May you do good and not evil.
120394
 
**    May you find forgiveness for yourself and forgive others.
120395
 
**    May you share freely, never taking more than you give.
120396
 
**
120397
 
******************************************************************************
120398
 
*/
120399
 
 
120400
 
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120401
 
 
120402
 
 
120403
 
/*
120404
 
** Characters that may appear in the second argument to matchinfo().
120405
 
*/
120406
 
#define FTS3_MATCHINFO_NPHRASE   'p'        /* 1 value */
120407
 
#define FTS3_MATCHINFO_NCOL      'c'        /* 1 value */
120408
 
#define FTS3_MATCHINFO_NDOC      'n'        /* 1 value */
120409
 
#define FTS3_MATCHINFO_AVGLENGTH 'a'        /* nCol values */
120410
 
#define FTS3_MATCHINFO_LENGTH    'l'        /* nCol values */
120411
 
#define FTS3_MATCHINFO_LCS       's'        /* nCol values */
120412
 
#define FTS3_MATCHINFO_HITS      'x'        /* 3*nCol*nPhrase values */
120413
 
 
120414
 
/*
120415
 
** The default value for the second argument to matchinfo(). 
120416
 
*/
120417
 
#define FTS3_MATCHINFO_DEFAULT   "pcx"
120418
 
 
120419
 
 
120420
 
/*
120421
 
** Used as an fts3ExprIterate() context when loading phrase doclists to
120422
 
** Fts3Expr.aDoclist[]/nDoclist.
120423
 
*/
120424
 
typedef struct LoadDoclistCtx LoadDoclistCtx;
120425
 
struct LoadDoclistCtx {
120426
 
  Fts3Cursor *pCsr;               /* FTS3 Cursor */
120427
 
  int nPhrase;                    /* Number of phrases seen so far */
120428
 
  int nToken;                     /* Number of tokens seen so far */
120429
 
};
120430
 
 
120431
 
/*
120432
 
** The following types are used as part of the implementation of the 
120433
 
** fts3BestSnippet() routine.
120434
 
*/
120435
 
typedef struct SnippetIter SnippetIter;
120436
 
typedef struct SnippetPhrase SnippetPhrase;
120437
 
typedef struct SnippetFragment SnippetFragment;
120438
 
 
120439
 
struct SnippetIter {
120440
 
  Fts3Cursor *pCsr;               /* Cursor snippet is being generated from */
120441
 
  int iCol;                       /* Extract snippet from this column */
120442
 
  int nSnippet;                   /* Requested snippet length (in tokens) */
120443
 
  int nPhrase;                    /* Number of phrases in query */
120444
 
  SnippetPhrase *aPhrase;         /* Array of size nPhrase */
120445
 
  int iCurrent;                   /* First token of current snippet */
120446
 
};
120447
 
 
120448
 
struct SnippetPhrase {
120449
 
  int nToken;                     /* Number of tokens in phrase */
120450
 
  char *pList;                    /* Pointer to start of phrase position list */
120451
 
  int iHead;                      /* Next value in position list */
120452
 
  char *pHead;                    /* Position list data following iHead */
120453
 
  int iTail;                      /* Next value in trailing position list */
120454
 
  char *pTail;                    /* Position list data following iTail */
120455
 
};
120456
 
 
120457
 
struct SnippetFragment {
120458
 
  int iCol;                       /* Column snippet is extracted from */
120459
 
  int iPos;                       /* Index of first token in snippet */
120460
 
  u64 covered;                    /* Mask of query phrases covered */
120461
 
  u64 hlmask;                     /* Mask of snippet terms to highlight */
120462
 
};
120463
 
 
120464
 
/*
120465
 
** This type is used as an fts3ExprIterate() context object while 
120466
 
** accumulating the data returned by the matchinfo() function.
120467
 
*/
120468
 
typedef struct MatchInfo MatchInfo;
120469
 
struct MatchInfo {
120470
 
  Fts3Cursor *pCursor;            /* FTS3 Cursor */
120471
 
  int nCol;                       /* Number of columns in table */
120472
 
  int nPhrase;                    /* Number of matchable phrases in query */
120473
 
  sqlite3_int64 nDoc;             /* Number of docs in database */
120474
 
  u32 *aMatchinfo;                /* Pre-allocated buffer */
120475
 
};
120476
 
 
120477
 
 
120478
 
 
120479
 
/*
120480
 
** The snippet() and offsets() functions both return text values. An instance
120481
 
** of the following structure is used to accumulate those values while the
120482
 
** functions are running. See fts3StringAppend() for details.
120483
 
*/
120484
 
typedef struct StrBuffer StrBuffer;
120485
 
struct StrBuffer {
120486
 
  char *z;                        /* Pointer to buffer containing string */
120487
 
  int n;                          /* Length of z in bytes (excl. nul-term) */
120488
 
  int nAlloc;                     /* Allocated size of buffer z in bytes */
120489
 
};
120490
 
 
120491
 
 
120492
 
/*
120493
 
** This function is used to help iterate through a position-list. A position
120494
 
** list is a list of unique integers, sorted from smallest to largest. Each
120495
 
** element of the list is represented by an FTS3 varint that takes the value
120496
 
** of the difference between the current element and the previous one plus
120497
 
** two. For example, to store the position-list:
120498
 
**
120499
 
**     4 9 113
120500
 
**
120501
 
** the three varints:
120502
 
**
120503
 
**     6 7 106
120504
 
**
120505
 
** are encoded.
120506
 
**
120507
 
** When this function is called, *pp points to the start of an element of
120508
 
** the list. *piPos contains the value of the previous entry in the list.
120509
 
** After it returns, *piPos contains the value of the next element of the
120510
 
** list and *pp is advanced to the following varint.
120511
 
*/
120512
 
static void fts3GetDeltaPosition(char **pp, int *piPos){
120513
 
  int iVal;
120514
 
  *pp += sqlite3Fts3GetVarint32(*pp, &iVal);
120515
 
  *piPos += (iVal-2);
120516
 
}
120517
 
 
120518
 
/*
120519
 
** Helper function for fts3ExprIterate() (see below).
120520
 
*/
120521
 
static int fts3ExprIterate2(
120522
 
  Fts3Expr *pExpr,                /* Expression to iterate phrases of */
120523
 
  int *piPhrase,                  /* Pointer to phrase counter */
120524
 
  int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
120525
 
  void *pCtx                      /* Second argument to pass to callback */
120526
 
){
120527
 
  int rc;                         /* Return code */
120528
 
  int eType = pExpr->eType;       /* Type of expression node pExpr */
120529
 
 
120530
 
  if( eType!=FTSQUERY_PHRASE ){
120531
 
    assert( pExpr->pLeft && pExpr->pRight );
120532
 
    rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
120533
 
    if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
120534
 
      rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
120535
 
    }
120536
 
  }else{
120537
 
    rc = x(pExpr, *piPhrase, pCtx);
120538
 
    (*piPhrase)++;
120539
 
  }
120540
 
  return rc;
120541
 
}
120542
 
 
120543
 
/*
120544
 
** Iterate through all phrase nodes in an FTS3 query, except those that
120545
 
** are part of a sub-tree that is the right-hand-side of a NOT operator.
120546
 
** For each phrase node found, the supplied callback function is invoked.
120547
 
**
120548
 
** If the callback function returns anything other than SQLITE_OK, 
120549
 
** the iteration is abandoned and the error code returned immediately.
120550
 
** Otherwise, SQLITE_OK is returned after a callback has been made for
120551
 
** all eligible phrase nodes.
120552
 
*/
120553
 
static int fts3ExprIterate(
120554
 
  Fts3Expr *pExpr,                /* Expression to iterate phrases of */
120555
 
  int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
120556
 
  void *pCtx                      /* Second argument to pass to callback */
120557
 
){
120558
 
  int iPhrase = 0;                /* Variable used as the phrase counter */
120559
 
  return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
120560
 
}
120561
 
 
120562
 
/*
120563
 
** The argument to this function is always a phrase node. Its doclist 
120564
 
** (Fts3Expr.aDoclist[]) and the doclists associated with all phrase nodes
120565
 
** to the left of this one in the query tree have already been loaded.
120566
 
**
120567
 
** If this phrase node is part of a series of phrase nodes joined by 
120568
 
** NEAR operators (and is not the left-most of said series), then elements are
120569
 
** removed from the phrases doclist consistent with the NEAR restriction. If
120570
 
** required, elements may be removed from the doclists of phrases to the
120571
 
** left of this one that are part of the same series of NEAR operator 
120572
 
** connected phrases.
120573
 
**
120574
 
** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
120575
 
*/
120576
 
static int fts3ExprNearTrim(Fts3Expr *pExpr){
120577
 
  int rc = SQLITE_OK;
120578
 
  Fts3Expr *pParent = pExpr->pParent;
120579
 
 
120580
 
  assert( pExpr->eType==FTSQUERY_PHRASE );
120581
 
  while( rc==SQLITE_OK
120582
 
   && pParent 
120583
 
   && pParent->eType==FTSQUERY_NEAR 
120584
 
   && pParent->pRight==pExpr 
120585
 
  ){
120586
 
    /* This expression (pExpr) is the right-hand-side of a NEAR operator. 
120587
 
    ** Find the expression to the left of the same operator.
120588
 
    */
120589
 
    int nNear = pParent->nNear;
120590
 
    Fts3Expr *pLeft = pParent->pLeft;
120591
 
 
120592
 
    if( pLeft->eType!=FTSQUERY_PHRASE ){
120593
 
      assert( pLeft->eType==FTSQUERY_NEAR );
120594
 
      assert( pLeft->pRight->eType==FTSQUERY_PHRASE );
120595
 
      pLeft = pLeft->pRight;
120596
 
    }
120597
 
 
120598
 
    rc = sqlite3Fts3ExprNearTrim(pLeft, pExpr, nNear);
120599
 
 
120600
 
    pExpr = pLeft;
120601
 
    pParent = pExpr->pParent;
120602
 
  }
120603
 
 
120604
 
  return rc;
120605
 
}
120606
 
 
120607
 
/*
120608
 
** This is an fts3ExprIterate() callback used while loading the doclists
120609
 
** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
120610
 
** fts3ExprLoadDoclists().
120611
 
*/
120612
 
static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
120613
 
  int rc = SQLITE_OK;
120614
 
  LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
120615
 
 
120616
 
  UNUSED_PARAMETER(iPhrase);
120617
 
 
120618
 
  p->nPhrase++;
120619
 
  p->nToken += pExpr->pPhrase->nToken;
120620
 
 
120621
 
  if( pExpr->isLoaded==0 ){
120622
 
    rc = sqlite3Fts3ExprLoadDoclist(p->pCsr, pExpr);
120623
 
    pExpr->isLoaded = 1;
120624
 
    if( rc==SQLITE_OK ){
120625
 
      rc = fts3ExprNearTrim(pExpr);
120626
 
    }
120627
 
  }
120628
 
 
120629
 
  return rc;
120630
 
}
120631
 
 
120632
 
/*
120633
 
** Load the doclists for each phrase in the query associated with FTS3 cursor
120634
 
** pCsr. 
120635
 
**
120636
 
** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable 
120637
 
** phrases in the expression (all phrases except those directly or 
120638
 
** indirectly descended from the right-hand-side of a NOT operator). If 
120639
 
** pnToken is not NULL, then it is set to the number of tokens in all
120640
 
** matchable phrases of the expression.
120641
 
*/
120642
 
static int fts3ExprLoadDoclists(
120643
 
  Fts3Cursor *pCsr,               /* Fts3 cursor for current query */
120644
 
  int *pnPhrase,                  /* OUT: Number of phrases in query */
120645
 
  int *pnToken                    /* OUT: Number of tokens in query */
120646
 
){
120647
 
  int rc;                         /* Return Code */
120648
 
  LoadDoclistCtx sCtx = {0,0,0};  /* Context for fts3ExprIterate() */
120649
 
  sCtx.pCsr = pCsr;
120650
 
  rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
120651
 
  if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
120652
 
  if( pnToken ) *pnToken = sCtx.nToken;
120653
 
  return rc;
120654
 
}
120655
 
 
120656
 
static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
120657
 
  (*(int *)ctx)++;
120658
 
  UNUSED_PARAMETER(pExpr);
120659
 
  UNUSED_PARAMETER(iPhrase);
120660
 
  return SQLITE_OK;
120661
 
}
120662
 
static int fts3ExprPhraseCount(Fts3Expr *pExpr){
120663
 
  int nPhrase = 0;
120664
 
  (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
120665
 
  return nPhrase;
120666
 
}
120667
 
 
120668
 
/*
120669
 
** Advance the position list iterator specified by the first two 
120670
 
** arguments so that it points to the first element with a value greater
120671
 
** than or equal to parameter iNext.
120672
 
*/
120673
 
static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
120674
 
  char *pIter = *ppIter;
120675
 
  if( pIter ){
120676
 
    int iIter = *piIter;
120677
 
 
120678
 
    while( iIter<iNext ){
120679
 
      if( 0==(*pIter & 0xFE) ){
120680
 
        iIter = -1;
120681
 
        pIter = 0;
120682
 
        break;
120683
 
      }
120684
 
      fts3GetDeltaPosition(&pIter, &iIter);
120685
 
    }
120686
 
 
120687
 
    *piIter = iIter;
120688
 
    *ppIter = pIter;
120689
 
  }
120690
 
}
120691
 
 
120692
 
/*
120693
 
** Advance the snippet iterator to the next candidate snippet.
120694
 
*/
120695
 
static int fts3SnippetNextCandidate(SnippetIter *pIter){
120696
 
  int i;                          /* Loop counter */
120697
 
 
120698
 
  if( pIter->iCurrent<0 ){
120699
 
    /* The SnippetIter object has just been initialized. The first snippet
120700
 
    ** candidate always starts at offset 0 (even if this candidate has a
120701
 
    ** score of 0.0).
120702
 
    */
120703
 
    pIter->iCurrent = 0;
120704
 
 
120705
 
    /* Advance the 'head' iterator of each phrase to the first offset that
120706
 
    ** is greater than or equal to (iNext+nSnippet).
120707
 
    */
120708
 
    for(i=0; i<pIter->nPhrase; i++){
120709
 
      SnippetPhrase *pPhrase = &pIter->aPhrase[i];
120710
 
      fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
120711
 
    }
120712
 
  }else{
120713
 
    int iStart;
120714
 
    int iEnd = 0x7FFFFFFF;
120715
 
 
120716
 
    for(i=0; i<pIter->nPhrase; i++){
120717
 
      SnippetPhrase *pPhrase = &pIter->aPhrase[i];
120718
 
      if( pPhrase->pHead && pPhrase->iHead<iEnd ){
120719
 
        iEnd = pPhrase->iHead;
120720
 
      }
120721
 
    }
120722
 
    if( iEnd==0x7FFFFFFF ){
120723
 
      return 1;
120724
 
    }
120725
 
 
120726
 
    pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
120727
 
    for(i=0; i<pIter->nPhrase; i++){
120728
 
      SnippetPhrase *pPhrase = &pIter->aPhrase[i];
120729
 
      fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
120730
 
      fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
120731
 
    }
120732
 
  }
120733
 
 
120734
 
  return 0;
120735
 
}
120736
 
 
120737
 
/*
120738
 
** Retrieve information about the current candidate snippet of snippet 
120739
 
** iterator pIter.
120740
 
*/
120741
 
static void fts3SnippetDetails(
120742
 
  SnippetIter *pIter,             /* Snippet iterator */
120743
 
  u64 mCovered,                   /* Bitmask of phrases already covered */
120744
 
  int *piToken,                   /* OUT: First token of proposed snippet */
120745
 
  int *piScore,                   /* OUT: "Score" for this snippet */
120746
 
  u64 *pmCover,                   /* OUT: Bitmask of phrases covered */
120747
 
  u64 *pmHighlight                /* OUT: Bitmask of terms to highlight */
120748
 
){
120749
 
  int iStart = pIter->iCurrent;   /* First token of snippet */
120750
 
  int iScore = 0;                 /* Score of this snippet */
120751
 
  int i;                          /* Loop counter */
120752
 
  u64 mCover = 0;                 /* Mask of phrases covered by this snippet */
120753
 
  u64 mHighlight = 0;             /* Mask of tokens to highlight in snippet */
120754
 
 
120755
 
  for(i=0; i<pIter->nPhrase; i++){
120756
 
    SnippetPhrase *pPhrase = &pIter->aPhrase[i];
120757
 
    if( pPhrase->pTail ){
120758
 
      char *pCsr = pPhrase->pTail;
120759
 
      int iCsr = pPhrase->iTail;
120760
 
 
120761
 
      while( iCsr<(iStart+pIter->nSnippet) ){
120762
 
        int j;
120763
 
        u64 mPhrase = (u64)1 << i;
120764
 
        u64 mPos = (u64)1 << (iCsr - iStart);
120765
 
        assert( iCsr>=iStart );
120766
 
        if( (mCover|mCovered)&mPhrase ){
120767
 
          iScore++;
120768
 
        }else{
120769
 
          iScore += 1000;
120770
 
        }
120771
 
        mCover |= mPhrase;
120772
 
 
120773
 
        for(j=0; j<pPhrase->nToken; j++){
120774
 
          mHighlight |= (mPos>>j);
120775
 
        }
120776
 
 
120777
 
        if( 0==(*pCsr & 0x0FE) ) break;
120778
 
        fts3GetDeltaPosition(&pCsr, &iCsr);
120779
 
      }
120780
 
    }
120781
 
  }
120782
 
 
120783
 
  /* Set the output variables before returning. */
120784
 
  *piToken = iStart;
120785
 
  *piScore = iScore;
120786
 
  *pmCover = mCover;
120787
 
  *pmHighlight = mHighlight;
120788
 
}
120789
 
 
120790
 
/*
120791
 
** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
120792
 
** Each invocation populates an element of the SnippetIter.aPhrase[] array.
120793
 
*/
120794
 
static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
120795
 
  SnippetIter *p = (SnippetIter *)ctx;
120796
 
  SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
120797
 
  char *pCsr;
120798
 
 
120799
 
  pPhrase->nToken = pExpr->pPhrase->nToken;
120800
 
 
120801
 
  pCsr = sqlite3Fts3FindPositions(pExpr, p->pCsr->iPrevId, p->iCol);
120802
 
  if( pCsr ){
120803
 
    int iFirst = 0;
120804
 
    pPhrase->pList = pCsr;
120805
 
    fts3GetDeltaPosition(&pCsr, &iFirst);
120806
 
    pPhrase->pHead = pCsr;
120807
 
    pPhrase->pTail = pCsr;
120808
 
    pPhrase->iHead = iFirst;
120809
 
    pPhrase->iTail = iFirst;
120810
 
  }else{
120811
 
    assert( pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 );
120812
 
  }
120813
 
 
120814
 
  return SQLITE_OK;
120815
 
}
120816
 
 
120817
 
/*
120818
 
** Select the fragment of text consisting of nFragment contiguous tokens 
120819
 
** from column iCol that represent the "best" snippet. The best snippet
120820
 
** is the snippet with the highest score, where scores are calculated
120821
 
** by adding:
120822
 
**
120823
 
**   (a) +1 point for each occurence of a matchable phrase in the snippet.
120824
 
**
120825
 
**   (b) +1000 points for the first occurence of each matchable phrase in 
120826
 
**       the snippet for which the corresponding mCovered bit is not set.
120827
 
**
120828
 
** The selected snippet parameters are stored in structure *pFragment before
120829
 
** returning. The score of the selected snippet is stored in *piScore
120830
 
** before returning.
120831
 
*/
120832
 
static int fts3BestSnippet(
120833
 
  int nSnippet,                   /* Desired snippet length */
120834
 
  Fts3Cursor *pCsr,               /* Cursor to create snippet for */
120835
 
  int iCol,                       /* Index of column to create snippet from */
120836
 
  u64 mCovered,                   /* Mask of phrases already covered */
120837
 
  u64 *pmSeen,                    /* IN/OUT: Mask of phrases seen */
120838
 
  SnippetFragment *pFragment,     /* OUT: Best snippet found */
120839
 
  int *piScore                    /* OUT: Score of snippet pFragment */
120840
 
){
120841
 
  int rc;                         /* Return Code */
120842
 
  int nList;                      /* Number of phrases in expression */
120843
 
  SnippetIter sIter;              /* Iterates through snippet candidates */
120844
 
  int nByte;                      /* Number of bytes of space to allocate */
120845
 
  int iBestScore = -1;            /* Best snippet score found so far */
120846
 
  int i;                          /* Loop counter */
120847
 
 
120848
 
  memset(&sIter, 0, sizeof(sIter));
120849
 
 
120850
 
  /* Iterate through the phrases in the expression to count them. The same
120851
 
  ** callback makes sure the doclists are loaded for each phrase.
120852
 
  */
120853
 
  rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
120854
 
  if( rc!=SQLITE_OK ){
120855
 
    return rc;
120856
 
  }
120857
 
 
120858
 
  /* Now that it is known how many phrases there are, allocate and zero
120859
 
  ** the required space using malloc().
120860
 
  */
120861
 
  nByte = sizeof(SnippetPhrase) * nList;
120862
 
  sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte);
120863
 
  if( !sIter.aPhrase ){
120864
 
    return SQLITE_NOMEM;
120865
 
  }
120866
 
  memset(sIter.aPhrase, 0, nByte);
120867
 
 
120868
 
  /* Initialize the contents of the SnippetIter object. Then iterate through
120869
 
  ** the set of phrases in the expression to populate the aPhrase[] array.
120870
 
  */
120871
 
  sIter.pCsr = pCsr;
120872
 
  sIter.iCol = iCol;
120873
 
  sIter.nSnippet = nSnippet;
120874
 
  sIter.nPhrase = nList;
120875
 
  sIter.iCurrent = -1;
120876
 
  (void)fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter);
120877
 
 
120878
 
  /* Set the *pmSeen output variable. */
120879
 
  for(i=0; i<nList; i++){
120880
 
    if( sIter.aPhrase[i].pHead ){
120881
 
      *pmSeen |= (u64)1 << i;
120882
 
    }
120883
 
  }
120884
 
 
120885
 
  /* Loop through all candidate snippets. Store the best snippet in 
120886
 
  ** *pFragment. Store its associated 'score' in iBestScore.
120887
 
  */
120888
 
  pFragment->iCol = iCol;
120889
 
  while( !fts3SnippetNextCandidate(&sIter) ){
120890
 
    int iPos;
120891
 
    int iScore;
120892
 
    u64 mCover;
120893
 
    u64 mHighlight;
120894
 
    fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover, &mHighlight);
120895
 
    assert( iScore>=0 );
120896
 
    if( iScore>iBestScore ){
120897
 
      pFragment->iPos = iPos;
120898
 
      pFragment->hlmask = mHighlight;
120899
 
      pFragment->covered = mCover;
120900
 
      iBestScore = iScore;
120901
 
    }
120902
 
  }
120903
 
 
120904
 
  sqlite3_free(sIter.aPhrase);
120905
 
  *piScore = iBestScore;
120906
 
  return SQLITE_OK;
120907
 
}
120908
 
 
120909
 
 
120910
 
/*
120911
 
** Append a string to the string-buffer passed as the first argument.
120912
 
**
120913
 
** If nAppend is negative, then the length of the string zAppend is
120914
 
** determined using strlen().
120915
 
*/
120916
 
static int fts3StringAppend(
120917
 
  StrBuffer *pStr,                /* Buffer to append to */
120918
 
  const char *zAppend,            /* Pointer to data to append to buffer */
120919
 
  int nAppend                     /* Size of zAppend in bytes (or -1) */
120920
 
){
120921
 
  if( nAppend<0 ){
120922
 
    nAppend = (int)strlen(zAppend);
120923
 
  }
120924
 
 
120925
 
  /* If there is insufficient space allocated at StrBuffer.z, use realloc()
120926
 
  ** to grow the buffer until so that it is big enough to accomadate the
120927
 
  ** appended data.
120928
 
  */
120929
 
  if( pStr->n+nAppend+1>=pStr->nAlloc ){
120930
 
    int nAlloc = pStr->nAlloc+nAppend+100;
120931
 
    char *zNew = sqlite3_realloc(pStr->z, nAlloc);
120932
 
    if( !zNew ){
120933
 
      return SQLITE_NOMEM;
120934
 
    }
120935
 
    pStr->z = zNew;
120936
 
    pStr->nAlloc = nAlloc;
120937
 
  }
120938
 
 
120939
 
  /* Append the data to the string buffer. */
120940
 
  memcpy(&pStr->z[pStr->n], zAppend, nAppend);
120941
 
  pStr->n += nAppend;
120942
 
  pStr->z[pStr->n] = '\0';
120943
 
 
120944
 
  return SQLITE_OK;
120945
 
}
120946
 
 
120947
 
/*
120948
 
** The fts3BestSnippet() function often selects snippets that end with a
120949
 
** query term. That is, the final term of the snippet is always a term
120950
 
** that requires highlighting. For example, if 'X' is a highlighted term
120951
 
** and '.' is a non-highlighted term, BestSnippet() may select:
120952
 
**
120953
 
**     ........X.....X
120954
 
**
120955
 
** This function "shifts" the beginning of the snippet forward in the 
120956
 
** document so that there are approximately the same number of 
120957
 
** non-highlighted terms to the right of the final highlighted term as there
120958
 
** are to the left of the first highlighted term. For example, to this:
120959
 
**
120960
 
**     ....X.....X....
120961
 
**
120962
 
** This is done as part of extracting the snippet text, not when selecting
120963
 
** the snippet. Snippet selection is done based on doclists only, so there
120964
 
** is no way for fts3BestSnippet() to know whether or not the document 
120965
 
** actually contains terms that follow the final highlighted term. 
120966
 
*/
120967
 
static int fts3SnippetShift(
120968
 
  Fts3Table *pTab,                /* FTS3 table snippet comes from */
120969
 
  int nSnippet,                   /* Number of tokens desired for snippet */
120970
 
  const char *zDoc,               /* Document text to extract snippet from */
120971
 
  int nDoc,                       /* Size of buffer zDoc in bytes */
120972
 
  int *piPos,                     /* IN/OUT: First token of snippet */
120973
 
  u64 *pHlmask                    /* IN/OUT: Mask of tokens to highlight */
120974
 
){
120975
 
  u64 hlmask = *pHlmask;          /* Local copy of initial highlight-mask */
120976
 
 
120977
 
  if( hlmask ){
120978
 
    int nLeft;                    /* Tokens to the left of first highlight */
120979
 
    int nRight;                   /* Tokens to the right of last highlight */
120980
 
    int nDesired;                 /* Ideal number of tokens to shift forward */
120981
 
 
120982
 
    for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
120983
 
    for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
120984
 
    nDesired = (nLeft-nRight)/2;
120985
 
 
120986
 
    /* Ideally, the start of the snippet should be pushed forward in the
120987
 
    ** document nDesired tokens. This block checks if there are actually
120988
 
    ** nDesired tokens to the right of the snippet. If so, *piPos and
120989
 
    ** *pHlMask are updated to shift the snippet nDesired tokens to the
120990
 
    ** right. Otherwise, the snippet is shifted by the number of tokens
120991
 
    ** available.
120992
 
    */
120993
 
    if( nDesired>0 ){
120994
 
      int nShift;                 /* Number of tokens to shift snippet by */
120995
 
      int iCurrent = 0;           /* Token counter */
120996
 
      int rc;                     /* Return Code */
120997
 
      sqlite3_tokenizer_module *pMod;
120998
 
      sqlite3_tokenizer_cursor *pC;
120999
 
      pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
121000
 
 
121001
 
      /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
121002
 
      ** or more tokens in zDoc/nDoc.
121003
 
      */
121004
 
      rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC);
121005
 
      if( rc!=SQLITE_OK ){
121006
 
        return rc;
121007
 
      }
121008
 
      pC->pTokenizer = pTab->pTokenizer;
121009
 
      while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
121010
 
        const char *ZDUMMY; int DUMMY1, DUMMY2, DUMMY3;
121011
 
        rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
121012
 
      }
121013
 
      pMod->xClose(pC);
121014
 
      if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
121015
 
 
121016
 
      nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
121017
 
      assert( nShift<=nDesired );
121018
 
      if( nShift>0 ){
121019
 
        *piPos += nShift;
121020
 
        *pHlmask = hlmask >> nShift;
121021
 
      }
121022
 
    }
121023
 
  }
121024
 
  return SQLITE_OK;
121025
 
}
121026
 
 
121027
 
/*
121028
 
** Extract the snippet text for fragment pFragment from cursor pCsr and
121029
 
** append it to string buffer pOut.
121030
 
*/
121031
 
static int fts3SnippetText(
121032
 
  Fts3Cursor *pCsr,               /* FTS3 Cursor */
121033
 
  SnippetFragment *pFragment,     /* Snippet to extract */
121034
 
  int iFragment,                  /* Fragment number */
121035
 
  int isLast,                     /* True for final fragment in snippet */
121036
 
  int nSnippet,                   /* Number of tokens in extracted snippet */
121037
 
  const char *zOpen,              /* String inserted before highlighted term */
121038
 
  const char *zClose,             /* String inserted after highlighted term */
121039
 
  const char *zEllipsis,          /* String inserted between snippets */
121040
 
  StrBuffer *pOut                 /* Write output here */
121041
 
){
121042
 
  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
121043
 
  int rc;                         /* Return code */
121044
 
  const char *zDoc;               /* Document text to extract snippet from */
121045
 
  int nDoc;                       /* Size of zDoc in bytes */
121046
 
  int iCurrent = 0;               /* Current token number of document */
121047
 
  int iEnd = 0;                   /* Byte offset of end of current token */
121048
 
  int isShiftDone = 0;            /* True after snippet is shifted */
121049
 
  int iPos = pFragment->iPos;     /* First token of snippet */
121050
 
  u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
121051
 
  int iCol = pFragment->iCol+1;   /* Query column to extract text from */
121052
 
  sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
121053
 
  sqlite3_tokenizer_cursor *pC;   /* Tokenizer cursor open on zDoc/nDoc */
121054
 
  const char *ZDUMMY;             /* Dummy argument used with tokenizer */
121055
 
  int DUMMY1;                     /* Dummy argument used with tokenizer */
121056
 
  
121057
 
  zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
121058
 
  if( zDoc==0 ){
121059
 
    if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
121060
 
      return SQLITE_NOMEM;
121061
 
    }
121062
 
    return SQLITE_OK;
121063
 
  }
121064
 
  nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
121065
 
 
121066
 
  /* Open a token cursor on the document. */
121067
 
  pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
121068
 
  rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC);
121069
 
  if( rc!=SQLITE_OK ){
121070
 
    return rc;
121071
 
  }
121072
 
  pC->pTokenizer = pTab->pTokenizer;
121073
 
 
121074
 
  while( rc==SQLITE_OK ){
121075
 
    int iBegin;                   /* Offset in zDoc of start of token */
121076
 
    int iFin;                     /* Offset in zDoc of end of token */
121077
 
    int isHighlight;              /* True for highlighted terms */
121078
 
 
121079
 
    rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
121080
 
    if( rc!=SQLITE_OK ){
121081
 
      if( rc==SQLITE_DONE ){
121082
 
        /* Special case - the last token of the snippet is also the last token
121083
 
        ** of the column. Append any punctuation that occurred between the end
121084
 
        ** of the previous token and the end of the document to the output. 
121085
 
        ** Then break out of the loop. */
121086
 
        rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
121087
 
      }
121088
 
      break;
121089
 
    }
121090
 
    if( iCurrent<iPos ){ continue; }
121091
 
 
121092
 
    if( !isShiftDone ){
121093
 
      int n = nDoc - iBegin;
121094
 
      rc = fts3SnippetShift(pTab, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask);
121095
 
      isShiftDone = 1;
121096
 
 
121097
 
      /* Now that the shift has been done, check if the initial "..." are
121098
 
      ** required. They are required if (a) this is not the first fragment,
121099
 
      ** or (b) this fragment does not begin at position 0 of its column. 
121100
 
      */
121101
 
      if( rc==SQLITE_OK && (iPos>0 || iFragment>0) ){
121102
 
        rc = fts3StringAppend(pOut, zEllipsis, -1);
121103
 
      }
121104
 
      if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
121105
 
    }
121106
 
 
121107
 
    if( iCurrent>=(iPos+nSnippet) ){
121108
 
      if( isLast ){
121109
 
        rc = fts3StringAppend(pOut, zEllipsis, -1);
121110
 
      }
121111
 
      break;
121112
 
    }
121113
 
 
121114
 
    /* Set isHighlight to true if this term should be highlighted. */
121115
 
    isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
121116
 
 
121117
 
    if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
121118
 
    if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
121119
 
    if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
121120
 
    if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
121121
 
 
121122
 
    iEnd = iFin;
121123
 
  }
121124
 
 
121125
 
  pMod->xClose(pC);
121126
 
  return rc;
121127
 
}
121128
 
 
121129
 
 
121130
 
/*
121131
 
** This function is used to count the entries in a column-list (a 
121132
 
** delta-encoded list of term offsets within a single column of a single 
121133
 
** row). When this function is called, *ppCollist should point to the
121134
 
** beginning of the first varint in the column-list (the varint that
121135
 
** contains the position of the first matching term in the column data).
121136
 
** Before returning, *ppCollist is set to point to the first byte after
121137
 
** the last varint in the column-list (either the 0x00 signifying the end
121138
 
** of the position-list, or the 0x01 that precedes the column number of
121139
 
** the next column in the position-list).
121140
 
**
121141
 
** The number of elements in the column-list is returned.
121142
 
*/
121143
 
static int fts3ColumnlistCount(char **ppCollist){
121144
 
  char *pEnd = *ppCollist;
121145
 
  char c = 0;
121146
 
  int nEntry = 0;
121147
 
 
121148
 
  /* A column-list is terminated by either a 0x01 or 0x00. */
121149
 
  while( 0xFE & (*pEnd | c) ){
121150
 
    c = *pEnd++ & 0x80;
121151
 
    if( !c ) nEntry++;
121152
 
  }
121153
 
 
121154
 
  *ppCollist = pEnd;
121155
 
  return nEntry;
121156
 
}
121157
 
 
121158
 
static void fts3LoadColumnlistCounts(char **pp, u32 *aOut, int isGlobal){
121159
 
  char *pCsr = *pp;
121160
 
  while( *pCsr ){
121161
 
    int nHit;
121162
 
    sqlite3_int64 iCol = 0;
121163
 
    if( *pCsr==0x01 ){
121164
 
      pCsr++;
121165
 
      pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
121166
 
    }
121167
 
    nHit = fts3ColumnlistCount(&pCsr);
121168
 
    assert( nHit>0 );
121169
 
    if( isGlobal ){
121170
 
      aOut[iCol*3+1]++;
121171
 
    }
121172
 
    aOut[iCol*3] += nHit;
121173
 
  }
121174
 
  pCsr++;
121175
 
  *pp = pCsr;
121176
 
}
121177
 
 
121178
 
/*
121179
 
** fts3ExprIterate() callback used to collect the "global" matchinfo stats
121180
 
** for a single query. 
121181
 
**
121182
 
** fts3ExprIterate() callback to load the 'global' elements of a
121183
 
** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements 
121184
 
** of the matchinfo array that are constant for all rows returned by the 
121185
 
** current query.
121186
 
**
121187
 
** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
121188
 
** function populates Matchinfo.aMatchinfo[] as follows:
121189
 
**
121190
 
**   for(iCol=0; iCol<nCol; iCol++){
121191
 
**     aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
121192
 
**     aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
121193
 
**   }
121194
 
**
121195
 
** where X is the number of matches for phrase iPhrase is column iCol of all
121196
 
** rows of the table. Y is the number of rows for which column iCol contains
121197
 
** at least one instance of phrase iPhrase.
121198
 
**
121199
 
** If the phrase pExpr consists entirely of deferred tokens, then all X and
121200
 
** Y values are set to nDoc, where nDoc is the number of documents in the 
121201
 
** file system. This is done because the full-text index doclist is required
121202
 
** to calculate these values properly, and the full-text index doclist is
121203
 
** not available for deferred tokens.
121204
 
*/
121205
 
static int fts3ExprGlobalHitsCb(
121206
 
  Fts3Expr *pExpr,                /* Phrase expression node */
121207
 
  int iPhrase,                    /* Phrase number (numbered from zero) */
121208
 
  void *pCtx                      /* Pointer to MatchInfo structure */
121209
 
){
121210
 
  MatchInfo *p = (MatchInfo *)pCtx;
121211
 
  Fts3Cursor *pCsr = p->pCursor;
121212
 
  char *pIter;
121213
 
  char *pEnd;
121214
 
  char *pFree = 0;
121215
 
  u32 *aOut = &p->aMatchinfo[3*iPhrase*p->nCol];
121216
 
 
121217
 
  assert( pExpr->isLoaded );
121218
 
  assert( pExpr->eType==FTSQUERY_PHRASE );
121219
 
 
121220
 
  if( pCsr->pDeferred ){
121221
 
    Fts3Phrase *pPhrase = pExpr->pPhrase;
121222
 
    int ii;
121223
 
    for(ii=0; ii<pPhrase->nToken; ii++){
121224
 
      if( pPhrase->aToken[ii].bFulltext ) break;
121225
 
    }
121226
 
    if( ii<pPhrase->nToken ){
121227
 
      int nFree = 0;
121228
 
      int rc = sqlite3Fts3ExprLoadFtDoclist(pCsr, pExpr, &pFree, &nFree);
121229
 
      if( rc!=SQLITE_OK ) return rc;
121230
 
      pIter = pFree;
121231
 
      pEnd = &pFree[nFree];
121232
 
    }else{
121233
 
      int iCol;                   /* Column index */
121234
 
      for(iCol=0; iCol<p->nCol; iCol++){
121235
 
        aOut[iCol*3 + 1] = (u32)p->nDoc;
121236
 
        aOut[iCol*3 + 2] = (u32)p->nDoc;
121237
 
      }
121238
 
      return SQLITE_OK;
121239
 
    }
121240
 
  }else{
121241
 
    pIter = pExpr->aDoclist;
121242
 
    pEnd = &pExpr->aDoclist[pExpr->nDoclist];
121243
 
  }
121244
 
 
121245
 
  /* Fill in the global hit count matrix row for this phrase. */
121246
 
  while( pIter<pEnd ){
121247
 
    while( *pIter++ & 0x80 );      /* Skip past docid. */
121248
 
    fts3LoadColumnlistCounts(&pIter, &aOut[1], 1);
121249
 
  }
121250
 
 
121251
 
  sqlite3_free(pFree);
121252
 
  return SQLITE_OK;
121253
 
}
121254
 
 
121255
 
/*
121256
 
** fts3ExprIterate() callback used to collect the "local" part of the
121257
 
** FTS3_MATCHINFO_HITS array. The local stats are those elements of the 
121258
 
** array that are different for each row returned by the query.
121259
 
*/
121260
 
static int fts3ExprLocalHitsCb(
121261
 
  Fts3Expr *pExpr,                /* Phrase expression node */
121262
 
  int iPhrase,                    /* Phrase number */
121263
 
  void *pCtx                      /* Pointer to MatchInfo structure */
121264
 
){
121265
 
  MatchInfo *p = (MatchInfo *)pCtx;
121266
 
  int iStart = iPhrase * p->nCol * 3;
121267
 
  int i;
121268
 
 
121269
 
  for(i=0; i<p->nCol; i++) p->aMatchinfo[iStart+i*3] = 0;
121270
 
 
121271
 
  if( pExpr->aDoclist ){
121272
 
    char *pCsr;
121273
 
 
121274
 
    pCsr = sqlite3Fts3FindPositions(pExpr, p->pCursor->iPrevId, -1);
121275
 
    if( pCsr ){
121276
 
      fts3LoadColumnlistCounts(&pCsr, &p->aMatchinfo[iStart], 0);
121277
 
    }
121278
 
  }
121279
 
 
121280
 
  return SQLITE_OK;
121281
 
}
121282
 
 
121283
 
static int fts3MatchinfoCheck(
121284
 
  Fts3Table *pTab, 
121285
 
  char cArg,
121286
 
  char **pzErr
121287
 
){
121288
 
  if( (cArg==FTS3_MATCHINFO_NPHRASE)
121289
 
   || (cArg==FTS3_MATCHINFO_NCOL)
121290
 
   || (cArg==FTS3_MATCHINFO_NDOC && pTab->bHasStat)
121291
 
   || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bHasStat)
121292
 
   || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
121293
 
   || (cArg==FTS3_MATCHINFO_LCS)
121294
 
   || (cArg==FTS3_MATCHINFO_HITS)
121295
 
  ){
121296
 
    return SQLITE_OK;
121297
 
  }
121298
 
  *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg);
121299
 
  return SQLITE_ERROR;
121300
 
}
121301
 
 
121302
 
static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
121303
 
  int nVal;                       /* Number of integers output by cArg */
121304
 
 
121305
 
  switch( cArg ){
121306
 
    case FTS3_MATCHINFO_NDOC:
121307
 
    case FTS3_MATCHINFO_NPHRASE: 
121308
 
    case FTS3_MATCHINFO_NCOL: 
121309
 
      nVal = 1;
121310
 
      break;
121311
 
 
121312
 
    case FTS3_MATCHINFO_AVGLENGTH:
121313
 
    case FTS3_MATCHINFO_LENGTH:
121314
 
    case FTS3_MATCHINFO_LCS:
121315
 
      nVal = pInfo->nCol;
121316
 
      break;
121317
 
 
121318
 
    default:
121319
 
      assert( cArg==FTS3_MATCHINFO_HITS );
121320
 
      nVal = pInfo->nCol * pInfo->nPhrase * 3;
121321
 
      break;
121322
 
  }
121323
 
 
121324
 
  return nVal;
121325
 
}
121326
 
 
121327
 
static int fts3MatchinfoSelectDoctotal(
121328
 
  Fts3Table *pTab,
121329
 
  sqlite3_stmt **ppStmt,
121330
 
  sqlite3_int64 *pnDoc,
121331
 
  const char **paLen
121332
 
){
121333
 
  sqlite3_stmt *pStmt;
121334
 
  const char *a;
121335
 
  sqlite3_int64 nDoc;
121336
 
 
121337
 
  if( !*ppStmt ){
121338
 
    int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
121339
 
    if( rc!=SQLITE_OK ) return rc;
121340
 
  }
121341
 
  pStmt = *ppStmt;
121342
 
  assert( sqlite3_data_count(pStmt)==1 );
121343
 
 
121344
 
  a = sqlite3_column_blob(pStmt, 0);
121345
 
  a += sqlite3Fts3GetVarint(a, &nDoc);
121346
 
  if( nDoc==0 ) return SQLITE_CORRUPT;
121347
 
  *pnDoc = (u32)nDoc;
121348
 
 
121349
 
  if( paLen ) *paLen = a;
121350
 
  return SQLITE_OK;
121351
 
}
121352
 
 
121353
 
/*
121354
 
** An instance of the following structure is used to store state while 
121355
 
** iterating through a multi-column position-list corresponding to the
121356
 
** hits for a single phrase on a single row in order to calculate the
121357
 
** values for a matchinfo() FTS3_MATCHINFO_LCS request.
121358
 
*/
121359
 
typedef struct LcsIterator LcsIterator;
121360
 
struct LcsIterator {
121361
 
  Fts3Expr *pExpr;                /* Pointer to phrase expression */
121362
 
  char *pRead;                    /* Cursor used to iterate through aDoclist */
121363
 
  int iPosOffset;                 /* Tokens count up to end of this phrase */
121364
 
  int iCol;                       /* Current column number */
121365
 
  int iPos;                       /* Current position */
121366
 
};
121367
 
 
121368
 
/* 
121369
 
** If LcsIterator.iCol is set to the following value, the iterator has
121370
 
** finished iterating through all offsets for all columns.
121371
 
*/
121372
 
#define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
121373
 
 
121374
 
static int fts3MatchinfoLcsCb(
121375
 
  Fts3Expr *pExpr,                /* Phrase expression node */
121376
 
  int iPhrase,                    /* Phrase number (numbered from zero) */
121377
 
  void *pCtx                      /* Pointer to MatchInfo structure */
121378
 
){
121379
 
  LcsIterator *aIter = (LcsIterator *)pCtx;
121380
 
  aIter[iPhrase].pExpr = pExpr;
121381
 
  return SQLITE_OK;
121382
 
}
121383
 
 
121384
 
/*
121385
 
** Advance the iterator passed as an argument to the next position. Return
121386
 
** 1 if the iterator is at EOF or if it now points to the start of the
121387
 
** position list for the next column.
121388
 
*/
121389
 
static int fts3LcsIteratorAdvance(LcsIterator *pIter){
121390
 
  char *pRead = pIter->pRead;
121391
 
  sqlite3_int64 iRead;
121392
 
  int rc = 0;
121393
 
 
121394
 
  pRead += sqlite3Fts3GetVarint(pRead, &iRead);
121395
 
  if( iRead==0 ){
121396
 
    pIter->iCol = LCS_ITERATOR_FINISHED;
121397
 
    rc = 1;
121398
 
  }else{
121399
 
    if( iRead==1 ){
121400
 
      pRead += sqlite3Fts3GetVarint(pRead, &iRead);
121401
 
      pIter->iCol = (int)iRead;
121402
 
      pIter->iPos = pIter->iPosOffset;
121403
 
      pRead += sqlite3Fts3GetVarint(pRead, &iRead);
121404
 
      rc = 1;
121405
 
    }
121406
 
    pIter->iPos += (int)(iRead-2);
121407
 
  }
121408
 
 
121409
 
  pIter->pRead = pRead;
121410
 
  return rc;
121411
 
}
121412
 
  
121413
 
/*
121414
 
** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag. 
121415
 
**
121416
 
** If the call is successful, the longest-common-substring lengths for each
121417
 
** column are written into the first nCol elements of the pInfo->aMatchinfo[] 
121418
 
** array before returning. SQLITE_OK is returned in this case.
121419
 
**
121420
 
** Otherwise, if an error occurs, an SQLite error code is returned and the
121421
 
** data written to the first nCol elements of pInfo->aMatchinfo[] is 
121422
 
** undefined.
121423
 
*/
121424
 
static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
121425
 
  LcsIterator *aIter;
121426
 
  int i;
121427
 
  int iCol;
121428
 
  int nToken = 0;
121429
 
 
121430
 
  /* Allocate and populate the array of LcsIterator objects. The array
121431
 
  ** contains one element for each matchable phrase in the query.
121432
 
  **/
121433
 
  aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
121434
 
  if( !aIter ) return SQLITE_NOMEM;
121435
 
  memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
121436
 
  (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
121437
 
  for(i=0; i<pInfo->nPhrase; i++){
121438
 
    LcsIterator *pIter = &aIter[i];
121439
 
    nToken -= pIter->pExpr->pPhrase->nToken;
121440
 
    pIter->iPosOffset = nToken;
121441
 
    pIter->pRead = sqlite3Fts3FindPositions(pIter->pExpr, pCsr->iPrevId, -1);
121442
 
    if( pIter->pRead ){
121443
 
      pIter->iPos = pIter->iPosOffset;
121444
 
      fts3LcsIteratorAdvance(&aIter[i]);
121445
 
    }else{
121446
 
      pIter->iCol = LCS_ITERATOR_FINISHED;
121447
 
    }
121448
 
  }
121449
 
 
121450
 
  for(iCol=0; iCol<pInfo->nCol; iCol++){
121451
 
    int nLcs = 0;                 /* LCS value for this column */
121452
 
    int nLive = 0;                /* Number of iterators in aIter not at EOF */
121453
 
 
121454
 
    /* Loop through the iterators in aIter[]. Set nLive to the number of
121455
 
    ** iterators that point to a position-list corresponding to column iCol.
121456
 
    */
121457
 
    for(i=0; i<pInfo->nPhrase; i++){
121458
 
      assert( aIter[i].iCol>=iCol );
121459
 
      if( aIter[i].iCol==iCol ) nLive++;
121460
 
    }
121461
 
 
121462
 
    /* The following loop runs until all iterators in aIter[] have finished
121463
 
    ** iterating through positions in column iCol. Exactly one of the 
121464
 
    ** iterators is advanced each time the body of the loop is run.
121465
 
    */
121466
 
    while( nLive>0 ){
121467
 
      LcsIterator *pAdv = 0;      /* The iterator to advance by one position */
121468
 
      int nThisLcs = 0;           /* LCS for the current iterator positions */
121469
 
 
121470
 
      for(i=0; i<pInfo->nPhrase; i++){
121471
 
        LcsIterator *pIter = &aIter[i];
121472
 
        if( iCol!=pIter->iCol ){  
121473
 
          /* This iterator is already at EOF for this column. */
121474
 
          nThisLcs = 0;
121475
 
        }else{
121476
 
          if( pAdv==0 || pIter->iPos<pAdv->iPos ){
121477
 
            pAdv = pIter;
121478
 
          }
121479
 
          if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
121480
 
            nThisLcs++;
121481
 
          }else{
121482
 
            nThisLcs = 1;
121483
 
          }
121484
 
          if( nThisLcs>nLcs ) nLcs = nThisLcs;
121485
 
        }
121486
 
      }
121487
 
      if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
121488
 
    }
121489
 
 
121490
 
    pInfo->aMatchinfo[iCol] = nLcs;
121491
 
  }
121492
 
 
121493
 
  sqlite3_free(aIter);
121494
 
  return SQLITE_OK;
121495
 
}
121496
 
 
121497
 
/*
121498
 
** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
121499
 
** be returned by the matchinfo() function. Argument zArg contains the 
121500
 
** format string passed as the second argument to matchinfo (or the
121501
 
** default value "pcx" if no second argument was specified). The format
121502
 
** string has already been validated and the pInfo->aMatchinfo[] array
121503
 
** is guaranteed to be large enough for the output.
121504
 
**
121505
 
** If bGlobal is true, then populate all fields of the matchinfo() output.
121506
 
** If it is false, then assume that those fields that do not change between
121507
 
** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
121508
 
** have already been populated.
121509
 
**
121510
 
** Return SQLITE_OK if successful, or an SQLite error code if an error 
121511
 
** occurs. If a value other than SQLITE_OK is returned, the state the
121512
 
** pInfo->aMatchinfo[] buffer is left in is undefined.
121513
 
*/
121514
 
static int fts3MatchinfoValues(
121515
 
  Fts3Cursor *pCsr,               /* FTS3 cursor object */
121516
 
  int bGlobal,                    /* True to grab the global stats */
121517
 
  MatchInfo *pInfo,               /* Matchinfo context object */
121518
 
  const char *zArg                /* Matchinfo format string */
121519
 
){
121520
 
  int rc = SQLITE_OK;
121521
 
  int i;
121522
 
  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
121523
 
  sqlite3_stmt *pSelect = 0;
121524
 
 
121525
 
  for(i=0; rc==SQLITE_OK && zArg[i]; i++){
121526
 
 
121527
 
    switch( zArg[i] ){
121528
 
      case FTS3_MATCHINFO_NPHRASE:
121529
 
        if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
121530
 
        break;
121531
 
 
121532
 
      case FTS3_MATCHINFO_NCOL:
121533
 
        if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
121534
 
        break;
121535
 
        
121536
 
      case FTS3_MATCHINFO_NDOC:
121537
 
        if( bGlobal ){
121538
 
          sqlite3_int64 nDoc;
121539
 
          rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0);
121540
 
          pInfo->aMatchinfo[0] = (u32)nDoc;
121541
 
        }
121542
 
        break;
121543
 
 
121544
 
      case FTS3_MATCHINFO_AVGLENGTH: 
121545
 
        if( bGlobal ){
121546
 
          sqlite3_int64 nDoc;     /* Number of rows in table */
121547
 
          const char *a;          /* Aggregate column length array */
121548
 
 
121549
 
          rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
121550
 
          if( rc==SQLITE_OK ){
121551
 
            int iCol;
121552
 
            for(iCol=0; iCol<pInfo->nCol; iCol++){
121553
 
              u32 iVal;
121554
 
              sqlite3_int64 nToken;
121555
 
              a += sqlite3Fts3GetVarint(a, &nToken);
121556
 
              iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
121557
 
              pInfo->aMatchinfo[iCol] = iVal;
121558
 
            }
121559
 
          }
121560
 
        }
121561
 
        break;
121562
 
 
121563
 
      case FTS3_MATCHINFO_LENGTH: {
121564
 
        sqlite3_stmt *pSelectDocsize = 0;
121565
 
        rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
121566
 
        if( rc==SQLITE_OK ){
121567
 
          int iCol;
121568
 
          const char *a = sqlite3_column_blob(pSelectDocsize, 0);
121569
 
          for(iCol=0; iCol<pInfo->nCol; iCol++){
121570
 
            sqlite3_int64 nToken;
121571
 
            a += sqlite3Fts3GetVarint(a, &nToken);
121572
 
            pInfo->aMatchinfo[iCol] = (u32)nToken;
121573
 
          }
121574
 
        }
121575
 
        sqlite3_reset(pSelectDocsize);
121576
 
        break;
121577
 
      }
121578
 
 
121579
 
      case FTS3_MATCHINFO_LCS:
121580
 
        rc = fts3ExprLoadDoclists(pCsr, 0, 0);
121581
 
        if( rc==SQLITE_OK ){
121582
 
          rc = fts3MatchinfoLcs(pCsr, pInfo);
121583
 
        }
121584
 
        break;
121585
 
 
121586
 
      default: {
121587
 
        Fts3Expr *pExpr;
121588
 
        assert( zArg[i]==FTS3_MATCHINFO_HITS );
121589
 
        pExpr = pCsr->pExpr;
121590
 
        rc = fts3ExprLoadDoclists(pCsr, 0, 0);
121591
 
        if( rc!=SQLITE_OK ) break;
121592
 
        if( bGlobal ){
121593
 
          if( pCsr->pDeferred ){
121594
 
            rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
121595
 
            if( rc!=SQLITE_OK ) break;
121596
 
          }
121597
 
          rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
121598
 
          if( rc!=SQLITE_OK ) break;
121599
 
        }
121600
 
        (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
121601
 
        break;
121602
 
      }
121603
 
    }
121604
 
 
121605
 
    pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
121606
 
  }
121607
 
 
121608
 
  sqlite3_reset(pSelect);
121609
 
  return rc;
121610
 
}
121611
 
 
121612
 
 
121613
 
/*
121614
 
** Populate pCsr->aMatchinfo[] with data for the current row. The 
121615
 
** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
121616
 
*/
121617
 
static int fts3GetMatchinfo(
121618
 
  Fts3Cursor *pCsr,               /* FTS3 Cursor object */
121619
 
  const char *zArg                /* Second argument to matchinfo() function */
121620
 
){
121621
 
  MatchInfo sInfo;
121622
 
  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
121623
 
  int rc = SQLITE_OK;
121624
 
  int bGlobal = 0;                /* Collect 'global' stats as well as local */
121625
 
 
121626
 
  memset(&sInfo, 0, sizeof(MatchInfo));
121627
 
  sInfo.pCursor = pCsr;
121628
 
  sInfo.nCol = pTab->nColumn;
121629
 
 
121630
 
  /* If there is cached matchinfo() data, but the format string for the 
121631
 
  ** cache does not match the format string for this request, discard 
121632
 
  ** the cached data. */
121633
 
  if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){
121634
 
    assert( pCsr->aMatchinfo );
121635
 
    sqlite3_free(pCsr->aMatchinfo);
121636
 
    pCsr->zMatchinfo = 0;
121637
 
    pCsr->aMatchinfo = 0;
121638
 
  }
121639
 
 
121640
 
  /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the
121641
 
  ** matchinfo function has been called for this query. In this case 
121642
 
  ** allocate the array used to accumulate the matchinfo data and
121643
 
  ** initialize those elements that are constant for every row.
121644
 
  */
121645
 
  if( pCsr->aMatchinfo==0 ){
121646
 
    int nMatchinfo = 0;           /* Number of u32 elements in match-info */
121647
 
    int nArg;                     /* Bytes in zArg */
121648
 
    int i;                        /* Used to iterate through zArg */
121649
 
 
121650
 
    /* Determine the number of phrases in the query */
121651
 
    pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
121652
 
    sInfo.nPhrase = pCsr->nPhrase;
121653
 
 
121654
 
    /* Determine the number of integers in the buffer returned by this call. */
121655
 
    for(i=0; zArg[i]; i++){
121656
 
      nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
121657
 
    }
121658
 
 
121659
 
    /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
121660
 
    nArg = (int)strlen(zArg);
121661
 
    pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1);
121662
 
    if( !pCsr->aMatchinfo ) return SQLITE_NOMEM;
121663
 
 
121664
 
    pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo];
121665
 
    pCsr->nMatchinfo = nMatchinfo;
121666
 
    memcpy(pCsr->zMatchinfo, zArg, nArg+1);
121667
 
    memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo);
121668
 
    pCsr->isMatchinfoNeeded = 1;
121669
 
    bGlobal = 1;
121670
 
  }
121671
 
 
121672
 
  sInfo.aMatchinfo = pCsr->aMatchinfo;
121673
 
  sInfo.nPhrase = pCsr->nPhrase;
121674
 
  if( pCsr->isMatchinfoNeeded ){
121675
 
    rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
121676
 
    pCsr->isMatchinfoNeeded = 0;
121677
 
  }
121678
 
 
121679
 
  return rc;
121680
 
}
121681
 
 
121682
 
/*
121683
 
** Implementation of snippet() function.
121684
 
*/
121685
 
SQLITE_PRIVATE void sqlite3Fts3Snippet(
121686
 
  sqlite3_context *pCtx,          /* SQLite function call context */
121687
 
  Fts3Cursor *pCsr,               /* Cursor object */
121688
 
  const char *zStart,             /* Snippet start text - "<b>" */
121689
 
  const char *zEnd,               /* Snippet end text - "</b>" */
121690
 
  const char *zEllipsis,          /* Snippet ellipsis text - "<b>...</b>" */
121691
 
  int iCol,                       /* Extract snippet from this column */
121692
 
  int nToken                      /* Approximate number of tokens in snippet */
121693
 
){
121694
 
  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
121695
 
  int rc = SQLITE_OK;
121696
 
  int i;
121697
 
  StrBuffer res = {0, 0, 0};
121698
 
 
121699
 
  /* The returned text includes up to four fragments of text extracted from
121700
 
  ** the data in the current row. The first iteration of the for(...) loop
121701
 
  ** below attempts to locate a single fragment of text nToken tokens in 
121702
 
  ** size that contains at least one instance of all phrases in the query
121703
 
  ** expression that appear in the current row. If such a fragment of text
121704
 
  ** cannot be found, the second iteration of the loop attempts to locate
121705
 
  ** a pair of fragments, and so on.
121706
 
  */
121707
 
  int nSnippet = 0;               /* Number of fragments in this snippet */
121708
 
  SnippetFragment aSnippet[4];    /* Maximum of 4 fragments per snippet */
121709
 
  int nFToken = -1;               /* Number of tokens in each fragment */
121710
 
 
121711
 
  if( !pCsr->pExpr ){
121712
 
    sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
121713
 
    return;
121714
 
  }
121715
 
 
121716
 
  for(nSnippet=1; 1; nSnippet++){
121717
 
 
121718
 
    int iSnip;                    /* Loop counter 0..nSnippet-1 */
121719
 
    u64 mCovered = 0;             /* Bitmask of phrases covered by snippet */
121720
 
    u64 mSeen = 0;                /* Bitmask of phrases seen by BestSnippet() */
121721
 
 
121722
 
    if( nToken>=0 ){
121723
 
      nFToken = (nToken+nSnippet-1) / nSnippet;
121724
 
    }else{
121725
 
      nFToken = -1 * nToken;
121726
 
    }
121727
 
 
121728
 
    for(iSnip=0; iSnip<nSnippet; iSnip++){
121729
 
      int iBestScore = -1;        /* Best score of columns checked so far */
121730
 
      int iRead;                  /* Used to iterate through columns */
121731
 
      SnippetFragment *pFragment = &aSnippet[iSnip];
121732
 
 
121733
 
      memset(pFragment, 0, sizeof(*pFragment));
121734
 
 
121735
 
      /* Loop through all columns of the table being considered for snippets.
121736
 
      ** If the iCol argument to this function was negative, this means all
121737
 
      ** columns of the FTS3 table. Otherwise, only column iCol is considered.
121738
 
      */
121739
 
      for(iRead=0; iRead<pTab->nColumn; iRead++){
121740
 
        SnippetFragment sF = {0, 0, 0, 0};
121741
 
        int iS;
121742
 
        if( iCol>=0 && iRead!=iCol ) continue;
121743
 
 
121744
 
        /* Find the best snippet of nFToken tokens in column iRead. */
121745
 
        rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
121746
 
        if( rc!=SQLITE_OK ){
121747
 
          goto snippet_out;
121748
 
        }
121749
 
        if( iS>iBestScore ){
121750
 
          *pFragment = sF;
121751
 
          iBestScore = iS;
121752
 
        }
121753
 
      }
121754
 
 
121755
 
      mCovered |= pFragment->covered;
121756
 
    }
121757
 
 
121758
 
    /* If all query phrases seen by fts3BestSnippet() are present in at least
121759
 
    ** one of the nSnippet snippet fragments, break out of the loop.
121760
 
    */
121761
 
    assert( (mCovered&mSeen)==mCovered );
121762
 
    if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
121763
 
  }
121764
 
 
121765
 
  assert( nFToken>0 );
121766
 
 
121767
 
  for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
121768
 
    rc = fts3SnippetText(pCsr, &aSnippet[i], 
121769
 
        i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
121770
 
    );
121771
 
  }
121772
 
 
121773
 
 snippet_out:
121774
 
  sqlite3Fts3SegmentsClose(pTab);
121775
 
  if( rc!=SQLITE_OK ){
121776
 
    sqlite3_result_error_code(pCtx, rc);
121777
 
    sqlite3_free(res.z);
121778
 
  }else{
121779
 
    sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
121780
 
  }
121781
 
}
121782
 
 
121783
 
 
121784
 
typedef struct TermOffset TermOffset;
121785
 
typedef struct TermOffsetCtx TermOffsetCtx;
121786
 
 
121787
 
struct TermOffset {
121788
 
  char *pList;                    /* Position-list */
121789
 
  int iPos;                       /* Position just read from pList */
121790
 
  int iOff;                       /* Offset of this term from read positions */
121791
 
};
121792
 
 
121793
 
struct TermOffsetCtx {
121794
 
  int iCol;                       /* Column of table to populate aTerm for */
121795
 
  int iTerm;
121796
 
  sqlite3_int64 iDocid;
121797
 
  TermOffset *aTerm;
121798
 
};
121799
 
 
121800
 
/*
121801
 
** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
121802
 
*/
121803
 
static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
121804
 
  TermOffsetCtx *p = (TermOffsetCtx *)ctx;
121805
 
  int nTerm;                      /* Number of tokens in phrase */
121806
 
  int iTerm;                      /* For looping through nTerm phrase terms */
121807
 
  char *pList;                    /* Pointer to position list for phrase */
121808
 
  int iPos = 0;                   /* First position in position-list */
121809
 
 
121810
 
  UNUSED_PARAMETER(iPhrase);
121811
 
  pList = sqlite3Fts3FindPositions(pExpr, p->iDocid, p->iCol);
121812
 
  nTerm = pExpr->pPhrase->nToken;
121813
 
  if( pList ){
121814
 
    fts3GetDeltaPosition(&pList, &iPos);
121815
 
    assert( iPos>=0 );
121816
 
  }
121817
 
 
121818
 
  for(iTerm=0; iTerm<nTerm; iTerm++){
121819
 
    TermOffset *pT = &p->aTerm[p->iTerm++];
121820
 
    pT->iOff = nTerm-iTerm-1;
121821
 
    pT->pList = pList;
121822
 
    pT->iPos = iPos;
121823
 
  }
121824
 
 
121825
 
  return SQLITE_OK;
121826
 
}
121827
 
 
121828
 
/*
121829
 
** Implementation of offsets() function.
121830
 
*/
121831
 
SQLITE_PRIVATE void sqlite3Fts3Offsets(
121832
 
  sqlite3_context *pCtx,          /* SQLite function call context */
121833
 
  Fts3Cursor *pCsr                /* Cursor object */
121834
 
){
121835
 
  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
121836
 
  sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
121837
 
  const char *ZDUMMY;             /* Dummy argument used with xNext() */
121838
 
  int NDUMMY;                     /* Dummy argument used with xNext() */
121839
 
  int rc;                         /* Return Code */
121840
 
  int nToken;                     /* Number of tokens in query */
121841
 
  int iCol;                       /* Column currently being processed */
121842
 
  StrBuffer res = {0, 0, 0};      /* Result string */
121843
 
  TermOffsetCtx sCtx;             /* Context for fts3ExprTermOffsetInit() */
121844
 
 
121845
 
  if( !pCsr->pExpr ){
121846
 
    sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
121847
 
    return;
121848
 
  }
121849
 
 
121850
 
  memset(&sCtx, 0, sizeof(sCtx));
121851
 
  assert( pCsr->isRequireSeek==0 );
121852
 
 
121853
 
  /* Count the number of terms in the query */
121854
 
  rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
121855
 
  if( rc!=SQLITE_OK ) goto offsets_out;
121856
 
 
121857
 
  /* Allocate the array of TermOffset iterators. */
121858
 
  sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken);
121859
 
  if( 0==sCtx.aTerm ){
121860
 
    rc = SQLITE_NOMEM;
121861
 
    goto offsets_out;
121862
 
  }
121863
 
  sCtx.iDocid = pCsr->iPrevId;
121864
 
 
121865
 
  /* Loop through the table columns, appending offset information to 
121866
 
  ** string-buffer res for each column.
121867
 
  */
121868
 
  for(iCol=0; iCol<pTab->nColumn; iCol++){
121869
 
    sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
121870
 
    int iStart;
121871
 
    int iEnd;
121872
 
    int iCurrent;
121873
 
    const char *zDoc;
121874
 
    int nDoc;
121875
 
 
121876
 
    /* Initialize the contents of sCtx.aTerm[] for column iCol. There is 
121877
 
    ** no way that this operation can fail, so the return code from
121878
 
    ** fts3ExprIterate() can be discarded.
121879
 
    */
121880
 
    sCtx.iCol = iCol;
121881
 
    sCtx.iTerm = 0;
121882
 
    (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx);
121883
 
 
121884
 
    /* Retreive the text stored in column iCol. If an SQL NULL is stored 
121885
 
    ** in column iCol, jump immediately to the next iteration of the loop.
121886
 
    ** If an OOM occurs while retrieving the data (this can happen if SQLite
121887
 
    ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM 
121888
 
    ** to the caller. 
121889
 
    */
121890
 
    zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
121891
 
    nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
121892
 
    if( zDoc==0 ){
121893
 
      if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
121894
 
        continue;
121895
 
      }
121896
 
      rc = SQLITE_NOMEM;
121897
 
      goto offsets_out;
121898
 
    }
121899
 
 
121900
 
    /* Initialize a tokenizer iterator to iterate through column iCol. */
121901
 
    rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC);
121902
 
    if( rc!=SQLITE_OK ) goto offsets_out;
121903
 
    pC->pTokenizer = pTab->pTokenizer;
121904
 
 
121905
 
    rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
121906
 
    while( rc==SQLITE_OK ){
121907
 
      int i;                      /* Used to loop through terms */
121908
 
      int iMinPos = 0x7FFFFFFF;   /* Position of next token */
121909
 
      TermOffset *pTerm = 0;      /* TermOffset associated with next token */
121910
 
 
121911
 
      for(i=0; i<nToken; i++){
121912
 
        TermOffset *pT = &sCtx.aTerm[i];
121913
 
        if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
121914
 
          iMinPos = pT->iPos-pT->iOff;
121915
 
          pTerm = pT;
121916
 
        }
121917
 
      }
121918
 
 
121919
 
      if( !pTerm ){
121920
 
        /* All offsets for this column have been gathered. */
121921
 
        break;
121922
 
      }else{
121923
 
        assert( iCurrent<=iMinPos );
121924
 
        if( 0==(0xFE&*pTerm->pList) ){
121925
 
          pTerm->pList = 0;
121926
 
        }else{
121927
 
          fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
121928
 
        }
121929
 
        while( rc==SQLITE_OK && iCurrent<iMinPos ){
121930
 
          rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
121931
 
        }
121932
 
        if( rc==SQLITE_OK ){
121933
 
          char aBuffer[64];
121934
 
          sqlite3_snprintf(sizeof(aBuffer), aBuffer, 
121935
 
              "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
121936
 
          );
121937
 
          rc = fts3StringAppend(&res, aBuffer, -1);
121938
 
        }else if( rc==SQLITE_DONE ){
121939
 
          rc = SQLITE_CORRUPT;
121940
 
        }
121941
 
      }
121942
 
    }
121943
 
    if( rc==SQLITE_DONE ){
121944
 
      rc = SQLITE_OK;
121945
 
    }
121946
 
 
121947
 
    pMod->xClose(pC);
121948
 
    if( rc!=SQLITE_OK ) goto offsets_out;
121949
 
  }
121950
 
 
121951
 
 offsets_out:
121952
 
  sqlite3_free(sCtx.aTerm);
121953
 
  assert( rc!=SQLITE_DONE );
121954
 
  sqlite3Fts3SegmentsClose(pTab);
121955
 
  if( rc!=SQLITE_OK ){
121956
 
    sqlite3_result_error_code(pCtx,  rc);
121957
 
    sqlite3_free(res.z);
121958
 
  }else{
121959
 
    sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
121960
 
  }
121961
 
  return;
121962
 
}
121963
 
 
121964
 
/*
121965
 
** Implementation of matchinfo() function.
121966
 
*/
121967
 
SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
121968
 
  sqlite3_context *pContext,      /* Function call context */
121969
 
  Fts3Cursor *pCsr,               /* FTS3 table cursor */
121970
 
  const char *zArg                /* Second arg to matchinfo() function */
121971
 
){
121972
 
  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
121973
 
  int rc;
121974
 
  int i;
121975
 
  const char *zFormat;
121976
 
 
121977
 
  if( zArg ){
121978
 
    for(i=0; zArg[i]; i++){
121979
 
      char *zErr = 0;
121980
 
      if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
121981
 
        sqlite3_result_error(pContext, zErr, -1);
121982
 
        sqlite3_free(zErr);
121983
 
        return;
121984
 
      }
121985
 
    }
121986
 
    zFormat = zArg;
121987
 
  }else{
121988
 
    zFormat = FTS3_MATCHINFO_DEFAULT;
121989
 
  }
121990
 
 
121991
 
  if( !pCsr->pExpr ){
121992
 
    sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
121993
 
    return;
121994
 
  }
121995
 
 
121996
 
  /* Retrieve matchinfo() data. */
121997
 
  rc = fts3GetMatchinfo(pCsr, zFormat);
121998
 
  sqlite3Fts3SegmentsClose(pTab);
121999
 
 
122000
 
  if( rc!=SQLITE_OK ){
122001
 
    sqlite3_result_error_code(pContext, rc);
122002
 
  }else{
122003
 
    int n = pCsr->nMatchinfo * sizeof(u32);
122004
 
    sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT);
122005
 
  }
122006
 
}
122007
 
 
122008
 
#endif
122009
 
 
122010
 
/************** End of fts3_snippet.c ****************************************/
122011
 
/************** Begin file rtree.c *******************************************/
122012
 
/*
122013
 
** 2001 September 15
122014
 
**
122015
 
** The author disclaims copyright to this source code.  In place of
122016
 
** a legal notice, here is a blessing:
122017
 
**
122018
 
**    May you do good and not evil.
122019
 
**    May you find forgiveness for yourself and forgive others.
122020
 
**    May you share freely, never taking more than you give.
122021
 
**
122022
 
*************************************************************************
122023
 
** This file contains code for implementations of the r-tree and r*-tree
122024
 
** algorithms packaged as an SQLite virtual table module.
122025
 
*/
122026
 
 
122027
 
/*
122028
 
** Database Format of R-Tree Tables
122029
 
** --------------------------------
122030
 
**
122031
 
** The data structure for a single virtual r-tree table is stored in three 
122032
 
** native SQLite tables declared as follows. In each case, the '%' character
122033
 
** in the table name is replaced with the user-supplied name of the r-tree
122034
 
** table.
122035
 
**
122036
 
**   CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
122037
 
**   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
122038
 
**   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
122039
 
**
122040
 
** The data for each node of the r-tree structure is stored in the %_node
122041
 
** table. For each node that is not the root node of the r-tree, there is
122042
 
** an entry in the %_parent table associating the node with its parent.
122043
 
** And for each row of data in the table, there is an entry in the %_rowid
122044
 
** table that maps from the entries rowid to the id of the node that it
122045
 
** is stored on.
122046
 
**
122047
 
** The root node of an r-tree always exists, even if the r-tree table is
122048
 
** empty. The nodeno of the root node is always 1. All other nodes in the
122049
 
** table must be the same size as the root node. The content of each node
122050
 
** is formatted as follows:
122051
 
**
122052
 
**   1. If the node is the root node (node 1), then the first 2 bytes
122053
 
**      of the node contain the tree depth as a big-endian integer.
122054
 
**      For non-root nodes, the first 2 bytes are left unused.
122055
 
**
122056
 
**   2. The next 2 bytes contain the number of entries currently 
122057
 
**      stored in the node.
122058
 
**
122059
 
**   3. The remainder of the node contains the node entries. Each entry
122060
 
**      consists of a single 8-byte integer followed by an even number
122061
 
**      of 4-byte coordinates. For leaf nodes the integer is the rowid
122062
 
**      of a record. For internal nodes it is the node number of a
122063
 
**      child page.
122064
 
*/
122065
 
 
122066
 
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
122067
 
 
122068
 
/*
122069
 
** This file contains an implementation of a couple of different variants
122070
 
** of the r-tree algorithm. See the README file for further details. The 
122071
 
** same data-structure is used for all, but the algorithms for insert and
122072
 
** delete operations vary. The variants used are selected at compile time 
122073
 
** by defining the following symbols:
122074
 
*/
122075
 
 
122076
 
/* Either, both or none of the following may be set to activate 
122077
 
** r*tree variant algorithms.
122078
 
*/
122079
 
#define VARIANT_RSTARTREE_CHOOSESUBTREE 0
122080
 
#define VARIANT_RSTARTREE_REINSERT      1
122081
 
 
122082
 
/* 
122083
 
** Exactly one of the following must be set to 1.
122084
 
*/
122085
 
#define VARIANT_GUTTMAN_QUADRATIC_SPLIT 0
122086
 
#define VARIANT_GUTTMAN_LINEAR_SPLIT    0
122087
 
#define VARIANT_RSTARTREE_SPLIT         1
122088
 
 
122089
 
#define VARIANT_GUTTMAN_SPLIT \
122090
 
        (VARIANT_GUTTMAN_LINEAR_SPLIT||VARIANT_GUTTMAN_QUADRATIC_SPLIT)
122091
 
 
122092
 
#if VARIANT_GUTTMAN_QUADRATIC_SPLIT
122093
 
  #define PickNext QuadraticPickNext
122094
 
  #define PickSeeds QuadraticPickSeeds
122095
 
  #define AssignCells splitNodeGuttman
122096
 
#endif
122097
 
#if VARIANT_GUTTMAN_LINEAR_SPLIT
122098
 
  #define PickNext LinearPickNext
122099
 
  #define PickSeeds LinearPickSeeds
122100
 
  #define AssignCells splitNodeGuttman
122101
 
#endif
122102
 
#if VARIANT_RSTARTREE_SPLIT
122103
 
  #define AssignCells splitNodeStartree
122104
 
#endif
122105
 
 
122106
 
#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
122107
 
# define NDEBUG 1
122108
 
#endif
122109
 
 
122110
 
#ifndef SQLITE_CORE
122111
 
  SQLITE_EXTENSION_INIT1
122112
 
#else
122113
 
#endif
122114
 
 
122115
 
 
122116
 
#ifndef SQLITE_AMALGAMATION
122117
 
#include "sqlite3rtree.h"
122118
 
typedef sqlite3_int64 i64;
122119
 
typedef unsigned char u8;
122120
 
typedef unsigned int u32;
122121
 
#endif
122122
 
 
122123
 
/*  The following macro is used to suppress compiler warnings.
122124
 
*/
122125
 
#ifndef UNUSED_PARAMETER
122126
 
# define UNUSED_PARAMETER(x) (void)(x)
122127
 
#endif
122128
 
 
122129
 
typedef struct Rtree Rtree;
122130
 
typedef struct RtreeCursor RtreeCursor;
122131
 
typedef struct RtreeNode RtreeNode;
122132
 
typedef struct RtreeCell RtreeCell;
122133
 
typedef struct RtreeConstraint RtreeConstraint;
122134
 
typedef struct RtreeMatchArg RtreeMatchArg;
122135
 
typedef struct RtreeGeomCallback RtreeGeomCallback;
122136
 
typedef union RtreeCoord RtreeCoord;
122137
 
 
122138
 
/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
122139
 
#define RTREE_MAX_DIMENSIONS 5
122140
 
 
122141
 
/* Size of hash table Rtree.aHash. This hash table is not expected to
122142
 
** ever contain very many entries, so a fixed number of buckets is 
122143
 
** used.
122144
 
*/
122145
 
#define HASHSIZE 128
122146
 
 
122147
 
/* 
122148
 
** An rtree virtual-table object.
122149
 
*/
122150
 
struct Rtree {
122151
 
  sqlite3_vtab base;
122152
 
  sqlite3 *db;                /* Host database connection */
122153
 
  int iNodeSize;              /* Size in bytes of each node in the node table */
122154
 
  int nDim;                   /* Number of dimensions */
122155
 
  int nBytesPerCell;          /* Bytes consumed per cell */
122156
 
  int iDepth;                 /* Current depth of the r-tree structure */
122157
 
  char *zDb;                  /* Name of database containing r-tree table */
122158
 
  char *zName;                /* Name of r-tree table */ 
122159
 
  RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */ 
122160
 
  int nBusy;                  /* Current number of users of this structure */
122161
 
 
122162
 
  /* List of nodes removed during a CondenseTree operation. List is
122163
 
  ** linked together via the pointer normally used for hash chains -
122164
 
  ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree 
122165
 
  ** headed by the node (leaf nodes have RtreeNode.iNode==0).
122166
 
  */
122167
 
  RtreeNode *pDeleted;
122168
 
  int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
122169
 
 
122170
 
  /* Statements to read/write/delete a record from xxx_node */
122171
 
  sqlite3_stmt *pReadNode;
122172
 
  sqlite3_stmt *pWriteNode;
122173
 
  sqlite3_stmt *pDeleteNode;
122174
 
 
122175
 
  /* Statements to read/write/delete a record from xxx_rowid */
122176
 
  sqlite3_stmt *pReadRowid;
122177
 
  sqlite3_stmt *pWriteRowid;
122178
 
  sqlite3_stmt *pDeleteRowid;
122179
 
 
122180
 
  /* Statements to read/write/delete a record from xxx_parent */
122181
 
  sqlite3_stmt *pReadParent;
122182
 
  sqlite3_stmt *pWriteParent;
122183
 
  sqlite3_stmt *pDeleteParent;
122184
 
 
122185
 
  int eCoordType;
122186
 
};
122187
 
 
122188
 
/* Possible values for eCoordType: */
122189
 
#define RTREE_COORD_REAL32 0
122190
 
#define RTREE_COORD_INT32  1
122191
 
 
122192
 
/*
122193
 
** The minimum number of cells allowed for a node is a third of the 
122194
 
** maximum. In Gutman's notation:
122195
 
**
122196
 
**     m = M/3
122197
 
**
122198
 
** If an R*-tree "Reinsert" operation is required, the same number of
122199
 
** cells are removed from the overfull node and reinserted into the tree.
122200
 
*/
122201
 
#define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
122202
 
#define RTREE_REINSERT(p) RTREE_MINCELLS(p)
122203
 
#define RTREE_MAXCELLS 51
122204
 
 
122205
 
/*
122206
 
** The smallest possible node-size is (512-64)==448 bytes. And the largest
122207
 
** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
122208
 
** Therefore all non-root nodes must contain at least 3 entries. Since 
122209
 
** 2^40 is greater than 2^64, an r-tree structure always has a depth of
122210
 
** 40 or less.
122211
 
*/
122212
 
#define RTREE_MAX_DEPTH 40
122213
 
 
122214
 
/* 
122215
 
** An rtree cursor object.
122216
 
*/
122217
 
struct RtreeCursor {
122218
 
  sqlite3_vtab_cursor base;
122219
 
  RtreeNode *pNode;                 /* Node cursor is currently pointing at */
122220
 
  int iCell;                        /* Index of current cell in pNode */
122221
 
  int iStrategy;                    /* Copy of idxNum search parameter */
122222
 
  int nConstraint;                  /* Number of entries in aConstraint */
122223
 
  RtreeConstraint *aConstraint;     /* Search constraints. */
122224
 
};
122225
 
 
122226
 
union RtreeCoord {
122227
 
  float f;
122228
 
  int i;
122229
 
};
122230
 
 
122231
 
/*
122232
 
** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
122233
 
** formatted as a double. This macro assumes that local variable pRtree points
122234
 
** to the Rtree structure associated with the RtreeCoord.
122235
 
*/
122236
 
#define DCOORD(coord) (                           \
122237
 
  (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
122238
 
    ((double)coord.f) :                           \
122239
 
    ((double)coord.i)                             \
122240
 
)
122241
 
 
122242
 
/*
122243
 
** A search constraint.
122244
 
*/
122245
 
struct RtreeConstraint {
122246
 
  int iCoord;                     /* Index of constrained coordinate */
122247
 
  int op;                         /* Constraining operation */
122248
 
  double rValue;                  /* Constraint value. */
122249
 
  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
122250
 
  sqlite3_rtree_geometry *pGeom;  /* Constraint callback argument for a MATCH */
122251
 
};
122252
 
 
122253
 
/* Possible values for RtreeConstraint.op */
122254
 
#define RTREE_EQ    0x41
122255
 
#define RTREE_LE    0x42
122256
 
#define RTREE_LT    0x43
122257
 
#define RTREE_GE    0x44
122258
 
#define RTREE_GT    0x45
122259
 
#define RTREE_MATCH 0x46
122260
 
 
122261
 
/* 
122262
 
** An rtree structure node.
122263
 
*/
122264
 
struct RtreeNode {
122265
 
  RtreeNode *pParent;               /* Parent node */
122266
 
  i64 iNode;
122267
 
  int nRef;
122268
 
  int isDirty;
122269
 
  u8 *zData;
122270
 
  RtreeNode *pNext;                 /* Next node in this hash chain */
122271
 
};
122272
 
#define NCELL(pNode) readInt16(&(pNode)->zData[2])
122273
 
 
122274
 
/* 
122275
 
** Structure to store a deserialized rtree record.
122276
 
*/
122277
 
struct RtreeCell {
122278
 
  i64 iRowid;
122279
 
  RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
122280
 
};
122281
 
 
122282
 
 
122283
 
/*
122284
 
** Value for the first field of every RtreeMatchArg object. The MATCH
122285
 
** operator tests that the first field of a blob operand matches this
122286
 
** value to avoid operating on invalid blobs (which could cause a segfault).
122287
 
*/
122288
 
#define RTREE_GEOMETRY_MAGIC 0x891245AB
122289
 
 
122290
 
/*
122291
 
** An instance of this structure must be supplied as a blob argument to
122292
 
** the right-hand-side of an SQL MATCH operator used to constrain an
122293
 
** r-tree query.
122294
 
*/
122295
 
struct RtreeMatchArg {
122296
 
  u32 magic;                      /* Always RTREE_GEOMETRY_MAGIC */
122297
 
  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
122298
 
  void *pContext;
122299
 
  int nParam;
122300
 
  double aParam[1];
122301
 
};
122302
 
 
122303
 
/*
122304
 
** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
122305
 
** a single instance of the following structure is allocated. It is used
122306
 
** as the context for the user-function created by by s_r_g_c(). The object
122307
 
** is eventually deleted by the destructor mechanism provided by
122308
 
** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
122309
 
** the geometry callback function).
122310
 
*/
122311
 
struct RtreeGeomCallback {
122312
 
  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
122313
 
  void *pContext;
122314
 
};
122315
 
 
122316
 
#ifndef MAX
122317
 
# define MAX(x,y) ((x) < (y) ? (y) : (x))
122318
 
#endif
122319
 
#ifndef MIN
122320
 
# define MIN(x,y) ((x) > (y) ? (y) : (x))
122321
 
#endif
122322
 
 
122323
 
/*
122324
 
** Functions to deserialize a 16 bit integer, 32 bit real number and
122325
 
** 64 bit integer. The deserialized value is returned.
122326
 
*/
122327
 
static int readInt16(u8 *p){
122328
 
  return (p[0]<<8) + p[1];
122329
 
}
122330
 
static void readCoord(u8 *p, RtreeCoord *pCoord){
122331
 
  u32 i = (
122332
 
    (((u32)p[0]) << 24) + 
122333
 
    (((u32)p[1]) << 16) + 
122334
 
    (((u32)p[2]) <<  8) + 
122335
 
    (((u32)p[3]) <<  0)
122336
 
  );
122337
 
  *(u32 *)pCoord = i;
122338
 
}
122339
 
static i64 readInt64(u8 *p){
122340
 
  return (
122341
 
    (((i64)p[0]) << 56) + 
122342
 
    (((i64)p[1]) << 48) + 
122343
 
    (((i64)p[2]) << 40) + 
122344
 
    (((i64)p[3]) << 32) + 
122345
 
    (((i64)p[4]) << 24) + 
122346
 
    (((i64)p[5]) << 16) + 
122347
 
    (((i64)p[6]) <<  8) + 
122348
 
    (((i64)p[7]) <<  0)
122349
 
  );
122350
 
}
122351
 
 
122352
 
/*
122353
 
** Functions to serialize a 16 bit integer, 32 bit real number and
122354
 
** 64 bit integer. The value returned is the number of bytes written
122355
 
** to the argument buffer (always 2, 4 and 8 respectively).
122356
 
*/
122357
 
static int writeInt16(u8 *p, int i){
122358
 
  p[0] = (i>> 8)&0xFF;
122359
 
  p[1] = (i>> 0)&0xFF;
122360
 
  return 2;
122361
 
}
122362
 
static int writeCoord(u8 *p, RtreeCoord *pCoord){
122363
 
  u32 i;
122364
 
  assert( sizeof(RtreeCoord)==4 );
122365
 
  assert( sizeof(u32)==4 );
122366
 
  i = *(u32 *)pCoord;
122367
 
  p[0] = (i>>24)&0xFF;
122368
 
  p[1] = (i>>16)&0xFF;
122369
 
  p[2] = (i>> 8)&0xFF;
122370
 
  p[3] = (i>> 0)&0xFF;
122371
 
  return 4;
122372
 
}
122373
 
static int writeInt64(u8 *p, i64 i){
122374
 
  p[0] = (i>>56)&0xFF;
122375
 
  p[1] = (i>>48)&0xFF;
122376
 
  p[2] = (i>>40)&0xFF;
122377
 
  p[3] = (i>>32)&0xFF;
122378
 
  p[4] = (i>>24)&0xFF;
122379
 
  p[5] = (i>>16)&0xFF;
122380
 
  p[6] = (i>> 8)&0xFF;
122381
 
  p[7] = (i>> 0)&0xFF;
122382
 
  return 8;
122383
 
}
122384
 
 
122385
 
/*
122386
 
** Increment the reference count of node p.
122387
 
*/
122388
 
static void nodeReference(RtreeNode *p){
122389
 
  if( p ){
122390
 
    p->nRef++;
122391
 
  }
122392
 
}
122393
 
 
122394
 
/*
122395
 
** Clear the content of node p (set all bytes to 0x00).
122396
 
*/
122397
 
static void nodeZero(Rtree *pRtree, RtreeNode *p){
122398
 
  memset(&p->zData[2], 0, pRtree->iNodeSize-2);
122399
 
  p->isDirty = 1;
122400
 
}
122401
 
 
122402
 
/*
122403
 
** Given a node number iNode, return the corresponding key to use
122404
 
** in the Rtree.aHash table.
122405
 
*/
122406
 
static int nodeHash(i64 iNode){
122407
 
  return (
122408
 
    (iNode>>56) ^ (iNode>>48) ^ (iNode>>40) ^ (iNode>>32) ^ 
122409
 
    (iNode>>24) ^ (iNode>>16) ^ (iNode>> 8) ^ (iNode>> 0)
122410
 
  ) % HASHSIZE;
122411
 
}
122412
 
 
122413
 
/*
122414
 
** Search the node hash table for node iNode. If found, return a pointer
122415
 
** to it. Otherwise, return 0.
122416
 
*/
122417
 
static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
122418
 
  RtreeNode *p;
122419
 
  for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
122420
 
  return p;
122421
 
}
122422
 
 
122423
 
/*
122424
 
** Add node pNode to the node hash table.
122425
 
*/
122426
 
static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
122427
 
  int iHash;
122428
 
  assert( pNode->pNext==0 );
122429
 
  iHash = nodeHash(pNode->iNode);
122430
 
  pNode->pNext = pRtree->aHash[iHash];
122431
 
  pRtree->aHash[iHash] = pNode;
122432
 
}
122433
 
 
122434
 
/*
122435
 
** Remove node pNode from the node hash table.
122436
 
*/
122437
 
static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
122438
 
  RtreeNode **pp;
122439
 
  if( pNode->iNode!=0 ){
122440
 
    pp = &pRtree->aHash[nodeHash(pNode->iNode)];
122441
 
    for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
122442
 
    *pp = pNode->pNext;
122443
 
    pNode->pNext = 0;
122444
 
  }
122445
 
}
122446
 
 
122447
 
/*
122448
 
** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
122449
 
** indicating that node has not yet been assigned a node number. It is
122450
 
** assigned a node number when nodeWrite() is called to write the
122451
 
** node contents out to the database.
122452
 
*/
122453
 
static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
122454
 
  RtreeNode *pNode;
122455
 
  pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
122456
 
  if( pNode ){
122457
 
    memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
122458
 
    pNode->zData = (u8 *)&pNode[1];
122459
 
    pNode->nRef = 1;
122460
 
    pNode->pParent = pParent;
122461
 
    pNode->isDirty = 1;
122462
 
    nodeReference(pParent);
122463
 
  }
122464
 
  return pNode;
122465
 
}
122466
 
 
122467
 
/*
122468
 
** Obtain a reference to an r-tree node.
122469
 
*/
122470
 
static int
122471
 
nodeAcquire(
122472
 
  Rtree *pRtree,             /* R-tree structure */
122473
 
  i64 iNode,                 /* Node number to load */
122474
 
  RtreeNode *pParent,        /* Either the parent node or NULL */
122475
 
  RtreeNode **ppNode         /* OUT: Acquired node */
122476
 
){
122477
 
  int rc;
122478
 
  int rc2 = SQLITE_OK;
122479
 
  RtreeNode *pNode;
122480
 
 
122481
 
  /* Check if the requested node is already in the hash table. If so,
122482
 
  ** increase its reference count and return it.
122483
 
  */
122484
 
  if( (pNode = nodeHashLookup(pRtree, iNode)) ){
122485
 
    assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
122486
 
    if( pParent && !pNode->pParent ){
122487
 
      nodeReference(pParent);
122488
 
      pNode->pParent = pParent;
122489
 
    }
122490
 
    pNode->nRef++;
122491
 
    *ppNode = pNode;
122492
 
    return SQLITE_OK;
122493
 
  }
122494
 
 
122495
 
  sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
122496
 
  rc = sqlite3_step(pRtree->pReadNode);
122497
 
  if( rc==SQLITE_ROW ){
122498
 
    const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
122499
 
    if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
122500
 
      pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
122501
 
      if( !pNode ){
122502
 
        rc2 = SQLITE_NOMEM;
122503
 
      }else{
122504
 
        pNode->pParent = pParent;
122505
 
        pNode->zData = (u8 *)&pNode[1];
122506
 
        pNode->nRef = 1;
122507
 
        pNode->iNode = iNode;
122508
 
        pNode->isDirty = 0;
122509
 
        pNode->pNext = 0;
122510
 
        memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
122511
 
        nodeReference(pParent);
122512
 
      }
122513
 
    }
122514
 
  }
122515
 
  rc = sqlite3_reset(pRtree->pReadNode);
122516
 
  if( rc==SQLITE_OK ) rc = rc2;
122517
 
 
122518
 
  /* If the root node was just loaded, set pRtree->iDepth to the height
122519
 
  ** of the r-tree structure. A height of zero means all data is stored on
122520
 
  ** the root node. A height of one means the children of the root node
122521
 
  ** are the leaves, and so on. If the depth as specified on the root node
122522
 
  ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
122523
 
  */
122524
 
  if( pNode && iNode==1 ){
122525
 
    pRtree->iDepth = readInt16(pNode->zData);
122526
 
    if( pRtree->iDepth>RTREE_MAX_DEPTH ){
122527
 
      rc = SQLITE_CORRUPT;
122528
 
    }
122529
 
  }
122530
 
 
122531
 
  /* If no error has occurred so far, check if the "number of entries"
122532
 
  ** field on the node is too large. If so, set the return code to 
122533
 
  ** SQLITE_CORRUPT.
122534
 
  */
122535
 
  if( pNode && rc==SQLITE_OK ){
122536
 
    if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
122537
 
      rc = SQLITE_CORRUPT;
122538
 
    }
122539
 
  }
122540
 
 
122541
 
  if( rc==SQLITE_OK ){
122542
 
    if( pNode!=0 ){
122543
 
      nodeHashInsert(pRtree, pNode);
122544
 
    }else{
122545
 
      rc = SQLITE_CORRUPT;
122546
 
    }
122547
 
    *ppNode = pNode;
122548
 
  }else{
122549
 
    sqlite3_free(pNode);
122550
 
    *ppNode = 0;
122551
 
  }
122552
 
 
122553
 
  return rc;
122554
 
}
122555
 
 
122556
 
/*
122557
 
** Overwrite cell iCell of node pNode with the contents of pCell.
122558
 
*/
122559
 
static void nodeOverwriteCell(
122560
 
  Rtree *pRtree, 
122561
 
  RtreeNode *pNode,  
122562
 
  RtreeCell *pCell, 
122563
 
  int iCell
122564
 
){
122565
 
  int ii;
122566
 
  u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
122567
 
  p += writeInt64(p, pCell->iRowid);
122568
 
  for(ii=0; ii<(pRtree->nDim*2); ii++){
122569
 
    p += writeCoord(p, &pCell->aCoord[ii]);
122570
 
  }
122571
 
  pNode->isDirty = 1;
122572
 
}
122573
 
 
122574
 
/*
122575
 
** Remove cell the cell with index iCell from node pNode.
122576
 
*/
122577
 
static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
122578
 
  u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
122579
 
  u8 *pSrc = &pDst[pRtree->nBytesPerCell];
122580
 
  int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
122581
 
  memmove(pDst, pSrc, nByte);
122582
 
  writeInt16(&pNode->zData[2], NCELL(pNode)-1);
122583
 
  pNode->isDirty = 1;
122584
 
}
122585
 
 
122586
 
/*
122587
 
** Insert the contents of cell pCell into node pNode. If the insert
122588
 
** is successful, return SQLITE_OK.
122589
 
**
122590
 
** If there is not enough free space in pNode, return SQLITE_FULL.
122591
 
*/
122592
 
static int
122593
 
nodeInsertCell(
122594
 
  Rtree *pRtree, 
122595
 
  RtreeNode *pNode, 
122596
 
  RtreeCell *pCell 
122597
 
){
122598
 
  int nCell;                    /* Current number of cells in pNode */
122599
 
  int nMaxCell;                 /* Maximum number of cells for pNode */
122600
 
 
122601
 
  nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
122602
 
  nCell = NCELL(pNode);
122603
 
 
122604
 
  assert( nCell<=nMaxCell );
122605
 
  if( nCell<nMaxCell ){
122606
 
    nodeOverwriteCell(pRtree, pNode, pCell, nCell);
122607
 
    writeInt16(&pNode->zData[2], nCell+1);
122608
 
    pNode->isDirty = 1;
122609
 
  }
122610
 
 
122611
 
  return (nCell==nMaxCell);
122612
 
}
122613
 
 
122614
 
/*
122615
 
** If the node is dirty, write it out to the database.
122616
 
*/
122617
 
static int
122618
 
nodeWrite(Rtree *pRtree, RtreeNode *pNode){
122619
 
  int rc = SQLITE_OK;
122620
 
  if( pNode->isDirty ){
122621
 
    sqlite3_stmt *p = pRtree->pWriteNode;
122622
 
    if( pNode->iNode ){
122623
 
      sqlite3_bind_int64(p, 1, pNode->iNode);
122624
 
    }else{
122625
 
      sqlite3_bind_null(p, 1);
122626
 
    }
122627
 
    sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
122628
 
    sqlite3_step(p);
122629
 
    pNode->isDirty = 0;
122630
 
    rc = sqlite3_reset(p);
122631
 
    if( pNode->iNode==0 && rc==SQLITE_OK ){
122632
 
      pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
122633
 
      nodeHashInsert(pRtree, pNode);
122634
 
    }
122635
 
  }
122636
 
  return rc;
122637
 
}
122638
 
 
122639
 
/*
122640
 
** Release a reference to a node. If the node is dirty and the reference
122641
 
** count drops to zero, the node data is written to the database.
122642
 
*/
122643
 
static int
122644
 
nodeRelease(Rtree *pRtree, RtreeNode *pNode){
122645
 
  int rc = SQLITE_OK;
122646
 
  if( pNode ){
122647
 
    assert( pNode->nRef>0 );
122648
 
    pNode->nRef--;
122649
 
    if( pNode->nRef==0 ){
122650
 
      if( pNode->iNode==1 ){
122651
 
        pRtree->iDepth = -1;
122652
 
      }
122653
 
      if( pNode->pParent ){
122654
 
        rc = nodeRelease(pRtree, pNode->pParent);
122655
 
      }
122656
 
      if( rc==SQLITE_OK ){
122657
 
        rc = nodeWrite(pRtree, pNode);
122658
 
      }
122659
 
      nodeHashDelete(pRtree, pNode);
122660
 
      sqlite3_free(pNode);
122661
 
    }
122662
 
  }
122663
 
  return rc;
122664
 
}
122665
 
 
122666
 
/*
122667
 
** Return the 64-bit integer value associated with cell iCell of
122668
 
** node pNode. If pNode is a leaf node, this is a rowid. If it is
122669
 
** an internal node, then the 64-bit integer is a child page number.
122670
 
*/
122671
 
static i64 nodeGetRowid(
122672
 
  Rtree *pRtree, 
122673
 
  RtreeNode *pNode, 
122674
 
  int iCell
122675
 
){
122676
 
  assert( iCell<NCELL(pNode) );
122677
 
  return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
122678
 
}
122679
 
 
122680
 
/*
122681
 
** Return coordinate iCoord from cell iCell in node pNode.
122682
 
*/
122683
 
static void nodeGetCoord(
122684
 
  Rtree *pRtree, 
122685
 
  RtreeNode *pNode, 
122686
 
  int iCell,
122687
 
  int iCoord,
122688
 
  RtreeCoord *pCoord           /* Space to write result to */
122689
 
){
122690
 
  readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
122691
 
}
122692
 
 
122693
 
/*
122694
 
** Deserialize cell iCell of node pNode. Populate the structure pointed
122695
 
** to by pCell with the results.
122696
 
*/
122697
 
static void nodeGetCell(
122698
 
  Rtree *pRtree, 
122699
 
  RtreeNode *pNode, 
122700
 
  int iCell,
122701
 
  RtreeCell *pCell
122702
 
){
122703
 
  int ii;
122704
 
  pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
122705
 
  for(ii=0; ii<pRtree->nDim*2; ii++){
122706
 
    nodeGetCoord(pRtree, pNode, iCell, ii, &pCell->aCoord[ii]);
122707
 
  }
122708
 
}
122709
 
 
122710
 
 
122711
 
/* Forward declaration for the function that does the work of
122712
 
** the virtual table module xCreate() and xConnect() methods.
122713
 
*/
122714
 
static int rtreeInit(
122715
 
  sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
122716
 
);
122717
 
 
122718
 
/* 
122719
 
** Rtree virtual table module xCreate method.
122720
 
*/
122721
 
static int rtreeCreate(
122722
 
  sqlite3 *db,
122723
 
  void *pAux,
122724
 
  int argc, const char *const*argv,
122725
 
  sqlite3_vtab **ppVtab,
122726
 
  char **pzErr
122727
 
){
122728
 
  return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
122729
 
}
122730
 
 
122731
 
/* 
122732
 
** Rtree virtual table module xConnect method.
122733
 
*/
122734
 
static int rtreeConnect(
122735
 
  sqlite3 *db,
122736
 
  void *pAux,
122737
 
  int argc, const char *const*argv,
122738
 
  sqlite3_vtab **ppVtab,
122739
 
  char **pzErr
122740
 
){
122741
 
  return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
122742
 
}
122743
 
 
122744
 
/*
122745
 
** Increment the r-tree reference count.
122746
 
*/
122747
 
static void rtreeReference(Rtree *pRtree){
122748
 
  pRtree->nBusy++;
122749
 
}
122750
 
 
122751
 
/*
122752
 
** Decrement the r-tree reference count. When the reference count reaches
122753
 
** zero the structure is deleted.
122754
 
*/
122755
 
static void rtreeRelease(Rtree *pRtree){
122756
 
  pRtree->nBusy--;
122757
 
  if( pRtree->nBusy==0 ){
122758
 
    sqlite3_finalize(pRtree->pReadNode);
122759
 
    sqlite3_finalize(pRtree->pWriteNode);
122760
 
    sqlite3_finalize(pRtree->pDeleteNode);
122761
 
    sqlite3_finalize(pRtree->pReadRowid);
122762
 
    sqlite3_finalize(pRtree->pWriteRowid);
122763
 
    sqlite3_finalize(pRtree->pDeleteRowid);
122764
 
    sqlite3_finalize(pRtree->pReadParent);
122765
 
    sqlite3_finalize(pRtree->pWriteParent);
122766
 
    sqlite3_finalize(pRtree->pDeleteParent);
122767
 
    sqlite3_free(pRtree);
122768
 
  }
122769
 
}
122770
 
 
122771
 
/* 
122772
 
** Rtree virtual table module xDisconnect method.
122773
 
*/
122774
 
static int rtreeDisconnect(sqlite3_vtab *pVtab){
122775
 
  rtreeRelease((Rtree *)pVtab);
122776
 
  return SQLITE_OK;
122777
 
}
122778
 
 
122779
 
/* 
122780
 
** Rtree virtual table module xDestroy method.
122781
 
*/
122782
 
static int rtreeDestroy(sqlite3_vtab *pVtab){
122783
 
  Rtree *pRtree = (Rtree *)pVtab;
122784
 
  int rc;
122785
 
  char *zCreate = sqlite3_mprintf(
122786
 
    "DROP TABLE '%q'.'%q_node';"
122787
 
    "DROP TABLE '%q'.'%q_rowid';"
122788
 
    "DROP TABLE '%q'.'%q_parent';",
122789
 
    pRtree->zDb, pRtree->zName, 
122790
 
    pRtree->zDb, pRtree->zName,
122791
 
    pRtree->zDb, pRtree->zName
122792
 
  );
122793
 
  if( !zCreate ){
122794
 
    rc = SQLITE_NOMEM;
122795
 
  }else{
122796
 
    rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
122797
 
    sqlite3_free(zCreate);
122798
 
  }
122799
 
  if( rc==SQLITE_OK ){
122800
 
    rtreeRelease(pRtree);
122801
 
  }
122802
 
 
122803
 
  return rc;
122804
 
}
122805
 
 
122806
 
/* 
122807
 
** Rtree virtual table module xOpen method.
122808
 
*/
122809
 
static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
122810
 
  int rc = SQLITE_NOMEM;
122811
 
  RtreeCursor *pCsr;
122812
 
 
122813
 
  pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
122814
 
  if( pCsr ){
122815
 
    memset(pCsr, 0, sizeof(RtreeCursor));
122816
 
    pCsr->base.pVtab = pVTab;
122817
 
    rc = SQLITE_OK;
122818
 
  }
122819
 
  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
122820
 
 
122821
 
  return rc;
122822
 
}
122823
 
 
122824
 
 
122825
 
/*
122826
 
** Free the RtreeCursor.aConstraint[] array and its contents.
122827
 
*/
122828
 
static void freeCursorConstraints(RtreeCursor *pCsr){
122829
 
  if( pCsr->aConstraint ){
122830
 
    int i;                        /* Used to iterate through constraint array */
122831
 
    for(i=0; i<pCsr->nConstraint; i++){
122832
 
      sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom;
122833
 
      if( pGeom ){
122834
 
        if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser);
122835
 
        sqlite3_free(pGeom);
122836
 
      }
122837
 
    }
122838
 
    sqlite3_free(pCsr->aConstraint);
122839
 
    pCsr->aConstraint = 0;
122840
 
  }
122841
 
}
122842
 
 
122843
 
/* 
122844
 
** Rtree virtual table module xClose method.
122845
 
*/
122846
 
static int rtreeClose(sqlite3_vtab_cursor *cur){
122847
 
  Rtree *pRtree = (Rtree *)(cur->pVtab);
122848
 
  int rc;
122849
 
  RtreeCursor *pCsr = (RtreeCursor *)cur;
122850
 
  freeCursorConstraints(pCsr);
122851
 
  rc = nodeRelease(pRtree, pCsr->pNode);
122852
 
  sqlite3_free(pCsr);
122853
 
  return rc;
122854
 
}
122855
 
 
122856
 
/*
122857
 
** Rtree virtual table module xEof method.
122858
 
**
122859
 
** Return non-zero if the cursor does not currently point to a valid 
122860
 
** record (i.e if the scan has finished), or zero otherwise.
122861
 
*/
122862
 
static int rtreeEof(sqlite3_vtab_cursor *cur){
122863
 
  RtreeCursor *pCsr = (RtreeCursor *)cur;
122864
 
  return (pCsr->pNode==0);
122865
 
}
122866
 
 
122867
 
/*
122868
 
** The r-tree constraint passed as the second argument to this function is
122869
 
** guaranteed to be a MATCH constraint.
122870
 
*/
122871
 
static int testRtreeGeom(
122872
 
  Rtree *pRtree,                  /* R-Tree object */
122873
 
  RtreeConstraint *pConstraint,   /* MATCH constraint to test */
122874
 
  RtreeCell *pCell,               /* Cell to test */
122875
 
  int *pbRes                      /* OUT: Test result */
122876
 
){
122877
 
  int i;
122878
 
  double aCoord[RTREE_MAX_DIMENSIONS*2];
122879
 
  int nCoord = pRtree->nDim*2;
122880
 
 
122881
 
  assert( pConstraint->op==RTREE_MATCH );
122882
 
  assert( pConstraint->pGeom );
122883
 
 
122884
 
  for(i=0; i<nCoord; i++){
122885
 
    aCoord[i] = DCOORD(pCell->aCoord[i]);
122886
 
  }
122887
 
  return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes);
122888
 
}
122889
 
 
122890
 
/* 
122891
 
** Cursor pCursor currently points to a cell in a non-leaf page.
122892
 
** Set *pbEof to true if the sub-tree headed by the cell is filtered
122893
 
** (excluded) by the constraints in the pCursor->aConstraint[] 
122894
 
** array, or false otherwise.
122895
 
**
122896
 
** Return SQLITE_OK if successful or an SQLite error code if an error
122897
 
** occurs within a geometry callback.
122898
 
*/
122899
 
static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
122900
 
  RtreeCell cell;
122901
 
  int ii;
122902
 
  int bRes = 0;
122903
 
  int rc = SQLITE_OK;
122904
 
 
122905
 
  nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
122906
 
  for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
122907
 
    RtreeConstraint *p = &pCursor->aConstraint[ii];
122908
 
    double cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
122909
 
    double cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
122910
 
 
122911
 
    assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
122912
 
        || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
122913
 
    );
122914
 
 
122915
 
    switch( p->op ){
122916
 
      case RTREE_LE: case RTREE_LT: 
122917
 
        bRes = p->rValue<cell_min; 
122918
 
        break;
122919
 
 
122920
 
      case RTREE_GE: case RTREE_GT: 
122921
 
        bRes = p->rValue>cell_max; 
122922
 
        break;
122923
 
 
122924
 
      case RTREE_EQ:
122925
 
        bRes = (p->rValue>cell_max || p->rValue<cell_min);
122926
 
        break;
122927
 
 
122928
 
      default: {
122929
 
        assert( p->op==RTREE_MATCH );
122930
 
        rc = testRtreeGeom(pRtree, p, &cell, &bRes);
122931
 
        bRes = !bRes;
122932
 
        break;
122933
 
      }
122934
 
    }
122935
 
  }
122936
 
 
122937
 
  *pbEof = bRes;
122938
 
  return rc;
122939
 
}
122940
 
 
122941
 
/* 
122942
 
** Test if the cell that cursor pCursor currently points to
122943
 
** would be filtered (excluded) by the constraints in the 
122944
 
** pCursor->aConstraint[] array. If so, set *pbEof to true before
122945
 
** returning. If the cell is not filtered (excluded) by the constraints,
122946
 
** set pbEof to zero.
122947
 
**
122948
 
** Return SQLITE_OK if successful or an SQLite error code if an error
122949
 
** occurs within a geometry callback.
122950
 
**
122951
 
** This function assumes that the cell is part of a leaf node.
122952
 
*/
122953
 
static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
122954
 
  RtreeCell cell;
122955
 
  int ii;
122956
 
  *pbEof = 0;
122957
 
 
122958
 
  nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
122959
 
  for(ii=0; ii<pCursor->nConstraint; ii++){
122960
 
    RtreeConstraint *p = &pCursor->aConstraint[ii];
122961
 
    double coord = DCOORD(cell.aCoord[p->iCoord]);
122962
 
    int res;
122963
 
    assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
122964
 
        || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
122965
 
    );
122966
 
    switch( p->op ){
122967
 
      case RTREE_LE: res = (coord<=p->rValue); break;
122968
 
      case RTREE_LT: res = (coord<p->rValue);  break;
122969
 
      case RTREE_GE: res = (coord>=p->rValue); break;
122970
 
      case RTREE_GT: res = (coord>p->rValue);  break;
122971
 
      case RTREE_EQ: res = (coord==p->rValue); break;
122972
 
      default: {
122973
 
        int rc;
122974
 
        assert( p->op==RTREE_MATCH );
122975
 
        rc = testRtreeGeom(pRtree, p, &cell, &res);
122976
 
        if( rc!=SQLITE_OK ){
122977
 
          return rc;
122978
 
        }
122979
 
        break;
122980
 
      }
122981
 
    }
122982
 
 
122983
 
    if( !res ){
122984
 
      *pbEof = 1;
122985
 
      return SQLITE_OK;
122986
 
    }
122987
 
  }
122988
 
 
122989
 
  return SQLITE_OK;
122990
 
}
122991
 
 
122992
 
/*
122993
 
** Cursor pCursor currently points at a node that heads a sub-tree of
122994
 
** height iHeight (if iHeight==0, then the node is a leaf). Descend
122995
 
** to point to the left-most cell of the sub-tree that matches the 
122996
 
** configured constraints.
122997
 
*/
122998
 
static int descendToCell(
122999
 
  Rtree *pRtree, 
123000
 
  RtreeCursor *pCursor, 
123001
 
  int iHeight,
123002
 
  int *pEof                 /* OUT: Set to true if cannot descend */
123003
 
){
123004
 
  int isEof;
123005
 
  int rc;
123006
 
  int ii;
123007
 
  RtreeNode *pChild;
123008
 
  sqlite3_int64 iRowid;
123009
 
 
123010
 
  RtreeNode *pSavedNode = pCursor->pNode;
123011
 
  int iSavedCell = pCursor->iCell;
123012
 
 
123013
 
  assert( iHeight>=0 );
123014
 
 
123015
 
  if( iHeight==0 ){
123016
 
    rc = testRtreeEntry(pRtree, pCursor, &isEof);
123017
 
  }else{
123018
 
    rc = testRtreeCell(pRtree, pCursor, &isEof);
123019
 
  }
123020
 
  if( rc!=SQLITE_OK || isEof || iHeight==0 ){
123021
 
    goto descend_to_cell_out;
123022
 
  }
123023
 
 
123024
 
  iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell);
123025
 
  rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild);
123026
 
  if( rc!=SQLITE_OK ){
123027
 
    goto descend_to_cell_out;
123028
 
  }
123029
 
 
123030
 
  nodeRelease(pRtree, pCursor->pNode);
123031
 
  pCursor->pNode = pChild;
123032
 
  isEof = 1;
123033
 
  for(ii=0; isEof && ii<NCELL(pChild); ii++){
123034
 
    pCursor->iCell = ii;
123035
 
    rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof);
123036
 
    if( rc!=SQLITE_OK ){
123037
 
      goto descend_to_cell_out;
123038
 
    }
123039
 
  }
123040
 
 
123041
 
  if( isEof ){
123042
 
    assert( pCursor->pNode==pChild );
123043
 
    nodeReference(pSavedNode);
123044
 
    nodeRelease(pRtree, pChild);
123045
 
    pCursor->pNode = pSavedNode;
123046
 
    pCursor->iCell = iSavedCell;
123047
 
  }
123048
 
 
123049
 
descend_to_cell_out:
123050
 
  *pEof = isEof;
123051
 
  return rc;
123052
 
}
123053
 
 
123054
 
/*
123055
 
** One of the cells in node pNode is guaranteed to have a 64-bit 
123056
 
** integer value equal to iRowid. Return the index of this cell.
123057
 
*/
123058
 
static int nodeRowidIndex(
123059
 
  Rtree *pRtree, 
123060
 
  RtreeNode *pNode, 
123061
 
  i64 iRowid,
123062
 
  int *piIndex
123063
 
){
123064
 
  int ii;
123065
 
  int nCell = NCELL(pNode);
123066
 
  for(ii=0; ii<nCell; ii++){
123067
 
    if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
123068
 
      *piIndex = ii;
123069
 
      return SQLITE_OK;
123070
 
    }
123071
 
  }
123072
 
  return SQLITE_CORRUPT;
123073
 
}
123074
 
 
123075
 
/*
123076
 
** Return the index of the cell containing a pointer to node pNode
123077
 
** in its parent. If pNode is the root node, return -1.
123078
 
*/
123079
 
static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
123080
 
  RtreeNode *pParent = pNode->pParent;
123081
 
  if( pParent ){
123082
 
    return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
123083
 
  }
123084
 
  *piIndex = -1;
123085
 
  return SQLITE_OK;
123086
 
}
123087
 
 
123088
 
/* 
123089
 
** Rtree virtual table module xNext method.
123090
 
*/
123091
 
static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
123092
 
  Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab);
123093
 
  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
123094
 
  int rc = SQLITE_OK;
123095
 
 
123096
 
  /* RtreeCursor.pNode must not be NULL. If is is NULL, then this cursor is
123097
 
  ** already at EOF. It is against the rules to call the xNext() method of
123098
 
  ** a cursor that has already reached EOF.
123099
 
  */
123100
 
  assert( pCsr->pNode );
123101
 
 
123102
 
  if( pCsr->iStrategy==1 ){
123103
 
    /* This "scan" is a direct lookup by rowid. There is no next entry. */
123104
 
    nodeRelease(pRtree, pCsr->pNode);
123105
 
    pCsr->pNode = 0;
123106
 
  }else{
123107
 
    /* Move to the next entry that matches the configured constraints. */
123108
 
    int iHeight = 0;
123109
 
    while( pCsr->pNode ){
123110
 
      RtreeNode *pNode = pCsr->pNode;
123111
 
      int nCell = NCELL(pNode);
123112
 
      for(pCsr->iCell++; pCsr->iCell<nCell; pCsr->iCell++){
123113
 
        int isEof;
123114
 
        rc = descendToCell(pRtree, pCsr, iHeight, &isEof);
123115
 
        if( rc!=SQLITE_OK || !isEof ){
123116
 
          return rc;
123117
 
        }
123118
 
      }
123119
 
      pCsr->pNode = pNode->pParent;
123120
 
      rc = nodeParentIndex(pRtree, pNode, &pCsr->iCell);
123121
 
      if( rc!=SQLITE_OK ){
123122
 
        return rc;
123123
 
      }
123124
 
      nodeReference(pCsr->pNode);
123125
 
      nodeRelease(pRtree, pNode);
123126
 
      iHeight++;
123127
 
    }
123128
 
  }
123129
 
 
123130
 
  return rc;
123131
 
}
123132
 
 
123133
 
/* 
123134
 
** Rtree virtual table module xRowid method.
123135
 
*/
123136
 
static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
123137
 
  Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
123138
 
  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
123139
 
 
123140
 
  assert(pCsr->pNode);
123141
 
  *pRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
123142
 
 
123143
 
  return SQLITE_OK;
123144
 
}
123145
 
 
123146
 
/* 
123147
 
** Rtree virtual table module xColumn method.
123148
 
*/
123149
 
static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
123150
 
  Rtree *pRtree = (Rtree *)cur->pVtab;
123151
 
  RtreeCursor *pCsr = (RtreeCursor *)cur;
123152
 
 
123153
 
  if( i==0 ){
123154
 
    i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
123155
 
    sqlite3_result_int64(ctx, iRowid);
123156
 
  }else{
123157
 
    RtreeCoord c;
123158
 
    nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
123159
 
    if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
123160
 
      sqlite3_result_double(ctx, c.f);
123161
 
    }else{
123162
 
      assert( pRtree->eCoordType==RTREE_COORD_INT32 );
123163
 
      sqlite3_result_int(ctx, c.i);
123164
 
    }
123165
 
  }
123166
 
 
123167
 
  return SQLITE_OK;
123168
 
}
123169
 
 
123170
 
/* 
123171
 
** Use nodeAcquire() to obtain the leaf node containing the record with 
123172
 
** rowid iRowid. If successful, set *ppLeaf to point to the node and
123173
 
** return SQLITE_OK. If there is no such record in the table, set
123174
 
** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
123175
 
** to zero and return an SQLite error code.
123176
 
*/
123177
 
static int findLeafNode(Rtree *pRtree, i64 iRowid, RtreeNode **ppLeaf){
123178
 
  int rc;
123179
 
  *ppLeaf = 0;
123180
 
  sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
123181
 
  if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
123182
 
    i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
123183
 
    rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
123184
 
    sqlite3_reset(pRtree->pReadRowid);
123185
 
  }else{
123186
 
    rc = sqlite3_reset(pRtree->pReadRowid);
123187
 
  }
123188
 
  return rc;
123189
 
}
123190
 
 
123191
 
/*
123192
 
** This function is called to configure the RtreeConstraint object passed
123193
 
** as the second argument for a MATCH constraint. The value passed as the
123194
 
** first argument to this function is the right-hand operand to the MATCH
123195
 
** operator.
123196
 
*/
123197
 
static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
123198
 
  RtreeMatchArg *p;
123199
 
  sqlite3_rtree_geometry *pGeom;
123200
 
  int nBlob;
123201
 
 
123202
 
  /* Check that value is actually a blob. */
123203
 
  if( !sqlite3_value_type(pValue)==SQLITE_BLOB ) return SQLITE_ERROR;
123204
 
 
123205
 
  /* Check that the blob is roughly the right size. */
123206
 
  nBlob = sqlite3_value_bytes(pValue);
123207
 
  if( nBlob<(int)sizeof(RtreeMatchArg) 
123208
 
   || ((nBlob-sizeof(RtreeMatchArg))%sizeof(double))!=0
123209
 
  ){
123210
 
    return SQLITE_ERROR;
123211
 
  }
123212
 
 
123213
 
  pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
123214
 
      sizeof(sqlite3_rtree_geometry) + nBlob
123215
 
  );
123216
 
  if( !pGeom ) return SQLITE_NOMEM;
123217
 
  memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
123218
 
  p = (RtreeMatchArg *)&pGeom[1];
123219
 
 
123220
 
  memcpy(p, sqlite3_value_blob(pValue), nBlob);
123221
 
  if( p->magic!=RTREE_GEOMETRY_MAGIC 
123222
 
   || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(double))
123223
 
  ){
123224
 
    sqlite3_free(pGeom);
123225
 
    return SQLITE_ERROR;
123226
 
  }
123227
 
 
123228
 
  pGeom->pContext = p->pContext;
123229
 
  pGeom->nParam = p->nParam;
123230
 
  pGeom->aParam = p->aParam;
123231
 
 
123232
 
  pCons->xGeom = p->xGeom;
123233
 
  pCons->pGeom = pGeom;
123234
 
  return SQLITE_OK;
123235
 
}
123236
 
 
123237
 
/* 
123238
 
** Rtree virtual table module xFilter method.
123239
 
*/
123240
 
static int rtreeFilter(
123241
 
  sqlite3_vtab_cursor *pVtabCursor, 
123242
 
  int idxNum, const char *idxStr,
123243
 
  int argc, sqlite3_value **argv
123244
 
){
123245
 
  Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
123246
 
  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
123247
 
 
123248
 
  RtreeNode *pRoot = 0;
123249
 
  int ii;
123250
 
  int rc = SQLITE_OK;
123251
 
 
123252
 
  rtreeReference(pRtree);
123253
 
 
123254
 
  freeCursorConstraints(pCsr);
123255
 
  pCsr->iStrategy = idxNum;
123256
 
 
123257
 
  if( idxNum==1 ){
123258
 
    /* Special case - lookup by rowid. */
123259
 
    RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
123260
 
    i64 iRowid = sqlite3_value_int64(argv[0]);
123261
 
    rc = findLeafNode(pRtree, iRowid, &pLeaf);
123262
 
    pCsr->pNode = pLeaf; 
123263
 
    if( pLeaf ){
123264
 
      assert( rc==SQLITE_OK );
123265
 
      rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &pCsr->iCell);
123266
 
    }
123267
 
  }else{
123268
 
    /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array 
123269
 
    ** with the configured constraints. 
123270
 
    */
123271
 
    if( argc>0 ){
123272
 
      pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
123273
 
      pCsr->nConstraint = argc;
123274
 
      if( !pCsr->aConstraint ){
123275
 
        rc = SQLITE_NOMEM;
123276
 
      }else{
123277
 
        memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
123278
 
        assert( (idxStr==0 && argc==0) || (int)strlen(idxStr)==argc*2 );
123279
 
        for(ii=0; ii<argc; ii++){
123280
 
          RtreeConstraint *p = &pCsr->aConstraint[ii];
123281
 
          p->op = idxStr[ii*2];
123282
 
          p->iCoord = idxStr[ii*2+1]-'a';
123283
 
          if( p->op==RTREE_MATCH ){
123284
 
            /* A MATCH operator. The right-hand-side must be a blob that
123285
 
            ** can be cast into an RtreeMatchArg object. One created using
123286
 
            ** an sqlite3_rtree_geometry_callback() SQL user function.
123287
 
            */
123288
 
            rc = deserializeGeometry(argv[ii], p);
123289
 
            if( rc!=SQLITE_OK ){
123290
 
              break;
123291
 
            }
123292
 
          }else{
123293
 
            p->rValue = sqlite3_value_double(argv[ii]);
123294
 
          }
123295
 
        }
123296
 
      }
123297
 
    }
123298
 
  
123299
 
    if( rc==SQLITE_OK ){
123300
 
      pCsr->pNode = 0;
123301
 
      rc = nodeAcquire(pRtree, 1, 0, &pRoot);
123302
 
    }
123303
 
    if( rc==SQLITE_OK ){
123304
 
      int isEof = 1;
123305
 
      int nCell = NCELL(pRoot);
123306
 
      pCsr->pNode = pRoot;
123307
 
      for(pCsr->iCell=0; rc==SQLITE_OK && pCsr->iCell<nCell; pCsr->iCell++){
123308
 
        assert( pCsr->pNode==pRoot );
123309
 
        rc = descendToCell(pRtree, pCsr, pRtree->iDepth, &isEof);
123310
 
        if( !isEof ){
123311
 
          break;
123312
 
        }
123313
 
      }
123314
 
      if( rc==SQLITE_OK && isEof ){
123315
 
        assert( pCsr->pNode==pRoot );
123316
 
        nodeRelease(pRtree, pRoot);
123317
 
        pCsr->pNode = 0;
123318
 
      }
123319
 
      assert( rc!=SQLITE_OK || !pCsr->pNode || pCsr->iCell<NCELL(pCsr->pNode) );
123320
 
    }
123321
 
  }
123322
 
 
123323
 
  rtreeRelease(pRtree);
123324
 
  return rc;
123325
 
}
123326
 
 
123327
 
/*
123328
 
** Rtree virtual table module xBestIndex method. There are three
123329
 
** table scan strategies to choose from (in order from most to 
123330
 
** least desirable):
123331
 
**
123332
 
**   idxNum     idxStr        Strategy
123333
 
**   ------------------------------------------------
123334
 
**     1        Unused        Direct lookup by rowid.
123335
 
**     2        See below     R-tree query or full-table scan.
123336
 
**   ------------------------------------------------
123337
 
**
123338
 
** If strategy 1 is used, then idxStr is not meaningful. If strategy
123339
 
** 2 is used, idxStr is formatted to contain 2 bytes for each 
123340
 
** constraint used. The first two bytes of idxStr correspond to 
123341
 
** the constraint in sqlite3_index_info.aConstraintUsage[] with
123342
 
** (argvIndex==1) etc.
123343
 
**
123344
 
** The first of each pair of bytes in idxStr identifies the constraint
123345
 
** operator as follows:
123346
 
**
123347
 
**   Operator    Byte Value
123348
 
**   ----------------------
123349
 
**      =        0x41 ('A')
123350
 
**     <=        0x42 ('B')
123351
 
**      <        0x43 ('C')
123352
 
**     >=        0x44 ('D')
123353
 
**      >        0x45 ('E')
123354
 
**   MATCH       0x46 ('F')
123355
 
**   ----------------------
123356
 
**
123357
 
** The second of each pair of bytes identifies the coordinate column
123358
 
** to which the constraint applies. The leftmost coordinate column
123359
 
** is 'a', the second from the left 'b' etc.
123360
 
*/
123361
 
static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
123362
 
  int rc = SQLITE_OK;
123363
 
  int ii;
123364
 
 
123365
 
  int iIdx = 0;
123366
 
  char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
123367
 
  memset(zIdxStr, 0, sizeof(zIdxStr));
123368
 
  UNUSED_PARAMETER(tab);
123369
 
 
123370
 
  assert( pIdxInfo->idxStr==0 );
123371
 
  for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
123372
 
    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
123373
 
 
123374
 
    if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
123375
 
      /* We have an equality constraint on the rowid. Use strategy 1. */
123376
 
      int jj;
123377
 
      for(jj=0; jj<ii; jj++){
123378
 
        pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
123379
 
        pIdxInfo->aConstraintUsage[jj].omit = 0;
123380
 
      }
123381
 
      pIdxInfo->idxNum = 1;
123382
 
      pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
123383
 
      pIdxInfo->aConstraintUsage[jj].omit = 1;
123384
 
 
123385
 
      /* This strategy involves a two rowid lookups on an B-Tree structures
123386
 
      ** and then a linear search of an R-Tree node. This should be 
123387
 
      ** considered almost as quick as a direct rowid lookup (for which 
123388
 
      ** sqlite uses an internal cost of 0.0).
123389
 
      */ 
123390
 
      pIdxInfo->estimatedCost = 10.0;
123391
 
      return SQLITE_OK;
123392
 
    }
123393
 
 
123394
 
    if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
123395
 
      u8 op;
123396
 
      switch( p->op ){
123397
 
        case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
123398
 
        case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
123399
 
        case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
123400
 
        case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
123401
 
        case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
123402
 
        default:
123403
 
          assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
123404
 
          op = RTREE_MATCH; 
123405
 
          break;
123406
 
      }
123407
 
      zIdxStr[iIdx++] = op;
123408
 
      zIdxStr[iIdx++] = p->iColumn - 1 + 'a';
123409
 
      pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
123410
 
      pIdxInfo->aConstraintUsage[ii].omit = 1;
123411
 
    }
123412
 
  }
123413
 
 
123414
 
  pIdxInfo->idxNum = 2;
123415
 
  pIdxInfo->needToFreeIdxStr = 1;
123416
 
  if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
123417
 
    return SQLITE_NOMEM;
123418
 
  }
123419
 
  assert( iIdx>=0 );
123420
 
  pIdxInfo->estimatedCost = (2000000.0 / (double)(iIdx + 1));
123421
 
  return rc;
123422
 
}
123423
 
 
123424
 
/*
123425
 
** Return the N-dimensional volumn of the cell stored in *p.
123426
 
*/
123427
 
static float cellArea(Rtree *pRtree, RtreeCell *p){
123428
 
  float area = 1.0;
123429
 
  int ii;
123430
 
  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
123431
 
    area = area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
123432
 
  }
123433
 
  return area;
123434
 
}
123435
 
 
123436
 
/*
123437
 
** Return the margin length of cell p. The margin length is the sum
123438
 
** of the objects size in each dimension.
123439
 
*/
123440
 
static float cellMargin(Rtree *pRtree, RtreeCell *p){
123441
 
  float margin = 0.0;
123442
 
  int ii;
123443
 
  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
123444
 
    margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
123445
 
  }
123446
 
  return margin;
123447
 
}
123448
 
 
123449
 
/*
123450
 
** Store the union of cells p1 and p2 in p1.
123451
 
*/
123452
 
static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
123453
 
  int ii;
123454
 
  if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
123455
 
    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
123456
 
      p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
123457
 
      p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
123458
 
    }
123459
 
  }else{
123460
 
    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
123461
 
      p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
123462
 
      p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
123463
 
    }
123464
 
  }
123465
 
}
123466
 
 
123467
 
/*
123468
 
** Return true if the area covered by p2 is a subset of the area covered
123469
 
** by p1. False otherwise.
123470
 
*/
123471
 
static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
123472
 
  int ii;
123473
 
  int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
123474
 
  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
123475
 
    RtreeCoord *a1 = &p1->aCoord[ii];
123476
 
    RtreeCoord *a2 = &p2->aCoord[ii];
123477
 
    if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f)) 
123478
 
     || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i)) 
123479
 
    ){
123480
 
      return 0;
123481
 
    }
123482
 
  }
123483
 
  return 1;
123484
 
}
123485
 
 
123486
 
/*
123487
 
** Return the amount cell p would grow by if it were unioned with pCell.
123488
 
*/
123489
 
static float cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
123490
 
  float area;
123491
 
  RtreeCell cell;
123492
 
  memcpy(&cell, p, sizeof(RtreeCell));
123493
 
  area = cellArea(pRtree, &cell);
123494
 
  cellUnion(pRtree, &cell, pCell);
123495
 
  return (cellArea(pRtree, &cell)-area);
123496
 
}
123497
 
 
123498
 
#if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
123499
 
static float cellOverlap(
123500
 
  Rtree *pRtree, 
123501
 
  RtreeCell *p, 
123502
 
  RtreeCell *aCell, 
123503
 
  int nCell, 
123504
 
  int iExclude
123505
 
){
123506
 
  int ii;
123507
 
  float overlap = 0.0;
123508
 
  for(ii=0; ii<nCell; ii++){
123509
 
#if VARIANT_RSTARTREE_CHOOSESUBTREE
123510
 
    if( ii!=iExclude )
123511
 
#else
123512
 
    assert( iExclude==-1 );
123513
 
    UNUSED_PARAMETER(iExclude);
123514
 
#endif
123515
 
    {
123516
 
      int jj;
123517
 
      float o = 1.0;
123518
 
      for(jj=0; jj<(pRtree->nDim*2); jj+=2){
123519
 
        double x1;
123520
 
        double x2;
123521
 
 
123522
 
        x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
123523
 
        x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
123524
 
 
123525
 
        if( x2<x1 ){
123526
 
          o = 0.0;
123527
 
          break;
123528
 
        }else{
123529
 
          o = o * (x2-x1);
123530
 
        }
123531
 
      }
123532
 
      overlap += o;
123533
 
    }
123534
 
  }
123535
 
  return overlap;
123536
 
}
123537
 
#endif
123538
 
 
123539
 
#if VARIANT_RSTARTREE_CHOOSESUBTREE
123540
 
static float cellOverlapEnlargement(
123541
 
  Rtree *pRtree, 
123542
 
  RtreeCell *p, 
123543
 
  RtreeCell *pInsert, 
123544
 
  RtreeCell *aCell, 
123545
 
  int nCell, 
123546
 
  int iExclude
123547
 
){
123548
 
  float before;
123549
 
  float after;
123550
 
  before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
123551
 
  cellUnion(pRtree, p, pInsert);
123552
 
  after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
123553
 
  return after-before;
123554
 
}
123555
 
#endif
123556
 
 
123557
 
 
123558
 
/*
123559
 
** This function implements the ChooseLeaf algorithm from Gutman[84].
123560
 
** ChooseSubTree in r*tree terminology.
123561
 
*/
123562
 
static int ChooseLeaf(
123563
 
  Rtree *pRtree,               /* Rtree table */
123564
 
  RtreeCell *pCell,            /* Cell to insert into rtree */
123565
 
  int iHeight,                 /* Height of sub-tree rooted at pCell */
123566
 
  RtreeNode **ppLeaf           /* OUT: Selected leaf page */
123567
 
){
123568
 
  int rc;
123569
 
  int ii;
123570
 
  RtreeNode *pNode;
123571
 
  rc = nodeAcquire(pRtree, 1, 0, &pNode);
123572
 
 
123573
 
  for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
123574
 
    int iCell;
123575
 
    sqlite3_int64 iBest;
123576
 
 
123577
 
    float fMinGrowth;
123578
 
    float fMinArea;
123579
 
    float fMinOverlap;
123580
 
 
123581
 
    int nCell = NCELL(pNode);
123582
 
    RtreeCell cell;
123583
 
    RtreeNode *pChild;
123584
 
 
123585
 
    RtreeCell *aCell = 0;
123586
 
 
123587
 
#if VARIANT_RSTARTREE_CHOOSESUBTREE
123588
 
    if( ii==(pRtree->iDepth-1) ){
123589
 
      int jj;
123590
 
      aCell = sqlite3_malloc(sizeof(RtreeCell)*nCell);
123591
 
      if( !aCell ){
123592
 
        rc = SQLITE_NOMEM;
123593
 
        nodeRelease(pRtree, pNode);
123594
 
        pNode = 0;
123595
 
        continue;
123596
 
      }
123597
 
      for(jj=0; jj<nCell; jj++){
123598
 
        nodeGetCell(pRtree, pNode, jj, &aCell[jj]);
123599
 
      }
123600
 
    }
123601
 
#endif
123602
 
 
123603
 
    /* Select the child node which will be enlarged the least if pCell
123604
 
    ** is inserted into it. Resolve ties by choosing the entry with
123605
 
    ** the smallest area.
123606
 
    */
123607
 
    for(iCell=0; iCell<nCell; iCell++){
123608
 
      int bBest = 0;
123609
 
      float growth;
123610
 
      float area;
123611
 
      float overlap = 0.0;
123612
 
      nodeGetCell(pRtree, pNode, iCell, &cell);
123613
 
      growth = cellGrowth(pRtree, &cell, pCell);
123614
 
      area = cellArea(pRtree, &cell);
123615
 
 
123616
 
#if VARIANT_RSTARTREE_CHOOSESUBTREE
123617
 
      if( ii==(pRtree->iDepth-1) ){
123618
 
        overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
123619
 
      }
123620
 
      if( (iCell==0) 
123621
 
       || (overlap<fMinOverlap) 
123622
 
       || (overlap==fMinOverlap && growth<fMinGrowth)
123623
 
       || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
123624
 
      ){
123625
 
        bBest = 1;
123626
 
      }
123627
 
#else
123628
 
      if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
123629
 
        bBest = 1;
123630
 
      }
123631
 
#endif
123632
 
      if( bBest ){
123633
 
        fMinOverlap = overlap;
123634
 
        fMinGrowth = growth;
123635
 
        fMinArea = area;
123636
 
        iBest = cell.iRowid;
123637
 
      }
123638
 
    }
123639
 
 
123640
 
    sqlite3_free(aCell);
123641
 
    rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
123642
 
    nodeRelease(pRtree, pNode);
123643
 
    pNode = pChild;
123644
 
  }
123645
 
 
123646
 
  *ppLeaf = pNode;
123647
 
  return rc;
123648
 
}
123649
 
 
123650
 
/*
123651
 
** A cell with the same content as pCell has just been inserted into
123652
 
** the node pNode. This function updates the bounding box cells in
123653
 
** all ancestor elements.
123654
 
*/
123655
 
static int AdjustTree(
123656
 
  Rtree *pRtree,                    /* Rtree table */
123657
 
  RtreeNode *pNode,                 /* Adjust ancestry of this node. */
123658
 
  RtreeCell *pCell                  /* This cell was just inserted */
123659
 
){
123660
 
  RtreeNode *p = pNode;
123661
 
  while( p->pParent ){
123662
 
    RtreeNode *pParent = p->pParent;
123663
 
    RtreeCell cell;
123664
 
    int iCell;
123665
 
 
123666
 
    if( nodeParentIndex(pRtree, p, &iCell) ){
123667
 
      return SQLITE_CORRUPT;
123668
 
    }
123669
 
 
123670
 
    nodeGetCell(pRtree, pParent, iCell, &cell);
123671
 
    if( !cellContains(pRtree, &cell, pCell) ){
123672
 
      cellUnion(pRtree, &cell, pCell);
123673
 
      nodeOverwriteCell(pRtree, pParent, &cell, iCell);
123674
 
    }
123675
 
 
123676
 
    p = pParent;
123677
 
  }
123678
 
  return SQLITE_OK;
123679
 
}
123680
 
 
123681
 
/*
123682
 
** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
123683
 
*/
123684
 
static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
123685
 
  sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
123686
 
  sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
123687
 
  sqlite3_step(pRtree->pWriteRowid);
123688
 
  return sqlite3_reset(pRtree->pWriteRowid);
123689
 
}
123690
 
 
123691
 
/*
123692
 
** Write mapping (iNode->iPar) to the <rtree>_parent table.
123693
 
*/
123694
 
static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
123695
 
  sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
123696
 
  sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
123697
 
  sqlite3_step(pRtree->pWriteParent);
123698
 
  return sqlite3_reset(pRtree->pWriteParent);
123699
 
}
123700
 
 
123701
 
static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
123702
 
 
123703
 
#if VARIANT_GUTTMAN_LINEAR_SPLIT
123704
 
/*
123705
 
** Implementation of the linear variant of the PickNext() function from
123706
 
** Guttman[84].
123707
 
*/
123708
 
static RtreeCell *LinearPickNext(
123709
 
  Rtree *pRtree,
123710
 
  RtreeCell *aCell, 
123711
 
  int nCell, 
123712
 
  RtreeCell *pLeftBox, 
123713
 
  RtreeCell *pRightBox,
123714
 
  int *aiUsed
123715
 
){
123716
 
  int ii;
123717
 
  for(ii=0; aiUsed[ii]; ii++);
123718
 
  aiUsed[ii] = 1;
123719
 
  return &aCell[ii];
123720
 
}
123721
 
 
123722
 
/*
123723
 
** Implementation of the linear variant of the PickSeeds() function from
123724
 
** Guttman[84].
123725
 
*/
123726
 
static void LinearPickSeeds(
123727
 
  Rtree *pRtree,
123728
 
  RtreeCell *aCell, 
123729
 
  int nCell, 
123730
 
  int *piLeftSeed, 
123731
 
  int *piRightSeed
123732
 
){
123733
 
  int i;
123734
 
  int iLeftSeed = 0;
123735
 
  int iRightSeed = 1;
123736
 
  float maxNormalInnerWidth = 0.0;
123737
 
 
123738
 
  /* Pick two "seed" cells from the array of cells. The algorithm used
123739
 
  ** here is the LinearPickSeeds algorithm from Gutman[1984]. The 
123740
 
  ** indices of the two seed cells in the array are stored in local
123741
 
  ** variables iLeftSeek and iRightSeed.
123742
 
  */
123743
 
  for(i=0; i<pRtree->nDim; i++){
123744
 
    float x1 = DCOORD(aCell[0].aCoord[i*2]);
123745
 
    float x2 = DCOORD(aCell[0].aCoord[i*2+1]);
123746
 
    float x3 = x1;
123747
 
    float x4 = x2;
123748
 
    int jj;
123749
 
 
123750
 
    int iCellLeft = 0;
123751
 
    int iCellRight = 0;
123752
 
 
123753
 
    for(jj=1; jj<nCell; jj++){
123754
 
      float left = DCOORD(aCell[jj].aCoord[i*2]);
123755
 
      float right = DCOORD(aCell[jj].aCoord[i*2+1]);
123756
 
 
123757
 
      if( left<x1 ) x1 = left;
123758
 
      if( right>x4 ) x4 = right;
123759
 
      if( left>x3 ){
123760
 
        x3 = left;
123761
 
        iCellRight = jj;
123762
 
      }
123763
 
      if( right<x2 ){
123764
 
        x2 = right;
123765
 
        iCellLeft = jj;
123766
 
      }
123767
 
    }
123768
 
 
123769
 
    if( x4!=x1 ){
123770
 
      float normalwidth = (x3 - x2) / (x4 - x1);
123771
 
      if( normalwidth>maxNormalInnerWidth ){
123772
 
        iLeftSeed = iCellLeft;
123773
 
        iRightSeed = iCellRight;
123774
 
      }
123775
 
    }
123776
 
  }
123777
 
 
123778
 
  *piLeftSeed = iLeftSeed;
123779
 
  *piRightSeed = iRightSeed;
123780
 
}
123781
 
#endif /* VARIANT_GUTTMAN_LINEAR_SPLIT */
123782
 
 
123783
 
#if VARIANT_GUTTMAN_QUADRATIC_SPLIT
123784
 
/*
123785
 
** Implementation of the quadratic variant of the PickNext() function from
123786
 
** Guttman[84].
123787
 
*/
123788
 
static RtreeCell *QuadraticPickNext(
123789
 
  Rtree *pRtree,
123790
 
  RtreeCell *aCell, 
123791
 
  int nCell, 
123792
 
  RtreeCell *pLeftBox, 
123793
 
  RtreeCell *pRightBox,
123794
 
  int *aiUsed
123795
 
){
123796
 
  #define FABS(a) ((a)<0.0?-1.0*(a):(a))
123797
 
 
123798
 
  int iSelect = -1;
123799
 
  float fDiff;
123800
 
  int ii;
123801
 
  for(ii=0; ii<nCell; ii++){
123802
 
    if( aiUsed[ii]==0 ){
123803
 
      float left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
123804
 
      float right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
123805
 
      float diff = FABS(right-left);
123806
 
      if( iSelect<0 || diff>fDiff ){
123807
 
        fDiff = diff;
123808
 
        iSelect = ii;
123809
 
      }
123810
 
    }
123811
 
  }
123812
 
  aiUsed[iSelect] = 1;
123813
 
  return &aCell[iSelect];
123814
 
}
123815
 
 
123816
 
/*
123817
 
** Implementation of the quadratic variant of the PickSeeds() function from
123818
 
** Guttman[84].
123819
 
*/
123820
 
static void QuadraticPickSeeds(
123821
 
  Rtree *pRtree,
123822
 
  RtreeCell *aCell, 
123823
 
  int nCell, 
123824
 
  int *piLeftSeed, 
123825
 
  int *piRightSeed
123826
 
){
123827
 
  int ii;
123828
 
  int jj;
123829
 
 
123830
 
  int iLeftSeed = 0;
123831
 
  int iRightSeed = 1;
123832
 
  float fWaste = 0.0;
123833
 
 
123834
 
  for(ii=0; ii<nCell; ii++){
123835
 
    for(jj=ii+1; jj<nCell; jj++){
123836
 
      float right = cellArea(pRtree, &aCell[jj]);
123837
 
      float growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
123838
 
      float waste = growth - right;
123839
 
 
123840
 
      if( waste>fWaste ){
123841
 
        iLeftSeed = ii;
123842
 
        iRightSeed = jj;
123843
 
        fWaste = waste;
123844
 
      }
123845
 
    }
123846
 
  }
123847
 
 
123848
 
  *piLeftSeed = iLeftSeed;
123849
 
  *piRightSeed = iRightSeed;
123850
 
}
123851
 
#endif /* VARIANT_GUTTMAN_QUADRATIC_SPLIT */
123852
 
 
123853
 
/*
123854
 
** Arguments aIdx, aDistance and aSpare all point to arrays of size
123855
 
** nIdx. The aIdx array contains the set of integers from 0 to 
123856
 
** (nIdx-1) in no particular order. This function sorts the values
123857
 
** in aIdx according to the indexed values in aDistance. For
123858
 
** example, assuming the inputs:
123859
 
**
123860
 
**   aIdx      = { 0,   1,   2,   3 }
123861
 
**   aDistance = { 5.0, 2.0, 7.0, 6.0 }
123862
 
**
123863
 
** this function sets the aIdx array to contain:
123864
 
**
123865
 
**   aIdx      = { 0,   1,   2,   3 }
123866
 
**
123867
 
** The aSpare array is used as temporary working space by the
123868
 
** sorting algorithm.
123869
 
*/
123870
 
static void SortByDistance(
123871
 
  int *aIdx, 
123872
 
  int nIdx, 
123873
 
  float *aDistance, 
123874
 
  int *aSpare
123875
 
){
123876
 
  if( nIdx>1 ){
123877
 
    int iLeft = 0;
123878
 
    int iRight = 0;
123879
 
 
123880
 
    int nLeft = nIdx/2;
123881
 
    int nRight = nIdx-nLeft;
123882
 
    int *aLeft = aIdx;
123883
 
    int *aRight = &aIdx[nLeft];
123884
 
 
123885
 
    SortByDistance(aLeft, nLeft, aDistance, aSpare);
123886
 
    SortByDistance(aRight, nRight, aDistance, aSpare);
123887
 
 
123888
 
    memcpy(aSpare, aLeft, sizeof(int)*nLeft);
123889
 
    aLeft = aSpare;
123890
 
 
123891
 
    while( iLeft<nLeft || iRight<nRight ){
123892
 
      if( iLeft==nLeft ){
123893
 
        aIdx[iLeft+iRight] = aRight[iRight];
123894
 
        iRight++;
123895
 
      }else if( iRight==nRight ){
123896
 
        aIdx[iLeft+iRight] = aLeft[iLeft];
123897
 
        iLeft++;
123898
 
      }else{
123899
 
        float fLeft = aDistance[aLeft[iLeft]];
123900
 
        float fRight = aDistance[aRight[iRight]];
123901
 
        if( fLeft<fRight ){
123902
 
          aIdx[iLeft+iRight] = aLeft[iLeft];
123903
 
          iLeft++;
123904
 
        }else{
123905
 
          aIdx[iLeft+iRight] = aRight[iRight];
123906
 
          iRight++;
123907
 
        }
123908
 
      }
123909
 
    }
123910
 
 
123911
 
#if 0
123912
 
    /* Check that the sort worked */
123913
 
    {
123914
 
      int jj;
123915
 
      for(jj=1; jj<nIdx; jj++){
123916
 
        float left = aDistance[aIdx[jj-1]];
123917
 
        float right = aDistance[aIdx[jj]];
123918
 
        assert( left<=right );
123919
 
      }
123920
 
    }
123921
 
#endif
123922
 
  }
123923
 
}
123924
 
 
123925
 
/*
123926
 
** Arguments aIdx, aCell and aSpare all point to arrays of size
123927
 
** nIdx. The aIdx array contains the set of integers from 0 to 
123928
 
** (nIdx-1) in no particular order. This function sorts the values
123929
 
** in aIdx according to dimension iDim of the cells in aCell. The
123930
 
** minimum value of dimension iDim is considered first, the
123931
 
** maximum used to break ties.
123932
 
**
123933
 
** The aSpare array is used as temporary working space by the
123934
 
** sorting algorithm.
123935
 
*/
123936
 
static void SortByDimension(
123937
 
  Rtree *pRtree,
123938
 
  int *aIdx, 
123939
 
  int nIdx, 
123940
 
  int iDim, 
123941
 
  RtreeCell *aCell, 
123942
 
  int *aSpare
123943
 
){
123944
 
  if( nIdx>1 ){
123945
 
 
123946
 
    int iLeft = 0;
123947
 
    int iRight = 0;
123948
 
 
123949
 
    int nLeft = nIdx/2;
123950
 
    int nRight = nIdx-nLeft;
123951
 
    int *aLeft = aIdx;
123952
 
    int *aRight = &aIdx[nLeft];
123953
 
 
123954
 
    SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
123955
 
    SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
123956
 
 
123957
 
    memcpy(aSpare, aLeft, sizeof(int)*nLeft);
123958
 
    aLeft = aSpare;
123959
 
    while( iLeft<nLeft || iRight<nRight ){
123960
 
      double xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
123961
 
      double xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
123962
 
      double xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
123963
 
      double xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
123964
 
      if( (iLeft!=nLeft) && ((iRight==nRight)
123965
 
       || (xleft1<xright1)
123966
 
       || (xleft1==xright1 && xleft2<xright2)
123967
 
      )){
123968
 
        aIdx[iLeft+iRight] = aLeft[iLeft];
123969
 
        iLeft++;
123970
 
      }else{
123971
 
        aIdx[iLeft+iRight] = aRight[iRight];
123972
 
        iRight++;
123973
 
      }
123974
 
    }
123975
 
 
123976
 
#if 0
123977
 
    /* Check that the sort worked */
123978
 
    {
123979
 
      int jj;
123980
 
      for(jj=1; jj<nIdx; jj++){
123981
 
        float xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
123982
 
        float xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
123983
 
        float xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
123984
 
        float xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
123985
 
        assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
123986
 
      }
123987
 
    }
123988
 
#endif
123989
 
  }
123990
 
}
123991
 
 
123992
 
#if VARIANT_RSTARTREE_SPLIT
123993
 
/*
123994
 
** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
123995
 
*/
123996
 
static int splitNodeStartree(
123997
 
  Rtree *pRtree,
123998
 
  RtreeCell *aCell,
123999
 
  int nCell,
124000
 
  RtreeNode *pLeft,
124001
 
  RtreeNode *pRight,
124002
 
  RtreeCell *pBboxLeft,
124003
 
  RtreeCell *pBboxRight
124004
 
){
124005
 
  int **aaSorted;
124006
 
  int *aSpare;
124007
 
  int ii;
124008
 
 
124009
 
  int iBestDim;
124010
 
  int iBestSplit;
124011
 
  float fBestMargin;
124012
 
 
124013
 
  int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
124014
 
 
124015
 
  aaSorted = (int **)sqlite3_malloc(nByte);
124016
 
  if( !aaSorted ){
124017
 
    return SQLITE_NOMEM;
124018
 
  }
124019
 
 
124020
 
  aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
124021
 
  memset(aaSorted, 0, nByte);
124022
 
  for(ii=0; ii<pRtree->nDim; ii++){
124023
 
    int jj;
124024
 
    aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
124025
 
    for(jj=0; jj<nCell; jj++){
124026
 
      aaSorted[ii][jj] = jj;
124027
 
    }
124028
 
    SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
124029
 
  }
124030
 
 
124031
 
  for(ii=0; ii<pRtree->nDim; ii++){
124032
 
    float margin = 0.0;
124033
 
    float fBestOverlap;
124034
 
    float fBestArea;
124035
 
    int iBestLeft;
124036
 
    int nLeft;
124037
 
 
124038
 
    for(
124039
 
      nLeft=RTREE_MINCELLS(pRtree); 
124040
 
      nLeft<=(nCell-RTREE_MINCELLS(pRtree)); 
124041
 
      nLeft++
124042
 
    ){
124043
 
      RtreeCell left;
124044
 
      RtreeCell right;
124045
 
      int kk;
124046
 
      float overlap;
124047
 
      float area;
124048
 
 
124049
 
      memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
124050
 
      memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
124051
 
      for(kk=1; kk<(nCell-1); kk++){
124052
 
        if( kk<nLeft ){
124053
 
          cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
124054
 
        }else{
124055
 
          cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
124056
 
        }
124057
 
      }
124058
 
      margin += cellMargin(pRtree, &left);
124059
 
      margin += cellMargin(pRtree, &right);
124060
 
      overlap = cellOverlap(pRtree, &left, &right, 1, -1);
124061
 
      area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
124062
 
      if( (nLeft==RTREE_MINCELLS(pRtree))
124063
 
       || (overlap<fBestOverlap)
124064
 
       || (overlap==fBestOverlap && area<fBestArea)
124065
 
      ){
124066
 
        iBestLeft = nLeft;
124067
 
        fBestOverlap = overlap;
124068
 
        fBestArea = area;
124069
 
      }
124070
 
    }
124071
 
 
124072
 
    if( ii==0 || margin<fBestMargin ){
124073
 
      iBestDim = ii;
124074
 
      fBestMargin = margin;
124075
 
      iBestSplit = iBestLeft;
124076
 
    }
124077
 
  }
124078
 
 
124079
 
  memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
124080
 
  memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
124081
 
  for(ii=0; ii<nCell; ii++){
124082
 
    RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
124083
 
    RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
124084
 
    RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
124085
 
    nodeInsertCell(pRtree, pTarget, pCell);
124086
 
    cellUnion(pRtree, pBbox, pCell);
124087
 
  }
124088
 
 
124089
 
  sqlite3_free(aaSorted);
124090
 
  return SQLITE_OK;
124091
 
}
124092
 
#endif
124093
 
 
124094
 
#if VARIANT_GUTTMAN_SPLIT
124095
 
/*
124096
 
** Implementation of the regular R-tree SplitNode from Guttman[1984].
124097
 
*/
124098
 
static int splitNodeGuttman(
124099
 
  Rtree *pRtree,
124100
 
  RtreeCell *aCell,
124101
 
  int nCell,
124102
 
  RtreeNode *pLeft,
124103
 
  RtreeNode *pRight,
124104
 
  RtreeCell *pBboxLeft,
124105
 
  RtreeCell *pBboxRight
124106
 
){
124107
 
  int iLeftSeed = 0;
124108
 
  int iRightSeed = 1;
124109
 
  int *aiUsed;
124110
 
  int i;
124111
 
 
124112
 
  aiUsed = sqlite3_malloc(sizeof(int)*nCell);
124113
 
  if( !aiUsed ){
124114
 
    return SQLITE_NOMEM;
124115
 
  }
124116
 
  memset(aiUsed, 0, sizeof(int)*nCell);
124117
 
 
124118
 
  PickSeeds(pRtree, aCell, nCell, &iLeftSeed, &iRightSeed);
124119
 
 
124120
 
  memcpy(pBboxLeft, &aCell[iLeftSeed], sizeof(RtreeCell));
124121
 
  memcpy(pBboxRight, &aCell[iRightSeed], sizeof(RtreeCell));
124122
 
  nodeInsertCell(pRtree, pLeft, &aCell[iLeftSeed]);
124123
 
  nodeInsertCell(pRtree, pRight, &aCell[iRightSeed]);
124124
 
  aiUsed[iLeftSeed] = 1;
124125
 
  aiUsed[iRightSeed] = 1;
124126
 
 
124127
 
  for(i=nCell-2; i>0; i--){
124128
 
    RtreeCell *pNext;
124129
 
    pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
124130
 
    float diff =  
124131
 
      cellGrowth(pRtree, pBboxLeft, pNext) - 
124132
 
      cellGrowth(pRtree, pBboxRight, pNext)
124133
 
    ;
124134
 
    if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
124135
 
     || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
124136
 
    ){
124137
 
      nodeInsertCell(pRtree, pRight, pNext);
124138
 
      cellUnion(pRtree, pBboxRight, pNext);
124139
 
    }else{
124140
 
      nodeInsertCell(pRtree, pLeft, pNext);
124141
 
      cellUnion(pRtree, pBboxLeft, pNext);
124142
 
    }
124143
 
  }
124144
 
 
124145
 
  sqlite3_free(aiUsed);
124146
 
  return SQLITE_OK;
124147
 
}
124148
 
#endif
124149
 
 
124150
 
static int updateMapping(
124151
 
  Rtree *pRtree, 
124152
 
  i64 iRowid, 
124153
 
  RtreeNode *pNode, 
124154
 
  int iHeight
124155
 
){
124156
 
  int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
124157
 
  xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
124158
 
  if( iHeight>0 ){
124159
 
    RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
124160
 
    if( pChild ){
124161
 
      nodeRelease(pRtree, pChild->pParent);
124162
 
      nodeReference(pNode);
124163
 
      pChild->pParent = pNode;
124164
 
    }
124165
 
  }
124166
 
  return xSetMapping(pRtree, iRowid, pNode->iNode);
124167
 
}
124168
 
 
124169
 
static int SplitNode(
124170
 
  Rtree *pRtree,
124171
 
  RtreeNode *pNode,
124172
 
  RtreeCell *pCell,
124173
 
  int iHeight
124174
 
){
124175
 
  int i;
124176
 
  int newCellIsRight = 0;
124177
 
 
124178
 
  int rc = SQLITE_OK;
124179
 
  int nCell = NCELL(pNode);
124180
 
  RtreeCell *aCell;
124181
 
  int *aiUsed;
124182
 
 
124183
 
  RtreeNode *pLeft = 0;
124184
 
  RtreeNode *pRight = 0;
124185
 
 
124186
 
  RtreeCell leftbbox;
124187
 
  RtreeCell rightbbox;
124188
 
 
124189
 
  /* Allocate an array and populate it with a copy of pCell and 
124190
 
  ** all cells from node pLeft. Then zero the original node.
124191
 
  */
124192
 
  aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
124193
 
  if( !aCell ){
124194
 
    rc = SQLITE_NOMEM;
124195
 
    goto splitnode_out;
124196
 
  }
124197
 
  aiUsed = (int *)&aCell[nCell+1];
124198
 
  memset(aiUsed, 0, sizeof(int)*(nCell+1));
124199
 
  for(i=0; i<nCell; i++){
124200
 
    nodeGetCell(pRtree, pNode, i, &aCell[i]);
124201
 
  }
124202
 
  nodeZero(pRtree, pNode);
124203
 
  memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
124204
 
  nCell++;
124205
 
 
124206
 
  if( pNode->iNode==1 ){
124207
 
    pRight = nodeNew(pRtree, pNode);
124208
 
    pLeft = nodeNew(pRtree, pNode);
124209
 
    pRtree->iDepth++;
124210
 
    pNode->isDirty = 1;
124211
 
    writeInt16(pNode->zData, pRtree->iDepth);
124212
 
  }else{
124213
 
    pLeft = pNode;
124214
 
    pRight = nodeNew(pRtree, pLeft->pParent);
124215
 
    nodeReference(pLeft);
124216
 
  }
124217
 
 
124218
 
  if( !pLeft || !pRight ){
124219
 
    rc = SQLITE_NOMEM;
124220
 
    goto splitnode_out;
124221
 
  }
124222
 
 
124223
 
  memset(pLeft->zData, 0, pRtree->iNodeSize);
124224
 
  memset(pRight->zData, 0, pRtree->iNodeSize);
124225
 
 
124226
 
  rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox);
124227
 
  if( rc!=SQLITE_OK ){
124228
 
    goto splitnode_out;
124229
 
  }
124230
 
 
124231
 
  /* Ensure both child nodes have node numbers assigned to them by calling
124232
 
  ** nodeWrite(). Node pRight always needs a node number, as it was created
124233
 
  ** by nodeNew() above. But node pLeft sometimes already has a node number.
124234
 
  ** In this case avoid the all to nodeWrite().
124235
 
  */
124236
 
  if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
124237
 
   || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
124238
 
  ){
124239
 
    goto splitnode_out;
124240
 
  }
124241
 
 
124242
 
  rightbbox.iRowid = pRight->iNode;
124243
 
  leftbbox.iRowid = pLeft->iNode;
124244
 
 
124245
 
  if( pNode->iNode==1 ){
124246
 
    rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
124247
 
    if( rc!=SQLITE_OK ){
124248
 
      goto splitnode_out;
124249
 
    }
124250
 
  }else{
124251
 
    RtreeNode *pParent = pLeft->pParent;
124252
 
    int iCell;
124253
 
    rc = nodeParentIndex(pRtree, pLeft, &iCell);
124254
 
    if( rc==SQLITE_OK ){
124255
 
      nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
124256
 
      rc = AdjustTree(pRtree, pParent, &leftbbox);
124257
 
    }
124258
 
    if( rc!=SQLITE_OK ){
124259
 
      goto splitnode_out;
124260
 
    }
124261
 
  }
124262
 
  if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
124263
 
    goto splitnode_out;
124264
 
  }
124265
 
 
124266
 
  for(i=0; i<NCELL(pRight); i++){
124267
 
    i64 iRowid = nodeGetRowid(pRtree, pRight, i);
124268
 
    rc = updateMapping(pRtree, iRowid, pRight, iHeight);
124269
 
    if( iRowid==pCell->iRowid ){
124270
 
      newCellIsRight = 1;
124271
 
    }
124272
 
    if( rc!=SQLITE_OK ){
124273
 
      goto splitnode_out;
124274
 
    }
124275
 
  }
124276
 
  if( pNode->iNode==1 ){
124277
 
    for(i=0; i<NCELL(pLeft); i++){
124278
 
      i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
124279
 
      rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
124280
 
      if( rc!=SQLITE_OK ){
124281
 
        goto splitnode_out;
124282
 
      }
124283
 
    }
124284
 
  }else if( newCellIsRight==0 ){
124285
 
    rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
124286
 
  }
124287
 
 
124288
 
  if( rc==SQLITE_OK ){
124289
 
    rc = nodeRelease(pRtree, pRight);
124290
 
    pRight = 0;
124291
 
  }
124292
 
  if( rc==SQLITE_OK ){
124293
 
    rc = nodeRelease(pRtree, pLeft);
124294
 
    pLeft = 0;
124295
 
  }
124296
 
 
124297
 
splitnode_out:
124298
 
  nodeRelease(pRtree, pRight);
124299
 
  nodeRelease(pRtree, pLeft);
124300
 
  sqlite3_free(aCell);
124301
 
  return rc;
124302
 
}
124303
 
 
124304
 
/*
124305
 
** If node pLeaf is not the root of the r-tree and its pParent pointer is 
124306
 
** still NULL, load all ancestor nodes of pLeaf into memory and populate
124307
 
** the pLeaf->pParent chain all the way up to the root node.
124308
 
**
124309
 
** This operation is required when a row is deleted (or updated - an update
124310
 
** is implemented as a delete followed by an insert). SQLite provides the
124311
 
** rowid of the row to delete, which can be used to find the leaf on which
124312
 
** the entry resides (argument pLeaf). Once the leaf is located, this 
124313
 
** function is called to determine its ancestry.
124314
 
*/
124315
 
static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
124316
 
  int rc = SQLITE_OK;
124317
 
  RtreeNode *pChild = pLeaf;
124318
 
  while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
124319
 
    int rc2 = SQLITE_OK;          /* sqlite3_reset() return code */
124320
 
    sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
124321
 
    rc = sqlite3_step(pRtree->pReadParent);
124322
 
    if( rc==SQLITE_ROW ){
124323
 
      RtreeNode *pTest;           /* Used to test for reference loops */
124324
 
      i64 iNode;                  /* Node number of parent node */
124325
 
 
124326
 
      /* Before setting pChild->pParent, test that we are not creating a
124327
 
      ** loop of references (as we would if, say, pChild==pParent). We don't
124328
 
      ** want to do this as it leads to a memory leak when trying to delete
124329
 
      ** the referenced counted node structures.
124330
 
      */
124331
 
      iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
124332
 
      for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
124333
 
      if( !pTest ){
124334
 
        rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
124335
 
      }
124336
 
    }
124337
 
    rc = sqlite3_reset(pRtree->pReadParent);
124338
 
    if( rc==SQLITE_OK ) rc = rc2;
124339
 
    if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT;
124340
 
    pChild = pChild->pParent;
124341
 
  }
124342
 
  return rc;
124343
 
}
124344
 
 
124345
 
static int deleteCell(Rtree *, RtreeNode *, int, int);
124346
 
 
124347
 
static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
124348
 
  int rc;
124349
 
  int rc2;
124350
 
  RtreeNode *pParent;
124351
 
  int iCell;
124352
 
 
124353
 
  assert( pNode->nRef==1 );
124354
 
 
124355
 
  /* Remove the entry in the parent cell. */
124356
 
  rc = nodeParentIndex(pRtree, pNode, &iCell);
124357
 
  if( rc==SQLITE_OK ){
124358
 
    pParent = pNode->pParent;
124359
 
    pNode->pParent = 0;
124360
 
    rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
124361
 
  }
124362
 
  rc2 = nodeRelease(pRtree, pParent);
124363
 
  if( rc==SQLITE_OK ){
124364
 
    rc = rc2;
124365
 
  }
124366
 
  if( rc!=SQLITE_OK ){
124367
 
    return rc;
124368
 
  }
124369
 
 
124370
 
  /* Remove the xxx_node entry. */
124371
 
  sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
124372
 
  sqlite3_step(pRtree->pDeleteNode);
124373
 
  if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
124374
 
    return rc;
124375
 
  }
124376
 
 
124377
 
  /* Remove the xxx_parent entry. */
124378
 
  sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
124379
 
  sqlite3_step(pRtree->pDeleteParent);
124380
 
  if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
124381
 
    return rc;
124382
 
  }
124383
 
  
124384
 
  /* Remove the node from the in-memory hash table and link it into
124385
 
  ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
124386
 
  */
124387
 
  nodeHashDelete(pRtree, pNode);
124388
 
  pNode->iNode = iHeight;
124389
 
  pNode->pNext = pRtree->pDeleted;
124390
 
  pNode->nRef++;
124391
 
  pRtree->pDeleted = pNode;
124392
 
 
124393
 
  return SQLITE_OK;
124394
 
}
124395
 
 
124396
 
static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
124397
 
  RtreeNode *pParent = pNode->pParent;
124398
 
  int rc = SQLITE_OK; 
124399
 
  if( pParent ){
124400
 
    int ii; 
124401
 
    int nCell = NCELL(pNode);
124402
 
    RtreeCell box;                            /* Bounding box for pNode */
124403
 
    nodeGetCell(pRtree, pNode, 0, &box);
124404
 
    for(ii=1; ii<nCell; ii++){
124405
 
      RtreeCell cell;
124406
 
      nodeGetCell(pRtree, pNode, ii, &cell);
124407
 
      cellUnion(pRtree, &box, &cell);
124408
 
    }
124409
 
    box.iRowid = pNode->iNode;
124410
 
    rc = nodeParentIndex(pRtree, pNode, &ii);
124411
 
    if( rc==SQLITE_OK ){
124412
 
      nodeOverwriteCell(pRtree, pParent, &box, ii);
124413
 
      rc = fixBoundingBox(pRtree, pParent);
124414
 
    }
124415
 
  }
124416
 
  return rc;
124417
 
}
124418
 
 
124419
 
/*
124420
 
** Delete the cell at index iCell of node pNode. After removing the
124421
 
** cell, adjust the r-tree data structure if required.
124422
 
*/
124423
 
static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
124424
 
  RtreeNode *pParent;
124425
 
  int rc;
124426
 
 
124427
 
  if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
124428
 
    return rc;
124429
 
  }
124430
 
 
124431
 
  /* Remove the cell from the node. This call just moves bytes around
124432
 
  ** the in-memory node image, so it cannot fail.
124433
 
  */
124434
 
  nodeDeleteCell(pRtree, pNode, iCell);
124435
 
 
124436
 
  /* If the node is not the tree root and now has less than the minimum
124437
 
  ** number of cells, remove it from the tree. Otherwise, update the
124438
 
  ** cell in the parent node so that it tightly contains the updated
124439
 
  ** node.
124440
 
  */
124441
 
  pParent = pNode->pParent;
124442
 
  assert( pParent || pNode->iNode==1 );
124443
 
  if( pParent ){
124444
 
    if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
124445
 
      rc = removeNode(pRtree, pNode, iHeight);
124446
 
    }else{
124447
 
      rc = fixBoundingBox(pRtree, pNode);
124448
 
    }
124449
 
  }
124450
 
 
124451
 
  return rc;
124452
 
}
124453
 
 
124454
 
static int Reinsert(
124455
 
  Rtree *pRtree, 
124456
 
  RtreeNode *pNode, 
124457
 
  RtreeCell *pCell, 
124458
 
  int iHeight
124459
 
){
124460
 
  int *aOrder;
124461
 
  int *aSpare;
124462
 
  RtreeCell *aCell;
124463
 
  float *aDistance;
124464
 
  int nCell;
124465
 
  float aCenterCoord[RTREE_MAX_DIMENSIONS];
124466
 
  int iDim;
124467
 
  int ii;
124468
 
  int rc = SQLITE_OK;
124469
 
 
124470
 
  memset(aCenterCoord, 0, sizeof(float)*RTREE_MAX_DIMENSIONS);
124471
 
 
124472
 
  nCell = NCELL(pNode)+1;
124473
 
 
124474
 
  /* Allocate the buffers used by this operation. The allocation is
124475
 
  ** relinquished before this function returns.
124476
 
  */
124477
 
  aCell = (RtreeCell *)sqlite3_malloc(nCell * (
124478
 
    sizeof(RtreeCell) +         /* aCell array */
124479
 
    sizeof(int)       +         /* aOrder array */
124480
 
    sizeof(int)       +         /* aSpare array */
124481
 
    sizeof(float)               /* aDistance array */
124482
 
  ));
124483
 
  if( !aCell ){
124484
 
    return SQLITE_NOMEM;
124485
 
  }
124486
 
  aOrder    = (int *)&aCell[nCell];
124487
 
  aSpare    = (int *)&aOrder[nCell];
124488
 
  aDistance = (float *)&aSpare[nCell];
124489
 
 
124490
 
  for(ii=0; ii<nCell; ii++){
124491
 
    if( ii==(nCell-1) ){
124492
 
      memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
124493
 
    }else{
124494
 
      nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
124495
 
    }
124496
 
    aOrder[ii] = ii;
124497
 
    for(iDim=0; iDim<pRtree->nDim; iDim++){
124498
 
      aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
124499
 
      aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
124500
 
    }
124501
 
  }
124502
 
  for(iDim=0; iDim<pRtree->nDim; iDim++){
124503
 
    aCenterCoord[iDim] = aCenterCoord[iDim]/((float)nCell*2.0);
124504
 
  }
124505
 
 
124506
 
  for(ii=0; ii<nCell; ii++){
124507
 
    aDistance[ii] = 0.0;
124508
 
    for(iDim=0; iDim<pRtree->nDim; iDim++){
124509
 
      float coord = DCOORD(aCell[ii].aCoord[iDim*2+1]) - 
124510
 
          DCOORD(aCell[ii].aCoord[iDim*2]);
124511
 
      aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
124512
 
    }
124513
 
  }
124514
 
 
124515
 
  SortByDistance(aOrder, nCell, aDistance, aSpare);
124516
 
  nodeZero(pRtree, pNode);
124517
 
 
124518
 
  for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
124519
 
    RtreeCell *p = &aCell[aOrder[ii]];
124520
 
    nodeInsertCell(pRtree, pNode, p);
124521
 
    if( p->iRowid==pCell->iRowid ){
124522
 
      if( iHeight==0 ){
124523
 
        rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
124524
 
      }else{
124525
 
        rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
124526
 
      }
124527
 
    }
124528
 
  }
124529
 
  if( rc==SQLITE_OK ){
124530
 
    rc = fixBoundingBox(pRtree, pNode);
124531
 
  }
124532
 
  for(; rc==SQLITE_OK && ii<nCell; ii++){
124533
 
    /* Find a node to store this cell in. pNode->iNode currently contains
124534
 
    ** the height of the sub-tree headed by the cell.
124535
 
    */
124536
 
    RtreeNode *pInsert;
124537
 
    RtreeCell *p = &aCell[aOrder[ii]];
124538
 
    rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
124539
 
    if( rc==SQLITE_OK ){
124540
 
      int rc2;
124541
 
      rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
124542
 
      rc2 = nodeRelease(pRtree, pInsert);
124543
 
      if( rc==SQLITE_OK ){
124544
 
        rc = rc2;
124545
 
      }
124546
 
    }
124547
 
  }
124548
 
 
124549
 
  sqlite3_free(aCell);
124550
 
  return rc;
124551
 
}
124552
 
 
124553
 
/*
124554
 
** Insert cell pCell into node pNode. Node pNode is the head of a 
124555
 
** subtree iHeight high (leaf nodes have iHeight==0).
124556
 
*/
124557
 
static int rtreeInsertCell(
124558
 
  Rtree *pRtree,
124559
 
  RtreeNode *pNode,
124560
 
  RtreeCell *pCell,
124561
 
  int iHeight
124562
 
){
124563
 
  int rc = SQLITE_OK;
124564
 
  if( iHeight>0 ){
124565
 
    RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
124566
 
    if( pChild ){
124567
 
      nodeRelease(pRtree, pChild->pParent);
124568
 
      nodeReference(pNode);
124569
 
      pChild->pParent = pNode;
124570
 
    }
124571
 
  }
124572
 
  if( nodeInsertCell(pRtree, pNode, pCell) ){
124573
 
#if VARIANT_RSTARTREE_REINSERT
124574
 
    if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
124575
 
      rc = SplitNode(pRtree, pNode, pCell, iHeight);
124576
 
    }else{
124577
 
      pRtree->iReinsertHeight = iHeight;
124578
 
      rc = Reinsert(pRtree, pNode, pCell, iHeight);
124579
 
    }
124580
 
#else
124581
 
    rc = SplitNode(pRtree, pNode, pCell, iHeight);
124582
 
#endif
124583
 
  }else{
124584
 
    rc = AdjustTree(pRtree, pNode, pCell);
124585
 
    if( rc==SQLITE_OK ){
124586
 
      if( iHeight==0 ){
124587
 
        rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
124588
 
      }else{
124589
 
        rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
124590
 
      }
124591
 
    }
124592
 
  }
124593
 
  return rc;
124594
 
}
124595
 
 
124596
 
static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
124597
 
  int ii;
124598
 
  int rc = SQLITE_OK;
124599
 
  int nCell = NCELL(pNode);
124600
 
 
124601
 
  for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
124602
 
    RtreeNode *pInsert;
124603
 
    RtreeCell cell;
124604
 
    nodeGetCell(pRtree, pNode, ii, &cell);
124605
 
 
124606
 
    /* Find a node to store this cell in. pNode->iNode currently contains
124607
 
    ** the height of the sub-tree headed by the cell.
124608
 
    */
124609
 
    rc = ChooseLeaf(pRtree, &cell, pNode->iNode, &pInsert);
124610
 
    if( rc==SQLITE_OK ){
124611
 
      int rc2;
124612
 
      rc = rtreeInsertCell(pRtree, pInsert, &cell, pNode->iNode);
124613
 
      rc2 = nodeRelease(pRtree, pInsert);
124614
 
      if( rc==SQLITE_OK ){
124615
 
        rc = rc2;
124616
 
      }
124617
 
    }
124618
 
  }
124619
 
  return rc;
124620
 
}
124621
 
 
124622
 
/*
124623
 
** Select a currently unused rowid for a new r-tree record.
124624
 
*/
124625
 
static int newRowid(Rtree *pRtree, i64 *piRowid){
124626
 
  int rc;
124627
 
  sqlite3_bind_null(pRtree->pWriteRowid, 1);
124628
 
  sqlite3_bind_null(pRtree->pWriteRowid, 2);
124629
 
  sqlite3_step(pRtree->pWriteRowid);
124630
 
  rc = sqlite3_reset(pRtree->pWriteRowid);
124631
 
  *piRowid = sqlite3_last_insert_rowid(pRtree->db);
124632
 
  return rc;
124633
 
}
124634
 
 
124635
 
/*
124636
 
** The xUpdate method for rtree module virtual tables.
124637
 
*/
124638
 
static int rtreeUpdate(
124639
 
  sqlite3_vtab *pVtab, 
124640
 
  int nData, 
124641
 
  sqlite3_value **azData, 
124642
 
  sqlite_int64 *pRowid
124643
 
){
124644
 
  Rtree *pRtree = (Rtree *)pVtab;
124645
 
  int rc = SQLITE_OK;
124646
 
 
124647
 
  rtreeReference(pRtree);
124648
 
 
124649
 
  assert(nData>=1);
124650
 
 
124651
 
  /* If azData[0] is not an SQL NULL value, it is the rowid of a
124652
 
  ** record to delete from the r-tree table. The following block does
124653
 
  ** just that.
124654
 
  */
124655
 
  if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){
124656
 
    i64 iDelete;                /* The rowid to delete */
124657
 
    RtreeNode *pLeaf;           /* Leaf node containing record iDelete */
124658
 
    int iCell;                  /* Index of iDelete cell in pLeaf */
124659
 
    RtreeNode *pRoot;
124660
 
 
124661
 
    /* Obtain a reference to the root node to initialise Rtree.iDepth */
124662
 
    rc = nodeAcquire(pRtree, 1, 0, &pRoot);
124663
 
 
124664
 
    /* Obtain a reference to the leaf node that contains the entry 
124665
 
    ** about to be deleted. 
124666
 
    */
124667
 
    if( rc==SQLITE_OK ){
124668
 
      iDelete = sqlite3_value_int64(azData[0]);
124669
 
      rc = findLeafNode(pRtree, iDelete, &pLeaf);
124670
 
    }
124671
 
 
124672
 
    /* Delete the cell in question from the leaf node. */
124673
 
    if( rc==SQLITE_OK ){
124674
 
      int rc2;
124675
 
      rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
124676
 
      if( rc==SQLITE_OK ){
124677
 
        rc = deleteCell(pRtree, pLeaf, iCell, 0);
124678
 
      }
124679
 
      rc2 = nodeRelease(pRtree, pLeaf);
124680
 
      if( rc==SQLITE_OK ){
124681
 
        rc = rc2;
124682
 
      }
124683
 
    }
124684
 
 
124685
 
    /* Delete the corresponding entry in the <rtree>_rowid table. */
124686
 
    if( rc==SQLITE_OK ){
124687
 
      sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
124688
 
      sqlite3_step(pRtree->pDeleteRowid);
124689
 
      rc = sqlite3_reset(pRtree->pDeleteRowid);
124690
 
    }
124691
 
 
124692
 
    /* Check if the root node now has exactly one child. If so, remove
124693
 
    ** it, schedule the contents of the child for reinsertion and 
124694
 
    ** reduce the tree height by one.
124695
 
    **
124696
 
    ** This is equivalent to copying the contents of the child into
124697
 
    ** the root node (the operation that Gutman's paper says to perform 
124698
 
    ** in this scenario).
124699
 
    */
124700
 
    if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
124701
 
      int rc2;
124702
 
      RtreeNode *pChild;
124703
 
      i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
124704
 
      rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
124705
 
      if( rc==SQLITE_OK ){
124706
 
        rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
124707
 
      }
124708
 
      rc2 = nodeRelease(pRtree, pChild);
124709
 
      if( rc==SQLITE_OK ) rc = rc2;
124710
 
      if( rc==SQLITE_OK ){
124711
 
        pRtree->iDepth--;
124712
 
        writeInt16(pRoot->zData, pRtree->iDepth);
124713
 
        pRoot->isDirty = 1;
124714
 
      }
124715
 
    }
124716
 
 
124717
 
    /* Re-insert the contents of any underfull nodes removed from the tree. */
124718
 
    for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
124719
 
      if( rc==SQLITE_OK ){
124720
 
        rc = reinsertNodeContent(pRtree, pLeaf);
124721
 
      }
124722
 
      pRtree->pDeleted = pLeaf->pNext;
124723
 
      sqlite3_free(pLeaf);
124724
 
    }
124725
 
 
124726
 
    /* Release the reference to the root node. */
124727
 
    if( rc==SQLITE_OK ){
124728
 
      rc = nodeRelease(pRtree, pRoot);
124729
 
    }else{
124730
 
      nodeRelease(pRtree, pRoot);
124731
 
    }
124732
 
  }
124733
 
 
124734
 
  /* If the azData[] array contains more than one element, elements
124735
 
  ** (azData[2]..azData[argc-1]) contain a new record to insert into
124736
 
  ** the r-tree structure.
124737
 
  */
124738
 
  if( rc==SQLITE_OK && nData>1 ){
124739
 
    /* Insert a new record into the r-tree */
124740
 
    RtreeCell cell;
124741
 
    int ii;
124742
 
    RtreeNode *pLeaf;
124743
 
 
124744
 
    /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
124745
 
    assert( nData==(pRtree->nDim*2 + 3) );
124746
 
    if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
124747
 
      for(ii=0; ii<(pRtree->nDim*2); ii+=2){
124748
 
        cell.aCoord[ii].f = (float)sqlite3_value_double(azData[ii+3]);
124749
 
        cell.aCoord[ii+1].f = (float)sqlite3_value_double(azData[ii+4]);
124750
 
        if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
124751
 
          rc = SQLITE_CONSTRAINT;
124752
 
          goto constraint;
124753
 
        }
124754
 
      }
124755
 
    }else{
124756
 
      for(ii=0; ii<(pRtree->nDim*2); ii+=2){
124757
 
        cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
124758
 
        cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
124759
 
        if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
124760
 
          rc = SQLITE_CONSTRAINT;
124761
 
          goto constraint;
124762
 
        }
124763
 
      }
124764
 
    }
124765
 
 
124766
 
    /* Figure out the rowid of the new row. */
124767
 
    if( sqlite3_value_type(azData[2])==SQLITE_NULL ){
124768
 
      rc = newRowid(pRtree, &cell.iRowid);
124769
 
    }else{
124770
 
      cell.iRowid = sqlite3_value_int64(azData[2]);
124771
 
      sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
124772
 
      if( SQLITE_ROW==sqlite3_step(pRtree->pReadRowid) ){
124773
 
        sqlite3_reset(pRtree->pReadRowid);
124774
 
        rc = SQLITE_CONSTRAINT;
124775
 
        goto constraint;
124776
 
      }
124777
 
      rc = sqlite3_reset(pRtree->pReadRowid);
124778
 
    }
124779
 
    *pRowid = cell.iRowid;
124780
 
 
124781
 
    if( rc==SQLITE_OK ){
124782
 
      rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
124783
 
    }
124784
 
    if( rc==SQLITE_OK ){
124785
 
      int rc2;
124786
 
      pRtree->iReinsertHeight = -1;
124787
 
      rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
124788
 
      rc2 = nodeRelease(pRtree, pLeaf);
124789
 
      if( rc==SQLITE_OK ){
124790
 
        rc = rc2;
124791
 
      }
124792
 
    }
124793
 
  }
124794
 
 
124795
 
constraint:
124796
 
  rtreeRelease(pRtree);
124797
 
  return rc;
124798
 
}
124799
 
 
124800
 
/*
124801
 
** The xRename method for rtree module virtual tables.
124802
 
*/
124803
 
static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
124804
 
  Rtree *pRtree = (Rtree *)pVtab;
124805
 
  int rc = SQLITE_NOMEM;
124806
 
  char *zSql = sqlite3_mprintf(
124807
 
    "ALTER TABLE %Q.'%q_node'   RENAME TO \"%w_node\";"
124808
 
    "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
124809
 
    "ALTER TABLE %Q.'%q_rowid'  RENAME TO \"%w_rowid\";"
124810
 
    , pRtree->zDb, pRtree->zName, zNewName 
124811
 
    , pRtree->zDb, pRtree->zName, zNewName 
124812
 
    , pRtree->zDb, pRtree->zName, zNewName
124813
 
  );
124814
 
  if( zSql ){
124815
 
    rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
124816
 
    sqlite3_free(zSql);
124817
 
  }
124818
 
  return rc;
124819
 
}
124820
 
 
124821
 
static sqlite3_module rtreeModule = {
124822
 
  0,                         /* iVersion */
124823
 
  rtreeCreate,                /* xCreate - create a table */
124824
 
  rtreeConnect,               /* xConnect - connect to an existing table */
124825
 
  rtreeBestIndex,             /* xBestIndex - Determine search strategy */
124826
 
  rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
124827
 
  rtreeDestroy,               /* xDestroy - Drop a table */
124828
 
  rtreeOpen,                  /* xOpen - open a cursor */
124829
 
  rtreeClose,                 /* xClose - close a cursor */
124830
 
  rtreeFilter,                /* xFilter - configure scan constraints */
124831
 
  rtreeNext,                  /* xNext - advance a cursor */
124832
 
  rtreeEof,                   /* xEof */
124833
 
  rtreeColumn,                /* xColumn - read data */
124834
 
  rtreeRowid,                 /* xRowid - read data */
124835
 
  rtreeUpdate,                /* xUpdate - write data */
124836
 
  0,                          /* xBegin - begin transaction */
124837
 
  0,                          /* xSync - sync transaction */
124838
 
  0,                          /* xCommit - commit transaction */
124839
 
  0,                          /* xRollback - rollback transaction */
124840
 
  0,                          /* xFindFunction - function overloading */
124841
 
  rtreeRename                 /* xRename - rename the table */
124842
 
};
124843
 
 
124844
 
static int rtreeSqlInit(
124845
 
  Rtree *pRtree, 
124846
 
  sqlite3 *db, 
124847
 
  const char *zDb, 
124848
 
  const char *zPrefix, 
124849
 
  int isCreate
124850
 
){
124851
 
  int rc = SQLITE_OK;
124852
 
 
124853
 
  #define N_STATEMENT 9
124854
 
  static const char *azSql[N_STATEMENT] = {
124855
 
    /* Read and write the xxx_node table */
124856
 
    "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
124857
 
    "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
124858
 
    "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
124859
 
 
124860
 
    /* Read and write the xxx_rowid table */
124861
 
    "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
124862
 
    "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
124863
 
    "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
124864
 
 
124865
 
    /* Read and write the xxx_parent table */
124866
 
    "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1",
124867
 
    "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)",
124868
 
    "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1"
124869
 
  };
124870
 
  sqlite3_stmt **appStmt[N_STATEMENT];
124871
 
  int i;
124872
 
 
124873
 
  pRtree->db = db;
124874
 
 
124875
 
  if( isCreate ){
124876
 
    char *zCreate = sqlite3_mprintf(
124877
 
"CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
124878
 
"CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
124879
 
"CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY, parentnode INTEGER);"
124880
 
"INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
124881
 
      zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
124882
 
    );
124883
 
    if( !zCreate ){
124884
 
      return SQLITE_NOMEM;
124885
 
    }
124886
 
    rc = sqlite3_exec(db, zCreate, 0, 0, 0);
124887
 
    sqlite3_free(zCreate);
124888
 
    if( rc!=SQLITE_OK ){
124889
 
      return rc;
124890
 
    }
124891
 
  }
124892
 
 
124893
 
  appStmt[0] = &pRtree->pReadNode;
124894
 
  appStmt[1] = &pRtree->pWriteNode;
124895
 
  appStmt[2] = &pRtree->pDeleteNode;
124896
 
  appStmt[3] = &pRtree->pReadRowid;
124897
 
  appStmt[4] = &pRtree->pWriteRowid;
124898
 
  appStmt[5] = &pRtree->pDeleteRowid;
124899
 
  appStmt[6] = &pRtree->pReadParent;
124900
 
  appStmt[7] = &pRtree->pWriteParent;
124901
 
  appStmt[8] = &pRtree->pDeleteParent;
124902
 
 
124903
 
  for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
124904
 
    char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
124905
 
    if( zSql ){
124906
 
      rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0); 
124907
 
    }else{
124908
 
      rc = SQLITE_NOMEM;
124909
 
    }
124910
 
    sqlite3_free(zSql);
124911
 
  }
124912
 
 
124913
 
  return rc;
124914
 
}
124915
 
 
124916
 
/*
124917
 
** The second argument to this function contains the text of an SQL statement
124918
 
** that returns a single integer value. The statement is compiled and executed
124919
 
** using database connection db. If successful, the integer value returned
124920
 
** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
124921
 
** code is returned and the value of *piVal after returning is not defined.
124922
 
*/
124923
 
static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
124924
 
  int rc = SQLITE_NOMEM;
124925
 
  if( zSql ){
124926
 
    sqlite3_stmt *pStmt = 0;
124927
 
    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
124928
 
    if( rc==SQLITE_OK ){
124929
 
      if( SQLITE_ROW==sqlite3_step(pStmt) ){
124930
 
        *piVal = sqlite3_column_int(pStmt, 0);
124931
 
      }
124932
 
      rc = sqlite3_finalize(pStmt);
124933
 
    }
124934
 
  }
124935
 
  return rc;
124936
 
}
124937
 
 
124938
 
/*
124939
 
** This function is called from within the xConnect() or xCreate() method to
124940
 
** determine the node-size used by the rtree table being created or connected
124941
 
** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
124942
 
** Otherwise, an SQLite error code is returned.
124943
 
**
124944
 
** If this function is being called as part of an xConnect(), then the rtree
124945
 
** table already exists. In this case the node-size is determined by inspecting
124946
 
** the root node of the tree.
124947
 
**
124948
 
** Otherwise, for an xCreate(), use 64 bytes less than the database page-size. 
124949
 
** This ensures that each node is stored on a single database page. If the 
124950
 
** database page-size is so large that more than RTREE_MAXCELLS entries 
124951
 
** would fit in a single node, use a smaller node-size.
124952
 
*/
124953
 
static int getNodeSize(
124954
 
  sqlite3 *db,                    /* Database handle */
124955
 
  Rtree *pRtree,                  /* Rtree handle */
124956
 
  int isCreate                    /* True for xCreate, false for xConnect */
124957
 
){
124958
 
  int rc;
124959
 
  char *zSql;
124960
 
  if( isCreate ){
124961
 
    int iPageSize;
124962
 
    zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
124963
 
    rc = getIntFromStmt(db, zSql, &iPageSize);
124964
 
    if( rc==SQLITE_OK ){
124965
 
      pRtree->iNodeSize = iPageSize-64;
124966
 
      if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
124967
 
        pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
124968
 
      }
124969
 
    }
124970
 
  }else{
124971
 
    zSql = sqlite3_mprintf(
124972
 
        "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
124973
 
        pRtree->zDb, pRtree->zName
124974
 
    );
124975
 
    rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
124976
 
  }
124977
 
 
124978
 
  sqlite3_free(zSql);
124979
 
  return rc;
124980
 
}
124981
 
 
124982
 
/* 
124983
 
** This function is the implementation of both the xConnect and xCreate
124984
 
** methods of the r-tree virtual table.
124985
 
**
124986
 
**   argv[0]   -> module name
124987
 
**   argv[1]   -> database name
124988
 
**   argv[2]   -> table name
124989
 
**   argv[...] -> column names...
124990
 
*/
124991
 
static int rtreeInit(
124992
 
  sqlite3 *db,                        /* Database connection */
124993
 
  void *pAux,                         /* One of the RTREE_COORD_* constants */
124994
 
  int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
124995
 
  sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
124996
 
  char **pzErr,                       /* OUT: Error message, if any */
124997
 
  int isCreate                        /* True for xCreate, false for xConnect */
124998
 
){
124999
 
  int rc = SQLITE_OK;
125000
 
  Rtree *pRtree;
125001
 
  int nDb;              /* Length of string argv[1] */
125002
 
  int nName;            /* Length of string argv[2] */
125003
 
  int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
125004
 
 
125005
 
  const char *aErrMsg[] = {
125006
 
    0,                                                    /* 0 */
125007
 
    "Wrong number of columns for an rtree table",         /* 1 */
125008
 
    "Too few columns for an rtree table",                 /* 2 */
125009
 
    "Too many columns for an rtree table"                 /* 3 */
125010
 
  };
125011
 
 
125012
 
  int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2;
125013
 
  if( aErrMsg[iErr] ){
125014
 
    *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
125015
 
    return SQLITE_ERROR;
125016
 
  }
125017
 
 
125018
 
  /* Allocate the sqlite3_vtab structure */
125019
 
  nDb = strlen(argv[1]);
125020
 
  nName = strlen(argv[2]);
125021
 
  pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
125022
 
  if( !pRtree ){
125023
 
    return SQLITE_NOMEM;
125024
 
  }
125025
 
  memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
125026
 
  pRtree->nBusy = 1;
125027
 
  pRtree->base.pModule = &rtreeModule;
125028
 
  pRtree->zDb = (char *)&pRtree[1];
125029
 
  pRtree->zName = &pRtree->zDb[nDb+1];
125030
 
  pRtree->nDim = (argc-4)/2;
125031
 
  pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
125032
 
  pRtree->eCoordType = eCoordType;
125033
 
  memcpy(pRtree->zDb, argv[1], nDb);
125034
 
  memcpy(pRtree->zName, argv[2], nName);
125035
 
 
125036
 
  /* Figure out the node size to use. */
125037
 
  rc = getNodeSize(db, pRtree, isCreate);
125038
 
 
125039
 
  /* Create/Connect to the underlying relational database schema. If
125040
 
  ** that is successful, call sqlite3_declare_vtab() to configure
125041
 
  ** the r-tree table schema.
125042
 
  */
125043
 
  if( rc==SQLITE_OK ){
125044
 
    if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){
125045
 
      *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
125046
 
    }else{
125047
 
      char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]);
125048
 
      char *zTmp;
125049
 
      int ii;
125050
 
      for(ii=4; zSql && ii<argc; ii++){
125051
 
        zTmp = zSql;
125052
 
        zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]);
125053
 
        sqlite3_free(zTmp);
125054
 
      }
125055
 
      if( zSql ){
125056
 
        zTmp = zSql;
125057
 
        zSql = sqlite3_mprintf("%s);", zTmp);
125058
 
        sqlite3_free(zTmp);
125059
 
      }
125060
 
      if( !zSql ){
125061
 
        rc = SQLITE_NOMEM;
125062
 
      }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
125063
 
        *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
125064
 
      }
125065
 
      sqlite3_free(zSql);
125066
 
    }
125067
 
  }
125068
 
 
125069
 
  if( rc==SQLITE_OK ){
125070
 
    *ppVtab = (sqlite3_vtab *)pRtree;
125071
 
  }else{
125072
 
    rtreeRelease(pRtree);
125073
 
  }
125074
 
  return rc;
125075
 
}
125076
 
 
125077
 
 
125078
 
/*
125079
 
** Implementation of a scalar function that decodes r-tree nodes to
125080
 
** human readable strings. This can be used for debugging and analysis.
125081
 
**
125082
 
** The scalar function takes two arguments, a blob of data containing
125083
 
** an r-tree node, and the number of dimensions the r-tree indexes.
125084
 
** For a two-dimensional r-tree structure called "rt", to deserialize
125085
 
** all nodes, a statement like:
125086
 
**
125087
 
**   SELECT rtreenode(2, data) FROM rt_node;
125088
 
**
125089
 
** The human readable string takes the form of a Tcl list with one
125090
 
** entry for each cell in the r-tree node. Each entry is itself a
125091
 
** list, containing the 8-byte rowid/pageno followed by the 
125092
 
** <num-dimension>*2 coordinates.
125093
 
*/
125094
 
static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
125095
 
  char *zText = 0;
125096
 
  RtreeNode node;
125097
 
  Rtree tree;
125098
 
  int ii;
125099
 
 
125100
 
  UNUSED_PARAMETER(nArg);
125101
 
  memset(&node, 0, sizeof(RtreeNode));
125102
 
  memset(&tree, 0, sizeof(Rtree));
125103
 
  tree.nDim = sqlite3_value_int(apArg[0]);
125104
 
  tree.nBytesPerCell = 8 + 8 * tree.nDim;
125105
 
  node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
125106
 
 
125107
 
  for(ii=0; ii<NCELL(&node); ii++){
125108
 
    char zCell[512];
125109
 
    int nCell = 0;
125110
 
    RtreeCell cell;
125111
 
    int jj;
125112
 
 
125113
 
    nodeGetCell(&tree, &node, ii, &cell);
125114
 
    sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
125115
 
    nCell = strlen(zCell);
125116
 
    for(jj=0; jj<tree.nDim*2; jj++){
125117
 
      sqlite3_snprintf(512-nCell,&zCell[nCell]," %f",(double)cell.aCoord[jj].f);
125118
 
      nCell = strlen(zCell);
125119
 
    }
125120
 
 
125121
 
    if( zText ){
125122
 
      char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
125123
 
      sqlite3_free(zText);
125124
 
      zText = zTextNew;
125125
 
    }else{
125126
 
      zText = sqlite3_mprintf("{%s}", zCell);
125127
 
    }
125128
 
  }
125129
 
  
125130
 
  sqlite3_result_text(ctx, zText, -1, sqlite3_free);
125131
 
}
125132
 
 
125133
 
static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
125134
 
  UNUSED_PARAMETER(nArg);
125135
 
  if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB 
125136
 
   || sqlite3_value_bytes(apArg[0])<2
125137
 
  ){
125138
 
    sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1); 
125139
 
  }else{
125140
 
    u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
125141
 
    sqlite3_result_int(ctx, readInt16(zBlob));
125142
 
  }
125143
 
}
125144
 
 
125145
 
/*
125146
 
** Register the r-tree module with database handle db. This creates the
125147
 
** virtual table module "rtree" and the debugging/analysis scalar 
125148
 
** function "rtreenode".
125149
 
*/
125150
 
SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
125151
 
  const int utf8 = SQLITE_UTF8;
125152
 
  int rc;
125153
 
 
125154
 
  rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
125155
 
  if( rc==SQLITE_OK ){
125156
 
    rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
125157
 
  }
125158
 
  if( rc==SQLITE_OK ){
125159
 
    void *c = (void *)RTREE_COORD_REAL32;
125160
 
    rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
125161
 
  }
125162
 
  if( rc==SQLITE_OK ){
125163
 
    void *c = (void *)RTREE_COORD_INT32;
125164
 
    rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
125165
 
  }
125166
 
 
125167
 
  return rc;
125168
 
}
125169
 
 
125170
 
/*
125171
 
** A version of sqlite3_free() that can be used as a callback. This is used
125172
 
** in two places - as the destructor for the blob value returned by the
125173
 
** invocation of a geometry function, and as the destructor for the geometry
125174
 
** functions themselves.
125175
 
*/
125176
 
static void doSqlite3Free(void *p){
125177
 
  sqlite3_free(p);
125178
 
}
125179
 
 
125180
 
/*
125181
 
** Each call to sqlite3_rtree_geometry_callback() creates an ordinary SQLite
125182
 
** scalar user function. This C function is the callback used for all such
125183
 
** registered SQL functions.
125184
 
**
125185
 
** The scalar user functions return a blob that is interpreted by r-tree
125186
 
** table MATCH operators.
125187
 
*/
125188
 
static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
125189
 
  RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
125190
 
  RtreeMatchArg *pBlob;
125191
 
  int nBlob;
125192
 
 
125193
 
  nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(double);
125194
 
  pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
125195
 
  if( !pBlob ){
125196
 
    sqlite3_result_error_nomem(ctx);
125197
 
  }else{
125198
 
    int i;
125199
 
    pBlob->magic = RTREE_GEOMETRY_MAGIC;
125200
 
    pBlob->xGeom = pGeomCtx->xGeom;
125201
 
    pBlob->pContext = pGeomCtx->pContext;
125202
 
    pBlob->nParam = nArg;
125203
 
    for(i=0; i<nArg; i++){
125204
 
      pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
125205
 
    }
125206
 
    sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
125207
 
  }
125208
 
}
125209
 
 
125210
 
/*
125211
 
** Register a new geometry function for use with the r-tree MATCH operator.
125212
 
*/
125213
 
SQLITE_API int sqlite3_rtree_geometry_callback(
125214
 
  sqlite3 *db,
125215
 
  const char *zGeom,
125216
 
  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *),
125217
 
  void *pContext
125218
 
){
125219
 
  RtreeGeomCallback *pGeomCtx;      /* Context object for new user-function */
125220
 
 
125221
 
  /* Allocate and populate the context object. */
125222
 
  pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
125223
 
  if( !pGeomCtx ) return SQLITE_NOMEM;
125224
 
  pGeomCtx->xGeom = xGeom;
125225
 
  pGeomCtx->pContext = pContext;
125226
 
 
125227
 
  /* Create the new user-function. Register a destructor function to delete
125228
 
  ** the context object when it is no longer required.  */
125229
 
  return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY, 
125230
 
      (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free
125231
 
  );
125232
 
}
125233
 
 
125234
 
#if !SQLITE_CORE
125235
 
SQLITE_API int sqlite3_extension_init(
125236
 
  sqlite3 *db,
125237
 
  char **pzErrMsg,
125238
 
  const sqlite3_api_routines *pApi
125239
 
){
125240
 
  SQLITE_EXTENSION_INIT2(pApi)
125241
 
  return sqlite3RtreeInit(db);
125242
 
}
125243
 
#endif
125244
 
 
125245
 
#endif
125246
 
 
125247
 
/************** End of rtree.c ***********************************************/
125248
 
/************** Begin file icu.c *********************************************/
125249
 
/*
125250
 
** 2007 May 6
125251
 
**
125252
 
** The author disclaims copyright to this source code.  In place of
125253
 
** a legal notice, here is a blessing:
125254
 
**
125255
 
**    May you do good and not evil.
125256
 
**    May you find forgiveness for yourself and forgive others.
125257
 
**    May you share freely, never taking more than you give.
125258
 
**
125259
 
*************************************************************************
125260
 
** $Id: sqlite3.c $
125261
 
**
125262
 
** This file implements an integration between the ICU library 
125263
 
** ("International Components for Unicode", an open-source library 
125264
 
** for handling unicode data) and SQLite. The integration uses 
125265
 
** ICU to provide the following to SQLite:
125266
 
**
125267
 
**   * An implementation of the SQL regexp() function (and hence REGEXP
125268
 
**     operator) using the ICU uregex_XX() APIs.
125269
 
**
125270
 
**   * Implementations of the SQL scalar upper() and lower() functions
125271
 
**     for case mapping.
125272
 
**
125273
 
**   * Integration of ICU and SQLite collation seqences.
125274
 
**
125275
 
**   * An implementation of the LIKE operator that uses ICU to 
125276
 
**     provide case-independent matching.
125277
 
*/
125278
 
 
125279
 
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
125280
 
 
125281
 
/* Include ICU headers */
125282
 
#include <unicode/utypes.h>
125283
 
#include <unicode/uregex.h>
125284
 
#include <unicode/ustring.h>
125285
 
#include <unicode/ucol.h>
125286
 
 
125287
 
 
125288
 
#ifndef SQLITE_CORE
125289
 
  SQLITE_EXTENSION_INIT1
125290
 
#else
125291
 
#endif
125292
 
 
125293
 
/*
125294
 
** Maximum length (in bytes) of the pattern in a LIKE or GLOB
125295
 
** operator.
125296
 
*/
125297
 
#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
125298
 
# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
125299
 
#endif
125300
 
 
125301
 
/*
125302
 
** Version of sqlite3_free() that is always a function, never a macro.
125303
 
*/
125304
 
static void xFree(void *p){
125305
 
  sqlite3_free(p);
125306
 
}
125307
 
 
125308
 
/*
125309
 
** Compare two UTF-8 strings for equality where the first string is
125310
 
** a "LIKE" expression. Return true (1) if they are the same and 
125311
 
** false (0) if they are different.
125312
 
*/
125313
 
static int icuLikeCompare(
125314
 
  const uint8_t *zPattern,   /* LIKE pattern */
125315
 
  const uint8_t *zString,    /* The UTF-8 string to compare against */
125316
 
  const UChar32 uEsc         /* The escape character */
125317
 
){
125318
 
  static const int MATCH_ONE = (UChar32)'_';
125319
 
  static const int MATCH_ALL = (UChar32)'%';
125320
 
 
125321
 
  int iPattern = 0;       /* Current byte index in zPattern */
125322
 
  int iString = 0;        /* Current byte index in zString */
125323
 
 
125324
 
  int prevEscape = 0;     /* True if the previous character was uEsc */
125325
 
 
125326
 
  while( zPattern[iPattern]!=0 ){
125327
 
 
125328
 
    /* Read (and consume) the next character from the input pattern. */
125329
 
    UChar32 uPattern;
125330
 
    U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
125331
 
    assert(uPattern!=0);
125332
 
 
125333
 
    /* There are now 4 possibilities:
125334
 
    **
125335
 
    **     1. uPattern is an unescaped match-all character "%",
125336
 
    **     2. uPattern is an unescaped match-one character "_",
125337
 
    **     3. uPattern is an unescaped escape character, or
125338
 
    **     4. uPattern is to be handled as an ordinary character
125339
 
    */
125340
 
    if( !prevEscape && uPattern==MATCH_ALL ){
125341
 
      /* Case 1. */
125342
 
      uint8_t c;
125343
 
 
125344
 
      /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
125345
 
      ** MATCH_ALL. For each MATCH_ONE, skip one character in the 
125346
 
      ** test string.
125347
 
      */
125348
 
      while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){
125349
 
        if( c==MATCH_ONE ){
125350
 
          if( zString[iString]==0 ) return 0;
125351
 
          U8_FWD_1_UNSAFE(zString, iString);
125352
 
        }
125353
 
        iPattern++;
125354
 
      }
125355
 
 
125356
 
      if( zPattern[iPattern]==0 ) return 1;
125357
 
 
125358
 
      while( zString[iString] ){
125359
 
        if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){
125360
 
          return 1;
125361
 
        }
125362
 
        U8_FWD_1_UNSAFE(zString, iString);
125363
 
      }
125364
 
      return 0;
125365
 
 
125366
 
    }else if( !prevEscape && uPattern==MATCH_ONE ){
125367
 
      /* Case 2. */
125368
 
      if( zString[iString]==0 ) return 0;
125369
 
      U8_FWD_1_UNSAFE(zString, iString);
125370
 
 
125371
 
    }else if( !prevEscape && uPattern==uEsc){
125372
 
      /* Case 3. */
125373
 
      prevEscape = 1;
125374
 
 
125375
 
    }else{
125376
 
      /* Case 4. */
125377
 
      UChar32 uString;
125378
 
      U8_NEXT_UNSAFE(zString, iString, uString);
125379
 
      uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
125380
 
      uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
125381
 
      if( uString!=uPattern ){
125382
 
        return 0;
125383
 
      }
125384
 
      prevEscape = 0;
125385
 
    }
125386
 
  }
125387
 
 
125388
 
  return zString[iString]==0;
125389
 
}
125390
 
 
125391
 
/*
125392
 
** Implementation of the like() SQL function.  This function implements
125393
 
** the build-in LIKE operator.  The first argument to the function is the
125394
 
** pattern and the second argument is the string.  So, the SQL statements:
125395
 
**
125396
 
**       A LIKE B
125397
 
**
125398
 
** is implemented as like(B, A). If there is an escape character E, 
125399
 
**
125400
 
**       A LIKE B ESCAPE E
125401
 
**
125402
 
** is mapped to like(B, A, E).
125403
 
*/
125404
 
static void icuLikeFunc(
125405
 
  sqlite3_context *context, 
125406
 
  int argc, 
125407
 
  sqlite3_value **argv
125408
 
){
125409
 
  const unsigned char *zA = sqlite3_value_text(argv[0]);
125410
 
  const unsigned char *zB = sqlite3_value_text(argv[1]);
125411
 
  UChar32 uEsc = 0;
125412
 
 
125413
 
  /* Limit the length of the LIKE or GLOB pattern to avoid problems
125414
 
  ** of deep recursion and N*N behavior in patternCompare().
125415
 
  */
125416
 
  if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
125417
 
    sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
125418
 
    return;
125419
 
  }
125420
 
 
125421
 
 
125422
 
  if( argc==3 ){
125423
 
    /* The escape character string must consist of a single UTF-8 character.
125424
 
    ** Otherwise, return an error.
125425
 
    */
125426
 
    int nE= sqlite3_value_bytes(argv[2]);
125427
 
    const unsigned char *zE = sqlite3_value_text(argv[2]);
125428
 
    int i = 0;
125429
 
    if( zE==0 ) return;
125430
 
    U8_NEXT(zE, i, nE, uEsc);
125431
 
    if( i!=nE){
125432
 
      sqlite3_result_error(context, 
125433
 
          "ESCAPE expression must be a single character", -1);
125434
 
      return;
125435
 
    }
125436
 
  }
125437
 
 
125438
 
  if( zA && zB ){
125439
 
    sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
125440
 
  }
125441
 
}
125442
 
 
125443
 
/*
125444
 
** This function is called when an ICU function called from within
125445
 
** the implementation of an SQL scalar function returns an error.
125446
 
**
125447
 
** The scalar function context passed as the first argument is 
125448
 
** loaded with an error message based on the following two args.
125449
 
*/
125450
 
static void icuFunctionError(
125451
 
  sqlite3_context *pCtx,       /* SQLite scalar function context */
125452
 
  const char *zName,           /* Name of ICU function that failed */
125453
 
  UErrorCode e                 /* Error code returned by ICU function */
125454
 
){
125455
 
  char zBuf[128];
125456
 
  sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
125457
 
  zBuf[127] = '\0';
125458
 
  sqlite3_result_error(pCtx, zBuf, -1);
125459
 
}
125460
 
 
125461
 
/*
125462
 
** Function to delete compiled regexp objects. Registered as
125463
 
** a destructor function with sqlite3_set_auxdata().
125464
 
*/
125465
 
static void icuRegexpDelete(void *p){
125466
 
  URegularExpression *pExpr = (URegularExpression *)p;
125467
 
  uregex_close(pExpr);
125468
 
}
125469
 
 
125470
 
/*
125471
 
** Implementation of SQLite REGEXP operator. This scalar function takes
125472
 
** two arguments. The first is a regular expression pattern to compile
125473
 
** the second is a string to match against that pattern. If either 
125474
 
** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
125475
 
** is 1 if the string matches the pattern, or 0 otherwise.
125476
 
**
125477
 
** SQLite maps the regexp() function to the regexp() operator such
125478
 
** that the following two are equivalent:
125479
 
**
125480
 
**     zString REGEXP zPattern
125481
 
**     regexp(zPattern, zString)
125482
 
**
125483
 
** Uses the following ICU regexp APIs:
125484
 
**
125485
 
**     uregex_open()
125486
 
**     uregex_matches()
125487
 
**     uregex_close()
125488
 
*/
125489
 
static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
125490
 
  UErrorCode status = U_ZERO_ERROR;
125491
 
  URegularExpression *pExpr;
125492
 
  UBool res;
125493
 
  const UChar *zString = sqlite3_value_text16(apArg[1]);
125494
 
 
125495
 
  (void)nArg;  /* Unused parameter */
125496
 
 
125497
 
  /* If the left hand side of the regexp operator is NULL, 
125498
 
  ** then the result is also NULL. 
125499
 
  */
125500
 
  if( !zString ){
125501
 
    return;
125502
 
  }
125503
 
 
125504
 
  pExpr = sqlite3_get_auxdata(p, 0);
125505
 
  if( !pExpr ){
125506
 
    const UChar *zPattern = sqlite3_value_text16(apArg[0]);
125507
 
    if( !zPattern ){
125508
 
      return;
125509
 
    }
125510
 
    pExpr = uregex_open(zPattern, -1, 0, 0, &status);
125511
 
 
125512
 
    if( U_SUCCESS(status) ){
125513
 
      sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
125514
 
    }else{
125515
 
      assert(!pExpr);
125516
 
      icuFunctionError(p, "uregex_open", status);
125517
 
      return;
125518
 
    }
125519
 
  }
125520
 
 
125521
 
  /* Configure the text that the regular expression operates on. */
125522
 
  uregex_setText(pExpr, zString, -1, &status);
125523
 
  if( !U_SUCCESS(status) ){
125524
 
    icuFunctionError(p, "uregex_setText", status);
125525
 
    return;
125526
 
  }
125527
 
 
125528
 
  /* Attempt the match */
125529
 
  res = uregex_matches(pExpr, 0, &status);
125530
 
  if( !U_SUCCESS(status) ){
125531
 
    icuFunctionError(p, "uregex_matches", status);
125532
 
    return;
125533
 
  }
125534
 
 
125535
 
  /* Set the text that the regular expression operates on to a NULL
125536
 
  ** pointer. This is not really necessary, but it is tidier than 
125537
 
  ** leaving the regular expression object configured with an invalid
125538
 
  ** pointer after this function returns.
125539
 
  */
125540
 
  uregex_setText(pExpr, 0, 0, &status);
125541
 
 
125542
 
  /* Return 1 or 0. */
125543
 
  sqlite3_result_int(p, res ? 1 : 0);
125544
 
}
125545
 
 
125546
 
/*
125547
 
** Implementations of scalar functions for case mapping - upper() and 
125548
 
** lower(). Function upper() converts its input to upper-case (ABC).
125549
 
** Function lower() converts to lower-case (abc).
125550
 
**
125551
 
** ICU provides two types of case mapping, "general" case mapping and
125552
 
** "language specific". Refer to ICU documentation for the differences
125553
 
** between the two.
125554
 
**
125555
 
** To utilise "general" case mapping, the upper() or lower() scalar 
125556
 
** functions are invoked with one argument:
125557
 
**
125558
 
**     upper('ABC') -> 'abc'
125559
 
**     lower('abc') -> 'ABC'
125560
 
**
125561
 
** To access ICU "language specific" case mapping, upper() or lower()
125562
 
** should be invoked with two arguments. The second argument is the name
125563
 
** of the locale to use. Passing an empty string ("") or SQL NULL value
125564
 
** as the second argument is the same as invoking the 1 argument version
125565
 
** of upper() or lower().
125566
 
**
125567
 
**     lower('I', 'en_us') -> 'i'
125568
 
**     lower('I', 'tr_tr') -> 'ı' (small dotless i)
125569
 
**
125570
 
** http://www.icu-project.org/userguide/posix.html#case_mappings
125571
 
*/
125572
 
static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
125573
 
  const UChar *zInput;
125574
 
  UChar *zOutput;
125575
 
  int nInput;
125576
 
  int nOutput;
125577
 
 
125578
 
  UErrorCode status = U_ZERO_ERROR;
125579
 
  const char *zLocale = 0;
125580
 
 
125581
 
  assert(nArg==1 || nArg==2);
125582
 
  if( nArg==2 ){
125583
 
    zLocale = (const char *)sqlite3_value_text(apArg[1]);
125584
 
  }
125585
 
 
125586
 
  zInput = sqlite3_value_text16(apArg[0]);
125587
 
  if( !zInput ){
125588
 
    return;
125589
 
  }
125590
 
  nInput = sqlite3_value_bytes16(apArg[0]);
125591
 
 
125592
 
  nOutput = nInput * 2 + 2;
125593
 
  zOutput = sqlite3_malloc(nOutput);
125594
 
  if( !zOutput ){
125595
 
    return;
125596
 
  }
125597
 
 
125598
 
  if( sqlite3_user_data(p) ){
125599
 
    u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
125600
 
  }else{
125601
 
    u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
125602
 
  }
125603
 
 
125604
 
  if( !U_SUCCESS(status) ){
125605
 
    icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
125606
 
    return;
125607
 
  }
125608
 
 
125609
 
  sqlite3_result_text16(p, zOutput, -1, xFree);
125610
 
}
125611
 
 
125612
 
/*
125613
 
** Collation sequence destructor function. The pCtx argument points to
125614
 
** a UCollator structure previously allocated using ucol_open().
125615
 
*/
125616
 
static void icuCollationDel(void *pCtx){
125617
 
  UCollator *p = (UCollator *)pCtx;
125618
 
  ucol_close(p);
125619
 
}
125620
 
 
125621
 
/*
125622
 
** Collation sequence comparison function. The pCtx argument points to
125623
 
** a UCollator structure previously allocated using ucol_open().
125624
 
*/
125625
 
static int icuCollationColl(
125626
 
  void *pCtx,
125627
 
  int nLeft,
125628
 
  const void *zLeft,
125629
 
  int nRight,
125630
 
  const void *zRight
125631
 
){
125632
 
  UCollationResult res;
125633
 
  UCollator *p = (UCollator *)pCtx;
125634
 
  res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
125635
 
  switch( res ){
125636
 
    case UCOL_LESS:    return -1;
125637
 
    case UCOL_GREATER: return +1;
125638
 
    case UCOL_EQUAL:   return 0;
125639
 
  }
125640
 
  assert(!"Unexpected return value from ucol_strcoll()");
125641
 
  return 0;
125642
 
}
125643
 
 
125644
 
/*
125645
 
** Implementation of the scalar function icu_load_collation().
125646
 
**
125647
 
** This scalar function is used to add ICU collation based collation 
125648
 
** types to an SQLite database connection. It is intended to be called
125649
 
** as follows:
125650
 
**
125651
 
**     SELECT icu_load_collation(<locale>, <collation-name>);
125652
 
**
125653
 
** Where <locale> is a string containing an ICU locale identifier (i.e.
125654
 
** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
125655
 
** collation sequence to create.
125656
 
*/
125657
 
static void icuLoadCollation(
125658
 
  sqlite3_context *p, 
125659
 
  int nArg, 
125660
 
  sqlite3_value **apArg
125661
 
){
125662
 
  sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
125663
 
  UErrorCode status = U_ZERO_ERROR;
125664
 
  const char *zLocale;      /* Locale identifier - (eg. "jp_JP") */
125665
 
  const char *zName;        /* SQL Collation sequence name (eg. "japanese") */
125666
 
  UCollator *pUCollator;    /* ICU library collation object */
125667
 
  int rc;                   /* Return code from sqlite3_create_collation_x() */
125668
 
 
125669
 
  assert(nArg==2);
125670
 
  zLocale = (const char *)sqlite3_value_text(apArg[0]);
125671
 
  zName = (const char *)sqlite3_value_text(apArg[1]);
125672
 
 
125673
 
  if( !zLocale || !zName ){
125674
 
    return;
125675
 
  }
125676
 
 
125677
 
  pUCollator = ucol_open(zLocale, &status);
125678
 
  if( !U_SUCCESS(status) ){
125679
 
    icuFunctionError(p, "ucol_open", status);
125680
 
    return;
125681
 
  }
125682
 
  assert(p);
125683
 
 
125684
 
  rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator, 
125685
 
      icuCollationColl, icuCollationDel
125686
 
  );
125687
 
  if( rc!=SQLITE_OK ){
125688
 
    ucol_close(pUCollator);
125689
 
    sqlite3_result_error(p, "Error registering collation function", -1);
125690
 
  }
125691
 
}
125692
 
 
125693
 
/*
125694
 
** Register the ICU extension functions with database db.
125695
 
*/
125696
 
SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
125697
 
  struct IcuScalar {
125698
 
    const char *zName;                        /* Function name */
125699
 
    int nArg;                                 /* Number of arguments */
125700
 
    int enc;                                  /* Optimal text encoding */
125701
 
    void *pContext;                           /* sqlite3_user_data() context */
125702
 
    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
125703
 
  } scalars[] = {
125704
 
    {"regexp", 2, SQLITE_ANY,          0, icuRegexpFunc},
125705
 
 
125706
 
    {"lower",  1, SQLITE_UTF16,        0, icuCaseFunc16},
125707
 
    {"lower",  2, SQLITE_UTF16,        0, icuCaseFunc16},
125708
 
    {"upper",  1, SQLITE_UTF16, (void*)1, icuCaseFunc16},
125709
 
    {"upper",  2, SQLITE_UTF16, (void*)1, icuCaseFunc16},
125710
 
 
125711
 
    {"lower",  1, SQLITE_UTF8,         0, icuCaseFunc16},
125712
 
    {"lower",  2, SQLITE_UTF8,         0, icuCaseFunc16},
125713
 
    {"upper",  1, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
125714
 
    {"upper",  2, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
125715
 
 
125716
 
    {"like",   2, SQLITE_UTF8,         0, icuLikeFunc},
125717
 
    {"like",   3, SQLITE_UTF8,         0, icuLikeFunc},
125718
 
 
125719
 
    {"icu_load_collation",  2, SQLITE_UTF8, (void*)db, icuLoadCollation},
125720
 
  };
125721
 
 
125722
 
  int rc = SQLITE_OK;
125723
 
  int i;
125724
 
 
125725
 
  for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
125726
 
    struct IcuScalar *p = &scalars[i];
125727
 
    rc = sqlite3_create_function(
125728
 
        db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
125729
 
    );
125730
 
  }
125731
 
 
125732
 
  return rc;
125733
 
}
125734
 
 
125735
 
#if !SQLITE_CORE
125736
 
SQLITE_API int sqlite3_extension_init(
125737
 
  sqlite3 *db, 
125738
 
  char **pzErrMsg,
125739
 
  const sqlite3_api_routines *pApi
125740
 
){
125741
 
  SQLITE_EXTENSION_INIT2(pApi)
125742
 
  return sqlite3IcuInit(db);
125743
 
}
125744
 
#endif
125745
 
 
125746
 
#endif
125747
 
 
125748
 
/************** End of icu.c *************************************************/
125749
 
/************** Begin file fts3_icu.c ****************************************/
125750
 
/*
125751
 
** 2007 June 22
125752
 
**
125753
 
** The author disclaims copyright to this source code.  In place of
125754
 
** a legal notice, here is a blessing:
125755
 
**
125756
 
**    May you do good and not evil.
125757
 
**    May you find forgiveness for yourself and forgive others.
125758
 
**    May you share freely, never taking more than you give.
125759
 
**
125760
 
*************************************************************************
125761
 
** This file implements a tokenizer for fts3 based on the ICU library.
125762
 
** 
125763
 
** $Id: sqlite3.c $
125764
 
*/
125765
 
 
125766
 
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
125767
 
#ifdef SQLITE_ENABLE_ICU
125768
 
 
125769
 
 
125770
 
#include <unicode/ubrk.h>
125771
 
#include <unicode/utf16.h>
125772
 
 
125773
 
typedef struct IcuTokenizer IcuTokenizer;
125774
 
typedef struct IcuCursor IcuCursor;
125775
 
 
125776
 
struct IcuTokenizer {
125777
 
  sqlite3_tokenizer base;
125778
 
  char *zLocale;
125779
 
};
125780
 
 
125781
 
struct IcuCursor {
125782
 
  sqlite3_tokenizer_cursor base;
125783
 
 
125784
 
  UBreakIterator *pIter;      /* ICU break-iterator object */
125785
 
  int nChar;                  /* Number of UChar elements in pInput */
125786
 
  UChar *aChar;               /* Copy of input using utf-16 encoding */
125787
 
  int *aOffset;               /* Offsets of each character in utf-8 input */
125788
 
 
125789
 
  int nBuffer;
125790
 
  char *zBuffer;
125791
 
 
125792
 
  int iToken;
125793
 
};
125794
 
 
125795
 
/*
125796
 
** Create a new tokenizer instance.
125797
 
*/
125798
 
static int icuCreate(
125799
 
  int argc,                            /* Number of entries in argv[] */
125800
 
  const char * const *argv,            /* Tokenizer creation arguments */
125801
 
  sqlite3_tokenizer **ppTokenizer      /* OUT: Created tokenizer */
125802
 
){
125803
 
  IcuTokenizer *p;
125804
 
  int n = 0;
125805
 
 
125806
 
  if( argc>0 ){
125807
 
    n = strlen(argv[0])+1;
125808
 
  }
125809
 
  p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n);
125810
 
  if( !p ){
125811
 
    return SQLITE_NOMEM;
125812
 
  }
125813
 
  memset(p, 0, sizeof(IcuTokenizer));
125814
 
 
125815
 
  if( n ){
125816
 
    p->zLocale = (char *)&p[1];
125817
 
    memcpy(p->zLocale, argv[0], n);
125818
 
  }
125819
 
 
125820
 
  *ppTokenizer = (sqlite3_tokenizer *)p;
125821
 
 
125822
 
  return SQLITE_OK;
125823
 
}
125824
 
 
125825
 
/*
125826
 
** Destroy a tokenizer
125827
 
*/
125828
 
static int icuDestroy(sqlite3_tokenizer *pTokenizer){
125829
 
  IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
125830
 
  sqlite3_free(p);
125831
 
  return SQLITE_OK;
125832
 
}
125833
 
 
125834
 
/*
125835
 
** Prepare to begin tokenizing a particular string.  The input
125836
 
** string to be tokenized is pInput[0..nBytes-1].  A cursor
125837
 
** used to incrementally tokenize this string is returned in 
125838
 
** *ppCursor.
125839
 
*/
125840
 
static int icuOpen(
125841
 
  sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
125842
 
  const char *zInput,                    /* Input string */
125843
 
  int nInput,                            /* Length of zInput in bytes */
125844
 
  sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
125845
 
){
125846
 
  IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
125847
 
  IcuCursor *pCsr;
125848
 
 
125849
 
  const int32_t opt = U_FOLD_CASE_DEFAULT;
125850
 
  UErrorCode status = U_ZERO_ERROR;
125851
 
  int nChar;
125852
 
 
125853
 
  UChar32 c;
125854
 
  int iInput = 0;
125855
 
  int iOut = 0;
125856
 
 
125857
 
  *ppCursor = 0;
125858
 
 
125859
 
  if( nInput<0 ){
125860
 
    nInput = strlen(zInput);
125861
 
  }
125862
 
  nChar = nInput+1;
125863
 
  pCsr = (IcuCursor *)sqlite3_malloc(
125864
 
      sizeof(IcuCursor) +                /* IcuCursor */
125865
 
      nChar * sizeof(UChar) +            /* IcuCursor.aChar[] */
125866
 
      (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */
125867
 
  );
125868
 
  if( !pCsr ){
125869
 
    return SQLITE_NOMEM;
125870
 
  }
125871
 
  memset(pCsr, 0, sizeof(IcuCursor));
125872
 
  pCsr->aChar = (UChar *)&pCsr[1];
125873
 
  pCsr->aOffset = (int *)&pCsr->aChar[nChar];
125874
 
 
125875
 
  pCsr->aOffset[iOut] = iInput;
125876
 
  U8_NEXT(zInput, iInput, nInput, c); 
125877
 
  while( c>0 ){
125878
 
    int isError = 0;
125879
 
    c = u_foldCase(c, opt);
125880
 
    U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
125881
 
    if( isError ){
125882
 
      sqlite3_free(pCsr);
125883
 
      return SQLITE_ERROR;
125884
 
    }
125885
 
    pCsr->aOffset[iOut] = iInput;
125886
 
 
125887
 
    if( iInput<nInput ){
125888
 
      U8_NEXT(zInput, iInput, nInput, c);
125889
 
    }else{
125890
 
      c = 0;
125891
 
    }
125892
 
  }
125893
 
 
125894
 
  pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
125895
 
  if( !U_SUCCESS(status) ){
125896
 
    sqlite3_free(pCsr);
125897
 
    return SQLITE_ERROR;
125898
 
  }
125899
 
  pCsr->nChar = iOut;
125900
 
 
125901
 
  ubrk_first(pCsr->pIter);
125902
 
  *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
125903
 
  return SQLITE_OK;
125904
 
}
125905
 
 
125906
 
/*
125907
 
** Close a tokenization cursor previously opened by a call to icuOpen().
125908
 
*/
125909
 
static int icuClose(sqlite3_tokenizer_cursor *pCursor){
125910
 
  IcuCursor *pCsr = (IcuCursor *)pCursor;
125911
 
  ubrk_close(pCsr->pIter);
125912
 
  sqlite3_free(pCsr->zBuffer);
125913
 
  sqlite3_free(pCsr);
125914
 
  return SQLITE_OK;
125915
 
}
125916
 
 
125917
 
/*
125918
 
** Extract the next token from a tokenization cursor.
125919
 
*/
125920
 
static int icuNext(
125921
 
  sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
125922
 
  const char **ppToken,               /* OUT: *ppToken is the token text */
125923
 
  int *pnBytes,                       /* OUT: Number of bytes in token */
125924
 
  int *piStartOffset,                 /* OUT: Starting offset of token */
125925
 
  int *piEndOffset,                   /* OUT: Ending offset of token */
125926
 
  int *piPosition                     /* OUT: Position integer of token */
125927
 
){
125928
 
  IcuCursor *pCsr = (IcuCursor *)pCursor;
125929
 
 
125930
 
  int iStart = 0;
125931
 
  int iEnd = 0;
125932
 
  int nByte = 0;
125933
 
 
125934
 
  while( iStart==iEnd ){
125935
 
    UChar32 c;
125936
 
 
125937
 
    iStart = ubrk_current(pCsr->pIter);
125938
 
    iEnd = ubrk_next(pCsr->pIter);
125939
 
    if( iEnd==UBRK_DONE ){
125940
 
      return SQLITE_DONE;
125941
 
    }
125942
 
 
125943
 
    while( iStart<iEnd ){
125944
 
      int iWhite = iStart;
125945
 
      U8_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
125946
 
      if( u_isspace(c) ){
125947
 
        iStart = iWhite;
125948
 
      }else{
125949
 
        break;
125950
 
      }
125951
 
    }
125952
 
    assert(iStart<=iEnd);
125953
 
  }
125954
 
 
125955
 
  do {
125956
 
    UErrorCode status = U_ZERO_ERROR;
125957
 
    if( nByte ){
125958
 
      char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
125959
 
      if( !zNew ){
125960
 
        return SQLITE_NOMEM;
125961
 
      }
125962
 
      pCsr->zBuffer = zNew;
125963
 
      pCsr->nBuffer = nByte;
125964
 
    }
125965
 
 
125966
 
    u_strToUTF8(
125967
 
        pCsr->zBuffer, pCsr->nBuffer, &nByte,    /* Output vars */
125968
 
        &pCsr->aChar[iStart], iEnd-iStart,       /* Input vars */
125969
 
        &status                                  /* Output success/failure */
125970
 
    );
125971
 
  } while( nByte>pCsr->nBuffer );
125972
 
 
125973
 
  *ppToken = pCsr->zBuffer;
125974
 
  *pnBytes = nByte;
125975
 
  *piStartOffset = pCsr->aOffset[iStart];
125976
 
  *piEndOffset = pCsr->aOffset[iEnd];
125977
 
  *piPosition = pCsr->iToken++;
125978
 
 
125979
 
  return SQLITE_OK;
125980
 
}
125981
 
 
125982
 
/*
125983
 
** The set of routines that implement the simple tokenizer
125984
 
*/
125985
 
static const sqlite3_tokenizer_module icuTokenizerModule = {
125986
 
  0,                           /* iVersion */
125987
 
  icuCreate,                   /* xCreate  */
125988
 
  icuDestroy,                  /* xCreate  */
125989
 
  icuOpen,                     /* xOpen    */
125990
 
  icuClose,                    /* xClose   */
125991
 
  icuNext,                     /* xNext    */
125992
 
};
125993
 
 
125994
 
/*
125995
 
** Set *ppModule to point at the implementation of the ICU tokenizer.
125996
 
*/
125997
 
SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
125998
 
  sqlite3_tokenizer_module const**ppModule
125999
 
){
126000
 
  *ppModule = &icuTokenizerModule;
126001
 
}
126002
 
 
126003
 
#endif /* defined(SQLITE_ENABLE_ICU) */
126004
 
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
126005
 
 
126006
 
/************** End of fts3_icu.c ********************************************/